• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <android-base/logging.h>
20 #include <android/hidl/memory/1.0/IMemory.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hidl/HidlSupport.h>
24 #include <hidl/ServiceManagement.h>
25 #include <hidl/Status.h>
26 #include <hidl/TaskRunner.h>
27 #include <condition_variable>
28 #include <fstream>
29 #include <vector>
30 
31 #define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
32 #define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
33         EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
34 
35 template<typename T, typename S>
isArrayEqual(const T arr1,const S arr2,size_t size)36 static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
37     for(size_t i = 0; i < size; i++)
38         if(arr1[i] != arr2[i])
39             return false;
40     return true;
41 }
42 
43 template<typename T, typename S>
is2dArrayEqual(const T arr1,const S arr2,size_t size1,size_t size2)44 static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
45     for(size_t i = 0; i < size1; i++)
46         for (size_t j = 0; j < size2; j++)
47             if(arr1[i][j] != arr2[i][j])
48                 return false;
49     return true;
50 }
51 
isLibraryOpen(const std::string & lib)52 bool isLibraryOpen(const std::string& lib) {
53     std::ifstream ifs("/proc/self/maps");
54     for (std::string line; std::getline(ifs, line);) {
55         if (line.size() >= lib.size() && line.substr(line.size() - lib.size()) == lib) {
56             return true;
57         }
58     }
59 
60     return false;
61 }
62 
63 class LibHidlTest : public ::testing::Test {
64 public:
SetUp()65     virtual void SetUp() override {
66     }
TearDown()67     virtual void TearDown() override {
68     }
69 };
70 
TEST_F(LibHidlTest,StringTest)71 TEST_F(LibHidlTest, StringTest) {
72     using android::hardware::hidl_string;
73     hidl_string s; // empty constructor
74     EXPECT_STREQ(s.c_str(), "");
75     hidl_string s1 = "s1"; // copy = from cstr
76     EXPECT_STREQ(s1.c_str(), "s1");
77     hidl_string s2("s2"); // copy constructor from cstr
78     EXPECT_STREQ(s2.c_str(), "s2");
79     hidl_string s2a(nullptr); // copy constructor from null cstr
80     EXPECT_STREQ("", s2a.c_str());
81     s2a = nullptr; // = from nullptr cstr
82     EXPECT_STREQ(s2a.c_str(), "");
83     hidl_string s3 = hidl_string("s3"); // move =
84     EXPECT_STREQ(s3.c_str(), "s3");
85     hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length
86     EXPECT_STREQ(s4.c_str(), "123");
87     hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
88     EXPECT_STREQ(s5.c_str(), "s5");
89     hidl_string s6(std::string("s6")); // copy constructor from std::string
90     EXPECT_STREQ(s6.c_str(), "s6");
91     hidl_string s7 = std::string("s7"); // copy = from std::string
92     EXPECT_STREQ(s7.c_str(), "s7");
93     hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor
94     EXPECT_STREQ(s8.c_str(), "s7");
95     hidl_string s9 = s8; // copy =  // NOLINT, test the copy operator
96     EXPECT_STREQ(s9.c_str(), "s7");
97     char myCString[20] = "myCString";
98     s.setToExternal(&myCString[0], strlen(myCString));
99     EXPECT_STREQ(s.c_str(), "myCString");
100     myCString[2] = 'D';
101     EXPECT_STREQ(s.c_str(), "myDString");
102     s.clear(); // should not affect myCString
103     EXPECT_STREQ(myCString, "myDString");
104 
105     // casts
106     s = "great";
107     std::string myString = s;
108     const char *anotherCString = s.c_str();
109     EXPECT_EQ(myString, "great");
110     EXPECT_STREQ(anotherCString, "great");
111 
112     const hidl_string t = "not so great";
113     std::string myTString = t;
114     const char * anotherTCString = t.c_str();
115     EXPECT_EQ(myTString, "not so great");
116     EXPECT_STREQ(anotherTCString, "not so great");
117 
118     // Assignment from hidl_string to std::string
119     std::string tgt;
120     hidl_string src("some stuff");
121     tgt = src;
122     EXPECT_STREQ(tgt.c_str(), "some stuff");
123 
124     // Stream output operator
125     hidl_string msg("hidl_string works with operator<<");
126     std::cout << msg;
127 
128     // Comparisons
129     const char * cstr1 = "abc";
130     std::string string1(cstr1);
131     hidl_string hs1(cstr1);
132     const char * cstrE = "abc";
133     std::string stringE(cstrE);
134     hidl_string hsE(cstrE);
135     const char * cstrNE = "ABC";
136     std::string stringNE(cstrNE);
137     hidl_string hsNE(cstrNE);
138     const char * cstr2 = "def";
139     std::string string2(cstr2);
140     hidl_string hs2(cstr2);
141 
142     EXPECT_TRUE(hs1  == hsE);
143     EXPECT_FALSE(hs1 == hsNE);
144     EXPECT_TRUE(hs1  == cstrE);
145     EXPECT_FALSE(hs1 == cstrNE);
146     EXPECT_TRUE(hs1  == stringE);
147     EXPECT_FALSE(hs1 == stringNE);
148     EXPECT_FALSE(hs1 != hsE);
149     EXPECT_TRUE(hs1  != hsNE);
150     EXPECT_FALSE(hs1 != cstrE);
151     EXPECT_TRUE(hs1  != cstrNE);
152     EXPECT_FALSE(hs1 != stringE);
153     EXPECT_TRUE(hs1  != stringNE);
154 
155     EXPECT_TRUE(hs1 < hs2);
156     EXPECT_FALSE(hs2 < hs1);
157     EXPECT_TRUE(hs2 > hs1);
158     EXPECT_FALSE(hs1 > hs2);
159     EXPECT_TRUE(hs1 <= hs1);
160     EXPECT_TRUE(hs1 <= hs2);
161     EXPECT_FALSE(hs2 <= hs1);
162     EXPECT_TRUE(hs1 >= hs1);
163     EXPECT_TRUE(hs2 >= hs1);
164     EXPECT_FALSE(hs2 <= hs1);
165 }
166 
TEST_F(LibHidlTest,MemoryTest)167 TEST_F(LibHidlTest, MemoryTest) {
168     using android::hardware::hidl_memory;
169 
170     hidl_memory mem1 = hidl_memory(); // default constructor
171     hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT
172 
173     EXPECT_EQ(nullptr, mem2.handle());
174 
175     native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
176 
177     hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
178     hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT
179 
180     EXPECT_EQ(mem3.name(), mem4.name());
181     EXPECT_EQ(mem3.size(), mem4.size());
182     EXPECT_NE(nullptr, mem4.handle());
183     EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
184 
185     hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
186     hidl_memory mem6 = mem5; // NOLINT, test copying
187     EXPECT_EQ(nullptr, mem5.handle());
188     EXPECT_EQ(nullptr, mem6.handle());
189 }
190 
TEST_F(LibHidlTest,VecInitTest)191 TEST_F(LibHidlTest, VecInitTest) {
192     using android::hardware::hidl_vec;
193     using std::vector;
194     int32_t array[] = {5, 6, 7};
195     vector<int32_t> v(array, array + 3);
196 
197     hidl_vec<int32_t> hv0(3);  // size
198     EXPECT_EQ(hv0.size(), 3ul);  // cannot say anything about its contents
199 
200     hidl_vec<int32_t> hv1 = v; // copy =
201     EXPECT_ARRAYEQ(hv1, array, 3);
202     EXPECT_ARRAYEQ(hv1, v, 3);
203     hidl_vec<int32_t> hv2(v); // copy constructor
204     EXPECT_ARRAYEQ(hv2, v, 3);
205 
206     vector<int32_t> v2 = hv1; // cast
207     EXPECT_ARRAYEQ(v2, v, 3);
208 
209     hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
210     EXPECT_EQ(v3.size(), 3ul);
211     EXPECT_ARRAYEQ(v3, array, v3.size());
212 }
213 
TEST_F(LibHidlTest,VecIterTest)214 TEST_F(LibHidlTest, VecIterTest) {
215     int32_t array[] = {5, 6, 7};
216     android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
217 
218     auto iter = hv1.begin();    // iterator begin()
219     EXPECT_EQ(*iter++, 5);
220     EXPECT_EQ(*iter, 6);
221     EXPECT_EQ(*++iter, 7);
222     EXPECT_EQ(*iter--, 7);
223     EXPECT_EQ(*iter, 6);
224     EXPECT_EQ(*--iter, 5);
225 
226     iter += 2;
227     EXPECT_EQ(*iter, 7);
228     iter -= 2;
229     EXPECT_EQ(*iter, 5);
230 
231     iter++;
232     EXPECT_EQ(*(iter + 1), 7);
233     EXPECT_EQ(*(1 + iter), 7);
234     EXPECT_EQ(*(iter - 1), 5);
235     EXPECT_EQ(*iter, 6);
236 
237     auto five = iter - 1;
238     auto seven = iter + 1;
239     EXPECT_EQ(seven - five, 2);
240     EXPECT_EQ(five - seven, -2);
241 
242     EXPECT_LT(five, seven);
243     EXPECT_LE(five, seven);
244     EXPECT_GT(seven, five);
245     EXPECT_GE(seven, five);
246 
247     EXPECT_EQ(seven[0], 7);
248     EXPECT_EQ(five[1], 6);
249 }
250 
TEST_F(LibHidlTest,VecIterForTest)251 TEST_F(LibHidlTest, VecIterForTest) {
252     using android::hardware::hidl_vec;
253     int32_t array[] = {5, 6, 7};
254     hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
255 
256     int32_t sum = 0;            // range based for loop interoperability
257     for (auto &&i: hv1) {
258         sum += i;
259     }
260     EXPECT_EQ(sum, 5+6+7);
261 
262     for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
263         *iter += 10;
264     }
265     const hidl_vec<int32_t> &v4 = hv1;
266     sum = 0;
267     for (const auto &i : v4) {
268         sum += i;
269     }
270     EXPECT_EQ(sum, 15+16+17);
271 }
272 
TEST_F(LibHidlTest,VecEqTest)273 TEST_F(LibHidlTest, VecEqTest) {
274     android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
275     android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
276     android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
277 
278     // use the == and != operator intentionally here
279     EXPECT_TRUE(hv1 == hv2);
280     EXPECT_TRUE(hv1 != hv3);
281 }
282 
TEST_F(LibHidlTest,VecEqInitializerTest)283 TEST_F(LibHidlTest, VecEqInitializerTest) {
284     std::vector<int32_t> reference{5, 6, 7};
285     android::hardware::hidl_vec<int32_t> hv1{1, 2, 3};
286     hv1 = {5, 6, 7};
287     android::hardware::hidl_vec<int32_t> hv2;
288     hv2 = {5, 6, 7};
289     android::hardware::hidl_vec<int32_t> hv3;
290     hv3 = {5, 6, 8};
291 
292     // use the == and != operator intentionally here
293     EXPECT_TRUE(hv1 == hv2);
294     EXPECT_TRUE(hv1 == reference);
295     EXPECT_TRUE(hv1 != hv3);
296 }
297 
TEST_F(LibHidlTest,VecRangeCtorTest)298 TEST_F(LibHidlTest, VecRangeCtorTest) {
299     struct ConvertibleType {
300         int val;
301 
302         explicit ConvertibleType(int val) : val(val) {}
303         explicit operator int() const { return val; }
304         bool operator==(const int& other) const { return val == other; }
305     };
306 
307     std::vector<ConvertibleType> input{
308         ConvertibleType(1), ConvertibleType(2), ConvertibleType(3),
309     };
310 
311     android::hardware::hidl_vec<int> hv(input.begin(), input.end());
312 
313     EXPECT_EQ(input.size(), hv.size());
314     int sum = 0;
315     for (unsigned i = 0; i < input.size(); i++) {
316         EXPECT_EQ(input[i], hv[i]);
317         sum += hv[i];
318     }
319     EXPECT_EQ(sum, 1 + 2 + 3);
320 }
321 
TEST_F(LibHidlTest,ArrayTest)322 TEST_F(LibHidlTest, ArrayTest) {
323     using android::hardware::hidl_array;
324     int32_t array[] = {5, 6, 7};
325 
326     hidl_array<int32_t, 3> ha(array);
327     EXPECT_ARRAYEQ(ha, array, 3);
328 }
329 
TEST_F(LibHidlTest,TaskRunnerTest)330 TEST_F(LibHidlTest, TaskRunnerTest) {
331     using android::hardware::details::TaskRunner;
332     using namespace std::chrono_literals;
333 
334     std::condition_variable cv;
335     std::mutex m;
336 
337     TaskRunner tr;
338     tr.start(1 /* limit */);
339     bool flag = false;
340     tr.push([&] {
341         flag = true;
342         cv.notify_all();
343     });
344 
345     std::unique_lock<std::mutex> lock(m);
346 
347     // 1s so this doesn't deadlock. This isn't a performance test.
348     EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;}));
349     EXPECT_TRUE(flag);
350 }
351 
TEST_F(LibHidlTest,StringCmpTest)352 TEST_F(LibHidlTest, StringCmpTest) {
353     using android::hardware::hidl_string;
354     const char * s = "good";
355     hidl_string hs(s);
356     EXPECT_NE(hs.c_str(), s);
357 
358     EXPECT_TRUE(hs == s); // operator ==
359     EXPECT_TRUE(s == hs);
360 
361     EXPECT_FALSE(hs != s); // operator ==
362     EXPECT_FALSE(s != hs);
363 }
364 
365 template <typename T>
great(android::hardware::hidl_vec<T>)366 void great(android::hardware::hidl_vec<T>) {}
367 
TEST_F(LibHidlTest,VecCopyTest)368 TEST_F(LibHidlTest, VecCopyTest) {
369     android::hardware::hidl_vec<int32_t> v;
370     great(v);
371 }
372 
TEST_F(LibHidlTest,StdArrayTest)373 TEST_F(LibHidlTest, StdArrayTest) {
374     using android::hardware::hidl_array;
375     hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
376     std::array<int32_t, 5> stdArray = array;
377     EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
378     hidl_array<int32_t, 5> array2 = stdArray;
379     EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
380 }
381 
TEST_F(LibHidlTest,MultiDimStdArrayTest)382 TEST_F(LibHidlTest, MultiDimStdArrayTest) {
383     using android::hardware::hidl_array;
384     hidl_array<int32_t, 2, 3> array;
385     for (size_t i = 0; i < 2; i++) {
386         for (size_t j = 0; j < 3; j++) {
387             array[i][j] = i + j + i * j;
388         }
389     }
390     std::array<std::array<int32_t, 3>, 2> stdArray = array;
391     EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
392     hidl_array<int32_t, 2, 3> array2 = stdArray;
393     EXPECT_2DARRAYEQ(array, array2, 2, 3);
394 }
395 
TEST_F(LibHidlTest,HidlVersionTest)396 TEST_F(LibHidlTest, HidlVersionTest) {
397     using android::hardware::hidl_version;
398     hidl_version v1_0{1, 0};
399     EXPECT_EQ(1, v1_0.get_major());
400     EXPECT_EQ(0, v1_0.get_minor());
401     hidl_version v2_0{2, 0};
402     hidl_version v2_1{2, 1};
403     hidl_version v2_2{2, 2};
404     hidl_version v3_0{3, 0};
405     hidl_version v3_0b{3,0};
406 
407     EXPECT_TRUE(v1_0 < v2_0);
408     EXPECT_TRUE(v1_0 != v2_0);
409     EXPECT_TRUE(v2_0 < v2_1);
410     EXPECT_TRUE(v2_1 < v3_0);
411     EXPECT_TRUE(v2_0 > v1_0);
412     EXPECT_TRUE(v2_0 != v1_0);
413     EXPECT_TRUE(v2_1 > v2_0);
414     EXPECT_TRUE(v3_0 > v2_1);
415     EXPECT_TRUE(v3_0 == v3_0b);
416     EXPECT_FALSE(v3_0 != v3_0b);
417     EXPECT_TRUE(v3_0 <= v3_0b);
418     EXPECT_TRUE(v2_2 <= v3_0);
419     EXPECT_TRUE(v3_0 >= v3_0b);
420     EXPECT_TRUE(v3_0 >= v2_2);
421 }
422 
TEST_F(LibHidlTest,ReturnMoveTest)423 TEST_F(LibHidlTest, ReturnMoveTest) {
424     using namespace ::android;
425     using ::android::hardware::Return;
426     using ::android::hardware::Status;
427     Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
428     ret.isOk();
429     ret = {Status::fromStatusT(DEAD_OBJECT)};
430     ret.isOk();
431 }
432 
TEST_F(LibHidlTest,ReturnTest)433 TEST_F(LibHidlTest, ReturnTest) {
434     using ::android::DEAD_OBJECT;
435     using ::android::hardware::Return;
436     using ::android::hardware::Status;
437     using ::android::hardware::hidl_string;
438 
439     EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk());
440     EXPECT_TRUE(Return<void>(Status::ok()).isOk());
441 
442     hidl_string one = "1";
443     hidl_string two = "2";
444     Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT));
445 
446     EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two));
447     EXPECT_EQ(two, ret.withDefault(two));
448 
449     hidl_string&& moved = ret.withDefault(std::move(two));
450     EXPECT_EQ("2", moved);
451 
452     const hidl_string three = "3";
453     EXPECT_EQ(three, ret.withDefault(three));
454 }
455 
toString(const::android::hardware::Status & s)456 std::string toString(const ::android::hardware::Status &s) {
457     using ::android::hardware::operator<<;
458     std::ostringstream oss;
459     oss << s;
460     return oss.str();
461 }
462 
TEST_F(LibHidlTest,StatusStringTest)463 TEST_F(LibHidlTest, StatusStringTest) {
464     using namespace ::android;
465     using ::android::hardware::Status;
466     using ::testing::HasSubstr;
467 
468     EXPECT_EQ(toString(Status::ok()), "No error");
469 
470     EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
471 
472     EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
473 
474     EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
475             HasSubstr("EX_NULL_POINTER"));
476 }
477 
TEST_F(LibHidlTest,PreloadTest)478 TEST_F(LibHidlTest, PreloadTest) {
479     using ::android::hardware::preloadPassthroughService;
480     using ::android::hidl::memory::V1_0::IMemory;
481 
482     // installed on all devices by default in both bitnesses and not otherwise a dependency of this
483     // test.
484     static const std::string kLib = "android.hidl.memory@1.0-impl.so";
485 
486     EXPECT_FALSE(isLibraryOpen(kLib));
487     preloadPassthroughService<IMemory>();
488     EXPECT_TRUE(isLibraryOpen(kLib));
489 }
490 
main(int argc,char ** argv)491 int main(int argc, char **argv) {
492     ::testing::InitGoogleTest(&argc, argv);
493     return RUN_ALL_TESTS();
494 }
495