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 if (cpp_java_tests) {
201 std::vector<sp<IBinder>> output;
202 std::vector<sp<IBinder>> reversed;
203 auto status = cpp_java_tests->ReverseNamedCallbackList(input, &output, &reversed);
204 ASSERT_TRUE(status.isOk());
205 ASSERT_THAT(output.size(), Eq(3u));
206 ASSERT_THAT(reversed.size(), Eq(3u));
207
208 for (int i = 0; i < 3; i++) {
209 String16 ret;
210 sp<INamedCallback> named_callback = android::interface_cast<INamedCallback>(output[i]);
211 auto status = named_callback->GetName(&ret);
212 ASSERT_TRUE(status.isOk());
213 ASSERT_THAT(ret, Eq(names[i]));
214 }
215
216 for (int i = 0; i < 3; i++) {
217 String16 ret;
218 sp<INamedCallback> named_callback = android::interface_cast<INamedCallback>(reversed[i]);
219 auto status = named_callback->GetName(&ret);
220 ASSERT_TRUE(status.isOk());
221 ASSERT_THAT(ret, Eq(names[2 - i]));
222 }
223 }
224 }
225
TEST_F(AidlPrimitiveTest,constantExpressions)226 TEST_F(AidlPrimitiveTest, constantExpressions) {
227 EXPECT_THAT(ITestService::A1, Eq(1));
228 EXPECT_THAT(ITestService::A2, Eq(1));
229 EXPECT_THAT(ITestService::A3, Eq(1));
230 EXPECT_THAT(ITestService::A4, Eq(1));
231 EXPECT_THAT(ITestService::A5, Eq(1));
232 EXPECT_THAT(ITestService::A6, Eq(1));
233 EXPECT_THAT(ITestService::A7, Eq(1));
234 EXPECT_THAT(ITestService::A8, Eq(1));
235 EXPECT_THAT(ITestService::A9, Eq(1));
236 EXPECT_THAT(ITestService::A10, Eq(1));
237 EXPECT_THAT(ITestService::A11, Eq(1));
238 EXPECT_THAT(ITestService::A12, Eq(1));
239 EXPECT_THAT(ITestService::A13, Eq(1));
240 EXPECT_THAT(ITestService::A14, Eq(1));
241 EXPECT_THAT(ITestService::A15, Eq(1));
242 EXPECT_THAT(ITestService::A16, Eq(1));
243 EXPECT_THAT(ITestService::A17, Eq(1));
244 EXPECT_THAT(ITestService::A18, Eq(1));
245 EXPECT_THAT(ITestService::A19, Eq(1));
246 EXPECT_THAT(ITestService::A20, Eq(1));
247 EXPECT_THAT(ITestService::A21, Eq(1));
248 EXPECT_THAT(ITestService::A22, Eq(1));
249 EXPECT_THAT(ITestService::A23, Eq(1));
250 EXPECT_THAT(ITestService::A24, Eq(1));
251 EXPECT_THAT(ITestService::A25, Eq(1));
252 EXPECT_THAT(ITestService::A26, Eq(1));
253 EXPECT_THAT(ITestService::A27, Eq(1));
254 EXPECT_THAT(ITestService::A28, Eq(1));
255 EXPECT_THAT(ITestService::A29, Eq(1));
256 EXPECT_THAT(ITestService::A30, Eq(1));
257 EXPECT_THAT(ITestService::A31, Eq(1));
258 EXPECT_THAT(ITestService::A32, Eq(1));
259 EXPECT_THAT(ITestService::A33, Eq(1));
260 EXPECT_THAT(ITestService::A34, Eq(1));
261 EXPECT_THAT(ITestService::A35, Eq(1));
262 EXPECT_THAT(ITestService::A36, Eq(1));
263 EXPECT_THAT(ITestService::A37, Eq(1));
264 EXPECT_THAT(ITestService::A38, Eq(1));
265 EXPECT_THAT(ITestService::A39, Eq(1));
266 EXPECT_THAT(ITestService::A40, Eq(1));
267 EXPECT_THAT(ITestService::A41, Eq(1));
268 EXPECT_THAT(ITestService::A42, Eq(1));
269 EXPECT_THAT(ITestService::A43, Eq(1));
270 EXPECT_THAT(ITestService::A44, Eq(1));
271 EXPECT_THAT(ITestService::A45, Eq(1));
272 EXPECT_THAT(ITestService::A46, Eq(1));
273 EXPECT_THAT(ITestService::A47, Eq(1));
274 EXPECT_THAT(ITestService::A48, Eq(1));
275 EXPECT_THAT(ITestService::A49, Eq(1));
276 EXPECT_THAT(ITestService::A50, Eq(1));
277 EXPECT_THAT(ITestService::A51, Eq(1));
278 EXPECT_THAT(ITestService::A52, Eq(1));
279 EXPECT_THAT(ITestService::A53, Eq(1));
280 EXPECT_THAT(ITestService::A54, Eq(1));
281 EXPECT_THAT(ITestService::A55, Eq(1));
282 EXPECT_THAT(ITestService::A56, Eq(1));
283 EXPECT_THAT(ITestService::A57, Eq(1));
284 }
285