blob: 3f94df2a4c8dcb7287d0ec3254a6738870354ed3 [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 Morelandd7302072021-05-15 01:32:04 +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
Yifan Hong00aeb762021-05-12 17:07:36 -070053TEST(BinderRpc, SetExternalServer) {
54 base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
55 int sinkFd = sink.get();
56 auto server = RpcServer::make();
57 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
58 ASSERT_FALSE(server->hasServer());
59 ASSERT_TRUE(server->setupExternalServer(std::move(sink)));
60 ASSERT_TRUE(server->hasServer());
61 base::unique_fd retrieved = server->releaseServer();
62 ASSERT_FALSE(server->hasServer());
63 ASSERT_EQ(sinkFd, retrieved.get());
64}
65
Steven Moreland5553ac42020-11-11 02:14:45 +000066using android::binder::Status;
67
68#define EXPECT_OK(status) \
69 do { \
70 Status stat = (status); \
71 EXPECT_TRUE(stat.isOk()) << stat; \
72 } while (false)
73
74class MyBinderRpcSession : public BnBinderRpcSession {
75public:
76 static std::atomic<int32_t> gNum;
77
78 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
79 Status getName(std::string* name) override {
80 *name = mName;
81 return Status::ok();
82 }
83 ~MyBinderRpcSession() { gNum--; }
84
85private:
86 std::string mName;
87};
88std::atomic<int32_t> MyBinderRpcSession::gNum;
89
90class MyBinderRpcTest : public BnBinderRpcTest {
91public:
Steven Moreland611d15f2021-05-01 01:28:27 +000092 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +000093
94 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +000095 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +000096 return Status::ok();
97 }
98 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +000099 *strstr = str + str;
100 return Status::ok();
101 }
Steven Moreland736664b2021-05-01 04:27:25 +0000102 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +0000103 sp<RpcServer> spServer = server.promote();
104 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000105 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
106 }
Steven Moreland736664b2021-05-01 04:27:25 +0000107 out->clear();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000108 for (auto session : spServer->listSessions()) {
109 size_t count = session->state()->countBinders();
Steven Moreland736664b2021-05-01 04:27:25 +0000110 if (count != 1) {
111 // this is called when there is only one binder held remaining,
112 // so to aid debugging
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000113 session->state()->dump();
Steven Moreland611d15f2021-05-01 01:28:27 +0000114 }
Steven Moreland736664b2021-05-01 04:27:25 +0000115 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000116 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000117 return Status::ok();
118 }
119 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
120 if (binder == nullptr) {
121 std::cout << "Received null binder!" << std::endl;
122 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
123 }
124 *out = binder->pingBinder();
125 return Status::ok();
126 }
127 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
128 *out = binder;
129 return Status::ok();
130 }
131 static sp<IBinder> mHeldBinder;
132 Status holdBinder(const sp<IBinder>& binder) override {
133 mHeldBinder = binder;
134 return Status::ok();
135 }
136 Status getHeldBinder(sp<IBinder>* held) override {
137 *held = mHeldBinder;
138 return Status::ok();
139 }
140 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
141 if (count <= 0) return Status::ok();
142 return binder->nestMe(this, count - 1);
143 }
144 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
145 static sp<IBinder> binder = new BBinder;
146 *out = binder;
147 return Status::ok();
148 }
149 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
150 *out = new MyBinderRpcSession(name);
151 return Status::ok();
152 }
153 Status getNumOpenSessions(int32_t* out) override {
154 *out = MyBinderRpcSession::gNum;
155 return Status::ok();
156 }
157
158 std::mutex blockMutex;
159 Status lock() override {
160 blockMutex.lock();
161 return Status::ok();
162 }
163 Status unlockInMsAsync(int32_t ms) override {
164 usleep(ms * 1000);
165 blockMutex.unlock();
166 return Status::ok();
167 }
168 Status lockUnlock() override {
169 std::lock_guard<std::mutex> _l(blockMutex);
170 return Status::ok();
171 }
172
173 Status sleepMs(int32_t ms) override {
174 usleep(ms * 1000);
175 return Status::ok();
176 }
177
178 Status sleepMsAsync(int32_t ms) override {
179 // In-process binder calls are asynchronous, but the call to this method
180 // is synchronous wrt its client. This in/out-process threading model
181 // diffentiation is a classic binder leaky abstraction (for better or
182 // worse) and is preserved here the way binder sockets plugs itself
183 // into BpBinder, as nothing is changed at the higher levels
184 // (IInterface) which result in this behavior.
185 return sleepMs(ms);
186 }
187
188 Status die(bool cleanup) override {
189 if (cleanup) {
190 exit(1);
191 } else {
192 _exit(1);
193 }
194 }
Steven Morelandd7302072021-05-15 01:32:04 +0000195 Status useKernelBinderCallingId() override {
196 // this is WRONG! It does not make sense when using RPC binder, and
197 // because it is SO wrong, and so much code calls this, it should abort!
198
199 (void)IPCThreadState::self()->getCallingPid();
200 return Status::ok();
201 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000202};
203sp<IBinder> MyBinderRpcTest::mHeldBinder;
204
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700205class Pipe {
206public:
207 Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); }
208 Pipe(Pipe&&) = default;
209 android::base::borrowed_fd readEnd() { return mRead; }
210 android::base::borrowed_fd writeEnd() { return mWrite; }
211
212private:
213 android::base::unique_fd mRead;
214 android::base::unique_fd mWrite;
215};
216
Steven Moreland5553ac42020-11-11 02:14:45 +0000217class Process {
218public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700219 Process(Process&&) = default;
220 Process(const std::function<void(Pipe*)>& f) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000221 if (0 == (mPid = fork())) {
222 // racey: assume parent doesn't crash before this is set
223 prctl(PR_SET_PDEATHSIG, SIGHUP);
224
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700225 f(&mPipe);
Steven Moreland5553ac42020-11-11 02:14:45 +0000226 }
227 }
228 ~Process() {
229 if (mPid != 0) {
230 kill(mPid, SIGKILL);
231 }
232 }
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700233 Pipe* getPipe() { return &mPipe; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000234
235private:
236 pid_t mPid = 0;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700237 Pipe mPipe;
Steven Moreland5553ac42020-11-11 02:14:45 +0000238};
239
240static std::string allocateSocketAddress() {
241 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000242 std::string temp = getenv("TMPDIR") ?: "/tmp";
243 return temp + "/binderRpcTest_" + std::to_string(id++);
Steven Moreland5553ac42020-11-11 02:14:45 +0000244};
245
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000246struct ProcessSession {
Steven Moreland5553ac42020-11-11 02:14:45 +0000247 // reference to process hosting a socket server
248 Process host;
249
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000250 struct SessionInfo {
251 sp<RpcSession> session;
Steven Moreland736664b2021-05-01 04:27:25 +0000252 sp<IBinder> root;
253 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000254
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000255 // client session objects associated with other process
256 // each one represents a separate session
257 std::vector<SessionInfo> sessions;
Steven Moreland5553ac42020-11-11 02:14:45 +0000258
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000259 ProcessSession(ProcessSession&&) = default;
260 ~ProcessSession() {
261 for (auto& session : sessions) {
262 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000263 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000264
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000265 for (auto& info : sessions) {
266 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000267
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000268 EXPECT_NE(nullptr, session);
269 EXPECT_NE(nullptr, session->state());
270 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000271
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000272 wp<RpcSession> weakSession = session;
273 session = nullptr;
274 EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session";
Steven Moreland736664b2021-05-01 04:27:25 +0000275 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000276 }
277};
278
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000279// Process session where the process hosts IBinderRpcTest, the server used
Steven Moreland5553ac42020-11-11 02:14:45 +0000280// for most testing here
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000281struct BinderRpcTestProcessSession {
282 ProcessSession proc;
Steven Moreland5553ac42020-11-11 02:14:45 +0000283
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000284 // pre-fetched root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000285 sp<IBinder> rootBinder;
286
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000287 // pre-casted root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000288 sp<IBinderRpcTest> rootIface;
289
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000290 // whether session should be invalidated by end of run
Steven Moreland736664b2021-05-01 04:27:25 +0000291 bool expectInvalid = false;
292
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000293 BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default;
294 ~BinderRpcTestProcessSession() {
Steven Moreland736664b2021-05-01 04:27:25 +0000295 if (!expectInvalid) {
296 std::vector<int32_t> remoteCounts;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000297 // calling over any sessions counts across all sessions
Steven Moreland736664b2021-05-01 04:27:25 +0000298 EXPECT_OK(rootIface->countBinders(&remoteCounts));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000299 EXPECT_EQ(remoteCounts.size(), proc.sessions.size());
Steven Moreland736664b2021-05-01 04:27:25 +0000300 for (auto remoteCount : remoteCounts) {
301 EXPECT_EQ(remoteCount, 1);
302 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000303 }
304
305 rootIface = nullptr;
306 rootBinder = nullptr;
307 }
308};
309
Steven Morelandc1635952021-04-01 16:20:47 +0000310enum class SocketType {
311 UNIX,
312 VSOCK,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700313 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000314};
315static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) {
316 switch (info.param) {
317 case SocketType::UNIX:
318 return "unix_domain_socket";
319 case SocketType::VSOCK:
320 return "vm_socket";
Yifan Hong0d2bd112021-04-13 17:38:36 -0700321 case SocketType::INET:
322 return "inet_socket";
Steven Morelandc1635952021-04-01 16:20:47 +0000323 default:
324 LOG_ALWAYS_FATAL("Unknown socket type");
325 return "";
326 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000327}
Steven Morelandc1635952021-04-01 16:20:47 +0000328class BinderRpc : public ::testing::TestWithParam<SocketType> {
329public:
330 // This creates a new process serving an interface on a certain number of
331 // threads.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000332 ProcessSession createRpcTestSocketServerProcess(
333 size_t numThreads, size_t numSessions,
Steven Moreland736664b2021-05-01 04:27:25 +0000334 const std::function<void(const sp<RpcServer>&)>& configure) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000335 CHECK_GE(numSessions, 1) << "Must have at least one session to a server";
Steven Moreland736664b2021-05-01 04:27:25 +0000336
Steven Morelandc1635952021-04-01 16:20:47 +0000337 SocketType socketType = GetParam();
338
339 std::string addr = allocateSocketAddress();
340 unlink(addr.c_str());
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700341 static unsigned int vsockPort = 3456;
342 vsockPort++;
Steven Morelandc1635952021-04-01 16:20:47 +0000343
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000344 auto ret = ProcessSession{
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700345 .host = Process([&](Pipe* pipe) {
Steven Morelandc1635952021-04-01 16:20:47 +0000346 sp<RpcServer> server = RpcServer::make();
347
348 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Morelandf137de92021-04-24 01:54:26 +0000349 server->setMaxThreads(numThreads);
Steven Morelandc1635952021-04-01 16:20:47 +0000350
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000351 unsigned int outPort = 0;
352
Steven Morelandc1635952021-04-01 16:20:47 +0000353 switch (socketType) {
354 case SocketType::UNIX:
Steven Moreland611d15f2021-05-01 01:28:27 +0000355 CHECK(server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000356 break;
357 case SocketType::VSOCK:
Steven Moreland611d15f2021-05-01 01:28:27 +0000358 CHECK(server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000359 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700360 case SocketType::INET: {
Steven Moreland611d15f2021-05-01 01:28:27 +0000361 CHECK(server->setupInetServer(0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700362 CHECK_NE(0, outPort);
Yifan Hong0d2bd112021-04-13 17:38:36 -0700363 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700364 }
Steven Morelandc1635952021-04-01 16:20:47 +0000365 default:
366 LOG_ALWAYS_FATAL("Unknown socket type");
367 }
368
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000369 CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort, sizeof(outPort)));
370
Steven Moreland611d15f2021-05-01 01:28:27 +0000371 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000372
Steven Morelandf137de92021-04-24 01:54:26 +0000373 server->join();
Steven Morelandc1635952021-04-01 16:20:47 +0000374 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000375 };
376
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000377 // always read socket, so that we have waited for the server to start
378 unsigned int outPort = 0;
379 CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &outPort, sizeof(outPort)));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700380 if (socketType == SocketType::INET) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000381 CHECK_NE(0, outPort);
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700382 }
383
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000384 for (size_t i = 0; i < numSessions; i++) {
385 sp<RpcSession> session = RpcSession::make();
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000386 switch (socketType) {
387 case SocketType::UNIX:
388 if (session->setupUnixDomainClient(addr.c_str())) goto success;
389 break;
390 case SocketType::VSOCK:
391 if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success;
392 break;
393 case SocketType::INET:
394 if (session->setupInetClient("127.0.0.1", outPort)) goto success;
395 break;
396 default:
397 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000398 }
Steven Moreland736664b2021-05-01 04:27:25 +0000399 LOG_ALWAYS_FATAL("Could not connect");
400 success:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000401 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000402 }
Steven Morelandc1635952021-04-01 16:20:47 +0000403 return ret;
404 }
405
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000406 BinderRpcTestProcessSession createRpcTestSocketServerProcess(size_t numThreads,
407 size_t numSessions = 1) {
408 BinderRpcTestProcessSession ret{
409 .proc = createRpcTestSocketServerProcess(numThreads, numSessions,
Steven Moreland611d15f2021-05-01 01:28:27 +0000410 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000411 sp<MyBinderRpcTest> service =
412 new MyBinderRpcTest;
413 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000414 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000415 }),
416 };
417
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000418 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000419 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
420
421 return ret;
422 }
423};
424
425TEST_P(BinderRpc, RootObjectIsNull) {
Steven Moreland736664b2021-05-01 04:27:25 +0000426 auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000427 // this is the default, but to be explicit
428 server->setRootObject(nullptr);
429 });
Steven Moreland5553ac42020-11-11 02:14:45 +0000430
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000431 EXPECT_EQ(nullptr, proc.sessions.at(0).root);
Steven Moreland5553ac42020-11-11 02:14:45 +0000432}
433
Steven Morelandc1635952021-04-01 16:20:47 +0000434TEST_P(BinderRpc, Ping) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000435 auto proc = createRpcTestSocketServerProcess(1);
436 ASSERT_NE(proc.rootBinder, nullptr);
437 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
438}
439
Steven Moreland4cf688f2021-03-31 01:48:58 +0000440TEST_P(BinderRpc, GetInterfaceDescriptor) {
441 auto proc = createRpcTestSocketServerProcess(1);
442 ASSERT_NE(proc.rootBinder, nullptr);
443 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
444}
445
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000446TEST_P(BinderRpc, MultipleSessions) {
447 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*sessions*/);
448 for (auto session : proc.proc.sessions) {
449 ASSERT_NE(nullptr, session.root);
450 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000451 }
452}
453
Steven Morelandc1635952021-04-01 16:20:47 +0000454TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000455 auto proc = createRpcTestSocketServerProcess(1);
456 Parcel data;
457 Parcel reply;
458 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
459}
460
Steven Moreland67753c32021-04-02 18:45:19 +0000461TEST_P(BinderRpc, AppendSeparateFormats) {
462 auto proc = createRpcTestSocketServerProcess(1);
463
464 Parcel p1;
465 p1.markForBinder(proc.rootBinder);
466 p1.writeInt32(3);
467
468 Parcel p2;
469
470 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
471 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
472}
473
Steven Morelandc1635952021-04-01 16:20:47 +0000474TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000475 auto proc = createRpcTestSocketServerProcess(1);
476 Parcel data;
477 data.markForBinder(proc.rootBinder);
478 Parcel reply;
479 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
480}
481
Steven Morelandc1635952021-04-01 16:20:47 +0000482TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000483 auto proc = createRpcTestSocketServerProcess(1);
484 EXPECT_OK(proc.rootIface->sendString("asdf"));
485}
486
Steven Morelandc1635952021-04-01 16:20:47 +0000487TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000488 auto proc = createRpcTestSocketServerProcess(1);
489 std::string doubled;
490 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
491 EXPECT_EQ("cool cool ", doubled);
492}
493
Steven Morelandc1635952021-04-01 16:20:47 +0000494TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000495 auto proc = createRpcTestSocketServerProcess(1);
496 std::string single = std::string(1024, 'a');
497 std::string doubled;
498 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
499 EXPECT_EQ(single + single, doubled);
500}
501
Steven Morelandc1635952021-04-01 16:20:47 +0000502TEST_P(BinderRpc, CallMeBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000503 auto proc = createRpcTestSocketServerProcess(1);
504
505 int32_t pingResult;
506 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
507 EXPECT_EQ(OK, pingResult);
508
509 EXPECT_EQ(0, MyBinderRpcSession::gNum);
510}
511
Steven Morelandc1635952021-04-01 16:20:47 +0000512TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000513 auto proc = createRpcTestSocketServerProcess(1);
514
515 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
516 sp<IBinder> outBinder;
517 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
518 EXPECT_EQ(inBinder, outBinder);
519
520 wp<IBinder> weak = inBinder;
521 inBinder = nullptr;
522 outBinder = nullptr;
523
524 // Force reading a reply, to process any pending dec refs from the other
525 // process (the other process will process dec refs there before processing
526 // the ping here).
527 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
528
529 EXPECT_EQ(nullptr, weak.promote());
530
531 EXPECT_EQ(0, MyBinderRpcSession::gNum);
532}
533
Steven Morelandc1635952021-04-01 16:20:47 +0000534TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000535 auto proc = createRpcTestSocketServerProcess(1);
536
537 sp<IBinderRpcSession> session;
538 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
539
540 sp<IBinder> inBinder = IInterface::asBinder(session);
541 sp<IBinder> outBinder;
542 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
543 EXPECT_EQ(inBinder, outBinder);
544
545 wp<IBinder> weak = inBinder;
546 session = nullptr;
547 inBinder = nullptr;
548 outBinder = nullptr;
549
550 // Force reading a reply, to process any pending dec refs from the other
551 // process (the other process will process dec refs there before processing
552 // the ping here).
553 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
554
555 EXPECT_EQ(nullptr, weak.promote());
556}
557
Steven Morelandc1635952021-04-01 16:20:47 +0000558TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000559 auto proc = createRpcTestSocketServerProcess(1);
560
561 sp<IBinder> outBinder;
562 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
563 EXPECT_EQ(nullptr, outBinder);
564}
565
Steven Morelandc1635952021-04-01 16:20:47 +0000566TEST_P(BinderRpc, HoldBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000567 auto proc = createRpcTestSocketServerProcess(1);
568
569 IBinder* ptr = nullptr;
570 {
571 sp<IBinder> binder = new BBinder();
572 ptr = binder.get();
573 EXPECT_OK(proc.rootIface->holdBinder(binder));
574 }
575
576 sp<IBinder> held;
577 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
578
579 EXPECT_EQ(held.get(), ptr);
580
581 // stop holding binder, because we test to make sure references are cleaned
582 // up
583 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
584 // and flush ref counts
585 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
586}
587
588// START TESTS FOR LIMITATIONS OF SOCKET BINDER
589// These are behavioral differences form regular binder, where certain usecases
590// aren't supported.
591
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000592TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000593 auto proc1 = createRpcTestSocketServerProcess(1);
594 auto proc2 = createRpcTestSocketServerProcess(1);
595
596 sp<IBinder> outBinder;
597 EXPECT_EQ(INVALID_OPERATION,
598 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
599}
600
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000601TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
602 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*sessions*/);
Steven Moreland736664b2021-05-01 04:27:25 +0000603
604 sp<IBinder> outBinder;
605 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000606 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000607 .transactionError());
608}
609
Steven Morelandc1635952021-04-01 16:20:47 +0000610TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000611 auto proc = createRpcTestSocketServerProcess(1);
612
613 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
614 sp<IBinder> outBinder;
615 EXPECT_EQ(INVALID_OPERATION,
616 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
617}
618
Steven Morelandc1635952021-04-01 16:20:47 +0000619TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000620 auto proc = createRpcTestSocketServerProcess(1);
621
622 // for historical reasons, IServiceManager interface only returns the
623 // exception code
624 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
625 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
626}
627
628// END TESTS FOR LIMITATIONS OF SOCKET BINDER
629
Steven Morelandc1635952021-04-01 16:20:47 +0000630TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000631 auto proc = createRpcTestSocketServerProcess(1);
632
633 sp<IBinder> outBinder;
634 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
635 EXPECT_EQ(proc.rootBinder, outBinder);
636}
637
Steven Morelandc1635952021-04-01 16:20:47 +0000638TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000639 auto proc = createRpcTestSocketServerProcess(1);
640
641 auto nastyNester = sp<MyBinderRpcTest>::make();
642 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
643
644 wp<IBinder> weak = nastyNester;
645 nastyNester = nullptr;
646 EXPECT_EQ(nullptr, weak.promote());
647}
648
Steven Morelandc1635952021-04-01 16:20:47 +0000649TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000650 auto proc = createRpcTestSocketServerProcess(1);
651
652 sp<IBinder> a;
653 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
654
655 sp<IBinder> b;
656 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
657
658 EXPECT_EQ(a, b);
659}
660
Steven Morelandc1635952021-04-01 16:20:47 +0000661TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000662 auto proc = createRpcTestSocketServerProcess(1);
663
664 sp<IBinder> a;
665 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
666 wp<IBinder> weak = a;
667 a = nullptr;
668
669 sp<IBinder> b;
670 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
671
672 // this is the wrong behavior, since BpBinder
673 // doesn't implement onIncStrongAttempted
674 // but make sure there is no crash
675 EXPECT_EQ(nullptr, weak.promote());
676
677 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
678
679 // In order to fix this:
680 // - need to have incStrongAttempted reflected across IPC boundary (wait for
681 // response to promote - round trip...)
682 // - sendOnLastWeakRef, to delete entries out of RpcState table
683 EXPECT_EQ(b, weak.promote());
684}
685
686#define expectSessions(expected, iface) \
687 do { \
688 int session; \
689 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
690 EXPECT_EQ(expected, session); \
691 } while (false)
692
Steven Morelandc1635952021-04-01 16:20:47 +0000693TEST_P(BinderRpc, SingleSession) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000694 auto proc = createRpcTestSocketServerProcess(1);
695
696 sp<IBinderRpcSession> session;
697 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
698 std::string out;
699 EXPECT_OK(session->getName(&out));
700 EXPECT_EQ("aoeu", out);
701
702 expectSessions(1, proc.rootIface);
703 session = nullptr;
704 expectSessions(0, proc.rootIface);
705}
706
Steven Morelandc1635952021-04-01 16:20:47 +0000707TEST_P(BinderRpc, ManySessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000708 auto proc = createRpcTestSocketServerProcess(1);
709
710 std::vector<sp<IBinderRpcSession>> sessions;
711
712 for (size_t i = 0; i < 15; i++) {
713 expectSessions(i, proc.rootIface);
714 sp<IBinderRpcSession> session;
715 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
716 sessions.push_back(session);
717 }
718 expectSessions(sessions.size(), proc.rootIface);
719 for (size_t i = 0; i < sessions.size(); i++) {
720 std::string out;
721 EXPECT_OK(sessions.at(i)->getName(&out));
722 EXPECT_EQ(std::to_string(i), out);
723 }
724 expectSessions(sessions.size(), proc.rootIface);
725
726 while (!sessions.empty()) {
727 sessions.pop_back();
728 expectSessions(sessions.size(), proc.rootIface);
729 }
730 expectSessions(0, proc.rootIface);
731}
732
733size_t epochMillis() {
734 using std::chrono::duration_cast;
735 using std::chrono::milliseconds;
736 using std::chrono::seconds;
737 using std::chrono::system_clock;
738 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
739}
740
Steven Morelandc1635952021-04-01 16:20:47 +0000741TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000742 constexpr size_t kNumThreads = 10;
743
744 auto proc = createRpcTestSocketServerProcess(kNumThreads);
745
746 EXPECT_OK(proc.rootIface->lock());
747
748 // block all but one thread taking locks
749 std::vector<std::thread> ts;
750 for (size_t i = 0; i < kNumThreads - 1; i++) {
751 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
752 }
753
754 usleep(100000); // give chance for calls on other threads
755
756 // other calls still work
757 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
758
759 constexpr size_t blockTimeMs = 500;
760 size_t epochMsBefore = epochMillis();
761 // after this, we should never see a response within this time
762 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
763
764 // this call should be blocked for blockTimeMs
765 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
766
767 size_t epochMsAfter = epochMillis();
768 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
769
770 for (auto& t : ts) t.join();
771}
772
Steven Morelandc1635952021-04-01 16:20:47 +0000773TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000774 constexpr size_t kNumThreads = 10;
775 constexpr size_t kNumCalls = kNumThreads + 3;
776 constexpr size_t kSleepMs = 500;
777
778 auto proc = createRpcTestSocketServerProcess(kNumThreads);
779
780 size_t epochMsBefore = epochMillis();
781
782 std::vector<std::thread> ts;
783 for (size_t i = 0; i < kNumCalls; i++) {
784 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
785 }
786
787 for (auto& t : ts) t.join();
788
789 size_t epochMsAfter = epochMillis();
790
791 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
792
793 // Potential flake, but make sure calls are handled in parallel.
794 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
795}
796
Steven Morelandc1635952021-04-01 16:20:47 +0000797TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000798 constexpr size_t kNumClientThreads = 10;
799 constexpr size_t kNumServerThreads = 10;
800 constexpr size_t kNumCalls = 100;
801
802 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
803
804 std::vector<std::thread> threads;
805 for (size_t i = 0; i < kNumClientThreads; i++) {
806 threads.push_back(std::thread([&] {
807 for (size_t j = 0; j < kNumCalls; j++) {
808 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000809 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000810 EXPECT_EQ(proc.rootBinder, out);
811 }
812 }));
813 }
814
815 for (auto& t : threads) t.join();
816}
817
Steven Morelandc6046982021-04-20 00:49:42 +0000818TEST_P(BinderRpc, OnewayStressTest) {
819 constexpr size_t kNumClientThreads = 10;
820 constexpr size_t kNumServerThreads = 10;
821 constexpr size_t kNumCalls = 100;
822
823 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
824
825 std::vector<std::thread> threads;
826 for (size_t i = 0; i < kNumClientThreads; i++) {
827 threads.push_back(std::thread([&] {
828 for (size_t j = 0; j < kNumCalls; j++) {
829 EXPECT_OK(proc.rootIface->sendString("a"));
830 }
831
832 // check threads are not stuck
833 EXPECT_OK(proc.rootIface->sleepMs(250));
834 }));
835 }
836
837 for (auto& t : threads) t.join();
838}
839
Steven Morelandc1635952021-04-01 16:20:47 +0000840TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000841 constexpr size_t kReallyLongTimeMs = 100;
842 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
843
844 // more than one thread, just so this doesn't deadlock
845 auto proc = createRpcTestSocketServerProcess(2);
846
847 size_t epochMsBefore = epochMillis();
848
849 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
850
851 size_t epochMsAfter = epochMillis();
852 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
853}
854
Steven Morelandc1635952021-04-01 16:20:47 +0000855TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000856 constexpr size_t kNumSleeps = 10;
857 constexpr size_t kNumExtraServerThreads = 4;
858 constexpr size_t kSleepMs = 50;
859
860 // make sure calls to the same object happen on the same thread
861 auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads);
862
863 EXPECT_OK(proc.rootIface->lock());
864
865 for (size_t i = 0; i < kNumSleeps; i++) {
866 // these should be processed serially
867 proc.rootIface->sleepMsAsync(kSleepMs);
868 }
869 // should also be processesed serially
870 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
871
872 size_t epochMsBefore = epochMillis();
873 EXPECT_OK(proc.rootIface->lockUnlock());
874 size_t epochMsAfter = epochMillis();
875
876 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
877}
878
Steven Morelandc1635952021-04-01 16:20:47 +0000879TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000880 for (bool doDeathCleanup : {true, false}) {
881 auto proc = createRpcTestSocketServerProcess(1);
882
883 // make sure there is some state during crash
884 // 1. we hold their binder
885 sp<IBinderRpcSession> session;
886 EXPECT_OK(proc.rootIface->openSession("happy", &session));
887 // 2. they hold our binder
888 sp<IBinder> binder = new BBinder();
889 EXPECT_OK(proc.rootIface->holdBinder(binder));
890
891 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
892 << "Do death cleanup: " << doDeathCleanup;
893
Steven Moreland736664b2021-05-01 04:27:25 +0000894 proc.expectInvalid = true;
Steven Moreland5553ac42020-11-11 02:14:45 +0000895 }
896}
897
Steven Morelandd7302072021-05-15 01:32:04 +0000898TEST_P(BinderRpc, UseKernelBinderCallingId) {
899 auto proc = createRpcTestSocketServerProcess(1);
900
901 // we can't allocate IPCThreadState so actually the first time should
902 // succeed :(
903 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
904
905 // second time! we catch the error :)
906 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
907
908 proc.expectInvalid = true;
909}
910
Steven Moreland37aff182021-03-26 02:04:16 +0000911TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
912 auto proc = createRpcTestSocketServerProcess(1);
913
914 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
915 ASSERT_NE(binder, nullptr);
916
917 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
918}
919
920TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
921 auto proc = createRpcTestSocketServerProcess(1);
922
923 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
924 ASSERT_NE(binder, nullptr);
925
926 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
927 ASSERT_NE(ndkBinder, nullptr);
928
929 std::string out;
930 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
931 ASSERT_TRUE(status.isOk()) << status.getDescription();
932 ASSERT_EQ("aoeuaoeu", out);
933}
934
Steven Moreland5553ac42020-11-11 02:14:45 +0000935ssize_t countFds() {
936 DIR* dir = opendir("/proc/self/fd/");
937 if (dir == nullptr) return -1;
938 ssize_t ret = 0;
939 dirent* ent;
940 while ((ent = readdir(dir)) != nullptr) ret++;
941 closedir(dir);
942 return ret;
943}
944
Steven Morelandc1635952021-04-01 16:20:47 +0000945TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000946 ssize_t beforeFds = countFds();
947 ASSERT_GE(beforeFds, 0);
948 {
949 auto proc = createRpcTestSocketServerProcess(10);
950 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
951 }
952 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
953}
954
Steven Morelandc1635952021-04-01 16:20:47 +0000955INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700956 ::testing::ValuesIn({
957 SocketType::UNIX,
Steven Morelandbd5002b2021-05-04 23:12:56 +0000958// TODO(b/185269356): working on host
Steven Morelandf6ec4632021-04-01 16:20:47 +0000959#ifdef __BIONIC__
Yifan Hong0d2bd112021-04-13 17:38:36 -0700960 SocketType::VSOCK,
Steven Morelandbd5002b2021-05-04 23:12:56 +0000961#endif
Yifan Hong0d2bd112021-04-13 17:38:36 -0700962 SocketType::INET,
963 }),
Steven Morelandf6ec4632021-04-01 16:20:47 +0000964 PrintSocketType);
Steven Morelandc1635952021-04-01 16:20:47 +0000965
Yifan Hong4ffb0c72021-05-07 18:35:14 -0700966class BinderRpcServerRootObject : public ::testing::TestWithParam<std::tuple<bool, bool>> {};
967
968TEST_P(BinderRpcServerRootObject, WeakRootObject) {
969 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
970 auto setRootObject = [](bool isStrong) -> SetFn {
971 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
972 };
973
974 auto server = RpcServer::make();
975 auto [isStrong1, isStrong2] = GetParam();
976 auto binder1 = sp<BBinder>::make();
977 IBinder* binderRaw1 = binder1.get();
978 setRootObject(isStrong1)(server.get(), binder1);
979 EXPECT_EQ(binderRaw1, server->getRootObject());
980 binder1.clear();
981 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
982
983 auto binder2 = sp<BBinder>::make();
984 IBinder* binderRaw2 = binder2.get();
985 setRootObject(isStrong2)(server.get(), binder2);
986 EXPECT_EQ(binderRaw2, server->getRootObject());
987 binder2.clear();
988 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
989}
990
991INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
992 ::testing::Combine(::testing::Bool(), ::testing::Bool()));
993
Steven Morelandc1635952021-04-01 16:20:47 +0000994} // namespace android
995
996int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000997 ::testing::InitGoogleTest(&argc, argv);
998 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
999 return RUN_ALL_TESTS();
1000}