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_TEST_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::TEST_CONSTANT, ITestService::TEST_CONSTANT2, ITestService::TEST_CONSTANT3,
108 ITestService::TEST_CONSTANT4, ITestService::TEST_CONSTANT5, ITestService::TEST_CONSTANT6,
109 ITestService::TEST_CONSTANT7, ITestService::TEST_CONSTANT8, ITestService::TEST_CONSTANT9,
110 ITestService::TEST_CONSTANT10, ITestService::TEST_CONSTANT11, ITestService::TEST_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_TEST_CONSTANT};
118 for (auto sent : consts) {
119 DoTest(&ITestService::RepeatLong, sent);
120 }
121 }
122
TEST_F(AidlPrimitiveTest,strings)123 TEST_F(AidlPrimitiveTest, strings) {
124 std::vector<String16> strings = {
125 String16("Deliver us from evil."), String16(), String16("\0\0", 2),
126 // This is actually two unicode code points:
127 // U+10437: The 'small letter yee' character in the deseret alphabet
128 // U+20AC: A euro sign
129 String16("\xD8\x01\xDC\x37\x20\xAC"), ITestService::STRING_TEST_CONSTANT(),
130 ITestService::STRING_TEST_CONSTANT2()};
131 for (auto sent : strings) {
132 DoTest(&ITestService::RepeatString, sent);
133 }
134 }
135
TEST_F(AidlPrimitiveTest,booleanArray)136 TEST_F(AidlPrimitiveTest, booleanArray) {
137 DoTest(&ITestService::ReverseBoolean, {true, false, false});
138 }
139
TEST_F(AidlPrimitiveTest,byteArrvay)140 TEST_F(AidlPrimitiveTest, byteArrvay) {
141 DoTest(&ITestService::ReverseByte, {uint8_t{255}, uint8_t{0}, uint8_t{127}});
142 }
143
TEST_F(AidlPrimitiveTest,charArray)144 TEST_F(AidlPrimitiveTest, charArray) {
145 DoTest(&ITestService::ReverseChar, {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}});
146 }
147
TEST_F(AidlPrimitiveTest,intArray)148 TEST_F(AidlPrimitiveTest, intArray) {
149 DoTest(&ITestService::ReverseInt, {1, 2, 3});
150 }
151
TEST_F(AidlPrimitiveTest,longArrayr)152 TEST_F(AidlPrimitiveTest, longArrayr) {
153 DoTest(&ITestService::ReverseLong, {-1LL, 0LL, int64_t{1LL << 60}});
154 }
155
TEST_F(AidlPrimitiveTest,floatArrays)156 TEST_F(AidlPrimitiveTest, floatArrays) {
157 DoTest(&ITestService::ReverseFloat, {-0.3f, -0.7f, 8.0f});
158 }
159
TEST_F(AidlPrimitiveTest,doubleArray)160 TEST_F(AidlPrimitiveTest, doubleArray) {
161 DoTest(&ITestService::ReverseDouble, {1.0 / 3.0, 1.0 / 7.0, 42.0});
162 }
163
TEST_F(AidlPrimitiveTest,stringArray)164 TEST_F(AidlPrimitiveTest, stringArray) {
165 DoTest(&ITestService::ReverseString, {String16{"f"}, String16{"a"}, String16{"b"}});
166 }
167
TEST_F(AidlPrimitiveTest,byteEnumArray)168 TEST_F(AidlPrimitiveTest, byteEnumArray) {
169 DoTest(&ITestService::ReverseByteEnum, {ByteEnum::FOO, ByteEnum::BAR, ByteEnum::BAR});
170 }
171
TEST_F(AidlPrimitiveTest,byteEnumArray2)172 TEST_F(AidlPrimitiveTest, byteEnumArray2) {
173 DoTest(&ITestService::ReverseByteEnum, {std::begin(::android::enum_range<ByteEnum>()),
174 std::end(::android::enum_range<ByteEnum>())});
175 }
176
TEST_F(AidlPrimitiveTest,intEnumArray)177 TEST_F(AidlPrimitiveTest, intEnumArray) {
178 DoTest(&ITestService::ReverseIntEnum, {IntEnum::FOO, IntEnum::BAR, IntEnum::BAR});
179 }
180
TEST_F(AidlPrimitiveTest,longEnumArray)181 TEST_F(AidlPrimitiveTest, longEnumArray) {
182 DoTest(&ITestService::ReverseLongEnum, {LongEnum::FOO, LongEnum::BAR, LongEnum::BAR});
183 }
184
TEST_F(AidlPrimitiveTest,stringList)185 TEST_F(AidlPrimitiveTest, stringList) {
186 DoTest(&ITestService::ReverseStringList, {String16{"f"}, String16{"a"}, String16{"b"}});
187 }
188
TEST_F(AidlPrimitiveTest,binderArray)189 TEST_F(AidlPrimitiveTest, binderArray) {
190 std::vector<String16> names = {String16{"Larry"}, String16{"Curly"}, String16{"Moe"}};
191
192 std::vector<sp<IBinder>> input;
193 for (int i = 0; i < 3; i++) {
194 sp<INamedCallback> got;
195 auto status = service->GetOtherTestService(names[i], &got);
196 ASSERT_TRUE(status.isOk());
197 input.push_back(INamedCallback::asBinder(got));
198 }
199 {
200 std::vector<sp<INamedCallback>> got;
201 auto status = service->GetInterfaceArray(names, &got);
202 ASSERT_TRUE(status.isOk());
203 bool verified = false;
204 status = service->VerifyNamesWithInterfaceArray(got, names, &verified);
205 ASSERT_TRUE(status.isOk());
206 ASSERT_TRUE(verified);
207 for (int i = 0; i < 3; i++) {
208 String16 name;
209 ASSERT_TRUE(got[i]->GetName(&name).isOk());
210 ASSERT_THAT(name, Eq(names[i]));
211 }
212 }
213 {
214 std::vector<std::optional<String16>> names = {String16{"Larry"}, std::nullopt, String16{"Moe"}};
215 std::optional<std::vector<sp<INamedCallback>>> got;
216 auto status = service->GetNullableInterfaceArray(names, &got);
217 ASSERT_TRUE(status.isOk());
218 bool verified = false;
219 status = service->VerifyNamesWithNullableInterfaceArray(got, names, &verified);
220 ASSERT_TRUE(status.isOk());
221 ASSERT_TRUE(verified);
222 ASSERT_TRUE(got.has_value());
223 for (int i = 0; i < 3; i++) {
224 if (names[i].has_value()) {
225 ASSERT_NE(got->at(i).get(), nullptr);
226 String16 name;
227 ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
228 ASSERT_THAT(name, Eq(names[i].value()));
229 } else {
230 ASSERT_EQ(got->at(i).get(), nullptr);
231 }
232 }
233 }
234 {
235 std::vector<std::optional<String16>> names = {String16{"Larry"}, std::nullopt, String16{"Moe"}};
236 std::optional<std::vector<sp<INamedCallback>>> got;
237 auto status = service->GetInterfaceList(names, &got);
238 ASSERT_TRUE(status.isOk());
239 bool verified = false;
240 status = service->VerifyNamesWithInterfaceList(got, names, &verified);
241 ASSERT_TRUE(status.isOk());
242 ASSERT_TRUE(verified);
243 ASSERT_TRUE(got.has_value());
244 for (int i = 0; i < 3; i++) {
245 if (names[i].has_value()) {
246 ASSERT_NE(got->at(i).get(), nullptr);
247 String16 name;
248 ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
249 ASSERT_THAT(name, Eq(names[i].value()));
250 } else {
251 ASSERT_EQ(got->at(i).get(), nullptr);
252 }
253 }
254 }
255 if (cpp_java_tests) {
256 std::vector<sp<IBinder>> output;
257 std::vector<sp<IBinder>> reversed;
258 auto status = cpp_java_tests->ReverseNamedCallbackList(input, &output, &reversed);
259 ASSERT_TRUE(status.isOk());
260 ASSERT_THAT(output.size(), Eq(3u));
261 ASSERT_THAT(reversed.size(), Eq(3u));
262
263 for (int i = 0; i < 3; i++) {
264 String16 ret;
265 sp<INamedCallback> named_callback = android::interface_cast<INamedCallback>(output[i]);
266 auto status = named_callback->GetName(&ret);
267 ASSERT_TRUE(status.isOk());
268 ASSERT_THAT(ret, Eq(names[i]));
269 }
270
271 for (int i = 0; i < 3; i++) {
272 String16 ret;
273 sp<INamedCallback> named_callback = android::interface_cast<INamedCallback>(reversed[i]);
274 auto status = named_callback->GetName(&ret);
275 ASSERT_TRUE(status.isOk());
276 ASSERT_THAT(ret, Eq(names[2 - i]));
277 }
278 }
279 }
280
TEST_F(AidlPrimitiveTest,constantExpressions)281 TEST_F(AidlPrimitiveTest, constantExpressions) {
282 EXPECT_THAT(ITestService::A1, Eq(1));
283 EXPECT_THAT(ITestService::A2, Eq(1));
284 EXPECT_THAT(ITestService::A3, Eq(1));
285 EXPECT_THAT(ITestService::A4, Eq(1));
286 EXPECT_THAT(ITestService::A5, Eq(1));
287 EXPECT_THAT(ITestService::A6, Eq(1));
288 EXPECT_THAT(ITestService::A7, Eq(1));
289 EXPECT_THAT(ITestService::A8, Eq(1));
290 EXPECT_THAT(ITestService::A9, Eq(1));
291 EXPECT_THAT(ITestService::A10, Eq(1));
292 EXPECT_THAT(ITestService::A11, Eq(1));
293 EXPECT_THAT(ITestService::A12, Eq(1));
294 EXPECT_THAT(ITestService::A13, Eq(1));
295 EXPECT_THAT(ITestService::A14, Eq(1));
296 EXPECT_THAT(ITestService::A15, Eq(1));
297 EXPECT_THAT(ITestService::A16, Eq(1));
298 EXPECT_THAT(ITestService::A17, Eq(1));
299 EXPECT_THAT(ITestService::A18, Eq(1));
300 EXPECT_THAT(ITestService::A19, Eq(1));
301 EXPECT_THAT(ITestService::A20, Eq(1));
302 EXPECT_THAT(ITestService::A21, Eq(1));
303 EXPECT_THAT(ITestService::A22, Eq(1));
304 EXPECT_THAT(ITestService::A23, Eq(1));
305 EXPECT_THAT(ITestService::A24, Eq(1));
306 EXPECT_THAT(ITestService::A25, Eq(1));
307 EXPECT_THAT(ITestService::A26, Eq(1));
308 EXPECT_THAT(ITestService::A27, Eq(1));
309 EXPECT_THAT(ITestService::A28, Eq(1));
310 EXPECT_THAT(ITestService::A29, Eq(1));
311 EXPECT_THAT(ITestService::A30, Eq(1));
312 EXPECT_THAT(ITestService::A31, Eq(1));
313 EXPECT_THAT(ITestService::A32, Eq(1));
314 EXPECT_THAT(ITestService::A33, Eq(1));
315 EXPECT_THAT(ITestService::A34, Eq(1));
316 EXPECT_THAT(ITestService::A35, Eq(1));
317 EXPECT_THAT(ITestService::A36, Eq(1));
318 EXPECT_THAT(ITestService::A37, Eq(1));
319 EXPECT_THAT(ITestService::A38, Eq(1));
320 EXPECT_THAT(ITestService::A39, Eq(1));
321 EXPECT_THAT(ITestService::A40, Eq(1));
322 EXPECT_THAT(ITestService::A41, Eq(1));
323 EXPECT_THAT(ITestService::A42, Eq(1));
324 EXPECT_THAT(ITestService::A43, Eq(1));
325 EXPECT_THAT(ITestService::A44, Eq(1));
326 EXPECT_THAT(ITestService::A45, Eq(1));
327 EXPECT_THAT(ITestService::A46, Eq(1));
328 EXPECT_THAT(ITestService::A47, Eq(1));
329 EXPECT_THAT(ITestService::A48, Eq(1));
330 EXPECT_THAT(ITestService::A49, Eq(1));
331 EXPECT_THAT(ITestService::A50, Eq(1));
332 EXPECT_THAT(ITestService::A51, Eq(1));
333 EXPECT_THAT(ITestService::A52, Eq(1));
334 EXPECT_THAT(ITestService::A53, Eq(1));
335 EXPECT_THAT(ITestService::A54, Eq(1));
336 EXPECT_THAT(ITestService::A55, Eq(1));
337 EXPECT_THAT(ITestService::A56, Eq(1));
338 EXPECT_THAT(ITestService::A57, Eq(1));
339 }
340