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; |
| 68 | std::call_once(gSmOnce, [] { setDefaultServiceManager(createRpcDelegateServiceManager()); }); |
| 69 | } |
| 70 | |
| 71 | // Test for host service manager. |
| 72 | class HostDeviceTest : public ::testing::Test { |
| 73 | public: |
| 74 | void SetUp() override { |
| 75 | auto debuggableResult = execute(Split("adb shell getprop ro.debuggable", " "), nullptr); |
| 76 | ASSERT_THAT(debuggableResult, Ok()); |
| 77 | ASSERT_EQ(0, debuggableResult->exitCode) << *debuggableResult; |
| 78 | auto debuggableBool = ParseBool(Trim(debuggableResult->stdout)); |
| 79 | ASSERT_NE(ParseBoolResult::kError, debuggableBool) << Trim(debuggableResult->stdout); |
| 80 | if (debuggableBool == ParseBoolResult::kFalse) { |
| 81 | GTEST_SKIP() << "ro.debuggable=" << Trim(debuggableResult->stdout); |
| 82 | } |
| 83 | |
| 84 | auto lsResult = execute(Split("adb shell which servicedispatcher", " "), nullptr); |
| 85 | ASSERT_THAT(lsResult, Ok()); |
| 86 | if (lsResult->exitCode != 0) { |
| 87 | GTEST_SKIP() << "b/182914638: until feature is fully enabled, skip test on devices " |
| 88 | "without servicedispatcher"; |
| 89 | } |
| 90 | |
| 91 | initHostRpcServiceManagerOnce(); |
| 92 | ASSERT_NE(nullptr, defaultServiceManager()) << "No defaultServiceManager() over RPC"; |
| 93 | |
| 94 | auto service = execute({"adb", "shell", kServiceBinary, kServiceName, kDescriptor}, |
| 95 | &CommandResult::stdoutEndsWithNewLine); |
| 96 | ASSERT_THAT(service, Ok()); |
| 97 | ASSERT_EQ(std::nullopt, service->exitCode) << *service; |
| 98 | mService = std::move(*service); |
| 99 | } |
| 100 | void TearDown() override { mService.reset(); } |
| 101 | |
| 102 | [[nodiscard]] static sp<IBinder> get(unsigned int hostPort) { |
| 103 | auto rpcSession = RpcSession::make(); |
| 104 | if (!rpcSession->setupInetClient("127.0.0.1", hostPort)) { |
| 105 | ADD_FAILURE() << "Failed to setupInetClient on " << hostPort; |
| 106 | return nullptr; |
| 107 | } |
| 108 | return rpcSession->getRootObject(); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | std::optional<CommandResult> mService; |
| 113 | }; |
| 114 | |
| 115 | TEST_F(HostDeviceTest, List) { |
| 116 | auto sm = defaultServiceManager(); |
| 117 | |
| 118 | auto services = sm->listServices(); |
| 119 | ASSERT_THAT(services, Contains(String16(kServiceName))); |
| 120 | } |
| 121 | |
| 122 | TEST_F(HostDeviceTest, CheckService) { |
| 123 | auto sm = defaultServiceManager(); |
| 124 | |
| 125 | auto rpcBinder = sm->checkService(String16(kServiceName)); |
| 126 | ASSERT_NE(nullptr, rpcBinder); |
| 127 | |
| 128 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 129 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 130 | } |
| 131 | |
| 132 | TEST_F(HostDeviceTest, GetService) { |
| 133 | auto sm = defaultServiceManager(); |
| 134 | |
| 135 | auto rpcBinder = sm->getService(String16(kServiceName)); |
| 136 | ASSERT_NE(nullptr, rpcBinder); |
| 137 | |
| 138 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 139 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 140 | } |
| 141 | |
| 142 | TEST_F(HostDeviceTest, WaitForService) { |
| 143 | auto sm = defaultServiceManager(); |
| 144 | |
| 145 | auto rpcBinder = sm->waitForService(String16(kServiceName)); |
| 146 | ASSERT_NE(nullptr, rpcBinder); |
| 147 | |
| 148 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 149 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 150 | } |
| 151 | |
| 152 | TEST_F(HostDeviceTest, TenClients) { |
| 153 | auto sm = defaultServiceManager(); |
| 154 | |
| 155 | auto threadFn = [&] { |
| 156 | auto rpcBinder = sm->checkService(String16(kServiceName)); |
| 157 | ASSERT_NE(nullptr, rpcBinder); |
| 158 | |
| 159 | EXPECT_THAT(rpcBinder->pingBinder(), StatusEq(OK)); |
| 160 | EXPECT_EQ(String16(kDescriptor), rpcBinder->getInterfaceDescriptor()); |
| 161 | }; |
| 162 | |
| 163 | std::vector<std::thread> threads; |
| 164 | for (size_t i = 0; i < 10; ++i) threads.emplace_back(threadFn); |
| 165 | for (auto& thread : threads) thread.join(); |
| 166 | } |
| 167 | |
| 168 | } // namespace |
| 169 | |
| 170 | } // namespace android |