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