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