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