Yifan Hong | b93f050 | 2016-10-28 10:42:57 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 17 | #define LOG_TAG "LibHidlTest" |
| 18 | |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 19 | #pragma clang diagnostic push |
Steven Moreland | 3bbb383 | 2021-01-23 01:35:03 +0000 | [diff] [blame] | 20 | #pragma clang diagnostic error "-Wpadded" |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 21 | #include <hidl/HidlInternal.h> |
| 22 | #include <hidl/HidlSupport.h> |
| 23 | #pragma clang diagnostic pop |
| 24 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Steven Moreland | 138c350 | 2018-11-27 14:27:04 -0800 | [diff] [blame] | 26 | #include <android/hidl/memory/1.0/IMemory.h> |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 27 | #include <gmock/gmock.h> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 28 | #include <gtest/gtest.h> |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 29 | #include <hidl/ServiceManagement.h> |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 30 | #include <hidl/Status.h> |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 31 | #include <hidl/TaskRunner.h> |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 32 | #include <condition_variable> |
| 33 | #include <fstream> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 36 | #ifdef __ANDROID__ |
| 37 | static bool kAndroid = true; |
| 38 | #else |
| 39 | static bool kAndroid = false; |
| 40 | #endif |
| 41 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 42 | #define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__)) |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 43 | #define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \ |
| 44 | EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__)) |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 45 | |
| 46 | template<typename T, typename S> |
| 47 | static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) { |
| 48 | for(size_t i = 0; i < size; i++) |
| 49 | if(arr1[i] != arr2[i]) |
| 50 | return false; |
| 51 | return true; |
| 52 | } |
| 53 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 54 | template<typename T, typename S> |
| 55 | static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) { |
| 56 | for(size_t i = 0; i < size1; i++) |
| 57 | for (size_t j = 0; j < size2; j++) |
| 58 | if(arr1[i][j] != arr2[i][j]) |
| 59 | return false; |
| 60 | return true; |
| 61 | } |
| 62 | |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 63 | bool isLibraryOpen(const std::string& lib) { |
| 64 | std::ifstream ifs("/proc/self/maps"); |
| 65 | for (std::string line; std::getline(ifs, line);) { |
| 66 | if (line.size() >= lib.size() && line.substr(line.size() - lib.size()) == lib) { |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 74 | class LibHidlTest : public ::testing::Test { |
| 75 | public: |
| 76 | virtual void SetUp() override { |
| 77 | } |
| 78 | virtual void TearDown() override { |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | TEST_F(LibHidlTest, StringTest) { |
| 83 | using android::hardware::hidl_string; |
| 84 | hidl_string s; // empty constructor |
| 85 | EXPECT_STREQ(s.c_str(), ""); |
| 86 | hidl_string s1 = "s1"; // copy = from cstr |
| 87 | EXPECT_STREQ(s1.c_str(), "s1"); |
| 88 | hidl_string s2("s2"); // copy constructor from cstr |
| 89 | EXPECT_STREQ(s2.c_str(), "s2"); |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 90 | hidl_string s2a(nullptr); // copy constructor from null cstr |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 91 | EXPECT_STREQ("", s2a.c_str()); |
Steven Moreland | 153f87a | 2017-02-28 09:42:26 -0800 | [diff] [blame] | 92 | s2a = nullptr; // = from nullptr cstr |
| 93 | EXPECT_STREQ(s2a.c_str(), ""); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 94 | hidl_string s3 = hidl_string("s3"); // move = |
| 95 | EXPECT_STREQ(s3.c_str(), "s3"); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 96 | hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length |
| 97 | EXPECT_STREQ(s4.c_str(), "123"); |
| 98 | hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor |
| 99 | EXPECT_STREQ(s5.c_str(), "s5"); |
| 100 | hidl_string s6(std::string("s6")); // copy constructor from std::string |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 101 | EXPECT_STREQ(s6.c_str(), "s6"); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 102 | hidl_string s7 = std::string("s7"); // copy = from std::string |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 103 | EXPECT_STREQ(s7.c_str(), "s7"); |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 104 | hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 105 | EXPECT_STREQ(s8.c_str(), "s7"); |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 106 | hidl_string s9 = s8; // copy = // NOLINT, test the copy operator |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 107 | EXPECT_STREQ(s9.c_str(), "s7"); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 108 | char myCString[20] = "myCString"; |
| 109 | s.setToExternal(&myCString[0], strlen(myCString)); |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 110 | EXPECT_STREQ(s.c_str(), "myCString"); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 111 | myCString[2] = 'D'; |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 112 | EXPECT_STREQ(s.c_str(), "myDString"); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 113 | s.clear(); // should not affect myCString |
| 114 | EXPECT_STREQ(myCString, "myDString"); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 115 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 116 | // casts |
| 117 | s = "great"; |
| 118 | std::string myString = s; |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 119 | const char *anotherCString = s.c_str(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 120 | EXPECT_EQ(myString, "great"); |
| 121 | EXPECT_STREQ(anotherCString, "great"); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 122 | |
Steven Moreland | 727e3dd | 2017-03-23 11:19:00 -0700 | [diff] [blame] | 123 | const hidl_string t = "not so great"; |
| 124 | std::string myTString = t; |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 125 | const char * anotherTCString = t.c_str(); |
Steven Moreland | 727e3dd | 2017-03-23 11:19:00 -0700 | [diff] [blame] | 126 | EXPECT_EQ(myTString, "not so great"); |
| 127 | EXPECT_STREQ(anotherTCString, "not so great"); |
| 128 | |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 129 | // Assignment from hidl_string to std::string |
| 130 | std::string tgt; |
| 131 | hidl_string src("some stuff"); |
| 132 | tgt = src; |
| 133 | EXPECT_STREQ(tgt.c_str(), "some stuff"); |
| 134 | |
| 135 | // Stream output operator |
| 136 | hidl_string msg("hidl_string works with operator<<"); |
| 137 | std::cout << msg; |
| 138 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 139 | // Comparisons |
| 140 | const char * cstr1 = "abc"; |
| 141 | std::string string1(cstr1); |
| 142 | hidl_string hs1(cstr1); |
| 143 | const char * cstrE = "abc"; |
| 144 | std::string stringE(cstrE); |
| 145 | hidl_string hsE(cstrE); |
| 146 | const char * cstrNE = "ABC"; |
| 147 | std::string stringNE(cstrNE); |
| 148 | hidl_string hsNE(cstrNE); |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 149 | const char * cstr2 = "def"; |
| 150 | std::string string2(cstr2); |
| 151 | hidl_string hs2(cstr2); |
| 152 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 153 | EXPECT_TRUE(hs1 == hsE); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 154 | EXPECT_FALSE(hs1 == hsNE); |
| 155 | EXPECT_TRUE(hs1 == cstrE); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 156 | EXPECT_FALSE(hs1 == cstrNE); |
| 157 | EXPECT_TRUE(hs1 == stringE); |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 158 | EXPECT_FALSE(hs1 == stringNE); |
| 159 | EXPECT_FALSE(hs1 != hsE); |
| 160 | EXPECT_TRUE(hs1 != hsNE); |
| 161 | EXPECT_FALSE(hs1 != cstrE); |
| 162 | EXPECT_TRUE(hs1 != cstrNE); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 163 | EXPECT_FALSE(hs1 != stringE); |
| 164 | EXPECT_TRUE(hs1 != stringNE); |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 165 | |
| 166 | EXPECT_TRUE(hs1 < hs2); |
| 167 | EXPECT_FALSE(hs2 < hs1); |
| 168 | EXPECT_TRUE(hs2 > hs1); |
| 169 | EXPECT_FALSE(hs1 > hs2); |
| 170 | EXPECT_TRUE(hs1 <= hs1); |
| 171 | EXPECT_TRUE(hs1 <= hs2); |
| 172 | EXPECT_FALSE(hs2 <= hs1); |
| 173 | EXPECT_TRUE(hs1 >= hs1); |
| 174 | EXPECT_TRUE(hs2 >= hs1); |
| 175 | EXPECT_FALSE(hs2 <= hs1); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Steven Moreland | e5b0df2 | 2021-03-29 17:28:48 +0000 | [diff] [blame] | 178 | // empty string optimization should apply for any constructor |
| 179 | TEST_F(LibHidlTest, HidlStringEmptyLiteralAllocation) { |
| 180 | using android::hardware::hidl_string; |
| 181 | |
| 182 | hidl_string empty1; |
| 183 | hidl_string empty2(""); |
| 184 | hidl_string empty3("foo", 0); |
| 185 | hidl_string empty4((std::string())); |
| 186 | |
| 187 | EXPECT_EQ(empty1.c_str(), empty2.c_str()); |
| 188 | EXPECT_EQ(empty1.c_str(), empty3.c_str()); |
| 189 | EXPECT_EQ(empty1.c_str(), empty4.c_str()); |
| 190 | } |
| 191 | |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 192 | TEST_F(LibHidlTest, MemoryTest) { |
| 193 | using android::hardware::hidl_memory; |
| 194 | |
| 195 | hidl_memory mem1 = hidl_memory(); // default constructor |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 196 | hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 197 | |
| 198 | EXPECT_EQ(nullptr, mem2.handle()); |
| 199 | |
| 200 | native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */); |
| 201 | |
| 202 | hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 203 | hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 204 | |
| 205 | EXPECT_EQ(mem3.name(), mem4.name()); |
| 206 | EXPECT_EQ(mem3.size(), mem4.size()); |
| 207 | EXPECT_NE(nullptr, mem4.handle()); |
| 208 | EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned |
| 209 | |
| 210 | hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 211 | hidl_memory mem6 = mem5; // NOLINT, test copying |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 212 | EXPECT_EQ(nullptr, mem5.handle()); |
| 213 | EXPECT_EQ(nullptr, mem6.handle()); |
| 214 | } |
| 215 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 216 | TEST_F(LibHidlTest, VecInitTest) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 217 | using android::hardware::hidl_vec; |
| 218 | using std::vector; |
| 219 | int32_t array[] = {5, 6, 7}; |
| 220 | vector<int32_t> v(array, array + 3); |
| 221 | |
Steven Moreland | 96e9de0 | 2017-09-29 14:11:20 -0700 | [diff] [blame] | 222 | hidl_vec<int32_t> hv0(3); // size |
Steven Moreland | aa661fc | 2017-10-06 09:40:18 -0700 | [diff] [blame] | 223 | EXPECT_EQ(hv0.size(), 3ul); // cannot say anything about its contents |
Steven Moreland | 96e9de0 | 2017-09-29 14:11:20 -0700 | [diff] [blame] | 224 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 225 | hidl_vec<int32_t> hv1 = v; // copy = |
| 226 | EXPECT_ARRAYEQ(hv1, array, 3); |
| 227 | EXPECT_ARRAYEQ(hv1, v, 3); |
| 228 | hidl_vec<int32_t> hv2(v); // copy constructor |
| 229 | EXPECT_ARRAYEQ(hv2, v, 3); |
| 230 | |
| 231 | vector<int32_t> v2 = hv1; // cast |
| 232 | EXPECT_ARRAYEQ(v2, v, 3); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 233 | |
| 234 | hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 235 | EXPECT_EQ(v3.size(), 3ul); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 236 | EXPECT_ARRAYEQ(v3, array, v3.size()); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Steven Moreland | e873cf3 | 2020-04-03 10:55:12 -0700 | [diff] [blame] | 239 | TEST_F(LibHidlTest, VecReleaseTest) { |
| 240 | // this test indicates an inconsistency of behaviors which is undesirable. |
| 241 | // Perhaps hidl-vec should always allocate an empty vector whenever it |
| 242 | // exposes its data. Alternatively, perhaps it should always free/reject |
| 243 | // empty vectors and always return nullptr for this state. While this second |
| 244 | // alternative is faster, it makes client code harder to write, and it would |
| 245 | // break existing client code. |
| 246 | using android::hardware::hidl_vec; |
| 247 | |
| 248 | hidl_vec<int32_t> empty; |
| 249 | EXPECT_EQ(nullptr, empty.releaseData()); |
| 250 | |
| 251 | empty.resize(0); |
| 252 | int32_t* data = empty.releaseData(); |
| 253 | EXPECT_NE(nullptr, data); |
| 254 | delete data; |
| 255 | } |
| 256 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 257 | TEST_F(LibHidlTest, VecIterTest) { |
| 258 | int32_t array[] = {5, 6, 7}; |
| 259 | android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 260 | |
| 261 | auto iter = hv1.begin(); // iterator begin() |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 262 | EXPECT_EQ(*iter++, 5); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 263 | EXPECT_EQ(*iter, 6); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 264 | EXPECT_EQ(*++iter, 7); |
| 265 | EXPECT_EQ(*iter--, 7); |
| 266 | EXPECT_EQ(*iter, 6); |
| 267 | EXPECT_EQ(*--iter, 5); |
| 268 | |
| 269 | iter += 2; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 270 | EXPECT_EQ(*iter, 7); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 271 | iter -= 2; |
| 272 | EXPECT_EQ(*iter, 5); |
| 273 | |
| 274 | iter++; |
| 275 | EXPECT_EQ(*(iter + 1), 7); |
| 276 | EXPECT_EQ(*(1 + iter), 7); |
| 277 | EXPECT_EQ(*(iter - 1), 5); |
| 278 | EXPECT_EQ(*iter, 6); |
| 279 | |
| 280 | auto five = iter - 1; |
| 281 | auto seven = iter + 1; |
| 282 | EXPECT_EQ(seven - five, 2); |
| 283 | EXPECT_EQ(five - seven, -2); |
| 284 | |
| 285 | EXPECT_LT(five, seven); |
| 286 | EXPECT_LE(five, seven); |
| 287 | EXPECT_GT(seven, five); |
| 288 | EXPECT_GE(seven, five); |
| 289 | |
| 290 | EXPECT_EQ(seven[0], 7); |
| 291 | EXPECT_EQ(five[1], 6); |
| 292 | } |
| 293 | |
| 294 | TEST_F(LibHidlTest, VecIterForTest) { |
| 295 | using android::hardware::hidl_vec; |
| 296 | int32_t array[] = {5, 6, 7}; |
| 297 | hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 298 | |
| 299 | int32_t sum = 0; // range based for loop interoperability |
| 300 | for (auto &&i: hv1) { |
| 301 | sum += i; |
| 302 | } |
| 303 | EXPECT_EQ(sum, 5+6+7); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 304 | |
| 305 | for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) { |
| 306 | *iter += 10; |
| 307 | } |
| 308 | const hidl_vec<int32_t> &v4 = hv1; |
| 309 | sum = 0; |
| 310 | for (const auto &i : v4) { |
| 311 | sum += i; |
| 312 | } |
| 313 | EXPECT_EQ(sum, 15+16+17); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 316 | TEST_F(LibHidlTest, VecEqTest) { |
| 317 | android::hardware::hidl_vec<int32_t> hv1{5, 6, 7}; |
| 318 | android::hardware::hidl_vec<int32_t> hv2{5, 6, 7}; |
| 319 | android::hardware::hidl_vec<int32_t> hv3{5, 6, 8}; |
| 320 | |
| 321 | // use the == and != operator intentionally here |
| 322 | EXPECT_TRUE(hv1 == hv2); |
| 323 | EXPECT_TRUE(hv1 != hv3); |
| 324 | } |
| 325 | |
Siarhei Vishniakou | c48d4f6 | 2019-02-08 12:48:04 -0800 | [diff] [blame] | 326 | TEST_F(LibHidlTest, VecEqInitializerTest) { |
| 327 | std::vector<int32_t> reference{5, 6, 7}; |
| 328 | android::hardware::hidl_vec<int32_t> hv1{1, 2, 3}; |
| 329 | hv1 = {5, 6, 7}; |
| 330 | android::hardware::hidl_vec<int32_t> hv2; |
| 331 | hv2 = {5, 6, 7}; |
| 332 | android::hardware::hidl_vec<int32_t> hv3; |
| 333 | hv3 = {5, 6, 8}; |
| 334 | |
| 335 | // use the == and != operator intentionally here |
| 336 | EXPECT_TRUE(hv1 == hv2); |
| 337 | EXPECT_TRUE(hv1 == reference); |
| 338 | EXPECT_TRUE(hv1 != hv3); |
| 339 | } |
| 340 | |
Tomasz Wasilczyk | a9f6073 | 2017-06-30 10:53:22 -0700 | [diff] [blame] | 341 | TEST_F(LibHidlTest, VecRangeCtorTest) { |
| 342 | struct ConvertibleType { |
| 343 | int val; |
| 344 | |
| 345 | explicit ConvertibleType(int val) : val(val) {} |
| 346 | explicit operator int() const { return val; } |
| 347 | bool operator==(const int& other) const { return val == other; } |
| 348 | }; |
| 349 | |
| 350 | std::vector<ConvertibleType> input{ |
| 351 | ConvertibleType(1), ConvertibleType(2), ConvertibleType(3), |
| 352 | }; |
| 353 | |
| 354 | android::hardware::hidl_vec<int> hv(input.begin(), input.end()); |
| 355 | |
| 356 | EXPECT_EQ(input.size(), hv.size()); |
| 357 | int sum = 0; |
| 358 | for (unsigned i = 0; i < input.size(); i++) { |
| 359 | EXPECT_EQ(input[i], hv[i]); |
| 360 | sum += hv[i]; |
| 361 | } |
| 362 | EXPECT_EQ(sum, 1 + 2 + 3); |
| 363 | } |
| 364 | |
Steven Moreland | dfbedb8 | 2019-06-19 14:51:11 -0700 | [diff] [blame] | 365 | struct FailsIfCopied { |
| 366 | FailsIfCopied() {} |
| 367 | |
| 368 | // add failure if copied since in general this can be expensive |
| 369 | FailsIfCopied(const FailsIfCopied& o) { *this = o; } |
| 370 | FailsIfCopied& operator=(const FailsIfCopied&) { |
| 371 | ADD_FAILURE() << "FailsIfCopied copied"; |
| 372 | return *this; |
| 373 | } |
| 374 | |
| 375 | // fine to move this type since in general this is cheaper |
| 376 | FailsIfCopied(FailsIfCopied&& o) = default; |
| 377 | FailsIfCopied& operator=(FailsIfCopied&&) = default; |
| 378 | }; |
| 379 | |
| 380 | TEST_F(LibHidlTest, VecResizeNoCopy) { |
| 381 | using android::hardware::hidl_vec; |
| 382 | |
| 383 | hidl_vec<FailsIfCopied> noCopies; |
| 384 | noCopies.resize(3); // instantiates three elements |
| 385 | |
| 386 | FailsIfCopied* oldPointer = noCopies.data(); |
| 387 | |
| 388 | noCopies.resize(6); // should move three elements, not copy |
| 389 | |
| 390 | // oldPointer should be invalidated at this point. |
| 391 | // hidl_vec doesn't currently try to realloc but if it ever switches |
| 392 | // to an implementation that does, this test wouldn't do anything. |
| 393 | EXPECT_NE(oldPointer, noCopies.data()); |
| 394 | } |
| 395 | |
Tomasz Wasilczyk | 93f7592 | 2019-07-16 08:44:15 -0700 | [diff] [blame] | 396 | TEST_F(LibHidlTest, VecFindTest) { |
| 397 | using android::hardware::hidl_vec; |
| 398 | hidl_vec<int32_t> hv1 = {10, 20, 30, 40}; |
| 399 | const hidl_vec<int32_t> hv2 = {1, 2, 3, 4}; |
| 400 | |
| 401 | auto it = hv1.find(20); |
| 402 | EXPECT_EQ(20, *it); |
| 403 | *it = 21; |
| 404 | EXPECT_EQ(21, *it); |
| 405 | it = hv1.find(20); |
| 406 | EXPECT_EQ(hv1.end(), it); |
| 407 | it = hv1.find(21); |
| 408 | EXPECT_EQ(21, *it); |
| 409 | |
| 410 | auto cit = hv2.find(4); |
| 411 | EXPECT_EQ(4, *cit); |
| 412 | } |
| 413 | |
| 414 | TEST_F(LibHidlTest, VecContainsTest) { |
| 415 | using android::hardware::hidl_vec; |
| 416 | hidl_vec<int32_t> hv1 = {10, 20, 30, 40}; |
| 417 | const hidl_vec<int32_t> hv2 = {0, 1, 2, 3, 4}; |
| 418 | |
| 419 | EXPECT_TRUE(hv1.contains(10)); |
| 420 | EXPECT_TRUE(hv1.contains(40)); |
| 421 | EXPECT_FALSE(hv1.contains(1)); |
| 422 | EXPECT_FALSE(hv1.contains(0)); |
| 423 | EXPECT_TRUE(hv2.contains(0)); |
| 424 | EXPECT_FALSE(hv2.contains(10)); |
| 425 | |
| 426 | hv1[0] = 11; |
| 427 | EXPECT_FALSE(hv1.contains(10)); |
| 428 | EXPECT_TRUE(hv1.contains(11)); |
| 429 | } |
| 430 | |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 431 | TEST_F(LibHidlTest, ArrayTest) { |
| 432 | using android::hardware::hidl_array; |
| 433 | int32_t array[] = {5, 6, 7}; |
| 434 | |
| 435 | hidl_array<int32_t, 3> ha(array); |
| 436 | EXPECT_ARRAYEQ(ha, array, 3); |
| 437 | } |
| 438 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 439 | TEST_F(LibHidlTest, TaskRunnerTest) { |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 440 | using android::hardware::details::TaskRunner; |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 441 | using namespace std::chrono_literals; |
| 442 | |
| 443 | std::condition_variable cv; |
| 444 | std::mutex m; |
| 445 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 446 | TaskRunner tr; |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 447 | tr.start(1 /* limit */); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 448 | bool flag = false; |
| 449 | tr.push([&] { |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 450 | flag = true; |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 451 | cv.notify_all(); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 452 | }); |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 453 | |
| 454 | std::unique_lock<std::mutex> lock(m); |
| 455 | |
| 456 | // 1s so this doesn't deadlock. This isn't a performance test. |
| 457 | EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;})); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 458 | EXPECT_TRUE(flag); |
| 459 | } |
| 460 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 461 | TEST_F(LibHidlTest, StringCmpTest) { |
| 462 | using android::hardware::hidl_string; |
| 463 | const char * s = "good"; |
| 464 | hidl_string hs(s); |
| 465 | EXPECT_NE(hs.c_str(), s); |
| 466 | |
| 467 | EXPECT_TRUE(hs == s); // operator == |
| 468 | EXPECT_TRUE(s == hs); |
| 469 | |
| 470 | EXPECT_FALSE(hs != s); // operator == |
| 471 | EXPECT_FALSE(s != hs); |
| 472 | } |
| 473 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 474 | template <typename T> |
| 475 | void great(android::hardware::hidl_vec<T>) {} |
| 476 | |
| 477 | TEST_F(LibHidlTest, VecCopyTest) { |
| 478 | android::hardware::hidl_vec<int32_t> v; |
| 479 | great(v); |
| 480 | } |
| 481 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 482 | TEST_F(LibHidlTest, StdArrayTest) { |
| 483 | using android::hardware::hidl_array; |
| 484 | hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}}; |
| 485 | std::array<int32_t, 5> stdArray = array; |
| 486 | EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5); |
| 487 | hidl_array<int32_t, 5> array2 = stdArray; |
| 488 | EXPECT_ARRAYEQ(array.data(), array2.data(), 5); |
| 489 | } |
| 490 | |
| 491 | TEST_F(LibHidlTest, MultiDimStdArrayTest) { |
| 492 | using android::hardware::hidl_array; |
| 493 | hidl_array<int32_t, 2, 3> array; |
| 494 | for (size_t i = 0; i < 2; i++) { |
| 495 | for (size_t j = 0; j < 3; j++) { |
| 496 | array[i][j] = i + j + i * j; |
| 497 | } |
| 498 | } |
| 499 | std::array<std::array<int32_t, 3>, 2> stdArray = array; |
| 500 | EXPECT_2DARRAYEQ(array, stdArray, 2, 3); |
| 501 | hidl_array<int32_t, 2, 3> array2 = stdArray; |
| 502 | EXPECT_2DARRAYEQ(array, array2, 2, 3); |
| 503 | } |
| 504 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 505 | TEST_F(LibHidlTest, HidlVersionTest) { |
| 506 | using android::hardware::hidl_version; |
| 507 | hidl_version v1_0{1, 0}; |
| 508 | EXPECT_EQ(1, v1_0.get_major()); |
| 509 | EXPECT_EQ(0, v1_0.get_minor()); |
| 510 | hidl_version v2_0{2, 0}; |
| 511 | hidl_version v2_1{2, 1}; |
| 512 | hidl_version v2_2{2, 2}; |
| 513 | hidl_version v3_0{3, 0}; |
| 514 | hidl_version v3_0b{3,0}; |
| 515 | |
| 516 | EXPECT_TRUE(v1_0 < v2_0); |
Steven Moreland | b328f01 | 2018-06-25 16:07:22 -0700 | [diff] [blame] | 517 | EXPECT_TRUE(v1_0 != v2_0); |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 518 | EXPECT_TRUE(v2_0 < v2_1); |
| 519 | EXPECT_TRUE(v2_1 < v3_0); |
| 520 | EXPECT_TRUE(v2_0 > v1_0); |
Steven Moreland | b328f01 | 2018-06-25 16:07:22 -0700 | [diff] [blame] | 521 | EXPECT_TRUE(v2_0 != v1_0); |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 522 | EXPECT_TRUE(v2_1 > v2_0); |
| 523 | EXPECT_TRUE(v3_0 > v2_1); |
| 524 | EXPECT_TRUE(v3_0 == v3_0b); |
Steven Moreland | b328f01 | 2018-06-25 16:07:22 -0700 | [diff] [blame] | 525 | EXPECT_FALSE(v3_0 != v3_0b); |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 526 | EXPECT_TRUE(v3_0 <= v3_0b); |
| 527 | EXPECT_TRUE(v2_2 <= v3_0); |
| 528 | EXPECT_TRUE(v3_0 >= v3_0b); |
| 529 | EXPECT_TRUE(v3_0 >= v2_2); |
| 530 | } |
| 531 | |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 532 | TEST_F(LibHidlTest, ReturnMoveTest) { |
| 533 | using namespace ::android; |
| 534 | using ::android::hardware::Return; |
| 535 | using ::android::hardware::Status; |
| 536 | Return<void> ret{Status::fromStatusT(DEAD_OBJECT)}; |
| 537 | ret.isOk(); |
| 538 | ret = {Status::fromStatusT(DEAD_OBJECT)}; |
| 539 | ret.isOk(); |
| 540 | } |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 541 | |
Steven Moreland | 4917296 | 2017-04-28 16:08:43 -0700 | [diff] [blame] | 542 | TEST_F(LibHidlTest, ReturnTest) { |
| 543 | using ::android::DEAD_OBJECT; |
| 544 | using ::android::hardware::Return; |
| 545 | using ::android::hardware::Status; |
| 546 | using ::android::hardware::hidl_string; |
| 547 | |
| 548 | EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk()); |
| 549 | EXPECT_TRUE(Return<void>(Status::ok()).isOk()); |
| 550 | |
| 551 | hidl_string one = "1"; |
| 552 | hidl_string two = "2"; |
Steven Moreland | faad16d | 2021-10-21 13:42:38 -0700 | [diff] [blame] | 553 | const Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT)); |
Steven Moreland | 4917296 | 2017-04-28 16:08:43 -0700 | [diff] [blame] | 554 | |
| 555 | EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two)); |
| 556 | EXPECT_EQ(two, ret.withDefault(two)); |
| 557 | |
| 558 | hidl_string&& moved = ret.withDefault(std::move(two)); |
| 559 | EXPECT_EQ("2", moved); |
| 560 | |
| 561 | const hidl_string three = "3"; |
| 562 | EXPECT_EQ(three, ret.withDefault(three)); |
| 563 | } |
| 564 | |
Steven Moreland | 9cafed5 | 2019-07-15 16:02:35 -0700 | [diff] [blame] | 565 | TEST_F(LibHidlTest, ReturnDies) { |
| 566 | using ::android::hardware::Return; |
| 567 | using ::android::hardware::Status; |
| 568 | |
| 569 | EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)); }, ""); |
| 570 | EXPECT_DEATH({ Return<void>(Status::fromStatusT(-EBUSY)).isDeadObject(); }, ""); |
| 571 | EXPECT_DEATH( |
| 572 | { |
| 573 | Return<int> ret = Return<int>(Status::fromStatusT(-EBUSY)); |
| 574 | int foo = ret; // should crash here |
| 575 | (void)foo; |
| 576 | ret.isOk(); |
| 577 | }, |
| 578 | ""); |
| 579 | } |
| 580 | |
Steven Moreland | d85fcd1 | 2019-11-05 16:39:53 -0800 | [diff] [blame] | 581 | TEST_F(LibHidlTest, DetectUncheckedReturn) { |
| 582 | using ::android::hardware::HidlReturnRestriction; |
| 583 | using ::android::hardware::Return; |
| 584 | using ::android::hardware::setProcessHidlReturnRestriction; |
| 585 | using ::android::hardware::Status; |
| 586 | |
| 587 | setProcessHidlReturnRestriction(HidlReturnRestriction::FATAL_IF_UNCHECKED); |
| 588 | |
| 589 | EXPECT_DEATH( |
| 590 | { |
| 591 | auto ret = Return<void>(Status::ok()); |
| 592 | (void)ret; |
| 593 | }, |
| 594 | ""); |
| 595 | EXPECT_DEATH( |
| 596 | { |
| 597 | auto ret = Return<void>(Status::ok()); |
| 598 | ret = Return<void>(Status::ok()); |
| 599 | ret.isOk(); |
| 600 | }, |
| 601 | ""); |
| 602 | |
| 603 | auto ret = Return<void>(Status::ok()); |
| 604 | (void)ret.isOk(); |
| 605 | ret = Return<void>(Status::ok()); |
| 606 | (void)ret.isOk(); |
| 607 | |
| 608 | setProcessHidlReturnRestriction(HidlReturnRestriction::NONE); |
| 609 | } |
| 610 | |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 611 | std::string toString(const ::android::hardware::Status &s) { |
| 612 | using ::android::hardware::operator<<; |
| 613 | std::ostringstream oss; |
| 614 | oss << s; |
| 615 | return oss.str(); |
| 616 | } |
| 617 | |
| 618 | TEST_F(LibHidlTest, StatusStringTest) { |
| 619 | using namespace ::android; |
| 620 | using ::android::hardware::Status; |
| 621 | using ::testing::HasSubstr; |
| 622 | |
| 623 | EXPECT_EQ(toString(Status::ok()), "No error"); |
| 624 | |
| 625 | EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT")); |
| 626 | |
| 627 | EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy")); |
| 628 | |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 629 | EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)), |
| 630 | HasSubstr("EX_NULL_POINTER")); |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 631 | } |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 632 | |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 633 | TEST_F(LibHidlTest, PreloadTest) { |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 634 | // HIDL doesn't have support to load passthrough implementations on host, but we |
| 635 | // could do this by loading implementations from the output directory |
| 636 | if (!kAndroid) GTEST_SKIP(); |
| 637 | |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 638 | using ::android::hardware::preloadPassthroughService; |
Steven Moreland | 138c350 | 2018-11-27 14:27:04 -0800 | [diff] [blame] | 639 | using ::android::hidl::memory::V1_0::IMemory; |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 640 | |
Steven Moreland | 138c350 | 2018-11-27 14:27:04 -0800 | [diff] [blame] | 641 | // installed on all devices by default in both bitnesses and not otherwise a dependency of this |
| 642 | // test. |
| 643 | static const std::string kLib = "android.hidl.memory@1.0-impl.so"; |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 644 | |
| 645 | EXPECT_FALSE(isLibraryOpen(kLib)); |
Steven Moreland | 138c350 | 2018-11-27 14:27:04 -0800 | [diff] [blame] | 646 | preloadPassthroughService<IMemory>(); |
Steven Moreland | 951fb13 | 2017-11-07 14:24:07 -0800 | [diff] [blame] | 647 | EXPECT_TRUE(isLibraryOpen(kLib)); |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Steven Moreland | f0dd160 | 2019-04-26 14:51:24 -0700 | [diff] [blame] | 650 | template <typename T, size_t start, size_t end> |
| 651 | static void assertZeroInRange(const T* t) { |
| 652 | static_assert(start < sizeof(T)); |
| 653 | static_assert(end <= sizeof(T)); |
| 654 | |
| 655 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(t); |
| 656 | |
| 657 | for (size_t i = start; i < end; i++) { |
| 658 | EXPECT_EQ(0, ptr[i]); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | template <typename T, size_t start, size_t end> |
| 663 | static void uninitTest() { |
| 664 | uint8_t buf[sizeof(T)]; |
| 665 | memset(buf, 0xFF, sizeof(T)); |
| 666 | |
| 667 | T* type = new (buf) T; |
| 668 | assertZeroInRange<T, start, end>(type); |
| 669 | type->~T(); |
| 670 | } |
| 671 | |
| 672 | TEST_F(LibHidlTest, HidlVecUninit) { |
| 673 | using ::android::hardware::hidl_vec; |
| 674 | struct SomeType {}; |
| 675 | static_assert(sizeof(hidl_vec<SomeType>) == 16); |
| 676 | |
| 677 | // padding after mOwnsBuffer |
| 678 | uninitTest<hidl_vec<SomeType>, 13, 16>(); |
| 679 | } |
| 680 | TEST_F(LibHidlTest, HidlHandleUninit) { |
| 681 | using ::android::hardware::hidl_handle; |
| 682 | static_assert(sizeof(hidl_handle) == 16); |
| 683 | |
| 684 | // padding after mOwnsHandle |
| 685 | uninitTest<hidl_handle, 9, 16>(); |
| 686 | } |
| 687 | TEST_F(LibHidlTest, HidlStringUninit) { |
| 688 | using ::android::hardware::hidl_string; |
| 689 | static_assert(sizeof(hidl_string) == 16); |
| 690 | |
| 691 | // padding after mOwnsBuffer |
| 692 | uninitTest<hidl_string, 13, 16>(); |
| 693 | } |
| 694 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 695 | int main(int argc, char **argv) { |
| 696 | ::testing::InitGoogleTest(&argc, argv); |
| 697 | return RUN_ALL_TESTS(); |
| 698 | } |