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