blob: d01e9d709042a346cdacb0053c21c0469a7eba4a [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
Andrei Homescu9d8adb12022-08-02 04:38:30 +000017#include <aidl/IBinderRpcTest.h>
Frederick Maylea12b0962022-06-25 01:13:22 +000018#include <android-base/stringprintf.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000019
Steven Morelandc1635952021-04-01 16:20:47 +000020#include <chrono>
21#include <cstdlib>
22#include <iostream>
23#include <thread>
Steven Moreland659416d2021-05-11 00:47:50 +000024#include <type_traits>
Steven Morelandc1635952021-04-01 16:20:47 +000025
Andrei Homescu2a298012022-06-15 01:08:54 +000026#include <dlfcn.h>
Yifan Hong1deca4b2021-09-10 16:16:44 -070027#include <poll.h>
Steven Morelandc1635952021-04-01 16:20:47 +000028#include <sys/prctl.h>
Andrei Homescu992a4052022-06-28 21:26:18 +000029#include <sys/socket.h>
Steven Morelandc1635952021-04-01 16:20:47 +000030
Andrei Homescud65666d2023-03-03 07:28:02 +000031#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +000032#include <binder/RpcTransportTipcAndroid.h>
33#include <trusty/tipc.h>
Andrei Homescud65666d2023-03-03 07:28:02 +000034#endif // BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +000035
Andrei Homescu2a298012022-06-15 01:08:54 +000036#include "binderRpcTestCommon.h"
Andrei Homescu96834632022-10-14 00:49:49 +000037#include "binderRpcTestFixture.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000038
Yifan Hong1a235852021-05-13 16:07:47 -070039using namespace std::chrono_literals;
Yifan Hong67519322021-09-13 18:51:16 -070040using namespace std::placeholders;
Yifan Hong1deca4b2021-09-10 16:16:44 -070041using testing::AssertionFailure;
42using testing::AssertionResult;
43using testing::AssertionSuccess;
Yifan Hong1a235852021-05-13 16:07:47 -070044
Steven Moreland5553ac42020-11-11 02:14:45 +000045namespace android {
46
Andrei Homescu12106de2022-04-27 04:42:21 +000047#ifdef BINDER_TEST_NO_SHARED_LIBS
48constexpr bool kEnableSharedLibs = false;
49#else
50constexpr bool kEnableSharedLibs = true;
51#endif
52
Andrei Homescud65666d2023-03-03 07:28:02 +000053#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +000054constexpr char kTrustyIpcDevice[] = "/dev/trusty-ipc-dev0";
55#endif
56
Frederick Maylea12b0962022-06-25 01:13:22 +000057static std::string WaitStatusToString(int wstatus) {
58 if (WIFEXITED(wstatus)) {
59 return base::StringPrintf("exit status %d", WEXITSTATUS(wstatus));
60 }
61 if (WIFSIGNALED(wstatus)) {
62 return base::StringPrintf("term signal %d", WTERMSIG(wstatus));
63 }
64 return base::StringPrintf("unexpected state %d", wstatus);
65}
66
Steven Moreland276d8df2022-09-28 23:56:39 +000067static void debugBacktrace(pid_t pid) {
68 std::cerr << "TAKING BACKTRACE FOR PID " << pid << std::endl;
69 system((std::string("debuggerd -b ") + std::to_string(pid)).c_str());
70}
71
Steven Moreland5553ac42020-11-11 02:14:45 +000072class Process {
73public:
Andrei Homescu96834632022-10-14 00:49:49 +000074 Process(Process&& other)
75 : mCustomExitStatusCheck(std::move(other.mCustomExitStatusCheck)),
76 mReadEnd(std::move(other.mReadEnd)),
77 mWriteEnd(std::move(other.mWriteEnd)) {
78 // The default move constructor doesn't clear mPid after moving it,
79 // which we need to do because the destructor checks for mPid!=0
80 mPid = other.mPid;
81 other.mPid = 0;
82 }
Yifan Hong1deca4b2021-09-10 16:16:44 -070083 Process(const std::function<void(android::base::borrowed_fd /* writeEnd */,
84 android::base::borrowed_fd /* readEnd */)>& f) {
85 android::base::unique_fd childWriteEnd;
86 android::base::unique_fd childReadEnd;
Andrei Homescu2a298012022-06-15 01:08:54 +000087 CHECK(android::base::Pipe(&mReadEnd, &childWriteEnd, 0)) << strerror(errno);
88 CHECK(android::base::Pipe(&childReadEnd, &mWriteEnd, 0)) << strerror(errno);
Steven Moreland5553ac42020-11-11 02:14:45 +000089 if (0 == (mPid = fork())) {
90 // racey: assume parent doesn't crash before this is set
91 prctl(PR_SET_PDEATHSIG, SIGHUP);
92
Yifan Hong1deca4b2021-09-10 16:16:44 -070093 f(childWriteEnd, childReadEnd);
Steven Morelandaf4ca712021-05-24 23:22:08 +000094
95 exit(0);
Steven Moreland5553ac42020-11-11 02:14:45 +000096 }
97 }
98 ~Process() {
99 if (mPid != 0) {
Frederick Maylea12b0962022-06-25 01:13:22 +0000100 int wstatus;
101 waitpid(mPid, &wstatus, 0);
102 if (mCustomExitStatusCheck) {
103 mCustomExitStatusCheck(wstatus);
104 } else {
105 EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0)
106 << "server process failed: " << WaitStatusToString(wstatus);
107 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000108 }
109 }
Yifan Hong0f58fb92021-06-16 16:09:23 -0700110 android::base::borrowed_fd readEnd() { return mReadEnd; }
Yifan Hong1deca4b2021-09-10 16:16:44 -0700111 android::base::borrowed_fd writeEnd() { return mWriteEnd; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000112
Frederick Maylea12b0962022-06-25 01:13:22 +0000113 void setCustomExitStatusCheck(std::function<void(int wstatus)> f) {
114 mCustomExitStatusCheck = std::move(f);
115 }
116
Frederick Mayle69a0c992022-05-26 20:38:39 +0000117 // Kill the process. Avoid if possible. Shutdown gracefully via an RPC instead.
118 void terminate() { kill(mPid, SIGTERM); }
119
Steven Moreland276d8df2022-09-28 23:56:39 +0000120 pid_t getPid() { return mPid; }
121
Steven Moreland5553ac42020-11-11 02:14:45 +0000122private:
Frederick Maylea12b0962022-06-25 01:13:22 +0000123 std::function<void(int wstatus)> mCustomExitStatusCheck;
Steven Moreland5553ac42020-11-11 02:14:45 +0000124 pid_t mPid = 0;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700125 android::base::unique_fd mReadEnd;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700126 android::base::unique_fd mWriteEnd;
Steven Moreland5553ac42020-11-11 02:14:45 +0000127};
128
129static std::string allocateSocketAddress() {
130 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000131 std::string temp = getenv("TMPDIR") ?: "/tmp";
Steven Morelanddfb05ad2023-03-07 17:00:53 +0000132 auto ret = temp + "/binderRpcTest_" + std::to_string(getpid()) + "_" + std::to_string(id++);
Yifan Hong1deca4b2021-09-10 16:16:44 -0700133 unlink(ret.c_str());
134 return ret;
Steven Moreland5553ac42020-11-11 02:14:45 +0000135};
136
Steven Morelandda573042021-06-12 01:13:45 +0000137static unsigned int allocateVsockPort() {
Andrei Homescu2a298012022-06-15 01:08:54 +0000138 static unsigned int vsockPort = 34567;
Steven Morelandda573042021-06-12 01:13:45 +0000139 return vsockPort++;
140}
141
Alice Wang893a9912022-10-24 10:44:09 +0000142static base::unique_fd initUnixSocket(std::string addr) {
143 auto socket_addr = UnixSocketAddress(addr.c_str());
144 base::unique_fd fd(
145 TEMP_FAILURE_RETRY(socket(socket_addr.addr()->sa_family, SOCK_STREAM, AF_UNIX)));
146 CHECK(fd.ok());
147 CHECK_EQ(0, TEMP_FAILURE_RETRY(bind(fd.get(), socket_addr.addr(), socket_addr.addrSize())));
148 return fd;
149}
150
Andrei Homescu96834632022-10-14 00:49:49 +0000151// Destructors need to be defined, even if pure virtual
152ProcessSession::~ProcessSession() {}
153
154class LinuxProcessSession : public ProcessSession {
155public:
Steven Moreland5553ac42020-11-11 02:14:45 +0000156 // reference to process hosting a socket server
157 Process host;
158
Andrei Homescu96834632022-10-14 00:49:49 +0000159 LinuxProcessSession(LinuxProcessSession&&) = default;
160 LinuxProcessSession(Process&& host) : host(std::move(host)) {}
161 ~LinuxProcessSession() override {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000162 for (auto& session : sessions) {
163 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000164 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000165
Steven Moreland67f85902023-03-15 01:13:49 +0000166 for (size_t sessionNum = 0; sessionNum < sessions.size(); sessionNum++) {
167 auto& info = sessions.at(sessionNum);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000168 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000169
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000170 EXPECT_NE(nullptr, session);
171 EXPECT_NE(nullptr, session->state());
172 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000173
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000174 wp<RpcSession> weakSession = session;
175 session = nullptr;
Steven Moreland276d8df2022-09-28 23:56:39 +0000176
Steven Moreland57042712022-10-04 23:56:45 +0000177 // b/244325464 - 'getStrongCount' is printing '1' on failure here, which indicates the
178 // the object should not actually be promotable. By looping, we distinguish a race here
179 // from a bug causing the object to not be promotable.
180 for (size_t i = 0; i < 3; i++) {
181 sp<RpcSession> strongSession = weakSession.promote();
182 EXPECT_EQ(nullptr, strongSession)
Steven Moreland67f85902023-03-15 01:13:49 +0000183 << "For session " << sessionNum << ". "
Steven Moreland57042712022-10-04 23:56:45 +0000184 << (debugBacktrace(host.getPid()), debugBacktrace(getpid()),
185 "Leaked sess: ")
186 << strongSession->getStrongCount() << " checked time " << i;
187
188 if (strongSession != nullptr) {
189 sleep(1);
190 }
191 }
Steven Moreland736664b2021-05-01 04:27:25 +0000192 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000193 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000194
Andrei Homescu96834632022-10-14 00:49:49 +0000195 void setCustomExitStatusCheck(std::function<void(int wstatus)> f) override {
196 host.setCustomExitStatusCheck(std::move(f));
Steven Moreland5553ac42020-11-11 02:14:45 +0000197 }
Andrei Homescu96834632022-10-14 00:49:49 +0000198
199 void terminate() override { host.terminate(); }
Steven Moreland5553ac42020-11-11 02:14:45 +0000200};
201
Yifan Hong1deca4b2021-09-10 16:16:44 -0700202static base::unique_fd connectTo(const RpcSocketAddress& addr) {
Steven Moreland4198a122021-08-03 17:37:58 -0700203 base::unique_fd serverFd(
204 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
205 int savedErrno = errno;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700206 CHECK(serverFd.ok()) << "Could not create socket " << addr.toString() << ": "
207 << strerror(savedErrno);
Steven Moreland4198a122021-08-03 17:37:58 -0700208
209 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
210 int savedErrno = errno;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700211 LOG(FATAL) << "Could not connect to socket " << addr.toString() << ": "
212 << strerror(savedErrno);
Steven Moreland4198a122021-08-03 17:37:58 -0700213 }
214 return serverFd;
215}
216
Andrei Homescud65666d2023-03-03 07:28:02 +0000217#ifndef BINDER_RPC_TO_TRUSTY_TEST
David Brazdil21c887c2022-09-23 12:25:18 +0100218static base::unique_fd connectToUnixBootstrap(const RpcTransportFd& transportFd) {
219 base::unique_fd sockClient, sockServer;
220 if (!base::Socketpair(SOCK_STREAM, &sockClient, &sockServer)) {
221 int savedErrno = errno;
222 LOG(FATAL) << "Failed socketpair(): " << strerror(savedErrno);
223 }
224
225 int zero = 0;
226 iovec iov{&zero, sizeof(zero)};
227 std::vector<std::variant<base::unique_fd, base::borrowed_fd>> fds;
228 fds.emplace_back(std::move(sockServer));
229
230 if (sendMessageOnSocket(transportFd, &iov, 1, &fds) < 0) {
231 int savedErrno = errno;
232 LOG(FATAL) << "Failed sendMessageOnSocket: " << strerror(savedErrno);
233 }
234 return std::move(sockClient);
235}
Andrei Homescud65666d2023-03-03 07:28:02 +0000236#endif // BINDER_RPC_TO_TRUSTY_TEST
David Brazdil21c887c2022-09-23 12:25:18 +0100237
Andrei Homescuf30148c2023-03-10 00:31:45 +0000238std::unique_ptr<RpcTransportCtxFactory> BinderRpc::newFactory(RpcSecurity rpcSecurity) {
239 return newTlsFactory(rpcSecurity);
Andrei Homescu96834632022-10-14 00:49:49 +0000240}
Andrei Homescu2a298012022-06-15 01:08:54 +0000241
Andrei Homescu96834632022-10-14 00:49:49 +0000242// This creates a new process serving an interface on a certain number of
243// threads.
244std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
245 const BinderRpcOptions& options) {
246 CHECK_GE(options.numSessions, 1) << "Must have at least one session to a server";
Frederick Mayle69a0c992022-05-26 20:38:39 +0000247
Steven Moreland67f85902023-03-15 01:13:49 +0000248 if (options.numIncomingConnectionsBySession.size() != 0) {
249 CHECK_EQ(options.numIncomingConnectionsBySession.size(), options.numSessions);
250 }
251
Andrei Homescu96834632022-10-14 00:49:49 +0000252 SocketType socketType = std::get<0>(GetParam());
253 RpcSecurity rpcSecurity = std::get<1>(GetParam());
254 uint32_t clientVersion = std::get<2>(GetParam());
255 uint32_t serverVersion = std::get<3>(GetParam());
256 bool singleThreaded = std::get<4>(GetParam());
257 bool noKernel = std::get<5>(GetParam());
258
259 std::string path = android::base::GetExecutableDirectory();
260 auto servicePath = android::base::StringPrintf("%s/binder_rpc_test_service%s%s", path.c_str(),
261 singleThreaded ? "_single_threaded" : "",
262 noKernel ? "_no_kernel" : "");
263
Alice Wang1ef010b2022-11-14 09:09:25 +0000264 base::unique_fd bootstrapClientFd, socketFd;
265
Alice Wang893a9912022-10-24 10:44:09 +0000266 auto addr = allocateSocketAddress();
267 // Initializes the socket before the fork/exec.
268 if (socketType == SocketType::UNIX_RAW) {
269 socketFd = initUnixSocket(addr);
Alice Wang1ef010b2022-11-14 09:09:25 +0000270 } else if (socketType == SocketType::UNIX_BOOTSTRAP) {
271 // Do not set O_CLOEXEC, bootstrapServerFd needs to survive fork/exec.
272 // This is because we cannot pass ParcelFileDescriptor over a pipe.
273 if (!base::Socketpair(SOCK_STREAM, &bootstrapClientFd, &socketFd)) {
274 int savedErrno = errno;
275 LOG(FATAL) << "Failed socketpair(): " << strerror(savedErrno);
276 }
Alice Wang893a9912022-10-24 10:44:09 +0000277 }
Andrei Homescua858b0e2022-08-01 23:43:09 +0000278
Andrei Homescu96834632022-10-14 00:49:49 +0000279 auto ret = std::make_unique<LinuxProcessSession>(
280 Process([=](android::base::borrowed_fd writeEnd, android::base::borrowed_fd readEnd) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000281 if (socketType == SocketType::TIPC) {
282 // Trusty has a single persistent service
283 return;
284 }
285
Andrei Homescu96834632022-10-14 00:49:49 +0000286 auto writeFd = std::to_string(writeEnd.get());
287 auto readFd = std::to_string(readEnd.get());
288 execl(servicePath.c_str(), servicePath.c_str(), writeFd.c_str(), readFd.c_str(),
289 NULL);
290 }));
291
292 BinderRpcTestServerConfig serverConfig;
293 serverConfig.numThreads = options.numThreads;
294 serverConfig.socketType = static_cast<int32_t>(socketType);
295 serverConfig.rpcSecurity = static_cast<int32_t>(rpcSecurity);
296 serverConfig.serverVersion = serverVersion;
297 serverConfig.vsockPort = allocateVsockPort();
Alice Wang893a9912022-10-24 10:44:09 +0000298 serverConfig.addr = addr;
Alice Wang893a9912022-10-24 10:44:09 +0000299 serverConfig.socketFd = socketFd.get();
Andrei Homescu96834632022-10-14 00:49:49 +0000300 for (auto mode : options.serverSupportedFileDescriptorTransportModes) {
301 serverConfig.serverSupportedFileDescriptorTransportModes.push_back(
302 static_cast<int32_t>(mode));
303 }
Andrei Homescu68a55612022-08-02 01:25:15 +0000304 if (socketType != SocketType::TIPC) {
305 writeToFd(ret->host.writeEnd(), serverConfig);
306 }
Andrei Homescu96834632022-10-14 00:49:49 +0000307
308 std::vector<sp<RpcSession>> sessions;
309 auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>();
310 for (size_t i = 0; i < options.numSessions; i++) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000311 std::unique_ptr<RpcTransportCtxFactory> factory;
312 if (socketType == SocketType::TIPC) {
Andrei Homescud65666d2023-03-03 07:28:02 +0000313#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +0000314 factory = RpcTransportCtxFactoryTipcAndroid::make();
315#else
316 LOG_ALWAYS_FATAL("TIPC socket type only supported on vendor");
317#endif
318 } else {
Andrei Homescuf30148c2023-03-10 00:31:45 +0000319 factory = newTlsFactory(rpcSecurity, certVerifier);
Andrei Homescu68a55612022-08-02 01:25:15 +0000320 }
321 sessions.emplace_back(RpcSession::make(std::move(factory)));
David Brazdil21c887c2022-09-23 12:25:18 +0100322 }
323
Andrei Homescu68a55612022-08-02 01:25:15 +0000324 BinderRpcTestServerInfo serverInfo;
325 if (socketType != SocketType::TIPC) {
326 serverInfo = readFromFd<BinderRpcTestServerInfo>(ret->host.readEnd());
327 BinderRpcTestClientInfo clientInfo;
328 for (const auto& session : sessions) {
329 auto& parcelableCert = clientInfo.certs.emplace_back();
330 parcelableCert.data = session->getCertificate(RpcCertificateFormat::PEM);
331 }
332 writeToFd(ret->host.writeEnd(), clientInfo);
Andrei Homescu96834632022-10-14 00:49:49 +0000333
Andrei Homescu68a55612022-08-02 01:25:15 +0000334 CHECK_LE(serverInfo.port, std::numeric_limits<unsigned int>::max());
335 if (socketType == SocketType::INET) {
336 CHECK_NE(0, serverInfo.port);
337 }
Frederick Mayle69a0c992022-05-26 20:38:39 +0000338
Andrei Homescu68a55612022-08-02 01:25:15 +0000339 if (rpcSecurity == RpcSecurity::TLS) {
340 const auto& serverCert = serverInfo.cert.data;
341 CHECK_EQ(OK,
342 certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM,
343 serverCert));
344 }
Yifan Hong1deca4b2021-09-10 16:16:44 -0700345 }
346
Andrei Homescu96834632022-10-14 00:49:49 +0000347 status_t status;
Steven Moreland736664b2021-05-01 04:27:25 +0000348
Steven Moreland67f85902023-03-15 01:13:49 +0000349 for (size_t i = 0; i < sessions.size(); i++) {
350 const auto& session = sessions.at(i);
351
352 size_t numIncoming = options.numIncomingConnectionsBySession.size() > 0
353 ? options.numIncomingConnectionsBySession.at(i)
354 : 0;
355
Andrei Homescu96834632022-10-14 00:49:49 +0000356 CHECK(session->setProtocolVersion(clientVersion));
Steven Moreland67f85902023-03-15 01:13:49 +0000357 session->setMaxIncomingThreads(numIncoming);
Steven Morelandfeb13e82023-03-01 01:25:33 +0000358 session->setMaxOutgoingConnections(options.numOutgoingConnections);
Andrei Homescu96834632022-10-14 00:49:49 +0000359 session->setFileDescriptorTransportMode(options.clientFileDescriptorTransportMode);
Steven Morelandc1635952021-04-01 16:20:47 +0000360
Andrei Homescu96834632022-10-14 00:49:49 +0000361 switch (socketType) {
362 case SocketType::PRECONNECTED:
363 status = session->setupPreconnectedClient({}, [=]() {
364 return connectTo(UnixSocketAddress(serverConfig.addr.c_str()));
365 });
Frederick Mayle69a0c992022-05-26 20:38:39 +0000366 break;
Alice Wang893a9912022-10-24 10:44:09 +0000367 case SocketType::UNIX_RAW:
Andrei Homescu96834632022-10-14 00:49:49 +0000368 case SocketType::UNIX:
369 status = session->setupUnixDomainClient(serverConfig.addr.c_str());
370 break;
371 case SocketType::UNIX_BOOTSTRAP:
372 status = session->setupUnixDomainSocketBootstrapClient(
373 base::unique_fd(dup(bootstrapClientFd.get())));
374 break;
375 case SocketType::VSOCK:
376 status = session->setupVsockClient(VMADDR_CID_LOCAL, serverConfig.vsockPort);
377 break;
378 case SocketType::INET:
379 status = session->setupInetClient("127.0.0.1", serverInfo.port);
380 break;
Andrei Homescu68a55612022-08-02 01:25:15 +0000381 case SocketType::TIPC:
382 status = session->setupPreconnectedClient({}, [=]() {
Andrei Homescud65666d2023-03-03 07:28:02 +0000383#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +0000384 auto port = trustyIpcPort(serverVersion);
Andrei Homescu4bea21772023-03-21 23:28:33 +0000385 for (size_t i = 0; i < 5; i++) {
386 // Try to connect several times,
387 // in case the service is slow to start
388 int tipcFd = tipc_connect(kTrustyIpcDevice, port.c_str());
389 if (tipcFd >= 0) {
390 return android::base::unique_fd(tipcFd);
391 }
392 usleep(50000);
393 }
394 return android::base::unique_fd();
Andrei Homescu68a55612022-08-02 01:25:15 +0000395#else
396 LOG_ALWAYS_FATAL("Tried to connect to Trusty outside of vendor");
397 return android::base::unique_fd();
398#endif
399 });
400 break;
Andrei Homescu96834632022-10-14 00:49:49 +0000401 default:
402 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000403 }
Andrei Homescu96834632022-10-14 00:49:49 +0000404 if (options.allowConnectFailure && status != OK) {
405 ret->sessions.clear();
406 break;
407 }
408 CHECK_EQ(status, OK) << "Could not connect: " << statusToString(status);
409 ret->sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000410 }
Andrei Homescu96834632022-10-14 00:49:49 +0000411 return ret;
412}
Steven Morelandc1635952021-04-01 16:20:47 +0000413
Andrei Homescua858b0e2022-08-01 23:43:09 +0000414TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
415 if (clientOrServerSingleThreaded()) {
416 GTEST_SKIP() << "This test requires multiple threads";
417 }
418
Steven Moreland5553ac42020-11-11 02:14:45 +0000419 constexpr size_t kNumThreads = 10;
420
Steven Moreland4313d7e2021-07-15 23:41:22 +0000421 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000422
423 EXPECT_OK(proc.rootIface->lock());
424
425 // block all but one thread taking locks
426 std::vector<std::thread> ts;
427 for (size_t i = 0; i < kNumThreads - 1; i++) {
428 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
429 }
430
Steven Morelandd6d816f2022-12-23 01:37:17 +0000431 usleep(100000); // give chance for calls on other threads
Steven Moreland5553ac42020-11-11 02:14:45 +0000432
433 // other calls still work
434 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
435
Steven Morelandd6d816f2022-12-23 01:37:17 +0000436 constexpr size_t blockTimeMs = 100;
Steven Moreland5553ac42020-11-11 02:14:45 +0000437 size_t epochMsBefore = epochMillis();
438 // after this, we should never see a response within this time
439 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
440
441 // this call should be blocked for blockTimeMs
442 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
443
444 size_t epochMsAfter = epochMillis();
445 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
446
447 for (auto& t : ts) t.join();
448}
449
Steven Moreland27f620a2023-03-06 19:44:36 +0000450static void testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t numCalls, size_t sleepMs) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000451 size_t epochMsBefore = epochMillis();
452
453 std::vector<std::thread> ts;
Yifan Hong1f44f982021-10-08 17:16:47 -0700454 for (size_t i = 0; i < numCalls; i++) {
455 ts.push_back(std::thread([&] { iface->sleepMs(sleepMs); }));
Steven Moreland5553ac42020-11-11 02:14:45 +0000456 }
457
458 for (auto& t : ts) t.join();
459
460 size_t epochMsAfter = epochMillis();
461
Yifan Hong1f44f982021-10-08 17:16:47 -0700462 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * sleepMs);
Steven Moreland5553ac42020-11-11 02:14:45 +0000463
464 // Potential flake, but make sure calls are handled in parallel.
Steven Moreland75105622023-04-26 22:32:18 +0000465 EXPECT_LE(epochMsAfter, epochMsBefore + 4 * sleepMs);
Yifan Hong1f44f982021-10-08 17:16:47 -0700466}
467
Andrei Homescua858b0e2022-08-01 23:43:09 +0000468TEST_P(BinderRpc, ThreadPoolOverSaturated) {
469 if (clientOrServerSingleThreaded()) {
470 GTEST_SKIP() << "This test requires multiple threads";
471 }
472
Yifan Hong1f44f982021-10-08 17:16:47 -0700473 constexpr size_t kNumThreads = 10;
474 constexpr size_t kNumCalls = kNumThreads + 3;
475 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland73aa6f82023-03-15 21:49:07 +0000476
477 // b/272429574 - below 500ms, the test fails
478 testThreadPoolOverSaturated(proc.rootIface, kNumCalls, 500 /*ms*/);
Yifan Hong1f44f982021-10-08 17:16:47 -0700479}
480
Andrei Homescua858b0e2022-08-01 23:43:09 +0000481TEST_P(BinderRpc, ThreadPoolLimitOutgoing) {
482 if (clientOrServerSingleThreaded()) {
483 GTEST_SKIP() << "This test requires multiple threads";
484 }
485
Yifan Hong1f44f982021-10-08 17:16:47 -0700486 constexpr size_t kNumThreads = 20;
487 constexpr size_t kNumOutgoingConnections = 10;
488 constexpr size_t kNumCalls = kNumOutgoingConnections + 3;
489 auto proc = createRpcTestSocketServerProcess(
490 {.numThreads = kNumThreads, .numOutgoingConnections = kNumOutgoingConnections});
Steven Moreland73aa6f82023-03-15 21:49:07 +0000491
492 // b/272429574 - below 500ms, the test fails
493 testThreadPoolOverSaturated(proc.rootIface, kNumCalls, 500 /*ms*/);
Steven Moreland5553ac42020-11-11 02:14:45 +0000494}
495
Andrei Homescua858b0e2022-08-01 23:43:09 +0000496TEST_P(BinderRpc, ThreadingStressTest) {
497 if (clientOrServerSingleThreaded()) {
498 GTEST_SKIP() << "This test requires multiple threads";
499 }
500
Steven Moreland27f620a2023-03-06 19:44:36 +0000501 constexpr size_t kNumClientThreads = 5;
502 constexpr size_t kNumServerThreads = 5;
503 constexpr size_t kNumCalls = 50;
Steven Moreland5553ac42020-11-11 02:14:45 +0000504
Steven Moreland4313d7e2021-07-15 23:41:22 +0000505 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000506
507 std::vector<std::thread> threads;
508 for (size_t i = 0; i < kNumClientThreads; i++) {
509 threads.push_back(std::thread([&] {
510 for (size_t j = 0; j < kNumCalls; j++) {
511 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000512 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000513 EXPECT_EQ(proc.rootBinder, out);
514 }
515 }));
516 }
517
518 for (auto& t : threads) t.join();
519}
520
Steven Moreland925ba0a2021-09-17 18:06:32 -0700521static void saturateThreadPool(size_t threadCount, const sp<IBinderRpcTest>& iface) {
522 std::vector<std::thread> threads;
523 for (size_t i = 0; i < threadCount; i++) {
524 threads.push_back(std::thread([&] { EXPECT_OK(iface->sleepMs(500)); }));
525 }
526 for (auto& t : threads) t.join();
527}
528
Andrei Homescua858b0e2022-08-01 23:43:09 +0000529TEST_P(BinderRpc, OnewayStressTest) {
530 if (clientOrServerSingleThreaded()) {
531 GTEST_SKIP() << "This test requires multiple threads";
532 }
533
Steven Morelandc6046982021-04-20 00:49:42 +0000534 constexpr size_t kNumClientThreads = 10;
535 constexpr size_t kNumServerThreads = 10;
Steven Moreland3c3ab8d2021-09-23 10:29:50 -0700536 constexpr size_t kNumCalls = 1000;
Steven Morelandc6046982021-04-20 00:49:42 +0000537
Steven Moreland4313d7e2021-07-15 23:41:22 +0000538 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Morelandc6046982021-04-20 00:49:42 +0000539
540 std::vector<std::thread> threads;
541 for (size_t i = 0; i < kNumClientThreads; i++) {
542 threads.push_back(std::thread([&] {
543 for (size_t j = 0; j < kNumCalls; j++) {
544 EXPECT_OK(proc.rootIface->sendString("a"));
545 }
Steven Morelandc6046982021-04-20 00:49:42 +0000546 }));
547 }
548
549 for (auto& t : threads) t.join();
Steven Moreland925ba0a2021-09-17 18:06:32 -0700550
551 saturateThreadPool(kNumServerThreads, proc.rootIface);
Steven Morelandc6046982021-04-20 00:49:42 +0000552}
553
Frederick Mayleb0221d12022-10-03 23:10:53 +0000554TEST_P(BinderRpc, OnewayCallQueueingWithFds) {
555 if (!supportsFdTransport()) {
556 GTEST_SKIP() << "Would fail trivially (which is tested elsewhere)";
557 }
558 if (clientOrServerSingleThreaded()) {
559 GTEST_SKIP() << "This test requires multiple threads";
560 }
561
Andrei Homescu5f2bc562023-02-25 04:59:43 +0000562 constexpr size_t kNumServerThreads = 3;
563
Frederick Mayleb0221d12022-10-03 23:10:53 +0000564 // This test forces a oneway transaction to be queued by issuing two
565 // `blockingSendFdOneway` calls, then drains the queue by issuing two
566 // `blockingRecvFd` calls.
567 //
568 // For more details about the queuing semantics see
569 // https://developer.android.com/reference/android/os/IBinder#FLAG_ONEWAY
570
571 auto proc = createRpcTestSocketServerProcess({
Andrei Homescu5f2bc562023-02-25 04:59:43 +0000572 .numThreads = kNumServerThreads,
Frederick Mayleb0221d12022-10-03 23:10:53 +0000573 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
574 .serverSupportedFileDescriptorTransportModes =
575 {RpcSession::FileDescriptorTransportMode::UNIX},
576 });
577
578 EXPECT_OK(proc.rootIface->blockingSendFdOneway(
579 android::os::ParcelFileDescriptor(mockFileDescriptor("a"))));
580 EXPECT_OK(proc.rootIface->blockingSendFdOneway(
581 android::os::ParcelFileDescriptor(mockFileDescriptor("b"))));
582
583 android::os::ParcelFileDescriptor fdA;
584 EXPECT_OK(proc.rootIface->blockingRecvFd(&fdA));
585 std::string result;
586 CHECK(android::base::ReadFdToString(fdA.get(), &result));
587 EXPECT_EQ(result, "a");
588
589 android::os::ParcelFileDescriptor fdB;
590 EXPECT_OK(proc.rootIface->blockingRecvFd(&fdB));
591 CHECK(android::base::ReadFdToString(fdB.get(), &result));
592 EXPECT_EQ(result, "b");
Andrei Homescu5f2bc562023-02-25 04:59:43 +0000593
594 saturateThreadPool(kNumServerThreads, proc.rootIface);
Frederick Mayleb0221d12022-10-03 23:10:53 +0000595}
596
Andrei Homescua858b0e2022-08-01 23:43:09 +0000597TEST_P(BinderRpc, OnewayCallQueueing) {
598 if (clientOrServerSingleThreaded()) {
599 GTEST_SKIP() << "This test requires multiple threads";
600 }
601
Frederick Mayle96872592023-03-07 14:56:15 -0800602 constexpr size_t kNumQueued = 10;
Steven Moreland5553ac42020-11-11 02:14:45 +0000603 constexpr size_t kNumExtraServerThreads = 4;
Steven Moreland5553ac42020-11-11 02:14:45 +0000604
605 // make sure calls to the same object happen on the same thread
Steven Moreland4313d7e2021-07-15 23:41:22 +0000606 auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000607
Frederick Mayle96872592023-03-07 14:56:15 -0800608 // all these *Oneway commands should be queued on the server sequentially,
Steven Moreland1c678802021-09-17 16:48:47 -0700609 // even though there are multiple threads.
Frederick Mayle96872592023-03-07 14:56:15 -0800610 for (size_t i = 0; i + 1 < kNumQueued; i++) {
611 proc.rootIface->blockingSendIntOneway(i);
Steven Moreland5553ac42020-11-11 02:14:45 +0000612 }
Frederick Mayle96872592023-03-07 14:56:15 -0800613 for (size_t i = 0; i + 1 < kNumQueued; i++) {
614 int n;
615 proc.rootIface->blockingRecvInt(&n);
616 EXPECT_EQ(n, i);
617 }
Steven Morelandf5174272021-05-25 00:39:28 +0000618
Steven Moreland925ba0a2021-09-17 18:06:32 -0700619 saturateThreadPool(1 + kNumExtraServerThreads, proc.rootIface);
Steven Moreland5553ac42020-11-11 02:14:45 +0000620}
621
Andrei Homescua858b0e2022-08-01 23:43:09 +0000622TEST_P(BinderRpc, OnewayCallExhaustion) {
623 if (clientOrServerSingleThreaded()) {
624 GTEST_SKIP() << "This test requires multiple threads";
625 }
626
Steven Morelandd45be622021-06-04 02:19:37 +0000627 constexpr size_t kNumClients = 2;
628 constexpr size_t kTooLongMs = 1000;
629
Steven Moreland4313d7e2021-07-15 23:41:22 +0000630 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2});
Steven Morelandd45be622021-06-04 02:19:37 +0000631
632 // Build up oneway calls on the second session to make sure it terminates
633 // and shuts down. The first session should be unaffected (proc destructor
634 // checks the first session).
Andrei Homescu96834632022-10-14 00:49:49 +0000635 auto iface = interface_cast<IBinderRpcTest>(proc.proc->sessions.at(1).root);
Steven Morelandd45be622021-06-04 02:19:37 +0000636
637 std::vector<std::thread> threads;
638 for (size_t i = 0; i < kNumClients; i++) {
639 // one of these threads will get stuck queueing a transaction once the
640 // socket fills up, the other will be able to fill up transactions on
641 // this object
642 threads.push_back(std::thread([&] {
643 while (iface->sleepMsAsync(kTooLongMs).isOk()) {
644 }
645 }));
646 }
647 for (auto& t : threads) t.join();
648
649 Status status = iface->sleepMsAsync(kTooLongMs);
650 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
651
Steven Moreland798e0d12021-07-14 23:19:25 +0000652 // now that it has died, wait for the remote session to shutdown
653 std::vector<int32_t> remoteCounts;
654 do {
655 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
656 } while (remoteCounts.size() == kNumClients);
657
Steven Morelandd45be622021-06-04 02:19:37 +0000658 // the second session should be shutdown in the other process by the time we
659 // are able to join above (it'll only be hung up once it finishes processing
660 // any pending commands). We need to erase this session from the record
661 // here, so that the destructor for our session won't check that this
662 // session is valid, but we still want it to test the other session.
Andrei Homescu96834632022-10-14 00:49:49 +0000663 proc.proc->sessions.erase(proc.proc->sessions.begin() + 1);
Steven Morelandd45be622021-06-04 02:19:37 +0000664}
665
Steven Moreland67f85902023-03-15 01:13:49 +0000666TEST_P(BinderRpc, SessionWithIncomingThreadpoolDoesntLeak) {
667 if (clientOrServerSingleThreaded()) {
668 GTEST_SKIP() << "This test requires multiple threads";
669 }
670
671 // session 0 - will check for leaks in destrutor of proc
672 // session 1 - we want to make sure it gets deleted when we drop all references to it
673 auto proc = createRpcTestSocketServerProcess(
674 {.numThreads = 1, .numIncomingConnectionsBySession = {0, 1}, .numSessions = 2});
675
676 wp<RpcSession> session = proc.proc->sessions.at(1).session;
677
678 // remove all references to the second session
679 proc.proc->sessions.at(1).root = nullptr;
680 proc.proc->sessions.erase(proc.proc->sessions.begin() + 1);
681
682 // TODO(b/271830568) more efficient way to wait for other incoming threadpool
683 // to drain commands.
684 for (size_t i = 0; i < 100; i++) {
685 usleep(10 * 1000);
686 if (session.promote() == nullptr) break;
687 }
688
689 EXPECT_EQ(nullptr, session.promote());
Steven Morelandb5d2b642023-05-04 00:31:45 +0000690
691 sleep(1); // give time for remote session to shutdown
Steven Moreland67f85902023-03-15 01:13:49 +0000692}
693
Devin Moore66d5b7a2022-07-07 21:42:10 +0000694TEST_P(BinderRpc, SingleDeathRecipient) {
Andrei Homescua858b0e2022-08-01 23:43:09 +0000695 if (clientOrServerSingleThreaded()) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000696 GTEST_SKIP() << "This test requires multiple threads";
697 }
698 class MyDeathRec : public IBinder::DeathRecipient {
699 public:
700 void binderDied(const wp<IBinder>& /* who */) override {
701 dead = true;
702 mCv.notify_one();
703 }
704 std::mutex mMtx;
705 std::condition_variable mCv;
706 bool dead = false;
707 };
708
709 // Death recipient needs to have an incoming connection to be called
710 auto proc = createRpcTestSocketServerProcess(
Steven Moreland67f85902023-03-15 01:13:49 +0000711 {.numThreads = 1, .numSessions = 1, .numIncomingConnectionsBySession = {1}});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000712
713 auto dr = sp<MyDeathRec>::make();
714 ASSERT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
715
716 if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) {
717 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
718 }
719
720 std::unique_lock<std::mutex> lock(dr->mMtx);
Steven Morelanddd231e22022-09-08 19:47:49 +0000721 ASSERT_TRUE(dr->mCv.wait_for(lock, 100ms, [&]() { return dr->dead; }));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000722
723 // need to wait for the session to shutdown so we don't "Leak session"
Steven Moreland67f85902023-03-15 01:13:49 +0000724 // can't do this before checking the death recipient by calling
725 // forceShutdown earlier, because shutdownAndWait will also trigger
726 // a death recipient, but if we had a way to wait for the service
727 // to gracefully shutdown, we could use that here.
Andrei Homescu96834632022-10-14 00:49:49 +0000728 EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000729 proc.expectAlreadyShutdown = true;
730}
731
732TEST_P(BinderRpc, SingleDeathRecipientOnShutdown) {
Andrei Homescua858b0e2022-08-01 23:43:09 +0000733 if (clientOrServerSingleThreaded()) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000734 GTEST_SKIP() << "This test requires multiple threads";
735 }
736 class MyDeathRec : public IBinder::DeathRecipient {
737 public:
738 void binderDied(const wp<IBinder>& /* who */) override {
739 dead = true;
740 mCv.notify_one();
741 }
742 std::mutex mMtx;
743 std::condition_variable mCv;
744 bool dead = false;
745 };
746
747 // Death recipient needs to have an incoming connection to be called
748 auto proc = createRpcTestSocketServerProcess(
Steven Moreland67f85902023-03-15 01:13:49 +0000749 {.numThreads = 1, .numSessions = 1, .numIncomingConnectionsBySession = {1}});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000750
751 auto dr = sp<MyDeathRec>::make();
752 EXPECT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
753
754 // Explicitly calling shutDownAndWait will cause the death recipients
755 // to be called.
Andrei Homescu96834632022-10-14 00:49:49 +0000756 EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000757
758 std::unique_lock<std::mutex> lock(dr->mMtx);
759 if (!dr->dead) {
Steven Morelanddd231e22022-09-08 19:47:49 +0000760 EXPECT_EQ(std::cv_status::no_timeout, dr->mCv.wait_for(lock, 100ms));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000761 }
762 EXPECT_TRUE(dr->dead) << "Failed to receive the death notification.";
763
Andrei Homescu96834632022-10-14 00:49:49 +0000764 proc.proc->terminate();
765 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000766 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM)
767 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
768 });
769 proc.expectAlreadyShutdown = true;
770}
771
Steven Moreland5ec743f2023-01-18 01:02:06 +0000772TEST_P(BinderRpc, DeathRecipientFailsWithoutIncoming) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000773 if (socketType() == SocketType::TIPC) {
774 // This should work, but Trusty takes too long to restart the service
775 GTEST_SKIP() << "Service death test not supported on Trusty";
776 }
Devin Moore66d5b7a2022-07-07 21:42:10 +0000777 class MyDeathRec : public IBinder::DeathRecipient {
778 public:
779 void binderDied(const wp<IBinder>& /* who */) override {}
780 };
781
Steven Moreland67f85902023-03-15 01:13:49 +0000782 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 1});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000783
784 auto dr = sp<MyDeathRec>::make();
Steven Moreland5ec743f2023-01-18 01:02:06 +0000785 EXPECT_EQ(INVALID_OPERATION, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000786}
787
788TEST_P(BinderRpc, UnlinkDeathRecipient) {
Andrei Homescua858b0e2022-08-01 23:43:09 +0000789 if (clientOrServerSingleThreaded()) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000790 GTEST_SKIP() << "This test requires multiple threads";
791 }
792 class MyDeathRec : public IBinder::DeathRecipient {
793 public:
794 void binderDied(const wp<IBinder>& /* who */) override {
795 GTEST_FAIL() << "This should not be called after unlinkToDeath";
796 }
797 };
798
799 // Death recipient needs to have an incoming connection to be called
800 auto proc = createRpcTestSocketServerProcess(
Steven Moreland67f85902023-03-15 01:13:49 +0000801 {.numThreads = 1, .numSessions = 1, .numIncomingConnectionsBySession = {1}});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000802
803 auto dr = sp<MyDeathRec>::make();
804 ASSERT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
805 ASSERT_EQ(OK, proc.rootBinder->unlinkToDeath(dr, (void*)1, 0, nullptr));
806
Steven Moreland67f85902023-03-15 01:13:49 +0000807 proc.forceShutdown();
Devin Moore66d5b7a2022-07-07 21:42:10 +0000808}
809
Steven Morelandc1635952021-04-01 16:20:47 +0000810TEST_P(BinderRpc, Die) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000811 if (socketType() == SocketType::TIPC) {
812 // This should work, but Trusty takes too long to restart the service
813 GTEST_SKIP() << "Service death test not supported on Trusty";
814 }
815
Steven Moreland5553ac42020-11-11 02:14:45 +0000816 for (bool doDeathCleanup : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000817 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000818
819 // make sure there is some state during crash
820 // 1. we hold their binder
821 sp<IBinderRpcSession> session;
822 EXPECT_OK(proc.rootIface->openSession("happy", &session));
823 // 2. they hold our binder
824 sp<IBinder> binder = new BBinder();
825 EXPECT_OK(proc.rootIface->holdBinder(binder));
826
827 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
828 << "Do death cleanup: " << doDeathCleanup;
829
Andrei Homescu96834632022-10-14 00:49:49 +0000830 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Maylea12b0962022-06-25 01:13:22 +0000831 EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 1)
832 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
833 });
Steven Morelandaf4ca712021-05-24 23:22:08 +0000834 proc.expectAlreadyShutdown = true;
Steven Moreland5553ac42020-11-11 02:14:45 +0000835 }
836}
837
Steven Morelandd7302072021-05-15 01:32:04 +0000838TEST_P(BinderRpc, UseKernelBinderCallingId) {
Andrei Homescu2a298012022-06-15 01:08:54 +0000839 // This test only works if the current process shared the internal state of
840 // ProcessState with the service across the call to fork(). Both the static
841 // libraries and libbinder.so have their own separate copies of all the
842 // globals, so the test only works when the test client and service both use
843 // libbinder.so (when using static libraries, even a client and service
844 // using the same kind of static library should have separate copies of the
845 // variables).
Andrei Homescua858b0e2022-08-01 23:43:09 +0000846 if (!kEnableSharedLibs || serverSingleThreaded() || noKernel()) {
Andrei Homescu12106de2022-04-27 04:42:21 +0000847 GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled "
848 "at build time.";
849 }
850
Steven Moreland4313d7e2021-07-15 23:41:22 +0000851 auto proc = createRpcTestSocketServerProcess({});
Steven Morelandd7302072021-05-15 01:32:04 +0000852
Andrei Homescu2a298012022-06-15 01:08:54 +0000853 // we can't allocate IPCThreadState so actually the first time should
854 // succeed :(
855 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
Steven Morelandd7302072021-05-15 01:32:04 +0000856
857 // second time! we catch the error :)
858 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
859
Andrei Homescu96834632022-10-14 00:49:49 +0000860 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Maylea12b0962022-06-25 01:13:22 +0000861 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)
862 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
863 });
Steven Morelandaf4ca712021-05-24 23:22:08 +0000864 proc.expectAlreadyShutdown = true;
Steven Morelandd7302072021-05-15 01:32:04 +0000865}
866
Frederick Mayle69a0c992022-05-26 20:38:39 +0000867TEST_P(BinderRpc, FileDescriptorTransportRejectNone) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000868 if (socketType() == SocketType::TIPC) {
869 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
870 }
871
Frederick Mayle69a0c992022-05-26 20:38:39 +0000872 auto proc = createRpcTestSocketServerProcess({
873 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE,
874 .serverSupportedFileDescriptorTransportModes =
875 {RpcSession::FileDescriptorTransportMode::UNIX},
876 .allowConnectFailure = true,
877 });
Andrei Homescu96834632022-10-14 00:49:49 +0000878 EXPECT_TRUE(proc.proc->sessions.empty()) << "session connections should have failed";
879 proc.proc->terminate();
880 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000881 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM)
882 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
883 });
884 proc.expectAlreadyShutdown = true;
885}
886
887TEST_P(BinderRpc, FileDescriptorTransportRejectUnix) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000888 if (socketType() == SocketType::TIPC) {
889 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
890 }
891
Frederick Mayle69a0c992022-05-26 20:38:39 +0000892 auto proc = createRpcTestSocketServerProcess({
893 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
894 .serverSupportedFileDescriptorTransportModes =
895 {RpcSession::FileDescriptorTransportMode::NONE},
896 .allowConnectFailure = true,
897 });
Andrei Homescu96834632022-10-14 00:49:49 +0000898 EXPECT_TRUE(proc.proc->sessions.empty()) << "session connections should have failed";
899 proc.proc->terminate();
900 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000901 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM)
902 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
903 });
904 proc.expectAlreadyShutdown = true;
905}
906
907TEST_P(BinderRpc, FileDescriptorTransportOptionalUnix) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000908 if (socketType() == SocketType::TIPC) {
909 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
910 }
911
Frederick Mayle69a0c992022-05-26 20:38:39 +0000912 auto proc = createRpcTestSocketServerProcess({
913 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE,
914 .serverSupportedFileDescriptorTransportModes =
915 {RpcSession::FileDescriptorTransportMode::NONE,
916 RpcSession::FileDescriptorTransportMode::UNIX},
917 });
918
919 android::os::ParcelFileDescriptor out;
920 auto status = proc.rootIface->echoAsFile("hello", &out);
921 EXPECT_EQ(status.transactionError(), FDS_NOT_ALLOWED) << status;
922}
923
924TEST_P(BinderRpc, ReceiveFile) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000925 if (socketType() == SocketType::TIPC) {
926 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
927 }
928
Frederick Mayle69a0c992022-05-26 20:38:39 +0000929 auto proc = createRpcTestSocketServerProcess({
930 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
931 .serverSupportedFileDescriptorTransportModes =
932 {RpcSession::FileDescriptorTransportMode::UNIX},
933 });
934
935 android::os::ParcelFileDescriptor out;
936 auto status = proc.rootIface->echoAsFile("hello", &out);
937 if (!supportsFdTransport()) {
938 EXPECT_EQ(status.transactionError(), BAD_VALUE) << status;
939 return;
940 }
941 ASSERT_TRUE(status.isOk()) << status;
942
943 std::string result;
944 CHECK(android::base::ReadFdToString(out.get(), &result));
945 EXPECT_EQ(result, "hello");
946}
947
948TEST_P(BinderRpc, SendFiles) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000949 if (socketType() == SocketType::TIPC) {
950 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
951 }
952
Frederick Mayle69a0c992022-05-26 20:38:39 +0000953 auto proc = createRpcTestSocketServerProcess({
954 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
955 .serverSupportedFileDescriptorTransportModes =
956 {RpcSession::FileDescriptorTransportMode::UNIX},
957 });
958
959 std::vector<android::os::ParcelFileDescriptor> files;
960 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("123")));
961 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a")));
962 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("b")));
963 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("cd")));
964
965 android::os::ParcelFileDescriptor out;
966 auto status = proc.rootIface->concatFiles(files, &out);
967 if (!supportsFdTransport()) {
968 EXPECT_EQ(status.transactionError(), BAD_VALUE) << status;
969 return;
970 }
971 ASSERT_TRUE(status.isOk()) << status;
972
973 std::string result;
974 CHECK(android::base::ReadFdToString(out.get(), &result));
975 EXPECT_EQ(result, "123abcd");
976}
977
978TEST_P(BinderRpc, SendMaxFiles) {
979 if (!supportsFdTransport()) {
980 GTEST_SKIP() << "Would fail trivially (which is tested by BinderRpc::SendFiles)";
981 }
982
983 auto proc = createRpcTestSocketServerProcess({
984 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
985 .serverSupportedFileDescriptorTransportModes =
986 {RpcSession::FileDescriptorTransportMode::UNIX},
987 });
988
989 std::vector<android::os::ParcelFileDescriptor> files;
990 for (int i = 0; i < 253; i++) {
991 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a")));
992 }
993
994 android::os::ParcelFileDescriptor out;
995 auto status = proc.rootIface->concatFiles(files, &out);
996 ASSERT_TRUE(status.isOk()) << status;
997
998 std::string result;
999 CHECK(android::base::ReadFdToString(out.get(), &result));
1000 EXPECT_EQ(result, std::string(253, 'a'));
1001}
1002
1003TEST_P(BinderRpc, SendTooManyFiles) {
1004 if (!supportsFdTransport()) {
1005 GTEST_SKIP() << "Would fail trivially (which is tested by BinderRpc::SendFiles)";
1006 }
1007
1008 auto proc = createRpcTestSocketServerProcess({
1009 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
1010 .serverSupportedFileDescriptorTransportModes =
1011 {RpcSession::FileDescriptorTransportMode::UNIX},
1012 });
1013
1014 std::vector<android::os::ParcelFileDescriptor> files;
1015 for (int i = 0; i < 254; i++) {
1016 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a")));
1017 }
1018
1019 android::os::ParcelFileDescriptor out;
1020 auto status = proc.rootIface->concatFiles(files, &out);
1021 EXPECT_EQ(status.transactionError(), BAD_VALUE) << status;
1022}
1023
Andrei Homescufc221502022-10-08 03:51:17 +00001024TEST_P(BinderRpc, AppendInvalidFd) {
Andrei Homescu68a55612022-08-02 01:25:15 +00001025 if (socketType() == SocketType::TIPC) {
1026 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
1027 }
1028
Andrei Homescufc221502022-10-08 03:51:17 +00001029 auto proc = createRpcTestSocketServerProcess({
1030 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
1031 .serverSupportedFileDescriptorTransportModes =
1032 {RpcSession::FileDescriptorTransportMode::UNIX},
1033 });
1034
1035 int badFd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0);
1036 ASSERT_NE(badFd, -1);
1037
1038 // Close the file descriptor so it becomes invalid for dup
1039 close(badFd);
1040
1041 Parcel p1;
1042 p1.markForBinder(proc.rootBinder);
1043 p1.writeInt32(3);
1044 EXPECT_EQ(OK, p1.writeFileDescriptor(badFd, false));
1045
1046 Parcel pRaw;
1047 pRaw.markForBinder(proc.rootBinder);
1048 EXPECT_EQ(OK, pRaw.appendFrom(&p1, 0, p1.dataSize()));
1049
1050 pRaw.setDataPosition(0);
1051 EXPECT_EQ(3, pRaw.readInt32());
1052 ASSERT_EQ(-1, pRaw.readFileDescriptor());
1053}
1054
Andrei Homescu68a55612022-08-02 01:25:15 +00001055#ifndef __ANDROID_VENDOR__ // No AIBinder_fromPlatformBinder on vendor
Steven Moreland37aff182021-03-26 02:04:16 +00001056TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
Andrei Homescu12106de2022-04-27 04:42:21 +00001057 if constexpr (!kEnableSharedLibs) {
1058 GTEST_SKIP() << "Test disabled because Binder was built as a static library";
1059 }
1060
Steven Moreland4313d7e2021-07-15 23:41:22 +00001061 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001062
1063 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1064 ASSERT_NE(binder, nullptr);
1065
1066 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1067}
1068
1069TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
Andrei Homescu12106de2022-04-27 04:42:21 +00001070 if constexpr (!kEnableSharedLibs) {
1071 GTEST_SKIP() << "Test disabled because Binder was built as a static library";
1072 }
1073
Steven Moreland4313d7e2021-07-15 23:41:22 +00001074 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001075
1076 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1077 ASSERT_NE(binder, nullptr);
1078
1079 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
1080 ASSERT_NE(ndkBinder, nullptr);
1081
1082 std::string out;
1083 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
1084 ASSERT_TRUE(status.isOk()) << status.getDescription();
1085 ASSERT_EQ("aoeuaoeu", out);
1086}
Andrei Homescu68a55612022-08-02 01:25:15 +00001087#endif // __ANDROID_VENDOR__
Steven Moreland37aff182021-03-26 02:04:16 +00001088
Steven Moreland5553ac42020-11-11 02:14:45 +00001089ssize_t countFds() {
1090 DIR* dir = opendir("/proc/self/fd/");
1091 if (dir == nullptr) return -1;
1092 ssize_t ret = 0;
1093 dirent* ent;
1094 while ((ent = readdir(dir)) != nullptr) ret++;
1095 closedir(dir);
1096 return ret;
1097}
1098
Andrei Homescua858b0e2022-08-01 23:43:09 +00001099TEST_P(BinderRpc, Fds) {
1100 if (serverSingleThreaded()) {
1101 GTEST_SKIP() << "This test requires multiple threads";
1102 }
Andrei Homescu68a55612022-08-02 01:25:15 +00001103 if (socketType() == SocketType::TIPC) {
1104 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
1105 }
Andrei Homescua858b0e2022-08-01 23:43:09 +00001106
Steven Moreland5553ac42020-11-11 02:14:45 +00001107 ssize_t beforeFds = countFds();
1108 ASSERT_GE(beforeFds, 0);
1109 {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001110 auto proc = createRpcTestSocketServerProcess({.numThreads = 10});
Steven Moreland5553ac42020-11-11 02:14:45 +00001111 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
1112 }
1113 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
1114}
1115
Andrei Homescud65666d2023-03-03 07:28:02 +00001116#ifdef BINDER_RPC_TO_TRUSTY_TEST
1117INSTANTIATE_TEST_CASE_P(Trusty, BinderRpc,
1118 ::testing::Combine(::testing::Values(SocketType::TIPC),
1119 ::testing::Values(RpcSecurity::RAW),
1120 ::testing::ValuesIn(testVersions()),
1121 ::testing::ValuesIn(testVersions()),
1122 ::testing::Values(true), ::testing::Values(true)),
1123 BinderRpc::PrintParamInfo);
1124#else // BINDER_RPC_TO_TRUSTY_TEST
Steven Morelandda573042021-06-12 01:13:45 +00001125static bool testSupportVsockLoopback() {
Yifan Hong702115c2021-06-24 15:39:18 -07001126 // We don't need to enable TLS to know if vsock is supported.
Steven Morelandda573042021-06-12 01:13:45 +00001127 unsigned int vsockPort = allocateVsockPort();
Steven Morelandda573042021-06-12 01:13:45 +00001128
Andrei Homescu992a4052022-06-28 21:26:18 +00001129 android::base::unique_fd serverFd(
1130 TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)));
Steven Morelanda27311b2023-04-11 22:13:35 +00001131
1132 if (errno == EAFNOSUPPORT) {
1133 return false;
1134 }
1135
Andrei Homescu992a4052022-06-28 21:26:18 +00001136 LOG_ALWAYS_FATAL_IF(serverFd == -1, "Could not create socket: %s", strerror(errno));
1137
1138 sockaddr_vm serverAddr{
1139 .svm_family = AF_VSOCK,
1140 .svm_port = vsockPort,
1141 .svm_cid = VMADDR_CID_ANY,
1142 };
1143 int ret = TEMP_FAILURE_RETRY(
1144 bind(serverFd.get(), reinterpret_cast<sockaddr*>(&serverAddr), sizeof(serverAddr)));
1145 LOG_ALWAYS_FATAL_IF(0 != ret, "Could not bind socket to port %u: %s", vsockPort,
1146 strerror(errno));
1147
1148 ret = TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/));
1149 LOG_ALWAYS_FATAL_IF(0 != ret, "Could not listen socket on port %u: %s", vsockPort,
1150 strerror(errno));
1151
1152 // Try to connect to the server using the VMADDR_CID_LOCAL cid
1153 // to see if the kernel supports it. It's safe to use a blocking
1154 // connect because vsock sockets have a 2 second connection timeout,
1155 // and they return ETIMEDOUT after that.
1156 android::base::unique_fd connectFd(
1157 TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)));
1158 LOG_ALWAYS_FATAL_IF(connectFd == -1, "Could not create socket for port %u: %s", vsockPort,
1159 strerror(errno));
1160
1161 bool success = false;
1162 sockaddr_vm connectAddr{
1163 .svm_family = AF_VSOCK,
1164 .svm_port = vsockPort,
1165 .svm_cid = VMADDR_CID_LOCAL,
1166 };
1167 ret = TEMP_FAILURE_RETRY(connect(connectFd.get(), reinterpret_cast<sockaddr*>(&connectAddr),
1168 sizeof(connectAddr)));
1169 if (ret != 0 && (errno == EAGAIN || errno == EINPROGRESS)) {
1170 android::base::unique_fd acceptFd;
1171 while (true) {
1172 pollfd pfd[]{
1173 {.fd = serverFd.get(), .events = POLLIN, .revents = 0},
1174 {.fd = connectFd.get(), .events = POLLOUT, .revents = 0},
1175 };
1176 ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
1177 LOG_ALWAYS_FATAL_IF(ret < 0, "Error polling: %s", strerror(errno));
1178
1179 if (pfd[0].revents & POLLIN) {
1180 sockaddr_vm acceptAddr;
1181 socklen_t acceptAddrLen = sizeof(acceptAddr);
1182 ret = TEMP_FAILURE_RETRY(accept4(serverFd.get(),
1183 reinterpret_cast<sockaddr*>(&acceptAddr),
1184 &acceptAddrLen, SOCK_CLOEXEC));
1185 LOG_ALWAYS_FATAL_IF(ret < 0, "Could not accept4 socket: %s", strerror(errno));
1186 LOG_ALWAYS_FATAL_IF(acceptAddrLen != static_cast<socklen_t>(sizeof(acceptAddr)),
1187 "Truncated address");
1188
1189 // Store the fd in acceptFd so we keep the connection alive
1190 // while polling connectFd
1191 acceptFd.reset(ret);
1192 }
1193
1194 if (pfd[1].revents & POLLOUT) {
1195 // Connect either succeeded or timed out
1196 int connectErrno;
1197 socklen_t connectErrnoLen = sizeof(connectErrno);
1198 int ret = getsockopt(connectFd.get(), SOL_SOCKET, SO_ERROR, &connectErrno,
1199 &connectErrnoLen);
1200 LOG_ALWAYS_FATAL_IF(ret == -1,
1201 "Could not getsockopt() after connect() "
1202 "on non-blocking socket: %s.",
1203 strerror(errno));
1204
1205 // We're done, this is all we wanted
1206 success = connectErrno == 0;
1207 break;
1208 }
1209 }
1210 } else {
1211 success = ret == 0;
1212 }
1213
1214 ALOGE("Detected vsock loopback supported: %s", success ? "yes" : "no");
1215
1216 return success;
Steven Morelandda573042021-06-12 01:13:45 +00001217}
1218
Yifan Hong1deca4b2021-09-10 16:16:44 -07001219static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) {
Alice Wang893a9912022-10-24 10:44:09 +00001220 std::vector<SocketType> ret = {SocketType::UNIX, SocketType::UNIX_BOOTSTRAP, SocketType::INET,
1221 SocketType::UNIX_RAW};
Yifan Hong1deca4b2021-09-10 16:16:44 -07001222
1223 if (hasPreconnected) ret.push_back(SocketType::PRECONNECTED);
Steven Morelandda573042021-06-12 01:13:45 +00001224
1225 static bool hasVsockLoopback = testSupportVsockLoopback();
1226
1227 if (hasVsockLoopback) {
1228 ret.push_back(SocketType::VSOCK);
1229 }
1230
1231 return ret;
1232}
1233
Yifan Hong702115c2021-06-24 15:39:18 -07001234INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
1235 ::testing::Combine(::testing::ValuesIn(testSocketTypes()),
Frederick Mayledc07cf82022-05-26 20:30:12 +00001236 ::testing::ValuesIn(RpcSecurityValues()),
1237 ::testing::ValuesIn(testVersions()),
Andrei Homescu2a298012022-06-15 01:08:54 +00001238 ::testing::ValuesIn(testVersions()),
1239 ::testing::Values(false, true),
1240 ::testing::Values(false, true)),
Yifan Hong702115c2021-06-24 15:39:18 -07001241 BinderRpc::PrintParamInfo);
Steven Morelandc1635952021-04-01 16:20:47 +00001242
Yifan Hong702115c2021-06-24 15:39:18 -07001243class BinderRpcServerRootObject
1244 : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001245
1246TEST_P(BinderRpcServerRootObject, WeakRootObject) {
1247 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
1248 auto setRootObject = [](bool isStrong) -> SetFn {
1249 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
1250 };
1251
Yifan Hong702115c2021-06-24 15:39:18 -07001252 auto [isStrong1, isStrong2, rpcSecurity] = GetParam();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001253 auto server = RpcServer::make(newTlsFactory(rpcSecurity));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001254 auto binder1 = sp<BBinder>::make();
1255 IBinder* binderRaw1 = binder1.get();
1256 setRootObject(isStrong1)(server.get(), binder1);
1257 EXPECT_EQ(binderRaw1, server->getRootObject());
1258 binder1.clear();
1259 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
1260
1261 auto binder2 = sp<BBinder>::make();
1262 IBinder* binderRaw2 = binder2.get();
1263 setRootObject(isStrong2)(server.get(), binder2);
1264 EXPECT_EQ(binderRaw2, server->getRootObject());
1265 binder2.clear();
1266 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
1267}
1268
1269INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
Yifan Hong702115c2021-06-24 15:39:18 -07001270 ::testing::Combine(::testing::Bool(), ::testing::Bool(),
1271 ::testing::ValuesIn(RpcSecurityValues())));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001272
Yifan Hong1a235852021-05-13 16:07:47 -07001273class OneOffSignal {
1274public:
1275 // If notify() was previously called, or is called within |duration|, return true; else false.
1276 template <typename R, typename P>
1277 bool wait(std::chrono::duration<R, P> duration) {
1278 std::unique_lock<std::mutex> lock(mMutex);
1279 return mCv.wait_for(lock, duration, [this] { return mValue; });
1280 }
1281 void notify() {
1282 std::unique_lock<std::mutex> lock(mMutex);
1283 mValue = true;
1284 lock.unlock();
1285 mCv.notify_all();
1286 }
1287
1288private:
1289 std::mutex mMutex;
1290 std::condition_variable mCv;
1291 bool mValue = false;
1292};
1293
Yifan Hong194acf22021-06-29 18:44:56 -07001294TEST(BinderRpc, Java) {
1295#if !defined(__ANDROID__)
1296 GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on"
1297 "createRpcDelegateServiceManager() with a device attached, such test belongs "
1298 "to binderHostDeviceTest. Hence, just disable this test on host.";
1299#endif // !__ANDROID__
Andrei Homescu12106de2022-04-27 04:42:21 +00001300 if constexpr (!kEnableKernelIpc) {
1301 GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled "
1302 "at build time.";
1303 }
1304
Yifan Hong194acf22021-06-29 18:44:56 -07001305 sp<IServiceManager> sm = defaultServiceManager();
1306 ASSERT_NE(nullptr, sm);
1307 // Any Java service with non-empty getInterfaceDescriptor() would do.
1308 // Let's pick batteryproperties.
1309 auto binder = sm->checkService(String16("batteryproperties"));
1310 ASSERT_NE(nullptr, binder);
1311 auto descriptor = binder->getInterfaceDescriptor();
1312 ASSERT_GE(descriptor.size(), 0);
1313 ASSERT_EQ(OK, binder->pingBinder());
1314
1315 auto rpcServer = RpcServer::make();
Yifan Hong194acf22021-06-29 18:44:56 -07001316 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001317 ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port));
Yifan Hong194acf22021-06-29 18:44:56 -07001318 auto socket = rpcServer->releaseServer();
1319
1320 auto keepAlive = sp<BBinder>::make();
Yifan Hongfe4b83f2021-11-08 16:29:53 -08001321 auto setRpcClientDebugStatus = binder->setRpcClientDebug(std::move(socket), keepAlive);
1322
Yifan Honge3caaf22022-01-12 14:46:56 -08001323 if (!android::base::GetBoolProperty("ro.debuggable", false) ||
1324 android::base::GetProperty("ro.build.type", "") == "user") {
Yifan Hongfe4b83f2021-11-08 16:29:53 -08001325 ASSERT_EQ(INVALID_OPERATION, setRpcClientDebugStatus)
Yifan Honge3caaf22022-01-12 14:46:56 -08001326 << "setRpcClientDebug should return INVALID_OPERATION on non-debuggable or user "
1327 "builds, but get "
Yifan Hongfe4b83f2021-11-08 16:29:53 -08001328 << statusToString(setRpcClientDebugStatus);
1329 GTEST_SKIP();
1330 }
1331
1332 ASSERT_EQ(OK, setRpcClientDebugStatus);
Yifan Hong194acf22021-06-29 18:44:56 -07001333
1334 auto rpcSession = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -07001335 ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port));
Yifan Hong194acf22021-06-29 18:44:56 -07001336 auto rpcBinder = rpcSession->getRootObject();
1337 ASSERT_NE(nullptr, rpcBinder);
1338
1339 ASSERT_EQ(OK, rpcBinder->pingBinder());
1340
1341 ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor())
1342 << "getInterfaceDescriptor should not crash system_server";
1343 ASSERT_EQ(OK, rpcBinder->pingBinder());
1344}
1345
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001346class BinderRpcServerOnly : public ::testing::TestWithParam<std::tuple<RpcSecurity, uint32_t>> {
1347public:
1348 static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) {
Andrei Homescuf30148c2023-03-10 00:31:45 +00001349 return std::string(newTlsFactory(std::get<0>(info.param))->toCString()) + "_serverV" +
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001350 std::to_string(std::get<1>(info.param));
1351 }
1352};
1353
1354TEST_P(BinderRpcServerOnly, SetExternalServerTest) {
1355 base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
1356 int sinkFd = sink.get();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001357 auto server = RpcServer::make(newTlsFactory(std::get<0>(GetParam())));
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001358 server->setProtocolVersion(std::get<1>(GetParam()));
1359 ASSERT_FALSE(server->hasServer());
1360 ASSERT_EQ(OK, server->setupExternalServer(std::move(sink)));
1361 ASSERT_TRUE(server->hasServer());
1362 base::unique_fd retrieved = server->releaseServer();
1363 ASSERT_FALSE(server->hasServer());
1364 ASSERT_EQ(sinkFd, retrieved.get());
1365}
1366
1367TEST_P(BinderRpcServerOnly, Shutdown) {
1368 if constexpr (!kEnableRpcThreads) {
1369 GTEST_SKIP() << "Test skipped because threads were disabled at build time";
1370 }
1371
1372 auto addr = allocateSocketAddress();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001373 auto server = RpcServer::make(newTlsFactory(std::get<0>(GetParam())));
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001374 server->setProtocolVersion(std::get<1>(GetParam()));
1375 ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str()));
1376 auto joinEnds = std::make_shared<OneOffSignal>();
1377
1378 // If things are broken and the thread never stops, don't block other tests. Because the thread
1379 // may run after the test finishes, it must not access the stack memory of the test. Hence,
1380 // shared pointers are passed.
1381 std::thread([server, joinEnds] {
1382 server->join();
1383 joinEnds->notify();
1384 }).detach();
1385
1386 bool shutdown = false;
1387 for (int i = 0; i < 10 && !shutdown; i++) {
Steven Morelanddd231e22022-09-08 19:47:49 +00001388 usleep(30 * 1000); // 30ms; total 300ms
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001389 if (server->shutdown()) shutdown = true;
1390 }
1391 ASSERT_TRUE(shutdown) << "server->shutdown() never returns true";
1392
1393 ASSERT_TRUE(joinEnds->wait(2s))
1394 << "After server->shutdown() returns true, join() did not stop after 2s";
1395}
1396
Frederick Mayledc07cf82022-05-26 20:30:12 +00001397INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerOnly,
1398 ::testing::Combine(::testing::ValuesIn(RpcSecurityValues()),
1399 ::testing::ValuesIn(testVersions())),
1400 BinderRpcServerOnly::PrintTestParam);
Yifan Hong702115c2021-06-24 15:39:18 -07001401
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001402class RpcTransportTestUtils {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001403public:
Frederick Mayledc07cf82022-05-26 20:30:12 +00001404 // Only parameterized only server version because `RpcSession` is bypassed
1405 // in the client half of the tests.
1406 using Param =
1407 std::tuple<SocketType, RpcSecurity, std::optional<RpcCertificateFormat>, uint32_t>;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001408 using ConnectToServer = std::function<base::unique_fd()>;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001409
1410 // A server that handles client socket connections.
1411 class Server {
1412 public:
David Brazdil21c887c2022-09-23 12:25:18 +01001413 using AcceptConnection = std::function<base::unique_fd(Server*)>;
1414
Yifan Hong1deca4b2021-09-10 16:16:44 -07001415 explicit Server() {}
1416 Server(Server&&) = default;
Yifan Honge07d2732021-09-13 21:59:14 -07001417 ~Server() { shutdownAndWait(); }
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001418 [[nodiscard]] AssertionResult setUp(
1419 const Param& param,
1420 std::unique_ptr<RpcAuth> auth = std::make_unique<RpcAuthSelfSigned>()) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001421 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param;
Andrei Homescuf30148c2023-03-10 00:31:45 +00001422 auto rpcServer = RpcServer::make(newTlsFactory(rpcSecurity));
Frederick Mayledc07cf82022-05-26 20:30:12 +00001423 rpcServer->setProtocolVersion(serverVersion);
Yifan Hong1deca4b2021-09-10 16:16:44 -07001424 switch (socketType) {
1425 case SocketType::PRECONNECTED: {
1426 return AssertionFailure() << "Not supported by this test";
1427 } break;
1428 case SocketType::UNIX: {
1429 auto addr = allocateSocketAddress();
1430 auto status = rpcServer->setupUnixDomainServer(addr.c_str());
1431 if (status != OK) {
1432 return AssertionFailure()
1433 << "setupUnixDomainServer: " << statusToString(status);
1434 }
1435 mConnectToServer = [addr] {
1436 return connectTo(UnixSocketAddress(addr.c_str()));
1437 };
1438 } break;
David Brazdil21c887c2022-09-23 12:25:18 +01001439 case SocketType::UNIX_BOOTSTRAP: {
1440 base::unique_fd bootstrapFdClient, bootstrapFdServer;
1441 if (!base::Socketpair(SOCK_STREAM, &bootstrapFdClient, &bootstrapFdServer)) {
1442 return AssertionFailure() << "Socketpair() failed";
1443 }
1444 auto status = rpcServer->setupUnixDomainSocketBootstrapServer(
1445 std::move(bootstrapFdServer));
1446 if (status != OK) {
1447 return AssertionFailure() << "setupUnixDomainSocketBootstrapServer: "
1448 << statusToString(status);
1449 }
1450 mBootstrapSocket = RpcTransportFd(std::move(bootstrapFdClient));
1451 mAcceptConnection = &Server::recvmsgServerConnection;
1452 mConnectToServer = [this] { return connectToUnixBootstrap(mBootstrapSocket); };
1453 } break;
Alice Wang893a9912022-10-24 10:44:09 +00001454 case SocketType::UNIX_RAW: {
1455 auto addr = allocateSocketAddress();
1456 auto status = rpcServer->setupRawSocketServer(initUnixSocket(addr));
1457 if (status != OK) {
1458 return AssertionFailure()
1459 << "setupRawSocketServer: " << statusToString(status);
1460 }
1461 mConnectToServer = [addr] {
1462 return connectTo(UnixSocketAddress(addr.c_str()));
1463 };
1464 } break;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001465 case SocketType::VSOCK: {
1466 auto port = allocateVsockPort();
David Brazdila47dfda2022-11-22 22:52:19 +00001467 auto status = rpcServer->setupVsockServer(VMADDR_CID_LOCAL, port);
Yifan Hong1deca4b2021-09-10 16:16:44 -07001468 if (status != OK) {
1469 return AssertionFailure() << "setupVsockServer: " << statusToString(status);
1470 }
1471 mConnectToServer = [port] {
1472 return connectTo(VsockSocketAddress(VMADDR_CID_LOCAL, port));
1473 };
1474 } break;
1475 case SocketType::INET: {
1476 unsigned int port;
1477 auto status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port);
1478 if (status != OK) {
1479 return AssertionFailure() << "setupInetServer: " << statusToString(status);
1480 }
1481 mConnectToServer = [port] {
1482 const char* addr = kLocalInetAddress;
1483 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
1484 if (aiStart == nullptr) return base::unique_fd{};
1485 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
1486 auto fd = connectTo(
1487 InetSocketAddress(ai->ai_addr, ai->ai_addrlen, addr, port));
1488 if (fd.ok()) return fd;
1489 }
1490 ALOGE("None of the socket address resolved for %s:%u can be connected",
1491 addr, port);
1492 return base::unique_fd{};
1493 };
Andrei Homescu68a55612022-08-02 01:25:15 +00001494 } break;
1495 case SocketType::TIPC: {
1496 LOG_ALWAYS_FATAL("RpcTransportTest should not be enabled for TIPC");
1497 } break;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001498 }
1499 mFd = rpcServer->releaseServer();
Pawan49d74cb2022-08-03 21:19:11 +00001500 if (!mFd.fd.ok()) return AssertionFailure() << "releaseServer returns invalid fd";
Andrei Homescuf30148c2023-03-10 00:31:45 +00001501 mCtx = newTlsFactory(rpcSecurity, mCertVerifier, std::move(auth))->newServerCtx();
Yifan Hong1deca4b2021-09-10 16:16:44 -07001502 if (mCtx == nullptr) return AssertionFailure() << "newServerCtx";
1503 mSetup = true;
1504 return AssertionSuccess();
1505 }
1506 RpcTransportCtx* getCtx() const { return mCtx.get(); }
1507 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
1508 return mCertVerifier;
1509 }
1510 ConnectToServer getConnectToServerFn() { return mConnectToServer; }
1511 void start() {
1512 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
1513 mThread = std::make_unique<std::thread>(&Server::run, this);
1514 }
David Brazdil21c887c2022-09-23 12:25:18 +01001515
1516 base::unique_fd acceptServerConnection() {
1517 return base::unique_fd(TEMP_FAILURE_RETRY(
1518 accept4(mFd.fd.get(), nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK)));
1519 }
1520
1521 base::unique_fd recvmsgServerConnection() {
1522 std::vector<std::variant<base::unique_fd, base::borrowed_fd>> fds;
1523 int buf;
1524 iovec iov{&buf, sizeof(buf)};
1525
1526 if (receiveMessageFromSocket(mFd, &iov, 1, &fds) < 0) {
1527 int savedErrno = errno;
1528 LOG(FATAL) << "Failed receiveMessage: " << strerror(savedErrno);
1529 }
1530 if (fds.size() != 1) {
1531 LOG(FATAL) << "Expected one FD from receiveMessage(), got " << fds.size();
1532 }
1533 return std::move(std::get<base::unique_fd>(fds[0]));
1534 }
1535
Yifan Hong1deca4b2021-09-10 16:16:44 -07001536 void run() {
1537 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
1538
1539 std::vector<std::thread> threads;
1540 while (OK == mFdTrigger->triggerablePoll(mFd, POLLIN)) {
David Brazdil21c887c2022-09-23 12:25:18 +01001541 base::unique_fd acceptedFd = mAcceptConnection(this);
Yifan Hong1deca4b2021-09-10 16:16:44 -07001542 threads.emplace_back(&Server::handleOne, this, std::move(acceptedFd));
1543 }
1544
1545 for (auto& thread : threads) thread.join();
1546 }
1547 void handleOne(android::base::unique_fd acceptedFd) {
1548 ASSERT_TRUE(acceptedFd.ok());
Pawan3e0061c2022-08-26 21:08:34 +00001549 RpcTransportFd transportFd(std::move(acceptedFd));
Pawan49d74cb2022-08-03 21:19:11 +00001550 auto serverTransport = mCtx->newTransport(std::move(transportFd), mFdTrigger.get());
Yifan Hong1deca4b2021-09-10 16:16:44 -07001551 if (serverTransport == nullptr) return; // handshake failed
Yifan Hong67519322021-09-13 18:51:16 -07001552 ASSERT_TRUE(mPostConnect(serverTransport.get(), mFdTrigger.get()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001553 }
Yifan Honge07d2732021-09-13 21:59:14 -07001554 void shutdownAndWait() {
Yifan Hong67519322021-09-13 18:51:16 -07001555 shutdown();
1556 join();
1557 }
1558 void shutdown() { mFdTrigger->trigger(); }
1559
1560 void setPostConnect(
1561 std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> fn) {
1562 mPostConnect = std::move(fn);
Yifan Hong1deca4b2021-09-10 16:16:44 -07001563 }
1564
1565 private:
1566 std::unique_ptr<std::thread> mThread;
1567 ConnectToServer mConnectToServer;
David Brazdil21c887c2022-09-23 12:25:18 +01001568 AcceptConnection mAcceptConnection = &Server::acceptServerConnection;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001569 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
David Brazdil21c887c2022-09-23 12:25:18 +01001570 RpcTransportFd mFd, mBootstrapSocket;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001571 std::unique_ptr<RpcTransportCtx> mCtx;
1572 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
1573 std::make_shared<RpcCertificateVerifierSimple>();
1574 bool mSetup = false;
Yifan Hong67519322021-09-13 18:51:16 -07001575 // The function invoked after connection and handshake. By default, it is
1576 // |defaultPostConnect| that sends |kMessage| to the client.
1577 std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> mPostConnect =
1578 Server::defaultPostConnect;
1579
1580 void join() {
1581 if (mThread != nullptr) {
1582 mThread->join();
1583 mThread = nullptr;
1584 }
1585 }
1586
1587 static AssertionResult defaultPostConnect(RpcTransport* serverTransport,
1588 FdTrigger* fdTrigger) {
1589 std::string message(kMessage);
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001590 iovec messageIov{message.data(), message.size()};
Devin Moore695368f2022-06-03 22:29:14 +00001591 auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1,
Frederick Mayle69a0c992022-05-26 20:38:39 +00001592 std::nullopt, nullptr);
Yifan Hong67519322021-09-13 18:51:16 -07001593 if (status != OK) return AssertionFailure() << statusToString(status);
1594 return AssertionSuccess();
1595 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07001596 };
1597
1598 class Client {
1599 public:
1600 explicit Client(ConnectToServer connectToServer) : mConnectToServer(connectToServer) {}
1601 Client(Client&&) = default;
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001602 [[nodiscard]] AssertionResult setUp(const Param& param) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001603 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param;
1604 (void)serverVersion;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001605 mFdTrigger = FdTrigger::make();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001606 mCtx = newTlsFactory(rpcSecurity, mCertVerifier)->newClientCtx();
Yifan Hong1deca4b2021-09-10 16:16:44 -07001607 if (mCtx == nullptr) return AssertionFailure() << "newClientCtx";
1608 return AssertionSuccess();
1609 }
1610 RpcTransportCtx* getCtx() const { return mCtx.get(); }
1611 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
1612 return mCertVerifier;
1613 }
Yifan Hong67519322021-09-13 18:51:16 -07001614 // connect() and do handshake
1615 bool setUpTransport() {
1616 mFd = mConnectToServer();
Pawan49d74cb2022-08-03 21:19:11 +00001617 if (!mFd.fd.ok()) return AssertionFailure() << "Cannot connect to server";
Yifan Hong67519322021-09-13 18:51:16 -07001618 mClientTransport = mCtx->newTransport(std::move(mFd), mFdTrigger.get());
1619 return mClientTransport != nullptr;
1620 }
1621 AssertionResult readMessage(const std::string& expectedMessage = kMessage) {
1622 LOG_ALWAYS_FATAL_IF(mClientTransport == nullptr, "setUpTransport not called or failed");
1623 std::string readMessage(expectedMessage.size(), '\0');
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001624 iovec readMessageIov{readMessage.data(), readMessage.size()};
Devin Moore695368f2022-06-03 22:29:14 +00001625 status_t readStatus =
1626 mClientTransport->interruptableReadFully(mFdTrigger.get(), &readMessageIov, 1,
Frederick Mayleffe9ac22022-06-30 02:07:36 +00001627 std::nullopt, nullptr);
Yifan Hong67519322021-09-13 18:51:16 -07001628 if (readStatus != OK) {
1629 return AssertionFailure() << statusToString(readStatus);
1630 }
1631 if (readMessage != expectedMessage) {
1632 return AssertionFailure()
1633 << "Expected " << expectedMessage << ", actual " << readMessage;
1634 }
1635 return AssertionSuccess();
1636 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07001637 void run(bool handshakeOk = true, bool readOk = true) {
Yifan Hong67519322021-09-13 18:51:16 -07001638 if (!setUpTransport()) {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001639 ASSERT_FALSE(handshakeOk) << "newTransport returns nullptr, but it shouldn't";
1640 return;
1641 }
1642 ASSERT_TRUE(handshakeOk) << "newTransport does not return nullptr, but it should";
Yifan Hong67519322021-09-13 18:51:16 -07001643 ASSERT_EQ(readOk, readMessage());
Yifan Hong1deca4b2021-09-10 16:16:44 -07001644 }
1645
Pawan49d74cb2022-08-03 21:19:11 +00001646 bool isTransportWaiting() { return mClientTransport->isWaiting(); }
1647
Yifan Hong1deca4b2021-09-10 16:16:44 -07001648 private:
1649 ConnectToServer mConnectToServer;
Pawan3e0061c2022-08-26 21:08:34 +00001650 RpcTransportFd mFd;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001651 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
1652 std::unique_ptr<RpcTransportCtx> mCtx;
1653 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
1654 std::make_shared<RpcCertificateVerifierSimple>();
Yifan Hong67519322021-09-13 18:51:16 -07001655 std::unique_ptr<RpcTransport> mClientTransport;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001656 };
1657
1658 // Make A trust B.
1659 template <typename A, typename B>
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001660 static status_t trust(RpcSecurity rpcSecurity,
1661 std::optional<RpcCertificateFormat> certificateFormat, const A& a,
1662 const B& b) {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001663 if (rpcSecurity != RpcSecurity::TLS) return OK;
Yifan Hong22211f82021-09-14 12:32:25 -07001664 LOG_ALWAYS_FATAL_IF(!certificateFormat.has_value());
1665 auto bCert = b->getCtx()->getCertificate(*certificateFormat);
1666 return a->getCertVerifier()->addTrustedPeerCertificate(*certificateFormat, bCert);
Yifan Hong1deca4b2021-09-10 16:16:44 -07001667 }
1668
1669 static constexpr const char* kMessage = "hello";
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001670};
1671
1672class RpcTransportTest : public testing::TestWithParam<RpcTransportTestUtils::Param> {
1673public:
1674 using Server = RpcTransportTestUtils::Server;
1675 using Client = RpcTransportTestUtils::Client;
1676 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001677 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = info.param;
Andrei Homescuf30148c2023-03-10 00:31:45 +00001678 auto ret = PrintToString(socketType) + "_" + newTlsFactory(rpcSecurity)->toCString();
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001679 if (certificateFormat.has_value()) ret += "_" + PrintToString(*certificateFormat);
Frederick Mayledc07cf82022-05-26 20:30:12 +00001680 ret += "_serverV" + std::to_string(serverVersion);
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001681 return ret;
1682 }
1683 static std::vector<ParamType> getRpcTranportTestParams() {
1684 std::vector<ParamType> ret;
Frederick Mayledc07cf82022-05-26 20:30:12 +00001685 for (auto serverVersion : testVersions()) {
1686 for (auto socketType : testSocketTypes(false /* hasPreconnected */)) {
1687 for (auto rpcSecurity : RpcSecurityValues()) {
1688 switch (rpcSecurity) {
1689 case RpcSecurity::RAW: {
1690 ret.emplace_back(socketType, rpcSecurity, std::nullopt, serverVersion);
1691 } break;
1692 case RpcSecurity::TLS: {
1693 ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::PEM,
1694 serverVersion);
1695 ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::DER,
1696 serverVersion);
1697 } break;
1698 }
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001699 }
1700 }
1701 }
1702 return ret;
1703 }
1704 template <typename A, typename B>
1705 status_t trust(const A& a, const B& b) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001706 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
1707 (void)serverVersion;
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001708 return RpcTransportTestUtils::trust(rpcSecurity, certificateFormat, a, b);
1709 }
Andrei Homescu12106de2022-04-27 04:42:21 +00001710 void SetUp() override {
1711 if constexpr (!kEnableRpcThreads) {
1712 GTEST_SKIP() << "Test skipped because threads were disabled at build time";
1713 }
1714 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07001715};
1716
1717TEST_P(RpcTransportTest, GoodCertificate) {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001718 auto server = std::make_unique<Server>();
1719 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001720
1721 Client client(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001722 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001723
1724 ASSERT_EQ(OK, trust(&client, server));
1725 ASSERT_EQ(OK, trust(server, &client));
1726
1727 server->start();
1728 client.run();
1729}
1730
1731TEST_P(RpcTransportTest, MultipleClients) {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001732 auto server = std::make_unique<Server>();
1733 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001734
1735 std::vector<Client> clients;
1736 for (int i = 0; i < 2; i++) {
1737 auto& client = clients.emplace_back(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001738 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001739 ASSERT_EQ(OK, trust(&client, server));
1740 ASSERT_EQ(OK, trust(server, &client));
1741 }
1742
1743 server->start();
1744 for (auto& client : clients) client.run();
1745}
1746
1747TEST_P(RpcTransportTest, UntrustedServer) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001748 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
1749 (void)serverVersion;
Yifan Hong1deca4b2021-09-10 16:16:44 -07001750
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001751 auto untrustedServer = std::make_unique<Server>();
1752 ASSERT_TRUE(untrustedServer->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001753
1754 Client client(untrustedServer->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001755 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001756
1757 ASSERT_EQ(OK, trust(untrustedServer, &client));
1758
1759 untrustedServer->start();
1760
1761 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
1762 // the client can't verify the server's identity.
1763 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
1764 client.run(handshakeOk);
1765}
1766TEST_P(RpcTransportTest, MaliciousServer) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001767 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
1768 (void)serverVersion;
1769
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001770 auto validServer = std::make_unique<Server>();
1771 ASSERT_TRUE(validServer->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001772
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001773 auto maliciousServer = std::make_unique<Server>();
1774 ASSERT_TRUE(maliciousServer->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001775
1776 Client client(maliciousServer->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001777 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001778
1779 ASSERT_EQ(OK, trust(&client, validServer));
1780 ASSERT_EQ(OK, trust(validServer, &client));
1781 ASSERT_EQ(OK, trust(maliciousServer, &client));
1782
1783 maliciousServer->start();
1784
1785 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
1786 // the client can't verify the server's identity.
1787 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
1788 client.run(handshakeOk);
1789}
1790
1791TEST_P(RpcTransportTest, UntrustedClient) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001792 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
1793 (void)serverVersion;
1794
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001795 auto server = std::make_unique<Server>();
1796 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001797
1798 Client client(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001799 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001800
1801 ASSERT_EQ(OK, trust(&client, server));
1802
1803 server->start();
1804
1805 // For TLS, Client should be able to verify server's identity, so client should see
1806 // do_handshake() successfully executed. However, server shouldn't be able to verify client's
1807 // identity and should drop the connection, so client shouldn't be able to read anything.
1808 bool readOk = rpcSecurity != RpcSecurity::TLS;
1809 client.run(true, readOk);
1810}
1811
1812TEST_P(RpcTransportTest, MaliciousClient) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001813 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
1814 (void)serverVersion;
1815
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001816 auto server = std::make_unique<Server>();
1817 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001818
1819 Client validClient(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001820 ASSERT_TRUE(validClient.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001821 Client maliciousClient(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001822 ASSERT_TRUE(maliciousClient.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07001823
1824 ASSERT_EQ(OK, trust(&validClient, server));
1825 ASSERT_EQ(OK, trust(&maliciousClient, server));
1826
1827 server->start();
1828
1829 // See UntrustedClient.
1830 bool readOk = rpcSecurity != RpcSecurity::TLS;
1831 maliciousClient.run(true, readOk);
1832}
1833
Yifan Hong67519322021-09-13 18:51:16 -07001834TEST_P(RpcTransportTest, Trigger) {
1835 std::string msg2 = ", world!";
1836 std::mutex writeMutex;
1837 std::condition_variable writeCv;
1838 bool shouldContinueWriting = false;
1839 auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001840 std::string message(RpcTransportTestUtils::kMessage);
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001841 iovec messageIov{message.data(), message.size()};
Frederick Mayle69a0c992022-05-26 20:38:39 +00001842 auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1,
1843 std::nullopt, nullptr);
Yifan Hong67519322021-09-13 18:51:16 -07001844 if (status != OK) return AssertionFailure() << statusToString(status);
1845
1846 {
1847 std::unique_lock<std::mutex> lock(writeMutex);
1848 if (!writeCv.wait_for(lock, 3s, [&] { return shouldContinueWriting; })) {
1849 return AssertionFailure() << "write barrier not cleared in time!";
1850 }
1851 }
1852
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001853 iovec msg2Iov{msg2.data(), msg2.size()};
Frederick Mayle69a0c992022-05-26 20:38:39 +00001854 status = serverTransport->interruptableWriteFully(fdTrigger, &msg2Iov, 1, std::nullopt,
1855 nullptr);
Steven Morelandc591b472021-09-16 13:56:11 -07001856 if (status != DEAD_OBJECT)
Yifan Hong67519322021-09-13 18:51:16 -07001857 return AssertionFailure() << "When FdTrigger is shut down, interruptableWriteFully "
Steven Morelandc591b472021-09-16 13:56:11 -07001858 "should return DEAD_OBJECT, but it is "
Yifan Hong67519322021-09-13 18:51:16 -07001859 << statusToString(status);
1860 return AssertionSuccess();
1861 };
1862
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001863 auto server = std::make_unique<Server>();
1864 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong67519322021-09-13 18:51:16 -07001865
1866 // Set up client
1867 Client client(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001868 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong67519322021-09-13 18:51:16 -07001869
1870 // Exchange keys
1871 ASSERT_EQ(OK, trust(&client, server));
1872 ASSERT_EQ(OK, trust(server, &client));
1873
1874 server->setPostConnect(serverPostConnect);
1875
Yifan Hong67519322021-09-13 18:51:16 -07001876 server->start();
1877 // connect() to server and do handshake
1878 ASSERT_TRUE(client.setUpTransport());
Yifan Hong22211f82021-09-14 12:32:25 -07001879 // read the first message. This ensures that server has finished handshake and start handling
1880 // client fd. Server thread should pause at writeCv.wait_for().
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001881 ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage));
Yifan Hong67519322021-09-13 18:51:16 -07001882 // Trigger server shutdown after server starts handling client FD. This ensures that the second
1883 // write is on an FdTrigger that has been shut down.
1884 server->shutdown();
1885 // Continues server thread to write the second message.
1886 {
Yifan Hong22211f82021-09-14 12:32:25 -07001887 std::lock_guard<std::mutex> lock(writeMutex);
Yifan Hong67519322021-09-13 18:51:16 -07001888 shouldContinueWriting = true;
Yifan Hong67519322021-09-13 18:51:16 -07001889 }
Yifan Hong22211f82021-09-14 12:32:25 -07001890 writeCv.notify_all();
Yifan Hong67519322021-09-13 18:51:16 -07001891 // After this line, server thread unblocks and attempts to write the second message, but
Steven Morelandc591b472021-09-16 13:56:11 -07001892 // shutdown is triggered, so write should failed with DEAD_OBJECT. See |serverPostConnect|.
Yifan Hong67519322021-09-13 18:51:16 -07001893 // On the client side, second read fails with DEAD_OBJECT
1894 ASSERT_FALSE(client.readMessage(msg2));
1895}
1896
Pawan49d74cb2022-08-03 21:19:11 +00001897TEST_P(RpcTransportTest, CheckWaitingForRead) {
1898 std::mutex readMutex;
1899 std::condition_variable readCv;
1900 bool shouldContinueReading = false;
1901 // Server will write data on transport once its started
1902 auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) {
1903 std::string message(RpcTransportTestUtils::kMessage);
1904 iovec messageIov{message.data(), message.size()};
1905 auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1,
1906 std::nullopt, nullptr);
1907 if (status != OK) return AssertionFailure() << statusToString(status);
1908
1909 {
1910 std::unique_lock<std::mutex> lock(readMutex);
1911 shouldContinueReading = true;
1912 lock.unlock();
1913 readCv.notify_all();
1914 }
1915 return AssertionSuccess();
1916 };
1917
1918 // Setup Server and client
1919 auto server = std::make_unique<Server>();
1920 ASSERT_TRUE(server->setUp(GetParam()));
1921
1922 Client client(server->getConnectToServerFn());
1923 ASSERT_TRUE(client.setUp(GetParam()));
1924
1925 ASSERT_EQ(OK, trust(&client, server));
1926 ASSERT_EQ(OK, trust(server, &client));
1927 server->setPostConnect(serverPostConnect);
1928
1929 server->start();
1930 ASSERT_TRUE(client.setUpTransport());
1931 {
1932 // Wait till server writes data
1933 std::unique_lock<std::mutex> lock(readMutex);
1934 ASSERT_TRUE(readCv.wait_for(lock, 3s, [&] { return shouldContinueReading; }));
1935 }
1936
1937 // Since there is no read polling here, we will get polling count 0
1938 ASSERT_FALSE(client.isTransportWaiting());
1939 ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage));
1940 // Thread should increment polling count, read and decrement polling count
1941 // Again, polling count should be zero here
1942 ASSERT_FALSE(client.isTransportWaiting());
1943
1944 server->shutdown();
1945}
1946
Yifan Hong1deca4b2021-09-10 16:16:44 -07001947INSTANTIATE_TEST_CASE_P(BinderRpc, RpcTransportTest,
Yifan Hong22211f82021-09-14 12:32:25 -07001948 ::testing::ValuesIn(RpcTransportTest::getRpcTranportTestParams()),
Yifan Hong1deca4b2021-09-10 16:16:44 -07001949 RpcTransportTest::PrintParamInfo);
1950
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001951class RpcTransportTlsKeyTest
Frederick Mayledc07cf82022-05-26 20:30:12 +00001952 : public testing::TestWithParam<
1953 std::tuple<SocketType, RpcCertificateFormat, RpcKeyFormat, uint32_t>> {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001954public:
1955 template <typename A, typename B>
1956 status_t trust(const A& a, const B& b) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001957 auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam();
1958 (void)serverVersion;
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001959 return RpcTransportTestUtils::trust(RpcSecurity::TLS, certificateFormat, a, b);
1960 }
1961 static std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00001962 auto [socketType, certificateFormat, keyFormat, serverVersion] = info.param;
1963 return PrintToString(socketType) + "_certificate_" + PrintToString(certificateFormat) +
1964 "_key_" + PrintToString(keyFormat) + "_serverV" + std::to_string(serverVersion);
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001965 };
1966};
1967
1968TEST_P(RpcTransportTlsKeyTest, PreSignedCertificate) {
Andrei Homescu12106de2022-04-27 04:42:21 +00001969 if constexpr (!kEnableRpcThreads) {
1970 GTEST_SKIP() << "Test skipped because threads were disabled at build time";
1971 }
1972
Frederick Mayledc07cf82022-05-26 20:30:12 +00001973 auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam();
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001974
1975 std::vector<uint8_t> pkeyData, certData;
1976 {
1977 auto pkey = makeKeyPairForSelfSignedCert();
1978 ASSERT_NE(nullptr, pkey);
1979 auto cert = makeSelfSignedCert(pkey.get(), kCertValidSeconds);
1980 ASSERT_NE(nullptr, cert);
1981 pkeyData = serializeUnencryptedPrivatekey(pkey.get(), keyFormat);
1982 certData = serializeCertificate(cert.get(), certificateFormat);
1983 }
1984
1985 auto desPkey = deserializeUnencryptedPrivatekey(pkeyData, keyFormat);
1986 auto desCert = deserializeCertificate(certData, certificateFormat);
1987 auto auth = std::make_unique<RpcAuthPreSigned>(std::move(desPkey), std::move(desCert));
Frederick Mayledc07cf82022-05-26 20:30:12 +00001988 auto utilsParam = std::make_tuple(socketType, RpcSecurity::TLS,
1989 std::make_optional(certificateFormat), serverVersion);
Yifan Hongb1ce80c2021-09-17 22:10:58 -07001990
1991 auto server = std::make_unique<RpcTransportTestUtils::Server>();
1992 ASSERT_TRUE(server->setUp(utilsParam, std::move(auth)));
1993
1994 RpcTransportTestUtils::Client client(server->getConnectToServerFn());
1995 ASSERT_TRUE(client.setUp(utilsParam));
1996
1997 ASSERT_EQ(OK, trust(&client, server));
1998 ASSERT_EQ(OK, trust(server, &client));
1999
2000 server->start();
2001 client.run();
2002}
2003
2004INSTANTIATE_TEST_CASE_P(
2005 BinderRpc, RpcTransportTlsKeyTest,
2006 testing::Combine(testing::ValuesIn(testSocketTypes(false /* hasPreconnected*/)),
2007 testing::Values(RpcCertificateFormat::PEM, RpcCertificateFormat::DER),
Frederick Mayledc07cf82022-05-26 20:30:12 +00002008 testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER),
2009 testing::ValuesIn(testVersions())),
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002010 RpcTransportTlsKeyTest::PrintParamInfo);
Andrei Homescud65666d2023-03-03 07:28:02 +00002011#endif // BINDER_RPC_TO_TRUSTY_TEST
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002012
Steven Morelandc1635952021-04-01 16:20:47 +00002013} // namespace android
2014
2015int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +00002016 ::testing::InitGoogleTest(&argc, argv);
2017 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
Steven Morelanda83191d2021-10-27 10:14:53 -07002018
Steven Moreland5553ac42020-11-11 02:14:45 +00002019 return RUN_ALL_TESTS();
2020}