blob: beb89d719b569a4a5f2bff95f84426528b978f30 [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>
Steven Moreland138c3502018-11-27 14:27:04 -080020#include <android/hidl/memory/1.0/IMemory.h>
Yifan Hong2be94182017-03-03 19:07:38 -080021#include <gmock/gmock.h>
Yifan Hong602b85a2016-10-24 13:40:01 -070022#include <gtest/gtest.h>
23#include <hidl/HidlSupport.h>
Steven Moreland951fb132017-11-07 14:24:07 -080024#include <hidl/ServiceManagement.h>
Yifan Hong2be94182017-03-03 19:07:38 -080025#include <hidl/Status.h>
Yifan Hong5e2318c2016-10-27 17:19:21 -070026#include <hidl/TaskRunner.h>
Steven Moreland951fb132017-11-07 14:24:07 -080027#include <condition_variable>
28#include <fstream>
Yifan Hong602b85a2016-10-24 13:40:01 -070029#include <vector>
30
31#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hong44ab6232016-11-22 16:28:24 -080032#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
33 EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
Yifan Hong602b85a2016-10-24 13:40:01 -070034
35template<typename T, typename S>
36static 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
Yifan Hong44ab6232016-11-22 16:28:24 -080043template<typename T, typename S>
44static 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
Steven Moreland951fb132017-11-07 14:24:07 -080052bool 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
Yifan Hong602b85a2016-10-24 13:40:01 -070063class LibHidlTest : public ::testing::Test {
64public:
65 virtual void SetUp() override {
66 }
67 virtual void TearDown() override {
68 }
69};
70
71TEST_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");
Steven Morelanda21d84f2017-02-16 09:23:45 -080079 hidl_string s2a(nullptr); // copy constructor from null cstr
Scott Randolpheb0c3372017-04-03 14:07:14 -070080 EXPECT_STREQ("", s2a.c_str());
Steven Moreland153f87a2017-02-28 09:42:26 -080081 s2a = nullptr; // = from nullptr cstr
82 EXPECT_STREQ(s2a.c_str(), "");
Yifan Hong602b85a2016-10-24 13:40:01 -070083 hidl_string s3 = hidl_string("s3"); // move =
84 EXPECT_STREQ(s3.c_str(), "s3");
Steven Moreland53120f72017-01-12 09:39:26 -080085 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
Scott Randolpheb0c3372017-04-03 14:07:14 -070090 EXPECT_STREQ(s6.c_str(), "s6");
Steven Moreland53120f72017-01-12 09:39:26 -080091 hidl_string s7 = std::string("s7"); // copy = from std::string
Scott Randolpheb0c3372017-04-03 14:07:14 -070092 EXPECT_STREQ(s7.c_str(), "s7");
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070093 hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor
Scott Randolpheb0c3372017-04-03 14:07:14 -070094 EXPECT_STREQ(s8.c_str(), "s7");
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070095 hidl_string s9 = s8; // copy = // NOLINT, test the copy operator
Scott Randolpheb0c3372017-04-03 14:07:14 -070096 EXPECT_STREQ(s9.c_str(), "s7");
Yifan Hong602b85a2016-10-24 13:40:01 -070097 char myCString[20] = "myCString";
98 s.setToExternal(&myCString[0], strlen(myCString));
Scott Randolpheb0c3372017-04-03 14:07:14 -070099 EXPECT_STREQ(s.c_str(), "myCString");
Yifan Hong602b85a2016-10-24 13:40:01 -0700100 myCString[2] = 'D';
Scott Randolpheb0c3372017-04-03 14:07:14 -0700101 EXPECT_STREQ(s.c_str(), "myDString");
Yifan Hong602b85a2016-10-24 13:40:01 -0700102 s.clear(); // should not affect myCString
103 EXPECT_STREQ(myCString, "myDString");
Scott Randolphbb840f72016-11-21 14:39:26 -0800104
Yifan Hong602b85a2016-10-24 13:40:01 -0700105 // casts
106 s = "great";
107 std::string myString = s;
Scott Randolpheb0c3372017-04-03 14:07:14 -0700108 const char *anotherCString = s.c_str();
Yifan Hong602b85a2016-10-24 13:40:01 -0700109 EXPECT_EQ(myString, "great");
110 EXPECT_STREQ(anotherCString, "great");
Scott Randolphbb840f72016-11-21 14:39:26 -0800111
Steven Moreland727e3dd2017-03-23 11:19:00 -0700112 const hidl_string t = "not so great";
113 std::string myTString = t;
Scott Randolpheb0c3372017-04-03 14:07:14 -0700114 const char * anotherTCString = t.c_str();
Steven Moreland727e3dd2017-03-23 11:19:00 -0700115 EXPECT_EQ(myTString, "not so great");
116 EXPECT_STREQ(anotherTCString, "not so great");
117
Scott Randolpheb0c3372017-04-03 14:07:14 -0700118 // 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
Scott Randolphbb840f72016-11-21 14:39:26 -0800128 // 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);
Steven Moreland551396a2017-02-13 18:33:20 -0800138 const char * cstr2 = "def";
139 std::string string2(cstr2);
140 hidl_string hs2(cstr2);
141
Scott Randolphbb840f72016-11-21 14:39:26 -0800142 EXPECT_TRUE(hs1 == hsE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800143 EXPECT_FALSE(hs1 == hsNE);
144 EXPECT_TRUE(hs1 == cstrE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800145 EXPECT_FALSE(hs1 == cstrNE);
146 EXPECT_TRUE(hs1 == stringE);
Steven Moreland551396a2017-02-13 18:33:20 -0800147 EXPECT_FALSE(hs1 == stringNE);
148 EXPECT_FALSE(hs1 != hsE);
149 EXPECT_TRUE(hs1 != hsNE);
150 EXPECT_FALSE(hs1 != cstrE);
151 EXPECT_TRUE(hs1 != cstrNE);
Scott Randolphbb840f72016-11-21 14:39:26 -0800152 EXPECT_FALSE(hs1 != stringE);
153 EXPECT_TRUE(hs1 != stringNE);
Steven Moreland551396a2017-02-13 18:33:20 -0800154
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);
Yifan Hong602b85a2016-10-24 13:40:01 -0700165}
166
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800167TEST_F(LibHidlTest, MemoryTest) {
168 using android::hardware::hidl_memory;
169
170 hidl_memory mem1 = hidl_memory(); // default constructor
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700171 hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800172
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
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700178 hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800179
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
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700186 hidl_memory mem6 = mem5; // NOLINT, test copying
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800187 EXPECT_EQ(nullptr, mem5.handle());
188 EXPECT_EQ(nullptr, mem6.handle());
189}
190
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800191TEST_F(LibHidlTest, VecInitTest) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700192 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
Steven Moreland96e9de02017-09-29 14:11:20 -0700197 hidl_vec<int32_t> hv0(3); // size
Steven Morelandaa661fc2017-10-06 09:40:18 -0700198 EXPECT_EQ(hv0.size(), 3ul); // cannot say anything about its contents
Steven Moreland96e9de02017-09-29 14:11:20 -0700199
Yifan Hong602b85a2016-10-24 13:40:01 -0700200 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);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800208
209 hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
Steven Morelandb69926a2016-11-15 10:02:57 -0800210 EXPECT_EQ(v3.size(), 3ul);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800211 EXPECT_ARRAYEQ(v3, array, v3.size());
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800212}
213
214TEST_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);
Scott Randolphbb840f72016-11-21 14:39:26 -0800217
218 auto iter = hv1.begin(); // iterator begin()
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800219 EXPECT_EQ(*iter++, 5);
Scott Randolphbb840f72016-11-21 14:39:26 -0800220 EXPECT_EQ(*iter, 6);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800221 EXPECT_EQ(*++iter, 7);
222 EXPECT_EQ(*iter--, 7);
223 EXPECT_EQ(*iter, 6);
224 EXPECT_EQ(*--iter, 5);
225
226 iter += 2;
Scott Randolphbb840f72016-11-21 14:39:26 -0800227 EXPECT_EQ(*iter, 7);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800228 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
251TEST_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);
Scott Randolphbb840f72016-11-21 14:39:26 -0800255
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);
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800261
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);
Yifan Hong602b85a2016-10-24 13:40:01 -0700271}
272
Yifan Hong9fcbb362016-12-20 16:46:41 -0800273TEST_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
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700283TEST_F(LibHidlTest, VecRangeCtorTest) {
284 struct ConvertibleType {
285 int val;
286
287 explicit ConvertibleType(int val) : val(val) {}
288 explicit operator int() const { return val; }
289 bool operator==(const int& other) const { return val == other; }
290 };
291
292 std::vector<ConvertibleType> input{
293 ConvertibleType(1), ConvertibleType(2), ConvertibleType(3),
294 };
295
296 android::hardware::hidl_vec<int> hv(input.begin(), input.end());
297
298 EXPECT_EQ(input.size(), hv.size());
299 int sum = 0;
300 for (unsigned i = 0; i < input.size(); i++) {
301 EXPECT_EQ(input[i], hv[i]);
302 sum += hv[i];
303 }
304 EXPECT_EQ(sum, 1 + 2 + 3);
305}
306
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800307TEST_F(LibHidlTest, ArrayTest) {
308 using android::hardware::hidl_array;
309 int32_t array[] = {5, 6, 7};
310
311 hidl_array<int32_t, 3> ha(array);
312 EXPECT_ARRAYEQ(ha, array, 3);
313}
314
Yifan Hong5e2318c2016-10-27 17:19:21 -0700315TEST_F(LibHidlTest, TaskRunnerTest) {
Yifan Hong0a351392017-03-20 17:17:52 -0700316 using android::hardware::details::TaskRunner;
Steven Moreland86fd2e12017-08-02 13:26:59 -0700317 using namespace std::chrono_literals;
318
319 std::condition_variable cv;
320 std::mutex m;
321
Yifan Hong5e2318c2016-10-27 17:19:21 -0700322 TaskRunner tr;
Yifan Hong6f667542017-03-20 19:04:05 -0700323 tr.start(1 /* limit */);
Yifan Hong5e2318c2016-10-27 17:19:21 -0700324 bool flag = false;
325 tr.push([&] {
Yifan Hong5e2318c2016-10-27 17:19:21 -0700326 flag = true;
Steven Moreland86fd2e12017-08-02 13:26:59 -0700327 cv.notify_all();
Yifan Hong5e2318c2016-10-27 17:19:21 -0700328 });
Steven Moreland86fd2e12017-08-02 13:26:59 -0700329
330 std::unique_lock<std::mutex> lock(m);
331
332 // 1s so this doesn't deadlock. This isn't a performance test.
333 EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;}));
Yifan Hong5e2318c2016-10-27 17:19:21 -0700334 EXPECT_TRUE(flag);
335}
336
Yifan Hong5708fb42016-10-26 17:50:29 -0700337TEST_F(LibHidlTest, StringCmpTest) {
338 using android::hardware::hidl_string;
339 const char * s = "good";
340 hidl_string hs(s);
341 EXPECT_NE(hs.c_str(), s);
342
343 EXPECT_TRUE(hs == s); // operator ==
344 EXPECT_TRUE(s == hs);
345
346 EXPECT_FALSE(hs != s); // operator ==
347 EXPECT_FALSE(s != hs);
348}
349
Yifan Hong602b85a2016-10-24 13:40:01 -0700350template <typename T>
351void great(android::hardware::hidl_vec<T>) {}
352
353TEST_F(LibHidlTest, VecCopyTest) {
354 android::hardware::hidl_vec<int32_t> v;
355 great(v);
356}
357
Yifan Hong44ab6232016-11-22 16:28:24 -0800358TEST_F(LibHidlTest, StdArrayTest) {
359 using android::hardware::hidl_array;
360 hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
361 std::array<int32_t, 5> stdArray = array;
362 EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
363 hidl_array<int32_t, 5> array2 = stdArray;
364 EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
365}
366
367TEST_F(LibHidlTest, MultiDimStdArrayTest) {
368 using android::hardware::hidl_array;
369 hidl_array<int32_t, 2, 3> array;
370 for (size_t i = 0; i < 2; i++) {
371 for (size_t j = 0; j < 3; j++) {
372 array[i][j] = i + j + i * j;
373 }
374 }
375 std::array<std::array<int32_t, 3>, 2> stdArray = array;
376 EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
377 hidl_array<int32_t, 2, 3> array2 = stdArray;
378 EXPECT_2DARRAYEQ(array, array2, 2, 3);
379}
380
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800381TEST_F(LibHidlTest, HidlVersionTest) {
382 using android::hardware::hidl_version;
383 hidl_version v1_0{1, 0};
384 EXPECT_EQ(1, v1_0.get_major());
385 EXPECT_EQ(0, v1_0.get_minor());
386 hidl_version v2_0{2, 0};
387 hidl_version v2_1{2, 1};
388 hidl_version v2_2{2, 2};
389 hidl_version v3_0{3, 0};
390 hidl_version v3_0b{3,0};
391
392 EXPECT_TRUE(v1_0 < v2_0);
Steven Morelandb328f012018-06-25 16:07:22 -0700393 EXPECT_TRUE(v1_0 != v2_0);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800394 EXPECT_TRUE(v2_0 < v2_1);
395 EXPECT_TRUE(v2_1 < v3_0);
396 EXPECT_TRUE(v2_0 > v1_0);
Steven Morelandb328f012018-06-25 16:07:22 -0700397 EXPECT_TRUE(v2_0 != v1_0);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800398 EXPECT_TRUE(v2_1 > v2_0);
399 EXPECT_TRUE(v3_0 > v2_1);
400 EXPECT_TRUE(v3_0 == v3_0b);
Steven Morelandb328f012018-06-25 16:07:22 -0700401 EXPECT_FALSE(v3_0 != v3_0b);
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800402 EXPECT_TRUE(v3_0 <= v3_0b);
403 EXPECT_TRUE(v2_2 <= v3_0);
404 EXPECT_TRUE(v3_0 >= v3_0b);
405 EXPECT_TRUE(v3_0 >= v2_2);
406}
407
Yifan Hongaf4e43c2017-03-03 19:10:52 -0800408TEST_F(LibHidlTest, ReturnMoveTest) {
409 using namespace ::android;
410 using ::android::hardware::Return;
411 using ::android::hardware::Status;
412 Return<void> ret{Status::fromStatusT(DEAD_OBJECT)};
413 ret.isOk();
414 ret = {Status::fromStatusT(DEAD_OBJECT)};
415 ret.isOk();
416}
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800417
Steven Moreland49172962017-04-28 16:08:43 -0700418TEST_F(LibHidlTest, ReturnTest) {
419 using ::android::DEAD_OBJECT;
420 using ::android::hardware::Return;
421 using ::android::hardware::Status;
422 using ::android::hardware::hidl_string;
423
424 EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk());
425 EXPECT_TRUE(Return<void>(Status::ok()).isOk());
426
427 hidl_string one = "1";
428 hidl_string two = "2";
429 Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT));
430
431 EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two));
432 EXPECT_EQ(two, ret.withDefault(two));
433
434 hidl_string&& moved = ret.withDefault(std::move(two));
435 EXPECT_EQ("2", moved);
436
437 const hidl_string three = "3";
438 EXPECT_EQ(three, ret.withDefault(three));
439}
440
Yifan Hong2be94182017-03-03 19:07:38 -0800441std::string toString(const ::android::hardware::Status &s) {
442 using ::android::hardware::operator<<;
443 std::ostringstream oss;
444 oss << s;
445 return oss.str();
446}
447
448TEST_F(LibHidlTest, StatusStringTest) {
449 using namespace ::android;
450 using ::android::hardware::Status;
451 using ::testing::HasSubstr;
452
453 EXPECT_EQ(toString(Status::ok()), "No error");
454
455 EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT"));
456
457 EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy"));
458
Yifan Hong2be94182017-03-03 19:07:38 -0800459 EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)),
460 HasSubstr("EX_NULL_POINTER"));
Steven Moreland951fb132017-11-07 14:24:07 -0800461}
Yifan Hong2be94182017-03-03 19:07:38 -0800462
Steven Moreland951fb132017-11-07 14:24:07 -0800463TEST_F(LibHidlTest, PreloadTest) {
464 using ::android::hardware::preloadPassthroughService;
Steven Moreland138c3502018-11-27 14:27:04 -0800465 using ::android::hidl::memory::V1_0::IMemory;
Steven Moreland951fb132017-11-07 14:24:07 -0800466
Steven Moreland138c3502018-11-27 14:27:04 -0800467 // installed on all devices by default in both bitnesses and not otherwise a dependency of this
468 // test.
469 static const std::string kLib = "android.hidl.memory@1.0-impl.so";
Steven Moreland951fb132017-11-07 14:24:07 -0800470
471 EXPECT_FALSE(isLibraryOpen(kLib));
Steven Moreland138c3502018-11-27 14:27:04 -0800472 preloadPassthroughService<IMemory>();
Steven Moreland951fb132017-11-07 14:24:07 -0800473 EXPECT_TRUE(isLibraryOpen(kLib));
Yifan Hong2be94182017-03-03 19:07:38 -0800474}
475
Yifan Hong602b85a2016-10-24 13:40:01 -0700476int main(int argc, char **argv) {
477 ::testing::InitGoogleTest(&argc, argv);
478 return RUN_ALL_TESTS();
479}