1 /*
2  * Copyright (C) 2016 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_TAG "LibHidlTest"
18 
19 #pragma clang diagnostic push
20 #pragma clang diagnostic fatal "-Wpadded"
21 #include <hidl/HidlInternal.h>
22 #include <hidl/HidlSupport.h>
23 #pragma clang diagnostic pop
24 
25 #include <android-base/logging.h>
26 #include <android/hidl/memory/1.0/IMemory.h>
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 #include <hidl/ServiceManagement.h>
30 #include <hidl/Status.h>
31 #include <hidl/TaskRunner.h>
32 #include <condition_variable>
33 #include <fstream>
34 #include <vector>
35 
36 #ifdef __ANDROID__
37 static bool kAndroid = true;
38 #else
39 static bool kAndroid = false;
40 #endif
41 
42 #define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
43 #define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
44         EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
45 
46 template<typename T, typename S>
47 static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
48     for(size_t i = 0; i < size; i++)
49         if(arr1[i] != arr2[i])
50             return false;
51     return true;
52 }
53 
54 template<typename T, typename S>
55 static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
56     for(size_t i = 0; i < size1; i++)
57         for (size_t j = 0; j < size2; j++)
58             if(arr1[i][j] != arr2[i][j])
59                 return false;
60     return true;
61 }
62 
63 bool isLibraryOpen(const std::string& lib) {
64     std::ifstream ifs("/proc/self/maps");
65     for (std::string line; std::getline(ifs, line);) {
66         if (line.size() >= lib.size() && line.substr(line.size() - lib.size()) == lib) {
67             return true;
68         }
69     }
70 
71     return false;
72 }
73 
74 class LibHidlTest : public ::testing::Test {
75 public:
76     virtual void SetUp() override {
77     }
78     virtual void TearDown() override {
79     }
80 };
81 
82 TEST_F(LibHidlTest, StringTest) {
83     using android::hardware::hidl_string;
84     hidl_string s; // empty constructor
85     EXPECT_STREQ(s.c_str(), "");
86     hidl_string s1 = "s1"; // copy = from cstr
87     EXPECT_STREQ(s1.c_str(), "s1");
88     hidl_string s2("s2"); // copy constructor from cstr
89     EXPECT_STREQ(s2.c_str(), "s2");
90     hidl_string s2a(nullptr); // copy constructor from null cstr
91     EXPECT_STREQ("", s2a.c_str());
92     s2a = nullptr; // = from nullptr cstr
93     EXPECT_STREQ(s2a.c_str(), "");
94     hidl_string s3 = hidl_string("s3"); // move =
95     EXPECT_STREQ(s3.c_str(), "s3");
96     hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length
97     EXPECT_STREQ(s4.c_str(), "123");
98     hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
99     EXPECT_STREQ(s5.c_str(), "s5");
100     hidl_string s6(std::string("s6")); // copy constructor from std::string
101     EXPECT_STREQ(s6.c_str(), "s6");
102     hidl_string s7 = std::string("s7"); // copy = from std::string
103     EXPECT_STREQ(s7.c_str(), "s7");
104     hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor
105     EXPECT_STREQ(s8.c_str(), "s7");
106     hidl_string s9 = s8; // copy =  // NOLINT, test the copy operator
107     EXPECT_STREQ(s9.c_str(), "s7");
108     char myCString[20] = "myCString";
109     s.setToExternal(&myCString[0], strlen(myCString));
110     EXPECT_STREQ(s.c_str(), "myCString");
111     myCString[2] = 'D';
112     EXPECT_STREQ(s.c_str(), "myDString");
113     s.clear(); // should not affect myCString
114     EXPECT_STREQ(myCString, "myDString");
115 
116     // casts
117     s = "great";
118     std::string myString = s;
119     const char *anotherCString = s.c_str();
120     EXPECT_EQ(myString, "great");
121     EXPECT_STREQ(anotherCString, "great");
122 
123     const hidl_string t = "not so great";
124     std::string myTString = t;
125     const char * anotherTCString = t.c_str();
126     EXPECT_EQ(myTString, "not so great");
127     EXPECT_STREQ(anotherTCString, "not so great");
128 
129     // Assignment from hidl_string to std::string
130     std::string tgt;
131     hidl_string src("some stuff");
132     tgt = src;
133     EXPECT_STREQ(tgt.c_str(), "some stuff");
134 
135     // Stream output operator
136     hidl_string msg("hidl_string works with operator<<");
137     std::cout << msg;
138 
139     // Comparisons
140     const char * cstr1 = "abc";
141     std::string string1(cstr1);
142     hidl_string hs1(cstr1);
143     const char * cstrE = "abc";
144     std::string stringE(cstrE);
145     hidl_string hsE(cstrE);
146     const char * cstrNE = "ABC";
147     std::string stringNE(cstrNE);
148     hidl_string hsNE(cstrNE);
149     const char * cstr2 = "def";
150     std::string string2(cstr2);
151     hidl_string hs2(cstr2);
152 
153     EXPECT_TRUE(hs1  == hsE);
154     EXPECT_FALSE(hs1 == hsNE);
155     EXPECT_TRUE(hs1  == cstrE);
156     EXPECT_FALSE(hs1 == cstrNE);
157     EXPECT_TRUE(hs1  == stringE);
158     EXPECT_FALSE(hs1 == stringNE);
159     EXPECT_FALSE(hs1 != hsE);
160     EXPECT_TRUE(hs1  != hsNE);
161     EXPECT_FALSE(hs1 != cstrE);
162     EXPECT_TRUE(hs1  != cstrNE);
163     EXPECT_FALSE(hs1 != stringE);
164     EXPECT_TRUE(hs1  != stringNE);
165 
166     EXPECT_TRUE(hs1 < hs2);
167     EXPECT_FALSE(hs2 < hs1);
168     EXPECT_TRUE(hs2 > hs1);
169     EXPECT_FALSE(hs1 > hs2);
170     EXPECT_TRUE(hs1 <= hs1);
171     EXPECT_TRUE(hs1 <= hs2);
172     EXPECT_FALSE(hs2 <= hs1);
173     EXPECT_TRUE(hs1 >= hs1);
174     EXPECT_TRUE(hs2 >= hs1);
175     EXPECT_FALSE(hs2 <= hs1);
176 }
177 
178 TEST_F(LibHidlTest, MemoryTest) {
179     using android::hardware::hidl_memory;
180 
181     hidl_memory mem1 = hidl_memory(); // default constructor
182     hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT
183 
184     EXPECT_EQ(nullptr, mem2.handle());
185 
186     native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
187 
188     hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
189     hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT
190 
191     EXPECT_EQ(mem3.name(), mem4.name());
192     EXPECT_EQ(mem3.size(), mem4.size());
193     EXPECT_NE(nullptr, mem4.handle());
194     EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
195 
196     hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
197     hidl_memory mem6 = mem5; // NOLINT, test copying
198     EXPECT_EQ(nullptr, mem5.handle());
199     EXPECT_EQ(nullptr, mem6.handle());
200 }
201 
202 TEST_F(LibHidlTest, VecInitTest) {
203     using android::hardware::hidl_vec;
204     using std::vector;
205     int32_t array[] = {5, 6, 7};
206     vector<int32_t> v(array, array + 3);
207 
208     hidl_vec<int32_t> hv0(3);  // size
209     EXPECT_EQ(hv0.size(), 3ul);  // cannot say anything about its contents
210 
211     hidl_vec<int32_t> hv1 = v; // copy =
212     EXPECT_ARRAYEQ(hv1, array, 3);
213     EXPECT_ARRAYEQ(hv1, v, 3);
214     hidl_vec<int32_t> hv2(v); // copy constructor
215     EXPECT_ARRAYEQ(hv2, v, 3);
216 
217     vector<int32_t> v2 = hv1; // cast
218     EXPECT_ARRAYEQ(v2, v, 3);
219 
220     hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
221     EXPECT_EQ(v3.size(), 3ul);
222     EXPECT_ARRAYEQ(v3, array, v3.size());
223 }
224 
225 TEST_F(LibHidlTest, VecIterTest) {
226     int32_t array[] = {5, 6, 7};
227     android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
228 
229     auto iter = hv1.begin();    // iterator begin()
230     EXPECT_EQ(*iter++, 5);
231     EXPECT_EQ(*iter, 6);
232     EXPECT_EQ(*++iter, 7);
233     EXPECT_EQ(*iter--, 7);
234     EXPECT_EQ(*iter, 6);
235     EXPECT_EQ(*--iter, 5);
236 
237     iter += 2;
238     EXPECT_EQ(*iter, 7);
239     iter -= 2;
240     EXPECT_EQ(*iter, 5);
241 
242     iter++;
243     EXPECT_EQ(*(iter + 1), 7);
244     EXPECT_EQ(*(1 + iter), 7);
245     EXPECT_EQ(*(iter - 1), 5);
246     EXPECT_EQ(*iter, 6);
247 
248     auto five = iter - 1;
249     auto seven = iter + 1;
250     EXPECT_EQ(seven - five, 2);
251     EXPECT_EQ(five - seven, -2);
252 
253     EXPECT_LT(five, seven);
254     EXPECT_LE(five, seven);
255     EXPECT_GT(seven, five);
256     EXPECT_GE(seven, five);
257 
258     EXPECT_EQ(seven[0], 7);
259     EXPECT_EQ(five[1], 6);
260 }
261 
262 TEST_F(LibHidlTest, VecIterForTest) {
263     using android::hardware::hidl_vec;
264     int32_t array[] = {5, 6, 7};
265     hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
266 
267     int32_t sum = 0;            // range based for loop interoperability
268     for (auto &&i: hv1) {
269         sum += i;
270     }
271     EXPECT_EQ(sum, 5+6+7);
272 
273     for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
274         *iter += 10;
275     }
276     const hidl_vec<int32_t> &v4 = hv1;
277     sum = 0;
278     for (const auto &i : v4) {
279         sum += i;
280     }
281     EXPECT_EQ(sum, 15+16+17);
282 }
283 
284 TEST_F(LibHidlTest, VecEqTest) {
285     android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
286     android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
287     android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
288 
289     // use the == and != operator intentionally here
290     EXPECT_TRUE(hv1 == hv2);
291     EXPECT_TRUE(hv1 != hv3);
292 }
293 
294 TEST_F(LibHidlTest, VecEqInitializerTest) {
295     std::vector<int32_t> reference{5, 6, 7};
296     android::hardware::hidl_vec<int32_t> hv1{1, 2, 3};
297     hv1 = {5, 6, 7};
298     android::hardware::hidl_vec<int32_t> hv2;
299     hv2 = {5, 6, 7};
300     android::hardware::hidl_vec<int32_t> hv3;
301     hv3 = {5, 6, 8};
302 
303     // use the == and != operator intentionally here
304     EXPECT_TRUE(hv1 == hv2);
305     EXPECT_TRUE(hv1 == reference);
306     EXPECT_TRUE(hv1 != hv3);
307 }
308 
309 TEST_F(LibHidlTest, VecRangeCtorTest) {
310     struct ConvertibleType {
311         int val;
312 
313         explicit ConvertibleType(int val) : val(val) {}
314         explicit operator int() const { return val; }
315         bool operator==(const int& other) const { return val == other; }
316     };
317 
318     std::vector<ConvertibleType> input{
319         ConvertibleType(1), ConvertibleType(2), ConvertibleType(3),
320     };
321 
322     android::hardware::hidl_vec<int> hv(input.begin(), input.end());
323 
324     EXPECT_EQ(input.size(), hv.size());
325     int sum = 0;
326     for (unsigned i = 0; i < input.size(); i++) {
327         EXPECT_EQ(input[i], hv[i]);
328         sum += hv[i];
329     }
330     EXPECT_EQ(sum, 1 + 2 + 3);
331 }
332 
333 struct FailsIfCopied {
334     FailsIfCopied() {}
335 
336     // add failure if copied since in general this can be expensive
337     FailsIfCopied(const FailsIfCopied& o) { *this = o; }
338     FailsIfCopied& operator=(const FailsIfCopied&) {
339         ADD_FAILURE() << "FailsIfCopied copied";
340         return *this;
341     }
342 
343     // fine to move this type since in general this is cheaper
344     FailsIfCopied(FailsIfCopied&& o) = default;
345     FailsIfCopied& operator=(FailsIfCopied&&) = default;
346 };
347 
348 TEST_F(LibHidlTest, VecResizeNoCopy) {
349     using android::hardware::hidl_vec;
350 
351     hidl_vec<FailsIfCopied> noCopies;
352     noCopies.resize(3);  // instantiates three elements
353 
354     FailsIfCopied* oldPointer = noCopies.data();
355 
356     noCopies.resize(6);  // should move three elements, not copy
357 
358     // oldPointer should be invalidated at this point.
359     // hidl_vec doesn't currently try to realloc but if it ever switches
360     // to an implementation that does, this test wouldn't do anything.
361     EXPECT_NE(oldPointer, noCopies.data());
362 }
363 
364 TEST_F(LibHidlTest, VecFindTest) {
365     using android::hardware::hidl_vec;
366     hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
367     const hidl_vec<int32_t> hv2 = {1, 2, 3, 4};
368 
369     auto it = hv1.find(20);
370     EXPECT_EQ(20, *it);
371     *it = 21;
372     EXPECT_EQ(21, *it);
373     it = hv1.find(20);
374     EXPECT_EQ(hv1.end(), it);
375     it = hv1.find(21);
376     EXPECT_EQ(21, *it);
377 
378     auto cit = hv2.find(4);
379     EXPECT_EQ(4, *cit);
380 }
381 
382 TEST_F(LibHidlTest, VecContainsTest) {
383     using android::hardware::hidl_vec;
384     hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
385     const hidl_vec<int32_t> hv2 = {0, 1, 2, 3, 4};
386 
387     EXPECT_TRUE(hv1.contains(10));
388     EXPECT_TRUE(hv1.contains(40));
389     EXPECT_FALSE(hv1.contains(1));
390     EXPECT_FALSE(hv1.contains(0));
391     EXPECT_TRUE(hv2.contains(0));
392     EXPECT_FALSE(hv2.contains(10));
393 
394     hv1[0] = 11;
395     EXPECT_FALSE(hv1.contains(10));
396     EXPECT_TRUE(hv1.contains(11));
397 }
398 
399 TEST_F(LibHidlTest, ArrayTest) {
400     using android::hardware::hidl_array;
401     int32_t array[] = {5, 6, 7};
402 
403     hidl_array<int32_t, 3> ha(array);
404     EXPECT_ARRAYEQ(ha, array, 3);
405 }
406 
407 TEST_F(LibHidlTest, TaskRunnerTest) {
408     using android::hardware::details::TaskRunner;
409     using namespace std::chrono_literals;
410 
411     std::condition_variable cv;
412     std::mutex m;
413 
414     TaskRunner tr;
415     tr.start(1 /* limit */);
416     bool flag = false;
417     tr.push([&] {
418         flag = true;
419         cv.notify_all();
420     });
421 
422     std::unique_lock<std::mutex> lock(m);
423 
424     // 1s so this doesn't deadlock. This isn't a performance test.
425     EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;}));
426     EXPECT_TRUE(flag);
427 }
428 
429 TEST_F(LibHidlTest, StringCmpTest) {
430     using android::hardware::hidl_string;
431     const char * s = "good";
432     hidl_string hs(s);
433     EXPECT_NE(hs.c_str(), s);
434 
435     EXPECT_TRUE(hs == s); // operator ==
436     EXPECT_TRUE(s == hs);
437 
438     EXPECT_FALSE(hs != s); // operator ==
439     EXPECT_FALSE(s != hs);
440 }
441 
442 template <typename T>
443 void great(android::hardware::hidl_vec<T>) {}
444 
445 TEST_F(LibHidlTest, VecCopyTest) {
446     android::hardware::hidl_vec<int32_t> v;
447     great(v);
448 }
449 
450 TEST_F(LibHidlTest, StdArrayTest) {
451     using android::hardware::hidl_array;
452     hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
453     std::array<int32_t, 5> stdArray = array;
454     EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
455     hidl_array<int32_t, 5> array2 = stdArray;
456     EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
457 }
458 
459 TEST_F(LibHidlTest, MultiDimStdArrayTest) {
460     using android::hardware::hidl_array;
461     hidl_array<int32_t, 2, 3> array;
462     for (size_t i = 0; i < 2; i++) {
463         for (size_t j = 0; j < 3; j++) {
464             array[i][j] = i + j + i * j;
465         }
466     }
467     std::array<std::array<int32_t, 3>, 2> stdArray = array;
468     EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
469     hidl_array<int32_t, 2, 3> array2 = stdArray;
470     EXPECT_2DARRAYEQ(array, array2, 2, 3);
471 }
472 
473 TEST_F(LibHidlTest, HidlVersionTest) {
474     using android::hardware::hidl_version;
475     hidl_version v1_0{1, 0};
476     EXPECT_EQ(1, v1_0.get_major());
477     EXPECT_EQ(0, v1_0.get_minor());
478     hidl_version v2_0{2, 0};
479     hidl_version v2_1{2, 1};
480     hidl_version v2_2{2, 2};
481     hidl_version v3_0{3, 0};
482     hidl_version v3_0b{3,0};
483 
484     EXPECT_TRUE(v1_0 < v2_0);
485     EXPECT_TRUE(v1_0 != v2_0);
486     EXPECT_TRUE(v2_0 < v2_1);
487     EXPECT_TRUE(v2_1 < v3_0);
488     EXPECT_TRUE(v2_0 > v1_0);
489     EXPECT_TRUE(v2_0 != v1_0);
490     EXPECT_TRUE(v2_1 > v2_0);
491     EXPECT_TRUE(v3_0 > v2_1);
492     EXPECT_TRUE(v3_0 == v3_0b);
493     EXPECT_FALSE(v3_0 != v3_0b);
494     EXPECT_TRUE(v3_0 <= v3_0b);
495     EXPECT_TRUE(v2_2 <= v3_0);
496     EXPECT_TRUE(v3_0 >= v3_0b);
497     EXPECT_TRUE(v3_0 >= v2_2);
498 }
499 
500 TEST_F(LibHidlTest, ReturnMoveTest) {
501     using namespace ::android;
502     using ::android::hardware::Return;
503     using ::android::hardware::Status;
504     Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
505     ret.isOk();
506     ret = {Status::fromStatusT(DEAD_OBJECT)};
507     ret.isOk();
508 }
509 
510 TEST_F(LibHidlTest, ReturnTest) {
511     using ::android::DEAD_OBJECT;
512     using ::android::hardware::Return;
513     using ::android::hardware::Status;
514     using ::android::hardware::hidl_string;
515 
516     EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk());
517     EXPECT_TRUE(Return<void>(Status::ok()).isOk());
518 
519     hidl_string one = "1";
520     hidl_string two = "2";
521     Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT));
522 
523     EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two));
524     EXPECT_EQ(two, ret.withDefault(two));
525 
526     hidl_string&& moved = ret.withDefault(std::move(two));
527     EXPECT_EQ("2", moved);
528 
529     const hidl_string three = "3";
530     EXPECT_EQ(three, ret.withDefault(three));
531 }
532 
533 TEST_F(LibHidlTest, ReturnDies) {
534     using ::android::hardware::Return;
535     using ::android::hardware::Status;
536 
537     EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)); }, "");
538     EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)).isDeadObject(); }, "");
539     EXPECT_DEATH(
540             {
541                 Return<int> ret = Return<int>(Status::fromStatusT(-EBUSY));
542                 int foo = ret;  // should crash here
543                 (void)foo;
544                 ret.isOk();
545             },
546             "");
547 }
548 
549 TEST_F(LibHidlTest, DetectUncheckedReturn) {
550     using ::android::hardware::HidlReturnRestriction;
551     using ::android::hardware::Return;
552     using ::android::hardware::setProcessHidlReturnRestriction;
553     using ::android::hardware::Status;
554 
555     setProcessHidlReturnRestriction(HidlReturnRestriction::FATAL_IF_UNCHECKED);
556 
557     EXPECT_DEATH(
558             {
559                 auto ret = Return<void>(Status::ok());
560                 (void)ret;
561             },
562             "");
563     EXPECT_DEATH(
564             {
565                 auto ret = Return<void>(Status::ok());
566                 ret = Return<void>(Status::ok());
567                 ret.isOk();
568             },
569             "");
570 
571     auto ret = Return<void>(Status::ok());
572     (void)ret.isOk();
573     ret = Return<void>(Status::ok());
574     (void)ret.isOk();
575 
576     setProcessHidlReturnRestriction(HidlReturnRestriction::NONE);
577 }
578 
579 std::string toString(const ::android::hardware::Status &s) {
580     using ::android::hardware::operator<<;
581     std::ostringstream oss;
582     oss << s;
583     return oss.str();
584 }
585 
586 TEST_F(LibHidlTest, StatusStringTest) {
587     using namespace ::android;
588     using ::android::hardware::Status;
589     using ::testing::HasSubstr;
590 
591     EXPECT_EQ(toString(Status::ok()), "No error");
592 
593     EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
594 
595     EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
596 
597     EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
598             HasSubstr("EX_NULL_POINTER"));
599 }
600 
601 TEST_F(LibHidlTest, PreloadTest) {
602     // HIDL doesn't have support to load passthrough implementations on host, but we
603     // could do this by loading implementations from the output directory
604     if (!kAndroid) GTEST_SKIP();
605 
606     using ::android::hardware::preloadPassthroughService;
607     using ::android::hidl::memory::V1_0::IMemory;
608 
609     // installed on all devices by default in both bitnesses and not otherwise a dependency of this
610     // test.
611     static const std::string kLib = "[email protected]";
612 
613     EXPECT_FALSE(isLibraryOpen(kLib));
614     preloadPassthroughService<IMemory>();
615     EXPECT_TRUE(isLibraryOpen(kLib));
616 }
617 
618 template <typename T, size_t start, size_t end>
619 static void assertZeroInRange(const T* t) {
620     static_assert(start < sizeof(T));
621     static_assert(end <= sizeof(T));
622 
623     const uint8_t* ptr = reinterpret_cast<const uint8_t*>(t);
624 
625     for (size_t i = start; i < end; i++) {
626         EXPECT_EQ(0, ptr[i]);
627     }
628 }
629 
630 template <typename T, size_t start, size_t end>
631 static void uninitTest() {
632     uint8_t buf[sizeof(T)];
633     memset(buf, 0xFF, sizeof(T));
634 
635     T* type = new (buf) T;
636     assertZeroInRange<T, start, end>(type);
637     type->~T();
638 }
639 
640 TEST_F(LibHidlTest, HidlVecUninit) {
641     using ::android::hardware::hidl_vec;
642     struct SomeType {};
643     static_assert(sizeof(hidl_vec<SomeType>) == 16);
644 
645     // padding after mOwnsBuffer
646     uninitTest<hidl_vec<SomeType>, 13, 16>();
647 }
648 TEST_F(LibHidlTest, HidlHandleUninit) {
649     using ::android::hardware::hidl_handle;
650     static_assert(sizeof(hidl_handle) == 16);
651 
652     // padding after mOwnsHandle
653     uninitTest<hidl_handle, 9, 16>();
654 }
655 TEST_F(LibHidlTest, HidlStringUninit) {
656     using ::android::hardware::hidl_string;
657     static_assert(sizeof(hidl_string) == 16);
658 
659     // padding after mOwnsBuffer
660     uninitTest<hidl_string, 13, 16>();
661 }
662 
663 int main(int argc, char **argv) {
664     ::testing::InitGoogleTest(&argc, argv);
665     return RUN_ALL_TESTS();
666 }
667