blob: 083cee431aef18bb94e69bfeeee6e5d5d731439e [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
36#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hong44ab6232016-11-22 16:28:24 -080037#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
38 EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
Yifan Hong602b85a2016-10-24 13:40:01 -070039
40template<typename T, typename S>
41static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
42 for(size_t i = 0; i < size; i++)
43 if(arr1[i] != arr2[i])
44 return false;
45 return true;
46}
47
Yifan Hong44ab6232016-11-22 16:28:24 -080048template<typename T, typename S>
49static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
50 for(size_t i = 0; i < size1; i++)
51 for (size_t j = 0; j < size2; j++)
52 if(arr1[i][j] != arr2[i][j])
53 return false;
54 return true;
55}
56
Steven Moreland951fb132017-11-07 14:24:07 -080057bool isLibraryOpen(const std::string& lib) {
58 std::ifstream ifs("/proc/self/maps");
59 for (std::string line; std::getline(ifs, line);) {
60 if (line.size() >= lib.size() && line.substr(line.size() - lib.size()) == lib) {
61 return true;
62 }
63 }
64
65 return false;
66}
67
Yifan Hong602b85a2016-10-24 13:40:01 -070068class LibHidlTest : public ::testing::Test {
69public:
70 virtual void SetUp() override {
71 }
72 virtual void TearDown() override {
73 }
74};
75
76TEST_F(LibHidlTest, StringTest) {
77 using android::hardware::hidl_string;
78 hidl_string s; // empty constructor
79 EXPECT_STREQ(s.c_str(), "");
80 hidl_string s1 = "s1"; // copy = from cstr
81 EXPECT_STREQ(s1.c_str(), "s1");
82 hidl_string s2("s2"); // copy constructor from cstr
83 EXPECT_STREQ(s2.c_str(), "s2");
Steven Morelanda21d84f2017-02-16 09:23:45 -080084 hidl_string s2a(nullptr); // copy constructor from null cstr
Scott Randolpheb0c3372017-04-03 14:07:14 -070085 EXPECT_STREQ("", s2a.c_str());
Steven Moreland153f87a2017-02-28 09:42:26 -080086 s2a = nullptr; // = from nullptr cstr
87 EXPECT_STREQ(s2a.c_str(), "");
Yifan Hong602b85a2016-10-24 13:40:01 -070088 hidl_string s3 = hidl_string("s3"); // move =
89 EXPECT_STREQ(s3.c_str(), "s3");
Steven Moreland53120f72017-01-12 09:39:26 -080090 hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length
91 EXPECT_STREQ(s4.c_str(), "123");
92 hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
93 EXPECT_STREQ(s5.c_str(), "s5");
94 hidl_string s6(std::string("s6")); // copy constructor from std::string
Scott Randolpheb0c3372017-04-03 14:07:14 -070095 EXPECT_STREQ(s6.c_str(), "s6");
Steven Moreland53120f72017-01-12 09:39:26 -080096 hidl_string s7 = std::string("s7"); // copy = from std::string
Scott Randolpheb0c3372017-04-03 14:07:14 -070097 EXPECT_STREQ(s7.c_str(), "s7");
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070098 hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor
Scott Randolpheb0c3372017-04-03 14:07:14 -070099 EXPECT_STREQ(s8.c_str(), "s7");
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700100 hidl_string s9 = s8; // copy = // NOLINT, test the copy operator
Scott Randolpheb0c3372017-04-03 14:07:14 -0700101 EXPECT_STREQ(s9.c_str(), "s7");
Yifan Hong602b85a2016-10-24 13:40:01 -0700102 char myCString[20] = "myCString";
103 s.setToExternal(&myCString[0], strlen(myCString));
Scott Randolpheb0c3372017-04-03 14:07:14 -0700104 EXPECT_STREQ(s.c_str(), "myCString");
Yifan Hong602b85a2016-10-24 13:40:01 -0700105 myCString[2] = 'D';
Scott Randolpheb0c3372017-04-03 14:07:14 -0700106 EXPECT_STREQ(s.c_str(), "myDString");
Yifan Hong602b85a2016-10-24 13:40:01 -0700107 s.clear(); // should not affect myCString
108 EXPECT_STREQ(myCString, "myDString");
Scott Randolphbb840f72016-11-21 14:39:26 -0800109
Yifan Hong602b85a2016-10-24 13:40:01 -0700110 // casts
111 s = "great";
112 std::string myString = s;
Scott Randolpheb0c3372017-04-03 14:07:14 -0700113 const char *anotherCString = s.c_str();
Yifan Hong602b85a2016-10-24 13:40:01 -0700114 EXPECT_EQ(myString, "great");
115 EXPECT_STREQ(anotherCString, "great");
Scott Randolphbb840f72016-11-21 14:39:26 -0800116
Steven Moreland727e3dd2017-03-23 11:19:00 -0700117 const hidl_string t = "not so great";
118 std::string myTString = t;
Scott Randolpheb0c3372017-04-03 14:07:14 -0700119 const char * anotherTCString = t.c_str();
Steven Moreland727e3dd2017-03-23 11:19:00 -0700120 EXPECT_EQ(myTString, "not so great");
121 EXPECT_STREQ(anotherTCString, "not so great");
122
Scott Randolpheb0c3372017-04-03 14:07:14 -0700123 // Assignment from hidl_string to std::string
124 std::string tgt;
125 hidl_string src("some stuff");
126 tgt = src;
127 EXPECT_STREQ(tgt.c_str(), "some stuff");
128
129 // Stream output operator
130 hidl_string msg("hidl_string works with operator<<");
131 std::cout << msg;
132
Scott Randolphbb840f72016-11-21 14:39:26 -0800133 // Comparisons
134 const char * cstr1 = "abc";
135 std::string string1(cstr1);
136 hidl_string hs1(cstr1);
137 const char * cstrE = "abc";
138 std::string stringE(cstrE);
139 hidl_string hsE(cstrE);
140 const char * cstrNE = "ABC";
141 std::string stringNE(cstrNE);
142 hidl_string hsNE(cstrNE);
Steven Moreland551396a2017-02-13 18:33:20 -0800143 const char * cstr2 = "def";
144 std::string string2(cstr2);
145 hidl_string hs2(cstr2);
146
Scott Randolphbb840f72016-11-21 14:39:26 -0800147 EXPECT_TRUE(hs1 == hsE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800148 EXPECT_FALSE(hs1 == hsNE);
149 EXPECT_TRUE(hs1 == cstrE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800150 EXPECT_FALSE(hs1 == cstrNE);
151 EXPECT_TRUE(hs1 == stringE);
Steven Moreland551396a2017-02-13 18:33:20 -0800152 EXPECT_FALSE(hs1 == stringNE);
153 EXPECT_FALSE(hs1 != hsE);
154 EXPECT_TRUE(hs1 != hsNE);
155 EXPECT_FALSE(hs1 != cstrE);
156 EXPECT_TRUE(hs1 != cstrNE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800157 EXPECT_FALSE(hs1 != stringE);
158 EXPECT_TRUE(hs1 != stringNE);
Steven Moreland551396a2017-02-13 18:33:20 -0800159
160 EXPECT_TRUE(hs1 < hs2);
161 EXPECT_FALSE(hs2 < hs1);
162 EXPECT_TRUE(hs2 > hs1);
163 EXPECT_FALSE(hs1 > hs2);
164 EXPECT_TRUE(hs1 <= hs1);
165 EXPECT_TRUE(hs1 <= hs2);
166 EXPECT_FALSE(hs2 <= hs1);
167 EXPECT_TRUE(hs1 >= hs1);
168 EXPECT_TRUE(hs2 >= hs1);
169 EXPECT_FALSE(hs2 <= hs1);
Yifan Hong602b85a2016-10-24 13:40:01 -0700170}
171
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800172TEST_F(LibHidlTest, MemoryTest) {
173 using android::hardware::hidl_memory;
174
175 hidl_memory mem1 = hidl_memory(); // default constructor
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700176 hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800177
178 EXPECT_EQ(nullptr, mem2.handle());
179
180 native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
181
182 hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700183 hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800184
185 EXPECT_EQ(mem3.name(), mem4.name());
186 EXPECT_EQ(mem3.size(), mem4.size());
187 EXPECT_NE(nullptr, mem4.handle());
188 EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
189
190 hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700191 hidl_memory mem6 = mem5; // NOLINT, test copying
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800192 EXPECT_EQ(nullptr, mem5.handle());
193 EXPECT_EQ(nullptr, mem6.handle());
194}
195
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800196TEST_F(LibHidlTest, VecInitTest) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700197 using android::hardware::hidl_vec;
198 using std::vector;
199 int32_t array[] = {5, 6, 7};
200 vector<int32_t> v(array, array + 3);
201
Steven Moreland96e9de02017-09-29 14:11:20 -0700202 hidl_vec<int32_t> hv0(3); // size
Steven Morelandaa661fc2017-10-06 09:40:18 -0700203 EXPECT_EQ(hv0.size(), 3ul); // cannot say anything about its contents
Steven Moreland96e9de02017-09-29 14:11:20 -0700204
Yifan Hong602b85a2016-10-24 13:40:01 -0700205 hidl_vec<int32_t> hv1 = v; // copy =
206 EXPECT_ARRAYEQ(hv1, array, 3);
207 EXPECT_ARRAYEQ(hv1, v, 3);
208 hidl_vec<int32_t> hv2(v); // copy constructor
209 EXPECT_ARRAYEQ(hv2, v, 3);
210
211 vector<int32_t> v2 = hv1; // cast
212 EXPECT_ARRAYEQ(v2, v, 3);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800213
214 hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
Steven Morelandb69926a2016-11-15 10:02:57 -0800215 EXPECT_EQ(v3.size(), 3ul);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800216 EXPECT_ARRAYEQ(v3, array, v3.size());
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800217}
218
219TEST_F(LibHidlTest, VecIterTest) {
220 int32_t array[] = {5, 6, 7};
221 android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
Scott Randolphbb840f72016-11-21 14:39:26 -0800222
223 auto iter = hv1.begin(); // iterator begin()
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800224 EXPECT_EQ(*iter++, 5);
Scott Randolphbb840f72016-11-21 14:39:26 -0800225 EXPECT_EQ(*iter, 6);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800226 EXPECT_EQ(*++iter, 7);
227 EXPECT_EQ(*iter--, 7);
228 EXPECT_EQ(*iter, 6);
229 EXPECT_EQ(*--iter, 5);
230
231 iter += 2;
Scott Randolphbb840f72016-11-21 14:39:26 -0800232 EXPECT_EQ(*iter, 7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800233 iter -= 2;
234 EXPECT_EQ(*iter, 5);
235
236 iter++;
237 EXPECT_EQ(*(iter + 1), 7);
238 EXPECT_EQ(*(1 + iter), 7);
239 EXPECT_EQ(*(iter - 1), 5);
240 EXPECT_EQ(*iter, 6);
241
242 auto five = iter - 1;
243 auto seven = iter + 1;
244 EXPECT_EQ(seven - five, 2);
245 EXPECT_EQ(five - seven, -2);
246
247 EXPECT_LT(five, seven);
248 EXPECT_LE(five, seven);
249 EXPECT_GT(seven, five);
250 EXPECT_GE(seven, five);
251
252 EXPECT_EQ(seven[0], 7);
253 EXPECT_EQ(five[1], 6);
254}
255
256TEST_F(LibHidlTest, VecIterForTest) {
257 using android::hardware::hidl_vec;
258 int32_t array[] = {5, 6, 7};
259 hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
Scott Randolphbb840f72016-11-21 14:39:26 -0800260
261 int32_t sum = 0; // range based for loop interoperability
262 for (auto &&i: hv1) {
263 sum += i;
264 }
265 EXPECT_EQ(sum, 5+6+7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800266
267 for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
268 *iter += 10;
269 }
270 const hidl_vec<int32_t> &v4 = hv1;
271 sum = 0;
272 for (const auto &i : v4) {
273 sum += i;
274 }
275 EXPECT_EQ(sum, 15+16+17);
Yifan Hong602b85a2016-10-24 13:40:01 -0700276}
277
Yifan Hong9fcbb362016-12-20 16:46:41 -0800278TEST_F(LibHidlTest, VecEqTest) {
279 android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
280 android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
281 android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
282
283 // use the == and != operator intentionally here
284 EXPECT_TRUE(hv1 == hv2);
285 EXPECT_TRUE(hv1 != hv3);
286}
287
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800288TEST_F(LibHidlTest, VecEqInitializerTest) {
289 std::vector<int32_t> reference{5, 6, 7};
290 android::hardware::hidl_vec<int32_t> hv1{1, 2, 3};
291 hv1 = {5, 6, 7};
292 android::hardware::hidl_vec<int32_t> hv2;
293 hv2 = {5, 6, 7};
294 android::hardware::hidl_vec<int32_t> hv3;
295 hv3 = {5, 6, 8};
296
297 // use the == and != operator intentionally here
298 EXPECT_TRUE(hv1 == hv2);
299 EXPECT_TRUE(hv1 == reference);
300 EXPECT_TRUE(hv1 != hv3);
301}
302
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700303TEST_F(LibHidlTest, VecRangeCtorTest) {
304 struct ConvertibleType {
305 int val;
306
307 explicit ConvertibleType(int val) : val(val) {}
308 explicit operator int() const { return val; }
309 bool operator==(const int& other) const { return val == other; }
310 };
311
312 std::vector<ConvertibleType> input{
313 ConvertibleType(1), ConvertibleType(2), ConvertibleType(3),
314 };
315
316 android::hardware::hidl_vec<int> hv(input.begin(), input.end());
317
318 EXPECT_EQ(input.size(), hv.size());
319 int sum = 0;
320 for (unsigned i = 0; i < input.size(); i++) {
321 EXPECT_EQ(input[i], hv[i]);
322 sum += hv[i];
323 }
324 EXPECT_EQ(sum, 1 + 2 + 3);
325}
326
Steven Morelanddfbedb82019-06-19 14:51:11 -0700327struct FailsIfCopied {
328 FailsIfCopied() {}
329
330 // add failure if copied since in general this can be expensive
331 FailsIfCopied(const FailsIfCopied& o) { *this = o; }
332 FailsIfCopied& operator=(const FailsIfCopied&) {
333 ADD_FAILURE() << "FailsIfCopied copied";
334 return *this;
335 }
336
337 // fine to move this type since in general this is cheaper
338 FailsIfCopied(FailsIfCopied&& o) = default;
339 FailsIfCopied& operator=(FailsIfCopied&&) = default;
340};
341
342TEST_F(LibHidlTest, VecResizeNoCopy) {
343 using android::hardware::hidl_vec;
344
345 hidl_vec<FailsIfCopied> noCopies;
346 noCopies.resize(3); // instantiates three elements
347
348 FailsIfCopied* oldPointer = noCopies.data();
349
350 noCopies.resize(6); // should move three elements, not copy
351
352 // oldPointer should be invalidated at this point.
353 // hidl_vec doesn't currently try to realloc but if it ever switches
354 // to an implementation that does, this test wouldn't do anything.
355 EXPECT_NE(oldPointer, noCopies.data());
356}
357
Tomasz Wasilczyk93f75922019-07-16 08:44:15 -0700358TEST_F(LibHidlTest, VecFindTest) {
359 using android::hardware::hidl_vec;
360 hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
361 const hidl_vec<int32_t> hv2 = {1, 2, 3, 4};
362
363 auto it = hv1.find(20);
364 EXPECT_EQ(20, *it);
365 *it = 21;
366 EXPECT_EQ(21, *it);
367 it = hv1.find(20);
368 EXPECT_EQ(hv1.end(), it);
369 it = hv1.find(21);
370 EXPECT_EQ(21, *it);
371
372 auto cit = hv2.find(4);
373 EXPECT_EQ(4, *cit);
374}
375
376TEST_F(LibHidlTest, VecContainsTest) {
377 using android::hardware::hidl_vec;
378 hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
379 const hidl_vec<int32_t> hv2 = {0, 1, 2, 3, 4};
380
381 EXPECT_TRUE(hv1.contains(10));
382 EXPECT_TRUE(hv1.contains(40));
383 EXPECT_FALSE(hv1.contains(1));
384 EXPECT_FALSE(hv1.contains(0));
385 EXPECT_TRUE(hv2.contains(0));
386 EXPECT_FALSE(hv2.contains(10));
387
388 hv1[0] = 11;
389 EXPECT_FALSE(hv1.contains(10));
390 EXPECT_TRUE(hv1.contains(11));
391}
392
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800393TEST_F(LibHidlTest, ArrayTest) {
394 using android::hardware::hidl_array;
395 int32_t array[] = {5, 6, 7};
396
397 hidl_array<int32_t, 3> ha(array);
398 EXPECT_ARRAYEQ(ha, array, 3);
399}
400
Yifan Hong5e2318c2016-10-27 17:19:21 -0700401TEST_F(LibHidlTest, TaskRunnerTest) {
Yifan Hong0a351392017-03-20 17:17:52 -0700402 using android::hardware::details::TaskRunner;
Steven Moreland86fd2e12017-08-02 13:26:59 -0700403 using namespace std::chrono_literals;
404
405 std::condition_variable cv;
406 std::mutex m;
407
Yifan Hong5e2318c2016-10-27 17:19:21 -0700408 TaskRunner tr;
Yifan Hong6f667542017-03-20 19:04:05 -0700409 tr.start(1 /* limit */);
Yifan Hong5e2318c2016-10-27 17:19:21 -0700410 bool flag = false;
411 tr.push([&] {
Yifan Hong5e2318c2016-10-27 17:19:21 -0700412 flag = true;
Steven Moreland86fd2e12017-08-02 13:26:59 -0700413 cv.notify_all();
Yifan Hong5e2318c2016-10-27 17:19:21 -0700414 });
Steven Moreland86fd2e12017-08-02 13:26:59 -0700415
416 std::unique_lock<std::mutex> lock(m);
417
418 // 1s so this doesn't deadlock. This isn't a performance test.
419 EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;}));
Yifan Hong5e2318c2016-10-27 17:19:21 -0700420 EXPECT_TRUE(flag);
421}
422
Yifan Hong5708fb42016-10-26 17:50:29 -0700423TEST_F(LibHidlTest, StringCmpTest) {
424 using android::hardware::hidl_string;
425 const char * s = "good";
426 hidl_string hs(s);
427 EXPECT_NE(hs.c_str(), s);
428
429 EXPECT_TRUE(hs == s); // operator ==
430 EXPECT_TRUE(s == hs);
431
432 EXPECT_FALSE(hs != s); // operator ==
433 EXPECT_FALSE(s != hs);
434}
435
Yifan Hong602b85a2016-10-24 13:40:01 -0700436template <typename T>
437void great(android::hardware::hidl_vec<T>) {}
438
439TEST_F(LibHidlTest, VecCopyTest) {
440 android::hardware::hidl_vec<int32_t> v;
441 great(v);
442}
443
Yifan Hong44ab6232016-11-22 16:28:24 -0800444TEST_F(LibHidlTest, StdArrayTest) {
445 using android::hardware::hidl_array;
446 hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
447 std::array<int32_t, 5> stdArray = array;
448 EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
449 hidl_array<int32_t, 5> array2 = stdArray;
450 EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
451}
452
453TEST_F(LibHidlTest, MultiDimStdArrayTest) {
454 using android::hardware::hidl_array;
455 hidl_array<int32_t, 2, 3> array;
456 for (size_t i = 0; i < 2; i++) {
457 for (size_t j = 0; j < 3; j++) {
458 array[i][j] = i + j + i * j;
459 }
460 }
461 std::array<std::array<int32_t, 3>, 2> stdArray = array;
462 EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
463 hidl_array<int32_t, 2, 3> array2 = stdArray;
464 EXPECT_2DARRAYEQ(array, array2, 2, 3);
465}
466
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800467TEST_F(LibHidlTest, HidlVersionTest) {
468 using android::hardware::hidl_version;
469 hidl_version v1_0{1, 0};
470 EXPECT_EQ(1, v1_0.get_major());
471 EXPECT_EQ(0, v1_0.get_minor());
472 hidl_version v2_0{2, 0};
473 hidl_version v2_1{2, 1};
474 hidl_version v2_2{2, 2};
475 hidl_version v3_0{3, 0};
476 hidl_version v3_0b{3,0};
477
478 EXPECT_TRUE(v1_0 < v2_0);
Steven Morelandb328f012018-06-25 16:07:22 -0700479 EXPECT_TRUE(v1_0 != v2_0);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800480 EXPECT_TRUE(v2_0 < v2_1);
481 EXPECT_TRUE(v2_1 < v3_0);
482 EXPECT_TRUE(v2_0 > v1_0);
Steven Morelandb328f012018-06-25 16:07:22 -0700483 EXPECT_TRUE(v2_0 != v1_0);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800484 EXPECT_TRUE(v2_1 > v2_0);
485 EXPECT_TRUE(v3_0 > v2_1);
486 EXPECT_TRUE(v3_0 == v3_0b);
Steven Morelandb328f012018-06-25 16:07:22 -0700487 EXPECT_FALSE(v3_0 != v3_0b);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800488 EXPECT_TRUE(v3_0 <= v3_0b);
489 EXPECT_TRUE(v2_2 <= v3_0);
490 EXPECT_TRUE(v3_0 >= v3_0b);
491 EXPECT_TRUE(v3_0 >= v2_2);
492}
493
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800494TEST_F(LibHidlTest, ReturnMoveTest) {
495 using namespace ::android;
496 using ::android::hardware::Return;
497 using ::android::hardware::Status;
498 Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
499 ret.isOk();
500 ret = {Status::fromStatusT(DEAD_OBJECT)};
501 ret.isOk();
502}
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800503
Steven Moreland49172962017-04-28 16:08:43 -0700504TEST_F(LibHidlTest, ReturnTest) {
505 using ::android::DEAD_OBJECT;
506 using ::android::hardware::Return;
507 using ::android::hardware::Status;
508 using ::android::hardware::hidl_string;
509
510 EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk());
511 EXPECT_TRUE(Return<void>(Status::ok()).isOk());
512
513 hidl_string one = "1";
514 hidl_string two = "2";
515 Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT));
516
517 EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two));
518 EXPECT_EQ(two, ret.withDefault(two));
519
520 hidl_string&& moved = ret.withDefault(std::move(two));
521 EXPECT_EQ("2", moved);
522
523 const hidl_string three = "3";
524 EXPECT_EQ(three, ret.withDefault(three));
525}
526
Steven Moreland9cafed52019-07-15 16:02:35 -0700527TEST_F(LibHidlTest, ReturnDies) {
528 using ::android::hardware::Return;
529 using ::android::hardware::Status;
530
531 EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)); }, "");
532 EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)).isDeadObject(); }, "");
533 EXPECT_DEATH(
534 {
535 Return<int> ret = Return<int>(Status::fromStatusT(-EBUSY));
536 int foo = ret; // should crash here
537 (void)foo;
538 ret.isOk();
539 },
540 "");
541}
542
Yifan Hong2be94182017-03-03 19:07:38 -0800543std::string toString(const ::android::hardware::Status &s) {
544 using ::android::hardware::operator<<;
545 std::ostringstream oss;
546 oss << s;
547 return oss.str();
548}
549
550TEST_F(LibHidlTest, StatusStringTest) {
551 using namespace ::android;
552 using ::android::hardware::Status;
553 using ::testing::HasSubstr;
554
555 EXPECT_EQ(toString(Status::ok()), "No error");
556
557 EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
558
559 EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
560
Yifan Hong2be94182017-03-03 19:07:38 -0800561 EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
562 HasSubstr("EX_NULL_POINTER"));
Steven Moreland951fb132017-11-07 14:24:07 -0800563}
Yifan Hong2be94182017-03-03 19:07:38 -0800564
Steven Moreland951fb132017-11-07 14:24:07 -0800565TEST_F(LibHidlTest, PreloadTest) {
566 using ::android::hardware::preloadPassthroughService;
Steven Moreland138c3502018-11-27 14:27:04 -0800567 using ::android::hidl::memory::V1_0::IMemory;
Steven Moreland951fb132017-11-07 14:24:07 -0800568
Steven Moreland138c3502018-11-27 14:27:04 -0800569 // installed on all devices by default in both bitnesses and not otherwise a dependency of this
570 // test.
571 static const std::string kLib = "android.hidl.memory@1.0-impl.so";
Steven Moreland951fb132017-11-07 14:24:07 -0800572
573 EXPECT_FALSE(isLibraryOpen(kLib));
Steven Moreland138c3502018-11-27 14:27:04 -0800574 preloadPassthroughService<IMemory>();
Steven Moreland951fb132017-11-07 14:24:07 -0800575 EXPECT_TRUE(isLibraryOpen(kLib));
Yifan Hong2be94182017-03-03 19:07:38 -0800576}
577
Steven Morelandf0dd1602019-04-26 14:51:24 -0700578template <typename T, size_t start, size_t end>
579static void assertZeroInRange(const T* t) {
580 static_assert(start < sizeof(T));
581 static_assert(end <= sizeof(T));
582
583 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(t);
584
585 for (size_t i = start; i < end; i++) {
586 EXPECT_EQ(0, ptr[i]);
587 }
588}
589
590template <typename T, size_t start, size_t end>
591static void uninitTest() {
592 uint8_t buf[sizeof(T)];
593 memset(buf, 0xFF, sizeof(T));
594
595 T* type = new (buf) T;
596 assertZeroInRange<T, start, end>(type);
597 type->~T();
598}
599
600TEST_F(LibHidlTest, HidlVecUninit) {
601 using ::android::hardware::hidl_vec;
602 struct SomeType {};
603 static_assert(sizeof(hidl_vec<SomeType>) == 16);
604
605 // padding after mOwnsBuffer
606 uninitTest<hidl_vec<SomeType>, 13, 16>();
607}
608TEST_F(LibHidlTest, HidlHandleUninit) {
609 using ::android::hardware::hidl_handle;
610 static_assert(sizeof(hidl_handle) == 16);
611
612 // padding after mOwnsHandle
613 uninitTest<hidl_handle, 9, 16>();
614}
615TEST_F(LibHidlTest, HidlStringUninit) {
616 using ::android::hardware::hidl_string;
617 static_assert(sizeof(hidl_string) == 16);
618
619 // padding after mOwnsBuffer
620 uninitTest<hidl_string, 13, 16>();
621}
622
Yifan Hong602b85a2016-10-24 13:40:01 -0700623int main(int argc, char **argv) {
624 ::testing::InitGoogleTest(&argc, argv);
625 return RUN_ALL_TESTS();
626}