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