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