Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | // Integration test for servicedispatcher + adb forward. Requires ADB. |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <android-base/parsebool.h> |
| 24 | #include <android-base/result-gmock.h> |
| 25 | #include <android-base/strings.h> |
| 26 | #include <gmock/gmock.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | #include <binder/IServiceManager.h> |
| 30 | #include <binder/RpcSession.h> |
| 31 | |
| 32 | #include "../UtilsHost.h" |
| 33 | |
| 34 | using ::android::setDefaultServiceManager; |
| 35 | using ::android::base::EndsWith; |
| 36 | using ::android::base::Join; |
| 37 | using ::android::base::ParseBool; |
| 38 | using ::android::base::ParseBoolResult; |
| 39 | using ::android::base::Split; |
| 40 | using ::android::base::StartsWith; |
| 41 | using ::android::base::StringReplace; |
| 42 | using ::android::base::Trim; |
| 43 | using ::android::base::testing::Ok; |
| 44 | using ::std::chrono_literals::operator""ms; |
| 45 | using ::std::string_literals::operator""s; |
| 46 | using ::testing::AllOf; |
| 47 | using ::testing::Contains; |
| 48 | using ::testing::ContainsRegex; |
| 49 | using ::testing::ExplainMatchResult; |
| 50 | using ::testing::InitGoogleMock; |
| 51 | |
| 52 | namespace android { |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | constexpr const char* kServiceBinary = "/data/local/tmp/binderHostDeviceTest-service"; |
| 57 | constexpr const char* kServiceName = "binderHostDeviceTestService"; |
| 58 | constexpr const char* kDescriptor = "android.binderHostDeviceTestService"; |
| 59 | |
| 60 | // e.g. EXPECT_THAT(expr, StatusEq(OK)) << "additional message"; |
| 61 | MATCHER_P(StatusEq, expected, (negation ? "not " : "") + statusToString(expected)) { |
| 62 | *result_listener << statusToString(arg); |
| 63 | return expected == arg; |
| 64 | } |
| 65 | |
| 66 | void initHostRpcServiceManagerOnce() { |
| 67 | static std::once_flag gSmOnce; |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 68 | std::call_once(gSmOnce, [] { |
Steven Moreland | feb13e8 | 2023-03-01 01:25:33 +0000 | [diff] [blame] | 69 | setDefaultServiceManager(createRpcDelegateServiceManager({.maxOutgoingConnections = 1})); |
Yifan Hong | 5a05ef7 | 2021-10-08 17:33:47 -0700 | [diff] [blame] | 70 | }); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Test for host service manager. |
| 74 | class HostDeviceTest : public ::testing::Test { |
| 75 | public: |
| 76 | void SetUp() override { |
| 77 | auto debuggableResult = execute(Split("adb shell getprop ro.debuggable", " "), nullptr); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(debuggableResult.has_value()); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 79 | ASSERT_EQ(0, debuggableResult->exitCode) << *debuggableResult; |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 80 | auto debuggableBool = ParseBool(Trim(debuggableResult->stdoutStr)); |
| 81 | ASSERT_NE(ParseBoolResult::kError, debuggableBool) << Trim(debuggableResult->stdoutStr); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 82 | if (debuggableBool == ParseBoolResult::kFalse) { |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 83 | GTEST_SKIP() << "ro.debuggable=" << Trim(debuggableResult->stdoutStr); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | auto lsResult = execute(Split("adb shell which servicedispatcher", " "), nullptr); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 87 | ASSERT_TRUE(lsResult.has_value()); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 88 | if (lsResult->exitCode != 0) { |
| 89 | GTEST_SKIP() << "b/182914638: until feature is fully enabled, skip test on devices " |
| 90 | "without servicedispatcher"; |
| 91 | } |
| 92 | |
| 93 | initHostRpcServiceManagerOnce(); |
| 94 | ASSERT_NE(nullptr, defaultServiceManager()) << "No defaultServiceManager() over RPC"; |
| 95 | |
| 96 | auto service = execute({"adb", "shell", kServiceBinary, kServiceName, kDescriptor}, |
| 97 | &CommandResult::stdoutEndsWithNewLine); |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 98 | ASSERT_TRUE(service.has_value()); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 99 | ASSERT_EQ(std::nullopt, service->exitCode) << *service; |
| 100 | mService = std::move(*service); |
| 101 | } |
| 102 | void TearDown() override { mService.reset(); } |
| 103 | |
| 104 | [[nodiscard]] static sp<IBinder> get(unsigned int hostPort) { |
| 105 | auto rpcSession = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 106 | if (status_t status = rpcSession->setupInetClient("127.0.0.1", hostPort); status != OK) { |
| 107 | ADD_FAILURE() << "Failed to setupInetClient on " << hostPort << ": " |
| 108 | << statusToString(status); |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
| 111 | return rpcSession->getRootObject(); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | std::optional<CommandResult> mService; |
| 116 | }; |
| 117 | |
| 118 | TEST_F(HostDeviceTest, List) { |
| 119 | auto sm = defaultServiceManager(); |
| 120 | |
| 121 | auto services = sm->listServices(); |
| 122 | ASSERT_THAT(services, Contains(String16(kServiceName))); |
| 123 | } |
| 124 | |
| 125 | TEST_F(HostDeviceTest, CheckService) { |
| 126 | auto sm = defaultServiceManager(); |
| 127 | |
| 128 | auto rpcBinder = sm->checkService(String16(kServiceName)); |
| 129 | ASSERT_NE(nullptr, rpcBinder); |
| 130 | |
| 131 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 132 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 133 | } |
| 134 | |
| 135 | TEST_F(HostDeviceTest, GetService) { |
| 136 | auto sm = defaultServiceManager(); |
| 137 | |
Tomasz Wasilczyk | bb07b98 | 2023-10-11 21:25:36 +0000 | [diff] [blame] | 138 | #pragma clang diagnostic push |
| 139 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 140 | auto rpcBinder = sm->getService(String16(kServiceName)); |
Tomasz Wasilczyk | bb07b98 | 2023-10-11 21:25:36 +0000 | [diff] [blame] | 141 | #pragma clang diagnostic pop |
Yifan Hong | 1b6f12f | 2021-06-03 20:01:57 -0700 | [diff] [blame] | 142 | ASSERT_NE(nullptr, rpcBinder); |
| 143 | |
| 144 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 145 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(HostDeviceTest, WaitForService) { |
| 149 | auto sm = defaultServiceManager(); |
| 150 | |
| 151 | auto rpcBinder = sm->waitForService(String16(kServiceName)); |
| 152 | ASSERT_NE(nullptr, rpcBinder); |
| 153 | |
| 154 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 155 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 156 | } |
| 157 | |
| 158 | TEST_F(HostDeviceTest, TenClients) { |
| 159 | auto sm = defaultServiceManager(); |
| 160 | |
| 161 | auto threadFn = [&] { |
| 162 | auto rpcBinder = sm->checkService(String16(kServiceName)); |
| 163 | ASSERT_NE(nullptr, rpcBinder); |
| 164 | |
| 165 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 166 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 167 | }; |
| 168 | |
| 169 | std::vector<std::thread> threads; |
| 170 | for (size_t i = 0; i < 10; ++i) threads.emplace_back(threadFn); |
| 171 | for (auto& thread : threads) thread.join(); |
| 172 | } |
| 173 | |
| 174 | } // namespace |
| 175 | |
| 176 | } // namespace android |