blob: a9273d6b821f58b25fca39b7e525d41ba94a729e [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 });
Steven Moreland4198a122021-08-03 17:37:58 -0700617 break;
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000618 case SocketType::UNIX:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700619 status = session->setupUnixDomainClient(addr.c_str());
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000620 break;
621 case SocketType::VSOCK:
Steven Moreland2372f9d2021-08-05 15:42:01 -0700622 status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort);
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000623 break;
624 case SocketType::INET:
Yifan Hong1deca4b2021-09-10 16:16:44 -0700625 status = session->setupInetClient("127.0.0.1", serverInfo.port);
Steven Moreland76d2c1f2021-05-05 20:28:58 +0000626 break;
627 default:
628 LOG_ALWAYS_FATAL("Unknown socket type");
Steven Morelandc1635952021-04-01 16:20:47 +0000629 }
Steven Moreland8a1a47d2021-09-14 10:54:04 -0700630 CHECK_EQ(status, OK) << "Could not connect: " << statusToString(status);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000631 ret.sessions.push_back({session, session->getRootObject()});
Steven Morelandc1635952021-04-01 16:20:47 +0000632 }
Steven Morelandc1635952021-04-01 16:20:47 +0000633 return ret;
634 }
635
Steven Moreland4313d7e2021-07-15 23:41:22 +0000636 BinderRpcTestProcessSession createRpcTestSocketServerProcess(const Options& options) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000637 BinderRpcTestProcessSession ret{
Steven Moreland4313d7e2021-07-15 23:41:22 +0000638 .proc = createRpcTestSocketServerProcess(options,
Steven Moreland611d15f2021-05-01 01:28:27 +0000639 [&](const sp<RpcServer>& server) {
Steven Morelandc1635952021-04-01 16:20:47 +0000640 sp<MyBinderRpcTest> service =
641 new MyBinderRpcTest;
642 server->setRootObject(service);
Steven Moreland611d15f2021-05-01 01:28:27 +0000643 service->server = server;
Steven Morelandc1635952021-04-01 16:20:47 +0000644 }),
645 };
646
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000647 ret.rootBinder = ret.proc.sessions.at(0).root;
Steven Morelandc1635952021-04-01 16:20:47 +0000648 ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder);
649
650 return ret;
651 }
652};
653
Steven Morelandc1635952021-04-01 16:20:47 +0000654TEST_P(BinderRpc, Ping) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000655 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000656 ASSERT_NE(proc.rootBinder, nullptr);
657 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
658}
659
Steven Moreland4cf688f2021-03-31 01:48:58 +0000660TEST_P(BinderRpc, GetInterfaceDescriptor) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000661 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland4cf688f2021-03-31 01:48:58 +0000662 ASSERT_NE(proc.rootBinder, nullptr);
663 EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor());
664}
665
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000666TEST_P(BinderRpc, MultipleSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000667 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 5});
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000668 for (auto session : proc.proc.sessions) {
669 ASSERT_NE(nullptr, session.root);
670 EXPECT_EQ(OK, session.root->pingBinder());
Steven Moreland736664b2021-05-01 04:27:25 +0000671 }
672}
673
Steven Morelandc1635952021-04-01 16:20:47 +0000674TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000675 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000676 Parcel data;
677 Parcel reply;
678 EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0));
679}
680
Steven Moreland67753c32021-04-02 18:45:19 +0000681TEST_P(BinderRpc, AppendSeparateFormats) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000682 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland67753c32021-04-02 18:45:19 +0000683
684 Parcel p1;
685 p1.markForBinder(proc.rootBinder);
686 p1.writeInt32(3);
687
688 Parcel p2;
689
690 EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize()));
691 EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize()));
692}
693
Steven Morelandc1635952021-04-01 16:20:47 +0000694TEST_P(BinderRpc, UnknownTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000695 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000696 Parcel data;
697 data.markForBinder(proc.rootBinder);
698 Parcel reply;
699 EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0));
700}
701
Steven Morelandc1635952021-04-01 16:20:47 +0000702TEST_P(BinderRpc, SendSomethingOneway) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000703 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000704 EXPECT_OK(proc.rootIface->sendString("asdf"));
705}
706
Steven Morelandc1635952021-04-01 16:20:47 +0000707TEST_P(BinderRpc, SendAndGetResultBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000708 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000709 std::string doubled;
710 EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled));
711 EXPECT_EQ("cool cool ", doubled);
712}
713
Steven Morelandc1635952021-04-01 16:20:47 +0000714TEST_P(BinderRpc, SendAndGetResultBackBig) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000715 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000716 std::string single = std::string(1024, 'a');
717 std::string doubled;
718 EXPECT_OK(proc.rootIface->doubleString(single, &doubled));
719 EXPECT_EQ(single + single, doubled);
720}
721
Steven Morelandc1635952021-04-01 16:20:47 +0000722TEST_P(BinderRpc, CallMeBack) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000723 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000724
725 int32_t pingResult;
726 EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult));
727 EXPECT_EQ(OK, pingResult);
728
729 EXPECT_EQ(0, MyBinderRpcSession::gNum);
730}
731
Steven Morelandc1635952021-04-01 16:20:47 +0000732TEST_P(BinderRpc, RepeatBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000733 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000734
735 sp<IBinder> inBinder = new MyBinderRpcSession("foo");
736 sp<IBinder> outBinder;
737 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
738 EXPECT_EQ(inBinder, outBinder);
739
740 wp<IBinder> weak = inBinder;
741 inBinder = nullptr;
742 outBinder = nullptr;
743
744 // Force reading a reply, to process any pending dec refs from the other
745 // process (the other process will process dec refs there before processing
746 // the ping here).
747 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
748
749 EXPECT_EQ(nullptr, weak.promote());
750
751 EXPECT_EQ(0, MyBinderRpcSession::gNum);
752}
753
Steven Morelandc1635952021-04-01 16:20:47 +0000754TEST_P(BinderRpc, RepeatTheirBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000755 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000756
757 sp<IBinderRpcSession> session;
758 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
759
760 sp<IBinder> inBinder = IInterface::asBinder(session);
761 sp<IBinder> outBinder;
762 EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder));
763 EXPECT_EQ(inBinder, outBinder);
764
765 wp<IBinder> weak = inBinder;
766 session = nullptr;
767 inBinder = nullptr;
768 outBinder = nullptr;
769
770 // Force reading a reply, to process any pending dec refs from the other
771 // process (the other process will process dec refs there before processing
772 // the ping here).
773 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
774
775 EXPECT_EQ(nullptr, weak.promote());
776}
777
Steven Morelandc1635952021-04-01 16:20:47 +0000778TEST_P(BinderRpc, RepeatBinderNull) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000779 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000780
781 sp<IBinder> outBinder;
782 EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder));
783 EXPECT_EQ(nullptr, outBinder);
784}
785
Steven Morelandc1635952021-04-01 16:20:47 +0000786TEST_P(BinderRpc, HoldBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000787 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000788
789 IBinder* ptr = nullptr;
790 {
791 sp<IBinder> binder = new BBinder();
792 ptr = binder.get();
793 EXPECT_OK(proc.rootIface->holdBinder(binder));
794 }
795
796 sp<IBinder> held;
797 EXPECT_OK(proc.rootIface->getHeldBinder(&held));
798
799 EXPECT_EQ(held.get(), ptr);
800
801 // stop holding binder, because we test to make sure references are cleaned
802 // up
803 EXPECT_OK(proc.rootIface->holdBinder(nullptr));
804 // and flush ref counts
805 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
806}
807
808// START TESTS FOR LIMITATIONS OF SOCKET BINDER
809// These are behavioral differences form regular binder, where certain usecases
810// aren't supported.
811
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000812TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000813 auto proc1 = createRpcTestSocketServerProcess({});
814 auto proc2 = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000815
816 sp<IBinder> outBinder;
817 EXPECT_EQ(INVALID_OPERATION,
818 proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError());
819}
820
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000821TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000822 auto proc = createRpcTestSocketServerProcess({.numThreads = 1, .numSessions = 2});
Steven Moreland736664b2021-05-01 04:27:25 +0000823
824 sp<IBinder> outBinder;
825 EXPECT_EQ(INVALID_OPERATION,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000826 proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder)
Steven Moreland736664b2021-05-01 04:27:25 +0000827 .transactionError());
828}
829
Steven Morelandc1635952021-04-01 16:20:47 +0000830TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000831 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000832
833 sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager());
834 sp<IBinder> outBinder;
835 EXPECT_EQ(INVALID_OPERATION,
836 proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError());
837}
838
Steven Morelandc1635952021-04-01 16:20:47 +0000839TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000840 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000841
842 // for historical reasons, IServiceManager interface only returns the
843 // exception code
844 EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED,
845 defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder));
846}
847
848// END TESTS FOR LIMITATIONS OF SOCKET BINDER
849
Steven Morelandc1635952021-04-01 16:20:47 +0000850TEST_P(BinderRpc, RepeatRootObject) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000851 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000852
853 sp<IBinder> outBinder;
854 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder));
855 EXPECT_EQ(proc.rootBinder, outBinder);
856}
857
Steven Morelandc1635952021-04-01 16:20:47 +0000858TEST_P(BinderRpc, NestedTransactions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000859 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000860
861 auto nastyNester = sp<MyBinderRpcTest>::make();
862 EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10));
863
864 wp<IBinder> weak = nastyNester;
865 nastyNester = nullptr;
866 EXPECT_EQ(nullptr, weak.promote());
867}
868
Steven Morelandc1635952021-04-01 16:20:47 +0000869TEST_P(BinderRpc, SameBinderEquality) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000870 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000871
872 sp<IBinder> a;
873 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
874
875 sp<IBinder> b;
876 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
877
878 EXPECT_EQ(a, b);
879}
880
Steven Morelandc1635952021-04-01 16:20:47 +0000881TEST_P(BinderRpc, SameBinderEqualityWeak) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000882 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000883
884 sp<IBinder> a;
885 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a));
886 wp<IBinder> weak = a;
887 a = nullptr;
888
889 sp<IBinder> b;
890 EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b));
891
892 // this is the wrong behavior, since BpBinder
893 // doesn't implement onIncStrongAttempted
894 // but make sure there is no crash
895 EXPECT_EQ(nullptr, weak.promote());
896
897 GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder.";
898
899 // In order to fix this:
900 // - need to have incStrongAttempted reflected across IPC boundary (wait for
901 // response to promote - round trip...)
902 // - sendOnLastWeakRef, to delete entries out of RpcState table
903 EXPECT_EQ(b, weak.promote());
904}
905
906#define expectSessions(expected, iface) \
907 do { \
908 int session; \
909 EXPECT_OK((iface)->getNumOpenSessions(&session)); \
910 EXPECT_EQ(expected, session); \
911 } while (false)
912
Steven Morelandc1635952021-04-01 16:20:47 +0000913TEST_P(BinderRpc, SingleSession) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000914 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000915
916 sp<IBinderRpcSession> session;
917 EXPECT_OK(proc.rootIface->openSession("aoeu", &session));
918 std::string out;
919 EXPECT_OK(session->getName(&out));
920 EXPECT_EQ("aoeu", out);
921
922 expectSessions(1, proc.rootIface);
923 session = nullptr;
924 expectSessions(0, proc.rootIface);
925}
926
Steven Morelandc1635952021-04-01 16:20:47 +0000927TEST_P(BinderRpc, ManySessions) {
Steven Moreland4313d7e2021-07-15 23:41:22 +0000928 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +0000929
930 std::vector<sp<IBinderRpcSession>> sessions;
931
932 for (size_t i = 0; i < 15; i++) {
933 expectSessions(i, proc.rootIface);
934 sp<IBinderRpcSession> session;
935 EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session));
936 sessions.push_back(session);
937 }
938 expectSessions(sessions.size(), proc.rootIface);
939 for (size_t i = 0; i < sessions.size(); i++) {
940 std::string out;
941 EXPECT_OK(sessions.at(i)->getName(&out));
942 EXPECT_EQ(std::to_string(i), out);
943 }
944 expectSessions(sessions.size(), proc.rootIface);
945
946 while (!sessions.empty()) {
947 sessions.pop_back();
948 expectSessions(sessions.size(), proc.rootIface);
949 }
950 expectSessions(0, proc.rootIface);
951}
952
953size_t epochMillis() {
954 using std::chrono::duration_cast;
955 using std::chrono::milliseconds;
956 using std::chrono::seconds;
957 using std::chrono::system_clock;
958 return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
959}
960
Steven Morelandc1635952021-04-01 16:20:47 +0000961TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000962 constexpr size_t kNumThreads = 10;
963
Steven Moreland4313d7e2021-07-15 23:41:22 +0000964 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000965
966 EXPECT_OK(proc.rootIface->lock());
967
968 // block all but one thread taking locks
969 std::vector<std::thread> ts;
970 for (size_t i = 0; i < kNumThreads - 1; i++) {
971 ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); }));
972 }
973
974 usleep(100000); // give chance for calls on other threads
975
976 // other calls still work
977 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
978
979 constexpr size_t blockTimeMs = 500;
980 size_t epochMsBefore = epochMillis();
981 // after this, we should never see a response within this time
982 EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs));
983
984 // this call should be blocked for blockTimeMs
985 EXPECT_EQ(OK, proc.rootBinder->pingBinder());
986
987 size_t epochMsAfter = epochMillis();
988 EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore;
989
990 for (auto& t : ts) t.join();
991}
992
Steven Morelandc1635952021-04-01 16:20:47 +0000993TEST_P(BinderRpc, ThreadPoolOverSaturated) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000994 constexpr size_t kNumThreads = 10;
995 constexpr size_t kNumCalls = kNumThreads + 3;
996 constexpr size_t kSleepMs = 500;
997
Steven Moreland4313d7e2021-07-15 23:41:22 +0000998 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +0000999
1000 size_t epochMsBefore = epochMillis();
1001
1002 std::vector<std::thread> ts;
1003 for (size_t i = 0; i < kNumCalls; i++) {
1004 ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); }));
1005 }
1006
1007 for (auto& t : ts) t.join();
1008
1009 size_t epochMsAfter = epochMillis();
1010
1011 EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs);
1012
1013 // Potential flake, but make sure calls are handled in parallel.
1014 EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs);
1015}
1016
Steven Morelandc1635952021-04-01 16:20:47 +00001017TEST_P(BinderRpc, ThreadingStressTest) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001018 constexpr size_t kNumClientThreads = 10;
1019 constexpr size_t kNumServerThreads = 10;
1020 constexpr size_t kNumCalls = 100;
1021
Steven Moreland4313d7e2021-07-15 23:41:22 +00001022 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +00001023
1024 std::vector<std::thread> threads;
1025 for (size_t i = 0; i < kNumClientThreads; i++) {
1026 threads.push_back(std::thread([&] {
1027 for (size_t j = 0; j < kNumCalls; j++) {
1028 sp<IBinder> out;
Steven Morelandc6046982021-04-20 00:49:42 +00001029 EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out));
Steven Moreland5553ac42020-11-11 02:14:45 +00001030 EXPECT_EQ(proc.rootBinder, out);
1031 }
1032 }));
1033 }
1034
1035 for (auto& t : threads) t.join();
1036}
1037
Steven Morelandc6046982021-04-20 00:49:42 +00001038TEST_P(BinderRpc, OnewayStressTest) {
1039 constexpr size_t kNumClientThreads = 10;
1040 constexpr size_t kNumServerThreads = 10;
Steven Moreland52eee942021-06-03 00:59:28 +00001041 constexpr size_t kNumCalls = 500;
Steven Morelandc6046982021-04-20 00:49:42 +00001042
Steven Moreland4313d7e2021-07-15 23:41:22 +00001043 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumServerThreads});
Steven Morelandc6046982021-04-20 00:49:42 +00001044
1045 std::vector<std::thread> threads;
1046 for (size_t i = 0; i < kNumClientThreads; i++) {
1047 threads.push_back(std::thread([&] {
1048 for (size_t j = 0; j < kNumCalls; j++) {
1049 EXPECT_OK(proc.rootIface->sendString("a"));
1050 }
1051
1052 // check threads are not stuck
1053 EXPECT_OK(proc.rootIface->sleepMs(250));
1054 }));
1055 }
1056
1057 for (auto& t : threads) t.join();
1058}
1059
Steven Morelandc1635952021-04-01 16:20:47 +00001060TEST_P(BinderRpc, OnewayCallDoesNotWait) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001061 constexpr size_t kReallyLongTimeMs = 100;
1062 constexpr size_t kSleepMs = kReallyLongTimeMs * 5;
1063
Steven Moreland4313d7e2021-07-15 23:41:22 +00001064 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +00001065
1066 size_t epochMsBefore = epochMillis();
1067
1068 EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs));
1069
1070 size_t epochMsAfter = epochMillis();
1071 EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs);
1072}
1073
Steven Morelandc1635952021-04-01 16:20:47 +00001074TEST_P(BinderRpc, OnewayCallQueueing) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001075 constexpr size_t kNumSleeps = 10;
1076 constexpr size_t kNumExtraServerThreads = 4;
1077 constexpr size_t kSleepMs = 50;
1078
1079 // make sure calls to the same object happen on the same thread
Steven Moreland4313d7e2021-07-15 23:41:22 +00001080 auto proc = createRpcTestSocketServerProcess({.numThreads = 1 + kNumExtraServerThreads});
Steven Moreland5553ac42020-11-11 02:14:45 +00001081
1082 EXPECT_OK(proc.rootIface->lock());
1083
1084 for (size_t i = 0; i < kNumSleeps; i++) {
1085 // these should be processed serially
1086 proc.rootIface->sleepMsAsync(kSleepMs);
1087 }
1088 // should also be processesed serially
1089 EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs));
1090
1091 size_t epochMsBefore = epochMillis();
1092 EXPECT_OK(proc.rootIface->lockUnlock());
1093 size_t epochMsAfter = epochMillis();
1094
1095 EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps);
Steven Morelandf5174272021-05-25 00:39:28 +00001096
1097 // pending oneway transactions hold ref, make sure we read data on all
1098 // sockets
1099 std::vector<std::thread> threads;
1100 for (size_t i = 0; i < 1 + kNumExtraServerThreads; i++) {
1101 threads.push_back(std::thread([&] { EXPECT_OK(proc.rootIface->sleepMs(250)); }));
1102 }
1103 for (auto& t : threads) t.join();
Steven Moreland5553ac42020-11-11 02:14:45 +00001104}
1105
Steven Morelandd45be622021-06-04 02:19:37 +00001106TEST_P(BinderRpc, OnewayCallExhaustion) {
1107 constexpr size_t kNumClients = 2;
1108 constexpr size_t kTooLongMs = 1000;
1109
Steven Moreland4313d7e2021-07-15 23:41:22 +00001110 auto proc = createRpcTestSocketServerProcess({.numThreads = kNumClients, .numSessions = 2});
Steven Morelandd45be622021-06-04 02:19:37 +00001111
1112 // Build up oneway calls on the second session to make sure it terminates
1113 // and shuts down. The first session should be unaffected (proc destructor
1114 // checks the first session).
1115 auto iface = interface_cast<IBinderRpcTest>(proc.proc.sessions.at(1).root);
1116
1117 std::vector<std::thread> threads;
1118 for (size_t i = 0; i < kNumClients; i++) {
1119 // one of these threads will get stuck queueing a transaction once the
1120 // socket fills up, the other will be able to fill up transactions on
1121 // this object
1122 threads.push_back(std::thread([&] {
1123 while (iface->sleepMsAsync(kTooLongMs).isOk()) {
1124 }
1125 }));
1126 }
1127 for (auto& t : threads) t.join();
1128
1129 Status status = iface->sleepMsAsync(kTooLongMs);
1130 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1131
Steven Moreland798e0d12021-07-14 23:19:25 +00001132 // now that it has died, wait for the remote session to shutdown
1133 std::vector<int32_t> remoteCounts;
1134 do {
1135 EXPECT_OK(proc.rootIface->countBinders(&remoteCounts));
1136 } while (remoteCounts.size() == kNumClients);
1137
Steven Morelandd45be622021-06-04 02:19:37 +00001138 // the second session should be shutdown in the other process by the time we
1139 // are able to join above (it'll only be hung up once it finishes processing
1140 // any pending commands). We need to erase this session from the record
1141 // here, so that the destructor for our session won't check that this
1142 // session is valid, but we still want it to test the other session.
1143 proc.proc.sessions.erase(proc.proc.sessions.begin() + 1);
1144}
1145
Steven Moreland659416d2021-05-11 00:47:50 +00001146TEST_P(BinderRpc, Callbacks) {
1147 const static std::string kTestString = "good afternoon!";
1148
Steven Morelandc7d40132021-06-10 03:42:11 +00001149 for (bool callIsOneway : {true, false}) {
1150 for (bool callbackIsOneway : {true, false}) {
1151 for (bool delayed : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001152 auto proc = createRpcTestSocketServerProcess(
1153 {.numThreads = 1, .numSessions = 1, .numIncomingConnections = 1});
Steven Morelandc7d40132021-06-10 03:42:11 +00001154 auto cb = sp<MyBinderRpcCallback>::make();
Steven Moreland659416d2021-05-11 00:47:50 +00001155
Steven Morelandc7d40132021-06-10 03:42:11 +00001156 if (callIsOneway) {
1157 EXPECT_OK(proc.rootIface->doCallbackAsync(cb, callbackIsOneway, delayed,
1158 kTestString));
1159 } else {
1160 EXPECT_OK(
1161 proc.rootIface->doCallback(cb, callbackIsOneway, delayed, kTestString));
1162 }
Steven Moreland659416d2021-05-11 00:47:50 +00001163
Steven Morelandc7d40132021-06-10 03:42:11 +00001164 using std::literals::chrono_literals::operator""s;
1165 std::unique_lock<std::mutex> _l(cb->mMutex);
1166 cb->mCv.wait_for(_l, 1s, [&] { return !cb->mValues.empty(); });
Steven Moreland659416d2021-05-11 00:47:50 +00001167
Steven Morelandc7d40132021-06-10 03:42:11 +00001168 EXPECT_EQ(cb->mValues.size(), 1)
1169 << "callIsOneway: " << callIsOneway
1170 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
1171 if (cb->mValues.empty()) continue;
1172 EXPECT_EQ(cb->mValues.at(0), kTestString)
1173 << "callIsOneway: " << callIsOneway
1174 << " callbackIsOneway: " << callbackIsOneway << " delayed: " << delayed;
Steven Moreland659416d2021-05-11 00:47:50 +00001175
Steven Morelandc7d40132021-06-10 03:42:11 +00001176 // since we are severing the connection, we need to go ahead and
1177 // tell the server to shutdown and exit so that waitpid won't hang
Steven Moreland798e0d12021-07-14 23:19:25 +00001178 if (auto status = proc.rootIface->scheduleShutdown(); !status.isOk()) {
1179 EXPECT_EQ(DEAD_OBJECT, status.transactionError()) << status;
1180 }
Steven Moreland659416d2021-05-11 00:47:50 +00001181
Steven Moreland1b304292021-07-15 22:59:34 +00001182 // since this session has an incoming connection w/ a threadpool, we
Steven Morelandc7d40132021-06-10 03:42:11 +00001183 // need to manually shut it down
1184 EXPECT_TRUE(proc.proc.sessions.at(0).session->shutdownAndWait(true));
Steven Moreland659416d2021-05-11 00:47:50 +00001185
Steven Morelandc7d40132021-06-10 03:42:11 +00001186 proc.expectAlreadyShutdown = true;
1187 }
Steven Moreland659416d2021-05-11 00:47:50 +00001188 }
1189 }
1190}
1191
Steven Moreland195edb82021-06-08 02:44:39 +00001192TEST_P(BinderRpc, OnewayCallbackWithNoThread) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001193 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland195edb82021-06-08 02:44:39 +00001194 auto cb = sp<MyBinderRpcCallback>::make();
1195
1196 Status status = proc.rootIface->doCallback(cb, true /*oneway*/, false /*delayed*/, "anything");
1197 EXPECT_EQ(WOULD_BLOCK, status.transactionError());
1198}
1199
Steven Morelandc1635952021-04-01 16:20:47 +00001200TEST_P(BinderRpc, Die) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001201 for (bool doDeathCleanup : {true, false}) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001202 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland5553ac42020-11-11 02:14:45 +00001203
1204 // make sure there is some state during crash
1205 // 1. we hold their binder
1206 sp<IBinderRpcSession> session;
1207 EXPECT_OK(proc.rootIface->openSession("happy", &session));
1208 // 2. they hold our binder
1209 sp<IBinder> binder = new BBinder();
1210 EXPECT_OK(proc.rootIface->holdBinder(binder));
1211
1212 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError())
1213 << "Do death cleanup: " << doDeathCleanup;
1214
Steven Morelandaf4ca712021-05-24 23:22:08 +00001215 proc.expectAlreadyShutdown = true;
Steven Moreland5553ac42020-11-11 02:14:45 +00001216 }
1217}
1218
Steven Morelandd7302072021-05-15 01:32:04 +00001219TEST_P(BinderRpc, UseKernelBinderCallingId) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001220 auto proc = createRpcTestSocketServerProcess({});
Steven Morelandd7302072021-05-15 01:32:04 +00001221
1222 // we can't allocate IPCThreadState so actually the first time should
1223 // succeed :(
1224 EXPECT_OK(proc.rootIface->useKernelBinderCallingId());
1225
1226 // second time! we catch the error :)
1227 EXPECT_EQ(DEAD_OBJECT, proc.rootIface->useKernelBinderCallingId().transactionError());
1228
Steven Morelandaf4ca712021-05-24 23:22:08 +00001229 proc.expectAlreadyShutdown = true;
Steven Morelandd7302072021-05-15 01:32:04 +00001230}
1231
Steven Moreland37aff182021-03-26 02:04:16 +00001232TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001233 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001234
1235 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1236 ASSERT_NE(binder, nullptr);
1237
1238 ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
1239}
1240
1241TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001242 auto proc = createRpcTestSocketServerProcess({});
Steven Moreland37aff182021-03-26 02:04:16 +00001243
1244 ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder));
1245 ASSERT_NE(binder, nullptr);
1246
1247 auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder);
1248 ASSERT_NE(ndkBinder, nullptr);
1249
1250 std::string out;
1251 ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out);
1252 ASSERT_TRUE(status.isOk()) << status.getDescription();
1253 ASSERT_EQ("aoeuaoeu", out);
1254}
1255
Steven Moreland5553ac42020-11-11 02:14:45 +00001256ssize_t countFds() {
1257 DIR* dir = opendir("/proc/self/fd/");
1258 if (dir == nullptr) return -1;
1259 ssize_t ret = 0;
1260 dirent* ent;
1261 while ((ent = readdir(dir)) != nullptr) ret++;
1262 closedir(dir);
1263 return ret;
1264}
1265
Steven Morelandc1635952021-04-01 16:20:47 +00001266TEST_P(BinderRpc, Fds) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001267 ssize_t beforeFds = countFds();
1268 ASSERT_GE(beforeFds, 0);
1269 {
Steven Moreland4313d7e2021-07-15 23:41:22 +00001270 auto proc = createRpcTestSocketServerProcess({.numThreads = 10});
Steven Moreland5553ac42020-11-11 02:14:45 +00001271 ASSERT_EQ(OK, proc.rootBinder->pingBinder());
1272 }
1273 ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?");
1274}
1275
Steven Morelandda573042021-06-12 01:13:45 +00001276static bool testSupportVsockLoopback() {
Yifan Hong702115c2021-06-24 15:39:18 -07001277 // We don't need to enable TLS to know if vsock is supported.
Steven Morelandda573042021-06-12 01:13:45 +00001278 unsigned int vsockPort = allocateVsockPort();
Yifan Hong702115c2021-06-24 15:39:18 -07001279 sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make());
Steven Morelandda573042021-06-12 01:13:45 +00001280 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland1eab3452021-08-05 16:56:20 -07001281 if (status_t status = server->setupVsockServer(vsockPort); status != OK) {
1282 if (status == -EAFNOSUPPORT) {
1283 return false;
1284 }
1285 LOG_ALWAYS_FATAL("Could not setup vsock server: %s", statusToString(status).c_str());
1286 }
Steven Morelandda573042021-06-12 01:13:45 +00001287 server->start();
1288
Yifan Hongfdd9f692021-09-09 15:12:52 -07001289 sp<RpcSession> session = RpcSession::make(RpcTransportCtxFactoryRaw::make());
Steven Moreland2372f9d2021-08-05 15:42:01 -07001290 status_t status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort);
Steven Moreland798e0d12021-07-14 23:19:25 +00001291 while (!server->shutdown()) usleep(10000);
Steven Moreland2372f9d2021-08-05 15:42:01 -07001292 ALOGE("Detected vsock loopback supported: %s", statusToString(status).c_str());
1293 return status == OK;
Steven Morelandda573042021-06-12 01:13:45 +00001294}
1295
Yifan Hong1deca4b2021-09-10 16:16:44 -07001296static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) {
1297 std::vector<SocketType> ret = {SocketType::UNIX, SocketType::INET};
1298
1299 if (hasPreconnected) ret.push_back(SocketType::PRECONNECTED);
Steven Morelandda573042021-06-12 01:13:45 +00001300
1301 static bool hasVsockLoopback = testSupportVsockLoopback();
1302
1303 if (hasVsockLoopback) {
1304 ret.push_back(SocketType::VSOCK);
1305 }
1306
1307 return ret;
1308}
1309
Yifan Hong702115c2021-06-24 15:39:18 -07001310INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
1311 ::testing::Combine(::testing::ValuesIn(testSocketTypes()),
1312 ::testing::ValuesIn(RpcSecurityValues())),
1313 BinderRpc::PrintParamInfo);
Steven Morelandc1635952021-04-01 16:20:47 +00001314
Yifan Hong702115c2021-06-24 15:39:18 -07001315class BinderRpcServerRootObject
1316 : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001317
1318TEST_P(BinderRpcServerRootObject, WeakRootObject) {
1319 using SetFn = std::function<void(RpcServer*, sp<IBinder>)>;
1320 auto setRootObject = [](bool isStrong) -> SetFn {
1321 return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak);
1322 };
1323
Yifan Hong702115c2021-06-24 15:39:18 -07001324 auto [isStrong1, isStrong2, rpcSecurity] = GetParam();
1325 auto server = RpcServer::make(newFactory(rpcSecurity));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001326 auto binder1 = sp<BBinder>::make();
1327 IBinder* binderRaw1 = binder1.get();
1328 setRootObject(isStrong1)(server.get(), binder1);
1329 EXPECT_EQ(binderRaw1, server->getRootObject());
1330 binder1.clear();
1331 EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject());
1332
1333 auto binder2 = sp<BBinder>::make();
1334 IBinder* binderRaw2 = binder2.get();
1335 setRootObject(isStrong2)(server.get(), binder2);
1336 EXPECT_EQ(binderRaw2, server->getRootObject());
1337 binder2.clear();
1338 EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject());
1339}
1340
1341INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject,
Yifan Hong702115c2021-06-24 15:39:18 -07001342 ::testing::Combine(::testing::Bool(), ::testing::Bool(),
1343 ::testing::ValuesIn(RpcSecurityValues())));
Yifan Hong4ffb0c72021-05-07 18:35:14 -07001344
Yifan Hong1a235852021-05-13 16:07:47 -07001345class OneOffSignal {
1346public:
1347 // If notify() was previously called, or is called within |duration|, return true; else false.
1348 template <typename R, typename P>
1349 bool wait(std::chrono::duration<R, P> duration) {
1350 std::unique_lock<std::mutex> lock(mMutex);
1351 return mCv.wait_for(lock, duration, [this] { return mValue; });
1352 }
1353 void notify() {
1354 std::unique_lock<std::mutex> lock(mMutex);
1355 mValue = true;
1356 lock.unlock();
1357 mCv.notify_all();
1358 }
1359
1360private:
1361 std::mutex mMutex;
1362 std::condition_variable mCv;
1363 bool mValue = false;
1364};
1365
Yifan Hong702115c2021-06-24 15:39:18 -07001366TEST_P(BinderRpcSimple, Shutdown) {
Yifan Hong1a235852021-05-13 16:07:47 -07001367 auto addr = allocateSocketAddress();
Yifan Hong702115c2021-06-24 15:39:18 -07001368 auto server = RpcServer::make(newFactory(GetParam()));
Yifan Hong1a235852021-05-13 16:07:47 -07001369 server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
Steven Moreland2372f9d2021-08-05 15:42:01 -07001370 ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str()));
Yifan Hong1a235852021-05-13 16:07:47 -07001371 auto joinEnds = std::make_shared<OneOffSignal>();
1372
1373 // If things are broken and the thread never stops, don't block other tests. Because the thread
1374 // may run after the test finishes, it must not access the stack memory of the test. Hence,
1375 // shared pointers are passed.
1376 std::thread([server, joinEnds] {
1377 server->join();
1378 joinEnds->notify();
1379 }).detach();
1380
1381 bool shutdown = false;
1382 for (int i = 0; i < 10 && !shutdown; i++) {
1383 usleep(300 * 1000); // 300ms; total 3s
1384 if (server->shutdown()) shutdown = true;
1385 }
1386 ASSERT_TRUE(shutdown) << "server->shutdown() never returns true";
1387
1388 ASSERT_TRUE(joinEnds->wait(2s))
1389 << "After server->shutdown() returns true, join() did not stop after 2s";
1390}
1391
Yifan Hong194acf22021-06-29 18:44:56 -07001392TEST(BinderRpc, Java) {
1393#if !defined(__ANDROID__)
1394 GTEST_SKIP() << "This test is only run on Android. Though it can technically run on host on"
1395 "createRpcDelegateServiceManager() with a device attached, such test belongs "
1396 "to binderHostDeviceTest. Hence, just disable this test on host.";
1397#endif // !__ANDROID__
1398 sp<IServiceManager> sm = defaultServiceManager();
1399 ASSERT_NE(nullptr, sm);
1400 // Any Java service with non-empty getInterfaceDescriptor() would do.
1401 // Let's pick batteryproperties.
1402 auto binder = sm->checkService(String16("batteryproperties"));
1403 ASSERT_NE(nullptr, binder);
1404 auto descriptor = binder->getInterfaceDescriptor();
1405 ASSERT_GE(descriptor.size(), 0);
1406 ASSERT_EQ(OK, binder->pingBinder());
1407
1408 auto rpcServer = RpcServer::make();
1409 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1410 unsigned int port;
Steven Moreland2372f9d2021-08-05 15:42:01 -07001411 ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port));
Yifan Hong194acf22021-06-29 18:44:56 -07001412 auto socket = rpcServer->releaseServer();
1413
1414 auto keepAlive = sp<BBinder>::make();
1415 ASSERT_EQ(OK, binder->setRpcClientDebug(std::move(socket), keepAlive));
1416
1417 auto rpcSession = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -07001418 ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port));
Yifan Hong194acf22021-06-29 18:44:56 -07001419 auto rpcBinder = rpcSession->getRootObject();
1420 ASSERT_NE(nullptr, rpcBinder);
1421
1422 ASSERT_EQ(OK, rpcBinder->pingBinder());
1423
1424 ASSERT_EQ(descriptor, rpcBinder->getInterfaceDescriptor())
1425 << "getInterfaceDescriptor should not crash system_server";
1426 ASSERT_EQ(OK, rpcBinder->pingBinder());
1427}
1428
Yifan Hong702115c2021-06-24 15:39:18 -07001429INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcSimple, ::testing::ValuesIn(RpcSecurityValues()),
1430 BinderRpcSimple::PrintTestParam);
1431
Yifan Hong1deca4b2021-09-10 16:16:44 -07001432class RpcTransportTest
Yifan Hong9734cfc2021-09-13 16:14:09 -07001433 : public ::testing::TestWithParam<std::tuple<SocketType, RpcSecurity, RpcCertificateFormat>> {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001434public:
1435 using ConnectToServer = std::function<base::unique_fd()>;
1436 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
1437 auto [socketType, rpcSecurity, certificateFormat] = info.param;
1438 return PrintToString(socketType) + "_" + newFactory(rpcSecurity)->toCString() + "_" +
1439 PrintToString(certificateFormat);
1440 }
1441 void TearDown() override {
1442 for (auto& server : mServers) server->shutdown();
1443 }
1444
1445 // A server that handles client socket connections.
1446 class Server {
1447 public:
1448 explicit Server() {}
1449 Server(Server&&) = default;
1450 ~Server() { shutdown(); }
1451 [[nodiscard]] AssertionResult setUp() {
1452 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1453 auto rpcServer = RpcServer::make(newFactory(rpcSecurity));
1454 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
1455 switch (socketType) {
1456 case SocketType::PRECONNECTED: {
1457 return AssertionFailure() << "Not supported by this test";
1458 } break;
1459 case SocketType::UNIX: {
1460 auto addr = allocateSocketAddress();
1461 auto status = rpcServer->setupUnixDomainServer(addr.c_str());
1462 if (status != OK) {
1463 return AssertionFailure()
1464 << "setupUnixDomainServer: " << statusToString(status);
1465 }
1466 mConnectToServer = [addr] {
1467 return connectTo(UnixSocketAddress(addr.c_str()));
1468 };
1469 } break;
1470 case SocketType::VSOCK: {
1471 auto port = allocateVsockPort();
1472 auto status = rpcServer->setupVsockServer(port);
1473 if (status != OK) {
1474 return AssertionFailure() << "setupVsockServer: " << statusToString(status);
1475 }
1476 mConnectToServer = [port] {
1477 return connectTo(VsockSocketAddress(VMADDR_CID_LOCAL, port));
1478 };
1479 } break;
1480 case SocketType::INET: {
1481 unsigned int port;
1482 auto status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port);
1483 if (status != OK) {
1484 return AssertionFailure() << "setupInetServer: " << statusToString(status);
1485 }
1486 mConnectToServer = [port] {
1487 const char* addr = kLocalInetAddress;
1488 auto aiStart = InetSocketAddress::getAddrInfo(addr, port);
1489 if (aiStart == nullptr) return base::unique_fd{};
1490 for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) {
1491 auto fd = connectTo(
1492 InetSocketAddress(ai->ai_addr, ai->ai_addrlen, addr, port));
1493 if (fd.ok()) return fd;
1494 }
1495 ALOGE("None of the socket address resolved for %s:%u can be connected",
1496 addr, port);
1497 return base::unique_fd{};
1498 };
1499 }
1500 }
1501 mFd = rpcServer->releaseServer();
1502 if (!mFd.ok()) return AssertionFailure() << "releaseServer returns invalid fd";
1503 mCtx = newFactory(rpcSecurity, mCertVerifier)->newServerCtx();
1504 if (mCtx == nullptr) return AssertionFailure() << "newServerCtx";
1505 mSetup = true;
1506 return AssertionSuccess();
1507 }
1508 RpcTransportCtx* getCtx() const { return mCtx.get(); }
1509 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
1510 return mCertVerifier;
1511 }
1512 ConnectToServer getConnectToServerFn() { return mConnectToServer; }
1513 void start() {
1514 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
1515 mThread = std::make_unique<std::thread>(&Server::run, this);
1516 }
1517 void run() {
1518 LOG_ALWAYS_FATAL_IF(!mSetup, "Call Server::setup first!");
1519
1520 std::vector<std::thread> threads;
1521 while (OK == mFdTrigger->triggerablePoll(mFd, POLLIN)) {
1522 base::unique_fd acceptedFd(
1523 TEMP_FAILURE_RETRY(accept4(mFd.get(), nullptr, nullptr /*length*/,
1524 SOCK_CLOEXEC | SOCK_NONBLOCK)));
1525 threads.emplace_back(&Server::handleOne, this, std::move(acceptedFd));
1526 }
1527
1528 for (auto& thread : threads) thread.join();
1529 }
1530 void handleOne(android::base::unique_fd acceptedFd) {
1531 ASSERT_TRUE(acceptedFd.ok());
1532 auto serverTransport = mCtx->newTransport(std::move(acceptedFd), mFdTrigger.get());
1533 if (serverTransport == nullptr) return; // handshake failed
1534 std::string message(kMessage);
1535 ASSERT_EQ(OK,
1536 serverTransport->interruptableWriteFully(mFdTrigger.get(), message.data(),
1537 message.size()));
1538 }
1539 void shutdown() {
1540 mFdTrigger->trigger();
1541 if (mThread != nullptr) {
1542 mThread->join();
1543 mThread = nullptr;
1544 }
1545 }
1546
1547 private:
1548 std::unique_ptr<std::thread> mThread;
1549 ConnectToServer mConnectToServer;
1550 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
1551 base::unique_fd mFd;
1552 std::unique_ptr<RpcTransportCtx> mCtx;
1553 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
1554 std::make_shared<RpcCertificateVerifierSimple>();
1555 bool mSetup = false;
1556 };
1557
1558 class Client {
1559 public:
1560 explicit Client(ConnectToServer connectToServer) : mConnectToServer(connectToServer) {}
1561 Client(Client&&) = default;
1562 [[nodiscard]] AssertionResult setUp() {
1563 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1564 mFd = mConnectToServer();
1565 if (!mFd.ok()) return AssertionFailure() << "Cannot connect to server";
1566 mFdTrigger = FdTrigger::make();
1567 mCtx = newFactory(rpcSecurity, mCertVerifier)->newClientCtx();
1568 if (mCtx == nullptr) return AssertionFailure() << "newClientCtx";
1569 return AssertionSuccess();
1570 }
1571 RpcTransportCtx* getCtx() const { return mCtx.get(); }
1572 std::shared_ptr<RpcCertificateVerifierSimple> getCertVerifier() const {
1573 return mCertVerifier;
1574 }
1575 void run(bool handshakeOk = true, bool readOk = true) {
1576 auto clientTransport = mCtx->newTransport(std::move(mFd), mFdTrigger.get());
1577 if (clientTransport == nullptr) {
1578 ASSERT_FALSE(handshakeOk) << "newTransport returns nullptr, but it shouldn't";
1579 return;
1580 }
1581 ASSERT_TRUE(handshakeOk) << "newTransport does not return nullptr, but it should";
1582 std::string expectedMessage(kMessage);
1583 std::string readMessage(expectedMessage.size(), '\0');
1584 status_t readStatus =
1585 clientTransport->interruptableReadFully(mFdTrigger.get(), readMessage.data(),
1586 readMessage.size());
1587 if (readOk) {
1588 ASSERT_EQ(OK, readStatus);
1589 ASSERT_EQ(readMessage, expectedMessage);
1590 } else {
1591 ASSERT_NE(OK, readStatus);
1592 }
1593 }
1594
1595 private:
1596 ConnectToServer mConnectToServer;
1597 base::unique_fd mFd;
1598 std::unique_ptr<FdTrigger> mFdTrigger = FdTrigger::make();
1599 std::unique_ptr<RpcTransportCtx> mCtx;
1600 std::shared_ptr<RpcCertificateVerifierSimple> mCertVerifier =
1601 std::make_shared<RpcCertificateVerifierSimple>();
1602 };
1603
1604 // Make A trust B.
1605 template <typename A, typename B>
1606 status_t trust(A* a, B* b) {
1607 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1608 if (rpcSecurity != RpcSecurity::TLS) return OK;
1609 auto bCert = b->getCtx()->getCertificate(certificateFormat);
1610 return a->getCertVerifier()->addTrustedPeerCertificate(certificateFormat, bCert);
1611 }
1612
1613 static constexpr const char* kMessage = "hello";
1614 std::vector<std::unique_ptr<Server>> mServers;
1615};
1616
1617TEST_P(RpcTransportTest, GoodCertificate) {
1618 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1619 ASSERT_TRUE(server->setUp());
1620
1621 Client client(server->getConnectToServerFn());
1622 ASSERT_TRUE(client.setUp());
1623
1624 ASSERT_EQ(OK, trust(&client, server));
1625 ASSERT_EQ(OK, trust(server, &client));
1626
1627 server->start();
1628 client.run();
1629}
1630
1631TEST_P(RpcTransportTest, MultipleClients) {
1632 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1633 ASSERT_TRUE(server->setUp());
1634
1635 std::vector<Client> clients;
1636 for (int i = 0; i < 2; i++) {
1637 auto& client = clients.emplace_back(server->getConnectToServerFn());
1638 ASSERT_TRUE(client.setUp());
1639 ASSERT_EQ(OK, trust(&client, server));
1640 ASSERT_EQ(OK, trust(server, &client));
1641 }
1642
1643 server->start();
1644 for (auto& client : clients) client.run();
1645}
1646
1647TEST_P(RpcTransportTest, UntrustedServer) {
1648 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1649
1650 auto untrustedServer = mServers.emplace_back(std::make_unique<Server>()).get();
1651 ASSERT_TRUE(untrustedServer->setUp());
1652
1653 Client client(untrustedServer->getConnectToServerFn());
1654 ASSERT_TRUE(client.setUp());
1655
1656 ASSERT_EQ(OK, trust(untrustedServer, &client));
1657
1658 untrustedServer->start();
1659
1660 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
1661 // the client can't verify the server's identity.
1662 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
1663 client.run(handshakeOk);
1664}
1665TEST_P(RpcTransportTest, MaliciousServer) {
1666 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1667 auto validServer = mServers.emplace_back(std::make_unique<Server>()).get();
1668 ASSERT_TRUE(validServer->setUp());
1669
1670 auto maliciousServer = mServers.emplace_back(std::make_unique<Server>()).get();
1671 ASSERT_TRUE(maliciousServer->setUp());
1672
1673 Client client(maliciousServer->getConnectToServerFn());
1674 ASSERT_TRUE(client.setUp());
1675
1676 ASSERT_EQ(OK, trust(&client, validServer));
1677 ASSERT_EQ(OK, trust(validServer, &client));
1678 ASSERT_EQ(OK, trust(maliciousServer, &client));
1679
1680 maliciousServer->start();
1681
1682 // For TLS, this should reject the certificate. For RAW sockets, it should pass because
1683 // the client can't verify the server's identity.
1684 bool handshakeOk = rpcSecurity != RpcSecurity::TLS;
1685 client.run(handshakeOk);
1686}
1687
1688TEST_P(RpcTransportTest, UntrustedClient) {
1689 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1690 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1691 ASSERT_TRUE(server->setUp());
1692
1693 Client client(server->getConnectToServerFn());
1694 ASSERT_TRUE(client.setUp());
1695
1696 ASSERT_EQ(OK, trust(&client, server));
1697
1698 server->start();
1699
1700 // For TLS, Client should be able to verify server's identity, so client should see
1701 // do_handshake() successfully executed. However, server shouldn't be able to verify client's
1702 // identity and should drop the connection, so client shouldn't be able to read anything.
1703 bool readOk = rpcSecurity != RpcSecurity::TLS;
1704 client.run(true, readOk);
1705}
1706
1707TEST_P(RpcTransportTest, MaliciousClient) {
1708 auto [socketType, rpcSecurity, certificateFormat] = GetParam();
1709 auto server = mServers.emplace_back(std::make_unique<Server>()).get();
1710 ASSERT_TRUE(server->setUp());
1711
1712 Client validClient(server->getConnectToServerFn());
1713 ASSERT_TRUE(validClient.setUp());
1714 Client maliciousClient(server->getConnectToServerFn());
1715 ASSERT_TRUE(maliciousClient.setUp());
1716
1717 ASSERT_EQ(OK, trust(&validClient, server));
1718 ASSERT_EQ(OK, trust(&maliciousClient, server));
1719
1720 server->start();
1721
1722 // See UntrustedClient.
1723 bool readOk = rpcSecurity != RpcSecurity::TLS;
1724 maliciousClient.run(true, readOk);
1725}
1726
Yifan Hong9734cfc2021-09-13 16:14:09 -07001727std::vector<RpcCertificateFormat> testRpcCertificateFormats() {
Yifan Hong1deca4b2021-09-10 16:16:44 -07001728 return {
Yifan Hong9734cfc2021-09-13 16:14:09 -07001729 RpcCertificateFormat::PEM,
1730 RpcCertificateFormat::DER,
Yifan Hong1deca4b2021-09-10 16:16:44 -07001731 };
1732}
1733
1734INSTANTIATE_TEST_CASE_P(BinderRpc, RpcTransportTest,
1735 ::testing::Combine(::testing::ValuesIn(testSocketTypes(false)),
1736 ::testing::ValuesIn(RpcSecurityValues()),
Yifan Hong9734cfc2021-09-13 16:14:09 -07001737 ::testing::ValuesIn(testRpcCertificateFormats())),
Yifan Hong1deca4b2021-09-10 16:16:44 -07001738 RpcTransportTest::PrintParamInfo);
1739
Steven Morelandc1635952021-04-01 16:20:47 +00001740} // namespace android
1741
1742int main(int argc, char** argv) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001743 ::testing::InitGoogleTest(&argc, argv);
1744 android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter);
1745 return RUN_ALL_TESTS();
1746}