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 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 17 | #include <BnBinderRpcCallback.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 18 | #include <BnBinderRpcSession.h> |
| 19 | #include <BnBinderRpcTest.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 20 | #include <aidl/IBinderRpcTest.h> |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 21 | #include <android-base/file.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 23 | #include <android/binder_auto_utils.h> |
| 24 | #include <android/binder_libbinder.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <binder/Binder.h> |
| 26 | #include <binder/BpBinder.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/ProcessState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 30 | #include <binder/RpcServer.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 31 | #include <binder/RpcSession.h> |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 32 | #include <binder/RpcTransport.h> |
| 33 | #include <binder/RpcTransportRaw.h> |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 34 | #include <binder/RpcTransportTls.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 35 | #include <gtest/gtest.h> |
| 36 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 37 | #include <chrono> |
| 38 | #include <cstdlib> |
| 39 | #include <iostream> |
| 40 | #include <thread> |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 41 | #include <type_traits> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 42 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 43 | #include <sys/prctl.h> |
| 44 | #include <unistd.h> |
| 45 | |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 46 | #include "../RpcSocketAddress.h" // for testing preconnected clients |
Steven Moreland | bd5002b | 2021-05-04 23:12:56 +0000 | [diff] [blame] | 47 | #include "../RpcState.h" // for debugging |
| 48 | #include "../vm_sockets.h" // for VMADDR_* |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 49 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 50 | using namespace std::chrono_literals; |
| 51 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 52 | namespace android { |
| 53 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 54 | static_assert(RPC_WIRE_PROTOCOL_VERSION + 1 == RPC_WIRE_PROTOCOL_VERSION_NEXT || |
| 55 | RPC_WIRE_PROTOCOL_VERSION == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); |
Devin Moore | f3b9c4f | 2021-08-03 15:50:13 +0000 | [diff] [blame] | 56 | const char* kLocalInetAddress = "127.0.0.1"; |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 57 | |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 58 | enum class RpcSecurity { RAW, TLS }; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 59 | |
| 60 | static inline std::vector<RpcSecurity> RpcSecurityValues() { |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 61 | return {RpcSecurity::RAW, RpcSecurity::TLS}; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static inline std::unique_ptr<RpcTransportCtxFactory> newFactory(RpcSecurity rpcSecurity) { |
| 65 | switch (rpcSecurity) { |
| 66 | case RpcSecurity::RAW: |
| 67 | return RpcTransportCtxFactoryRaw::make(); |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 68 | case RpcSecurity::TLS: |
| 69 | return RpcTransportCtxFactoryTls::make(); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 70 | default: |
| 71 | LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity); |
| 72 | } |
| 73 | } |
| 74 | |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 75 | TEST(BinderRpcParcel, EntireParcelFormatted) { |
| 76 | Parcel p; |
| 77 | p.writeInt32(3); |
| 78 | |
| 79 | EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), ""); |
| 80 | } |
| 81 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 82 | class BinderRpcSimple : public ::testing::TestWithParam<RpcSecurity> { |
| 83 | public: |
| 84 | static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) { |
| 85 | return newFactory(info.param)->toCString(); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | TEST_P(BinderRpcSimple, SetExternalServerTest) { |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 90 | base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 91 | int sinkFd = sink.get(); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 92 | auto server = RpcServer::make(newFactory(GetParam())); |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 93 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 94 | ASSERT_FALSE(server->hasServer()); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 95 | ASSERT_EQ(OK, server->setupExternalServer(std::move(sink))); |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 96 | ASSERT_TRUE(server->hasServer()); |
| 97 | base::unique_fd retrieved = server->releaseServer(); |
| 98 | ASSERT_FALSE(server->hasServer()); |
| 99 | ASSERT_EQ(sinkFd, retrieved.get()); |
| 100 | } |
| 101 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 102 | TEST(BinderRpc, CannotUseNextWireVersion) { |
| 103 | auto session = RpcSession::make(); |
| 104 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT)); |
| 105 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 1)); |
| 106 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 2)); |
| 107 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 15)); |
| 108 | } |
| 109 | |
| 110 | TEST(BinderRpc, CanUseExperimentalWireVersion) { |
| 111 | auto session = RpcSession::make(); |
| 112 | EXPECT_TRUE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL)); |
| 113 | } |
| 114 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 115 | using android::binder::Status; |
| 116 | |
| 117 | #define EXPECT_OK(status) \ |
| 118 | do { \ |
| 119 | Status stat = (status); \ |
| 120 | EXPECT_TRUE(stat.isOk()) << stat; \ |
| 121 | } while (false) |
| 122 | |
| 123 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 124 | public: |
| 125 | static std::atomic<int32_t> gNum; |
| 126 | |
| 127 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 128 | Status getName(std::string* name) override { |
| 129 | *name = mName; |
| 130 | return Status::ok(); |
| 131 | } |
| 132 | ~MyBinderRpcSession() { gNum--; } |
| 133 | |
| 134 | private: |
| 135 | std::string mName; |
| 136 | }; |
| 137 | std::atomic<int32_t> MyBinderRpcSession::gNum; |
| 138 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 139 | class MyBinderRpcCallback : public BnBinderRpcCallback { |
| 140 | Status sendCallback(const std::string& value) { |
| 141 | std::unique_lock _l(mMutex); |
| 142 | mValues.push_back(value); |
| 143 | _l.unlock(); |
| 144 | mCv.notify_one(); |
| 145 | return Status::ok(); |
| 146 | } |
| 147 | Status sendOnewayCallback(const std::string& value) { return sendCallback(value); } |
| 148 | |
| 149 | public: |
| 150 | std::mutex mMutex; |
| 151 | std::condition_variable mCv; |
| 152 | std::vector<std::string> mValues; |
| 153 | }; |
| 154 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 155 | class MyBinderRpcTest : public BnBinderRpcTest { |
| 156 | public: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 157 | wp<RpcServer> server; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 158 | |
| 159 | Status sendString(const std::string& str) override { |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 160 | (void)str; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 161 | return Status::ok(); |
| 162 | } |
| 163 | Status doubleString(const std::string& str, std::string* strstr) override { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 164 | *strstr = str + str; |
| 165 | return Status::ok(); |
| 166 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 167 | Status countBinders(std::vector<int32_t>* out) override { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 168 | sp<RpcServer> spServer = server.promote(); |
| 169 | if (spServer == nullptr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 170 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 171 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 172 | out->clear(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 173 | for (auto session : spServer->listSessions()) { |
| 174 | size_t count = session->state()->countBinders(); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 175 | out->push_back(count); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 176 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 177 | return Status::ok(); |
| 178 | } |
| 179 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 180 | if (binder == nullptr) { |
| 181 | std::cout << "Received null binder!" << std::endl; |
| 182 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 183 | } |
| 184 | *out = binder->pingBinder(); |
| 185 | return Status::ok(); |
| 186 | } |
| 187 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 188 | *out = binder; |
| 189 | return Status::ok(); |
| 190 | } |
| 191 | static sp<IBinder> mHeldBinder; |
| 192 | Status holdBinder(const sp<IBinder>& binder) override { |
| 193 | mHeldBinder = binder; |
| 194 | return Status::ok(); |
| 195 | } |
| 196 | Status getHeldBinder(sp<IBinder>* held) override { |
| 197 | *held = mHeldBinder; |
| 198 | return Status::ok(); |
| 199 | } |
| 200 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 201 | if (count <= 0) return Status::ok(); |
| 202 | return binder->nestMe(this, count - 1); |
| 203 | } |
| 204 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 205 | static sp<IBinder> binder = new BBinder; |
| 206 | *out = binder; |
| 207 | return Status::ok(); |
| 208 | } |
| 209 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 210 | *out = new MyBinderRpcSession(name); |
| 211 | return Status::ok(); |
| 212 | } |
| 213 | Status getNumOpenSessions(int32_t* out) override { |
| 214 | *out = MyBinderRpcSession::gNum; |
| 215 | return Status::ok(); |
| 216 | } |
| 217 | |
| 218 | std::mutex blockMutex; |
| 219 | Status lock() override { |
| 220 | blockMutex.lock(); |
| 221 | return Status::ok(); |
| 222 | } |
| 223 | Status unlockInMsAsync(int32_t ms) override { |
| 224 | usleep(ms * 1000); |
| 225 | blockMutex.unlock(); |
| 226 | return Status::ok(); |
| 227 | } |
| 228 | Status lockUnlock() override { |
| 229 | std::lock_guard<std::mutex> _l(blockMutex); |
| 230 | return Status::ok(); |
| 231 | } |
| 232 | |
| 233 | Status sleepMs(int32_t ms) override { |
| 234 | usleep(ms * 1000); |
| 235 | return Status::ok(); |
| 236 | } |
| 237 | |
| 238 | Status sleepMsAsync(int32_t ms) override { |
| 239 | // In-process binder calls are asynchronous, but the call to this method |
| 240 | // is synchronous wrt its client. This in/out-process threading model |
| 241 | // diffentiation is a classic binder leaky abstraction (for better or |
| 242 | // worse) and is preserved here the way binder sockets plugs itself |
| 243 | // into BpBinder, as nothing is changed at the higher levels |
| 244 | // (IInterface) which result in this behavior. |
| 245 | return sleepMs(ms); |
| 246 | } |
| 247 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 248 | Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 249 | const std::string& value) override { |
| 250 | if (callback == nullptr) { |
| 251 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 252 | } |
| 253 | |
| 254 | if (delayed) { |
| 255 | std::thread([=]() { |
| 256 | ALOGE("Executing delayed callback: '%s'", value.c_str()); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 257 | Status status = doCallback(callback, oneway, false, value); |
| 258 | ALOGE("Delayed callback status: '%s'", status.toString8().c_str()); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 259 | }).detach(); |
| 260 | return Status::ok(); |
| 261 | } |
| 262 | |
| 263 | if (oneway) { |
| 264 | return callback->sendOnewayCallback(value); |
| 265 | } |
| 266 | |
| 267 | return callback->sendCallback(value); |
| 268 | } |
| 269 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 270 | Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 271 | const std::string& value) override { |
| 272 | return doCallback(callback, oneway, delayed, value); |
| 273 | } |
| 274 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 275 | Status die(bool cleanup) override { |
| 276 | if (cleanup) { |
| 277 | exit(1); |
| 278 | } else { |
| 279 | _exit(1); |
| 280 | } |
| 281 | } |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 282 | |
| 283 | Status scheduleShutdown() override { |
| 284 | sp<RpcServer> strongServer = server.promote(); |
| 285 | if (strongServer == nullptr) { |
| 286 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 287 | } |
| 288 | std::thread([=] { |
| 289 | LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); |
| 290 | }).detach(); |
| 291 | return Status::ok(); |
| 292 | } |
| 293 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 294 | Status useKernelBinderCallingId() override { |
| 295 | // this is WRONG! It does not make sense when using RPC binder, and |
| 296 | // because it is SO wrong, and so much code calls this, it should abort! |
| 297 | |
| 298 | (void)IPCThreadState::self()->getCallingPid(); |
| 299 | return Status::ok(); |
| 300 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 301 | }; |
| 302 | sp<IBinder> MyBinderRpcTest::mHeldBinder; |
| 303 | |
| 304 | class Process { |
| 305 | public: |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 306 | Process(Process&&) = default; |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 307 | Process(const std::function<void(android::base::borrowed_fd /* writeEnd */)>& f) { |
| 308 | android::base::unique_fd writeEnd; |
| 309 | CHECK(android::base::Pipe(&mReadEnd, &writeEnd)) << strerror(errno); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 310 | if (0 == (mPid = fork())) { |
| 311 | // racey: assume parent doesn't crash before this is set |
| 312 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 313 | |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 314 | f(writeEnd); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 315 | |
| 316 | exit(0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | ~Process() { |
| 320 | if (mPid != 0) { |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 321 | waitpid(mPid, nullptr, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 324 | android::base::borrowed_fd readEnd() { return mReadEnd; } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 325 | |
| 326 | private: |
| 327 | pid_t mPid = 0; |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 328 | android::base::unique_fd mReadEnd; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | static std::string allocateSocketAddress() { |
| 332 | static size_t id = 0; |
Steven Moreland | 4bfbf2e | 2021-04-14 22:15:16 +0000 | [diff] [blame] | 333 | std::string temp = getenv("TMPDIR") ?: "/tmp"; |
| 334 | return temp + "/binderRpcTest_" + std::to_string(id++); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 337 | static unsigned int allocateVsockPort() { |
| 338 | static unsigned int vsockPort = 3456; |
| 339 | return vsockPort++; |
| 340 | } |
| 341 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 342 | struct ProcessSession { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 343 | // reference to process hosting a socket server |
| 344 | Process host; |
| 345 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 346 | struct SessionInfo { |
| 347 | sp<RpcSession> session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 348 | sp<IBinder> root; |
| 349 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 350 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 351 | // client session objects associated with other process |
| 352 | // each one represents a separate session |
| 353 | std::vector<SessionInfo> sessions; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 354 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 355 | ProcessSession(ProcessSession&&) = default; |
| 356 | ~ProcessSession() { |
| 357 | for (auto& session : sessions) { |
| 358 | session.root = nullptr; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 359 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 360 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 361 | for (auto& info : sessions) { |
| 362 | sp<RpcSession>& session = info.session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 363 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 364 | EXPECT_NE(nullptr, session); |
| 365 | EXPECT_NE(nullptr, session->state()); |
| 366 | EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:"); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 367 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 368 | wp<RpcSession> weakSession = session; |
| 369 | session = nullptr; |
| 370 | EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session"; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 371 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 372 | } |
| 373 | }; |
| 374 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 375 | // Process session where the process hosts IBinderRpcTest, the server used |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 376 | // for most testing here |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 377 | struct BinderRpcTestProcessSession { |
| 378 | ProcessSession proc; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 379 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 380 | // pre-fetched root object (for first session) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 381 | sp<IBinder> rootBinder; |
| 382 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 383 | // pre-casted root object (for first session) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 384 | sp<IBinderRpcTest> rootIface; |
| 385 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 386 | // whether session should be invalidated by end of run |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 387 | bool expectAlreadyShutdown = false; |
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 | BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default; |
| 390 | ~BinderRpcTestProcessSession() { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 391 | EXPECT_NE(nullptr, rootIface); |
| 392 | if (rootIface == nullptr) return; |
| 393 | |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 394 | if (!expectAlreadyShutdown) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 395 | std::vector<int32_t> remoteCounts; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 396 | // calling over any sessions counts across all sessions |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 397 | EXPECT_OK(rootIface->countBinders(&remoteCounts)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 398 | EXPECT_EQ(remoteCounts.size(), proc.sessions.size()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 399 | for (auto remoteCount : remoteCounts) { |
| 400 | EXPECT_EQ(remoteCount, 1); |
| 401 | } |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 402 | |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 403 | // even though it is on another thread, shutdown races with |
| 404 | // the transaction reply being written |
| 405 | if (auto status = rootIface->scheduleShutdown(); !status.isOk()) { |
| 406 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 407 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | rootIface = nullptr; |
| 411 | rootBinder = nullptr; |
| 412 | } |
| 413 | }; |
| 414 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 415 | enum class SocketType { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 416 | PRECONNECTED, |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 417 | UNIX, |
| 418 | VSOCK, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 419 | INET, |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 420 | }; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 421 | static inline std::string PrintToString(SocketType socketType) { |
| 422 | switch (socketType) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 423 | case SocketType::PRECONNECTED: |
| 424 | return "preconnected_uds"; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 425 | case SocketType::UNIX: |
| 426 | return "unix_domain_socket"; |
| 427 | case SocketType::VSOCK: |
| 428 | return "vm_socket"; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 429 | case SocketType::INET: |
| 430 | return "inet_socket"; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 431 | default: |
| 432 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 433 | return ""; |
| 434 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 435 | } |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 436 | |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 437 | static base::unique_fd connectToUds(const char* addrStr) { |
| 438 | UnixSocketAddress addr(addrStr); |
| 439 | base::unique_fd serverFd( |
| 440 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 441 | int savedErrno = errno; |
| 442 | CHECK(serverFd.ok()) << "Could not create socket " << addrStr << ": " << strerror(savedErrno); |
| 443 | |
| 444 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 445 | int savedErrno = errno; |
| 446 | LOG(FATAL) << "Could not connect to socket " << addrStr << ": " << strerror(savedErrno); |
| 447 | } |
| 448 | return serverFd; |
| 449 | } |
| 450 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 451 | class BinderRpc : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity>> { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 452 | public: |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 453 | struct Options { |
| 454 | size_t numThreads = 1; |
| 455 | size_t numSessions = 1; |
| 456 | size_t numIncomingConnections = 0; |
| 457 | }; |
| 458 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 459 | static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
| 460 | auto [type, security] = info.param; |
| 461 | return PrintToString(type) + "_" + newFactory(security)->toCString(); |
| 462 | } |
| 463 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 464 | // This creates a new process serving an interface on a certain number of |
| 465 | // threads. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 466 | ProcessSession createRpcTestSocketServerProcess( |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 467 | const Options& options, const std::function<void(const sp<RpcServer>&)>& configure) { |
| 468 | 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] | 469 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 470 | SocketType socketType = std::get<0>(GetParam()); |
| 471 | RpcSecurity rpcSecurity = std::get<1>(GetParam()); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 472 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 473 | unsigned int vsockPort = allocateVsockPort(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 474 | std::string addr = allocateSocketAddress(); |
| 475 | unlink(addr.c_str()); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 476 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 477 | auto ret = ProcessSession{ |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 478 | .host = Process([&](android::base::borrowed_fd writeEnd) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 479 | sp<RpcServer> server = RpcServer::make(newFactory(rpcSecurity)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 480 | |
| 481 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 482 | server->setMaxThreads(options.numThreads); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 483 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 484 | unsigned int outPort = 0; |
| 485 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 486 | switch (socketType) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 487 | case SocketType::PRECONNECTED: |
| 488 | [[fallthrough]]; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 489 | case SocketType::UNIX: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 490 | CHECK_EQ(OK, server->setupUnixDomainServer(addr.c_str())) << addr; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 491 | break; |
| 492 | case SocketType::VSOCK: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 493 | CHECK_EQ(OK, server->setupVsockServer(vsockPort)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 494 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 495 | case SocketType::INET: { |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 496 | CHECK_EQ(OK, server->setupInetServer(kLocalInetAddress, 0, &outPort)); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 497 | CHECK_NE(0, outPort); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 498 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 499 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 500 | default: |
| 501 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 502 | } |
| 503 | |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 504 | CHECK(android::base::WriteFully(writeEnd, &outPort, sizeof(outPort))); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 505 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 506 | configure(server); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 507 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 508 | server->join(); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 509 | |
| 510 | // Another thread calls shutdown. Wait for it to complete. |
| 511 | (void)server->shutdown(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 512 | }), |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 515 | // always read socket, so that we have waited for the server to start |
| 516 | unsigned int outPort = 0; |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 517 | CHECK(android::base::ReadFully(ret.host.readEnd(), &outPort, sizeof(outPort))); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 518 | if (socketType == SocketType::INET) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 519 | CHECK_NE(0, outPort); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 522 | status_t status; |
| 523 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 524 | for (size_t i = 0; i < options.numSessions; i++) { |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame^] | 525 | sp<RpcSession> session = |
| 526 | RpcSession::make(newFactory(rpcSecurity), std::nullopt, std::nullopt); |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 527 | session->setMaxThreads(options.numIncomingConnections); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 528 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 529 | switch (socketType) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 530 | case SocketType::PRECONNECTED: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 531 | status = session->setupPreconnectedClient({}, [=]() { |
| 532 | return connectToUds(addr.c_str()); |
| 533 | }); |
| 534 | if (status == OK) goto success; |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 535 | break; |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 536 | case SocketType::UNIX: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 537 | status = session->setupUnixDomainClient(addr.c_str()); |
| 538 | if (status == OK) goto success; |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 539 | break; |
| 540 | case SocketType::VSOCK: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 541 | status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); |
| 542 | if (status == OK) goto success; |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 543 | break; |
| 544 | case SocketType::INET: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 545 | status = session->setupInetClient("127.0.0.1", outPort); |
| 546 | if (status == OK) goto success; |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 547 | break; |
| 548 | default: |
| 549 | LOG_ALWAYS_FATAL("Unknown socket type"); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 550 | } |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 551 | LOG_ALWAYS_FATAL("Could not connect %s", statusToString(status).c_str()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 552 | success: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 553 | ret.sessions.push_back({session, session->getRootObject()}); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 554 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 555 | return ret; |
| 556 | } |
| 557 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 558 | BinderRpcTestProcessSession createRpcTestSocketServerProcess(const Options& options) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 559 | BinderRpcTestProcessSession ret{ |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 560 | .proc = createRpcTestSocketServerProcess(options, |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 561 | [&](const sp<RpcServer>& server) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 562 | sp<MyBinderRpcTest> service = |
| 563 | new MyBinderRpcTest; |
| 564 | server->setRootObject(service); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 565 | service->server = server; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 566 | }), |
| 567 | }; |
| 568 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 569 | ret.rootBinder = ret.proc.sessions.at(0).root; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 570 | ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder); |
| 571 | |
| 572 | return ret; |
| 573 | } |
| 574 | }; |
| 575 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 576 | TEST_P(BinderRpc, Ping) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 577 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 578 | ASSERT_NE(proc.rootBinder, nullptr); |
| 579 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 580 | } |
| 581 | |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 582 | TEST_P(BinderRpc, GetInterfaceDescriptor) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 583 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 584 | ASSERT_NE(proc.rootBinder, nullptr); |
| 585 | EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); |
| 586 | } |
| 587 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 588 | TEST_P(BinderRpc, MultipleSessions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 589 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5}); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 590 | for (auto session : proc.proc.sessions) { |
| 591 | ASSERT_NE(nullptr, session.root); |
| 592 | EXPECT_EQ(OK, session.root->pingBinder()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 596 | TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 597 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 598 | Parcel data; |
| 599 | Parcel reply; |
| 600 | EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0)); |
| 601 | } |
| 602 | |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 603 | TEST_P(BinderRpc, AppendSeparateFormats) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 604 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 605 | |
| 606 | Parcel p1; |
| 607 | p1.markForBinder(proc.rootBinder); |
| 608 | p1.writeInt32(3); |
| 609 | |
| 610 | Parcel p2; |
| 611 | |
| 612 | EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize())); |
| 613 | EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize())); |
| 614 | } |
| 615 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 616 | TEST_P(BinderRpc, UnknownTransaction) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 617 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 618 | Parcel data; |
| 619 | data.markForBinder(proc.rootBinder); |
| 620 | Parcel reply; |
| 621 | EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0)); |
| 622 | } |
| 623 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 624 | TEST_P(BinderRpc, SendSomethingOneway) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 625 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 626 | EXPECT_OK(proc.rootIface->sendString("asdf")); |
| 627 | } |
| 628 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 629 | TEST_P(BinderRpc, SendAndGetResultBack) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 630 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 631 | std::string doubled; |
| 632 | EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled)); |
| 633 | EXPECT_EQ("cool cool ", doubled); |
| 634 | } |
| 635 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 636 | TEST_P(BinderRpc, SendAndGetResultBackBig) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 637 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 638 | std::string single = std::string(1024, 'a'); |
| 639 | std::string doubled; |
| 640 | EXPECT_OK(proc.rootIface->doubleString(single, &doubled)); |
| 641 | EXPECT_EQ(single + single, doubled); |
| 642 | } |
| 643 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 644 | TEST_P(BinderRpc, CallMeBack) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 645 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 646 | |
| 647 | int32_t pingResult; |
| 648 | EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult)); |
| 649 | EXPECT_EQ(OK, pingResult); |
| 650 | |
| 651 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 652 | } |
| 653 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 654 | TEST_P(BinderRpc, RepeatBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 655 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 656 | |
| 657 | sp<IBinder> inBinder = new MyBinderRpcSession("foo"); |
| 658 | sp<IBinder> outBinder; |
| 659 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 660 | EXPECT_EQ(inBinder, outBinder); |
| 661 | |
| 662 | wp<IBinder> weak = inBinder; |
| 663 | inBinder = nullptr; |
| 664 | outBinder = nullptr; |
| 665 | |
| 666 | // Force reading a reply, to process any pending dec refs from the other |
| 667 | // process (the other process will process dec refs there before processing |
| 668 | // the ping here). |
| 669 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 670 | |
| 671 | EXPECT_EQ(nullptr, weak.promote()); |
| 672 | |
| 673 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 674 | } |
| 675 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 676 | TEST_P(BinderRpc, RepeatTheirBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 677 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 678 | |
| 679 | sp<IBinderRpcSession> session; |
| 680 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 681 | |
| 682 | sp<IBinder> inBinder = IInterface::asBinder(session); |
| 683 | sp<IBinder> outBinder; |
| 684 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 685 | EXPECT_EQ(inBinder, outBinder); |
| 686 | |
| 687 | wp<IBinder> weak = inBinder; |
| 688 | session = nullptr; |
| 689 | inBinder = nullptr; |
| 690 | outBinder = nullptr; |
| 691 | |
| 692 | // Force reading a reply, to process any pending dec refs from the other |
| 693 | // process (the other process will process dec refs there before processing |
| 694 | // the ping here). |
| 695 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 696 | |
| 697 | EXPECT_EQ(nullptr, weak.promote()); |
| 698 | } |
| 699 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 700 | TEST_P(BinderRpc, RepeatBinderNull) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 701 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 702 | |
| 703 | sp<IBinder> outBinder; |
| 704 | EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder)); |
| 705 | EXPECT_EQ(nullptr, outBinder); |
| 706 | } |
| 707 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 708 | TEST_P(BinderRpc, HoldBinder) { |
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 | |
| 711 | IBinder* ptr = nullptr; |
| 712 | { |
| 713 | sp<IBinder> binder = new BBinder(); |
| 714 | ptr = binder.get(); |
| 715 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 716 | } |
| 717 | |
| 718 | sp<IBinder> held; |
| 719 | EXPECT_OK(proc.rootIface->getHeldBinder(&held)); |
| 720 | |
| 721 | EXPECT_EQ(held.get(), ptr); |
| 722 | |
| 723 | // stop holding binder, because we test to make sure references are cleaned |
| 724 | // up |
| 725 | EXPECT_OK(proc.rootIface->holdBinder(nullptr)); |
| 726 | // and flush ref counts |
| 727 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 728 | } |
| 729 | |
| 730 | // START TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 731 | // These are behavioral differences form regular binder, where certain usecases |
| 732 | // aren't supported. |
| 733 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 734 | TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 735 | auto proc1 = createRpcTestSocketServerProcess({}); |
| 736 | auto proc2 = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 737 | |
| 738 | sp<IBinder> outBinder; |
| 739 | EXPECT_EQ(INVALID_OPERATION, |
| 740 | proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); |
| 741 | } |
| 742 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 743 | TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 744 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2}); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 745 | |
| 746 | sp<IBinder> outBinder; |
| 747 | EXPECT_EQ(INVALID_OPERATION, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 748 | proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder) |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 749 | .transactionError()); |
| 750 | } |
| 751 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 752 | TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 753 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 754 | |
| 755 | sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); |
| 756 | sp<IBinder> outBinder; |
| 757 | EXPECT_EQ(INVALID_OPERATION, |
| 758 | proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError()); |
| 759 | } |
| 760 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 761 | TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 762 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 763 | |
| 764 | // for historical reasons, IServiceManager interface only returns the |
| 765 | // exception code |
| 766 | EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED, |
| 767 | defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder)); |
| 768 | } |
| 769 | |
| 770 | // END TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 771 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 772 | TEST_P(BinderRpc, RepeatRootObject) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 773 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 774 | |
| 775 | sp<IBinder> outBinder; |
| 776 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder)); |
| 777 | EXPECT_EQ(proc.rootBinder, outBinder); |
| 778 | } |
| 779 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 780 | TEST_P(BinderRpc, NestedTransactions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 781 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 782 | |
| 783 | auto nastyNester = sp<MyBinderRpcTest>::make(); |
| 784 | EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10)); |
| 785 | |
| 786 | wp<IBinder> weak = nastyNester; |
| 787 | nastyNester = nullptr; |
| 788 | EXPECT_EQ(nullptr, weak.promote()); |
| 789 | } |
| 790 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 791 | TEST_P(BinderRpc, SameBinderEquality) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 792 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 793 | |
| 794 | sp<IBinder> a; |
| 795 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 796 | |
| 797 | sp<IBinder> b; |
| 798 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 799 | |
| 800 | EXPECT_EQ(a, b); |
| 801 | } |
| 802 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 803 | TEST_P(BinderRpc, SameBinderEqualityWeak) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 804 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 805 | |
| 806 | sp<IBinder> a; |
| 807 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 808 | wp<IBinder> weak = a; |
| 809 | a = nullptr; |
| 810 | |
| 811 | sp<IBinder> b; |
| 812 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 813 | |
| 814 | // this is the wrong behavior, since BpBinder |
| 815 | // doesn't implement onIncStrongAttempted |
| 816 | // but make sure there is no crash |
| 817 | EXPECT_EQ(nullptr, weak.promote()); |
| 818 | |
| 819 | GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder."; |
| 820 | |
| 821 | // In order to fix this: |
| 822 | // - need to have incStrongAttempted reflected across IPC boundary (wait for |
| 823 | // response to promote - round trip...) |
| 824 | // - sendOnLastWeakRef, to delete entries out of RpcState table |
| 825 | EXPECT_EQ(b, weak.promote()); |
| 826 | } |
| 827 | |
| 828 | #define expectSessions(expected, iface) \ |
| 829 | do { \ |
| 830 | int session; \ |
| 831 | EXPECT_OK((iface)->getNumOpenSessions(&session)); \ |
| 832 | EXPECT_EQ(expected, session); \ |
| 833 | } while (false) |
| 834 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 835 | TEST_P(BinderRpc, SingleSession) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 836 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 837 | |
| 838 | sp<IBinderRpcSession> session; |
| 839 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 840 | std::string out; |
| 841 | EXPECT_OK(session->getName(&out)); |
| 842 | EXPECT_EQ("aoeu", out); |
| 843 | |
| 844 | expectSessions(1, proc.rootIface); |
| 845 | session = nullptr; |
| 846 | expectSessions(0, proc.rootIface); |
| 847 | } |
| 848 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 849 | TEST_P(BinderRpc, ManySessions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 850 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 851 | |
| 852 | std::vector<sp<IBinderRpcSession>> sessions; |
| 853 | |
| 854 | for (size_t i = 0; i < 15; i++) { |
| 855 | expectSessions(i, proc.rootIface); |
| 856 | sp<IBinderRpcSession> session; |
| 857 | EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session)); |
| 858 | sessions.push_back(session); |
| 859 | } |
| 860 | expectSessions(sessions.size(), proc.rootIface); |
| 861 | for (size_t i = 0; i < sessions.size(); i++) { |
| 862 | std::string out; |
| 863 | EXPECT_OK(sessions.at(i)->getName(&out)); |
| 864 | EXPECT_EQ(std::to_string(i), out); |
| 865 | } |
| 866 | expectSessions(sessions.size(), proc.rootIface); |
| 867 | |
| 868 | while (!sessions.empty()) { |
| 869 | sessions.pop_back(); |
| 870 | expectSessions(sessions.size(), proc.rootIface); |
| 871 | } |
| 872 | expectSessions(0, proc.rootIface); |
| 873 | } |
| 874 | |
| 875 | size_t epochMillis() { |
| 876 | using std::chrono::duration_cast; |
| 877 | using std::chrono::milliseconds; |
| 878 | using std::chrono::seconds; |
| 879 | using std::chrono::system_clock; |
| 880 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 881 | } |
| 882 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 883 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 884 | constexpr size_t kNumThreads = 10; |
| 885 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 886 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 887 | |
| 888 | EXPECT_OK(proc.rootIface->lock()); |
| 889 | |
| 890 | // block all but one thread taking locks |
| 891 | std::vector<std::thread> ts; |
| 892 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 893 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 894 | } |
| 895 | |
| 896 | usleep(100000); // give chance for calls on other threads |
| 897 | |
| 898 | // other calls still work |
| 899 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 900 | |
| 901 | constexpr size_t blockTimeMs = 500; |
| 902 | size_t epochMsBefore = epochMillis(); |
| 903 | // after this, we should never see a response within this time |
| 904 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 905 | |
| 906 | // this call should be blocked for blockTimeMs |
| 907 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 908 | |
| 909 | size_t epochMsAfter = epochMillis(); |
| 910 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 911 | |
| 912 | for (auto& t : ts) t.join(); |
| 913 | } |
| 914 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 915 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 916 | constexpr size_t kNumThreads = 10; |
| 917 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 918 | constexpr size_t kSleepMs = 500; |
| 919 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 920 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 921 | |
| 922 | size_t epochMsBefore = epochMillis(); |
| 923 | |
| 924 | std::vector<std::thread> ts; |
| 925 | for (size_t i = 0; i < kNumCalls; i++) { |
| 926 | ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); })); |
| 927 | } |
| 928 | |
| 929 | for (auto& t : ts) t.join(); |
| 930 | |
| 931 | size_t epochMsAfter = epochMillis(); |
| 932 | |
| 933 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs); |
| 934 | |
| 935 | // Potential flake, but make sure calls are handled in parallel. |
| 936 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs); |
| 937 | } |
| 938 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 939 | TEST_P(BinderRpc, ThreadingStressTest) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 940 | constexpr size_t kNumClientThreads = 10; |
| 941 | constexpr size_t kNumServerThreads = 10; |
| 942 | constexpr size_t kNumCalls = 100; |
| 943 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 944 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 945 | |
| 946 | std::vector<std::thread> threads; |
| 947 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 948 | threads.push_back(std::thread([&] { |
| 949 | for (size_t j = 0; j < kNumCalls; j++) { |
| 950 | sp<IBinder> out; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 951 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 952 | EXPECT_EQ(proc.rootBinder, out); |
| 953 | } |
| 954 | })); |
| 955 | } |
| 956 | |
| 957 | for (auto& t : threads) t.join(); |
| 958 | } |
| 959 | |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 960 | TEST_P(BinderRpc, OnewayStressTest) { |
| 961 | constexpr size_t kNumClientThreads = 10; |
| 962 | constexpr size_t kNumServerThreads = 10; |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 963 | constexpr size_t kNumCalls = 500; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 964 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 965 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads}); |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 966 | |
| 967 | std::vector<std::thread> threads; |
| 968 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 969 | threads.push_back(std::thread([&] { |
| 970 | for (size_t j = 0; j < kNumCalls; j++) { |
| 971 | EXPECT_OK(proc.rootIface->sendString("a")); |
| 972 | } |
| 973 | |
| 974 | // check threads are not stuck |
| 975 | EXPECT_OK(proc.rootIface->sleepMs(250)); |
| 976 | })); |
| 977 | } |
| 978 | |
| 979 | for (auto& t : threads) t.join(); |
| 980 | } |
| 981 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 982 | TEST_P(BinderRpc, OnewayCallDoesNotWait) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 983 | constexpr size_t kReallyLongTimeMs = 100; |
| 984 | constexpr size_t kSleepMs = kReallyLongTimeMs * 5; |
| 985 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 986 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 987 | |
| 988 | size_t epochMsBefore = epochMillis(); |
| 989 | |
| 990 | EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs)); |
| 991 | |
| 992 | size_t epochMsAfter = epochMillis(); |
| 993 | EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); |
| 994 | } |
| 995 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 996 | TEST_P(BinderRpc, OnewayCallQueueing) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 997 | constexpr size_t kNumSleeps = 10; |
| 998 | constexpr size_t kNumExtraServerThreads = 4; |
| 999 | constexpr size_t kSleepMs = 50; |
| 1000 | |
| 1001 | // make sure calls to the same object happen on the same thread |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1002 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1003 | |
| 1004 | EXPECT_OK(proc.rootIface->lock()); |
| 1005 | |
| 1006 | for (size_t i = 0; i < kNumSleeps; i++) { |
| 1007 | // these should be processed serially |
| 1008 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 1009 | } |
| 1010 | // should also be processesed serially |
| 1011 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 1012 | |
| 1013 | size_t epochMsBefore = epochMillis(); |
| 1014 | EXPECT_OK(proc.rootIface->lockUnlock()); |
| 1015 | size_t epochMsAfter = epochMillis(); |
| 1016 | |
| 1017 | EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1018 | |
| 1019 | // pending oneway transactions hold ref, make sure we read data on all |
| 1020 | // sockets |
| 1021 | std::vector<std::thread> threads; |
| 1022 | for (size_t i = 0; i < 1 + kNumExtraServerThreads; i++) { |
| 1023 | threads.push_back(std::thread([&] { EXPECT_OK(proc.rootIface->sleepMs(250)); })); |
| 1024 | } |
| 1025 | for (auto& t : threads) t.join(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 1028 | TEST_P(BinderRpc, OnewayCallExhaustion) { |
| 1029 | constexpr size_t kNumClients = 2; |
| 1030 | constexpr size_t kTooLongMs = 1000; |
| 1031 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1032 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2}); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Build up oneway calls on the second session to make sure it terminates |
| 1035 | // and shuts down. The first session should be unaffected (proc destructor |
| 1036 | // checks the first session). |
| 1037 | auto iface = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root); |
| 1038 | |
| 1039 | std::vector<std::thread> threads; |
| 1040 | for (size_t i = 0; i < kNumClients; i++) { |
| 1041 | // one of these threads will get stuck queueing a transaction once the |
| 1042 | // socket fills up, the other will be able to fill up transactions on |
| 1043 | // this object |
| 1044 | threads.push_back(std::thread([&] { |
| 1045 | while (iface->sleepMsAsync(kTooLongMs).isOk()) { |
| 1046 | } |
| 1047 | })); |
| 1048 | } |
| 1049 | for (auto& t : threads) t.join(); |
| 1050 | |
| 1051 | Status status = iface->sleepMsAsync(kTooLongMs); |
| 1052 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 1053 | |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 1054 | // now that it has died, wait for the remote session to shutdown |
| 1055 | std::vector<int32_t> remoteCounts; |
| 1056 | do { |
| 1057 | EXPECT_OK(proc.rootIface->countBinders(&remoteCounts)); |
| 1058 | } while (remoteCounts.size() == kNumClients); |
| 1059 | |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 1060 | // the second session should be shutdown in the other process by the time we |
| 1061 | // are able to join above (it'll only be hung up once it finishes processing |
| 1062 | // any pending commands). We need to erase this session from the record |
| 1063 | // here, so that the destructor for our session won't check that this |
| 1064 | // session is valid, but we still want it to test the other session. |
| 1065 | proc.proc.sessions.erase(proc.proc.sessions.begin() + 1); |
| 1066 | } |
| 1067 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1068 | TEST_P(BinderRpc, Callbacks) { |
| 1069 | const static std::string kTestString = "good afternoon!"; |
| 1070 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1071 | for (bool callIsOneway : {true, false}) { |
| 1072 | for (bool callbackIsOneway : {true, false}) { |
| 1073 | for (bool delayed : {true, false}) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1074 | auto proc = createRpcTestSocketServerProcess( |
| 1075 | {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1076 | auto cb = sp<MyBinderRpcCallback>::make(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1077 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1078 | if (callIsOneway) { |
| 1079 | EXPECT_OK(proc.rootIface->doCallbackAsync(cb, callbackIsOneway, delayed, |
| 1080 | kTestString)); |
| 1081 | } else { |
| 1082 | EXPECT_OK( |
| 1083 | proc.rootIface->doCallback(cb, callbackIsOneway, delayed, kTestString)); |
| 1084 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1085 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1086 | using std::literals::chrono_literals::operator""s; |
| 1087 | std::unique_lock<std::mutex> _l(cb->mMutex); |
| 1088 | cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); }); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1089 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1090 | EXPECT_EQ(cb->mValues.size(), 1) |
| 1091 | << "callIsOneway: " << callIsOneway |
| 1092 | << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed; |
| 1093 | if (cb->mValues.empty()) continue; |
| 1094 | EXPECT_EQ(cb->mValues.at(0), kTestString) |
| 1095 | << "callIsOneway: " << callIsOneway |
| 1096 | << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1097 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1098 | // since we are severing the connection, we need to go ahead and |
| 1099 | // 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] | 1100 | if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) { |
| 1101 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 1102 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1103 | |
Steven Moreland | 1b30429 | 2021-07-15 22:59:34 +0000 | [diff] [blame] | 1104 | // since this session has an incoming connection w/ a threadpool, we |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1105 | // need to manually shut it down |
| 1106 | EXPECT_TRUE(proc.proc.sessions.at(0).session->shutdownAndWait(true)); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1107 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1108 | proc.expectAlreadyShutdown = true; |
| 1109 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 1114 | TEST_P(BinderRpc, OnewayCallbackWithNoThread) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1115 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 1116 | auto cb = sp<MyBinderRpcCallback>::make(); |
| 1117 | |
| 1118 | Status status = proc.rootIface->doCallback(cb, true /*oneway*/, false /*delayed*/, "anything"); |
| 1119 | EXPECT_EQ(WOULD_BLOCK, status.transactionError()); |
| 1120 | } |
| 1121 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1122 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1123 | for (bool doDeathCleanup : {true, false}) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1124 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1125 | |
| 1126 | // make sure there is some state during crash |
| 1127 | // 1. we hold their binder |
| 1128 | sp<IBinderRpcSession> session; |
| 1129 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 1130 | // 2. they hold our binder |
| 1131 | sp<IBinder> binder = new BBinder(); |
| 1132 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 1133 | |
| 1134 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 1135 | << "Do death cleanup: " << doDeathCleanup; |
| 1136 | |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 1137 | proc.expectAlreadyShutdown = true; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1141 | TEST_P(BinderRpc, UseKernelBinderCallingId) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1142 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1143 | |
| 1144 | // we can't allocate IPCThreadState so actually the first time should |
| 1145 | // succeed :( |
| 1146 | EXPECT_OK(proc.rootIface->useKernelBinderCallingId()); |
| 1147 | |
| 1148 | // second time! we catch the error :) |
| 1149 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError()); |
| 1150 | |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 1151 | proc.expectAlreadyShutdown = true; |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 1154 | TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1155 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 1156 | |
| 1157 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 1158 | ASSERT_NE(binder, nullptr); |
| 1159 | |
| 1160 | ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get())); |
| 1161 | } |
| 1162 | |
| 1163 | TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1164 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 1165 | |
| 1166 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 1167 | ASSERT_NE(binder, nullptr); |
| 1168 | |
| 1169 | auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder); |
| 1170 | ASSERT_NE(ndkBinder, nullptr); |
| 1171 | |
| 1172 | std::string out; |
| 1173 | ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out); |
| 1174 | ASSERT_TRUE(status.isOk()) << status.getDescription(); |
| 1175 | ASSERT_EQ("aoeuaoeu", out); |
| 1176 | } |
| 1177 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1178 | ssize_t countFds() { |
| 1179 | DIR* dir = opendir("/proc/self/fd/"); |
| 1180 | if (dir == nullptr) return -1; |
| 1181 | ssize_t ret = 0; |
| 1182 | dirent* ent; |
| 1183 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 1184 | closedir(dir); |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1188 | TEST_P(BinderRpc, Fds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1189 | ssize_t beforeFds = countFds(); |
| 1190 | ASSERT_GE(beforeFds, 0); |
| 1191 | { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1192 | auto proc = createRpcTestSocketServerProcess({.numThreads = 10}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1193 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 1194 | } |
| 1195 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 1196 | } |
| 1197 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1198 | static bool testSupportVsockLoopback() { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1199 | // 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] | 1200 | unsigned int vsockPort = allocateVsockPort(); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1201 | sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make()); |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1202 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
Steven Moreland | 1eab345 | 2021-08-05 16:56:20 -0700 | [diff] [blame] | 1203 | if (status_t status = server->setupVsockServer(vsockPort); status != OK) { |
| 1204 | if (status == -EAFNOSUPPORT) { |
| 1205 | return false; |
| 1206 | } |
| 1207 | LOG_ALWAYS_FATAL("Could not setup vsock server: %s", statusToString(status).c_str()); |
| 1208 | } |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1209 | server->start(); |
| 1210 | |
Yifan Hong | ecf937d | 2021-08-11 17:29:28 -0700 | [diff] [blame^] | 1211 | sp<RpcSession> session = |
| 1212 | RpcSession::make(RpcTransportCtxFactoryRaw::make(), std::nullopt, std::nullopt); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1213 | status_t status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 1214 | while (!server->shutdown()) usleep(10000); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1215 | ALOGE("Detected vsock loopback supported: %s", statusToString(status).c_str()); |
| 1216 | return status == OK; |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | static std::vector<SocketType> testSocketTypes() { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 1220 | std::vector<SocketType> ret = {SocketType::PRECONNECTED, SocketType::UNIX, SocketType::INET}; |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1221 | |
| 1222 | static bool hasVsockLoopback = testSupportVsockLoopback(); |
| 1223 | |
| 1224 | if (hasVsockLoopback) { |
| 1225 | ret.push_back(SocketType::VSOCK); |
| 1226 | } |
| 1227 | |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1231 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
| 1232 | ::testing::Combine(::testing::ValuesIn(testSocketTypes()), |
| 1233 | ::testing::ValuesIn(RpcSecurityValues())), |
| 1234 | BinderRpc::PrintParamInfo); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1235 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1236 | class BinderRpcServerRootObject |
| 1237 | : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {}; |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1238 | |
| 1239 | TEST_P(BinderRpcServerRootObject, WeakRootObject) { |
| 1240 | using SetFn = std::function<void(RpcServer*, sp<IBinder>)>; |
| 1241 | auto setRootObject = [](bool isStrong) -> SetFn { |
| 1242 | return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak); |
| 1243 | }; |
| 1244 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1245 | auto [isStrong1, isStrong2, rpcSecurity] = GetParam(); |
| 1246 | auto server = RpcServer::make(newFactory(rpcSecurity)); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1247 | auto binder1 = sp<BBinder>::make(); |
| 1248 | IBinder* binderRaw1 = binder1.get(); |
| 1249 | setRootObject(isStrong1)(server.get(), binder1); |
| 1250 | EXPECT_EQ(binderRaw1, server->getRootObject()); |
| 1251 | binder1.clear(); |
| 1252 | EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject()); |
| 1253 | |
| 1254 | auto binder2 = sp<BBinder>::make(); |
| 1255 | IBinder* binderRaw2 = binder2.get(); |
| 1256 | setRootObject(isStrong2)(server.get(), binder2); |
| 1257 | EXPECT_EQ(binderRaw2, server->getRootObject()); |
| 1258 | binder2.clear(); |
| 1259 | EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject()); |
| 1260 | } |
| 1261 | |
| 1262 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject, |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1263 | ::testing::Combine(::testing::Bool(), ::testing::Bool(), |
| 1264 | ::testing::ValuesIn(RpcSecurityValues()))); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1265 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1266 | class OneOffSignal { |
| 1267 | public: |
| 1268 | // If notify() was previously called, or is called within |duration|, return true; else false. |
| 1269 | template <typename R, typename P> |
| 1270 | bool wait(std::chrono::duration<R, P> duration) { |
| 1271 | std::unique_lock<std::mutex> lock(mMutex); |
| 1272 | return mCv.wait_for(lock, duration, [this] { return mValue; }); |
| 1273 | } |
| 1274 | void notify() { |
| 1275 | std::unique_lock<std::mutex> lock(mMutex); |
| 1276 | mValue = true; |
| 1277 | lock.unlock(); |
| 1278 | mCv.notify_all(); |
| 1279 | } |
| 1280 | |
| 1281 | private: |
| 1282 | std::mutex mMutex; |
| 1283 | std::condition_variable mCv; |
| 1284 | bool mValue = false; |
| 1285 | }; |
| 1286 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1287 | TEST_P(BinderRpcSimple, Shutdown) { |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1288 | auto addr = allocateSocketAddress(); |
| 1289 | unlink(addr.c_str()); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1290 | auto server = RpcServer::make(newFactory(GetParam())); |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1291 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1292 | ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str())); |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1293 | auto joinEnds = std::make_shared<OneOffSignal>(); |
| 1294 | |
| 1295 | // If things are broken and the thread never stops, don't block other tests. Because the thread |
| 1296 | // may run after the test finishes, it must not access the stack memory of the test. Hence, |
| 1297 | // shared pointers are passed. |
| 1298 | std::thread([server, joinEnds] { |
| 1299 | server->join(); |
| 1300 | joinEnds->notify(); |
| 1301 | }).detach(); |
| 1302 | |
| 1303 | bool shutdown = false; |
| 1304 | for (int i = 0; i < 10 && !shutdown; i++) { |
| 1305 | usleep(300 * 1000); // 300ms; total 3s |
| 1306 | if (server->shutdown()) shutdown = true; |
| 1307 | } |
| 1308 | ASSERT_TRUE(shutdown) << "server->shutdown() never returns true"; |
| 1309 | |
| 1310 | ASSERT_TRUE(joinEnds->wait(2s)) |
| 1311 | << "After server->shutdown() returns true, join() did not stop after 2s"; |
| 1312 | } |
| 1313 | |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1314 | TEST(BinderRpc, Java) { |
| 1315 | #if !defined(__ANDROID__) |
| 1316 | GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on" |
| 1317 | "createRpcDelegateServiceManager() with a device attached, such test belongs " |
| 1318 | "to binderHostDeviceTest. Hence, just disable this test on host."; |
| 1319 | #endif // !__ANDROID__ |
| 1320 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1321 | ASSERT_NE(nullptr, sm); |
| 1322 | // Any Java service with non-empty getInterfaceDescriptor() would do. |
| 1323 | // Let's pick batteryproperties. |
| 1324 | auto binder = sm->checkService(String16("batteryproperties")); |
| 1325 | ASSERT_NE(nullptr, binder); |
| 1326 | auto descriptor = binder->getInterfaceDescriptor(); |
| 1327 | ASSERT_GE(descriptor.size(), 0); |
| 1328 | ASSERT_EQ(OK, binder->pingBinder()); |
| 1329 | |
| 1330 | auto rpcServer = RpcServer::make(); |
| 1331 | rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 1332 | unsigned int port; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1333 | ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port)); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1334 | auto socket = rpcServer->releaseServer(); |
| 1335 | |
| 1336 | auto keepAlive = sp<BBinder>::make(); |
| 1337 | ASSERT_EQ(OK, binder->setRpcClientDebug(std::move(socket), keepAlive)); |
| 1338 | |
| 1339 | auto rpcSession = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1340 | ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port)); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1341 | auto rpcBinder = rpcSession->getRootObject(); |
| 1342 | ASSERT_NE(nullptr, rpcBinder); |
| 1343 | |
| 1344 | ASSERT_EQ(OK, rpcBinder->pingBinder()); |
| 1345 | |
| 1346 | ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor()) |
| 1347 | << "getInterfaceDescriptor should not crash system_server"; |
| 1348 | ASSERT_EQ(OK, rpcBinder->pingBinder()); |
| 1349 | } |
| 1350 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1351 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcSimple, ::testing::ValuesIn(RpcSecurityValues()), |
| 1352 | BinderRpcSimple::PrintTestParam); |
| 1353 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1354 | } // namespace android |
| 1355 | |
| 1356 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1357 | ::testing::InitGoogleTest(&argc, argv); |
| 1358 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
| 1359 | return RUN_ALL_TESTS(); |
| 1360 | } |