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