• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/memory/2.0/IMemoryInterface.h>
25 #include <android/hardware/tests/memory/2.0/types.h>
26 #include <android/hardware/tests/safeunion/1.0/ISafeUnion.h>
27 #include <android/hidl/allocator/1.0/IAllocator.h>
28 #include <android/hidl/manager/1.0/IServiceManager.h>
29 
30 #include <hidlmemory/mapping.h>
31 #include <hidl/LegacySupport.h>
32 #include <hidl/ServiceManagement.h>
33 #include <gtest/gtest.h>
34 
35 #include <hidl/HidlTransportSupport.h>
36 #include <hidl/Status.h>
37 
38 #include <numeric>
39 #include <sys/stat.h>
40 
41 using ::android::sp;
42 using ::android::hardware::tests::baz::V1_0::IBase;
43 using ::android::hardware::tests::baz::V1_0::IBaz;
44 using ::android::hardware::tests::baz::V1_0::IBazCallback;
45 using ::android::hardware::tests::memory::V2_0::IMemoryInterface;
46 using ::android::hardware::tests::memory::V2_0::TwoMemory;
47 using ::android::hardware::tests::safeunion::V1_0::ISafeUnion;
48 
49 using ::android::hardware::hidl_array;
50 using ::android::hardware::hidl_vec;
51 using ::android::hardware::hidl_memory;
52 using ::android::hardware::hidl_handle;
53 using ::android::hardware::hidl_string;
54 using ::android::hardware::defaultPassthroughServiceImplementation;
55 using ::android::hardware::Return;
56 using ::android::hardware::Status;
57 using ::android::hardware::Void;
58 
59 using ::android::hidl::allocator::V1_0::IAllocator;
60 using ::android::hidl::memory::V1_0::IMemory;
61 
62 using HandleTypeSafeUnion = ISafeUnion::HandleTypeSafeUnion;
63 using InterfaceTypeSafeUnion = ISafeUnion::InterfaceTypeSafeUnion;
64 using LargeSafeUnion = ISafeUnion::LargeSafeUnion;
65 using SmallSafeUnion = ISafeUnion::SmallSafeUnion;
66 
67 struct BazCallback : public IBazCallback {
68     Return<void> heyItsMe(const sp<IBazCallback> &cb) override;
69     Return<void> hey() override;
70 };
71 
heyItsMe(const sp<IBazCallback> & cb)72 Return<void> BazCallback::heyItsMe(
73         const sp<IBazCallback> &cb) {
74     LOG(INFO) << "SERVER: heyItsMe cb = " << cb.get();
75 
76     return Void();
77 }
78 
hey()79 Return<void> BazCallback::hey() {
80     LOG(INFO) << "SERVER: hey";
81 
82     return Void();
83 }
84 
85 struct MemoryInterface : public IMemoryInterface {
MemoryInterfaceMemoryInterface86     MemoryInterface() {
87         sp<IAllocator> ashmem = IAllocator::getService("ashmem");
88         LOG_FATAL_IF(ashmem == nullptr);
89         ashmem->allocate(8, [&](bool success, const hidl_memory& m) {
90             LOG_FATAL_IF(!success);
91             (void) success;
92             mMemory = m;
93         });
94         sp<IMemory> memory = mapMemory(mMemory);
95 
96         LOG_FATAL_IF(memory == nullptr);
97         uint8_t* data =
98             static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
99         for (size_t i = 0; i < 8; ++i) {
100             data[i] = i;
101         }
102         memory->commit();
103     }
104 
bitwiseNotMemoryInterface105     Return<void> bitwiseNot(const hidl_memory& mem) override {
106         sp<IMemory> memory = mapMemory(mem);
107         if (memory == nullptr) {
108             return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT,
109                                              "Could not map hidl_memory");
110         }
111         uint8_t* data =
112             static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
113 
114         memory->update();
115         for (size_t i = 0; i < memory->getSize(); i++) {
116             data[i] = ~data[i];
117         }
118         memory->commit();
119 
120         return Void();
121     }
122 
getTestMemMemoryInterface123     Return<void> getTestMem(getTestMem_cb _hidl_cb) override {
124         _hidl_cb(mMemory);
125         return Status::ok();
126     }
127 
getSumDiffMemoryInterface128     Return<void> getSumDiff(const TwoMemory& in, getSumDiff_cb _hidl_cb) override {
129         if (in.mem1.size() != in.mem2.size()) {
130             return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT,
131                                              "Buffers must be the same size.");
132         }
133         const size_t size = in.mem1.size();
134 
135         // Map first input.
136         sp<IMemory> memory_in1 = mapMemory(in.mem1);
137         if (memory_in1 == nullptr) {
138             return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT,
139                                              "Could not map hidl_memory");
140         }
141         uint8_t* data_in1 =
142             static_cast<uint8_t*>(static_cast<void*>(memory_in1->getPointer()));
143         memory_in1->update();
144 
145         // Map second input.
146         sp<IMemory> memory_in2 = mapMemory(in.mem2);
147         if (memory_in2 == nullptr) {
148             return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT,
149                                              "Could not map hidl_memory");
150         }
151         uint8_t* data_in2 =
152             static_cast<uint8_t*>(static_cast<void*>(memory_in2->getPointer()));
153         memory_in2->update();
154 
155         TwoMemory out;
156         sp<IAllocator> ashmem = IAllocator::getService("ashmem");
157         LOG_FATAL_IF(ashmem == nullptr);
158 
159         // Map first output.
160         ashmem->allocate(size, [&](bool success, const hidl_memory& m) {
161             LOG_FATAL_IF(!success);
162             (void) success;
163             out.mem1 = m;
164         });
165         sp<IMemory> memory_out1 = mapMemory(out.mem1);
166         LOG_FATAL_IF(memory_out1 == nullptr);
167         uint8_t* data_out1 =
168             static_cast<uint8_t*>(static_cast<void*>(memory_out1->getPointer()));
169 
170         // Map second output.
171         ashmem->allocate(size, [&](bool success, const hidl_memory& m) {
172             LOG_FATAL_IF(!success);
173             (void) success;
174             out.mem2 = m;
175         });
176         sp<IMemory> memory_out2 = mapMemory(out.mem2);
177         LOG_FATAL_IF(memory_out2 == nullptr);
178         uint8_t* data_out2 =
179             static_cast<uint8_t*>(static_cast<void*>(memory_out2->getPointer()));
180 
181         for (size_t i = 0; i < size; ++i) {
182             data_out1[i] = data_in1[i] + data_in2[i];
183             data_out2[i] = data_in1[i] - data_in2[i];
184         }
185 
186         memory_out1->commit();
187         memory_out2->commit();
188 
189         _hidl_cb(out);
190         return Status::ok();
191     }
192 
193  private:
194     hidl_memory mMemory;
195 };
196 
197 using std::to_string;
198 
usage(const char * me)199 static void usage(const char *me) {
200     fprintf(stderr, "%s [-c]lient | [-s]erver\n", me);
201 }
202 
203 struct HidlEnvironment : public ::testing::Environment {
SetUpHidlEnvironment204     void SetUp() override {
205     }
206 
TearDownHidlEnvironment207     void TearDown() override {
208     }
209 };
210 
211 struct HidlTest : public ::testing::Test {
212     sp<IBaz> baz;
213     sp<ISafeUnion> safeunionInterface;
214 
SetUpHidlTest215     void SetUp() override {
216         using namespace ::android::hardware;
217 
218         ::android::hardware::details::waitForHwService(IBaz::descriptor, "default");
219         baz = IBaz::getService();
220         CHECK(baz != nullptr);
221         CHECK(baz->isRemote());
222 
223         ::android::hardware::details::waitForHwService(ISafeUnion::descriptor, "default");
224         safeunionInterface = ISafeUnion::getService();
225         CHECK(safeunionInterface != nullptr);
226         CHECK(safeunionInterface->isRemote());
227     }
228 
TearDownHidlTest229     void TearDown() override {
230     }
231 };
232 
233 template <typename T>
EXPECT_OK(const::android::hardware::Return<T> & ret)234 static void EXPECT_OK(const ::android::hardware::Return<T> &ret) {
235     EXPECT_TRUE(ret.isOk());
236 }
237 
238 template<typename T, typename S>
isArrayEqual(const T arr1,const S arr2,size_t size)239 static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
240     for(size_t i = 0; i < size; i++)
241         if(arr1[i] != arr2[i])
242             return false;
243     return true;
244 }
245 
TEST_F(HidlTest,GetDescriptorTest)246 TEST_F(HidlTest, GetDescriptorTest) {
247     EXPECT_OK(baz->interfaceDescriptor([&] (const auto &desc) {
248         EXPECT_EQ(desc, IBaz::descriptor);
249     }));
250 }
251 
TEST_F(HidlTest,BazSomeBaseMethodTest)252 TEST_F(HidlTest, BazSomeBaseMethodTest) {
253     EXPECT_OK(baz->someBaseMethod());
254 }
255 
TEST_F(HidlTest,BazSomeOtherBaseMethodTest)256 TEST_F(HidlTest, BazSomeOtherBaseMethodTest) {
257     IBase::Foo foo;
258     foo.x = 1;
259     foo.y.z = 2.5;
260     // A valid UTF-8 string
261     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";
262 
263     foo.aaa.resize(5);
264     for (size_t i = 0; i < foo.aaa.size(); ++i) {
265         foo.aaa[i].z = 1.0f + (float)i * 0.01f;
266         foo.aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
267     }
268 
269     EXPECT_OK(
270             baz->someOtherBaseMethod(
271                 foo,
272                 [&](const auto &result) {
273                     // Strings should have the same size as they did before
274                     // marshaling. b/35038064
275                     EXPECT_EQ(result.y.s.size(), foo.y.s.size());
276                     EXPECT_EQ(foo, result);
277                }));
278 }
279 
TEST_F(HidlTest,SomeOtherBaseMethodInvalidString)280 TEST_F(HidlTest, SomeOtherBaseMethodInvalidString) {
281     Return<bool> isJava = baz->isJava();
282     ASSERT_TRUE(isJava.isOk());
283     if (!isJava) {
284         GTEST_SKIP() << "Test only applies to Java";
285     }
286 
287     IBase::Foo foo {
288         .y = {
289             .s = "\xff",
290         }
291     };
292 
293     auto ret = baz->someOtherBaseMethod(foo, [](const IBase::Foo& ret) {
294         EXPECT_EQ(ret.y.s, "?");  // :)
295     });
296 
297     EXPECT_TRUE(ret.isOk());
298 }
299 
TEST_F(HidlTest,BazSomeMethodWithFooArraysTest)300 TEST_F(HidlTest, BazSomeMethodWithFooArraysTest) {
301     hidl_array<IBase::Foo, 2> foo;
302 
303     foo[0].x = 1;
304     foo[0].y.z = 2.5;
305     foo[0].y.s = "Hello, world";
306 
307     foo[0].aaa.resize(5);
308     for (size_t i = 0; i < foo[0].aaa.size(); ++i) {
309         foo[0].aaa[i].z = 1.0f + (float)i * 0.01f;
310         foo[0].aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
311     }
312 
313     foo[1].x = 2;
314     foo[1].y.z = -2.5;
315     foo[1].y.s = "Morituri te salutant";
316 
317     foo[1].aaa.resize(3);
318     for (size_t i = 0; i < foo[1].aaa.size(); ++i) {
319         foo[1].aaa[i].z = 2.0f - (float)i * 0.01f;
320         foo[1].aaa[i].s = ("Alea iacta est: " + std::to_string(i)).c_str();
321     }
322 
323     hidl_array<IBaz::Foo, 2> fooExpectedOutput;
324     fooExpectedOutput[0] = foo[1];
325     fooExpectedOutput[1] = foo[0];
326 
327     EXPECT_OK(
328             baz->someMethodWithFooArrays(
329                 foo,
330                 [&](const auto &result) {
331                     EXPECT_EQ(result, fooExpectedOutput);
332                }));
333 }
334 
TEST_F(HidlTest,BazSomeMethodWithFooVectorsTest)335 TEST_F(HidlTest, BazSomeMethodWithFooVectorsTest) {
336     hidl_vec<IBase::Foo> foo;
337     foo.resize(2);
338 
339     foo[0].x = 1;
340     foo[0].y.z = 2.5;
341     foo[0].y.s = "Hello, world";
342 
343     foo[0].aaa.resize(5);
344     for (size_t i = 0; i < foo[0].aaa.size(); ++i) {
345         foo[0].aaa[i].z = 1.0f + (float)i * 0.01f;
346         foo[0].aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
347     }
348 
349     foo[1].x = 2;
350     foo[1].y.z = -2.5;
351     foo[1].y.s = "Morituri te salutant";
352 
353     foo[1].aaa.resize(3);
354     for (size_t i = 0; i < foo[1].aaa.size(); ++i) {
355         foo[1].aaa[i].z = 2.0f - (float)i * 0.01f;
356         foo[1].aaa[i].s = ("Alea iacta est: " + std::to_string(i)).c_str();
357     }
358 
359     hidl_vec<IBaz::Foo> fooExpectedOutput;
360     fooExpectedOutput.resize(2);
361     fooExpectedOutput[0] = foo[1];
362     fooExpectedOutput[1] = foo[0];
363 
364     EXPECT_OK(
365             baz->someMethodWithFooVectors(
366                 foo,
367                 [&](const auto &result) {
368                     EXPECT_EQ(result, fooExpectedOutput);
369                 }));
370 }
371 
TEST_F(HidlTest,BazSomeMethodWithVectorOfArray)372 TEST_F(HidlTest, BazSomeMethodWithVectorOfArray) {
373     IBase::VectorOfArray in, expectedOut;
374     in.addresses.resize(3);
375     expectedOut.addresses.resize(3);
376 
377     size_t k = 0;
378     const size_t n = in.addresses.size();
379 
380     for (size_t i = 0; i < n; ++i) {
381         for (size_t j = 0; j < 6; ++j, ++k) {
382             in.addresses[i][j] = k;
383             expectedOut.addresses[n - 1 - i][j] = k;
384         }
385     }
386 
387     EXPECT_OK(
388             baz->someMethodWithVectorOfArray(
389                 in,
390                 [&](const auto &out) {
391                     EXPECT_EQ(expectedOut, out);
392                 }));
393 }
394 
TEST_F(HidlTest,BazSomeMethodTakingAVectorOfArray)395 TEST_F(HidlTest, BazSomeMethodTakingAVectorOfArray) {
396     hidl_vec<hidl_array<uint8_t, 6> > in, expectedOut;
397     in.resize(3);
398     expectedOut.resize(3);
399 
400     size_t k = 0;
401     const size_t n = in.size();
402     for (size_t i = 0; i < n; ++i) {
403         for (size_t j = 0; j < 6; ++j, ++k) {
404             in[i][j] = k;
405             expectedOut[n - 1 - i][j] = k;
406         }
407     }
408 
409     EXPECT_OK(
410             baz->someMethodTakingAVectorOfArray(
411                 in,
412                 [&](const auto &out) {
413                     EXPECT_EQ(expectedOut, out);
414                 }));
415 }
416 
numberToEnglish(int x)417 static std::string numberToEnglish(int x) {
418     static const char *const kDigits[] = {
419         "zero",
420         "one",
421         "two",
422         "three",
423         "four",
424         "five",
425         "six",
426         "seven",
427         "eight",
428         "nine",
429     };
430 
431     if (x < 0) {
432         return "negative " + numberToEnglish(-x);
433     }
434 
435     if (x < 10) {
436         return kDigits[x];
437     }
438 
439     if (x <= 15) {
440         static const char *const kSpecialTens[] = {
441             "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
442         };
443 
444         return kSpecialTens[x - 10];
445     }
446 
447     if (x < 20) {
448         return std::string(kDigits[x % 10]) + "teen";
449     }
450 
451     if (x < 100) {
452         static const char *const kDecades[] = {
453             "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
454             "eighty", "ninety",
455         };
456 
457         return std::string(kDecades[x / 10 - 2]) + kDigits[x % 10];
458     }
459 
460     return "positively huge!";
461 }
462 
TEST_F(HidlTest,BazTransposeTest)463 TEST_F(HidlTest, BazTransposeTest) {
464     IBase::StringMatrix5x3 in;
465     IBase::StringMatrix3x5 expectedOut;
466 
467     for (int i = 0; i < 5; ++i) {
468         for (int j = 0; j < 3; ++j) {
469             in.s[i][j] = expectedOut.s[j][i] = numberToEnglish(3 * i + j + 1).c_str();
470         }
471     }
472 
473     EXPECT_OK(baz->transpose(
474                 in,
475                 [&](const auto &out) {
476                     EXPECT_EQ(expectedOut, out);
477                 }));
478 }
479 
TEST_F(HidlTest,BazTranspose2Test)480 TEST_F(HidlTest, BazTranspose2Test) {
481     hidl_array<hidl_string, 5, 3> in;
482     hidl_array<hidl_string, 3, 5> expectedOut;
483 
484     for (int i = 0; i < 5; ++i) {
485         for (int j = 0; j < 3; ++j) {
486             in[i][j] = expectedOut[j][i] = numberToEnglish(3 * i + j + 1).c_str();
487         }
488     }
489 
490     EXPECT_OK(baz->transpose2(
491                 in,
492                 [&](const auto &out) {
493                     EXPECT_EQ(expectedOut, out);
494                 }));
495 }
496 
TEST_F(HidlTest,BazSomeBoolMethodTest)497 TEST_F(HidlTest, BazSomeBoolMethodTest) {
498     auto result = baz->someBoolMethod(true);
499     EXPECT_OK(result);
500     EXPECT_EQ(result, false);
501 }
502 
TEST_F(HidlTest,BazSomeBoolArrayMethodTest)503 TEST_F(HidlTest, BazSomeBoolArrayMethodTest) {
504     hidl_array<bool, 3> someBoolArray;
505     someBoolArray[0] = true;
506     someBoolArray[1] = false;
507     someBoolArray[2] = true;
508 
509     hidl_array<bool, 4> expectedOut;
510     expectedOut[0] = false;
511     expectedOut[1] = true;
512     expectedOut[2] = false;
513     expectedOut[3] = true;
514 
515     EXPECT_OK(
516             baz->someBoolArrayMethod(
517                 someBoolArray,
518                 [&](const auto &result) {
519                     EXPECT_EQ(expectedOut, result);
520                 }));
521 }
522 
TEST_F(HidlTest,BazSomeBoolVectorMethodTest)523 TEST_F(HidlTest, BazSomeBoolVectorMethodTest) {
524     hidl_vec<bool> someBoolVector, expected;
525     someBoolVector.resize(4);
526     expected.resize(4);
527 
528     for (size_t i = 0; i < someBoolVector.size(); ++i) {
529         someBoolVector[i] = ((i & 1) == 0);
530         expected[i] = !someBoolVector[i];
531     }
532 
533     EXPECT_OK(
534             baz->someBoolVectorMethod(
535                 someBoolVector,
536                 [&](const auto &result) {
537                     EXPECT_EQ(expected, result);
538                 }));
539 }
540 
TEST_F(HidlTest,BazDoThisMethodTest)541 TEST_F(HidlTest, BazDoThisMethodTest) {
542     EXPECT_OK(baz->doThis(1.0f));
543 }
544 
TEST_F(HidlTest,BazDoThatAndReturnSomethingMethodTest)545 TEST_F(HidlTest, BazDoThatAndReturnSomethingMethodTest) {
546     auto result = baz->doThatAndReturnSomething(1);
547     EXPECT_OK(result);
548     EXPECT_EQ(result, 666);
549 }
550 
TEST_F(HidlTest,BazDoQuiteABitMethodTest)551 TEST_F(HidlTest, BazDoQuiteABitMethodTest) {
552     auto result = baz->doQuiteABit(1, 2LL, 3.0f, 4.0);
553 
554     EXPECT_OK(result);
555     EXPECT_EQ(result, 666.5);
556 }
557 
TEST_F(HidlTest,BazDoSomethingElseMethodTest)558 TEST_F(HidlTest, BazDoSomethingElseMethodTest) {
559     hidl_array<int32_t, 15> param;
560     hidl_array<int32_t, 32> expected;
561 
562     for (size_t i = 0; i < 15; ++i) {
563         param[i] = expected[15 + i] = i;
564         expected[i] = 2 * i;
565     }
566 
567     expected[30] = 1;
568     expected[31] = 2;
569 
570     EXPECT_OK(
571             baz->doSomethingElse(
572                 param,
573                 [&](const auto &result) {
574                     EXPECT_EQ(expected, result);
575                 }));
576 }
577 
TEST_F(HidlTest,BazDoStuffAndReturnAStringMethodTest)578 TEST_F(HidlTest, BazDoStuffAndReturnAStringMethodTest) {
579     std::string expected = "Hello, world!";
580     EXPECT_OK(
581             baz->doStuffAndReturnAString(
582                 [&](const auto &result) {
583                     EXPECT_EQ(expected, result);
584                 }));
585 }
586 
TEST_F(HidlTest,BazMapThisVectorMethodTest)587 TEST_F(HidlTest, BazMapThisVectorMethodTest) {
588     hidl_vec<int32_t> vec_param, expected;
589     vec_param.resize(15);
590     expected.resize(15);
591 
592     for (size_t i = 0; i < 15; ++i) {
593         vec_param[i] = i;
594         expected[i] = 2 * i;
595     }
596 
597     EXPECT_OK(
598             baz->mapThisVector(
599                 vec_param,
600                 [&](const auto &result) {
601                     EXPECT_EQ(expected, result);
602                 }));
603 }
604 
TEST_F(HidlTest,BazCallMeMethodTest)605 TEST_F(HidlTest, BazCallMeMethodTest) {
606     EXPECT_OK(baz->callMe(new BazCallback()));
607 }
608 
TEST_F(HidlTest,BazCallMeLaterMethodTest)609 TEST_F(HidlTest, BazCallMeLaterMethodTest) {
610     EXPECT_OK(baz->callMeLater(new BazCallback()));
611     EXPECT_OK(baz->iAmFreeNow());
612 }
613 
TEST_F(HidlTest,BazUseAnEnumMethodTest)614 TEST_F(HidlTest, BazUseAnEnumMethodTest) {
615     auto result = baz->useAnEnum(IBaz::SomeEnum::bar);
616 
617     EXPECT_OK(result);
618     EXPECT_TRUE(result == IBaz::SomeEnum::quux);
619 }
620 
TEST_F(HidlTest,BazHaveSomeStringsMethodTest)621 TEST_F(HidlTest, BazHaveSomeStringsMethodTest) {
622     hidl_array<hidl_string, 3> string_params;
623     string_params[0] = "one";
624     string_params[1] = "two";
625     string_params[2] = "three";
626 
627     hidl_array<hidl_string, 2> expected;
628     expected[0] = "Hello";
629     expected[1] = "World";
630 
631     EXPECT_OK(
632             baz->haveSomeStrings(
633                 string_params,
634                 [&](const auto &result) {
635                     EXPECT_EQ(expected, result);
636                 }));
637 }
638 
TEST_F(HidlTest,BazHaveAStringVecMethodTest)639 TEST_F(HidlTest, BazHaveAStringVecMethodTest) {
640     hidl_vec<hidl_string> string_vec{ "Uno", "Dos", "Tres", "Cuatro" };
641     hidl_vec<hidl_string> expected{"Hello", "World"};
642 
643     EXPECT_OK(
644             baz->haveAStringVec(
645                 string_vec,
646                 [&](const auto &result) {
647                     EXPECT_EQ(expected, result);
648                 }));
649 }
650 
TEST_F(HidlTest,BazRepeatBitfieldVecTest)651 TEST_F(HidlTest, BazRepeatBitfieldVecTest) {
652     hidl_vec<uint8_t> vec{0 | IBaz::BitField::V1, 0 | IBaz::BitField::V2};
653 
654     EXPECT_OK(baz->repeatBitfieldVec(vec, [&](const auto& result) { EXPECT_EQ(vec, result); }));
655 }
656 
TEST_F(HidlTest,BazReturnABunchOfStringsMethodTest)657 TEST_F(HidlTest, BazReturnABunchOfStringsMethodTest) {
658     std::string expectedA = "Eins";
659     std::string expectedB = "Zwei";
660     std::string expectedC = "Drei";
661     EXPECT_OK(
662             baz->returnABunchOfStrings(
663                 [&](const auto &a, const auto &b, const auto &c) {
664                     EXPECT_EQ(a, expectedA);
665                     EXPECT_EQ(b, expectedB);
666                     EXPECT_EQ(c, expectedC);
667                 }));
668 }
669 
TEST_F(HidlTest,BazTestArrays)670 TEST_F(HidlTest, BazTestArrays) {
671     IBase::LotsOfPrimitiveArrays in;
672 
673     for (size_t i = 0; i < 128; ++i) {
674         in.byte1[i] = i;
675         in.boolean1[i] = (i & 4) != 0;
676         in.double1[i] = i;
677     }
678 
679     size_t k = 0;
680     for (size_t i = 0; i < 8; ++i) {
681         for (size_t j = 0; j < 128; ++j, ++k) {
682             in.byte2[i][j] = k;
683             in.boolean2[i][j] = (k & 4) != 0;
684             in.double2[i][j] = k;
685         }
686     }
687 
688     size_t m = 0;
689     for (size_t i = 0; i < 8; ++i) {
690         for (size_t j = 0; j < 16; ++j) {
691             for (size_t k = 0; k < 128; ++k, ++m) {
692                 in.byte3[i][j][k] = m;
693                 in.boolean3[i][j][k] = (m & 4) != 0;
694                 in.double3[i][j][k] = m;
695             }
696         }
697     }
698 
699     EXPECT_OK(
700             baz->testArrays(in,
701                 [&](const auto &out) {
702                     EXPECT_EQ(in, out);
703                 }));
704 }
705 
TEST_F(HidlTest,BazTestByteVecs)706 TEST_F(HidlTest, BazTestByteVecs) {
707     hidl_vec<IBase::ByteOneDim> in;
708     in.resize(8);
709 
710     size_t k = 0;
711     for (size_t i = 0; i < in.size(); ++i) {
712         for (size_t j = 0; j < 128; ++j, ++k) {
713             in[i][j] = k;
714         }
715     }
716 
717     EXPECT_OK(baz->testByteVecs(
718                 in, [&](const auto &out) { EXPECT_EQ(in, out); }));
719 }
720 
TEST_F(HidlTest,BazTestBooleanVecs)721 TEST_F(HidlTest, BazTestBooleanVecs) {
722     hidl_vec<IBase::BooleanOneDim> in;
723     in.resize(8);
724 
725     size_t k = 0;
726     for (size_t i = 0; i < in.size(); ++i) {
727         for (size_t j = 0; j < 128; ++j, ++k) {
728             in[i][j] = (k & 4) != 0;
729         }
730     }
731 
732     EXPECT_OK(baz->testBooleanVecs(
733                 in, [&](const auto &out) { EXPECT_EQ(in, out); }));
734 }
735 
TEST_F(HidlTest,BazTestDoubleVecs)736 TEST_F(HidlTest, BazTestDoubleVecs) {
737     hidl_vec<IBase::DoubleOneDim> in;
738     in.resize(8);
739 
740     size_t k = 0;
741     for (size_t i = 0; i < in.size(); ++i) {
742         for (size_t j = 0; j < 128; ++j, ++k) {
743             in[i][j] = k;
744         }
745     }
746 
747     EXPECT_OK(baz->testDoubleVecs(
748                 in, [&](const auto &out) { EXPECT_EQ(in, out); }));
749 }
750 
TEST_F(HidlTest,TwowayMethodOnewayEnabledTest)751 TEST_F(HidlTest, TwowayMethodOnewayEnabledTest) {
752     using ::android::hardware::IBinder;
753     using ::android::hardware::Parcel;
754 
755     sp<IBinder> binder = ::android::hardware::toBinder(baz);
756 
757     Parcel request, reply;
758     EXPECT_EQ(::android::OK, request.writeInterfaceToken(IBaz::descriptor));
759     EXPECT_EQ(::android::OK, request.writeInt64(1234));
760     // IBaz::doThatAndReturnSomething is two-way but we call it using FLAG_ONEWAY.
761     EXPECT_EQ(::android::OK, binder->transact(19 /*doThatAndReturnSomething*/, request, &reply,
762                                               IBinder::FLAG_ONEWAY));
763 
764     ::android::hardware::Status status;
765     EXPECT_EQ(::android::NOT_ENOUGH_DATA, ::android::hardware::readFromParcel(&status, reply));
766     EXPECT_EQ(::android::hardware::Status::EX_TRANSACTION_FAILED, status.exceptionCode());
767 
768     EXPECT_OK(baz->ping());  // still works
769 }
770 
TEST_F(HidlTest,OnewayMethodOnewayDisabledTest)771 TEST_F(HidlTest, OnewayMethodOnewayDisabledTest) {
772     Return<bool> isJava = baz->isJava();
773     ASSERT_TRUE(isJava.isOk());
774 
775     using ::android::hardware::IBinder;
776     using ::android::hardware::Parcel;
777 
778     sp<IBinder> binder = ::android::hardware::toBinder(baz);
779 
780     Parcel request, reply;
781     EXPECT_EQ(::android::OK, request.writeInterfaceToken(IBaz::descriptor));
782     EXPECT_EQ(::android::OK, request.writeFloat(1.0f));
783     // IBaz::doThis is oneway but we call it without using FLAG_ONEWAY.
784     EXPECT_EQ(
785             // Expect UNKNOWN_ERROR because the JNI class JHwBinder always sets
786             // the reply to UNKNOWN_ERROR for two-way transactions if the
787             // transaction itself did not send a reply.
788             //
789             // C++ does not specifically check this error case.
790             (isJava ? ::android::UNKNOWN_ERROR : 0),
791             binder->transact(18 /*doThis*/, request, &reply, 0 /* Not FLAG_ONEWAY */));
792 
793     EXPECT_OK(baz->ping());  // still works
794 }
795 
TEST_F(HidlTest,SafeUnionNoInitTest)796 TEST_F(HidlTest, SafeUnionNoInitTest) {
797     EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
798         EXPECT_EQ(LargeSafeUnion::hidl_discriminator::noinit, safeUnion.getDiscriminator());
799     }));
800 }
801 
TEST_F(HidlTest,SafeUnionSimpleTest)802 TEST_F(HidlTest, SafeUnionSimpleTest) {
803     EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
804         EXPECT_OK(safeunionInterface->setA(safeUnion, -5, [&](const LargeSafeUnion& safeUnion) {
805             EXPECT_EQ(LargeSafeUnion::hidl_discriminator::a, safeUnion.getDiscriminator());
806             EXPECT_EQ(-5, safeUnion.a());
807 
808             uint64_t max = std::numeric_limits<uint64_t>::max();
809             EXPECT_OK(
810                 safeunionInterface->setD(safeUnion, max, [&](const LargeSafeUnion& safeUnion) {
811                     EXPECT_EQ(LargeSafeUnion::hidl_discriminator::d, safeUnion.getDiscriminator());
812                     EXPECT_EQ(max, safeUnion.d());
813                 }));
814         }));
815     }));
816 }
817 
TEST_F(HidlTest,SafeUnionArrayLikeTypesTest)818 TEST_F(HidlTest, SafeUnionArrayLikeTypesTest) {
819     const std::array<int64_t, 5> testArray{1, -2, 3, -4, 5};
820     const hidl_vec<uint64_t> testVector{std::numeric_limits<uint64_t>::max()};
821 
822     EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
823         EXPECT_OK(
824             safeunionInterface->setF(safeUnion, testArray, [&](const LargeSafeUnion& safeUnion) {
825                 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::f, safeUnion.getDiscriminator());
826 
827                 for (size_t i = 0; i < testArray.size(); i++) {
828                     EXPECT_EQ(testArray[i], safeUnion.f()[i]);
829                 }
830             }));
831 
832         EXPECT_OK(
833             safeunionInterface->setI(safeUnion, testVector, [&](const LargeSafeUnion& safeUnion) {
834                 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::i, safeUnion.getDiscriminator());
835                 EXPECT_EQ(testVector, safeUnion.i());
836             }));
837     }));
838 }
839 
TEST_F(HidlTest,SafeUnionStringTypeTest)840 TEST_F(HidlTest, SafeUnionStringTypeTest) {
841     const std::string testString =
842         "This is an inordinately long test string to exercise hidl_string types in safe unions.";
843 
844     EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
845         EXPECT_OK(safeunionInterface->setG(
846             safeUnion, hidl_string(testString), [&](const LargeSafeUnion& safeUnion) {
847                 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::g, safeUnion.getDiscriminator());
848                 EXPECT_EQ(testString, std::string(safeUnion.g()));
849             }));
850     }));
851 }
852 
TEST_F(HidlTest,SafeUnionNestedTest)853 TEST_F(HidlTest, SafeUnionNestedTest) {
854     SmallSafeUnion smallSafeUnion;
855     smallSafeUnion.a(1);
856 
857     EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& safeUnion) {
858         EXPECT_OK(safeunionInterface->setL(
859             safeUnion, smallSafeUnion, [&](const LargeSafeUnion& safeUnion) {
860                 EXPECT_EQ(LargeSafeUnion::hidl_discriminator::l, safeUnion.getDiscriminator());
861 
862                 EXPECT_EQ(SmallSafeUnion::hidl_discriminator::a, safeUnion.l().getDiscriminator());
863                 EXPECT_EQ(1, safeUnion.l().a());
864             }));
865     }));
866 }
867 
868 // does not check for fd equality
checkNativeHandlesDataEquality(const native_handle_t * reference,const native_handle_t * result)869 static void checkNativeHandlesDataEquality(const native_handle_t* reference,
870                                        const native_handle_t* result) {
871     if (reference == nullptr || result == nullptr) {
872         EXPECT_EQ(reference == nullptr, result == nullptr);
873         return;
874     }
875 
876     ASSERT_NE(reference, result);
877     ASSERT_EQ(reference->version, result->version);
878     EXPECT_EQ(reference->numFds, result->numFds);
879     EXPECT_EQ(reference->numInts, result->numInts);
880 
881     int offset = reference->numFds;
882     int numInts = reference->numInts;
883     EXPECT_TRUE(isArrayEqual(&(reference->data[offset]), &(result->data[offset]), numInts));
884 }
885 
TEST_F(HidlTest,SafeUnionInterfaceNullHandleTest)886 TEST_F(HidlTest, SafeUnionInterfaceNullHandleTest) {
887     InterfaceTypeSafeUnion safeUnion;
888 
889     EXPECT_OK(safeunionInterface->setInterfaceF(
890         safeUnion, hidl_handle(nullptr), [&](const InterfaceTypeSafeUnion& safeUnion) {
891             EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::f,
892                       safeUnion.getDiscriminator());
893 
894             checkNativeHandlesDataEquality(nullptr, safeUnion.f().getNativeHandle());
895         }));
896 }
897 
TEST_F(HidlTest,SafeUnionInterfaceTest)898 TEST_F(HidlTest, SafeUnionInterfaceTest) {
899     const std::array<int8_t, 7> testArray{-1, -2, -3, 0, 1, 2, 3};
900     const hidl_vec<hidl_string> testVector{"So", "Many", "Words"};
901     const std::string testStringA = "Hello";
902     const std::string testStringB = "World";
903 
904     const std::array<int, 6> testHandleData{2, -32, 10, -4329454, 11, 24};
905     native_handle_t* h = native_handle_create(0, testHandleData.size());
906     CHECK(sizeof(testHandleData) == testHandleData.size() * sizeof(int));
907     std::memcpy(h->data, testHandleData.data(), sizeof(testHandleData));
908 
909     std::vector<hidl_handle> testHandlesVector(256);
910     for (size_t i = 0; i < testHandlesVector.size(); i++) {
911         testHandlesVector[i].setTo(native_handle_clone(h), true /* shouldOwn */);
912     }
913 
914     EXPECT_OK(
915         safeunionInterface->newInterfaceTypeSafeUnion([&](const InterfaceTypeSafeUnion& safeUnion) {
916             EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::noinit,
917                       safeUnion.getDiscriminator());
918 
919             EXPECT_OK(safeunionInterface->setInterfaceB(
920                 safeUnion, testArray, [&](const InterfaceTypeSafeUnion& safeUnion) {
921                     EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::b,
922                               safeUnion.getDiscriminator());
923 
924                     for (size_t i = 0; i < testArray.size(); i++) {
925                         EXPECT_EQ(testArray[i], safeUnion.b()[i]);
926                     }
927                 }));
928 
929             EXPECT_OK(safeunionInterface->setInterfaceD(
930                 safeUnion, testStringA, [&](const InterfaceTypeSafeUnion& safeUnion) {
931                     EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::d,
932                               safeUnion.getDiscriminator());
933                     EXPECT_EQ(testStringA, safeUnion.d());
934                 }));
935 
936             EXPECT_OK(safeunionInterface->setInterfaceE(
937                 safeUnion, testVector, [&](const InterfaceTypeSafeUnion& safeUnion) {
938                     EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::e,
939                               safeUnion.getDiscriminator());
940                     EXPECT_EQ(testVector, safeUnion.e());
941                 }));
942 
943             EXPECT_OK(safeunionInterface->setInterfaceF(
944                 safeUnion, hidl_handle(h), [&](const InterfaceTypeSafeUnion& safeUnion) {
945                     EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::f,
946                               safeUnion.getDiscriminator());
947 
948                     const native_handle_t* result = safeUnion.f().getNativeHandle();
949                     checkNativeHandlesDataEquality(h, result);
950                 }));
951 
952             EXPECT_OK(safeunionInterface->setInterfaceG(
953                 safeUnion, testHandlesVector, [&](const InterfaceTypeSafeUnion& safeUnion) {
954                     EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::g,
955                               safeUnion.getDiscriminator());
956 
957                     for (size_t i = 0; i < testHandlesVector.size(); i++) {
958                         checkNativeHandlesDataEquality(h, safeUnion.g()[i].getNativeHandle());
959                     }
960                 }));
961         }));
962 
963     using android::hardware::defaultServiceManager;
964     using android::hardware::interfacesEqual;
965 
966     InterfaceTypeSafeUnion safeUnion;
967     safeUnion.c(defaultServiceManager());
968 
969     EXPECT_EQ(InterfaceTypeSafeUnion::hidl_discriminator::c, safeUnion.getDiscriminator());
970     EXPECT_TRUE(interfacesEqual(safeUnion.c(), defaultServiceManager()));
971 
972     native_handle_delete(h);
973 }
974 
TEST_F(HidlTest,SafeUnionNullHandleTest)975 TEST_F(HidlTest, SafeUnionNullHandleTest) {
976     HandleTypeSafeUnion safeUnion;
977 
978     EXPECT_OK(safeunionInterface->setHandleA(
979         safeUnion, hidl_handle(nullptr), [&](const HandleTypeSafeUnion& safeUnion) {
980             EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::a,
981                       safeUnion.getDiscriminator());
982 
983             checkNativeHandlesDataEquality(nullptr, safeUnion.a().getNativeHandle());
984         }));
985 }
986 
TEST_F(HidlTest,SafeUnionSimpleHandleTest)987 TEST_F(HidlTest, SafeUnionSimpleHandleTest) {
988     const std::array<int, 6> testData{2, -32, 10, -4329454, 11, 24};
989     native_handle_t* h = native_handle_create(0, testData.size());
990     ASSERT_EQ(sizeof(testData), testData.size() * sizeof(int));
991     std::memcpy(h->data, testData.data(), sizeof(testData));
992 
993     std::array<hidl_handle, 5> testArray;
994     for (size_t i = 0; i < testArray.size(); i++) {
995         testArray[i].setTo(native_handle_clone(h), true /* shouldOwn */);
996     }
997 
998     std::vector<hidl_handle> testVector(256);
999     for (size_t i = 0; i < testVector.size(); i++) {
1000         testVector[i].setTo(native_handle_clone(h), true /* shouldOwn */);
1001     }
1002 
1003     EXPECT_OK(
1004         safeunionInterface->newHandleTypeSafeUnion([&](const HandleTypeSafeUnion& safeUnion) {
1005             EXPECT_OK(safeunionInterface->setHandleA(
1006                 safeUnion, hidl_handle(h), [&](const HandleTypeSafeUnion& safeUnion) {
1007                     EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::a,
1008                               safeUnion.getDiscriminator());
1009 
1010                     checkNativeHandlesDataEquality(h, safeUnion.a().getNativeHandle());
1011                 }));
1012 
1013             EXPECT_OK(safeunionInterface->setHandleB(
1014                 safeUnion, testArray, [&](const HandleTypeSafeUnion& safeUnion) {
1015                     EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::b,
1016                               safeUnion.getDiscriminator());
1017 
1018                     for (size_t i = 0; i < testArray.size(); i++) {
1019                         checkNativeHandlesDataEquality(h, safeUnion.b()[i].getNativeHandle());
1020                     }
1021                 }));
1022 
1023             EXPECT_OK(safeunionInterface->setHandleC(
1024                 safeUnion, testVector, [&](const HandleTypeSafeUnion& safeUnion) {
1025                     EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::c,
1026                               safeUnion.getDiscriminator());
1027 
1028                     for (size_t i = 0; i < testVector.size(); i++) {
1029                         checkNativeHandlesDataEquality(h, safeUnion.c()[i].getNativeHandle());
1030                     }
1031                 }));
1032         }));
1033 
1034     native_handle_delete(h);
1035 }
1036 
TEST_F(HidlTest,SafeUnionVecOfHandlesWithOneFdTest)1037 TEST_F(HidlTest, SafeUnionVecOfHandlesWithOneFdTest) {
1038     const std::vector<std::string> testStrings{"This ", "is ", "so ", "much ", "data!\n"};
1039     const std::string testFileName = "/data/local/tmp/SafeUnionVecOfHandlesWithOneFdTest";
1040     const std::array<int, 6> testData{2, -32, 10, -4329454, 11, 24};
1041     ASSERT_EQ(sizeof(testData), testData.size() * sizeof(int));
1042 
1043     const std::string goldenResult = std::accumulate(testStrings.begin(),
1044                                                      testStrings.end(),
1045                                                      std::string());
1046 
1047     int fd = open(testFileName.c_str(), (O_RDWR | O_TRUNC | O_CREAT), (S_IRUSR | S_IWUSR));
1048     ASSERT_TRUE(fd >= 0);
1049 
1050     native_handle* h = native_handle_create(1 /* numFds */, testData.size() /* numInts */);
1051     std::memcpy(&(h->data[1]), testData.data(), sizeof(testData));
1052     h->data[0] = fd;
1053 
1054     hidl_vec<hidl_handle> testHandles(testStrings.size());
1055     for (size_t i = 0; i < testHandles.size(); i++) {
1056         testHandles[i].setTo(native_handle_clone(h), true /* shouldOwn */);
1057     }
1058 
1059     EXPECT_OK(
1060         safeunionInterface->newHandleTypeSafeUnion([&](const HandleTypeSafeUnion& safeUnion) {
1061             EXPECT_OK(safeunionInterface->setHandleC(
1062                 safeUnion, testHandles, [&](const HandleTypeSafeUnion& safeUnion) {
1063                     EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::c,
1064                               safeUnion.getDiscriminator());
1065 
1066                     for (size_t i = 0; i < safeUnion.c().size(); i++) {
1067                         const native_handle_t* reference = testHandles[i].getNativeHandle();
1068                         const native_handle_t* result = safeUnion.c()[i].getNativeHandle();
1069                         checkNativeHandlesDataEquality(reference, result);
1070 
1071                         // Original FDs should be dup'd
1072                         int resultFd = result->data[0];
1073                         EXPECT_NE(reference->data[0], resultFd);
1074 
1075                         EXPECT_TRUE(android::base::WriteStringToFd(testStrings[i], resultFd));
1076                         EXPECT_EQ(0, fsync(resultFd));
1077                     }
1078                 }));
1079         }));
1080 
1081     std::string result;
1082     lseek(fd, 0, SEEK_SET);
1083 
1084     EXPECT_TRUE(android::base::ReadFdToString(fd, &result));
1085     EXPECT_EQ(goldenResult, result);
1086 
1087     native_handle_delete(h);
1088     EXPECT_EQ(0, close(fd));
1089     EXPECT_EQ(0, remove(testFileName.c_str()));
1090 }
1091 
TEST_F(HidlTest,SafeUnionHandleWithMultipleFdsTest)1092 TEST_F(HidlTest, SafeUnionHandleWithMultipleFdsTest) {
1093     const std::vector<std::string> testStrings{"This ", "is ", "so ", "much ", "data!\n"};
1094     const std::string testFileName = "/data/local/tmp/SafeUnionHandleWithMultipleFdsTest";
1095     const std::array<int, 6> testData{2, -32, 10, -4329454, 11, 24};
1096     ASSERT_EQ(sizeof(testData), testData.size() * sizeof(int));
1097 
1098     const std::string goldenResult = std::accumulate(testStrings.begin(),
1099                                                      testStrings.end(),
1100                                                      std::string());
1101 
1102     int fd = open(testFileName.c_str(), (O_RDWR | O_TRUNC | O_CREAT), (S_IRUSR | S_IWUSR));
1103     ASSERT_TRUE(fd >= 0);
1104 
1105     const int numFds = testStrings.size();
1106     native_handle* h = native_handle_create(numFds, testData.size() /* numInts */);
1107     std::memcpy(&(h->data[numFds]), testData.data(), sizeof(testData));
1108     for (size_t i = 0; i < numFds; i++) {
1109         h->data[i] = fd;
1110     }
1111 
1112     hidl_handle testHandle;
1113     testHandle.setTo(h, false /* shouldOwn */);
1114 
1115     EXPECT_OK(
1116         safeunionInterface->newHandleTypeSafeUnion([&](const HandleTypeSafeUnion& safeUnion) {
1117             EXPECT_OK(safeunionInterface->setHandleA(
1118                 safeUnion, testHandle, [&](const HandleTypeSafeUnion& safeUnion) {
1119                     EXPECT_EQ(HandleTypeSafeUnion::hidl_discriminator::a,
1120                               safeUnion.getDiscriminator());
1121 
1122                     const native_handle_t* result = safeUnion.a().getNativeHandle();
1123                     checkNativeHandlesDataEquality(h, result);
1124 
1125                     for (size_t i = 0; i < result->numFds; i++) {
1126                         // Original FDs should be dup'd
1127                         int resultFd = result->data[i];
1128                         EXPECT_NE(h->data[i], resultFd);
1129 
1130                         EXPECT_TRUE(android::base::WriteStringToFd(testStrings[i], resultFd));
1131                         EXPECT_EQ(0, fsync(resultFd));
1132                     }
1133                 }));
1134         }));
1135 
1136     std::string result;
1137     lseek(fd, 0, SEEK_SET);
1138 
1139     EXPECT_TRUE(android::base::ReadFdToString(fd, &result));
1140     EXPECT_EQ(goldenResult, result);
1141 
1142     native_handle_delete(h);
1143     EXPECT_EQ(0, close(fd));
1144     EXPECT_EQ(0, remove(testFileName.c_str()));
1145 }
1146 
TEST_F(HidlTest,SafeUnionEqualityTest)1147 TEST_F(HidlTest, SafeUnionEqualityTest) {
1148     EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& one) {
1149         EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1150             EXPECT_TRUE(one == two);
1151             EXPECT_FALSE(one != two);
1152         }));
1153 
1154         EXPECT_OK(safeunionInterface->setA(one, 1, [&](const LargeSafeUnion& one) {
1155             EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1156                 EXPECT_FALSE(one == two);
1157                 EXPECT_TRUE(one != two);
1158             }));
1159 
1160             EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1161                 EXPECT_OK(safeunionInterface->setB(two, 1, [&](const LargeSafeUnion& two) {
1162                     EXPECT_FALSE(one == two);
1163                     EXPECT_TRUE(one != two);
1164                 }));
1165             }));
1166 
1167             EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1168                 EXPECT_OK(safeunionInterface->setA(two, 2, [&](const LargeSafeUnion& two) {
1169                     EXPECT_FALSE(one == two);
1170                     EXPECT_TRUE(one != two);
1171                 }));
1172             }));
1173 
1174             EXPECT_OK(safeunionInterface->newLargeSafeUnion([&](const LargeSafeUnion& two) {
1175                 EXPECT_OK(safeunionInterface->setA(two, 1, [&](const LargeSafeUnion& two) {
1176                     EXPECT_TRUE(one == two);
1177                     EXPECT_FALSE(one != two);
1178                 }));
1179             }));
1180         }));
1181     }));
1182 }
1183 
1184 template <typename T, size_t start, size_t end>
expectRangeEqual(const T * t,uint8_t byte)1185 void expectRangeEqual(const T* t, uint8_t byte) {
1186     static_assert(start < sizeof(T));
1187     static_assert(end <= sizeof(T));
1188 
1189     const uint8_t* buf = reinterpret_cast<const uint8_t*>(t);
1190 
1191     for (size_t i = start; i < end; i++) {
1192         EXPECT_EQ(byte, buf[i]) << i;
1193     }
1194 }
1195 
TEST_F(HidlTest,UninitTest)1196 TEST_F(HidlTest, UninitTest) {
1197     Return<bool> isJava = baz->isJava();
1198     ASSERT_TRUE(isJava.isOk());
1199     if (!isJava) {
1200         GTEST_SKIP() << "Test only applies to Java";
1201     }
1202 
1203     IBase::Foo foo;
1204     foo.x = 1;
1205     foo.y = {0, ""};
1206 
1207     static_assert(offsetof(IBase::Foo, x) == 0);
1208     static_assert(sizeof(foo.x) == 4);
1209     static_assert(offsetof(IBase::Foo, aaa) == 8);
1210 
1211     uint8_t* buf = reinterpret_cast<uint8_t*>(&foo);
1212     memset(buf + 4, 0xFF, 4);
1213 
1214     // this should not affect the result for remote Java (but would for remote C++)
1215     expectRangeEqual<IBase::Foo, 4, 8>(&foo, 0xFF);
1216 
1217     // run many times, if this error case is hit, it will only be hit
1218     // sometimes.
1219     for (size_t i = 0; i < 100; i++) {
1220         EXPECT_OK(baz->someOtherBaseMethod(
1221                 foo, [](const IBase::Foo& foo) { expectRangeEqual<IBase::Foo, 4, 8>(&foo, 0); }));
1222     }
1223 }
1224 
main(int argc,char ** argv)1225 int main(int argc, char **argv) {
1226     using namespace android::hardware;
1227     details::setTrebleTestingOverride(true);
1228 
1229     const char *me = argv[0];
1230 
1231     bool wantClient = false;
1232     bool wantServer = false;
1233 
1234     int res;
1235     while ((res = getopt(argc, argv, "chs")) >= 0) {
1236         switch (res) {
1237             case 'c':
1238             {
1239                 wantClient = true;
1240                 break;
1241             }
1242 
1243             case 's':
1244             {
1245                 wantServer = true;
1246                 break;
1247             }
1248 
1249             case '?':
1250             case 'h':
1251             default:
1252             {
1253                 usage(me);
1254                 exit(1);
1255                 break;
1256             }
1257         }
1258     }
1259 
1260     if ((!wantClient && !wantServer) || (wantClient && wantServer)) {
1261         usage(me);
1262         exit(1);
1263     }
1264 
1265     if (wantClient) {
1266         ::testing::AddGlobalTestEnvironment(new HidlEnvironment);
1267         ::testing::InitGoogleTest(&argc, argv);
1268         int status = RUN_ALL_TESTS();
1269         return status;
1270     }
1271 
1272     ::android::status_t status;
1273     configureRpcThreadpool(1, true);
1274 
1275     status = registerPassthroughServiceImplementation<IBaz>();
1276     CHECK(status == ::android::OK) << "IBaz didn't register";
1277 
1278     status = registerPassthroughServiceImplementation<ISafeUnion>();
1279     CHECK(status == ::android::OK) << "ISafeUnion didn't register";
1280 
1281     sp<IMemoryInterface> memoryInterface = new MemoryInterface();
1282     status = memoryInterface->registerAsService();
1283     CHECK(status == ::android::OK) << "IMemoryInterface didn't register";
1284 
1285     joinRpcThreadpool();
1286     return 0;
1287 }
1288