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 <vector>
18
19 #include <utils/String16.h>
20 #include <utils/String8.h>
21
22 #include "android/aidl/tests/ByteEnum.h"
23 #include "android/aidl/tests/INamedCallback.h"
24 #include "android/aidl/tests/IntEnum.h"
25 #include "android/aidl/tests/LongEnum.h"
26
27 #include "aidl_test_client.h"
28 #include "gmock/gmock.h"
29
30 using android::IBinder;
31 using android::sp;
32 using android::String16;
33 using android::String8;
34 using android::binder::Status;
35
36 // generated
37 using android::aidl::tests::ByteEnum;
38 using android::aidl::tests::INamedCallback;
39 using android::aidl::tests::IntEnum;
40 using android::aidl::tests::ITestService;
41 using android::aidl::tests::LongEnum;
42
43 using testing::Eq;
44
45 struct AidlPrimitiveTest : public AidlTest {
46 template <typename T, typename U, typename V>
DoTestAidlPrimitiveTest47 void DoTest(Status (ITestService::*func)(T, U*), V input) {
48 U repeated;
49 auto status = (*service.*func)(input, &repeated);
50 ASSERT_TRUE(status.isOk()) << status;
51 ASSERT_THAT(repeated, Eq(input));
52 }
53
54 template <typename T>
DoTestAidlPrimitiveTest55 void DoTest(Status (ITestService::*func)(const std::vector<T>&, std::vector<T>*, std::vector<T>*),
56 const std::vector<T>& input) {
57 // must be preallocated for Java servers
58 std::vector<T> repeated(input.size());
59 std::vector<T> reversed;
60 auto status = (*service.*func)(input, &repeated, &reversed);
61 ASSERT_TRUE(status.isOk()) << status;
62 ASSERT_THAT(repeated, Eq(input));
63
64 std::vector<T> reversed_input(input);
65 std::reverse(reversed_input.begin(), reversed_input.end());
66 ASSERT_THAT(reversed, Eq(reversed_input));
67 }
68 };
69
TEST_F(AidlPrimitiveTest,aBoolean)70 TEST_F(AidlPrimitiveTest, aBoolean) {
71 DoTest(&ITestService::RepeatBoolean, true);
72 }
73
TEST_F(AidlPrimitiveTest,aByte)74 TEST_F(AidlPrimitiveTest, aByte) {
75 DoTest(&ITestService::RepeatByte, int8_t{-128});
76 }
77
TEST_F(AidlPrimitiveTest,aChar)78 TEST_F(AidlPrimitiveTest, aChar) {
79 DoTest(&ITestService::RepeatChar, char16_t{'A'});
80 }
81
TEST_F(AidlPrimitiveTest,aInt)82 TEST_F(AidlPrimitiveTest, aInt) {
83 DoTest(&ITestService::RepeatInt, int32_t{1 << 30});
84 }
85
TEST_F(AidlPrimitiveTest,aLong)86 TEST_F(AidlPrimitiveTest, aLong) {
87 DoTest(&ITestService::RepeatLong, int64_t{1LL << 60});
88 }
89
TEST_F(AidlPrimitiveTest,aFloat)90 TEST_F(AidlPrimitiveTest, aFloat) {
91 DoTest(&ITestService::RepeatFloat, float{1.0f / 3.0f});
92 }
93
TEST_F(AidlPrimitiveTest,aDouble)94 TEST_F(AidlPrimitiveTest, aDouble) {
95 DoTest(&ITestService::RepeatDouble, double{1.0 / 3.0});
96 }
97
TEST_F(AidlPrimitiveTest,byteConstants)98 TEST_F(AidlPrimitiveTest, byteConstants) {
99 constexpr int8_t consts[] = {ITestService::BYTE_CONSTANT};
100 for (auto sent : consts) {
101 DoTest(&ITestService::RepeatByte, sent);
102 }
103 }
104
TEST_F(AidlPrimitiveTest,intConstants)105 TEST_F(AidlPrimitiveTest, intConstants) {
106 constexpr int32_t consts[] = {
107 ITestService::CONSTANT, ITestService::CONSTANT2, ITestService::CONSTANT3,
108 ITestService::CONSTANT4, ITestService::CONSTANT5, ITestService::CONSTANT6,
109 ITestService::CONSTANT7, ITestService::CONSTANT8, ITestService::CONSTANT9,
110 ITestService::CONSTANT10, ITestService::CONSTANT11, ITestService::CONSTANT12};
111 for (auto sent : consts) {
112 DoTest(&ITestService::RepeatInt, sent);
113 }
114 }
115
TEST_F(AidlPrimitiveTest,longConstants)116 TEST_F(AidlPrimitiveTest, longConstants) {
117 constexpr int64_t consts[] = {ITestService::LONG_CONSTANT};
118 for (auto sent : consts) {
119 DoTest(&ITestService::RepeatLong, sent);
120 }
121 }
122
TEST_F(AidlPrimitiveTest,floatConstants)123 TEST_F(AidlPrimitiveTest, floatConstants) {
124 constexpr float consts[] = {
125 ITestService::FLOAT_CONSTANT, ITestService::FLOAT_CONSTANT2, ITestService::FLOAT_CONSTANT3,
126 ITestService::FLOAT_CONSTANT4, ITestService::FLOAT_CONSTANT5, ITestService::FLOAT_CONSTANT6,
127 ITestService::FLOAT_CONSTANT7,
128 };
129 for (auto sent : consts) {
130 DoTest(&ITestService::RepeatFloat, sent);
131 }
132 }
133
TEST_F(AidlPrimitiveTest,doubleConstants)134 TEST_F(AidlPrimitiveTest, doubleConstants) {
135 constexpr double consts[] = {
136 ITestService::DOUBLE_CONSTANT, ITestService::DOUBLE_CONSTANT2,
137 ITestService::DOUBLE_CONSTANT3, ITestService::DOUBLE_CONSTANT4,
138 ITestService::DOUBLE_CONSTANT5, ITestService::DOUBLE_CONSTANT6,
139 ITestService::DOUBLE_CONSTANT7, ITestService::DOUBLE_CONSTANT8,
140 ITestService::DOUBLE_CONSTANT9,
141 };
142 for (auto sent : consts) {
143 DoTest(&ITestService::RepeatDouble, sent);
144 }
145 }
146
TEST_F(AidlPrimitiveTest,strings)147 TEST_F(AidlPrimitiveTest, strings) {
148 std::vector<String16> strings = {
149 String16("Deliver us from evil."), String16(), String16("\0\0", 2),
150 // This is actually two unicode code points:
151 // U+10437: The 'small letter yee' character in the deseret alphabet
152 // U+20AC: A euro sign
153 String16("\xD8\x01\xDC\x37\x20\xAC"), ITestService::STRING_CONSTANT(),
154 ITestService::STRING_CONSTANT2()};
155 for (auto sent : strings) {
156 DoTest(&ITestService::RepeatString, sent);
157 }
158 }
159
TEST_F(AidlPrimitiveTest,booleanArray)160 TEST_F(AidlPrimitiveTest, booleanArray) {
161 DoTest(&ITestService::ReverseBoolean, {true, false, false});
162 }
163
TEST_F(AidlPrimitiveTest,byteArrvay)164 TEST_F(AidlPrimitiveTest, byteArrvay) {
165 DoTest(&ITestService::ReverseByte, {uint8_t{255}, uint8_t{0}, uint8_t{127}});
166 }
167
TEST_F(AidlPrimitiveTest,charArray)168 TEST_F(AidlPrimitiveTest, charArray) {
169 DoTest(&ITestService::ReverseChar, {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}});
170 }
171
TEST_F(AidlPrimitiveTest,intArray)172 TEST_F(AidlPrimitiveTest, intArray) {
173 DoTest(&ITestService::ReverseInt, {1, 2, 3});
174 }
175
TEST_F(AidlPrimitiveTest,longArrayr)176 TEST_F(AidlPrimitiveTest, longArrayr) {
177 DoTest(&ITestService::ReverseLong, {-1LL, 0LL, int64_t{1LL << 60}});
178 }
179
TEST_F(AidlPrimitiveTest,floatArrays)180 TEST_F(AidlPrimitiveTest, floatArrays) {
181 DoTest(&ITestService::ReverseFloat, {-0.3f, -0.7f, 8.0f});
182 }
183
TEST_F(AidlPrimitiveTest,doubleArray)184 TEST_F(AidlPrimitiveTest, doubleArray) {
185 DoTest(&ITestService::ReverseDouble, {1.0 / 3.0, 1.0 / 7.0, 42.0});
186 }
187
TEST_F(AidlPrimitiveTest,stringArray)188 TEST_F(AidlPrimitiveTest, stringArray) {
189 DoTest(&ITestService::ReverseString, {String16{"f"}, String16{"a"}, String16{"b"}});
190 }
191
TEST_F(AidlPrimitiveTest,byteEnumArray)192 TEST_F(AidlPrimitiveTest, byteEnumArray) {
193 DoTest(&ITestService::ReverseByteEnum, {ByteEnum::FOO, ByteEnum::BAR, ByteEnum::BAR});
194 }
195
TEST_F(AidlPrimitiveTest,byteEnumArray2)196 TEST_F(AidlPrimitiveTest, byteEnumArray2) {
197 DoTest(&ITestService::ReverseByteEnum, {std::begin(::android::enum_range<ByteEnum>()),
198 std::end(::android::enum_range<ByteEnum>())});
199 }
200
TEST_F(AidlPrimitiveTest,intEnumArray)201 TEST_F(AidlPrimitiveTest, intEnumArray) {
202 DoTest(&ITestService::ReverseIntEnum, {IntEnum::FOO, IntEnum::BAR, IntEnum::BAR});
203 }
204
TEST_F(AidlPrimitiveTest,longEnumArray)205 TEST_F(AidlPrimitiveTest, longEnumArray) {
206 DoTest(&ITestService::ReverseLongEnum, {LongEnum::FOO, LongEnum::BAR, LongEnum::BAR});
207 }
208
TEST_F(AidlPrimitiveTest,stringList)209 TEST_F(AidlPrimitiveTest, stringList) {
210 DoTest(&ITestService::ReverseStringList, {String16{"f"}, String16{"a"}, String16{"b"}});
211 }
212
TEST_F(AidlPrimitiveTest,binderArray)213 TEST_F(AidlPrimitiveTest, binderArray) {
214 std::vector<String16> names = {String16{"Larry"}, String16{"Curly"}, String16{"Moe"}};
215
216 std::vector<sp<IBinder>> input;
217 for (int i = 0; i < 3; i++) {
218 sp<INamedCallback> got;
219 auto status = service->GetOtherTestService(names[i], &got);
220 ASSERT_TRUE(status.isOk());
221 input.push_back(INamedCallback::asBinder(got));
222 }
223 {
224 std::vector<sp<INamedCallback>> got;
225 auto status = service->GetInterfaceArray(names, &got);
226 ASSERT_TRUE(status.isOk());
227 bool verified = false;
228 status = service->VerifyNamesWithInterfaceArray(got, names, &verified);
229 ASSERT_TRUE(status.isOk());
230 ASSERT_TRUE(verified);
231 for (int i = 0; i < 3; i++) {
232 String16 name;
233 ASSERT_TRUE(got[i]->GetName(&name).isOk());
234 ASSERT_THAT(name, Eq(names[i]));
235 }
236 }
237 {
238 std::vector<std::optional<String16>> names = {String16{"Larry"}, std::nullopt, String16{"Moe"}};
239 std::optional<std::vector<sp<INamedCallback>>> got;
240 auto status = service->GetNullableInterfaceArray(names, &got);
241 ASSERT_TRUE(status.isOk());
242 bool verified = false;
243 status = service->VerifyNamesWithNullableInterfaceArray(got, names, &verified);
244 ASSERT_TRUE(status.isOk());
245 ASSERT_TRUE(verified);
246 ASSERT_TRUE(got.has_value());
247 for (int i = 0; i < 3; i++) {
248 if (names[i].has_value()) {
249 ASSERT_NE(got->at(i).get(), nullptr);
250 String16 name;
251 ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
252 ASSERT_THAT(name, Eq(names[i].value()));
253 } else {
254 ASSERT_EQ(got->at(i).get(), nullptr);
255 }
256 }
257 }
258 {
259 std::vector<std::optional<String16>> names = {String16{"Larry"}, std::nullopt, String16{"Moe"}};
260 std::optional<std::vector<sp<INamedCallback>>> got;
261 auto status = service->GetInterfaceList(names, &got);
262 ASSERT_TRUE(status.isOk());
263 bool verified = false;
264 status = service->VerifyNamesWithInterfaceList(got, names, &verified);
265 ASSERT_TRUE(status.isOk());
266 ASSERT_TRUE(verified);
267 ASSERT_TRUE(got.has_value());
268 for (int i = 0; i < 3; i++) {
269 if (names[i].has_value()) {
270 ASSERT_NE(got->at(i).get(), nullptr);
271 String16 name;
272 ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
273 ASSERT_THAT(name, Eq(names[i].value()));
274 } else {
275 ASSERT_EQ(got->at(i).get(), nullptr);
276 }
277 }
278 }
279 if (cpp_java_tests) {
280 std::vector<sp<IBinder>> output;
281 std::vector<sp<IBinder>> reversed;
282 auto status = cpp_java_tests->ReverseNamedCallbackList(input, &output, &reversed);
283 ASSERT_TRUE(status.isOk());
284 ASSERT_THAT(output.size(), Eq(3u));
285 ASSERT_THAT(reversed.size(), Eq(3u));
286
287 for (int i = 0; i < 3; i++) {
288 String16 ret;
289 sp<INamedCallback> named_callback = android::interface_cast<INamedCallback>(output[i]);
290 auto status = named_callback->GetName(&ret);
291 ASSERT_TRUE(status.isOk());
292 ASSERT_THAT(ret, Eq(names[i]));
293 }
294
295 for (int i = 0; i < 3; i++) {
296 String16 ret;
297 sp<INamedCallback> named_callback = android::interface_cast<INamedCallback>(reversed[i]);
298 auto status = named_callback->GetName(&ret);
299 ASSERT_TRUE(status.isOk());
300 ASSERT_THAT(ret, Eq(names[2 - i]));
301 }
302 }
303 }
304
TEST_F(AidlPrimitiveTest,constantExpressions)305 TEST_F(AidlPrimitiveTest, constantExpressions) {
306 EXPECT_THAT(ITestService::A1, Eq(1));
307 EXPECT_THAT(ITestService::A2, Eq(1));
308 EXPECT_THAT(ITestService::A3, Eq(1));
309 EXPECT_THAT(ITestService::A4, Eq(1));
310 EXPECT_THAT(ITestService::A5, Eq(1));
311 EXPECT_THAT(ITestService::A6, Eq(1));
312 EXPECT_THAT(ITestService::A7, Eq(1));
313 EXPECT_THAT(ITestService::A8, Eq(1));
314 EXPECT_THAT(ITestService::A9, Eq(1));
315 EXPECT_THAT(ITestService::A10, Eq(1));
316 EXPECT_THAT(ITestService::A11, Eq(1));
317 EXPECT_THAT(ITestService::A12, Eq(1));
318 EXPECT_THAT(ITestService::A13, Eq(1));
319 EXPECT_THAT(ITestService::A14, Eq(1));
320 EXPECT_THAT(ITestService::A15, Eq(1));
321 EXPECT_THAT(ITestService::A16, Eq(1));
322 EXPECT_THAT(ITestService::A17, Eq(1));
323 EXPECT_THAT(ITestService::A18, Eq(1));
324 EXPECT_THAT(ITestService::A19, Eq(1));
325 EXPECT_THAT(ITestService::A20, Eq(1));
326 EXPECT_THAT(ITestService::A21, Eq(1));
327 EXPECT_THAT(ITestService::A22, Eq(1));
328 EXPECT_THAT(ITestService::A23, Eq(1));
329 EXPECT_THAT(ITestService::A24, Eq(1));
330 EXPECT_THAT(ITestService::A25, Eq(1));
331 EXPECT_THAT(ITestService::A26, Eq(1));
332 EXPECT_THAT(ITestService::A27, Eq(1));
333 EXPECT_THAT(ITestService::A28, Eq(1));
334 EXPECT_THAT(ITestService::A29, Eq(1));
335 EXPECT_THAT(ITestService::A30, Eq(1));
336 EXPECT_THAT(ITestService::A31, Eq(1));
337 EXPECT_THAT(ITestService::A32, Eq(1));
338 EXPECT_THAT(ITestService::A33, Eq(1));
339 EXPECT_THAT(ITestService::A34, Eq(1));
340 EXPECT_THAT(ITestService::A35, Eq(1));
341 EXPECT_THAT(ITestService::A36, Eq(1));
342 EXPECT_THAT(ITestService::A37, Eq(1));
343 EXPECT_THAT(ITestService::A38, Eq(1));
344 EXPECT_THAT(ITestService::A39, Eq(1));
345 EXPECT_THAT(ITestService::A40, Eq(1));
346 EXPECT_THAT(ITestService::A41, Eq(1));
347 EXPECT_THAT(ITestService::A42, Eq(1));
348 EXPECT_THAT(ITestService::A43, Eq(1));
349 EXPECT_THAT(ITestService::A44, Eq(1));
350 EXPECT_THAT(ITestService::A45, Eq(1));
351 EXPECT_THAT(ITestService::A46, Eq(1));
352 EXPECT_THAT(ITestService::A47, Eq(1));
353 EXPECT_THAT(ITestService::A48, Eq(1));
354 EXPECT_THAT(ITestService::A49, Eq(1));
355 EXPECT_THAT(ITestService::A50, Eq(1));
356 EXPECT_THAT(ITestService::A51, Eq(1));
357 EXPECT_THAT(ITestService::A52, Eq(1));
358 EXPECT_THAT(ITestService::A53, Eq(1));
359 EXPECT_THAT(ITestService::A54, Eq(1));
360 EXPECT_THAT(ITestService::A55, Eq(1));
361 EXPECT_THAT(ITestService::A56, Eq(1));
362 EXPECT_THAT(ITestService::A57, Eq(1));
363 EXPECT_THAT(ITestService::FLOAT_CONSTANT4, Eq(2.2f));
364 EXPECT_THAT(ITestService::FLOAT_CONSTANT5, Eq(-2.2f));
365 EXPECT_THAT(ITestService::DOUBLE_CONSTANT4, Eq(2.2));
366 EXPECT_THAT(ITestService::DOUBLE_CONSTANT5, Eq(-2.2));
367 }
368