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