1 /*
2 * Copyright (C) 2017 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "hidl_test_java_native"
19
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22
23 #include <android/hardware/tests/baz/1.0/IBaz.h>
24 #include <android/hardware/tests/safeunion/1.0/IOtherInterface.h>
25 #include <android/hardware/tests/safeunion/1.0/ISafeUnion.h>
26
27 #include <hidl/LegacySupport.h>
28 #include <hidl/ServiceManagement.h>
29 #include <gtest/gtest.h>
30
31 #include <hidl/HidlTransportSupport.h>
32 #include <hidl/Status.h>
33
34 #include <numeric>
35 #include <sys/stat.h>
36
37 using ::android::sp;
38 using ::android::hardware::tests::baz::V1_0::IBase;
39 using ::android::hardware::tests::baz::V1_0::IBaz;
40 using ::android::hardware::tests::baz::V1_0::IBazCallback;
41 using ::android::hardware::tests::safeunion::V1_0::IOtherInterface;
42 using ::android::hardware::tests::safeunion::V1_0::ISafeUnion;
43
44 using ::android::hardware::hidl_array;
45 using ::android::hardware::hidl_vec;
46 using ::android::hardware::hidl_handle;
47 using ::android::hardware::hidl_string;
48 using ::android::hardware::defaultPassthroughServiceImplementation;
49 using ::android::hardware::Return;
50 using ::android::hardware::Void;
51
52 using HandleTypeSafeUnion = ISafeUnion::HandleTypeSafeUnion;
53 using InterfaceTypeSafeUnion = ISafeUnion::InterfaceTypeSafeUnion;
54 using LargeSafeUnion = ISafeUnion::LargeSafeUnion;
55 using SmallSafeUnion = ISafeUnion::SmallSafeUnion;
56
57 struct BazCallback : public IBazCallback {
58 Return<void> heyItsMe(const sp<IBazCallback> &cb) override;
59 Return<void> hey() override;
60 };
61
heyItsMe(const sp<IBazCallback> & cb)62 Return<void> BazCallback::heyItsMe(
63 const sp<IBazCallback> &cb) {
64 LOG(INFO) << "SERVER: heyItsMe cb = " << cb.get();
65
66 return Void();
67 }
68
hey()69 Return<void> BazCallback::hey() {
70 LOG(INFO) << "SERVER: hey";
71
72 return Void();
73 }
74
75 struct OtherInterface : public IOtherInterface {
concatTwoStringsOtherInterface76 Return<void> concatTwoStrings(const hidl_string& a, const hidl_string& b,
77 concatTwoStrings_cb _hidl_cb) override {
78 hidl_string result = std::string(a) + std::string(b);
79 _hidl_cb(result);
80
81 return Void();
82 }
83 };
84
85 using std::to_string;
86
usage(const char * me)87 static void usage(const char *me) {
88 fprintf(stderr, "%s [-c]lient | [-s]erver\n", me);
89 }
90
91 struct HidlEnvironment : public ::testing::Environment {
SetUpHidlEnvironment92 void SetUp() override {
93 }
94
TearDownHidlEnvironment95 void TearDown() override {
96 }
97 };
98
99 struct HidlTest : public ::testing::Test {
100 sp<IBaz> baz;
101 sp<ISafeUnion> safeunionInterface;
102 sp<IOtherInterface> otherInterface;
103
SetUpHidlTest104 void SetUp() override {
105 using namespace ::android::hardware;
106
107 ::android::hardware::details::waitForHwService(IBaz::descriptor, "default");
108 baz = IBaz::getService();
109 CHECK(baz != nullptr);
110 CHECK(baz->isRemote());
111
112 ::android::hardware::details::waitForHwService(ISafeUnion::descriptor, "default");
113 safeunionInterface = ISafeUnion::getService();
114 CHECK(safeunionInterface != nullptr);
115 CHECK(safeunionInterface->isRemote());
116
117 ::android::hardware::details::waitForHwService(IOtherInterface::descriptor, "default");
118 otherInterface = IOtherInterface::getService();
119 CHECK(otherInterface != nullptr);
120 CHECK(otherInterface->isRemote());
121 }
122
TearDownHidlTest123 void TearDown() override {
124 }
125 };
126
127 template <typename T>
EXPECT_OK(const::android::hardware::Return<T> & ret)128 static void EXPECT_OK(const ::android::hardware::Return<T> &ret) {
129 EXPECT_TRUE(ret.isOk());
130 }
131
132 template<typename T, typename S>
isArrayEqual(const T arr1,const S arr2,size_t size)133 static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
134 for(size_t i = 0; i < size; i++)
135 if(arr1[i] != arr2[i])
136 return false;
137 return true;
138 }
139
TEST_F(HidlTest,GetDescriptorTest)140 TEST_F(HidlTest, GetDescriptorTest) {
141 EXPECT_OK(baz->interfaceDescriptor([&] (const auto &desc) {
142 EXPECT_EQ(desc, IBaz::descriptor);
143 }));
144 }
145
TEST_F(HidlTest,BazSomeBaseMethodTest)146 TEST_F(HidlTest, BazSomeBaseMethodTest) {
147 EXPECT_OK(baz->someBaseMethod());
148 }
149
TEST_F(HidlTest,BazSomeOtherBaseMethodTest)150 TEST_F(HidlTest, BazSomeOtherBaseMethodTest) {
151 IBase::Foo foo;
152 foo.x = 1;
153 foo.y.z = 2.5;
154 // A valid UTF-8 string
155 foo.y.s = "Hello, world, \x46\x6F\x6F\x20\xC2\xA9\x20\x62\x61\x72\x20\xF0\x9D\x8C\x86\x20\x54\x72\x65\x62\x6C\x65\x20\xE2\x98\x83\x20\x72\x6F\x63\x6B\x73";
156
157 foo.aaa.resize(5);
158 for (size_t i = 0; i < foo.aaa.size(); ++i) {
159 foo.aaa[i].z = 1.0f + (float)i * 0.01f;
160 foo.aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
161 }
162
163 EXPECT_OK(
164 baz->someOtherBaseMethod(
165 foo,
166 [&](const auto &result) {
167 // Strings should have the same size as they did before
168 // marshaling. b/35038064
169 EXPECT_EQ(result.y.s.size(), foo.y.s.size());
170 EXPECT_EQ(foo, result);
171 }));
172 }
173
TEST_F(HidlTest,SomeOtherBaseMethodInvalidString)174 TEST_F(HidlTest, SomeOtherBaseMethodInvalidString) {
175 IBase::Foo foo {
176 .y = {
177 .s = "\xff",
178 }
179 };
180
181 auto ret = baz->someOtherBaseMethod(foo, [&](const auto&) {
182 ADD_FAILURE() << "Should not accept invalid UTF-8 String";
183 });
184
185 EXPECT_FALSE(ret.isOk());
186
187 EXPECT_OK(baz->ping());
188 }
189
TEST_F(HidlTest,BazSomeMethodWithFooArraysTest)190 TEST_F(HidlTest, BazSomeMethodWithFooArraysTest) {
191 hidl_array<IBase::Foo, 2> foo;
192
193 foo[0].x = 1;
194 foo[0].y.z = 2.5;
195 foo[0].y.s = "Hello, world";
196
197 foo[0].aaa.resize(5);
198 for (size_t i = 0; i < foo[0].aaa.size(); ++i) {
199 foo[0].aaa[i].z = 1.0f + (float)i * 0.01f;
200 foo[0].aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
201 }
202
203 foo[1].x = 2;
204 foo[1].y.z = -2.5;
205 foo[1].y.s = "Morituri te salutant";
206
207 foo[1].aaa.resize(3);
208 for (size_t i = 0; i < foo[1].aaa.size(); ++i) {
209 foo[1].aaa[i].z = 2.0f - (float)i * 0.01f;
210 foo[1].aaa[i].s = ("Alea iacta est: " + std::to_string(i)).c_str();
211 }
212
213 hidl_array<IBaz::Foo, 2> fooExpectedOutput;
214 fooExpectedOutput[0] = foo[1];
215 fooExpectedOutput[1] = foo[0];
216
217 EXPECT_OK(
218 baz->someMethodWithFooArrays(
219 foo,
220 [&](const auto &result) {
221 EXPECT_EQ(result, fooExpectedOutput);
222 }));
223 }
224
TEST_F(HidlTest,BazSomeMethodWithFooVectorsTest)225 TEST_F(HidlTest, BazSomeMethodWithFooVectorsTest) {
226 hidl_vec<IBase::Foo> foo;
227 foo.resize(2);
228
229 foo[0].x = 1;
230 foo[0].y.z = 2.5;
231 foo[0].y.s = "Hello, world";
232
233 foo[0].aaa.resize(5);
234 for (size_t i = 0; i < foo[0].aaa.size(); ++i) {
235 foo[0].aaa[i].z = 1.0f + (float)i * 0.01f;
236 foo[0].aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
237 }
238
239 foo[1].x = 2;
240 foo[1].y.z = -2.5;
241 foo[1].y.s = "Morituri te salutant";
242
243 foo[1].aaa.resize(3);
244 for (size_t i = 0; i < foo[1].aaa.size(); ++i) {
245 foo[1].aaa[i].z = 2.0f - (float)i * 0.01f;
246 foo[1].aaa[i].s = ("Alea iacta est: " + std::to_string(i)).c_str();
247 }
248
249 hidl_vec<IBaz::Foo> fooExpectedOutput;
250 fooExpectedOutput.resize(2);
251 fooExpectedOutput[0] = foo[1];
252 fooExpectedOutput[1] = foo[0];
253
254 EXPECT_OK(
255 baz->someMethodWithFooVectors(
256 foo,
257 [&](const auto &result) {
258 EXPECT_EQ(result, fooExpectedOutput);
259 }));
260 }
261
TEST_F(HidlTest,BazSomeMethodWithVectorOfArray)262 TEST_F(HidlTest, BazSomeMethodWithVectorOfArray) {
263 IBase::VectorOfArray in, expectedOut;
264 in.addresses.resize(3);
265 expectedOut.addresses.resize(3);
266
267 size_t k = 0;
268 const size_t n = in.addresses.size();
269
270 for (size_t i = 0; i < n; ++i) {
271 for (size_t j = 0; j < 6; ++j, ++k) {
272 in.addresses[i][j] = k;
273 expectedOut.addresses[n - 1 - i][j] = k;
274 }
275 }
276
277 EXPECT_OK(
278 baz->someMethodWithVectorOfArray(
279 in,
280 [&](const auto &out) {
281 EXPECT_EQ(expectedOut, out);
282 }));
283 }
284
TEST_F(HidlTest,BazSomeMethodTakingAVectorOfArray)285 TEST_F(HidlTest, BazSomeMethodTakingAVectorOfArray) {
286 hidl_vec<hidl_array<uint8_t, 6> > in, expectedOut;
287 in.resize(3);
288 expectedOut.resize(3);
289
290 size_t k = 0;
291 const size_t n = in.size();
292 for (size_t i = 0; i < n; ++i) {
293 for (size_t j = 0; j < 6; ++j, ++k) {
294 in[i][j] = k;
295 expectedOut[n - 1 - i][j] = k;
296 }
297 }
298
299 EXPECT_OK(
300 baz->someMethodTakingAVectorOfArray(
301 in,
302 [&](const auto &out) {
303 EXPECT_EQ(expectedOut, out);
304 }));
305 }
306
numberToEnglish(int x)307 static std::string numberToEnglish(int x) {
308 static const char *const kDigits[] = {
309 "zero",
310 "one",
311 "two",
312 "three",
313 "four",
314 "five",
315 "six",
316 "seven",
317 "eight",
318 "nine",
319 };
320
321 if (x < 0) {
322 return "negative " + numberToEnglish(-x);
323 }
324
325 if (x < 10) {
326 return kDigits[x];
327 }
328
329 if (x <= 15) {
330 static const char *const kSpecialTens[] = {
331 "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
332 };
333
334 return kSpecialTens[x - 10];
335 }
336
337 if (x < 20) {
338 return std::string(kDigits[x % 10]) + "teen";
339 }
340
341 if (x < 100) {
342 static const char *const kDecades[] = {
343 "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
344 "eighty", "ninety",
345 };
346
347 return std::string(kDecades[x / 10 - 2]) + kDigits[x % 10];
348 }
349
350 return "positively huge!";
351 }
352
TEST_F(HidlTest,BazTransposeTest)353 TEST_F(HidlTest, BazTransposeTest) {
354 IBase::StringMatrix5x3 in;
355 IBase::StringMatrix3x5 expectedOut;
356
357 for (int i = 0; i < 5; ++i) {
358 for (int j = 0; j < 3; ++j) {
359 in.s[i][j] = expectedOut.s[j][i] = numberToEnglish(3 * i + j + 1).c_str();
360 }
361 }
362
363 EXPECT_OK(baz->transpose(
364 in,
365 [&](const auto &out) {
366 EXPECT_EQ(expectedOut, out);
367 }));
368 }
369
TEST_F(HidlTest,BazTranspose2Test)370 TEST_F(HidlTest, BazTranspose2Test) {
371 hidl_array<hidl_string, 5, 3> in;
372 hidl_array<hidl_string, 3, 5> expectedOut;
373
374 for (int i = 0; i < 5; ++i) {
375 for (int j = 0; j < 3; ++j) {
376 in[i][j] = expectedOut[j][i] = numberToEnglish(3 * i + j + 1).c_str();
377 }
378 }
379
380 EXPECT_OK(baz->transpose2(
381 in,
382 [&](const auto &out) {
383 EXPECT_EQ(expectedOut, out);
384 }));
385 }
386
TEST_F(HidlTest,BazSomeBoolMethodTest)387 TEST_F(HidlTest, BazSomeBoolMethodTest) {
388 auto result = baz->someBoolMethod(true);
389 EXPECT_OK(result);
390 EXPECT_EQ(result, false);
391 }
392
TEST_F(HidlTest,BazSomeBoolArrayMethodTest)393 TEST_F(HidlTest, BazSomeBoolArrayMethodTest) {
394 hidl_array<bool, 3> someBoolArray;
395 someBoolArray[0] = true;
396 someBoolArray[1] = false;
397 someBoolArray[2] = true;
398
399 hidl_array<bool, 4> expectedOut;
400 expectedOut[0] = false;
401 expectedOut[1] = true;
402 expectedOut[2] = false;
403 expectedOut[3] = true;
404
405 EXPECT_OK(
406 baz->someBoolArrayMethod(
407 someBoolArray,
408 [&](const auto &result) {
409 EXPECT_EQ(expectedOut, result);
410 }));
411 }
412
TEST_F(HidlTest,BazSomeBoolVectorMethodTest)413 TEST_F(HidlTest, BazSomeBoolVectorMethodTest) {
414 hidl_vec<bool> someBoolVector, expected;
415 someBoolVector.resize(4);
416 expected.resize(4);
417
418 for (size_t i = 0; i < someBoolVector.size(); ++i) {
419 someBoolVector[i] = ((i & 1) == 0);
420 expected[i] = !someBoolVector[i];
421 }
422
423 EXPECT_OK(
424 baz->someBoolVectorMethod(
425 someBoolVector,
426 [&](const auto &result) {
427 EXPECT_EQ(expected, result);
428 }));
429 }
430
TEST_F(HidlTest,BazDoThisMethodTest)431 TEST_F(HidlTest, BazDoThisMethodTest) {
432 EXPECT_OK(baz->doThis(1.0f));
433 }
434
TEST_F(HidlTest,BazDoThatAndReturnSomethingMethodTest)435 TEST_F(HidlTest, BazDoThatAndReturnSomethingMethodTest) {
436 auto result = baz->doThatAndReturnSomething(1);
437 EXPECT_OK(result);
438 EXPECT_EQ(result, 666);
439 }
440
TEST_F(HidlTest,BazDoQuiteABitMethodTest)441 TEST_F(HidlTest, BazDoQuiteABitMethodTest) {
442 auto result = baz->doQuiteABit(1, 2LL, 3.0f, 4.0);
443
444 EXPECT_OK(result);
445 EXPECT_EQ(result, 666.5);
446 }
447
TEST_F(HidlTest,BazDoSomethingElseMethodTest)448 TEST_F(HidlTest, BazDoSomethingElseMethodTest) {
449 hidl_array<int32_t, 15> param;
450 hidl_array<int32_t, 32> expected;
451
452 for (size_t i = 0; i < 15; ++i) {
453 param[i] = expected[15 + i] = i;
454 expected[i] = 2 * i;
455 }
456
457 expected[30] = 1;
458 expected[31] = 2;
459
460 EXPECT_OK(
461 baz->doSomethingElse(
462 param,
463 [&](const auto &result) {
464 EXPECT_EQ(expected, result);
465 }));
466 }
467
TEST_F(HidlTest,BazDoStuffAndReturnAStringMethodTest)468 TEST_F(HidlTest, BazDoStuffAndReturnAStringMethodTest) {
469 std::string expected = "Hello, world!";
470 EXPECT_OK(
471 baz->doStuffAndReturnAString(
472 [&](const auto &result) {
473 EXPECT_EQ(expected, result);
474 }));
475 }
476
TEST_F(HidlTest,BazMapThisVectorMethodTest)477 TEST_F(HidlTest, BazMapThisVectorMethodTest) {
478 hidl_vec<int32_t> vec_param, expected;
479 vec_param.resize(15);
480 expected.resize(15);
481
482 for (size_t i = 0; i < 15; ++i) {
483 vec_param[i] = i;
484 expected[i] = 2 * i;
485 }
486
487 EXPECT_OK(
488 baz->mapThisVector(
489 vec_param,
490 [&](const auto &result) {
491 EXPECT_EQ(expected, result);
492 }));
493 }
494
TEST_F(HidlTest,BazCallMeMethodTest)495 TEST_F(HidlTest, BazCallMeMethodTest) {
496 EXPECT_OK(baz->callMe(new BazCallback()));
497 }
498
TEST_F(HidlTest,BazCallMeLaterMethodTest)499 TEST_F(HidlTest, BazCallMeLaterMethodTest) {
500 EXPECT_OK(baz->callMeLater(new BazCallback()));
501 EXPECT_OK(baz->iAmFreeNow());
502 }
503
TEST_F(HidlTest,BazUseAnEnumMethodTest)504 TEST_F(HidlTest, BazUseAnEnumMethodTest) {
505 auto result = baz->useAnEnum(IBaz::SomeEnum::bar);
506
507 EXPECT_OK(result);
508 EXPECT_TRUE(result == IBaz::SomeEnum::quux);
509 }
510
TEST_F(HidlTest,BazHaveSomeStringsMethodTest)511 TEST_F(HidlTest, BazHaveSomeStringsMethodTest) {
512 hidl_array<hidl_string, 3> string_params;
513 string_params[0] = "one";
514 string_params[1] = "two";
515 string_params[2] = "three";
516
517 hidl_array<hidl_string, 2> expected;
518 expected[0] = "Hello";
519 expected[1] = "World";
520
521 EXPECT_OK(
522 baz->haveSomeStrings(
523 string_params,
524 [&](const auto &result) {
525 EXPECT_EQ(expected, result);
526 }));
527 }
528
TEST_F(HidlTest,BazHaveAStringVecMethodTest)529 TEST_F(HidlTest, BazHaveAStringVecMethodTest) {
530 hidl_vec<hidl_string> string_vec{ "Uno", "Dos", "Tres", "Cuatro" };
531 hidl_vec<hidl_string> expected{"Hello", "World"};
532
533 EXPECT_OK(
534 baz->haveAStringVec(
535 string_vec,
536 [&](const auto &result) {
537 EXPECT_EQ(expected, result);
538 }));
539 }
540
TEST_F(HidlTest,BazReturnABunchOfStringsMethodTest)541 TEST_F(HidlTest, BazReturnABunchOfStringsMethodTest) {
542 std::string expectedA = "Eins";
543 std::string expectedB = "Zwei";
544 std::string expectedC = "Drei";
545 EXPECT_OK(
546 baz->returnABunchOfStrings(
547 [&](const auto &a, const auto &b, const auto &c) {
548 EXPECT_EQ(a, expectedA);
549 EXPECT_EQ(b, expectedB);
550 EXPECT_EQ(c, expectedC);
551 }));
552 }
553
TEST_F(HidlTest,BazTestArrays)554 TEST_F(HidlTest, BazTestArrays) {
555 IBase::LotsOfPrimitiveArrays in;
556
557 for (size_t i = 0; i < 128; ++i) {
558 in.byte1[i] = i;
559 in.boolean1[i] = (i & 4) != 0;
560 in.double1[i] = i;
561 }
562
563 size_t k = 0;
564 for (size_t i = 0; i < 8; ++i) {
565 for (size_t j = 0; j < 128; ++j, ++k) {
566 in.byte2[i][j] = k;
567 in.boolean2[i][j] = (k & 4) != 0;
568 in.double2[i][j] = k;
569 }
570 }
571
572 size_t m = 0;
573 for (size_t i = 0; i < 8; ++i) {
574 for (size_t j = 0; j < 16; ++j) {
575 for (size_t k = 0; k < 128; ++k, ++m) {
576 in.byte3[i][j][k] = m;
577 in.boolean3[i][j][k] = (m & 4) != 0;
578 in.double3[i][j][k] = m;
579 }
580 }
581 }
582
583 EXPECT_OK(
584 baz->testArrays(in,
585 [&](const auto &out) {
586 EXPECT_EQ(in, out);
587 }));
588 }
589
TEST_F(HidlTest,BazTestByteVecs)590 TEST_F(HidlTest, BazTestByteVecs) {
591 hidl_vec<IBase::ByteOneDim> in;
592 in.resize(8);
593
594 size_t k = 0;
595 for (size_t i = 0; i < in.size(); ++i) {
596 for (size_t j = 0; j < 128; ++j, ++k) {
597 in[i][j] = k;
598 }
599 }
600
601 EXPECT_OK(baz->testByteVecs(
602 in, [&](const auto &out) { EXPECT_EQ(in, out); }));
603 }
604
TEST_F(HidlTest,BazTestBooleanVecs)605 TEST_F(HidlTest, BazTestBooleanVecs) {
606 hidl_vec<IBase::BooleanOneDim> in;
607 in.resize(8);
608
609 size_t k = 0;
610 for (size_t i = 0; i < in.size(); ++i) {
611 for (size_t j = 0; j < 128; ++j, ++k) {
612 in[i][j] = (k & 4) != 0;
613 }
614 }
615
616 EXPECT_OK(baz->testBooleanVecs(
617 in, [&](const auto &out) { EXPECT_EQ(in, out); }));
618 }
619
TEST_F(HidlTest,BazTestDoubleVecs)620 TEST_F(HidlTest, BazTestDoubleVecs) {
621 hidl_vec<IBase::DoubleOneDim> in;
622 in.resize(8);
623
624 size_t k = 0;
625 for (size_t i = 0; i < in.size(); ++i) {
626 for (size_t j = 0; j < 128; ++j, ++k) {
627 in[i][j] = k;
628 }
629 }
630
631 EXPECT_OK(baz->testDoubleVecs(
632 in, [&](const auto &out) { EXPECT_EQ(in, out); }));
633 }
634
TEST_F(HidlTest,SafeUnionNoInitTest)635 TEST_F(HidlTest, SafeUnionNoInitTest) {
636 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
637 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::noinit, safeUnion.getDiscriminator());
638 }));
639 }
640
TEST_F(HidlTest,SafeUnionSimpleTest)641 TEST_F(HidlTest, SafeUnionSimpleTest) {
642 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
643 EXPECT_OK(safeunionInterface->setA(safeUnion, -5, [&](const LargeSafeUnion& safeUnion) {
644 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::a, safeUnion.getDiscriminator());
645 EXPECT_EQ(-5, safeUnion.a());
646
647 uint64_t max = std::numeric_limits<uint64_t>::max();
648 EXPECT_OK(
649 safeunionInterface->setD(safeUnion, max, [&](const LargeSafeUnion& safeUnion) {
650 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::d, safeUnion.getDiscriminator());
651 EXPECT_EQ(max, safeUnion.d());
652 }));
653 }));
654 }));
655 }
656
TEST_F(HidlTest,SafeUnionArrayLikeTypesTest)657 TEST_F(HidlTest, SafeUnionArrayLikeTypesTest) {
658 const std::array<int64_t, 5> testArray{1, -2, 3, -4, 5};
659 const hidl_vec<uint64_t> testVector{std::numeric_limits<uint64_t>::max()};
660
661 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
662 EXPECT_OK(
663 safeunionInterface->setF(safeUnion, testArray, [&](const LargeSafeUnion& safeUnion) {
664 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::f, safeUnion.getDiscriminator());
665
666 for (size_t i = 0; i < testArray.size(); i++) {
667 EXPECT_EQ(testArray[i], safeUnion.f()[i]);
668 }
669 }));
670
671 EXPECT_OK(
672 safeunionInterface->setI(safeUnion, testVector, [&](const LargeSafeUnion& safeUnion) {
673 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::i, safeUnion.getDiscriminator());
674 EXPECT_EQ(testVector, safeUnion.i());
675 }));
676 }));
677 }
678
TEST_F(HidlTest,SafeUnionStringTypeTest)679 TEST_F(HidlTest, SafeUnionStringTypeTest) {
680 const std::string testString =
681 "This is an inordinately long test string to exercise hidl_string types in safe unions.";
682
683 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
684 EXPECT_OK(safeunionInterface->setG(
685 safeUnion, hidl_string(testString), [&](const LargeSafeUnion& safeUnion) {
686 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::g, safeUnion.getDiscriminator());
687 EXPECT_EQ(testString, std::string(safeUnion.g()));
688 }));
689 }));
690 }
691
TEST_F(HidlTest,SafeUnionNestedTest)692 TEST_F(HidlTest, SafeUnionNestedTest) {
693 SmallSafeUnion smallSafeUnion;
694 smallSafeUnion.a(1);
695
696 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
697 EXPECT_OK(safeunionInterface->setL(
698 safeUnion, smallSafeUnion, [&](const LargeSafeUnion& safeUnion) {
699 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::l, safeUnion.getDiscriminator());
700
701 EXPECT_EQ(SmallSafeUnion::hidl_discriminator::a, safeUnion.l().getDiscriminator());
702 EXPECT_EQ(1, safeUnion.l().a());
703 }));
704 }));
705 }
706
707 // does not check for fd equality
checkNativeHandlesDataEquality(const native_handle_t * reference,const native_handle_t * result)708 static void checkNativeHandlesDataEquality(const native_handle_t* reference,
709 const native_handle_t* result) {
710 if (reference == nullptr || result == nullptr) {
711 EXPECT_EQ(reference == nullptr, result == nullptr);
712 return;
713 }
714
715 ASSERT_NE(reference, result);
716 ASSERT_EQ(reference->version, result->version);
717 EXPECT_EQ(reference->numFds, result->numFds);
718 EXPECT_EQ(reference->numInts, result->numInts);
719
720 int offset = reference->numFds;
721 int numInts = reference->numInts;
722 EXPECT_TRUE(isArrayEqual(&(reference->data[offset]), &(result->data[offset]), numInts));
723 }
724
TEST_F(HidlTest,SafeUnionInterfaceNullHandleTest)725 TEST_F(HidlTest, SafeUnionInterfaceNullHandleTest) {
726 InterfaceTypeSafeUnion safeUnion;
727
728 EXPECT_OK(safeunionInterface->setInterfaceF(
729 safeUnion, hidl_handle(nullptr), [&](const InterfaceTypeSafeUnion& safeUnion) {
730 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::f,
731 safeUnion.getDiscriminator());
732
733 checkNativeHandlesDataEquality(nullptr, safeUnion.f().getNativeHandle());
734 }));
735 }
736
TEST_F(HidlTest,SafeUnionInterfaceTest)737 TEST_F(HidlTest, SafeUnionInterfaceTest) {
738 const std::array<int8_t, 7> testArray{-1, -2, -3, 0, 1, 2, 3};
739 const hidl_vec<hidl_string> testVector{"So", "Many", "Words"};
740 const std::string testStringA = "Hello";
741 const std::string testStringB = "World";
742
743 const std::array<int, 6> testHandleData{2, -32, 10, -4329454, 11, 24};
744 native_handle_t* h = native_handle_create(0, testHandleData.size());
745 CHECK(sizeof(testHandleData) == testHandleData.size() * sizeof(int));
746 std::memcpy(h->data, testHandleData.data(), sizeof(testHandleData));
747
748 std::vector<hidl_handle> testHandlesVector(256);
749 for (size_t i = 0; i < testHandlesVector.size(); i++) {
750 testHandlesVector[i].setTo(native_handle_clone(h), true /* shouldOwn */);
751 }
752
753 EXPECT_OK(
754 safeunionInterface->newInterfaceTypeSafeUnion([&](const InterfaceTypeSafeUnion& safeUnion) {
755 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::noinit,
756 safeUnion.getDiscriminator());
757
758 EXPECT_OK(safeunionInterface->setInterfaceB(
759 safeUnion, testArray, [&](const InterfaceTypeSafeUnion& safeUnion) {
760 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::b,
761 safeUnion.getDiscriminator());
762
763 for (size_t i = 0; i < testArray.size(); i++) {
764 EXPECT_EQ(testArray[i], safeUnion.b()[i]);
765 }
766 }));
767
768 EXPECT_OK(safeunionInterface->setInterfaceD(
769 safeUnion, testStringA, [&](const InterfaceTypeSafeUnion& safeUnion) {
770 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::d,
771 safeUnion.getDiscriminator());
772 EXPECT_EQ(testStringA, safeUnion.d());
773 }));
774
775 EXPECT_OK(safeunionInterface->setInterfaceE(
776 safeUnion, testVector, [&](const InterfaceTypeSafeUnion& safeUnion) {
777 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::e,
778 safeUnion.getDiscriminator());
779 EXPECT_EQ(testVector, safeUnion.e());
780 }));
781
782 EXPECT_OK(safeunionInterface->setInterfaceF(
783 safeUnion, hidl_handle(h), [&](const InterfaceTypeSafeUnion& safeUnion) {
784 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::f,
785 safeUnion.getDiscriminator());
786
787 const native_handle_t* result = safeUnion.f().getNativeHandle();
788 checkNativeHandlesDataEquality(h, result);
789 }));
790
791 EXPECT_OK(safeunionInterface->setInterfaceG(
792 safeUnion, testHandlesVector, [&](const InterfaceTypeSafeUnion& safeUnion) {
793 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::g,
794 safeUnion.getDiscriminator());
795
796 for (size_t i = 0; i < testHandlesVector.size(); i++) {
797 checkNativeHandlesDataEquality(h, safeUnion.g()[i].getNativeHandle());
798 }
799 }));
800 }));
801
802 // Same-process interface calls are not supported in Java, so we use
803 // a safe_union instance bound to this (client) process instead of
804 // safeunionInterface to exercise this test-case. Ref: b/110957763.
805 InterfaceTypeSafeUnion safeUnion;
806 safeUnion.c(otherInterface);
807
808 EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::c, safeUnion.getDiscriminator());
809 EXPECT_OK(safeUnion.c()->concatTwoStrings(
810 hidl_string(testStringA), hidl_string(testStringB), [&](const hidl_string& result) {
811 EXPECT_EQ(testStringA + testStringB, std::string(result));
812 }));
813
814 native_handle_delete(h);
815 }
816
TEST_F(HidlTest,SafeUnionNullHandleTest)817 TEST_F(HidlTest, SafeUnionNullHandleTest) {
818 HandleTypeSafeUnion safeUnion;
819
820 EXPECT_OK(safeunionInterface->setHandleA(
821 safeUnion, hidl_handle(nullptr), [&](const HandleTypeSafeUnion& safeUnion) {
822 EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::a,
823 safeUnion.getDiscriminator());
824
825 checkNativeHandlesDataEquality(nullptr, safeUnion.a().getNativeHandle());
826 }));
827 }
828
TEST_F(HidlTest,SafeUnionSimpleHandleTest)829 TEST_F(HidlTest, SafeUnionSimpleHandleTest) {
830 const std::array<int, 6> testData{2, -32, 10, -4329454, 11, 24};
831 native_handle_t* h = native_handle_create(0, testData.size());
832 ASSERT_EQ(sizeof(testData), testData.size() * sizeof(int));
833 std::memcpy(h->data, testData.data(), sizeof(testData));
834
835 std::array<hidl_handle, 5> testArray;
836 for (size_t i = 0; i < testArray.size(); i++) {
837 testArray[i].setTo(native_handle_clone(h), true /* shouldOwn */);
838 }
839
840 std::vector<hidl_handle> testVector(256);
841 for (size_t i = 0; i < testVector.size(); i++) {
842 testVector[i].setTo(native_handle_clone(h), true /* shouldOwn */);
843 }
844
845 EXPECT_OK(
846 safeunionInterface->newHandleTypeSafeUnion([&](const HandleTypeSafeUnion& safeUnion) {
847 EXPECT_OK(safeunionInterface->setHandleA(
848 safeUnion, hidl_handle(h), [&](const HandleTypeSafeUnion& safeUnion) {
849 EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::a,
850 safeUnion.getDiscriminator());
851
852 checkNativeHandlesDataEquality(h, safeUnion.a().getNativeHandle());
853 }));
854
855 EXPECT_OK(safeunionInterface->setHandleB(
856 safeUnion, testArray, [&](const HandleTypeSafeUnion& safeUnion) {
857 EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::b,
858 safeUnion.getDiscriminator());
859
860 for (size_t i = 0; i < testArray.size(); i++) {
861 checkNativeHandlesDataEquality(h, safeUnion.b()[i].getNativeHandle());
862 }
863 }));
864
865 EXPECT_OK(safeunionInterface->setHandleC(
866 safeUnion, testVector, [&](const HandleTypeSafeUnion& safeUnion) {
867 EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::c,
868 safeUnion.getDiscriminator());
869
870 for (size_t i = 0; i < testVector.size(); i++) {
871 checkNativeHandlesDataEquality(h, safeUnion.c()[i].getNativeHandle());
872 }
873 }));
874 }));
875
876 native_handle_delete(h);
877 }
878
TEST_F(HidlTest,SafeUnionVecOfHandlesWithOneFdTest)879 TEST_F(HidlTest, SafeUnionVecOfHandlesWithOneFdTest) {
880 const std::vector<std::string> testStrings{"This ", "is ", "so ", "much ", "data!\n"};
881 const std::string testFileName = "/data/local/tmp/SafeUnionVecOfHandlesWithOneFdTest";
882 const std::array<int, 6> testData{2, -32, 10, -4329454, 11, 24};
883 ASSERT_EQ(sizeof(testData), testData.size() * sizeof(int));
884
885 const std::string goldenResult = std::accumulate(testStrings.begin(),
886 testStrings.end(),
887 std::string());
888
889 int fd = open(testFileName.c_str(), (O_RDWR | O_TRUNC | O_CREAT), (S_IRUSR | S_IWUSR));
890 ASSERT_TRUE(fd >= 0);
891
892 native_handle* h = native_handle_create(1 /* numFds */, testData.size() /* numInts */);
893 std::memcpy(&(h->data[1]), testData.data(), sizeof(testData));
894 h->data[0] = fd;
895
896 hidl_vec<hidl_handle> testHandles(testStrings.size());
897 for (size_t i = 0; i < testHandles.size(); i++) {
898 testHandles[i].setTo(native_handle_clone(h), true /* shouldOwn */);
899 }
900
901 EXPECT_OK(
902 safeunionInterface->newHandleTypeSafeUnion([&](const HandleTypeSafeUnion& safeUnion) {
903 EXPECT_OK(safeunionInterface->setHandleC(
904 safeUnion, testHandles, [&](const HandleTypeSafeUnion& safeUnion) {
905 EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::c,
906 safeUnion.getDiscriminator());
907
908 for (size_t i = 0; i < safeUnion.c().size(); i++) {
909 const native_handle_t* reference = testHandles[i].getNativeHandle();
910 const native_handle_t* result = safeUnion.c()[i].getNativeHandle();
911 checkNativeHandlesDataEquality(reference, result);
912
913 // Original FDs should be dup'd
914 int resultFd = result->data[0];
915 EXPECT_NE(reference->data[0], resultFd);
916
917 EXPECT_TRUE(android::base::WriteStringToFd(testStrings[i], resultFd));
918 EXPECT_EQ(0, fsync(resultFd));
919 }
920 }));
921 }));
922
923 std::string result;
924 lseek(fd, 0, SEEK_SET);
925
926 EXPECT_TRUE(android::base::ReadFdToString(fd, &result));
927 EXPECT_EQ(goldenResult, result);
928
929 native_handle_delete(h);
930 EXPECT_EQ(0, close(fd));
931 EXPECT_EQ(0, remove(testFileName.c_str()));
932 }
933
TEST_F(HidlTest,SafeUnionHandleWithMultipleFdsTest)934 TEST_F(HidlTest, SafeUnionHandleWithMultipleFdsTest) {
935 const std::vector<std::string> testStrings{"This ", "is ", "so ", "much ", "data!\n"};
936 const std::string testFileName = "/data/local/tmp/SafeUnionHandleWithMultipleFdsTest";
937 const std::array<int, 6> testData{2, -32, 10, -4329454, 11, 24};
938 ASSERT_EQ(sizeof(testData), testData.size() * sizeof(int));
939
940 const std::string goldenResult = std::accumulate(testStrings.begin(),
941 testStrings.end(),
942 std::string());
943
944 int fd = open(testFileName.c_str(), (O_RDWR | O_TRUNC | O_CREAT), (S_IRUSR | S_IWUSR));
945 ASSERT_TRUE(fd >= 0);
946
947 const int numFds = testStrings.size();
948 native_handle* h = native_handle_create(numFds, testData.size() /* numInts */);
949 std::memcpy(&(h->data[numFds]), testData.data(), sizeof(testData));
950 for (size_t i = 0; i < numFds; i++) {
951 h->data[i] = fd;
952 }
953
954 hidl_handle testHandle;
955 testHandle.setTo(h, false /* shouldOwn */);
956
957 EXPECT_OK(
958 safeunionInterface->newHandleTypeSafeUnion([&](const HandleTypeSafeUnion& safeUnion) {
959 EXPECT_OK(safeunionInterface->setHandleA(
960 safeUnion, testHandle, [&](const HandleTypeSafeUnion& safeUnion) {
961 EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::a,
962 safeUnion.getDiscriminator());
963
964 const native_handle_t* result = safeUnion.a().getNativeHandle();
965 checkNativeHandlesDataEquality(h, result);
966
967 for (size_t i = 0; i < result->numFds; i++) {
968 // Original FDs should be dup'd
969 int resultFd = result->data[i];
970 EXPECT_NE(h->data[i], resultFd);
971
972 EXPECT_TRUE(android::base::WriteStringToFd(testStrings[i], resultFd));
973 EXPECT_EQ(0, fsync(resultFd));
974 }
975 }));
976 }));
977
978 std::string result;
979 lseek(fd, 0, SEEK_SET);
980
981 EXPECT_TRUE(android::base::ReadFdToString(fd, &result));
982 EXPECT_EQ(goldenResult, result);
983
984 native_handle_delete(h);
985 EXPECT_EQ(0, close(fd));
986 EXPECT_EQ(0, remove(testFileName.c_str()));
987 }
988
TEST_F(HidlTest,SafeUnionEqualityTest)989 TEST_F(HidlTest, SafeUnionEqualityTest) {
990 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& one) {
991 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
992 EXPECT_TRUE(one == two);
993 EXPECT_FALSE(one != two);
994 }));
995
996 EXPECT_OK(safeunionInterface->setA(one, 1, [&](const LargeSafeUnion& one) {
997 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
998 EXPECT_FALSE(one == two);
999 EXPECT_TRUE(one != two);
1000 }));
1001
1002 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1003 EXPECT_OK(safeunionInterface->setB(two, 1, [&](const LargeSafeUnion& two) {
1004 EXPECT_FALSE(one == two);
1005 EXPECT_TRUE(one != two);
1006 }));
1007 }));
1008
1009 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1010 EXPECT_OK(safeunionInterface->setA(two, 2, [&](const LargeSafeUnion& two) {
1011 EXPECT_FALSE(one == two);
1012 EXPECT_TRUE(one != two);
1013 }));
1014 }));
1015
1016 EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1017 EXPECT_OK(safeunionInterface->setA(two, 1, [&](const LargeSafeUnion& two) {
1018 EXPECT_TRUE(one == two);
1019 EXPECT_FALSE(one != two);
1020 }));
1021 }));
1022 }));
1023 }));
1024 }
1025
main(int argc,char ** argv)1026 int main(int argc, char **argv) {
1027 setenv("TREBLE_TESTING_OVERRIDE", "true", true);
1028
1029 using namespace android::hardware;
1030
1031 const char *me = argv[0];
1032
1033 bool wantClient = false;
1034 bool wantServer = false;
1035
1036 int res;
1037 while ((res = getopt(argc, argv, "chs")) >= 0) {
1038 switch (res) {
1039 case 'c':
1040 {
1041 wantClient = true;
1042 break;
1043 }
1044
1045 case 's':
1046 {
1047 wantServer = true;
1048 break;
1049 }
1050
1051 case '?':
1052 case 'h':
1053 default:
1054 {
1055 usage(me);
1056 exit(1);
1057 break;
1058 }
1059 }
1060 }
1061
1062 if ((!wantClient && !wantServer) || (wantClient && wantServer)) {
1063 usage(me);
1064 exit(1);
1065 }
1066
1067 if (wantClient) {
1068 ::testing::AddGlobalTestEnvironment(new HidlEnvironment);
1069 ::testing::InitGoogleTest(&argc, argv);
1070 int status = RUN_ALL_TESTS();
1071 return status;
1072 }
1073
1074 ::android::status_t status;
1075 configureRpcThreadpool(1, true);
1076
1077 status = registerPassthroughServiceImplementation<IBaz>();
1078 CHECK(status == ::android::OK) << "IBaz didn't register";
1079
1080 status = registerPassthroughServiceImplementation<ISafeUnion>();
1081 CHECK(status == ::android::OK) << "ISafeUnion didn't register";
1082
1083 sp<IOtherInterface> otherInterface = new OtherInterface();
1084 status = otherInterface->registerAsService();
1085 CHECK(status == ::android::OK) << "IOtherInterface didn't register";
1086
1087 joinRpcThreadpool();
1088 return 0;
1089 }
1090