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