blob: da5a8e388151d6349e2fe890d1bbb4b06931032f [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
Tomasz Wasilczyk38a22ee2023-10-19 20:04:46 +000017#ifndef __ANDROID_VENDOR__
18// only used on NDK tests outside of vendor
Andrei Homescu9d8adb12022-08-02 04:38:30 +000019#include <aidl/IBinderRpcTest.h>
Tomasz Wasilczyk38a22ee2023-10-19 20:04:46 +000020#endif
Steven Moreland5553ac42020-11-11 02:14:45 +000021
Devin Moore1df9c5f2024-07-23 22:09:29 +000022#if defined(__LP64__)
23#define TEST_FILE_SUFFIX "64"
24#else
25#define TEST_FILE_SUFFIX "32"
26#endif
27
Steven Morelandc1635952021-04-01 16:20:47 +000028#include <chrono>
29#include <cstdlib>
30#include <iostream>
31#include <thread>
Steven Moreland659416d2021-05-11 00:47:50 +000032#include <type_traits>
Steven Morelandc1635952021-04-01 16:20:47 +000033
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070034#include <dirent.h>
Andrei Homescu2a298012022-06-15 01:08:54 +000035#include <dlfcn.h>
Yifan Hong1deca4b2021-09-10 16:16:44 -070036#include <poll.h>
Steven Morelandc1635952021-04-01 16:20:47 +000037#include <sys/prctl.h>
Andrei Homescu992a4052022-06-28 21:26:18 +000038#include <sys/socket.h>
Steven Morelandc1635952021-04-01 16:20:47 +000039
Andrei Homescud65666d2023-03-03 07:28:02 +000040#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +000041#include <binder/RpcTransportTipcAndroid.h>
42#include <trusty/tipc.h>
Andrei Homescud65666d2023-03-03 07:28:02 +000043#endif // BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +000044
Tomasz Wasilczyk657c2bc2023-11-07 06:57:42 -080045#include "../Utils.h"
Andrei Homescu2a298012022-06-15 01:08:54 +000046#include "binderRpcTestCommon.h"
Andrei Homescu96834632022-10-14 00:49:49 +000047#include "binderRpcTestFixture.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000048
Devin Moorec370db42024-08-09 23:18:05 +000049// TODO need to add IServiceManager.cpp/.h to libbinder_no_kernel
50#ifdef BINDER_WITH_KERNEL_IPC
51#include "android-base/logging.h"
52#include "android/binder_manager.h"
53#include "android/binder_rpc.h"
54#endif // BINDER_WITH_KERNEL_IPC
55
Yifan Hong1a235852021-05-13 16:07:47 -070056using namespace std::chrono_literals;
Yifan Hong67519322021-09-13 18:51:16 -070057using namespace std::placeholders;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070058using android::binder::borrowed_fd;
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -070059using android::binder::GetExecutableDirectory;
60using android::binder::ReadFdToString;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070061using android::binder::unique_fd;
Yifan Hong1deca4b2021-09-10 16:16:44 -070062using testing::AssertionFailure;
63using testing::AssertionResult;
64using testing::AssertionSuccess;
Yifan Hong1a235852021-05-13 16:07:47 -070065
Steven Moreland5553ac42020-11-11 02:14:45 +000066namespace android {
67
Andrei Homescu12106de2022-04-27 04:42:21 +000068#ifdef BINDER_TEST_NO_SHARED_LIBS
69constexpr bool kEnableSharedLibs = false;
70#else
71constexpr bool kEnableSharedLibs = true;
72#endif
73
Andrei Homescud65666d2023-03-03 07:28:02 +000074#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +000075constexpr char kTrustyIpcDevice[] = "/dev/trusty-ipc-dev0";
76#endif
77
Devin Moore0555fbf2024-08-29 15:51:50 +000078constexpr char kKnownAidlService[] = "activity";
79
Frederick Maylea12b0962022-06-25 01:13:22 +000080static std::string WaitStatusToString(int wstatus) {
81 if (WIFEXITED(wstatus)) {
Tomasz Wasilczyk68c42082024-05-20 11:59:35 -070082 return "exit status " + std::to_string(WEXITSTATUS(wstatus));
Frederick Maylea12b0962022-06-25 01:13:22 +000083 }
84 if (WIFSIGNALED(wstatus)) {
Tomasz Wasilczyk68c42082024-05-20 11:59:35 -070085 return "term signal " + std::to_string(WTERMSIG(wstatus));
Frederick Maylea12b0962022-06-25 01:13:22 +000086 }
Tomasz Wasilczyk68c42082024-05-20 11:59:35 -070087 return "unexpected state " + std::to_string(wstatus);
Frederick Maylea12b0962022-06-25 01:13:22 +000088}
89
Steven Moreland276d8df2022-09-28 23:56:39 +000090static void debugBacktrace(pid_t pid) {
91 std::cerr << "TAKING BACKTRACE FOR PID " << pid << std::endl;
92 system((std::string("debuggerd -b ") + std::to_string(pid)).c_str());
93}
94
Steven Moreland5553ac42020-11-11 02:14:45 +000095class Process {
96public:
Andrei Homescu96834632022-10-14 00:49:49 +000097 Process(Process&& other)
98 : mCustomExitStatusCheck(std::move(other.mCustomExitStatusCheck)),
99 mReadEnd(std::move(other.mReadEnd)),
100 mWriteEnd(std::move(other.mWriteEnd)) {
101 // The default move constructor doesn't clear mPid after moving it,
102 // which we need to do because the destructor checks for mPid!=0
103 mPid = other.mPid;
104 other.mPid = 0;
105 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700106 Process(const std::function<void(borrowed_fd /* writeEnd */, borrowed_fd /* readEnd */)>& f) {
107 unique_fd childWriteEnd;
108 unique_fd childReadEnd;
109 if (!binder::Pipe(&mReadEnd, &childWriteEnd, 0)) PLOGF("child write pipe failed");
110 if (!binder::Pipe(&childReadEnd, &mWriteEnd, 0)) PLOGF("child read pipe failed");
Steven Moreland5553ac42020-11-11 02:14:45 +0000111 if (0 == (mPid = fork())) {
112 // racey: assume parent doesn't crash before this is set
113 prctl(PR_SET_PDEATHSIG, SIGHUP);
114
Yifan Hong1deca4b2021-09-10 16:16:44 -0700115 f(childWriteEnd, childReadEnd);
Steven Morelandaf4ca712021-05-24 23:22:08 +0000116
117 exit(0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000118 }
119 }
120 ~Process() {
121 if (mPid != 0) {
Frederick Maylea12b0962022-06-25 01:13:22 +0000122 int wstatus;
123 waitpid(mPid, &wstatus, 0);
124 if (mCustomExitStatusCheck) {
125 mCustomExitStatusCheck(wstatus);
126 } else {
127 EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0)
128 << "server process failed: " << WaitStatusToString(wstatus);
129 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000130 }
131 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700132 borrowed_fd readEnd() { return mReadEnd; }
133 borrowed_fd writeEnd() { return mWriteEnd; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000134
Frederick Maylea12b0962022-06-25 01:13:22 +0000135 void setCustomExitStatusCheck(std::function<void(int wstatus)> f) {
136 mCustomExitStatusCheck = std::move(f);
137 }
138
Frederick Mayle69a0c992022-05-26 20:38:39 +0000139 // Kill the process. Avoid if possible. Shutdown gracefully via an RPC instead.
140 void terminate() { kill(mPid, SIGTERM); }
141
Steven Moreland276d8df2022-09-28 23:56:39 +0000142 pid_t getPid() { return mPid; }
143
Steven Moreland5553ac42020-11-11 02:14:45 +0000144private:
Frederick Maylea12b0962022-06-25 01:13:22 +0000145 std::function<void(int wstatus)> mCustomExitStatusCheck;
Steven Moreland5553ac42020-11-11 02:14:45 +0000146 pid_t mPid = 0;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700147 unique_fd mReadEnd;
148 unique_fd mWriteEnd;
Steven Moreland5553ac42020-11-11 02:14:45 +0000149};
150
151static std::string allocateSocketAddress() {
152 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000153 std::string temp = getenv("TMPDIR") ?: "/tmp";
Steven Morelanddfb05ad2023-03-07 17:00:53 +0000154 auto ret = temp + "/binderRpcTest_" + std::to_string(getpid()) + "_" + std::to_string(id++);
Yifan Hong1deca4b2021-09-10 16:16:44 -0700155 unlink(ret.c_str());
156 return ret;
Steven Moreland5553ac42020-11-11 02:14:45 +0000157};
158
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700159static unique_fd initUnixSocket(std::string addr) {
Alice Wang893a9912022-10-24 10:44:09 +0000160 auto socket_addr = UnixSocketAddress(addr.c_str());
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700161 unique_fd fd(TEMP_FAILURE_RETRY(socket(socket_addr.addr()->sa_family, SOCK_STREAM, AF_UNIX)));
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700162 if (!fd.ok()) PLOGF("initUnixSocket failed to create socket");
163 if (0 != TEMP_FAILURE_RETRY(bind(fd.get(), socket_addr.addr(), socket_addr.addrSize()))) {
164 PLOGF("initUnixSocket failed to bind");
165 }
Alice Wang893a9912022-10-24 10:44:09 +0000166 return fd;
167}
168
Andrei Homescu96834632022-10-14 00:49:49 +0000169// Destructors need to be defined, even if pure virtual
170ProcessSession::~ProcessSession() {}
171
172class LinuxProcessSession : public ProcessSession {
173public:
Steven Moreland5553ac42020-11-11 02:14:45 +0000174 // reference to process hosting a socket server
175 Process host;
176
Andrei Homescu96834632022-10-14 00:49:49 +0000177 LinuxProcessSession(LinuxProcessSession&&) = default;
178 LinuxProcessSession(Process&& host) : host(std::move(host)) {}
179 ~LinuxProcessSession() override {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000180 for (auto& session : sessions) {
181 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000182 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000183
Steven Moreland67f85902023-03-15 01:13:49 +0000184 for (size_t sessionNum = 0; sessionNum < sessions.size(); sessionNum++) {
185 auto& info = sessions.at(sessionNum);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000186 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000187
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000188 EXPECT_NE(nullptr, session);
189 EXPECT_NE(nullptr, session->state());
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -0700190 EXPECT_EQ(0u, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000191
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000192 wp<RpcSession> weakSession = session;
193 session = nullptr;
Steven Moreland276d8df2022-09-28 23:56:39 +0000194
Steven Moreland57042712022-10-04 23:56:45 +0000195 // b/244325464 - 'getStrongCount' is printing '1' on failure here, which indicates the
196 // the object should not actually be promotable. By looping, we distinguish a race here
197 // from a bug causing the object to not be promotable.
198 for (size_t i = 0; i < 3; i++) {
199 sp<RpcSession> strongSession = weakSession.promote();
200 EXPECT_EQ(nullptr, strongSession)
Steven Moreland67f85902023-03-15 01:13:49 +0000201 << "For session " << sessionNum << ". "
Steven Moreland57042712022-10-04 23:56:45 +0000202 << (debugBacktrace(host.getPid()), debugBacktrace(getpid()),
203 "Leaked sess: ")
204 << strongSession->getStrongCount() << " checked time " << i;
205
206 if (strongSession != nullptr) {
207 sleep(1);
208 }
209 }
Steven Moreland736664b2021-05-01 04:27:25 +0000210 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000211 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000212
Andrei Homescu96834632022-10-14 00:49:49 +0000213 void setCustomExitStatusCheck(std::function<void(int wstatus)> f) override {
214 host.setCustomExitStatusCheck(std::move(f));
Steven Moreland5553ac42020-11-11 02:14:45 +0000215 }
Andrei Homescu96834632022-10-14 00:49:49 +0000216
217 void terminate() override { host.terminate(); }
Steven Moreland5553ac42020-11-11 02:14:45 +0000218};
219
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700220static unique_fd connectTo(const RpcSocketAddress& addr) {
221 unique_fd serverFd(
Steven Moreland4198a122021-08-03 17:37:58 -0700222 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700223 if (!serverFd.ok()) {
224 PLOGF("Could not create socket %s", addr.toString().c_str());
225 }
Steven Moreland4198a122021-08-03 17:37:58 -0700226
227 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700228 PLOGF("Could not connect to socket %s", addr.toString().c_str());
Steven Moreland4198a122021-08-03 17:37:58 -0700229 }
230 return serverFd;
231}
232
Andrei Homescud65666d2023-03-03 07:28:02 +0000233#ifndef BINDER_RPC_TO_TRUSTY_TEST
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700234static unique_fd connectToUnixBootstrap(const RpcTransportFd& transportFd) {
235 unique_fd sockClient, sockServer;
236 if (!binder::Socketpair(SOCK_STREAM, &sockClient, &sockServer)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700237 PLOGF("Failed socketpair()");
David Brazdil21c887c2022-09-23 12:25:18 +0100238 }
239
240 int zero = 0;
241 iovec iov{&zero, sizeof(zero)};
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700242 std::vector<std::variant<unique_fd, borrowed_fd>> fds;
David Brazdil21c887c2022-09-23 12:25:18 +0100243 fds.emplace_back(std::move(sockServer));
244
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000245 if (binder::os::sendMessageOnSocket(transportFd, &iov, 1, &fds) < 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700246 PLOGF("Failed sendMessageOnSocket");
David Brazdil21c887c2022-09-23 12:25:18 +0100247 }
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -0700248 return sockClient;
David Brazdil21c887c2022-09-23 12:25:18 +0100249}
Andrei Homescud65666d2023-03-03 07:28:02 +0000250#endif // BINDER_RPC_TO_TRUSTY_TEST
David Brazdil21c887c2022-09-23 12:25:18 +0100251
Andrei Homescuf30148c2023-03-10 00:31:45 +0000252std::unique_ptr<RpcTransportCtxFactory> BinderRpc::newFactory(RpcSecurity rpcSecurity) {
253 return newTlsFactory(rpcSecurity);
Andrei Homescu96834632022-10-14 00:49:49 +0000254}
Andrei Homescu2a298012022-06-15 01:08:54 +0000255
Andrei Homescu96834632022-10-14 00:49:49 +0000256// This creates a new process serving an interface on a certain number of
257// threads.
258std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
259 const BinderRpcOptions& options) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700260 LOG_ALWAYS_FATAL_IF(options.numSessions < 1, "Must have at least one session to a server");
Frederick Mayle69a0c992022-05-26 20:38:39 +0000261
Steven Moreland67f85902023-03-15 01:13:49 +0000262 if (options.numIncomingConnectionsBySession.size() != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700263 LOG_ALWAYS_FATAL_IF(options.numIncomingConnectionsBySession.size() != options.numSessions,
264 "%s: %zu != %zu", __func__,
265 options.numIncomingConnectionsBySession.size(), options.numSessions);
Steven Moreland67f85902023-03-15 01:13:49 +0000266 }
267
Steven Morelandb469f432023-07-28 22:13:47 +0000268 SocketType socketType = GetParam().type;
269 RpcSecurity rpcSecurity = GetParam().security;
270 uint32_t clientVersion = GetParam().clientVersion;
271 uint32_t serverVersion = GetParam().serverVersion;
272 bool singleThreaded = GetParam().singleThreaded;
273 bool noKernel = GetParam().noKernel;
Andrei Homescu96834632022-10-14 00:49:49 +0000274
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -0700275 std::string path = GetExecutableDirectory();
Tomasz Wasilczyk68c42082024-05-20 11:59:35 -0700276 auto servicePath = path + "/binder_rpc_test_service" +
Devin Moore1df9c5f2024-07-23 22:09:29 +0000277 (singleThreaded ? "_single_threaded" : "") + (noKernel ? "_no_kernel" : "") +
278 TEST_FILE_SUFFIX;
Andrei Homescu96834632022-10-14 00:49:49 +0000279
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700280 unique_fd bootstrapClientFd, socketFd;
Alice Wang1ef010b2022-11-14 09:09:25 +0000281
Alice Wang893a9912022-10-24 10:44:09 +0000282 auto addr = allocateSocketAddress();
283 // Initializes the socket before the fork/exec.
284 if (socketType == SocketType::UNIX_RAW) {
285 socketFd = initUnixSocket(addr);
Alice Wang1ef010b2022-11-14 09:09:25 +0000286 } else if (socketType == SocketType::UNIX_BOOTSTRAP) {
287 // Do not set O_CLOEXEC, bootstrapServerFd needs to survive fork/exec.
288 // This is because we cannot pass ParcelFileDescriptor over a pipe.
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700289 if (!binder::Socketpair(SOCK_STREAM, &bootstrapClientFd, &socketFd)) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700290 PLOGF("Failed socketpair()");
Alice Wang1ef010b2022-11-14 09:09:25 +0000291 }
Alice Wang893a9912022-10-24 10:44:09 +0000292 }
Andrei Homescua858b0e2022-08-01 23:43:09 +0000293
Andrei Homescu96834632022-10-14 00:49:49 +0000294 auto ret = std::make_unique<LinuxProcessSession>(
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700295 Process([=](borrowed_fd writeEnd, borrowed_fd readEnd) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000296 if (socketType == SocketType::TIPC) {
297 // Trusty has a single persistent service
298 return;
299 }
300
Andrei Homescu96834632022-10-14 00:49:49 +0000301 auto writeFd = std::to_string(writeEnd.get());
302 auto readFd = std::to_string(readEnd.get());
Tomasz Wasilczyk7ba2e7e2023-11-13 13:18:57 -0800303 auto status = execl(servicePath.c_str(), servicePath.c_str(), writeFd.c_str(),
304 readFd.c_str(), NULL);
305 PLOGF("execl('%s', _, %s, %s) should not return at all, but it returned %d",
306 servicePath.c_str(), writeFd.c_str(), readFd.c_str(), status);
Andrei Homescu96834632022-10-14 00:49:49 +0000307 }));
308
309 BinderRpcTestServerConfig serverConfig;
310 serverConfig.numThreads = options.numThreads;
311 serverConfig.socketType = static_cast<int32_t>(socketType);
312 serverConfig.rpcSecurity = static_cast<int32_t>(rpcSecurity);
313 serverConfig.serverVersion = serverVersion;
Alice Wang893a9912022-10-24 10:44:09 +0000314 serverConfig.addr = addr;
Alice Wang893a9912022-10-24 10:44:09 +0000315 serverConfig.socketFd = socketFd.get();
Andrei Homescu96834632022-10-14 00:49:49 +0000316 for (auto mode : options.serverSupportedFileDescriptorTransportModes) {
317 serverConfig.serverSupportedFileDescriptorTransportModes.push_back(
318 static_cast<int32_t>(mode));
319 }
Andrei Homescu68a55612022-08-02 01:25:15 +0000320 if (socketType != SocketType::TIPC) {
321 writeToFd(ret->host.writeEnd(), serverConfig);
322 }
Andrei Homescu96834632022-10-14 00:49:49 +0000323
324 std::vector<sp<RpcSession>> sessions;
325 auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>();
326 for (size_t i = 0; i < options.numSessions; i++) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000327 std::unique_ptr<RpcTransportCtxFactory> factory;
328 if (socketType == SocketType::TIPC) {
Andrei Homescud65666d2023-03-03 07:28:02 +0000329#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +0000330 factory = RpcTransportCtxFactoryTipcAndroid::make();
331#else
332 LOG_ALWAYS_FATAL("TIPC socket type only supported on vendor");
333#endif
334 } else {
Andrei Homescuf30148c2023-03-10 00:31:45 +0000335 factory = newTlsFactory(rpcSecurity, certVerifier);
Andrei Homescu68a55612022-08-02 01:25:15 +0000336 }
337 sessions.emplace_back(RpcSession::make(std::move(factory)));
David Brazdil21c887c2022-09-23 12:25:18 +0100338 }
339
Andrei Homescu68a55612022-08-02 01:25:15 +0000340 BinderRpcTestServerInfo serverInfo;
341 if (socketType != SocketType::TIPC) {
342 serverInfo = readFromFd<BinderRpcTestServerInfo>(ret->host.readEnd());
343 BinderRpcTestClientInfo clientInfo;
344 for (const auto& session : sessions) {
345 auto& parcelableCert = clientInfo.certs.emplace_back();
346 parcelableCert.data = session->getCertificate(RpcCertificateFormat::PEM);
347 }
348 writeToFd(ret->host.writeEnd(), clientInfo);
Andrei Homescu96834632022-10-14 00:49:49 +0000349
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700350 LOG_ALWAYS_FATAL_IF(serverInfo.port > std::numeric_limits<unsigned int>::max());
Andrei Homescu68a55612022-08-02 01:25:15 +0000351 if (socketType == SocketType::INET) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700352 LOG_ALWAYS_FATAL_IF(0 == serverInfo.port);
Andrei Homescu68a55612022-08-02 01:25:15 +0000353 }
Frederick Mayle69a0c992022-05-26 20:38:39 +0000354
Andrei Homescu68a55612022-08-02 01:25:15 +0000355 if (rpcSecurity == RpcSecurity::TLS) {
356 const auto& serverCert = serverInfo.cert.data;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700357 LOG_ALWAYS_FATAL_IF(
358 OK !=
359 certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM, serverCert));
Andrei Homescu68a55612022-08-02 01:25:15 +0000360 }
Yifan Hong1deca4b2021-09-10 16:16:44 -0700361 }
362
Andrei Homescu96834632022-10-14 00:49:49 +0000363 status_t status;
Steven Moreland736664b2021-05-01 04:27:25 +0000364
Steven Moreland67f85902023-03-15 01:13:49 +0000365 for (size_t i = 0; i < sessions.size(); i++) {
366 const auto& session = sessions.at(i);
367
368 size_t numIncoming = options.numIncomingConnectionsBySession.size() > 0
369 ? options.numIncomingConnectionsBySession.at(i)
370 : 0;
371
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700372 LOG_ALWAYS_FATAL_IF(!session->setProtocolVersion(clientVersion));
Steven Moreland67f85902023-03-15 01:13:49 +0000373 session->setMaxIncomingThreads(numIncoming);
Steven Morelandfeb13e82023-03-01 01:25:33 +0000374 session->setMaxOutgoingConnections(options.numOutgoingConnections);
Andrei Homescu96834632022-10-14 00:49:49 +0000375 session->setFileDescriptorTransportMode(options.clientFileDescriptorTransportMode);
Steven Morelandc1635952021-04-01 16:20:47 +0000376
Devin Moore18f63752024-08-08 21:01:24 +0000377 sockaddr_storage addr{};
378 socklen_t addrLen = 0;
379
Andrei Homescu96834632022-10-14 00:49:49 +0000380 switch (socketType) {
Devin Moore18f63752024-08-08 21:01:24 +0000381 case SocketType::PRECONNECTED: {
382 sockaddr_un addr_un{};
383 addr_un.sun_family = AF_UNIX;
384 strcpy(addr_un.sun_path, serverConfig.addr.c_str());
385 addr = *reinterpret_cast<sockaddr_storage*>(&addr_un);
386 addrLen = sizeof(sockaddr_un);
387
Andrei Homescu96834632022-10-14 00:49:49 +0000388 status = session->setupPreconnectedClient({}, [=]() {
389 return connectTo(UnixSocketAddress(serverConfig.addr.c_str()));
390 });
Devin Moore18f63752024-08-08 21:01:24 +0000391 } break;
Alice Wang893a9912022-10-24 10:44:09 +0000392 case SocketType::UNIX_RAW:
Devin Moore18f63752024-08-08 21:01:24 +0000393 case SocketType::UNIX: {
394 sockaddr_un addr_un{};
395 addr_un.sun_family = AF_UNIX;
396 strcpy(addr_un.sun_path, serverConfig.addr.c_str());
397 addr = *reinterpret_cast<sockaddr_storage*>(&addr_un);
398 addrLen = sizeof(sockaddr_un);
399
Andrei Homescu96834632022-10-14 00:49:49 +0000400 status = session->setupUnixDomainClient(serverConfig.addr.c_str());
Devin Moore18f63752024-08-08 21:01:24 +0000401 } break;
Andrei Homescu96834632022-10-14 00:49:49 +0000402 case SocketType::UNIX_BOOTSTRAP:
403 status = session->setupUnixDomainSocketBootstrapClient(
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700404 unique_fd(dup(bootstrapClientFd.get())));
Andrei Homescu96834632022-10-14 00:49:49 +0000405 break;
Devin Moore18f63752024-08-08 21:01:24 +0000406 case SocketType::VSOCK: {
407 sockaddr_vm addr_vm{
408 .svm_family = AF_VSOCK,
409 .svm_port = static_cast<unsigned int>(serverInfo.port),
410 .svm_cid = VMADDR_CID_LOCAL,
411 };
412 addr = *reinterpret_cast<sockaddr_storage*>(&addr_vm);
413 addrLen = sizeof(sockaddr_vm);
414
Tomasz Wasilczyk022db682024-06-17 13:55:51 -0700415 status = session->setupVsockClient(VMADDR_CID_LOCAL, serverInfo.port);
Devin Moore18f63752024-08-08 21:01:24 +0000416 } break;
417 case SocketType::INET: {
418 const std::string ip_addr = "127.0.0.1";
419 sockaddr_in addr_in{};
420 addr_in.sin_family = AF_INET;
421 addr_in.sin_port = htons(serverInfo.port);
422 inet_aton(ip_addr.c_str(), &addr_in.sin_addr);
423 addr = *reinterpret_cast<sockaddr_storage*>(&addr_in);
424 addrLen = sizeof(sockaddr_in);
425
426 status = session->setupInetClient(ip_addr.c_str(), serverInfo.port);
427 } break;
Andrei Homescu68a55612022-08-02 01:25:15 +0000428 case SocketType::TIPC:
429 status = session->setupPreconnectedClient({}, [=]() {
Andrei Homescud65666d2023-03-03 07:28:02 +0000430#ifdef BINDER_RPC_TO_TRUSTY_TEST
Andrei Homescu68a55612022-08-02 01:25:15 +0000431 auto port = trustyIpcPort(serverVersion);
Andrei Homescu4bea21772023-03-21 23:28:33 +0000432 for (size_t i = 0; i < 5; i++) {
433 // Try to connect several times,
434 // in case the service is slow to start
435 int tipcFd = tipc_connect(kTrustyIpcDevice, port.c_str());
436 if (tipcFd >= 0) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700437 return unique_fd(tipcFd);
Andrei Homescu4bea21772023-03-21 23:28:33 +0000438 }
439 usleep(50000);
440 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700441 return unique_fd();
Andrei Homescu68a55612022-08-02 01:25:15 +0000442#else
443 LOG_ALWAYS_FATAL("Tried to connect to Trusty outside of vendor");
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700444 return unique_fd();
Andrei Homescu68a55612022-08-02 01:25:15 +0000445#endif
446 });
447 break;
Andrei Homescu96834632022-10-14 00:49:49 +0000448 default:
449 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000450 }
Andrei Homescu96834632022-10-14 00:49:49 +0000451 if (options.allowConnectFailure && status != OK) {
452 ret->sessions.clear();
453 break;
454 }
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700455 LOG_ALWAYS_FATAL_IF(status != OK, "Could not connect: %s", statusToString(status).c_str());
Devin Moore18f63752024-08-08 21:01:24 +0000456 ret->sessions.push_back({session, session->getRootObject(), addr, addrLen});
Steven Morelandc1635952021-04-01 16:20:47 +0000457 }
Andrei Homescu96834632022-10-14 00:49:49 +0000458 return ret;
459}
Steven Morelandc1635952021-04-01 16:20:47 +0000460
Andrei Homescua858b0e2022-08-01 23:43:09 +0000461TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
462 if (clientOrServerSingleThreaded()) {
463 GTEST_SKIP() << "This test requires multiple threads";
464 }
465
Steven Moreland51cdc2c2024-09-17 17:21:27 +0000466 constexpr size_t kNumThreads = 5;
Steven Moreland5553ac42020-11-11 02:14:45 +0000467
Steven Moreland4313d7e2021-07-15 23:41:22 +0000468 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000469
470 EXPECT_OK(proc.rootIface->lock());
471
472 // block all but one thread taking locks
473 std::vector<std::thread> ts;
474 for (size_t i = 0; i < kNumThreads - 1; i++) {
475 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
476 }
477
Steven Morelandd6d816f2022-12-23 01:37:17 +0000478 usleep(100000); // give chance for calls on other threads
Steven Moreland5553ac42020-11-11 02:14:45 +0000479
480 // other calls still work
481 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
482
Steven Morelandd6d816f2022-12-23 01:37:17 +0000483 constexpr size_t blockTimeMs = 100;
Steven Moreland5553ac42020-11-11 02:14:45 +0000484 size_t epochMsBefore = epochMillis();
485 // after this, we should never see a response within this time
486 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
487
488 // this call should be blocked for blockTimeMs
489 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
490
491 size_t epochMsAfter = epochMillis();
492 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
493
494 for (auto& t : ts) t.join();
495}
496
Steven Moreland27f620a2023-03-06 19:44:36 +0000497static void testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t numCalls, size_t sleepMs) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000498 size_t epochMsBefore = epochMillis();
499
500 std::vector<std::thread> ts;
Yifan Hong1f44f982021-10-08 17:16:47 -0700501 for (size_t i = 0; i < numCalls; i++) {
502 ts.push_back(std::thread([&] { iface->sleepMs(sleepMs); }));
Steven Moreland5553ac42020-11-11 02:14:45 +0000503 }
504
505 for (auto& t : ts) t.join();
506
507 size_t epochMsAfter = epochMillis();
508
Yifan Hong1f44f982021-10-08 17:16:47 -0700509 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * sleepMs);
Steven Moreland5553ac42020-11-11 02:14:45 +0000510
Steven Moreland68e2ee22024-09-18 01:18:48 +0000511 // b/272429574, b/365294257
512 // This flakes too much to test. Parallelization is tested
513 // in ThreadPoolGreaterThanEqualRequested and other tests.
514 // Test to make sure calls are handled in parallel.
515 // EXPECT_LE(epochMsAfter, epochMsBefore + (numCalls - 1) * sleepMs);
Yifan Hong1f44f982021-10-08 17:16:47 -0700516}
517
Andrei Homescua858b0e2022-08-01 23:43:09 +0000518TEST_P(BinderRpc, ThreadPoolOverSaturated) {
519 if (clientOrServerSingleThreaded()) {
520 GTEST_SKIP() << "This test requires multiple threads";
521 }
522
Yifan Hong1f44f982021-10-08 17:16:47 -0700523 constexpr size_t kNumThreads = 10;
524 constexpr size_t kNumCalls = kNumThreads + 3;
525 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland73aa6f82023-03-15 21:49:07 +0000526
Steven Moreland68e2ee22024-09-18 01:18:48 +0000527 testThreadPoolOverSaturated(proc.rootIface, kNumCalls, 200 /*ms*/);
Yifan Hong1f44f982021-10-08 17:16:47 -0700528}
529
Andrei Homescua858b0e2022-08-01 23:43:09 +0000530TEST_P(BinderRpc, ThreadPoolLimitOutgoing) {
531 if (clientOrServerSingleThreaded()) {
532 GTEST_SKIP() << "This test requires multiple threads";
533 }
534
Yifan Hong1f44f982021-10-08 17:16:47 -0700535 constexpr size_t kNumThreads = 20;
536 constexpr size_t kNumOutgoingConnections = 10;
537 constexpr size_t kNumCalls = kNumOutgoingConnections + 3;
538 auto proc = createRpcTestSocketServerProcess(
539 {.numThreads = kNumThreads, .numOutgoingConnections = kNumOutgoingConnections});
Steven Moreland73aa6f82023-03-15 21:49:07 +0000540
Steven Moreland68e2ee22024-09-18 01:18:48 +0000541 testThreadPoolOverSaturated(proc.rootIface, kNumCalls, 200 /*ms*/);
Steven Moreland5553ac42020-11-11 02:14:45 +0000542}
543
Andrei Homescua858b0e2022-08-01 23:43:09 +0000544TEST_P(BinderRpc, ThreadingStressTest) {
545 if (clientOrServerSingleThreaded()) {
546 GTEST_SKIP() << "This test requires multiple threads";
547 }
548
Steven Moreland27f620a2023-03-06 19:44:36 +0000549 constexpr size_t kNumClientThreads = 5;
550 constexpr size_t kNumServerThreads = 5;
551 constexpr size_t kNumCalls = 50;
Steven Moreland5553ac42020-11-11 02:14:45 +0000552
Steven Moreland4313d7e2021-07-15 23:41:22 +0000553 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000554
555 std::vector<std::thread> threads;
556 for (size_t i = 0; i < kNumClientThreads; i++) {
557 threads.push_back(std::thread([&] {
558 for (size_t j = 0; j < kNumCalls; j++) {
559 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +0000560 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +0000561 EXPECT_EQ(proc.rootBinder, out);
562 }
563 }));
564 }
565
566 for (auto& t : threads) t.join();
567}
568
Steven Moreland925ba0a2021-09-17 18:06:32 -0700569static void saturateThreadPool(size_t threadCount, const sp<IBinderRpcTest>& iface) {
570 std::vector<std::thread> threads;
571 for (size_t i = 0; i < threadCount; i++) {
572 threads.push_back(std::thread([&] { EXPECT_OK(iface->sleepMs(500)); }));
573 }
574 for (auto& t : threads) t.join();
575}
576
Andrei Homescua858b0e2022-08-01 23:43:09 +0000577TEST_P(BinderRpc, OnewayStressTest) {
578 if (clientOrServerSingleThreaded()) {
579 GTEST_SKIP() << "This test requires multiple threads";
580 }
581
Steven Morelandc6046982021-04-20 00:49:42 +0000582 constexpr size_t kNumClientThreads = 10;
583 constexpr size_t kNumServerThreads = 10;
Steven Moreland3c3ab8d2021-09-23 10:29:50 -0700584 constexpr size_t kNumCalls = 1000;
Steven Morelandc6046982021-04-20 00:49:42 +0000585
Steven Moreland4313d7e2021-07-15 23:41:22 +0000586 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Morelandc6046982021-04-20 00:49:42 +0000587
588 std::vector<std::thread> threads;
589 for (size_t i = 0; i < kNumClientThreads; i++) {
590 threads.push_back(std::thread([&] {
591 for (size_t j = 0; j < kNumCalls; j++) {
592 EXPECT_OK(proc.rootIface->sendString("a"));
593 }
Steven Morelandc6046982021-04-20 00:49:42 +0000594 }));
595 }
596
597 for (auto& t : threads) t.join();
Steven Moreland925ba0a2021-09-17 18:06:32 -0700598
599 saturateThreadPool(kNumServerThreads, proc.rootIface);
Steven Morelandc6046982021-04-20 00:49:42 +0000600}
601
Frederick Mayleb0221d12022-10-03 23:10:53 +0000602TEST_P(BinderRpc, OnewayCallQueueingWithFds) {
603 if (!supportsFdTransport()) {
604 GTEST_SKIP() << "Would fail trivially (which is tested elsewhere)";
605 }
606 if (clientOrServerSingleThreaded()) {
607 GTEST_SKIP() << "This test requires multiple threads";
608 }
609
Andrei Homescu5f2bc562023-02-25 04:59:43 +0000610 constexpr size_t kNumServerThreads = 3;
611
Frederick Mayleb0221d12022-10-03 23:10:53 +0000612 // This test forces a oneway transaction to be queued by issuing two
613 // `blockingSendFdOneway` calls, then drains the queue by issuing two
614 // `blockingRecvFd` calls.
615 //
616 // For more details about the queuing semantics see
617 // https://developer.android.com/reference/android/os/IBinder#FLAG_ONEWAY
618
619 auto proc = createRpcTestSocketServerProcess({
Andrei Homescu5f2bc562023-02-25 04:59:43 +0000620 .numThreads = kNumServerThreads,
Frederick Mayleb0221d12022-10-03 23:10:53 +0000621 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
622 .serverSupportedFileDescriptorTransportModes =
623 {RpcSession::FileDescriptorTransportMode::UNIX},
624 });
625
626 EXPECT_OK(proc.rootIface->blockingSendFdOneway(
627 android::os::ParcelFileDescriptor(mockFileDescriptor("a"))));
628 EXPECT_OK(proc.rootIface->blockingSendFdOneway(
629 android::os::ParcelFileDescriptor(mockFileDescriptor("b"))));
630
631 android::os::ParcelFileDescriptor fdA;
632 EXPECT_OK(proc.rootIface->blockingRecvFd(&fdA));
633 std::string result;
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -0700634 ASSERT_TRUE(ReadFdToString(fdA.get(), &result));
Frederick Mayleb0221d12022-10-03 23:10:53 +0000635 EXPECT_EQ(result, "a");
636
637 android::os::ParcelFileDescriptor fdB;
638 EXPECT_OK(proc.rootIface->blockingRecvFd(&fdB));
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -0700639 ASSERT_TRUE(ReadFdToString(fdB.get(), &result));
Frederick Mayleb0221d12022-10-03 23:10:53 +0000640 EXPECT_EQ(result, "b");
Andrei Homescu5f2bc562023-02-25 04:59:43 +0000641
642 saturateThreadPool(kNumServerThreads, proc.rootIface);
Frederick Mayleb0221d12022-10-03 23:10:53 +0000643}
644
Andrei Homescua858b0e2022-08-01 23:43:09 +0000645TEST_P(BinderRpc, OnewayCallQueueing) {
646 if (clientOrServerSingleThreaded()) {
647 GTEST_SKIP() << "This test requires multiple threads";
648 }
649
Frederick Mayle96872592023-03-07 14:56:15 -0800650 constexpr size_t kNumQueued = 10;
Steven Moreland5553ac42020-11-11 02:14:45 +0000651 constexpr size_t kNumExtraServerThreads = 4;
Steven Moreland5553ac42020-11-11 02:14:45 +0000652
653 // make sure calls to the same object happen on the same thread
Steven Moreland4313d7e2021-07-15 23:41:22 +0000654 auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000655
Frederick Mayle96872592023-03-07 14:56:15 -0800656 // all these *Oneway commands should be queued on the server sequentially,
Steven Moreland1c678802021-09-17 16:48:47 -0700657 // even though there are multiple threads.
Frederick Mayle96872592023-03-07 14:56:15 -0800658 for (size_t i = 0; i + 1 < kNumQueued; i++) {
659 proc.rootIface->blockingSendIntOneway(i);
Steven Moreland5553ac42020-11-11 02:14:45 +0000660 }
Frederick Mayle96872592023-03-07 14:56:15 -0800661 for (size_t i = 0; i + 1 < kNumQueued; i++) {
662 int n;
663 proc.rootIface->blockingRecvInt(&n);
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -0700664 EXPECT_EQ(n, static_cast<ssize_t>(i));
Frederick Mayle96872592023-03-07 14:56:15 -0800665 }
Steven Morelandf5174272021-05-25 00:39:28 +0000666
Steven Moreland925ba0a2021-09-17 18:06:32 -0700667 saturateThreadPool(1 + kNumExtraServerThreads, proc.rootIface);
Steven Moreland5553ac42020-11-11 02:14:45 +0000668}
669
Andrei Homescua858b0e2022-08-01 23:43:09 +0000670TEST_P(BinderRpc, OnewayCallExhaustion) {
671 if (clientOrServerSingleThreaded()) {
672 GTEST_SKIP() << "This test requires multiple threads";
673 }
674
Steven Morelandd45be622021-06-04 02:19:37 +0000675 constexpr size_t kNumClients = 2;
676 constexpr size_t kTooLongMs = 1000;
677
Steven Moreland4313d7e2021-07-15 23:41:22 +0000678 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2});
Steven Morelandd45be622021-06-04 02:19:37 +0000679
680 // Build up oneway calls on the second session to make sure it terminates
681 // and shuts down. The first session should be unaffected (proc destructor
682 // checks the first session).
Andrei Homescu96834632022-10-14 00:49:49 +0000683 auto iface = interface_cast<IBinderRpcTest>(proc.proc->sessions.at(1).root);
Steven Morelandd45be622021-06-04 02:19:37 +0000684
685 std::vector<std::thread> threads;
686 for (size_t i = 0; i < kNumClients; i++) {
687 // one of these threads will get stuck queueing a transaction once the
688 // socket fills up, the other will be able to fill up transactions on
689 // this object
690 threads.push_back(std::thread([&] {
691 while (iface->sleepMsAsync(kTooLongMs).isOk()) {
692 }
693 }));
694 }
695 for (auto& t : threads) t.join();
696
697 Status status = iface->sleepMsAsync(kTooLongMs);
698 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
699
Steven Moreland798e0d12021-07-14 23:19:25 +0000700 // now that it has died, wait for the remote session to shutdown
701 std::vector<int32_t> remoteCounts;
702 do {
703 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
704 } while (remoteCounts.size() == kNumClients);
705
Steven Morelandd45be622021-06-04 02:19:37 +0000706 // the second session should be shutdown in the other process by the time we
707 // are able to join above (it'll only be hung up once it finishes processing
708 // any pending commands). We need to erase this session from the record
709 // here, so that the destructor for our session won't check that this
710 // session is valid, but we still want it to test the other session.
Andrei Homescu96834632022-10-14 00:49:49 +0000711 proc.proc->sessions.erase(proc.proc->sessions.begin() + 1);
Steven Morelandd45be622021-06-04 02:19:37 +0000712}
713
Steven Moreland67f85902023-03-15 01:13:49 +0000714TEST_P(BinderRpc, SessionWithIncomingThreadpoolDoesntLeak) {
715 if (clientOrServerSingleThreaded()) {
716 GTEST_SKIP() << "This test requires multiple threads";
717 }
718
719 // session 0 - will check for leaks in destrutor of proc
720 // session 1 - we want to make sure it gets deleted when we drop all references to it
721 auto proc = createRpcTestSocketServerProcess(
Tomasz Wasilczyk5da65602023-06-29 10:12:50 -0700722 {.numThreads = 1, .numSessions = 2, .numIncomingConnectionsBySession = {0, 1}});
Steven Moreland67f85902023-03-15 01:13:49 +0000723
724 wp<RpcSession> session = proc.proc->sessions.at(1).session;
725
726 // remove all references to the second session
727 proc.proc->sessions.at(1).root = nullptr;
728 proc.proc->sessions.erase(proc.proc->sessions.begin() + 1);
729
730 // TODO(b/271830568) more efficient way to wait for other incoming threadpool
731 // to drain commands.
732 for (size_t i = 0; i < 100; i++) {
733 usleep(10 * 1000);
734 if (session.promote() == nullptr) break;
735 }
736
737 EXPECT_EQ(nullptr, session.promote());
Steven Morelandb5d2b642023-05-04 00:31:45 +0000738
Steven Moreland0ebdaad2023-06-14 19:33:37 +0000739 // now that it has died, wait for the remote session to shutdown
740 std::vector<int32_t> remoteCounts;
741 do {
742 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
743 } while (remoteCounts.size() > 1);
Steven Moreland67f85902023-03-15 01:13:49 +0000744}
745
Devin Moore66d5b7a2022-07-07 21:42:10 +0000746TEST_P(BinderRpc, SingleDeathRecipient) {
Andrei Homescua858b0e2022-08-01 23:43:09 +0000747 if (clientOrServerSingleThreaded()) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000748 GTEST_SKIP() << "This test requires multiple threads";
749 }
750 class MyDeathRec : public IBinder::DeathRecipient {
751 public:
752 void binderDied(const wp<IBinder>& /* who */) override {
753 dead = true;
754 mCv.notify_one();
755 }
756 std::mutex mMtx;
757 std::condition_variable mCv;
758 bool dead = false;
759 };
760
761 // Death recipient needs to have an incoming connection to be called
762 auto proc = createRpcTestSocketServerProcess(
Steven Moreland67f85902023-03-15 01:13:49 +0000763 {.numThreads = 1, .numSessions = 1, .numIncomingConnectionsBySession = {1}});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000764
765 auto dr = sp<MyDeathRec>::make();
766 ASSERT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
767
768 if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) {
769 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
770 }
771
772 std::unique_lock<std::mutex> lock(dr->mMtx);
Steven Morelanddd231e22022-09-08 19:47:49 +0000773 ASSERT_TRUE(dr->mCv.wait_for(lock, 100ms, [&]() { return dr->dead; }));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000774
775 // need to wait for the session to shutdown so we don't "Leak session"
Steven Moreland67f85902023-03-15 01:13:49 +0000776 // can't do this before checking the death recipient by calling
777 // forceShutdown earlier, because shutdownAndWait will also trigger
778 // a death recipient, but if we had a way to wait for the service
779 // to gracefully shutdown, we could use that here.
Andrei Homescu96834632022-10-14 00:49:49 +0000780 EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000781 proc.expectAlreadyShutdown = true;
782}
783
784TEST_P(BinderRpc, SingleDeathRecipientOnShutdown) {
Andrei Homescua858b0e2022-08-01 23:43:09 +0000785 if (clientOrServerSingleThreaded()) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000786 GTEST_SKIP() << "This test requires multiple threads";
787 }
788 class MyDeathRec : public IBinder::DeathRecipient {
789 public:
790 void binderDied(const wp<IBinder>& /* who */) override {
791 dead = true;
792 mCv.notify_one();
793 }
794 std::mutex mMtx;
795 std::condition_variable mCv;
796 bool dead = false;
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 EXPECT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
805
806 // Explicitly calling shutDownAndWait will cause the death recipients
807 // to be called.
Andrei Homescu96834632022-10-14 00:49:49 +0000808 EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000809
810 std::unique_lock<std::mutex> lock(dr->mMtx);
811 if (!dr->dead) {
Steven Morelanddd231e22022-09-08 19:47:49 +0000812 EXPECT_EQ(std::cv_status::no_timeout, dr->mCv.wait_for(lock, 100ms));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000813 }
814 EXPECT_TRUE(dr->dead) << "Failed to receive the death notification.";
815
Andrei Homescu96834632022-10-14 00:49:49 +0000816 proc.proc->terminate();
817 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000818 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM)
819 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
820 });
821 proc.expectAlreadyShutdown = true;
822}
823
Steven Moreland5ec743f2023-01-18 01:02:06 +0000824TEST_P(BinderRpc, DeathRecipientFailsWithoutIncoming) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000825 if (socketType() == SocketType::TIPC) {
826 // This should work, but Trusty takes too long to restart the service
827 GTEST_SKIP() << "Service death test not supported on Trusty";
828 }
Devin Moore66d5b7a2022-07-07 21:42:10 +0000829 class MyDeathRec : public IBinder::DeathRecipient {
830 public:
831 void binderDied(const wp<IBinder>& /* who */) override {}
832 };
833
Steven Moreland67f85902023-03-15 01:13:49 +0000834 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 1});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000835
836 auto dr = sp<MyDeathRec>::make();
Steven Moreland5ec743f2023-01-18 01:02:06 +0000837 EXPECT_EQ(INVALID_OPERATION, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
Devin Moore66d5b7a2022-07-07 21:42:10 +0000838}
839
840TEST_P(BinderRpc, UnlinkDeathRecipient) {
Andrei Homescua858b0e2022-08-01 23:43:09 +0000841 if (clientOrServerSingleThreaded()) {
Devin Moore66d5b7a2022-07-07 21:42:10 +0000842 GTEST_SKIP() << "This test requires multiple threads";
843 }
844 class MyDeathRec : public IBinder::DeathRecipient {
845 public:
846 void binderDied(const wp<IBinder>& /* who */) override {
847 GTEST_FAIL() << "This should not be called after unlinkToDeath";
848 }
849 };
850
851 // Death recipient needs to have an incoming connection to be called
852 auto proc = createRpcTestSocketServerProcess(
Steven Moreland67f85902023-03-15 01:13:49 +0000853 {.numThreads = 1, .numSessions = 1, .numIncomingConnectionsBySession = {1}});
Devin Moore66d5b7a2022-07-07 21:42:10 +0000854
855 auto dr = sp<MyDeathRec>::make();
856 ASSERT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0));
857 ASSERT_EQ(OK, proc.rootBinder->unlinkToDeath(dr, (void*)1, 0, nullptr));
858
Steven Moreland67f85902023-03-15 01:13:49 +0000859 proc.forceShutdown();
Devin Moore66d5b7a2022-07-07 21:42:10 +0000860}
861
Steven Morelandc1635952021-04-01 16:20:47 +0000862TEST_P(BinderRpc, Die) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000863 if (socketType() == SocketType::TIPC) {
864 // This should work, but Trusty takes too long to restart the service
865 GTEST_SKIP() << "Service death test not supported on Trusty";
866 }
867
Steven Moreland5553ac42020-11-11 02:14:45 +0000868 for (bool doDeathCleanup : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000869 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000870
871 // make sure there is some state during crash
872 // 1. we hold their binder
873 sp<IBinderRpcSession> session;
874 EXPECT_OK(proc.rootIface->openSession("happy", &session));
875 // 2. they hold our binder
876 sp<IBinder> binder = new BBinder();
877 EXPECT_OK(proc.rootIface->holdBinder(binder));
878
879 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
880 << "Do death cleanup: " << doDeathCleanup;
881
Andrei Homescu96834632022-10-14 00:49:49 +0000882 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Maylea12b0962022-06-25 01:13:22 +0000883 EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 1)
884 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
885 });
Steven Morelandaf4ca712021-05-24 23:22:08 +0000886 proc.expectAlreadyShutdown = true;
Steven Moreland5553ac42020-11-11 02:14:45 +0000887 }
888}
889
Steven Morelandd7302072021-05-15 01:32:04 +0000890TEST_P(BinderRpc, UseKernelBinderCallingId) {
Andrei Homescu2a298012022-06-15 01:08:54 +0000891 // This test only works if the current process shared the internal state of
892 // ProcessState with the service across the call to fork(). Both the static
893 // libraries and libbinder.so have their own separate copies of all the
894 // globals, so the test only works when the test client and service both use
895 // libbinder.so (when using static libraries, even a client and service
896 // using the same kind of static library should have separate copies of the
897 // variables).
Andrei Homescua858b0e2022-08-01 23:43:09 +0000898 if (!kEnableSharedLibs || serverSingleThreaded() || noKernel()) {
Andrei Homescu12106de2022-04-27 04:42:21 +0000899 GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled "
900 "at build time.";
901 }
902
Steven Moreland4313d7e2021-07-15 23:41:22 +0000903 auto proc = createRpcTestSocketServerProcess({});
Steven Morelandd7302072021-05-15 01:32:04 +0000904
Andrei Homescu2a298012022-06-15 01:08:54 +0000905 // we can't allocate IPCThreadState so actually the first time should
906 // succeed :(
907 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
Steven Morelandd7302072021-05-15 01:32:04 +0000908
909 // second time! we catch the error :)
910 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
911
Andrei Homescu96834632022-10-14 00:49:49 +0000912 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Maylea12b0962022-06-25 01:13:22 +0000913 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)
914 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
915 });
Steven Morelandaf4ca712021-05-24 23:22:08 +0000916 proc.expectAlreadyShutdown = true;
Steven Morelandd7302072021-05-15 01:32:04 +0000917}
918
Frederick Mayle69a0c992022-05-26 20:38:39 +0000919TEST_P(BinderRpc, FileDescriptorTransportRejectNone) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000920 if (socketType() == SocketType::TIPC) {
921 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
922 }
923
Frederick Mayle69a0c992022-05-26 20:38:39 +0000924 auto proc = createRpcTestSocketServerProcess({
925 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE,
926 .serverSupportedFileDescriptorTransportModes =
927 {RpcSession::FileDescriptorTransportMode::UNIX},
928 .allowConnectFailure = true,
929 });
Andrei Homescu96834632022-10-14 00:49:49 +0000930 EXPECT_TRUE(proc.proc->sessions.empty()) << "session connections should have failed";
931 proc.proc->terminate();
932 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000933 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM)
934 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
935 });
936 proc.expectAlreadyShutdown = true;
937}
938
939TEST_P(BinderRpc, FileDescriptorTransportRejectUnix) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000940 if (socketType() == SocketType::TIPC) {
941 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
942 }
943
Frederick Mayle69a0c992022-05-26 20:38:39 +0000944 auto proc = createRpcTestSocketServerProcess({
945 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
946 .serverSupportedFileDescriptorTransportModes =
947 {RpcSession::FileDescriptorTransportMode::NONE},
948 .allowConnectFailure = true,
949 });
Andrei Homescu96834632022-10-14 00:49:49 +0000950 EXPECT_TRUE(proc.proc->sessions.empty()) << "session connections should have failed";
951 proc.proc->terminate();
952 proc.proc->setCustomExitStatusCheck([](int wstatus) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000953 EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM)
954 << "server process failed incorrectly: " << WaitStatusToString(wstatus);
955 });
956 proc.expectAlreadyShutdown = true;
957}
958
959TEST_P(BinderRpc, FileDescriptorTransportOptionalUnix) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000960 if (socketType() == SocketType::TIPC) {
961 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
962 }
963
Frederick Mayle69a0c992022-05-26 20:38:39 +0000964 auto proc = createRpcTestSocketServerProcess({
965 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE,
966 .serverSupportedFileDescriptorTransportModes =
967 {RpcSession::FileDescriptorTransportMode::NONE,
968 RpcSession::FileDescriptorTransportMode::UNIX},
969 });
970
971 android::os::ParcelFileDescriptor out;
972 auto status = proc.rootIface->echoAsFile("hello", &out);
973 EXPECT_EQ(status.transactionError(), FDS_NOT_ALLOWED) << status;
974}
975
976TEST_P(BinderRpc, ReceiveFile) {
Andrei Homescu68a55612022-08-02 01:25:15 +0000977 if (socketType() == SocketType::TIPC) {
978 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
979 }
980
Frederick Mayle69a0c992022-05-26 20:38:39 +0000981 auto proc = createRpcTestSocketServerProcess({
982 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
983 .serverSupportedFileDescriptorTransportModes =
984 {RpcSession::FileDescriptorTransportMode::UNIX},
985 });
986
987 android::os::ParcelFileDescriptor out;
988 auto status = proc.rootIface->echoAsFile("hello", &out);
989 if (!supportsFdTransport()) {
990 EXPECT_EQ(status.transactionError(), BAD_VALUE) << status;
991 return;
992 }
993 ASSERT_TRUE(status.isOk()) << status;
994
995 std::string result;
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -0700996 ASSERT_TRUE(ReadFdToString(out.get(), &result));
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700997 ASSERT_EQ(result, "hello");
Frederick Mayle69a0c992022-05-26 20:38:39 +0000998}
999
1000TEST_P(BinderRpc, SendFiles) {
Andrei Homescu68a55612022-08-02 01:25:15 +00001001 if (socketType() == SocketType::TIPC) {
1002 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
1003 }
1004
Frederick Mayle69a0c992022-05-26 20:38:39 +00001005 auto proc = createRpcTestSocketServerProcess({
1006 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
1007 .serverSupportedFileDescriptorTransportModes =
1008 {RpcSession::FileDescriptorTransportMode::UNIX},
1009 });
1010
1011 std::vector<android::os::ParcelFileDescriptor> files;
1012 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("123")));
1013 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a")));
1014 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("b")));
1015 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("cd")));
1016
1017 android::os::ParcelFileDescriptor out;
1018 auto status = proc.rootIface->concatFiles(files, &out);
1019 if (!supportsFdTransport()) {
1020 EXPECT_EQ(status.transactionError(), BAD_VALUE) << status;
1021 return;
1022 }
1023 ASSERT_TRUE(status.isOk()) << status;
1024
1025 std::string result;
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -07001026 EXPECT_TRUE(ReadFdToString(out.get(), &result));
Frederick Mayle69a0c992022-05-26 20:38:39 +00001027 EXPECT_EQ(result, "123abcd");
1028}
1029
1030TEST_P(BinderRpc, SendMaxFiles) {
1031 if (!supportsFdTransport()) {
1032 GTEST_SKIP() << "Would fail trivially (which is tested by BinderRpc::SendFiles)";
1033 }
1034
1035 auto proc = createRpcTestSocketServerProcess({
1036 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
1037 .serverSupportedFileDescriptorTransportModes =
1038 {RpcSession::FileDescriptorTransportMode::UNIX},
1039 });
1040
1041 std::vector<android::os::ParcelFileDescriptor> files;
1042 for (int i = 0; i < 253; i++) {
1043 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a")));
1044 }
1045
1046 android::os::ParcelFileDescriptor out;
1047 auto status = proc.rootIface->concatFiles(files, &out);
1048 ASSERT_TRUE(status.isOk()) << status;
1049
1050 std::string result;
Tomasz Wasilczyk26db5e42023-11-02 11:45:11 -07001051 EXPECT_TRUE(ReadFdToString(out.get(), &result));
Frederick Mayle69a0c992022-05-26 20:38:39 +00001052 EXPECT_EQ(result, std::string(253, 'a'));
1053}
1054
1055TEST_P(BinderRpc, SendTooManyFiles) {
1056 if (!supportsFdTransport()) {
1057 GTEST_SKIP() << "Would fail trivially (which is tested by BinderRpc::SendFiles)";
1058 }
1059
1060 auto proc = createRpcTestSocketServerProcess({
1061 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
1062 .serverSupportedFileDescriptorTransportModes =
1063 {RpcSession::FileDescriptorTransportMode::UNIX},
1064 });
1065
1066 std::vector<android::os::ParcelFileDescriptor> files;
1067 for (int i = 0; i < 254; i++) {
1068 files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a")));
1069 }
1070
1071 android::os::ParcelFileDescriptor out;
1072 auto status = proc.rootIface->concatFiles(files, &out);
1073 EXPECT_EQ(status.transactionError(), BAD_VALUE) << status;
1074}
1075
Andrei Homescufc221502022-10-08 03:51:17 +00001076TEST_P(BinderRpc, AppendInvalidFd) {
Andrei Homescu68a55612022-08-02 01:25:15 +00001077 if (socketType() == SocketType::TIPC) {
1078 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
1079 }
1080
Andrei Homescufc221502022-10-08 03:51:17 +00001081 auto proc = createRpcTestSocketServerProcess({
1082 .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
1083 .serverSupportedFileDescriptorTransportModes =
1084 {RpcSession::FileDescriptorTransportMode::UNIX},
1085 });
1086
1087 int badFd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0);
1088 ASSERT_NE(badFd, -1);
1089
1090 // Close the file descriptor so it becomes invalid for dup
1091 close(badFd);
1092
1093 Parcel p1;
1094 p1.markForBinder(proc.rootBinder);
1095 p1.writeInt32(3);
1096 EXPECT_EQ(OK, p1.writeFileDescriptor(badFd, false));
1097
1098 Parcel pRaw;
1099 pRaw.markForBinder(proc.rootBinder);
1100 EXPECT_EQ(OK, pRaw.appendFrom(&p1, 0, p1.dataSize()));
1101
1102 pRaw.setDataPosition(0);
1103 EXPECT_EQ(3, pRaw.readInt32());
1104 ASSERT_EQ(-1, pRaw.readFileDescriptor());
1105}
1106
Andrei Homescu68a55612022-08-02 01:25:15 +00001107#ifndef __ANDROID_VENDOR__ // No AIBinder_fromPlatformBinder on vendor
Steven Moreland37aff182021-03-26 02:04:16 +00001108TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
Andrei Homescu12106de2022-04-27 04:42:21 +00001109 if constexpr (!kEnableSharedLibs) {
1110 GTEST_SKIP() << "Test disabled because Binder was built as a static library";
1111 }
1112
Steven Moreland4313d7e2021-07-15 23:41:22 +00001113 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001114
1115 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1116 ASSERT_NE(binder, nullptr);
1117
1118 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1119}
1120
1121TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
Andrei Homescu12106de2022-04-27 04:42:21 +00001122 if constexpr (!kEnableSharedLibs) {
1123 GTEST_SKIP() << "Test disabled because Binder was built as a static library";
1124 }
1125
Steven Moreland4313d7e2021-07-15 23:41:22 +00001126 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001127
1128 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1129 ASSERT_NE(binder, nullptr);
1130
1131 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
1132 ASSERT_NE(ndkBinder, nullptr);
1133
1134 std::string out;
1135 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
1136 ASSERT_TRUE(status.isOk()) << status.getDescription();
1137 ASSERT_EQ("aoeuaoeu", out);
1138}
Andrei Homescu68a55612022-08-02 01:25:15 +00001139#endif // __ANDROID_VENDOR__
Steven Moreland37aff182021-03-26 02:04:16 +00001140
Steven Moreland5553ac42020-11-11 02:14:45 +00001141ssize_t countFds() {
1142 DIR* dir = opendir("/proc/self/fd/");
1143 if (dir == nullptr) return -1;
1144 ssize_t ret = 0;
1145 dirent* ent;
1146 while ((ent = readdir(dir)) != nullptr) ret++;
1147 closedir(dir);
1148 return ret;
1149}
1150
Andrei Homescua858b0e2022-08-01 23:43:09 +00001151TEST_P(BinderRpc, Fds) {
1152 if (serverSingleThreaded()) {
1153 GTEST_SKIP() << "This test requires multiple threads";
1154 }
Andrei Homescu68a55612022-08-02 01:25:15 +00001155 if (socketType() == SocketType::TIPC) {
1156 GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
1157 }
Andrei Homescua858b0e2022-08-01 23:43:09 +00001158
Steven Moreland5553ac42020-11-11 02:14:45 +00001159 ssize_t beforeFds = countFds();
1160 ASSERT_GE(beforeFds, 0);
1161 {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001162 auto proc = createRpcTestSocketServerProcess({.numThreads = 10});
Steven Moreland5553ac42020-11-11 02:14:45 +00001163 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
1164 }
1165 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
1166}
1167
Devin Moore18f63752024-08-08 21:01:24 +00001168// TODO need to add IServiceManager.cpp/.h to libbinder_no_kernel
1169#ifdef BINDER_WITH_KERNEL_IPC
1170
1171class BinderRpcAccessor : public BinderRpc {
1172 void SetUp() override {
1173 if (serverSingleThreaded()) {
1174 // This blocks on android::FdTrigger::triggerablePoll when attempting to set
1175 // up the client RpcSession
1176 GTEST_SKIP() << "Accessors are not supported for single threaded libbinder";
1177 }
1178 if (rpcSecurity() == RpcSecurity::TLS) {
1179 GTEST_SKIP() << "Accessors are not supported with TLS";
1180 // ... for now
1181 }
1182
1183 if (socketType() == SocketType::UNIX_BOOTSTRAP) {
1184 GTEST_SKIP() << "Accessors do not support UNIX_BOOTSTRAP because no connection "
1185 "information is known";
1186 }
1187 if (socketType() == SocketType::TIPC) {
1188 GTEST_SKIP() << "Accessors do not support TIPC because the socket transport is not "
1189 "known in libbinder";
1190 }
1191 BinderRpc::SetUp();
1192 }
1193};
1194
1195inline void waitForExtraSessionCleanup(const BinderRpcTestProcessSession& proc) {
1196 // Need to give the server some time to delete its RpcSession after our last
1197 // reference is dropped, closing the connection. Check for up to 1 second,
1198 // every 10 ms.
1199 for (size_t i = 0; i < 100; i++) {
1200 std::vector<int32_t> remoteCounts;
1201 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
1202 // We exect the original binder to still be alive, we just want to wait
1203 // for this extra session to be cleaned up.
1204 if (remoteCounts.size() == proc.proc->sessions.size()) break;
1205 usleep(10000);
1206 }
1207}
1208
1209TEST_P(BinderRpcAccessor, InjectAndGetServiceHappyPath) {
1210 constexpr size_t kNumThreads = 10;
1211 const String16 kInstanceName("super.cool.service/better_than_default");
1212
1213 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
1214 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
1215
Devin Moorec370db42024-08-09 23:18:05 +00001216 auto receipt = addAccessorProvider(
1217 {String8(kInstanceName).c_str()}, [&](const String16& name) -> sp<IBinder> {
1218 return createAccessor(name,
1219 [&](const String16& name, sockaddr* outAddr,
1220 socklen_t addrSize) -> status_t {
1221 if (outAddr == nullptr ||
1222 addrSize < proc.proc->sessions[0].addrLen) {
1223 return BAD_VALUE;
1224 }
1225 if (name == kInstanceName) {
1226 if (proc.proc->sessions[0].addr.ss_family ==
1227 AF_UNIX) {
1228 sockaddr_un* un = reinterpret_cast<sockaddr_un*>(
1229 &proc.proc->sessions[0].addr);
1230 ALOGE("inside callback: %s", un->sun_path);
1231 }
1232 std::memcpy(outAddr, &proc.proc->sessions[0].addr,
1233 proc.proc->sessions[0].addrLen);
1234 return OK;
1235 }
1236 return NAME_NOT_FOUND;
1237 });
1238 });
Devin Moore18f63752024-08-08 21:01:24 +00001239
1240 EXPECT_FALSE(receipt.expired());
1241
1242 sp<IBinder> binder = defaultServiceManager()->checkService(kInstanceName);
1243 sp<IBinderRpcTest> service = checked_interface_cast<IBinderRpcTest>(binder);
1244 EXPECT_NE(service, nullptr);
1245
1246 sp<IBinder> out;
1247 EXPECT_OK(service->repeatBinder(binder, &out));
1248 EXPECT_EQ(binder, out);
1249
1250 out.clear();
1251 binder.clear();
1252 service.clear();
1253
1254 status_t status = removeAccessorProvider(receipt);
1255 EXPECT_EQ(status, OK);
1256
1257 waitForExtraSessionCleanup(proc);
1258}
1259
1260TEST_P(BinderRpcAccessor, InjectNoAccessorProvided) {
1261 const String16 kInstanceName("doesnt_matter_nothing_checks");
1262
1263 bool isProviderDeleted = false;
1264
Devin Moorec370db42024-08-09 23:18:05 +00001265 auto receipt = addAccessorProvider({String8(kInstanceName).c_str()},
1266 [&](const String16&) -> sp<IBinder> { return nullptr; });
Devin Moore18f63752024-08-08 21:01:24 +00001267 EXPECT_FALSE(receipt.expired());
1268
1269 sp<IBinder> binder = defaultServiceManager()->checkService(kInstanceName);
1270 EXPECT_EQ(binder, nullptr);
1271
1272 status_t status = removeAccessorProvider(receipt);
1273 EXPECT_EQ(status, OK);
1274}
1275
Devin Moorec370db42024-08-09 23:18:05 +00001276TEST_P(BinderRpcAccessor, InjectDuplicateAccessorProvider) {
1277 const String16 kInstanceName("super.cool.service/better_than_default");
1278 const String16 kInstanceName2("super.cool.service/better_than_default2");
1279
1280 auto receipt =
1281 addAccessorProvider({String8(kInstanceName).c_str(), String8(kInstanceName2).c_str()},
1282 [&](const String16&) -> sp<IBinder> { return nullptr; });
1283 EXPECT_FALSE(receipt.expired());
1284 // reject this because it's associated with an already used instance name
1285 auto receipt2 = addAccessorProvider({String8(kInstanceName).c_str()},
1286 [&](const String16&) -> sp<IBinder> { return nullptr; });
1287 EXPECT_TRUE(receipt2.expired());
1288
1289 // the first provider should still be usable
1290 sp<IBinder> binder = defaultServiceManager()->checkService(kInstanceName);
1291 EXPECT_EQ(binder, nullptr);
1292
1293 status_t status = removeAccessorProvider(receipt);
1294 EXPECT_EQ(status, OK);
1295}
1296
1297TEST_P(BinderRpcAccessor, InjectAccessorProviderNoInstance) {
1298 auto receipt = addAccessorProvider({}, [&](const String16&) -> sp<IBinder> { return nullptr; });
1299 EXPECT_TRUE(receipt.expired());
1300}
1301
Devin Moore18f63752024-08-08 21:01:24 +00001302TEST_P(BinderRpcAccessor, InjectNoSockaddrProvided) {
1303 constexpr size_t kNumThreads = 10;
1304 const String16 kInstanceName("super.cool.service/better_than_default");
1305
1306 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
1307 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
1308
1309 bool isProviderDeleted = false;
1310 bool isAccessorDeleted = false;
1311
Devin Moorec370db42024-08-09 23:18:05 +00001312 auto receipt = addAccessorProvider({String8(kInstanceName).c_str()},
1313 [&](const String16& name) -> sp<IBinder> {
1314 return createAccessor(name,
1315 [&](const String16&, sockaddr*,
1316 socklen_t) -> status_t {
1317 // don't fill in outAddr
1318 return NAME_NOT_FOUND;
1319 });
1320 });
Devin Moore18f63752024-08-08 21:01:24 +00001321
1322 EXPECT_FALSE(receipt.expired());
1323
1324 sp<IBinder> binder = defaultServiceManager()->checkService(kInstanceName);
1325 EXPECT_EQ(binder, nullptr);
1326
1327 status_t status = removeAccessorProvider(receipt);
1328 EXPECT_EQ(status, OK);
1329}
1330
Devin Moorec370db42024-08-09 23:18:05 +00001331constexpr const char* kARpcInstance = "some.instance.name.IFoo/default";
1332const char* kARpcSupportedServices[] = {
1333 kARpcInstance,
1334};
1335const uint32_t kARpcNumSupportedServices = 1;
1336
1337struct ConnectionInfoData {
1338 sockaddr_storage addr;
1339 socklen_t len;
1340 bool* isDeleted;
1341 ~ConnectionInfoData() {
1342 if (isDeleted) *isDeleted = true;
1343 }
1344};
1345
1346struct AccessorProviderData {
1347 sockaddr_storage addr;
1348 socklen_t len;
1349 bool* isDeleted;
1350 ~AccessorProviderData() {
1351 if (isDeleted) *isDeleted = true;
1352 }
1353};
1354
1355void accessorProviderDataOnDelete(void* data) {
1356 delete reinterpret_cast<AccessorProviderData*>(data);
1357}
1358void infoProviderDataOnDelete(void* data) {
1359 delete reinterpret_cast<ConnectionInfoData*>(data);
1360}
1361
1362ABinderRpc_ConnectionInfo* infoProvider(const char* instance, void* cookie) {
1363 if (instance == nullptr || cookie == nullptr) return nullptr;
1364 ConnectionInfoData* data = reinterpret_cast<ConnectionInfoData*>(cookie);
1365 return ABinderRpc_ConnectionInfo_new(reinterpret_cast<const sockaddr*>(&data->addr), data->len);
1366}
1367
1368ABinderRpc_Accessor* getAccessor(const char* instance, void* cookie) {
1369 if (instance == nullptr || cookie == nullptr) return nullptr;
1370 if (0 != strcmp(instance, kARpcInstance)) return nullptr;
1371
1372 AccessorProviderData* data = reinterpret_cast<AccessorProviderData*>(cookie);
1373
1374 ConnectionInfoData* info = new ConnectionInfoData{
1375 .addr = data->addr,
1376 .len = data->len,
1377 .isDeleted = nullptr,
1378 };
1379
1380 return ABinderRpc_Accessor_new(instance, infoProvider, info, infoProviderDataOnDelete);
1381}
1382
1383class BinderARpcNdk : public ::testing::Test {};
1384
1385TEST_F(BinderARpcNdk, ARpcProviderNewDelete) {
1386 bool isDeleted = false;
1387
1388 AccessorProviderData* data = new AccessorProviderData{{}, 0, &isDeleted};
1389
1390 ABinderRpc_AccessorProvider* provider =
1391 ABinderRpc_registerAccessorProvider(getAccessor, kARpcSupportedServices,
1392 kARpcNumSupportedServices, data,
1393 accessorProviderDataOnDelete);
1394
1395 ASSERT_NE(provider, nullptr);
1396 EXPECT_FALSE(isDeleted);
1397
1398 ABinderRpc_unregisterAccessorProvider(provider);
1399
1400 EXPECT_TRUE(isDeleted);
1401}
1402
Devin Moore5fd22472024-11-19 01:31:47 +00001403TEST_F(BinderARpcNdk, ARpcProviderDeleteOnError) {
1404 bool isDeleted = false;
1405 AccessorProviderData* data = new AccessorProviderData{{}, 0, &isDeleted};
1406
1407 ABinderRpc_AccessorProvider* provider =
1408 ABinderRpc_registerAccessorProvider(getAccessor, kARpcSupportedServices, 0, data,
1409 accessorProviderDataOnDelete);
1410
1411 ASSERT_EQ(provider, nullptr);
1412 EXPECT_TRUE(isDeleted);
1413}
1414
1415TEST_F(BinderARpcNdk, ARpcProvideOnErrorNoDeleteCbNoCrash) {
1416 ABinderRpc_AccessorProvider* provider =
1417 ABinderRpc_registerAccessorProvider(getAccessor, kARpcSupportedServices, 0, nullptr,
1418 nullptr);
1419
1420 ASSERT_EQ(provider, nullptr);
1421}
1422
Devin Moorec370db42024-08-09 23:18:05 +00001423TEST_F(BinderARpcNdk, ARpcProviderDuplicateInstance) {
1424 const char* instance = "some.instance.name.IFoo/default";
1425 const uint32_t numInstances = 2;
1426 const char* instances[numInstances] = {
1427 instance,
1428 "some.other.instance/default",
1429 };
1430
1431 bool isDeleted = false;
1432
1433 AccessorProviderData* data = new AccessorProviderData{{}, 0, &isDeleted};
1434
1435 ABinderRpc_AccessorProvider* provider =
1436 ABinderRpc_registerAccessorProvider(getAccessor, instances, numInstances, data,
1437 accessorProviderDataOnDelete);
1438
1439 ASSERT_NE(provider, nullptr);
1440 EXPECT_FALSE(isDeleted);
1441
1442 const uint32_t numInstances2 = 1;
1443 const char* instances2[numInstances2] = {
1444 instance,
1445 };
1446 bool isDeleted2 = false;
1447 AccessorProviderData* data2 = new AccessorProviderData{{}, 0, &isDeleted2};
1448 ABinderRpc_AccessorProvider* provider2 =
1449 ABinderRpc_registerAccessorProvider(getAccessor, instances2, numInstances2, data2,
1450 accessorProviderDataOnDelete);
1451
1452 EXPECT_EQ(provider2, nullptr);
1453 // If it fails to be registered, the data is still cleaned up with
1454 // accessorProviderDataOnDelete
1455 EXPECT_TRUE(isDeleted2);
1456
1457 ABinderRpc_unregisterAccessorProvider(provider);
1458
1459 EXPECT_TRUE(isDeleted);
1460}
1461
1462TEST_F(BinderARpcNdk, ARpcProviderRegisterNoInstance) {
1463 const uint32_t numInstances = 0;
1464 const char* instances[numInstances] = {};
1465
1466 bool isDeleted = false;
1467 AccessorProviderData* data = new AccessorProviderData{{}, 0, &isDeleted};
1468
1469 ABinderRpc_AccessorProvider* provider =
1470 ABinderRpc_registerAccessorProvider(getAccessor, instances, numInstances, data,
1471 accessorProviderDataOnDelete);
1472 ASSERT_EQ(provider, nullptr);
1473}
1474
1475TEST_F(BinderARpcNdk, ARpcAccessorNewDelete) {
1476 bool isDeleted = false;
1477
1478 ConnectionInfoData* data = new ConnectionInfoData{{}, 0, &isDeleted};
1479
1480 ABinderRpc_Accessor* accessor =
1481 ABinderRpc_Accessor_new("gshoe_service", infoProvider, data, infoProviderDataOnDelete);
1482 ASSERT_NE(accessor, nullptr);
1483 EXPECT_FALSE(isDeleted);
1484
1485 ABinderRpc_Accessor_delete(accessor);
1486 EXPECT_TRUE(isDeleted);
1487}
1488
1489TEST_F(BinderARpcNdk, ARpcConnectionInfoNewDelete) {
1490 sockaddr_vm addr{
1491 .svm_family = AF_VSOCK,
1492 .svm_port = VMADDR_PORT_ANY,
1493 .svm_cid = VMADDR_CID_ANY,
1494 };
1495
1496 ABinderRpc_ConnectionInfo* info =
1497 ABinderRpc_ConnectionInfo_new(reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr_vm));
1498 EXPECT_NE(info, nullptr);
1499
1500 ABinderRpc_ConnectionInfo_delete(info);
1501}
1502
1503TEST_F(BinderARpcNdk, ARpcAsFromBinderAsBinder) {
1504 bool isDeleted = false;
1505
1506 ConnectionInfoData* data = new ConnectionInfoData{{}, 0, &isDeleted};
1507
1508 ABinderRpc_Accessor* accessor =
1509 ABinderRpc_Accessor_new("gshoe_service", infoProvider, data, infoProviderDataOnDelete);
1510 ASSERT_NE(accessor, nullptr);
1511 EXPECT_FALSE(isDeleted);
1512
1513 {
1514 ndk::SpAIBinder binder = ndk::SpAIBinder(ABinderRpc_Accessor_asBinder(accessor));
1515 EXPECT_NE(binder.get(), nullptr);
1516
1517 ABinderRpc_Accessor* accessor2 =
1518 ABinderRpc_Accessor_fromBinder("wrong_service_name", binder.get());
1519 // The API checks for the expected service name that is associated with
1520 // the accessor!
1521 EXPECT_EQ(accessor2, nullptr);
1522
1523 accessor2 = ABinderRpc_Accessor_fromBinder("gshoe_service", binder.get());
1524 EXPECT_NE(accessor2, nullptr);
1525
1526 // this is a new ABinderRpc_Accessor object that wraps the underlying
1527 // libbinder object.
1528 EXPECT_NE(accessor, accessor2);
1529
1530 ndk::SpAIBinder binder2 = ndk::SpAIBinder(ABinderRpc_Accessor_asBinder(accessor2));
1531 EXPECT_EQ(binder.get(), binder2.get());
1532
1533 ABinderRpc_Accessor_delete(accessor2);
1534 }
1535
1536 EXPECT_FALSE(isDeleted);
1537 ABinderRpc_Accessor_delete(accessor);
1538 EXPECT_TRUE(isDeleted);
1539}
1540
1541TEST_F(BinderARpcNdk, ARpcRequireProviderOnDeleteCallback) {
1542 EXPECT_EQ(nullptr,
1543 ABinderRpc_registerAccessorProvider(getAccessor, kARpcSupportedServices,
1544 kARpcNumSupportedServices,
1545 reinterpret_cast<void*>(1), nullptr));
1546}
1547
1548TEST_F(BinderARpcNdk, ARpcRequireInfoOnDeleteCallback) {
1549 EXPECT_EQ(nullptr,
1550 ABinderRpc_Accessor_new("the_best_service_name", infoProvider,
1551 reinterpret_cast<void*>(1), nullptr));
1552}
1553
1554TEST_F(BinderARpcNdk, ARpcNoDataNoProviderOnDeleteCallback) {
1555 ABinderRpc_AccessorProvider* provider =
1556 ABinderRpc_registerAccessorProvider(getAccessor, kARpcSupportedServices,
1557 kARpcNumSupportedServices, nullptr, nullptr);
1558 ASSERT_NE(nullptr, provider);
1559 ABinderRpc_unregisterAccessorProvider(provider);
1560}
1561
1562TEST_F(BinderARpcNdk, ARpcNoDataNoInfoOnDeleteCallback) {
1563 ABinderRpc_Accessor* accessor =
1564 ABinderRpc_Accessor_new("the_best_service_name", infoProvider, nullptr, nullptr);
1565 ASSERT_NE(nullptr, accessor);
1566 ABinderRpc_Accessor_delete(accessor);
1567}
1568
Devin Moorec370db42024-08-09 23:18:05 +00001569TEST_F(BinderARpcNdk, ARpcNullArgs_ConnectionInfo_new) {
1570 sockaddr_storage addr;
1571 EXPECT_EQ(nullptr, ABinderRpc_ConnectionInfo_new(reinterpret_cast<const sockaddr*>(&addr), 0));
1572}
1573
Devin Moore0555fbf2024-08-29 15:51:50 +00001574TEST_F(BinderARpcNdk, ARpcDelegateAccessorWrongInstance) {
1575 AccessorProviderData* data = new AccessorProviderData();
1576 ABinderRpc_Accessor* accessor = getAccessor(kARpcInstance, data);
1577 ASSERT_NE(accessor, nullptr);
1578 AIBinder* localAccessorBinder = ABinderRpc_Accessor_asBinder(accessor);
1579 EXPECT_NE(localAccessorBinder, nullptr);
1580
1581 AIBinder* delegatorBinder = nullptr;
1582 binder_status_t status =
1583 ABinderRpc_Accessor_delegateAccessor("bar", localAccessorBinder, &delegatorBinder);
1584 EXPECT_EQ(status, NAME_NOT_FOUND);
1585
1586 AIBinder_decStrong(localAccessorBinder);
1587 ABinderRpc_Accessor_delete(accessor);
1588 delete data;
1589}
1590
1591TEST_F(BinderARpcNdk, ARpcDelegateNonAccessor) {
1592 auto service = defaultServiceManager()->checkService(String16(kKnownAidlService));
1593 ASSERT_NE(nullptr, service);
1594 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(service));
1595
1596 AIBinder* delegatorBinder = nullptr;
1597 binder_status_t status =
1598 ABinderRpc_Accessor_delegateAccessor("bar", binder.get(), &delegatorBinder);
1599
1600 EXPECT_EQ(status, BAD_TYPE);
1601}
1602
1603inline void getServiceTest(BinderRpcTestProcessSession& proc,
1604 ABinderRpc_AccessorProvider_getAccessorCallback getAccessor) {
Devin Moorec370db42024-08-09 23:18:05 +00001605 constexpr size_t kNumThreads = 10;
1606 bool isDeleted = false;
1607
Devin Moorec370db42024-08-09 23:18:05 +00001608 AccessorProviderData* data =
1609 new AccessorProviderData{proc.proc->sessions[0].addr, proc.proc->sessions[0].addrLen,
1610 &isDeleted};
Devin Moorec370db42024-08-09 23:18:05 +00001611 ABinderRpc_AccessorProvider* provider =
1612 ABinderRpc_registerAccessorProvider(getAccessor, kARpcSupportedServices,
1613 kARpcNumSupportedServices, data,
1614 accessorProviderDataOnDelete);
Devin Moorec370db42024-08-09 23:18:05 +00001615 EXPECT_NE(provider, nullptr);
1616 EXPECT_FALSE(isDeleted);
1617
1618 {
1619 ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_checkService(kARpcInstance));
1620 ASSERT_NE(binder.get(), nullptr);
1621 EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1622 }
1623
1624 ABinderRpc_unregisterAccessorProvider(provider);
1625 EXPECT_TRUE(isDeleted);
1626
1627 waitForExtraSessionCleanup(proc);
1628}
1629
Devin Moore0555fbf2024-08-29 15:51:50 +00001630TEST_P(BinderRpcAccessor, ARpcGetService) {
1631 constexpr size_t kNumThreads = 10;
1632 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
1633 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
1634
1635 getServiceTest(proc, getAccessor);
1636}
1637
1638// Create accessors and wrap each of the accessors in a delegator
1639ABinderRpc_Accessor* getDelegatedAccessor(const char* instance, void* cookie) {
1640 ABinderRpc_Accessor* accessor = getAccessor(instance, cookie);
1641 AIBinder* accessorBinder = ABinderRpc_Accessor_asBinder(accessor);
1642 // Once we have a handle to the AIBinder which holds a reference to the
1643 // underlying accessor IBinder, we can get rid of the ABinderRpc_Accessor
1644 ABinderRpc_Accessor_delete(accessor);
1645
1646 AIBinder* delegatorBinder = nullptr;
1647 binder_status_t status =
1648 ABinderRpc_Accessor_delegateAccessor(instance, accessorBinder, &delegatorBinder);
1649 // No longer need this AIBinder. The delegator has a reference to the
1650 // underlying IBinder on success, and on failure we are done here.
1651 AIBinder_decStrong(accessorBinder);
1652 if (status != OK || delegatorBinder == nullptr) {
1653 ALOGE("Unexpected behavior. Status: %s, delegator ptr: %p", statusToString(status).c_str(),
1654 delegatorBinder);
1655 return nullptr;
1656 }
1657
1658 return ABinderRpc_Accessor_fromBinder(instance, delegatorBinder);
1659}
1660
1661TEST_P(BinderRpcAccessor, ARpcGetServiceWithDelegator) {
1662 constexpr size_t kNumThreads = 10;
1663 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
1664 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
1665
1666 getServiceTest(proc, getDelegatedAccessor);
1667}
1668
Devin Moore18f63752024-08-08 21:01:24 +00001669#endif // BINDER_WITH_KERNEL_IPC
1670
Andrei Homescud65666d2023-03-03 07:28:02 +00001671#ifdef BINDER_RPC_TO_TRUSTY_TEST
Steven Morelandb469f432023-07-28 22:13:47 +00001672
1673static std::vector<BinderRpc::ParamType> getTrustyBinderRpcParams() {
1674 std::vector<BinderRpc::ParamType> ret;
1675
1676 for (const auto& clientVersion : testVersions()) {
1677 for (const auto& serverVersion : testVersions()) {
1678 ret.push_back(BinderRpc::ParamType{
1679 .type = SocketType::TIPC,
1680 .security = RpcSecurity::RAW,
1681 .clientVersion = clientVersion,
1682 .serverVersion = serverVersion,
1683 .singleThreaded = true,
1684 .noKernel = true,
1685 });
1686 }
1687 }
1688
1689 return ret;
1690}
1691
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07001692INSTANTIATE_TEST_SUITE_P(Trusty, BinderRpc, ::testing::ValuesIn(getTrustyBinderRpcParams()),
1693 BinderRpc::PrintParamInfo);
Andrei Homescud65666d2023-03-03 07:28:02 +00001694#else // BINDER_RPC_TO_TRUSTY_TEST
Steven Moreland9f250b02023-05-16 23:27:42 +00001695bool testSupportVsockLoopback() {
Yifan Hong702115c2021-06-24 15:39:18 -07001696 // We don't need to enable TLS to know if vsock is supported.
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001697 unique_fd serverFd(
Andrei Homescu992a4052022-06-28 21:26:18 +00001698 TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)));
Steven Morelanda27311b2023-04-11 22:13:35 +00001699
1700 if (errno == EAFNOSUPPORT) {
1701 return false;
1702 }
1703
Tomasz Wasilczykbfb13a82023-11-14 11:33:10 -08001704 LOG_ALWAYS_FATAL_IF(!serverFd.ok(), "Could not create socket: %s", strerror(errno));
Andrei Homescu992a4052022-06-28 21:26:18 +00001705
1706 sockaddr_vm serverAddr{
1707 .svm_family = AF_VSOCK,
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001708 .svm_port = VMADDR_PORT_ANY,
Andrei Homescu992a4052022-06-28 21:26:18 +00001709 .svm_cid = VMADDR_CID_ANY,
1710 };
1711 int ret = TEMP_FAILURE_RETRY(
1712 bind(serverFd.get(), reinterpret_cast<sockaddr*>(&serverAddr), sizeof(serverAddr)));
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001713 LOG_ALWAYS_FATAL_IF(0 != ret, "Could not bind socket to port VMADDR_PORT_ANY: %s",
Andrei Homescu992a4052022-06-28 21:26:18 +00001714 strerror(errno));
1715
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001716 socklen_t len = sizeof(serverAddr);
1717 ret = getsockname(serverFd.get(), reinterpret_cast<sockaddr*>(&serverAddr), &len);
1718 LOG_ALWAYS_FATAL_IF(0 != ret, "Failed to getsockname: %s", strerror(errno));
Hannah.Hsu6b1036f2024-07-09 11:20:20 +08001719 LOG_ALWAYS_FATAL_IF(len < static_cast<socklen_t>(sizeof(serverAddr)),
1720 "getsockname didn't read the full addr struct");
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001721
Andrei Homescu992a4052022-06-28 21:26:18 +00001722 ret = TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/));
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001723 LOG_ALWAYS_FATAL_IF(0 != ret, "Could not listen socket on port %u: %s", serverAddr.svm_port,
Andrei Homescu992a4052022-06-28 21:26:18 +00001724 strerror(errno));
1725
1726 // Try to connect to the server using the VMADDR_CID_LOCAL cid
1727 // to see if the kernel supports it. It's safe to use a blocking
1728 // connect because vsock sockets have a 2 second connection timeout,
1729 // and they return ETIMEDOUT after that.
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001730 unique_fd connectFd(
Andrei Homescu992a4052022-06-28 21:26:18 +00001731 TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)));
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001732 LOG_ALWAYS_FATAL_IF(!connectFd.ok(), "Could not create socket for port %u: %s",
1733 serverAddr.svm_port, strerror(errno));
Andrei Homescu992a4052022-06-28 21:26:18 +00001734
1735 bool success = false;
1736 sockaddr_vm connectAddr{
1737 .svm_family = AF_VSOCK,
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07001738 .svm_port = serverAddr.svm_port,
Andrei Homescu992a4052022-06-28 21:26:18 +00001739 .svm_cid = VMADDR_CID_LOCAL,
1740 };
1741 ret = TEMP_FAILURE_RETRY(connect(connectFd.get(), reinterpret_cast<sockaddr*>(&connectAddr),
1742 sizeof(connectAddr)));
1743 if (ret != 0 && (errno == EAGAIN || errno == EINPROGRESS)) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001744 unique_fd acceptFd;
Andrei Homescu992a4052022-06-28 21:26:18 +00001745 while (true) {
1746 pollfd pfd[]{
1747 {.fd = serverFd.get(), .events = POLLIN, .revents = 0},
1748 {.fd = connectFd.get(), .events = POLLOUT, .revents = 0},
1749 };
Tomasz Wasilczyk657c2bc2023-11-07 06:57:42 -08001750 ret = TEMP_FAILURE_RETRY(poll(pfd, countof(pfd), -1));
Andrei Homescu992a4052022-06-28 21:26:18 +00001751 LOG_ALWAYS_FATAL_IF(ret < 0, "Error polling: %s", strerror(errno));
1752
1753 if (pfd[0].revents & POLLIN) {
1754 sockaddr_vm acceptAddr;
1755 socklen_t acceptAddrLen = sizeof(acceptAddr);
1756 ret = TEMP_FAILURE_RETRY(accept4(serverFd.get(),
1757 reinterpret_cast<sockaddr*>(&acceptAddr),
1758 &acceptAddrLen, SOCK_CLOEXEC));
1759 LOG_ALWAYS_FATAL_IF(ret < 0, "Could not accept4 socket: %s", strerror(errno));
1760 LOG_ALWAYS_FATAL_IF(acceptAddrLen != static_cast<socklen_t>(sizeof(acceptAddr)),
1761 "Truncated address");
1762
1763 // Store the fd in acceptFd so we keep the connection alive
1764 // while polling connectFd
1765 acceptFd.reset(ret);
1766 }
1767
1768 if (pfd[1].revents & POLLOUT) {
1769 // Connect either succeeded or timed out
1770 int connectErrno;
1771 socklen_t connectErrnoLen = sizeof(connectErrno);
1772 int ret = getsockopt(connectFd.get(), SOL_SOCKET, SO_ERROR, &connectErrno,
1773 &connectErrnoLen);
1774 LOG_ALWAYS_FATAL_IF(ret == -1,
1775 "Could not getsockopt() after connect() "
1776 "on non-blocking socket: %s.",
1777 strerror(errno));
1778
1779 // We're done, this is all we wanted
1780 success = connectErrno == 0;
1781 break;
1782 }
1783 }
1784 } else {
1785 success = ret == 0;
1786 }
1787
1788 ALOGE("Detected vsock loopback supported: %s", success ? "yes" : "no");
1789
1790 return success;
Steven Morelandda573042021-06-12 01:13:45 +00001791}
1792
Yifan Hong1deca4b2021-09-10 16:16:44 -07001793static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) {
Alice Wang893a9912022-10-24 10:44:09 +00001794 std::vector<SocketType> ret = {SocketType::UNIX, SocketType::UNIX_BOOTSTRAP, SocketType::INET,
1795 SocketType::UNIX_RAW};
Yifan Hong1deca4b2021-09-10 16:16:44 -07001796
1797 if (hasPreconnected) ret.push_back(SocketType::PRECONNECTED);
Steven Morelandda573042021-06-12 01:13:45 +00001798
Steven Moreland9f250b02023-05-16 23:27:42 +00001799#ifdef __BIONIC__
1800 // Devices may not have vsock support. AVF tests will verify whether they do, but
1801 // we can't require it due to old kernels for the time being.
Steven Morelandda573042021-06-12 01:13:45 +00001802 static bool hasVsockLoopback = testSupportVsockLoopback();
Steven Moreland9f250b02023-05-16 23:27:42 +00001803#else
1804 // On host machines, we always assume we have vsock loopback. If we don't, the
1805 // subsequent failures will be more clear than showing one now.
1806 static bool hasVsockLoopback = true;
1807#endif
Steven Morelandda573042021-06-12 01:13:45 +00001808
1809 if (hasVsockLoopback) {
1810 ret.push_back(SocketType::VSOCK);
1811 }
1812
1813 return ret;
1814}
1815
Steven Morelandb469f432023-07-28 22:13:47 +00001816static std::vector<BinderRpc::ParamType> getBinderRpcParams() {
1817 std::vector<BinderRpc::ParamType> ret;
1818
Steven Morelandf7421432023-07-28 22:41:44 +00001819 constexpr bool full = false;
1820
Steven Morelandb469f432023-07-28 22:13:47 +00001821 for (const auto& type : testSocketTypes()) {
Steven Morelandf7421432023-07-28 22:41:44 +00001822 if (full || type == SocketType::UNIX) {
1823 for (const auto& security : RpcSecurityValues()) {
1824 for (const auto& clientVersion : testVersions()) {
1825 for (const auto& serverVersion : testVersions()) {
1826 for (bool singleThreaded : {false, true}) {
Tomasz Wasilczyk2265ad92024-05-01 12:54:46 -07001827 for (bool noKernel : noKernelValues()) {
Steven Morelandf7421432023-07-28 22:41:44 +00001828 ret.push_back(BinderRpc::ParamType{
1829 .type = type,
1830 .security = security,
1831 .clientVersion = clientVersion,
1832 .serverVersion = serverVersion,
1833 .singleThreaded = singleThreaded,
1834 .noKernel = noKernel,
1835 });
1836 }
Steven Morelandb469f432023-07-28 22:13:47 +00001837 }
1838 }
1839 }
1840 }
Steven Morelandf7421432023-07-28 22:41:44 +00001841 } else {
1842 ret.push_back(BinderRpc::ParamType{
1843 .type = type,
1844 .security = RpcSecurity::RAW,
1845 .clientVersion = RPC_WIRE_PROTOCOL_VERSION,
1846 .serverVersion = RPC_WIRE_PROTOCOL_VERSION,
1847 .singleThreaded = false,
Tomasz Wasilczyk2265ad92024-05-01 12:54:46 -07001848 .noKernel = !kEnableKernelIpcTesting,
Steven Morelandf7421432023-07-28 22:41:44 +00001849 });
Steven Morelandb469f432023-07-28 22:13:47 +00001850 }
1851 }
Steven Morelandf7421432023-07-28 22:41:44 +00001852
Steven Morelandb469f432023-07-28 22:13:47 +00001853 return ret;
1854}
1855
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07001856INSTANTIATE_TEST_SUITE_P(PerSocket, BinderRpc, ::testing::ValuesIn(getBinderRpcParams()),
1857 BinderRpc::PrintParamInfo);
Steven Morelandc1635952021-04-01 16:20:47 +00001858
Devin Moore18f63752024-08-08 21:01:24 +00001859#ifdef BINDER_WITH_KERNEL_IPC
1860INSTANTIATE_TEST_SUITE_P(PerSocket, BinderRpcAccessor, ::testing::ValuesIn(getBinderRpcParams()),
1861 BinderRpc::PrintParamInfo);
1862#endif // BINDER_WITH_KERNEL_IPC
1863
Yifan Hong702115c2021-06-24 15:39:18 -07001864class BinderRpcServerRootObject
1865 : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001866
1867TEST_P(BinderRpcServerRootObject, WeakRootObject) {
1868 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
1869 auto setRootObject = [](bool isStrong) -> SetFn {
1870 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
1871 };
1872
Yifan Hong702115c2021-06-24 15:39:18 -07001873 auto [isStrong1, isStrong2, rpcSecurity] = GetParam();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001874 auto server = RpcServer::make(newTlsFactory(rpcSecurity));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001875 auto binder1 = sp<BBinder>::make();
1876 IBinder* binderRaw1 = binder1.get();
1877 setRootObject(isStrong1)(server.get(), binder1);
1878 EXPECT_EQ(binderRaw1, server->getRootObject());
1879 binder1.clear();
1880 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
1881
1882 auto binder2 = sp<BBinder>::make();
1883 IBinder* binderRaw2 = binder2.get();
1884 setRootObject(isStrong2)(server.get(), binder2);
1885 EXPECT_EQ(binderRaw2, server->getRootObject());
1886 binder2.clear();
1887 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
1888}
1889
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07001890INSTANTIATE_TEST_SUITE_P(BinderRpc, BinderRpcServerRootObject,
1891 ::testing::Combine(::testing::Bool(), ::testing::Bool(),
1892 ::testing::ValuesIn(RpcSecurityValues())));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001893
Yifan Hong1a235852021-05-13 16:07:47 -07001894class OneOffSignal {
1895public:
1896 // If notify() was previously called, or is called within |duration|, return true; else false.
1897 template <typename R, typename P>
1898 bool wait(std::chrono::duration<R, P> duration) {
1899 std::unique_lock<std::mutex> lock(mMutex);
1900 return mCv.wait_for(lock, duration, [this] { return mValue; });
1901 }
1902 void notify() {
1903 std::unique_lock<std::mutex> lock(mMutex);
1904 mValue = true;
1905 lock.unlock();
1906 mCv.notify_all();
1907 }
1908
1909private:
1910 std::mutex mMutex;
1911 std::condition_variable mCv;
1912 bool mValue = false;
1913};
1914
Yifan Hong194acf22021-06-29 18:44:56 -07001915TEST(BinderRpc, Java) {
Tomasz Wasilczykc2b71d52023-11-06 16:32:12 -08001916 bool expectDebuggable = false;
1917#if defined(__ANDROID__)
1918 expectDebuggable = android::base::GetBoolProperty("ro.debuggable", false) &&
1919 android::base::GetProperty("ro.build.type", "") != "user";
1920#else
Yifan Hong194acf22021-06-29 18:44:56 -07001921 GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on"
1922 "createRpcDelegateServiceManager() with a device attached, such test belongs "
1923 "to binderHostDeviceTest. Hence, just disable this test on host.";
1924#endif // !__ANDROID__
Andrei Homescu12106de2022-04-27 04:42:21 +00001925 if constexpr (!kEnableKernelIpc) {
1926 GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled "
1927 "at build time.";
1928 }
1929
Yifan Hong194acf22021-06-29 18:44:56 -07001930 sp<IServiceManager> sm = defaultServiceManager();
1931 ASSERT_NE(nullptr, sm);
1932 // Any Java service with non-empty getInterfaceDescriptor() would do.
Devin Moore29db8602024-08-14 22:21:04 +00001933 // Let's pick activity.
Devin Moore0555fbf2024-08-29 15:51:50 +00001934 auto binder = sm->checkService(String16(kKnownAidlService));
Yifan Hong194acf22021-06-29 18:44:56 -07001935 ASSERT_NE(nullptr, binder);
1936 auto descriptor = binder->getInterfaceDescriptor();
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07001937 ASSERT_GE(descriptor.size(), 0u);
Yifan Hong194acf22021-06-29 18:44:56 -07001938 ASSERT_EQ(OK, binder->pingBinder());
1939
1940 auto rpcServer = RpcServer::make();
Yifan Hong194acf22021-06-29 18:44:56 -07001941 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001942 ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port));
Yifan Hong194acf22021-06-29 18:44:56 -07001943 auto socket = rpcServer->releaseServer();
1944
1945 auto keepAlive = sp<BBinder>::make();
Yifan Hongfe4b83f2021-11-08 16:29:53 -08001946 auto setRpcClientDebugStatus = binder->setRpcClientDebug(std::move(socket), keepAlive);
1947
Tomasz Wasilczykc2b71d52023-11-06 16:32:12 -08001948 if (!expectDebuggable) {
Yifan Hongfe4b83f2021-11-08 16:29:53 -08001949 ASSERT_EQ(INVALID_OPERATION, setRpcClientDebugStatus)
Yifan Honge3caaf22022-01-12 14:46:56 -08001950 << "setRpcClientDebug should return INVALID_OPERATION on non-debuggable or user "
1951 "builds, but get "
Yifan Hongfe4b83f2021-11-08 16:29:53 -08001952 << statusToString(setRpcClientDebugStatus);
1953 GTEST_SKIP();
1954 }
1955
1956 ASSERT_EQ(OK, setRpcClientDebugStatus);
Yifan Hong194acf22021-06-29 18:44:56 -07001957
1958 auto rpcSession = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -07001959 ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port));
Yifan Hong194acf22021-06-29 18:44:56 -07001960 auto rpcBinder = rpcSession->getRootObject();
1961 ASSERT_NE(nullptr, rpcBinder);
1962
1963 ASSERT_EQ(OK, rpcBinder->pingBinder());
1964
1965 ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor())
1966 << "getInterfaceDescriptor should not crash system_server";
1967 ASSERT_EQ(OK, rpcBinder->pingBinder());
1968}
1969
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001970class BinderRpcServerOnly : public ::testing::TestWithParam<std::tuple<RpcSecurity, uint32_t>> {
1971public:
1972 static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) {
Andrei Homescuf30148c2023-03-10 00:31:45 +00001973 return std::string(newTlsFactory(std::get<0>(info.param))->toCString()) + "_serverV" +
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001974 std::to_string(std::get<1>(info.param));
1975 }
1976};
1977
1978TEST_P(BinderRpcServerOnly, SetExternalServerTest) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001979 unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001980 int sinkFd = sink.get();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001981 auto server = RpcServer::make(newTlsFactory(std::get<0>(GetParam())));
Steven Morelandca3f6382023-05-11 23:23:26 +00001982 ASSERT_TRUE(server->setProtocolVersion(std::get<1>(GetParam())));
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001983 ASSERT_FALSE(server->hasServer());
1984 ASSERT_EQ(OK, server->setupExternalServer(std::move(sink)));
1985 ASSERT_TRUE(server->hasServer());
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07001986 unique_fd retrieved = server->releaseServer();
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001987 ASSERT_FALSE(server->hasServer());
1988 ASSERT_EQ(sinkFd, retrieved.get());
1989}
1990
1991TEST_P(BinderRpcServerOnly, Shutdown) {
1992 if constexpr (!kEnableRpcThreads) {
1993 GTEST_SKIP() << "Test skipped because threads were disabled at build time";
1994 }
1995
1996 auto addr = allocateSocketAddress();
Andrei Homescuf30148c2023-03-10 00:31:45 +00001997 auto server = RpcServer::make(newTlsFactory(std::get<0>(GetParam())));
Steven Morelandca3f6382023-05-11 23:23:26 +00001998 ASSERT_TRUE(server->setProtocolVersion(std::get<1>(GetParam())));
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00001999 ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str()));
2000 auto joinEnds = std::make_shared<OneOffSignal>();
2001
2002 // If things are broken and the thread never stops, don't block other tests. Because the thread
2003 // may run after the test finishes, it must not access the stack memory of the test. Hence,
2004 // shared pointers are passed.
2005 std::thread([server, joinEnds] {
2006 server->join();
2007 joinEnds->notify();
2008 }).detach();
2009
2010 bool shutdown = false;
2011 for (int i = 0; i < 10 && !shutdown; i++) {
Steven Morelanddd231e22022-09-08 19:47:49 +00002012 usleep(30 * 1000); // 30ms; total 300ms
Andrei Homescu8d7f4bd2022-08-03 05:46:17 +00002013 if (server->shutdown()) shutdown = true;
2014 }
2015 ASSERT_TRUE(shutdown) << "server->shutdown() never returns true";
2016
2017 ASSERT_TRUE(joinEnds->wait(2s))
2018 << "After server->shutdown() returns true, join() did not stop after 2s";
2019}
2020
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07002021INSTANTIATE_TEST_SUITE_P(BinderRpc, BinderRpcServerOnly,
2022 ::testing::Combine(::testing::ValuesIn(RpcSecurityValues()),
2023 ::testing::ValuesIn(testVersions())),
2024 BinderRpcServerOnly::PrintTestParam);
Yifan Hong702115c2021-06-24 15:39:18 -07002025
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002026class RpcTransportTestUtils {
Yifan Hong1deca4b2021-09-10 16:16:44 -07002027public:
Frederick Mayledc07cf82022-05-26 20:30:12 +00002028 // Only parameterized only server version because `RpcSession` is bypassed
2029 // in the client half of the tests.
2030 using Param =
2031 std::tuple<SocketType, RpcSecurity, std::optional<RpcCertificateFormat>, uint32_t>;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002032 using ConnectToServer = std::function<unique_fd()>;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002033
2034 // A server that handles client socket connections.
2035 class Server {
2036 public:
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002037 using AcceptConnection = std::function<unique_fd(Server*)>;
David Brazdil21c887c2022-09-23 12:25:18 +01002038
Yifan Hong1deca4b2021-09-10 16:16:44 -07002039 explicit Server() {}
2040 Server(Server&&) = default;
Yifan Honge07d2732021-09-13 21:59:14 -07002041 ~Server() { shutdownAndWait(); }
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002042 [[nodiscard]] AssertionResult setUp(
2043 const Param& param,
2044 std::unique_ptr<RpcAuth> auth = std::make_unique<RpcAuthSelfSigned>()) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002045 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param;
Andrei Homescuf30148c2023-03-10 00:31:45 +00002046 auto rpcServer = RpcServer::make(newTlsFactory(rpcSecurity));
Steven Morelandca3f6382023-05-11 23:23:26 +00002047 if (!rpcServer->setProtocolVersion(serverVersion)) {
2048 return AssertionFailure() << "Invalid protocol version: " << serverVersion;
2049 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07002050 switch (socketType) {
2051 case SocketType::PRECONNECTED: {
2052 return AssertionFailure() << "Not supported by this test";
2053 } break;
2054 case SocketType::UNIX: {
2055 auto addr = allocateSocketAddress();
2056 auto status = rpcServer->setupUnixDomainServer(addr.c_str());
2057 if (status != OK) {
2058 return AssertionFailure()
2059 << "setupUnixDomainServer: " << statusToString(status);
2060 }
2061 mConnectToServer = [addr] {
2062 return connectTo(UnixSocketAddress(addr.c_str()));
2063 };
2064 } break;
David Brazdil21c887c2022-09-23 12:25:18 +01002065 case SocketType::UNIX_BOOTSTRAP: {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002066 unique_fd bootstrapFdClient, bootstrapFdServer;
2067 if (!binder::Socketpair(SOCK_STREAM, &bootstrapFdClient, &bootstrapFdServer)) {
David Brazdil21c887c2022-09-23 12:25:18 +01002068 return AssertionFailure() << "Socketpair() failed";
2069 }
2070 auto status = rpcServer->setupUnixDomainSocketBootstrapServer(
2071 std::move(bootstrapFdServer));
2072 if (status != OK) {
2073 return AssertionFailure() << "setupUnixDomainSocketBootstrapServer: "
2074 << statusToString(status);
2075 }
2076 mBootstrapSocket = RpcTransportFd(std::move(bootstrapFdClient));
2077 mAcceptConnection = &Server::recvmsgServerConnection;
2078 mConnectToServer = [this] { return connectToUnixBootstrap(mBootstrapSocket); };
2079 } break;
Alice Wang893a9912022-10-24 10:44:09 +00002080 case SocketType::UNIX_RAW: {
2081 auto addr = allocateSocketAddress();
2082 auto status = rpcServer->setupRawSocketServer(initUnixSocket(addr));
2083 if (status != OK) {
2084 return AssertionFailure()
2085 << "setupRawSocketServer: " << statusToString(status);
2086 }
2087 mConnectToServer = [addr] {
2088 return connectTo(UnixSocketAddress(addr.c_str()));
2089 };
2090 } break;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002091 case SocketType::VSOCK: {
Tomasz Wasilczyk022db682024-06-17 13:55:51 -07002092 unsigned port;
2093 auto status =
2094 rpcServer->setupVsockServer(VMADDR_CID_LOCAL, VMADDR_PORT_ANY, &port);
Yifan Hong1deca4b2021-09-10 16:16:44 -07002095 if (status != OK) {
2096 return AssertionFailure() << "setupVsockServer: " << statusToString(status);
2097 }
2098 mConnectToServer = [port] {
2099 return connectTo(VsockSocketAddress(VMADDR_CID_LOCAL, port));
2100 };
2101 } break;
2102 case SocketType::INET: {
2103 unsigned int port;
2104 auto status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port);
2105 if (status != OK) {
2106 return AssertionFailure() << "setupInetServer: " << statusToString(status);
2107 }
2108 mConnectToServer = [port] {
2109 const char* addr = kLocalInetAddress;
2110 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002111 if (aiStart == nullptr) return unique_fd{};
Yifan Hong1deca4b2021-09-10 16:16:44 -07002112 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
2113 auto fd = connectTo(
2114 InetSocketAddress(ai->ai_addr, ai->ai_addrlen, addr, port));
2115 if (fd.ok()) return fd;
2116 }
2117 ALOGE("None of the socket address resolved for %s:%u can be connected",
2118 addr, port);
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002119 return unique_fd{};
Yifan Hong1deca4b2021-09-10 16:16:44 -07002120 };
Andrei Homescu68a55612022-08-02 01:25:15 +00002121 } break;
2122 case SocketType::TIPC: {
2123 LOG_ALWAYS_FATAL("RpcTransportTest should not be enabled for TIPC");
2124 } break;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002125 }
2126 mFd = rpcServer->releaseServer();
Pawan49d74cb2022-08-03 21:19:11 +00002127 if (!mFd.fd.ok()) return AssertionFailure() << "releaseServer returns invalid fd";
Andrei Homescuf30148c2023-03-10 00:31:45 +00002128 mCtx = newTlsFactory(rpcSecurity, mCertVerifier, std::move(auth))->newServerCtx();
Yifan Hong1deca4b2021-09-10 16:16:44 -07002129 if (mCtx == nullptr) return AssertionFailure() << "newServerCtx";
2130 mSetup = true;
2131 return AssertionSuccess();
2132 }
2133 RpcTransportCtx* getCtx() const { return mCtx.get(); }
2134 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
2135 return mCertVerifier;
2136 }
2137 ConnectToServer getConnectToServerFn() { return mConnectToServer; }
2138 void start() {
2139 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
2140 mThread = std::make_unique<std::thread>(&Server::run, this);
2141 }
David Brazdil21c887c2022-09-23 12:25:18 +01002142
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002143 unique_fd acceptServerConnection() {
2144 return unique_fd(TEMP_FAILURE_RETRY(
David Brazdil21c887c2022-09-23 12:25:18 +01002145 accept4(mFd.fd.get(), nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK)));
2146 }
2147
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002148 unique_fd recvmsgServerConnection() {
2149 std::vector<std::variant<unique_fd, borrowed_fd>> fds;
David Brazdil21c887c2022-09-23 12:25:18 +01002150 int buf;
2151 iovec iov{&buf, sizeof(buf)};
2152
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +00002153 if (binder::os::receiveMessageFromSocket(mFd, &iov, 1, &fds) < 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -07002154 PLOGF("Failed receiveMessage");
David Brazdil21c887c2022-09-23 12:25:18 +01002155 }
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -07002156 LOG_ALWAYS_FATAL_IF(fds.size() != 1, "Expected one FD from receiveMessage(), got %zu",
2157 fds.size());
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002158 return std::move(std::get<unique_fd>(fds[0]));
David Brazdil21c887c2022-09-23 12:25:18 +01002159 }
2160
Yifan Hong1deca4b2021-09-10 16:16:44 -07002161 void run() {
2162 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
2163
2164 std::vector<std::thread> threads;
2165 while (OK == mFdTrigger->triggerablePoll(mFd, POLLIN)) {
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002166 unique_fd acceptedFd = mAcceptConnection(this);
Yifan Hong1deca4b2021-09-10 16:16:44 -07002167 threads.emplace_back(&Server::handleOne, this, std::move(acceptedFd));
2168 }
2169
2170 for (auto& thread : threads) thread.join();
2171 }
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -07002172 void handleOne(unique_fd acceptedFd) {
Yifan Hong1deca4b2021-09-10 16:16:44 -07002173 ASSERT_TRUE(acceptedFd.ok());
Pawan3e0061c2022-08-26 21:08:34 +00002174 RpcTransportFd transportFd(std::move(acceptedFd));
Pawan49d74cb2022-08-03 21:19:11 +00002175 auto serverTransport = mCtx->newTransport(std::move(transportFd), mFdTrigger.get());
Yifan Hong1deca4b2021-09-10 16:16:44 -07002176 if (serverTransport == nullptr) return; // handshake failed
Yifan Hong67519322021-09-13 18:51:16 -07002177 ASSERT_TRUE(mPostConnect(serverTransport.get(), mFdTrigger.get()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002178 }
Yifan Honge07d2732021-09-13 21:59:14 -07002179 void shutdownAndWait() {
Yifan Hong67519322021-09-13 18:51:16 -07002180 shutdown();
2181 join();
2182 }
2183 void shutdown() { mFdTrigger->trigger(); }
2184
2185 void setPostConnect(
2186 std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> fn) {
2187 mPostConnect = std::move(fn);
Yifan Hong1deca4b2021-09-10 16:16:44 -07002188 }
2189
2190 private:
2191 std::unique_ptr<std::thread> mThread;
2192 ConnectToServer mConnectToServer;
David Brazdil21c887c2022-09-23 12:25:18 +01002193 AcceptConnection mAcceptConnection = &Server::acceptServerConnection;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002194 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
David Brazdil21c887c2022-09-23 12:25:18 +01002195 RpcTransportFd mFd, mBootstrapSocket;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002196 std::unique_ptr<RpcTransportCtx> mCtx;
2197 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
2198 std::make_shared<RpcCertificateVerifierSimple>();
2199 bool mSetup = false;
Yifan Hong67519322021-09-13 18:51:16 -07002200 // The function invoked after connection and handshake. By default, it is
2201 // |defaultPostConnect| that sends |kMessage| to the client.
2202 std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> mPostConnect =
2203 Server::defaultPostConnect;
2204
2205 void join() {
2206 if (mThread != nullptr) {
2207 mThread->join();
2208 mThread = nullptr;
2209 }
2210 }
2211
2212 static AssertionResult defaultPostConnect(RpcTransport* serverTransport,
2213 FdTrigger* fdTrigger) {
2214 std::string message(kMessage);
Andrei Homescua39e4ed2021-12-10 08:41:54 +00002215 iovec messageIov{message.data(), message.size()};
Devin Moore695368f2022-06-03 22:29:14 +00002216 auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1,
Frederick Mayle69a0c992022-05-26 20:38:39 +00002217 std::nullopt, nullptr);
Yifan Hong67519322021-09-13 18:51:16 -07002218 if (status != OK) return AssertionFailure() << statusToString(status);
2219 return AssertionSuccess();
2220 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07002221 };
2222
2223 class Client {
2224 public:
2225 explicit Client(ConnectToServer connectToServer) : mConnectToServer(connectToServer) {}
2226 Client(Client&&) = default;
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002227 [[nodiscard]] AssertionResult setUp(const Param& param) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002228 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param;
2229 (void)serverVersion;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002230 mFdTrigger = FdTrigger::make();
Andrei Homescuf30148c2023-03-10 00:31:45 +00002231 mCtx = newTlsFactory(rpcSecurity, mCertVerifier)->newClientCtx();
Yifan Hong1deca4b2021-09-10 16:16:44 -07002232 if (mCtx == nullptr) return AssertionFailure() << "newClientCtx";
2233 return AssertionSuccess();
2234 }
2235 RpcTransportCtx* getCtx() const { return mCtx.get(); }
2236 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
2237 return mCertVerifier;
2238 }
Yifan Hong67519322021-09-13 18:51:16 -07002239 // connect() and do handshake
2240 bool setUpTransport() {
2241 mFd = mConnectToServer();
Pawan49d74cb2022-08-03 21:19:11 +00002242 if (!mFd.fd.ok()) return AssertionFailure() << "Cannot connect to server";
Yifan Hong67519322021-09-13 18:51:16 -07002243 mClientTransport = mCtx->newTransport(std::move(mFd), mFdTrigger.get());
2244 return mClientTransport != nullptr;
2245 }
2246 AssertionResult readMessage(const std::string& expectedMessage = kMessage) {
2247 LOG_ALWAYS_FATAL_IF(mClientTransport == nullptr, "setUpTransport not called or failed");
2248 std::string readMessage(expectedMessage.size(), '\0');
Andrei Homescua39e4ed2021-12-10 08:41:54 +00002249 iovec readMessageIov{readMessage.data(), readMessage.size()};
Devin Moore695368f2022-06-03 22:29:14 +00002250 status_t readStatus =
2251 mClientTransport->interruptableReadFully(mFdTrigger.get(), &readMessageIov, 1,
Frederick Mayleffe9ac22022-06-30 02:07:36 +00002252 std::nullopt, nullptr);
Yifan Hong67519322021-09-13 18:51:16 -07002253 if (readStatus != OK) {
2254 return AssertionFailure() << statusToString(readStatus);
2255 }
2256 if (readMessage != expectedMessage) {
2257 return AssertionFailure()
2258 << "Expected " << expectedMessage << ", actual " << readMessage;
2259 }
2260 return AssertionSuccess();
2261 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07002262 void run(bool handshakeOk = true, bool readOk = true) {
Yifan Hong67519322021-09-13 18:51:16 -07002263 if (!setUpTransport()) {
Yifan Hong1deca4b2021-09-10 16:16:44 -07002264 ASSERT_FALSE(handshakeOk) << "newTransport returns nullptr, but it shouldn't";
2265 return;
2266 }
2267 ASSERT_TRUE(handshakeOk) << "newTransport does not return nullptr, but it should";
Yifan Hong67519322021-09-13 18:51:16 -07002268 ASSERT_EQ(readOk, readMessage());
Yifan Hong1deca4b2021-09-10 16:16:44 -07002269 }
2270
Pawan49d74cb2022-08-03 21:19:11 +00002271 bool isTransportWaiting() { return mClientTransport->isWaiting(); }
2272
Yifan Hong1deca4b2021-09-10 16:16:44 -07002273 private:
2274 ConnectToServer mConnectToServer;
Pawan3e0061c2022-08-26 21:08:34 +00002275 RpcTransportFd mFd;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002276 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
2277 std::unique_ptr<RpcTransportCtx> mCtx;
2278 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
2279 std::make_shared<RpcCertificateVerifierSimple>();
Yifan Hong67519322021-09-13 18:51:16 -07002280 std::unique_ptr<RpcTransport> mClientTransport;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002281 };
2282
2283 // Make A trust B.
2284 template <typename A, typename B>
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002285 static status_t trust(RpcSecurity rpcSecurity,
2286 std::optional<RpcCertificateFormat> certificateFormat, const A& a,
2287 const B& b) {
Yifan Hong1deca4b2021-09-10 16:16:44 -07002288 if (rpcSecurity != RpcSecurity::TLS) return OK;
Yifan Hong22211f82021-09-14 12:32:25 -07002289 LOG_ALWAYS_FATAL_IF(!certificateFormat.has_value());
2290 auto bCert = b->getCtx()->getCertificate(*certificateFormat);
2291 return a->getCertVerifier()->addTrustedPeerCertificate(*certificateFormat, bCert);
Yifan Hong1deca4b2021-09-10 16:16:44 -07002292 }
2293
2294 static constexpr const char* kMessage = "hello";
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002295};
2296
2297class RpcTransportTest : public testing::TestWithParam<RpcTransportTestUtils::Param> {
2298public:
2299 using Server = RpcTransportTestUtils::Server;
2300 using Client = RpcTransportTestUtils::Client;
2301 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002302 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = info.param;
Andrei Homescuf30148c2023-03-10 00:31:45 +00002303 auto ret = PrintToString(socketType) + "_" + newTlsFactory(rpcSecurity)->toCString();
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002304 if (certificateFormat.has_value()) ret += "_" + PrintToString(*certificateFormat);
Frederick Mayledc07cf82022-05-26 20:30:12 +00002305 ret += "_serverV" + std::to_string(serverVersion);
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002306 return ret;
2307 }
2308 static std::vector<ParamType> getRpcTranportTestParams() {
2309 std::vector<ParamType> ret;
Frederick Mayledc07cf82022-05-26 20:30:12 +00002310 for (auto serverVersion : testVersions()) {
2311 for (auto socketType : testSocketTypes(false /* hasPreconnected */)) {
2312 for (auto rpcSecurity : RpcSecurityValues()) {
2313 switch (rpcSecurity) {
2314 case RpcSecurity::RAW: {
2315 ret.emplace_back(socketType, rpcSecurity, std::nullopt, serverVersion);
2316 } break;
2317 case RpcSecurity::TLS: {
2318 ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::PEM,
2319 serverVersion);
2320 ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::DER,
2321 serverVersion);
2322 } break;
2323 }
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002324 }
2325 }
2326 }
2327 return ret;
2328 }
2329 template <typename A, typename B>
2330 status_t trust(const A& a, const B& b) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002331 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
2332 (void)serverVersion;
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002333 return RpcTransportTestUtils::trust(rpcSecurity, certificateFormat, a, b);
2334 }
Andrei Homescu12106de2022-04-27 04:42:21 +00002335 void SetUp() override {
2336 if constexpr (!kEnableRpcThreads) {
2337 GTEST_SKIP() << "Test skipped because threads were disabled at build time";
2338 }
2339 }
Yifan Hong1deca4b2021-09-10 16:16:44 -07002340};
2341
2342TEST_P(RpcTransportTest, GoodCertificate) {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002343 auto server = std::make_unique<Server>();
2344 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002345
2346 Client client(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002347 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002348
2349 ASSERT_EQ(OK, trust(&client, server));
2350 ASSERT_EQ(OK, trust(server, &client));
2351
2352 server->start();
2353 client.run();
2354}
2355
2356TEST_P(RpcTransportTest, MultipleClients) {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002357 auto server = std::make_unique<Server>();
2358 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002359
2360 std::vector<Client> clients;
2361 for (int i = 0; i < 2; i++) {
2362 auto& client = clients.emplace_back(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002363 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002364 ASSERT_EQ(OK, trust(&client, server));
2365 ASSERT_EQ(OK, trust(server, &client));
2366 }
2367
2368 server->start();
2369 for (auto& client : clients) client.run();
2370}
2371
2372TEST_P(RpcTransportTest, UntrustedServer) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002373 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
2374 (void)serverVersion;
Yifan Hong1deca4b2021-09-10 16:16:44 -07002375
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002376 auto untrustedServer = std::make_unique<Server>();
2377 ASSERT_TRUE(untrustedServer->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002378
2379 Client client(untrustedServer->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002380 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002381
2382 ASSERT_EQ(OK, trust(untrustedServer, &client));
2383
2384 untrustedServer->start();
2385
2386 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
2387 // the client can't verify the server's identity.
2388 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
2389 client.run(handshakeOk);
2390}
2391TEST_P(RpcTransportTest, MaliciousServer) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002392 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
2393 (void)serverVersion;
2394
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002395 auto validServer = std::make_unique<Server>();
2396 ASSERT_TRUE(validServer->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002397
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002398 auto maliciousServer = std::make_unique<Server>();
2399 ASSERT_TRUE(maliciousServer->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002400
2401 Client client(maliciousServer->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002402 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002403
2404 ASSERT_EQ(OK, trust(&client, validServer));
2405 ASSERT_EQ(OK, trust(validServer, &client));
2406 ASSERT_EQ(OK, trust(maliciousServer, &client));
2407
2408 maliciousServer->start();
2409
2410 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
2411 // the client can't verify the server's identity.
2412 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
2413 client.run(handshakeOk);
2414}
2415
2416TEST_P(RpcTransportTest, UntrustedClient) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002417 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
2418 (void)serverVersion;
2419
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002420 auto server = std::make_unique<Server>();
2421 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002422
2423 Client client(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002424 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002425
2426 ASSERT_EQ(OK, trust(&client, server));
2427
2428 server->start();
2429
2430 // For TLS, Client should be able to verify server's identity, so client should see
2431 // do_handshake() successfully executed. However, server shouldn't be able to verify client's
2432 // identity and should drop the connection, so client shouldn't be able to read anything.
2433 bool readOk = rpcSecurity != RpcSecurity::TLS;
2434 client.run(true, readOk);
2435}
2436
2437TEST_P(RpcTransportTest, MaliciousClient) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002438 auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam();
2439 (void)serverVersion;
2440
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002441 auto server = std::make_unique<Server>();
2442 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002443
2444 Client validClient(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002445 ASSERT_TRUE(validClient.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002446 Client maliciousClient(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002447 ASSERT_TRUE(maliciousClient.setUp(GetParam()));
Yifan Hong1deca4b2021-09-10 16:16:44 -07002448
2449 ASSERT_EQ(OK, trust(&validClient, server));
2450 ASSERT_EQ(OK, trust(&maliciousClient, server));
2451
2452 server->start();
2453
2454 // See UntrustedClient.
2455 bool readOk = rpcSecurity != RpcSecurity::TLS;
2456 maliciousClient.run(true, readOk);
2457}
2458
Yifan Hong67519322021-09-13 18:51:16 -07002459TEST_P(RpcTransportTest, Trigger) {
2460 std::string msg2 = ", world!";
2461 std::mutex writeMutex;
2462 std::condition_variable writeCv;
2463 bool shouldContinueWriting = false;
2464 auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002465 std::string message(RpcTransportTestUtils::kMessage);
Andrei Homescua39e4ed2021-12-10 08:41:54 +00002466 iovec messageIov{message.data(), message.size()};
Frederick Mayle69a0c992022-05-26 20:38:39 +00002467 auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1,
2468 std::nullopt, nullptr);
Yifan Hong67519322021-09-13 18:51:16 -07002469 if (status != OK) return AssertionFailure() << statusToString(status);
2470
2471 {
2472 std::unique_lock<std::mutex> lock(writeMutex);
2473 if (!writeCv.wait_for(lock, 3s, [&] { return shouldContinueWriting; })) {
2474 return AssertionFailure() << "write barrier not cleared in time!";
2475 }
2476 }
2477
Andrei Homescua39e4ed2021-12-10 08:41:54 +00002478 iovec msg2Iov{msg2.data(), msg2.size()};
Frederick Mayle69a0c992022-05-26 20:38:39 +00002479 status = serverTransport->interruptableWriteFully(fdTrigger, &msg2Iov, 1, std::nullopt,
2480 nullptr);
Steven Morelandc591b472021-09-16 13:56:11 -07002481 if (status != DEAD_OBJECT)
Yifan Hong67519322021-09-13 18:51:16 -07002482 return AssertionFailure() << "When FdTrigger is shut down, interruptableWriteFully "
Steven Morelandc591b472021-09-16 13:56:11 -07002483 "should return DEAD_OBJECT, but it is "
Yifan Hong67519322021-09-13 18:51:16 -07002484 << statusToString(status);
2485 return AssertionSuccess();
2486 };
2487
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002488 auto server = std::make_unique<Server>();
2489 ASSERT_TRUE(server->setUp(GetParam()));
Yifan Hong67519322021-09-13 18:51:16 -07002490
2491 // Set up client
2492 Client client(server->getConnectToServerFn());
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002493 ASSERT_TRUE(client.setUp(GetParam()));
Yifan Hong67519322021-09-13 18:51:16 -07002494
2495 // Exchange keys
2496 ASSERT_EQ(OK, trust(&client, server));
2497 ASSERT_EQ(OK, trust(server, &client));
2498
2499 server->setPostConnect(serverPostConnect);
2500
Yifan Hong67519322021-09-13 18:51:16 -07002501 server->start();
2502 // connect() to server and do handshake
2503 ASSERT_TRUE(client.setUpTransport());
Yifan Hong22211f82021-09-14 12:32:25 -07002504 // read the first message. This ensures that server has finished handshake and start handling
2505 // client fd. Server thread should pause at writeCv.wait_for().
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002506 ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage));
Yifan Hong67519322021-09-13 18:51:16 -07002507 // Trigger server shutdown after server starts handling client FD. This ensures that the second
2508 // write is on an FdTrigger that has been shut down.
2509 server->shutdown();
2510 // Continues server thread to write the second message.
2511 {
Yifan Hong22211f82021-09-14 12:32:25 -07002512 std::lock_guard<std::mutex> lock(writeMutex);
Yifan Hong67519322021-09-13 18:51:16 -07002513 shouldContinueWriting = true;
Yifan Hong67519322021-09-13 18:51:16 -07002514 }
Yifan Hong22211f82021-09-14 12:32:25 -07002515 writeCv.notify_all();
Yifan Hong67519322021-09-13 18:51:16 -07002516 // After this line, server thread unblocks and attempts to write the second message, but
Steven Morelandc591b472021-09-16 13:56:11 -07002517 // shutdown is triggered, so write should failed with DEAD_OBJECT. See |serverPostConnect|.
Yifan Hong67519322021-09-13 18:51:16 -07002518 // On the client side, second read fails with DEAD_OBJECT
2519 ASSERT_FALSE(client.readMessage(msg2));
2520}
2521
Pawan49d74cb2022-08-03 21:19:11 +00002522TEST_P(RpcTransportTest, CheckWaitingForRead) {
2523 std::mutex readMutex;
2524 std::condition_variable readCv;
2525 bool shouldContinueReading = false;
2526 // Server will write data on transport once its started
2527 auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) {
2528 std::string message(RpcTransportTestUtils::kMessage);
2529 iovec messageIov{message.data(), message.size()};
2530 auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1,
2531 std::nullopt, nullptr);
2532 if (status != OK) return AssertionFailure() << statusToString(status);
2533
2534 {
2535 std::unique_lock<std::mutex> lock(readMutex);
2536 shouldContinueReading = true;
2537 lock.unlock();
2538 readCv.notify_all();
2539 }
2540 return AssertionSuccess();
2541 };
2542
2543 // Setup Server and client
2544 auto server = std::make_unique<Server>();
2545 ASSERT_TRUE(server->setUp(GetParam()));
2546
2547 Client client(server->getConnectToServerFn());
2548 ASSERT_TRUE(client.setUp(GetParam()));
2549
2550 ASSERT_EQ(OK, trust(&client, server));
2551 ASSERT_EQ(OK, trust(server, &client));
2552 server->setPostConnect(serverPostConnect);
2553
2554 server->start();
2555 ASSERT_TRUE(client.setUpTransport());
2556 {
2557 // Wait till server writes data
2558 std::unique_lock<std::mutex> lock(readMutex);
2559 ASSERT_TRUE(readCv.wait_for(lock, 3s, [&] { return shouldContinueReading; }));
2560 }
2561
2562 // Since there is no read polling here, we will get polling count 0
2563 ASSERT_FALSE(client.isTransportWaiting());
2564 ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage));
2565 // Thread should increment polling count, read and decrement polling count
2566 // Again, polling count should be zero here
2567 ASSERT_FALSE(client.isTransportWaiting());
2568
2569 server->shutdown();
2570}
2571
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07002572INSTANTIATE_TEST_SUITE_P(BinderRpc, RpcTransportTest,
2573 ::testing::ValuesIn(RpcTransportTest::getRpcTranportTestParams()),
2574 RpcTransportTest::PrintParamInfo);
Yifan Hong1deca4b2021-09-10 16:16:44 -07002575
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002576class RpcTransportTlsKeyTest
Frederick Mayledc07cf82022-05-26 20:30:12 +00002577 : public testing::TestWithParam<
2578 std::tuple<SocketType, RpcCertificateFormat, RpcKeyFormat, uint32_t>> {
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002579public:
2580 template <typename A, typename B>
2581 status_t trust(const A& a, const B& b) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002582 auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam();
2583 (void)serverVersion;
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002584 return RpcTransportTestUtils::trust(RpcSecurity::TLS, certificateFormat, a, b);
2585 }
2586 static std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
Frederick Mayledc07cf82022-05-26 20:30:12 +00002587 auto [socketType, certificateFormat, keyFormat, serverVersion] = info.param;
2588 return PrintToString(socketType) + "_certificate_" + PrintToString(certificateFormat) +
2589 "_key_" + PrintToString(keyFormat) + "_serverV" + std::to_string(serverVersion);
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002590 };
2591};
2592
2593TEST_P(RpcTransportTlsKeyTest, PreSignedCertificate) {
Andrei Homescu12106de2022-04-27 04:42:21 +00002594 if constexpr (!kEnableRpcThreads) {
2595 GTEST_SKIP() << "Test skipped because threads were disabled at build time";
2596 }
2597
Frederick Mayledc07cf82022-05-26 20:30:12 +00002598 auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam();
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002599
2600 std::vector<uint8_t> pkeyData, certData;
2601 {
2602 auto pkey = makeKeyPairForSelfSignedCert();
2603 ASSERT_NE(nullptr, pkey);
2604 auto cert = makeSelfSignedCert(pkey.get(), kCertValidSeconds);
2605 ASSERT_NE(nullptr, cert);
2606 pkeyData = serializeUnencryptedPrivatekey(pkey.get(), keyFormat);
2607 certData = serializeCertificate(cert.get(), certificateFormat);
2608 }
2609
2610 auto desPkey = deserializeUnencryptedPrivatekey(pkeyData, keyFormat);
2611 auto desCert = deserializeCertificate(certData, certificateFormat);
2612 auto auth = std::make_unique<RpcAuthPreSigned>(std::move(desPkey), std::move(desCert));
Frederick Mayledc07cf82022-05-26 20:30:12 +00002613 auto utilsParam = std::make_tuple(socketType, RpcSecurity::TLS,
2614 std::make_optional(certificateFormat), serverVersion);
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002615
2616 auto server = std::make_unique<RpcTransportTestUtils::Server>();
2617 ASSERT_TRUE(server->setUp(utilsParam, std::move(auth)));
2618
2619 RpcTransportTestUtils::Client client(server->getConnectToServerFn());
2620 ASSERT_TRUE(client.setUp(utilsParam));
2621
2622 ASSERT_EQ(OK, trust(&client, server));
2623 ASSERT_EQ(OK, trust(server, &client));
2624
2625 server->start();
2626 client.run();
2627}
2628
Tomasz Wasilczyke97f3a82024-04-30 10:37:32 -07002629INSTANTIATE_TEST_SUITE_P(
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002630 BinderRpc, RpcTransportTlsKeyTest,
2631 testing::Combine(testing::ValuesIn(testSocketTypes(false /* hasPreconnected*/)),
2632 testing::Values(RpcCertificateFormat::PEM, RpcCertificateFormat::DER),
Frederick Mayledc07cf82022-05-26 20:30:12 +00002633 testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER),
2634 testing::ValuesIn(testVersions())),
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002635 RpcTransportTlsKeyTest::PrintParamInfo);
Andrei Homescud65666d2023-03-03 07:28:02 +00002636#endif // BINDER_RPC_TO_TRUSTY_TEST
Yifan Hongb1ce80c2021-09-17 22:10:58 -07002637
Steven Morelandc1635952021-04-01 16:20:47 +00002638} // namespace android
2639
2640int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +00002641 ::testing::InitGoogleTest(&argc, argv);
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -07002642 __android_log_set_logger(__android_log_stderr_logger);
Steven Morelanda83191d2021-10-27 10:14:53 -07002643
Steven Moreland5553ac42020-11-11 02:14:45 +00002644 return RUN_ALL_TESTS();
2645}