• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 <optional>
18 #include <vector>
19 
20 #include <utils/String16.h>
21 #include <utils/String8.h>
22 
23 #include "aidl_test_client.h"
24 #include "gmock/gmock.h"
25 
26 using android::BBinder;
27 using android::IBinder;
28 using android::sp;
29 using android::String16;
30 using android::String8;
31 using android::binder::Status;
32 
33 using android::aidl::tests::BackendType;
34 using android::aidl::tests::ByteEnum;
35 using android::aidl::tests::INamedCallback;
36 using android::aidl::tests::IntEnum;
37 using android::aidl::tests::ITestService;
38 using android::aidl::tests::LongEnum;
39 using android::aidl::tests::SimpleParcelable;
40 using android::aidl::tests::StructuredParcelable;
41 
42 using testing::Eq;
43 using testing::Ne;
44 
45 struct RepeatNullableTest : public AidlTest {
46   template <typename T>
DoTestRepeatNullableTest47   void DoTest(Status (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(RepeatNullableTest,intArray)62 TEST_F(RepeatNullableTest, intArray) {
63   DoTest(&ITestService::RepeatNullableIntArray, std::make_optional(std::vector<int32_t>{1, 2, 3}));
64 }
65 
TEST_F(RepeatNullableTest,byteEnumArray)66 TEST_F(RepeatNullableTest, byteEnumArray) {
67   DoTest(&ITestService::RepeatNullableByteEnumArray,
68          std::make_optional(std::vector<ByteEnum>{ByteEnum::FOO, ByteEnum::BAR}));
69 }
70 
TEST_F(RepeatNullableTest,intEnumArray)71 TEST_F(RepeatNullableTest, intEnumArray) {
72   DoTest(&ITestService::RepeatNullableIntEnumArray,
73          std::make_optional(std::vector<IntEnum>{IntEnum::FOO, IntEnum::BAR}));
74 }
75 
TEST_F(RepeatNullableTest,longEnumArray)76 TEST_F(RepeatNullableTest, longEnumArray) {
77   DoTest(&ITestService::RepeatNullableLongEnumArray,
78          std::make_optional(std::vector<LongEnum>{LongEnum::FOO, LongEnum::BAR}));
79 }
80 
TEST_F(RepeatNullableTest,string)81 TEST_F(RepeatNullableTest, string) {
82   DoTest(&ITestService::RepeatNullableString, std::optional<String16>("Blooob"));
83 }
84 
TEST_F(RepeatNullableTest,stringArray)85 TEST_F(RepeatNullableTest, stringArray) {
86   std::vector<std::optional<String16>> input;
87   input.push_back(String16("Wat"));
88   input.push_back(String16("Blooob"));
89   input.push_back(String16("Wat"));
90   input.push_back(std::nullopt);
91   input.push_back(String16("YEAH"));
92   input.push_back(String16("OKAAAAY"));
93 
94   DoTest(&ITestService::RepeatNullableStringList, std::make_optional(input));
95 }
96 
TEST_F(RepeatNullableTest,parcelable)97 TEST_F(RepeatNullableTest, parcelable) {
98   auto input = std::make_optional<ITestService::Empty>();
99   std::optional<ITestService::Empty> output;
100   auto status = service->RepeatNullableParcelable(input, &output);
101   ASSERT_TRUE(status.isOk());
102   ASSERT_TRUE(output.has_value());
103   ASSERT_THAT(*output, Eq(*input));
104 
105   input.reset();
106   status = service->RepeatNullableParcelable(input, &output);
107   ASSERT_TRUE(status.isOk());
108   ASSERT_FALSE(output.has_value());
109 }
110 
TEST_F(RepeatNullableTest,parcelableArray)111 TEST_F(RepeatNullableTest, parcelableArray) {
112   std::vector<std::optional<ITestService::Empty>> input;
113   input.push_back(ITestService::Empty());
114   input.push_back(std::nullopt);
115   DoTest(&ITestService::RepeatNullableParcelableArray, std::make_optional(input));
116 }
117 
TEST_F(RepeatNullableTest,parcelableList)118 TEST_F(RepeatNullableTest, parcelableList) {
119   std::vector<std::optional<ITestService::Empty>> input;
120   input.push_back(ITestService::Empty());
121   input.push_back(std::nullopt);
122   DoTest(&ITestService::RepeatNullableParcelableList, std::make_optional(input));
123 }
124 
TEST_F(AidlTest,nullBinder)125 TEST_F(AidlTest, nullBinder) {
126   auto status = service->TakesAnIBinder(nullptr);
127 
128   if (backend == BackendType::JAVA) {
129     ASSERT_TRUE(status.isOk()) << status;
130   } else {
131     ASSERT_THAT(status.exceptionCode(), Eq(android::binder::Status::EX_NULL_POINTER)) << status;
132   }
133 }
134 
TEST_F(AidlTest,binderListWithNull)135 TEST_F(AidlTest, binderListWithNull) {
136   std::vector<sp<IBinder>> input{new BBinder(), nullptr};
137   auto status = service->TakesAnIBinderList(input);
138 
139   if (backend == BackendType::JAVA) {
140     ASSERT_TRUE(status.isOk()) << status;
141   } else {
142     ASSERT_THAT(status.exceptionCode(), Eq(android::binder::Status::EX_NULL_POINTER));
143   }
144 }
145 
TEST_F(AidlTest,nonNullBinder)146 TEST_F(AidlTest, nonNullBinder) {
147   sp<IBinder> input = new BBinder();
148   auto status = service->TakesAnIBinder(input);
149   ASSERT_TRUE(status.isOk());
150 }
151 
TEST_F(AidlTest,binderListWithoutNull)152 TEST_F(AidlTest, binderListWithoutNull) {
153   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
154   auto status = service->TakesAnIBinderList(input);
155   ASSERT_TRUE(status.isOk());
156 }
157 
TEST_F(AidlTest,nullBinderToAnnotatedMethod)158 TEST_F(AidlTest, nullBinderToAnnotatedMethod) {
159   auto status = service->TakesANullableIBinder(nullptr);
160   ASSERT_TRUE(status.isOk());
161 }
162 
TEST_F(AidlTest,binderListWithNullToAnnotatedMethod)163 TEST_F(AidlTest, binderListWithNullToAnnotatedMethod) {
164   std::vector<sp<IBinder>> input{new BBinder(), nullptr};
165   auto status = service->TakesANullableIBinderList(input);
166   ASSERT_TRUE(status.isOk());
167 }
168 
TEST_F(AidlTest,binderArray)169 TEST_F(AidlTest, binderArray) {
170   std::vector<sp<IBinder>> repeated;
171   if (backend == BackendType::JAVA) {
172     // Java can only modify out-argument arrays in-place
173     repeated.resize(2);
174   }
175 
176   std::vector<sp<IBinder>> reversed;
177   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
178   auto status = service->ReverseIBinderArray(input, &repeated, &reversed);
179   ASSERT_TRUE(status.isOk()) << status;
180 
181   EXPECT_THAT(input, Eq(repeated));
182   std::reverse(std::begin(reversed), std::end(reversed));
183   EXPECT_THAT(input, Eq(reversed));
184 }
185 
TEST_F(AidlTest,nullableBinderArray)186 TEST_F(AidlTest, nullableBinderArray) {
187   std::optional<std::vector<sp<IBinder>>> repeated;
188   if (backend == BackendType::JAVA) {
189     // Java can only modify out-argument arrays in-place
190     repeated = std::vector<sp<IBinder>>();
191     repeated->resize(2);
192   }
193 
194   std::optional<std::vector<sp<IBinder>>> reversed;
195   std::optional<std::vector<sp<IBinder>>> input = std::vector<sp<IBinder>>{new BBinder(), nullptr};
196   auto status = service->ReverseNullableIBinderArray(input, &repeated, &reversed);
197   ASSERT_TRUE(status.isOk()) << status;
198 
199   EXPECT_THAT(input, Eq(repeated));
200   ASSERT_TRUE(reversed);
201   std::reverse(std::begin(*reversed), std::end(*reversed));
202   EXPECT_THAT(input, Eq(reversed));
203 }
204 
TEST_F(AidlTest,nonNullBinderToAnnotatedMethod)205 TEST_F(AidlTest, nonNullBinderToAnnotatedMethod) {
206   sp<IBinder> input = new BBinder();
207   auto status = service->TakesANullableIBinder(input);
208   ASSERT_TRUE(status.isOk());
209 }
210 
TEST_F(AidlTest,binderListWithoutNullToAnnotatedMethod)211 TEST_F(AidlTest, binderListWithoutNullToAnnotatedMethod) {
212   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
213   auto status = service->TakesANullableIBinderList(input);
214   ASSERT_TRUE(status.isOk());
215 }
216 
TEST_F(AidlTest,interface)217 TEST_F(AidlTest, interface) {
218   sp<INamedCallback> callback;
219   auto status = service->GetCallback(false, &callback);
220   ASSERT_TRUE(status.isOk());
221   ASSERT_THAT(callback.get(), Ne(nullptr));
222 }
223 
TEST_F(AidlTest,nullInterface)224 TEST_F(AidlTest, nullInterface) {
225   sp<INamedCallback> callback;
226   auto status = service->GetCallback(true, &callback);
227   ASSERT_TRUE(status.isOk());
228   ASSERT_THAT(callback.get(), Eq(nullptr));
229 }
230