blob: db7f67da5ae8c5535e1481330e03e00cc1646923 [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>
Steven Moreland5553ac42020-11-11 02:14:45 +000028#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000029#include <binder/RpcSession.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000030#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 Morelandc1635952021-04-01 16:20:47 +000037#include <sys/prctl.h>
38#include <unistd.h>
39
Steven Morelandbd5002b2021-05-04 23:12:56 +000040#include "../RpcState.h" // for debugging
41#include "../vm_sockets.h" // for VMADDR_*
Steven Moreland5553ac42020-11-11 02:14:45 +000042
43namespace android {
44
Steven Moreland1fda67b2021-04-02 18:35:50 +000045TEST(BinderRpcParcel, EntireParcelFormatted) {
46 Parcel p;
47 p.writeInt32(3);
48
49 EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), "");
50}
51
Steven Moreland5553ac42020-11-11 02:14:45 +000052using android::binder::Status;
53
54#define EXPECT_OK(status) \
55 do { \
56 Status stat = (status); \
57 EXPECT_TRUE(stat.isOk()) << stat; \
58 } while (false)
59
60class MyBinderRpcSession : public BnBinderRpcSession {
61public:
62 static std::atomic<int32_t> gNum;
63
64 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
65 Status getName(std::string* name) override {
66 *name = mName;
67 return Status::ok();
68 }
69 ~MyBinderRpcSession() { gNum--; }
70
71private:
72 std::string mName;
73};
74std::atomic<int32_t> MyBinderRpcSession::gNum;
75
76class MyBinderRpcTest : public BnBinderRpcTest {
77public:
Steven Moreland611d15f2021-05-01 01:28:27 +000078 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +000079
80 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +000081 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +000082 return Status::ok();
83 }
84 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +000085 *strstr = str + str;
86 return Status::ok();
87 }
Steven Moreland736664b2021-05-01 04:27:25 +000088 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +000089 sp<RpcServer> spServer = server.promote();
90 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +000091 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
92 }
Steven Moreland736664b2021-05-01 04:27:25 +000093 out->clear();
Steven Morelandbdb53ab2021-05-05 17:57:41 +000094 for (auto session : spServer->listSessions()) {
95 size_t count = session->state()->countBinders();
Steven Moreland736664b2021-05-01 04:27:25 +000096 if (count != 1) {
97 // this is called when there is only one binder held remaining,
98 // so to aid debugging
Steven Morelandbdb53ab2021-05-05 17:57:41 +000099 session->state()->dump();
Steven Moreland611d15f2021-05-01 01:28:27 +0000100 }
Steven Moreland736664b2021-05-01 04:27:25 +0000101 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000102 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000103 return Status::ok();
104 }
105 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
106 if (binder == nullptr) {
107 std::cout << "Received null binder!" << std::endl;
108 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
109 }
110 *out = binder->pingBinder();
111 return Status::ok();
112 }
113 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
114 *out = binder;
115 return Status::ok();
116 }
117 static sp<IBinder> mHeldBinder;
118 Status holdBinder(const sp<IBinder>& binder) override {
119 mHeldBinder = binder;
120 return Status::ok();
121 }
122 Status getHeldBinder(sp<IBinder>* held) override {
123 *held = mHeldBinder;
124 return Status::ok();
125 }
126 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
127 if (count <= 0) return Status::ok();
128 return binder->nestMe(this, count - 1);
129 }
130 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
131 static sp<IBinder> binder = new BBinder;
132 *out = binder;
133 return Status::ok();
134 }
135 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
136 *out = new MyBinderRpcSession(name);
137 return Status::ok();
138 }
139 Status getNumOpenSessions(int32_t* out) override {
140 *out = MyBinderRpcSession::gNum;
141 return Status::ok();
142 }
143
144 std::mutex blockMutex;
145 Status lock() override {
146 blockMutex.lock();
147 return Status::ok();
148 }
149 Status unlockInMsAsync(int32_t ms) override {
150 usleep(ms * 1000);
151 blockMutex.unlock();
152 return Status::ok();
153 }
154 Status lockUnlock() override {
155 std::lock_guard<std::mutex> _l(blockMutex);
156 return Status::ok();
157 }
158
159 Status sleepMs(int32_t ms) override {
160 usleep(ms * 1000);
161 return Status::ok();
162 }
163
164 Status sleepMsAsync(int32_t ms) override {
165 // In-process binder calls are asynchronous, but the call to this method
166 // is synchronous wrt its client. This in/out-process threading model
167 // diffentiation is a classic binder leaky abstraction (for better or
168 // worse) and is preserved here the way binder sockets plugs itself
169 // into BpBinder, as nothing is changed at the higher levels
170 // (IInterface) which result in this behavior.
171 return sleepMs(ms);
172 }
173
174 Status die(bool cleanup) override {
175 if (cleanup) {
176 exit(1);
177 } else {
178 _exit(1);
179 }
180 }
181};
182sp<IBinder> MyBinderRpcTest::mHeldBinder;
183
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700184class Pipe {
185public:
186 Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); }
187 Pipe(Pipe&&) = default;
188 android::base::borrowed_fd readEnd() { return mRead; }
189 android::base::borrowed_fd writeEnd() { return mWrite; }
190
191private:
192 android::base::unique_fd mRead;
193 android::base::unique_fd mWrite;
194};
195
Steven Moreland5553ac42020-11-11 02:14:45 +0000196class Process {
197public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700198 Process(Process&&) = default;
199 Process(const std::function<void(Pipe*)>& f) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000200 if (0 == (mPid = fork())) {
201 // racey: assume parent doesn't crash before this is set
202 prctl(PR_SET_PDEATHSIG, SIGHUP);
203
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700204 f(&mPipe);
Steven Moreland5553ac42020-11-11 02:14:45 +0000205 }
206 }
207 ~Process() {
208 if (mPid != 0) {
209 kill(mPid, SIGKILL);
210 }
211 }
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700212 Pipe* getPipe() { return &mPipe; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000213
214private:
215 pid_t mPid = 0;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700216 Pipe mPipe;
Steven Moreland5553ac42020-11-11 02:14:45 +0000217};
218
219static std::string allocateSocketAddress() {
220 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000221 std::string temp = getenv("TMPDIR") ?: "/tmp";
222 return temp + "/binderRpcTest_" + std::to_string(id++);
Steven Moreland5553ac42020-11-11 02:14:45 +0000223};
224
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000225struct ProcessSession {
Steven Moreland5553ac42020-11-11 02:14:45 +0000226 // reference to process hosting a socket server
227 Process host;
228
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000229 struct SessionInfo {
230 sp<RpcSession> session;
Steven Moreland736664b2021-05-01 04:27:25 +0000231 sp<IBinder> root;
232 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000233
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000234 // client session objects associated with other process
235 // each one represents a separate session
236 std::vector<SessionInfo> sessions;
Steven Moreland5553ac42020-11-11 02:14:45 +0000237
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000238 ProcessSession(ProcessSession&&) = default;
239 ~ProcessSession() {
240 for (auto& session : sessions) {
241 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000242 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000243
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000244 for (auto& info : sessions) {
245 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000246
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000247 EXPECT_NE(nullptr, session);
248 EXPECT_NE(nullptr, session->state());
249 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000250
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000251 wp<RpcSession> weakSession = session;
252 session = nullptr;
253 EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session";
Steven Moreland736664b2021-05-01 04:27:25 +0000254 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000255 }
256};
257
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000258// Process session where the process hosts IBinderRpcTest, the server used
Steven Moreland5553ac42020-11-11 02:14:45 +0000259// for most testing here
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000260struct BinderRpcTestProcessSession {
261 ProcessSession proc;
Steven Moreland5553ac42020-11-11 02:14:45 +0000262
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000263 // pre-fetched root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000264 sp<IBinder> rootBinder;
265
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000266 // pre-casted root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000267 sp<IBinderRpcTest> rootIface;
268
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000269 // whether session should be invalidated by end of run
Steven Moreland736664b2021-05-01 04:27:25 +0000270 bool expectInvalid = false;
271
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000272 BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default;
273 ~BinderRpcTestProcessSession() {
Steven Moreland736664b2021-05-01 04:27:25 +0000274 if (!expectInvalid) {
275 std::vector<int32_t> remoteCounts;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000276 // calling over any sessions counts across all sessions
Steven Moreland736664b2021-05-01 04:27:25 +0000277 EXPECT_OK(rootIface->countBinders(&remoteCounts));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000278 EXPECT_EQ(remoteCounts.size(), proc.sessions.size());
Steven Moreland736664b2021-05-01 04:27:25 +0000279 for (auto remoteCount : remoteCounts) {
280 EXPECT_EQ(remoteCount, 1);
281 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000282 }
283
284 rootIface = nullptr;
285 rootBinder = nullptr;
286 }
287};
288
Steven Morelandc1635952021-04-01 16:20:47 +0000289enum class SocketType {
290 UNIX,
291 VSOCK,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700292 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000293};
294static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) {
295 switch (info.param) {
296 case SocketType::UNIX:
297 return "unix_domain_socket";
298 case SocketType::VSOCK:
299 return "vm_socket";
Yifan Hong0d2bd112021-04-13 17:38:36 -0700300 case SocketType::INET:
301 return "inet_socket";
Steven Morelandc1635952021-04-01 16:20:47 +0000302 default:
303 LOG_ALWAYS_FATAL("Unknown socket type");
304 return "";
305 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000306}
Steven Morelandc1635952021-04-01 16:20:47 +0000307class BinderRpc : public ::testing::TestWithParam<SocketType> {
308public:
309 // This creates a new process serving an interface on a certain number of
310 // threads.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000311 ProcessSession createRpcTestSocketServerProcess(
312 size_t numThreads, size_t numSessions,
Steven Moreland736664b2021-05-01 04:27:25 +0000313 const std::function<void(const sp<RpcServer>&)>& configure) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000314 CHECK_GE(numSessions, 1) << "Must have at least one session to a server";
Steven Moreland736664b2021-05-01 04:27:25 +0000315
Steven Morelandc1635952021-04-01 16:20:47 +0000316 SocketType socketType = GetParam();
317
318 std::string addr = allocateSocketAddress();
319 unlink(addr.c_str());
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700320 static unsigned int vsockPort = 3456;
321 vsockPort++;
Steven Morelandc1635952021-04-01 16:20:47 +0000322
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000323 auto ret = ProcessSession{
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700324 .host = Process([&](Pipe* pipe) {
Steven Morelandc1635952021-04-01 16:20:47 +0000325 sp<RpcServer> server = RpcServer::make();
326
327 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Morelandf137de92021-04-24 01:54:26 +0000328 server->setMaxThreads(numThreads);
Steven Morelandc1635952021-04-01 16:20:47 +0000329
Steven Morelandc1635952021-04-01 16:20:47 +0000330 switch (socketType) {
331 case SocketType::UNIX:
Steven Moreland611d15f2021-05-01 01:28:27 +0000332 CHECK(server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000333 break;
334 case SocketType::VSOCK:
Steven Moreland611d15f2021-05-01 01:28:27 +0000335 CHECK(server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000336 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700337 case SocketType::INET: {
338 unsigned int outPort = 0;
Steven Moreland611d15f2021-05-01 01:28:27 +0000339 CHECK(server->setupInetServer(0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700340 CHECK_NE(0, outPort);
341 CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort,
342 sizeof(outPort)));
Yifan Hong0d2bd112021-04-13 17:38:36 -0700343 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700344 }
Steven Morelandc1635952021-04-01 16:20:47 +0000345 default:
346 LOG_ALWAYS_FATAL("Unknown socket type");
347 }
348
Steven Moreland611d15f2021-05-01 01:28:27 +0000349 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000350
Steven Morelandf137de92021-04-24 01:54:26 +0000351 server->join();
Steven Morelandc1635952021-04-01 16:20:47 +0000352 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000353 };
354
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700355 unsigned int inetPort = 0;
356 if (socketType == SocketType::INET) {
357 CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &inetPort,
358 sizeof(inetPort)));
359 CHECK_NE(0, inetPort);
360 }
361
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000362 for (size_t i = 0; i < numSessions; i++) {
363 sp<RpcSession> session = RpcSession::make();
Steven Moreland736664b2021-05-01 04:27:25 +0000364 for (size_t tries = 0; tries < 10; tries++) {
365 usleep(10000);
366 switch (socketType) {
367 case SocketType::UNIX:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000368 if (session->setupUnixDomainClient(addr.c_str())) goto success;
Steven Moreland736664b2021-05-01 04:27:25 +0000369 break;
Steven Moreland736664b2021-05-01 04:27:25 +0000370 case SocketType::VSOCK:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000371 if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success;
Steven Moreland736664b2021-05-01 04:27:25 +0000372 break;
Steven Moreland736664b2021-05-01 04:27:25 +0000373 case SocketType::INET:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000374 if (session->setupInetClient("127.0.0.1", inetPort)) goto success;
Steven Moreland736664b2021-05-01 04:27:25 +0000375 break;
376 default:
377 LOG_ALWAYS_FATAL("Unknown socket type");
378 }
Steven Morelandc1635952021-04-01 16:20:47 +0000379 }
Steven Moreland736664b2021-05-01 04:27:25 +0000380 LOG_ALWAYS_FATAL("Could not connect");
381 success:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000382 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000383 }
Steven Morelandc1635952021-04-01 16:20:47 +0000384 return ret;
385 }
386
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000387 BinderRpcTestProcessSession createRpcTestSocketServerProcess(size_t numThreads,
388 size_t numSessions = 1) {
389 BinderRpcTestProcessSession ret{
390 .proc = createRpcTestSocketServerProcess(numThreads, numSessions,
Steven Moreland611d15f2021-05-01 01:28:27 +0000391 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000392 sp<MyBinderRpcTest> service =
393 new MyBinderRpcTest;
394 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000395 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000396 }),
397 };
398
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000399 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000400 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
401
402 return ret;
403 }
404};
405
406TEST_P(BinderRpc, RootObjectIsNull) {
Steven Moreland736664b2021-05-01 04:27:25 +0000407 auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) {
Steven Moreland611d15f2021-05-01 01:28:27 +0000408 // this is the default, but to be explicit
409 server->setRootObject(nullptr);
410 });
Steven Moreland5553ac42020-11-11 02:14:45 +0000411
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000412 EXPECT_EQ(nullptr, proc.sessions.at(0).root);
Steven Moreland5553ac42020-11-11 02:14:45 +0000413}
414
Steven Morelandc1635952021-04-01 16:20:47 +0000415TEST_P(BinderRpc, Ping) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000416 auto proc = createRpcTestSocketServerProcess(1);
417 ASSERT_NE(proc.rootBinder, nullptr);
418 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
419}
420
Steven Moreland4cf688f2021-03-31 01:48:58 +0000421TEST_P(BinderRpc, GetInterfaceDescriptor) {
422 auto proc = createRpcTestSocketServerProcess(1);
423 ASSERT_NE(proc.rootBinder, nullptr);
424 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
425}
426
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000427TEST_P(BinderRpc, MultipleSessions) {
428 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*sessions*/);
429 for (auto session : proc.proc.sessions) {
430 ASSERT_NE(nullptr, session.root);
431 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000432 }
433}
434
Steven Morelandc1635952021-04-01 16:20:47 +0000435TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000436 auto proc = createRpcTestSocketServerProcess(1);
437 Parcel data;
438 Parcel reply;
439 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
440}
441
Steven Moreland67753c32021-04-02 18:45:19 +0000442TEST_P(BinderRpc, AppendSeparateFormats) {
443 auto proc = createRpcTestSocketServerProcess(1);
444
445 Parcel p1;
446 p1.markForBinder(proc.rootBinder);
447 p1.writeInt32(3);
448
449 Parcel p2;
450
451 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
452 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
453}
454
Steven Morelandc1635952021-04-01 16:20:47 +0000455TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000456 auto proc = createRpcTestSocketServerProcess(1);
457 Parcel data;
458 data.markForBinder(proc.rootBinder);
459 Parcel reply;
460 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
461}
462
Steven Morelandc1635952021-04-01 16:20:47 +0000463TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000464 auto proc = createRpcTestSocketServerProcess(1);
465 EXPECT_OK(proc.rootIface->sendString("asdf"));
466}
467
Steven Morelandc1635952021-04-01 16:20:47 +0000468TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000469 auto proc = createRpcTestSocketServerProcess(1);
470 std::string doubled;
471 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
472 EXPECT_EQ("cool cool ", doubled);
473}
474
Steven Morelandc1635952021-04-01 16:20:47 +0000475TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000476 auto proc = createRpcTestSocketServerProcess(1);
477 std::string single = std::string(1024, 'a');
478 std::string doubled;
479 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
480 EXPECT_EQ(single + single, doubled);
481}
482
Steven Morelandc1635952021-04-01 16:20:47 +0000483TEST_P(BinderRpc, CallMeBack) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000484 auto proc = createRpcTestSocketServerProcess(1);
485
486 int32_t pingResult;
487 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
488 EXPECT_EQ(OK, pingResult);
489
490 EXPECT_EQ(0, MyBinderRpcSession::gNum);
491}
492
Steven Morelandc1635952021-04-01 16:20:47 +0000493TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000494 auto proc = createRpcTestSocketServerProcess(1);
495
496 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
497 sp<IBinder> outBinder;
498 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
499 EXPECT_EQ(inBinder, outBinder);
500
501 wp<IBinder> weak = inBinder;
502 inBinder = nullptr;
503 outBinder = nullptr;
504
505 // Force reading a reply, to process any pending dec refs from the other
506 // process (the other process will process dec refs there before processing
507 // the ping here).
508 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
509
510 EXPECT_EQ(nullptr, weak.promote());
511
512 EXPECT_EQ(0, MyBinderRpcSession::gNum);
513}
514
Steven Morelandc1635952021-04-01 16:20:47 +0000515TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000516 auto proc = createRpcTestSocketServerProcess(1);
517
518 sp<IBinderRpcSession> session;
519 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
520
521 sp<IBinder> inBinder = IInterface::asBinder(session);
522 sp<IBinder> outBinder;
523 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
524 EXPECT_EQ(inBinder, outBinder);
525
526 wp<IBinder> weak = inBinder;
527 session = nullptr;
528 inBinder = nullptr;
529 outBinder = nullptr;
530
531 // Force reading a reply, to process any pending dec refs from the other
532 // process (the other process will process dec refs there before processing
533 // the ping here).
534 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
535
536 EXPECT_EQ(nullptr, weak.promote());
537}
538
Steven Morelandc1635952021-04-01 16:20:47 +0000539TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000540 auto proc = createRpcTestSocketServerProcess(1);
541
542 sp<IBinder> outBinder;
543 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
544 EXPECT_EQ(nullptr, outBinder);
545}
546
Steven Morelandc1635952021-04-01 16:20:47 +0000547TEST_P(BinderRpc, HoldBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000548 auto proc = createRpcTestSocketServerProcess(1);
549
550 IBinder* ptr = nullptr;
551 {
552 sp<IBinder> binder = new BBinder();
553 ptr = binder.get();
554 EXPECT_OK(proc.rootIface->holdBinder(binder));
555 }
556
557 sp<IBinder> held;
558 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
559
560 EXPECT_EQ(held.get(), ptr);
561
562 // stop holding binder, because we test to make sure references are cleaned
563 // up
564 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
565 // and flush ref counts
566 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
567}
568
569// START TESTS FOR LIMITATIONS OF SOCKET BINDER
570// These are behavioral differences form regular binder, where certain usecases
571// aren't supported.
572
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000573TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000574 auto proc1 = createRpcTestSocketServerProcess(1);
575 auto proc2 = createRpcTestSocketServerProcess(1);
576
577 sp<IBinder> outBinder;
578 EXPECT_EQ(INVALID_OPERATION,
579 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
580}
581
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000582TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
583 auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*sessions*/);
Steven Moreland736664b2021-05-01 04:27:25 +0000584
585 sp<IBinder> outBinder;
586 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000587 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000588 .transactionError());
589}
590
Steven Morelandc1635952021-04-01 16:20:47 +0000591TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000592 auto proc = createRpcTestSocketServerProcess(1);
593
594 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
595 sp<IBinder> outBinder;
596 EXPECT_EQ(INVALID_OPERATION,
597 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
598}
599
Steven Morelandc1635952021-04-01 16:20:47 +0000600TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000601 auto proc = createRpcTestSocketServerProcess(1);
602
603 // for historical reasons, IServiceManager interface only returns the
604 // exception code
605 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
606 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
607}
608
609// END TESTS FOR LIMITATIONS OF SOCKET BINDER
610
Steven Morelandc1635952021-04-01 16:20:47 +0000611TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000612 auto proc = createRpcTestSocketServerProcess(1);
613
614 sp<IBinder> outBinder;
615 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
616 EXPECT_EQ(proc.rootBinder, outBinder);
617}
618
Steven Morelandc1635952021-04-01 16:20:47 +0000619TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000620 auto proc = createRpcTestSocketServerProcess(1);
621
622 auto nastyNester = sp<MyBinderRpcTest>::make();
623 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
624
625 wp<IBinder> weak = nastyNester;
626 nastyNester = nullptr;
627 EXPECT_EQ(nullptr, weak.promote());
628}
629
Steven Morelandc1635952021-04-01 16:20:47 +0000630TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000631 auto proc = createRpcTestSocketServerProcess(1);
632
633 sp<IBinder> a;
634 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
635
636 sp<IBinder> b;
637 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
638
639 EXPECT_EQ(a, b);
640}
641
Steven Morelandc1635952021-04-01 16:20:47 +0000642TEST_P(BinderRpc, SameBinderEqualityWeak) {
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 wp<IBinder> weak = a;
648 a = nullptr;
649
650 sp<IBinder> b;
651 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
652
653 // this is the wrong behavior, since BpBinder
654 // doesn't implement onIncStrongAttempted
655 // but make sure there is no crash
656 EXPECT_EQ(nullptr, weak.promote());
657
658 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
659
660 // In order to fix this:
661 // - need to have incStrongAttempted reflected across IPC boundary (wait for
662 // response to promote - round trip...)
663 // - sendOnLastWeakRef, to delete entries out of RpcState table
664 EXPECT_EQ(b, weak.promote());
665}
666
667#define expectSessions(expected, iface) \
668 do { \
669 int session; \
670 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
671 EXPECT_EQ(expected, session); \
672 } while (false)
673
Steven Morelandc1635952021-04-01 16:20:47 +0000674TEST_P(BinderRpc, SingleSession) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000675 auto proc = createRpcTestSocketServerProcess(1);
676
677 sp<IBinderRpcSession> session;
678 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
679 std::string out;
680 EXPECT_OK(session->getName(&out));
681 EXPECT_EQ("aoeu", out);
682
683 expectSessions(1, proc.rootIface);
684 session = nullptr;
685 expectSessions(0, proc.rootIface);
686}
687
Steven Morelandc1635952021-04-01 16:20:47 +0000688TEST_P(BinderRpc, ManySessions) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000689 auto proc = createRpcTestSocketServerProcess(1);
690
691 std::vector<sp<IBinderRpcSession>> sessions;
692
693 for (size_t i = 0; i < 15; i++) {
694 expectSessions(i, proc.rootIface);
695 sp<IBinderRpcSession> session;
696 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
697 sessions.push_back(session);
698 }
699 expectSessions(sessions.size(), proc.rootIface);
700 for (size_t i = 0; i < sessions.size(); i++) {
701 std::string out;
702 EXPECT_OK(sessions.at(i)->getName(&out));
703 EXPECT_EQ(std::to_string(i), out);
704 }
705 expectSessions(sessions.size(), proc.rootIface);
706
707 while (!sessions.empty()) {
708 sessions.pop_back();
709 expectSessions(sessions.size(), proc.rootIface);
710 }
711 expectSessions(0, proc.rootIface);
712}
713
714size_t epochMillis() {
715 using std::chrono::duration_cast;
716 using std::chrono::milliseconds;
717 using std::chrono::seconds;
718 using std::chrono::system_clock;
719 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
720}
721
Steven Morelandc1635952021-04-01 16:20:47 +0000722TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000723 constexpr size_t kNumThreads = 10;
724
725 auto proc = createRpcTestSocketServerProcess(kNumThreads);
726
727 EXPECT_OK(proc.rootIface->lock());
728
729 // block all but one thread taking locks
730 std::vector<std::thread> ts;
731 for (size_t i = 0; i < kNumThreads - 1; i++) {
732 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
733 }
734
735 usleep(100000); // give chance for calls on other threads
736
737 // other calls still work
738 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
739
740 constexpr size_t blockTimeMs = 500;
741 size_t epochMsBefore = epochMillis();
742 // after this, we should never see a response within this time
743 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
744
745 // this call should be blocked for blockTimeMs
746 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
747
748 size_t epochMsAfter = epochMillis();
749 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
750
751 for (auto& t : ts) t.join();
752}
753
Steven Morelandc1635952021-04-01 16:20:47 +0000754TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000755 constexpr size_t kNumThreads = 10;
756 constexpr size_t kNumCalls = kNumThreads + 3;
757 constexpr size_t kSleepMs = 500;
758
759 auto proc = createRpcTestSocketServerProcess(kNumThreads);
760
761 size_t epochMsBefore = epochMillis();
762
763 std::vector<std::thread> ts;
764 for (size_t i = 0; i < kNumCalls; i++) {
765 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
766 }
767
768 for (auto& t : ts) t.join();
769
770 size_t epochMsAfter = epochMillis();
771
772 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
773
774 // Potential flake, but make sure calls are handled in parallel.
775 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
776}
777
Steven Morelandc1635952021-04-01 16:20:47 +0000778TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000779 constexpr size_t kNumClientThreads = 10;
780 constexpr size_t kNumServerThreads = 10;
781 constexpr size_t kNumCalls = 100;
782
783 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
784
785 std::vector<std::thread> threads;
786 for (size_t i = 0; i < kNumClientThreads; i++) {
787 threads.push_back(std::thread([&] {
788 for (size_t j = 0; j < kNumCalls; j++) {
789 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000790 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000791 EXPECT_EQ(proc.rootBinder, out);
792 }
793 }));
794 }
795
796 for (auto& t : threads) t.join();
797}
798
Steven Morelandc6046982021-04-20 00:49:42 +0000799TEST_P(BinderRpc, OnewayStressTest) {
800 constexpr size_t kNumClientThreads = 10;
801 constexpr size_t kNumServerThreads = 10;
802 constexpr size_t kNumCalls = 100;
803
804 auto proc = createRpcTestSocketServerProcess(kNumServerThreads);
805
806 std::vector<std::thread> threads;
807 for (size_t i = 0; i < kNumClientThreads; i++) {
808 threads.push_back(std::thread([&] {
809 for (size_t j = 0; j < kNumCalls; j++) {
810 EXPECT_OK(proc.rootIface->sendString("a"));
811 }
812
813 // check threads are not stuck
814 EXPECT_OK(proc.rootIface->sleepMs(250));
815 }));
816 }
817
818 for (auto& t : threads) t.join();
819}
820
Steven Morelandc1635952021-04-01 16:20:47 +0000821TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000822 constexpr size_t kReallyLongTimeMs = 100;
823 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
824
825 // more than one thread, just so this doesn't deadlock
826 auto proc = createRpcTestSocketServerProcess(2);
827
828 size_t epochMsBefore = epochMillis();
829
830 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
831
832 size_t epochMsAfter = epochMillis();
833 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
834}
835
Steven Morelandc1635952021-04-01 16:20:47 +0000836TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000837 constexpr size_t kNumSleeps = 10;
838 constexpr size_t kNumExtraServerThreads = 4;
839 constexpr size_t kSleepMs = 50;
840
841 // make sure calls to the same object happen on the same thread
842 auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads);
843
844 EXPECT_OK(proc.rootIface->lock());
845
846 for (size_t i = 0; i < kNumSleeps; i++) {
847 // these should be processed serially
848 proc.rootIface->sleepMsAsync(kSleepMs);
849 }
850 // should also be processesed serially
851 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
852
853 size_t epochMsBefore = epochMillis();
854 EXPECT_OK(proc.rootIface->lockUnlock());
855 size_t epochMsAfter = epochMillis();
856
857 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
858}
859
Steven Morelandc1635952021-04-01 16:20:47 +0000860TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000861 for (bool doDeathCleanup : {true, false}) {
862 auto proc = createRpcTestSocketServerProcess(1);
863
864 // make sure there is some state during crash
865 // 1. we hold their binder
866 sp<IBinderRpcSession> session;
867 EXPECT_OK(proc.rootIface->openSession("happy", &session));
868 // 2. they hold our binder
869 sp<IBinder> binder = new BBinder();
870 EXPECT_OK(proc.rootIface->holdBinder(binder));
871
872 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
873 << "Do death cleanup: " << doDeathCleanup;
874
Steven Moreland736664b2021-05-01 04:27:25 +0000875 proc.expectInvalid = true;
Steven Moreland5553ac42020-11-11 02:14:45 +0000876 }
877}
878
Steven Moreland37aff182021-03-26 02:04:16 +0000879TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
880 auto proc = createRpcTestSocketServerProcess(1);
881
882 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
883 ASSERT_NE(binder, nullptr);
884
885 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
886}
887
888TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
889 auto proc = createRpcTestSocketServerProcess(1);
890
891 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
892 ASSERT_NE(binder, nullptr);
893
894 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
895 ASSERT_NE(ndkBinder, nullptr);
896
897 std::string out;
898 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
899 ASSERT_TRUE(status.isOk()) << status.getDescription();
900 ASSERT_EQ("aoeuaoeu", out);
901}
902
Steven Moreland5553ac42020-11-11 02:14:45 +0000903ssize_t countFds() {
904 DIR* dir = opendir("/proc/self/fd/");
905 if (dir == nullptr) return -1;
906 ssize_t ret = 0;
907 dirent* ent;
908 while ((ent = readdir(dir)) != nullptr) ret++;
909 closedir(dir);
910 return ret;
911}
912
Steven Morelandc1635952021-04-01 16:20:47 +0000913TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000914 ssize_t beforeFds = countFds();
915 ASSERT_GE(beforeFds, 0);
916 {
917 auto proc = createRpcTestSocketServerProcess(10);
918 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
919 }
920 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
921}
922
Steven Morelandc1635952021-04-01 16:20:47 +0000923INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700924 ::testing::ValuesIn({
925 SocketType::UNIX,
Steven Morelandbd5002b2021-05-04 23:12:56 +0000926// TODO(b/185269356): working on host
Steven Morelandf6ec4632021-04-01 16:20:47 +0000927#ifdef __BIONIC__
Yifan Hong0d2bd112021-04-13 17:38:36 -0700928 SocketType::VSOCK,
Steven Morelandbd5002b2021-05-04 23:12:56 +0000929#endif
Yifan Hong0d2bd112021-04-13 17:38:36 -0700930 SocketType::INET,
931 }),
Steven Morelandf6ec4632021-04-01 16:20:47 +0000932 PrintSocketType);
Steven Morelandc1635952021-04-01 16:20:47 +0000933
934} // namespace android
935
936int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000937 ::testing::InitGoogleTest(&argc, argv);
938 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
939 return RUN_ALL_TESTS();
940}