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