Andrei Homescu | 4d9420e | 2023-01-31 01:38:48 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | namespace android { |
| 27 | |
| 28 | // Destructors need to be defined, even if pure virtual |
| 29 | ProcessSession::~ProcessSession() {} |
| 30 | |
| 31 | class TrustyProcessSession : public ProcessSession { |
| 32 | public: |
| 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 Homescu | f30148c | 2023-03-10 00:31:45 +0000 | [diff] [blame^] | 42 | std::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 Homescu | 4d9420e | 2023-01-31 01:38:48 +0000 | [diff] [blame] | 48 | } |
Andrei Homescu | 4d9420e | 2023-01-31 01:38:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // This creates a new process serving an interface on a certain number of |
| 52 | // threads. |
| 53 | std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc( |
| 54 | const BinderRpcOptions& options) { |
| 55 | LOG_ALWAYS_FATAL_IF(options.numIncomingConnections != 0, |
| 56 | "Non-zero incoming connections %zu on Trusty", |
| 57 | options.numIncomingConnections); |
| 58 | |
Andrei Homescu | f30148c | 2023-03-10 00:31:45 +0000 | [diff] [blame^] | 59 | RpcSecurity rpcSecurity = std::get<1>(GetParam()); |
Andrei Homescu | 4d9420e | 2023-01-31 01:38:48 +0000 | [diff] [blame] | 60 | uint32_t clientVersion = std::get<2>(GetParam()); |
| 61 | uint32_t serverVersion = std::get<3>(GetParam()); |
| 62 | |
| 63 | auto ret = std::make_unique<TrustyProcessSession>(); |
| 64 | |
| 65 | status_t status; |
| 66 | for (size_t i = 0; i < options.numSessions; i++) { |
Andrei Homescu | f30148c | 2023-03-10 00:31:45 +0000 | [diff] [blame^] | 67 | auto session = android::RpcSession::make(newFactory(rpcSecurity)); |
Andrei Homescu | 4d9420e | 2023-01-31 01:38:48 +0000 | [diff] [blame] | 68 | |
| 69 | EXPECT_TRUE(session->setProtocolVersion(clientVersion)); |
Steven Moreland | feb13e8 | 2023-03-01 01:25:33 +0000 | [diff] [blame] | 70 | session->setMaxOutgoingConnections(options.numOutgoingConnections); |
Andrei Homescu | 4d9420e | 2023-01-31 01:38:48 +0000 | [diff] [blame] | 71 | session->setFileDescriptorTransportMode(options.clientFileDescriptorTransportMode); |
| 72 | |
| 73 | status = session->setupPreconnectedClient({}, [&]() { |
| 74 | auto port = trustyIpcPort(serverVersion); |
| 75 | int rc = connect(port.c_str(), IPC_CONNECT_WAIT_FOR_PORT); |
| 76 | LOG_ALWAYS_FATAL_IF(rc < 0, "Failed to connect to service: %d", rc); |
| 77 | return base::unique_fd(rc); |
| 78 | }); |
| 79 | if (options.allowConnectFailure && status != OK) { |
| 80 | ret->sessions.clear(); |
| 81 | break; |
| 82 | } |
| 83 | LOG_ALWAYS_FATAL_IF(status != OK, "Failed to connect to service: %s", |
| 84 | statusToString(status).c_str()); |
| 85 | ret->sessions.push_back({session, session->getRootObject()}); |
| 86 | } |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | INSTANTIATE_TEST_CASE_P(Trusty, BinderRpc, |
| 92 | ::testing::Combine(::testing::Values(SocketType::TIPC), |
| 93 | ::testing::Values(RpcSecurity::RAW), |
| 94 | ::testing::ValuesIn(testVersions()), |
| 95 | ::testing::ValuesIn(testVersions()), |
| 96 | ::testing::Values(false), ::testing::Values(true)), |
| 97 | BinderRpc::PrintParamInfo); |
| 98 | |
| 99 | } // namespace android |
| 100 | |
| 101 | PORT_GTEST(BinderRpcTest, "com.android.trusty.binderRpcTest"); |