blob: fcb83bdabd91967e1f0e16118afe46d836a35c0f [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
Andrei Homescuf30148c2023-03-10 00:31:45 +000042std::unique_ptr<RpcTransportCtxFactory> BinderRpc::newFactory(RpcSecurity rpcSecurity) {
43 switch (rpcSecurity) {
44 case RpcSecurity::RAW:
45 return RpcTransportCtxFactoryTipcTrusty::make();
46 default:
47 LOG_ALWAYS_FATAL("Unknown RpcSecurity %d", rpcSecurity);
Andrei Homescu4d9420e2023-01-31 01:38:48 +000048 }
Andrei Homescu4d9420e2023-01-31 01:38:48 +000049}
50
51// This creates a new process serving an interface on a certain number of
52// threads.
53std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
54 const BinderRpcOptions& options) {
Andrei Homescua646c582023-03-15 01:47:21 +000055 LOG_ALWAYS_FATAL_IF(std::any_of(options.numIncomingConnectionsBySession.begin(),
56 options.numIncomingConnectionsBySession.end(),
57 [](size_t n) { return n != 0; }),
58 "Non-zero incoming connections on Trusty");
Andrei Homescu4d9420e2023-01-31 01:38:48 +000059
Steven Morelandb469f432023-07-28 22:13:47 +000060 RpcSecurity rpcSecurity = GetParam().security;
61 uint32_t clientVersion = GetParam().clientVersion;
62 uint32_t serverVersion = GetParam().serverVersion;
Andrei Homescu4d9420e2023-01-31 01:38:48 +000063
64 auto ret = std::make_unique<TrustyProcessSession>();
65
66 status_t status;
67 for (size_t i = 0; i < options.numSessions; i++) {
Andrei Homescuf30148c2023-03-10 00:31:45 +000068 auto session = android::RpcSession::make(newFactory(rpcSecurity));
Andrei Homescu4d9420e2023-01-31 01:38:48 +000069
70 EXPECT_TRUE(session->setProtocolVersion(clientVersion));
Steven Morelandfeb13e82023-03-01 01:25:33 +000071 session->setMaxOutgoingConnections(options.numOutgoingConnections);
Andrei Homescu4d9420e2023-01-31 01:38:48 +000072 session->setFileDescriptorTransportMode(options.clientFileDescriptorTransportMode);
73
74 status = session->setupPreconnectedClient({}, [&]() {
75 auto port = trustyIpcPort(serverVersion);
76 int rc = connect(port.c_str(), IPC_CONNECT_WAIT_FOR_PORT);
77 LOG_ALWAYS_FATAL_IF(rc < 0, "Failed to connect to service: %d", rc);
78 return base::unique_fd(rc);
79 });
80 if (options.allowConnectFailure && status != OK) {
81 ret->sessions.clear();
82 break;
83 }
84 LOG_ALWAYS_FATAL_IF(status != OK, "Failed to connect to service: %s",
85 statusToString(status).c_str());
86 ret->sessions.push_back({session, session->getRootObject()});
87 }
88
89 return ret;
90}
91
Steven Morelandb469f432023-07-28 22:13:47 +000092static std::vector<BinderRpc::ParamType> getTrustyBinderRpcParams() {
93 std::vector<BinderRpc::ParamType> ret;
94
95 for (const auto& clientVersion : testVersions()) {
96 for (const auto& serverVersion : testVersions()) {
97 ret.push_back(BinderRpc::ParamType{
98 .type = SocketType::TIPC,
99 .security = RpcSecurity::RAW,
100 .clientVersion = clientVersion,
101 .serverVersion = serverVersion,
102 // TODO: should we test both versions here?
103 .singleThreaded = false,
104 .noKernel = true,
105 });
106 }
107 }
108
109 return ret;
110}
111
112INSTANTIATE_TEST_CASE_P(Trusty, BinderRpc, ::testing::ValuesIn(getTrustyBinderRpcParams()),
Andrei Homescu4d9420e2023-01-31 01:38:48 +0000113 BinderRpc::PrintParamInfo);
114
115} // namespace android
116
117PORT_GTEST(BinderRpcTest, "com.android.trusty.binderRpcTest");