Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 17 | #include <android-base/stringprintf.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 18 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 19 | #include <chrono> |
| 20 | #include <cstdlib> |
| 21 | #include <iostream> |
| 22 | #include <thread> |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 23 | #include <type_traits> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 24 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 25 | #include <dlfcn.h> |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 26 | #include <poll.h> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Andrei Homescu | 992a405 | 2022-06-28 21:26:18 +0000 | [diff] [blame] | 28 | #include <sys/socket.h> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 29 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 30 | #include "binderRpcTestCommon.h" |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 31 | #include "binderRpcTestFixture.h" |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 32 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 33 | using namespace std::chrono_literals; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 34 | using namespace std::placeholders; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 35 | using testing::AssertionFailure; |
| 36 | using testing::AssertionResult; |
| 37 | using testing::AssertionSuccess; |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 38 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 41 | #ifdef BINDER_TEST_NO_SHARED_LIBS |
| 42 | constexpr bool kEnableSharedLibs = false; |
| 43 | #else |
| 44 | constexpr bool kEnableSharedLibs = true; |
| 45 | #endif |
| 46 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 47 | static std::string WaitStatusToString(int wstatus) { |
| 48 | if (WIFEXITED(wstatus)) { |
| 49 | return base::StringPrintf("exit status %d", WEXITSTATUS(wstatus)); |
| 50 | } |
| 51 | if (WIFSIGNALED(wstatus)) { |
| 52 | return base::StringPrintf("term signal %d", WTERMSIG(wstatus)); |
| 53 | } |
| 54 | return base::StringPrintf("unexpected state %d", wstatus); |
| 55 | } |
| 56 | |
Steven Moreland | 276d8df | 2022-09-28 23:56:39 +0000 | [diff] [blame] | 57 | static void debugBacktrace(pid_t pid) { |
| 58 | std::cerr << "TAKING BACKTRACE FOR PID " << pid << std::endl; |
| 59 | system((std::string("debuggerd -b ") + std::to_string(pid)).c_str()); |
| 60 | } |
| 61 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 62 | class Process { |
| 63 | public: |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 64 | Process(Process&& other) |
| 65 | : mCustomExitStatusCheck(std::move(other.mCustomExitStatusCheck)), |
| 66 | mReadEnd(std::move(other.mReadEnd)), |
| 67 | mWriteEnd(std::move(other.mWriteEnd)) { |
| 68 | // The default move constructor doesn't clear mPid after moving it, |
| 69 | // which we need to do because the destructor checks for mPid!=0 |
| 70 | mPid = other.mPid; |
| 71 | other.mPid = 0; |
| 72 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 73 | Process(const std::function<void(android::base::borrowed_fd /* writeEnd */, |
| 74 | android::base::borrowed_fd /* readEnd */)>& f) { |
| 75 | android::base::unique_fd childWriteEnd; |
| 76 | android::base::unique_fd childReadEnd; |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 77 | CHECK(android::base::Pipe(&mReadEnd, &childWriteEnd, 0)) << strerror(errno); |
| 78 | CHECK(android::base::Pipe(&childReadEnd, &mWriteEnd, 0)) << strerror(errno); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 79 | if (0 == (mPid = fork())) { |
| 80 | // racey: assume parent doesn't crash before this is set |
| 81 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 82 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 83 | f(childWriteEnd, childReadEnd); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 84 | |
| 85 | exit(0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | ~Process() { |
| 89 | if (mPid != 0) { |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 90 | int wstatus; |
| 91 | waitpid(mPid, &wstatus, 0); |
| 92 | if (mCustomExitStatusCheck) { |
| 93 | mCustomExitStatusCheck(wstatus); |
| 94 | } else { |
| 95 | EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0) |
| 96 | << "server process failed: " << WaitStatusToString(wstatus); |
| 97 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 100 | android::base::borrowed_fd readEnd() { return mReadEnd; } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 101 | android::base::borrowed_fd writeEnd() { return mWriteEnd; } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 102 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 103 | void setCustomExitStatusCheck(std::function<void(int wstatus)> f) { |
| 104 | mCustomExitStatusCheck = std::move(f); |
| 105 | } |
| 106 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 107 | // Kill the process. Avoid if possible. Shutdown gracefully via an RPC instead. |
| 108 | void terminate() { kill(mPid, SIGTERM); } |
| 109 | |
Steven Moreland | 276d8df | 2022-09-28 23:56:39 +0000 | [diff] [blame] | 110 | pid_t getPid() { return mPid; } |
| 111 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 112 | private: |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 113 | std::function<void(int wstatus)> mCustomExitStatusCheck; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 114 | pid_t mPid = 0; |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 115 | android::base::unique_fd mReadEnd; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 116 | android::base::unique_fd mWriteEnd; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static std::string allocateSocketAddress() { |
| 120 | static size_t id = 0; |
Steven Moreland | 4bfbf2e | 2021-04-14 22:15:16 +0000 | [diff] [blame] | 121 | std::string temp = getenv("TMPDIR") ?: "/tmp"; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 122 | auto ret = temp + "/binderRpcTest_" + std::to_string(id++); |
| 123 | unlink(ret.c_str()); |
| 124 | return ret; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 127 | static unsigned int allocateVsockPort() { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 128 | static unsigned int vsockPort = 34567; |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 129 | return vsockPort++; |
| 130 | } |
| 131 | |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 132 | static base::unique_fd initUnixSocket(std::string addr) { |
| 133 | auto socket_addr = UnixSocketAddress(addr.c_str()); |
| 134 | base::unique_fd fd( |
| 135 | TEMP_FAILURE_RETRY(socket(socket_addr.addr()->sa_family, SOCK_STREAM, AF_UNIX))); |
| 136 | CHECK(fd.ok()); |
| 137 | CHECK_EQ(0, TEMP_FAILURE_RETRY(bind(fd.get(), socket_addr.addr(), socket_addr.addrSize()))); |
| 138 | return fd; |
| 139 | } |
| 140 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 141 | // Destructors need to be defined, even if pure virtual |
| 142 | ProcessSession::~ProcessSession() {} |
| 143 | |
| 144 | class LinuxProcessSession : public ProcessSession { |
| 145 | public: |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 146 | // reference to process hosting a socket server |
| 147 | Process host; |
| 148 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 149 | LinuxProcessSession(LinuxProcessSession&&) = default; |
| 150 | LinuxProcessSession(Process&& host) : host(std::move(host)) {} |
| 151 | ~LinuxProcessSession() override { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 152 | for (auto& session : sessions) { |
| 153 | session.root = nullptr; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 154 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 155 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 156 | for (auto& info : sessions) { |
| 157 | sp<RpcSession>& session = info.session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 158 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 159 | EXPECT_NE(nullptr, session); |
| 160 | EXPECT_NE(nullptr, session->state()); |
| 161 | EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:"); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 162 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 163 | wp<RpcSession> weakSession = session; |
| 164 | session = nullptr; |
Steven Moreland | 276d8df | 2022-09-28 23:56:39 +0000 | [diff] [blame] | 165 | |
Steven Moreland | 5704271 | 2022-10-04 23:56:45 +0000 | [diff] [blame] | 166 | // b/244325464 - 'getStrongCount' is printing '1' on failure here, which indicates the |
| 167 | // the object should not actually be promotable. By looping, we distinguish a race here |
| 168 | // from a bug causing the object to not be promotable. |
| 169 | for (size_t i = 0; i < 3; i++) { |
| 170 | sp<RpcSession> strongSession = weakSession.promote(); |
| 171 | EXPECT_EQ(nullptr, strongSession) |
| 172 | << (debugBacktrace(host.getPid()), debugBacktrace(getpid()), |
| 173 | "Leaked sess: ") |
| 174 | << strongSession->getStrongCount() << " checked time " << i; |
| 175 | |
| 176 | if (strongSession != nullptr) { |
| 177 | sleep(1); |
| 178 | } |
| 179 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 180 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 181 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 182 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 183 | void setCustomExitStatusCheck(std::function<void(int wstatus)> f) override { |
| 184 | host.setCustomExitStatusCheck(std::move(f)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 185 | } |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 186 | |
| 187 | void terminate() override { host.terminate(); } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 190 | static base::unique_fd connectTo(const RpcSocketAddress& addr) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 191 | base::unique_fd serverFd( |
| 192 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 193 | int savedErrno = errno; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 194 | CHECK(serverFd.ok()) << "Could not create socket " << addr.toString() << ": " |
| 195 | << strerror(savedErrno); |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 196 | |
| 197 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 198 | int savedErrno = errno; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 199 | LOG(FATAL) << "Could not connect to socket " << addr.toString() << ": " |
| 200 | << strerror(savedErrno); |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 201 | } |
| 202 | return serverFd; |
| 203 | } |
| 204 | |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 205 | static base::unique_fd connectToUnixBootstrap(const RpcTransportFd& transportFd) { |
| 206 | base::unique_fd sockClient, sockServer; |
| 207 | if (!base::Socketpair(SOCK_STREAM, &sockClient, &sockServer)) { |
| 208 | int savedErrno = errno; |
| 209 | LOG(FATAL) << "Failed socketpair(): " << strerror(savedErrno); |
| 210 | } |
| 211 | |
| 212 | int zero = 0; |
| 213 | iovec iov{&zero, sizeof(zero)}; |
| 214 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>> fds; |
| 215 | fds.emplace_back(std::move(sockServer)); |
| 216 | |
| 217 | if (sendMessageOnSocket(transportFd, &iov, 1, &fds) < 0) { |
| 218 | int savedErrno = errno; |
| 219 | LOG(FATAL) << "Failed sendMessageOnSocket: " << strerror(savedErrno); |
| 220 | } |
| 221 | return std::move(sockClient); |
| 222 | } |
| 223 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 224 | std::string BinderRpc::PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
| 225 | auto [type, security, clientVersion, serverVersion, singleThreaded, noKernel] = info.param; |
| 226 | auto ret = PrintToString(type) + "_" + newFactory(security)->toCString() + "_clientV" + |
| 227 | std::to_string(clientVersion) + "_serverV" + std::to_string(serverVersion); |
| 228 | if (singleThreaded) { |
| 229 | ret += "_single_threaded"; |
| 230 | } |
| 231 | if (noKernel) { |
| 232 | ret += "_no_kernel"; |
| 233 | } |
| 234 | return ret; |
| 235 | } |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 236 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 237 | // This creates a new process serving an interface on a certain number of |
| 238 | // threads. |
| 239 | std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc( |
| 240 | const BinderRpcOptions& options) { |
| 241 | CHECK_GE(options.numSessions, 1) << "Must have at least one session to a server"; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 242 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 243 | SocketType socketType = std::get<0>(GetParam()); |
| 244 | RpcSecurity rpcSecurity = std::get<1>(GetParam()); |
| 245 | uint32_t clientVersion = std::get<2>(GetParam()); |
| 246 | uint32_t serverVersion = std::get<3>(GetParam()); |
| 247 | bool singleThreaded = std::get<4>(GetParam()); |
| 248 | bool noKernel = std::get<5>(GetParam()); |
| 249 | |
| 250 | std::string path = android::base::GetExecutableDirectory(); |
| 251 | auto servicePath = android::base::StringPrintf("%s/binder_rpc_test_service%s%s", path.c_str(), |
| 252 | singleThreaded ? "_single_threaded" : "", |
| 253 | noKernel ? "_no_kernel" : ""); |
| 254 | |
Alice Wang | 1ef010b | 2022-11-14 09:09:25 +0000 | [diff] [blame] | 255 | base::unique_fd bootstrapClientFd, socketFd; |
| 256 | |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 257 | auto addr = allocateSocketAddress(); |
| 258 | // Initializes the socket before the fork/exec. |
| 259 | if (socketType == SocketType::UNIX_RAW) { |
| 260 | socketFd = initUnixSocket(addr); |
Alice Wang | 1ef010b | 2022-11-14 09:09:25 +0000 | [diff] [blame] | 261 | } else if (socketType == SocketType::UNIX_BOOTSTRAP) { |
| 262 | // Do not set O_CLOEXEC, bootstrapServerFd needs to survive fork/exec. |
| 263 | // This is because we cannot pass ParcelFileDescriptor over a pipe. |
| 264 | if (!base::Socketpair(SOCK_STREAM, &bootstrapClientFd, &socketFd)) { |
| 265 | int savedErrno = errno; |
| 266 | LOG(FATAL) << "Failed socketpair(): " << strerror(savedErrno); |
| 267 | } |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 268 | } |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 269 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 270 | auto ret = std::make_unique<LinuxProcessSession>( |
| 271 | Process([=](android::base::borrowed_fd writeEnd, android::base::borrowed_fd readEnd) { |
| 272 | auto writeFd = std::to_string(writeEnd.get()); |
| 273 | auto readFd = std::to_string(readEnd.get()); |
| 274 | execl(servicePath.c_str(), servicePath.c_str(), writeFd.c_str(), readFd.c_str(), |
| 275 | NULL); |
| 276 | })); |
| 277 | |
| 278 | BinderRpcTestServerConfig serverConfig; |
| 279 | serverConfig.numThreads = options.numThreads; |
| 280 | serverConfig.socketType = static_cast<int32_t>(socketType); |
| 281 | serverConfig.rpcSecurity = static_cast<int32_t>(rpcSecurity); |
| 282 | serverConfig.serverVersion = serverVersion; |
| 283 | serverConfig.vsockPort = allocateVsockPort(); |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 284 | serverConfig.addr = addr; |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 285 | serverConfig.socketFd = socketFd.get(); |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 286 | for (auto mode : options.serverSupportedFileDescriptorTransportModes) { |
| 287 | serverConfig.serverSupportedFileDescriptorTransportModes.push_back( |
| 288 | static_cast<int32_t>(mode)); |
| 289 | } |
| 290 | writeToFd(ret->host.writeEnd(), serverConfig); |
| 291 | |
| 292 | std::vector<sp<RpcSession>> sessions; |
| 293 | auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>(); |
| 294 | for (size_t i = 0; i < options.numSessions; i++) { |
| 295 | sessions.emplace_back(RpcSession::make(newFactory(rpcSecurity, certVerifier))); |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 298 | auto serverInfo = readFromFd<BinderRpcTestServerInfo>(ret->host.readEnd()); |
| 299 | BinderRpcTestClientInfo clientInfo; |
| 300 | for (const auto& session : sessions) { |
| 301 | auto& parcelableCert = clientInfo.certs.emplace_back(); |
| 302 | parcelableCert.data = session->getCertificate(RpcCertificateFormat::PEM); |
| 303 | } |
| 304 | writeToFd(ret->host.writeEnd(), clientInfo); |
| 305 | |
| 306 | CHECK_LE(serverInfo.port, std::numeric_limits<unsigned int>::max()); |
| 307 | if (socketType == SocketType::INET) { |
| 308 | CHECK_NE(0, serverInfo.port); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 311 | if (rpcSecurity == RpcSecurity::TLS) { |
| 312 | const auto& serverCert = serverInfo.cert.data; |
| 313 | CHECK_EQ(OK, |
| 314 | certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM, serverCert)); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 317 | status_t status; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 318 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 319 | for (const auto& session : sessions) { |
| 320 | CHECK(session->setProtocolVersion(clientVersion)); |
| 321 | session->setMaxIncomingThreads(options.numIncomingConnections); |
| 322 | session->setMaxOutgoingThreads(options.numOutgoingConnections); |
| 323 | session->setFileDescriptorTransportMode(options.clientFileDescriptorTransportMode); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 324 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 325 | switch (socketType) { |
| 326 | case SocketType::PRECONNECTED: |
| 327 | status = session->setupPreconnectedClient({}, [=]() { |
| 328 | return connectTo(UnixSocketAddress(serverConfig.addr.c_str())); |
| 329 | }); |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 330 | break; |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 331 | case SocketType::UNIX_RAW: |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 332 | case SocketType::UNIX: |
| 333 | status = session->setupUnixDomainClient(serverConfig.addr.c_str()); |
| 334 | break; |
| 335 | case SocketType::UNIX_BOOTSTRAP: |
| 336 | status = session->setupUnixDomainSocketBootstrapClient( |
| 337 | base::unique_fd(dup(bootstrapClientFd.get()))); |
| 338 | break; |
| 339 | case SocketType::VSOCK: |
| 340 | status = session->setupVsockClient(VMADDR_CID_LOCAL, serverConfig.vsockPort); |
| 341 | break; |
| 342 | case SocketType::INET: |
| 343 | status = session->setupInetClient("127.0.0.1", serverInfo.port); |
| 344 | break; |
| 345 | default: |
| 346 | LOG_ALWAYS_FATAL("Unknown socket type"); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 347 | } |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 348 | if (options.allowConnectFailure && status != OK) { |
| 349 | ret->sessions.clear(); |
| 350 | break; |
| 351 | } |
| 352 | CHECK_EQ(status, OK) << "Could not connect: " << statusToString(status); |
| 353 | ret->sessions.push_back({session, session->getRootObject()}); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 354 | } |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 355 | return ret; |
| 356 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 357 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 358 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
| 359 | if (clientOrServerSingleThreaded()) { |
| 360 | GTEST_SKIP() << "This test requires multiple threads"; |
| 361 | } |
| 362 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 363 | constexpr size_t kNumThreads = 10; |
| 364 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 365 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 366 | |
| 367 | EXPECT_OK(proc.rootIface->lock()); |
| 368 | |
| 369 | // block all but one thread taking locks |
| 370 | std::vector<std::thread> ts; |
| 371 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 372 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 373 | } |
| 374 | |
Steven Moreland | d6d816f | 2022-12-23 01:37:17 +0000 | [diff] [blame] | 375 | usleep(100000); // give chance for calls on other threads |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 376 | |
| 377 | // other calls still work |
| 378 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 379 | |
Steven Moreland | d6d816f | 2022-12-23 01:37:17 +0000 | [diff] [blame] | 380 | constexpr size_t blockTimeMs = 100; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 381 | size_t epochMsBefore = epochMillis(); |
| 382 | // after this, we should never see a response within this time |
| 383 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 384 | |
| 385 | // this call should be blocked for blockTimeMs |
| 386 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 387 | |
| 388 | size_t epochMsAfter = epochMillis(); |
| 389 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 390 | |
| 391 | for (auto& t : ts) t.join(); |
| 392 | } |
| 393 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 394 | static void testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t numCalls, |
| 395 | size_t sleepMs = 500) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 396 | size_t epochMsBefore = epochMillis(); |
| 397 | |
| 398 | std::vector<std::thread> ts; |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 399 | for (size_t i = 0; i < numCalls; i++) { |
| 400 | ts.push_back(std::thread([&] { iface->sleepMs(sleepMs); })); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | for (auto& t : ts) t.join(); |
| 404 | |
| 405 | size_t epochMsAfter = epochMillis(); |
| 406 | |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 407 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * sleepMs); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 408 | |
| 409 | // Potential flake, but make sure calls are handled in parallel. |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 410 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * sleepMs); |
| 411 | } |
| 412 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 413 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
| 414 | if (clientOrServerSingleThreaded()) { |
| 415 | GTEST_SKIP() << "This test requires multiple threads"; |
| 416 | } |
| 417 | |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 418 | constexpr size_t kNumThreads = 10; |
| 419 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 420 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); |
| 421 | testThreadPoolOverSaturated(proc.rootIface, kNumCalls); |
| 422 | } |
| 423 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 424 | TEST_P(BinderRpc, ThreadPoolLimitOutgoing) { |
| 425 | if (clientOrServerSingleThreaded()) { |
| 426 | GTEST_SKIP() << "This test requires multiple threads"; |
| 427 | } |
| 428 | |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 429 | constexpr size_t kNumThreads = 20; |
| 430 | constexpr size_t kNumOutgoingConnections = 10; |
| 431 | constexpr size_t kNumCalls = kNumOutgoingConnections + 3; |
| 432 | auto proc = createRpcTestSocketServerProcess( |
| 433 | {.numThreads = kNumThreads, .numOutgoingConnections = kNumOutgoingConnections}); |
| 434 | testThreadPoolOverSaturated(proc.rootIface, kNumCalls); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 437 | TEST_P(BinderRpc, ThreadingStressTest) { |
| 438 | if (clientOrServerSingleThreaded()) { |
| 439 | GTEST_SKIP() << "This test requires multiple threads"; |
| 440 | } |
| 441 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 442 | constexpr size_t kNumClientThreads = 10; |
| 443 | constexpr size_t kNumServerThreads = 10; |
| 444 | constexpr size_t kNumCalls = 100; |
| 445 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 446 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 447 | |
| 448 | std::vector<std::thread> threads; |
| 449 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 450 | threads.push_back(std::thread([&] { |
| 451 | for (size_t j = 0; j < kNumCalls; j++) { |
| 452 | sp<IBinder> out; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 453 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 454 | EXPECT_EQ(proc.rootBinder, out); |
| 455 | } |
| 456 | })); |
| 457 | } |
| 458 | |
| 459 | for (auto& t : threads) t.join(); |
| 460 | } |
| 461 | |
Steven Moreland | 925ba0a | 2021-09-17 18:06:32 -0700 | [diff] [blame] | 462 | static void saturateThreadPool(size_t threadCount, const sp<IBinderRpcTest>& iface) { |
| 463 | std::vector<std::thread> threads; |
| 464 | for (size_t i = 0; i < threadCount; i++) { |
| 465 | threads.push_back(std::thread([&] { EXPECT_OK(iface->sleepMs(500)); })); |
| 466 | } |
| 467 | for (auto& t : threads) t.join(); |
| 468 | } |
| 469 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 470 | TEST_P(BinderRpc, OnewayStressTest) { |
| 471 | if (clientOrServerSingleThreaded()) { |
| 472 | GTEST_SKIP() << "This test requires multiple threads"; |
| 473 | } |
| 474 | |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 475 | constexpr size_t kNumClientThreads = 10; |
| 476 | constexpr size_t kNumServerThreads = 10; |
Steven Moreland | 3c3ab8d | 2021-09-23 10:29:50 -0700 | [diff] [blame] | 477 | constexpr size_t kNumCalls = 1000; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 478 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 479 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads}); |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 480 | |
| 481 | std::vector<std::thread> threads; |
| 482 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 483 | threads.push_back(std::thread([&] { |
| 484 | for (size_t j = 0; j < kNumCalls; j++) { |
| 485 | EXPECT_OK(proc.rootIface->sendString("a")); |
| 486 | } |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 487 | })); |
| 488 | } |
| 489 | |
| 490 | for (auto& t : threads) t.join(); |
Steven Moreland | 925ba0a | 2021-09-17 18:06:32 -0700 | [diff] [blame] | 491 | |
| 492 | saturateThreadPool(kNumServerThreads, proc.rootIface); |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Frederick Mayle | b0221d1 | 2022-10-03 23:10:53 +0000 | [diff] [blame] | 495 | TEST_P(BinderRpc, OnewayCallQueueingWithFds) { |
| 496 | if (!supportsFdTransport()) { |
| 497 | GTEST_SKIP() << "Would fail trivially (which is tested elsewhere)"; |
| 498 | } |
| 499 | if (clientOrServerSingleThreaded()) { |
| 500 | GTEST_SKIP() << "This test requires multiple threads"; |
| 501 | } |
| 502 | |
| 503 | // This test forces a oneway transaction to be queued by issuing two |
| 504 | // `blockingSendFdOneway` calls, then drains the queue by issuing two |
| 505 | // `blockingRecvFd` calls. |
| 506 | // |
| 507 | // For more details about the queuing semantics see |
| 508 | // https://developer.android.com/reference/android/os/IBinder#FLAG_ONEWAY |
| 509 | |
| 510 | auto proc = createRpcTestSocketServerProcess({ |
| 511 | .numThreads = 3, |
| 512 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 513 | .serverSupportedFileDescriptorTransportModes = |
| 514 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 515 | }); |
| 516 | |
| 517 | EXPECT_OK(proc.rootIface->blockingSendFdOneway( |
| 518 | android::os::ParcelFileDescriptor(mockFileDescriptor("a")))); |
| 519 | EXPECT_OK(proc.rootIface->blockingSendFdOneway( |
| 520 | android::os::ParcelFileDescriptor(mockFileDescriptor("b")))); |
| 521 | |
| 522 | android::os::ParcelFileDescriptor fdA; |
| 523 | EXPECT_OK(proc.rootIface->blockingRecvFd(&fdA)); |
| 524 | std::string result; |
| 525 | CHECK(android::base::ReadFdToString(fdA.get(), &result)); |
| 526 | EXPECT_EQ(result, "a"); |
| 527 | |
| 528 | android::os::ParcelFileDescriptor fdB; |
| 529 | EXPECT_OK(proc.rootIface->blockingRecvFd(&fdB)); |
| 530 | CHECK(android::base::ReadFdToString(fdB.get(), &result)); |
| 531 | EXPECT_EQ(result, "b"); |
| 532 | } |
| 533 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 534 | TEST_P(BinderRpc, OnewayCallQueueing) { |
| 535 | if (clientOrServerSingleThreaded()) { |
| 536 | GTEST_SKIP() << "This test requires multiple threads"; |
| 537 | } |
| 538 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 539 | constexpr size_t kNumSleeps = 10; |
| 540 | constexpr size_t kNumExtraServerThreads = 4; |
| 541 | constexpr size_t kSleepMs = 50; |
| 542 | |
| 543 | // make sure calls to the same object happen on the same thread |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 544 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 545 | |
| 546 | EXPECT_OK(proc.rootIface->lock()); |
| 547 | |
Steven Moreland | 1c67880 | 2021-09-17 16:48:47 -0700 | [diff] [blame] | 548 | size_t epochMsBefore = epochMillis(); |
| 549 | |
| 550 | // all these *Async commands should be queued on the server sequentially, |
| 551 | // even though there are multiple threads. |
| 552 | for (size_t i = 0; i + 1 < kNumSleeps; i++) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 553 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 554 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 555 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 556 | |
Steven Moreland | 1c67880 | 2021-09-17 16:48:47 -0700 | [diff] [blame] | 557 | // this can only return once the final async call has unlocked |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 558 | EXPECT_OK(proc.rootIface->lockUnlock()); |
Steven Moreland | 1c67880 | 2021-09-17 16:48:47 -0700 | [diff] [blame] | 559 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 560 | size_t epochMsAfter = epochMillis(); |
| 561 | |
Frederick Mayle | 3fa815d | 2022-07-12 22:52:52 +0000 | [diff] [blame] | 562 | EXPECT_GE(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 563 | |
Steven Moreland | 925ba0a | 2021-09-17 18:06:32 -0700 | [diff] [blame] | 564 | saturateThreadPool(1 + kNumExtraServerThreads, proc.rootIface); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 567 | TEST_P(BinderRpc, OnewayCallExhaustion) { |
| 568 | if (clientOrServerSingleThreaded()) { |
| 569 | GTEST_SKIP() << "This test requires multiple threads"; |
| 570 | } |
| 571 | |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 572 | constexpr size_t kNumClients = 2; |
| 573 | constexpr size_t kTooLongMs = 1000; |
| 574 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 575 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2}); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 576 | |
| 577 | // Build up oneway calls on the second session to make sure it terminates |
| 578 | // and shuts down. The first session should be unaffected (proc destructor |
| 579 | // checks the first session). |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 580 | auto iface = interface_cast<IBinderRpcTest>(proc.proc->sessions.at(1).root); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 581 | |
| 582 | std::vector<std::thread> threads; |
| 583 | for (size_t i = 0; i < kNumClients; i++) { |
| 584 | // one of these threads will get stuck queueing a transaction once the |
| 585 | // socket fills up, the other will be able to fill up transactions on |
| 586 | // this object |
| 587 | threads.push_back(std::thread([&] { |
| 588 | while (iface->sleepMsAsync(kTooLongMs).isOk()) { |
| 589 | } |
| 590 | })); |
| 591 | } |
| 592 | for (auto& t : threads) t.join(); |
| 593 | |
| 594 | Status status = iface->sleepMsAsync(kTooLongMs); |
| 595 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 596 | |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 597 | // now that it has died, wait for the remote session to shutdown |
| 598 | std::vector<int32_t> remoteCounts; |
| 599 | do { |
| 600 | EXPECT_OK(proc.rootIface->countBinders(&remoteCounts)); |
| 601 | } while (remoteCounts.size() == kNumClients); |
| 602 | |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 603 | // the second session should be shutdown in the other process by the time we |
| 604 | // are able to join above (it'll only be hung up once it finishes processing |
| 605 | // any pending commands). We need to erase this session from the record |
| 606 | // here, so that the destructor for our session won't check that this |
| 607 | // session is valid, but we still want it to test the other session. |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 608 | proc.proc->sessions.erase(proc.proc->sessions.begin() + 1); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 611 | TEST_P(BinderRpc, SingleDeathRecipient) { |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 612 | if (clientOrServerSingleThreaded()) { |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 613 | GTEST_SKIP() << "This test requires multiple threads"; |
| 614 | } |
| 615 | class MyDeathRec : public IBinder::DeathRecipient { |
| 616 | public: |
| 617 | void binderDied(const wp<IBinder>& /* who */) override { |
| 618 | dead = true; |
| 619 | mCv.notify_one(); |
| 620 | } |
| 621 | std::mutex mMtx; |
| 622 | std::condition_variable mCv; |
| 623 | bool dead = false; |
| 624 | }; |
| 625 | |
| 626 | // Death recipient needs to have an incoming connection to be called |
| 627 | auto proc = createRpcTestSocketServerProcess( |
| 628 | {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); |
| 629 | |
| 630 | auto dr = sp<MyDeathRec>::make(); |
| 631 | ASSERT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0)); |
| 632 | |
| 633 | if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) { |
| 634 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 635 | } |
| 636 | |
| 637 | std::unique_lock<std::mutex> lock(dr->mMtx); |
Steven Moreland | dd231e2 | 2022-09-08 19:47:49 +0000 | [diff] [blame] | 638 | ASSERT_TRUE(dr->mCv.wait_for(lock, 100ms, [&]() { return dr->dead; })); |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 639 | |
| 640 | // need to wait for the session to shutdown so we don't "Leak session" |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 641 | EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true)); |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 642 | proc.expectAlreadyShutdown = true; |
| 643 | } |
| 644 | |
| 645 | TEST_P(BinderRpc, SingleDeathRecipientOnShutdown) { |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 646 | if (clientOrServerSingleThreaded()) { |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 647 | GTEST_SKIP() << "This test requires multiple threads"; |
| 648 | } |
| 649 | class MyDeathRec : public IBinder::DeathRecipient { |
| 650 | public: |
| 651 | void binderDied(const wp<IBinder>& /* who */) override { |
| 652 | dead = true; |
| 653 | mCv.notify_one(); |
| 654 | } |
| 655 | std::mutex mMtx; |
| 656 | std::condition_variable mCv; |
| 657 | bool dead = false; |
| 658 | }; |
| 659 | |
| 660 | // Death recipient needs to have an incoming connection to be called |
| 661 | auto proc = createRpcTestSocketServerProcess( |
| 662 | {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); |
| 663 | |
| 664 | auto dr = sp<MyDeathRec>::make(); |
| 665 | EXPECT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0)); |
| 666 | |
| 667 | // Explicitly calling shutDownAndWait will cause the death recipients |
| 668 | // to be called. |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 669 | EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true)); |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 670 | |
| 671 | std::unique_lock<std::mutex> lock(dr->mMtx); |
| 672 | if (!dr->dead) { |
Steven Moreland | dd231e2 | 2022-09-08 19:47:49 +0000 | [diff] [blame] | 673 | EXPECT_EQ(std::cv_status::no_timeout, dr->mCv.wait_for(lock, 100ms)); |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 674 | } |
| 675 | EXPECT_TRUE(dr->dead) << "Failed to receive the death notification."; |
| 676 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 677 | proc.proc->terminate(); |
| 678 | proc.proc->setCustomExitStatusCheck([](int wstatus) { |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 679 | EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM) |
| 680 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 681 | }); |
| 682 | proc.expectAlreadyShutdown = true; |
| 683 | } |
| 684 | |
Steven Moreland | 5ec743f | 2023-01-18 01:02:06 +0000 | [diff] [blame^] | 685 | TEST_P(BinderRpc, DeathRecipientFailsWithoutIncoming) { |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 686 | class MyDeathRec : public IBinder::DeathRecipient { |
| 687 | public: |
| 688 | void binderDied(const wp<IBinder>& /* who */) override {} |
| 689 | }; |
| 690 | |
| 691 | auto proc = createRpcTestSocketServerProcess( |
| 692 | {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 0}); |
| 693 | |
| 694 | auto dr = sp<MyDeathRec>::make(); |
Steven Moreland | 5ec743f | 2023-01-18 01:02:06 +0000 | [diff] [blame^] | 695 | EXPECT_EQ(INVALID_OPERATION, proc.rootBinder->linkToDeath(dr, (void*)1, 0)); |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | TEST_P(BinderRpc, UnlinkDeathRecipient) { |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 699 | if (clientOrServerSingleThreaded()) { |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 700 | GTEST_SKIP() << "This test requires multiple threads"; |
| 701 | } |
| 702 | class MyDeathRec : public IBinder::DeathRecipient { |
| 703 | public: |
| 704 | void binderDied(const wp<IBinder>& /* who */) override { |
| 705 | GTEST_FAIL() << "This should not be called after unlinkToDeath"; |
| 706 | } |
| 707 | }; |
| 708 | |
| 709 | // Death recipient needs to have an incoming connection to be called |
| 710 | auto proc = createRpcTestSocketServerProcess( |
| 711 | {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); |
| 712 | |
| 713 | auto dr = sp<MyDeathRec>::make(); |
| 714 | ASSERT_EQ(OK, proc.rootBinder->linkToDeath(dr, (void*)1, 0)); |
| 715 | ASSERT_EQ(OK, proc.rootBinder->unlinkToDeath(dr, (void*)1, 0, nullptr)); |
| 716 | |
| 717 | if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) { |
| 718 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 719 | } |
| 720 | |
| 721 | // need to wait for the session to shutdown so we don't "Leak session" |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 722 | EXPECT_TRUE(proc.proc->sessions.at(0).session->shutdownAndWait(true)); |
Devin Moore | 66d5b7a | 2022-07-07 21:42:10 +0000 | [diff] [blame] | 723 | proc.expectAlreadyShutdown = true; |
| 724 | } |
| 725 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 726 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 727 | for (bool doDeathCleanup : {true, false}) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 728 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 729 | |
| 730 | // make sure there is some state during crash |
| 731 | // 1. we hold their binder |
| 732 | sp<IBinderRpcSession> session; |
| 733 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 734 | // 2. they hold our binder |
| 735 | sp<IBinder> binder = new BBinder(); |
| 736 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 737 | |
| 738 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 739 | << "Do death cleanup: " << doDeathCleanup; |
| 740 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 741 | proc.proc->setCustomExitStatusCheck([](int wstatus) { |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 742 | EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 1) |
| 743 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 744 | }); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 745 | proc.expectAlreadyShutdown = true; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 749 | TEST_P(BinderRpc, UseKernelBinderCallingId) { |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 750 | // This test only works if the current process shared the internal state of |
| 751 | // ProcessState with the service across the call to fork(). Both the static |
| 752 | // libraries and libbinder.so have their own separate copies of all the |
| 753 | // globals, so the test only works when the test client and service both use |
| 754 | // libbinder.so (when using static libraries, even a client and service |
| 755 | // using the same kind of static library should have separate copies of the |
| 756 | // variables). |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 757 | if (!kEnableSharedLibs || serverSingleThreaded() || noKernel()) { |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 758 | GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " |
| 759 | "at build time."; |
| 760 | } |
| 761 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 762 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 763 | |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 764 | // we can't allocate IPCThreadState so actually the first time should |
| 765 | // succeed :( |
| 766 | EXPECT_OK(proc.rootIface->useKernelBinderCallingId()); |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 767 | |
| 768 | // second time! we catch the error :) |
| 769 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError()); |
| 770 | |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 771 | proc.proc->setCustomExitStatusCheck([](int wstatus) { |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame] | 772 | EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT) |
| 773 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 774 | }); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 775 | proc.expectAlreadyShutdown = true; |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 778 | TEST_P(BinderRpc, FileDescriptorTransportRejectNone) { |
| 779 | auto proc = createRpcTestSocketServerProcess({ |
| 780 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE, |
| 781 | .serverSupportedFileDescriptorTransportModes = |
| 782 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 783 | .allowConnectFailure = true, |
| 784 | }); |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 785 | EXPECT_TRUE(proc.proc->sessions.empty()) << "session connections should have failed"; |
| 786 | proc.proc->terminate(); |
| 787 | proc.proc->setCustomExitStatusCheck([](int wstatus) { |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 788 | EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM) |
| 789 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 790 | }); |
| 791 | proc.expectAlreadyShutdown = true; |
| 792 | } |
| 793 | |
| 794 | TEST_P(BinderRpc, FileDescriptorTransportRejectUnix) { |
| 795 | auto proc = createRpcTestSocketServerProcess({ |
| 796 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 797 | .serverSupportedFileDescriptorTransportModes = |
| 798 | {RpcSession::FileDescriptorTransportMode::NONE}, |
| 799 | .allowConnectFailure = true, |
| 800 | }); |
Andrei Homescu | 9683463 | 2022-10-14 00:49:49 +0000 | [diff] [blame] | 801 | EXPECT_TRUE(proc.proc->sessions.empty()) << "session connections should have failed"; |
| 802 | proc.proc->terminate(); |
| 803 | proc.proc->setCustomExitStatusCheck([](int wstatus) { |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 804 | EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGTERM) |
| 805 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 806 | }); |
| 807 | proc.expectAlreadyShutdown = true; |
| 808 | } |
| 809 | |
| 810 | TEST_P(BinderRpc, FileDescriptorTransportOptionalUnix) { |
| 811 | auto proc = createRpcTestSocketServerProcess({ |
| 812 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE, |
| 813 | .serverSupportedFileDescriptorTransportModes = |
| 814 | {RpcSession::FileDescriptorTransportMode::NONE, |
| 815 | RpcSession::FileDescriptorTransportMode::UNIX}, |
| 816 | }); |
| 817 | |
| 818 | android::os::ParcelFileDescriptor out; |
| 819 | auto status = proc.rootIface->echoAsFile("hello", &out); |
| 820 | EXPECT_EQ(status.transactionError(), FDS_NOT_ALLOWED) << status; |
| 821 | } |
| 822 | |
| 823 | TEST_P(BinderRpc, ReceiveFile) { |
| 824 | auto proc = createRpcTestSocketServerProcess({ |
| 825 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 826 | .serverSupportedFileDescriptorTransportModes = |
| 827 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 828 | }); |
| 829 | |
| 830 | android::os::ParcelFileDescriptor out; |
| 831 | auto status = proc.rootIface->echoAsFile("hello", &out); |
| 832 | if (!supportsFdTransport()) { |
| 833 | EXPECT_EQ(status.transactionError(), BAD_VALUE) << status; |
| 834 | return; |
| 835 | } |
| 836 | ASSERT_TRUE(status.isOk()) << status; |
| 837 | |
| 838 | std::string result; |
| 839 | CHECK(android::base::ReadFdToString(out.get(), &result)); |
| 840 | EXPECT_EQ(result, "hello"); |
| 841 | } |
| 842 | |
| 843 | TEST_P(BinderRpc, SendFiles) { |
| 844 | auto proc = createRpcTestSocketServerProcess({ |
| 845 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 846 | .serverSupportedFileDescriptorTransportModes = |
| 847 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 848 | }); |
| 849 | |
| 850 | std::vector<android::os::ParcelFileDescriptor> files; |
| 851 | files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("123"))); |
| 852 | files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a"))); |
| 853 | files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("b"))); |
| 854 | files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("cd"))); |
| 855 | |
| 856 | android::os::ParcelFileDescriptor out; |
| 857 | auto status = proc.rootIface->concatFiles(files, &out); |
| 858 | if (!supportsFdTransport()) { |
| 859 | EXPECT_EQ(status.transactionError(), BAD_VALUE) << status; |
| 860 | return; |
| 861 | } |
| 862 | ASSERT_TRUE(status.isOk()) << status; |
| 863 | |
| 864 | std::string result; |
| 865 | CHECK(android::base::ReadFdToString(out.get(), &result)); |
| 866 | EXPECT_EQ(result, "123abcd"); |
| 867 | } |
| 868 | |
| 869 | TEST_P(BinderRpc, SendMaxFiles) { |
| 870 | if (!supportsFdTransport()) { |
| 871 | GTEST_SKIP() << "Would fail trivially (which is tested by BinderRpc::SendFiles)"; |
| 872 | } |
| 873 | |
| 874 | auto proc = createRpcTestSocketServerProcess({ |
| 875 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 876 | .serverSupportedFileDescriptorTransportModes = |
| 877 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 878 | }); |
| 879 | |
| 880 | std::vector<android::os::ParcelFileDescriptor> files; |
| 881 | for (int i = 0; i < 253; i++) { |
| 882 | files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a"))); |
| 883 | } |
| 884 | |
| 885 | android::os::ParcelFileDescriptor out; |
| 886 | auto status = proc.rootIface->concatFiles(files, &out); |
| 887 | ASSERT_TRUE(status.isOk()) << status; |
| 888 | |
| 889 | std::string result; |
| 890 | CHECK(android::base::ReadFdToString(out.get(), &result)); |
| 891 | EXPECT_EQ(result, std::string(253, 'a')); |
| 892 | } |
| 893 | |
| 894 | TEST_P(BinderRpc, SendTooManyFiles) { |
| 895 | if (!supportsFdTransport()) { |
| 896 | GTEST_SKIP() << "Would fail trivially (which is tested by BinderRpc::SendFiles)"; |
| 897 | } |
| 898 | |
| 899 | auto proc = createRpcTestSocketServerProcess({ |
| 900 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 901 | .serverSupportedFileDescriptorTransportModes = |
| 902 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 903 | }); |
| 904 | |
| 905 | std::vector<android::os::ParcelFileDescriptor> files; |
| 906 | for (int i = 0; i < 254; i++) { |
| 907 | files.emplace_back(android::os::ParcelFileDescriptor(mockFileDescriptor("a"))); |
| 908 | } |
| 909 | |
| 910 | android::os::ParcelFileDescriptor out; |
| 911 | auto status = proc.rootIface->concatFiles(files, &out); |
| 912 | EXPECT_EQ(status.transactionError(), BAD_VALUE) << status; |
| 913 | } |
| 914 | |
Andrei Homescu | fc22150 | 2022-10-08 03:51:17 +0000 | [diff] [blame] | 915 | TEST_P(BinderRpc, AppendInvalidFd) { |
| 916 | auto proc = createRpcTestSocketServerProcess({ |
| 917 | .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX, |
| 918 | .serverSupportedFileDescriptorTransportModes = |
| 919 | {RpcSession::FileDescriptorTransportMode::UNIX}, |
| 920 | }); |
| 921 | |
| 922 | int badFd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0); |
| 923 | ASSERT_NE(badFd, -1); |
| 924 | |
| 925 | // Close the file descriptor so it becomes invalid for dup |
| 926 | close(badFd); |
| 927 | |
| 928 | Parcel p1; |
| 929 | p1.markForBinder(proc.rootBinder); |
| 930 | p1.writeInt32(3); |
| 931 | EXPECT_EQ(OK, p1.writeFileDescriptor(badFd, false)); |
| 932 | |
| 933 | Parcel pRaw; |
| 934 | pRaw.markForBinder(proc.rootBinder); |
| 935 | EXPECT_EQ(OK, pRaw.appendFrom(&p1, 0, p1.dataSize())); |
| 936 | |
| 937 | pRaw.setDataPosition(0); |
| 938 | EXPECT_EQ(3, pRaw.readInt32()); |
| 939 | ASSERT_EQ(-1, pRaw.readFileDescriptor()); |
| 940 | } |
| 941 | |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 942 | TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 943 | if constexpr (!kEnableSharedLibs) { |
| 944 | GTEST_SKIP() << "Test disabled because Binder was built as a static library"; |
| 945 | } |
| 946 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 947 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 948 | |
| 949 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 950 | ASSERT_NE(binder, nullptr); |
| 951 | |
| 952 | ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get())); |
| 953 | } |
| 954 | |
| 955 | TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 956 | if constexpr (!kEnableSharedLibs) { |
| 957 | GTEST_SKIP() << "Test disabled because Binder was built as a static library"; |
| 958 | } |
| 959 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 960 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 961 | |
| 962 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 963 | ASSERT_NE(binder, nullptr); |
| 964 | |
| 965 | auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder); |
| 966 | ASSERT_NE(ndkBinder, nullptr); |
| 967 | |
| 968 | std::string out; |
| 969 | ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out); |
| 970 | ASSERT_TRUE(status.isOk()) << status.getDescription(); |
| 971 | ASSERT_EQ("aoeuaoeu", out); |
| 972 | } |
| 973 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 974 | ssize_t countFds() { |
| 975 | DIR* dir = opendir("/proc/self/fd/"); |
| 976 | if (dir == nullptr) return -1; |
| 977 | ssize_t ret = 0; |
| 978 | dirent* ent; |
| 979 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 980 | closedir(dir); |
| 981 | return ret; |
| 982 | } |
| 983 | |
Andrei Homescu | a858b0e | 2022-08-01 23:43:09 +0000 | [diff] [blame] | 984 | TEST_P(BinderRpc, Fds) { |
| 985 | if (serverSingleThreaded()) { |
| 986 | GTEST_SKIP() << "This test requires multiple threads"; |
| 987 | } |
| 988 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 989 | ssize_t beforeFds = countFds(); |
| 990 | ASSERT_GE(beforeFds, 0); |
| 991 | { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 992 | auto proc = createRpcTestSocketServerProcess({.numThreads = 10}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 993 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 994 | } |
| 995 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 996 | } |
| 997 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 998 | static bool testSupportVsockLoopback() { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 999 | // We don't need to enable TLS to know if vsock is supported. |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1000 | unsigned int vsockPort = allocateVsockPort(); |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1001 | |
Andrei Homescu | 992a405 | 2022-06-28 21:26:18 +0000 | [diff] [blame] | 1002 | android::base::unique_fd serverFd( |
| 1003 | TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0))); |
| 1004 | LOG_ALWAYS_FATAL_IF(serverFd == -1, "Could not create socket: %s", strerror(errno)); |
| 1005 | |
| 1006 | sockaddr_vm serverAddr{ |
| 1007 | .svm_family = AF_VSOCK, |
| 1008 | .svm_port = vsockPort, |
| 1009 | .svm_cid = VMADDR_CID_ANY, |
| 1010 | }; |
| 1011 | int ret = TEMP_FAILURE_RETRY( |
| 1012 | bind(serverFd.get(), reinterpret_cast<sockaddr*>(&serverAddr), sizeof(serverAddr))); |
| 1013 | LOG_ALWAYS_FATAL_IF(0 != ret, "Could not bind socket to port %u: %s", vsockPort, |
| 1014 | strerror(errno)); |
| 1015 | |
| 1016 | ret = TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/)); |
| 1017 | LOG_ALWAYS_FATAL_IF(0 != ret, "Could not listen socket on port %u: %s", vsockPort, |
| 1018 | strerror(errno)); |
| 1019 | |
| 1020 | // Try to connect to the server using the VMADDR_CID_LOCAL cid |
| 1021 | // to see if the kernel supports it. It's safe to use a blocking |
| 1022 | // connect because vsock sockets have a 2 second connection timeout, |
| 1023 | // and they return ETIMEDOUT after that. |
| 1024 | android::base::unique_fd connectFd( |
| 1025 | TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0))); |
| 1026 | LOG_ALWAYS_FATAL_IF(connectFd == -1, "Could not create socket for port %u: %s", vsockPort, |
| 1027 | strerror(errno)); |
| 1028 | |
| 1029 | bool success = false; |
| 1030 | sockaddr_vm connectAddr{ |
| 1031 | .svm_family = AF_VSOCK, |
| 1032 | .svm_port = vsockPort, |
| 1033 | .svm_cid = VMADDR_CID_LOCAL, |
| 1034 | }; |
| 1035 | ret = TEMP_FAILURE_RETRY(connect(connectFd.get(), reinterpret_cast<sockaddr*>(&connectAddr), |
| 1036 | sizeof(connectAddr))); |
| 1037 | if (ret != 0 && (errno == EAGAIN || errno == EINPROGRESS)) { |
| 1038 | android::base::unique_fd acceptFd; |
| 1039 | while (true) { |
| 1040 | pollfd pfd[]{ |
| 1041 | {.fd = serverFd.get(), .events = POLLIN, .revents = 0}, |
| 1042 | {.fd = connectFd.get(), .events = POLLOUT, .revents = 0}, |
| 1043 | }; |
| 1044 | ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1)); |
| 1045 | LOG_ALWAYS_FATAL_IF(ret < 0, "Error polling: %s", strerror(errno)); |
| 1046 | |
| 1047 | if (pfd[0].revents & POLLIN) { |
| 1048 | sockaddr_vm acceptAddr; |
| 1049 | socklen_t acceptAddrLen = sizeof(acceptAddr); |
| 1050 | ret = TEMP_FAILURE_RETRY(accept4(serverFd.get(), |
| 1051 | reinterpret_cast<sockaddr*>(&acceptAddr), |
| 1052 | &acceptAddrLen, SOCK_CLOEXEC)); |
| 1053 | LOG_ALWAYS_FATAL_IF(ret < 0, "Could not accept4 socket: %s", strerror(errno)); |
| 1054 | LOG_ALWAYS_FATAL_IF(acceptAddrLen != static_cast<socklen_t>(sizeof(acceptAddr)), |
| 1055 | "Truncated address"); |
| 1056 | |
| 1057 | // Store the fd in acceptFd so we keep the connection alive |
| 1058 | // while polling connectFd |
| 1059 | acceptFd.reset(ret); |
| 1060 | } |
| 1061 | |
| 1062 | if (pfd[1].revents & POLLOUT) { |
| 1063 | // Connect either succeeded or timed out |
| 1064 | int connectErrno; |
| 1065 | socklen_t connectErrnoLen = sizeof(connectErrno); |
| 1066 | int ret = getsockopt(connectFd.get(), SOL_SOCKET, SO_ERROR, &connectErrno, |
| 1067 | &connectErrnoLen); |
| 1068 | LOG_ALWAYS_FATAL_IF(ret == -1, |
| 1069 | "Could not getsockopt() after connect() " |
| 1070 | "on non-blocking socket: %s.", |
| 1071 | strerror(errno)); |
| 1072 | |
| 1073 | // We're done, this is all we wanted |
| 1074 | success = connectErrno == 0; |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | } else { |
| 1079 | success = ret == 0; |
| 1080 | } |
| 1081 | |
| 1082 | ALOGE("Detected vsock loopback supported: %s", success ? "yes" : "no"); |
| 1083 | |
| 1084 | return success; |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1087 | static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) { |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 1088 | std::vector<SocketType> ret = {SocketType::UNIX, SocketType::UNIX_BOOTSTRAP, SocketType::INET, |
| 1089 | SocketType::UNIX_RAW}; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1090 | |
| 1091 | if (hasPreconnected) ret.push_back(SocketType::PRECONNECTED); |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1092 | |
| 1093 | static bool hasVsockLoopback = testSupportVsockLoopback(); |
| 1094 | |
| 1095 | if (hasVsockLoopback) { |
| 1096 | ret.push_back(SocketType::VSOCK); |
| 1097 | } |
| 1098 | |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1102 | static std::vector<uint32_t> testVersions() { |
| 1103 | std::vector<uint32_t> versions; |
| 1104 | for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { |
| 1105 | versions.push_back(i); |
| 1106 | } |
| 1107 | versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); |
| 1108 | return versions; |
| 1109 | } |
| 1110 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1111 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
| 1112 | ::testing::Combine(::testing::ValuesIn(testSocketTypes()), |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1113 | ::testing::ValuesIn(RpcSecurityValues()), |
| 1114 | ::testing::ValuesIn(testVersions()), |
Andrei Homescu | 2a29801 | 2022-06-15 01:08:54 +0000 | [diff] [blame] | 1115 | ::testing::ValuesIn(testVersions()), |
| 1116 | ::testing::Values(false, true), |
| 1117 | ::testing::Values(false, true)), |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1118 | BinderRpc::PrintParamInfo); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1119 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1120 | class BinderRpcServerRootObject |
| 1121 | : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {}; |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1122 | |
| 1123 | TEST_P(BinderRpcServerRootObject, WeakRootObject) { |
| 1124 | using SetFn = std::function<void(RpcServer*, sp<IBinder>)>; |
| 1125 | auto setRootObject = [](bool isStrong) -> SetFn { |
| 1126 | return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak); |
| 1127 | }; |
| 1128 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1129 | auto [isStrong1, isStrong2, rpcSecurity] = GetParam(); |
| 1130 | auto server = RpcServer::make(newFactory(rpcSecurity)); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1131 | auto binder1 = sp<BBinder>::make(); |
| 1132 | IBinder* binderRaw1 = binder1.get(); |
| 1133 | setRootObject(isStrong1)(server.get(), binder1); |
| 1134 | EXPECT_EQ(binderRaw1, server->getRootObject()); |
| 1135 | binder1.clear(); |
| 1136 | EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject()); |
| 1137 | |
| 1138 | auto binder2 = sp<BBinder>::make(); |
| 1139 | IBinder* binderRaw2 = binder2.get(); |
| 1140 | setRootObject(isStrong2)(server.get(), binder2); |
| 1141 | EXPECT_EQ(binderRaw2, server->getRootObject()); |
| 1142 | binder2.clear(); |
| 1143 | EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject()); |
| 1144 | } |
| 1145 | |
| 1146 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject, |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1147 | ::testing::Combine(::testing::Bool(), ::testing::Bool(), |
| 1148 | ::testing::ValuesIn(RpcSecurityValues()))); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1149 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1150 | class OneOffSignal { |
| 1151 | public: |
| 1152 | // If notify() was previously called, or is called within |duration|, return true; else false. |
| 1153 | template <typename R, typename P> |
| 1154 | bool wait(std::chrono::duration<R, P> duration) { |
| 1155 | std::unique_lock<std::mutex> lock(mMutex); |
| 1156 | return mCv.wait_for(lock, duration, [this] { return mValue; }); |
| 1157 | } |
| 1158 | void notify() { |
| 1159 | std::unique_lock<std::mutex> lock(mMutex); |
| 1160 | mValue = true; |
| 1161 | lock.unlock(); |
| 1162 | mCv.notify_all(); |
| 1163 | } |
| 1164 | |
| 1165 | private: |
| 1166 | std::mutex mMutex; |
| 1167 | std::condition_variable mCv; |
| 1168 | bool mValue = false; |
| 1169 | }; |
| 1170 | |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1171 | TEST(BinderRpc, Java) { |
| 1172 | #if !defined(__ANDROID__) |
| 1173 | GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on" |
| 1174 | "createRpcDelegateServiceManager() with a device attached, such test belongs " |
| 1175 | "to binderHostDeviceTest. Hence, just disable this test on host."; |
| 1176 | #endif // !__ANDROID__ |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 1177 | if constexpr (!kEnableKernelIpc) { |
| 1178 | GTEST_SKIP() << "Test disabled because Binder kernel driver was disabled " |
| 1179 | "at build time."; |
| 1180 | } |
| 1181 | |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1182 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1183 | ASSERT_NE(nullptr, sm); |
| 1184 | // Any Java service with non-empty getInterfaceDescriptor() would do. |
| 1185 | // Let's pick batteryproperties. |
| 1186 | auto binder = sm->checkService(String16("batteryproperties")); |
| 1187 | ASSERT_NE(nullptr, binder); |
| 1188 | auto descriptor = binder->getInterfaceDescriptor(); |
| 1189 | ASSERT_GE(descriptor.size(), 0); |
| 1190 | ASSERT_EQ(OK, binder->pingBinder()); |
| 1191 | |
| 1192 | auto rpcServer = RpcServer::make(); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1193 | unsigned int port; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1194 | ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port)); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1195 | auto socket = rpcServer->releaseServer(); |
| 1196 | |
| 1197 | auto keepAlive = sp<BBinder>::make(); |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 1198 | auto setRpcClientDebugStatus = binder->setRpcClientDebug(std::move(socket), keepAlive); |
| 1199 | |
Yifan Hong | e3caaf2 | 2022-01-12 14:46:56 -0800 | [diff] [blame] | 1200 | if (!android::base::GetBoolProperty("ro.debuggable", false) || |
| 1201 | android::base::GetProperty("ro.build.type", "") == "user") { |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 1202 | ASSERT_EQ(INVALID_OPERATION, setRpcClientDebugStatus) |
Yifan Hong | e3caaf2 | 2022-01-12 14:46:56 -0800 | [diff] [blame] | 1203 | << "setRpcClientDebug should return INVALID_OPERATION on non-debuggable or user " |
| 1204 | "builds, but get " |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 1205 | << statusToString(setRpcClientDebugStatus); |
| 1206 | GTEST_SKIP(); |
| 1207 | } |
| 1208 | |
| 1209 | ASSERT_EQ(OK, setRpcClientDebugStatus); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1210 | |
| 1211 | auto rpcSession = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1212 | ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port)); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1213 | auto rpcBinder = rpcSession->getRootObject(); |
| 1214 | ASSERT_NE(nullptr, rpcBinder); |
| 1215 | |
| 1216 | ASSERT_EQ(OK, rpcBinder->pingBinder()); |
| 1217 | |
| 1218 | ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor()) |
| 1219 | << "getInterfaceDescriptor should not crash system_server"; |
| 1220 | ASSERT_EQ(OK, rpcBinder->pingBinder()); |
| 1221 | } |
| 1222 | |
Andrei Homescu | 8d7f4bd | 2022-08-03 05:46:17 +0000 | [diff] [blame] | 1223 | class BinderRpcServerOnly : public ::testing::TestWithParam<std::tuple<RpcSecurity, uint32_t>> { |
| 1224 | public: |
| 1225 | static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) { |
| 1226 | return std::string(newFactory(std::get<0>(info.param))->toCString()) + "_serverV" + |
| 1227 | std::to_string(std::get<1>(info.param)); |
| 1228 | } |
| 1229 | }; |
| 1230 | |
| 1231 | TEST_P(BinderRpcServerOnly, SetExternalServerTest) { |
| 1232 | base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 1233 | int sinkFd = sink.get(); |
| 1234 | auto server = RpcServer::make(newFactory(std::get<0>(GetParam()))); |
| 1235 | server->setProtocolVersion(std::get<1>(GetParam())); |
| 1236 | ASSERT_FALSE(server->hasServer()); |
| 1237 | ASSERT_EQ(OK, server->setupExternalServer(std::move(sink))); |
| 1238 | ASSERT_TRUE(server->hasServer()); |
| 1239 | base::unique_fd retrieved = server->releaseServer(); |
| 1240 | ASSERT_FALSE(server->hasServer()); |
| 1241 | ASSERT_EQ(sinkFd, retrieved.get()); |
| 1242 | } |
| 1243 | |
| 1244 | TEST_P(BinderRpcServerOnly, Shutdown) { |
| 1245 | if constexpr (!kEnableRpcThreads) { |
| 1246 | GTEST_SKIP() << "Test skipped because threads were disabled at build time"; |
| 1247 | } |
| 1248 | |
| 1249 | auto addr = allocateSocketAddress(); |
| 1250 | auto server = RpcServer::make(newFactory(std::get<0>(GetParam()))); |
| 1251 | server->setProtocolVersion(std::get<1>(GetParam())); |
| 1252 | ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str())); |
| 1253 | auto joinEnds = std::make_shared<OneOffSignal>(); |
| 1254 | |
| 1255 | // If things are broken and the thread never stops, don't block other tests. Because the thread |
| 1256 | // may run after the test finishes, it must not access the stack memory of the test. Hence, |
| 1257 | // shared pointers are passed. |
| 1258 | std::thread([server, joinEnds] { |
| 1259 | server->join(); |
| 1260 | joinEnds->notify(); |
| 1261 | }).detach(); |
| 1262 | |
| 1263 | bool shutdown = false; |
| 1264 | for (int i = 0; i < 10 && !shutdown; i++) { |
Steven Moreland | dd231e2 | 2022-09-08 19:47:49 +0000 | [diff] [blame] | 1265 | usleep(30 * 1000); // 30ms; total 300ms |
Andrei Homescu | 8d7f4bd | 2022-08-03 05:46:17 +0000 | [diff] [blame] | 1266 | if (server->shutdown()) shutdown = true; |
| 1267 | } |
| 1268 | ASSERT_TRUE(shutdown) << "server->shutdown() never returns true"; |
| 1269 | |
| 1270 | ASSERT_TRUE(joinEnds->wait(2s)) |
| 1271 | << "After server->shutdown() returns true, join() did not stop after 2s"; |
| 1272 | } |
| 1273 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1274 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerOnly, |
| 1275 | ::testing::Combine(::testing::ValuesIn(RpcSecurityValues()), |
| 1276 | ::testing::ValuesIn(testVersions())), |
| 1277 | BinderRpcServerOnly::PrintTestParam); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1278 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1279 | class RpcTransportTestUtils { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1280 | public: |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1281 | // Only parameterized only server version because `RpcSession` is bypassed |
| 1282 | // in the client half of the tests. |
| 1283 | using Param = |
| 1284 | std::tuple<SocketType, RpcSecurity, std::optional<RpcCertificateFormat>, uint32_t>; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1285 | using ConnectToServer = std::function<base::unique_fd()>; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1286 | |
| 1287 | // A server that handles client socket connections. |
| 1288 | class Server { |
| 1289 | public: |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 1290 | using AcceptConnection = std::function<base::unique_fd(Server*)>; |
| 1291 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1292 | explicit Server() {} |
| 1293 | Server(Server&&) = default; |
Yifan Hong | e07d273 | 2021-09-13 21:59:14 -0700 | [diff] [blame] | 1294 | ~Server() { shutdownAndWait(); } |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1295 | [[nodiscard]] AssertionResult setUp( |
| 1296 | const Param& param, |
| 1297 | std::unique_ptr<RpcAuth> auth = std::make_unique<RpcAuthSelfSigned>()) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1298 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1299 | auto rpcServer = RpcServer::make(newFactory(rpcSecurity)); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1300 | rpcServer->setProtocolVersion(serverVersion); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1301 | switch (socketType) { |
| 1302 | case SocketType::PRECONNECTED: { |
| 1303 | return AssertionFailure() << "Not supported by this test"; |
| 1304 | } break; |
| 1305 | case SocketType::UNIX: { |
| 1306 | auto addr = allocateSocketAddress(); |
| 1307 | auto status = rpcServer->setupUnixDomainServer(addr.c_str()); |
| 1308 | if (status != OK) { |
| 1309 | return AssertionFailure() |
| 1310 | << "setupUnixDomainServer: " << statusToString(status); |
| 1311 | } |
| 1312 | mConnectToServer = [addr] { |
| 1313 | return connectTo(UnixSocketAddress(addr.c_str())); |
| 1314 | }; |
| 1315 | } break; |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 1316 | case SocketType::UNIX_BOOTSTRAP: { |
| 1317 | base::unique_fd bootstrapFdClient, bootstrapFdServer; |
| 1318 | if (!base::Socketpair(SOCK_STREAM, &bootstrapFdClient, &bootstrapFdServer)) { |
| 1319 | return AssertionFailure() << "Socketpair() failed"; |
| 1320 | } |
| 1321 | auto status = rpcServer->setupUnixDomainSocketBootstrapServer( |
| 1322 | std::move(bootstrapFdServer)); |
| 1323 | if (status != OK) { |
| 1324 | return AssertionFailure() << "setupUnixDomainSocketBootstrapServer: " |
| 1325 | << statusToString(status); |
| 1326 | } |
| 1327 | mBootstrapSocket = RpcTransportFd(std::move(bootstrapFdClient)); |
| 1328 | mAcceptConnection = &Server::recvmsgServerConnection; |
| 1329 | mConnectToServer = [this] { return connectToUnixBootstrap(mBootstrapSocket); }; |
| 1330 | } break; |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 1331 | case SocketType::UNIX_RAW: { |
| 1332 | auto addr = allocateSocketAddress(); |
| 1333 | auto status = rpcServer->setupRawSocketServer(initUnixSocket(addr)); |
| 1334 | if (status != OK) { |
| 1335 | return AssertionFailure() |
| 1336 | << "setupRawSocketServer: " << statusToString(status); |
| 1337 | } |
| 1338 | mConnectToServer = [addr] { |
| 1339 | return connectTo(UnixSocketAddress(addr.c_str())); |
| 1340 | }; |
| 1341 | } break; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1342 | case SocketType::VSOCK: { |
| 1343 | auto port = allocateVsockPort(); |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame] | 1344 | auto status = rpcServer->setupVsockServer(VMADDR_CID_LOCAL, port); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1345 | if (status != OK) { |
| 1346 | return AssertionFailure() << "setupVsockServer: " << statusToString(status); |
| 1347 | } |
| 1348 | mConnectToServer = [port] { |
| 1349 | return connectTo(VsockSocketAddress(VMADDR_CID_LOCAL, port)); |
| 1350 | }; |
| 1351 | } break; |
| 1352 | case SocketType::INET: { |
| 1353 | unsigned int port; |
| 1354 | auto status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port); |
| 1355 | if (status != OK) { |
| 1356 | return AssertionFailure() << "setupInetServer: " << statusToString(status); |
| 1357 | } |
| 1358 | mConnectToServer = [port] { |
| 1359 | const char* addr = kLocalInetAddress; |
| 1360 | auto aiStart = InetSocketAddress::getAddrInfo(addr, port); |
| 1361 | if (aiStart == nullptr) return base::unique_fd{}; |
| 1362 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 1363 | auto fd = connectTo( |
| 1364 | InetSocketAddress(ai->ai_addr, ai->ai_addrlen, addr, port)); |
| 1365 | if (fd.ok()) return fd; |
| 1366 | } |
| 1367 | ALOGE("None of the socket address resolved for %s:%u can be connected", |
| 1368 | addr, port); |
| 1369 | return base::unique_fd{}; |
| 1370 | }; |
| 1371 | } |
| 1372 | } |
| 1373 | mFd = rpcServer->releaseServer(); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 1374 | if (!mFd.fd.ok()) return AssertionFailure() << "releaseServer returns invalid fd"; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1375 | mCtx = newFactory(rpcSecurity, mCertVerifier, std::move(auth))->newServerCtx(); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1376 | if (mCtx == nullptr) return AssertionFailure() << "newServerCtx"; |
| 1377 | mSetup = true; |
| 1378 | return AssertionSuccess(); |
| 1379 | } |
| 1380 | RpcTransportCtx* getCtx() const { return mCtx.get(); } |
| 1381 | std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const { |
| 1382 | return mCertVerifier; |
| 1383 | } |
| 1384 | ConnectToServer getConnectToServerFn() { return mConnectToServer; } |
| 1385 | void start() { |
| 1386 | LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!"); |
| 1387 | mThread = std::make_unique<std::thread>(&Server::run, this); |
| 1388 | } |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 1389 | |
| 1390 | base::unique_fd acceptServerConnection() { |
| 1391 | return base::unique_fd(TEMP_FAILURE_RETRY( |
| 1392 | accept4(mFd.fd.get(), nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK))); |
| 1393 | } |
| 1394 | |
| 1395 | base::unique_fd recvmsgServerConnection() { |
| 1396 | std::vector<std::variant<base::unique_fd, base::borrowed_fd>> fds; |
| 1397 | int buf; |
| 1398 | iovec iov{&buf, sizeof(buf)}; |
| 1399 | |
| 1400 | if (receiveMessageFromSocket(mFd, &iov, 1, &fds) < 0) { |
| 1401 | int savedErrno = errno; |
| 1402 | LOG(FATAL) << "Failed receiveMessage: " << strerror(savedErrno); |
| 1403 | } |
| 1404 | if (fds.size() != 1) { |
| 1405 | LOG(FATAL) << "Expected one FD from receiveMessage(), got " << fds.size(); |
| 1406 | } |
| 1407 | return std::move(std::get<base::unique_fd>(fds[0])); |
| 1408 | } |
| 1409 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1410 | void run() { |
| 1411 | LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!"); |
| 1412 | |
| 1413 | std::vector<std::thread> threads; |
| 1414 | while (OK == mFdTrigger->triggerablePoll(mFd, POLLIN)) { |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 1415 | base::unique_fd acceptedFd = mAcceptConnection(this); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1416 | threads.emplace_back(&Server::handleOne, this, std::move(acceptedFd)); |
| 1417 | } |
| 1418 | |
| 1419 | for (auto& thread : threads) thread.join(); |
| 1420 | } |
| 1421 | void handleOne(android::base::unique_fd acceptedFd) { |
| 1422 | ASSERT_TRUE(acceptedFd.ok()); |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 1423 | RpcTransportFd transportFd(std::move(acceptedFd)); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 1424 | auto serverTransport = mCtx->newTransport(std::move(transportFd), mFdTrigger.get()); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1425 | if (serverTransport == nullptr) return; // handshake failed |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1426 | ASSERT_TRUE(mPostConnect(serverTransport.get(), mFdTrigger.get())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1427 | } |
Yifan Hong | e07d273 | 2021-09-13 21:59:14 -0700 | [diff] [blame] | 1428 | void shutdownAndWait() { |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1429 | shutdown(); |
| 1430 | join(); |
| 1431 | } |
| 1432 | void shutdown() { mFdTrigger->trigger(); } |
| 1433 | |
| 1434 | void setPostConnect( |
| 1435 | std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> fn) { |
| 1436 | mPostConnect = std::move(fn); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | private: |
| 1440 | std::unique_ptr<std::thread> mThread; |
| 1441 | ConnectToServer mConnectToServer; |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 1442 | AcceptConnection mAcceptConnection = &Server::acceptServerConnection; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1443 | std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make(); |
David Brazdil | 21c887c | 2022-09-23 12:25:18 +0100 | [diff] [blame] | 1444 | RpcTransportFd mFd, mBootstrapSocket; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1445 | std::unique_ptr<RpcTransportCtx> mCtx; |
| 1446 | std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier = |
| 1447 | std::make_shared<RpcCertificateVerifierSimple>(); |
| 1448 | bool mSetup = false; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1449 | // The function invoked after connection and handshake. By default, it is |
| 1450 | // |defaultPostConnect| that sends |kMessage| to the client. |
| 1451 | std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> mPostConnect = |
| 1452 | Server::defaultPostConnect; |
| 1453 | |
| 1454 | void join() { |
| 1455 | if (mThread != nullptr) { |
| 1456 | mThread->join(); |
| 1457 | mThread = nullptr; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | static AssertionResult defaultPostConnect(RpcTransport* serverTransport, |
| 1462 | FdTrigger* fdTrigger) { |
| 1463 | std::string message(kMessage); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1464 | iovec messageIov{message.data(), message.size()}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 1465 | auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1, |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1466 | std::nullopt, nullptr); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1467 | if (status != OK) return AssertionFailure() << statusToString(status); |
| 1468 | return AssertionSuccess(); |
| 1469 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1470 | }; |
| 1471 | |
| 1472 | class Client { |
| 1473 | public: |
| 1474 | explicit Client(ConnectToServer connectToServer) : mConnectToServer(connectToServer) {} |
| 1475 | Client(Client&&) = default; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1476 | [[nodiscard]] AssertionResult setUp(const Param& param) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1477 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param; |
| 1478 | (void)serverVersion; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1479 | mFdTrigger = FdTrigger::make(); |
| 1480 | mCtx = newFactory(rpcSecurity, mCertVerifier)->newClientCtx(); |
| 1481 | if (mCtx == nullptr) return AssertionFailure() << "newClientCtx"; |
| 1482 | return AssertionSuccess(); |
| 1483 | } |
| 1484 | RpcTransportCtx* getCtx() const { return mCtx.get(); } |
| 1485 | std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const { |
| 1486 | return mCertVerifier; |
| 1487 | } |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1488 | // connect() and do handshake |
| 1489 | bool setUpTransport() { |
| 1490 | mFd = mConnectToServer(); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 1491 | if (!mFd.fd.ok()) return AssertionFailure() << "Cannot connect to server"; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1492 | mClientTransport = mCtx->newTransport(std::move(mFd), mFdTrigger.get()); |
| 1493 | return mClientTransport != nullptr; |
| 1494 | } |
| 1495 | AssertionResult readMessage(const std::string& expectedMessage = kMessage) { |
| 1496 | LOG_ALWAYS_FATAL_IF(mClientTransport == nullptr, "setUpTransport not called or failed"); |
| 1497 | std::string readMessage(expectedMessage.size(), '\0'); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1498 | iovec readMessageIov{readMessage.data(), readMessage.size()}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 1499 | status_t readStatus = |
| 1500 | mClientTransport->interruptableReadFully(mFdTrigger.get(), &readMessageIov, 1, |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 1501 | std::nullopt, nullptr); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1502 | if (readStatus != OK) { |
| 1503 | return AssertionFailure() << statusToString(readStatus); |
| 1504 | } |
| 1505 | if (readMessage != expectedMessage) { |
| 1506 | return AssertionFailure() |
| 1507 | << "Expected " << expectedMessage << ", actual " << readMessage; |
| 1508 | } |
| 1509 | return AssertionSuccess(); |
| 1510 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1511 | void run(bool handshakeOk = true, bool readOk = true) { |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1512 | if (!setUpTransport()) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1513 | ASSERT_FALSE(handshakeOk) << "newTransport returns nullptr, but it shouldn't"; |
| 1514 | return; |
| 1515 | } |
| 1516 | ASSERT_TRUE(handshakeOk) << "newTransport does not return nullptr, but it should"; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1517 | ASSERT_EQ(readOk, readMessage()); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 1520 | bool isTransportWaiting() { return mClientTransport->isWaiting(); } |
| 1521 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1522 | private: |
| 1523 | ConnectToServer mConnectToServer; |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 1524 | RpcTransportFd mFd; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1525 | std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make(); |
| 1526 | std::unique_ptr<RpcTransportCtx> mCtx; |
| 1527 | std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier = |
| 1528 | std::make_shared<RpcCertificateVerifierSimple>(); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1529 | std::unique_ptr<RpcTransport> mClientTransport; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1530 | }; |
| 1531 | |
| 1532 | // Make A trust B. |
| 1533 | template <typename A, typename B> |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1534 | static status_t trust(RpcSecurity rpcSecurity, |
| 1535 | std::optional<RpcCertificateFormat> certificateFormat, const A& a, |
| 1536 | const B& b) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1537 | if (rpcSecurity != RpcSecurity::TLS) return OK; |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 1538 | LOG_ALWAYS_FATAL_IF(!certificateFormat.has_value()); |
| 1539 | auto bCert = b->getCtx()->getCertificate(*certificateFormat); |
| 1540 | return a->getCertVerifier()->addTrustedPeerCertificate(*certificateFormat, bCert); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | static constexpr const char* kMessage = "hello"; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1544 | }; |
| 1545 | |
| 1546 | class RpcTransportTest : public testing::TestWithParam<RpcTransportTestUtils::Param> { |
| 1547 | public: |
| 1548 | using Server = RpcTransportTestUtils::Server; |
| 1549 | using Client = RpcTransportTestUtils::Client; |
| 1550 | static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1551 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = info.param; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1552 | auto ret = PrintToString(socketType) + "_" + newFactory(rpcSecurity)->toCString(); |
| 1553 | if (certificateFormat.has_value()) ret += "_" + PrintToString(*certificateFormat); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1554 | ret += "_serverV" + std::to_string(serverVersion); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1555 | return ret; |
| 1556 | } |
| 1557 | static std::vector<ParamType> getRpcTranportTestParams() { |
| 1558 | std::vector<ParamType> ret; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1559 | for (auto serverVersion : testVersions()) { |
| 1560 | for (auto socketType : testSocketTypes(false /* hasPreconnected */)) { |
| 1561 | for (auto rpcSecurity : RpcSecurityValues()) { |
| 1562 | switch (rpcSecurity) { |
| 1563 | case RpcSecurity::RAW: { |
| 1564 | ret.emplace_back(socketType, rpcSecurity, std::nullopt, serverVersion); |
| 1565 | } break; |
| 1566 | case RpcSecurity::TLS: { |
| 1567 | ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::PEM, |
| 1568 | serverVersion); |
| 1569 | ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::DER, |
| 1570 | serverVersion); |
| 1571 | } break; |
| 1572 | } |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | return ret; |
| 1577 | } |
| 1578 | template <typename A, typename B> |
| 1579 | status_t trust(const A& a, const B& b) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1580 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1581 | (void)serverVersion; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1582 | return RpcTransportTestUtils::trust(rpcSecurity, certificateFormat, a, b); |
| 1583 | } |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 1584 | void SetUp() override { |
| 1585 | if constexpr (!kEnableRpcThreads) { |
| 1586 | GTEST_SKIP() << "Test skipped because threads were disabled at build time"; |
| 1587 | } |
| 1588 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1589 | }; |
| 1590 | |
| 1591 | TEST_P(RpcTransportTest, GoodCertificate) { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1592 | auto server = std::make_unique<Server>(); |
| 1593 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1594 | |
| 1595 | Client client(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1596 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1597 | |
| 1598 | ASSERT_EQ(OK, trust(&client, server)); |
| 1599 | ASSERT_EQ(OK, trust(server, &client)); |
| 1600 | |
| 1601 | server->start(); |
| 1602 | client.run(); |
| 1603 | } |
| 1604 | |
| 1605 | TEST_P(RpcTransportTest, MultipleClients) { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1606 | auto server = std::make_unique<Server>(); |
| 1607 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1608 | |
| 1609 | std::vector<Client> clients; |
| 1610 | for (int i = 0; i < 2; i++) { |
| 1611 | auto& client = clients.emplace_back(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1612 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1613 | ASSERT_EQ(OK, trust(&client, server)); |
| 1614 | ASSERT_EQ(OK, trust(server, &client)); |
| 1615 | } |
| 1616 | |
| 1617 | server->start(); |
| 1618 | for (auto& client : clients) client.run(); |
| 1619 | } |
| 1620 | |
| 1621 | TEST_P(RpcTransportTest, UntrustedServer) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1622 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1623 | (void)serverVersion; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1624 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1625 | auto untrustedServer = std::make_unique<Server>(); |
| 1626 | ASSERT_TRUE(untrustedServer->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1627 | |
| 1628 | Client client(untrustedServer->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1629 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1630 | |
| 1631 | ASSERT_EQ(OK, trust(untrustedServer, &client)); |
| 1632 | |
| 1633 | untrustedServer->start(); |
| 1634 | |
| 1635 | // For TLS, this should reject the certificate. For RAW sockets, it should pass because |
| 1636 | // the client can't verify the server's identity. |
| 1637 | bool handshakeOk = rpcSecurity != RpcSecurity::TLS; |
| 1638 | client.run(handshakeOk); |
| 1639 | } |
| 1640 | TEST_P(RpcTransportTest, MaliciousServer) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1641 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1642 | (void)serverVersion; |
| 1643 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1644 | auto validServer = std::make_unique<Server>(); |
| 1645 | ASSERT_TRUE(validServer->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1646 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1647 | auto maliciousServer = std::make_unique<Server>(); |
| 1648 | ASSERT_TRUE(maliciousServer->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1649 | |
| 1650 | Client client(maliciousServer->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1651 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1652 | |
| 1653 | ASSERT_EQ(OK, trust(&client, validServer)); |
| 1654 | ASSERT_EQ(OK, trust(validServer, &client)); |
| 1655 | ASSERT_EQ(OK, trust(maliciousServer, &client)); |
| 1656 | |
| 1657 | maliciousServer->start(); |
| 1658 | |
| 1659 | // For TLS, this should reject the certificate. For RAW sockets, it should pass because |
| 1660 | // the client can't verify the server's identity. |
| 1661 | bool handshakeOk = rpcSecurity != RpcSecurity::TLS; |
| 1662 | client.run(handshakeOk); |
| 1663 | } |
| 1664 | |
| 1665 | TEST_P(RpcTransportTest, UntrustedClient) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1666 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1667 | (void)serverVersion; |
| 1668 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1669 | auto server = std::make_unique<Server>(); |
| 1670 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1671 | |
| 1672 | Client client(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1673 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1674 | |
| 1675 | ASSERT_EQ(OK, trust(&client, server)); |
| 1676 | |
| 1677 | server->start(); |
| 1678 | |
| 1679 | // For TLS, Client should be able to verify server's identity, so client should see |
| 1680 | // do_handshake() successfully executed. However, server shouldn't be able to verify client's |
| 1681 | // identity and should drop the connection, so client shouldn't be able to read anything. |
| 1682 | bool readOk = rpcSecurity != RpcSecurity::TLS; |
| 1683 | client.run(true, readOk); |
| 1684 | } |
| 1685 | |
| 1686 | TEST_P(RpcTransportTest, MaliciousClient) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1687 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1688 | (void)serverVersion; |
| 1689 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1690 | auto server = std::make_unique<Server>(); |
| 1691 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1692 | |
| 1693 | Client validClient(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1694 | ASSERT_TRUE(validClient.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1695 | Client maliciousClient(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1696 | ASSERT_TRUE(maliciousClient.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1697 | |
| 1698 | ASSERT_EQ(OK, trust(&validClient, server)); |
| 1699 | ASSERT_EQ(OK, trust(&maliciousClient, server)); |
| 1700 | |
| 1701 | server->start(); |
| 1702 | |
| 1703 | // See UntrustedClient. |
| 1704 | bool readOk = rpcSecurity != RpcSecurity::TLS; |
| 1705 | maliciousClient.run(true, readOk); |
| 1706 | } |
| 1707 | |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1708 | TEST_P(RpcTransportTest, Trigger) { |
| 1709 | std::string msg2 = ", world!"; |
| 1710 | std::mutex writeMutex; |
| 1711 | std::condition_variable writeCv; |
| 1712 | bool shouldContinueWriting = false; |
| 1713 | auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1714 | std::string message(RpcTransportTestUtils::kMessage); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1715 | iovec messageIov{message.data(), message.size()}; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1716 | auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1, |
| 1717 | std::nullopt, nullptr); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1718 | if (status != OK) return AssertionFailure() << statusToString(status); |
| 1719 | |
| 1720 | { |
| 1721 | std::unique_lock<std::mutex> lock(writeMutex); |
| 1722 | if (!writeCv.wait_for(lock, 3s, [&] { return shouldContinueWriting; })) { |
| 1723 | return AssertionFailure() << "write barrier not cleared in time!"; |
| 1724 | } |
| 1725 | } |
| 1726 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1727 | iovec msg2Iov{msg2.data(), msg2.size()}; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 1728 | status = serverTransport->interruptableWriteFully(fdTrigger, &msg2Iov, 1, std::nullopt, |
| 1729 | nullptr); |
Steven Moreland | c591b47 | 2021-09-16 13:56:11 -0700 | [diff] [blame] | 1730 | if (status != DEAD_OBJECT) |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1731 | return AssertionFailure() << "When FdTrigger is shut down, interruptableWriteFully " |
Steven Moreland | c591b47 | 2021-09-16 13:56:11 -0700 | [diff] [blame] | 1732 | "should return DEAD_OBJECT, but it is " |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1733 | << statusToString(status); |
| 1734 | return AssertionSuccess(); |
| 1735 | }; |
| 1736 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1737 | auto server = std::make_unique<Server>(); |
| 1738 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1739 | |
| 1740 | // Set up client |
| 1741 | Client client(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1742 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1743 | |
| 1744 | // Exchange keys |
| 1745 | ASSERT_EQ(OK, trust(&client, server)); |
| 1746 | ASSERT_EQ(OK, trust(server, &client)); |
| 1747 | |
| 1748 | server->setPostConnect(serverPostConnect); |
| 1749 | |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1750 | server->start(); |
| 1751 | // connect() to server and do handshake |
| 1752 | ASSERT_TRUE(client.setUpTransport()); |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 1753 | // read the first message. This ensures that server has finished handshake and start handling |
| 1754 | // client fd. Server thread should pause at writeCv.wait_for(). |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1755 | ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage)); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1756 | // Trigger server shutdown after server starts handling client FD. This ensures that the second |
| 1757 | // write is on an FdTrigger that has been shut down. |
| 1758 | server->shutdown(); |
| 1759 | // Continues server thread to write the second message. |
| 1760 | { |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 1761 | std::lock_guard<std::mutex> lock(writeMutex); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1762 | shouldContinueWriting = true; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1763 | } |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 1764 | writeCv.notify_all(); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1765 | // After this line, server thread unblocks and attempts to write the second message, but |
Steven Moreland | c591b47 | 2021-09-16 13:56:11 -0700 | [diff] [blame] | 1766 | // shutdown is triggered, so write should failed with DEAD_OBJECT. See |serverPostConnect|. |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1767 | // On the client side, second read fails with DEAD_OBJECT |
| 1768 | ASSERT_FALSE(client.readMessage(msg2)); |
| 1769 | } |
| 1770 | |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 1771 | TEST_P(RpcTransportTest, CheckWaitingForRead) { |
| 1772 | std::mutex readMutex; |
| 1773 | std::condition_variable readCv; |
| 1774 | bool shouldContinueReading = false; |
| 1775 | // Server will write data on transport once its started |
| 1776 | auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) { |
| 1777 | std::string message(RpcTransportTestUtils::kMessage); |
| 1778 | iovec messageIov{message.data(), message.size()}; |
| 1779 | auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1, |
| 1780 | std::nullopt, nullptr); |
| 1781 | if (status != OK) return AssertionFailure() << statusToString(status); |
| 1782 | |
| 1783 | { |
| 1784 | std::unique_lock<std::mutex> lock(readMutex); |
| 1785 | shouldContinueReading = true; |
| 1786 | lock.unlock(); |
| 1787 | readCv.notify_all(); |
| 1788 | } |
| 1789 | return AssertionSuccess(); |
| 1790 | }; |
| 1791 | |
| 1792 | // Setup Server and client |
| 1793 | auto server = std::make_unique<Server>(); |
| 1794 | ASSERT_TRUE(server->setUp(GetParam())); |
| 1795 | |
| 1796 | Client client(server->getConnectToServerFn()); |
| 1797 | ASSERT_TRUE(client.setUp(GetParam())); |
| 1798 | |
| 1799 | ASSERT_EQ(OK, trust(&client, server)); |
| 1800 | ASSERT_EQ(OK, trust(server, &client)); |
| 1801 | server->setPostConnect(serverPostConnect); |
| 1802 | |
| 1803 | server->start(); |
| 1804 | ASSERT_TRUE(client.setUpTransport()); |
| 1805 | { |
| 1806 | // Wait till server writes data |
| 1807 | std::unique_lock<std::mutex> lock(readMutex); |
| 1808 | ASSERT_TRUE(readCv.wait_for(lock, 3s, [&] { return shouldContinueReading; })); |
| 1809 | } |
| 1810 | |
| 1811 | // Since there is no read polling here, we will get polling count 0 |
| 1812 | ASSERT_FALSE(client.isTransportWaiting()); |
| 1813 | ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage)); |
| 1814 | // Thread should increment polling count, read and decrement polling count |
| 1815 | // Again, polling count should be zero here |
| 1816 | ASSERT_FALSE(client.isTransportWaiting()); |
| 1817 | |
| 1818 | server->shutdown(); |
| 1819 | } |
| 1820 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1821 | INSTANTIATE_TEST_CASE_P(BinderRpc, RpcTransportTest, |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 1822 | ::testing::ValuesIn(RpcTransportTest::getRpcTranportTestParams()), |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1823 | RpcTransportTest::PrintParamInfo); |
| 1824 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1825 | class RpcTransportTlsKeyTest |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1826 | : public testing::TestWithParam< |
| 1827 | std::tuple<SocketType, RpcCertificateFormat, RpcKeyFormat, uint32_t>> { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1828 | public: |
| 1829 | template <typename A, typename B> |
| 1830 | status_t trust(const A& a, const B& b) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1831 | auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam(); |
| 1832 | (void)serverVersion; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1833 | return RpcTransportTestUtils::trust(RpcSecurity::TLS, certificateFormat, a, b); |
| 1834 | } |
| 1835 | static std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1836 | auto [socketType, certificateFormat, keyFormat, serverVersion] = info.param; |
| 1837 | return PrintToString(socketType) + "_certificate_" + PrintToString(certificateFormat) + |
| 1838 | "_key_" + PrintToString(keyFormat) + "_serverV" + std::to_string(serverVersion); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1839 | }; |
| 1840 | }; |
| 1841 | |
| 1842 | TEST_P(RpcTransportTlsKeyTest, PreSignedCertificate) { |
Andrei Homescu | 12106de | 2022-04-27 04:42:21 +0000 | [diff] [blame] | 1843 | if constexpr (!kEnableRpcThreads) { |
| 1844 | GTEST_SKIP() << "Test skipped because threads were disabled at build time"; |
| 1845 | } |
| 1846 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1847 | auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam(); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1848 | |
| 1849 | std::vector<uint8_t> pkeyData, certData; |
| 1850 | { |
| 1851 | auto pkey = makeKeyPairForSelfSignedCert(); |
| 1852 | ASSERT_NE(nullptr, pkey); |
| 1853 | auto cert = makeSelfSignedCert(pkey.get(), kCertValidSeconds); |
| 1854 | ASSERT_NE(nullptr, cert); |
| 1855 | pkeyData = serializeUnencryptedPrivatekey(pkey.get(), keyFormat); |
| 1856 | certData = serializeCertificate(cert.get(), certificateFormat); |
| 1857 | } |
| 1858 | |
| 1859 | auto desPkey = deserializeUnencryptedPrivatekey(pkeyData, keyFormat); |
| 1860 | auto desCert = deserializeCertificate(certData, certificateFormat); |
| 1861 | auto auth = std::make_unique<RpcAuthPreSigned>(std::move(desPkey), std::move(desCert)); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1862 | auto utilsParam = std::make_tuple(socketType, RpcSecurity::TLS, |
| 1863 | std::make_optional(certificateFormat), serverVersion); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1864 | |
| 1865 | auto server = std::make_unique<RpcTransportTestUtils::Server>(); |
| 1866 | ASSERT_TRUE(server->setUp(utilsParam, std::move(auth))); |
| 1867 | |
| 1868 | RpcTransportTestUtils::Client client(server->getConnectToServerFn()); |
| 1869 | ASSERT_TRUE(client.setUp(utilsParam)); |
| 1870 | |
| 1871 | ASSERT_EQ(OK, trust(&client, server)); |
| 1872 | ASSERT_EQ(OK, trust(server, &client)); |
| 1873 | |
| 1874 | server->start(); |
| 1875 | client.run(); |
| 1876 | } |
| 1877 | |
| 1878 | INSTANTIATE_TEST_CASE_P( |
| 1879 | BinderRpc, RpcTransportTlsKeyTest, |
| 1880 | testing::Combine(testing::ValuesIn(testSocketTypes(false /* hasPreconnected*/)), |
| 1881 | testing::Values(RpcCertificateFormat::PEM, RpcCertificateFormat::DER), |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1882 | testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER), |
| 1883 | testing::ValuesIn(testVersions())), |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1884 | RpcTransportTlsKeyTest::PrintParamInfo); |
| 1885 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1886 | } // namespace android |
| 1887 | |
| 1888 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1889 | ::testing::InitGoogleTest(&argc, argv); |
| 1890 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
Steven Moreland | a83191d | 2021-10-27 10:14:53 -0700 | [diff] [blame] | 1891 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1892 | return RUN_ALL_TESTS(); |
| 1893 | } |