blob: 386183d7ffe522a372a85a2287f83757165642d8 [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 Moreland659416d2021-05-11 00:47:50 +000017#include <BnBinderRpcCallback.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000018#include <BnBinderRpcSession.h>
19#include <BnBinderRpcTest.h>
Steven Moreland37aff182021-03-26 02:04:16 +000020#include <aidl/IBinderRpcTest.h>
Yifan Hong6d82c8a2021-04-26 20:26:45 -070021#include <android-base/file.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000022#include <android-base/logging.h>
Steven Moreland37aff182021-03-26 02:04:16 +000023#include <android/binder_auto_utils.h>
24#include <android/binder_libbinder.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000025#include <binder/Binder.h>
26#include <binder/BpBinder.h>
Steven Morelandd7302072021-05-15 01:32:04 +000027#include <binder/IPCThreadState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000028#include <binder/IServiceManager.h>
29#include <binder/ProcessState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000030#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000031#include <binder/RpcSession.h>
Yifan Hong702115c2021-06-24 15:39:18 -070032#include <binder/RpcTransport.h>
33#include <binder/RpcTransportRaw.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000034#include <gtest/gtest.h>
35
Steven Morelandc1635952021-04-01 16:20:47 +000036#include <chrono>
37#include <cstdlib>
38#include <iostream>
39#include <thread>
Steven Moreland659416d2021-05-11 00:47:50 +000040#include <type_traits>
Steven Morelandc1635952021-04-01 16:20:47 +000041
Steven Morelandc1635952021-04-01 16:20:47 +000042#include <sys/prctl.h>
43#include <unistd.h>
44
Steven Morelandbd5002b2021-05-04 23:12:56 +000045#include "../RpcState.h" // for debugging
46#include "../vm_sockets.h" // for VMADDR_*
Steven Moreland5553ac42020-11-11 02:14:45 +000047
Yifan Hong1a235852021-05-13 16:07:47 -070048using namespace std::chrono_literals;
49
Steven Moreland5553ac42020-11-11 02:14:45 +000050namespace android {
51
Steven Morelandbf57bce2021-07-26 15:26:12 -070052static_assert(RPC_WIRE_PROTOCOL_VERSION + 1 == RPC_WIRE_PROTOCOL_VERSION_NEXT ||
53 RPC_WIRE_PROTOCOL_VERSION == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL);
54
Yifan Hong702115c2021-06-24 15:39:18 -070055enum class RpcSecurity { RAW };
56
57static inline std::vector<RpcSecurity> RpcSecurityValues() {
58 return {RpcSecurity::RAW};
59}
60
61static inline std::unique_ptr<RpcTransportCtxFactory> newFactory(RpcSecurity rpcSecurity) {
62 switch (rpcSecurity) {
63 case RpcSecurity::RAW:
64 return RpcTransportCtxFactoryRaw::make();
65 default:
66 LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity);
67 }
68}
69
Steven Moreland1fda67b2021-04-02 18:35:50 +000070TEST(BinderRpcParcel, EntireParcelFormatted) {
71 Parcel p;
72 p.writeInt32(3);
73
74 EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), "");
75}
76
Yifan Hong702115c2021-06-24 15:39:18 -070077class BinderRpcSimple : public ::testing::TestWithParam<RpcSecurity> {
78public:
79 static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) {
80 return newFactory(info.param)->toCString();
81 }
82};
83
84TEST_P(BinderRpcSimple, SetExternalServerTest) {
Yifan Hong00aeb762021-05-12 17:07:36 -070085 base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
86 int sinkFd = sink.get();
Yifan Hong702115c2021-06-24 15:39:18 -070087 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong00aeb762021-05-12 17:07:36 -070088 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
89 ASSERT_FALSE(server->hasServer());
90 ASSERT_TRUE(server->setupExternalServer(std::move(sink)));
91 ASSERT_TRUE(server->hasServer());
92 base::unique_fd retrieved = server->releaseServer();
93 ASSERT_FALSE(server->hasServer());
94 ASSERT_EQ(sinkFd, retrieved.get());
95}
96
Steven Morelandbf57bce2021-07-26 15:26:12 -070097TEST(BinderRpc, CannotUseNextWireVersion) {
98 auto session = RpcSession::make();
99 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT));
100 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 1));
101 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 2));
102 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 15));
103}
104
105TEST(BinderRpc, CanUseExperimentalWireVersion) {
106 auto session = RpcSession::make();
107 EXPECT_TRUE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL));
108}
109
Steven Moreland5553ac42020-11-11 02:14:45 +0000110using android::binder::Status;
111
112#define EXPECT_OK(status) \
113 do { \
114 Status stat = (status); \
115 EXPECT_TRUE(stat.isOk()) << stat; \
116 } while (false)
117
118class MyBinderRpcSession : public BnBinderRpcSession {
119public:
120 static std::atomic<int32_t> gNum;
121
122 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
123 Status getName(std::string* name) override {
124 *name = mName;
125 return Status::ok();
126 }
127 ~MyBinderRpcSession() { gNum--; }
128
129private:
130 std::string mName;
131};
132std::atomic<int32_t> MyBinderRpcSession::gNum;
133
Steven Moreland659416d2021-05-11 00:47:50 +0000134class MyBinderRpcCallback : public BnBinderRpcCallback {
135 Status sendCallback(const std::string& value) {
136 std::unique_lock _l(mMutex);
137 mValues.push_back(value);
138 _l.unlock();
139 mCv.notify_one();
140 return Status::ok();
141 }
142 Status sendOnewayCallback(const std::string& value) { return sendCallback(value); }
143
144public:
145 std::mutex mMutex;
146 std::condition_variable mCv;
147 std::vector<std::string> mValues;
148};
149
Steven Moreland5553ac42020-11-11 02:14:45 +0000150class MyBinderRpcTest : public BnBinderRpcTest {
151public:
Steven Moreland611d15f2021-05-01 01:28:27 +0000152 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +0000153
154 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +0000155 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +0000156 return Status::ok();
157 }
158 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +0000159 *strstr = str + str;
160 return Status::ok();
161 }
Steven Moreland736664b2021-05-01 04:27:25 +0000162 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +0000163 sp<RpcServer> spServer = server.promote();
164 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000165 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
166 }
Steven Moreland736664b2021-05-01 04:27:25 +0000167 out->clear();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000168 for (auto session : spServer->listSessions()) {
169 size_t count = session->state()->countBinders();
Steven Moreland736664b2021-05-01 04:27:25 +0000170 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000171 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000172 return Status::ok();
173 }
174 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
175 if (binder == nullptr) {
176 std::cout << "Received null binder!" << std::endl;
177 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
178 }
179 *out = binder->pingBinder();
180 return Status::ok();
181 }
182 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
183 *out = binder;
184 return Status::ok();
185 }
186 static sp<IBinder> mHeldBinder;
187 Status holdBinder(const sp<IBinder>& binder) override {
188 mHeldBinder = binder;
189 return Status::ok();
190 }
191 Status getHeldBinder(sp<IBinder>* held) override {
192 *held = mHeldBinder;
193 return Status::ok();
194 }
195 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
196 if (count <= 0) return Status::ok();
197 return binder->nestMe(this, count - 1);
198 }
199 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
200 static sp<IBinder> binder = new BBinder;
201 *out = binder;
202 return Status::ok();
203 }
204 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
205 *out = new MyBinderRpcSession(name);
206 return Status::ok();
207 }
208 Status getNumOpenSessions(int32_t* out) override {
209 *out = MyBinderRpcSession::gNum;
210 return Status::ok();
211 }
212
213 std::mutex blockMutex;
214 Status lock() override {
215 blockMutex.lock();
216 return Status::ok();
217 }
218 Status unlockInMsAsync(int32_t ms) override {
219 usleep(ms * 1000);
220 blockMutex.unlock();
221 return Status::ok();
222 }
223 Status lockUnlock() override {
224 std::lock_guard<std::mutex> _l(blockMutex);
225 return Status::ok();
226 }
227
228 Status sleepMs(int32_t ms) override {
229 usleep(ms * 1000);
230 return Status::ok();
231 }
232
233 Status sleepMsAsync(int32_t ms) override {
234 // In-process binder calls are asynchronous, but the call to this method
235 // is synchronous wrt its client. This in/out-process threading model
236 // diffentiation is a classic binder leaky abstraction (for better or
237 // worse) and is preserved here the way binder sockets plugs itself
238 // into BpBinder, as nothing is changed at the higher levels
239 // (IInterface) which result in this behavior.
240 return sleepMs(ms);
241 }
242
Steven Moreland659416d2021-05-11 00:47:50 +0000243 Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed,
244 const std::string& value) override {
245 if (callback == nullptr) {
246 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
247 }
248
249 if (delayed) {
250 std::thread([=]() {
251 ALOGE("Executing delayed callback: '%s'", value.c_str());
Steven Morelandc7d40132021-06-10 03:42:11 +0000252 Status status = doCallback(callback, oneway, false, value);
253 ALOGE("Delayed callback status: '%s'", status.toString8().c_str());
Steven Moreland659416d2021-05-11 00:47:50 +0000254 }).detach();
255 return Status::ok();
256 }
257
258 if (oneway) {
259 return callback->sendOnewayCallback(value);
260 }
261
262 return callback->sendCallback(value);
263 }
264
Steven Morelandc7d40132021-06-10 03:42:11 +0000265 Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed,
266 const std::string& value) override {
267 return doCallback(callback, oneway, delayed, value);
268 }
269
Steven Moreland5553ac42020-11-11 02:14:45 +0000270 Status die(bool cleanup) override {
271 if (cleanup) {
272 exit(1);
273 } else {
274 _exit(1);
275 }
276 }
Steven Morelandaf4ca712021-05-24 23:22:08 +0000277
278 Status scheduleShutdown() override {
279 sp<RpcServer> strongServer = server.promote();
280 if (strongServer == nullptr) {
281 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
282 }
283 std::thread([=] {
284 LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown");
285 }).detach();
286 return Status::ok();
287 }
288
Steven Morelandd7302072021-05-15 01:32:04 +0000289 Status useKernelBinderCallingId() override {
290 // this is WRONG! It does not make sense when using RPC binder, and
291 // because it is SO wrong, and so much code calls this, it should abort!
292
293 (void)IPCThreadState::self()->getCallingPid();
294 return Status::ok();
295 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000296};
297sp<IBinder> MyBinderRpcTest::mHeldBinder;
298
299class Process {
300public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700301 Process(Process&&) = default;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700302 Process(const std::function<void(android::base::borrowed_fd /* writeEnd */)>& f) {
303 android::base::unique_fd writeEnd;
304 CHECK(android::base::Pipe(&mReadEnd, &writeEnd)) << strerror(errno);
Steven Moreland5553ac42020-11-11 02:14:45 +0000305 if (0 == (mPid = fork())) {
306 // racey: assume parent doesn't crash before this is set
307 prctl(PR_SET_PDEATHSIG, SIGHUP);
308
Yifan Hong0f58fb92021-06-16 16:09:23 -0700309 f(writeEnd);
Steven Morelandaf4ca712021-05-24 23:22:08 +0000310
311 exit(0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000312 }
313 }
314 ~Process() {
315 if (mPid != 0) {
Steven Morelandaf4ca712021-05-24 23:22:08 +0000316 waitpid(mPid, nullptr, 0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000317 }
318 }
Yifan Hong0f58fb92021-06-16 16:09:23 -0700319 android::base::borrowed_fd readEnd() { return mReadEnd; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000320
321private:
322 pid_t mPid = 0;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700323 android::base::unique_fd mReadEnd;
Steven Moreland5553ac42020-11-11 02:14:45 +0000324};
325
326static std::string allocateSocketAddress() {
327 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000328 std::string temp = getenv("TMPDIR") ?: "/tmp";
329 return temp + "/binderRpcTest_" + std::to_string(id++);
Steven Moreland5553ac42020-11-11 02:14:45 +0000330};
331
Steven Morelandda573042021-06-12 01:13:45 +0000332static unsigned int allocateVsockPort() {
333 static unsigned int vsockPort = 3456;
334 return vsockPort++;
335}
336
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000337struct ProcessSession {
Steven Moreland5553ac42020-11-11 02:14:45 +0000338 // reference to process hosting a socket server
339 Process host;
340
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000341 struct SessionInfo {
342 sp<RpcSession> session;
Steven Moreland736664b2021-05-01 04:27:25 +0000343 sp<IBinder> root;
344 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000345
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000346 // client session objects associated with other process
347 // each one represents a separate session
348 std::vector<SessionInfo> sessions;
Steven Moreland5553ac42020-11-11 02:14:45 +0000349
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000350 ProcessSession(ProcessSession&&) = default;
351 ~ProcessSession() {
352 for (auto& session : sessions) {
353 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000354 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000355
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000356 for (auto& info : sessions) {
357 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000358
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000359 EXPECT_NE(nullptr, session);
360 EXPECT_NE(nullptr, session->state());
361 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000362
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000363 wp<RpcSession> weakSession = session;
364 session = nullptr;
365 EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session";
Steven Moreland736664b2021-05-01 04:27:25 +0000366 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000367 }
368};
369
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000370// Process session where the process hosts IBinderRpcTest, the server used
Steven Moreland5553ac42020-11-11 02:14:45 +0000371// for most testing here
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000372struct BinderRpcTestProcessSession {
373 ProcessSession proc;
Steven Moreland5553ac42020-11-11 02:14:45 +0000374
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000375 // pre-fetched root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000376 sp<IBinder> rootBinder;
377
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000378 // pre-casted root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000379 sp<IBinderRpcTest> rootIface;
380
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000381 // whether session should be invalidated by end of run
Steven Morelandaf4ca712021-05-24 23:22:08 +0000382 bool expectAlreadyShutdown = false;
Steven Moreland736664b2021-05-01 04:27:25 +0000383
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000384 BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default;
385 ~BinderRpcTestProcessSession() {
Steven Moreland659416d2021-05-11 00:47:50 +0000386 EXPECT_NE(nullptr, rootIface);
387 if (rootIface == nullptr) return;
388
Steven Morelandaf4ca712021-05-24 23:22:08 +0000389 if (!expectAlreadyShutdown) {
Steven Moreland736664b2021-05-01 04:27:25 +0000390 std::vector<int32_t> remoteCounts;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000391 // calling over any sessions counts across all sessions
Steven Moreland736664b2021-05-01 04:27:25 +0000392 EXPECT_OK(rootIface->countBinders(&remoteCounts));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000393 EXPECT_EQ(remoteCounts.size(), proc.sessions.size());
Steven Moreland736664b2021-05-01 04:27:25 +0000394 for (auto remoteCount : remoteCounts) {
395 EXPECT_EQ(remoteCount, 1);
396 }
Steven Morelandaf4ca712021-05-24 23:22:08 +0000397
Steven Moreland798e0d12021-07-14 23:19:25 +0000398 // even though it is on another thread, shutdown races with
399 // the transaction reply being written
400 if (auto status = rootIface->scheduleShutdown(); !status.isOk()) {
401 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
402 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000403 }
404
405 rootIface = nullptr;
406 rootBinder = nullptr;
407 }
408};
409
Steven Morelandc1635952021-04-01 16:20:47 +0000410enum class SocketType {
411 UNIX,
412 VSOCK,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700413 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000414};
Yifan Hong702115c2021-06-24 15:39:18 -0700415static inline std::string PrintToString(SocketType socketType) {
416 switch (socketType) {
Steven Morelandc1635952021-04-01 16:20:47 +0000417 case SocketType::UNIX:
418 return "unix_domain_socket";
419 case SocketType::VSOCK:
420 return "vm_socket";
Yifan Hong0d2bd112021-04-13 17:38:36 -0700421 case SocketType::INET:
422 return "inet_socket";
Steven Morelandc1635952021-04-01 16:20:47 +0000423 default:
424 LOG_ALWAYS_FATAL("Unknown socket type");
425 return "";
426 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000427}
Steven Morelandda573042021-06-12 01:13:45 +0000428
Yifan Hong702115c2021-06-24 15:39:18 -0700429class BinderRpc : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity>> {
Steven Morelandc1635952021-04-01 16:20:47 +0000430public:
Steven Moreland4313d7e2021-07-15 23:41:22 +0000431 struct Options {
432 size_t numThreads = 1;
433 size_t numSessions = 1;
434 size_t numIncomingConnections = 0;
435 };
436
Yifan Hong702115c2021-06-24 15:39:18 -0700437 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
438 auto [type, security] = info.param;
439 return PrintToString(type) + "_" + newFactory(security)->toCString();
440 }
441
Steven Morelandc1635952021-04-01 16:20:47 +0000442 // This creates a new process serving an interface on a certain number of
443 // threads.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000444 ProcessSession createRpcTestSocketServerProcess(
Steven Moreland4313d7e2021-07-15 23:41:22 +0000445 const Options& options, const std::function<void(const sp<RpcServer>&)>& configure) {
446 CHECK_GE(options.numSessions, 1) << "Must have at least one session to a server";
Steven Moreland736664b2021-05-01 04:27:25 +0000447
Yifan Hong702115c2021-06-24 15:39:18 -0700448 SocketType socketType = std::get<0>(GetParam());
449 RpcSecurity rpcSecurity = std::get<1>(GetParam());
Steven Morelandc1635952021-04-01 16:20:47 +0000450
Steven Morelandda573042021-06-12 01:13:45 +0000451 unsigned int vsockPort = allocateVsockPort();
Steven Morelandc1635952021-04-01 16:20:47 +0000452 std::string addr = allocateSocketAddress();
453 unlink(addr.c_str());
Steven Morelandc1635952021-04-01 16:20:47 +0000454
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000455 auto ret = ProcessSession{
Yifan Hong0f58fb92021-06-16 16:09:23 -0700456 .host = Process([&](android::base::borrowed_fd writeEnd) {
Yifan Hong702115c2021-06-24 15:39:18 -0700457 sp<RpcServer> server = RpcServer::make(newFactory(rpcSecurity));
Steven Morelandc1635952021-04-01 16:20:47 +0000458
459 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland4313d7e2021-07-15 23:41:22 +0000460 server->setMaxThreads(options.numThreads);
Steven Morelandc1635952021-04-01 16:20:47 +0000461
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000462 unsigned int outPort = 0;
463
Steven Morelandc1635952021-04-01 16:20:47 +0000464 switch (socketType) {
465 case SocketType::UNIX:
Steven Moreland611d15f2021-05-01 01:28:27 +0000466 CHECK(server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000467 break;
468 case SocketType::VSOCK:
Steven Moreland611d15f2021-05-01 01:28:27 +0000469 CHECK(server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000470 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700471 case SocketType::INET: {
Steven Moreland611d15f2021-05-01 01:28:27 +0000472 CHECK(server->setupInetServer(0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700473 CHECK_NE(0, outPort);
Yifan Hong0d2bd112021-04-13 17:38:36 -0700474 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700475 }
Steven Morelandc1635952021-04-01 16:20:47 +0000476 default:
477 LOG_ALWAYS_FATAL("Unknown socket type");
478 }
479
Yifan Hong0f58fb92021-06-16 16:09:23 -0700480 CHECK(android::base::WriteFully(writeEnd, &outPort, sizeof(outPort)));
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000481
Steven Moreland611d15f2021-05-01 01:28:27 +0000482 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000483
Steven Morelandf137de92021-04-24 01:54:26 +0000484 server->join();
Steven Morelandaf4ca712021-05-24 23:22:08 +0000485
486 // Another thread calls shutdown. Wait for it to complete.
487 (void)server->shutdown();
Steven Morelandc1635952021-04-01 16:20:47 +0000488 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000489 };
490
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000491 // always read socket, so that we have waited for the server to start
492 unsigned int outPort = 0;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700493 CHECK(android::base::ReadFully(ret.host.readEnd(), &outPort, sizeof(outPort)));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700494 if (socketType == SocketType::INET) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000495 CHECK_NE(0, outPort);
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700496 }
497
Steven Moreland4313d7e2021-07-15 23:41:22 +0000498 for (size_t i = 0; i < options.numSessions; i++) {
Yifan Hong702115c2021-06-24 15:39:18 -0700499 sp<RpcSession> session = RpcSession::make(newFactory(rpcSecurity));
Steven Moreland4313d7e2021-07-15 23:41:22 +0000500 session->setMaxThreads(options.numIncomingConnections);
Steven Moreland659416d2021-05-11 00:47:50 +0000501
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000502 switch (socketType) {
503 case SocketType::UNIX:
504 if (session->setupUnixDomainClient(addr.c_str())) goto success;
505 break;
506 case SocketType::VSOCK:
507 if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success;
508 break;
509 case SocketType::INET:
510 if (session->setupInetClient("127.0.0.1", outPort)) goto success;
511 break;
512 default:
513 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000514 }
Steven Moreland736664b2021-05-01 04:27:25 +0000515 LOG_ALWAYS_FATAL("Could not connect");
516 success:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000517 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000518 }
Steven Morelandc1635952021-04-01 16:20:47 +0000519 return ret;
520 }
521
Steven Moreland4313d7e2021-07-15 23:41:22 +0000522 BinderRpcTestProcessSession createRpcTestSocketServerProcess(const Options& options) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000523 BinderRpcTestProcessSession ret{
Steven Moreland4313d7e2021-07-15 23:41:22 +0000524 .proc = createRpcTestSocketServerProcess(options,
Steven Moreland611d15f2021-05-01 01:28:27 +0000525 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000526 sp<MyBinderRpcTest> service =
527 new MyBinderRpcTest;
528 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000529 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000530 }),
531 };
532
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000533 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000534 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
535
536 return ret;
537 }
538};
539
Steven Morelandc1635952021-04-01 16:20:47 +0000540TEST_P(BinderRpc, Ping) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000541 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000542 ASSERT_NE(proc.rootBinder, nullptr);
543 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
544}
545
Steven Moreland4cf688f2021-03-31 01:48:58 +0000546TEST_P(BinderRpc, GetInterfaceDescriptor) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000547 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland4cf688f2021-03-31 01:48:58 +0000548 ASSERT_NE(proc.rootBinder, nullptr);
549 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
550}
551
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000552TEST_P(BinderRpc, MultipleSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000553 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5});
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000554 for (auto session : proc.proc.sessions) {
555 ASSERT_NE(nullptr, session.root);
556 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000557 }
558}
559
Steven Morelandc1635952021-04-01 16:20:47 +0000560TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000561 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000562 Parcel data;
563 Parcel reply;
564 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
565}
566
Steven Moreland67753c32021-04-02 18:45:19 +0000567TEST_P(BinderRpc, AppendSeparateFormats) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000568 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland67753c32021-04-02 18:45:19 +0000569
570 Parcel p1;
571 p1.markForBinder(proc.rootBinder);
572 p1.writeInt32(3);
573
574 Parcel p2;
575
576 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
577 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
578}
579
Steven Morelandc1635952021-04-01 16:20:47 +0000580TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000581 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000582 Parcel data;
583 data.markForBinder(proc.rootBinder);
584 Parcel reply;
585 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
586}
587
Steven Morelandc1635952021-04-01 16:20:47 +0000588TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000589 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000590 EXPECT_OK(proc.rootIface->sendString("asdf"));
591}
592
Steven Morelandc1635952021-04-01 16:20:47 +0000593TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000594 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000595 std::string doubled;
596 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
597 EXPECT_EQ("cool cool ", doubled);
598}
599
Steven Morelandc1635952021-04-01 16:20:47 +0000600TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000601 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000602 std::string single = std::string(1024, 'a');
603 std::string doubled;
604 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
605 EXPECT_EQ(single + single, doubled);
606}
607
Steven Morelandc1635952021-04-01 16:20:47 +0000608TEST_P(BinderRpc, CallMeBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000609 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000610
611 int32_t pingResult;
612 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
613 EXPECT_EQ(OK, pingResult);
614
615 EXPECT_EQ(0, MyBinderRpcSession::gNum);
616}
617
Steven Morelandc1635952021-04-01 16:20:47 +0000618TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000619 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000620
621 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
622 sp<IBinder> outBinder;
623 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
624 EXPECT_EQ(inBinder, outBinder);
625
626 wp<IBinder> weak = inBinder;
627 inBinder = nullptr;
628 outBinder = nullptr;
629
630 // Force reading a reply, to process any pending dec refs from the other
631 // process (the other process will process dec refs there before processing
632 // the ping here).
633 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
634
635 EXPECT_EQ(nullptr, weak.promote());
636
637 EXPECT_EQ(0, MyBinderRpcSession::gNum);
638}
639
Steven Morelandc1635952021-04-01 16:20:47 +0000640TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000641 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000642
643 sp<IBinderRpcSession> session;
644 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
645
646 sp<IBinder> inBinder = IInterface::asBinder(session);
647 sp<IBinder> outBinder;
648 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
649 EXPECT_EQ(inBinder, outBinder);
650
651 wp<IBinder> weak = inBinder;
652 session = nullptr;
653 inBinder = nullptr;
654 outBinder = nullptr;
655
656 // Force reading a reply, to process any pending dec refs from the other
657 // process (the other process will process dec refs there before processing
658 // the ping here).
659 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
660
661 EXPECT_EQ(nullptr, weak.promote());
662}
663
Steven Morelandc1635952021-04-01 16:20:47 +0000664TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000665 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000666
667 sp<IBinder> outBinder;
668 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
669 EXPECT_EQ(nullptr, outBinder);
670}
671
Steven Morelandc1635952021-04-01 16:20:47 +0000672TEST_P(BinderRpc, HoldBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000673 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000674
675 IBinder* ptr = nullptr;
676 {
677 sp<IBinder> binder = new BBinder();
678 ptr = binder.get();
679 EXPECT_OK(proc.rootIface->holdBinder(binder));
680 }
681
682 sp<IBinder> held;
683 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
684
685 EXPECT_EQ(held.get(), ptr);
686
687 // stop holding binder, because we test to make sure references are cleaned
688 // up
689 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
690 // and flush ref counts
691 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
692}
693
694// START TESTS FOR LIMITATIONS OF SOCKET BINDER
695// These are behavioral differences form regular binder, where certain usecases
696// aren't supported.
697
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000698TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000699 auto proc1 = createRpcTestSocketServerProcess({});
700 auto proc2 = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000701
702 sp<IBinder> outBinder;
703 EXPECT_EQ(INVALID_OPERATION,
704 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
705}
706
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000707TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000708 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2});
Steven Moreland736664b2021-05-01 04:27:25 +0000709
710 sp<IBinder> outBinder;
711 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000712 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000713 .transactionError());
714}
715
Steven Morelandc1635952021-04-01 16:20:47 +0000716TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000717 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000718
719 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
720 sp<IBinder> outBinder;
721 EXPECT_EQ(INVALID_OPERATION,
722 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
723}
724
Steven Morelandc1635952021-04-01 16:20:47 +0000725TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000726 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000727
728 // for historical reasons, IServiceManager interface only returns the
729 // exception code
730 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
731 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
732}
733
734// END TESTS FOR LIMITATIONS OF SOCKET BINDER
735
Steven Morelandc1635952021-04-01 16:20:47 +0000736TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000737 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000738
739 sp<IBinder> outBinder;
740 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
741 EXPECT_EQ(proc.rootBinder, outBinder);
742}
743
Steven Morelandc1635952021-04-01 16:20:47 +0000744TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000745 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000746
747 auto nastyNester = sp<MyBinderRpcTest>::make();
748 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
749
750 wp<IBinder> weak = nastyNester;
751 nastyNester = nullptr;
752 EXPECT_EQ(nullptr, weak.promote());
753}
754
Steven Morelandc1635952021-04-01 16:20:47 +0000755TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000756 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000757
758 sp<IBinder> a;
759 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
760
761 sp<IBinder> b;
762 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
763
764 EXPECT_EQ(a, b);
765}
766
Steven Morelandc1635952021-04-01 16:20:47 +0000767TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000768 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000769
770 sp<IBinder> a;
771 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
772 wp<IBinder> weak = a;
773 a = nullptr;
774
775 sp<IBinder> b;
776 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
777
778 // this is the wrong behavior, since BpBinder
779 // doesn't implement onIncStrongAttempted
780 // but make sure there is no crash
781 EXPECT_EQ(nullptr, weak.promote());
782
783 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
784
785 // In order to fix this:
786 // - need to have incStrongAttempted reflected across IPC boundary (wait for
787 // response to promote - round trip...)
788 // - sendOnLastWeakRef, to delete entries out of RpcState table
789 EXPECT_EQ(b, weak.promote());
790}
791
792#define expectSessions(expected, iface) \
793 do { \
794 int session; \
795 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
796 EXPECT_EQ(expected, session); \
797 } while (false)
798
Steven Morelandc1635952021-04-01 16:20:47 +0000799TEST_P(BinderRpc, SingleSession) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000800 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000801
802 sp<IBinderRpcSession> session;
803 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
804 std::string out;
805 EXPECT_OK(session->getName(&out));
806 EXPECT_EQ("aoeu", out);
807
808 expectSessions(1, proc.rootIface);
809 session = nullptr;
810 expectSessions(0, proc.rootIface);
811}
812
Steven Morelandc1635952021-04-01 16:20:47 +0000813TEST_P(BinderRpc, ManySessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000814 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000815
816 std::vector<sp<IBinderRpcSession>> sessions;
817
818 for (size_t i = 0; i < 15; i++) {
819 expectSessions(i, proc.rootIface);
820 sp<IBinderRpcSession> session;
821 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
822 sessions.push_back(session);
823 }
824 expectSessions(sessions.size(), proc.rootIface);
825 for (size_t i = 0; i < sessions.size(); i++) {
826 std::string out;
827 EXPECT_OK(sessions.at(i)->getName(&out));
828 EXPECT_EQ(std::to_string(i), out);
829 }
830 expectSessions(sessions.size(), proc.rootIface);
831
832 while (!sessions.empty()) {
833 sessions.pop_back();
834 expectSessions(sessions.size(), proc.rootIface);
835 }
836 expectSessions(0, proc.rootIface);
837}
838
839size_t epochMillis() {
840 using std::chrono::duration_cast;
841 using std::chrono::milliseconds;
842 using std::chrono::seconds;
843 using std::chrono::system_clock;
844 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
845}
846
Steven Morelandc1635952021-04-01 16:20:47 +0000847TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000848 constexpr size_t kNumThreads = 10;
849
Steven Moreland4313d7e2021-07-15 23:41:22 +0000850 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000851
852 EXPECT_OK(proc.rootIface->lock());
853
854 // block all but one thread taking locks
855 std::vector<std::thread> ts;
856 for (size_t i = 0; i < kNumThreads - 1; i++) {
857 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
858 }
859
860 usleep(100000); // give chance for calls on other threads
861
862 // other calls still work
863 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
864
865 constexpr size_t blockTimeMs = 500;
866 size_t epochMsBefore = epochMillis();
867 // after this, we should never see a response within this time
868 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
869
870 // this call should be blocked for blockTimeMs
871 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
872
873 size_t epochMsAfter = epochMillis();
874 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
875
876 for (auto& t : ts) t.join();
877}
878
Steven Morelandc1635952021-04-01 16:20:47 +0000879TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000880 constexpr size_t kNumThreads = 10;
881 constexpr size_t kNumCalls = kNumThreads + 3;
882 constexpr size_t kSleepMs = 500;
883
Steven Moreland4313d7e2021-07-15 23:41:22 +0000884 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000885
886 size_t epochMsBefore = epochMillis();
887
888 std::vector<std::thread> ts;
889 for (size_t i = 0; i < kNumCalls; i++) {
890 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
891 }
892
893 for (auto& t : ts) t.join();
894
895 size_t epochMsAfter = epochMillis();
896
897 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
898
899 // Potential flake, but make sure calls are handled in parallel.
900 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
901}
902
Steven Morelandc1635952021-04-01 16:20:47 +0000903TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000904 constexpr size_t kNumClientThreads = 10;
905 constexpr size_t kNumServerThreads = 10;
906 constexpr size_t kNumCalls = 100;
907
Steven Moreland4313d7e2021-07-15 23:41:22 +0000908 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000909
910 std::vector<std::thread> threads;
911 for (size_t i = 0; i < kNumClientThreads; i++) {
912 threads.push_back(std::thread([&] {
913 for (size_t j = 0; j < kNumCalls; j++) {
914 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000915 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000916 EXPECT_EQ(proc.rootBinder, out);
917 }
918 }));
919 }
920
921 for (auto& t : threads) t.join();
922}
923
Steven Morelandc6046982021-04-20 00:49:42 +0000924TEST_P(BinderRpc, OnewayStressTest) {
925 constexpr size_t kNumClientThreads = 10;
926 constexpr size_t kNumServerThreads = 10;
Steven Moreland52eee942021-06-03 00:59:28 +0000927 constexpr size_t kNumCalls = 500;
Steven Morelandc6046982021-04-20 00:49:42 +0000928
Steven Moreland4313d7e2021-07-15 23:41:22 +0000929 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Morelandc6046982021-04-20 00:49:42 +0000930
931 std::vector<std::thread> threads;
932 for (size_t i = 0; i < kNumClientThreads; i++) {
933 threads.push_back(std::thread([&] {
934 for (size_t j = 0; j < kNumCalls; j++) {
935 EXPECT_OK(proc.rootIface->sendString("a"));
936 }
937
938 // check threads are not stuck
939 EXPECT_OK(proc.rootIface->sleepMs(250));
940 }));
941 }
942
943 for (auto& t : threads) t.join();
944}
945
Steven Morelandc1635952021-04-01 16:20:47 +0000946TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000947 constexpr size_t kReallyLongTimeMs = 100;
948 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
949
Steven Moreland4313d7e2021-07-15 23:41:22 +0000950 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000951
952 size_t epochMsBefore = epochMillis();
953
954 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
955
956 size_t epochMsAfter = epochMillis();
957 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
958}
959
Steven Morelandc1635952021-04-01 16:20:47 +0000960TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000961 constexpr size_t kNumSleeps = 10;
962 constexpr size_t kNumExtraServerThreads = 4;
963 constexpr size_t kSleepMs = 50;
964
965 // make sure calls to the same object happen on the same thread
Steven Moreland4313d7e2021-07-15 23:41:22 +0000966 auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000967
968 EXPECT_OK(proc.rootIface->lock());
969
970 for (size_t i = 0; i < kNumSleeps; i++) {
971 // these should be processed serially
972 proc.rootIface->sleepMsAsync(kSleepMs);
973 }
974 // should also be processesed serially
975 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
976
977 size_t epochMsBefore = epochMillis();
978 EXPECT_OK(proc.rootIface->lockUnlock());
979 size_t epochMsAfter = epochMillis();
980
981 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
Steven Morelandf5174272021-05-25 00:39:28 +0000982
983 // pending oneway transactions hold ref, make sure we read data on all
984 // sockets
985 std::vector<std::thread> threads;
986 for (size_t i = 0; i < 1 + kNumExtraServerThreads; i++) {
987 threads.push_back(std::thread([&] { EXPECT_OK(proc.rootIface->sleepMs(250)); }));
988 }
989 for (auto& t : threads) t.join();
Steven Moreland5553ac42020-11-11 02:14:45 +0000990}
991
Steven Morelandd45be622021-06-04 02:19:37 +0000992TEST_P(BinderRpc, OnewayCallExhaustion) {
993 constexpr size_t kNumClients = 2;
994 constexpr size_t kTooLongMs = 1000;
995
Steven Moreland4313d7e2021-07-15 23:41:22 +0000996 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2});
Steven Morelandd45be622021-06-04 02:19:37 +0000997
998 // Build up oneway calls on the second session to make sure it terminates
999 // and shuts down. The first session should be unaffected (proc destructor
1000 // checks the first session).
1001 auto iface = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root);
1002
1003 std::vector<std::thread> threads;
1004 for (size_t i = 0; i < kNumClients; i++) {
1005 // one of these threads will get stuck queueing a transaction once the
1006 // socket fills up, the other will be able to fill up transactions on
1007 // this object
1008 threads.push_back(std::thread([&] {
1009 while (iface->sleepMsAsync(kTooLongMs).isOk()) {
1010 }
1011 }));
1012 }
1013 for (auto& t : threads) t.join();
1014
1015 Status status = iface->sleepMsAsync(kTooLongMs);
1016 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1017
Steven Moreland798e0d12021-07-14 23:19:25 +00001018 // now that it has died, wait for the remote session to shutdown
1019 std::vector<int32_t> remoteCounts;
1020 do {
1021 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
1022 } while (remoteCounts.size() == kNumClients);
1023
Steven Morelandd45be622021-06-04 02:19:37 +00001024 // the second session should be shutdown in the other process by the time we
1025 // are able to join above (it'll only be hung up once it finishes processing
1026 // any pending commands). We need to erase this session from the record
1027 // here, so that the destructor for our session won't check that this
1028 // session is valid, but we still want it to test the other session.
1029 proc.proc.sessions.erase(proc.proc.sessions.begin() + 1);
1030}
1031
Steven Moreland659416d2021-05-11 00:47:50 +00001032TEST_P(BinderRpc, Callbacks) {
1033 const static std::string kTestString = "good afternoon!";
1034
Steven Morelandc7d40132021-06-10 03:42:11 +00001035 for (bool callIsOneway : {true, false}) {
1036 for (bool callbackIsOneway : {true, false}) {
1037 for (bool delayed : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001038 auto proc = createRpcTestSocketServerProcess(
1039 {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1});
Steven Morelandc7d40132021-06-10 03:42:11 +00001040 auto cb = sp<MyBinderRpcCallback>::make();
Steven Moreland659416d2021-05-11 00:47:50 +00001041
Steven Morelandc7d40132021-06-10 03:42:11 +00001042 if (callIsOneway) {
1043 EXPECT_OK(proc.rootIface->doCallbackAsync(cb, callbackIsOneway, delayed,
1044 kTestString));
1045 } else {
1046 EXPECT_OK(
1047 proc.rootIface->doCallback(cb, callbackIsOneway, delayed, kTestString));
1048 }
Steven Moreland659416d2021-05-11 00:47:50 +00001049
Steven Morelandc7d40132021-06-10 03:42:11 +00001050 using std::literals::chrono_literals::operator""s;
1051 std::unique_lock<std::mutex> _l(cb->mMutex);
1052 cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); });
Steven Moreland659416d2021-05-11 00:47:50 +00001053
Steven Morelandc7d40132021-06-10 03:42:11 +00001054 EXPECT_EQ(cb->mValues.size(), 1)
1055 << "callIsOneway: " << callIsOneway
1056 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
1057 if (cb->mValues.empty()) continue;
1058 EXPECT_EQ(cb->mValues.at(0), kTestString)
1059 << "callIsOneway: " << callIsOneway
1060 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
Steven Moreland659416d2021-05-11 00:47:50 +00001061
Steven Morelandc7d40132021-06-10 03:42:11 +00001062 // since we are severing the connection, we need to go ahead and
1063 // tell the server to shutdown and exit so that waitpid won't hang
Steven Moreland798e0d12021-07-14 23:19:25 +00001064 if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) {
1065 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1066 }
Steven Moreland659416d2021-05-11 00:47:50 +00001067
Steven Moreland1b304292021-07-15 22:59:34 +00001068 // since this session has an incoming connection w/ a threadpool, we
Steven Morelandc7d40132021-06-10 03:42:11 +00001069 // need to manually shut it down
1070 EXPECT_TRUE(proc.proc.sessions.at(0).session->shutdownAndWait(true));
Steven Moreland659416d2021-05-11 00:47:50 +00001071
Steven Morelandc7d40132021-06-10 03:42:11 +00001072 proc.expectAlreadyShutdown = true;
1073 }
Steven Moreland659416d2021-05-11 00:47:50 +00001074 }
1075 }
1076}
1077
Steven Moreland195edb82021-06-08 02:44:39 +00001078TEST_P(BinderRpc, OnewayCallbackWithNoThread) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001079 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland195edb82021-06-08 02:44:39 +00001080 auto cb = sp<MyBinderRpcCallback>::make();
1081
1082 Status status = proc.rootIface->doCallback(cb, true /*oneway*/, false /*delayed*/, "anything");
1083 EXPECT_EQ(WOULD_BLOCK, status.transactionError());
1084}
1085
Steven Morelandc1635952021-04-01 16:20:47 +00001086TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001087 for (bool doDeathCleanup : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001088 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +00001089
1090 // make sure there is some state during crash
1091 // 1. we hold their binder
1092 sp<IBinderRpcSession> session;
1093 EXPECT_OK(proc.rootIface->openSession("happy", &session));
1094 // 2. they hold our binder
1095 sp<IBinder> binder = new BBinder();
1096 EXPECT_OK(proc.rootIface->holdBinder(binder));
1097
1098 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
1099 << "Do death cleanup: " << doDeathCleanup;
1100
Steven Morelandaf4ca712021-05-24 23:22:08 +00001101 proc.expectAlreadyShutdown = true;
Steven Moreland5553ac42020-11-11 02:14:45 +00001102 }
1103}
1104
Steven Morelandd7302072021-05-15 01:32:04 +00001105TEST_P(BinderRpc, UseKernelBinderCallingId) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001106 auto proc = createRpcTestSocketServerProcess({});
Steven Morelandd7302072021-05-15 01:32:04 +00001107
1108 // we can't allocate IPCThreadState so actually the first time should
1109 // succeed :(
1110 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
1111
1112 // second time! we catch the error :)
1113 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
1114
Steven Morelandaf4ca712021-05-24 23:22:08 +00001115 proc.expectAlreadyShutdown = true;
Steven Morelandd7302072021-05-15 01:32:04 +00001116}
1117
Steven Moreland37aff182021-03-26 02:04:16 +00001118TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001119 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001120
1121 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1122 ASSERT_NE(binder, nullptr);
1123
1124 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1125}
1126
1127TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001128 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001129
1130 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1131 ASSERT_NE(binder, nullptr);
1132
1133 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
1134 ASSERT_NE(ndkBinder, nullptr);
1135
1136 std::string out;
1137 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
1138 ASSERT_TRUE(status.isOk()) << status.getDescription();
1139 ASSERT_EQ("aoeuaoeu", out);
1140}
1141
Steven Moreland5553ac42020-11-11 02:14:45 +00001142ssize_t countFds() {
1143 DIR* dir = opendir("/proc/self/fd/");
1144 if (dir == nullptr) return -1;
1145 ssize_t ret = 0;
1146 dirent* ent;
1147 while ((ent = readdir(dir)) != nullptr) ret++;
1148 closedir(dir);
1149 return ret;
1150}
1151
Steven Morelandc1635952021-04-01 16:20:47 +00001152TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001153 ssize_t beforeFds = countFds();
1154 ASSERT_GE(beforeFds, 0);
1155 {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001156 auto proc = createRpcTestSocketServerProcess({.numThreads = 10});
Steven Moreland5553ac42020-11-11 02:14:45 +00001157 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
1158 }
1159 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
1160}
1161
Steven Morelandda573042021-06-12 01:13:45 +00001162static bool testSupportVsockLoopback() {
Yifan Hong702115c2021-06-24 15:39:18 -07001163 // We don't need to enable TLS to know if vsock is supported.
Steven Morelandda573042021-06-12 01:13:45 +00001164 unsigned int vsockPort = allocateVsockPort();
Yifan Hong702115c2021-06-24 15:39:18 -07001165 sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make());
Steven Morelandda573042021-06-12 01:13:45 +00001166 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1167 CHECK(server->setupVsockServer(vsockPort));
1168 server->start();
1169
Yifan Hong702115c2021-06-24 15:39:18 -07001170 sp<RpcSession> session = RpcSession::make(RpcTransportCtxFactoryRaw::make());
Steven Morelandda573042021-06-12 01:13:45 +00001171 bool okay = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort);
Steven Moreland798e0d12021-07-14 23:19:25 +00001172 while (!server->shutdown()) usleep(10000);
Steven Morelandda573042021-06-12 01:13:45 +00001173 ALOGE("Detected vsock loopback supported: %d", okay);
1174 return okay;
1175}
1176
1177static std::vector<SocketType> testSocketTypes() {
1178 std::vector<SocketType> ret = {SocketType::UNIX, SocketType::INET};
1179
1180 static bool hasVsockLoopback = testSupportVsockLoopback();
1181
1182 if (hasVsockLoopback) {
1183 ret.push_back(SocketType::VSOCK);
1184 }
1185
1186 return ret;
1187}
1188
Yifan Hong702115c2021-06-24 15:39:18 -07001189INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
1190 ::testing::Combine(::testing::ValuesIn(testSocketTypes()),
1191 ::testing::ValuesIn(RpcSecurityValues())),
1192 BinderRpc::PrintParamInfo);
Steven Morelandc1635952021-04-01 16:20:47 +00001193
Yifan Hong702115c2021-06-24 15:39:18 -07001194class BinderRpcServerRootObject
1195 : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001196
1197TEST_P(BinderRpcServerRootObject, WeakRootObject) {
1198 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
1199 auto setRootObject = [](bool isStrong) -> SetFn {
1200 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
1201 };
1202
Yifan Hong702115c2021-06-24 15:39:18 -07001203 auto [isStrong1, isStrong2, rpcSecurity] = GetParam();
1204 auto server = RpcServer::make(newFactory(rpcSecurity));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001205 auto binder1 = sp<BBinder>::make();
1206 IBinder* binderRaw1 = binder1.get();
1207 setRootObject(isStrong1)(server.get(), binder1);
1208 EXPECT_EQ(binderRaw1, server->getRootObject());
1209 binder1.clear();
1210 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
1211
1212 auto binder2 = sp<BBinder>::make();
1213 IBinder* binderRaw2 = binder2.get();
1214 setRootObject(isStrong2)(server.get(), binder2);
1215 EXPECT_EQ(binderRaw2, server->getRootObject());
1216 binder2.clear();
1217 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
1218}
1219
1220INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
Yifan Hong702115c2021-06-24 15:39:18 -07001221 ::testing::Combine(::testing::Bool(), ::testing::Bool(),
1222 ::testing::ValuesIn(RpcSecurityValues())));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001223
Yifan Hong1a235852021-05-13 16:07:47 -07001224class OneOffSignal {
1225public:
1226 // If notify() was previously called, or is called within |duration|, return true; else false.
1227 template <typename R, typename P>
1228 bool wait(std::chrono::duration<R, P> duration) {
1229 std::unique_lock<std::mutex> lock(mMutex);
1230 return mCv.wait_for(lock, duration, [this] { return mValue; });
1231 }
1232 void notify() {
1233 std::unique_lock<std::mutex> lock(mMutex);
1234 mValue = true;
1235 lock.unlock();
1236 mCv.notify_all();
1237 }
1238
1239private:
1240 std::mutex mMutex;
1241 std::condition_variable mCv;
1242 bool mValue = false;
1243};
1244
Yifan Hong702115c2021-06-24 15:39:18 -07001245TEST_P(BinderRpcSimple, Shutdown) {
Yifan Hong1a235852021-05-13 16:07:47 -07001246 auto addr = allocateSocketAddress();
1247 unlink(addr.c_str());
Yifan Hong702115c2021-06-24 15:39:18 -07001248 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong1a235852021-05-13 16:07:47 -07001249 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1250 ASSERT_TRUE(server->setupUnixDomainServer(addr.c_str()));
1251 auto joinEnds = std::make_shared<OneOffSignal>();
1252
1253 // If things are broken and the thread never stops, don't block other tests. Because the thread
1254 // may run after the test finishes, it must not access the stack memory of the test. Hence,
1255 // shared pointers are passed.
1256 std::thread([server, joinEnds] {
1257 server->join();
1258 joinEnds->notify();
1259 }).detach();
1260
1261 bool shutdown = false;
1262 for (int i = 0; i < 10 && !shutdown; i++) {
1263 usleep(300 * 1000); // 300ms; total 3s
1264 if (server->shutdown()) shutdown = true;
1265 }
1266 ASSERT_TRUE(shutdown) << "server->shutdown() never returns true";
1267
1268 ASSERT_TRUE(joinEnds->wait(2s))
1269 << "After server->shutdown() returns true, join() did not stop after 2s";
1270}
1271
Yifan Hong194acf22021-06-29 18:44:56 -07001272TEST(BinderRpc, Java) {
1273#if !defined(__ANDROID__)
1274 GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on"
1275 "createRpcDelegateServiceManager() with a device attached, such test belongs "
1276 "to binderHostDeviceTest. Hence, just disable this test on host.";
1277#endif // !__ANDROID__
1278 sp<IServiceManager> sm = defaultServiceManager();
1279 ASSERT_NE(nullptr, sm);
1280 // Any Java service with non-empty getInterfaceDescriptor() would do.
1281 // Let's pick batteryproperties.
1282 auto binder = sm->checkService(String16("batteryproperties"));
1283 ASSERT_NE(nullptr, binder);
1284 auto descriptor = binder->getInterfaceDescriptor();
1285 ASSERT_GE(descriptor.size(), 0);
1286 ASSERT_EQ(OK, binder->pingBinder());
1287
1288 auto rpcServer = RpcServer::make();
1289 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1290 unsigned int port;
1291 ASSERT_TRUE(rpcServer->setupInetServer(0, &port));
1292 auto socket = rpcServer->releaseServer();
1293
1294 auto keepAlive = sp<BBinder>::make();
1295 ASSERT_EQ(OK, binder->setRpcClientDebug(std::move(socket), keepAlive));
1296
1297 auto rpcSession = RpcSession::make();
1298 ASSERT_TRUE(rpcSession->setupInetClient("127.0.0.1", port));
1299 auto rpcBinder = rpcSession->getRootObject();
1300 ASSERT_NE(nullptr, rpcBinder);
1301
1302 ASSERT_EQ(OK, rpcBinder->pingBinder());
1303
1304 ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor())
1305 << "getInterfaceDescriptor should not crash system_server";
1306 ASSERT_EQ(OK, rpcBinder->pingBinder());
1307}
1308
Yifan Hong702115c2021-06-24 15:39:18 -07001309INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcSimple, ::testing::ValuesIn(RpcSecurityValues()),
1310 BinderRpcSimple::PrintTestParam);
1311
Steven Morelandc1635952021-04-01 16:20:47 +00001312} // namespace android
1313
1314int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001315 ::testing::InitGoogleTest(&argc, argv);
1316 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
1317 return RUN_ALL_TESTS();
1318}