summaryrefslogtreecommitdiffstats
path: root/libmemunreachable/tests/Binder_test.cpp
blob: 6e85d5ab33903b7e98a2a32aedec8f1a714d5d61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binder/IServiceManager.h>
#include <binder/Parcel.h>
#include <binder/ProcessState.h>

#include <gtest/gtest.h>

#include "Allocator.h"
#include "Binder.h"

namespace android {

static const String16 service_name("test.libmemunreachable_binder");

class BinderService : public BBinder {
 public:
  BinderService() = default;
  virtual ~BinderService() = default;

  virtual status_t onTransact(uint32_t /*code*/, const Parcel& data, Parcel* reply,
                              uint32_t /*flags*/ = 0) {
    reply->writeStrongBinder(ref);
    ref = data.readStrongBinder();
    return 0;
  }

 private:
  sp<IBinder> ref;
};

class BinderObject : public BBinder {
 public:
  BinderObject() = default;
  ~BinderObject() = default;
};

class ServiceProcess {
 public:
  ServiceProcess() : child_(0) {}
  ~ServiceProcess() { Stop(); }

  bool Run() {
    pid_t ret = fork();
    if (ret < 0) {
      return false;
    } else if (ret == 0) {
      // child
      _exit(Service());
    } else {
      // parent
      child_ = ret;
      return true;
    }
  }

  bool Stop() {
    if (child_ > 0) {
      if (kill(child_, SIGTERM)) {
        return false;
      }
      int status = 0;
      if (TEMP_FAILURE_RETRY(waitpid(child_, &status, 0)) != child_) {
        return false;
      }
      child_ = 0;
      return WIFEXITED(status) && WEXITSTATUS(status) == 0;
    }

    return true;
  }

  int Service() {
    sp<ProcessState> proc{ProcessState::self()};
    sp<IServiceManager> sm = defaultServiceManager();
    if (sm == nullptr) {
      fprintf(stderr, "Failed to get service manager\n");
      return 1;
    }
    if (sm->addService(service_name, new BinderService()) != OK) {
      fprintf(stderr, "Failed to add test service\n");
      return 1;
    }
    proc->startThreadPool();
    pause();
    return 0;
  }

 private:
  pid_t child_;
};

class BinderTest : public ::testing::Test {
 protected:
  ServiceProcess service_process_;
};

TEST_F(BinderTest, binder) {
  ServiceProcess service_process;
  ASSERT_TRUE(service_process.Run());

  sp<IServiceManager> sm = defaultServiceManager();
  ASSERT_TRUE(sm != nullptr);

  // A small sleep allows the service to start, which
  // prevents a longer sleep in getService.
  usleep(100000);

  sp<IBinder> service = sm->getService(service_name);
  ASSERT_TRUE(service != nullptr);

  sp<IBinder> binder{new BinderObject()};

  Parcel send;
  Parcel reply;

  send.writeStrongBinder(binder);
  status_t rv = service->transact(0, send, &reply);
  ASSERT_EQ(static_cast<status_t>(OK), rv);

  Heap heap;
  allocator::vector<uintptr_t> refs{heap};

  ASSERT_TRUE(BinderReferences(refs));

  bool found_ref = false;
  for (auto ref : refs) {
    if (ref == reinterpret_cast<uintptr_t>(binder.get())) {
      found_ref = true;
    }
  }

  ASSERT_TRUE(found_ref);
}

}  // namespace android