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