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 #include <android/binder_auto_utils.h>
18 #include <android/binder_manager.h>
19 #include <binder/ProcessState.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include <aidl/android/aidl/tests/ITestService.h>
24
25 using aidl::android::aidl::tests::INamedCallback;
26 using aidl::android::aidl::tests::ITestService;
27 using testing::Eq;
28
29 struct AidlTest : testing::Test {
30 template <typename T>
getServiceAidlTest31 std::shared_ptr<T> getService() {
32 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
33 android::ProcessState::self()->startThreadPool();
34 ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_waitForService(T::descriptor));
35 return T::fromBinder(binder);
36 }
37 };
38
TEST_F(AidlTest,InterfaceExchange)39 TEST_F(AidlTest, InterfaceExchange) {
40 auto service = getService<ITestService>();
41 std::vector<std::string> names = {"Larry", "Curly", "Moe"};
42
43 for (size_t i = 0; i < names.size(); i++) {
44 std::shared_ptr<INamedCallback> got;
45 ASSERT_TRUE(service->GetOtherTestService(names[i], &got).isOk());
46 std::string name;
47 ASSERT_TRUE(got->GetName(&name).isOk());
48 ASSERT_THAT(name, Eq(names[i]));
49 }
50 {
51 std::vector<std::shared_ptr<INamedCallback>> got;
52 ASSERT_TRUE(service->GetInterfaceArray(names, &got).isOk());
53
54 bool verified = false;
55 ASSERT_TRUE(service->VerifyNamesWithInterfaceArray(got, names, &verified).isOk());
56 ASSERT_TRUE(verified);
57
58 for (size_t i = 0; i < names.size(); i++) {
59 std::string name;
60 ASSERT_TRUE(got[i]->GetName(&name).isOk());
61 ASSERT_THAT(name, Eq(names[i]));
62 }
63 }
64 {
65 std::vector<std::optional<std::string>> names = {"Larry", std::nullopt, "Moe"};
66 std::optional<std::vector<std::shared_ptr<INamedCallback>>> got;
67 ASSERT_TRUE(service->GetNullableInterfaceArray(names, &got).isOk());
68 bool verified = false;
69 ASSERT_TRUE(service->VerifyNamesWithNullableInterfaceArray(got, names, &verified).isOk());
70 ASSERT_TRUE(verified);
71 ASSERT_TRUE(got.has_value());
72 for (size_t i = 0; i < names.size(); i++) {
73 if (names[i].has_value()) {
74 ASSERT_NE(got->at(i).get(), nullptr);
75 std::string name;
76 ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
77 ASSERT_THAT(name, Eq(names[i].value()));
78 } else {
79 ASSERT_EQ(got->at(i).get(), nullptr);
80 }
81 }
82 }
83 {
84 std::vector<std::optional<std::string>> names = {"Larry", std::nullopt, "Moe"};
85 std::optional<std::vector<std::shared_ptr<INamedCallback>>> got;
86 ASSERT_TRUE(service->GetInterfaceList(names, &got).isOk());
87 bool verified = false;
88 ASSERT_TRUE(service->VerifyNamesWithInterfaceList(got, names, &verified).isOk());
89 ASSERT_TRUE(verified);
90 ASSERT_TRUE(got.has_value());
91 for (size_t i = 0; i < names.size(); i++) {
92 if (names[i].has_value()) {
93 ASSERT_NE(got->at(i).get(), nullptr);
94 std::string name;
95 ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
96 ASSERT_THAT(name, Eq(names[i].value()));
97 } else {
98 ASSERT_EQ(got->at(i).get(), nullptr);
99 }
100 }
101 }
102 }
103