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