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