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