blob: 658a7eb840e74df76bbdfe31d3bc80c00840bc2c [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
19#include <android-base/logging.h>
Yifan Hong2be94182017-03-03 19:07:38 -080020#include <gmock/gmock.h>
Yifan Hong602b85a2016-10-24 13:40:01 -070021#include <gtest/gtest.h>
22#include <hidl/HidlSupport.h>
Yifan Hong2be94182017-03-03 19:07:38 -080023#include <hidl/Status.h>
Yifan Hong5e2318c2016-10-27 17:19:21 -070024#include <hidl/TaskRunner.h>
Yifan Hong602b85a2016-10-24 13:40:01 -070025#include <vector>
26
27#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hong44ab6232016-11-22 16:28:24 -080028#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
29 EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
Yifan Hong602b85a2016-10-24 13:40:01 -070030
31template<typename T, typename S>
32static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
33 for(size_t i = 0; i < size; i++)
34 if(arr1[i] != arr2[i])
35 return false;
36 return true;
37}
38
Yifan Hong44ab6232016-11-22 16:28:24 -080039template<typename T, typename S>
40static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
41 for(size_t i = 0; i < size1; i++)
42 for (size_t j = 0; j < size2; j++)
43 if(arr1[i][j] != arr2[i][j])
44 return false;
45 return true;
46}
47
Yifan Hong602b85a2016-10-24 13:40:01 -070048class LibHidlTest : public ::testing::Test {
49public:
50 virtual void SetUp() override {
51 }
52 virtual void TearDown() override {
53 }
54};
55
56TEST_F(LibHidlTest, StringTest) {
57 using android::hardware::hidl_string;
58 hidl_string s; // empty constructor
59 EXPECT_STREQ(s.c_str(), "");
60 hidl_string s1 = "s1"; // copy = from cstr
61 EXPECT_STREQ(s1.c_str(), "s1");
62 hidl_string s2("s2"); // copy constructor from cstr
63 EXPECT_STREQ(s2.c_str(), "s2");
Steven Morelanda21d84f2017-02-16 09:23:45 -080064 hidl_string s2a(nullptr); // copy constructor from null cstr
65 EXPECT_STREQ("", s2a);
Steven Moreland153f87a2017-02-28 09:42:26 -080066 s2a = nullptr; // = from nullptr cstr
67 EXPECT_STREQ(s2a.c_str(), "");
Yifan Hong602b85a2016-10-24 13:40:01 -070068 hidl_string s3 = hidl_string("s3"); // move =
69 EXPECT_STREQ(s3.c_str(), "s3");
Steven Moreland53120f72017-01-12 09:39:26 -080070 hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length
71 EXPECT_STREQ(s4.c_str(), "123");
72 hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
73 EXPECT_STREQ(s5.c_str(), "s5");
74 hidl_string s6(std::string("s6")); // copy constructor from std::string
Yifan Hong602b85a2016-10-24 13:40:01 -070075 EXPECT_STREQ(s6, "s6");
Steven Moreland53120f72017-01-12 09:39:26 -080076 hidl_string s7 = std::string("s7"); // copy = from std::string
77 EXPECT_STREQ(s7, "s7");
78 hidl_string s8(s7); // copy constructor
79 EXPECT_STREQ(s8, "s7");
80 hidl_string s9 = s8; // copy =
81 EXPECT_STREQ(s9, "s7");
Yifan Hong602b85a2016-10-24 13:40:01 -070082 char myCString[20] = "myCString";
83 s.setToExternal(&myCString[0], strlen(myCString));
84 EXPECT_STREQ(s, "myCString");
85 myCString[2] = 'D';
86 EXPECT_STREQ(s, "myDString");
87 s.clear(); // should not affect myCString
88 EXPECT_STREQ(myCString, "myDString");
Scott Randolphbb840f72016-11-21 14:39:26 -080089
Yifan Hong602b85a2016-10-24 13:40:01 -070090 // casts
91 s = "great";
92 std::string myString = s;
93 const char *anotherCString = s;
94 EXPECT_EQ(myString, "great");
95 EXPECT_STREQ(anotherCString, "great");
Scott Randolphbb840f72016-11-21 14:39:26 -080096
Steven Moreland727e3dd2017-03-23 11:19:00 -070097 const hidl_string t = "not so great";
98 std::string myTString = t;
99 const char * anotherTCString = t;
100 EXPECT_EQ(myTString, "not so great");
101 EXPECT_STREQ(anotherTCString, "not so great");
102
Scott Randolphbb840f72016-11-21 14:39:26 -0800103 // Comparisons
104 const char * cstr1 = "abc";
105 std::string string1(cstr1);
106 hidl_string hs1(cstr1);
107 const char * cstrE = "abc";
108 std::string stringE(cstrE);
109 hidl_string hsE(cstrE);
110 const char * cstrNE = "ABC";
111 std::string stringNE(cstrNE);
112 hidl_string hsNE(cstrNE);
Steven Moreland551396a2017-02-13 18:33:20 -0800113 const char * cstr2 = "def";
114 std::string string2(cstr2);
115 hidl_string hs2(cstr2);
116
Scott Randolphbb840f72016-11-21 14:39:26 -0800117 EXPECT_TRUE(hs1 == hsE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800118 EXPECT_FALSE(hs1 == hsNE);
119 EXPECT_TRUE(hs1 == cstrE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800120 EXPECT_FALSE(hs1 == cstrNE);
121 EXPECT_TRUE(hs1 == stringE);
Steven Moreland551396a2017-02-13 18:33:20 -0800122 EXPECT_FALSE(hs1 == stringNE);
123 EXPECT_FALSE(hs1 != hsE);
124 EXPECT_TRUE(hs1 != hsNE);
125 EXPECT_FALSE(hs1 != cstrE);
126 EXPECT_TRUE(hs1 != cstrNE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800127 EXPECT_FALSE(hs1 != stringE);
128 EXPECT_TRUE(hs1 != stringNE);
Steven Moreland551396a2017-02-13 18:33:20 -0800129
130 EXPECT_TRUE(hs1 < hs2);
131 EXPECT_FALSE(hs2 < hs1);
132 EXPECT_TRUE(hs2 > hs1);
133 EXPECT_FALSE(hs1 > hs2);
134 EXPECT_TRUE(hs1 <= hs1);
135 EXPECT_TRUE(hs1 <= hs2);
136 EXPECT_FALSE(hs2 <= hs1);
137 EXPECT_TRUE(hs1 >= hs1);
138 EXPECT_TRUE(hs2 >= hs1);
139 EXPECT_FALSE(hs2 <= hs1);
Yifan Hong602b85a2016-10-24 13:40:01 -0700140}
141
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800142TEST_F(LibHidlTest, MemoryTest) {
143 using android::hardware::hidl_memory;
144
145 hidl_memory mem1 = hidl_memory(); // default constructor
146 hidl_memory mem2 = mem1; // copy constructor (nullptr)
147
148 EXPECT_EQ(nullptr, mem2.handle());
149
150 native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
151
152 hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
153 hidl_memory mem4 = mem3; // copy constructor (regular handle)
154
155 EXPECT_EQ(mem3.name(), mem4.name());
156 EXPECT_EQ(mem3.size(), mem4.size());
157 EXPECT_NE(nullptr, mem4.handle());
158 EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
159
160 hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
161 hidl_memory mem6 = mem5;
162 EXPECT_EQ(nullptr, mem5.handle());
163 EXPECT_EQ(nullptr, mem6.handle());
164}
165
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800166TEST_F(LibHidlTest, VecInitTest) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700167 using android::hardware::hidl_vec;
168 using std::vector;
169 int32_t array[] = {5, 6, 7};
170 vector<int32_t> v(array, array + 3);
171
172 hidl_vec<int32_t> hv1 = v; // copy =
173 EXPECT_ARRAYEQ(hv1, array, 3);
174 EXPECT_ARRAYEQ(hv1, v, 3);
175 hidl_vec<int32_t> hv2(v); // copy constructor
176 EXPECT_ARRAYEQ(hv2, v, 3);
177
178 vector<int32_t> v2 = hv1; // cast
179 EXPECT_ARRAYEQ(v2, v, 3);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800180
181 hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
Steven Morelandb69926a2016-11-15 10:02:57 -0800182 EXPECT_EQ(v3.size(), 3ul);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800183 EXPECT_ARRAYEQ(v3, array, v3.size());
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800184}
185
186TEST_F(LibHidlTest, VecIterTest) {
187 int32_t array[] = {5, 6, 7};
188 android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
Scott Randolphbb840f72016-11-21 14:39:26 -0800189
190 auto iter = hv1.begin(); // iterator begin()
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800191 EXPECT_EQ(*iter++, 5);
Scott Randolphbb840f72016-11-21 14:39:26 -0800192 EXPECT_EQ(*iter, 6);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800193 EXPECT_EQ(*++iter, 7);
194 EXPECT_EQ(*iter--, 7);
195 EXPECT_EQ(*iter, 6);
196 EXPECT_EQ(*--iter, 5);
197
198 iter += 2;
Scott Randolphbb840f72016-11-21 14:39:26 -0800199 EXPECT_EQ(*iter, 7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800200 iter -= 2;
201 EXPECT_EQ(*iter, 5);
202
203 iter++;
204 EXPECT_EQ(*(iter + 1), 7);
205 EXPECT_EQ(*(1 + iter), 7);
206 EXPECT_EQ(*(iter - 1), 5);
207 EXPECT_EQ(*iter, 6);
208
209 auto five = iter - 1;
210 auto seven = iter + 1;
211 EXPECT_EQ(seven - five, 2);
212 EXPECT_EQ(five - seven, -2);
213
214 EXPECT_LT(five, seven);
215 EXPECT_LE(five, seven);
216 EXPECT_GT(seven, five);
217 EXPECT_GE(seven, five);
218
219 EXPECT_EQ(seven[0], 7);
220 EXPECT_EQ(five[1], 6);
221}
222
223TEST_F(LibHidlTest, VecIterForTest) {
224 using android::hardware::hidl_vec;
225 int32_t array[] = {5, 6, 7};
226 hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
Scott Randolphbb840f72016-11-21 14:39:26 -0800227
228 int32_t sum = 0; // range based for loop interoperability
229 for (auto &&i: hv1) {
230 sum += i;
231 }
232 EXPECT_EQ(sum, 5+6+7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800233
234 for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
235 *iter += 10;
236 }
237 const hidl_vec<int32_t> &v4 = hv1;
238 sum = 0;
239 for (const auto &i : v4) {
240 sum += i;
241 }
242 EXPECT_EQ(sum, 15+16+17);
Yifan Hong602b85a2016-10-24 13:40:01 -0700243}
244
Yifan Hong9fcbb362016-12-20 16:46:41 -0800245TEST_F(LibHidlTest, VecEqTest) {
246 android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
247 android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
248 android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
249
250 // use the == and != operator intentionally here
251 EXPECT_TRUE(hv1 == hv2);
252 EXPECT_TRUE(hv1 != hv3);
253}
254
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800255TEST_F(LibHidlTest, ArrayTest) {
256 using android::hardware::hidl_array;
257 int32_t array[] = {5, 6, 7};
258
259 hidl_array<int32_t, 3> ha(array);
260 EXPECT_ARRAYEQ(ha, array, 3);
261}
262
Yifan Hong5e2318c2016-10-27 17:19:21 -0700263TEST_F(LibHidlTest, TaskRunnerTest) {
Yifan Hong0a351392017-03-20 17:17:52 -0700264 using android::hardware::details::TaskRunner;
Yifan Hong5e2318c2016-10-27 17:19:21 -0700265 TaskRunner tr;
Yifan Hong6f667542017-03-20 19:04:05 -0700266 tr.start(1 /* limit */);
Yifan Hong5e2318c2016-10-27 17:19:21 -0700267 bool flag = false;
268 tr.push([&] {
269 usleep(1000);
270 flag = true;
271 });
272 usleep(500);
273 EXPECT_FALSE(flag);
274 usleep(1000);
275 EXPECT_TRUE(flag);
276}
277
Yifan Hong5708fb42016-10-26 17:50:29 -0700278TEST_F(LibHidlTest, StringCmpTest) {
279 using android::hardware::hidl_string;
280 const char * s = "good";
281 hidl_string hs(s);
282 EXPECT_NE(hs.c_str(), s);
283
284 EXPECT_TRUE(hs == s); // operator ==
285 EXPECT_TRUE(s == hs);
286
287 EXPECT_FALSE(hs != s); // operator ==
288 EXPECT_FALSE(s != hs);
289}
290
Yifan Hong602b85a2016-10-24 13:40:01 -0700291template <typename T>
292void great(android::hardware::hidl_vec<T>) {}
293
294TEST_F(LibHidlTest, VecCopyTest) {
295 android::hardware::hidl_vec<int32_t> v;
296 great(v);
297}
298
Yifan Hong44ab6232016-11-22 16:28:24 -0800299TEST_F(LibHidlTest, StdArrayTest) {
300 using android::hardware::hidl_array;
301 hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
302 std::array<int32_t, 5> stdArray = array;
303 EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
304 hidl_array<int32_t, 5> array2 = stdArray;
305 EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
306}
307
308TEST_F(LibHidlTest, MultiDimStdArrayTest) {
309 using android::hardware::hidl_array;
310 hidl_array<int32_t, 2, 3> array;
311 for (size_t i = 0; i < 2; i++) {
312 for (size_t j = 0; j < 3; j++) {
313 array[i][j] = i + j + i * j;
314 }
315 }
316 std::array<std::array<int32_t, 3>, 2> stdArray = array;
317 EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
318 hidl_array<int32_t, 2, 3> array2 = stdArray;
319 EXPECT_2DARRAYEQ(array, array2, 2, 3);
320}
321
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800322TEST_F(LibHidlTest, HidlVersionTest) {
323 using android::hardware::hidl_version;
324 hidl_version v1_0{1, 0};
325 EXPECT_EQ(1, v1_0.get_major());
326 EXPECT_EQ(0, v1_0.get_minor());
327 hidl_version v2_0{2, 0};
328 hidl_version v2_1{2, 1};
329 hidl_version v2_2{2, 2};
330 hidl_version v3_0{3, 0};
331 hidl_version v3_0b{3,0};
332
333 EXPECT_TRUE(v1_0 < v2_0);
334 EXPECT_TRUE(v2_0 < v2_1);
335 EXPECT_TRUE(v2_1 < v3_0);
336 EXPECT_TRUE(v2_0 > v1_0);
337 EXPECT_TRUE(v2_1 > v2_0);
338 EXPECT_TRUE(v3_0 > v2_1);
339 EXPECT_TRUE(v3_0 == v3_0b);
340 EXPECT_TRUE(v3_0 <= v3_0b);
341 EXPECT_TRUE(v2_2 <= v3_0);
342 EXPECT_TRUE(v3_0 >= v3_0b);
343 EXPECT_TRUE(v3_0 >= v2_2);
344}
345
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800346TEST_F(LibHidlTest, ReturnMoveTest) {
347 using namespace ::android;
348 using ::android::hardware::Return;
349 using ::android::hardware::Status;
350 Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
351 ret.isOk();
352 ret = {Status::fromStatusT(DEAD_OBJECT)};
353 ret.isOk();
354}
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800355
Yifan Hong2be94182017-03-03 19:07:38 -0800356std::string toString(const ::android::hardware::Status &s) {
357 using ::android::hardware::operator<<;
358 std::ostringstream oss;
359 oss << s;
360 return oss.str();
361}
362
363TEST_F(LibHidlTest, StatusStringTest) {
364 using namespace ::android;
365 using ::android::hardware::Status;
366 using ::testing::HasSubstr;
367
368 EXPECT_EQ(toString(Status::ok()), "No error");
369
370 EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
371
372 EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
373
Yifan Hong2be94182017-03-03 19:07:38 -0800374 EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
375 HasSubstr("EX_NULL_POINTER"));
376
377}
378
Yifan Hong602b85a2016-10-24 13:40:01 -0700379int main(int argc, char **argv) {
380 ::testing::InitGoogleTest(&argc, argv);
381 return RUN_ALL_TESTS();
382}