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