blob: 63b56a3a64b1666f8fc4662d25e16e74e8fb0bc7 [file] [log] [blame]
Andrei Homescu4d9420e2023-01-31 01:38:48 +00001/*
2 * Copyright (C) 2022 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
17#define LOG_TAG "binderRpcTest"
18
19#include <android-base/stringprintf.h>
20#include <binder/RpcTransportTipcTrusty.h>
21#include <trusty-gtest.h>
22#include <trusty_ipc.h>
23
24#include "binderRpcTestFixture.h"
25
26namespace android {
27
28// Destructors need to be defined, even if pure virtual
29ProcessSession::~ProcessSession() {}
30
31class TrustyProcessSession : public ProcessSession {
32public:
33 ~TrustyProcessSession() override {}
34
35 void setCustomExitStatusCheck(std::function<void(int wstatus)> /*f*/) override {
36 LOG_ALWAYS_FATAL("setCustomExitStatusCheck() not supported");
37 }
38
39 void terminate() override { LOG_ALWAYS_FATAL("terminate() not supported"); }
40};
41
42std::string BinderRpc::PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
43 auto [type, security, clientVersion, serverVersion, singleThreaded, noKernel] = info.param;
44 auto ret = PrintToString(type) + "_clientV" + std::to_string(clientVersion) + "_serverV" +
45 std::to_string(serverVersion);
46 if (singleThreaded) {
47 ret += "_single_threaded";
Steven Moreland5602a1a2023-03-06 19:25:46 +000048 } else {
49 ret += "_multi_threaded";
Andrei Homescu4d9420e2023-01-31 01:38:48 +000050 }
51 if (noKernel) {
52 ret += "_no_kernel";
Steven Moreland5602a1a2023-03-06 19:25:46 +000053 } else {
54 ret += "_with_kernel";
Andrei Homescu4d9420e2023-01-31 01:38:48 +000055 }
56 return ret;
57}
58
59// This creates a new process serving an interface on a certain number of
60// threads.
61std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
62 const BinderRpcOptions& options) {
63 LOG_ALWAYS_FATAL_IF(options.numIncomingConnections != 0,
64 "Non-zero incoming connections %zu on Trusty",
65 options.numIncomingConnections);
66
67 uint32_t clientVersion = std::get<2>(GetParam());
68 uint32_t serverVersion = std::get<3>(GetParam());
69
70 auto ret = std::make_unique<TrustyProcessSession>();
71
72 status_t status;
73 for (size_t i = 0; i < options.numSessions; i++) {
74 auto factory = android::RpcTransportCtxFactoryTipcTrusty::make();
75 auto session = android::RpcSession::make(std::move(factory));
76
77 EXPECT_TRUE(session->setProtocolVersion(clientVersion));
Steven Morelandfeb13e82023-03-01 01:25:33 +000078 session->setMaxOutgoingConnections(options.numOutgoingConnections);
Andrei Homescu4d9420e2023-01-31 01:38:48 +000079 session->setFileDescriptorTransportMode(options.clientFileDescriptorTransportMode);
80
81 status = session->setupPreconnectedClient({}, [&]() {
82 auto port = trustyIpcPort(serverVersion);
83 int rc = connect(port.c_str(), IPC_CONNECT_WAIT_FOR_PORT);
84 LOG_ALWAYS_FATAL_IF(rc < 0, "Failed to connect to service: %d", rc);
85 return base::unique_fd(rc);
86 });
87 if (options.allowConnectFailure && status != OK) {
88 ret->sessions.clear();
89 break;
90 }
91 LOG_ALWAYS_FATAL_IF(status != OK, "Failed to connect to service: %s",
92 statusToString(status).c_str());
93 ret->sessions.push_back({session, session->getRootObject()});
94 }
95
96 return ret;
97}
98
99INSTANTIATE_TEST_CASE_P(Trusty, BinderRpc,
100 ::testing::Combine(::testing::Values(SocketType::TIPC),
101 ::testing::Values(RpcSecurity::RAW),
102 ::testing::ValuesIn(testVersions()),
103 ::testing::ValuesIn(testVersions()),
104 ::testing::Values(false), ::testing::Values(true)),
105 BinderRpc::PrintParamInfo);
106
107} // namespace android
108
109PORT_GTEST(BinderRpcTest, "com.android.trusty.binderRpcTest");