• 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 <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <hidl/HidlSupport.h>
23 #include <hidl/Status.h>
24 #include <hidl/TaskRunner.h>
25 #include <vector>
26 
27 #define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
28 #define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
29         EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
30 
31 template<typename T, typename S>
isArrayEqual(const T arr1,const S arr2,size_t size)32 static 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 
39 template<typename T, typename S>
is2dArrayEqual(const T arr1,const S arr2,size_t size1,size_t size2)40 static 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 
48 class LibHidlTest : public ::testing::Test {
49 public:
SetUp()50     virtual void SetUp() override {
51     }
TearDown()52     virtual void TearDown() override {
53     }
54 };
55 
TEST_F(LibHidlTest,StringTest)56 TEST_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");
64     hidl_string s2a(nullptr); // copy constructor from null cstr
65     EXPECT_STREQ("", s2a.c_str());
66     s2a = nullptr; // = from nullptr cstr
67     EXPECT_STREQ(s2a.c_str(), "");
68     hidl_string s3 = hidl_string("s3"); // move =
69     EXPECT_STREQ(s3.c_str(), "s3");
70     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
75     EXPECT_STREQ(s6.c_str(), "s6");
76     hidl_string s7 = std::string("s7"); // copy = from std::string
77     EXPECT_STREQ(s7.c_str(), "s7");
78     hidl_string s8(s7); // copy constructor
79     EXPECT_STREQ(s8.c_str(), "s7");
80     hidl_string s9 = s8; // copy =
81     EXPECT_STREQ(s9.c_str(), "s7");
82     char myCString[20] = "myCString";
83     s.setToExternal(&myCString[0], strlen(myCString));
84     EXPECT_STREQ(s.c_str(), "myCString");
85     myCString[2] = 'D';
86     EXPECT_STREQ(s.c_str(), "myDString");
87     s.clear(); // should not affect myCString
88     EXPECT_STREQ(myCString, "myDString");
89 
90     // casts
91     s = "great";
92     std::string myString = s;
93     const char *anotherCString = s.c_str();
94     EXPECT_EQ(myString, "great");
95     EXPECT_STREQ(anotherCString, "great");
96 
97     const hidl_string t = "not so great";
98     std::string myTString = t;
99     const char * anotherTCString = t.c_str();
100     EXPECT_EQ(myTString, "not so great");
101     EXPECT_STREQ(anotherTCString, "not so great");
102 
103     // Assignment from hidl_string to std::string
104     std::string tgt;
105     hidl_string src("some stuff");
106     tgt = src;
107     EXPECT_STREQ(tgt.c_str(), "some stuff");
108 
109     // Stream output operator
110     hidl_string msg("hidl_string works with operator<<");
111     std::cout << msg;
112 
113     // Comparisons
114     const char * cstr1 = "abc";
115     std::string string1(cstr1);
116     hidl_string hs1(cstr1);
117     const char * cstrE = "abc";
118     std::string stringE(cstrE);
119     hidl_string hsE(cstrE);
120     const char * cstrNE = "ABC";
121     std::string stringNE(cstrNE);
122     hidl_string hsNE(cstrNE);
123     const char * cstr2 = "def";
124     std::string string2(cstr2);
125     hidl_string hs2(cstr2);
126 
127     EXPECT_TRUE(hs1  == hsE);
128     EXPECT_FALSE(hs1 == hsNE);
129     EXPECT_TRUE(hs1  == cstrE);
130     EXPECT_FALSE(hs1 == cstrNE);
131     EXPECT_TRUE(hs1  == stringE);
132     EXPECT_FALSE(hs1 == stringNE);
133     EXPECT_FALSE(hs1 != hsE);
134     EXPECT_TRUE(hs1  != hsNE);
135     EXPECT_FALSE(hs1 != cstrE);
136     EXPECT_TRUE(hs1  != cstrNE);
137     EXPECT_FALSE(hs1 != stringE);
138     EXPECT_TRUE(hs1  != stringNE);
139 
140     EXPECT_TRUE(hs1 < hs2);
141     EXPECT_FALSE(hs2 < hs1);
142     EXPECT_TRUE(hs2 > hs1);
143     EXPECT_FALSE(hs1 > hs2);
144     EXPECT_TRUE(hs1 <= hs1);
145     EXPECT_TRUE(hs1 <= hs2);
146     EXPECT_FALSE(hs2 <= hs1);
147     EXPECT_TRUE(hs1 >= hs1);
148     EXPECT_TRUE(hs2 >= hs1);
149     EXPECT_FALSE(hs2 <= hs1);
150 }
151 
TEST_F(LibHidlTest,MemoryTest)152 TEST_F(LibHidlTest, MemoryTest) {
153     using android::hardware::hidl_memory;
154 
155     hidl_memory mem1 = hidl_memory(); // default constructor
156     hidl_memory mem2 = mem1; // copy constructor (nullptr)
157 
158     EXPECT_EQ(nullptr, mem2.handle());
159 
160     native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
161 
162     hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
163     hidl_memory mem4 = mem3; // copy constructor (regular handle)
164 
165     EXPECT_EQ(mem3.name(), mem4.name());
166     EXPECT_EQ(mem3.size(), mem4.size());
167     EXPECT_NE(nullptr, mem4.handle());
168     EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
169 
170     hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
171     hidl_memory mem6 = mem5;
172     EXPECT_EQ(nullptr, mem5.handle());
173     EXPECT_EQ(nullptr, mem6.handle());
174 }
175 
TEST_F(LibHidlTest,VecInitTest)176 TEST_F(LibHidlTest, VecInitTest) {
177     using android::hardware::hidl_vec;
178     using std::vector;
179     int32_t array[] = {5, 6, 7};
180     vector<int32_t> v(array, array + 3);
181 
182     hidl_vec<int32_t> hv1 = v; // copy =
183     EXPECT_ARRAYEQ(hv1, array, 3);
184     EXPECT_ARRAYEQ(hv1, v, 3);
185     hidl_vec<int32_t> hv2(v); // copy constructor
186     EXPECT_ARRAYEQ(hv2, v, 3);
187 
188     vector<int32_t> v2 = hv1; // cast
189     EXPECT_ARRAYEQ(v2, v, 3);
190 
191     hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
192     EXPECT_EQ(v3.size(), 3ul);
193     EXPECT_ARRAYEQ(v3, array, v3.size());
194 }
195 
TEST_F(LibHidlTest,VecIterTest)196 TEST_F(LibHidlTest, VecIterTest) {
197     int32_t array[] = {5, 6, 7};
198     android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
199 
200     auto iter = hv1.begin();    // iterator begin()
201     EXPECT_EQ(*iter++, 5);
202     EXPECT_EQ(*iter, 6);
203     EXPECT_EQ(*++iter, 7);
204     EXPECT_EQ(*iter--, 7);
205     EXPECT_EQ(*iter, 6);
206     EXPECT_EQ(*--iter, 5);
207 
208     iter += 2;
209     EXPECT_EQ(*iter, 7);
210     iter -= 2;
211     EXPECT_EQ(*iter, 5);
212 
213     iter++;
214     EXPECT_EQ(*(iter + 1), 7);
215     EXPECT_EQ(*(1 + iter), 7);
216     EXPECT_EQ(*(iter - 1), 5);
217     EXPECT_EQ(*iter, 6);
218 
219     auto five = iter - 1;
220     auto seven = iter + 1;
221     EXPECT_EQ(seven - five, 2);
222     EXPECT_EQ(five - seven, -2);
223 
224     EXPECT_LT(five, seven);
225     EXPECT_LE(five, seven);
226     EXPECT_GT(seven, five);
227     EXPECT_GE(seven, five);
228 
229     EXPECT_EQ(seven[0], 7);
230     EXPECT_EQ(five[1], 6);
231 }
232 
TEST_F(LibHidlTest,VecIterForTest)233 TEST_F(LibHidlTest, VecIterForTest) {
234     using android::hardware::hidl_vec;
235     int32_t array[] = {5, 6, 7};
236     hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
237 
238     int32_t sum = 0;            // range based for loop interoperability
239     for (auto &&i: hv1) {
240         sum += i;
241     }
242     EXPECT_EQ(sum, 5+6+7);
243 
244     for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
245         *iter += 10;
246     }
247     const hidl_vec<int32_t> &v4 = hv1;
248     sum = 0;
249     for (const auto &i : v4) {
250         sum += i;
251     }
252     EXPECT_EQ(sum, 15+16+17);
253 }
254 
TEST_F(LibHidlTest,VecEqTest)255 TEST_F(LibHidlTest, VecEqTest) {
256     android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
257     android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
258     android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
259 
260     // use the == and != operator intentionally here
261     EXPECT_TRUE(hv1 == hv2);
262     EXPECT_TRUE(hv1 != hv3);
263 }
264 
TEST_F(LibHidlTest,ArrayTest)265 TEST_F(LibHidlTest, ArrayTest) {
266     using android::hardware::hidl_array;
267     int32_t array[] = {5, 6, 7};
268 
269     hidl_array<int32_t, 3> ha(array);
270     EXPECT_ARRAYEQ(ha, array, 3);
271 }
272 
TEST_F(LibHidlTest,TaskRunnerTest)273 TEST_F(LibHidlTest, TaskRunnerTest) {
274     using android::hardware::details::TaskRunner;
275     TaskRunner tr;
276     tr.start(1 /* limit */);
277     bool flag = false;
278     tr.push([&] {
279         usleep(1000);
280         flag = true;
281     });
282     usleep(500);
283     EXPECT_FALSE(flag);
284     usleep(1000);
285     EXPECT_TRUE(flag);
286 }
287 
TEST_F(LibHidlTest,StringCmpTest)288 TEST_F(LibHidlTest, StringCmpTest) {
289     using android::hardware::hidl_string;
290     const char * s = "good";
291     hidl_string hs(s);
292     EXPECT_NE(hs.c_str(), s);
293 
294     EXPECT_TRUE(hs == s); // operator ==
295     EXPECT_TRUE(s == hs);
296 
297     EXPECT_FALSE(hs != s); // operator ==
298     EXPECT_FALSE(s != hs);
299 }
300 
301 template <typename T>
great(android::hardware::hidl_vec<T>)302 void great(android::hardware::hidl_vec<T>) {}
303 
TEST_F(LibHidlTest,VecCopyTest)304 TEST_F(LibHidlTest, VecCopyTest) {
305     android::hardware::hidl_vec<int32_t> v;
306     great(v);
307 }
308 
TEST_F(LibHidlTest,StdArrayTest)309 TEST_F(LibHidlTest, StdArrayTest) {
310     using android::hardware::hidl_array;
311     hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
312     std::array<int32_t, 5> stdArray = array;
313     EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
314     hidl_array<int32_t, 5> array2 = stdArray;
315     EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
316 }
317 
TEST_F(LibHidlTest,MultiDimStdArrayTest)318 TEST_F(LibHidlTest, MultiDimStdArrayTest) {
319     using android::hardware::hidl_array;
320     hidl_array<int32_t, 2, 3> array;
321     for (size_t i = 0; i < 2; i++) {
322         for (size_t j = 0; j < 3; j++) {
323             array[i][j] = i + j + i * j;
324         }
325     }
326     std::array<std::array<int32_t, 3>, 2> stdArray = array;
327     EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
328     hidl_array<int32_t, 2, 3> array2 = stdArray;
329     EXPECT_2DARRAYEQ(array, array2, 2, 3);
330 }
331 
TEST_F(LibHidlTest,HidlVersionTest)332 TEST_F(LibHidlTest, HidlVersionTest) {
333     using android::hardware::hidl_version;
334     hidl_version v1_0{1, 0};
335     EXPECT_EQ(1, v1_0.get_major());
336     EXPECT_EQ(0, v1_0.get_minor());
337     hidl_version v2_0{2, 0};
338     hidl_version v2_1{2, 1};
339     hidl_version v2_2{2, 2};
340     hidl_version v3_0{3, 0};
341     hidl_version v3_0b{3,0};
342 
343     EXPECT_TRUE(v1_0 < v2_0);
344     EXPECT_TRUE(v2_0 < v2_1);
345     EXPECT_TRUE(v2_1 < v3_0);
346     EXPECT_TRUE(v2_0 > v1_0);
347     EXPECT_TRUE(v2_1 > v2_0);
348     EXPECT_TRUE(v3_0 > v2_1);
349     EXPECT_TRUE(v3_0 == v3_0b);
350     EXPECT_TRUE(v3_0 <= v3_0b);
351     EXPECT_TRUE(v2_2 <= v3_0);
352     EXPECT_TRUE(v3_0 >= v3_0b);
353     EXPECT_TRUE(v3_0 >= v2_2);
354 }
355 
TEST_F(LibHidlTest,ReturnMoveTest)356 TEST_F(LibHidlTest, ReturnMoveTest) {
357     using namespace ::android;
358     using ::android::hardware::Return;
359     using ::android::hardware::Status;
360     Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
361     ret.isOk();
362     ret = {Status::fromStatusT(DEAD_OBJECT)};
363     ret.isOk();
364 }
365 
toString(const::android::hardware::Status & s)366 std::string toString(const ::android::hardware::Status &s) {
367     using ::android::hardware::operator<<;
368     std::ostringstream oss;
369     oss << s;
370     return oss.str();
371 }
372 
TEST_F(LibHidlTest,StatusStringTest)373 TEST_F(LibHidlTest, StatusStringTest) {
374     using namespace ::android;
375     using ::android::hardware::Status;
376     using ::testing::HasSubstr;
377 
378     EXPECT_EQ(toString(Status::ok()), "No error");
379 
380     EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
381 
382     EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
383 
384     EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
385             HasSubstr("EX_NULL_POINTER"));
386 
387 }
388 
main(int argc,char ** argv)389 int main(int argc, char **argv) {
390     ::testing::InitGoogleTest(&argc, argv);
391     return RUN_ALL_TESTS();
392 }
393