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