• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::BackendType;
26 using aidl::android::aidl::tests::INamedCallback;
27 using aidl::android::aidl::tests::ITestService;
28 using testing::Eq;
29 
30 struct AidlTest : testing::Test {
31   template <typename T>
getServiceAidlTest32   std::shared_ptr<T> getService() {
33     android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
34     android::ProcessState::self()->startThreadPool();
35     ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_waitForService(T::descriptor));
36     return T::fromBinder(binder);
37   }
SetUpAidlTest38   void SetUp() override {
39     service = getService<ITestService>();
40     auto status = service->getBackendType(&backend);
41     ASSERT_TRUE(status.isOk()) << status.getDescription();
42   }
43   std::shared_ptr<ITestService> service;
44   BackendType backend;
45 
46   template <typename T>
DoTestAidlTest47   void DoTest(ndk::ScopedAStatus (ITestService::*func)(const std::optional<T>&, std::optional<T>*),
48               std::optional<T> input) {
49     std::optional<T> output;
50     auto status = (*service.*func)(input, &output);
51     ASSERT_TRUE(status.isOk());
52     ASSERT_TRUE(output.has_value());
53     ASSERT_THAT(*output, Eq(*input));
54 
55     input.reset();
56     status = (*service.*func)(input, &output);
57     ASSERT_TRUE(status.isOk());
58     ASSERT_FALSE(output.has_value());
59   }
60 };
61 
TEST_F(AidlTest,parcelableArray)62 TEST_F(AidlTest, parcelableArray) {
63   std::vector<std::optional<ITestService::Empty>> input;
64   input.push_back(ITestService::Empty());
65   input.push_back(std::nullopt);
66   DoTest(&ITestService::RepeatNullableParcelableArray, std::make_optional(input));
67 }
68 
TEST_F(AidlTest,parcelableList)69 TEST_F(AidlTest, parcelableList) {
70   std::vector<std::optional<ITestService::Empty>> input;
71   input.push_back(ITestService::Empty());
72   input.push_back(std::nullopt);
73   DoTest(&ITestService::RepeatNullableParcelableList, std::make_optional(input));
74 }
75 
TEST_F(AidlTest,nullBinder)76 TEST_F(AidlTest, nullBinder) {
77   auto status = service->TakesAnIBinder(nullptr);
78   ASSERT_THAT(status.getStatus(), Eq(STATUS_UNEXPECTED_NULL)) << status.getDescription();
79   // Note that NDK backend checks null before transaction while C++ backends doesn't.
80 }
81 
TEST_F(AidlTest,binderListWithNull)82 TEST_F(AidlTest, binderListWithNull) {
83   std::vector<ndk::SpAIBinder> input{service->asBinder(), nullptr};
84   auto status = service->TakesAnIBinderList(input);
85   ASSERT_THAT(status.getStatus(), Eq(STATUS_UNEXPECTED_NULL));
86   // Note that NDK backend checks null before transaction while C++ backends doesn't.
87 }
88 
TEST_F(AidlTest,nonNullBinder)89 TEST_F(AidlTest, nonNullBinder) {
90   auto status = service->TakesAnIBinder(service->asBinder());
91   ASSERT_TRUE(status.isOk());
92 }
93 
TEST_F(AidlTest,binderListWithoutNull)94 TEST_F(AidlTest, binderListWithoutNull) {
95   std::vector<ndk::SpAIBinder> input{service->asBinder()};
96   auto status = service->TakesAnIBinderList(input);
97   ASSERT_TRUE(status.isOk());
98 }
99 
TEST_F(AidlTest,nullBinderToAnnotatedMethod)100 TEST_F(AidlTest, nullBinderToAnnotatedMethod) {
101   auto status = service->TakesANullableIBinder(nullptr);
102   ASSERT_TRUE(status.isOk());
103 }
104 
TEST_F(AidlTest,binderListWithNullToAnnotatedMethod)105 TEST_F(AidlTest, binderListWithNullToAnnotatedMethod) {
106   std::vector<ndk::SpAIBinder> input{service->asBinder(), nullptr};
107   auto status = service->TakesANullableIBinderList(input);
108   ASSERT_TRUE(status.isOk());
109 }
110 
TEST_F(AidlTest,binderArray)111 TEST_F(AidlTest, binderArray) {
112   std::vector<ndk::SpAIBinder> repeated;
113   if (backend == BackendType::JAVA) {
114     // Java can only modify out-argument arrays in-place
115     repeated.resize(2);
116   }
117   // get INamedCallback for "SpAIBinder" object
118   std::shared_ptr<INamedCallback> callback;
119   auto status = service->GetCallback(false, &callback);
120   ASSERT_TRUE(status.isOk()) << status.getDescription();
121 
122   std::vector<ndk::SpAIBinder> reversed;
123   std::vector<ndk::SpAIBinder> input{service->asBinder(), callback->asBinder()};
124   status = service->ReverseIBinderArray(input, &repeated, &reversed);
125   ASSERT_TRUE(status.isOk()) << status.getDescription();
126 
127   EXPECT_THAT(input, Eq(repeated));
128   std::reverse(std::begin(reversed), std::end(reversed));
129   EXPECT_THAT(input, Eq(reversed));
130 }
131 
TEST_F(AidlTest,nullableBinderArray)132 TEST_F(AidlTest, nullableBinderArray) {
133   std::optional<std::vector<ndk::SpAIBinder>> repeated;
134   if (backend == BackendType::JAVA) {
135     // Java can only modify out-argument arrays in-place
136     repeated = std::vector<ndk::SpAIBinder>(2);
137   }
138 
139   std::optional<std::vector<ndk::SpAIBinder>> reversed;
140   std::optional<std::vector<ndk::SpAIBinder>> input =
141       std::vector<ndk::SpAIBinder>{service->asBinder(), service->asBinder()};
142   auto status = service->ReverseNullableIBinderArray(input, &repeated, &reversed);
143   ASSERT_TRUE(status.isOk()) << status.getDescription();
144 
145   EXPECT_THAT(input, Eq(repeated));
146   ASSERT_TRUE(reversed);
147   std::reverse(std::begin(*reversed), std::end(*reversed));
148   EXPECT_THAT(input, Eq(reversed));
149 }
150