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 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 17 | #include <BinderRpcTestClientInfo.h> |
| 18 | #include <BinderRpcTestServerInfo.h> |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 19 | #include <BnBinderRpcCallback.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 20 | #include <BnBinderRpcSession.h> |
| 21 | #include <BnBinderRpcTest.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 22 | #include <aidl/IBinderRpcTest.h> |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 23 | #include <android-base/file.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 26 | #include <android-base/stringprintf.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 27 | #include <android/binder_auto_utils.h> |
| 28 | #include <android/binder_libbinder.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 29 | #include <binder/Binder.h> |
| 30 | #include <binder/BpBinder.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 31 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 32 | #include <binder/IServiceManager.h> |
| 33 | #include <binder/ProcessState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | #include <binder/RpcServer.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 35 | #include <binder/RpcSession.h> |
Yifan Hong | e0e5328 | 2021-09-23 18:37:21 -0700 | [diff] [blame] | 36 | #include <binder/RpcTlsTestUtils.h> |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 37 | #include <binder/RpcTlsUtils.h> |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 38 | #include <binder/RpcTransport.h> |
| 39 | #include <binder/RpcTransportRaw.h> |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 40 | #include <binder/RpcTransportTls.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 41 | #include <gtest/gtest.h> |
| 42 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 43 | #include <chrono> |
| 44 | #include <cstdlib> |
| 45 | #include <iostream> |
| 46 | #include <thread> |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 47 | #include <type_traits> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 48 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 49 | #include <poll.h> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 50 | #include <sys/prctl.h> |
| 51 | #include <unistd.h> |
| 52 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 53 | #include "../FdTrigger.h" |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 54 | #include "../RpcSocketAddress.h" // for testing preconnected clients |
Yifan Hong | ffdaf95 | 2021-09-17 18:08:38 -0700 | [diff] [blame] | 55 | #include "../RpcState.h" // for debugging |
| 56 | #include "../vm_sockets.h" // for VMADDR_* |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 57 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 58 | using namespace std::chrono_literals; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 59 | using namespace std::placeholders; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 60 | using testing::AssertionFailure; |
| 61 | using testing::AssertionResult; |
| 62 | using testing::AssertionSuccess; |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 63 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 64 | namespace android { |
| 65 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 66 | static_assert(RPC_WIRE_PROTOCOL_VERSION + 1 == RPC_WIRE_PROTOCOL_VERSION_NEXT || |
| 67 | RPC_WIRE_PROTOCOL_VERSION == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); |
Devin Moore | f3b9c4f | 2021-08-03 15:50:13 +0000 | [diff] [blame] | 68 | const char* kLocalInetAddress = "127.0.0.1"; |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 69 | |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 70 | enum class RpcSecurity { RAW, TLS }; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 71 | |
| 72 | static inline std::vector<RpcSecurity> RpcSecurityValues() { |
Yifan Hong | 9240975 | 2021-07-30 21:25:32 -0700 | [diff] [blame] | 73 | return {RpcSecurity::RAW, RpcSecurity::TLS}; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 76 | static inline std::unique_ptr<RpcTransportCtxFactory> newFactory( |
Yifan Hong | ffdaf95 | 2021-09-17 18:08:38 -0700 | [diff] [blame] | 77 | RpcSecurity rpcSecurity, std::shared_ptr<RpcCertificateVerifier> verifier = nullptr, |
| 78 | std::unique_ptr<RpcAuth> auth = nullptr) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 79 | switch (rpcSecurity) { |
| 80 | case RpcSecurity::RAW: |
| 81 | return RpcTransportCtxFactoryRaw::make(); |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 82 | case RpcSecurity::TLS: { |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 83 | if (verifier == nullptr) { |
| 84 | verifier = std::make_shared<RpcCertificateVerifierSimple>(); |
| 85 | } |
Yifan Hong | ffdaf95 | 2021-09-17 18:08:38 -0700 | [diff] [blame] | 86 | if (auth == nullptr) { |
| 87 | auth = std::make_unique<RpcAuthSelfSigned>(); |
| 88 | } |
| 89 | return RpcTransportCtxFactoryTls::make(std::move(verifier), std::move(auth)); |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 90 | } |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 91 | default: |
| 92 | LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity); |
| 93 | } |
| 94 | } |
| 95 | |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 96 | TEST(BinderRpcParcel, EntireParcelFormatted) { |
| 97 | Parcel p; |
| 98 | p.writeInt32(3); |
| 99 | |
| 100 | EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), ""); |
| 101 | } |
| 102 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 103 | class BinderRpcServerOnly : public ::testing::TestWithParam<std::tuple<RpcSecurity, uint32_t>> { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 104 | public: |
| 105 | static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 106 | return std::string(newFactory(std::get<0>(info.param))->toCString()) + "_serverV" + |
| 107 | std::to_string(std::get<1>(info.param)); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 111 | TEST_P(BinderRpcServerOnly, SetExternalServerTest) { |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 112 | base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 113 | int sinkFd = sink.get(); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 114 | auto server = RpcServer::make(newFactory(std::get<0>(GetParam()))); |
| 115 | server->setProtocolVersion(std::get<1>(GetParam())); |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 116 | ASSERT_FALSE(server->hasServer()); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 117 | ASSERT_EQ(OK, server->setupExternalServer(std::move(sink))); |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 118 | ASSERT_TRUE(server->hasServer()); |
| 119 | base::unique_fd retrieved = server->releaseServer(); |
| 120 | ASSERT_FALSE(server->hasServer()); |
| 121 | ASSERT_EQ(sinkFd, retrieved.get()); |
| 122 | } |
| 123 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 124 | TEST(BinderRpc, CannotUseNextWireVersion) { |
| 125 | auto session = RpcSession::make(); |
| 126 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT)); |
| 127 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 1)); |
| 128 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 2)); |
| 129 | EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 15)); |
| 130 | } |
| 131 | |
| 132 | TEST(BinderRpc, CanUseExperimentalWireVersion) { |
| 133 | auto session = RpcSession::make(); |
| 134 | EXPECT_TRUE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL)); |
| 135 | } |
| 136 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 137 | using android::binder::Status; |
| 138 | |
| 139 | #define EXPECT_OK(status) \ |
| 140 | do { \ |
| 141 | Status stat = (status); \ |
| 142 | EXPECT_TRUE(stat.isOk()) << stat; \ |
| 143 | } while (false) |
| 144 | |
| 145 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 146 | public: |
| 147 | static std::atomic<int32_t> gNum; |
| 148 | |
| 149 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 150 | Status getName(std::string* name) override { |
| 151 | *name = mName; |
| 152 | return Status::ok(); |
| 153 | } |
| 154 | ~MyBinderRpcSession() { gNum--; } |
| 155 | |
| 156 | private: |
| 157 | std::string mName; |
| 158 | }; |
| 159 | std::atomic<int32_t> MyBinderRpcSession::gNum; |
| 160 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 161 | class MyBinderRpcCallback : public BnBinderRpcCallback { |
| 162 | Status sendCallback(const std::string& value) { |
| 163 | std::unique_lock _l(mMutex); |
| 164 | mValues.push_back(value); |
| 165 | _l.unlock(); |
| 166 | mCv.notify_one(); |
| 167 | return Status::ok(); |
| 168 | } |
| 169 | Status sendOnewayCallback(const std::string& value) { return sendCallback(value); } |
| 170 | |
| 171 | public: |
| 172 | std::mutex mMutex; |
| 173 | std::condition_variable mCv; |
| 174 | std::vector<std::string> mValues; |
| 175 | }; |
| 176 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 177 | class MyBinderRpcTest : public BnBinderRpcTest { |
| 178 | public: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 179 | wp<RpcServer> server; |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 180 | int port = 0; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 181 | |
| 182 | Status sendString(const std::string& str) override { |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 183 | (void)str; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 184 | return Status::ok(); |
| 185 | } |
| 186 | Status doubleString(const std::string& str, std::string* strstr) override { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 187 | *strstr = str + str; |
| 188 | return Status::ok(); |
| 189 | } |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 190 | Status getClientPort(int* out) override { |
| 191 | *out = port; |
| 192 | return Status::ok(); |
| 193 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 194 | Status countBinders(std::vector<int32_t>* out) override { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 195 | sp<RpcServer> spServer = server.promote(); |
| 196 | if (spServer == nullptr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 197 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 198 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 199 | out->clear(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 200 | for (auto session : spServer->listSessions()) { |
| 201 | size_t count = session->state()->countBinders(); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 202 | out->push_back(count); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 203 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 204 | return Status::ok(); |
| 205 | } |
Frederick Mayle | ae9deeb | 2022-06-23 23:42:08 +0000 | [diff] [blame] | 206 | Status getNullBinder(sp<IBinder>* out) override { |
| 207 | out->clear(); |
| 208 | return Status::ok(); |
| 209 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 210 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 211 | if (binder == nullptr) { |
| 212 | std::cout << "Received null binder!" << std::endl; |
| 213 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 214 | } |
| 215 | *out = binder->pingBinder(); |
| 216 | return Status::ok(); |
| 217 | } |
| 218 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 219 | *out = binder; |
| 220 | return Status::ok(); |
| 221 | } |
| 222 | static sp<IBinder> mHeldBinder; |
| 223 | Status holdBinder(const sp<IBinder>& binder) override { |
| 224 | mHeldBinder = binder; |
| 225 | return Status::ok(); |
| 226 | } |
| 227 | Status getHeldBinder(sp<IBinder>* held) override { |
| 228 | *held = mHeldBinder; |
| 229 | return Status::ok(); |
| 230 | } |
| 231 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 232 | if (count <= 0) return Status::ok(); |
| 233 | return binder->nestMe(this, count - 1); |
| 234 | } |
| 235 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 236 | static sp<IBinder> binder = new BBinder; |
| 237 | *out = binder; |
| 238 | return Status::ok(); |
| 239 | } |
| 240 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 241 | *out = new MyBinderRpcSession(name); |
| 242 | return Status::ok(); |
| 243 | } |
| 244 | Status getNumOpenSessions(int32_t* out) override { |
| 245 | *out = MyBinderRpcSession::gNum; |
| 246 | return Status::ok(); |
| 247 | } |
| 248 | |
| 249 | std::mutex blockMutex; |
| 250 | Status lock() override { |
| 251 | blockMutex.lock(); |
| 252 | return Status::ok(); |
| 253 | } |
| 254 | Status unlockInMsAsync(int32_t ms) override { |
| 255 | usleep(ms * 1000); |
| 256 | blockMutex.unlock(); |
| 257 | return Status::ok(); |
| 258 | } |
| 259 | Status lockUnlock() override { |
| 260 | std::lock_guard<std::mutex> _l(blockMutex); |
| 261 | return Status::ok(); |
| 262 | } |
| 263 | |
| 264 | Status sleepMs(int32_t ms) override { |
| 265 | usleep(ms * 1000); |
| 266 | return Status::ok(); |
| 267 | } |
| 268 | |
| 269 | Status sleepMsAsync(int32_t ms) override { |
| 270 | // In-process binder calls are asynchronous, but the call to this method |
| 271 | // is synchronous wrt its client. This in/out-process threading model |
| 272 | // diffentiation is a classic binder leaky abstraction (for better or |
| 273 | // worse) and is preserved here the way binder sockets plugs itself |
| 274 | // into BpBinder, as nothing is changed at the higher levels |
| 275 | // (IInterface) which result in this behavior. |
| 276 | return sleepMs(ms); |
| 277 | } |
| 278 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 279 | Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 280 | const std::string& value) override { |
| 281 | if (callback == nullptr) { |
| 282 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 283 | } |
| 284 | |
| 285 | if (delayed) { |
| 286 | std::thread([=]() { |
| 287 | ALOGE("Executing delayed callback: '%s'", value.c_str()); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 288 | Status status = doCallback(callback, oneway, false, value); |
| 289 | ALOGE("Delayed callback status: '%s'", status.toString8().c_str()); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 290 | }).detach(); |
| 291 | return Status::ok(); |
| 292 | } |
| 293 | |
| 294 | if (oneway) { |
| 295 | return callback->sendOnewayCallback(value); |
| 296 | } |
| 297 | |
| 298 | return callback->sendCallback(value); |
| 299 | } |
| 300 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 301 | Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed, |
| 302 | const std::string& value) override { |
| 303 | return doCallback(callback, oneway, delayed, value); |
| 304 | } |
| 305 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 306 | Status die(bool cleanup) override { |
| 307 | if (cleanup) { |
| 308 | exit(1); |
| 309 | } else { |
| 310 | _exit(1); |
| 311 | } |
| 312 | } |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 313 | |
| 314 | Status scheduleShutdown() override { |
| 315 | sp<RpcServer> strongServer = server.promote(); |
| 316 | if (strongServer == nullptr) { |
| 317 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 318 | } |
| 319 | std::thread([=] { |
| 320 | LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown"); |
| 321 | }).detach(); |
| 322 | return Status::ok(); |
| 323 | } |
| 324 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 325 | Status useKernelBinderCallingId() override { |
| 326 | // this is WRONG! It does not make sense when using RPC binder, and |
| 327 | // because it is SO wrong, and so much code calls this, it should abort! |
| 328 | |
| 329 | (void)IPCThreadState::self()->getCallingPid(); |
| 330 | return Status::ok(); |
| 331 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 332 | }; |
| 333 | sp<IBinder> MyBinderRpcTest::mHeldBinder; |
| 334 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 335 | static std::string WaitStatusToString(int wstatus) { |
| 336 | if (WIFEXITED(wstatus)) { |
| 337 | return base::StringPrintf("exit status %d", WEXITSTATUS(wstatus)); |
| 338 | } |
| 339 | if (WIFSIGNALED(wstatus)) { |
| 340 | return base::StringPrintf("term signal %d", WTERMSIG(wstatus)); |
| 341 | } |
| 342 | return base::StringPrintf("unexpected state %d", wstatus); |
| 343 | } |
| 344 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 345 | class Process { |
| 346 | public: |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 347 | Process(Process&&) = default; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 348 | Process(const std::function<void(android::base::borrowed_fd /* writeEnd */, |
| 349 | android::base::borrowed_fd /* readEnd */)>& f) { |
| 350 | android::base::unique_fd childWriteEnd; |
| 351 | android::base::unique_fd childReadEnd; |
| 352 | CHECK(android::base::Pipe(&mReadEnd, &childWriteEnd)) << strerror(errno); |
| 353 | CHECK(android::base::Pipe(&childReadEnd, &mWriteEnd)) << strerror(errno); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 354 | if (0 == (mPid = fork())) { |
| 355 | // racey: assume parent doesn't crash before this is set |
| 356 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 357 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 358 | f(childWriteEnd, childReadEnd); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 359 | |
| 360 | exit(0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | ~Process() { |
| 364 | if (mPid != 0) { |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 365 | int wstatus; |
| 366 | waitpid(mPid, &wstatus, 0); |
| 367 | if (mCustomExitStatusCheck) { |
| 368 | mCustomExitStatusCheck(wstatus); |
| 369 | } else { |
| 370 | EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0) |
| 371 | << "server process failed: " << WaitStatusToString(wstatus); |
| 372 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 375 | android::base::borrowed_fd readEnd() { return mReadEnd; } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 376 | android::base::borrowed_fd writeEnd() { return mWriteEnd; } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 377 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 378 | void setCustomExitStatusCheck(std::function<void(int wstatus)> f) { |
| 379 | mCustomExitStatusCheck = std::move(f); |
| 380 | } |
| 381 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 382 | private: |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 383 | std::function<void(int wstatus)> mCustomExitStatusCheck; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 384 | pid_t mPid = 0; |
Yifan Hong | 0f58fb9 | 2021-06-16 16:09:23 -0700 | [diff] [blame] | 385 | android::base::unique_fd mReadEnd; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 386 | android::base::unique_fd mWriteEnd; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | static std::string allocateSocketAddress() { |
| 390 | static size_t id = 0; |
Steven Moreland | 4bfbf2e | 2021-04-14 22:15:16 +0000 | [diff] [blame] | 391 | std::string temp = getenv("TMPDIR") ?: "/tmp"; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 392 | auto ret = temp + "/binderRpcTest_" + std::to_string(id++); |
| 393 | unlink(ret.c_str()); |
| 394 | return ret; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 397 | static unsigned int allocateVsockPort() { |
| 398 | static unsigned int vsockPort = 3456; |
| 399 | return vsockPort++; |
| 400 | } |
| 401 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 402 | struct ProcessSession { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 403 | // reference to process hosting a socket server |
| 404 | Process host; |
| 405 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 406 | struct SessionInfo { |
| 407 | sp<RpcSession> session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 408 | sp<IBinder> root; |
| 409 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 410 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 411 | // client session objects associated with other process |
| 412 | // each one represents a separate session |
| 413 | std::vector<SessionInfo> sessions; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 414 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 415 | ProcessSession(ProcessSession&&) = default; |
| 416 | ~ProcessSession() { |
| 417 | for (auto& session : sessions) { |
| 418 | session.root = nullptr; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 419 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 420 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 421 | for (auto& info : sessions) { |
| 422 | sp<RpcSession>& session = info.session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 423 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 424 | EXPECT_NE(nullptr, session); |
| 425 | EXPECT_NE(nullptr, session->state()); |
| 426 | EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:"); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 427 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 428 | wp<RpcSession> weakSession = session; |
| 429 | session = nullptr; |
| 430 | EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session"; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 431 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 432 | } |
| 433 | }; |
| 434 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 435 | // Process session where the process hosts IBinderRpcTest, the server used |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 436 | // for most testing here |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 437 | struct BinderRpcTestProcessSession { |
| 438 | ProcessSession proc; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 439 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 440 | // pre-fetched root object (for first session) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 441 | sp<IBinder> rootBinder; |
| 442 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 443 | // pre-casted root object (for first session) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 444 | sp<IBinderRpcTest> rootIface; |
| 445 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 446 | // whether session should be invalidated by end of run |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 447 | bool expectAlreadyShutdown = false; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 448 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 449 | BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default; |
| 450 | ~BinderRpcTestProcessSession() { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 451 | EXPECT_NE(nullptr, rootIface); |
| 452 | if (rootIface == nullptr) return; |
| 453 | |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 454 | if (!expectAlreadyShutdown) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 455 | std::vector<int32_t> remoteCounts; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 456 | // calling over any sessions counts across all sessions |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 457 | EXPECT_OK(rootIface->countBinders(&remoteCounts)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 458 | EXPECT_EQ(remoteCounts.size(), proc.sessions.size()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 459 | for (auto remoteCount : remoteCounts) { |
| 460 | EXPECT_EQ(remoteCount, 1); |
| 461 | } |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 462 | |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 463 | // even though it is on another thread, shutdown races with |
| 464 | // the transaction reply being written |
| 465 | if (auto status = rootIface->scheduleShutdown(); !status.isOk()) { |
| 466 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 467 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | rootIface = nullptr; |
| 471 | rootBinder = nullptr; |
| 472 | } |
| 473 | }; |
| 474 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 475 | enum class SocketType { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 476 | PRECONNECTED, |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 477 | UNIX, |
| 478 | VSOCK, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 479 | INET, |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 480 | }; |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 481 | static inline std::string PrintToString(SocketType socketType) { |
| 482 | switch (socketType) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 483 | case SocketType::PRECONNECTED: |
| 484 | return "preconnected_uds"; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 485 | case SocketType::UNIX: |
| 486 | return "unix_domain_socket"; |
| 487 | case SocketType::VSOCK: |
| 488 | return "vm_socket"; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 489 | case SocketType::INET: |
| 490 | return "inet_socket"; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 491 | default: |
| 492 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 493 | return ""; |
| 494 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 495 | } |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 496 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 497 | static base::unique_fd connectTo(const RpcSocketAddress& addr) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 498 | base::unique_fd serverFd( |
| 499 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 500 | int savedErrno = errno; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 501 | CHECK(serverFd.ok()) << "Could not create socket " << addr.toString() << ": " |
| 502 | << strerror(savedErrno); |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 503 | |
| 504 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 505 | int savedErrno = errno; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 506 | LOG(FATAL) << "Could not connect to socket " << addr.toString() << ": " |
| 507 | << strerror(savedErrno); |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 508 | } |
| 509 | return serverFd; |
| 510 | } |
| 511 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 512 | class BinderRpc |
| 513 | : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity, uint32_t, uint32_t>> { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 514 | public: |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 515 | struct Options { |
| 516 | size_t numThreads = 1; |
| 517 | size_t numSessions = 1; |
| 518 | size_t numIncomingConnections = 0; |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 519 | size_t numOutgoingConnections = SIZE_MAX; |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 520 | }; |
| 521 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 522 | static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 523 | auto [type, security, clientVersion, serverVersion] = info.param; |
| 524 | return PrintToString(type) + "_" + newFactory(security)->toCString() + "_clientV" + |
| 525 | std::to_string(clientVersion) + "_serverV" + std::to_string(serverVersion); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 528 | static inline void writeString(android::base::borrowed_fd fd, std::string_view str) { |
| 529 | uint64_t length = str.length(); |
| 530 | CHECK(android::base::WriteFully(fd, &length, sizeof(length))); |
| 531 | CHECK(android::base::WriteFully(fd, str.data(), str.length())); |
| 532 | } |
| 533 | |
| 534 | static inline std::string readString(android::base::borrowed_fd fd) { |
| 535 | uint64_t length; |
| 536 | CHECK(android::base::ReadFully(fd, &length, sizeof(length))); |
| 537 | std::string ret(length, '\0'); |
| 538 | CHECK(android::base::ReadFully(fd, ret.data(), length)); |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | static inline void writeToFd(android::base::borrowed_fd fd, const Parcelable& parcelable) { |
| 543 | Parcel parcel; |
| 544 | CHECK_EQ(OK, parcelable.writeToParcel(&parcel)); |
| 545 | writeString(fd, |
| 546 | std::string(reinterpret_cast<const char*>(parcel.data()), parcel.dataSize())); |
| 547 | } |
| 548 | |
| 549 | template <typename T> |
| 550 | static inline T readFromFd(android::base::borrowed_fd fd) { |
| 551 | std::string data = readString(fd); |
| 552 | Parcel parcel; |
| 553 | CHECK_EQ(OK, parcel.setData(reinterpret_cast<const uint8_t*>(data.data()), data.size())); |
| 554 | T object; |
| 555 | CHECK_EQ(OK, object.readFromParcel(&parcel)); |
| 556 | return object; |
| 557 | } |
| 558 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 559 | // This creates a new process serving an interface on a certain number of |
| 560 | // threads. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 561 | ProcessSession createRpcTestSocketServerProcess( |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 562 | const Options& options, const std::function<void(const sp<RpcServer>&)>& configure) { |
| 563 | CHECK_GE(options.numSessions, 1) << "Must have at least one session to a server"; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 564 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 565 | SocketType socketType = std::get<0>(GetParam()); |
| 566 | RpcSecurity rpcSecurity = std::get<1>(GetParam()); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 567 | uint32_t clientVersion = std::get<2>(GetParam()); |
| 568 | uint32_t serverVersion = std::get<3>(GetParam()); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 569 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 570 | unsigned int vsockPort = allocateVsockPort(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 571 | std::string addr = allocateSocketAddress(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 572 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 573 | auto ret = ProcessSession{ |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 574 | .host = Process([=](android::base::borrowed_fd writeEnd, |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 575 | android::base::borrowed_fd readEnd) { |
| 576 | auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>(); |
| 577 | sp<RpcServer> server = RpcServer::make(newFactory(rpcSecurity, certVerifier)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 578 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 579 | server->setProtocolVersion(serverVersion); |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 580 | server->setMaxThreads(options.numThreads); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 581 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 582 | unsigned int outPort = 0; |
| 583 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 584 | switch (socketType) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 585 | case SocketType::PRECONNECTED: |
| 586 | [[fallthrough]]; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 587 | case SocketType::UNIX: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 588 | CHECK_EQ(OK, server->setupUnixDomainServer(addr.c_str())) << addr; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 589 | break; |
| 590 | case SocketType::VSOCK: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 591 | CHECK_EQ(OK, server->setupVsockServer(vsockPort)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 592 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 593 | case SocketType::INET: { |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 594 | CHECK_EQ(OK, server->setupInetServer(kLocalInetAddress, 0, &outPort)); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 595 | CHECK_NE(0, outPort); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 596 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 597 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 598 | default: |
| 599 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 600 | } |
| 601 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 602 | BinderRpcTestServerInfo serverInfo; |
| 603 | serverInfo.port = static_cast<int64_t>(outPort); |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 604 | serverInfo.cert.data = server->getCertificate(RpcCertificateFormat::PEM); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 605 | writeToFd(writeEnd, serverInfo); |
| 606 | auto clientInfo = readFromFd<BinderRpcTestClientInfo>(readEnd); |
| 607 | |
| 608 | if (rpcSecurity == RpcSecurity::TLS) { |
| 609 | for (const auto& clientCert : clientInfo.certs) { |
| 610 | CHECK_EQ(OK, |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 611 | certVerifier |
| 612 | ->addTrustedPeerCertificate(RpcCertificateFormat::PEM, |
| 613 | clientCert.data)); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 616 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 617 | configure(server); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 618 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 619 | server->join(); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 620 | |
| 621 | // Another thread calls shutdown. Wait for it to complete. |
| 622 | (void)server->shutdown(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 623 | }), |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 626 | std::vector<sp<RpcSession>> sessions; |
| 627 | auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>(); |
| 628 | for (size_t i = 0; i < options.numSessions; i++) { |
| 629 | sessions.emplace_back(RpcSession::make(newFactory(rpcSecurity, certVerifier))); |
| 630 | } |
| 631 | |
| 632 | auto serverInfo = readFromFd<BinderRpcTestServerInfo>(ret.host.readEnd()); |
| 633 | BinderRpcTestClientInfo clientInfo; |
| 634 | for (const auto& session : sessions) { |
| 635 | auto& parcelableCert = clientInfo.certs.emplace_back(); |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 636 | parcelableCert.data = session->getCertificate(RpcCertificateFormat::PEM); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 637 | } |
| 638 | writeToFd(ret.host.writeEnd(), clientInfo); |
| 639 | |
| 640 | CHECK_LE(serverInfo.port, std::numeric_limits<unsigned int>::max()); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 641 | if (socketType == SocketType::INET) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 642 | CHECK_NE(0, serverInfo.port); |
| 643 | } |
| 644 | |
| 645 | if (rpcSecurity == RpcSecurity::TLS) { |
| 646 | const auto& serverCert = serverInfo.cert.data; |
| 647 | CHECK_EQ(OK, |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 648 | certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM, |
| 649 | serverCert)); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 652 | status_t status; |
| 653 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 654 | for (const auto& session : sessions) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 655 | CHECK(session->setProtocolVersion(clientVersion)); |
Yifan Hong | 1042306 | 2021-10-08 16:26:32 -0700 | [diff] [blame] | 656 | session->setMaxIncomingThreads(options.numIncomingConnections); |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 657 | session->setMaxOutgoingThreads(options.numOutgoingConnections); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 658 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 659 | switch (socketType) { |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 660 | case SocketType::PRECONNECTED: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 661 | status = session->setupPreconnectedClient({}, [=]() { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 662 | return connectTo(UnixSocketAddress(addr.c_str())); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 663 | }); |
Steven Moreland | 4198a12 | 2021-08-03 17:37:58 -0700 | [diff] [blame] | 664 | break; |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 665 | case SocketType::UNIX: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 666 | status = session->setupUnixDomainClient(addr.c_str()); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 667 | break; |
| 668 | case SocketType::VSOCK: |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 669 | status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 670 | break; |
| 671 | case SocketType::INET: |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 672 | status = session->setupInetClient("127.0.0.1", serverInfo.port); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 673 | break; |
| 674 | default: |
| 675 | LOG_ALWAYS_FATAL("Unknown socket type"); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 676 | } |
Steven Moreland | 8a1a47d | 2021-09-14 10:54:04 -0700 | [diff] [blame] | 677 | CHECK_EQ(status, OK) << "Could not connect: " << statusToString(status); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 678 | ret.sessions.push_back({session, session->getRootObject()}); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 679 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 680 | return ret; |
| 681 | } |
| 682 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 683 | BinderRpcTestProcessSession createRpcTestSocketServerProcess(const Options& options) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 684 | BinderRpcTestProcessSession ret{ |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 685 | .proc = createRpcTestSocketServerProcess( |
| 686 | options, |
| 687 | [&](const sp<RpcServer>& server) { |
Andrei Homescu | 86124ca | 2022-04-21 22:22:48 +0000 | [diff] [blame] | 688 | server->setPerSessionRootObject([&](const void* addrPtr, size_t len) { |
| 689 | // UNIX sockets with abstract addresses return |
| 690 | // sizeof(sa_family_t)==2 in addrlen |
| 691 | CHECK_GE(len, sizeof(sa_family_t)); |
| 692 | const sockaddr* addr = reinterpret_cast<const sockaddr*>(addrPtr); |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 693 | sp<MyBinderRpcTest> service = sp<MyBinderRpcTest>::make(); |
| 694 | switch (addr->sa_family) { |
| 695 | case AF_UNIX: |
| 696 | // nothing to save |
| 697 | break; |
| 698 | case AF_VSOCK: |
| 699 | CHECK_EQ(len, sizeof(sockaddr_vm)); |
| 700 | service->port = reinterpret_cast<const sockaddr_vm*>(addr) |
| 701 | ->svm_port; |
| 702 | break; |
| 703 | case AF_INET: |
| 704 | CHECK_EQ(len, sizeof(sockaddr_in)); |
Steven Moreland | 6a0dc96 | 2021-10-25 15:35:43 -0700 | [diff] [blame] | 705 | service->port = |
| 706 | ntohs(reinterpret_cast<const sockaddr_in*>(addr) |
| 707 | ->sin_port); |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 708 | break; |
| 709 | case AF_INET6: |
| 710 | CHECK_EQ(len, sizeof(sockaddr_in)); |
Steven Moreland | 6a0dc96 | 2021-10-25 15:35:43 -0700 | [diff] [blame] | 711 | service->port = |
| 712 | ntohs(reinterpret_cast<const sockaddr_in6*>(addr) |
| 713 | ->sin6_port); |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 714 | break; |
| 715 | default: |
| 716 | LOG_ALWAYS_FATAL("Unrecognized address family %d", |
| 717 | addr->sa_family); |
| 718 | } |
| 719 | service->server = server; |
| 720 | return service; |
| 721 | }); |
| 722 | }), |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 723 | }; |
| 724 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 725 | ret.rootBinder = ret.proc.sessions.at(0).root; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 726 | ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder); |
| 727 | |
| 728 | return ret; |
| 729 | } |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 730 | |
| 731 | void testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t numCalls, |
| 732 | size_t sleepMs = 500); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 733 | }; |
| 734 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 735 | TEST_P(BinderRpc, Ping) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 736 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 737 | ASSERT_NE(proc.rootBinder, nullptr); |
| 738 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 739 | } |
| 740 | |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 741 | TEST_P(BinderRpc, GetInterfaceDescriptor) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 742 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 743 | ASSERT_NE(proc.rootBinder, nullptr); |
| 744 | EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); |
| 745 | } |
| 746 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 747 | TEST_P(BinderRpc, MultipleSessions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 748 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5}); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 749 | for (auto session : proc.proc.sessions) { |
| 750 | ASSERT_NE(nullptr, session.root); |
| 751 | EXPECT_EQ(OK, session.root->pingBinder()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Steven Moreland | 51c44a9 | 2021-10-14 16:50:35 -0700 | [diff] [blame] | 755 | TEST_P(BinderRpc, SeparateRootObject) { |
| 756 | SocketType type = std::get<0>(GetParam()); |
| 757 | if (type == SocketType::PRECONNECTED || type == SocketType::UNIX) { |
| 758 | // we can't get port numbers for unix sockets |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | auto proc = createRpcTestSocketServerProcess({.numSessions = 2}); |
| 763 | |
| 764 | int port1 = 0; |
| 765 | EXPECT_OK(proc.rootIface->getClientPort(&port1)); |
| 766 | |
| 767 | sp<IBinderRpcTest> rootIface2 = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root); |
| 768 | int port2; |
| 769 | EXPECT_OK(rootIface2->getClientPort(&port2)); |
| 770 | |
| 771 | // we should have a different IBinderRpcTest object created for each |
| 772 | // session, because we use setPerSessionRootObject |
| 773 | EXPECT_NE(port1, port2); |
| 774 | } |
| 775 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 776 | TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 777 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 778 | Parcel data; |
| 779 | Parcel reply; |
| 780 | EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0)); |
| 781 | } |
| 782 | |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 783 | TEST_P(BinderRpc, AppendSeparateFormats) { |
Steven Moreland | 2034eff | 2021-10-13 11:24:35 -0700 | [diff] [blame] | 784 | auto proc1 = createRpcTestSocketServerProcess({}); |
| 785 | auto proc2 = createRpcTestSocketServerProcess({}); |
| 786 | |
| 787 | Parcel pRaw; |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 788 | |
| 789 | Parcel p1; |
Steven Moreland | 2034eff | 2021-10-13 11:24:35 -0700 | [diff] [blame] | 790 | p1.markForBinder(proc1.rootBinder); |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 791 | p1.writeInt32(3); |
| 792 | |
Frederick Mayle | a4ed567 | 2022-06-17 22:03:38 +0000 | [diff] [blame] | 793 | EXPECT_EQ(BAD_TYPE, p1.appendFrom(&pRaw, 0, pRaw.dataSize())); |
Steven Moreland | 2034eff | 2021-10-13 11:24:35 -0700 | [diff] [blame] | 794 | EXPECT_EQ(BAD_TYPE, pRaw.appendFrom(&p1, 0, p1.dataSize())); |
| 795 | |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 796 | Parcel p2; |
Steven Moreland | 2034eff | 2021-10-13 11:24:35 -0700 | [diff] [blame] | 797 | p2.markForBinder(proc2.rootBinder); |
| 798 | p2.writeInt32(7); |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 799 | |
| 800 | EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize())); |
| 801 | EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize())); |
| 802 | } |
| 803 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 804 | TEST_P(BinderRpc, UnknownTransaction) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 805 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 806 | Parcel data; |
| 807 | data.markForBinder(proc.rootBinder); |
| 808 | Parcel reply; |
| 809 | EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0)); |
| 810 | } |
| 811 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 812 | TEST_P(BinderRpc, SendSomethingOneway) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 813 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 814 | EXPECT_OK(proc.rootIface->sendString("asdf")); |
| 815 | } |
| 816 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 817 | TEST_P(BinderRpc, SendAndGetResultBack) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 818 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 819 | std::string doubled; |
| 820 | EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled)); |
| 821 | EXPECT_EQ("cool cool ", doubled); |
| 822 | } |
| 823 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 824 | TEST_P(BinderRpc, SendAndGetResultBackBig) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 825 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 826 | std::string single = std::string(1024, 'a'); |
| 827 | std::string doubled; |
| 828 | EXPECT_OK(proc.rootIface->doubleString(single, &doubled)); |
| 829 | EXPECT_EQ(single + single, doubled); |
| 830 | } |
| 831 | |
Frederick Mayle | ae9deeb | 2022-06-23 23:42:08 +0000 | [diff] [blame] | 832 | TEST_P(BinderRpc, InvalidNullBinderReturn) { |
| 833 | auto proc = createRpcTestSocketServerProcess({}); |
| 834 | |
| 835 | sp<IBinder> outBinder; |
| 836 | EXPECT_EQ(proc.rootIface->getNullBinder(&outBinder).transactionError(), UNEXPECTED_NULL); |
| 837 | } |
| 838 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 839 | TEST_P(BinderRpc, CallMeBack) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 840 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 841 | |
| 842 | int32_t pingResult; |
| 843 | EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult)); |
| 844 | EXPECT_EQ(OK, pingResult); |
| 845 | |
| 846 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 847 | } |
| 848 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 849 | TEST_P(BinderRpc, RepeatBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 850 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 851 | |
| 852 | sp<IBinder> inBinder = new MyBinderRpcSession("foo"); |
| 853 | sp<IBinder> outBinder; |
| 854 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 855 | EXPECT_EQ(inBinder, outBinder); |
| 856 | |
| 857 | wp<IBinder> weak = inBinder; |
| 858 | inBinder = nullptr; |
| 859 | outBinder = nullptr; |
| 860 | |
| 861 | // Force reading a reply, to process any pending dec refs from the other |
| 862 | // process (the other process will process dec refs there before processing |
| 863 | // the ping here). |
| 864 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 865 | |
| 866 | EXPECT_EQ(nullptr, weak.promote()); |
| 867 | |
| 868 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 869 | } |
| 870 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 871 | TEST_P(BinderRpc, RepeatTheirBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 872 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 873 | |
| 874 | sp<IBinderRpcSession> session; |
| 875 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 876 | |
| 877 | sp<IBinder> inBinder = IInterface::asBinder(session); |
| 878 | sp<IBinder> outBinder; |
| 879 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 880 | EXPECT_EQ(inBinder, outBinder); |
| 881 | |
| 882 | wp<IBinder> weak = inBinder; |
| 883 | session = nullptr; |
| 884 | inBinder = nullptr; |
| 885 | outBinder = nullptr; |
| 886 | |
| 887 | // Force reading a reply, to process any pending dec refs from the other |
| 888 | // process (the other process will process dec refs there before processing |
| 889 | // the ping here). |
| 890 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 891 | |
| 892 | EXPECT_EQ(nullptr, weak.promote()); |
| 893 | } |
| 894 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 895 | TEST_P(BinderRpc, RepeatBinderNull) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 896 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 897 | |
| 898 | sp<IBinder> outBinder; |
| 899 | EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder)); |
| 900 | EXPECT_EQ(nullptr, outBinder); |
| 901 | } |
| 902 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 903 | TEST_P(BinderRpc, HoldBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 904 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 905 | |
| 906 | IBinder* ptr = nullptr; |
| 907 | { |
| 908 | sp<IBinder> binder = new BBinder(); |
| 909 | ptr = binder.get(); |
| 910 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 911 | } |
| 912 | |
| 913 | sp<IBinder> held; |
| 914 | EXPECT_OK(proc.rootIface->getHeldBinder(&held)); |
| 915 | |
| 916 | EXPECT_EQ(held.get(), ptr); |
| 917 | |
| 918 | // stop holding binder, because we test to make sure references are cleaned |
| 919 | // up |
| 920 | EXPECT_OK(proc.rootIface->holdBinder(nullptr)); |
| 921 | // and flush ref counts |
| 922 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 923 | } |
| 924 | |
| 925 | // START TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 926 | // These are behavioral differences form regular binder, where certain usecases |
| 927 | // aren't supported. |
| 928 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 929 | TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 930 | auto proc1 = createRpcTestSocketServerProcess({}); |
| 931 | auto proc2 = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 932 | |
| 933 | sp<IBinder> outBinder; |
| 934 | EXPECT_EQ(INVALID_OPERATION, |
| 935 | proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); |
| 936 | } |
| 937 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 938 | TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 939 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2}); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 940 | |
| 941 | sp<IBinder> outBinder; |
| 942 | EXPECT_EQ(INVALID_OPERATION, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 943 | proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder) |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 944 | .transactionError()); |
| 945 | } |
| 946 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 947 | TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 948 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 949 | |
| 950 | sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); |
| 951 | sp<IBinder> outBinder; |
| 952 | EXPECT_EQ(INVALID_OPERATION, |
| 953 | proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError()); |
| 954 | } |
| 955 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 956 | TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 957 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 958 | |
| 959 | // for historical reasons, IServiceManager interface only returns the |
| 960 | // exception code |
| 961 | EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED, |
| 962 | defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder)); |
| 963 | } |
| 964 | |
| 965 | // END TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 966 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 967 | TEST_P(BinderRpc, RepeatRootObject) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 968 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 969 | |
| 970 | sp<IBinder> outBinder; |
| 971 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder)); |
| 972 | EXPECT_EQ(proc.rootBinder, outBinder); |
| 973 | } |
| 974 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 975 | TEST_P(BinderRpc, NestedTransactions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 976 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 977 | |
| 978 | auto nastyNester = sp<MyBinderRpcTest>::make(); |
| 979 | EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10)); |
| 980 | |
| 981 | wp<IBinder> weak = nastyNester; |
| 982 | nastyNester = nullptr; |
| 983 | EXPECT_EQ(nullptr, weak.promote()); |
| 984 | } |
| 985 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 986 | TEST_P(BinderRpc, SameBinderEquality) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 987 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 988 | |
| 989 | sp<IBinder> a; |
| 990 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 991 | |
| 992 | sp<IBinder> b; |
| 993 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 994 | |
| 995 | EXPECT_EQ(a, b); |
| 996 | } |
| 997 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 998 | TEST_P(BinderRpc, SameBinderEqualityWeak) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 999 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1000 | |
| 1001 | sp<IBinder> a; |
| 1002 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 1003 | wp<IBinder> weak = a; |
| 1004 | a = nullptr; |
| 1005 | |
| 1006 | sp<IBinder> b; |
| 1007 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 1008 | |
| 1009 | // this is the wrong behavior, since BpBinder |
| 1010 | // doesn't implement onIncStrongAttempted |
| 1011 | // but make sure there is no crash |
| 1012 | EXPECT_EQ(nullptr, weak.promote()); |
| 1013 | |
| 1014 | GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder."; |
| 1015 | |
| 1016 | // In order to fix this: |
| 1017 | // - need to have incStrongAttempted reflected across IPC boundary (wait for |
| 1018 | // response to promote - round trip...) |
| 1019 | // - sendOnLastWeakRef, to delete entries out of RpcState table |
| 1020 | EXPECT_EQ(b, weak.promote()); |
| 1021 | } |
| 1022 | |
| 1023 | #define expectSessions(expected, iface) \ |
| 1024 | do { \ |
| 1025 | int session; \ |
| 1026 | EXPECT_OK((iface)->getNumOpenSessions(&session)); \ |
| 1027 | EXPECT_EQ(expected, session); \ |
| 1028 | } while (false) |
| 1029 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1030 | TEST_P(BinderRpc, SingleSession) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1031 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1032 | |
| 1033 | sp<IBinderRpcSession> session; |
| 1034 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 1035 | std::string out; |
| 1036 | EXPECT_OK(session->getName(&out)); |
| 1037 | EXPECT_EQ("aoeu", out); |
| 1038 | |
| 1039 | expectSessions(1, proc.rootIface); |
| 1040 | session = nullptr; |
| 1041 | expectSessions(0, proc.rootIface); |
| 1042 | } |
| 1043 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1044 | TEST_P(BinderRpc, ManySessions) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1045 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1046 | |
| 1047 | std::vector<sp<IBinderRpcSession>> sessions; |
| 1048 | |
| 1049 | for (size_t i = 0; i < 15; i++) { |
| 1050 | expectSessions(i, proc.rootIface); |
| 1051 | sp<IBinderRpcSession> session; |
| 1052 | EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session)); |
| 1053 | sessions.push_back(session); |
| 1054 | } |
| 1055 | expectSessions(sessions.size(), proc.rootIface); |
| 1056 | for (size_t i = 0; i < sessions.size(); i++) { |
| 1057 | std::string out; |
| 1058 | EXPECT_OK(sessions.at(i)->getName(&out)); |
| 1059 | EXPECT_EQ(std::to_string(i), out); |
| 1060 | } |
| 1061 | expectSessions(sessions.size(), proc.rootIface); |
| 1062 | |
| 1063 | while (!sessions.empty()) { |
| 1064 | sessions.pop_back(); |
| 1065 | expectSessions(sessions.size(), proc.rootIface); |
| 1066 | } |
| 1067 | expectSessions(0, proc.rootIface); |
| 1068 | } |
| 1069 | |
| 1070 | size_t epochMillis() { |
| 1071 | using std::chrono::duration_cast; |
| 1072 | using std::chrono::milliseconds; |
| 1073 | using std::chrono::seconds; |
| 1074 | using std::chrono::system_clock; |
| 1075 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 1076 | } |
| 1077 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1078 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1079 | constexpr size_t kNumThreads = 10; |
| 1080 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1081 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1082 | |
| 1083 | EXPECT_OK(proc.rootIface->lock()); |
| 1084 | |
| 1085 | // block all but one thread taking locks |
| 1086 | std::vector<std::thread> ts; |
| 1087 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 1088 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 1089 | } |
| 1090 | |
| 1091 | usleep(100000); // give chance for calls on other threads |
| 1092 | |
| 1093 | // other calls still work |
| 1094 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 1095 | |
| 1096 | constexpr size_t blockTimeMs = 500; |
| 1097 | size_t epochMsBefore = epochMillis(); |
| 1098 | // after this, we should never see a response within this time |
| 1099 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 1100 | |
| 1101 | // this call should be blocked for blockTimeMs |
| 1102 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 1103 | |
| 1104 | size_t epochMsAfter = epochMillis(); |
| 1105 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 1106 | |
| 1107 | for (auto& t : ts) t.join(); |
| 1108 | } |
| 1109 | |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 1110 | void BinderRpc::testThreadPoolOverSaturated(sp<IBinderRpcTest> iface, size_t numCalls, |
| 1111 | size_t sleepMs) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1112 | size_t epochMsBefore = epochMillis(); |
| 1113 | |
| 1114 | std::vector<std::thread> ts; |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 1115 | for (size_t i = 0; i < numCalls; i++) { |
| 1116 | ts.push_back(std::thread([&] { iface->sleepMs(sleepMs); })); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | for (auto& t : ts) t.join(); |
| 1120 | |
| 1121 | size_t epochMsAfter = epochMillis(); |
| 1122 | |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 1123 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * sleepMs); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1124 | |
| 1125 | // Potential flake, but make sure calls are handled in parallel. |
Yifan Hong | 1f44f98 | 2021-10-08 17:16:47 -0700 | [diff] [blame] | 1126 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * sleepMs); |
| 1127 | } |
| 1128 | |
| 1129 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
| 1130 | constexpr size_t kNumThreads = 10; |
| 1131 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 1132 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads}); |
| 1133 | testThreadPoolOverSaturated(proc.rootIface, kNumCalls); |
| 1134 | } |
| 1135 | |
| 1136 | TEST_P(BinderRpc, ThreadPoolLimitOutgoing) { |
| 1137 | constexpr size_t kNumThreads = 20; |
| 1138 | constexpr size_t kNumOutgoingConnections = 10; |
| 1139 | constexpr size_t kNumCalls = kNumOutgoingConnections + 3; |
| 1140 | auto proc = createRpcTestSocketServerProcess( |
| 1141 | {.numThreads = kNumThreads, .numOutgoingConnections = kNumOutgoingConnections}); |
| 1142 | testThreadPoolOverSaturated(proc.rootIface, kNumCalls); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1145 | TEST_P(BinderRpc, ThreadingStressTest) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1146 | constexpr size_t kNumClientThreads = 10; |
| 1147 | constexpr size_t kNumServerThreads = 10; |
| 1148 | constexpr size_t kNumCalls = 100; |
| 1149 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1150 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1151 | |
| 1152 | std::vector<std::thread> threads; |
| 1153 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 1154 | threads.push_back(std::thread([&] { |
| 1155 | for (size_t j = 0; j < kNumCalls; j++) { |
| 1156 | sp<IBinder> out; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 1157 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1158 | EXPECT_EQ(proc.rootBinder, out); |
| 1159 | } |
| 1160 | })); |
| 1161 | } |
| 1162 | |
| 1163 | for (auto& t : threads) t.join(); |
| 1164 | } |
| 1165 | |
Steven Moreland | 925ba0a | 2021-09-17 18:06:32 -0700 | [diff] [blame] | 1166 | static void saturateThreadPool(size_t threadCount, const sp<IBinderRpcTest>& iface) { |
| 1167 | std::vector<std::thread> threads; |
| 1168 | for (size_t i = 0; i < threadCount; i++) { |
| 1169 | threads.push_back(std::thread([&] { EXPECT_OK(iface->sleepMs(500)); })); |
| 1170 | } |
| 1171 | for (auto& t : threads) t.join(); |
| 1172 | } |
| 1173 | |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 1174 | TEST_P(BinderRpc, OnewayStressTest) { |
| 1175 | constexpr size_t kNumClientThreads = 10; |
| 1176 | constexpr size_t kNumServerThreads = 10; |
Steven Moreland | 3c3ab8d | 2021-09-23 10:29:50 -0700 | [diff] [blame] | 1177 | constexpr size_t kNumCalls = 1000; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 1178 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1179 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads}); |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 1180 | |
| 1181 | std::vector<std::thread> threads; |
| 1182 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 1183 | threads.push_back(std::thread([&] { |
| 1184 | for (size_t j = 0; j < kNumCalls; j++) { |
| 1185 | EXPECT_OK(proc.rootIface->sendString("a")); |
| 1186 | } |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 1187 | })); |
| 1188 | } |
| 1189 | |
| 1190 | for (auto& t : threads) t.join(); |
Steven Moreland | 925ba0a | 2021-09-17 18:06:32 -0700 | [diff] [blame] | 1191 | |
| 1192 | saturateThreadPool(kNumServerThreads, proc.rootIface); |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1195 | TEST_P(BinderRpc, OnewayCallDoesNotWait) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1196 | constexpr size_t kReallyLongTimeMs = 100; |
| 1197 | constexpr size_t kSleepMs = kReallyLongTimeMs * 5; |
| 1198 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1199 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1200 | |
| 1201 | size_t epochMsBefore = epochMillis(); |
| 1202 | |
| 1203 | EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs)); |
| 1204 | |
| 1205 | size_t epochMsAfter = epochMillis(); |
| 1206 | EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); |
| 1207 | } |
| 1208 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1209 | TEST_P(BinderRpc, OnewayCallQueueing) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1210 | constexpr size_t kNumSleeps = 10; |
| 1211 | constexpr size_t kNumExtraServerThreads = 4; |
| 1212 | constexpr size_t kSleepMs = 50; |
| 1213 | |
| 1214 | // make sure calls to the same object happen on the same thread |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1215 | auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1216 | |
| 1217 | EXPECT_OK(proc.rootIface->lock()); |
| 1218 | |
Steven Moreland | 1c67880 | 2021-09-17 16:48:47 -0700 | [diff] [blame] | 1219 | size_t epochMsBefore = epochMillis(); |
| 1220 | |
| 1221 | // all these *Async commands should be queued on the server sequentially, |
| 1222 | // even though there are multiple threads. |
| 1223 | for (size_t i = 0; i + 1 < kNumSleeps; i++) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1224 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 1225 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1226 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 1227 | |
Steven Moreland | 1c67880 | 2021-09-17 16:48:47 -0700 | [diff] [blame] | 1228 | // this can only return once the final async call has unlocked |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1229 | EXPECT_OK(proc.rootIface->lockUnlock()); |
Steven Moreland | 1c67880 | 2021-09-17 16:48:47 -0700 | [diff] [blame] | 1230 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1231 | size_t epochMsAfter = epochMillis(); |
| 1232 | |
| 1233 | EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 1234 | |
Steven Moreland | 925ba0a | 2021-09-17 18:06:32 -0700 | [diff] [blame] | 1235 | saturateThreadPool(1 + kNumExtraServerThreads, proc.rootIface); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 1238 | TEST_P(BinderRpc, OnewayCallExhaustion) { |
| 1239 | constexpr size_t kNumClients = 2; |
| 1240 | constexpr size_t kTooLongMs = 1000; |
| 1241 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1242 | auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2}); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Build up oneway calls on the second session to make sure it terminates |
| 1245 | // and shuts down. The first session should be unaffected (proc destructor |
| 1246 | // checks the first session). |
| 1247 | auto iface = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root); |
| 1248 | |
| 1249 | std::vector<std::thread> threads; |
| 1250 | for (size_t i = 0; i < kNumClients; i++) { |
| 1251 | // one of these threads will get stuck queueing a transaction once the |
| 1252 | // socket fills up, the other will be able to fill up transactions on |
| 1253 | // this object |
| 1254 | threads.push_back(std::thread([&] { |
| 1255 | while (iface->sleepMsAsync(kTooLongMs).isOk()) { |
| 1256 | } |
| 1257 | })); |
| 1258 | } |
| 1259 | for (auto& t : threads) t.join(); |
| 1260 | |
| 1261 | Status status = iface->sleepMsAsync(kTooLongMs); |
| 1262 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 1263 | |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 1264 | // now that it has died, wait for the remote session to shutdown |
| 1265 | std::vector<int32_t> remoteCounts; |
| 1266 | do { |
| 1267 | EXPECT_OK(proc.rootIface->countBinders(&remoteCounts)); |
| 1268 | } while (remoteCounts.size() == kNumClients); |
| 1269 | |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 1270 | // the second session should be shutdown in the other process by the time we |
| 1271 | // are able to join above (it'll only be hung up once it finishes processing |
| 1272 | // any pending commands). We need to erase this session from the record |
| 1273 | // here, so that the destructor for our session won't check that this |
| 1274 | // session is valid, but we still want it to test the other session. |
| 1275 | proc.proc.sessions.erase(proc.proc.sessions.begin() + 1); |
| 1276 | } |
| 1277 | |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1278 | TEST_P(BinderRpc, Callbacks) { |
| 1279 | const static std::string kTestString = "good afternoon!"; |
| 1280 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1281 | for (bool callIsOneway : {true, false}) { |
| 1282 | for (bool callbackIsOneway : {true, false}) { |
| 1283 | for (bool delayed : {true, false}) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1284 | auto proc = createRpcTestSocketServerProcess( |
| 1285 | {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1}); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1286 | auto cb = sp<MyBinderRpcCallback>::make(); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1287 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1288 | if (callIsOneway) { |
| 1289 | EXPECT_OK(proc.rootIface->doCallbackAsync(cb, callbackIsOneway, delayed, |
| 1290 | kTestString)); |
| 1291 | } else { |
| 1292 | EXPECT_OK( |
| 1293 | proc.rootIface->doCallback(cb, callbackIsOneway, delayed, kTestString)); |
| 1294 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1295 | |
Steven Moreland | 03ecce6 | 2022-05-13 23:22:05 +0000 | [diff] [blame] | 1296 | // if both transactions are synchronous and the response is sent back on the |
| 1297 | // same thread, everything should have happened in a nested call. Otherwise, |
| 1298 | // the callback will be processed on another thread. |
| 1299 | if (callIsOneway || callbackIsOneway || delayed) { |
| 1300 | using std::literals::chrono_literals::operator""s; |
| 1301 | std::unique_lock<std::mutex> _l(cb->mMutex); |
| 1302 | cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); }); |
| 1303 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1304 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1305 | EXPECT_EQ(cb->mValues.size(), 1) |
| 1306 | << "callIsOneway: " << callIsOneway |
| 1307 | << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed; |
| 1308 | if (cb->mValues.empty()) continue; |
| 1309 | EXPECT_EQ(cb->mValues.at(0), kTestString) |
| 1310 | << "callIsOneway: " << callIsOneway |
| 1311 | << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1312 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1313 | // since we are severing the connection, we need to go ahead and |
| 1314 | // tell the server to shutdown and exit so that waitpid won't hang |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 1315 | if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) { |
| 1316 | EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status; |
| 1317 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1318 | |
Steven Moreland | 1b30429 | 2021-07-15 22:59:34 +0000 | [diff] [blame] | 1319 | // since this session has an incoming connection w/ a threadpool, we |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1320 | // need to manually shut it down |
| 1321 | EXPECT_TRUE(proc.proc.sessions.at(0).session->shutdownAndWait(true)); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1322 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 1323 | proc.proc.host.setCustomExitStatusCheck([](int wstatus) { |
| 1324 | // Flaky. Sometimes gets SIGABRT. |
| 1325 | EXPECT_TRUE((WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0) || |
| 1326 | (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)) |
| 1327 | << "server process failed: " << WaitStatusToString(wstatus); |
| 1328 | }); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 1329 | proc.expectAlreadyShutdown = true; |
| 1330 | } |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 1335 | TEST_P(BinderRpc, OnewayCallbackWithNoThread) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1336 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 195edb8 | 2021-06-08 02:44:39 +0000 | [diff] [blame] | 1337 | auto cb = sp<MyBinderRpcCallback>::make(); |
| 1338 | |
| 1339 | Status status = proc.rootIface->doCallback(cb, true /*oneway*/, false /*delayed*/, "anything"); |
| 1340 | EXPECT_EQ(WOULD_BLOCK, status.transactionError()); |
| 1341 | } |
| 1342 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1343 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1344 | for (bool doDeathCleanup : {true, false}) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1345 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1346 | |
| 1347 | // make sure there is some state during crash |
| 1348 | // 1. we hold their binder |
| 1349 | sp<IBinderRpcSession> session; |
| 1350 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 1351 | // 2. they hold our binder |
| 1352 | sp<IBinder> binder = new BBinder(); |
| 1353 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 1354 | |
| 1355 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 1356 | << "Do death cleanup: " << doDeathCleanup; |
| 1357 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 1358 | proc.proc.host.setCustomExitStatusCheck([](int wstatus) { |
| 1359 | EXPECT_TRUE(WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 1) |
| 1360 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 1361 | }); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 1362 | proc.expectAlreadyShutdown = true; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1366 | TEST_P(BinderRpc, UseKernelBinderCallingId) { |
Steven Moreland | a83191d | 2021-10-27 10:14:53 -0700 | [diff] [blame] | 1367 | bool okToFork = ProcessState::selfOrNull() == nullptr; |
| 1368 | |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1369 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1370 | |
Steven Moreland | a83191d | 2021-10-27 10:14:53 -0700 | [diff] [blame] | 1371 | // If this process has used ProcessState already, then the forked process |
| 1372 | // cannot use it at all. If this process hasn't used it (depending on the |
| 1373 | // order tests are run), then the forked process can use it, and we'll only |
| 1374 | // catch the invalid usage the second time. Such is the burden of global |
| 1375 | // state! |
| 1376 | if (okToFork) { |
| 1377 | // we can't allocate IPCThreadState so actually the first time should |
| 1378 | // succeed :( |
| 1379 | EXPECT_OK(proc.rootIface->useKernelBinderCallingId()); |
| 1380 | } |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1381 | |
| 1382 | // second time! we catch the error :) |
| 1383 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError()); |
| 1384 | |
Frederick Mayle | a12b096 | 2022-06-25 01:13:22 +0000 | [diff] [blame^] | 1385 | proc.proc.host.setCustomExitStatusCheck([](int wstatus) { |
| 1386 | EXPECT_TRUE(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT) |
| 1387 | << "server process failed incorrectly: " << WaitStatusToString(wstatus); |
| 1388 | }); |
Steven Moreland | af4ca71 | 2021-05-24 23:22:08 +0000 | [diff] [blame] | 1389 | proc.expectAlreadyShutdown = true; |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 1392 | TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1393 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 1394 | |
| 1395 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 1396 | ASSERT_NE(binder, nullptr); |
| 1397 | |
| 1398 | ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get())); |
| 1399 | } |
| 1400 | |
| 1401 | TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1402 | auto proc = createRpcTestSocketServerProcess({}); |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 1403 | |
| 1404 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 1405 | ASSERT_NE(binder, nullptr); |
| 1406 | |
| 1407 | auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder); |
| 1408 | ASSERT_NE(ndkBinder, nullptr); |
| 1409 | |
| 1410 | std::string out; |
| 1411 | ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out); |
| 1412 | ASSERT_TRUE(status.isOk()) << status.getDescription(); |
| 1413 | ASSERT_EQ("aoeuaoeu", out); |
| 1414 | } |
| 1415 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1416 | ssize_t countFds() { |
| 1417 | DIR* dir = opendir("/proc/self/fd/"); |
| 1418 | if (dir == nullptr) return -1; |
| 1419 | ssize_t ret = 0; |
| 1420 | dirent* ent; |
| 1421 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 1422 | closedir(dir); |
| 1423 | return ret; |
| 1424 | } |
| 1425 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1426 | TEST_P(BinderRpc, Fds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1427 | ssize_t beforeFds = countFds(); |
| 1428 | ASSERT_GE(beforeFds, 0); |
| 1429 | { |
Steven Moreland | 4313d7e | 2021-07-15 23:41:22 +0000 | [diff] [blame] | 1430 | auto proc = createRpcTestSocketServerProcess({.numThreads = 10}); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1431 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 1432 | } |
| 1433 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 1434 | } |
| 1435 | |
Devin Moore | 800b225 | 2021-10-15 16:22:57 +0000 | [diff] [blame] | 1436 | TEST_P(BinderRpc, AidlDelegatorTest) { |
| 1437 | auto proc = createRpcTestSocketServerProcess({}); |
| 1438 | auto myDelegator = sp<IBinderRpcTestDelegator>::make(proc.rootIface); |
| 1439 | ASSERT_NE(nullptr, myDelegator); |
| 1440 | |
| 1441 | std::string doubled; |
| 1442 | EXPECT_OK(myDelegator->doubleString("cool ", &doubled)); |
| 1443 | EXPECT_EQ("cool cool ", doubled); |
| 1444 | } |
| 1445 | |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1446 | static bool testSupportVsockLoopback() { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1447 | // We don't need to enable TLS to know if vsock is supported. |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1448 | unsigned int vsockPort = allocateVsockPort(); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1449 | sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make()); |
Steven Moreland | 1eab345 | 2021-08-05 16:56:20 -0700 | [diff] [blame] | 1450 | if (status_t status = server->setupVsockServer(vsockPort); status != OK) { |
| 1451 | if (status == -EAFNOSUPPORT) { |
| 1452 | return false; |
| 1453 | } |
| 1454 | LOG_ALWAYS_FATAL("Could not setup vsock server: %s", statusToString(status).c_str()); |
| 1455 | } |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1456 | server->start(); |
| 1457 | |
Yifan Hong | fdd9f69 | 2021-09-09 15:12:52 -0700 | [diff] [blame] | 1458 | sp<RpcSession> session = RpcSession::make(RpcTransportCtxFactoryRaw::make()); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1459 | status_t status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 1460 | while (!server->shutdown()) usleep(10000); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1461 | ALOGE("Detected vsock loopback supported: %s", statusToString(status).c_str()); |
| 1462 | return status == OK; |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1465 | static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) { |
| 1466 | std::vector<SocketType> ret = {SocketType::UNIX, SocketType::INET}; |
| 1467 | |
| 1468 | if (hasPreconnected) ret.push_back(SocketType::PRECONNECTED); |
Steven Moreland | da57304 | 2021-06-12 01:13:45 +0000 | [diff] [blame] | 1469 | |
| 1470 | static bool hasVsockLoopback = testSupportVsockLoopback(); |
| 1471 | |
| 1472 | if (hasVsockLoopback) { |
| 1473 | ret.push_back(SocketType::VSOCK); |
| 1474 | } |
| 1475 | |
| 1476 | return ret; |
| 1477 | } |
| 1478 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1479 | static std::vector<uint32_t> testVersions() { |
| 1480 | std::vector<uint32_t> versions; |
| 1481 | for (size_t i = 0; i < RPC_WIRE_PROTOCOL_VERSION_NEXT; i++) { |
| 1482 | versions.push_back(i); |
| 1483 | } |
| 1484 | versions.push_back(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL); |
| 1485 | return versions; |
| 1486 | } |
| 1487 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1488 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
| 1489 | ::testing::Combine(::testing::ValuesIn(testSocketTypes()), |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1490 | ::testing::ValuesIn(RpcSecurityValues()), |
| 1491 | ::testing::ValuesIn(testVersions()), |
| 1492 | ::testing::ValuesIn(testVersions())), |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1493 | BinderRpc::PrintParamInfo); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1494 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1495 | class BinderRpcServerRootObject |
| 1496 | : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {}; |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1497 | |
| 1498 | TEST_P(BinderRpcServerRootObject, WeakRootObject) { |
| 1499 | using SetFn = std::function<void(RpcServer*, sp<IBinder>)>; |
| 1500 | auto setRootObject = [](bool isStrong) -> SetFn { |
| 1501 | return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak); |
| 1502 | }; |
| 1503 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1504 | auto [isStrong1, isStrong2, rpcSecurity] = GetParam(); |
| 1505 | auto server = RpcServer::make(newFactory(rpcSecurity)); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1506 | auto binder1 = sp<BBinder>::make(); |
| 1507 | IBinder* binderRaw1 = binder1.get(); |
| 1508 | setRootObject(isStrong1)(server.get(), binder1); |
| 1509 | EXPECT_EQ(binderRaw1, server->getRootObject()); |
| 1510 | binder1.clear(); |
| 1511 | EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject()); |
| 1512 | |
| 1513 | auto binder2 = sp<BBinder>::make(); |
| 1514 | IBinder* binderRaw2 = binder2.get(); |
| 1515 | setRootObject(isStrong2)(server.get(), binder2); |
| 1516 | EXPECT_EQ(binderRaw2, server->getRootObject()); |
| 1517 | binder2.clear(); |
| 1518 | EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject()); |
| 1519 | } |
| 1520 | |
| 1521 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject, |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1522 | ::testing::Combine(::testing::Bool(), ::testing::Bool(), |
| 1523 | ::testing::ValuesIn(RpcSecurityValues()))); |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 1524 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1525 | class OneOffSignal { |
| 1526 | public: |
| 1527 | // If notify() was previously called, or is called within |duration|, return true; else false. |
| 1528 | template <typename R, typename P> |
| 1529 | bool wait(std::chrono::duration<R, P> duration) { |
| 1530 | std::unique_lock<std::mutex> lock(mMutex); |
| 1531 | return mCv.wait_for(lock, duration, [this] { return mValue; }); |
| 1532 | } |
| 1533 | void notify() { |
| 1534 | std::unique_lock<std::mutex> lock(mMutex); |
| 1535 | mValue = true; |
| 1536 | lock.unlock(); |
| 1537 | mCv.notify_all(); |
| 1538 | } |
| 1539 | |
| 1540 | private: |
| 1541 | std::mutex mMutex; |
| 1542 | std::condition_variable mCv; |
| 1543 | bool mValue = false; |
| 1544 | }; |
| 1545 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1546 | TEST_P(BinderRpcServerOnly, Shutdown) { |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1547 | auto addr = allocateSocketAddress(); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1548 | auto server = RpcServer::make(newFactory(std::get<0>(GetParam()))); |
| 1549 | server->setProtocolVersion(std::get<1>(GetParam())); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1550 | ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str())); |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame] | 1551 | auto joinEnds = std::make_shared<OneOffSignal>(); |
| 1552 | |
| 1553 | // If things are broken and the thread never stops, don't block other tests. Because the thread |
| 1554 | // may run after the test finishes, it must not access the stack memory of the test. Hence, |
| 1555 | // shared pointers are passed. |
| 1556 | std::thread([server, joinEnds] { |
| 1557 | server->join(); |
| 1558 | joinEnds->notify(); |
| 1559 | }).detach(); |
| 1560 | |
| 1561 | bool shutdown = false; |
| 1562 | for (int i = 0; i < 10 && !shutdown; i++) { |
| 1563 | usleep(300 * 1000); // 300ms; total 3s |
| 1564 | if (server->shutdown()) shutdown = true; |
| 1565 | } |
| 1566 | ASSERT_TRUE(shutdown) << "server->shutdown() never returns true"; |
| 1567 | |
| 1568 | ASSERT_TRUE(joinEnds->wait(2s)) |
| 1569 | << "After server->shutdown() returns true, join() did not stop after 2s"; |
| 1570 | } |
| 1571 | |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1572 | TEST(BinderRpc, Java) { |
| 1573 | #if !defined(__ANDROID__) |
| 1574 | GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on" |
| 1575 | "createRpcDelegateServiceManager() with a device attached, such test belongs " |
| 1576 | "to binderHostDeviceTest. Hence, just disable this test on host."; |
| 1577 | #endif // !__ANDROID__ |
| 1578 | sp<IServiceManager> sm = defaultServiceManager(); |
| 1579 | ASSERT_NE(nullptr, sm); |
| 1580 | // Any Java service with non-empty getInterfaceDescriptor() would do. |
| 1581 | // Let's pick batteryproperties. |
| 1582 | auto binder = sm->checkService(String16("batteryproperties")); |
| 1583 | ASSERT_NE(nullptr, binder); |
| 1584 | auto descriptor = binder->getInterfaceDescriptor(); |
| 1585 | ASSERT_GE(descriptor.size(), 0); |
| 1586 | ASSERT_EQ(OK, binder->pingBinder()); |
| 1587 | |
| 1588 | auto rpcServer = RpcServer::make(); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1589 | unsigned int port; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1590 | ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port)); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1591 | auto socket = rpcServer->releaseServer(); |
| 1592 | |
| 1593 | auto keepAlive = sp<BBinder>::make(); |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 1594 | auto setRpcClientDebugStatus = binder->setRpcClientDebug(std::move(socket), keepAlive); |
| 1595 | |
Yifan Hong | e3caaf2 | 2022-01-12 14:46:56 -0800 | [diff] [blame] | 1596 | if (!android::base::GetBoolProperty("ro.debuggable", false) || |
| 1597 | android::base::GetProperty("ro.build.type", "") == "user") { |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 1598 | ASSERT_EQ(INVALID_OPERATION, setRpcClientDebugStatus) |
Yifan Hong | e3caaf2 | 2022-01-12 14:46:56 -0800 | [diff] [blame] | 1599 | << "setRpcClientDebug should return INVALID_OPERATION on non-debuggable or user " |
| 1600 | "builds, but get " |
Yifan Hong | fe4b83f | 2021-11-08 16:29:53 -0800 | [diff] [blame] | 1601 | << statusToString(setRpcClientDebugStatus); |
| 1602 | GTEST_SKIP(); |
| 1603 | } |
| 1604 | |
| 1605 | ASSERT_EQ(OK, setRpcClientDebugStatus); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1606 | |
| 1607 | auto rpcSession = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 1608 | ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port)); |
Yifan Hong | 194acf2 | 2021-06-29 18:44:56 -0700 | [diff] [blame] | 1609 | auto rpcBinder = rpcSession->getRootObject(); |
| 1610 | ASSERT_NE(nullptr, rpcBinder); |
| 1611 | |
| 1612 | ASSERT_EQ(OK, rpcBinder->pingBinder()); |
| 1613 | |
| 1614 | ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor()) |
| 1615 | << "getInterfaceDescriptor should not crash system_server"; |
| 1616 | ASSERT_EQ(OK, rpcBinder->pingBinder()); |
| 1617 | } |
| 1618 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1619 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerOnly, |
| 1620 | ::testing::Combine(::testing::ValuesIn(RpcSecurityValues()), |
| 1621 | ::testing::ValuesIn(testVersions())), |
| 1622 | BinderRpcServerOnly::PrintTestParam); |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 1623 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1624 | class RpcTransportTestUtils { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1625 | public: |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1626 | // Only parameterized only server version because `RpcSession` is bypassed |
| 1627 | // in the client half of the tests. |
| 1628 | using Param = |
| 1629 | std::tuple<SocketType, RpcSecurity, std::optional<RpcCertificateFormat>, uint32_t>; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1630 | using ConnectToServer = std::function<base::unique_fd()>; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1631 | |
| 1632 | // A server that handles client socket connections. |
| 1633 | class Server { |
| 1634 | public: |
| 1635 | explicit Server() {} |
| 1636 | Server(Server&&) = default; |
Yifan Hong | e07d273 | 2021-09-13 21:59:14 -0700 | [diff] [blame] | 1637 | ~Server() { shutdownAndWait(); } |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1638 | [[nodiscard]] AssertionResult setUp( |
| 1639 | const Param& param, |
| 1640 | std::unique_ptr<RpcAuth> auth = std::make_unique<RpcAuthSelfSigned>()) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1641 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1642 | auto rpcServer = RpcServer::make(newFactory(rpcSecurity)); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1643 | rpcServer->setProtocolVersion(serverVersion); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1644 | switch (socketType) { |
| 1645 | case SocketType::PRECONNECTED: { |
| 1646 | return AssertionFailure() << "Not supported by this test"; |
| 1647 | } break; |
| 1648 | case SocketType::UNIX: { |
| 1649 | auto addr = allocateSocketAddress(); |
| 1650 | auto status = rpcServer->setupUnixDomainServer(addr.c_str()); |
| 1651 | if (status != OK) { |
| 1652 | return AssertionFailure() |
| 1653 | << "setupUnixDomainServer: " << statusToString(status); |
| 1654 | } |
| 1655 | mConnectToServer = [addr] { |
| 1656 | return connectTo(UnixSocketAddress(addr.c_str())); |
| 1657 | }; |
| 1658 | } break; |
| 1659 | case SocketType::VSOCK: { |
| 1660 | auto port = allocateVsockPort(); |
| 1661 | auto status = rpcServer->setupVsockServer(port); |
| 1662 | if (status != OK) { |
| 1663 | return AssertionFailure() << "setupVsockServer: " << statusToString(status); |
| 1664 | } |
| 1665 | mConnectToServer = [port] { |
| 1666 | return connectTo(VsockSocketAddress(VMADDR_CID_LOCAL, port)); |
| 1667 | }; |
| 1668 | } break; |
| 1669 | case SocketType::INET: { |
| 1670 | unsigned int port; |
| 1671 | auto status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port); |
| 1672 | if (status != OK) { |
| 1673 | return AssertionFailure() << "setupInetServer: " << statusToString(status); |
| 1674 | } |
| 1675 | mConnectToServer = [port] { |
| 1676 | const char* addr = kLocalInetAddress; |
| 1677 | auto aiStart = InetSocketAddress::getAddrInfo(addr, port); |
| 1678 | if (aiStart == nullptr) return base::unique_fd{}; |
| 1679 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 1680 | auto fd = connectTo( |
| 1681 | InetSocketAddress(ai->ai_addr, ai->ai_addrlen, addr, port)); |
| 1682 | if (fd.ok()) return fd; |
| 1683 | } |
| 1684 | ALOGE("None of the socket address resolved for %s:%u can be connected", |
| 1685 | addr, port); |
| 1686 | return base::unique_fd{}; |
| 1687 | }; |
| 1688 | } |
| 1689 | } |
| 1690 | mFd = rpcServer->releaseServer(); |
| 1691 | if (!mFd.ok()) return AssertionFailure() << "releaseServer returns invalid fd"; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1692 | mCtx = newFactory(rpcSecurity, mCertVerifier, std::move(auth))->newServerCtx(); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1693 | if (mCtx == nullptr) return AssertionFailure() << "newServerCtx"; |
| 1694 | mSetup = true; |
| 1695 | return AssertionSuccess(); |
| 1696 | } |
| 1697 | RpcTransportCtx* getCtx() const { return mCtx.get(); } |
| 1698 | std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const { |
| 1699 | return mCertVerifier; |
| 1700 | } |
| 1701 | ConnectToServer getConnectToServerFn() { return mConnectToServer; } |
| 1702 | void start() { |
| 1703 | LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!"); |
| 1704 | mThread = std::make_unique<std::thread>(&Server::run, this); |
| 1705 | } |
| 1706 | void run() { |
| 1707 | LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!"); |
| 1708 | |
| 1709 | std::vector<std::thread> threads; |
| 1710 | while (OK == mFdTrigger->triggerablePoll(mFd, POLLIN)) { |
| 1711 | base::unique_fd acceptedFd( |
| 1712 | TEMP_FAILURE_RETRY(accept4(mFd.get(), nullptr, nullptr /*length*/, |
| 1713 | SOCK_CLOEXEC | SOCK_NONBLOCK))); |
| 1714 | threads.emplace_back(&Server::handleOne, this, std::move(acceptedFd)); |
| 1715 | } |
| 1716 | |
| 1717 | for (auto& thread : threads) thread.join(); |
| 1718 | } |
| 1719 | void handleOne(android::base::unique_fd acceptedFd) { |
| 1720 | ASSERT_TRUE(acceptedFd.ok()); |
| 1721 | auto serverTransport = mCtx->newTransport(std::move(acceptedFd), mFdTrigger.get()); |
| 1722 | if (serverTransport == nullptr) return; // handshake failed |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1723 | ASSERT_TRUE(mPostConnect(serverTransport.get(), mFdTrigger.get())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1724 | } |
Yifan Hong | e07d273 | 2021-09-13 21:59:14 -0700 | [diff] [blame] | 1725 | void shutdownAndWait() { |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1726 | shutdown(); |
| 1727 | join(); |
| 1728 | } |
| 1729 | void shutdown() { mFdTrigger->trigger(); } |
| 1730 | |
| 1731 | void setPostConnect( |
| 1732 | std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> fn) { |
| 1733 | mPostConnect = std::move(fn); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | private: |
| 1737 | std::unique_ptr<std::thread> mThread; |
| 1738 | ConnectToServer mConnectToServer; |
| 1739 | std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make(); |
| 1740 | base::unique_fd mFd; |
| 1741 | std::unique_ptr<RpcTransportCtx> mCtx; |
| 1742 | std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier = |
| 1743 | std::make_shared<RpcCertificateVerifierSimple>(); |
| 1744 | bool mSetup = false; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1745 | // The function invoked after connection and handshake. By default, it is |
| 1746 | // |defaultPostConnect| that sends |kMessage| to the client. |
| 1747 | std::function<AssertionResult(RpcTransport*, FdTrigger* fdTrigger)> mPostConnect = |
| 1748 | Server::defaultPostConnect; |
| 1749 | |
| 1750 | void join() { |
| 1751 | if (mThread != nullptr) { |
| 1752 | mThread->join(); |
| 1753 | mThread = nullptr; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | static AssertionResult defaultPostConnect(RpcTransport* serverTransport, |
| 1758 | FdTrigger* fdTrigger) { |
| 1759 | std::string message(kMessage); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1760 | iovec messageIov{message.data(), message.size()}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 1761 | auto status = serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1, |
| 1762 | std::nullopt); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1763 | if (status != OK) return AssertionFailure() << statusToString(status); |
| 1764 | return AssertionSuccess(); |
| 1765 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1766 | }; |
| 1767 | |
| 1768 | class Client { |
| 1769 | public: |
| 1770 | explicit Client(ConnectToServer connectToServer) : mConnectToServer(connectToServer) {} |
| 1771 | Client(Client&&) = default; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1772 | [[nodiscard]] AssertionResult setUp(const Param& param) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1773 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = param; |
| 1774 | (void)serverVersion; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1775 | mFdTrigger = FdTrigger::make(); |
| 1776 | mCtx = newFactory(rpcSecurity, mCertVerifier)->newClientCtx(); |
| 1777 | if (mCtx == nullptr) return AssertionFailure() << "newClientCtx"; |
| 1778 | return AssertionSuccess(); |
| 1779 | } |
| 1780 | RpcTransportCtx* getCtx() const { return mCtx.get(); } |
| 1781 | std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const { |
| 1782 | return mCertVerifier; |
| 1783 | } |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1784 | // connect() and do handshake |
| 1785 | bool setUpTransport() { |
| 1786 | mFd = mConnectToServer(); |
| 1787 | if (!mFd.ok()) return AssertionFailure() << "Cannot connect to server"; |
| 1788 | mClientTransport = mCtx->newTransport(std::move(mFd), mFdTrigger.get()); |
| 1789 | return mClientTransport != nullptr; |
| 1790 | } |
| 1791 | AssertionResult readMessage(const std::string& expectedMessage = kMessage) { |
| 1792 | LOG_ALWAYS_FATAL_IF(mClientTransport == nullptr, "setUpTransport not called or failed"); |
| 1793 | std::string readMessage(expectedMessage.size(), '\0'); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 1794 | iovec readMessageIov{readMessage.data(), readMessage.size()}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 1795 | status_t readStatus = |
| 1796 | mClientTransport->interruptableReadFully(mFdTrigger.get(), &readMessageIov, 1, |
| 1797 | std::nullopt); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1798 | if (readStatus != OK) { |
| 1799 | return AssertionFailure() << statusToString(readStatus); |
| 1800 | } |
| 1801 | if (readMessage != expectedMessage) { |
| 1802 | return AssertionFailure() |
| 1803 | << "Expected " << expectedMessage << ", actual " << readMessage; |
| 1804 | } |
| 1805 | return AssertionSuccess(); |
| 1806 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1807 | void run(bool handshakeOk = true, bool readOk = true) { |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1808 | if (!setUpTransport()) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1809 | ASSERT_FALSE(handshakeOk) << "newTransport returns nullptr, but it shouldn't"; |
| 1810 | return; |
| 1811 | } |
| 1812 | ASSERT_TRUE(handshakeOk) << "newTransport does not return nullptr, but it should"; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1813 | ASSERT_EQ(readOk, readMessage()); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | private: |
| 1817 | ConnectToServer mConnectToServer; |
| 1818 | base::unique_fd mFd; |
| 1819 | std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make(); |
| 1820 | std::unique_ptr<RpcTransportCtx> mCtx; |
| 1821 | std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier = |
| 1822 | std::make_shared<RpcCertificateVerifierSimple>(); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1823 | std::unique_ptr<RpcTransport> mClientTransport; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1824 | }; |
| 1825 | |
| 1826 | // Make A trust B. |
| 1827 | template <typename A, typename B> |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1828 | static status_t trust(RpcSecurity rpcSecurity, |
| 1829 | std::optional<RpcCertificateFormat> certificateFormat, const A& a, |
| 1830 | const B& b) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1831 | if (rpcSecurity != RpcSecurity::TLS) return OK; |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 1832 | LOG_ALWAYS_FATAL_IF(!certificateFormat.has_value()); |
| 1833 | auto bCert = b->getCtx()->getCertificate(*certificateFormat); |
| 1834 | return a->getCertVerifier()->addTrustedPeerCertificate(*certificateFormat, bCert); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | static constexpr const char* kMessage = "hello"; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1838 | }; |
| 1839 | |
| 1840 | class RpcTransportTest : public testing::TestWithParam<RpcTransportTestUtils::Param> { |
| 1841 | public: |
| 1842 | using Server = RpcTransportTestUtils::Server; |
| 1843 | using Client = RpcTransportTestUtils::Client; |
| 1844 | static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1845 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = info.param; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1846 | auto ret = PrintToString(socketType) + "_" + newFactory(rpcSecurity)->toCString(); |
| 1847 | if (certificateFormat.has_value()) ret += "_" + PrintToString(*certificateFormat); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1848 | ret += "_serverV" + std::to_string(serverVersion); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1849 | return ret; |
| 1850 | } |
| 1851 | static std::vector<ParamType> getRpcTranportTestParams() { |
| 1852 | std::vector<ParamType> ret; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1853 | for (auto serverVersion : testVersions()) { |
| 1854 | for (auto socketType : testSocketTypes(false /* hasPreconnected */)) { |
| 1855 | for (auto rpcSecurity : RpcSecurityValues()) { |
| 1856 | switch (rpcSecurity) { |
| 1857 | case RpcSecurity::RAW: { |
| 1858 | ret.emplace_back(socketType, rpcSecurity, std::nullopt, serverVersion); |
| 1859 | } break; |
| 1860 | case RpcSecurity::TLS: { |
| 1861 | ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::PEM, |
| 1862 | serverVersion); |
| 1863 | ret.emplace_back(socketType, rpcSecurity, RpcCertificateFormat::DER, |
| 1864 | serverVersion); |
| 1865 | } break; |
| 1866 | } |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | return ret; |
| 1871 | } |
| 1872 | template <typename A, typename B> |
| 1873 | status_t trust(const A& a, const B& b) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1874 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1875 | (void)serverVersion; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1876 | return RpcTransportTestUtils::trust(rpcSecurity, certificateFormat, a, b); |
| 1877 | } |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1878 | }; |
| 1879 | |
| 1880 | TEST_P(RpcTransportTest, GoodCertificate) { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1881 | auto server = std::make_unique<Server>(); |
| 1882 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1883 | |
| 1884 | Client client(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1885 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1886 | |
| 1887 | ASSERT_EQ(OK, trust(&client, server)); |
| 1888 | ASSERT_EQ(OK, trust(server, &client)); |
| 1889 | |
| 1890 | server->start(); |
| 1891 | client.run(); |
| 1892 | } |
| 1893 | |
| 1894 | TEST_P(RpcTransportTest, MultipleClients) { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1895 | auto server = std::make_unique<Server>(); |
| 1896 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1897 | |
| 1898 | std::vector<Client> clients; |
| 1899 | for (int i = 0; i < 2; i++) { |
| 1900 | auto& client = clients.emplace_back(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1901 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1902 | ASSERT_EQ(OK, trust(&client, server)); |
| 1903 | ASSERT_EQ(OK, trust(server, &client)); |
| 1904 | } |
| 1905 | |
| 1906 | server->start(); |
| 1907 | for (auto& client : clients) client.run(); |
| 1908 | } |
| 1909 | |
| 1910 | TEST_P(RpcTransportTest, UntrustedServer) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1911 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1912 | (void)serverVersion; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1913 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1914 | auto untrustedServer = std::make_unique<Server>(); |
| 1915 | ASSERT_TRUE(untrustedServer->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1916 | |
| 1917 | Client client(untrustedServer->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1918 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1919 | |
| 1920 | ASSERT_EQ(OK, trust(untrustedServer, &client)); |
| 1921 | |
| 1922 | untrustedServer->start(); |
| 1923 | |
| 1924 | // For TLS, this should reject the certificate. For RAW sockets, it should pass because |
| 1925 | // the client can't verify the server's identity. |
| 1926 | bool handshakeOk = rpcSecurity != RpcSecurity::TLS; |
| 1927 | client.run(handshakeOk); |
| 1928 | } |
| 1929 | TEST_P(RpcTransportTest, MaliciousServer) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1930 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1931 | (void)serverVersion; |
| 1932 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1933 | auto validServer = std::make_unique<Server>(); |
| 1934 | ASSERT_TRUE(validServer->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1935 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1936 | auto maliciousServer = std::make_unique<Server>(); |
| 1937 | ASSERT_TRUE(maliciousServer->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1938 | |
| 1939 | Client client(maliciousServer->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1940 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1941 | |
| 1942 | ASSERT_EQ(OK, trust(&client, validServer)); |
| 1943 | ASSERT_EQ(OK, trust(validServer, &client)); |
| 1944 | ASSERT_EQ(OK, trust(maliciousServer, &client)); |
| 1945 | |
| 1946 | maliciousServer->start(); |
| 1947 | |
| 1948 | // For TLS, this should reject the certificate. For RAW sockets, it should pass because |
| 1949 | // the client can't verify the server's identity. |
| 1950 | bool handshakeOk = rpcSecurity != RpcSecurity::TLS; |
| 1951 | client.run(handshakeOk); |
| 1952 | } |
| 1953 | |
| 1954 | TEST_P(RpcTransportTest, UntrustedClient) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1955 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1956 | (void)serverVersion; |
| 1957 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1958 | auto server = std::make_unique<Server>(); |
| 1959 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1960 | |
| 1961 | Client client(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1962 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1963 | |
| 1964 | ASSERT_EQ(OK, trust(&client, server)); |
| 1965 | |
| 1966 | server->start(); |
| 1967 | |
| 1968 | // For TLS, Client should be able to verify server's identity, so client should see |
| 1969 | // do_handshake() successfully executed. However, server shouldn't be able to verify client's |
| 1970 | // identity and should drop the connection, so client shouldn't be able to read anything. |
| 1971 | bool readOk = rpcSecurity != RpcSecurity::TLS; |
| 1972 | client.run(true, readOk); |
| 1973 | } |
| 1974 | |
| 1975 | TEST_P(RpcTransportTest, MaliciousClient) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 1976 | auto [socketType, rpcSecurity, certificateFormat, serverVersion] = GetParam(); |
| 1977 | (void)serverVersion; |
| 1978 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1979 | auto server = std::make_unique<Server>(); |
| 1980 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1981 | |
| 1982 | Client validClient(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1983 | ASSERT_TRUE(validClient.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1984 | Client maliciousClient(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 1985 | ASSERT_TRUE(maliciousClient.setUp(GetParam())); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1986 | |
| 1987 | ASSERT_EQ(OK, trust(&validClient, server)); |
| 1988 | ASSERT_EQ(OK, trust(&maliciousClient, server)); |
| 1989 | |
| 1990 | server->start(); |
| 1991 | |
| 1992 | // See UntrustedClient. |
| 1993 | bool readOk = rpcSecurity != RpcSecurity::TLS; |
| 1994 | maliciousClient.run(true, readOk); |
| 1995 | } |
| 1996 | |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 1997 | TEST_P(RpcTransportTest, Trigger) { |
| 1998 | std::string msg2 = ", world!"; |
| 1999 | std::mutex writeMutex; |
| 2000 | std::condition_variable writeCv; |
| 2001 | bool shouldContinueWriting = false; |
| 2002 | auto serverPostConnect = [&](RpcTransport* serverTransport, FdTrigger* fdTrigger) { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2003 | std::string message(RpcTransportTestUtils::kMessage); |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 2004 | iovec messageIov{message.data(), message.size()}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 2005 | auto status = |
| 2006 | serverTransport->interruptableWriteFully(fdTrigger, &messageIov, 1, std::nullopt); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2007 | if (status != OK) return AssertionFailure() << statusToString(status); |
| 2008 | |
| 2009 | { |
| 2010 | std::unique_lock<std::mutex> lock(writeMutex); |
| 2011 | if (!writeCv.wait_for(lock, 3s, [&] { return shouldContinueWriting; })) { |
| 2012 | return AssertionFailure() << "write barrier not cleared in time!"; |
| 2013 | } |
| 2014 | } |
| 2015 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 2016 | iovec msg2Iov{msg2.data(), msg2.size()}; |
Devin Moore | 695368f | 2022-06-03 22:29:14 +0000 | [diff] [blame] | 2017 | status = serverTransport->interruptableWriteFully(fdTrigger, &msg2Iov, 1, std::nullopt); |
Steven Moreland | c591b47 | 2021-09-16 13:56:11 -0700 | [diff] [blame] | 2018 | if (status != DEAD_OBJECT) |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2019 | return AssertionFailure() << "When FdTrigger is shut down, interruptableWriteFully " |
Steven Moreland | c591b47 | 2021-09-16 13:56:11 -0700 | [diff] [blame] | 2020 | "should return DEAD_OBJECT, but it is " |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2021 | << statusToString(status); |
| 2022 | return AssertionSuccess(); |
| 2023 | }; |
| 2024 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2025 | auto server = std::make_unique<Server>(); |
| 2026 | ASSERT_TRUE(server->setUp(GetParam())); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2027 | |
| 2028 | // Set up client |
| 2029 | Client client(server->getConnectToServerFn()); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2030 | ASSERT_TRUE(client.setUp(GetParam())); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2031 | |
| 2032 | // Exchange keys |
| 2033 | ASSERT_EQ(OK, trust(&client, server)); |
| 2034 | ASSERT_EQ(OK, trust(server, &client)); |
| 2035 | |
| 2036 | server->setPostConnect(serverPostConnect); |
| 2037 | |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2038 | server->start(); |
| 2039 | // connect() to server and do handshake |
| 2040 | ASSERT_TRUE(client.setUpTransport()); |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 2041 | // read the first message. This ensures that server has finished handshake and start handling |
| 2042 | // client fd. Server thread should pause at writeCv.wait_for(). |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2043 | ASSERT_TRUE(client.readMessage(RpcTransportTestUtils::kMessage)); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2044 | // Trigger server shutdown after server starts handling client FD. This ensures that the second |
| 2045 | // write is on an FdTrigger that has been shut down. |
| 2046 | server->shutdown(); |
| 2047 | // Continues server thread to write the second message. |
| 2048 | { |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 2049 | std::lock_guard<std::mutex> lock(writeMutex); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2050 | shouldContinueWriting = true; |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2051 | } |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 2052 | writeCv.notify_all(); |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2053 | // After this line, server thread unblocks and attempts to write the second message, but |
Steven Moreland | c591b47 | 2021-09-16 13:56:11 -0700 | [diff] [blame] | 2054 | // shutdown is triggered, so write should failed with DEAD_OBJECT. See |serverPostConnect|. |
Yifan Hong | 6751932 | 2021-09-13 18:51:16 -0700 | [diff] [blame] | 2055 | // On the client side, second read fails with DEAD_OBJECT |
| 2056 | ASSERT_FALSE(client.readMessage(msg2)); |
| 2057 | } |
| 2058 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 2059 | INSTANTIATE_TEST_CASE_P(BinderRpc, RpcTransportTest, |
Yifan Hong | 22211f8 | 2021-09-14 12:32:25 -0700 | [diff] [blame] | 2060 | ::testing::ValuesIn(RpcTransportTest::getRpcTranportTestParams()), |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 2061 | RpcTransportTest::PrintParamInfo); |
| 2062 | |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2063 | class RpcTransportTlsKeyTest |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 2064 | : public testing::TestWithParam< |
| 2065 | std::tuple<SocketType, RpcCertificateFormat, RpcKeyFormat, uint32_t>> { |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2066 | public: |
| 2067 | template <typename A, typename B> |
| 2068 | status_t trust(const A& a, const B& b) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 2069 | auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam(); |
| 2070 | (void)serverVersion; |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2071 | return RpcTransportTestUtils::trust(RpcSecurity::TLS, certificateFormat, a, b); |
| 2072 | } |
| 2073 | static std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 2074 | auto [socketType, certificateFormat, keyFormat, serverVersion] = info.param; |
| 2075 | return PrintToString(socketType) + "_certificate_" + PrintToString(certificateFormat) + |
| 2076 | "_key_" + PrintToString(keyFormat) + "_serverV" + std::to_string(serverVersion); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2077 | }; |
| 2078 | }; |
| 2079 | |
| 2080 | TEST_P(RpcTransportTlsKeyTest, PreSignedCertificate) { |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 2081 | auto [socketType, certificateFormat, keyFormat, serverVersion] = GetParam(); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2082 | |
| 2083 | std::vector<uint8_t> pkeyData, certData; |
| 2084 | { |
| 2085 | auto pkey = makeKeyPairForSelfSignedCert(); |
| 2086 | ASSERT_NE(nullptr, pkey); |
| 2087 | auto cert = makeSelfSignedCert(pkey.get(), kCertValidSeconds); |
| 2088 | ASSERT_NE(nullptr, cert); |
| 2089 | pkeyData = serializeUnencryptedPrivatekey(pkey.get(), keyFormat); |
| 2090 | certData = serializeCertificate(cert.get(), certificateFormat); |
| 2091 | } |
| 2092 | |
| 2093 | auto desPkey = deserializeUnencryptedPrivatekey(pkeyData, keyFormat); |
| 2094 | auto desCert = deserializeCertificate(certData, certificateFormat); |
| 2095 | auto auth = std::make_unique<RpcAuthPreSigned>(std::move(desPkey), std::move(desCert)); |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 2096 | auto utilsParam = std::make_tuple(socketType, RpcSecurity::TLS, |
| 2097 | std::make_optional(certificateFormat), serverVersion); |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2098 | |
| 2099 | auto server = std::make_unique<RpcTransportTestUtils::Server>(); |
| 2100 | ASSERT_TRUE(server->setUp(utilsParam, std::move(auth))); |
| 2101 | |
| 2102 | RpcTransportTestUtils::Client client(server->getConnectToServerFn()); |
| 2103 | ASSERT_TRUE(client.setUp(utilsParam)); |
| 2104 | |
| 2105 | ASSERT_EQ(OK, trust(&client, server)); |
| 2106 | ASSERT_EQ(OK, trust(server, &client)); |
| 2107 | |
| 2108 | server->start(); |
| 2109 | client.run(); |
| 2110 | } |
| 2111 | |
| 2112 | INSTANTIATE_TEST_CASE_P( |
| 2113 | BinderRpc, RpcTransportTlsKeyTest, |
| 2114 | testing::Combine(testing::ValuesIn(testSocketTypes(false /* hasPreconnected*/)), |
| 2115 | testing::Values(RpcCertificateFormat::PEM, RpcCertificateFormat::DER), |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 2116 | testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER), |
| 2117 | testing::ValuesIn(testVersions())), |
Yifan Hong | b1ce80c | 2021-09-17 22:10:58 -0700 | [diff] [blame] | 2118 | RpcTransportTlsKeyTest::PrintParamInfo); |
| 2119 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 2120 | } // namespace android |
| 2121 | |
| 2122 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 2123 | ::testing::InitGoogleTest(&argc, argv); |
| 2124 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
Steven Moreland | a83191d | 2021-10-27 10:14:53 -0700 | [diff] [blame] | 2125 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 2126 | return RUN_ALL_TESTS(); |
| 2127 | } |