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