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 | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 17 | #include <BnBinderRpcSession.h> |
| 18 | #include <BnBinderRpcTest.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 19 | #include <aidl/IBinderRpcTest.h> |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 20 | #include <android-base/file.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 22 | #include <android/binder_auto_utils.h> |
| 23 | #include <android/binder_libbinder.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 24 | #include <binder/Binder.h> |
| 25 | #include <binder/BpBinder.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/ProcessState.h> |
| 28 | #include <binder/RpcConnection.h> |
| 29 | #include <binder/RpcServer.h> |
| 30 | #include <gtest/gtest.h> |
| 31 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 32 | #include <chrono> |
| 33 | #include <cstdlib> |
| 34 | #include <iostream> |
| 35 | #include <thread> |
| 36 | |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 37 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 38 | #include <linux/vm_sockets.h> |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 39 | #endif //__BIONIC__ |
| 40 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 41 | #include <sys/prctl.h> |
| 42 | #include <unistd.h> |
| 43 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 44 | #include "../RpcState.h" // for debugging |
| 45 | |
| 46 | namespace android { |
| 47 | |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 48 | TEST(BinderRpcParcel, EntireParcelFormatted) { |
| 49 | Parcel p; |
| 50 | p.writeInt32(3); |
| 51 | |
| 52 | EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), ""); |
| 53 | } |
| 54 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 55 | using android::binder::Status; |
| 56 | |
| 57 | #define EXPECT_OK(status) \ |
| 58 | do { \ |
| 59 | Status stat = (status); \ |
| 60 | EXPECT_TRUE(stat.isOk()) << stat; \ |
| 61 | } while (false) |
| 62 | |
| 63 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 64 | public: |
| 65 | static std::atomic<int32_t> gNum; |
| 66 | |
| 67 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 68 | Status getName(std::string* name) override { |
| 69 | *name = mName; |
| 70 | return Status::ok(); |
| 71 | } |
| 72 | ~MyBinderRpcSession() { gNum--; } |
| 73 | |
| 74 | private: |
| 75 | std::string mName; |
| 76 | }; |
| 77 | std::atomic<int32_t> MyBinderRpcSession::gNum; |
| 78 | |
| 79 | class MyBinderRpcTest : public BnBinderRpcTest { |
| 80 | public: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 81 | wp<RpcServer> server; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 82 | |
| 83 | Status sendString(const std::string& str) override { |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 84 | (void)str; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 85 | return Status::ok(); |
| 86 | } |
| 87 | Status doubleString(const std::string& str, std::string* strstr) override { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 88 | *strstr = str + str; |
| 89 | return Status::ok(); |
| 90 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 91 | Status countBinders(std::vector<int32_t>* out) override { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 92 | sp<RpcServer> spServer = server.promote(); |
| 93 | if (spServer == nullptr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 94 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 95 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 96 | out->clear(); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 97 | for (auto connection : spServer->listConnections()) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 98 | size_t count = connection->state()->countBinders(); |
| 99 | if (count != 1) { |
| 100 | // this is called when there is only one binder held remaining, |
| 101 | // so to aid debugging |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 102 | connection->state()->dump(); |
| 103 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 104 | out->push_back(count); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 105 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 106 | return Status::ok(); |
| 107 | } |
| 108 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 109 | if (binder == nullptr) { |
| 110 | std::cout << "Received null binder!" << std::endl; |
| 111 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 112 | } |
| 113 | *out = binder->pingBinder(); |
| 114 | return Status::ok(); |
| 115 | } |
| 116 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 117 | *out = binder; |
| 118 | return Status::ok(); |
| 119 | } |
| 120 | static sp<IBinder> mHeldBinder; |
| 121 | Status holdBinder(const sp<IBinder>& binder) override { |
| 122 | mHeldBinder = binder; |
| 123 | return Status::ok(); |
| 124 | } |
| 125 | Status getHeldBinder(sp<IBinder>* held) override { |
| 126 | *held = mHeldBinder; |
| 127 | return Status::ok(); |
| 128 | } |
| 129 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 130 | if (count <= 0) return Status::ok(); |
| 131 | return binder->nestMe(this, count - 1); |
| 132 | } |
| 133 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 134 | static sp<IBinder> binder = new BBinder; |
| 135 | *out = binder; |
| 136 | return Status::ok(); |
| 137 | } |
| 138 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 139 | *out = new MyBinderRpcSession(name); |
| 140 | return Status::ok(); |
| 141 | } |
| 142 | Status getNumOpenSessions(int32_t* out) override { |
| 143 | *out = MyBinderRpcSession::gNum; |
| 144 | return Status::ok(); |
| 145 | } |
| 146 | |
| 147 | std::mutex blockMutex; |
| 148 | Status lock() override { |
| 149 | blockMutex.lock(); |
| 150 | return Status::ok(); |
| 151 | } |
| 152 | Status unlockInMsAsync(int32_t ms) override { |
| 153 | usleep(ms * 1000); |
| 154 | blockMutex.unlock(); |
| 155 | return Status::ok(); |
| 156 | } |
| 157 | Status lockUnlock() override { |
| 158 | std::lock_guard<std::mutex> _l(blockMutex); |
| 159 | return Status::ok(); |
| 160 | } |
| 161 | |
| 162 | Status sleepMs(int32_t ms) override { |
| 163 | usleep(ms * 1000); |
| 164 | return Status::ok(); |
| 165 | } |
| 166 | |
| 167 | Status sleepMsAsync(int32_t ms) override { |
| 168 | // In-process binder calls are asynchronous, but the call to this method |
| 169 | // is synchronous wrt its client. This in/out-process threading model |
| 170 | // diffentiation is a classic binder leaky abstraction (for better or |
| 171 | // worse) and is preserved here the way binder sockets plugs itself |
| 172 | // into BpBinder, as nothing is changed at the higher levels |
| 173 | // (IInterface) which result in this behavior. |
| 174 | return sleepMs(ms); |
| 175 | } |
| 176 | |
| 177 | Status die(bool cleanup) override { |
| 178 | if (cleanup) { |
| 179 | exit(1); |
| 180 | } else { |
| 181 | _exit(1); |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | sp<IBinder> MyBinderRpcTest::mHeldBinder; |
| 186 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 187 | class Pipe { |
| 188 | public: |
| 189 | Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); } |
| 190 | Pipe(Pipe&&) = default; |
| 191 | android::base::borrowed_fd readEnd() { return mRead; } |
| 192 | android::base::borrowed_fd writeEnd() { return mWrite; } |
| 193 | |
| 194 | private: |
| 195 | android::base::unique_fd mRead; |
| 196 | android::base::unique_fd mWrite; |
| 197 | }; |
| 198 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 199 | class Process { |
| 200 | public: |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 201 | Process(Process&&) = default; |
| 202 | Process(const std::function<void(Pipe*)>& f) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 203 | if (0 == (mPid = fork())) { |
| 204 | // racey: assume parent doesn't crash before this is set |
| 205 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 206 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 207 | f(&mPipe); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | ~Process() { |
| 211 | if (mPid != 0) { |
| 212 | kill(mPid, SIGKILL); |
| 213 | } |
| 214 | } |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 215 | Pipe* getPipe() { return &mPipe; } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 216 | |
| 217 | private: |
| 218 | pid_t mPid = 0; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 219 | Pipe mPipe; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | static std::string allocateSocketAddress() { |
| 223 | static size_t id = 0; |
Steven Moreland | 4bfbf2e | 2021-04-14 22:15:16 +0000 | [diff] [blame] | 224 | std::string temp = getenv("TMPDIR") ?: "/tmp"; |
| 225 | return temp + "/binderRpcTest_" + std::to_string(id++); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | struct ProcessConnection { |
| 229 | // reference to process hosting a socket server |
| 230 | Process host; |
| 231 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 232 | struct ConnectionInfo { |
| 233 | sp<RpcConnection> connection; |
| 234 | sp<IBinder> root; |
| 235 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 236 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 237 | // client connection objects associated with other process |
| 238 | // each one represents a separate connection |
| 239 | std::vector<ConnectionInfo> connections; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 240 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 241 | ProcessConnection(ProcessConnection&&) = default; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 242 | ~ProcessConnection() { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 243 | for (auto& connection : connections) { |
| 244 | connection.root = nullptr; |
| 245 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 246 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 247 | for (auto& info : connections) { |
| 248 | sp<RpcConnection>& connection = info.connection; |
| 249 | |
| 250 | EXPECT_NE(nullptr, connection); |
| 251 | EXPECT_NE(nullptr, connection->state()); |
| 252 | EXPECT_EQ(0, connection->state()->countBinders()) |
| 253 | << (connection->state()->dump(), "dump:"); |
| 254 | |
| 255 | wp<RpcConnection> weakConnection = connection; |
| 256 | connection = nullptr; |
| 257 | EXPECT_EQ(nullptr, weakConnection.promote()) << "Leaked connection"; |
| 258 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 259 | } |
| 260 | }; |
| 261 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 262 | // Process connection where the process hosts IBinderRpcTest, the server used |
| 263 | // for most testing here |
| 264 | struct BinderRpcTestProcessConnection { |
| 265 | ProcessConnection proc; |
| 266 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 267 | // pre-fetched root object (for first connection) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 268 | sp<IBinder> rootBinder; |
| 269 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 270 | // pre-casted root object (for first connection) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 271 | sp<IBinderRpcTest> rootIface; |
| 272 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 273 | // whether connection should be invalidated by end of run |
| 274 | bool expectInvalid = false; |
| 275 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 276 | BinderRpcTestProcessConnection(BinderRpcTestProcessConnection&&) = default; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 277 | ~BinderRpcTestProcessConnection() { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 278 | if (!expectInvalid) { |
| 279 | std::vector<int32_t> remoteCounts; |
| 280 | // calling over any connections counts across all connections |
| 281 | EXPECT_OK(rootIface->countBinders(&remoteCounts)); |
| 282 | EXPECT_EQ(remoteCounts.size(), proc.connections.size()); |
| 283 | for (auto remoteCount : remoteCounts) { |
| 284 | EXPECT_EQ(remoteCount, 1); |
| 285 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | rootIface = nullptr; |
| 289 | rootBinder = nullptr; |
| 290 | } |
| 291 | }; |
| 292 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 293 | enum class SocketType { |
| 294 | UNIX, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 295 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 296 | VSOCK, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 297 | #endif // __BIONIC__ |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 298 | INET, |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 299 | }; |
| 300 | static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) { |
| 301 | switch (info.param) { |
| 302 | case SocketType::UNIX: |
| 303 | return "unix_domain_socket"; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 304 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 305 | case SocketType::VSOCK: |
| 306 | return "vm_socket"; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 307 | #endif // __BIONIC__ |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 308 | case SocketType::INET: |
| 309 | return "inet_socket"; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 310 | default: |
| 311 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 312 | return ""; |
| 313 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 314 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 315 | class BinderRpc : public ::testing::TestWithParam<SocketType> { |
| 316 | public: |
| 317 | // This creates a new process serving an interface on a certain number of |
| 318 | // threads. |
| 319 | ProcessConnection createRpcTestSocketServerProcess( |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 320 | size_t numThreads, size_t numConnections, |
| 321 | const std::function<void(const sp<RpcServer>&)>& configure) { |
| 322 | CHECK_GE(numConnections, 1) << "Must have at least one connection to a server"; |
| 323 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 324 | SocketType socketType = GetParam(); |
| 325 | |
| 326 | std::string addr = allocateSocketAddress(); |
| 327 | unlink(addr.c_str()); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 328 | static unsigned int vsockPort = 3456; |
| 329 | vsockPort++; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 330 | |
| 331 | auto ret = ProcessConnection{ |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 332 | .host = Process([&](Pipe* pipe) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 333 | sp<RpcServer> server = RpcServer::make(); |
| 334 | |
| 335 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 336 | server->setMaxThreads(numThreads); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 337 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 338 | switch (socketType) { |
| 339 | case SocketType::UNIX: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 340 | CHECK(server->setupUnixDomainServer(addr.c_str())) << addr; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 341 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 342 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 343 | case SocketType::VSOCK: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 344 | CHECK(server->setupVsockServer(vsockPort)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 345 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 346 | #endif // __BIONIC__ |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 347 | case SocketType::INET: { |
| 348 | unsigned int outPort = 0; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 349 | CHECK(server->setupInetServer(0, &outPort)); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 350 | CHECK_NE(0, outPort); |
| 351 | CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort, |
| 352 | sizeof(outPort))); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 353 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 354 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 355 | default: |
| 356 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 357 | } |
| 358 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 359 | configure(server); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 360 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 361 | server->join(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 362 | }), |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 365 | unsigned int inetPort = 0; |
| 366 | if (socketType == SocketType::INET) { |
| 367 | CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &inetPort, |
| 368 | sizeof(inetPort))); |
| 369 | CHECK_NE(0, inetPort); |
| 370 | } |
| 371 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 372 | for (size_t i = 0; i < numConnections; i++) { |
| 373 | sp<RpcConnection> connection = RpcConnection::make(); |
| 374 | for (size_t tries = 0; tries < 10; tries++) { |
| 375 | usleep(10000); |
| 376 | switch (socketType) { |
| 377 | case SocketType::UNIX: |
| 378 | if (connection->setupUnixDomainClient(addr.c_str())) goto success; |
| 379 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 380 | #ifdef __BIONIC__ |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 381 | case SocketType::VSOCK: |
| 382 | if (connection->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success; |
| 383 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 384 | #endif // __BIONIC__ |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 385 | case SocketType::INET: |
| 386 | if (connection->setupInetClient("127.0.0.1", inetPort)) goto success; |
| 387 | break; |
| 388 | default: |
| 389 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 390 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 391 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 392 | LOG_ALWAYS_FATAL("Could not connect"); |
| 393 | success: |
| 394 | ret.connections.push_back({connection, connection->getRootObject()}); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 395 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 399 | BinderRpcTestProcessConnection createRpcTestSocketServerProcess(size_t numThreads, |
| 400 | size_t numConnections = 1) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 401 | BinderRpcTestProcessConnection ret{ |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 402 | .proc = createRpcTestSocketServerProcess(numThreads, numConnections, |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 403 | [&](const sp<RpcServer>& server) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 404 | sp<MyBinderRpcTest> service = |
| 405 | new MyBinderRpcTest; |
| 406 | server->setRootObject(service); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 407 | service->server = server; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 408 | }), |
| 409 | }; |
| 410 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 411 | ret.rootBinder = ret.proc.connections.at(0).root; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 412 | ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder); |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | TEST_P(BinderRpc, RootObjectIsNull) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 419 | auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 420 | // this is the default, but to be explicit |
| 421 | server->setRootObject(nullptr); |
| 422 | }); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 423 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 424 | EXPECT_EQ(nullptr, proc.connections.at(0).root); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 427 | TEST_P(BinderRpc, Ping) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 428 | auto proc = createRpcTestSocketServerProcess(1); |
| 429 | ASSERT_NE(proc.rootBinder, nullptr); |
| 430 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 431 | } |
| 432 | |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 433 | TEST_P(BinderRpc, GetInterfaceDescriptor) { |
| 434 | auto proc = createRpcTestSocketServerProcess(1); |
| 435 | ASSERT_NE(proc.rootBinder, nullptr); |
| 436 | EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); |
| 437 | } |
| 438 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 439 | TEST_P(BinderRpc, MultipleConnections) { |
| 440 | auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*connections*/); |
| 441 | for (auto connection : proc.proc.connections) { |
| 442 | ASSERT_NE(nullptr, connection.root); |
| 443 | EXPECT_EQ(OK, connection.root->pingBinder()); |
| 444 | } |
| 445 | } |
| 446 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 447 | TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 448 | auto proc = createRpcTestSocketServerProcess(1); |
| 449 | Parcel data; |
| 450 | Parcel reply; |
| 451 | EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0)); |
| 452 | } |
| 453 | |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 454 | TEST_P(BinderRpc, AppendSeparateFormats) { |
| 455 | auto proc = createRpcTestSocketServerProcess(1); |
| 456 | |
| 457 | Parcel p1; |
| 458 | p1.markForBinder(proc.rootBinder); |
| 459 | p1.writeInt32(3); |
| 460 | |
| 461 | Parcel p2; |
| 462 | |
| 463 | EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize())); |
| 464 | EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize())); |
| 465 | } |
| 466 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 467 | TEST_P(BinderRpc, UnknownTransaction) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 468 | auto proc = createRpcTestSocketServerProcess(1); |
| 469 | Parcel data; |
| 470 | data.markForBinder(proc.rootBinder); |
| 471 | Parcel reply; |
| 472 | EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0)); |
| 473 | } |
| 474 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 475 | TEST_P(BinderRpc, SendSomethingOneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 476 | auto proc = createRpcTestSocketServerProcess(1); |
| 477 | EXPECT_OK(proc.rootIface->sendString("asdf")); |
| 478 | } |
| 479 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 480 | TEST_P(BinderRpc, SendAndGetResultBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 481 | auto proc = createRpcTestSocketServerProcess(1); |
| 482 | std::string doubled; |
| 483 | EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled)); |
| 484 | EXPECT_EQ("cool cool ", doubled); |
| 485 | } |
| 486 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 487 | TEST_P(BinderRpc, SendAndGetResultBackBig) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 488 | auto proc = createRpcTestSocketServerProcess(1); |
| 489 | std::string single = std::string(1024, 'a'); |
| 490 | std::string doubled; |
| 491 | EXPECT_OK(proc.rootIface->doubleString(single, &doubled)); |
| 492 | EXPECT_EQ(single + single, doubled); |
| 493 | } |
| 494 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 495 | TEST_P(BinderRpc, CallMeBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 496 | auto proc = createRpcTestSocketServerProcess(1); |
| 497 | |
| 498 | int32_t pingResult; |
| 499 | EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult)); |
| 500 | EXPECT_EQ(OK, pingResult); |
| 501 | |
| 502 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 503 | } |
| 504 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 505 | TEST_P(BinderRpc, RepeatBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 506 | auto proc = createRpcTestSocketServerProcess(1); |
| 507 | |
| 508 | sp<IBinder> inBinder = new MyBinderRpcSession("foo"); |
| 509 | sp<IBinder> outBinder; |
| 510 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 511 | EXPECT_EQ(inBinder, outBinder); |
| 512 | |
| 513 | wp<IBinder> weak = inBinder; |
| 514 | inBinder = nullptr; |
| 515 | outBinder = nullptr; |
| 516 | |
| 517 | // Force reading a reply, to process any pending dec refs from the other |
| 518 | // process (the other process will process dec refs there before processing |
| 519 | // the ping here). |
| 520 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 521 | |
| 522 | EXPECT_EQ(nullptr, weak.promote()); |
| 523 | |
| 524 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 525 | } |
| 526 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 527 | TEST_P(BinderRpc, RepeatTheirBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 528 | auto proc = createRpcTestSocketServerProcess(1); |
| 529 | |
| 530 | sp<IBinderRpcSession> session; |
| 531 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 532 | |
| 533 | sp<IBinder> inBinder = IInterface::asBinder(session); |
| 534 | sp<IBinder> outBinder; |
| 535 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 536 | EXPECT_EQ(inBinder, outBinder); |
| 537 | |
| 538 | wp<IBinder> weak = inBinder; |
| 539 | session = nullptr; |
| 540 | inBinder = nullptr; |
| 541 | outBinder = nullptr; |
| 542 | |
| 543 | // Force reading a reply, to process any pending dec refs from the other |
| 544 | // process (the other process will process dec refs there before processing |
| 545 | // the ping here). |
| 546 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 547 | |
| 548 | EXPECT_EQ(nullptr, weak.promote()); |
| 549 | } |
| 550 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 551 | TEST_P(BinderRpc, RepeatBinderNull) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 552 | auto proc = createRpcTestSocketServerProcess(1); |
| 553 | |
| 554 | sp<IBinder> outBinder; |
| 555 | EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder)); |
| 556 | EXPECT_EQ(nullptr, outBinder); |
| 557 | } |
| 558 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 559 | TEST_P(BinderRpc, HoldBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 560 | auto proc = createRpcTestSocketServerProcess(1); |
| 561 | |
| 562 | IBinder* ptr = nullptr; |
| 563 | { |
| 564 | sp<IBinder> binder = new BBinder(); |
| 565 | ptr = binder.get(); |
| 566 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 567 | } |
| 568 | |
| 569 | sp<IBinder> held; |
| 570 | EXPECT_OK(proc.rootIface->getHeldBinder(&held)); |
| 571 | |
| 572 | EXPECT_EQ(held.get(), ptr); |
| 573 | |
| 574 | // stop holding binder, because we test to make sure references are cleaned |
| 575 | // up |
| 576 | EXPECT_OK(proc.rootIface->holdBinder(nullptr)); |
| 577 | // and flush ref counts |
| 578 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 579 | } |
| 580 | |
| 581 | // START TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 582 | // These are behavioral differences form regular binder, where certain usecases |
| 583 | // aren't supported. |
| 584 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 585 | TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketConnections) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 586 | auto proc1 = createRpcTestSocketServerProcess(1); |
| 587 | auto proc2 = createRpcTestSocketServerProcess(1); |
| 588 | |
| 589 | sp<IBinder> outBinder; |
| 590 | EXPECT_EQ(INVALID_OPERATION, |
| 591 | proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); |
| 592 | } |
| 593 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 594 | TEST_P(BinderRpc, CannotMixBindersBetweenTwoConnectionsToTheSameServer) { |
| 595 | auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*connections*/); |
| 596 | |
| 597 | sp<IBinder> outBinder; |
| 598 | EXPECT_EQ(INVALID_OPERATION, |
| 599 | proc.rootIface->repeatBinder(proc.proc.connections.at(1).root, &outBinder) |
| 600 | .transactionError()); |
| 601 | } |
| 602 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 603 | TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 604 | auto proc = createRpcTestSocketServerProcess(1); |
| 605 | |
| 606 | sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); |
| 607 | sp<IBinder> outBinder; |
| 608 | EXPECT_EQ(INVALID_OPERATION, |
| 609 | proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError()); |
| 610 | } |
| 611 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 612 | TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 613 | auto proc = createRpcTestSocketServerProcess(1); |
| 614 | |
| 615 | // for historical reasons, IServiceManager interface only returns the |
| 616 | // exception code |
| 617 | EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED, |
| 618 | defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder)); |
| 619 | } |
| 620 | |
| 621 | // END TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 622 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 623 | TEST_P(BinderRpc, RepeatRootObject) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 624 | auto proc = createRpcTestSocketServerProcess(1); |
| 625 | |
| 626 | sp<IBinder> outBinder; |
| 627 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder)); |
| 628 | EXPECT_EQ(proc.rootBinder, outBinder); |
| 629 | } |
| 630 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 631 | TEST_P(BinderRpc, NestedTransactions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 632 | auto proc = createRpcTestSocketServerProcess(1); |
| 633 | |
| 634 | auto nastyNester = sp<MyBinderRpcTest>::make(); |
| 635 | EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10)); |
| 636 | |
| 637 | wp<IBinder> weak = nastyNester; |
| 638 | nastyNester = nullptr; |
| 639 | EXPECT_EQ(nullptr, weak.promote()); |
| 640 | } |
| 641 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 642 | TEST_P(BinderRpc, SameBinderEquality) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 643 | auto proc = createRpcTestSocketServerProcess(1); |
| 644 | |
| 645 | sp<IBinder> a; |
| 646 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 647 | |
| 648 | sp<IBinder> b; |
| 649 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 650 | |
| 651 | EXPECT_EQ(a, b); |
| 652 | } |
| 653 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 654 | TEST_P(BinderRpc, SameBinderEqualityWeak) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 655 | auto proc = createRpcTestSocketServerProcess(1); |
| 656 | |
| 657 | sp<IBinder> a; |
| 658 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 659 | wp<IBinder> weak = a; |
| 660 | a = nullptr; |
| 661 | |
| 662 | sp<IBinder> b; |
| 663 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 664 | |
| 665 | // this is the wrong behavior, since BpBinder |
| 666 | // doesn't implement onIncStrongAttempted |
| 667 | // but make sure there is no crash |
| 668 | EXPECT_EQ(nullptr, weak.promote()); |
| 669 | |
| 670 | GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder."; |
| 671 | |
| 672 | // In order to fix this: |
| 673 | // - need to have incStrongAttempted reflected across IPC boundary (wait for |
| 674 | // response to promote - round trip...) |
| 675 | // - sendOnLastWeakRef, to delete entries out of RpcState table |
| 676 | EXPECT_EQ(b, weak.promote()); |
| 677 | } |
| 678 | |
| 679 | #define expectSessions(expected, iface) \ |
| 680 | do { \ |
| 681 | int session; \ |
| 682 | EXPECT_OK((iface)->getNumOpenSessions(&session)); \ |
| 683 | EXPECT_EQ(expected, session); \ |
| 684 | } while (false) |
| 685 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 686 | TEST_P(BinderRpc, SingleSession) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 687 | auto proc = createRpcTestSocketServerProcess(1); |
| 688 | |
| 689 | sp<IBinderRpcSession> session; |
| 690 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 691 | std::string out; |
| 692 | EXPECT_OK(session->getName(&out)); |
| 693 | EXPECT_EQ("aoeu", out); |
| 694 | |
| 695 | expectSessions(1, proc.rootIface); |
| 696 | session = nullptr; |
| 697 | expectSessions(0, proc.rootIface); |
| 698 | } |
| 699 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 700 | TEST_P(BinderRpc, ManySessions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 701 | auto proc = createRpcTestSocketServerProcess(1); |
| 702 | |
| 703 | std::vector<sp<IBinderRpcSession>> sessions; |
| 704 | |
| 705 | for (size_t i = 0; i < 15; i++) { |
| 706 | expectSessions(i, proc.rootIface); |
| 707 | sp<IBinderRpcSession> session; |
| 708 | EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session)); |
| 709 | sessions.push_back(session); |
| 710 | } |
| 711 | expectSessions(sessions.size(), proc.rootIface); |
| 712 | for (size_t i = 0; i < sessions.size(); i++) { |
| 713 | std::string out; |
| 714 | EXPECT_OK(sessions.at(i)->getName(&out)); |
| 715 | EXPECT_EQ(std::to_string(i), out); |
| 716 | } |
| 717 | expectSessions(sessions.size(), proc.rootIface); |
| 718 | |
| 719 | while (!sessions.empty()) { |
| 720 | sessions.pop_back(); |
| 721 | expectSessions(sessions.size(), proc.rootIface); |
| 722 | } |
| 723 | expectSessions(0, proc.rootIface); |
| 724 | } |
| 725 | |
| 726 | size_t epochMillis() { |
| 727 | using std::chrono::duration_cast; |
| 728 | using std::chrono::milliseconds; |
| 729 | using std::chrono::seconds; |
| 730 | using std::chrono::system_clock; |
| 731 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 732 | } |
| 733 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 734 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 735 | constexpr size_t kNumThreads = 10; |
| 736 | |
| 737 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 738 | |
| 739 | EXPECT_OK(proc.rootIface->lock()); |
| 740 | |
| 741 | // block all but one thread taking locks |
| 742 | std::vector<std::thread> ts; |
| 743 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 744 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 745 | } |
| 746 | |
| 747 | usleep(100000); // give chance for calls on other threads |
| 748 | |
| 749 | // other calls still work |
| 750 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 751 | |
| 752 | constexpr size_t blockTimeMs = 500; |
| 753 | size_t epochMsBefore = epochMillis(); |
| 754 | // after this, we should never see a response within this time |
| 755 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 756 | |
| 757 | // this call should be blocked for blockTimeMs |
| 758 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 759 | |
| 760 | size_t epochMsAfter = epochMillis(); |
| 761 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 762 | |
| 763 | for (auto& t : ts) t.join(); |
| 764 | } |
| 765 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 766 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 767 | constexpr size_t kNumThreads = 10; |
| 768 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 769 | constexpr size_t kSleepMs = 500; |
| 770 | |
| 771 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 772 | |
| 773 | size_t epochMsBefore = epochMillis(); |
| 774 | |
| 775 | std::vector<std::thread> ts; |
| 776 | for (size_t i = 0; i < kNumCalls; i++) { |
| 777 | ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); })); |
| 778 | } |
| 779 | |
| 780 | for (auto& t : ts) t.join(); |
| 781 | |
| 782 | size_t epochMsAfter = epochMillis(); |
| 783 | |
| 784 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs); |
| 785 | |
| 786 | // Potential flake, but make sure calls are handled in parallel. |
| 787 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs); |
| 788 | } |
| 789 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 790 | TEST_P(BinderRpc, ThreadingStressTest) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 791 | constexpr size_t kNumClientThreads = 10; |
| 792 | constexpr size_t kNumServerThreads = 10; |
| 793 | constexpr size_t kNumCalls = 100; |
| 794 | |
| 795 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 796 | |
| 797 | std::vector<std::thread> threads; |
| 798 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 799 | threads.push_back(std::thread([&] { |
| 800 | for (size_t j = 0; j < kNumCalls; j++) { |
| 801 | sp<IBinder> out; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 802 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 803 | EXPECT_EQ(proc.rootBinder, out); |
| 804 | } |
| 805 | })); |
| 806 | } |
| 807 | |
| 808 | for (auto& t : threads) t.join(); |
| 809 | } |
| 810 | |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 811 | TEST_P(BinderRpc, OnewayStressTest) { |
| 812 | constexpr size_t kNumClientThreads = 10; |
| 813 | constexpr size_t kNumServerThreads = 10; |
| 814 | constexpr size_t kNumCalls = 100; |
| 815 | |
| 816 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 817 | |
| 818 | std::vector<std::thread> threads; |
| 819 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 820 | threads.push_back(std::thread([&] { |
| 821 | for (size_t j = 0; j < kNumCalls; j++) { |
| 822 | EXPECT_OK(proc.rootIface->sendString("a")); |
| 823 | } |
| 824 | |
| 825 | // check threads are not stuck |
| 826 | EXPECT_OK(proc.rootIface->sleepMs(250)); |
| 827 | })); |
| 828 | } |
| 829 | |
| 830 | for (auto& t : threads) t.join(); |
| 831 | } |
| 832 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 833 | TEST_P(BinderRpc, OnewayCallDoesNotWait) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 834 | constexpr size_t kReallyLongTimeMs = 100; |
| 835 | constexpr size_t kSleepMs = kReallyLongTimeMs * 5; |
| 836 | |
| 837 | // more than one thread, just so this doesn't deadlock |
| 838 | auto proc = createRpcTestSocketServerProcess(2); |
| 839 | |
| 840 | size_t epochMsBefore = epochMillis(); |
| 841 | |
| 842 | EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs)); |
| 843 | |
| 844 | size_t epochMsAfter = epochMillis(); |
| 845 | EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); |
| 846 | } |
| 847 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 848 | TEST_P(BinderRpc, OnewayCallQueueing) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 849 | constexpr size_t kNumSleeps = 10; |
| 850 | constexpr size_t kNumExtraServerThreads = 4; |
| 851 | constexpr size_t kSleepMs = 50; |
| 852 | |
| 853 | // make sure calls to the same object happen on the same thread |
| 854 | auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads); |
| 855 | |
| 856 | EXPECT_OK(proc.rootIface->lock()); |
| 857 | |
| 858 | for (size_t i = 0; i < kNumSleeps; i++) { |
| 859 | // these should be processed serially |
| 860 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 861 | } |
| 862 | // should also be processesed serially |
| 863 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 864 | |
| 865 | size_t epochMsBefore = epochMillis(); |
| 866 | EXPECT_OK(proc.rootIface->lockUnlock()); |
| 867 | size_t epochMsAfter = epochMillis(); |
| 868 | |
| 869 | EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
| 870 | } |
| 871 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 872 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 873 | for (bool doDeathCleanup : {true, false}) { |
| 874 | auto proc = createRpcTestSocketServerProcess(1); |
| 875 | |
| 876 | // make sure there is some state during crash |
| 877 | // 1. we hold their binder |
| 878 | sp<IBinderRpcSession> session; |
| 879 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 880 | // 2. they hold our binder |
| 881 | sp<IBinder> binder = new BBinder(); |
| 882 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 883 | |
| 884 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 885 | << "Do death cleanup: " << doDeathCleanup; |
| 886 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 887 | proc.expectInvalid = true; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 891 | TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { |
| 892 | auto proc = createRpcTestSocketServerProcess(1); |
| 893 | |
| 894 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 895 | ASSERT_NE(binder, nullptr); |
| 896 | |
| 897 | ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get())); |
| 898 | } |
| 899 | |
| 900 | TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { |
| 901 | auto proc = createRpcTestSocketServerProcess(1); |
| 902 | |
| 903 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 904 | ASSERT_NE(binder, nullptr); |
| 905 | |
| 906 | auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder); |
| 907 | ASSERT_NE(ndkBinder, nullptr); |
| 908 | |
| 909 | std::string out; |
| 910 | ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out); |
| 911 | ASSERT_TRUE(status.isOk()) << status.getDescription(); |
| 912 | ASSERT_EQ("aoeuaoeu", out); |
| 913 | } |
| 914 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 915 | ssize_t countFds() { |
| 916 | DIR* dir = opendir("/proc/self/fd/"); |
| 917 | if (dir == nullptr) return -1; |
| 918 | ssize_t ret = 0; |
| 919 | dirent* ent; |
| 920 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 921 | closedir(dir); |
| 922 | return ret; |
| 923 | } |
| 924 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 925 | TEST_P(BinderRpc, Fds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 926 | ssize_t beforeFds = countFds(); |
| 927 | ASSERT_GE(beforeFds, 0); |
| 928 | { |
| 929 | auto proc = createRpcTestSocketServerProcess(10); |
| 930 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 931 | } |
| 932 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 933 | } |
| 934 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 935 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 936 | ::testing::ValuesIn({ |
| 937 | SocketType::UNIX, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 938 | #ifdef __BIONIC__ |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 939 | SocketType::VSOCK, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 940 | #endif // __BIONIC__ |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 941 | SocketType::INET, |
| 942 | }), |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 943 | PrintSocketType); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 944 | |
| 945 | } // namespace android |
| 946 | |
| 947 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 948 | ::testing::InitGoogleTest(&argc, argv); |
| 949 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
| 950 | return RUN_ALL_TESTS(); |
| 951 | } |