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