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