blob: 50bff91f71bc552af09e95f66ed5a6d7babc4e6b [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>
26#include <binder/IServiceManager.h>
27#include <binder/ProcessState.h>
28#include <binder/RpcConnection.h>
29#include <binder/RpcServer.h>
30#include <gtest/gtest.h>
31
Steven Morelandc1635952021-04-01 16:20:47 +000032#include <chrono>
33#include <cstdlib>
34#include <iostream>
35#include <thread>
36
Steven Morelandf6ec4632021-04-01 16:20:47 +000037#ifdef __BIONIC__
Steven Morelandc1635952021-04-01 16:20:47 +000038#include <linux/vm_sockets.h>
Steven Morelandf6ec4632021-04-01 16:20:47 +000039#endif //__BIONIC__
40
Steven Morelandc1635952021-04-01 16:20:47 +000041#include <sys/prctl.h>
42#include <unistd.h>
43
Steven Moreland5553ac42020-11-11 02:14:45 +000044#include "../RpcState.h" // for debugging
45
46namespace android {
47
Steven Moreland1fda67b2021-04-02 18:35:50 +000048TEST(BinderRpcParcel, EntireParcelFormatted) {
49 Parcel p;
50 p.writeInt32(3);
51
52 EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), "");
53}
54
Steven Moreland5553ac42020-11-11 02:14:45 +000055using android::binder::Status;
56
57#define EXPECT_OK(status) \
58 do { \
59 Status stat = (status); \
60 EXPECT_TRUE(stat.isOk()) << stat; \
61 } while (false)
62
63class MyBinderRpcSession : public BnBinderRpcSession {
64public:
65 static std::atomic<int32_t> gNum;
66
67 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
68 Status getName(std::string* name) override {
69 *name = mName;
70 return Status::ok();
71 }
72 ~MyBinderRpcSession() { gNum--; }
73
74private:
75 std::string mName;
76};
77std::atomic<int32_t> MyBinderRpcSession::gNum;
78
79class MyBinderRpcTest : public BnBinderRpcTest {
80public:
Steven Moreland611d15f2021-05-01 01:28:27 +000081 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +000082
83 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +000084 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +000085 return Status::ok();
86 }
87 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +000088 *strstr = str + str;
89 return Status::ok();
90 }
Steven Moreland736664b2021-05-01 04:27:25 +000091 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +000092 sp<RpcServer> spServer = server.promote();
93 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +000094 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
95 }
Steven Moreland736664b2021-05-01 04:27:25 +000096 out->clear();
Steven Moreland611d15f2021-05-01 01:28:27 +000097 for (auto connection : spServer->listConnections()) {
Steven Moreland736664b2021-05-01 04:27:25 +000098 size_t count = connection->state()->countBinders();
99 if (count != 1) {
100 // this is called when there is only one binder held remaining,
101 // so to aid debugging
Steven Moreland611d15f2021-05-01 01:28:27 +0000102 connection->state()->dump();
103 }
Steven Moreland736664b2021-05-01 04:27:25 +0000104 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000105 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000106 return Status::ok();
107 }
108 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
109 if (binder == nullptr) {
110 std::cout << "Received null binder!" << std::endl;
111 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
112 }
113 *out = binder->pingBinder();
114 return Status::ok();
115 }
116 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
117 *out = binder;
118 return Status::ok();
119 }
120 static sp<IBinder> mHeldBinder;
121 Status holdBinder(const sp<IBinder>& binder) override {
122 mHeldBinder = binder;
123 return Status::ok();
124 }
125 Status getHeldBinder(sp<IBinder>* held) override {
126 *held = mHeldBinder;
127 return Status::ok();
128 }
129 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
130 if (count <= 0) return Status::ok();
131 return binder->nestMe(this, count - 1);
132 }
133 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
134 static sp<IBinder> binder = new BBinder;
135 *out = binder;
136 return Status::ok();
137 }
138 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
139 *out = new MyBinderRpcSession(name);
140 return Status::ok();
141 }
142 Status getNumOpenSessions(int32_t* out) override {
143 *out = MyBinderRpcSession::gNum;
144 return Status::ok();
145 }
146
147 std::mutex blockMutex;
148 Status lock() override {
149 blockMutex.lock();
150 return Status::ok();
151 }
152 Status unlockInMsAsync(int32_t ms) override {
153 usleep(ms * 1000);
154 blockMutex.unlock();
155 return Status::ok();
156 }
157 Status lockUnlock() override {
158 std::lock_guard<std::mutex> _l(blockMutex);
159 return Status::ok();
160 }
161
162 Status sleepMs(int32_t ms) override {
163 usleep(ms * 1000);
164 return Status::ok();
165 }
166
167 Status sleepMsAsync(int32_t ms) override {
168 // In-process binder calls are asynchronous, but the call to this method
169 // is synchronous wrt its client. This in/out-process threading model
170 // diffentiation is a classic binder leaky abstraction (for better or
171 // worse) and is preserved here the way binder sockets plugs itself
172 // into BpBinder, as nothing is changed at the higher levels
173 // (IInterface) which result in this behavior.
174 return sleepMs(ms);
175 }
176
177 Status die(bool cleanup) override {
178 if (cleanup) {
179 exit(1);
180 } else {
181 _exit(1);
182 }
183 }
184};
185sp<IBinder> MyBinderRpcTest::mHeldBinder;
186
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700187class Pipe {
188public:
189 Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); }
190 Pipe(Pipe&&) = default;
191 android::base::borrowed_fd readEnd() { return mRead; }
192 android::base::borrowed_fd writeEnd() { return mWrite; }
193
194private:
195 android::base::unique_fd mRead;
196 android::base::unique_fd mWrite;
197};
198
Steven Moreland5553ac42020-11-11 02:14:45 +0000199class Process {
200public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700201 Process(Process&&) = default;
202 Process(const std::function<void(Pipe*)>& f) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000203 if (0 == (mPid = fork())) {
204 // racey: assume parent doesn't crash before this is set
205 prctl(PR_SET_PDEATHSIG, SIGHUP);
206
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700207 f(&mPipe);
Steven Moreland5553ac42020-11-11 02:14:45 +0000208 }
209 }
210 ~Process() {
211 if (mPid != 0) {
212 kill(mPid, SIGKILL);
213 }
214 }
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700215 Pipe* getPipe() { return &mPipe; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000216
217private:
218 pid_t mPid = 0;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700219 Pipe mPipe;
Steven Moreland5553ac42020-11-11 02:14:45 +0000220};
221
222static std::string allocateSocketAddress() {
223 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000224 std::string temp = getenv("TMPDIR") ?: "/tmp";
225 return temp + "/binderRpcTest_" + std::to_string(id++);
Steven Moreland5553ac42020-11-11 02:14:45 +0000226};
227
228struct ProcessConnection {
229 // reference to process hosting a socket server
230 Process host;
231
Steven Moreland736664b2021-05-01 04:27:25 +0000232 struct ConnectionInfo {
233 sp<RpcConnection> connection;
234 sp<IBinder> root;
235 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000236
Steven Moreland736664b2021-05-01 04:27:25 +0000237 // client connection objects associated with other process
238 // each one represents a separate connection
239 std::vector<ConnectionInfo> connections;
Steven Moreland5553ac42020-11-11 02:14:45 +0000240
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700241 ProcessConnection(ProcessConnection&&) = default;
Steven Moreland5553ac42020-11-11 02:14:45 +0000242 ~ProcessConnection() {
Steven Moreland736664b2021-05-01 04:27:25 +0000243 for (auto& connection : connections) {
244 connection.root = nullptr;
245 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000246
Steven Moreland736664b2021-05-01 04:27:25 +0000247 for (auto& info : connections) {
248 sp<RpcConnection>& connection = info.connection;
249
250 EXPECT_NE(nullptr, connection);
251 EXPECT_NE(nullptr, connection->state());
252 EXPECT_EQ(0, connection->state()->countBinders())
253 << (connection->state()->dump(), "dump:");
254
255 wp<RpcConnection> weakConnection = connection;
256 connection = nullptr;
257 EXPECT_EQ(nullptr, weakConnection.promote()) << "Leaked connection";
258 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000259 }
260};
261
Steven Moreland5553ac42020-11-11 02:14:45 +0000262// Process connection where the process hosts IBinderRpcTest, the server used
263// for most testing here
264struct BinderRpcTestProcessConnection {
265 ProcessConnection proc;
266
Steven Moreland736664b2021-05-01 04:27:25 +0000267 // pre-fetched root object (for first connection)
Steven Moreland5553ac42020-11-11 02:14:45 +0000268 sp<IBinder> rootBinder;
269
Steven Moreland736664b2021-05-01 04:27:25 +0000270 // pre-casted root object (for first connection)
Steven Moreland5553ac42020-11-11 02:14:45 +0000271 sp<IBinderRpcTest> rootIface;
272
Steven Moreland736664b2021-05-01 04:27:25 +0000273 // whether connection should be invalidated by end of run
274 bool expectInvalid = false;
275
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700276 BinderRpcTestProcessConnection(BinderRpcTestProcessConnection&&) = default;
Steven Moreland5553ac42020-11-11 02:14:45 +0000277 ~BinderRpcTestProcessConnection() {
Steven Moreland736664b2021-05-01 04:27:25 +0000278 if (!expectInvalid) {
279 std::vector<int32_t> remoteCounts;
280 // calling over any connections counts across all connections
281 EXPECT_OK(rootIface->countBinders(&remoteCounts));
282 EXPECT_EQ(remoteCounts.size(), proc.connections.size());
283 for (auto remoteCount : remoteCounts) {
284 EXPECT_EQ(remoteCount, 1);
285 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000286 }
287
288 rootIface = nullptr;
289 rootBinder = nullptr;
290 }
291};
292
Steven Morelandc1635952021-04-01 16:20:47 +0000293enum class SocketType {
294 UNIX,
Steven Morelandf6ec4632021-04-01 16:20:47 +0000295#ifdef __BIONIC__
Steven Morelandc1635952021-04-01 16:20:47 +0000296 VSOCK,
Steven Morelandf6ec4632021-04-01 16:20:47 +0000297#endif // __BIONIC__
Yifan Hong0d2bd112021-04-13 17:38:36 -0700298 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000299};
300static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) {
301 switch (info.param) {
302 case SocketType::UNIX:
303 return "unix_domain_socket";
Steven Morelandf6ec4632021-04-01 16:20:47 +0000304#ifdef __BIONIC__
Steven Morelandc1635952021-04-01 16:20:47 +0000305 case SocketType::VSOCK:
306 return "vm_socket";
Steven Morelandf6ec4632021-04-01 16:20:47 +0000307#endif // __BIONIC__
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.
319 ProcessConnection createRpcTestSocketServerProcess(
Steven Moreland736664b2021-05-01 04:27:25 +0000320 size_t numThreads, size_t numConnections,
321 const std::function<void(const sp<RpcServer>&)>& configure) {
322 CHECK_GE(numConnections, 1) << "Must have at least one connection to a server";
323
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
331 auto ret = ProcessConnection{
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 Morelandc1635952021-04-01 16:20:47 +0000338 switch (socketType) {
339 case SocketType::UNIX:
Steven Moreland611d15f2021-05-01 01:28:27 +0000340 CHECK(server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000341 break;
Steven Morelandf6ec4632021-04-01 16:20:47 +0000342#ifdef __BIONIC__
Steven Morelandc1635952021-04-01 16:20:47 +0000343 case SocketType::VSOCK:
Steven Moreland611d15f2021-05-01 01:28:27 +0000344 CHECK(server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000345 break;
Steven Morelandf6ec4632021-04-01 16:20:47 +0000346#endif // __BIONIC__
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700347 case SocketType::INET: {
348 unsigned int outPort = 0;
Steven Moreland611d15f2021-05-01 01:28:27 +0000349 CHECK(server->setupInetServer(0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700350 CHECK_NE(0, outPort);
351 CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort,
352 sizeof(outPort)));
Yifan Hong0d2bd112021-04-13 17:38:36 -0700353 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700354 }
Steven Morelandc1635952021-04-01 16:20:47 +0000355 default:
356 LOG_ALWAYS_FATAL("Unknown socket type");
357 }
358
Steven Moreland611d15f2021-05-01 01:28:27 +0000359 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000360
Steven Morelandf137de92021-04-24 01:54:26 +0000361 server->join();
Steven Morelandc1635952021-04-01 16:20:47 +0000362 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000363 };
364
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700365 unsigned int inetPort = 0;
366 if (socketType == SocketType::INET) {
367 CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &inetPort,
368 sizeof(inetPort)));
369 CHECK_NE(0, inetPort);
370 }
371
Steven Moreland736664b2021-05-01 04:27:25 +0000372 for (size_t i = 0; i < numConnections; i++) {
373 sp<RpcConnection> connection = RpcConnection::make();
374 for (size_t tries = 0; tries < 10; tries++) {
375 usleep(10000);
376 switch (socketType) {
377 case SocketType::UNIX:
378 if (connection->setupUnixDomainClient(addr.c_str())) goto success;
379 break;
Steven Morelandf6ec4632021-04-01 16:20:47 +0000380#ifdef __BIONIC__
Steven Moreland736664b2021-05-01 04:27:25 +0000381 case SocketType::VSOCK:
382 if (connection->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success;
383 break;
Steven Morelandf6ec4632021-04-01 16:20:47 +0000384#endif // __BIONIC__
Steven Moreland736664b2021-05-01 04:27:25 +0000385 case SocketType::INET:
386 if (connection->setupInetClient("127.0.0.1", inetPort)) goto success;
387 break;
388 default:
389 LOG_ALWAYS_FATAL("Unknown socket type");
390 }
Steven Morelandc1635952021-04-01 16:20:47 +0000391 }
Steven Moreland736664b2021-05-01 04:27:25 +0000392 LOG_ALWAYS_FATAL("Could not connect");
393 success:
394 ret.connections.push_back({connection, connection->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000395 }
Steven Morelandc1635952021-04-01 16:20:47 +0000396 return ret;
397 }
398
Steven Moreland736664b2021-05-01 04:27:25 +0000399 BinderRpcTestProcessConnection createRpcTestSocketServerProcess(size_t numThreads,
400 size_t numConnections = 1) {
Steven Morelandc1635952021-04-01 16:20:47 +0000401 BinderRpcTestProcessConnection ret{
Steven Moreland736664b2021-05-01 04:27:25 +0000402 .proc = createRpcTestSocketServerProcess(numThreads, numConnections,
Steven Moreland611d15f2021-05-01 01:28:27 +0000403 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000404 sp<MyBinderRpcTest> service =
405 new MyBinderRpcTest;
406 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000407 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000408 }),
409 };
410
Steven Moreland736664b2021-05-01 04:27:25 +0000411 ret.rootBinder = ret.proc.connections.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000412 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
413
414 return ret;
415 }
416};
417
418TEST_P(BinderRpc, RootObjectIsNull) {
Steven Moreland736664b2021-05-01 04:27:25 +0000419 auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000420 // this is the default, but to be explicit
421 server->setRootObject(nullptr);
422 });
Steven Moreland5553ac42020-11-11 02:14:45 +0000423
Steven Moreland736664b2021-05-01 04:27:25 +0000424 EXPECT_EQ(nullptr, proc.connections.at(0).root);
Steven Moreland5553ac42020-11-11 02:14:45 +0000425}
426
Steven Morelandc1635952021-04-01 16:20:47 +0000427TEST_P(BinderRpc, Ping) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000428 auto proc = createRpcTestSocketServerProcess(1);
429 ASSERT_NE(proc.rootBinder, nullptr);
430 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
431}
432
Steven Moreland4cf688f2021-03-31 01:48:58 +0000433TEST_P(BinderRpc, GetInterfaceDescriptor) {
434 auto proc = createRpcTestSocketServerProcess(1);
435 ASSERT_NE(proc.rootBinder, nullptr);
436 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
437}
438
Steven Moreland736664b2021-05-01 04:27:25 +0000439TEST_P(BinderRpc, MultipleConnections) {
440 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*connections*/);
441 for (auto connection : proc.proc.connections) {
442 ASSERT_NE(nullptr, connection.root);
443 EXPECT_EQ(OK, connection.root->pingBinder());
444 }
445}
446
Steven Morelandc1635952021-04-01 16:20:47 +0000447TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000448 auto proc = createRpcTestSocketServerProcess(1);
449 Parcel data;
450 Parcel reply;
451 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
452}
453
Steven Moreland67753c32021-04-02 18:45:19 +0000454TEST_P(BinderRpc, AppendSeparateFormats) {
455 auto proc = createRpcTestSocketServerProcess(1);
456
457 Parcel p1;
458 p1.markForBinder(proc.rootBinder);
459 p1.writeInt32(3);
460
461 Parcel p2;
462
463 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
464 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
465}
466
Steven Morelandc1635952021-04-01 16:20:47 +0000467TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000468 auto proc = createRpcTestSocketServerProcess(1);
469 Parcel data;
470 data.markForBinder(proc.rootBinder);
471 Parcel reply;
472 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
473}
474
Steven Morelandc1635952021-04-01 16:20:47 +0000475TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000476 auto proc = createRpcTestSocketServerProcess(1);
477 EXPECT_OK(proc.rootIface->sendString("asdf"));
478}
479
Steven Morelandc1635952021-04-01 16:20:47 +0000480TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000481 auto proc = createRpcTestSocketServerProcess(1);
482 std::string doubled;
483 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
484 EXPECT_EQ("cool cool ", doubled);
485}
486
Steven Morelandc1635952021-04-01 16:20:47 +0000487TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000488 auto proc = createRpcTestSocketServerProcess(1);
489 std::string single = std::string(1024, 'a');
490 std::string doubled;
491 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
492 EXPECT_EQ(single + single, doubled);
493}
494
Steven Morelandc1635952021-04-01 16:20:47 +0000495TEST_P(BinderRpc, CallMeBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000496 auto proc = createRpcTestSocketServerProcess(1);
497
498 int32_t pingResult;
499 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
500 EXPECT_EQ(OK, pingResult);
501
502 EXPECT_EQ(0, MyBinderRpcSession::gNum);
503}
504
Steven Morelandc1635952021-04-01 16:20:47 +0000505TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000506 auto proc = createRpcTestSocketServerProcess(1);
507
508 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
509 sp<IBinder> outBinder;
510 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
511 EXPECT_EQ(inBinder, outBinder);
512
513 wp<IBinder> weak = inBinder;
514 inBinder = nullptr;
515 outBinder = nullptr;
516
517 // Force reading a reply, to process any pending dec refs from the other
518 // process (the other process will process dec refs there before processing
519 // the ping here).
520 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
521
522 EXPECT_EQ(nullptr, weak.promote());
523
524 EXPECT_EQ(0, MyBinderRpcSession::gNum);
525}
526
Steven Morelandc1635952021-04-01 16:20:47 +0000527TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000528 auto proc = createRpcTestSocketServerProcess(1);
529
530 sp<IBinderRpcSession> session;
531 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
532
533 sp<IBinder> inBinder = IInterface::asBinder(session);
534 sp<IBinder> outBinder;
535 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
536 EXPECT_EQ(inBinder, outBinder);
537
538 wp<IBinder> weak = inBinder;
539 session = nullptr;
540 inBinder = nullptr;
541 outBinder = nullptr;
542
543 // Force reading a reply, to process any pending dec refs from the other
544 // process (the other process will process dec refs there before processing
545 // the ping here).
546 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
547
548 EXPECT_EQ(nullptr, weak.promote());
549}
550
Steven Morelandc1635952021-04-01 16:20:47 +0000551TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000552 auto proc = createRpcTestSocketServerProcess(1);
553
554 sp<IBinder> outBinder;
555 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
556 EXPECT_EQ(nullptr, outBinder);
557}
558
Steven Morelandc1635952021-04-01 16:20:47 +0000559TEST_P(BinderRpc, HoldBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000560 auto proc = createRpcTestSocketServerProcess(1);
561
562 IBinder* ptr = nullptr;
563 {
564 sp<IBinder> binder = new BBinder();
565 ptr = binder.get();
566 EXPECT_OK(proc.rootIface->holdBinder(binder));
567 }
568
569 sp<IBinder> held;
570 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
571
572 EXPECT_EQ(held.get(), ptr);
573
574 // stop holding binder, because we test to make sure references are cleaned
575 // up
576 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
577 // and flush ref counts
578 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
579}
580
581// START TESTS FOR LIMITATIONS OF SOCKET BINDER
582// These are behavioral differences form regular binder, where certain usecases
583// aren't supported.
584
Steven Morelandc1635952021-04-01 16:20:47 +0000585TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketConnections) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000586 auto proc1 = createRpcTestSocketServerProcess(1);
587 auto proc2 = createRpcTestSocketServerProcess(1);
588
589 sp<IBinder> outBinder;
590 EXPECT_EQ(INVALID_OPERATION,
591 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
592}
593
Steven Moreland736664b2021-05-01 04:27:25 +0000594TEST_P(BinderRpc, CannotMixBindersBetweenTwoConnectionsToTheSameServer) {
595 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*connections*/);
596
597 sp<IBinder> outBinder;
598 EXPECT_EQ(INVALID_OPERATION,
599 proc.rootIface->repeatBinder(proc.proc.connections.at(1).root, &outBinder)
600 .transactionError());
601}
602
Steven Morelandc1635952021-04-01 16:20:47 +0000603TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000604 auto proc = createRpcTestSocketServerProcess(1);
605
606 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
607 sp<IBinder> outBinder;
608 EXPECT_EQ(INVALID_OPERATION,
609 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
610}
611
Steven Morelandc1635952021-04-01 16:20:47 +0000612TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000613 auto proc = createRpcTestSocketServerProcess(1);
614
615 // for historical reasons, IServiceManager interface only returns the
616 // exception code
617 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
618 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
619}
620
621// END TESTS FOR LIMITATIONS OF SOCKET BINDER
622
Steven Morelandc1635952021-04-01 16:20:47 +0000623TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000624 auto proc = createRpcTestSocketServerProcess(1);
625
626 sp<IBinder> outBinder;
627 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
628 EXPECT_EQ(proc.rootBinder, outBinder);
629}
630
Steven Morelandc1635952021-04-01 16:20:47 +0000631TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000632 auto proc = createRpcTestSocketServerProcess(1);
633
634 auto nastyNester = sp<MyBinderRpcTest>::make();
635 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
636
637 wp<IBinder> weak = nastyNester;
638 nastyNester = nullptr;
639 EXPECT_EQ(nullptr, weak.promote());
640}
641
Steven Morelandc1635952021-04-01 16:20:47 +0000642TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000643 auto proc = createRpcTestSocketServerProcess(1);
644
645 sp<IBinder> a;
646 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
647
648 sp<IBinder> b;
649 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
650
651 EXPECT_EQ(a, b);
652}
653
Steven Morelandc1635952021-04-01 16:20:47 +0000654TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000655 auto proc = createRpcTestSocketServerProcess(1);
656
657 sp<IBinder> a;
658 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
659 wp<IBinder> weak = a;
660 a = nullptr;
661
662 sp<IBinder> b;
663 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
664
665 // this is the wrong behavior, since BpBinder
666 // doesn't implement onIncStrongAttempted
667 // but make sure there is no crash
668 EXPECT_EQ(nullptr, weak.promote());
669
670 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
671
672 // In order to fix this:
673 // - need to have incStrongAttempted reflected across IPC boundary (wait for
674 // response to promote - round trip...)
675 // - sendOnLastWeakRef, to delete entries out of RpcState table
676 EXPECT_EQ(b, weak.promote());
677}
678
679#define expectSessions(expected, iface) \
680 do { \
681 int session; \
682 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
683 EXPECT_EQ(expected, session); \
684 } while (false)
685
Steven Morelandc1635952021-04-01 16:20:47 +0000686TEST_P(BinderRpc, SingleSession) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000687 auto proc = createRpcTestSocketServerProcess(1);
688
689 sp<IBinderRpcSession> session;
690 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
691 std::string out;
692 EXPECT_OK(session->getName(&out));
693 EXPECT_EQ("aoeu", out);
694
695 expectSessions(1, proc.rootIface);
696 session = nullptr;
697 expectSessions(0, proc.rootIface);
698}
699
Steven Morelandc1635952021-04-01 16:20:47 +0000700TEST_P(BinderRpc, ManySessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000701 auto proc = createRpcTestSocketServerProcess(1);
702
703 std::vector<sp<IBinderRpcSession>> sessions;
704
705 for (size_t i = 0; i < 15; i++) {
706 expectSessions(i, proc.rootIface);
707 sp<IBinderRpcSession> session;
708 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
709 sessions.push_back(session);
710 }
711 expectSessions(sessions.size(), proc.rootIface);
712 for (size_t i = 0; i < sessions.size(); i++) {
713 std::string out;
714 EXPECT_OK(sessions.at(i)->getName(&out));
715 EXPECT_EQ(std::to_string(i), out);
716 }
717 expectSessions(sessions.size(), proc.rootIface);
718
719 while (!sessions.empty()) {
720 sessions.pop_back();
721 expectSessions(sessions.size(), proc.rootIface);
722 }
723 expectSessions(0, proc.rootIface);
724}
725
726size_t epochMillis() {
727 using std::chrono::duration_cast;
728 using std::chrono::milliseconds;
729 using std::chrono::seconds;
730 using std::chrono::system_clock;
731 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
732}
733
Steven Morelandc1635952021-04-01 16:20:47 +0000734TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000735 constexpr size_t kNumThreads = 10;
736
737 auto proc = createRpcTestSocketServerProcess(kNumThreads);
738
739 EXPECT_OK(proc.rootIface->lock());
740
741 // block all but one thread taking locks
742 std::vector<std::thread> ts;
743 for (size_t i = 0; i < kNumThreads - 1; i++) {
744 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
745 }
746
747 usleep(100000); // give chance for calls on other threads
748
749 // other calls still work
750 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
751
752 constexpr size_t blockTimeMs = 500;
753 size_t epochMsBefore = epochMillis();
754 // after this, we should never see a response within this time
755 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
756
757 // this call should be blocked for blockTimeMs
758 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
759
760 size_t epochMsAfter = epochMillis();
761 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
762
763 for (auto& t : ts) t.join();
764}
765
Steven Morelandc1635952021-04-01 16:20:47 +0000766TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000767 constexpr size_t kNumThreads = 10;
768 constexpr size_t kNumCalls = kNumThreads + 3;
769 constexpr size_t kSleepMs = 500;
770
771 auto proc = createRpcTestSocketServerProcess(kNumThreads);
772
773 size_t epochMsBefore = epochMillis();
774
775 std::vector<std::thread> ts;
776 for (size_t i = 0; i < kNumCalls; i++) {
777 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
778 }
779
780 for (auto& t : ts) t.join();
781
782 size_t epochMsAfter = epochMillis();
783
784 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
785
786 // Potential flake, but make sure calls are handled in parallel.
787 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
788}
789
Steven Morelandc1635952021-04-01 16:20:47 +0000790TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000791 constexpr size_t kNumClientThreads = 10;
792 constexpr size_t kNumServerThreads = 10;
793 constexpr size_t kNumCalls = 100;
794
795 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
796
797 std::vector<std::thread> threads;
798 for (size_t i = 0; i < kNumClientThreads; i++) {
799 threads.push_back(std::thread([&] {
800 for (size_t j = 0; j < kNumCalls; j++) {
801 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000802 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000803 EXPECT_EQ(proc.rootBinder, out);
804 }
805 }));
806 }
807
808 for (auto& t : threads) t.join();
809}
810
Steven Morelandc6046982021-04-20 00:49:42 +0000811TEST_P(BinderRpc, OnewayStressTest) {
812 constexpr size_t kNumClientThreads = 10;
813 constexpr size_t kNumServerThreads = 10;
814 constexpr size_t kNumCalls = 100;
815
816 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
817
818 std::vector<std::thread> threads;
819 for (size_t i = 0; i < kNumClientThreads; i++) {
820 threads.push_back(std::thread([&] {
821 for (size_t j = 0; j < kNumCalls; j++) {
822 EXPECT_OK(proc.rootIface->sendString("a"));
823 }
824
825 // check threads are not stuck
826 EXPECT_OK(proc.rootIface->sleepMs(250));
827 }));
828 }
829
830 for (auto& t : threads) t.join();
831}
832
Steven Morelandc1635952021-04-01 16:20:47 +0000833TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000834 constexpr size_t kReallyLongTimeMs = 100;
835 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
836
837 // more than one thread, just so this doesn't deadlock
838 auto proc = createRpcTestSocketServerProcess(2);
839
840 size_t epochMsBefore = epochMillis();
841
842 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
843
844 size_t epochMsAfter = epochMillis();
845 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
846}
847
Steven Morelandc1635952021-04-01 16:20:47 +0000848TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000849 constexpr size_t kNumSleeps = 10;
850 constexpr size_t kNumExtraServerThreads = 4;
851 constexpr size_t kSleepMs = 50;
852
853 // make sure calls to the same object happen on the same thread
854 auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads);
855
856 EXPECT_OK(proc.rootIface->lock());
857
858 for (size_t i = 0; i < kNumSleeps; i++) {
859 // these should be processed serially
860 proc.rootIface->sleepMsAsync(kSleepMs);
861 }
862 // should also be processesed serially
863 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
864
865 size_t epochMsBefore = epochMillis();
866 EXPECT_OK(proc.rootIface->lockUnlock());
867 size_t epochMsAfter = epochMillis();
868
869 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
870}
871
Steven Morelandc1635952021-04-01 16:20:47 +0000872TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000873 for (bool doDeathCleanup : {true, false}) {
874 auto proc = createRpcTestSocketServerProcess(1);
875
876 // make sure there is some state during crash
877 // 1. we hold their binder
878 sp<IBinderRpcSession> session;
879 EXPECT_OK(proc.rootIface->openSession("happy", &session));
880 // 2. they hold our binder
881 sp<IBinder> binder = new BBinder();
882 EXPECT_OK(proc.rootIface->holdBinder(binder));
883
884 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
885 << "Do death cleanup: " << doDeathCleanup;
886
Steven Moreland736664b2021-05-01 04:27:25 +0000887 proc.expectInvalid = true;
Steven Moreland5553ac42020-11-11 02:14:45 +0000888 }
889}
890
Steven Moreland37aff182021-03-26 02:04:16 +0000891TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
892 auto proc = createRpcTestSocketServerProcess(1);
893
894 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
895 ASSERT_NE(binder, nullptr);
896
897 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
898}
899
900TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
901 auto proc = createRpcTestSocketServerProcess(1);
902
903 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
904 ASSERT_NE(binder, nullptr);
905
906 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
907 ASSERT_NE(ndkBinder, nullptr);
908
909 std::string out;
910 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
911 ASSERT_TRUE(status.isOk()) << status.getDescription();
912 ASSERT_EQ("aoeuaoeu", out);
913}
914
Steven Moreland5553ac42020-11-11 02:14:45 +0000915ssize_t countFds() {
916 DIR* dir = opendir("/proc/self/fd/");
917 if (dir == nullptr) return -1;
918 ssize_t ret = 0;
919 dirent* ent;
920 while ((ent = readdir(dir)) != nullptr) ret++;
921 closedir(dir);
922 return ret;
923}
924
Steven Morelandc1635952021-04-01 16:20:47 +0000925TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000926 ssize_t beforeFds = countFds();
927 ASSERT_GE(beforeFds, 0);
928 {
929 auto proc = createRpcTestSocketServerProcess(10);
930 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
931 }
932 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
933}
934
Steven Morelandc1635952021-04-01 16:20:47 +0000935INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700936 ::testing::ValuesIn({
937 SocketType::UNIX,
Steven Morelandf6ec4632021-04-01 16:20:47 +0000938#ifdef __BIONIC__
Yifan Hong0d2bd112021-04-13 17:38:36 -0700939 SocketType::VSOCK,
Steven Morelandf6ec4632021-04-01 16:20:47 +0000940#endif // __BIONIC__
Yifan Hong0d2bd112021-04-13 17:38:36 -0700941 SocketType::INET,
942 }),
Steven Morelandf6ec4632021-04-01 16:20:47 +0000943 PrintSocketType);
Steven Morelandc1635952021-04-01 16:20:47 +0000944
945} // namespace android
946
947int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000948 ::testing::InitGoogleTest(&argc, argv);
949 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
950 return RUN_ALL_TESTS();
951}