blob: 0a1e97baec90260541295db2ce4f6cbfdb096af0 [file] [log] [blame]
Yifan Hongb93f0502016-10-28 10:42:57 -07001/*
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
Yifan Hong602b85a2016-10-24 13:40:01 -070017#define LOG_TAG "LibHidlTest"
18
Steven Morelandf0dd1602019-04-26 14:51:24 -070019#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
Yifan Hong602b85a2016-10-24 13:40:01 -070025#include <android-base/logging.h>
Steven Moreland138c3502018-11-27 14:27:04 -080026#include <android/hidl/memory/1.0/IMemory.h>
Yifan Hong2be94182017-03-03 19:07:38 -080027#include <gmock/gmock.h>
Yifan Hong602b85a2016-10-24 13:40:01 -070028#include <gtest/gtest.h>
Steven Moreland951fb132017-11-07 14:24:07 -080029#include <hidl/ServiceManagement.h>
Yifan Hong2be94182017-03-03 19:07:38 -080030#include <hidl/Status.h>
Yifan Hong5e2318c2016-10-27 17:19:21 -070031#include <hidl/TaskRunner.h>
Steven Moreland951fb132017-11-07 14:24:07 -080032#include <condition_variable>
33#include <fstream>
Yifan Hong602b85a2016-10-24 13:40:01 -070034#include <vector>
35
Steven Moreland2056cf32019-09-17 17:41:02 -070036#ifdef __ANDROID__
37static bool kAndroid = true;
38#else
39static bool kAndroid = false;
40#endif
41
Yifan Hong602b85a2016-10-24 13:40:01 -070042#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hong44ab6232016-11-22 16:28:24 -080043#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
44 EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
Yifan Hong602b85a2016-10-24 13:40:01 -070045
46template<typename T, typename S>
47static 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
Yifan Hong44ab6232016-11-22 16:28:24 -080054template<typename T, typename S>
55static 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
Steven Moreland951fb132017-11-07 14:24:07 -080063bool 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
Yifan Hong602b85a2016-10-24 13:40:01 -070074class LibHidlTest : public ::testing::Test {
75public:
76 virtual void SetUp() override {
77 }
78 virtual void TearDown() override {
79 }
80};
81
82TEST_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");
Steven Morelanda21d84f2017-02-16 09:23:45 -080090 hidl_string s2a(nullptr); // copy constructor from null cstr
Scott Randolpheb0c3372017-04-03 14:07:14 -070091 EXPECT_STREQ("", s2a.c_str());
Steven Moreland153f87a2017-02-28 09:42:26 -080092 s2a = nullptr; // = from nullptr cstr
93 EXPECT_STREQ(s2a.c_str(), "");
Yifan Hong602b85a2016-10-24 13:40:01 -070094 hidl_string s3 = hidl_string("s3"); // move =
95 EXPECT_STREQ(s3.c_str(), "s3");
Steven Moreland53120f72017-01-12 09:39:26 -080096 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
Scott Randolpheb0c3372017-04-03 14:07:14 -0700101 EXPECT_STREQ(s6.c_str(), "s6");
Steven Moreland53120f72017-01-12 09:39:26 -0800102 hidl_string s7 = std::string("s7"); // copy = from std::string
Scott Randolpheb0c3372017-04-03 14:07:14 -0700103 EXPECT_STREQ(s7.c_str(), "s7");
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700104 hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor
Scott Randolpheb0c3372017-04-03 14:07:14 -0700105 EXPECT_STREQ(s8.c_str(), "s7");
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700106 hidl_string s9 = s8; // copy = // NOLINT, test the copy operator
Scott Randolpheb0c3372017-04-03 14:07:14 -0700107 EXPECT_STREQ(s9.c_str(), "s7");
Yifan Hong602b85a2016-10-24 13:40:01 -0700108 char myCString[20] = "myCString";
109 s.setToExternal(&myCString[0], strlen(myCString));
Scott Randolpheb0c3372017-04-03 14:07:14 -0700110 EXPECT_STREQ(s.c_str(), "myCString");
Yifan Hong602b85a2016-10-24 13:40:01 -0700111 myCString[2] = 'D';
Scott Randolpheb0c3372017-04-03 14:07:14 -0700112 EXPECT_STREQ(s.c_str(), "myDString");
Yifan Hong602b85a2016-10-24 13:40:01 -0700113 s.clear(); // should not affect myCString
114 EXPECT_STREQ(myCString, "myDString");
Scott Randolphbb840f72016-11-21 14:39:26 -0800115
Yifan Hong602b85a2016-10-24 13:40:01 -0700116 // casts
117 s = "great";
118 std::string myString = s;
Scott Randolpheb0c3372017-04-03 14:07:14 -0700119 const char *anotherCString = s.c_str();
Yifan Hong602b85a2016-10-24 13:40:01 -0700120 EXPECT_EQ(myString, "great");
121 EXPECT_STREQ(anotherCString, "great");
Scott Randolphbb840f72016-11-21 14:39:26 -0800122
Steven Moreland727e3dd2017-03-23 11:19:00 -0700123 const hidl_string t = "not so great";
124 std::string myTString = t;
Scott Randolpheb0c3372017-04-03 14:07:14 -0700125 const char * anotherTCString = t.c_str();
Steven Moreland727e3dd2017-03-23 11:19:00 -0700126 EXPECT_EQ(myTString, "not so great");
127 EXPECT_STREQ(anotherTCString, "not so great");
128
Scott Randolpheb0c3372017-04-03 14:07:14 -0700129 // 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
Scott Randolphbb840f72016-11-21 14:39:26 -0800139 // 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);
Steven Moreland551396a2017-02-13 18:33:20 -0800149 const char * cstr2 = "def";
150 std::string string2(cstr2);
151 hidl_string hs2(cstr2);
152
Scott Randolphbb840f72016-11-21 14:39:26 -0800153 EXPECT_TRUE(hs1 == hsE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800154 EXPECT_FALSE(hs1 == hsNE);
155 EXPECT_TRUE(hs1 == cstrE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800156 EXPECT_FALSE(hs1 == cstrNE);
157 EXPECT_TRUE(hs1 == stringE);
Steven Moreland551396a2017-02-13 18:33:20 -0800158 EXPECT_FALSE(hs1 == stringNE);
159 EXPECT_FALSE(hs1 != hsE);
160 EXPECT_TRUE(hs1 != hsNE);
161 EXPECT_FALSE(hs1 != cstrE);
162 EXPECT_TRUE(hs1 != cstrNE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800163 EXPECT_FALSE(hs1 != stringE);
164 EXPECT_TRUE(hs1 != stringNE);
Steven Moreland551396a2017-02-13 18:33:20 -0800165
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);
Yifan Hong602b85a2016-10-24 13:40:01 -0700176}
177
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800178TEST_F(LibHidlTest, MemoryTest) {
179 using android::hardware::hidl_memory;
180
181 hidl_memory mem1 = hidl_memory(); // default constructor
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700182 hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800183
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
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700189 hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800190
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
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700197 hidl_memory mem6 = mem5; // NOLINT, test copying
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800198 EXPECT_EQ(nullptr, mem5.handle());
199 EXPECT_EQ(nullptr, mem6.handle());
200}
201
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800202TEST_F(LibHidlTest, VecInitTest) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700203 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
Steven Moreland96e9de02017-09-29 14:11:20 -0700208 hidl_vec<int32_t> hv0(3); // size
Steven Morelandaa661fc2017-10-06 09:40:18 -0700209 EXPECT_EQ(hv0.size(), 3ul); // cannot say anything about its contents
Steven Moreland96e9de02017-09-29 14:11:20 -0700210
Yifan Hong602b85a2016-10-24 13:40:01 -0700211 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);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800219
220 hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
Steven Morelandb69926a2016-11-15 10:02:57 -0800221 EXPECT_EQ(v3.size(), 3ul);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800222 EXPECT_ARRAYEQ(v3, array, v3.size());
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800223}
224
Steven Morelande873cf32020-04-03 10:55:12 -0700225TEST_F(LibHidlTest, VecReleaseTest) {
226 // this test indicates an inconsistency of behaviors which is undesirable.
227 // Perhaps hidl-vec should always allocate an empty vector whenever it
228 // exposes its data. Alternatively, perhaps it should always free/reject
229 // empty vectors and always return nullptr for this state. While this second
230 // alternative is faster, it makes client code harder to write, and it would
231 // break existing client code.
232 using android::hardware::hidl_vec;
233
234 hidl_vec<int32_t> empty;
235 EXPECT_EQ(nullptr, empty.releaseData());
236
237 empty.resize(0);
238 int32_t* data = empty.releaseData();
239 EXPECT_NE(nullptr, data);
240 delete data;
241}
242
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800243TEST_F(LibHidlTest, VecIterTest) {
244 int32_t array[] = {5, 6, 7};
245 android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
Scott Randolphbb840f72016-11-21 14:39:26 -0800246
247 auto iter = hv1.begin(); // iterator begin()
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800248 EXPECT_EQ(*iter++, 5);
Scott Randolphbb840f72016-11-21 14:39:26 -0800249 EXPECT_EQ(*iter, 6);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800250 EXPECT_EQ(*++iter, 7);
251 EXPECT_EQ(*iter--, 7);
252 EXPECT_EQ(*iter, 6);
253 EXPECT_EQ(*--iter, 5);
254
255 iter += 2;
Scott Randolphbb840f72016-11-21 14:39:26 -0800256 EXPECT_EQ(*iter, 7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800257 iter -= 2;
258 EXPECT_EQ(*iter, 5);
259
260 iter++;
261 EXPECT_EQ(*(iter + 1), 7);
262 EXPECT_EQ(*(1 + iter), 7);
263 EXPECT_EQ(*(iter - 1), 5);
264 EXPECT_EQ(*iter, 6);
265
266 auto five = iter - 1;
267 auto seven = iter + 1;
268 EXPECT_EQ(seven - five, 2);
269 EXPECT_EQ(five - seven, -2);
270
271 EXPECT_LT(five, seven);
272 EXPECT_LE(five, seven);
273 EXPECT_GT(seven, five);
274 EXPECT_GE(seven, five);
275
276 EXPECT_EQ(seven[0], 7);
277 EXPECT_EQ(five[1], 6);
278}
279
280TEST_F(LibHidlTest, VecIterForTest) {
281 using android::hardware::hidl_vec;
282 int32_t array[] = {5, 6, 7};
283 hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
Scott Randolphbb840f72016-11-21 14:39:26 -0800284
285 int32_t sum = 0; // range based for loop interoperability
286 for (auto &&i: hv1) {
287 sum += i;
288 }
289 EXPECT_EQ(sum, 5+6+7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800290
291 for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
292 *iter += 10;
293 }
294 const hidl_vec<int32_t> &v4 = hv1;
295 sum = 0;
296 for (const auto &i : v4) {
297 sum += i;
298 }
299 EXPECT_EQ(sum, 15+16+17);
Yifan Hong602b85a2016-10-24 13:40:01 -0700300}
301
Yifan Hong9fcbb362016-12-20 16:46:41 -0800302TEST_F(LibHidlTest, VecEqTest) {
303 android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
304 android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
305 android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
306
307 // use the == and != operator intentionally here
308 EXPECT_TRUE(hv1 == hv2);
309 EXPECT_TRUE(hv1 != hv3);
310}
311
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800312TEST_F(LibHidlTest, VecEqInitializerTest) {
313 std::vector<int32_t> reference{5, 6, 7};
314 android::hardware::hidl_vec<int32_t> hv1{1, 2, 3};
315 hv1 = {5, 6, 7};
316 android::hardware::hidl_vec<int32_t> hv2;
317 hv2 = {5, 6, 7};
318 android::hardware::hidl_vec<int32_t> hv3;
319 hv3 = {5, 6, 8};
320
321 // use the == and != operator intentionally here
322 EXPECT_TRUE(hv1 == hv2);
323 EXPECT_TRUE(hv1 == reference);
324 EXPECT_TRUE(hv1 != hv3);
325}
326
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700327TEST_F(LibHidlTest, VecRangeCtorTest) {
328 struct ConvertibleType {
329 int val;
330
331 explicit ConvertibleType(int val) : val(val) {}
332 explicit operator int() const { return val; }
333 bool operator==(const int& other) const { return val == other; }
334 };
335
336 std::vector<ConvertibleType> input{
337 ConvertibleType(1), ConvertibleType(2), ConvertibleType(3),
338 };
339
340 android::hardware::hidl_vec<int> hv(input.begin(), input.end());
341
342 EXPECT_EQ(input.size(), hv.size());
343 int sum = 0;
344 for (unsigned i = 0; i < input.size(); i++) {
345 EXPECT_EQ(input[i], hv[i]);
346 sum += hv[i];
347 }
348 EXPECT_EQ(sum, 1 + 2 + 3);
349}
350
Steven Morelanddfbedb82019-06-19 14:51:11 -0700351struct FailsIfCopied {
352 FailsIfCopied() {}
353
354 // add failure if copied since in general this can be expensive
355 FailsIfCopied(const FailsIfCopied& o) { *this = o; }
356 FailsIfCopied& operator=(const FailsIfCopied&) {
357 ADD_FAILURE() << "FailsIfCopied copied";
358 return *this;
359 }
360
361 // fine to move this type since in general this is cheaper
362 FailsIfCopied(FailsIfCopied&& o) = default;
363 FailsIfCopied& operator=(FailsIfCopied&&) = default;
364};
365
366TEST_F(LibHidlTest, VecResizeNoCopy) {
367 using android::hardware::hidl_vec;
368
369 hidl_vec<FailsIfCopied> noCopies;
370 noCopies.resize(3); // instantiates three elements
371
372 FailsIfCopied* oldPointer = noCopies.data();
373
374 noCopies.resize(6); // should move three elements, not copy
375
376 // oldPointer should be invalidated at this point.
377 // hidl_vec doesn't currently try to realloc but if it ever switches
378 // to an implementation that does, this test wouldn't do anything.
379 EXPECT_NE(oldPointer, noCopies.data());
380}
381
Tomasz Wasilczyk93f75922019-07-16 08:44:15 -0700382TEST_F(LibHidlTest, VecFindTest) {
383 using android::hardware::hidl_vec;
384 hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
385 const hidl_vec<int32_t> hv2 = {1, 2, 3, 4};
386
387 auto it = hv1.find(20);
388 EXPECT_EQ(20, *it);
389 *it = 21;
390 EXPECT_EQ(21, *it);
391 it = hv1.find(20);
392 EXPECT_EQ(hv1.end(), it);
393 it = hv1.find(21);
394 EXPECT_EQ(21, *it);
395
396 auto cit = hv2.find(4);
397 EXPECT_EQ(4, *cit);
398}
399
400TEST_F(LibHidlTest, VecContainsTest) {
401 using android::hardware::hidl_vec;
402 hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
403 const hidl_vec<int32_t> hv2 = {0, 1, 2, 3, 4};
404
405 EXPECT_TRUE(hv1.contains(10));
406 EXPECT_TRUE(hv1.contains(40));
407 EXPECT_FALSE(hv1.contains(1));
408 EXPECT_FALSE(hv1.contains(0));
409 EXPECT_TRUE(hv2.contains(0));
410 EXPECT_FALSE(hv2.contains(10));
411
412 hv1[0] = 11;
413 EXPECT_FALSE(hv1.contains(10));
414 EXPECT_TRUE(hv1.contains(11));
415}
416
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800417TEST_F(LibHidlTest, ArrayTest) {
418 using android::hardware::hidl_array;
419 int32_t array[] = {5, 6, 7};
420
421 hidl_array<int32_t, 3> ha(array);
422 EXPECT_ARRAYEQ(ha, array, 3);
423}
424
Yifan Hong5e2318c2016-10-27 17:19:21 -0700425TEST_F(LibHidlTest, TaskRunnerTest) {
Yifan Hong0a351392017-03-20 17:17:52 -0700426 using android::hardware::details::TaskRunner;
Steven Moreland86fd2e12017-08-02 13:26:59 -0700427 using namespace std::chrono_literals;
428
429 std::condition_variable cv;
430 std::mutex m;
431
Yifan Hong5e2318c2016-10-27 17:19:21 -0700432 TaskRunner tr;
Yifan Hong6f667542017-03-20 19:04:05 -0700433 tr.start(1 /* limit */);
Yifan Hong5e2318c2016-10-27 17:19:21 -0700434 bool flag = false;
435 tr.push([&] {
Yifan Hong5e2318c2016-10-27 17:19:21 -0700436 flag = true;
Steven Moreland86fd2e12017-08-02 13:26:59 -0700437 cv.notify_all();
Yifan Hong5e2318c2016-10-27 17:19:21 -0700438 });
Steven Moreland86fd2e12017-08-02 13:26:59 -0700439
440 std::unique_lock<std::mutex> lock(m);
441
442 // 1s so this doesn't deadlock. This isn't a performance test.
443 EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;}));
Yifan Hong5e2318c2016-10-27 17:19:21 -0700444 EXPECT_TRUE(flag);
445}
446
Yifan Hong5708fb42016-10-26 17:50:29 -0700447TEST_F(LibHidlTest, StringCmpTest) {
448 using android::hardware::hidl_string;
449 const char * s = "good";
450 hidl_string hs(s);
451 EXPECT_NE(hs.c_str(), s);
452
453 EXPECT_TRUE(hs == s); // operator ==
454 EXPECT_TRUE(s == hs);
455
456 EXPECT_FALSE(hs != s); // operator ==
457 EXPECT_FALSE(s != hs);
458}
459
Yifan Hong602b85a2016-10-24 13:40:01 -0700460template <typename T>
461void great(android::hardware::hidl_vec<T>) {}
462
463TEST_F(LibHidlTest, VecCopyTest) {
464 android::hardware::hidl_vec<int32_t> v;
465 great(v);
466}
467
Yifan Hong44ab6232016-11-22 16:28:24 -0800468TEST_F(LibHidlTest, StdArrayTest) {
469 using android::hardware::hidl_array;
470 hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
471 std::array<int32_t, 5> stdArray = array;
472 EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
473 hidl_array<int32_t, 5> array2 = stdArray;
474 EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
475}
476
477TEST_F(LibHidlTest, MultiDimStdArrayTest) {
478 using android::hardware::hidl_array;
479 hidl_array<int32_t, 2, 3> array;
480 for (size_t i = 0; i < 2; i++) {
481 for (size_t j = 0; j < 3; j++) {
482 array[i][j] = i + j + i * j;
483 }
484 }
485 std::array<std::array<int32_t, 3>, 2> stdArray = array;
486 EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
487 hidl_array<int32_t, 2, 3> array2 = stdArray;
488 EXPECT_2DARRAYEQ(array, array2, 2, 3);
489}
490
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800491TEST_F(LibHidlTest, HidlVersionTest) {
492 using android::hardware::hidl_version;
493 hidl_version v1_0{1, 0};
494 EXPECT_EQ(1, v1_0.get_major());
495 EXPECT_EQ(0, v1_0.get_minor());
496 hidl_version v2_0{2, 0};
497 hidl_version v2_1{2, 1};
498 hidl_version v2_2{2, 2};
499 hidl_version v3_0{3, 0};
500 hidl_version v3_0b{3,0};
501
502 EXPECT_TRUE(v1_0 < v2_0);
Steven Morelandb328f012018-06-25 16:07:22 -0700503 EXPECT_TRUE(v1_0 != v2_0);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800504 EXPECT_TRUE(v2_0 < v2_1);
505 EXPECT_TRUE(v2_1 < v3_0);
506 EXPECT_TRUE(v2_0 > v1_0);
Steven Morelandb328f012018-06-25 16:07:22 -0700507 EXPECT_TRUE(v2_0 != v1_0);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800508 EXPECT_TRUE(v2_1 > v2_0);
509 EXPECT_TRUE(v3_0 > v2_1);
510 EXPECT_TRUE(v3_0 == v3_0b);
Steven Morelandb328f012018-06-25 16:07:22 -0700511 EXPECT_FALSE(v3_0 != v3_0b);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800512 EXPECT_TRUE(v3_0 <= v3_0b);
513 EXPECT_TRUE(v2_2 <= v3_0);
514 EXPECT_TRUE(v3_0 >= v3_0b);
515 EXPECT_TRUE(v3_0 >= v2_2);
516}
517
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800518TEST_F(LibHidlTest, ReturnMoveTest) {
519 using namespace ::android;
520 using ::android::hardware::Return;
521 using ::android::hardware::Status;
522 Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
523 ret.isOk();
524 ret = {Status::fromStatusT(DEAD_OBJECT)};
525 ret.isOk();
526}
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800527
Steven Moreland49172962017-04-28 16:08:43 -0700528TEST_F(LibHidlTest, ReturnTest) {
529 using ::android::DEAD_OBJECT;
530 using ::android::hardware::Return;
531 using ::android::hardware::Status;
532 using ::android::hardware::hidl_string;
533
534 EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk());
535 EXPECT_TRUE(Return<void>(Status::ok()).isOk());
536
537 hidl_string one = "1";
538 hidl_string two = "2";
539 Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT));
540
541 EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two));
542 EXPECT_EQ(two, ret.withDefault(two));
543
544 hidl_string&& moved = ret.withDefault(std::move(two));
545 EXPECT_EQ("2", moved);
546
547 const hidl_string three = "3";
548 EXPECT_EQ(three, ret.withDefault(three));
549}
550
Steven Moreland9cafed52019-07-15 16:02:35 -0700551TEST_F(LibHidlTest, ReturnDies) {
552 using ::android::hardware::Return;
553 using ::android::hardware::Status;
554
555 EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)); }, "");
556 EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)).isDeadObject(); }, "");
557 EXPECT_DEATH(
558 {
559 Return<int> ret = Return<int>(Status::fromStatusT(-EBUSY));
560 int foo = ret; // should crash here
561 (void)foo;
562 ret.isOk();
563 },
564 "");
565}
566
Steven Morelandd85fcd12019-11-05 16:39:53 -0800567TEST_F(LibHidlTest, DetectUncheckedReturn) {
568 using ::android::hardware::HidlReturnRestriction;
569 using ::android::hardware::Return;
570 using ::android::hardware::setProcessHidlReturnRestriction;
571 using ::android::hardware::Status;
572
573 setProcessHidlReturnRestriction(HidlReturnRestriction::FATAL_IF_UNCHECKED);
574
575 EXPECT_DEATH(
576 {
577 auto ret = Return<void>(Status::ok());
578 (void)ret;
579 },
580 "");
581 EXPECT_DEATH(
582 {
583 auto ret = Return<void>(Status::ok());
584 ret = Return<void>(Status::ok());
585 ret.isOk();
586 },
587 "");
588
589 auto ret = Return<void>(Status::ok());
590 (void)ret.isOk();
591 ret = Return<void>(Status::ok());
592 (void)ret.isOk();
593
594 setProcessHidlReturnRestriction(HidlReturnRestriction::NONE);
595}
596
Yifan Hong2be94182017-03-03 19:07:38 -0800597std::string toString(const ::android::hardware::Status &s) {
598 using ::android::hardware::operator<<;
599 std::ostringstream oss;
600 oss << s;
601 return oss.str();
602}
603
604TEST_F(LibHidlTest, StatusStringTest) {
605 using namespace ::android;
606 using ::android::hardware::Status;
607 using ::testing::HasSubstr;
608
609 EXPECT_EQ(toString(Status::ok()), "No error");
610
611 EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
612
613 EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
614
Yifan Hong2be94182017-03-03 19:07:38 -0800615 EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
616 HasSubstr("EX_NULL_POINTER"));
Steven Moreland951fb132017-11-07 14:24:07 -0800617}
Yifan Hong2be94182017-03-03 19:07:38 -0800618
Steven Moreland951fb132017-11-07 14:24:07 -0800619TEST_F(LibHidlTest, PreloadTest) {
Steven Moreland2056cf32019-09-17 17:41:02 -0700620 // HIDL doesn't have support to load passthrough implementations on host, but we
621 // could do this by loading implementations from the output directory
622 if (!kAndroid) GTEST_SKIP();
623
Steven Moreland951fb132017-11-07 14:24:07 -0800624 using ::android::hardware::preloadPassthroughService;
Steven Moreland138c3502018-11-27 14:27:04 -0800625 using ::android::hidl::memory::V1_0::IMemory;
Steven Moreland951fb132017-11-07 14:24:07 -0800626
Steven Moreland138c3502018-11-27 14:27:04 -0800627 // installed on all devices by default in both bitnesses and not otherwise a dependency of this
628 // test.
629 static const std::string kLib = "android.hidl.memory@1.0-impl.so";
Steven Moreland951fb132017-11-07 14:24:07 -0800630
631 EXPECT_FALSE(isLibraryOpen(kLib));
Steven Moreland138c3502018-11-27 14:27:04 -0800632 preloadPassthroughService<IMemory>();
Steven Moreland951fb132017-11-07 14:24:07 -0800633 EXPECT_TRUE(isLibraryOpen(kLib));
Yifan Hong2be94182017-03-03 19:07:38 -0800634}
635
Steven Morelandf0dd1602019-04-26 14:51:24 -0700636template <typename T, size_t start, size_t end>
637static void assertZeroInRange(const T* t) {
638 static_assert(start < sizeof(T));
639 static_assert(end <= sizeof(T));
640
641 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(t);
642
643 for (size_t i = start; i < end; i++) {
644 EXPECT_EQ(0, ptr[i]);
645 }
646}
647
648template <typename T, size_t start, size_t end>
649static void uninitTest() {
650 uint8_t buf[sizeof(T)];
651 memset(buf, 0xFF, sizeof(T));
652
653 T* type = new (buf) T;
654 assertZeroInRange<T, start, end>(type);
655 type->~T();
656}
657
658TEST_F(LibHidlTest, HidlVecUninit) {
659 using ::android::hardware::hidl_vec;
660 struct SomeType {};
661 static_assert(sizeof(hidl_vec<SomeType>) == 16);
662
663 // padding after mOwnsBuffer
664 uninitTest<hidl_vec<SomeType>, 13, 16>();
665}
666TEST_F(LibHidlTest, HidlHandleUninit) {
667 using ::android::hardware::hidl_handle;
668 static_assert(sizeof(hidl_handle) == 16);
669
670 // padding after mOwnsHandle
671 uninitTest<hidl_handle, 9, 16>();
672}
673TEST_F(LibHidlTest, HidlStringUninit) {
674 using ::android::hardware::hidl_string;
675 static_assert(sizeof(hidl_string) == 16);
676
677 // padding after mOwnsBuffer
678 uninitTest<hidl_string, 13, 16>();
679}
680
Yifan Hong602b85a2016-10-24 13:40:01 -0700681int main(int argc, char **argv) {
682 ::testing::InitGoogleTest(&argc, argv);
683 return RUN_ALL_TESTS();
684}