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