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