blob: 880b9ce0d7485f52fa03f4633bfad85936299d1a [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
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 Hong1deca4b2021-09-10 16:16:44 -070017#include <BinderRpcTestClientInfo.h>
18#include <BinderRpcTestServerInfo.h>
Steven Moreland659416d2021-05-11 00:47:50 +000019#include <BnBinderRpcCallback.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000020#include <BnBinderRpcSession.h>
21#include <BnBinderRpcTest.h>
Steven Moreland37aff182021-03-26 02:04:16 +000022#include <aidl/IBinderRpcTest.h>
Yifan Hong6d82c8a2021-04-26 20:26:45 -070023#include <android-base/file.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000024#include <android-base/logging.h>
Steven Moreland37aff182021-03-26 02:04:16 +000025#include <android/binder_auto_utils.h>
26#include <android/binder_libbinder.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000027#include <binder/Binder.h>
28#include <binder/BpBinder.h>
Steven Morelandd7302072021-05-15 01:32:04 +000029#include <binder/IPCThreadState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000030#include <binder/IServiceManager.h>
31#include <binder/ProcessState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000032#include <binder/RpcServer.h>
Steven Morelandbdb53ab2021-05-05 17:57:41 +000033#include <binder/RpcSession.h>
Yifan Hong702115c2021-06-24 15:39:18 -070034#include <binder/RpcTransport.h>
35#include <binder/RpcTransportRaw.h>
Yifan Hong92409752021-07-30 21:25:32 -070036#include <binder/RpcTransportTls.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000037#include <gtest/gtest.h>
38
Steven Morelandc1635952021-04-01 16:20:47 +000039#include <chrono>
40#include <cstdlib>
41#include <iostream>
42#include <thread>
Steven Moreland659416d2021-05-11 00:47:50 +000043#include <type_traits>
Steven Morelandc1635952021-04-01 16:20:47 +000044
Yifan Hong1deca4b2021-09-10 16:16:44 -070045#include <poll.h>
Steven Morelandc1635952021-04-01 16:20:47 +000046#include <sys/prctl.h>
47#include <unistd.h>
48
Yifan Hong1deca4b2021-09-10 16:16:44 -070049#include "../FdTrigger.h"
Steven Moreland4198a122021-08-03 17:37:58 -070050#include "../RpcSocketAddress.h" // for testing preconnected clients
Steven Morelandbd5002b2021-05-04 23:12:56 +000051#include "../RpcState.h" // for debugging
52#include "../vm_sockets.h" // for VMADDR_*
Yifan Hong13c90062021-09-09 14:59:53 -070053#include "RpcCertificateVerifierSimple.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000054
Yifan Hong1a235852021-05-13 16:07:47 -070055using namespace std::chrono_literals;
Yifan Hong1deca4b2021-09-10 16:16:44 -070056using testing::AssertionFailure;
57using testing::AssertionResult;
58using testing::AssertionSuccess;
Yifan Hong1a235852021-05-13 16:07:47 -070059
Steven Moreland5553ac42020-11-11 02:14:45 +000060namespace android {
61
Steven Morelandbf57bce2021-07-26 15:26:12 -070062static_assert(RPC_WIRE_PROTOCOL_VERSION + 1 == RPC_WIRE_PROTOCOL_VERSION_NEXT ||
63 RPC_WIRE_PROTOCOL_VERSION == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL);
Devin Mooref3b9c4f2021-08-03 15:50:13 +000064const char* kLocalInetAddress = "127.0.0.1";
Steven Morelandbf57bce2021-07-26 15:26:12 -070065
Yifan Hong92409752021-07-30 21:25:32 -070066enum class RpcSecurity { RAW, TLS };
Yifan Hong702115c2021-06-24 15:39:18 -070067
68static inline std::vector<RpcSecurity> RpcSecurityValues() {
Yifan Hong92409752021-07-30 21:25:32 -070069 return {RpcSecurity::RAW, RpcSecurity::TLS};
Yifan Hong702115c2021-06-24 15:39:18 -070070}
71
Yifan Hong13c90062021-09-09 14:59:53 -070072static inline std::unique_ptr<RpcTransportCtxFactory> newFactory(
73 RpcSecurity rpcSecurity, std::shared_ptr<RpcCertificateVerifier> verifier = nullptr) {
Yifan Hong702115c2021-06-24 15:39:18 -070074 switch (rpcSecurity) {
75 case RpcSecurity::RAW:
76 return RpcTransportCtxFactoryRaw::make();
Yifan Hong13c90062021-09-09 14:59:53 -070077 case RpcSecurity::TLS: {
Yifan Hong13c90062021-09-09 14:59:53 -070078 if (verifier == nullptr) {
79 verifier = std::make_shared<RpcCertificateVerifierSimple>();
80 }
81 return RpcTransportCtxFactoryTls::make(std::move(verifier));
82 }
Yifan Hong702115c2021-06-24 15:39:18 -070083 default:
84 LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity);
85 }
86}
87
Steven Moreland1fda67b2021-04-02 18:35:50 +000088TEST(BinderRpcParcel, EntireParcelFormatted) {
89 Parcel p;
90 p.writeInt32(3);
91
92 EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), "");
93}
94
Yifan Hong702115c2021-06-24 15:39:18 -070095class BinderRpcSimple : public ::testing::TestWithParam<RpcSecurity> {
96public:
97 static std::string PrintTestParam(const ::testing::TestParamInfo<ParamType>& info) {
98 return newFactory(info.param)->toCString();
99 }
100};
101
102TEST_P(BinderRpcSimple, SetExternalServerTest) {
Yifan Hong00aeb762021-05-12 17:07:36 -0700103 base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)));
104 int sinkFd = sink.get();
Yifan Hong702115c2021-06-24 15:39:18 -0700105 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong00aeb762021-05-12 17:07:36 -0700106 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
107 ASSERT_FALSE(server->hasServer());
Steven Moreland2372f9d2021-08-05 15:42:01 -0700108 ASSERT_EQ(OK, server->setupExternalServer(std::move(sink)));
Yifan Hong00aeb762021-05-12 17:07:36 -0700109 ASSERT_TRUE(server->hasServer());
110 base::unique_fd retrieved = server->releaseServer();
111 ASSERT_FALSE(server->hasServer());
112 ASSERT_EQ(sinkFd, retrieved.get());
113}
114
Steven Morelandbf57bce2021-07-26 15:26:12 -0700115TEST(BinderRpc, CannotUseNextWireVersion) {
116 auto session = RpcSession::make();
117 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT));
118 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 1));
119 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 2));
120 EXPECT_FALSE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_NEXT + 15));
121}
122
123TEST(BinderRpc, CanUseExperimentalWireVersion) {
124 auto session = RpcSession::make();
125 EXPECT_TRUE(session->setProtocolVersion(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL));
126}
127
Steven Moreland5553ac42020-11-11 02:14:45 +0000128using android::binder::Status;
129
130#define EXPECT_OK(status) \
131 do { \
132 Status stat = (status); \
133 EXPECT_TRUE(stat.isOk()) << stat; \
134 } while (false)
135
136class MyBinderRpcSession : public BnBinderRpcSession {
137public:
138 static std::atomic<int32_t> gNum;
139
140 MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; }
141 Status getName(std::string* name) override {
142 *name = mName;
143 return Status::ok();
144 }
145 ~MyBinderRpcSession() { gNum--; }
146
147private:
148 std::string mName;
149};
150std::atomic<int32_t> MyBinderRpcSession::gNum;
151
Steven Moreland659416d2021-05-11 00:47:50 +0000152class MyBinderRpcCallback : public BnBinderRpcCallback {
153 Status sendCallback(const std::string& value) {
154 std::unique_lock _l(mMutex);
155 mValues.push_back(value);
156 _l.unlock();
157 mCv.notify_one();
158 return Status::ok();
159 }
160 Status sendOnewayCallback(const std::string& value) { return sendCallback(value); }
161
162public:
163 std::mutex mMutex;
164 std::condition_variable mCv;
165 std::vector<std::string> mValues;
166};
167
Steven Moreland5553ac42020-11-11 02:14:45 +0000168class MyBinderRpcTest : public BnBinderRpcTest {
169public:
Steven Moreland611d15f2021-05-01 01:28:27 +0000170 wp<RpcServer> server;
Steven Moreland5553ac42020-11-11 02:14:45 +0000171
172 Status sendString(const std::string& str) override {
Steven Morelandc6046982021-04-20 00:49:42 +0000173 (void)str;
Steven Moreland5553ac42020-11-11 02:14:45 +0000174 return Status::ok();
175 }
176 Status doubleString(const std::string& str, std::string* strstr) override {
Steven Moreland5553ac42020-11-11 02:14:45 +0000177 *strstr = str + str;
178 return Status::ok();
179 }
Steven Moreland736664b2021-05-01 04:27:25 +0000180 Status countBinders(std::vector<int32_t>* out) override {
Steven Moreland611d15f2021-05-01 01:28:27 +0000181 sp<RpcServer> spServer = server.promote();
182 if (spServer == nullptr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000183 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
184 }
Steven Moreland736664b2021-05-01 04:27:25 +0000185 out->clear();
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000186 for (auto session : spServer->listSessions()) {
187 size_t count = session->state()->countBinders();
Steven Moreland736664b2021-05-01 04:27:25 +0000188 out->push_back(count);
Steven Moreland611d15f2021-05-01 01:28:27 +0000189 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000190 return Status::ok();
191 }
192 Status pingMe(const sp<IBinder>& binder, int32_t* out) override {
193 if (binder == nullptr) {
194 std::cout << "Received null binder!" << std::endl;
195 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
196 }
197 *out = binder->pingBinder();
198 return Status::ok();
199 }
200 Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override {
201 *out = binder;
202 return Status::ok();
203 }
204 static sp<IBinder> mHeldBinder;
205 Status holdBinder(const sp<IBinder>& binder) override {
206 mHeldBinder = binder;
207 return Status::ok();
208 }
209 Status getHeldBinder(sp<IBinder>* held) override {
210 *held = mHeldBinder;
211 return Status::ok();
212 }
213 Status nestMe(const sp<IBinderRpcTest>& binder, int count) override {
214 if (count <= 0) return Status::ok();
215 return binder->nestMe(this, count - 1);
216 }
217 Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override {
218 static sp<IBinder> binder = new BBinder;
219 *out = binder;
220 return Status::ok();
221 }
222 Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override {
223 *out = new MyBinderRpcSession(name);
224 return Status::ok();
225 }
226 Status getNumOpenSessions(int32_t* out) override {
227 *out = MyBinderRpcSession::gNum;
228 return Status::ok();
229 }
230
231 std::mutex blockMutex;
232 Status lock() override {
233 blockMutex.lock();
234 return Status::ok();
235 }
236 Status unlockInMsAsync(int32_t ms) override {
237 usleep(ms * 1000);
238 blockMutex.unlock();
239 return Status::ok();
240 }
241 Status lockUnlock() override {
242 std::lock_guard<std::mutex> _l(blockMutex);
243 return Status::ok();
244 }
245
246 Status sleepMs(int32_t ms) override {
247 usleep(ms * 1000);
248 return Status::ok();
249 }
250
251 Status sleepMsAsync(int32_t ms) override {
252 // In-process binder calls are asynchronous, but the call to this method
253 // is synchronous wrt its client. This in/out-process threading model
254 // diffentiation is a classic binder leaky abstraction (for better or
255 // worse) and is preserved here the way binder sockets plugs itself
256 // into BpBinder, as nothing is changed at the higher levels
257 // (IInterface) which result in this behavior.
258 return sleepMs(ms);
259 }
260
Steven Moreland659416d2021-05-11 00:47:50 +0000261 Status doCallback(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed,
262 const std::string& value) override {
263 if (callback == nullptr) {
264 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
265 }
266
267 if (delayed) {
268 std::thread([=]() {
269 ALOGE("Executing delayed callback: '%s'", value.c_str());
Steven Morelandc7d40132021-06-10 03:42:11 +0000270 Status status = doCallback(callback, oneway, false, value);
271 ALOGE("Delayed callback status: '%s'", status.toString8().c_str());
Steven Moreland659416d2021-05-11 00:47:50 +0000272 }).detach();
273 return Status::ok();
274 }
275
276 if (oneway) {
277 return callback->sendOnewayCallback(value);
278 }
279
280 return callback->sendCallback(value);
281 }
282
Steven Morelandc7d40132021-06-10 03:42:11 +0000283 Status doCallbackAsync(const sp<IBinderRpcCallback>& callback, bool oneway, bool delayed,
284 const std::string& value) override {
285 return doCallback(callback, oneway, delayed, value);
286 }
287
Steven Moreland5553ac42020-11-11 02:14:45 +0000288 Status die(bool cleanup) override {
289 if (cleanup) {
290 exit(1);
291 } else {
292 _exit(1);
293 }
294 }
Steven Morelandaf4ca712021-05-24 23:22:08 +0000295
296 Status scheduleShutdown() override {
297 sp<RpcServer> strongServer = server.promote();
298 if (strongServer == nullptr) {
299 return Status::fromExceptionCode(Status::EX_NULL_POINTER);
300 }
301 std::thread([=] {
302 LOG_ALWAYS_FATAL_IF(!strongServer->shutdown(), "Could not shutdown");
303 }).detach();
304 return Status::ok();
305 }
306
Steven Morelandd7302072021-05-15 01:32:04 +0000307 Status useKernelBinderCallingId() override {
308 // this is WRONG! It does not make sense when using RPC binder, and
309 // because it is SO wrong, and so much code calls this, it should abort!
310
311 (void)IPCThreadState::self()->getCallingPid();
312 return Status::ok();
313 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000314};
315sp<IBinder> MyBinderRpcTest::mHeldBinder;
316
317class Process {
318public:
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700319 Process(Process&&) = default;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700320 Process(const std::function<void(android::base::borrowed_fd /* writeEnd */,
321 android::base::borrowed_fd /* readEnd */)>& f) {
322 android::base::unique_fd childWriteEnd;
323 android::base::unique_fd childReadEnd;
324 CHECK(android::base::Pipe(&mReadEnd, &childWriteEnd)) << strerror(errno);
325 CHECK(android::base::Pipe(&childReadEnd, &mWriteEnd)) << strerror(errno);
Steven Moreland5553ac42020-11-11 02:14:45 +0000326 if (0 == (mPid = fork())) {
327 // racey: assume parent doesn't crash before this is set
328 prctl(PR_SET_PDEATHSIG, SIGHUP);
329
Yifan Hong1deca4b2021-09-10 16:16:44 -0700330 f(childWriteEnd, childReadEnd);
Steven Morelandaf4ca712021-05-24 23:22:08 +0000331
332 exit(0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000333 }
334 }
335 ~Process() {
336 if (mPid != 0) {
Steven Morelandaf4ca712021-05-24 23:22:08 +0000337 waitpid(mPid, nullptr, 0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000338 }
339 }
Yifan Hong0f58fb92021-06-16 16:09:23 -0700340 android::base::borrowed_fd readEnd() { return mReadEnd; }
Yifan Hong1deca4b2021-09-10 16:16:44 -0700341 android::base::borrowed_fd writeEnd() { return mWriteEnd; }
Steven Moreland5553ac42020-11-11 02:14:45 +0000342
343private:
344 pid_t mPid = 0;
Yifan Hong0f58fb92021-06-16 16:09:23 -0700345 android::base::unique_fd mReadEnd;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700346 android::base::unique_fd mWriteEnd;
Steven Moreland5553ac42020-11-11 02:14:45 +0000347};
348
349static std::string allocateSocketAddress() {
350 static size_t id = 0;
Steven Moreland4bfbf2e2021-04-14 22:15:16 +0000351 std::string temp = getenv("TMPDIR") ?: "/tmp";
Yifan Hong1deca4b2021-09-10 16:16:44 -0700352 auto ret = temp + "/binderRpcTest_" + std::to_string(id++);
353 unlink(ret.c_str());
354 return ret;
Steven Moreland5553ac42020-11-11 02:14:45 +0000355};
356
Steven Morelandda573042021-06-12 01:13:45 +0000357static unsigned int allocateVsockPort() {
358 static unsigned int vsockPort = 3456;
359 return vsockPort++;
360}
361
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000362struct ProcessSession {
Steven Moreland5553ac42020-11-11 02:14:45 +0000363 // reference to process hosting a socket server
364 Process host;
365
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000366 struct SessionInfo {
367 sp<RpcSession> session;
Steven Moreland736664b2021-05-01 04:27:25 +0000368 sp<IBinder> root;
369 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000370
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000371 // client session objects associated with other process
372 // each one represents a separate session
373 std::vector<SessionInfo> sessions;
Steven Moreland5553ac42020-11-11 02:14:45 +0000374
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000375 ProcessSession(ProcessSession&&) = default;
376 ~ProcessSession() {
377 for (auto& session : sessions) {
378 session.root = nullptr;
Steven Moreland736664b2021-05-01 04:27:25 +0000379 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000380
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000381 for (auto& info : sessions) {
382 sp<RpcSession>& session = info.session;
Steven Moreland736664b2021-05-01 04:27:25 +0000383
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000384 EXPECT_NE(nullptr, session);
385 EXPECT_NE(nullptr, session->state());
386 EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:");
Steven Moreland736664b2021-05-01 04:27:25 +0000387
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000388 wp<RpcSession> weakSession = session;
389 session = nullptr;
390 EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session";
Steven Moreland736664b2021-05-01 04:27:25 +0000391 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000392 }
393};
394
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000395// Process session where the process hosts IBinderRpcTest, the server used
Steven Moreland5553ac42020-11-11 02:14:45 +0000396// for most testing here
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000397struct BinderRpcTestProcessSession {
398 ProcessSession proc;
Steven Moreland5553ac42020-11-11 02:14:45 +0000399
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000400 // pre-fetched root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000401 sp<IBinder> rootBinder;
402
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000403 // pre-casted root object (for first session)
Steven Moreland5553ac42020-11-11 02:14:45 +0000404 sp<IBinderRpcTest> rootIface;
405
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000406 // whether session should be invalidated by end of run
Steven Morelandaf4ca712021-05-24 23:22:08 +0000407 bool expectAlreadyShutdown = false;
Steven Moreland736664b2021-05-01 04:27:25 +0000408
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000409 BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default;
410 ~BinderRpcTestProcessSession() {
Steven Moreland659416d2021-05-11 00:47:50 +0000411 EXPECT_NE(nullptr, rootIface);
412 if (rootIface == nullptr) return;
413
Steven Morelandaf4ca712021-05-24 23:22:08 +0000414 if (!expectAlreadyShutdown) {
Steven Moreland736664b2021-05-01 04:27:25 +0000415 std::vector<int32_t> remoteCounts;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000416 // calling over any sessions counts across all sessions
Steven Moreland736664b2021-05-01 04:27:25 +0000417 EXPECT_OK(rootIface->countBinders(&remoteCounts));
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000418 EXPECT_EQ(remoteCounts.size(), proc.sessions.size());
Steven Moreland736664b2021-05-01 04:27:25 +0000419 for (auto remoteCount : remoteCounts) {
420 EXPECT_EQ(remoteCount, 1);
421 }
Steven Morelandaf4ca712021-05-24 23:22:08 +0000422
Steven Moreland798e0d12021-07-14 23:19:25 +0000423 // even though it is on another thread, shutdown races with
424 // the transaction reply being written
425 if (auto status = rootIface->scheduleShutdown(); !status.isOk()) {
426 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
427 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000428 }
429
430 rootIface = nullptr;
431 rootBinder = nullptr;
432 }
433};
434
Steven Morelandc1635952021-04-01 16:20:47 +0000435enum class SocketType {
Steven Moreland4198a122021-08-03 17:37:58 -0700436 PRECONNECTED,
Steven Morelandc1635952021-04-01 16:20:47 +0000437 UNIX,
438 VSOCK,
Yifan Hong0d2bd112021-04-13 17:38:36 -0700439 INET,
Steven Morelandc1635952021-04-01 16:20:47 +0000440};
Yifan Hong702115c2021-06-24 15:39:18 -0700441static inline std::string PrintToString(SocketType socketType) {
442 switch (socketType) {
Steven Moreland4198a122021-08-03 17:37:58 -0700443 case SocketType::PRECONNECTED:
444 return "preconnected_uds";
Steven Morelandc1635952021-04-01 16:20:47 +0000445 case SocketType::UNIX:
446 return "unix_domain_socket";
447 case SocketType::VSOCK:
448 return "vm_socket";
Yifan Hong0d2bd112021-04-13 17:38:36 -0700449 case SocketType::INET:
450 return "inet_socket";
Steven Morelandc1635952021-04-01 16:20:47 +0000451 default:
452 LOG_ALWAYS_FATAL("Unknown socket type");
453 return "";
454 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000455}
Steven Morelandda573042021-06-12 01:13:45 +0000456
Yifan Hong1deca4b2021-09-10 16:16:44 -0700457static base::unique_fd connectTo(const RpcSocketAddress& addr) {
Steven Moreland4198a122021-08-03 17:37:58 -0700458 base::unique_fd serverFd(
459 TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0)));
460 int savedErrno = errno;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700461 CHECK(serverFd.ok()) << "Could not create socket " << addr.toString() << ": "
462 << strerror(savedErrno);
Steven Moreland4198a122021-08-03 17:37:58 -0700463
464 if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) {
465 int savedErrno = errno;
Yifan Hong1deca4b2021-09-10 16:16:44 -0700466 LOG(FATAL) << "Could not connect to socket " << addr.toString() << ": "
467 << strerror(savedErrno);
Steven Moreland4198a122021-08-03 17:37:58 -0700468 }
469 return serverFd;
470}
471
Yifan Hong702115c2021-06-24 15:39:18 -0700472class BinderRpc : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity>> {
Steven Morelandc1635952021-04-01 16:20:47 +0000473public:
Steven Moreland4313d7e2021-07-15 23:41:22 +0000474 struct Options {
475 size_t numThreads = 1;
476 size_t numSessions = 1;
477 size_t numIncomingConnections = 0;
478 };
479
Yifan Hong702115c2021-06-24 15:39:18 -0700480 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
481 auto [type, security] = info.param;
482 return PrintToString(type) + "_" + newFactory(security)->toCString();
483 }
484
Yifan Hong1deca4b2021-09-10 16:16:44 -0700485 static inline void writeString(android::base::borrowed_fd fd, std::string_view str) {
486 uint64_t length = str.length();
487 CHECK(android::base::WriteFully(fd, &length, sizeof(length)));
488 CHECK(android::base::WriteFully(fd, str.data(), str.length()));
489 }
490
491 static inline std::string readString(android::base::borrowed_fd fd) {
492 uint64_t length;
493 CHECK(android::base::ReadFully(fd, &length, sizeof(length)));
494 std::string ret(length, '\0');
495 CHECK(android::base::ReadFully(fd, ret.data(), length));
496 return ret;
497 }
498
499 static inline void writeToFd(android::base::borrowed_fd fd, const Parcelable& parcelable) {
500 Parcel parcel;
501 CHECK_EQ(OK, parcelable.writeToParcel(&parcel));
502 writeString(fd,
503 std::string(reinterpret_cast<const char*>(parcel.data()), parcel.dataSize()));
504 }
505
506 template <typename T>
507 static inline T readFromFd(android::base::borrowed_fd fd) {
508 std::string data = readString(fd);
509 Parcel parcel;
510 CHECK_EQ(OK, parcel.setData(reinterpret_cast<const uint8_t*>(data.data()), data.size()));
511 T object;
512 CHECK_EQ(OK, object.readFromParcel(&parcel));
513 return object;
514 }
515
Steven Morelandc1635952021-04-01 16:20:47 +0000516 // This creates a new process serving an interface on a certain number of
517 // threads.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000518 ProcessSession createRpcTestSocketServerProcess(
Steven Moreland4313d7e2021-07-15 23:41:22 +0000519 const Options& options, const std::function<void(const sp<RpcServer>&)>& configure) {
520 CHECK_GE(options.numSessions, 1) << "Must have at least one session to a server";
Steven Moreland736664b2021-05-01 04:27:25 +0000521
Yifan Hong702115c2021-06-24 15:39:18 -0700522 SocketType socketType = std::get<0>(GetParam());
523 RpcSecurity rpcSecurity = std::get<1>(GetParam());
Steven Morelandc1635952021-04-01 16:20:47 +0000524
Steven Morelandda573042021-06-12 01:13:45 +0000525 unsigned int vsockPort = allocateVsockPort();
Steven Morelandc1635952021-04-01 16:20:47 +0000526 std::string addr = allocateSocketAddress();
Steven Morelandc1635952021-04-01 16:20:47 +0000527
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000528 auto ret = ProcessSession{
Yifan Hong1deca4b2021-09-10 16:16:44 -0700529 .host = Process([&](android::base::borrowed_fd writeEnd,
530 android::base::borrowed_fd readEnd) {
531 auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>();
532 sp<RpcServer> server = RpcServer::make(newFactory(rpcSecurity, certVerifier));
Steven Morelandc1635952021-04-01 16:20:47 +0000533
534 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland4313d7e2021-07-15 23:41:22 +0000535 server->setMaxThreads(options.numThreads);
Steven Morelandc1635952021-04-01 16:20:47 +0000536
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000537 unsigned int outPort = 0;
538
Steven Morelandc1635952021-04-01 16:20:47 +0000539 switch (socketType) {
Steven Moreland4198a122021-08-03 17:37:58 -0700540 case SocketType::PRECONNECTED:
541 [[fallthrough]];
Steven Morelandc1635952021-04-01 16:20:47 +0000542 case SocketType::UNIX:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700543 CHECK_EQ(OK, server->setupUnixDomainServer(addr.c_str())) << addr;
Steven Morelandc1635952021-04-01 16:20:47 +0000544 break;
545 case SocketType::VSOCK:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700546 CHECK_EQ(OK, server->setupVsockServer(vsockPort));
Steven Morelandc1635952021-04-01 16:20:47 +0000547 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700548 case SocketType::INET: {
Steven Moreland2372f9d2021-08-05 15:42:01 -0700549 CHECK_EQ(OK, server->setupInetServer(kLocalInetAddress, 0, &outPort));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700550 CHECK_NE(0, outPort);
Yifan Hong0d2bd112021-04-13 17:38:36 -0700551 break;
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700552 }
Steven Morelandc1635952021-04-01 16:20:47 +0000553 default:
554 LOG_ALWAYS_FATAL("Unknown socket type");
555 }
556
Yifan Hong1deca4b2021-09-10 16:16:44 -0700557 BinderRpcTestServerInfo serverInfo;
558 serverInfo.port = static_cast<int64_t>(outPort);
Yifan Hong9734cfc2021-09-13 16:14:09 -0700559 serverInfo.cert.data = server->getCertificate(RpcCertificateFormat::PEM);
Yifan Hong1deca4b2021-09-10 16:16:44 -0700560 writeToFd(writeEnd, serverInfo);
561 auto clientInfo = readFromFd<BinderRpcTestClientInfo>(readEnd);
562
563 if (rpcSecurity == RpcSecurity::TLS) {
564 for (const auto& clientCert : clientInfo.certs) {
565 CHECK_EQ(OK,
Yifan Hong9734cfc2021-09-13 16:14:09 -0700566 certVerifier
567 ->addTrustedPeerCertificate(RpcCertificateFormat::PEM,
568 clientCert.data));
Yifan Hong1deca4b2021-09-10 16:16:44 -0700569 }
570 }
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000571
Steven Moreland611d15f2021-05-01 01:28:27 +0000572 configure(server);
Steven Morelandc1635952021-04-01 16:20:47 +0000573
Steven Morelandf137de92021-04-24 01:54:26 +0000574 server->join();
Steven Morelandaf4ca712021-05-24 23:22:08 +0000575
576 // Another thread calls shutdown. Wait for it to complete.
577 (void)server->shutdown();
Steven Morelandc1635952021-04-01 16:20:47 +0000578 }),
Steven Morelandc1635952021-04-01 16:20:47 +0000579 };
580
Yifan Hong1deca4b2021-09-10 16:16:44 -0700581 std::vector<sp<RpcSession>> sessions;
582 auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>();
583 for (size_t i = 0; i < options.numSessions; i++) {
584 sessions.emplace_back(RpcSession::make(newFactory(rpcSecurity, certVerifier)));
585 }
586
587 auto serverInfo = readFromFd<BinderRpcTestServerInfo>(ret.host.readEnd());
588 BinderRpcTestClientInfo clientInfo;
589 for (const auto& session : sessions) {
590 auto& parcelableCert = clientInfo.certs.emplace_back();
Yifan Hong9734cfc2021-09-13 16:14:09 -0700591 parcelableCert.data = session->getCertificate(RpcCertificateFormat::PEM);
Yifan Hong1deca4b2021-09-10 16:16:44 -0700592 }
593 writeToFd(ret.host.writeEnd(), clientInfo);
594
595 CHECK_LE(serverInfo.port, std::numeric_limits<unsigned int>::max());
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700596 if (socketType == SocketType::INET) {
Yifan Hong1deca4b2021-09-10 16:16:44 -0700597 CHECK_NE(0, serverInfo.port);
598 }
599
600 if (rpcSecurity == RpcSecurity::TLS) {
601 const auto& serverCert = serverInfo.cert.data;
602 CHECK_EQ(OK,
Yifan Hong9734cfc2021-09-13 16:14:09 -0700603 certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM,
604 serverCert));
Yifan Hong6d82c8a2021-04-26 20:26:45 -0700605 }
606
Steven Moreland2372f9d2021-08-05 15:42:01 -0700607 status_t status;
608
Yifan Hong1deca4b2021-09-10 16:16:44 -0700609 for (const auto& session : sessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000610 session->setMaxThreads(options.numIncomingConnections);
Steven Moreland659416d2021-05-11 00:47:50 +0000611
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000612 switch (socketType) {
Steven Moreland4198a122021-08-03 17:37:58 -0700613 case SocketType::PRECONNECTED:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700614 status = session->setupPreconnectedClient({}, [=]() {
Yifan Hong1deca4b2021-09-10 16:16:44 -0700615 return connectTo(UnixSocketAddress(addr.c_str()));
Steven Moreland2372f9d2021-08-05 15:42:01 -0700616 });
617 if (status == OK) goto success;
Steven Moreland4198a122021-08-03 17:37:58 -0700618 break;
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000619 case SocketType::UNIX:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700620 status = session->setupUnixDomainClient(addr.c_str());
621 if (status == OK) goto success;
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000622 break;
623 case SocketType::VSOCK:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700624 status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort);
625 if (status == OK) goto success;
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000626 break;
627 case SocketType::INET:
Yifan Hong1deca4b2021-09-10 16:16:44 -0700628 status = session->setupInetClient("127.0.0.1", serverInfo.port);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700629 if (status == OK) goto success;
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000630 break;
631 default:
632 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000633 }
Steven Moreland2372f9d2021-08-05 15:42:01 -0700634 LOG_ALWAYS_FATAL("Could not connect %s", statusToString(status).c_str());
Steven Moreland736664b2021-05-01 04:27:25 +0000635 success:
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000636 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000637 }
Steven Morelandc1635952021-04-01 16:20:47 +0000638 return ret;
639 }
640
Steven Moreland4313d7e2021-07-15 23:41:22 +0000641 BinderRpcTestProcessSession createRpcTestSocketServerProcess(const Options& options) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000642 BinderRpcTestProcessSession ret{
Steven Moreland4313d7e2021-07-15 23:41:22 +0000643 .proc = createRpcTestSocketServerProcess(options,
Steven Moreland611d15f2021-05-01 01:28:27 +0000644 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000645 sp<MyBinderRpcTest> service =
646 new MyBinderRpcTest;
647 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000648 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000649 }),
650 };
651
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000652 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000653 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
654
655 return ret;
656 }
657};
658
Steven Morelandc1635952021-04-01 16:20:47 +0000659TEST_P(BinderRpc, Ping) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000660 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000661 ASSERT_NE(proc.rootBinder, nullptr);
662 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
663}
664
Steven Moreland4cf688f2021-03-31 01:48:58 +0000665TEST_P(BinderRpc, GetInterfaceDescriptor) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000666 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland4cf688f2021-03-31 01:48:58 +0000667 ASSERT_NE(proc.rootBinder, nullptr);
668 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
669}
670
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000671TEST_P(BinderRpc, MultipleSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000672 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5});
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000673 for (auto session : proc.proc.sessions) {
674 ASSERT_NE(nullptr, session.root);
675 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000676 }
677}
678
Steven Morelandc1635952021-04-01 16:20:47 +0000679TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000680 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000681 Parcel data;
682 Parcel reply;
683 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
684}
685
Steven Moreland67753c32021-04-02 18:45:19 +0000686TEST_P(BinderRpc, AppendSeparateFormats) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000687 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland67753c32021-04-02 18:45:19 +0000688
689 Parcel p1;
690 p1.markForBinder(proc.rootBinder);
691 p1.writeInt32(3);
692
693 Parcel p2;
694
695 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
696 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
697}
698
Steven Morelandc1635952021-04-01 16:20:47 +0000699TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000700 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000701 Parcel data;
702 data.markForBinder(proc.rootBinder);
703 Parcel reply;
704 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
705}
706
Steven Morelandc1635952021-04-01 16:20:47 +0000707TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000708 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000709 EXPECT_OK(proc.rootIface->sendString("asdf"));
710}
711
Steven Morelandc1635952021-04-01 16:20:47 +0000712TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000713 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000714 std::string doubled;
715 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
716 EXPECT_EQ("cool cool ", doubled);
717}
718
Steven Morelandc1635952021-04-01 16:20:47 +0000719TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000720 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000721 std::string single = std::string(1024, 'a');
722 std::string doubled;
723 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
724 EXPECT_EQ(single + single, doubled);
725}
726
Steven Morelandc1635952021-04-01 16:20:47 +0000727TEST_P(BinderRpc, CallMeBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000728 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000729
730 int32_t pingResult;
731 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
732 EXPECT_EQ(OK, pingResult);
733
734 EXPECT_EQ(0, MyBinderRpcSession::gNum);
735}
736
Steven Morelandc1635952021-04-01 16:20:47 +0000737TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000738 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000739
740 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
741 sp<IBinder> outBinder;
742 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
743 EXPECT_EQ(inBinder, outBinder);
744
745 wp<IBinder> weak = inBinder;
746 inBinder = nullptr;
747 outBinder = nullptr;
748
749 // Force reading a reply, to process any pending dec refs from the other
750 // process (the other process will process dec refs there before processing
751 // the ping here).
752 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
753
754 EXPECT_EQ(nullptr, weak.promote());
755
756 EXPECT_EQ(0, MyBinderRpcSession::gNum);
757}
758
Steven Morelandc1635952021-04-01 16:20:47 +0000759TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000760 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000761
762 sp<IBinderRpcSession> session;
763 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
764
765 sp<IBinder> inBinder = IInterface::asBinder(session);
766 sp<IBinder> outBinder;
767 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
768 EXPECT_EQ(inBinder, outBinder);
769
770 wp<IBinder> weak = inBinder;
771 session = nullptr;
772 inBinder = nullptr;
773 outBinder = nullptr;
774
775 // Force reading a reply, to process any pending dec refs from the other
776 // process (the other process will process dec refs there before processing
777 // the ping here).
778 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
779
780 EXPECT_EQ(nullptr, weak.promote());
781}
782
Steven Morelandc1635952021-04-01 16:20:47 +0000783TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000784 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000785
786 sp<IBinder> outBinder;
787 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
788 EXPECT_EQ(nullptr, outBinder);
789}
790
Steven Morelandc1635952021-04-01 16:20:47 +0000791TEST_P(BinderRpc, HoldBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000792 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000793
794 IBinder* ptr = nullptr;
795 {
796 sp<IBinder> binder = new BBinder();
797 ptr = binder.get();
798 EXPECT_OK(proc.rootIface->holdBinder(binder));
799 }
800
801 sp<IBinder> held;
802 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
803
804 EXPECT_EQ(held.get(), ptr);
805
806 // stop holding binder, because we test to make sure references are cleaned
807 // up
808 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
809 // and flush ref counts
810 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
811}
812
813// START TESTS FOR LIMITATIONS OF SOCKET BINDER
814// These are behavioral differences form regular binder, where certain usecases
815// aren't supported.
816
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000817TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000818 auto proc1 = createRpcTestSocketServerProcess({});
819 auto proc2 = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000820
821 sp<IBinder> outBinder;
822 EXPECT_EQ(INVALID_OPERATION,
823 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
824}
825
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000826TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000827 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2});
Steven Moreland736664b2021-05-01 04:27:25 +0000828
829 sp<IBinder> outBinder;
830 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000831 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000832 .transactionError());
833}
834
Steven Morelandc1635952021-04-01 16:20:47 +0000835TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000836 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000837
838 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
839 sp<IBinder> outBinder;
840 EXPECT_EQ(INVALID_OPERATION,
841 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
842}
843
Steven Morelandc1635952021-04-01 16:20:47 +0000844TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000845 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000846
847 // for historical reasons, IServiceManager interface only returns the
848 // exception code
849 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
850 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
851}
852
853// END TESTS FOR LIMITATIONS OF SOCKET BINDER
854
Steven Morelandc1635952021-04-01 16:20:47 +0000855TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000856 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000857
858 sp<IBinder> outBinder;
859 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
860 EXPECT_EQ(proc.rootBinder, outBinder);
861}
862
Steven Morelandc1635952021-04-01 16:20:47 +0000863TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000864 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000865
866 auto nastyNester = sp<MyBinderRpcTest>::make();
867 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
868
869 wp<IBinder> weak = nastyNester;
870 nastyNester = nullptr;
871 EXPECT_EQ(nullptr, weak.promote());
872}
873
Steven Morelandc1635952021-04-01 16:20:47 +0000874TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000875 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000876
877 sp<IBinder> a;
878 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
879
880 sp<IBinder> b;
881 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
882
883 EXPECT_EQ(a, b);
884}
885
Steven Morelandc1635952021-04-01 16:20:47 +0000886TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000887 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000888
889 sp<IBinder> a;
890 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
891 wp<IBinder> weak = a;
892 a = nullptr;
893
894 sp<IBinder> b;
895 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
896
897 // this is the wrong behavior, since BpBinder
898 // doesn't implement onIncStrongAttempted
899 // but make sure there is no crash
900 EXPECT_EQ(nullptr, weak.promote());
901
902 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
903
904 // In order to fix this:
905 // - need to have incStrongAttempted reflected across IPC boundary (wait for
906 // response to promote - round trip...)
907 // - sendOnLastWeakRef, to delete entries out of RpcState table
908 EXPECT_EQ(b, weak.promote());
909}
910
911#define expectSessions(expected, iface) \
912 do { \
913 int session; \
914 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
915 EXPECT_EQ(expected, session); \
916 } while (false)
917
Steven Morelandc1635952021-04-01 16:20:47 +0000918TEST_P(BinderRpc, SingleSession) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000919 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000920
921 sp<IBinderRpcSession> session;
922 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
923 std::string out;
924 EXPECT_OK(session->getName(&out));
925 EXPECT_EQ("aoeu", out);
926
927 expectSessions(1, proc.rootIface);
928 session = nullptr;
929 expectSessions(0, proc.rootIface);
930}
931
Steven Morelandc1635952021-04-01 16:20:47 +0000932TEST_P(BinderRpc, ManySessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000933 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000934
935 std::vector<sp<IBinderRpcSession>> sessions;
936
937 for (size_t i = 0; i < 15; i++) {
938 expectSessions(i, proc.rootIface);
939 sp<IBinderRpcSession> session;
940 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
941 sessions.push_back(session);
942 }
943 expectSessions(sessions.size(), proc.rootIface);
944 for (size_t i = 0; i < sessions.size(); i++) {
945 std::string out;
946 EXPECT_OK(sessions.at(i)->getName(&out));
947 EXPECT_EQ(std::to_string(i), out);
948 }
949 expectSessions(sessions.size(), proc.rootIface);
950
951 while (!sessions.empty()) {
952 sessions.pop_back();
953 expectSessions(sessions.size(), proc.rootIface);
954 }
955 expectSessions(0, proc.rootIface);
956}
957
958size_t epochMillis() {
959 using std::chrono::duration_cast;
960 using std::chrono::milliseconds;
961 using std::chrono::seconds;
962 using std::chrono::system_clock;
963 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
964}
965
Steven Morelandc1635952021-04-01 16:20:47 +0000966TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000967 constexpr size_t kNumThreads = 10;
968
Steven Moreland4313d7e2021-07-15 23:41:22 +0000969 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000970
971 EXPECT_OK(proc.rootIface->lock());
972
973 // block all but one thread taking locks
974 std::vector<std::thread> ts;
975 for (size_t i = 0; i < kNumThreads - 1; i++) {
976 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
977 }
978
979 usleep(100000); // give chance for calls on other threads
980
981 // other calls still work
982 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
983
984 constexpr size_t blockTimeMs = 500;
985 size_t epochMsBefore = epochMillis();
986 // after this, we should never see a response within this time
987 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
988
989 // this call should be blocked for blockTimeMs
990 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
991
992 size_t epochMsAfter = epochMillis();
993 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
994
995 for (auto& t : ts) t.join();
996}
997
Steven Morelandc1635952021-04-01 16:20:47 +0000998TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000999 constexpr size_t kNumThreads = 10;
1000 constexpr size_t kNumCalls = kNumThreads + 3;
1001 constexpr size_t kSleepMs = 500;
1002
Steven Moreland4313d7e2021-07-15 23:41:22 +00001003 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +00001004
1005 size_t epochMsBefore = epochMillis();
1006
1007 std::vector<std::thread> ts;
1008 for (size_t i = 0; i < kNumCalls; i++) {
1009 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
1010 }
1011
1012 for (auto& t : ts) t.join();
1013
1014 size_t epochMsAfter = epochMillis();
1015
1016 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
1017
1018 // Potential flake, but make sure calls are handled in parallel.
1019 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
1020}
1021
Steven Morelandc1635952021-04-01 16:20:47 +00001022TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001023 constexpr size_t kNumClientThreads = 10;
1024 constexpr size_t kNumServerThreads = 10;
1025 constexpr size_t kNumCalls = 100;
1026
Steven Moreland4313d7e2021-07-15 23:41:22 +00001027 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +00001028
1029 std::vector<std::thread> threads;
1030 for (size_t i = 0; i < kNumClientThreads; i++) {
1031 threads.push_back(std::thread([&] {
1032 for (size_t j = 0; j < kNumCalls; j++) {
1033 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +00001034 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +00001035 EXPECT_EQ(proc.rootBinder, out);
1036 }
1037 }));
1038 }
1039
1040 for (auto& t : threads) t.join();
1041}
1042
Steven Morelandc6046982021-04-20 00:49:42 +00001043TEST_P(BinderRpc, OnewayStressTest) {
1044 constexpr size_t kNumClientThreads = 10;
1045 constexpr size_t kNumServerThreads = 10;
Steven Moreland52eee942021-06-03 00:59:28 +00001046 constexpr size_t kNumCalls = 500;
Steven Morelandc6046982021-04-20 00:49:42 +00001047
Steven Moreland4313d7e2021-07-15 23:41:22 +00001048 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Morelandc6046982021-04-20 00:49:42 +00001049
1050 std::vector<std::thread> threads;
1051 for (size_t i = 0; i < kNumClientThreads; i++) {
1052 threads.push_back(std::thread([&] {
1053 for (size_t j = 0; j < kNumCalls; j++) {
1054 EXPECT_OK(proc.rootIface->sendString("a"));
1055 }
1056
1057 // check threads are not stuck
1058 EXPECT_OK(proc.rootIface->sleepMs(250));
1059 }));
1060 }
1061
1062 for (auto& t : threads) t.join();
1063}
1064
Steven Morelandc1635952021-04-01 16:20:47 +00001065TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001066 constexpr size_t kReallyLongTimeMs = 100;
1067 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
1068
Steven Moreland4313d7e2021-07-15 23:41:22 +00001069 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +00001070
1071 size_t epochMsBefore = epochMillis();
1072
1073 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
1074
1075 size_t epochMsAfter = epochMillis();
1076 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
1077}
1078
Steven Morelandc1635952021-04-01 16:20:47 +00001079TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001080 constexpr size_t kNumSleeps = 10;
1081 constexpr size_t kNumExtraServerThreads = 4;
1082 constexpr size_t kSleepMs = 50;
1083
1084 // make sure calls to the same object happen on the same thread
Steven Moreland4313d7e2021-07-15 23:41:22 +00001085 auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +00001086
1087 EXPECT_OK(proc.rootIface->lock());
1088
1089 for (size_t i = 0; i < kNumSleeps; i++) {
1090 // these should be processed serially
1091 proc.rootIface->sleepMsAsync(kSleepMs);
1092 }
1093 // should also be processesed serially
1094 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
1095
1096 size_t epochMsBefore = epochMillis();
1097 EXPECT_OK(proc.rootIface->lockUnlock());
1098 size_t epochMsAfter = epochMillis();
1099
1100 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
Steven Morelandf5174272021-05-25 00:39:28 +00001101
1102 // pending oneway transactions hold ref, make sure we read data on all
1103 // sockets
1104 std::vector<std::thread> threads;
1105 for (size_t i = 0; i < 1 + kNumExtraServerThreads; i++) {
1106 threads.push_back(std::thread([&] { EXPECT_OK(proc.rootIface->sleepMs(250)); }));
1107 }
1108 for (auto& t : threads) t.join();
Steven Moreland5553ac42020-11-11 02:14:45 +00001109}
1110
Steven Morelandd45be622021-06-04 02:19:37 +00001111TEST_P(BinderRpc, OnewayCallExhaustion) {
1112 constexpr size_t kNumClients = 2;
1113 constexpr size_t kTooLongMs = 1000;
1114
Steven Moreland4313d7e2021-07-15 23:41:22 +00001115 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2});
Steven Morelandd45be622021-06-04 02:19:37 +00001116
1117 // Build up oneway calls on the second session to make sure it terminates
1118 // and shuts down. The first session should be unaffected (proc destructor
1119 // checks the first session).
1120 auto iface = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root);
1121
1122 std::vector<std::thread> threads;
1123 for (size_t i = 0; i < kNumClients; i++) {
1124 // one of these threads will get stuck queueing a transaction once the
1125 // socket fills up, the other will be able to fill up transactions on
1126 // this object
1127 threads.push_back(std::thread([&] {
1128 while (iface->sleepMsAsync(kTooLongMs).isOk()) {
1129 }
1130 }));
1131 }
1132 for (auto& t : threads) t.join();
1133
1134 Status status = iface->sleepMsAsync(kTooLongMs);
1135 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1136
Steven Moreland798e0d12021-07-14 23:19:25 +00001137 // now that it has died, wait for the remote session to shutdown
1138 std::vector<int32_t> remoteCounts;
1139 do {
1140 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
1141 } while (remoteCounts.size() == kNumClients);
1142
Steven Morelandd45be622021-06-04 02:19:37 +00001143 // the second session should be shutdown in the other process by the time we
1144 // are able to join above (it'll only be hung up once it finishes processing
1145 // any pending commands). We need to erase this session from the record
1146 // here, so that the destructor for our session won't check that this
1147 // session is valid, but we still want it to test the other session.
1148 proc.proc.sessions.erase(proc.proc.sessions.begin() + 1);
1149}
1150
Steven Moreland659416d2021-05-11 00:47:50 +00001151TEST_P(BinderRpc, Callbacks) {
1152 const static std::string kTestString = "good afternoon!";
1153
Steven Morelandc7d40132021-06-10 03:42:11 +00001154 for (bool callIsOneway : {true, false}) {
1155 for (bool callbackIsOneway : {true, false}) {
1156 for (bool delayed : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001157 auto proc = createRpcTestSocketServerProcess(
1158 {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1});
Steven Morelandc7d40132021-06-10 03:42:11 +00001159 auto cb = sp<MyBinderRpcCallback>::make();
Steven Moreland659416d2021-05-11 00:47:50 +00001160
Steven Morelandc7d40132021-06-10 03:42:11 +00001161 if (callIsOneway) {
1162 EXPECT_OK(proc.rootIface->doCallbackAsync(cb, callbackIsOneway, delayed,
1163 kTestString));
1164 } else {
1165 EXPECT_OK(
1166 proc.rootIface->doCallback(cb, callbackIsOneway, delayed, kTestString));
1167 }
Steven Moreland659416d2021-05-11 00:47:50 +00001168
Steven Morelandc7d40132021-06-10 03:42:11 +00001169 using std::literals::chrono_literals::operator""s;
1170 std::unique_lock<std::mutex> _l(cb->mMutex);
1171 cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); });
Steven Moreland659416d2021-05-11 00:47:50 +00001172
Steven Morelandc7d40132021-06-10 03:42:11 +00001173 EXPECT_EQ(cb->mValues.size(), 1)
1174 << "callIsOneway: " << callIsOneway
1175 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
1176 if (cb->mValues.empty()) continue;
1177 EXPECT_EQ(cb->mValues.at(0), kTestString)
1178 << "callIsOneway: " << callIsOneway
1179 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
Steven Moreland659416d2021-05-11 00:47:50 +00001180
Steven Morelandc7d40132021-06-10 03:42:11 +00001181 // since we are severing the connection, we need to go ahead and
1182 // tell the server to shutdown and exit so that waitpid won't hang
Steven Moreland798e0d12021-07-14 23:19:25 +00001183 if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) {
1184 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1185 }
Steven Moreland659416d2021-05-11 00:47:50 +00001186
Steven Moreland1b304292021-07-15 22:59:34 +00001187 // since this session has an incoming connection w/ a threadpool, we
Steven Morelandc7d40132021-06-10 03:42:11 +00001188 // need to manually shut it down
1189 EXPECT_TRUE(proc.proc.sessions.at(0).session->shutdownAndWait(true));
Steven Moreland659416d2021-05-11 00:47:50 +00001190
Steven Morelandc7d40132021-06-10 03:42:11 +00001191 proc.expectAlreadyShutdown = true;
1192 }
Steven Moreland659416d2021-05-11 00:47:50 +00001193 }
1194 }
1195}
1196
Steven Moreland195edb82021-06-08 02:44:39 +00001197TEST_P(BinderRpc, OnewayCallbackWithNoThread) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001198 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland195edb82021-06-08 02:44:39 +00001199 auto cb = sp<MyBinderRpcCallback>::make();
1200
1201 Status status = proc.rootIface->doCallback(cb, true /*oneway*/, false /*delayed*/, "anything");
1202 EXPECT_EQ(WOULD_BLOCK, status.transactionError());
1203}
1204
Steven Morelandc1635952021-04-01 16:20:47 +00001205TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001206 for (bool doDeathCleanup : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001207 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +00001208
1209 // make sure there is some state during crash
1210 // 1. we hold their binder
1211 sp<IBinderRpcSession> session;
1212 EXPECT_OK(proc.rootIface->openSession("happy", &session));
1213 // 2. they hold our binder
1214 sp<IBinder> binder = new BBinder();
1215 EXPECT_OK(proc.rootIface->holdBinder(binder));
1216
1217 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
1218 << "Do death cleanup: " << doDeathCleanup;
1219
Steven Morelandaf4ca712021-05-24 23:22:08 +00001220 proc.expectAlreadyShutdown = true;
Steven Moreland5553ac42020-11-11 02:14:45 +00001221 }
1222}
1223
Steven Morelandd7302072021-05-15 01:32:04 +00001224TEST_P(BinderRpc, UseKernelBinderCallingId) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001225 auto proc = createRpcTestSocketServerProcess({});
Steven Morelandd7302072021-05-15 01:32:04 +00001226
1227 // we can't allocate IPCThreadState so actually the first time should
1228 // succeed :(
1229 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
1230
1231 // second time! we catch the error :)
1232 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
1233
Steven Morelandaf4ca712021-05-24 23:22:08 +00001234 proc.expectAlreadyShutdown = true;
Steven Morelandd7302072021-05-15 01:32:04 +00001235}
1236
Steven Moreland37aff182021-03-26 02:04:16 +00001237TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001238 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001239
1240 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1241 ASSERT_NE(binder, nullptr);
1242
1243 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1244}
1245
1246TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001247 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001248
1249 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1250 ASSERT_NE(binder, nullptr);
1251
1252 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
1253 ASSERT_NE(ndkBinder, nullptr);
1254
1255 std::string out;
1256 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
1257 ASSERT_TRUE(status.isOk()) << status.getDescription();
1258 ASSERT_EQ("aoeuaoeu", out);
1259}
1260
Steven Moreland5553ac42020-11-11 02:14:45 +00001261ssize_t countFds() {
1262 DIR* dir = opendir("/proc/self/fd/");
1263 if (dir == nullptr) return -1;
1264 ssize_t ret = 0;
1265 dirent* ent;
1266 while ((ent = readdir(dir)) != nullptr) ret++;
1267 closedir(dir);
1268 return ret;
1269}
1270
Steven Morelandc1635952021-04-01 16:20:47 +00001271TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001272 ssize_t beforeFds = countFds();
1273 ASSERT_GE(beforeFds, 0);
1274 {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001275 auto proc = createRpcTestSocketServerProcess({.numThreads = 10});
Steven Moreland5553ac42020-11-11 02:14:45 +00001276 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
1277 }
1278 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
1279}
1280
Steven Morelandda573042021-06-12 01:13:45 +00001281static bool testSupportVsockLoopback() {
Yifan Hong702115c2021-06-24 15:39:18 -07001282 // We don't need to enable TLS to know if vsock is supported.
Steven Morelandda573042021-06-12 01:13:45 +00001283 unsigned int vsockPort = allocateVsockPort();
Yifan Hong702115c2021-06-24 15:39:18 -07001284 sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make());
Steven Morelandda573042021-06-12 01:13:45 +00001285 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland1eab3452021-08-05 16:56:20 -07001286 if (status_t status = server->setupVsockServer(vsockPort); status != OK) {
1287 if (status == -EAFNOSUPPORT) {
1288 return false;
1289 }
1290 LOG_ALWAYS_FATAL("Could not setup vsock server: %s", statusToString(status).c_str());
1291 }
Steven Morelandda573042021-06-12 01:13:45 +00001292 server->start();
1293
Yifan Hongfdd9f692021-09-09 15:12:52 -07001294 sp<RpcSession> session = RpcSession::make(RpcTransportCtxFactoryRaw::make());
Steven Moreland2372f9d2021-08-05 15:42:01 -07001295 status_t status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort);
Steven Moreland798e0d12021-07-14 23:19:25 +00001296 while (!server->shutdown()) usleep(10000);
Steven Moreland2372f9d2021-08-05 15:42:01 -07001297 ALOGE("Detected vsock loopback supported: %s", statusToString(status).c_str());
1298 return status == OK;
Steven Morelandda573042021-06-12 01:13:45 +00001299}
1300
Yifan Hong1deca4b2021-09-10 16:16:44 -07001301static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) {
1302 std::vector<SocketType> ret = {SocketType::UNIX, SocketType::INET};
1303
1304 if (hasPreconnected) ret.push_back(SocketType::PRECONNECTED);
Steven Morelandda573042021-06-12 01:13:45 +00001305
1306 static bool hasVsockLoopback = testSupportVsockLoopback();
1307
1308 if (hasVsockLoopback) {
1309 ret.push_back(SocketType::VSOCK);
1310 }
1311
1312 return ret;
1313}
1314
Yifan Hong702115c2021-06-24 15:39:18 -07001315INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
1316 ::testing::Combine(::testing::ValuesIn(testSocketTypes()),
1317 ::testing::ValuesIn(RpcSecurityValues())),
1318 BinderRpc::PrintParamInfo);
Steven Morelandc1635952021-04-01 16:20:47 +00001319
Yifan Hong702115c2021-06-24 15:39:18 -07001320class BinderRpcServerRootObject
1321 : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001322
1323TEST_P(BinderRpcServerRootObject, WeakRootObject) {
1324 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
1325 auto setRootObject = [](bool isStrong) -> SetFn {
1326 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
1327 };
1328
Yifan Hong702115c2021-06-24 15:39:18 -07001329 auto [isStrong1, isStrong2, rpcSecurity] = GetParam();
1330 auto server = RpcServer::make(newFactory(rpcSecurity));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001331 auto binder1 = sp<BBinder>::make();
1332 IBinder* binderRaw1 = binder1.get();
1333 setRootObject(isStrong1)(server.get(), binder1);
1334 EXPECT_EQ(binderRaw1, server->getRootObject());
1335 binder1.clear();
1336 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
1337
1338 auto binder2 = sp<BBinder>::make();
1339 IBinder* binderRaw2 = binder2.get();
1340 setRootObject(isStrong2)(server.get(), binder2);
1341 EXPECT_EQ(binderRaw2, server->getRootObject());
1342 binder2.clear();
1343 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
1344}
1345
1346INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
Yifan Hong702115c2021-06-24 15:39:18 -07001347 ::testing::Combine(::testing::Bool(), ::testing::Bool(),
1348 ::testing::ValuesIn(RpcSecurityValues())));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001349
Yifan Hong1a235852021-05-13 16:07:47 -07001350class OneOffSignal {
1351public:
1352 // If notify() was previously called, or is called within |duration|, return true; else false.
1353 template <typename R, typename P>
1354 bool wait(std::chrono::duration<R, P> duration) {
1355 std::unique_lock<std::mutex> lock(mMutex);
1356 return mCv.wait_for(lock, duration, [this] { return mValue; });
1357 }
1358 void notify() {
1359 std::unique_lock<std::mutex> lock(mMutex);
1360 mValue = true;
1361 lock.unlock();
1362 mCv.notify_all();
1363 }
1364
1365private:
1366 std::mutex mMutex;
1367 std::condition_variable mCv;
1368 bool mValue = false;
1369};
1370
Yifan Hong702115c2021-06-24 15:39:18 -07001371TEST_P(BinderRpcSimple, Shutdown) {
Yifan Hong1a235852021-05-13 16:07:47 -07001372 auto addr = allocateSocketAddress();
Yifan Hong702115c2021-06-24 15:39:18 -07001373 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong1a235852021-05-13 16:07:47 -07001374 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland2372f9d2021-08-05 15:42:01 -07001375 ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str()));
Yifan Hong1a235852021-05-13 16:07:47 -07001376 auto joinEnds = std::make_shared<OneOffSignal>();
1377
1378 // If things are broken and the thread never stops, don't block other tests. Because the thread
1379 // may run after the test finishes, it must not access the stack memory of the test. Hence,
1380 // shared pointers are passed.
1381 std::thread([server, joinEnds] {
1382 server->join();
1383 joinEnds->notify();
1384 }).detach();
1385
1386 bool shutdown = false;
1387 for (int i = 0; i < 10 && !shutdown; i++) {
1388 usleep(300 * 1000); // 300ms; total 3s
1389 if (server->shutdown()) shutdown = true;
1390 }
1391 ASSERT_TRUE(shutdown) << "server->shutdown() never returns true";
1392
1393 ASSERT_TRUE(joinEnds->wait(2s))
1394 << "After server->shutdown() returns true, join() did not stop after 2s";
1395}
1396
Yifan Hong194acf22021-06-29 18:44:56 -07001397TEST(BinderRpc, Java) {
1398#if !defined(__ANDROID__)
1399 GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on"
1400 "createRpcDelegateServiceManager() with a device attached, such test belongs "
1401 "to binderHostDeviceTest. Hence, just disable this test on host.";
1402#endif // !__ANDROID__
1403 sp<IServiceManager> sm = defaultServiceManager();
1404 ASSERT_NE(nullptr, sm);
1405 // Any Java service with non-empty getInterfaceDescriptor() would do.
1406 // Let's pick batteryproperties.
1407 auto binder = sm->checkService(String16("batteryproperties"));
1408 ASSERT_NE(nullptr, binder);
1409 auto descriptor = binder->getInterfaceDescriptor();
1410 ASSERT_GE(descriptor.size(), 0);
1411 ASSERT_EQ(OK, binder->pingBinder());
1412
1413 auto rpcServer = RpcServer::make();
1414 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1415 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001416 ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port));
Yifan Hong194acf22021-06-29 18:44:56 -07001417 auto socket = rpcServer->releaseServer();
1418
1419 auto keepAlive = sp<BBinder>::make();
1420 ASSERT_EQ(OK, binder->setRpcClientDebug(std::move(socket), keepAlive));
1421
1422 auto rpcSession = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -07001423 ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port));
Yifan Hong194acf22021-06-29 18:44:56 -07001424 auto rpcBinder = rpcSession->getRootObject();
1425 ASSERT_NE(nullptr, rpcBinder);
1426
1427 ASSERT_EQ(OK, rpcBinder->pingBinder());
1428
1429 ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor())
1430 << "getInterfaceDescriptor should not crash system_server";
1431 ASSERT_EQ(OK, rpcBinder->pingBinder());
1432}
1433
Yifan Hong702115c2021-06-24 15:39:18 -07001434INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcSimple, ::testing::ValuesIn(RpcSecurityValues()),
1435 BinderRpcSimple::PrintTestParam);
1436
Yifan Hong1deca4b2021-09-10 16:16:44 -07001437class RpcTransportTest
Yifan Hong9734cfc2021-09-13 16:14:09 -07001438 : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity, RpcCertificateFormat>> {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001439public:
1440 using ConnectToServer = std::function<base::unique_fd()>;
1441 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
1442 auto [socketType, rpcSecurity, certificateFormat] = info.param;
1443 return PrintToString(socketType) + "_" + newFactory(rpcSecurity)->toCString() + "_" +
1444 PrintToString(certificateFormat);
1445 }
1446 void TearDown() override {
Yifan Honge07d2732021-09-13 21:59:14 -07001447 for (auto& server : mServers) server->shutdownAndWait();
Yifan Hong1deca4b2021-09-10 16:16:44 -07001448 }
1449
1450 // A server that handles client socket connections.
1451 class Server {
1452 public:
1453 explicit Server() {}
1454 Server(Server&&) = default;
Yifan Honge07d2732021-09-13 21:59:14 -07001455 ~Server() { shutdownAndWait(); }
Yifan Hong1deca4b2021-09-10 16:16:44 -07001456 [[nodiscard]] AssertionResult setUp() {
1457 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1458 auto rpcServer = RpcServer::make(newFactory(rpcSecurity));
1459 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1460 switch (socketType) {
1461 case SocketType::PRECONNECTED: {
1462 return AssertionFailure() << "Not supported by this test";
1463 } break;
1464 case SocketType::UNIX: {
1465 auto addr = allocateSocketAddress();
1466 auto status = rpcServer->setupUnixDomainServer(addr.c_str());
1467 if (status != OK) {
1468 return AssertionFailure()
1469 << "setupUnixDomainServer: " << statusToString(status);
1470 }
1471 mConnectToServer = [addr] {
1472 return connectTo(UnixSocketAddress(addr.c_str()));
1473 };
1474 } break;
1475 case SocketType::VSOCK: {
1476 auto port = allocateVsockPort();
1477 auto status = rpcServer->setupVsockServer(port);
1478 if (status != OK) {
1479 return AssertionFailure() << "setupVsockServer: " << statusToString(status);
1480 }
1481 mConnectToServer = [port] {
1482 return connectTo(VsockSocketAddress(VMADDR_CID_LOCAL, port));
1483 };
1484 } break;
1485 case SocketType::INET: {
1486 unsigned int port;
1487 auto status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port);
1488 if (status != OK) {
1489 return AssertionFailure() << "setupInetServer: " << statusToString(status);
1490 }
1491 mConnectToServer = [port] {
1492 const char* addr = kLocalInetAddress;
1493 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
1494 if (aiStart == nullptr) return base::unique_fd{};
1495 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
1496 auto fd = connectTo(
1497 InetSocketAddress(ai->ai_addr, ai->ai_addrlen, addr, port));
1498 if (fd.ok()) return fd;
1499 }
1500 ALOGE("None of the socket address resolved for %s:%u can be connected",
1501 addr, port);
1502 return base::unique_fd{};
1503 };
1504 }
1505 }
1506 mFd = rpcServer->releaseServer();
1507 if (!mFd.ok()) return AssertionFailure() << "releaseServer returns invalid fd";
1508 mCtx = newFactory(rpcSecurity, mCertVerifier)->newServerCtx();
1509 if (mCtx == nullptr) return AssertionFailure() << "newServerCtx";
1510 mSetup = true;
1511 return AssertionSuccess();
1512 }
1513 RpcTransportCtx* getCtx() const { return mCtx.get(); }
1514 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
1515 return mCertVerifier;
1516 }
1517 ConnectToServer getConnectToServerFn() { return mConnectToServer; }
1518 void start() {
1519 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
1520 mThread = std::make_unique<std::thread>(&Server::run, this);
1521 }
1522 void run() {
1523 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
1524
1525 std::vector<std::thread> threads;
1526 while (OK == mFdTrigger->triggerablePoll(mFd, POLLIN)) {
1527 base::unique_fd acceptedFd(
1528 TEMP_FAILURE_RETRY(accept4(mFd.get(), nullptr, nullptr /*length*/,
1529 SOCK_CLOEXEC | SOCK_NONBLOCK)));
1530 threads.emplace_back(&Server::handleOne, this, std::move(acceptedFd));
1531 }
1532
1533 for (auto& thread : threads) thread.join();
1534 }
1535 void handleOne(android::base::unique_fd acceptedFd) {
1536 ASSERT_TRUE(acceptedFd.ok());
1537 auto serverTransport = mCtx->newTransport(std::move(acceptedFd), mFdTrigger.get());
1538 if (serverTransport == nullptr) return; // handshake failed
1539 std::string message(kMessage);
1540 ASSERT_EQ(OK,
1541 serverTransport->interruptableWriteFully(mFdTrigger.get(), message.data(),
1542 message.size()));
1543 }
Yifan Honge07d2732021-09-13 21:59:14 -07001544 void shutdownAndWait() {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001545 mFdTrigger->trigger();
1546 if (mThread != nullptr) {
1547 mThread->join();
1548 mThread = nullptr;
1549 }
1550 }
1551
1552 private:
1553 std::unique_ptr<std::thread> mThread;
1554 ConnectToServer mConnectToServer;
1555 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
1556 base::unique_fd mFd;
1557 std::unique_ptr<RpcTransportCtx> mCtx;
1558 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
1559 std::make_shared<RpcCertificateVerifierSimple>();
1560 bool mSetup = false;
1561 };
1562
1563 class Client {
1564 public:
1565 explicit Client(ConnectToServer connectToServer) : mConnectToServer(connectToServer) {}
1566 Client(Client&&) = default;
1567 [[nodiscard]] AssertionResult setUp() {
1568 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1569 mFd = mConnectToServer();
1570 if (!mFd.ok()) return AssertionFailure() << "Cannot connect to server";
1571 mFdTrigger = FdTrigger::make();
1572 mCtx = newFactory(rpcSecurity, mCertVerifier)->newClientCtx();
1573 if (mCtx == nullptr) return AssertionFailure() << "newClientCtx";
1574 return AssertionSuccess();
1575 }
1576 RpcTransportCtx* getCtx() const { return mCtx.get(); }
1577 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
1578 return mCertVerifier;
1579 }
1580 void run(bool handshakeOk = true, bool readOk = true) {
1581 auto clientTransport = mCtx->newTransport(std::move(mFd), mFdTrigger.get());
1582 if (clientTransport == nullptr) {
1583 ASSERT_FALSE(handshakeOk) << "newTransport returns nullptr, but it shouldn't";
1584 return;
1585 }
1586 ASSERT_TRUE(handshakeOk) << "newTransport does not return nullptr, but it should";
1587 std::string expectedMessage(kMessage);
1588 std::string readMessage(expectedMessage.size(), '\0');
1589 status_t readStatus =
1590 clientTransport->interruptableReadFully(mFdTrigger.get(), readMessage.data(),
1591 readMessage.size());
1592 if (readOk) {
1593 ASSERT_EQ(OK, readStatus);
1594 ASSERT_EQ(readMessage, expectedMessage);
1595 } else {
1596 ASSERT_NE(OK, readStatus);
1597 }
1598 }
1599
1600 private:
1601 ConnectToServer mConnectToServer;
1602 base::unique_fd mFd;
1603 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
1604 std::unique_ptr<RpcTransportCtx> mCtx;
1605 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
1606 std::make_shared<RpcCertificateVerifierSimple>();
1607 };
1608
1609 // Make A trust B.
1610 template <typename A, typename B>
1611 status_t trust(A* a, B* b) {
1612 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1613 if (rpcSecurity != RpcSecurity::TLS) return OK;
1614 auto bCert = b->getCtx()->getCertificate(certificateFormat);
1615 return a->getCertVerifier()->addTrustedPeerCertificate(certificateFormat, bCert);
1616 }
1617
1618 static constexpr const char* kMessage = "hello";
1619 std::vector<std::unique_ptr<Server>> mServers;
1620};
1621
1622TEST_P(RpcTransportTest, GoodCertificate) {
1623 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1624 ASSERT_TRUE(server->setUp());
1625
1626 Client client(server->getConnectToServerFn());
1627 ASSERT_TRUE(client.setUp());
1628
1629 ASSERT_EQ(OK, trust(&client, server));
1630 ASSERT_EQ(OK, trust(server, &client));
1631
1632 server->start();
1633 client.run();
1634}
1635
1636TEST_P(RpcTransportTest, MultipleClients) {
1637 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1638 ASSERT_TRUE(server->setUp());
1639
1640 std::vector<Client> clients;
1641 for (int i = 0; i < 2; i++) {
1642 auto& client = clients.emplace_back(server->getConnectToServerFn());
1643 ASSERT_TRUE(client.setUp());
1644 ASSERT_EQ(OK, trust(&client, server));
1645 ASSERT_EQ(OK, trust(server, &client));
1646 }
1647
1648 server->start();
1649 for (auto& client : clients) client.run();
1650}
1651
1652TEST_P(RpcTransportTest, UntrustedServer) {
1653 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1654
1655 auto untrustedServer = mServers.emplace_back(std::make_unique<Server>()).get();
1656 ASSERT_TRUE(untrustedServer->setUp());
1657
1658 Client client(untrustedServer->getConnectToServerFn());
1659 ASSERT_TRUE(client.setUp());
1660
1661 ASSERT_EQ(OK, trust(untrustedServer, &client));
1662
1663 untrustedServer->start();
1664
1665 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
1666 // the client can't verify the server's identity.
1667 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
1668 client.run(handshakeOk);
1669}
1670TEST_P(RpcTransportTest, MaliciousServer) {
1671 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1672 auto validServer = mServers.emplace_back(std::make_unique<Server>()).get();
1673 ASSERT_TRUE(validServer->setUp());
1674
1675 auto maliciousServer = mServers.emplace_back(std::make_unique<Server>()).get();
1676 ASSERT_TRUE(maliciousServer->setUp());
1677
1678 Client client(maliciousServer->getConnectToServerFn());
1679 ASSERT_TRUE(client.setUp());
1680
1681 ASSERT_EQ(OK, trust(&client, validServer));
1682 ASSERT_EQ(OK, trust(validServer, &client));
1683 ASSERT_EQ(OK, trust(maliciousServer, &client));
1684
1685 maliciousServer->start();
1686
1687 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
1688 // the client can't verify the server's identity.
1689 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
1690 client.run(handshakeOk);
1691}
1692
1693TEST_P(RpcTransportTest, UntrustedClient) {
1694 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1695 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1696 ASSERT_TRUE(server->setUp());
1697
1698 Client client(server->getConnectToServerFn());
1699 ASSERT_TRUE(client.setUp());
1700
1701 ASSERT_EQ(OK, trust(&client, server));
1702
1703 server->start();
1704
1705 // For TLS, Client should be able to verify server's identity, so client should see
1706 // do_handshake() successfully executed. However, server shouldn't be able to verify client's
1707 // identity and should drop the connection, so client shouldn't be able to read anything.
1708 bool readOk = rpcSecurity != RpcSecurity::TLS;
1709 client.run(true, readOk);
1710}
1711
1712TEST_P(RpcTransportTest, MaliciousClient) {
1713 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1714 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1715 ASSERT_TRUE(server->setUp());
1716
1717 Client validClient(server->getConnectToServerFn());
1718 ASSERT_TRUE(validClient.setUp());
1719 Client maliciousClient(server->getConnectToServerFn());
1720 ASSERT_TRUE(maliciousClient.setUp());
1721
1722 ASSERT_EQ(OK, trust(&validClient, server));
1723 ASSERT_EQ(OK, trust(&maliciousClient, server));
1724
1725 server->start();
1726
1727 // See UntrustedClient.
1728 bool readOk = rpcSecurity != RpcSecurity::TLS;
1729 maliciousClient.run(true, readOk);
1730}
1731
Yifan Hong9734cfc2021-09-13 16:14:09 -07001732std::vector<RpcCertificateFormat> testRpcCertificateFormats() {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001733 return {
Yifan Hong9734cfc2021-09-13 16:14:09 -07001734 RpcCertificateFormat::PEM,
1735 RpcCertificateFormat::DER,
Yifan Hong1deca4b2021-09-10 16:16:44 -07001736 };
1737}
1738
1739INSTANTIATE_TEST_CASE_P(BinderRpc, RpcTransportTest,
1740 ::testing::Combine(::testing::ValuesIn(testSocketTypes(false)),
1741 ::testing::ValuesIn(RpcSecurityValues()),
Yifan Hong9734cfc2021-09-13 16:14:09 -07001742 ::testing::ValuesIn(testRpcCertificateFormats())),
Yifan Hong1deca4b2021-09-10 16:16:44 -07001743 RpcTransportTest::PrintParamInfo);
1744
Steven Morelandc1635952021-04-01 16:20:47 +00001745} // namespace android
1746
1747int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001748 ::testing::InitGoogleTest(&argc, argv);
1749 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
1750 return RUN_ALL_TESTS();
1751}