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 | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame^] | 330 | unsigned int outPort = 0; |
| 331 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 332 | switch (socketType) { |
| 333 | case SocketType::UNIX: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 334 | CHECK(server->setupUnixDomainServer(addr.c_str())) << addr; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 335 | break; |
| 336 | case SocketType::VSOCK: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 337 | CHECK(server->setupVsockServer(vsockPort)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 338 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 339 | case SocketType::INET: { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 340 | CHECK(server->setupInetServer(0, &outPort)); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 341 | CHECK_NE(0, outPort); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 342 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 343 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 344 | default: |
| 345 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 346 | } |
| 347 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame^] | 348 | CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort, sizeof(outPort))); |
| 349 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 350 | configure(server); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 351 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 352 | server->join(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 353 | }), |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame^] | 356 | // always read socket, so that we have waited for the server to start |
| 357 | unsigned int outPort = 0; |
| 358 | CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &outPort, sizeof(outPort))); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 359 | if (socketType == SocketType::INET) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame^] | 360 | CHECK_NE(0, outPort); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 363 | for (size_t i = 0; i < numSessions; i++) { |
| 364 | sp<RpcSession> session = RpcSession::make(); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame^] | 365 | switch (socketType) { |
| 366 | case SocketType::UNIX: |
| 367 | if (session->setupUnixDomainClient(addr.c_str())) goto success; |
| 368 | break; |
| 369 | case SocketType::VSOCK: |
| 370 | if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success; |
| 371 | break; |
| 372 | case SocketType::INET: |
| 373 | if (session->setupInetClient("127.0.0.1", outPort)) goto success; |
| 374 | break; |
| 375 | default: |
| 376 | LOG_ALWAYS_FATAL("Unknown socket type"); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 377 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 378 | LOG_ALWAYS_FATAL("Could not connect"); |
| 379 | success: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 380 | ret.sessions.push_back({session, session->getRootObject()}); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 381 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 382 | return ret; |
| 383 | } |
| 384 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 385 | BinderRpcTestProcessSession createRpcTestSocketServerProcess(size_t numThreads, |
| 386 | size_t numSessions = 1) { |
| 387 | BinderRpcTestProcessSession ret{ |
| 388 | .proc = createRpcTestSocketServerProcess(numThreads, numSessions, |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 389 | [&](const sp<RpcServer>& server) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 390 | sp<MyBinderRpcTest> service = |
| 391 | new MyBinderRpcTest; |
| 392 | server->setRootObject(service); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 393 | service->server = server; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 394 | }), |
| 395 | }; |
| 396 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 397 | ret.rootBinder = ret.proc.sessions.at(0).root; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 398 | ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder); |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | TEST_P(BinderRpc, RootObjectIsNull) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 405 | auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 406 | // this is the default, but to be explicit |
| 407 | server->setRootObject(nullptr); |
| 408 | }); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 409 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 410 | EXPECT_EQ(nullptr, proc.sessions.at(0).root); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 413 | TEST_P(BinderRpc, Ping) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 414 | auto proc = createRpcTestSocketServerProcess(1); |
| 415 | ASSERT_NE(proc.rootBinder, nullptr); |
| 416 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 417 | } |
| 418 | |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 419 | TEST_P(BinderRpc, GetInterfaceDescriptor) { |
| 420 | auto proc = createRpcTestSocketServerProcess(1); |
| 421 | ASSERT_NE(proc.rootBinder, nullptr); |
| 422 | EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); |
| 423 | } |
| 424 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 425 | TEST_P(BinderRpc, MultipleSessions) { |
| 426 | auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*sessions*/); |
| 427 | for (auto session : proc.proc.sessions) { |
| 428 | ASSERT_NE(nullptr, session.root); |
| 429 | EXPECT_EQ(OK, session.root->pingBinder()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 433 | TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 434 | auto proc = createRpcTestSocketServerProcess(1); |
| 435 | Parcel data; |
| 436 | Parcel reply; |
| 437 | EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0)); |
| 438 | } |
| 439 | |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 440 | TEST_P(BinderRpc, AppendSeparateFormats) { |
| 441 | auto proc = createRpcTestSocketServerProcess(1); |
| 442 | |
| 443 | Parcel p1; |
| 444 | p1.markForBinder(proc.rootBinder); |
| 445 | p1.writeInt32(3); |
| 446 | |
| 447 | Parcel p2; |
| 448 | |
| 449 | EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize())); |
| 450 | EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize())); |
| 451 | } |
| 452 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 453 | TEST_P(BinderRpc, UnknownTransaction) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 454 | auto proc = createRpcTestSocketServerProcess(1); |
| 455 | Parcel data; |
| 456 | data.markForBinder(proc.rootBinder); |
| 457 | Parcel reply; |
| 458 | EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0)); |
| 459 | } |
| 460 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 461 | TEST_P(BinderRpc, SendSomethingOneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 462 | auto proc = createRpcTestSocketServerProcess(1); |
| 463 | EXPECT_OK(proc.rootIface->sendString("asdf")); |
| 464 | } |
| 465 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 466 | TEST_P(BinderRpc, SendAndGetResultBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 467 | auto proc = createRpcTestSocketServerProcess(1); |
| 468 | std::string doubled; |
| 469 | EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled)); |
| 470 | EXPECT_EQ("cool cool ", doubled); |
| 471 | } |
| 472 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 473 | TEST_P(BinderRpc, SendAndGetResultBackBig) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 474 | auto proc = createRpcTestSocketServerProcess(1); |
| 475 | std::string single = std::string(1024, 'a'); |
| 476 | std::string doubled; |
| 477 | EXPECT_OK(proc.rootIface->doubleString(single, &doubled)); |
| 478 | EXPECT_EQ(single + single, doubled); |
| 479 | } |
| 480 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 481 | TEST_P(BinderRpc, CallMeBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 482 | auto proc = createRpcTestSocketServerProcess(1); |
| 483 | |
| 484 | int32_t pingResult; |
| 485 | EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult)); |
| 486 | EXPECT_EQ(OK, pingResult); |
| 487 | |
| 488 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 489 | } |
| 490 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 491 | TEST_P(BinderRpc, RepeatBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 492 | auto proc = createRpcTestSocketServerProcess(1); |
| 493 | |
| 494 | sp<IBinder> inBinder = new MyBinderRpcSession("foo"); |
| 495 | sp<IBinder> outBinder; |
| 496 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 497 | EXPECT_EQ(inBinder, outBinder); |
| 498 | |
| 499 | wp<IBinder> weak = inBinder; |
| 500 | inBinder = nullptr; |
| 501 | outBinder = nullptr; |
| 502 | |
| 503 | // Force reading a reply, to process any pending dec refs from the other |
| 504 | // process (the other process will process dec refs there before processing |
| 505 | // the ping here). |
| 506 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 507 | |
| 508 | EXPECT_EQ(nullptr, weak.promote()); |
| 509 | |
| 510 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 511 | } |
| 512 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 513 | TEST_P(BinderRpc, RepeatTheirBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 514 | auto proc = createRpcTestSocketServerProcess(1); |
| 515 | |
| 516 | sp<IBinderRpcSession> session; |
| 517 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 518 | |
| 519 | sp<IBinder> inBinder = IInterface::asBinder(session); |
| 520 | sp<IBinder> outBinder; |
| 521 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 522 | EXPECT_EQ(inBinder, outBinder); |
| 523 | |
| 524 | wp<IBinder> weak = inBinder; |
| 525 | session = nullptr; |
| 526 | inBinder = nullptr; |
| 527 | outBinder = nullptr; |
| 528 | |
| 529 | // Force reading a reply, to process any pending dec refs from the other |
| 530 | // process (the other process will process dec refs there before processing |
| 531 | // the ping here). |
| 532 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 533 | |
| 534 | EXPECT_EQ(nullptr, weak.promote()); |
| 535 | } |
| 536 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 537 | TEST_P(BinderRpc, RepeatBinderNull) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 538 | auto proc = createRpcTestSocketServerProcess(1); |
| 539 | |
| 540 | sp<IBinder> outBinder; |
| 541 | EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder)); |
| 542 | EXPECT_EQ(nullptr, outBinder); |
| 543 | } |
| 544 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 545 | TEST_P(BinderRpc, HoldBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 546 | auto proc = createRpcTestSocketServerProcess(1); |
| 547 | |
| 548 | IBinder* ptr = nullptr; |
| 549 | { |
| 550 | sp<IBinder> binder = new BBinder(); |
| 551 | ptr = binder.get(); |
| 552 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 553 | } |
| 554 | |
| 555 | sp<IBinder> held; |
| 556 | EXPECT_OK(proc.rootIface->getHeldBinder(&held)); |
| 557 | |
| 558 | EXPECT_EQ(held.get(), ptr); |
| 559 | |
| 560 | // stop holding binder, because we test to make sure references are cleaned |
| 561 | // up |
| 562 | EXPECT_OK(proc.rootIface->holdBinder(nullptr)); |
| 563 | // and flush ref counts |
| 564 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 565 | } |
| 566 | |
| 567 | // START TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 568 | // These are behavioral differences form regular binder, where certain usecases |
| 569 | // aren't supported. |
| 570 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 571 | TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 572 | auto proc1 = createRpcTestSocketServerProcess(1); |
| 573 | auto proc2 = createRpcTestSocketServerProcess(1); |
| 574 | |
| 575 | sp<IBinder> outBinder; |
| 576 | EXPECT_EQ(INVALID_OPERATION, |
| 577 | proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); |
| 578 | } |
| 579 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 580 | TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { |
| 581 | auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*sessions*/); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 582 | |
| 583 | sp<IBinder> outBinder; |
| 584 | EXPECT_EQ(INVALID_OPERATION, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 585 | proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder) |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 586 | .transactionError()); |
| 587 | } |
| 588 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 589 | TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 590 | auto proc = createRpcTestSocketServerProcess(1); |
| 591 | |
| 592 | sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); |
| 593 | sp<IBinder> outBinder; |
| 594 | EXPECT_EQ(INVALID_OPERATION, |
| 595 | proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError()); |
| 596 | } |
| 597 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 598 | TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 599 | auto proc = createRpcTestSocketServerProcess(1); |
| 600 | |
| 601 | // for historical reasons, IServiceManager interface only returns the |
| 602 | // exception code |
| 603 | EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED, |
| 604 | defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder)); |
| 605 | } |
| 606 | |
| 607 | // END TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 608 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 609 | TEST_P(BinderRpc, RepeatRootObject) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 610 | auto proc = createRpcTestSocketServerProcess(1); |
| 611 | |
| 612 | sp<IBinder> outBinder; |
| 613 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder)); |
| 614 | EXPECT_EQ(proc.rootBinder, outBinder); |
| 615 | } |
| 616 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 617 | TEST_P(BinderRpc, NestedTransactions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 618 | auto proc = createRpcTestSocketServerProcess(1); |
| 619 | |
| 620 | auto nastyNester = sp<MyBinderRpcTest>::make(); |
| 621 | EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10)); |
| 622 | |
| 623 | wp<IBinder> weak = nastyNester; |
| 624 | nastyNester = nullptr; |
| 625 | EXPECT_EQ(nullptr, weak.promote()); |
| 626 | } |
| 627 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 628 | TEST_P(BinderRpc, SameBinderEquality) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 629 | auto proc = createRpcTestSocketServerProcess(1); |
| 630 | |
| 631 | sp<IBinder> a; |
| 632 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 633 | |
| 634 | sp<IBinder> b; |
| 635 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 636 | |
| 637 | EXPECT_EQ(a, b); |
| 638 | } |
| 639 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 640 | TEST_P(BinderRpc, SameBinderEqualityWeak) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 641 | auto proc = createRpcTestSocketServerProcess(1); |
| 642 | |
| 643 | sp<IBinder> a; |
| 644 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 645 | wp<IBinder> weak = a; |
| 646 | a = nullptr; |
| 647 | |
| 648 | sp<IBinder> b; |
| 649 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 650 | |
| 651 | // this is the wrong behavior, since BpBinder |
| 652 | // doesn't implement onIncStrongAttempted |
| 653 | // but make sure there is no crash |
| 654 | EXPECT_EQ(nullptr, weak.promote()); |
| 655 | |
| 656 | GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder."; |
| 657 | |
| 658 | // In order to fix this: |
| 659 | // - need to have incStrongAttempted reflected across IPC boundary (wait for |
| 660 | // response to promote - round trip...) |
| 661 | // - sendOnLastWeakRef, to delete entries out of RpcState table |
| 662 | EXPECT_EQ(b, weak.promote()); |
| 663 | } |
| 664 | |
| 665 | #define expectSessions(expected, iface) \ |
| 666 | do { \ |
| 667 | int session; \ |
| 668 | EXPECT_OK((iface)->getNumOpenSessions(&session)); \ |
| 669 | EXPECT_EQ(expected, session); \ |
| 670 | } while (false) |
| 671 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 672 | TEST_P(BinderRpc, SingleSession) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 673 | auto proc = createRpcTestSocketServerProcess(1); |
| 674 | |
| 675 | sp<IBinderRpcSession> session; |
| 676 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 677 | std::string out; |
| 678 | EXPECT_OK(session->getName(&out)); |
| 679 | EXPECT_EQ("aoeu", out); |
| 680 | |
| 681 | expectSessions(1, proc.rootIface); |
| 682 | session = nullptr; |
| 683 | expectSessions(0, proc.rootIface); |
| 684 | } |
| 685 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 686 | TEST_P(BinderRpc, ManySessions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 687 | auto proc = createRpcTestSocketServerProcess(1); |
| 688 | |
| 689 | std::vector<sp<IBinderRpcSession>> sessions; |
| 690 | |
| 691 | for (size_t i = 0; i < 15; i++) { |
| 692 | expectSessions(i, proc.rootIface); |
| 693 | sp<IBinderRpcSession> session; |
| 694 | EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session)); |
| 695 | sessions.push_back(session); |
| 696 | } |
| 697 | expectSessions(sessions.size(), proc.rootIface); |
| 698 | for (size_t i = 0; i < sessions.size(); i++) { |
| 699 | std::string out; |
| 700 | EXPECT_OK(sessions.at(i)->getName(&out)); |
| 701 | EXPECT_EQ(std::to_string(i), out); |
| 702 | } |
| 703 | expectSessions(sessions.size(), proc.rootIface); |
| 704 | |
| 705 | while (!sessions.empty()) { |
| 706 | sessions.pop_back(); |
| 707 | expectSessions(sessions.size(), proc.rootIface); |
| 708 | } |
| 709 | expectSessions(0, proc.rootIface); |
| 710 | } |
| 711 | |
| 712 | size_t epochMillis() { |
| 713 | using std::chrono::duration_cast; |
| 714 | using std::chrono::milliseconds; |
| 715 | using std::chrono::seconds; |
| 716 | using std::chrono::system_clock; |
| 717 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 718 | } |
| 719 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 720 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 721 | constexpr size_t kNumThreads = 10; |
| 722 | |
| 723 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 724 | |
| 725 | EXPECT_OK(proc.rootIface->lock()); |
| 726 | |
| 727 | // block all but one thread taking locks |
| 728 | std::vector<std::thread> ts; |
| 729 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 730 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 731 | } |
| 732 | |
| 733 | usleep(100000); // give chance for calls on other threads |
| 734 | |
| 735 | // other calls still work |
| 736 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 737 | |
| 738 | constexpr size_t blockTimeMs = 500; |
| 739 | size_t epochMsBefore = epochMillis(); |
| 740 | // after this, we should never see a response within this time |
| 741 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 742 | |
| 743 | // this call should be blocked for blockTimeMs |
| 744 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 745 | |
| 746 | size_t epochMsAfter = epochMillis(); |
| 747 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 748 | |
| 749 | for (auto& t : ts) t.join(); |
| 750 | } |
| 751 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 752 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 753 | constexpr size_t kNumThreads = 10; |
| 754 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 755 | constexpr size_t kSleepMs = 500; |
| 756 | |
| 757 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 758 | |
| 759 | size_t epochMsBefore = epochMillis(); |
| 760 | |
| 761 | std::vector<std::thread> ts; |
| 762 | for (size_t i = 0; i < kNumCalls; i++) { |
| 763 | ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); })); |
| 764 | } |
| 765 | |
| 766 | for (auto& t : ts) t.join(); |
| 767 | |
| 768 | size_t epochMsAfter = epochMillis(); |
| 769 | |
| 770 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs); |
| 771 | |
| 772 | // Potential flake, but make sure calls are handled in parallel. |
| 773 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs); |
| 774 | } |
| 775 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 776 | TEST_P(BinderRpc, ThreadingStressTest) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 777 | constexpr size_t kNumClientThreads = 10; |
| 778 | constexpr size_t kNumServerThreads = 10; |
| 779 | constexpr size_t kNumCalls = 100; |
| 780 | |
| 781 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 782 | |
| 783 | std::vector<std::thread> threads; |
| 784 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 785 | threads.push_back(std::thread([&] { |
| 786 | for (size_t j = 0; j < kNumCalls; j++) { |
| 787 | sp<IBinder> out; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 788 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 789 | EXPECT_EQ(proc.rootBinder, out); |
| 790 | } |
| 791 | })); |
| 792 | } |
| 793 | |
| 794 | for (auto& t : threads) t.join(); |
| 795 | } |
| 796 | |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 797 | TEST_P(BinderRpc, OnewayStressTest) { |
| 798 | constexpr size_t kNumClientThreads = 10; |
| 799 | constexpr size_t kNumServerThreads = 10; |
| 800 | constexpr size_t kNumCalls = 100; |
| 801 | |
| 802 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 803 | |
| 804 | std::vector<std::thread> threads; |
| 805 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 806 | threads.push_back(std::thread([&] { |
| 807 | for (size_t j = 0; j < kNumCalls; j++) { |
| 808 | EXPECT_OK(proc.rootIface->sendString("a")); |
| 809 | } |
| 810 | |
| 811 | // check threads are not stuck |
| 812 | EXPECT_OK(proc.rootIface->sleepMs(250)); |
| 813 | })); |
| 814 | } |
| 815 | |
| 816 | for (auto& t : threads) t.join(); |
| 817 | } |
| 818 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 819 | TEST_P(BinderRpc, OnewayCallDoesNotWait) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 820 | constexpr size_t kReallyLongTimeMs = 100; |
| 821 | constexpr size_t kSleepMs = kReallyLongTimeMs * 5; |
| 822 | |
| 823 | // more than one thread, just so this doesn't deadlock |
| 824 | auto proc = createRpcTestSocketServerProcess(2); |
| 825 | |
| 826 | size_t epochMsBefore = epochMillis(); |
| 827 | |
| 828 | EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs)); |
| 829 | |
| 830 | size_t epochMsAfter = epochMillis(); |
| 831 | EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); |
| 832 | } |
| 833 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 834 | TEST_P(BinderRpc, OnewayCallQueueing) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 835 | constexpr size_t kNumSleeps = 10; |
| 836 | constexpr size_t kNumExtraServerThreads = 4; |
| 837 | constexpr size_t kSleepMs = 50; |
| 838 | |
| 839 | // make sure calls to the same object happen on the same thread |
| 840 | auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads); |
| 841 | |
| 842 | EXPECT_OK(proc.rootIface->lock()); |
| 843 | |
| 844 | for (size_t i = 0; i < kNumSleeps; i++) { |
| 845 | // these should be processed serially |
| 846 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 847 | } |
| 848 | // should also be processesed serially |
| 849 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 850 | |
| 851 | size_t epochMsBefore = epochMillis(); |
| 852 | EXPECT_OK(proc.rootIface->lockUnlock()); |
| 853 | size_t epochMsAfter = epochMillis(); |
| 854 | |
| 855 | EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
| 856 | } |
| 857 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 858 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 859 | for (bool doDeathCleanup : {true, false}) { |
| 860 | auto proc = createRpcTestSocketServerProcess(1); |
| 861 | |
| 862 | // make sure there is some state during crash |
| 863 | // 1. we hold their binder |
| 864 | sp<IBinderRpcSession> session; |
| 865 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 866 | // 2. they hold our binder |
| 867 | sp<IBinder> binder = new BBinder(); |
| 868 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 869 | |
| 870 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 871 | << "Do death cleanup: " << doDeathCleanup; |
| 872 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 873 | proc.expectInvalid = true; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 877 | TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { |
| 878 | auto proc = createRpcTestSocketServerProcess(1); |
| 879 | |
| 880 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 881 | ASSERT_NE(binder, nullptr); |
| 882 | |
| 883 | ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get())); |
| 884 | } |
| 885 | |
| 886 | TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { |
| 887 | auto proc = createRpcTestSocketServerProcess(1); |
| 888 | |
| 889 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 890 | ASSERT_NE(binder, nullptr); |
| 891 | |
| 892 | auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder); |
| 893 | ASSERT_NE(ndkBinder, nullptr); |
| 894 | |
| 895 | std::string out; |
| 896 | ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out); |
| 897 | ASSERT_TRUE(status.isOk()) << status.getDescription(); |
| 898 | ASSERT_EQ("aoeuaoeu", out); |
| 899 | } |
| 900 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 901 | ssize_t countFds() { |
| 902 | DIR* dir = opendir("/proc/self/fd/"); |
| 903 | if (dir == nullptr) return -1; |
| 904 | ssize_t ret = 0; |
| 905 | dirent* ent; |
| 906 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 907 | closedir(dir); |
| 908 | return ret; |
| 909 | } |
| 910 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 911 | TEST_P(BinderRpc, Fds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 912 | ssize_t beforeFds = countFds(); |
| 913 | ASSERT_GE(beforeFds, 0); |
| 914 | { |
| 915 | auto proc = createRpcTestSocketServerProcess(10); |
| 916 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 917 | } |
| 918 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 919 | } |
| 920 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 921 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 922 | ::testing::ValuesIn({ |
| 923 | SocketType::UNIX, |
Steven Moreland | bd5002b | 2021-05-04 23:12:56 +0000 | [diff] [blame] | 924 | // TODO(b/185269356): working on host |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 925 | #ifdef __BIONIC__ |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 926 | SocketType::VSOCK, |
Steven Moreland | bd5002b | 2021-05-04 23:12:56 +0000 | [diff] [blame] | 927 | #endif |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 928 | SocketType::INET, |
| 929 | }), |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 930 | PrintSocketType); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 931 | |
| 932 | } // namespace android |
| 933 | |
| 934 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 935 | ::testing::InitGoogleTest(&argc, argv); |
| 936 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
| 937 | return RUN_ALL_TESTS(); |
| 938 | } |