blob: 98839f75030404f2d3ca16cc2c8e309a6335f49b [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);
Devin Mooref3b9c4f2021-08-03 15:50:13 +000054const char* kLocalInetAddress = "127.0.0.1";
Steven Morelandbf57bce2021-07-26 15:26:12 -070055
Yifan Hong702115c2021-06-24 15:39:18 -070056enum class RpcSecurity { RAW };
57
58static inline std::vector<RpcSecurity> RpcSecurityValues() {
59 return {RpcSecurity::RAW};
60}
61
62static inline std::unique_ptr<RpcTransportCtxFactory> newFactory(RpcSecurity rpcSecurity) {
63 switch (rpcSecurity) {
64 case RpcSecurity::RAW:
65 return RpcTransportCtxFactoryRaw::make();
66 default:
67 LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity);
68 }
69}
70
Steven Moreland1fda67b2021-04-02 18:35:50 +000071TEST(BinderRpcParcel, EntireParcelFormatted) {
72 Parcel p;
73 p.writeInt32(3);
74
75 EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), "");
76}
77
Yifan Hong702115c2021-06-24 15:39:18 -070078class BinderRpcSimple : public ::testing::TestWithParam<RpcSecurity> {
79public:
80 static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) {
81 return newFactory(info.param)->toCString();
82 }
83};
84
85TEST_P(BinderRpcSimple, SetExternalServerTest) {
Yifan Hong00aeb762021-05-12 17:07:36 -070086 base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
87 int sinkFd = sink.get();
Yifan Hong702115c2021-06-24 15:39:18 -070088 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong00aeb762021-05-12 17:07:36 -070089 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
90 ASSERT_FALSE(server->hasServer());
91 ASSERT_TRUE(server->setupExternalServer(std::move(sink)));
92 ASSERT_TRUE(server->hasServer());
93 base::unique_fd retrieved = server->releaseServer();
94 ASSERT_FALSE(server->hasServer());
95 ASSERT_EQ(sinkFd, retrieved.get());
96}
97
Steven Morelandbf57bce2021-07-26 15:26:12 -070098TEST(BinderRpc, CannotUseNextWireVersion) {
99 auto session = RpcSession::make();
100 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT));
101 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 1));
102 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 2));
103 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 15));
104}
105
106TEST(BinderRpc, CanUseExperimentalWireVersion) {
107 auto session = RpcSession::make();
108 EXPECT_TRUE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL));
109}
110
Steven Moreland5553ac42020-11-11 02:14:45 +0000111using android::binder::Status;
112
113#define EXPECT_OK(status) \
114 do { \
115 Status stat = (status); \
116 EXPECT_TRUE(stat.isOk()) << stat; \
117 } while (false)
118
119class MyBinderRpcSession : public BnBinderRpcSession {
120public:
121 static std::atomic<int32_t> gNum;
122
123 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
124 Status getName(std::string* name) override {
125 *name = mName;
126 return Status::ok();
127 }
128 ~MyBinderRpcSession() { gNum--; }
129
130private:
131 std::string mName;
132};
133std::atomic<int32_t> MyBinderRpcSession::gNum;
134
Steven Moreland659416d2021-05-11 00:47:50 +0000135class MyBinderRpcCallback : public BnBinderRpcCallback {
136 Status sendCallback(const std::string& value) {
137 std::unique_lock _l(mMutex);
138 mValues.push_back(value);
139 _l.unlock();
140 mCv.notify_one();
141 return Status::ok();
142 }
143 Status sendOnewayCallback(const std::string& value) { return sendCallback(value); }
144
145public:
146 std::mutex mMutex;
147 std::condition_variable mCv;
148 std::vector<std::string> mValues;
149};
150
Steven Moreland5553ac42020-11-11 02:14:45 +0000151class MyBinderRpcTest : public BnBinderRpcTest {
152public:
Steven Moreland611d15f2021-05-01 01:28:27 +0000153 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +0000154
155 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +0000156 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +0000157 return Status::ok();
158 }
159 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +0000160 *strstr = str + str;
161 return Status::ok();
162 }
Steven Moreland736664b2021-05-01 04:27:25 +0000163 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +0000164 sp<RpcServer> spServer = server.promote();
165 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000166 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
167 }
Steven Moreland736664b2021-05-01 04:27:25 +0000168 out->clear();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000169 for (auto session : spServer->listSessions()) {
170 size_t count = session->state()->countBinders();
Steven Moreland736664b2021-05-01 04:27:25 +0000171 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000172 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000173 return Status::ok();
174 }
175 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
176 if (binder == nullptr) {
177 std::cout << "Received null binder!" << std::endl;
178 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
179 }
180 *out = binder->pingBinder();
181 return Status::ok();
182 }
183 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
184 *out = binder;
185 return Status::ok();
186 }
187 static sp<IBinder> mHeldBinder;
188 Status holdBinder(const sp<IBinder>& binder) override {
189 mHeldBinder = binder;
190 return Status::ok();
191 }
192 Status getHeldBinder(sp<IBinder>* held) override {
193 *held = mHeldBinder;
194 return Status::ok();
195 }
196 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
197 if (count <= 0) return Status::ok();
198 return binder->nestMe(this, count - 1);
199 }
200 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
201 static sp<IBinder> binder = new BBinder;
202 *out = binder;
203 return Status::ok();
204 }
205 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
206 *out = new MyBinderRpcSession(name);
207 return Status::ok();
208 }
209 Status getNumOpenSessions(int32_t* out) override {
210 *out = MyBinderRpcSession::gNum;
211 return Status::ok();
212 }
213
214 std::mutex blockMutex;
215 Status lock() override {
216 blockMutex.lock();
217 return Status::ok();
218 }
219 Status unlockInMsAsync(int32_t ms) override {
220 usleep(ms * 1000);
221 blockMutex.unlock();
222 return Status::ok();
223 }
224 Status lockUnlock() override {
225 std::lock_guard<std::mutex> _l(blockMutex);
226 return Status::ok();
227 }
228
229 Status sleepMs(int32_t ms) override {
230 usleep(ms * 1000);
231 return Status::ok();
232 }
233
234 Status sleepMsAsync(int32_t ms) override {
235 // In-process binder calls are asynchronous, but the call to this method
236 // is synchronous wrt its client. This in/out-process threading model
237 // diffentiation is a classic binder leaky abstraction (for better or
238 // worse) and is preserved here the way binder sockets plugs itself
239 // into BpBinder, as nothing is changed at the higher levels
240 // (IInterface) which result in this behavior.
241 return sleepMs(ms);
242 }
243
Steven Moreland659416d2021-05-11 00:47:50 +0000244 Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed,
245 const std::string& value) override {
246 if (callback == nullptr) {
247 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
248 }
249
250 if (delayed) {
251 std::thread([=]() {
252 ALOGE("Executing delayed callback: '%s'", value.c_str());
Steven Morelandc7d40132021-06-10 03:42:11 +0000253 Status status = doCallback(callback, oneway, false, value);
254 ALOGE("Delayed callback status: '%s'", status.toString8().c_str());
Steven Moreland659416d2021-05-11 00:47:50 +0000255 }).detach();
256 return Status::ok();
257 }
258
259 if (oneway) {
260 return callback->sendOnewayCallback(value);
261 }
262
263 return callback->sendCallback(value);
264 }
265
Steven Morelandc7d40132021-06-10 03:42:11 +0000266 Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed,
267 const std::string& value) override {
268 return doCallback(callback, oneway, delayed, value);
269 }
270
Steven Moreland5553ac42020-11-11 02:14:45 +0000271 Status die(bool cleanup) override {
272 if (cleanup) {
273 exit(1);
274 } else {
275 _exit(1);
276 }
277 }
Steven Morelandaf4ca712021-05-24 23:22:08 +0000278
279 Status scheduleShutdown() override {
280 sp<RpcServer> strongServer = server.promote();
281 if (strongServer == nullptr) {
282 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
283 }
284 std::thread([=] {
285 LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown");
286 }).detach();
287 return Status::ok();
288 }
289
Steven Morelandd7302072021-05-15 01:32:04 +0000290 Status useKernelBinderCallingId() override {
291 // this is WRONG! It does not make sense when using RPC binder, and
292 // because it is SO wrong, and so much code calls this, it should abort!
293
294 (void)IPCThreadState::self()->getCallingPid();
295 return Status::ok();
296 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000297};
298sp<IBinder> MyBinderRpcTest::mHeldBinder;
299
300class Process {
301public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700302 Process(Process&&) = default;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700303 Process(const std::function<void(android::base::borrowed_fd /* writeEnd */)>& f) {
304 android::base::unique_fd writeEnd;
305 CHECK(android::base::Pipe(&mReadEnd, &writeEnd)) << strerror(errno);
Steven Moreland5553ac42020-11-11 02:14:45 +0000306 if (0 == (mPid = fork())) {
307 // racey: assume parent doesn't crash before this is set
308 prctl(PR_SET_PDEATHSIG, SIGHUP);
309
Yifan Hong0f58fb92021-06-16 16:09:23 -0700310 f(writeEnd);
Steven Morelandaf4ca712021-05-24 23:22:08 +0000311
312 exit(0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000313 }
314 }
315 ~Process() {
316 if (mPid != 0) {
Steven Morelandaf4ca712021-05-24 23:22:08 +0000317 waitpid(mPid, nullptr, 0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000318 }
319 }
Yifan Hong0f58fb92021-06-16 16:09:23 -0700320 android::base::borrowed_fd readEnd() { return mReadEnd; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000321
322private:
323 pid_t mPid = 0;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700324 android::base::unique_fd mReadEnd;
Steven Moreland5553ac42020-11-11 02:14:45 +0000325};
326
327static std::string allocateSocketAddress() {
328 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000329 std::string temp = getenv("TMPDIR") ?: "/tmp";
330 return temp + "/binderRpcTest_" + std::to_string(id++);
Steven Moreland5553ac42020-11-11 02:14:45 +0000331};
332
Steven Morelandda573042021-06-12 01:13:45 +0000333static unsigned int allocateVsockPort() {
334 static unsigned int vsockPort = 3456;
335 return vsockPort++;
336}
337
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000338struct ProcessSession {
Steven Moreland5553ac42020-11-11 02:14:45 +0000339 // reference to process hosting a socket server
340 Process host;
341
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000342 struct SessionInfo {
343 sp<RpcSession> session;
Steven Moreland736664b2021-05-01 04:27:25 +0000344 sp<IBinder> root;
345 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000346
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000347 // client session objects associated with other process
348 // each one represents a separate session
349 std::vector<SessionInfo> sessions;
Steven Moreland5553ac42020-11-11 02:14:45 +0000350
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000351 ProcessSession(ProcessSession&&) = default;
352 ~ProcessSession() {
353 for (auto& session : sessions) {
354 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000355 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000356
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000357 for (auto& info : sessions) {
358 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000359
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000360 EXPECT_NE(nullptr, session);
361 EXPECT_NE(nullptr, session->state());
362 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000363
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000364 wp<RpcSession> weakSession = session;
365 session = nullptr;
366 EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session";
Steven Moreland736664b2021-05-01 04:27:25 +0000367 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000368 }
369};
370
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000371// Process session where the process hosts IBinderRpcTest, the server used
Steven Moreland5553ac42020-11-11 02:14:45 +0000372// for most testing here
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000373struct BinderRpcTestProcessSession {
374 ProcessSession proc;
Steven Moreland5553ac42020-11-11 02:14:45 +0000375
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000376 // pre-fetched root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000377 sp<IBinder> rootBinder;
378
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000379 // pre-casted root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000380 sp<IBinderRpcTest> rootIface;
381
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000382 // whether session should be invalidated by end of run
Steven Morelandaf4ca712021-05-24 23:22:08 +0000383 bool expectAlreadyShutdown = false;
Steven Moreland736664b2021-05-01 04:27:25 +0000384
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000385 BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default;
386 ~BinderRpcTestProcessSession() {
Steven Moreland659416d2021-05-11 00:47:50 +0000387 EXPECT_NE(nullptr, rootIface);
388 if (rootIface == nullptr) return;
389
Steven Morelandaf4ca712021-05-24 23:22:08 +0000390 if (!expectAlreadyShutdown) {
Steven Moreland736664b2021-05-01 04:27:25 +0000391 std::vector<int32_t> remoteCounts;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000392 // calling over any sessions counts across all sessions
Steven Moreland736664b2021-05-01 04:27:25 +0000393 EXPECT_OK(rootIface->countBinders(&remoteCounts));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000394 EXPECT_EQ(remoteCounts.size(), proc.sessions.size());
Steven Moreland736664b2021-05-01 04:27:25 +0000395 for (auto remoteCount : remoteCounts) {
396 EXPECT_EQ(remoteCount, 1);
397 }
Steven Morelandaf4ca712021-05-24 23:22:08 +0000398
Steven Moreland798e0d12021-07-14 23:19:25 +0000399 // even though it is on another thread, shutdown races with
400 // the transaction reply being written
401 if (auto status = rootIface->scheduleShutdown(); !status.isOk()) {
402 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
403 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000404 }
405
406 rootIface = nullptr;
407 rootBinder = nullptr;
408 }
409};
410
Steven Morelandc1635952021-04-01 16:20:47 +0000411enum class SocketType {
412 UNIX,
413 VSOCK,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700414 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000415};
Yifan Hong702115c2021-06-24 15:39:18 -0700416static inline std::string PrintToString(SocketType socketType) {
417 switch (socketType) {
Steven Morelandc1635952021-04-01 16:20:47 +0000418 case SocketType::UNIX:
419 return "unix_domain_socket";
420 case SocketType::VSOCK:
421 return "vm_socket";
Yifan Hong0d2bd112021-04-13 17:38:36 -0700422 case SocketType::INET:
423 return "inet_socket";
Steven Morelandc1635952021-04-01 16:20:47 +0000424 default:
425 LOG_ALWAYS_FATAL("Unknown socket type");
426 return "";
427 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000428}
Steven Morelandda573042021-06-12 01:13:45 +0000429
Yifan Hong702115c2021-06-24 15:39:18 -0700430class BinderRpc : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity>> {
Steven Morelandc1635952021-04-01 16:20:47 +0000431public:
Steven Moreland4313d7e2021-07-15 23:41:22 +0000432 struct Options {
433 size_t numThreads = 1;
434 size_t numSessions = 1;
435 size_t numIncomingConnections = 0;
436 };
437
Yifan Hong702115c2021-06-24 15:39:18 -0700438 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
439 auto [type, security] = info.param;
440 return PrintToString(type) + "_" + newFactory(security)->toCString();
441 }
442
Steven Morelandc1635952021-04-01 16:20:47 +0000443 // This creates a new process serving an interface on a certain number of
444 // threads.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000445 ProcessSession createRpcTestSocketServerProcess(
Steven Moreland4313d7e2021-07-15 23:41:22 +0000446 const Options& options, const std::function<void(const sp<RpcServer>&)>& configure) {
447 CHECK_GE(options.numSessions, 1) << "Must have at least one session to a server";
Steven Moreland736664b2021-05-01 04:27:25 +0000448
Yifan Hong702115c2021-06-24 15:39:18 -0700449 SocketType socketType = std::get<0>(GetParam());
450 RpcSecurity rpcSecurity = std::get<1>(GetParam());
Steven Morelandc1635952021-04-01 16:20:47 +0000451
Steven Morelandda573042021-06-12 01:13:45 +0000452 unsigned int vsockPort = allocateVsockPort();
Steven Morelandc1635952021-04-01 16:20:47 +0000453 std::string addr = allocateSocketAddress();
454 unlink(addr.c_str());
Steven Morelandc1635952021-04-01 16:20:47 +0000455
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000456 auto ret = ProcessSession{
Yifan Hong0f58fb92021-06-16 16:09:23 -0700457 .host = Process([&](android::base::borrowed_fd writeEnd) {
Yifan Hong702115c2021-06-24 15:39:18 -0700458 sp<RpcServer> server = RpcServer::make(newFactory(rpcSecurity));
Steven Morelandc1635952021-04-01 16:20:47 +0000459
460 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland4313d7e2021-07-15 23:41:22 +0000461 server->setMaxThreads(options.numThreads);
Steven Morelandc1635952021-04-01 16:20:47 +0000462
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000463 unsigned int outPort = 0;
464
Steven Morelandc1635952021-04-01 16:20:47 +0000465 switch (socketType) {
466 case SocketType::UNIX:
Steven Moreland611d15f2021-05-01 01:28:27 +0000467 CHECK(server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000468 break;
469 case SocketType::VSOCK:
Steven Moreland611d15f2021-05-01 01:28:27 +0000470 CHECK(server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000471 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700472 case SocketType::INET: {
Devin Mooref3b9c4f2021-08-03 15:50:13 +0000473 CHECK(server->setupInetServer(kLocalInetAddress, 0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700474 CHECK_NE(0, outPort);
Yifan Hong0d2bd112021-04-13 17:38:36 -0700475 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700476 }
Steven Morelandc1635952021-04-01 16:20:47 +0000477 default:
478 LOG_ALWAYS_FATAL("Unknown socket type");
479 }
480
Yifan Hong0f58fb92021-06-16 16:09:23 -0700481 CHECK(android::base::WriteFully(writeEnd, &outPort, sizeof(outPort)));
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000482
Steven Moreland611d15f2021-05-01 01:28:27 +0000483 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000484
Steven Morelandf137de92021-04-24 01:54:26 +0000485 server->join();
Steven Morelandaf4ca712021-05-24 23:22:08 +0000486
487 // Another thread calls shutdown. Wait for it to complete.
488 (void)server->shutdown();
Steven Morelandc1635952021-04-01 16:20:47 +0000489 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000490 };
491
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000492 // always read socket, so that we have waited for the server to start
493 unsigned int outPort = 0;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700494 CHECK(android::base::ReadFully(ret.host.readEnd(), &outPort, sizeof(outPort)));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700495 if (socketType == SocketType::INET) {
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000496 CHECK_NE(0, outPort);
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700497 }
498
Steven Moreland4313d7e2021-07-15 23:41:22 +0000499 for (size_t i = 0; i < options.numSessions; i++) {
Yifan Hong702115c2021-06-24 15:39:18 -0700500 sp<RpcSession> session = RpcSession::make(newFactory(rpcSecurity));
Steven Moreland4313d7e2021-07-15 23:41:22 +0000501 session->setMaxThreads(options.numIncomingConnections);
Steven Moreland659416d2021-05-11 00:47:50 +0000502
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000503 switch (socketType) {
504 case SocketType::UNIX:
505 if (session->setupUnixDomainClient(addr.c_str())) goto success;
506 break;
507 case SocketType::VSOCK:
508 if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success;
509 break;
510 case SocketType::INET:
511 if (session->setupInetClient("127.0.0.1", outPort)) goto success;
512 break;
513 default:
514 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000515 }
Steven Moreland736664b2021-05-01 04:27:25 +0000516 LOG_ALWAYS_FATAL("Could not connect");
517 success:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000518 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000519 }
Steven Morelandc1635952021-04-01 16:20:47 +0000520 return ret;
521 }
522
Steven Moreland4313d7e2021-07-15 23:41:22 +0000523 BinderRpcTestProcessSession createRpcTestSocketServerProcess(const Options& options) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000524 BinderRpcTestProcessSession ret{
Steven Moreland4313d7e2021-07-15 23:41:22 +0000525 .proc = createRpcTestSocketServerProcess(options,
Steven Moreland611d15f2021-05-01 01:28:27 +0000526 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000527 sp<MyBinderRpcTest> service =
528 new MyBinderRpcTest;
529 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000530 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000531 }),
532 };
533
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000534 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000535 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
536
537 return ret;
538 }
539};
540
Steven Morelandc1635952021-04-01 16:20:47 +0000541TEST_P(BinderRpc, Ping) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000542 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000543 ASSERT_NE(proc.rootBinder, nullptr);
544 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
545}
546
Steven Moreland4cf688f2021-03-31 01:48:58 +0000547TEST_P(BinderRpc, GetInterfaceDescriptor) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000548 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland4cf688f2021-03-31 01:48:58 +0000549 ASSERT_NE(proc.rootBinder, nullptr);
550 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
551}
552
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000553TEST_P(BinderRpc, MultipleSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000554 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5});
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000555 for (auto session : proc.proc.sessions) {
556 ASSERT_NE(nullptr, session.root);
557 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000558 }
559}
560
Steven Morelandc1635952021-04-01 16:20:47 +0000561TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000562 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000563 Parcel data;
564 Parcel reply;
565 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
566}
567
Steven Moreland67753c32021-04-02 18:45:19 +0000568TEST_P(BinderRpc, AppendSeparateFormats) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000569 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland67753c32021-04-02 18:45:19 +0000570
571 Parcel p1;
572 p1.markForBinder(proc.rootBinder);
573 p1.writeInt32(3);
574
575 Parcel p2;
576
577 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
578 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
579}
580
Steven Morelandc1635952021-04-01 16:20:47 +0000581TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000582 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000583 Parcel data;
584 data.markForBinder(proc.rootBinder);
585 Parcel reply;
586 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
587}
588
Steven Morelandc1635952021-04-01 16:20:47 +0000589TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000590 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000591 EXPECT_OK(proc.rootIface->sendString("asdf"));
592}
593
Steven Morelandc1635952021-04-01 16:20:47 +0000594TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000595 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000596 std::string doubled;
597 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
598 EXPECT_EQ("cool cool ", doubled);
599}
600
Steven Morelandc1635952021-04-01 16:20:47 +0000601TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000602 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000603 std::string single = std::string(1024, 'a');
604 std::string doubled;
605 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
606 EXPECT_EQ(single + single, doubled);
607}
608
Steven Morelandc1635952021-04-01 16:20:47 +0000609TEST_P(BinderRpc, CallMeBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000610 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000611
612 int32_t pingResult;
613 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
614 EXPECT_EQ(OK, pingResult);
615
616 EXPECT_EQ(0, MyBinderRpcSession::gNum);
617}
618
Steven Morelandc1635952021-04-01 16:20:47 +0000619TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000620 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000621
622 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
623 sp<IBinder> outBinder;
624 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
625 EXPECT_EQ(inBinder, outBinder);
626
627 wp<IBinder> weak = inBinder;
628 inBinder = nullptr;
629 outBinder = nullptr;
630
631 // Force reading a reply, to process any pending dec refs from the other
632 // process (the other process will process dec refs there before processing
633 // the ping here).
634 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
635
636 EXPECT_EQ(nullptr, weak.promote());
637
638 EXPECT_EQ(0, MyBinderRpcSession::gNum);
639}
640
Steven Morelandc1635952021-04-01 16:20:47 +0000641TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000642 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000643
644 sp<IBinderRpcSession> session;
645 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
646
647 sp<IBinder> inBinder = IInterface::asBinder(session);
648 sp<IBinder> outBinder;
649 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
650 EXPECT_EQ(inBinder, outBinder);
651
652 wp<IBinder> weak = inBinder;
653 session = nullptr;
654 inBinder = nullptr;
655 outBinder = nullptr;
656
657 // Force reading a reply, to process any pending dec refs from the other
658 // process (the other process will process dec refs there before processing
659 // the ping here).
660 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
661
662 EXPECT_EQ(nullptr, weak.promote());
663}
664
Steven Morelandc1635952021-04-01 16:20:47 +0000665TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000666 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000667
668 sp<IBinder> outBinder;
669 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
670 EXPECT_EQ(nullptr, outBinder);
671}
672
Steven Morelandc1635952021-04-01 16:20:47 +0000673TEST_P(BinderRpc, HoldBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000674 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000675
676 IBinder* ptr = nullptr;
677 {
678 sp<IBinder> binder = new BBinder();
679 ptr = binder.get();
680 EXPECT_OK(proc.rootIface->holdBinder(binder));
681 }
682
683 sp<IBinder> held;
684 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
685
686 EXPECT_EQ(held.get(), ptr);
687
688 // stop holding binder, because we test to make sure references are cleaned
689 // up
690 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
691 // and flush ref counts
692 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
693}
694
695// START TESTS FOR LIMITATIONS OF SOCKET BINDER
696// These are behavioral differences form regular binder, where certain usecases
697// aren't supported.
698
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000699TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000700 auto proc1 = createRpcTestSocketServerProcess({});
701 auto proc2 = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000702
703 sp<IBinder> outBinder;
704 EXPECT_EQ(INVALID_OPERATION,
705 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
706}
707
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000708TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000709 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2});
Steven Moreland736664b2021-05-01 04:27:25 +0000710
711 sp<IBinder> outBinder;
712 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000713 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000714 .transactionError());
715}
716
Steven Morelandc1635952021-04-01 16:20:47 +0000717TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000718 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000719
720 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
721 sp<IBinder> outBinder;
722 EXPECT_EQ(INVALID_OPERATION,
723 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
724}
725
Steven Morelandc1635952021-04-01 16:20:47 +0000726TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000727 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000728
729 // for historical reasons, IServiceManager interface only returns the
730 // exception code
731 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
732 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
733}
734
735// END TESTS FOR LIMITATIONS OF SOCKET BINDER
736
Steven Morelandc1635952021-04-01 16:20:47 +0000737TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000738 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000739
740 sp<IBinder> outBinder;
741 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
742 EXPECT_EQ(proc.rootBinder, outBinder);
743}
744
Steven Morelandc1635952021-04-01 16:20:47 +0000745TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000746 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000747
748 auto nastyNester = sp<MyBinderRpcTest>::make();
749 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
750
751 wp<IBinder> weak = nastyNester;
752 nastyNester = nullptr;
753 EXPECT_EQ(nullptr, weak.promote());
754}
755
Steven Morelandc1635952021-04-01 16:20:47 +0000756TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000757 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000758
759 sp<IBinder> a;
760 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
761
762 sp<IBinder> b;
763 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
764
765 EXPECT_EQ(a, b);
766}
767
Steven Morelandc1635952021-04-01 16:20:47 +0000768TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000769 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000770
771 sp<IBinder> a;
772 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
773 wp<IBinder> weak = a;
774 a = nullptr;
775
776 sp<IBinder> b;
777 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
778
779 // this is the wrong behavior, since BpBinder
780 // doesn't implement onIncStrongAttempted
781 // but make sure there is no crash
782 EXPECT_EQ(nullptr, weak.promote());
783
784 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
785
786 // In order to fix this:
787 // - need to have incStrongAttempted reflected across IPC boundary (wait for
788 // response to promote - round trip...)
789 // - sendOnLastWeakRef, to delete entries out of RpcState table
790 EXPECT_EQ(b, weak.promote());
791}
792
793#define expectSessions(expected, iface) \
794 do { \
795 int session; \
796 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
797 EXPECT_EQ(expected, session); \
798 } while (false)
799
Steven Morelandc1635952021-04-01 16:20:47 +0000800TEST_P(BinderRpc, SingleSession) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000801 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000802
803 sp<IBinderRpcSession> session;
804 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
805 std::string out;
806 EXPECT_OK(session->getName(&out));
807 EXPECT_EQ("aoeu", out);
808
809 expectSessions(1, proc.rootIface);
810 session = nullptr;
811 expectSessions(0, proc.rootIface);
812}
813
Steven Morelandc1635952021-04-01 16:20:47 +0000814TEST_P(BinderRpc, ManySessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000815 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000816
817 std::vector<sp<IBinderRpcSession>> sessions;
818
819 for (size_t i = 0; i < 15; i++) {
820 expectSessions(i, proc.rootIface);
821 sp<IBinderRpcSession> session;
822 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
823 sessions.push_back(session);
824 }
825 expectSessions(sessions.size(), proc.rootIface);
826 for (size_t i = 0; i < sessions.size(); i++) {
827 std::string out;
828 EXPECT_OK(sessions.at(i)->getName(&out));
829 EXPECT_EQ(std::to_string(i), out);
830 }
831 expectSessions(sessions.size(), proc.rootIface);
832
833 while (!sessions.empty()) {
834 sessions.pop_back();
835 expectSessions(sessions.size(), proc.rootIface);
836 }
837 expectSessions(0, proc.rootIface);
838}
839
840size_t epochMillis() {
841 using std::chrono::duration_cast;
842 using std::chrono::milliseconds;
843 using std::chrono::seconds;
844 using std::chrono::system_clock;
845 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
846}
847
Steven Morelandc1635952021-04-01 16:20:47 +0000848TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000849 constexpr size_t kNumThreads = 10;
850
Steven Moreland4313d7e2021-07-15 23:41:22 +0000851 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000852
853 EXPECT_OK(proc.rootIface->lock());
854
855 // block all but one thread taking locks
856 std::vector<std::thread> ts;
857 for (size_t i = 0; i < kNumThreads - 1; i++) {
858 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
859 }
860
861 usleep(100000); // give chance for calls on other threads
862
863 // other calls still work
864 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
865
866 constexpr size_t blockTimeMs = 500;
867 size_t epochMsBefore = epochMillis();
868 // after this, we should never see a response within this time
869 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
870
871 // this call should be blocked for blockTimeMs
872 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
873
874 size_t epochMsAfter = epochMillis();
875 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
876
877 for (auto& t : ts) t.join();
878}
879
Steven Morelandc1635952021-04-01 16:20:47 +0000880TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000881 constexpr size_t kNumThreads = 10;
882 constexpr size_t kNumCalls = kNumThreads + 3;
883 constexpr size_t kSleepMs = 500;
884
Steven Moreland4313d7e2021-07-15 23:41:22 +0000885 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000886
887 size_t epochMsBefore = epochMillis();
888
889 std::vector<std::thread> ts;
890 for (size_t i = 0; i < kNumCalls; i++) {
891 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
892 }
893
894 for (auto& t : ts) t.join();
895
896 size_t epochMsAfter = epochMillis();
897
898 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
899
900 // Potential flake, but make sure calls are handled in parallel.
901 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
902}
903
Steven Morelandc1635952021-04-01 16:20:47 +0000904TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000905 constexpr size_t kNumClientThreads = 10;
906 constexpr size_t kNumServerThreads = 10;
907 constexpr size_t kNumCalls = 100;
908
Steven Moreland4313d7e2021-07-15 23:41:22 +0000909 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000910
911 std::vector<std::thread> threads;
912 for (size_t i = 0; i < kNumClientThreads; i++) {
913 threads.push_back(std::thread([&] {
914 for (size_t j = 0; j < kNumCalls; j++) {
915 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000916 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000917 EXPECT_EQ(proc.rootBinder, out);
918 }
919 }));
920 }
921
922 for (auto& t : threads) t.join();
923}
924
Steven Morelandc6046982021-04-20 00:49:42 +0000925TEST_P(BinderRpc, OnewayStressTest) {
926 constexpr size_t kNumClientThreads = 10;
927 constexpr size_t kNumServerThreads = 10;
Steven Moreland52eee942021-06-03 00:59:28 +0000928 constexpr size_t kNumCalls = 500;
Steven Morelandc6046982021-04-20 00:49:42 +0000929
Steven Moreland4313d7e2021-07-15 23:41:22 +0000930 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Morelandc6046982021-04-20 00:49:42 +0000931
932 std::vector<std::thread> threads;
933 for (size_t i = 0; i < kNumClientThreads; i++) {
934 threads.push_back(std::thread([&] {
935 for (size_t j = 0; j < kNumCalls; j++) {
936 EXPECT_OK(proc.rootIface->sendString("a"));
937 }
938
939 // check threads are not stuck
940 EXPECT_OK(proc.rootIface->sleepMs(250));
941 }));
942 }
943
944 for (auto& t : threads) t.join();
945}
946
Steven Morelandc1635952021-04-01 16:20:47 +0000947TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000948 constexpr size_t kReallyLongTimeMs = 100;
949 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
950
Steven Moreland4313d7e2021-07-15 23:41:22 +0000951 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000952
953 size_t epochMsBefore = epochMillis();
954
955 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
956
957 size_t epochMsAfter = epochMillis();
958 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
959}
960
Steven Morelandc1635952021-04-01 16:20:47 +0000961TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000962 constexpr size_t kNumSleeps = 10;
963 constexpr size_t kNumExtraServerThreads = 4;
964 constexpr size_t kSleepMs = 50;
965
966 // make sure calls to the same object happen on the same thread
Steven Moreland4313d7e2021-07-15 23:41:22 +0000967 auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000968
969 EXPECT_OK(proc.rootIface->lock());
970
971 for (size_t i = 0; i < kNumSleeps; i++) {
972 // these should be processed serially
973 proc.rootIface->sleepMsAsync(kSleepMs);
974 }
975 // should also be processesed serially
976 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
977
978 size_t epochMsBefore = epochMillis();
979 EXPECT_OK(proc.rootIface->lockUnlock());
980 size_t epochMsAfter = epochMillis();
981
982 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
Steven Morelandf5174272021-05-25 00:39:28 +0000983
984 // pending oneway transactions hold ref, make sure we read data on all
985 // sockets
986 std::vector<std::thread> threads;
987 for (size_t i = 0; i < 1 + kNumExtraServerThreads; i++) {
988 threads.push_back(std::thread([&] { EXPECT_OK(proc.rootIface->sleepMs(250)); }));
989 }
990 for (auto& t : threads) t.join();
Steven Moreland5553ac42020-11-11 02:14:45 +0000991}
992
Steven Morelandd45be622021-06-04 02:19:37 +0000993TEST_P(BinderRpc, OnewayCallExhaustion) {
994 constexpr size_t kNumClients = 2;
995 constexpr size_t kTooLongMs = 1000;
996
Steven Moreland4313d7e2021-07-15 23:41:22 +0000997 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2});
Steven Morelandd45be622021-06-04 02:19:37 +0000998
999 // Build up oneway calls on the second session to make sure it terminates
1000 // and shuts down. The first session should be unaffected (proc destructor
1001 // checks the first session).
1002 auto iface = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root);
1003
1004 std::vector<std::thread> threads;
1005 for (size_t i = 0; i < kNumClients; i++) {
1006 // one of these threads will get stuck queueing a transaction once the
1007 // socket fills up, the other will be able to fill up transactions on
1008 // this object
1009 threads.push_back(std::thread([&] {
1010 while (iface->sleepMsAsync(kTooLongMs).isOk()) {
1011 }
1012 }));
1013 }
1014 for (auto& t : threads) t.join();
1015
1016 Status status = iface->sleepMsAsync(kTooLongMs);
1017 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1018
Steven Moreland798e0d12021-07-14 23:19:25 +00001019 // now that it has died, wait for the remote session to shutdown
1020 std::vector<int32_t> remoteCounts;
1021 do {
1022 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
1023 } while (remoteCounts.size() == kNumClients);
1024
Steven Morelandd45be622021-06-04 02:19:37 +00001025 // the second session should be shutdown in the other process by the time we
1026 // are able to join above (it'll only be hung up once it finishes processing
1027 // any pending commands). We need to erase this session from the record
1028 // here, so that the destructor for our session won't check that this
1029 // session is valid, but we still want it to test the other session.
1030 proc.proc.sessions.erase(proc.proc.sessions.begin() + 1);
1031}
1032
Steven Moreland659416d2021-05-11 00:47:50 +00001033TEST_P(BinderRpc, Callbacks) {
1034 const static std::string kTestString = "good afternoon!";
1035
Steven Morelandc7d40132021-06-10 03:42:11 +00001036 for (bool callIsOneway : {true, false}) {
1037 for (bool callbackIsOneway : {true, false}) {
1038 for (bool delayed : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001039 auto proc = createRpcTestSocketServerProcess(
1040 {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1});
Steven Morelandc7d40132021-06-10 03:42:11 +00001041 auto cb = sp<MyBinderRpcCallback>::make();
Steven Moreland659416d2021-05-11 00:47:50 +00001042
Steven Morelandc7d40132021-06-10 03:42:11 +00001043 if (callIsOneway) {
1044 EXPECT_OK(proc.rootIface->doCallbackAsync(cb, callbackIsOneway, delayed,
1045 kTestString));
1046 } else {
1047 EXPECT_OK(
1048 proc.rootIface->doCallback(cb, callbackIsOneway, delayed, kTestString));
1049 }
Steven Moreland659416d2021-05-11 00:47:50 +00001050
Steven Morelandc7d40132021-06-10 03:42:11 +00001051 using std::literals::chrono_literals::operator""s;
1052 std::unique_lock<std::mutex> _l(cb->mMutex);
1053 cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); });
Steven Moreland659416d2021-05-11 00:47:50 +00001054
Steven Morelandc7d40132021-06-10 03:42:11 +00001055 EXPECT_EQ(cb->mValues.size(), 1)
1056 << "callIsOneway: " << callIsOneway
1057 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
1058 if (cb->mValues.empty()) continue;
1059 EXPECT_EQ(cb->mValues.at(0), kTestString)
1060 << "callIsOneway: " << callIsOneway
1061 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
Steven Moreland659416d2021-05-11 00:47:50 +00001062
Steven Morelandc7d40132021-06-10 03:42:11 +00001063 // since we are severing the connection, we need to go ahead and
1064 // tell the server to shutdown and exit so that waitpid won't hang
Steven Moreland798e0d12021-07-14 23:19:25 +00001065 if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) {
1066 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1067 }
Steven Moreland659416d2021-05-11 00:47:50 +00001068
Steven Moreland1b304292021-07-15 22:59:34 +00001069 // since this session has an incoming connection w/ a threadpool, we
Steven Morelandc7d40132021-06-10 03:42:11 +00001070 // need to manually shut it down
1071 EXPECT_TRUE(proc.proc.sessions.at(0).session->shutdownAndWait(true));
Steven Moreland659416d2021-05-11 00:47:50 +00001072
Steven Morelandc7d40132021-06-10 03:42:11 +00001073 proc.expectAlreadyShutdown = true;
1074 }
Steven Moreland659416d2021-05-11 00:47:50 +00001075 }
1076 }
1077}
1078
Steven Moreland195edb82021-06-08 02:44:39 +00001079TEST_P(BinderRpc, OnewayCallbackWithNoThread) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001080 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland195edb82021-06-08 02:44:39 +00001081 auto cb = sp<MyBinderRpcCallback>::make();
1082
1083 Status status = proc.rootIface->doCallback(cb, true /*oneway*/, false /*delayed*/, "anything");
1084 EXPECT_EQ(WOULD_BLOCK, status.transactionError());
1085}
1086
Steven Morelandc1635952021-04-01 16:20:47 +00001087TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001088 for (bool doDeathCleanup : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001089 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +00001090
1091 // make sure there is some state during crash
1092 // 1. we hold their binder
1093 sp<IBinderRpcSession> session;
1094 EXPECT_OK(proc.rootIface->openSession("happy", &session));
1095 // 2. they hold our binder
1096 sp<IBinder> binder = new BBinder();
1097 EXPECT_OK(proc.rootIface->holdBinder(binder));
1098
1099 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
1100 << "Do death cleanup: " << doDeathCleanup;
1101
Steven Morelandaf4ca712021-05-24 23:22:08 +00001102 proc.expectAlreadyShutdown = true;
Steven Moreland5553ac42020-11-11 02:14:45 +00001103 }
1104}
1105
Steven Morelandd7302072021-05-15 01:32:04 +00001106TEST_P(BinderRpc, UseKernelBinderCallingId) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001107 auto proc = createRpcTestSocketServerProcess({});
Steven Morelandd7302072021-05-15 01:32:04 +00001108
1109 // we can't allocate IPCThreadState so actually the first time should
1110 // succeed :(
1111 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
1112
1113 // second time! we catch the error :)
1114 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
1115
Steven Morelandaf4ca712021-05-24 23:22:08 +00001116 proc.expectAlreadyShutdown = true;
Steven Morelandd7302072021-05-15 01:32:04 +00001117}
1118
Steven Moreland37aff182021-03-26 02:04:16 +00001119TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001120 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001121
1122 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1123 ASSERT_NE(binder, nullptr);
1124
1125 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1126}
1127
1128TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001129 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001130
1131 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1132 ASSERT_NE(binder, nullptr);
1133
1134 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
1135 ASSERT_NE(ndkBinder, nullptr);
1136
1137 std::string out;
1138 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
1139 ASSERT_TRUE(status.isOk()) << status.getDescription();
1140 ASSERT_EQ("aoeuaoeu", out);
1141}
1142
Steven Moreland5553ac42020-11-11 02:14:45 +00001143ssize_t countFds() {
1144 DIR* dir = opendir("/proc/self/fd/");
1145 if (dir == nullptr) return -1;
1146 ssize_t ret = 0;
1147 dirent* ent;
1148 while ((ent = readdir(dir)) != nullptr) ret++;
1149 closedir(dir);
1150 return ret;
1151}
1152
Steven Morelandc1635952021-04-01 16:20:47 +00001153TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001154 ssize_t beforeFds = countFds();
1155 ASSERT_GE(beforeFds, 0);
1156 {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001157 auto proc = createRpcTestSocketServerProcess({.numThreads = 10});
Steven Moreland5553ac42020-11-11 02:14:45 +00001158 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
1159 }
1160 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
1161}
1162
Steven Morelandda573042021-06-12 01:13:45 +00001163static bool testSupportVsockLoopback() {
Yifan Hong702115c2021-06-24 15:39:18 -07001164 // We don't need to enable TLS to know if vsock is supported.
Steven Morelandda573042021-06-12 01:13:45 +00001165 unsigned int vsockPort = allocateVsockPort();
Yifan Hong702115c2021-06-24 15:39:18 -07001166 sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make());
Steven Morelandda573042021-06-12 01:13:45 +00001167 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1168 CHECK(server->setupVsockServer(vsockPort));
1169 server->start();
1170
Yifan Hong702115c2021-06-24 15:39:18 -07001171 sp<RpcSession> session = RpcSession::make(RpcTransportCtxFactoryRaw::make());
Steven Morelandda573042021-06-12 01:13:45 +00001172 bool okay = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort);
Steven Moreland798e0d12021-07-14 23:19:25 +00001173 while (!server->shutdown()) usleep(10000);
Steven Morelandda573042021-06-12 01:13:45 +00001174 ALOGE("Detected vsock loopback supported: %d", okay);
1175 return okay;
1176}
1177
1178static std::vector<SocketType> testSocketTypes() {
1179 std::vector<SocketType> ret = {SocketType::UNIX, SocketType::INET};
1180
1181 static bool hasVsockLoopback = testSupportVsockLoopback();
1182
1183 if (hasVsockLoopback) {
1184 ret.push_back(SocketType::VSOCK);
1185 }
1186
1187 return ret;
1188}
1189
Yifan Hong702115c2021-06-24 15:39:18 -07001190INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
1191 ::testing::Combine(::testing::ValuesIn(testSocketTypes()),
1192 ::testing::ValuesIn(RpcSecurityValues())),
1193 BinderRpc::PrintParamInfo);
Steven Morelandc1635952021-04-01 16:20:47 +00001194
Yifan Hong702115c2021-06-24 15:39:18 -07001195class BinderRpcServerRootObject
1196 : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001197
1198TEST_P(BinderRpcServerRootObject, WeakRootObject) {
1199 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
1200 auto setRootObject = [](bool isStrong) -> SetFn {
1201 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
1202 };
1203
Yifan Hong702115c2021-06-24 15:39:18 -07001204 auto [isStrong1, isStrong2, rpcSecurity] = GetParam();
1205 auto server = RpcServer::make(newFactory(rpcSecurity));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001206 auto binder1 = sp<BBinder>::make();
1207 IBinder* binderRaw1 = binder1.get();
1208 setRootObject(isStrong1)(server.get(), binder1);
1209 EXPECT_EQ(binderRaw1, server->getRootObject());
1210 binder1.clear();
1211 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
1212
1213 auto binder2 = sp<BBinder>::make();
1214 IBinder* binderRaw2 = binder2.get();
1215 setRootObject(isStrong2)(server.get(), binder2);
1216 EXPECT_EQ(binderRaw2, server->getRootObject());
1217 binder2.clear();
1218 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
1219}
1220
1221INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
Yifan Hong702115c2021-06-24 15:39:18 -07001222 ::testing::Combine(::testing::Bool(), ::testing::Bool(),
1223 ::testing::ValuesIn(RpcSecurityValues())));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001224
Yifan Hong1a235852021-05-13 16:07:47 -07001225class OneOffSignal {
1226public:
1227 // If notify() was previously called, or is called within |duration|, return true; else false.
1228 template <typename R, typename P>
1229 bool wait(std::chrono::duration<R, P> duration) {
1230 std::unique_lock<std::mutex> lock(mMutex);
1231 return mCv.wait_for(lock, duration, [this] { return mValue; });
1232 }
1233 void notify() {
1234 std::unique_lock<std::mutex> lock(mMutex);
1235 mValue = true;
1236 lock.unlock();
1237 mCv.notify_all();
1238 }
1239
1240private:
1241 std::mutex mMutex;
1242 std::condition_variable mCv;
1243 bool mValue = false;
1244};
1245
Yifan Hong702115c2021-06-24 15:39:18 -07001246TEST_P(BinderRpcSimple, Shutdown) {
Yifan Hong1a235852021-05-13 16:07:47 -07001247 auto addr = allocateSocketAddress();
1248 unlink(addr.c_str());
Yifan Hong702115c2021-06-24 15:39:18 -07001249 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong1a235852021-05-13 16:07:47 -07001250 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1251 ASSERT_TRUE(server->setupUnixDomainServer(addr.c_str()));
1252 auto joinEnds = std::make_shared<OneOffSignal>();
1253
1254 // If things are broken and the thread never stops, don't block other tests. Because the thread
1255 // may run after the test finishes, it must not access the stack memory of the test. Hence,
1256 // shared pointers are passed.
1257 std::thread([server, joinEnds] {
1258 server->join();
1259 joinEnds->notify();
1260 }).detach();
1261
1262 bool shutdown = false;
1263 for (int i = 0; i < 10 && !shutdown; i++) {
1264 usleep(300 * 1000); // 300ms; total 3s
1265 if (server->shutdown()) shutdown = true;
1266 }
1267 ASSERT_TRUE(shutdown) << "server->shutdown() never returns true";
1268
1269 ASSERT_TRUE(joinEnds->wait(2s))
1270 << "After server->shutdown() returns true, join() did not stop after 2s";
1271}
1272
Yifan Hong194acf22021-06-29 18:44:56 -07001273TEST(BinderRpc, Java) {
1274#if !defined(__ANDROID__)
1275 GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on"
1276 "createRpcDelegateServiceManager() with a device attached, such test belongs "
1277 "to binderHostDeviceTest. Hence, just disable this test on host.";
1278#endif // !__ANDROID__
1279 sp<IServiceManager> sm = defaultServiceManager();
1280 ASSERT_NE(nullptr, sm);
1281 // Any Java service with non-empty getInterfaceDescriptor() would do.
1282 // Let's pick batteryproperties.
1283 auto binder = sm->checkService(String16("batteryproperties"));
1284 ASSERT_NE(nullptr, binder);
1285 auto descriptor = binder->getInterfaceDescriptor();
1286 ASSERT_GE(descriptor.size(), 0);
1287 ASSERT_EQ(OK, binder->pingBinder());
1288
1289 auto rpcServer = RpcServer::make();
1290 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1291 unsigned int port;
Devin Mooref3b9c4f2021-08-03 15:50:13 +00001292 ASSERT_TRUE(rpcServer->setupInetServer(kLocalInetAddress, 0, &port));
Yifan Hong194acf22021-06-29 18:44:56 -07001293 auto socket = rpcServer->releaseServer();
1294
1295 auto keepAlive = sp<BBinder>::make();
1296 ASSERT_EQ(OK, binder->setRpcClientDebug(std::move(socket), keepAlive));
1297
1298 auto rpcSession = RpcSession::make();
1299 ASSERT_TRUE(rpcSession->setupInetClient("127.0.0.1", port));
1300 auto rpcBinder = rpcSession->getRootObject();
1301 ASSERT_NE(nullptr, rpcBinder);
1302
1303 ASSERT_EQ(OK, rpcBinder->pingBinder());
1304
1305 ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor())
1306 << "getInterfaceDescriptor should not crash system_server";
1307 ASSERT_EQ(OK, rpcBinder->pingBinder());
1308}
1309
Yifan Hong702115c2021-06-24 15:39:18 -07001310INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcSimple, ::testing::ValuesIn(RpcSecurityValues()),
1311 BinderRpcSimple::PrintTestParam);
1312
Steven Morelandc1635952021-04-01 16:20:47 +00001313} // namespace android
1314
1315int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001316 ::testing::InitGoogleTest(&argc, argv);
1317 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
1318 return RUN_ALL_TESTS();
1319}