test_main.cpp 15 KB

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