blob: 9c263b1f2161aae90f681a540e859d8a1665cb57 [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland5553ac42020-11-11 02:14:45 +000017#include <BnBinderRpcSession.h>
18#include <BnBinderRpcTest.h>
Steven Moreland37aff182021-03-26 02:04:16 +000019#include <aidl/IBinderRpcTest.h>
Yifan Hong6d82c8a2021-04-26 20:26:45 -070020#include <android-base/file.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000021#include <android-base/logging.h>
Steven Moreland37aff182021-03-26 02:04:16 +000022#include <android/binder_auto_utils.h>
23#include <android/binder_libbinder.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000024#include <binder/Binder.h>
25#include <binder/BpBinder.h>
Steven Morelandeb258ff2021-05-14 03:20:06 +000026#include <binder/IPCThreadState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000027#include <binder/IServiceManager.h>
28#include <binder/ProcessState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000029#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000030#include <binder/RpcSession.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000031#include <gtest/gtest.h>
32
Steven Morelandc1635952021-04-01 16:20:47 +000033#include <chrono>
34#include <cstdlib>
35#include <iostream>
36#include <thread>
37
Steven Morelandc1635952021-04-01 16:20:47 +000038#include <sys/prctl.h>
39#include <unistd.h>
40
Steven Morelandbd5002b2021-05-04 23:12:56 +000041#include "../RpcState.h" // for debugging
42#include "../vm_sockets.h" // for VMADDR_*
Steven Moreland5553ac42020-11-11 02:14:45 +000043
44namespace android {
45
Steven Moreland1fda67b2021-04-02 18:35:50 +000046TEST(BinderRpcParcel, EntireParcelFormatted) {
47 Parcel p;
48 p.writeInt32(3);
49
50 EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), "");
51}
52
Steven Moreland5553ac42020-11-11 02:14:45 +000053using android::binder::Status;
54
55#define EXPECT_OK(status) \
56 do { \
57 Status stat = (status); \
58 EXPECT_TRUE(stat.isOk()) << stat; \
59 } while (false)
60
61class MyBinderRpcSession : public BnBinderRpcSession {
62public:
63 static std::atomic<int32_t> gNum;
64
65 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
66 Status getName(std::string* name) override {
67 *name = mName;
68 return Status::ok();
69 }
70 ~MyBinderRpcSession() { gNum--; }
71
72private:
73 std::string mName;
74};
75std::atomic<int32_t> MyBinderRpcSession::gNum;
76
77class MyBinderRpcTest : public BnBinderRpcTest {
78public:
Steven Moreland611d15f2021-05-01 01:28:27 +000079 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +000080
81 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +000082 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +000083 return Status::ok();
84 }
85 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +000086 *strstr = str + str;
87 return Status::ok();
88 }
Steven Moreland736664b2021-05-01 04:27:25 +000089 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +000090 sp<RpcServer> spServer = server.promote();
91 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +000092 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
93 }
Steven Moreland736664b2021-05-01 04:27:25 +000094 out->clear();
Steven Morelandbdb53ab2021-05-05 17:57:41 +000095 for (auto session : spServer->listSessions()) {
96 size_t count = session->state()->countBinders();
Steven Moreland736664b2021-05-01 04:27:25 +000097 if (count != 1) {
98 // this is called when there is only one binder held remaining,
99 // so to aid debugging
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000100 session->state()->dump();
Steven Moreland611d15f2021-05-01 01:28:27 +0000101 }
Steven Moreland736664b2021-05-01 04:27:25 +0000102 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000103 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000104 return Status::ok();
105 }
106 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
107 if (binder == nullptr) {
108 std::cout << "Received null binder!" << std::endl;
109 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
110 }
111 *out = binder->pingBinder();
112 return Status::ok();
113 }
114 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
115 *out = binder;
116 return Status::ok();
117 }
118 static sp<IBinder> mHeldBinder;
119 Status holdBinder(const sp<IBinder>& binder) override {
120 mHeldBinder = binder;
121 return Status::ok();
122 }
123 Status getHeldBinder(sp<IBinder>* held) override {
124 *held = mHeldBinder;
125 return Status::ok();
126 }
127 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
128 if (count <= 0) return Status::ok();
129 return binder->nestMe(this, count - 1);
130 }
131 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
132 static sp<IBinder> binder = new BBinder;
133 *out = binder;
134 return Status::ok();
135 }
136 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
137 *out = new MyBinderRpcSession(name);
138 return Status::ok();
139 }
140 Status getNumOpenSessions(int32_t* out) override {
141 *out = MyBinderRpcSession::gNum;
142 return Status::ok();
143 }
144
145 std::mutex blockMutex;
146 Status lock() override {
147 blockMutex.lock();
148 return Status::ok();
149 }
150 Status unlockInMsAsync(int32_t ms) override {
151 usleep(ms * 1000);
152 blockMutex.unlock();
153 return Status::ok();
154 }
155 Status lockUnlock() override {
156 std::lock_guard<std::mutex> _l(blockMutex);
157 return Status::ok();
158 }
159
160 Status sleepMs(int32_t ms) override {
161 usleep(ms * 1000);
162 return Status::ok();
163 }
164
165 Status sleepMsAsync(int32_t ms) override {
166 // In-process binder calls are asynchronous, but the call to this method
167 // is synchronous wrt its client. This in/out-process threading model
168 // diffentiation is a classic binder leaky abstraction (for better or
169 // worse) and is preserved here the way binder sockets plugs itself
170 // into BpBinder, as nothing is changed at the higher levels
171 // (IInterface) which result in this behavior.
172 return sleepMs(ms);
173 }
174
175 Status die(bool cleanup) override {
176 if (cleanup) {
177 exit(1);
178 } else {
179 _exit(1);
180 }
181 }
Steven Morelandeb258ff2021-05-14 03:20:06 +0000182 Status useKernelBinderCallingId() override {
183 // this is WRONG! It does not make sense when using RPC binder, and
184 // because it is SO wrong, and so much code calls this, it should abort!
185
186 (void)IPCThreadState::self()->getCallingPid();
187 return Status::ok();
188 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000189};
190sp<IBinder> MyBinderRpcTest::mHeldBinder;
191
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700192class Pipe {
193public:
194 Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); }
195 Pipe(Pipe&&) = default;
196 android::base::borrowed_fd readEnd() { return mRead; }
197 android::base::borrowed_fd writeEnd() { return mWrite; }
198
199private:
200 android::base::unique_fd mRead;
201 android::base::unique_fd mWrite;
202};
203
Steven Moreland5553ac42020-11-11 02:14:45 +0000204class Process {
205public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700206 Process(Process&&) = default;
207 Process(const std::function<void(Pipe*)>& f) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000208 if (0 == (mPid = fork())) {
209 // racey: assume parent doesn't crash before this is set
210 prctl(PR_SET_PDEATHSIG, SIGHUP);
211
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700212 f(&mPipe);
Steven Moreland5553ac42020-11-11 02:14:45 +0000213 }
214 }
215 ~Process() {
216 if (mPid != 0) {
217 kill(mPid, SIGKILL);
218 }
219 }
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700220 Pipe* getPipe() { return &mPipe; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000221
222private:
223 pid_t mPid = 0;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700224 Pipe mPipe;
Steven Moreland5553ac42020-11-11 02:14:45 +0000225};
226
227static std::string allocateSocketAddress() {
228 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000229 std::string temp = getenv("TMPDIR") ?: "/tmp";
230 return temp + "/binderRpcTest_" + std::to_string(id++);
Steven Moreland5553ac42020-11-11 02:14:45 +0000231};
232
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000233struct ProcessSession {
Steven Moreland5553ac42020-11-11 02:14:45 +0000234 // reference to process hosting a socket server
235 Process host;
236
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000237 struct SessionInfo {
238 sp<RpcSession> session;
Steven Moreland736664b2021-05-01 04:27:25 +0000239 sp<IBinder> root;
240 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000241
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000242 // client session objects associated with other process
243 // each one represents a separate session
244 std::vector<SessionInfo> sessions;
Steven Moreland5553ac42020-11-11 02:14:45 +0000245
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000246 ProcessSession(ProcessSession&&) = default;
247 ~ProcessSession() {
248 for (auto& session : sessions) {
249 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000250 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000251
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000252 for (auto& info : sessions) {
253 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000254
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000255 EXPECT_NE(nullptr, session);
256 EXPECT_NE(nullptr, session->state());
257 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000258
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000259 wp<RpcSession> weakSession = session;
260 session = nullptr;
261 EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session";
Steven Moreland736664b2021-05-01 04:27:25 +0000262 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000263 }
264};
265
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000266// Process session where the process hosts IBinderRpcTest, the server used
Steven Moreland5553ac42020-11-11 02:14:45 +0000267// for most testing here
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000268struct BinderRpcTestProcessSession {
269 ProcessSession proc;
Steven Moreland5553ac42020-11-11 02:14:45 +0000270
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000271 // pre-fetched root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000272 sp<IBinder> rootBinder;
273
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000274 // pre-casted root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000275 sp<IBinderRpcTest> rootIface;
276
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000277 // whether session should be invalidated by end of run
Steven Moreland736664b2021-05-01 04:27:25 +0000278 bool expectInvalid = false;
279
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000280 BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default;
281 ~BinderRpcTestProcessSession() {
Steven Moreland736664b2021-05-01 04:27:25 +0000282 if (!expectInvalid) {
283 std::vector<int32_t> remoteCounts;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000284 // calling over any sessions counts across all sessions
Steven Moreland736664b2021-05-01 04:27:25 +0000285 EXPECT_OK(rootIface->countBinders(&remoteCounts));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000286 EXPECT_EQ(remoteCounts.size(), proc.sessions.size());
Steven Moreland736664b2021-05-01 04:27:25 +0000287 for (auto remoteCount : remoteCounts) {
288 EXPECT_EQ(remoteCount, 1);
289 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000290 }
291
292 rootIface = nullptr;
293 rootBinder = nullptr;
294 }
295};
296
Steven Morelandc1635952021-04-01 16:20:47 +0000297enum class SocketType {
298 UNIX,
299 VSOCK,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700300 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000301};
302static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) {
303 switch (info.param) {
304 case SocketType::UNIX:
305 return "unix_domain_socket";
306 case SocketType::VSOCK:
307 return "vm_socket";
Yifan Hong0d2bd112021-04-13 17:38:36 -0700308 case SocketType::INET:
309 return "inet_socket";
Steven Morelandc1635952021-04-01 16:20:47 +0000310 default:
311 LOG_ALWAYS_FATAL("Unknown socket type");
312 return "";
313 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000314}
Steven Morelandc1635952021-04-01 16:20:47 +0000315class BinderRpc : public ::testing::TestWithParam<SocketType> {
316public:
317 // This creates a new process serving an interface on a certain number of
318 // threads.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000319 ProcessSession createRpcTestSocketServerProcess(
320 size_t numThreads, size_t numSessions,
Steven Moreland736664b2021-05-01 04:27:25 +0000321 const std::function<void(const sp<RpcServer>&)>& configure) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000322 CHECK_GE(numSessions, 1) << "Must have at least one session to a server";
Steven Moreland736664b2021-05-01 04:27:25 +0000323
Steven Morelandc1635952021-04-01 16:20:47 +0000324 SocketType socketType = GetParam();
325
326 std::string addr = allocateSocketAddress();
327 unlink(addr.c_str());
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700328 static unsigned int vsockPort = 3456;
329 vsockPort++;
Steven Morelandc1635952021-04-01 16:20:47 +0000330
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000331 auto ret = ProcessSession{
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700332 .host = Process([&](Pipe* pipe) {
Steven Morelandc1635952021-04-01 16:20:47 +0000333 sp<RpcServer> server = RpcServer::make();
334
335 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Morelandf137de92021-04-24 01:54:26 +0000336 server->setMaxThreads(numThreads);
Steven Morelandc1635952021-04-01 16:20:47 +0000337
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000338 unsigned int outPort = 0;
339
Steven Morelandc1635952021-04-01 16:20:47 +0000340 switch (socketType) {
341 case SocketType::UNIX:
Steven Moreland611d15f2021-05-01 01:28:27 +0000342 CHECK(server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000343 break;
344 case SocketType::VSOCK:
Steven Moreland611d15f2021-05-01 01:28:27 +0000345 CHECK(server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000346 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700347 case SocketType::INET: {
Steven Moreland611d15f2021-05-01 01:28:27 +0000348 CHECK(server->setupInetServer(0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700349 CHECK_NE(0, outPort);
Yifan Hong0d2bd112021-04-13 17:38:36 -0700350 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700351 }
Steven Morelandc1635952021-04-01 16:20:47 +0000352 default:
353 LOG_ALWAYS_FATAL("Unknown socket type");
354 }
355
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000356 CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort, sizeof(outPort)));
357
Steven Moreland611d15f2021-05-01 01:28:27 +0000358 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000359
Steven Morelandf137de92021-04-24 01:54:26 +0000360 server->join();
Steven Morelandc1635952021-04-01 16:20:47 +0000361 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000362 };
363
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000364 // always read socket, so that we have waited for the server to start
365 unsigned int outPort = 0;
366 CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &outPort, sizeof(outPort)));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700367 if (socketType == SocketType::INET) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000368 CHECK_NE(0, outPort);
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700369 }
370
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000371 for (size_t i = 0; i < numSessions; i++) {
372 sp<RpcSession> session = RpcSession::make();
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000373 switch (socketType) {
374 case SocketType::UNIX:
375 if (session->setupUnixDomainClient(addr.c_str())) goto success;
376 break;
377 case SocketType::VSOCK:
378 if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success;
379 break;
380 case SocketType::INET:
381 if (session->setupInetClient("127.0.0.1", outPort)) goto success;
382 break;
383 default:
384 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000385 }
Steven Moreland736664b2021-05-01 04:27:25 +0000386 LOG_ALWAYS_FATAL("Could not connect");
387 success:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000388 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000389 }
Steven Morelandc1635952021-04-01 16:20:47 +0000390 return ret;
391 }
392
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000393 BinderRpcTestProcessSession createRpcTestSocketServerProcess(size_t numThreads,
394 size_t numSessions = 1) {
395 BinderRpcTestProcessSession ret{
396 .proc = createRpcTestSocketServerProcess(numThreads, numSessions,
Steven Moreland611d15f2021-05-01 01:28:27 +0000397 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000398 sp<MyBinderRpcTest> service =
399 new MyBinderRpcTest;
400 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000401 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000402 }),
403 };
404
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000405 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000406 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
407
408 return ret;
409 }
410};
411
412TEST_P(BinderRpc, RootObjectIsNull) {
Steven Moreland736664b2021-05-01 04:27:25 +0000413 auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000414 // this is the default, but to be explicit
415 server->setRootObject(nullptr);
416 });
Steven Moreland5553ac42020-11-11 02:14:45 +0000417
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000418 EXPECT_EQ(nullptr, proc.sessions.at(0).root);
Steven Moreland5553ac42020-11-11 02:14:45 +0000419}
420
Steven Morelandc1635952021-04-01 16:20:47 +0000421TEST_P(BinderRpc, Ping) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000422 auto proc = createRpcTestSocketServerProcess(1);
423 ASSERT_NE(proc.rootBinder, nullptr);
424 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
425}
426
Steven Moreland4cf688f2021-03-31 01:48:58 +0000427TEST_P(BinderRpc, GetInterfaceDescriptor) {
428 auto proc = createRpcTestSocketServerProcess(1);
429 ASSERT_NE(proc.rootBinder, nullptr);
430 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
431}
432
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000433TEST_P(BinderRpc, MultipleSessions) {
434 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*sessions*/);
435 for (auto session : proc.proc.sessions) {
436 ASSERT_NE(nullptr, session.root);
437 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000438 }
439}
440
Steven Morelandc1635952021-04-01 16:20:47 +0000441TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000442 auto proc = createRpcTestSocketServerProcess(1);
443 Parcel data;
444 Parcel reply;
445 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
446}
447
Steven Moreland67753c32021-04-02 18:45:19 +0000448TEST_P(BinderRpc, AppendSeparateFormats) {
449 auto proc = createRpcTestSocketServerProcess(1);
450
451 Parcel p1;
452 p1.markForBinder(proc.rootBinder);
453 p1.writeInt32(3);
454
455 Parcel p2;
456
457 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
458 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
459}
460
Steven Morelandc1635952021-04-01 16:20:47 +0000461TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000462 auto proc = createRpcTestSocketServerProcess(1);
463 Parcel data;
464 data.markForBinder(proc.rootBinder);
465 Parcel reply;
466 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
467}
468
Steven Morelandc1635952021-04-01 16:20:47 +0000469TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000470 auto proc = createRpcTestSocketServerProcess(1);
471 EXPECT_OK(proc.rootIface->sendString("asdf"));
472}
473
Steven Morelandc1635952021-04-01 16:20:47 +0000474TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000475 auto proc = createRpcTestSocketServerProcess(1);
476 std::string doubled;
477 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
478 EXPECT_EQ("cool cool ", doubled);
479}
480
Steven Morelandc1635952021-04-01 16:20:47 +0000481TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000482 auto proc = createRpcTestSocketServerProcess(1);
483 std::string single = std::string(1024, 'a');
484 std::string doubled;
485 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
486 EXPECT_EQ(single + single, doubled);
487}
488
Steven Morelandc1635952021-04-01 16:20:47 +0000489TEST_P(BinderRpc, CallMeBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000490 auto proc = createRpcTestSocketServerProcess(1);
491
492 int32_t pingResult;
493 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
494 EXPECT_EQ(OK, pingResult);
495
496 EXPECT_EQ(0, MyBinderRpcSession::gNum);
497}
498
Steven Morelandc1635952021-04-01 16:20:47 +0000499TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000500 auto proc = createRpcTestSocketServerProcess(1);
501
502 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
503 sp<IBinder> outBinder;
504 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
505 EXPECT_EQ(inBinder, outBinder);
506
507 wp<IBinder> weak = inBinder;
508 inBinder = nullptr;
509 outBinder = nullptr;
510
511 // Force reading a reply, to process any pending dec refs from the other
512 // process (the other process will process dec refs there before processing
513 // the ping here).
514 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
515
516 EXPECT_EQ(nullptr, weak.promote());
517
518 EXPECT_EQ(0, MyBinderRpcSession::gNum);
519}
520
Steven Morelandc1635952021-04-01 16:20:47 +0000521TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000522 auto proc = createRpcTestSocketServerProcess(1);
523
524 sp<IBinderRpcSession> session;
525 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
526
527 sp<IBinder> inBinder = IInterface::asBinder(session);
528 sp<IBinder> outBinder;
529 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
530 EXPECT_EQ(inBinder, outBinder);
531
532 wp<IBinder> weak = inBinder;
533 session = nullptr;
534 inBinder = nullptr;
535 outBinder = nullptr;
536
537 // Force reading a reply, to process any pending dec refs from the other
538 // process (the other process will process dec refs there before processing
539 // the ping here).
540 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
541
542 EXPECT_EQ(nullptr, weak.promote());
543}
544
Steven Morelandc1635952021-04-01 16:20:47 +0000545TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000546 auto proc = createRpcTestSocketServerProcess(1);
547
548 sp<IBinder> outBinder;
549 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
550 EXPECT_EQ(nullptr, outBinder);
551}
552
Steven Morelandc1635952021-04-01 16:20:47 +0000553TEST_P(BinderRpc, HoldBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000554 auto proc = createRpcTestSocketServerProcess(1);
555
556 IBinder* ptr = nullptr;
557 {
558 sp<IBinder> binder = new BBinder();
559 ptr = binder.get();
560 EXPECT_OK(proc.rootIface->holdBinder(binder));
561 }
562
563 sp<IBinder> held;
564 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
565
566 EXPECT_EQ(held.get(), ptr);
567
568 // stop holding binder, because we test to make sure references are cleaned
569 // up
570 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
571 // and flush ref counts
572 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
573}
574
575// START TESTS FOR LIMITATIONS OF SOCKET BINDER
576// These are behavioral differences form regular binder, where certain usecases
577// aren't supported.
578
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000579TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000580 auto proc1 = createRpcTestSocketServerProcess(1);
581 auto proc2 = createRpcTestSocketServerProcess(1);
582
583 sp<IBinder> outBinder;
584 EXPECT_EQ(INVALID_OPERATION,
585 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
586}
587
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000588TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
589 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*sessions*/);
Steven Moreland736664b2021-05-01 04:27:25 +0000590
591 sp<IBinder> outBinder;
592 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000593 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000594 .transactionError());
595}
596
Steven Morelandc1635952021-04-01 16:20:47 +0000597TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000598 auto proc = createRpcTestSocketServerProcess(1);
599
600 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
601 sp<IBinder> outBinder;
602 EXPECT_EQ(INVALID_OPERATION,
603 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
604}
605
Steven Morelandc1635952021-04-01 16:20:47 +0000606TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000607 auto proc = createRpcTestSocketServerProcess(1);
608
609 // for historical reasons, IServiceManager interface only returns the
610 // exception code
611 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
612 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
613}
614
615// END TESTS FOR LIMITATIONS OF SOCKET BINDER
616
Steven Morelandc1635952021-04-01 16:20:47 +0000617TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000618 auto proc = createRpcTestSocketServerProcess(1);
619
620 sp<IBinder> outBinder;
621 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
622 EXPECT_EQ(proc.rootBinder, outBinder);
623}
624
Steven Morelandc1635952021-04-01 16:20:47 +0000625TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000626 auto proc = createRpcTestSocketServerProcess(1);
627
628 auto nastyNester = sp<MyBinderRpcTest>::make();
629 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
630
631 wp<IBinder> weak = nastyNester;
632 nastyNester = nullptr;
633 EXPECT_EQ(nullptr, weak.promote());
634}
635
Steven Morelandc1635952021-04-01 16:20:47 +0000636TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000637 auto proc = createRpcTestSocketServerProcess(1);
638
639 sp<IBinder> a;
640 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
641
642 sp<IBinder> b;
643 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
644
645 EXPECT_EQ(a, b);
646}
647
Steven Morelandc1635952021-04-01 16:20:47 +0000648TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000649 auto proc = createRpcTestSocketServerProcess(1);
650
651 sp<IBinder> a;
652 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
653 wp<IBinder> weak = a;
654 a = nullptr;
655
656 sp<IBinder> b;
657 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
658
659 // this is the wrong behavior, since BpBinder
660 // doesn't implement onIncStrongAttempted
661 // but make sure there is no crash
662 EXPECT_EQ(nullptr, weak.promote());
663
664 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
665
666 // In order to fix this:
667 // - need to have incStrongAttempted reflected across IPC boundary (wait for
668 // response to promote - round trip...)
669 // - sendOnLastWeakRef, to delete entries out of RpcState table
670 EXPECT_EQ(b, weak.promote());
671}
672
673#define expectSessions(expected, iface) \
674 do { \
675 int session; \
676 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
677 EXPECT_EQ(expected, session); \
678 } while (false)
679
Steven Morelandc1635952021-04-01 16:20:47 +0000680TEST_P(BinderRpc, SingleSession) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000681 auto proc = createRpcTestSocketServerProcess(1);
682
683 sp<IBinderRpcSession> session;
684 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
685 std::string out;
686 EXPECT_OK(session->getName(&out));
687 EXPECT_EQ("aoeu", out);
688
689 expectSessions(1, proc.rootIface);
690 session = nullptr;
691 expectSessions(0, proc.rootIface);
692}
693
Steven Morelandc1635952021-04-01 16:20:47 +0000694TEST_P(BinderRpc, ManySessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000695 auto proc = createRpcTestSocketServerProcess(1);
696
697 std::vector<sp<IBinderRpcSession>> sessions;
698
699 for (size_t i = 0; i < 15; i++) {
700 expectSessions(i, proc.rootIface);
701 sp<IBinderRpcSession> session;
702 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
703 sessions.push_back(session);
704 }
705 expectSessions(sessions.size(), proc.rootIface);
706 for (size_t i = 0; i < sessions.size(); i++) {
707 std::string out;
708 EXPECT_OK(sessions.at(i)->getName(&out));
709 EXPECT_EQ(std::to_string(i), out);
710 }
711 expectSessions(sessions.size(), proc.rootIface);
712
713 while (!sessions.empty()) {
714 sessions.pop_back();
715 expectSessions(sessions.size(), proc.rootIface);
716 }
717 expectSessions(0, proc.rootIface);
718}
719
720size_t epochMillis() {
721 using std::chrono::duration_cast;
722 using std::chrono::milliseconds;
723 using std::chrono::seconds;
724 using std::chrono::system_clock;
725 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
726}
727
Steven Morelandc1635952021-04-01 16:20:47 +0000728TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000729 constexpr size_t kNumThreads = 10;
730
731 auto proc = createRpcTestSocketServerProcess(kNumThreads);
732
733 EXPECT_OK(proc.rootIface->lock());
734
735 // block all but one thread taking locks
736 std::vector<std::thread> ts;
737 for (size_t i = 0; i < kNumThreads - 1; i++) {
738 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
739 }
740
741 usleep(100000); // give chance for calls on other threads
742
743 // other calls still work
744 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
745
746 constexpr size_t blockTimeMs = 500;
747 size_t epochMsBefore = epochMillis();
748 // after this, we should never see a response within this time
749 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
750
751 // this call should be blocked for blockTimeMs
752 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
753
754 size_t epochMsAfter = epochMillis();
755 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
756
757 for (auto& t : ts) t.join();
758}
759
Steven Morelandc1635952021-04-01 16:20:47 +0000760TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000761 constexpr size_t kNumThreads = 10;
762 constexpr size_t kNumCalls = kNumThreads + 3;
763 constexpr size_t kSleepMs = 500;
764
765 auto proc = createRpcTestSocketServerProcess(kNumThreads);
766
767 size_t epochMsBefore = epochMillis();
768
769 std::vector<std::thread> ts;
770 for (size_t i = 0; i < kNumCalls; i++) {
771 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
772 }
773
774 for (auto& t : ts) t.join();
775
776 size_t epochMsAfter = epochMillis();
777
778 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
779
780 // Potential flake, but make sure calls are handled in parallel.
781 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
782}
783
Steven Morelandc1635952021-04-01 16:20:47 +0000784TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000785 constexpr size_t kNumClientThreads = 10;
786 constexpr size_t kNumServerThreads = 10;
787 constexpr size_t kNumCalls = 100;
788
789 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
790
791 std::vector<std::thread> threads;
792 for (size_t i = 0; i < kNumClientThreads; i++) {
793 threads.push_back(std::thread([&] {
794 for (size_t j = 0; j < kNumCalls; j++) {
795 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000796 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000797 EXPECT_EQ(proc.rootBinder, out);
798 }
799 }));
800 }
801
802 for (auto& t : threads) t.join();
803}
804
Steven Morelandc6046982021-04-20 00:49:42 +0000805TEST_P(BinderRpc, OnewayStressTest) {
806 constexpr size_t kNumClientThreads = 10;
807 constexpr size_t kNumServerThreads = 10;
808 constexpr size_t kNumCalls = 100;
809
810 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
811
812 std::vector<std::thread> threads;
813 for (size_t i = 0; i < kNumClientThreads; i++) {
814 threads.push_back(std::thread([&] {
815 for (size_t j = 0; j < kNumCalls; j++) {
816 EXPECT_OK(proc.rootIface->sendString("a"));
817 }
818
819 // check threads are not stuck
820 EXPECT_OK(proc.rootIface->sleepMs(250));
821 }));
822 }
823
824 for (auto& t : threads) t.join();
825}
826
Steven Morelandc1635952021-04-01 16:20:47 +0000827TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000828 constexpr size_t kReallyLongTimeMs = 100;
829 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
830
831 // more than one thread, just so this doesn't deadlock
832 auto proc = createRpcTestSocketServerProcess(2);
833
834 size_t epochMsBefore = epochMillis();
835
836 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
837
838 size_t epochMsAfter = epochMillis();
839 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
840}
841
Steven Morelandc1635952021-04-01 16:20:47 +0000842TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000843 constexpr size_t kNumSleeps = 10;
844 constexpr size_t kNumExtraServerThreads = 4;
845 constexpr size_t kSleepMs = 50;
846
847 // make sure calls to the same object happen on the same thread
848 auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads);
849
850 EXPECT_OK(proc.rootIface->lock());
851
852 for (size_t i = 0; i < kNumSleeps; i++) {
853 // these should be processed serially
854 proc.rootIface->sleepMsAsync(kSleepMs);
855 }
856 // should also be processesed serially
857 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
858
859 size_t epochMsBefore = epochMillis();
860 EXPECT_OK(proc.rootIface->lockUnlock());
861 size_t epochMsAfter = epochMillis();
862
863 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
864}
865
Steven Morelandc1635952021-04-01 16:20:47 +0000866TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000867 for (bool doDeathCleanup : {true, false}) {
868 auto proc = createRpcTestSocketServerProcess(1);
869
870 // make sure there is some state during crash
871 // 1. we hold their binder
872 sp<IBinderRpcSession> session;
873 EXPECT_OK(proc.rootIface->openSession("happy", &session));
874 // 2. they hold our binder
875 sp<IBinder> binder = new BBinder();
876 EXPECT_OK(proc.rootIface->holdBinder(binder));
877
878 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
879 << "Do death cleanup: " << doDeathCleanup;
880
Steven Moreland736664b2021-05-01 04:27:25 +0000881 proc.expectInvalid = true;
Steven Moreland5553ac42020-11-11 02:14:45 +0000882 }
883}
884
Steven Morelandeb258ff2021-05-14 03:20:06 +0000885TEST_P(BinderRpc, UseKernelBinderCallingId) {
886 auto proc = createRpcTestSocketServerProcess(1);
887
888 // we can't allocate IPCThreadState so actually the first time should
889 // succeed :(
890 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
891
892 // second time! we catch the error :)
893 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
894
895 proc.expectInvalid = true;
896}
897
Steven Moreland37aff182021-03-26 02:04:16 +0000898TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
899 auto proc = createRpcTestSocketServerProcess(1);
900
901 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
902 ASSERT_NE(binder, nullptr);
903
904 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
905}
906
907TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
908 auto proc = createRpcTestSocketServerProcess(1);
909
910 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
911 ASSERT_NE(binder, nullptr);
912
913 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
914 ASSERT_NE(ndkBinder, nullptr);
915
916 std::string out;
917 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
918 ASSERT_TRUE(status.isOk()) << status.getDescription();
919 ASSERT_EQ("aoeuaoeu", out);
920}
921
Steven Moreland5553ac42020-11-11 02:14:45 +0000922ssize_t countFds() {
923 DIR* dir = opendir("/proc/self/fd/");
924 if (dir == nullptr) return -1;
925 ssize_t ret = 0;
926 dirent* ent;
927 while ((ent = readdir(dir)) != nullptr) ret++;
928 closedir(dir);
929 return ret;
930}
931
Steven Morelandc1635952021-04-01 16:20:47 +0000932TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000933 ssize_t beforeFds = countFds();
934 ASSERT_GE(beforeFds, 0);
935 {
936 auto proc = createRpcTestSocketServerProcess(10);
937 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
938 }
939 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
940}
941
Steven Morelandc1635952021-04-01 16:20:47 +0000942INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700943 ::testing::ValuesIn({
944 SocketType::UNIX,
Steven Morelandbd5002b2021-05-04 23:12:56 +0000945// TODO(b/185269356): working on host
Steven Morelandf6ec4632021-04-01 16:20:47 +0000946#ifdef __BIONIC__
Yifan Hong0d2bd112021-04-13 17:38:36 -0700947 SocketType::VSOCK,
Steven Morelandbd5002b2021-05-04 23:12:56 +0000948#endif
Yifan Hong0d2bd112021-04-13 17:38:36 -0700949 SocketType::INET,
950 }),
Steven Morelandf6ec4632021-04-01 16:20:47 +0000951 PrintSocketType);
Steven Morelandc1635952021-04-01 16:20:47 +0000952
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700953class BinderRpcServerRootObject : public ::testing::TestWithParam<std::tuple<bool, bool>> {};
954
955TEST_P(BinderRpcServerRootObject, WeakRootObject) {
956 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
957 auto setRootObject = [](bool isStrong) -> SetFn {
958 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
959 };
960
961 auto server = RpcServer::make();
962 auto [isStrong1, isStrong2] = GetParam();
963 auto binder1 = sp<BBinder>::make();
964 IBinder* binderRaw1 = binder1.get();
965 setRootObject(isStrong1)(server.get(), binder1);
966 EXPECT_EQ(binderRaw1, server->getRootObject());
967 binder1.clear();
968 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
969
970 auto binder2 = sp<BBinder>::make();
971 IBinder* binderRaw2 = binder2.get();
972 setRootObject(isStrong2)(server.get(), binder2);
973 EXPECT_EQ(binderRaw2, server->getRootObject());
974 binder2.clear();
975 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
976}
977
978INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
979 ::testing::Combine(::testing::Bool(), ::testing::Bool()));
980
Steven Morelandc1635952021-04-01 16:20:47 +0000981} // namespace android
982
983int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000984 ::testing::InitGoogleTest(&argc, argv);
985 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
986 return RUN_ALL_TESTS();
987}