blob: c713814c472796feb8718ed4dd91a79fb36e6183 [file] [log] [blame]
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001#define LOG_TAG "hidl_test_client"
Yifan Hong11992a62016-11-09 18:07:40 -08002
3#include "FooCallback.h"
Steven Moreland7b680eb2017-04-19 16:34:26 -07004#include "hidl_test.h"
Yifan Hong11992a62016-11-09 18:07:40 -08005
Andreas Huber9cd48d02016-08-03 14:25:59 -07006#include <android-base/logging.h>
7
Steven Moreland01bcb772016-11-08 15:57:25 -08008#include <android/hidl/manager/1.0/IServiceManager.h>
Steven Moreland0693f312016-11-09 15:06:14 -08009#include <android/hidl/manager/1.0/IServiceNotification.h>
Steven Moreland01bcb772016-11-08 15:57:25 -080010
Steven Morelandffc4e512017-02-16 19:22:34 -080011#include <android/hidl/allocator/1.0/IAllocator.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010012#include <android/hidl/memory/1.0/IMemory.h>
13
Steven Morelandaa2b83a2016-12-21 15:52:11 -080014#include <android/hidl/token/1.0/ITokenManager.h>
15
Steven Morelandb48a7da2016-11-11 14:12:46 -080016#include <android/hardware/tests/foo/1.0/IFoo.h>
Yifan Hong01e7cde2017-01-09 17:45:45 -080017#include <android/hardware/tests/foo/1.0/BnHwSimple.h>
Yifan Hongdef2cfb2016-12-14 16:00:24 -080018#include <android/hardware/tests/foo/1.0/BsSimple.h>
Yifan Hong01e7cde2017-01-09 17:45:45 -080019#include <android/hardware/tests/foo/1.0/BpHwSimple.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080020#include <android/hardware/tests/bar/1.0/IBar.h>
Yifan Hong5749b2c2016-11-28 12:52:36 -080021#include <android/hardware/tests/bar/1.0/IComplicated.h>
Yifan Hong87ff8232017-01-09 12:07:05 -080022#include <android/hardware/tests/bar/1.0/IImportRules.h>
Hridya Valsaraju179379a2017-02-09 16:38:12 -080023#include <android/hardware/tests/baz/1.0/IBaz.h>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070024#include <android/hardware/tests/hash/1.0/IHash.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080025#include <android/hardware/tests/inheritance/1.0/IFetcher.h>
26#include <android/hardware/tests/inheritance/1.0/IGrandparent.h>
27#include <android/hardware/tests/inheritance/1.0/IParent.h>
28#include <android/hardware/tests/inheritance/1.0/IChild.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010029#include <android/hardware/tests/memory/1.0/IMemoryTest.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080030#include <android/hardware/tests/pointer/1.0/IGraph.h>
31#include <android/hardware/tests/pointer/1.0/IPointer.h>
Andreas Huber9cd48d02016-08-03 14:25:59 -070032
Yifan Hong1dc87932016-08-19 09:51:01 -070033#include <gtest/gtest.h>
34#if GTEST_IS_THREADSAFE
35#include <sys/types.h>
Yifan Hongc70f0d82016-10-10 14:50:22 -070036#include <sys/wait.h>
Yifan Hong1dc87932016-08-19 09:51:01 -070037#include <signal.h>
38#include <errno.h>
39#include <pthread.h>
40#else
41#error "GTest did not detect pthread library."
42#endif
43
Steven Moreland01bcb772016-11-08 15:57:25 -080044#include <algorithm>
Steven Moreland0693f312016-11-09 15:06:14 -080045#include <condition_variable>
Yifan Hongd12398d2016-10-13 11:05:29 -070046#include <getopt.h>
47#include <inttypes.h>
Steven Moreland0693f312016-11-09 15:06:14 -080048#include <mutex>
Steven Moreland01bcb772016-11-08 15:57:25 -080049#include <set>
Yifan Hongbf459bc2016-08-23 16:50:37 -070050#include <sstream>
Yifan Hongccd782b2016-11-28 09:41:46 -080051#include <utility>
Yifan Hongd12398d2016-10-13 11:05:29 -070052#include <vector>
Yifan Hongbf459bc2016-08-23 16:50:37 -070053
Yifan Hong398e6fb2016-10-17 11:38:09 -070054#include <hidl-test/FooHelper.h>
55#include <hidl-test/PointerHelper.h>
56
Martijn Coenen93915102016-09-01 01:35:52 +020057#include <hidl/Status.h>
Steven Moreland7b680eb2017-04-19 16:34:26 -070058#include <hidl/ServiceManagement.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010059#include <hidlmemory/mapping.h>
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010060
Iliyan Malchev0acf4192016-08-22 19:33:20 -070061#include <utils/Condition.h>
62#include <utils/Timers.h>
63
Yifan Hongbf459bc2016-08-23 16:50:37 -070064#define EXPECT_OK(__ret__) EXPECT_TRUE(isOk(__ret__))
Yifan Hong84465902016-09-27 15:52:17 -070065#define EXPECT_FAIL(__ret__) EXPECT_FALSE(isOk(__ret__))
Yifan Hong8c48ad72016-10-14 11:34:59 -070066#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hongbf459bc2016-08-23 16:50:37 -070067
68// TODO uncomment this when kernel is patched with pointer changes.
69//#define HIDL_RUN_POINTER_TESTS 1
70
Yifan Hongca890522016-10-12 10:03:41 -070071// forward declarations.
Hridya Valsaraju179379a2017-02-09 16:38:12 -080072class HidlEnvironment;
Yifan Hongca890522016-10-12 10:03:41 -070073
Yifan Hongc70f0d82016-10-10 14:50:22 -070074// static storage
Hridya Valsaraju179379a2017-02-09 16:38:12 -080075enum TestMode {
Yifan Hongc70f0d82016-10-10 14:50:22 -070076 BINDERIZED,
77 PASSTHROUGH
Hridya Valsaraju179379a2017-02-09 16:38:12 -080078};
79
80static HidlEnvironment *gHidlEnvironment = nullptr;
Yifan Hongc70f0d82016-10-10 14:50:22 -070081
Andreas Huber86a112b2016-10-19 14:25:16 -070082using ::android::hardware::tests::foo::V1_0::Abc;
Steven Moreland88ca4512016-08-11 11:24:10 -070083using ::android::hardware::tests::foo::V1_0::IFoo;
84using ::android::hardware::tests::foo::V1_0::IFooCallback;
Andreas Huber86a112b2016-10-19 14:25:16 -070085using ::android::hardware::tests::foo::V1_0::ISimple;
Yifan Hong11992a62016-11-09 18:07:40 -080086using ::android::hardware::tests::foo::V1_0::implementation::FooCallback;
Steven Moreland88ca4512016-08-11 11:24:10 -070087using ::android::hardware::tests::bar::V1_0::IBar;
Yifan Hong5749b2c2016-11-28 12:52:36 -080088using ::android::hardware::tests::bar::V1_0::IComplicated;
Hridya Valsaraju179379a2017-02-09 16:38:12 -080089using ::android::hardware::tests::baz::V1_0::IBaz;
Yifan Hong30b5d1f2017-04-03 12:19:25 -070090using ::android::hardware::tests::hash::V1_0::IHash;
Yifan Hong1e81c532016-10-18 18:43:46 -070091using ::android::hardware::tests::inheritance::V1_0::IFetcher;
92using ::android::hardware::tests::inheritance::V1_0::IGrandparent;
93using ::android::hardware::tests::inheritance::V1_0::IParent;
94using ::android::hardware::tests::inheritance::V1_0::IChild;
Yifan Hongbf459bc2016-08-23 16:50:37 -070095using ::android::hardware::tests::pointer::V1_0::IGraph;
96using ::android::hardware::tests::pointer::V1_0::IPointer;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010097using ::android::hardware::tests::memory::V1_0::IMemoryTest;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -070098using ::android::hardware::Return;
Iliyan Malchevd57066f2016-09-08 13:59:38 -070099using ::android::hardware::Void;
Andreas Huberf03332a2016-09-22 15:35:43 -0700100using ::android::hardware::hidl_array;
Martijn Coenen115d4282016-12-19 05:14:04 +0100101using ::android::hardware::hidl_death_recipient;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100102using ::android::hardware::hidl_memory;
Andreas Huber8a82ff72016-08-04 10:29:39 -0700103using ::android::hardware::hidl_string;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100104using ::android::hardware::hidl_vec;
Steven Morelandffc4e512017-02-16 19:22:34 -0800105using ::android::hidl::allocator::V1_0::IAllocator;
Martijn Coenen115d4282016-12-19 05:14:04 +0100106using ::android::hidl::base::V1_0::IBase;
Steven Moreland01bcb772016-11-08 15:57:25 -0800107using ::android::hidl::manager::V1_0::IServiceManager;
Steven Moreland0693f312016-11-09 15:06:14 -0800108using ::android::hidl::manager::V1_0::IServiceNotification;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100109using ::android::hidl::memory::V1_0::IMemory;
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800110using ::android::hidl::token::V1_0::ITokenManager;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700111using ::android::sp;
Martijn Coenen115d4282016-12-19 05:14:04 +0100112using ::android::wp;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700113using ::android::to_string;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700114using ::android::Mutex;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700115using ::android::MultiDimensionalToString;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700116using ::android::Condition;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700117using ::android::DELAY_S;
118using ::android::DELAY_NS;
119using ::android::TOLERANCE_NS;
120using ::android::ONEWAY_TOLERANCE_NS;
121using std::to_string;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700122
Yifan Hongbf459bc2016-08-23 16:50:37 -0700123template <typename T>
Yifan Honga3e8b232017-02-22 10:56:09 -0800124static inline ::testing::AssertionResult isOk(const ::android::hardware::Return<T> &ret) {
Steven Morelandcd00b9b2016-12-29 10:34:03 -0800125 return ret.isOk()
126 ? (::testing::AssertionSuccess() << ret.description())
127 : (::testing::AssertionFailure() << ret.description());
Yifan Hongbf459bc2016-08-23 16:50:37 -0700128}
129
130template<typename T, typename S>
131static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
132 for(size_t i = 0; i < size; i++)
133 if(arr1[i] != arr2[i])
134 return false;
135 return true;
136}
137
Steven Moreland01bcb772016-11-08 15:57:25 -0800138template<typename T>
139std::string to_string(std::set<T> set) {
140 std::stringstream ss;
141 ss << "{";
142
143 bool first = true;
144 for (const T &item : set) {
145 if (first) {
146 first = false;
147 } else {
148 ss << ", ";
149 }
150
151 ss << to_string(item);
152 }
153
154 ss << "}";
155
156 return ss.str();
157}
158
Andreas Huber86a112b2016-10-19 14:25:16 -0700159struct Simple : public ISimple {
160 Simple(int32_t cookie)
161 : mCookie(cookie) {
162 }
163
164 Return<int32_t> getCookie() override {
165 return mCookie;
166 }
167
Yifan Hong5749b2c2016-11-28 12:52:36 -0800168 Return<void> customVecInt(customVecInt_cb _cb) override {
169 _cb(hidl_vec<int32_t>());
170 return Void();
171 }
172
173 Return<void> customVecStr(customVecStr_cb _cb) override {
174 hidl_vec<hidl_string> vec;
175 vec.resize(2);
176 _cb(vec);
177 return Void();
178 }
179
180 Return<void> mystr(mystr_cb _cb) override {
181 _cb(hidl_string());
182 return Void();
183 }
184
185 Return<void> myhandle(myhandle_cb _cb) override {
186 auto h = native_handle_create(0, 1);
187 _cb(h);
188 native_handle_delete(h);
189 return Void();
190 }
191
192private:
193 int32_t mCookie;
194};
195
Steven Moreland21aa43c2017-03-07 17:15:24 -0800196struct SimpleParent : public IParent {
197 Return<void> doGrandparent() override {
198 return Void();
199 }
200 Return<void> doParent() override {
201 return Void();
202 }
203};
204
205struct SimpleChild : public IChild {
206 Return<void> doGrandparent() override {
207 return Void();
208 }
209 Return <void> doParent() override {
210 return Void();
211 }
212 Return <void> doChild() override {
213 return Void();
214 }
215};
216
Yifan Hong5749b2c2016-11-28 12:52:36 -0800217struct Complicated : public IComplicated {
218 Complicated(int32_t cookie)
219 : mCookie(cookie) {
220 }
221
222 Return<int32_t> getCookie() override {
223 return mCookie;
224 }
225
226 Return<void> customVecInt(customVecInt_cb _cb) override {
227 _cb(hidl_vec<int32_t>());
228 return Void();
229 }
230 Return<void> customVecStr(customVecStr_cb _cb) override {
231 hidl_vec<hidl_string> vec;
232 vec.resize(2);
233 _cb(vec);
234 return Void();
235 }
236
237 Return<void> mystr(mystr_cb _cb) override {
238 _cb(hidl_string());
239 return Void();
240 }
241
242 Return<void> myhandle(myhandle_cb _cb) override {
243 auto h = native_handle_create(0, 1);
244 _cb(h);
245 native_handle_delete(h);
246 return Void();
247 }
248
Andreas Huber86a112b2016-10-19 14:25:16 -0700249private:
250 int32_t mCookie;
251};
Steven Moreland0693f312016-11-09 15:06:14 -0800252
Yifan Hong87ff8232017-01-09 12:07:05 -0800253// Ensure (statically) that the types in IImportRules resolves to the correct types by
254// overriding the methods with fully namespaced types as arguments.
255struct MyImportRules : public ::android::hardware::tests::bar::V1_0::IImportRules {
256 Return<void> rule0a(
257 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer&) override {
258 return Void();
259 }
260
261 Return<void> rule0a1(
262 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer&) override {
263 return Void();
264 }
265
266 Return<void> rule0b(
267 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer&) override {
268 return Void();
269 }
270
271 Return<void> rule0c(const ::android::hardware::tests::foo::V1_0::Outer&) override {
272 return Void();
273 }
274
275 Return<void> rule0d(const ::android::hardware::tests::foo::V1_0::Outer&) override {
276 return Void();
277 }
278
279 Return<void> rule0e(
280 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer::Inner&) override {
281 return Void();
282 }
283
284 Return<void> rule0f(
285 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer::Inner&) override {
286 return Void();
287 }
288
289 Return<void> rule0g(const ::android::hardware::tests::foo::V1_0::Outer::Inner&) override {
290 return Void();
291 }
292
293 Return<void> rule0h(const ::android::hardware::tests::foo::V1_0::Outer::Inner&) override {
294 return Void();
295 }
296
297 Return<void> rule1a(const ::android::hardware::tests::bar::V1_0::Def&) override {
298 return Void();
299 }
300
301 Return<void> rule1b(const ::android::hardware::tests::foo::V1_0::Def&) override {
302 return Void();
303 }
304
305 Return<void> rule2a(const ::android::hardware::tests::foo::V1_0::Unrelated&) override {
306 return Void();
307 }
308
309 Return<void> rule2b(const sp<::android::hardware::tests::foo::V1_0::IFooCallback>&) override {
310 return Void();
311 }
312};
313
Steven Moreland0693f312016-11-09 15:06:14 -0800314struct ServiceNotification : public IServiceNotification {
315 std::mutex mutex;
316 std::condition_variable condition;
317
318 Return<void> onRegistration(const hidl_string &fqName,
319 const hidl_string &name,
320 bool preexisting) override {
321 if (preexisting) {
322 // not interested in things registered from previous runs of hidl_test
323 return Void();
324 }
325
326 std::unique_lock<std::mutex> lock(mutex);
327
328 mRegistered.push_back(std::string(fqName.c_str()) + "/" + name.c_str());
329
330 lock.unlock();
331 condition.notify_one();
332
333 return Void();
334 }
335
336 const std::vector<std::string> &getRegistrations() const {
337 return mRegistered;
338 }
339
340private:
341 std::vector<std::string> mRegistered{};
342};
343
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800344class HidlEnvironment : public ::testing::Environment {
Yifan Hong1dc87932016-08-19 09:51:01 -0700345public:
Steven Moreland01bcb772016-11-08 15:57:25 -0800346 sp<IServiceManager> manager;
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800347 sp<ITokenManager> tokenManager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100348 sp<IAllocator> ashmemAllocator;
349 sp<IMemoryTest> memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700350 sp<IFetcher> fetcher;
Yifan Hong1dc87932016-08-19 09:51:01 -0700351 sp<IFoo> foo;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800352 sp<IBaz> dyingBaz;
Yifan Hong1dc87932016-08-19 09:51:01 -0700353 sp<IBar> bar;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700354 sp<IGraph> graphInterface;
355 sp<IPointer> pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700356 sp<IPointer> validationPointerInterface;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800357 TestMode mode;
Hridya Valsarajud7899cd2017-03-30 16:36:48 -0700358 bool enableDelayMeasurementTests;
359 HidlEnvironment(TestMode mode, bool enableDelayMeasurementTests) :
360 mode(mode), enableDelayMeasurementTests(enableDelayMeasurementTests) {};
Yifan Hongccd782b2016-11-28 09:41:46 -0800361
Yifan Hongca890522016-10-12 10:03:41 -0700362 void getServices() {
Yifan Hong8b98d8d2017-02-24 17:06:06 -0800363 manager = IServiceManager::getService();
Steven Moreland01bcb772016-11-08 15:57:25 -0800364
365 // alternatively:
366 // manager = defaultServiceManager()
367
368 ASSERT_NE(manager, nullptr);
369 ASSERT_TRUE(manager->isRemote()); // manager is always remote
370
Yifan Hong8b98d8d2017-02-24 17:06:06 -0800371 tokenManager = ITokenManager::getService();
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800372 ASSERT_NE(tokenManager, nullptr);
373 ASSERT_TRUE(tokenManager->isRemote()); // tokenManager is always remote
374
Steven Moreland2a753532016-12-15 12:37:49 -0800375 ashmemAllocator = IAllocator::getService("ashmem");
376 ASSERT_NE(ashmemAllocator, nullptr);
377 ASSERT_TRUE(ashmemAllocator->isRemote()); // allocator is always remote
378
Yifan Hongc70f0d82016-10-10 14:50:22 -0700379 // getStub is true if we are in passthrough mode to skip checking
380 // binderized server, false for binderized mode.
Yifan Hong1e81c532016-10-18 18:43:46 -0700381
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800382 memoryTest = IMemoryTest::getService("memory", mode == PASSTHROUGH /* getStub */);
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100383 ASSERT_NE(memoryTest, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800384 ASSERT_EQ(memoryTest->isRemote(), mode == BINDERIZED);
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100385
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800386 fetcher = IFetcher::getService("fetcher", mode == PASSTHROUGH /* getStub */);
Yifan Hong1e81c532016-10-18 18:43:46 -0700387 ASSERT_NE(fetcher, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800388 ASSERT_EQ(fetcher->isRemote(), mode == BINDERIZED);
Yifan Hong1e81c532016-10-18 18:43:46 -0700389
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800390 foo = IFoo::getService("foo", mode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700391 ASSERT_NE(foo, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800392 ASSERT_EQ(foo->isRemote(), mode == BINDERIZED);
Yifan Hong1dc87932016-08-19 09:51:01 -0700393
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800394 dyingBaz = IBaz::getService("dyingBaz", mode == PASSTHROUGH /* getStub */);
Martijn Coenen115d4282016-12-19 05:14:04 +0100395 ASSERT_NE(foo, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800396 ASSERT_EQ(foo->isRemote(), mode == BINDERIZED);
Martijn Coenen115d4282016-12-19 05:14:04 +0100397
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800398 bar = IBar::getService("foo", mode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700399 ASSERT_NE(bar, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800400 ASSERT_EQ(bar->isRemote(), mode == BINDERIZED);
Yifan Hong1dc87932016-08-19 09:51:01 -0700401
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800402 graphInterface = IGraph::getService("graph", mode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700403 ASSERT_NE(graphInterface, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800404 ASSERT_EQ(graphInterface->isRemote(), mode == BINDERIZED);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700405
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800406 pointerInterface = IPointer::getService("pointer", mode == PASSTHROUGH /* getStub */);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700407 ASSERT_NE(pointerInterface, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800408 ASSERT_EQ(pointerInterface->isRemote(), mode == BINDERIZED);
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700409
410 // use passthrough mode as the validation object.
411 validationPointerInterface = IPointer::getService("pointer", true /* getStub */);
412 ASSERT_NE(validationPointerInterface, nullptr);
Yifan Hong1dc87932016-08-19 09:51:01 -0700413 }
Yifan Hong1e81c532016-10-18 18:43:46 -0700414
Yifan Hongca890522016-10-12 10:03:41 -0700415 virtual void SetUp() {
416 ALOGI("Environment setup beginning...");
Yifan Hongca890522016-10-12 10:03:41 -0700417 getServices();
Yifan Hong1dc87932016-08-19 09:51:01 -0700418 ALOGI("Environment setup complete.");
419 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700420};
421
Yifan Hongca890522016-10-12 10:03:41 -0700422class HidlTest : public ::testing::Test {
423public:
Steven Moreland01bcb772016-11-08 15:57:25 -0800424 sp<IServiceManager> manager;
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800425 sp<ITokenManager> tokenManager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100426 sp<IAllocator> ashmemAllocator;
427 sp<IMemoryTest> memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700428 sp<IFetcher> fetcher;
Yifan Hongca890522016-10-12 10:03:41 -0700429 sp<IFoo> foo;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800430 sp<IBaz> dyingBaz;
Yifan Hongca890522016-10-12 10:03:41 -0700431 sp<IBar> bar;
Yifan Hongca890522016-10-12 10:03:41 -0700432 sp<IGraph> graphInterface;
433 sp<IPointer> pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700434 sp<IPointer> validationPointerInterface;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800435 TestMode mode = TestMode::PASSTHROUGH;
Martijn Coenen115d4282016-12-19 05:14:04 +0100436
Yifan Hongca890522016-10-12 10:03:41 -0700437 virtual void SetUp() override {
438 ALOGI("Test setup beginning...");
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800439 manager = gHidlEnvironment->manager;
440 tokenManager = gHidlEnvironment->tokenManager;
441 ashmemAllocator = gHidlEnvironment->ashmemAllocator;
442 memoryTest = gHidlEnvironment->memoryTest;
443 fetcher = gHidlEnvironment->fetcher;
444 foo = gHidlEnvironment->foo;
445 dyingBaz = gHidlEnvironment->dyingBaz;
446 bar = gHidlEnvironment->bar;
447 graphInterface = gHidlEnvironment->graphInterface;
448 pointerInterface = gHidlEnvironment->pointerInterface;
449 validationPointerInterface = gHidlEnvironment->validationPointerInterface;
450 mode = gHidlEnvironment->mode;
Yifan Hongca890522016-10-12 10:03:41 -0700451 ALOGI("Test setup complete");
452 }
453};
454
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800455TEST_F(HidlTest, ToStringTest) {
456 using namespace android::hardware;
457
458 LOG(INFO) << toString(IFoo::Everything{});
459
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100460 // Note that handles don't need to be deleted because MQDescriptor takes ownership
461 // and deletes them when destructed.
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800462 auto handle = native_handle_create(0, 1);
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100463 auto handle2 = native_handle_create(0, 1);
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800464 handle->data[0] = 5;
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100465 handle2->data[0] = 6;
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800466 IFoo::Everything e {
467 .u = {.p = reinterpret_cast<void *>(0x5)},
468 .number = 10,
469 .h = handle,
470 .descSync = {std::vector<GrantorDescriptor>(), handle, 5},
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100471 .descUnsync = {std::vector<GrantorDescriptor>(), handle2, 6},
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800472 .mem = hidl_memory("mymem", handle, 5),
473 .p = reinterpret_cast<void *>(0x6),
474 .vs = {"hello", "world"},
475 .multidimArray = hidl_vec<hidl_string>{"hello", "great", "awesome", "nice"}.data(),
476 .sArray = hidl_vec<hidl_string>{"awesome", "thanks", "you're welcome"}.data(),
477 .anotherStruct = {.first = "first", .last = "last"},
478 .bf = IFoo::BitField::V0 | IFoo::BitField::V2
479 };
480 LOG(INFO) << toString(e);
481 LOG(INFO) << toString(foo);
482 // toString is for debugging purposes only; no good EXPECT
483 // statement can be written here.
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800484}
485
Steven Morelandf1a00ac2017-04-06 17:22:56 -0700486TEST_F(HidlTest, PassthroughLookupTest) {
487 // IFoo is special because it returns an interface no matter
488 // what instance name is requested. In general, this is BAD!
489 EXPECT_NE(nullptr, IFoo::getService("", true /* getStub */).get());
490 EXPECT_NE(nullptr, IFoo::getService("a", true /* getStub */).get());
491 EXPECT_NE(nullptr, IFoo::getService("asdf", true /* getStub */).get());
492 EXPECT_NE(nullptr, IFoo::getService("::::::::", true /* getStub */).get());
493 EXPECT_NE(nullptr, IFoo::getService("/////", true /* getStub */).get());
494 EXPECT_NE(nullptr, IFoo::getService("\n", true /* getStub */).get());
495}
496
Yifan Hong870d1a72017-02-22 14:24:17 -0800497TEST_F(HidlTest, EnumToStringTest) {
498 using namespace std::string_literals;
499 using ::android::hardware::tests::foo::V1_0::toString;
500 // toString for enum
501 EXPECT_EQ(toString(IFoo::BitField::V0), "V0"s);
502 EXPECT_EQ(toString(static_cast<IFoo::BitField>(0)), "0"s)
503 << "Invalid enum isn't stringified correctly.";
504 EXPECT_EQ(toString(static_cast<IFoo::BitField>(IFoo::BitField::V0 | IFoo::BitField::V2)), "0x5"s)
505 << "Invalid enum isn't stringified correctly.";
506 // dump bitfields
507 EXPECT_EQ(toString<IFoo::BitField>(0 | IFoo::BitField::V0), "V0 (0x1)"s);
508 EXPECT_EQ(toString<IFoo::BitField>(0 | IFoo::BitField::V0 | IFoo::BitField::V2), "V0 | V2 (0x5)"s);
509 EXPECT_EQ(toString<IFoo::BitField>(0xF), "V0 | V1 | V2 | V3 | VALL (0xf)"s);
510 EXPECT_EQ(toString<IFoo::BitField>(0xFF), "V0 | V1 | V2 | V3 | VALL | 0xf0 (0xff)"s);
511}
512
Steven Moreland424a9482017-02-13 19:20:40 -0800513TEST_F(HidlTest, PingTest) {
514 EXPECT_OK(manager->ping());
515}
516
Steven Moreland038903b2017-03-30 12:11:24 -0700517TEST_F(HidlTest, TryGetServiceTest) {
518 sp<IServiceManager> dne = IServiceManager::tryGetService("boss");
519 ASSERT_EQ(dne, nullptr);
520
521 sp<IServiceManager> manager = IServiceManager::tryGetService();
522 ASSERT_NE(manager, nullptr);
523}
524
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700525TEST_F(HidlTest, HashTest) {
526 uint8_t ihash[32] = {74,38,204,105,102,117,11,15,207,7,238,198,29,35,30,62,100,
527 216,131,182,3,61,162,241,215,211,6,20,251,143,125,161};
528 auto service = IHash::getService(mode == PASSTHROUGH /* getStub */);
529 EXPECT_OK(service->getHashChain([&] (const auto &chain) {
530 EXPECT_EQ(chain[0].size(), 32u);
531 EXPECT_ARRAYEQ(ihash, chain[0], 32);
532 EXPECT_OK(manager->getHashChain([&] (const auto &managerChain) {
533 EXPECT_EQ(chain[chain.size() - 1].size(), managerChain[managerChain.size() - 1].size());
534 EXPECT_ARRAYEQ(chain[chain.size() - 1], managerChain[managerChain.size() - 1],
535 chain[chain.size() - 1].size()) << "Hash for IBase doesn't match!";
536 }));
537 }));
538}
539
Steven Moreland01bcb772016-11-08 15:57:25 -0800540TEST_F(HidlTest, ServiceListTest) {
541 static const std::set<std::string> binderizedSet = {
542 "android.hardware.tests.pointer@1.0::IPointer/pointer",
543 "android.hardware.tests.bar@1.0::IBar/foo",
544 "android.hardware.tests.inheritance@1.0::IFetcher/fetcher",
Steven Moreland01bcb772016-11-08 15:57:25 -0800545 "android.hardware.tests.inheritance@1.0::IParent/parent",
546 "android.hardware.tests.inheritance@1.0::IParent/child",
547 "android.hardware.tests.inheritance@1.0::IChild/child",
548 "android.hardware.tests.pointer@1.0::IGraph/graph",
549 "android.hardware.tests.inheritance@1.0::IGrandparent/child",
550 "android.hardware.tests.foo@1.0::IFoo/foo",
Martijn Coenenc20e35b2017-03-02 14:59:41 +0100551 "android.hidl.manager@1.0::IServiceManager/default",
Steven Moreland01bcb772016-11-08 15:57:25 -0800552 };
553
554 static const std::set<std::string> passthroughSet = {
Martijn Coenenc20e35b2017-03-02 14:59:41 +0100555 "android.hidl.manager@1.0::IServiceManager/default"
Steven Moreland01bcb772016-11-08 15:57:25 -0800556 };
557
558 std::set<std::string> activeSet;
559
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800560 switch(mode) {
Steven Moreland01bcb772016-11-08 15:57:25 -0800561 case BINDERIZED: {
562 activeSet = binderizedSet;
563 } break;
564
565 case PASSTHROUGH: {
566 activeSet = passthroughSet;
567 } break;
568 default:
569 EXPECT_TRUE(false) << "unrecognized mode";
570 }
571
572 EXPECT_OK(manager->list([&activeSet](const hidl_vec<hidl_string> &registered){
573 std::set<std::string> registeredSet;
574
575 for (size_t i = 0; i < registered.size(); i++) {
576 registeredSet.insert(registered[i]);
577 }
578
579 std::set<std::string> difference;
580 std::set_difference(activeSet.begin(), activeSet.end(),
581 registeredSet.begin(), registeredSet.end(),
582 std::inserter(difference, difference.begin()));
583
584 EXPECT_EQ(difference.size(), 0u) << "service(s) not registered " << to_string(difference);
585 }));
586}
587
Steven Moreland72d94882017-03-14 02:30:15 -0700588// passthrough TODO(b/31959402)
Steven Moreland01bcb772016-11-08 15:57:25 -0800589TEST_F(HidlTest, ServiceListByInterfaceTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800590 if (mode == BINDERIZED) {
Steven Morelandd39133b2016-11-11 12:30:08 -0800591 EXPECT_OK(manager->listByInterface(IParent::descriptor,
Steven Moreland01bcb772016-11-08 15:57:25 -0800592 [](const hidl_vec<hidl_string> &registered) {
593 std::set<std::string> registeredSet;
594
595 for (size_t i = 0; i < registered.size(); i++) {
596 registeredSet.insert(registered[i]);
597 }
598
599 std::set<std::string> activeSet = {
600 "parent", "child"
601 };
602 std::set<std::string> difference;
603 std::set_difference(activeSet.begin(), activeSet.end(),
604 registeredSet.begin(), registeredSet.end(),
605 std::inserter(difference, difference.begin()));
606
607 EXPECT_EQ(difference.size(), 0u) << "service(s) not registered " << to_string(difference);
608 }));
609 }
610}
611
Steven Moreland72d94882017-03-14 02:30:15 -0700612// passthrough TODO(b/31959402)
Steven Moreland01bcb772016-11-08 15:57:25 -0800613TEST_F(HidlTest, ServiceParentTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800614 if (mode == BINDERIZED) {
Steven Moreland01bcb772016-11-08 15:57:25 -0800615 sp<IParent> parent = IParent::getService("child");
616
617 EXPECT_NE(parent, nullptr);
618 }
619}
620
Steven Moreland72d94882017-03-14 02:30:15 -0700621// passthrough TODO(b/31959402)
Steven Moreland0693f312016-11-09 15:06:14 -0800622TEST_F(HidlTest, ServiceNotificationTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800623 if (mode == BINDERIZED) {
Steven Moreland0693f312016-11-09 15:06:14 -0800624 ServiceNotification *notification = new ServiceNotification();
625
626 std::string instanceName = "test-instance";
Steven Moreland21aa43c2017-03-07 17:15:24 -0800627 EXPECT_TRUE(IParent::registerForNotifications(instanceName, notification));
Steven Moreland0693f312016-11-09 15:06:14 -0800628
Steven Moreland21aa43c2017-03-07 17:15:24 -0800629 EXPECT_EQ(::android::OK, (new SimpleChild())->registerAsService(instanceName));
630 EXPECT_EQ(::android::OK, (new SimpleParent())->registerAsService(instanceName));
Steven Moreland0693f312016-11-09 15:06:14 -0800631
632 std::unique_lock<std::mutex> lock(notification->mutex);
633
634 notification->condition.wait_for(
635 lock,
636 std::chrono::milliseconds(2),
637 [&notification]() {
Steven Moreland21aa43c2017-03-07 17:15:24 -0800638 return notification->getRegistrations().size() >= 2;
Steven Moreland0693f312016-11-09 15:06:14 -0800639 });
640
641 std::vector<std::string> registrations = notification->getRegistrations();
642
Steven Moreland21aa43c2017-03-07 17:15:24 -0800643 EXPECT_EQ(registrations.size(), 2u);
Steven Moreland0693f312016-11-09 15:06:14 -0800644
645 EXPECT_EQ(to_string(registrations.data(), registrations.size()),
Steven Moreland21aa43c2017-03-07 17:15:24 -0800646 std::string("['") + IParent::descriptor + "/" + instanceName +
647 "', '" + IParent::descriptor + "/" + instanceName + "']");
Steven Moreland0693f312016-11-09 15:06:14 -0800648 }
649}
650
Steven Moreland72d94882017-03-14 02:30:15 -0700651// passthrough TODO(b/31959402)
Steven Moreland0693f312016-11-09 15:06:14 -0800652TEST_F(HidlTest, ServiceAllNotificationTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800653 if (mode == BINDERIZED) {
Steven Moreland0693f312016-11-09 15:06:14 -0800654 ServiceNotification *notification = new ServiceNotification();
655
656 std::string instanceOne = "test-instance-one";
657 std::string instanceTwo = "test-instance-two";
658 EXPECT_TRUE(ISimple::registerForNotifications("", notification));
659
Steven Moreland0693f312016-11-09 15:06:14 -0800660 Simple* instanceA = new Simple(1);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800661 EXPECT_EQ(::android::OK, instanceA->registerAsService(instanceOne));
Steven Moreland0693f312016-11-09 15:06:14 -0800662 Simple* instanceB = new Simple(2);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800663 EXPECT_EQ(::android::OK, instanceB->registerAsService(instanceTwo));
Steven Moreland0693f312016-11-09 15:06:14 -0800664
665 std::unique_lock<std::mutex> lock(notification->mutex);
666
667 notification->condition.wait_for(
668 lock,
669 std::chrono::milliseconds(2),
670 [&notification]() {
671 return notification->getRegistrations().size() >= 2;
672 });
673
674 std::vector<std::string> registrations = notification->getRegistrations();
675 std::sort(registrations.begin(), registrations.end());
676
677 EXPECT_EQ(registrations.size(), 2u);
678
Steven Morelandd39133b2016-11-11 12:30:08 -0800679 std::string descriptor = ISimple::descriptor;
Steven Moreland0693f312016-11-09 15:06:14 -0800680
681 EXPECT_EQ(to_string(registrations.data(), registrations.size()),
682 "['" + descriptor + "/" + instanceOne + "', '"
683 + descriptor + "/" + instanceTwo + "']");
684 }
685}
686
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800687TEST_F(HidlTest, TestToken) {
Steven Moreland1cefc392017-04-04 14:25:17 -0700688 Return<void> ret = tokenManager->createToken(manager, [&] (const hidl_vec<uint8_t> &token) {
689 Return<sp<IBase>> retService = tokenManager->get(token);
690 EXPECT_OK(retService);
691 if (retService.isOk()) {
692 sp<IBase> service = retService;
693 EXPECT_NE(nullptr, service.get());
694 sp<IServiceManager> retManager = IServiceManager::castFrom(service);
695
696 // TODO(b/33818800): should have only one Bp per process
697 // EXPECT_EQ(manager, retManager);
698
699 EXPECT_NE(nullptr, retManager.get());
700 }
701
702 Return<bool> unregisterRet = tokenManager->unregister(token);
703
704 EXPECT_OK(unregisterRet);
705 if (unregisterRet.isOk()) {
706 EXPECT_TRUE(unregisterRet);
707 }
708 });
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800709 EXPECT_OK(ret);
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800710}
711
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100712TEST_F(HidlTest, TestSharedMemory) {
713 const uint8_t kValue = 0xCA;
714 hidl_memory mem_copy;
715 EXPECT_OK(ashmemAllocator->allocate(1024, [&](bool success, const hidl_memory& mem) {
716 EXPECT_EQ(success, true);
717
718 sp<IMemory> memory = mapMemory(mem);
719
720 EXPECT_NE(memory, nullptr);
721
722 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
723 EXPECT_NE(data, nullptr);
724
725 EXPECT_EQ(memory->getSize(), mem.size());
726
727 memory->update();
728 memset(data, 0, memory->getSize());
729 memory->commit();
730
731 mem_copy = mem;
732 memoryTest->fillMemory(mem, kValue);
Steven Morelandfc060162017-03-10 17:26:48 -0800733
734 memory->read();
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100735 for (size_t i = 0; i < mem.size(); i++) {
736 EXPECT_EQ(kValue, data[i]);
737 }
Steven Morelandfc060162017-03-10 17:26:48 -0800738 memory->commit();
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100739 }));
740
741 // Test the memory persists after the call
742 sp<IMemory> memory = mapMemory(mem_copy);
743
744 EXPECT_NE(memory, nullptr);
745
746 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
747 EXPECT_NE(data, nullptr);
748
Steven Morelandfc060162017-03-10 17:26:48 -0800749 memory->read();
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100750 for (size_t i = 0; i < mem_copy.size(); i++) {
751 EXPECT_EQ(kValue, data[i]);
752 }
Steven Morelandfc060162017-03-10 17:26:48 -0800753 memory->commit();
Hridya Valsaraju8951e552017-02-27 12:37:20 -0800754
755 hidl_memory mem_move(std::move(mem_copy));
756 ASSERT_EQ(nullptr, mem_copy.handle());
757 ASSERT_EQ(0UL, mem_copy.size());
758 ASSERT_EQ("", mem_copy.name());
759
760 memory.clear();
761 memory = mapMemory(mem_move);
762
763 EXPECT_NE(memory, nullptr);
764
765 data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
766 EXPECT_NE(data, nullptr);
767
Steven Morelandfc060162017-03-10 17:26:48 -0800768 memory->read();
Hridya Valsaraju8951e552017-02-27 12:37:20 -0800769 for (size_t i = 0; i < mem_move.size(); i++) {
770 EXPECT_EQ(kValue, data[i]);
771 }
Steven Morelandfc060162017-03-10 17:26:48 -0800772 memory->commit();
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100773}
774
Steven Morelandf31f1652017-01-06 18:33:42 -0800775TEST_F(HidlTest, NullSharedMemory) {
776 hidl_memory memory{};
777
778 EXPECT_EQ(nullptr, memory.handle());
779
780 EXPECT_OK(memoryTest->haveSomeMemory(memory, [&](const hidl_memory &mem) {
781 EXPECT_EQ(nullptr, mem.handle());
782 }));
783}
784
Yifan Hongc75fd472017-01-11 12:37:31 -0800785TEST_F(HidlTest, FooGetDescriptorTest) {
786 EXPECT_OK(foo->interfaceDescriptor([&] (const auto &desc) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800787 EXPECT_EQ(desc, mode == BINDERIZED
Yifan Hongc75fd472017-01-11 12:37:31 -0800788 ? IBar::descriptor // service is actually IBar in binderized mode
789 : IFoo::descriptor); // dlopened, so service is IFoo
790 }));
791}
792
Yifan Hong1dc87932016-08-19 09:51:01 -0700793TEST_F(HidlTest, FooDoThisTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700794 ALOGI("CLIENT call doThis.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700795 EXPECT_OK(foo->doThis(1.0f));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700796 ALOGI("CLIENT doThis returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700797}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700798
Yifan Hong1dc87932016-08-19 09:51:01 -0700799TEST_F(HidlTest, FooDoThatAndReturnSomethingTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700800 ALOGI("CLIENT call doThatAndReturnSomething.");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000801 int32_t result = foo->doThatAndReturnSomething(2.0f);
802 ALOGI("CLIENT doThatAndReturnSomething returned %d.", result);
803 EXPECT_EQ(result, 666);
Yifan Hong1dc87932016-08-19 09:51:01 -0700804}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700805
Yifan Hong1dc87932016-08-19 09:51:01 -0700806TEST_F(HidlTest, FooDoQuiteABitTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700807 ALOGI("CLIENT call doQuiteABit");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000808 double something = foo->doQuiteABit(1, 2, 3.0f, 4.0);
809 ALOGI("CLIENT doQuiteABit returned %f.", something);
810 EXPECT_DOUBLE_EQ(something, 666.5);
Yifan Hong1dc87932016-08-19 09:51:01 -0700811}
812
813TEST_F(HidlTest, FooDoSomethingElseTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700814
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700815 ALOGI("CLIENT call doSomethingElse");
Andreas Huberf03332a2016-09-22 15:35:43 -0700816 hidl_array<int32_t, 15> param;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700817 for (size_t i = 0; i < sizeof(param) / sizeof(param[0]); ++i) {
818 param[i] = i;
819 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700820 EXPECT_OK(foo->doSomethingElse(param, [&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700821 ALOGI("CLIENT doSomethingElse returned %s.",
Andreas Huber5e44a292016-09-27 14:52:39 -0700822 to_string(something).c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700823 int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
824 26, 28, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2};
Steven Morelande70455b2016-09-14 15:46:36 -0700825 EXPECT_TRUE(isArrayEqual(something, expect, 32));
Yifan Hong1dc87932016-08-19 09:51:01 -0700826 }));
827}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700828
Yifan Hong1dc87932016-08-19 09:51:01 -0700829TEST_F(HidlTest, FooDoStuffAndReturnAStringTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700830 ALOGI("CLIENT call doStuffAndReturnAString");
Yifan Hong1dc87932016-08-19 09:51:01 -0700831 EXPECT_OK(foo->doStuffAndReturnAString([&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700832 ALOGI("CLIENT doStuffAndReturnAString returned '%s'.",
Andreas Huber9cd48d02016-08-03 14:25:59 -0700833 something.c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700834 EXPECT_STREQ(something.c_str(), "Hello, world");
Steven Moreland6e3f9f42017-01-30 11:41:49 -0800835 EXPECT_EQ(strlen("Hello, world"), something.size());
Yifan Hong1dc87932016-08-19 09:51:01 -0700836 }));
837}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700838
Yifan Hong1dc87932016-08-19 09:51:01 -0700839TEST_F(HidlTest, FooMapThisVectorTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700840 hidl_vec<int32_t> vecParam;
841 vecParam.resize(10);
842 for (size_t i = 0; i < 10; ++i) {
843 vecParam[i] = i;
844 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700845 EXPECT_OK(foo->mapThisVector(vecParam, [&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700846 ALOGI("CLIENT mapThisVector returned %s.",
Andreas Huber5e44a292016-09-27 14:52:39 -0700847 to_string(something).c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700848 int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
Steven Morelande70455b2016-09-14 15:46:36 -0700849 EXPECT_TRUE(isArrayEqual(something, expect, something.size()));
Yifan Hong1dc87932016-08-19 09:51:01 -0700850 }));
851}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700852
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800853TEST_F(HidlTest, WrapTest) {
Hridya Valsarajud7899cd2017-03-30 16:36:48 -0700854 if (!gHidlEnvironment->enableDelayMeasurementTests) {
855 return;
856 }
857
Yifan Hong01e7cde2017-01-09 17:45:45 -0800858 using ::android::hardware::tests::foo::V1_0::BnHwSimple;
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800859 using ::android::hardware::tests::foo::V1_0::BsSimple;
Yifan Hong01e7cde2017-01-09 17:45:45 -0800860 using ::android::hardware::tests::foo::V1_0::BpHwSimple;
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000861 using ::android::hardware::details::HidlInstrumentor;
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800862 nsecs_t now;
863 int i = 0;
864
865 now = systemTime();
Yifan Hong01e7cde2017-01-09 17:45:45 -0800866 new BnHwSimple(new Simple(1));
867 EXPECT_LT(systemTime() - now, 2000000) << " for BnHwSimple(nonnull)";
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800868
869 now = systemTime();
Yifan Hong01e7cde2017-01-09 17:45:45 -0800870 new BnHwSimple(nullptr);
871 EXPECT_LT(systemTime() - now, 2000000) << " for BnHwSimple(null)";
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800872
873 now = systemTime();
874 new BsSimple(new Simple(1));
875 EXPECT_LT(systemTime() - now, 2000000) << " for BsSimple(nonnull)";
876
877 now = systemTime();
878 new BsSimple(nullptr);
879 EXPECT_LT(systemTime() - now, 2000000) << " for BsSimple(null)";
880
881 now = systemTime();
Yifan Hong01e7cde2017-01-09 17:45:45 -0800882 new BpHwSimple(nullptr);
883 EXPECT_LT(systemTime() - now, 2000000) << " for BpHwSimple(null)";
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800884
885 now = systemTime();
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000886 new ::android::hardware::details::HidlInstrumentor("", "");
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800887 EXPECT_LT(systemTime() - now, 2000000) << " for HidlInstrumentor";
888
889 now = systemTime();
890 i++;
891 EXPECT_LT(systemTime() - now, 1000) << " for nothing";
892}
893
Yifan Hong1dc87932016-08-19 09:51:01 -0700894TEST_F(HidlTest, FooCallMeTest) {
Hridya Valsarajud7899cd2017-03-30 16:36:48 -0700895 if (!gHidlEnvironment->enableDelayMeasurementTests) {
896 return;
897 }
Yifan Hong11992a62016-11-09 18:07:40 -0800898 sp<IFooCallback> fooCb = new FooCallback();
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700899 ALOGI("CLIENT call callMe.");
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700900 // callMe is oneway, should return instantly.
901 nsecs_t now;
902 now = systemTime();
903 EXPECT_OK(foo->callMe(fooCb));
Yifan Hong3eac8a32016-10-11 10:02:59 -0700904 EXPECT_LT(systemTime() - now, ONEWAY_TOLERANCE_NS);
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700905 ALOGI("CLIENT callMe returned.");
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700906
907 // Bar::callMe will invoke three methods on FooCallback; one will return
908 // right away (even though it is a two-way method); the second one will
Yifan Hong3eac8a32016-10-11 10:02:59 -0700909 // block Bar for DELAY_S seconds, and the third one will return
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700910 // to Bar right away (is oneway) but will itself block for DELAY_S seconds.
911 // We need a way to make sure that these three things have happened within
912 // 2*DELAY_S seconds plus some small tolerance.
913 //
914 // Method FooCallback::reportResults() takes a timeout parameter. It blocks for
915 // that length of time, while waiting for the three methods above to
916 // complete. It returns the information of whether each method was invoked,
917 // as well as how long the body of the method took to execute. We verify
918 // the information returned by reportResults() against the timeout we pass (which
919 // is long enough for the method bodies to execute, plus tolerance), and
920 // verify that eachof them executed, as expected, and took the length of
921 // time to execute that we also expect.
922
Yifan Hongfcf94e42016-12-07 15:49:51 -0800923 const nsecs_t waitNs =
924 3 * DELAY_NS + TOLERANCE_NS;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700925 const nsecs_t reportResultsNs =
Yifan Hong3eac8a32016-10-11 10:02:59 -0700926 2 * DELAY_NS + TOLERANCE_NS;
Andreas Huber03866f52016-08-30 14:19:52 -0700927
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700928 ALOGI("CLIENT: Waiting for up to %" PRId64 " seconds.",
Yifan Hongfcf94e42016-12-07 15:49:51 -0800929 nanoseconds_to_seconds(waitNs));
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700930
Yifan Hongfcf94e42016-12-07 15:49:51 -0800931 fooCb->reportResults(waitNs,
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700932 [&](int64_t timeLeftNs,
Andreas Huberf03332a2016-09-22 15:35:43 -0700933 const hidl_array<IFooCallback::InvokeInfo, 3> &invokeResults) {
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700934 ALOGI("CLIENT: FooCallback::reportResults() is returning data.");
935 ALOGI("CLIENT: Waited for %" PRId64 " milliseconds.",
Yifan Hongfcf94e42016-12-07 15:49:51 -0800936 nanoseconds_to_milliseconds(waitNs - timeLeftNs));
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700937
Yifan Hongfcf94e42016-12-07 15:49:51 -0800938 EXPECT_LE(waitNs - timeLeftNs, reportResultsNs)
939 << "waited for "
940 << (timeLeftNs >= 0 ? "" : "more than ")
941 << (timeLeftNs >= 0 ? (waitNs - timeLeftNs) : waitNs)
942 << "ns, expect to finish in "
943 << reportResultsNs << " ns";
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700944
945 // two-way method, was supposed to return right away
946 EXPECT_TRUE(invokeResults[0].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700947 EXPECT_LE(invokeResults[0].timeNs, invokeResults[0].callerBlockedNs);
948 EXPECT_LE(invokeResults[0].callerBlockedNs, TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700949 // two-way method, was supposed to block caller for DELAY_NS
950 EXPECT_TRUE(invokeResults[1].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700951 EXPECT_LE(invokeResults[1].timeNs, invokeResults[1].callerBlockedNs);
952 EXPECT_LE(invokeResults[1].callerBlockedNs,
953 DELAY_NS + TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700954 // one-way method, do not block caller, but body was supposed to block for DELAY_NS
955 EXPECT_TRUE(invokeResults[2].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700956 EXPECT_LE(invokeResults[2].callerBlockedNs, ONEWAY_TOLERANCE_NS);
957 EXPECT_LE(invokeResults[2].timeNs, DELAY_NS + TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700958 });
959}
960
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000961
962
Yifan Hong1dc87932016-08-19 09:51:01 -0700963TEST_F(HidlTest, FooUseAnEnumTest) {
964 ALOGI("CLIENT call useAnEnum.");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000965 IFoo::SomeEnum sleepy = foo->useAnEnum(IFoo::SomeEnum::quux);
966 ALOGI("CLIENT useAnEnum returned %u", (unsigned)sleepy);
967 EXPECT_EQ(sleepy, IFoo::SomeEnum::goober);
Yifan Hong1dc87932016-08-19 09:51:01 -0700968}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700969
Yifan Hong1dc87932016-08-19 09:51:01 -0700970TEST_F(HidlTest, FooHaveAGooberTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700971 hidl_vec<IFoo::Goober> gooberVecParam;
972 gooberVecParam.resize(2);
973 gooberVecParam[0].name = "Hello";
974 gooberVecParam[1].name = "World";
975
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700976 ALOGI("CLIENT call haveAGooberVec.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700977 EXPECT_OK(foo->haveAGooberVec(gooberVecParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700978 ALOGI("CLIENT haveAGooberVec returned.");
Andreas Huber9cd48d02016-08-03 14:25:59 -0700979
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700980 ALOGI("CLIENT call haveaGoober.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700981 EXPECT_OK(foo->haveAGoober(gooberVecParam[0]));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700982 ALOGI("CLIENT haveaGoober returned.");
983
984 ALOGI("CLIENT call haveAGooberArray.");
Andreas Huberf03332a2016-09-22 15:35:43 -0700985 hidl_array<IFoo::Goober, 20> gooberArrayParam;
Yifan Hong1dc87932016-08-19 09:51:01 -0700986 EXPECT_OK(foo->haveAGooberArray(gooberArrayParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700987 ALOGI("CLIENT haveAGooberArray returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700988}
Steven Moreland88ca4512016-08-11 11:24:10 -0700989
Yifan Hong1dc87932016-08-19 09:51:01 -0700990TEST_F(HidlTest, FooHaveATypeFromAnotherFileTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700991 ALOGI("CLIENT call haveATypeFromAnotherFile.");
Steven Moreland88ca4512016-08-11 11:24:10 -0700992 Abc abcParam{};
993 abcParam.x = "alphabet";
994 abcParam.y = 3.14f;
Yifan Honga65bb2c2016-10-27 13:17:14 -0700995 native_handle_t *handle = native_handle_create(0, 0);
996 abcParam.z = handle;
Yifan Hong1dc87932016-08-19 09:51:01 -0700997 EXPECT_OK(foo->haveATypeFromAnotherFile(abcParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700998 ALOGI("CLIENT haveATypeFromAnotherFile returned.");
Yifan Honga65bb2c2016-10-27 13:17:14 -0700999 native_handle_delete(handle);
Steven Moreland88ca4512016-08-11 11:24:10 -07001000 abcParam.z = NULL;
Yifan Hong1dc87932016-08-19 09:51:01 -07001001}
Andreas Huber9cd48d02016-08-03 14:25:59 -07001002
Yifan Hong1dc87932016-08-19 09:51:01 -07001003TEST_F(HidlTest, FooHaveSomeStringsTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001004 ALOGI("CLIENT call haveSomeStrings.");
Andreas Huberf03332a2016-09-22 15:35:43 -07001005 hidl_array<hidl_string, 3> stringArrayParam;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001006 stringArrayParam[0] = "What";
1007 stringArrayParam[1] = "a";
1008 stringArrayParam[2] = "disaster";
Steven Moreland67f67b42016-09-29 08:59:02 -07001009 EXPECT_OK(foo->haveSomeStrings(
1010 stringArrayParam,
1011 [&](const auto &out) {
1012 ALOGI("CLIENT haveSomeStrings returned %s.",
1013 to_string(out).c_str());
1014
1015 EXPECT_EQ(to_string(out), "['Hello', 'World']");
1016 }));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001017 ALOGI("CLIENT haveSomeStrings returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -07001018}
Andreas Huber9cd48d02016-08-03 14:25:59 -07001019
Yifan Hong1dc87932016-08-19 09:51:01 -07001020TEST_F(HidlTest, FooHaveAStringVecTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001021 ALOGI("CLIENT call haveAStringVec.");
Andreas Huber9cd48d02016-08-03 14:25:59 -07001022 hidl_vec<hidl_string> stringVecParam;
1023 stringVecParam.resize(3);
1024 stringVecParam[0] = "What";
1025 stringVecParam[1] = "a";
1026 stringVecParam[2] = "disaster";
Steven Moreland67f67b42016-09-29 08:59:02 -07001027 EXPECT_OK(foo->haveAStringVec(
1028 stringVecParam,
1029 [&](const auto &out) {
1030 ALOGI("CLIENT haveAStringVec returned %s.",
1031 to_string(out).c_str());
1032
1033 EXPECT_EQ(to_string(out), "['Hello', 'World']");
1034 }));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001035 ALOGI("CLIENT haveAStringVec returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -07001036}
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001037
Andreas Huberf9d49f12016-09-12 14:58:36 -07001038TEST_F(HidlTest, FooTransposeMeTest) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001039 hidl_array<float, 3, 5> in;
Andreas Huberf9d49f12016-09-12 14:58:36 -07001040 float k = 1.0f;
1041 for (size_t i = 0; i < 3; ++i) {
1042 for (size_t j = 0; j < 5; ++j, ++k) {
1043 in[i][j] = k;
1044 }
1045 }
1046
Andreas Huberf03332a2016-09-22 15:35:43 -07001047 ALOGI("CLIENT call transposeMe(%s).", to_string(in).c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001048
1049 EXPECT_OK(foo->transposeMe(
Andreas Huberf03332a2016-09-22 15:35:43 -07001050 in,
Andreas Huberf9d49f12016-09-12 14:58:36 -07001051 [&](const auto &out) {
1052 ALOGI("CLIENT transposeMe returned %s.",
Andreas Huberf03332a2016-09-22 15:35:43 -07001053 to_string(out).c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001054
1055 for (size_t i = 0; i < 3; ++i) {
1056 for (size_t j = 0; j < 5; ++j) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001057 EXPECT_EQ(out[j][i], in[i][j]);
Andreas Huberf9d49f12016-09-12 14:58:36 -07001058 }
1059 }
1060 }));
1061}
1062
1063TEST_F(HidlTest, FooCallingDrWhoTest) {
1064 IFoo::MultiDimensional in;
1065
1066 size_t k = 0;
1067 for (size_t i = 0; i < 5; ++i) {
1068 for (size_t j = 0; j < 3; ++j, ++k) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001069 in.quuxMatrix[i][j].first = ("First " + std::to_string(k)).c_str();
1070 in.quuxMatrix[i][j].last = ("Last " + std::to_string(15-k)).c_str();
Andreas Huberf9d49f12016-09-12 14:58:36 -07001071 }
1072 }
1073
1074 ALOGI("CLIENT call callingDrWho(%s).",
1075 MultiDimensionalToString(in).c_str());
1076
1077 EXPECT_OK(foo->callingDrWho(
1078 in,
1079 [&](const auto &out) {
1080 ALOGI("CLIENT callingDrWho returned %s.",
1081 MultiDimensionalToString(out).c_str());
1082
Andreas Huber709b62d2016-09-19 11:21:18 -07001083 size_t k = 0;
Andreas Huberf9d49f12016-09-12 14:58:36 -07001084 for (size_t i = 0; i < 5; ++i) {
Andreas Huber709b62d2016-09-19 11:21:18 -07001085 for (size_t j = 0; j < 3; ++j, ++k) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001086 EXPECT_STREQ(
1087 out.quuxMatrix[i][j].first.c_str(),
1088 in.quuxMatrix[4 - i][2 - j].last.c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001089
1090 EXPECT_STREQ(
Andreas Huberf03332a2016-09-22 15:35:43 -07001091 out.quuxMatrix[i][j].last.c_str(),
1092 in.quuxMatrix[4 - i][2 - j].first.c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001093 }
1094 }
1095 }));
1096}
1097
Andreas Huber709b62d2016-09-19 11:21:18 -07001098static std::string numberToEnglish(int x) {
1099 static const char *const kDigits[] = {
1100 "zero",
1101 "one",
1102 "two",
1103 "three",
1104 "four",
1105 "five",
1106 "six",
1107 "seven",
1108 "eight",
1109 "nine",
1110 };
1111
1112 if (x < 0) {
1113 return "negative " + numberToEnglish(-x);
1114 }
1115
1116 if (x < 10) {
1117 return kDigits[x];
1118 }
1119
1120 if (x <= 15) {
1121 static const char *const kSpecialTens[] = {
1122 "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
1123 };
1124
1125 return kSpecialTens[x - 10];
1126 }
1127
1128 if (x < 20) {
1129 return std::string(kDigits[x % 10]) + "teen";
1130 }
1131
1132 if (x < 100) {
1133 static const char *const kDecades[] = {
1134 "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
1135 "eighty", "ninety",
1136 };
1137
1138 return std::string(kDecades[x / 10 - 2]) + kDigits[x % 10];
1139 }
1140
1141 return "positively huge!";
1142}
1143
1144TEST_F(HidlTest, FooTransposeTest) {
1145 IFoo::StringMatrix5x3 in;
1146
Andreas Huber709b62d2016-09-19 11:21:18 -07001147 for (int i = 0; i < 5; ++i) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001148 for (int j = 0; j < 3; ++j) {
1149 in.s[i][j] = numberToEnglish(3 * i + j + 1).c_str();
Andreas Huber709b62d2016-09-19 11:21:18 -07001150 }
1151 }
1152
1153 EXPECT_OK(foo->transpose(
1154 in,
1155 [&](const auto &out) {
1156 EXPECT_EQ(
1157 to_string(out),
1158 "[['one', 'four', 'seven', 'ten', 'thirteen'], "
1159 "['two', 'five', 'eight', 'eleven', 'fourteen'], "
1160 "['three', 'six', 'nine', 'twelve', 'fifteen']]");
1161 }));
1162}
1163
1164TEST_F(HidlTest, FooTranspose2Test) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001165 hidl_array<hidl_string, 5, 3> in;
Andreas Huber709b62d2016-09-19 11:21:18 -07001166
Andreas Huber709b62d2016-09-19 11:21:18 -07001167 for (int i = 0; i < 5; ++i) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001168 for (int j = 0; j < 3; ++j) {
1169 in[i][j] = numberToEnglish(3 * i + j + 1).c_str();
Andreas Huber709b62d2016-09-19 11:21:18 -07001170 }
1171 }
1172
1173 EXPECT_OK(foo->transpose2(
1174 in,
1175 [&](const auto &out) {
1176 EXPECT_EQ(
Andreas Huberf03332a2016-09-22 15:35:43 -07001177 to_string(out),
Andreas Huber709b62d2016-09-19 11:21:18 -07001178 "[['one', 'four', 'seven', 'ten', 'thirteen'], "
1179 "['two', 'five', 'eight', 'eleven', 'fourteen'], "
1180 "['three', 'six', 'nine', 'twelve', 'fifteen']]");
1181 }));
1182}
1183
Yifan Hongb725d672016-10-10 10:14:15 -07001184TEST_F(HidlTest, FooNullNativeHandleTest) {
Martijn Coenen9d97da22017-01-12 17:29:47 +01001185 Abc xyz;
1186 xyz.z = nullptr;
1187 EXPECT_OK(bar->expectNullHandle(nullptr, xyz, [](bool hIsNull, bool xyzHasNull) {
1188 EXPECT_TRUE(hIsNull);
1189 EXPECT_TRUE(xyzHasNull);
1190 }));
Yifan Hongb725d672016-10-10 10:14:15 -07001191}
Steven Moreland67f67b42016-09-29 08:59:02 -07001192
Steven Moreland610002f2017-06-16 13:02:49 -07001193TEST_F(HidlTest, FooNullSynchronousCallbackTest) {
1194 Return<void> ret = foo->echoNullInterface(nullptr, nullptr /* synchronous callback */);
1195
1196 EXPECT_FAIL(ret);
1197 EXPECT_TRUE(ret.description().find("Null synchronous callback passed") != std::string::npos);
1198}
1199
Martijn Coenene1638232016-10-26 12:51:34 +02001200TEST_F(HidlTest, FooNullCallbackTest) {
1201 EXPECT_OK(foo->echoNullInterface(nullptr,
1202 [](const auto receivedNull, const auto &intf) {
1203 EXPECT_TRUE(receivedNull);
1204 EXPECT_EQ(intf, nullptr);
1205 }));
1206}
1207
Steven Moreland67f67b42016-09-29 08:59:02 -07001208TEST_F(HidlTest, FooNonNullCallbackTest) {
1209 hidl_array<hidl_string, 5, 3> in;
1210
1211 EXPECT_FAIL(foo->transpose2(in, nullptr /* _hidl_cb */));
1212}
1213
Andreas Huber5e44a292016-09-27 14:52:39 -07001214TEST_F(HidlTest, FooSendVecTest) {
1215 hidl_vec<uint8_t> in;
1216 in.resize(16);
1217 for (size_t i = 0; i < in.size(); ++i) {
1218 in[i] = i;
1219 }
1220
1221 EXPECT_OK(foo->sendVec(
1222 in,
1223 [&](const auto &out) {
1224 EXPECT_EQ(to_string(in), to_string(out));
1225 }));
1226}
1227
Martijn Coenenafb30cb2017-01-13 00:24:02 +01001228TEST_F(HidlTest, FooSendEmptyVecTest) {
1229 hidl_vec<uint8_t> in;
1230 EXPECT_OK(foo->sendVec(
1231 in,
1232 [&](const auto &out) {
1233 EXPECT_EQ(out.size(), 0u);
1234 EXPECT_EQ(to_string(in), to_string(out));
1235 }));
1236}
1237
Andreas Huber86a112b2016-10-19 14:25:16 -07001238TEST_F(HidlTest, FooHaveAVectorOfInterfacesTest) {
1239 hidl_vec<sp<ISimple> > in;
1240 in.resize(16);
1241 for (size_t i = 0; i < in.size(); ++i) {
1242 in[i] = new Simple(i);
1243 }
1244
1245 EXPECT_OK(foo->haveAVectorOfInterfaces(
1246 in,
1247 [&](const auto &out) {
1248 EXPECT_EQ(in.size(), out.size());
1249 for (size_t i = 0; i < in.size(); ++i) {
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001250 int32_t inCookie = in[i]->getCookie();
1251 int32_t outCookie = out[i]->getCookie();
1252 EXPECT_EQ(inCookie, outCookie);
Andreas Huber86a112b2016-10-19 14:25:16 -07001253 }
1254 }));
1255}
1256
1257TEST_F(HidlTest, FooHaveAVectorOfGenericInterfacesTest) {
Andreas Huber86a112b2016-10-19 14:25:16 -07001258
Yifan Hongc8934042016-11-17 17:10:52 -08001259 hidl_vec<sp<::android::hidl::base::V1_0::IBase> > in;
Andreas Huber86a112b2016-10-19 14:25:16 -07001260 in.resize(16);
1261 for (size_t i = 0; i < in.size(); ++i) {
Yifan Hongc8934042016-11-17 17:10:52 -08001262 sp<ISimple> s = new Simple(i);
1263 in[i] = s;
Andreas Huber86a112b2016-10-19 14:25:16 -07001264 }
1265
1266 EXPECT_OK(foo->haveAVectorOfGenericInterfaces(
1267 in,
1268 [&](const auto &out) {
1269 EXPECT_EQ(in.size(), out.size());
Yifan Hongc8934042016-11-17 17:10:52 -08001270
Yifan Hongfbcdc802017-02-22 18:12:48 -08001271 EXPECT_OK(out[0]->interfaceDescriptor([](const auto &name) {
1272 ASSERT_STREQ(name.c_str(), ISimple::descriptor);
Yifan Hongc8934042016-11-17 17:10:52 -08001273 }));
Andreas Huber86a112b2016-10-19 14:25:16 -07001274 for (size_t i = 0; i < in.size(); ++i) {
Yifan Hongc8934042016-11-17 17:10:52 -08001275 sp<ISimple> inSimple = ISimple::castFrom(in[i]);
1276 sp<ISimple> outSimple = ISimple::castFrom(out[i]);
1277
1278 ASSERT_NE(inSimple.get(), nullptr);
1279 ASSERT_NE(outSimple.get(), nullptr);
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001280 EXPECT_EQ(in[i], inSimple.get()); // pointers must be equal!
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001281 int32_t inCookie = inSimple->getCookie();
1282 int32_t outCookie = outSimple->getCookie();
1283 EXPECT_EQ(inCookie, outCookie);
Andreas Huber86a112b2016-10-19 14:25:16 -07001284 }
1285 }));
1286}
1287
Yifan Hong8c48ad72016-10-14 11:34:59 -07001288TEST_F(HidlTest, FooStructEmbeddedHandleTest) {
1289 EXPECT_OK(foo->createMyHandle([&](const auto &myHandle) {
1290 EXPECT_EQ(myHandle.guard, 666);
Martijn Coenen2f69a5b2016-11-18 15:26:38 +01001291 const native_handle_t* handle = myHandle.h.getNativeHandle();
1292 EXPECT_EQ(handle->numInts, 10);
1293 EXPECT_EQ(handle->numFds, 0);
Yifan Hong8c48ad72016-10-14 11:34:59 -07001294 int data[] = {2,3,5,7,11,13,17,19,21,23};
Martijn Coenen2f69a5b2016-11-18 15:26:38 +01001295 EXPECT_ARRAYEQ(handle->data, data, 10);
Yifan Hong8c48ad72016-10-14 11:34:59 -07001296 }));
1297
1298 EXPECT_OK(foo->closeHandles());
1299}
1300
1301TEST_F(HidlTest, FooHandleVecTest) {
1302 EXPECT_OK(foo->createHandles(3, [&](const auto &handles) {
1303 EXPECT_EQ(handles.size(), 3ull);
1304 int data[] = {2,3,5,7,11,13,17,19,21,23};
1305 for (size_t i = 0; i < 3; i++) {
1306 const native_handle_t *h = handles[i];
1307 EXPECT_EQ(h->numInts, 10) << " for element " << i;
1308 EXPECT_EQ(h->numFds, 0) << " for element " << i;
1309 EXPECT_ARRAYEQ(h->data, data, 10);
1310 }
1311 }));
1312
1313 EXPECT_OK(foo->closeHandles());
1314}
1315
Martijn Coenen115d4282016-12-19 05:14:04 +01001316struct HidlDeathRecipient : hidl_death_recipient {
1317 std::mutex mutex;
1318 std::condition_variable condition;
1319 wp<IBase> who;
1320 bool fired = false;
1321 uint64_t cookie = 0;
1322
1323 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who) {
1324 std::unique_lock<std::mutex> lock(mutex);
1325 fired = true;
1326 this->cookie = cookie;
1327 this->who = who;
1328 condition.notify_one();
1329 };
1330};
1331
1332TEST_F(HidlTest, DeathRecipientTest) {
Martijn Coenen115d4282016-12-19 05:14:04 +01001333 sp<HidlDeathRecipient> recipient = new HidlDeathRecipient();
1334 sp<HidlDeathRecipient> recipient2 = new HidlDeathRecipient();
1335
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001336 EXPECT_TRUE(dyingBaz->linkToDeath(recipient, 0x1481));
1337 EXPECT_TRUE(dyingBaz->linkToDeath(recipient2, 0x2592));
1338 EXPECT_TRUE(dyingBaz->unlinkToDeath(recipient2));
Martijn Coenen115d4282016-12-19 05:14:04 +01001339
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001340 if (mode != BINDERIZED) {
Martijn Coenen115d4282016-12-19 05:14:04 +01001341 // Passthrough doesn't fire, nor does it keep state of
1342 // registered death recipients (so it won't fail unlinking
1343 // the same recipient twice).
1344 return;
1345 }
1346
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001347 EXPECT_FALSE(dyingBaz->unlinkToDeath(recipient2));
1348 auto ret = dyingBaz->dieNow();
1349 if (!ret.isOk()) {
1350 //do nothing, this is expected
1351 }
Martijn Coenen115d4282016-12-19 05:14:04 +01001352
1353 std::unique_lock<std::mutex> lock(recipient->mutex);
Steven Moreland7b680eb2017-04-19 16:34:26 -07001354 recipient->condition.wait_for(lock, std::chrono::milliseconds(100), [&recipient]() {
Martijn Coenen115d4282016-12-19 05:14:04 +01001355 return recipient->fired;
1356 });
1357 EXPECT_TRUE(recipient->fired);
1358 EXPECT_EQ(recipient->cookie, 0x1481u);
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001359 EXPECT_EQ(recipient->who, dyingBaz);
Martijn Coenen115d4282016-12-19 05:14:04 +01001360 std::unique_lock<std::mutex> lock2(recipient2->mutex);
Steven Moreland7b680eb2017-04-19 16:34:26 -07001361 recipient2->condition.wait_for(lock2, std::chrono::milliseconds(100), [&recipient2]() {
Martijn Coenen115d4282016-12-19 05:14:04 +01001362 return recipient2->fired;
1363 });
1364 EXPECT_FALSE(recipient2->fired);
1365
1366 // Verify servicemanager dropped its reference too
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001367 sp<IBaz> deadBaz = IBaz::getService("dyingBaz", false);
1368 if (deadBaz != nullptr) {
Martijn Coenen115d4282016-12-19 05:14:04 +01001369 // Got a passthrough
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001370 EXPECT_FALSE(deadBaz->isRemote());
Martijn Coenen115d4282016-12-19 05:14:04 +01001371 }
1372}
1373
Yifan Hong1dc87932016-08-19 09:51:01 -07001374TEST_F(HidlTest, BarThisIsNewTest) {
Iliyan Malchev0acf4192016-08-22 19:33:20 -07001375 // Now the tricky part, get access to the derived interface.
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001376 ALOGI("CLIENT call thisIsNew.");
Yifan Hong1dc87932016-08-19 09:51:01 -07001377 EXPECT_OK(bar->thisIsNew());
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001378 ALOGI("CLIENT thisIsNew returned.");
Andreas Huber9cd48d02016-08-03 14:25:59 -07001379}
1380
Steven Morelandf2fd9ba2017-06-26 09:47:23 -07001381static void expectGoodChild(sp<IChild> child) {
1382 ASSERT_NE(child.get(), nullptr);
1383 child = IChild::castFrom(child);
Yifan Hong1e81c532016-10-18 18:43:46 -07001384 ASSERT_NE(child.get(), nullptr);
1385 EXPECT_OK(child->doGrandparent());
1386 EXPECT_OK(child->doParent());
1387 EXPECT_OK(child->doChild());
1388}
1389
Steven Morelandf2fd9ba2017-06-26 09:47:23 -07001390static void expectGoodParent(sp<IParent> parent) {
1391 ASSERT_NE(parent.get(), nullptr);
1392 parent = IParent::castFrom(parent);
Yifan Hong1e81c532016-10-18 18:43:46 -07001393 ASSERT_NE(parent.get(), nullptr);
1394 EXPECT_OK(parent->doGrandparent());
1395 EXPECT_OK(parent->doParent());
1396 sp<IChild> child = IChild::castFrom(parent);
1397 expectGoodChild(child);
1398}
1399
Steven Morelandf2fd9ba2017-06-26 09:47:23 -07001400static void expectGoodGrandparent(sp<IGrandparent> grandparent) {
1401 ASSERT_NE(grandparent.get(), nullptr);
1402 grandparent = IGrandparent::castFrom(grandparent);
Yifan Hong1e81c532016-10-18 18:43:46 -07001403 ASSERT_NE(grandparent.get(), nullptr);
1404 EXPECT_OK(grandparent->doGrandparent());
1405 sp<IParent> parent = IParent::castFrom(grandparent);
1406 expectGoodParent(parent);
1407}
1408
Yifan Hong5749b2c2016-11-28 12:52:36 -08001409TEST_F(HidlTest, FooHaveAnInterfaceTest) {
Yifan Hong5749b2c2016-11-28 12:52:36 -08001410 sp<ISimple> in = new Complicated(42);
Martijn Coenenb40ef022017-01-02 15:21:46 +01001411 Return<sp<ISimple>> ret = bar->haveAInterface(in);
1412 EXPECT_OK(ret);
1413 sp<ISimple> out = ret;
1414 ASSERT_NE(out.get(), nullptr);
1415 EXPECT_EQ(out->getCookie(), 42);
Yifan Hongfbcdc802017-02-22 18:12:48 -08001416 EXPECT_OK(out->customVecInt([](const auto &) { }));
1417 EXPECT_OK(out->customVecStr([](const auto &) { }));
1418 EXPECT_OK(out->ping());
1419 EXPECT_OK(out->mystr([](const auto &) { }));
1420 EXPECT_OK(out->myhandle([](const auto &) { }));
Yifan Hong5749b2c2016-11-28 12:52:36 -08001421}
1422
Yifan Hong1e81c532016-10-18 18:43:46 -07001423TEST_F(HidlTest, InheritRemoteGrandparentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001424 Return<sp<IGrandparent>> ret = fetcher->getGrandparent(true);
1425 EXPECT_OK(ret);
1426 expectGoodGrandparent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001427}
1428
1429TEST_F(HidlTest, InheritLocalGrandparentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001430 Return<sp<IGrandparent>> ret = fetcher->getGrandparent(false);
1431 EXPECT_OK(ret);
1432 expectGoodGrandparent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001433}
1434
Yifan Hong4b0214d2016-12-07 14:57:57 -08001435TEST_F(HidlTest, InheritRemoteParentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001436 Return<sp<IParent>> ret = fetcher->getParent(true);
1437 EXPECT_OK(ret);
1438 expectGoodParent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001439}
1440
Yifan Hong4b0214d2016-12-07 14:57:57 -08001441TEST_F(HidlTest, InheritLocalParentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001442 Return<sp<IParent>> ret = fetcher->getParent(false);
1443 EXPECT_OK(ret);
1444 expectGoodParent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001445}
1446
1447TEST_F(HidlTest, InheritRemoteChildTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001448 Return<sp<IChild>> ret = fetcher->getChild(true);
1449 EXPECT_OK(ret);
1450 expectGoodChild(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001451}
1452
1453TEST_F(HidlTest, InheritLocalChildTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001454 Return<sp<IChild>> ret = fetcher->getChild(false);
1455 EXPECT_OK(ret);
1456 expectGoodChild(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001457}
1458
Andreas Huber5d034772016-09-28 14:23:51 -07001459TEST_F(HidlTest, TestArrayDimensionality) {
1460 hidl_array<int, 2> oneDim;
1461 hidl_array<int, 2, 3> twoDim;
1462 hidl_array<int, 2, 3, 4> threeDim;
1463
1464 EXPECT_EQ(oneDim.size(), 2u);
1465 EXPECT_EQ(twoDim.size(), std::make_tuple(2u, 3u));
1466 EXPECT_EQ(threeDim.size(), std::make_tuple(2u, 3u, 4u));
1467}
1468
Yifan Hongc6752dc2016-12-20 14:00:14 -08001469TEST_F(HidlTest, StructEqualTest) {
1470 using G = IFoo::Goober;
1471 using F = IFoo::Fumble;
1472 G g1{
1473 .q = 42,
1474 .name = "The Ultimate Question of Life, the Universe, and Everything",
1475 .address = "North Pole",
1476 .numbers = std::array<double, 10>{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} },
1477 .fumble = F{.data = {.data = 50}},
1478 .gumble = F{.data = {.data = 60}}
1479 };
1480 G g2{
1481 .q = 42,
1482 .name = "The Ultimate Question of Life, the Universe, and Everything",
1483 .address = "North Pole",
1484 .numbers = std::array<double, 10>{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} },
1485 .fumble = F{.data = {.data = 50}},
1486 .gumble = F{.data = {.data = 60}}
1487 };
1488 G g3{
1489 .q = 42,
1490 .name = "The Ultimate Question of Life, the Universe, and Everything",
1491 .address = "North Pole",
1492 .numbers = std::array<double, 10>{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} },
1493 .fumble = F{.data = {.data = 50}},
1494 .gumble = F{.data = {.data = 61}}
1495 };
1496 // explicitly invoke operator== here.
1497 EXPECT_TRUE(g1 == g2);
1498 EXPECT_TRUE(g1 != g3);
1499}
1500
1501TEST_F(HidlTest, EnumEqualTest) {
1502 using E = IFoo::SomeEnum;
1503 E e1 = E::quux;
1504 E e2 = E::quux;
1505 E e3 = E::goober;
1506 // explicitly invoke operator== here.
1507 EXPECT_TRUE(e1 == e2);
1508 EXPECT_TRUE(e1 != e3);
1509}
1510
Yifan Hongbf459bc2016-08-23 16:50:37 -07001511#if HIDL_RUN_POINTER_TESTS
Andreas Huber9cd48d02016-08-03 14:25:59 -07001512
Yifan Hongbf459bc2016-08-23 16:50:37 -07001513TEST_F(HidlTest, PassAGraphTest) {
1514 IGraph::Graph g;
Yifan Hong398e6fb2016-10-17 11:38:09 -07001515 ::android::simpleGraph(g);
1516 ::android::logSimpleGraph("CLIENT", g);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001517 ALOGI("CLIENT call passAGraph");
1518 EXPECT_OK(graphInterface->passAGraph(g));
1519}
1520
1521TEST_F(HidlTest, GiveAGraphTest) {
1522 EXPECT_OK(graphInterface->giveAGraph([&](const auto &newGraph) {
Yifan Hong398e6fb2016-10-17 11:38:09 -07001523 ::android::logSimpleGraph("CLIENT", newGraph);
1524 EXPECT_TRUE(::android::isSimpleGraph(newGraph));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001525 }));
1526}
1527TEST_F(HidlTest, PassANodeTest) {
1528 IGraph::Node node; node.data = 10;
1529 EXPECT_OK(graphInterface->passANode(node));
1530}
1531TEST_F(HidlTest, PassTwoGraphsTest) {
1532 IGraph::Graph g;
Yifan Hong398e6fb2016-10-17 11:38:09 -07001533 ::android::simpleGraph(g);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001534 EXPECT_OK(graphInterface->passTwoGraphs(&g, &g));
1535}
1536TEST_F(HidlTest, PassAGammaTest) {
1537 IGraph::Theta s; s.data = 500;
1538 IGraph::Alpha a; a.s_ptr = &s;
1539 IGraph::Beta b; b.s_ptr = &s;
1540 IGraph::Gamma c; c.a_ptr = &a; c.b_ptr = &b;
1541 ALOGI("CLIENT calling passAGamma: c.a = %p, c.b = %p, c.a->s = %p, c.b->s = %p",
1542 c.a_ptr, c.b_ptr, c.a_ptr->s_ptr, c.b_ptr->s_ptr);
1543 EXPECT_OK(graphInterface->passAGamma(c));
1544}
1545TEST_F(HidlTest, PassNullTest) {
1546 IGraph::Gamma c;
1547 c.a_ptr = nullptr;
1548 c.b_ptr = nullptr;
1549 EXPECT_OK(graphInterface->passAGamma(c));
1550}
1551TEST_F(HidlTest, PassASimpleRefTest) {
1552 IGraph::Theta s;
1553 s.data = 500;
1554 IGraph::Alpha a;
1555 a.s_ptr = &s;
1556 EXPECT_OK(graphInterface->passASimpleRef(&a));
1557}
1558TEST_F(HidlTest, PassASimpleRefSTest) {
1559 IGraph::Theta s;
1560 s.data = 500;
1561 ALOGI("CLIENT call passASimpleRefS with %p", &s);
1562 EXPECT_OK(graphInterface->passASimpleRefS(&s));
1563}
1564TEST_F(HidlTest, GiveASimpleRefTest) {
1565 EXPECT_OK(graphInterface->giveASimpleRef([&](const auto & a_ptr) {
1566 EXPECT_EQ(a_ptr->s_ptr->data, 500);
1567 }));
1568}
1569TEST_F(HidlTest, GraphReportErrorsTest) {
1570 Return<int32_t> ret = graphInterface->getErrors();
1571 EXPECT_OK(ret);
1572 EXPECT_EQ(int32_t(ret), 0);
1573}
1574
1575TEST_F(HidlTest, PointerPassOldBufferTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001576 EXPECT_OK(validationPointerInterface->bar1([&](const auto& sptr, const auto& s) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001577 EXPECT_OK(pointerInterface->foo1(sptr, s));
1578 }));
1579}
1580TEST_F(HidlTest, PointerPassOldBufferTest2) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001581 EXPECT_OK(validationPointerInterface->bar2([&](const auto& s, const auto& a) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001582 EXPECT_OK(pointerInterface->foo2(s, a));
1583 }));
1584}
1585TEST_F(HidlTest, PointerPassSameOldBufferPointerTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001586 EXPECT_OK(validationPointerInterface->bar3([&](const auto& s, const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001587 EXPECT_OK(pointerInterface->foo3(s, a, b));
1588 }));
1589}
1590TEST_F(HidlTest, PointerPassOnlyTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001591 EXPECT_OK(validationPointerInterface->bar4([&](const auto& s) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001592 EXPECT_OK(pointerInterface->foo4(s));
1593 }));
1594}
1595TEST_F(HidlTest, PointerPassTwoEmbeddedTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001596 EXPECT_OK(validationPointerInterface->bar5([&](const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001597 EXPECT_OK(pointerInterface->foo5(a, b));
1598 }));
1599}
1600TEST_F(HidlTest, PointerPassIndirectBufferHasDataTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001601 EXPECT_OK(validationPointerInterface->bar6([&](const auto& a) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001602 EXPECT_OK(pointerInterface->foo6(a));
1603 }));
1604}
1605TEST_F(HidlTest, PointerPassTwoIndirectBufferTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001606 EXPECT_OK(validationPointerInterface->bar7([&](const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001607 EXPECT_OK(pointerInterface->foo7(a, b));
1608 }));
1609}
1610TEST_F(HidlTest, PointerPassDeeplyIndirectTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001611 EXPECT_OK(validationPointerInterface->bar8([&](const auto& d) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001612 EXPECT_OK(pointerInterface->foo8(d));
1613 }));
1614}
1615TEST_F(HidlTest, PointerPassStringRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001616 EXPECT_OK(validationPointerInterface->bar9([&](const auto& str) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001617 EXPECT_OK(pointerInterface->foo9(str));
1618 }));
1619}
1620TEST_F(HidlTest, PointerPassRefVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001621 EXPECT_OK(validationPointerInterface->bar10([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001622 EXPECT_OK(pointerInterface->foo10(v));
1623 }));
1624}
1625TEST_F(HidlTest, PointerPassVecRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001626 EXPECT_OK(validationPointerInterface->bar11([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001627 EXPECT_OK(pointerInterface->foo11(v));
1628 }));
1629}
1630TEST_F(HidlTest, PointerPassArrayRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001631 EXPECT_OK(validationPointerInterface->bar12([&](const auto& array) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001632 EXPECT_OK(pointerInterface->foo12(array));
1633 }));
1634}
1635TEST_F(HidlTest, PointerPassRefArrayTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001636 EXPECT_OK(validationPointerInterface->bar13([&](const auto& array) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001637 EXPECT_OK(pointerInterface->foo13(array));
1638 }));
1639}
1640TEST_F(HidlTest, PointerPass3RefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001641 EXPECT_OK(validationPointerInterface->bar14([&](const auto& p3) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001642 EXPECT_OK(pointerInterface->foo14(p3));
1643 }));
1644}
1645TEST_F(HidlTest, PointerPassInt3RefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001646 EXPECT_OK(validationPointerInterface->bar15([&](const auto& p3) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001647 EXPECT_OK(pointerInterface->foo15(p3));
1648 }));
1649}
1650TEST_F(HidlTest, PointerPassEmbeddedPointersTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001651 EXPECT_OK(validationPointerInterface->bar16([&](const auto& p) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001652 EXPECT_OK(pointerInterface->foo16(p));
1653 }));
1654}
1655TEST_F(HidlTest, PointerPassEmbeddedPointers2Test) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001656 EXPECT_OK(validationPointerInterface->bar17([&](const auto& p) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001657 EXPECT_OK(pointerInterface->foo17(p));
1658 }));
1659}
1660TEST_F(HidlTest, PointerPassCopiedStringTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001661 EXPECT_OK(validationPointerInterface->bar18([&](const auto& str_ref, const auto& str_ref2, const auto& str) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001662 EXPECT_OK(pointerInterface->foo18(str_ref, str_ref2, str));
1663 }));
1664}
1665TEST_F(HidlTest, PointerPassCopiedVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001666 EXPECT_OK(validationPointerInterface->bar19([&](const auto& a_vec_ref, const auto& a_vec, const auto& a_vec_ref2) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001667 EXPECT_OK(pointerInterface->foo19(a_vec_ref, a_vec, a_vec_ref2));
1668 }));
1669}
Yifan Hong84465902016-09-27 15:52:17 -07001670TEST_F(HidlTest, PointerPassBigRefVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001671 EXPECT_OK(validationPointerInterface->bar20([&](const auto& v) {
Yifan Hong84465902016-09-27 15:52:17 -07001672 EXPECT_FAIL(pointerInterface->foo20(v));
1673 }));
1674}
Yifan Hongbf459bc2016-08-23 16:50:37 -07001675TEST_F(HidlTest, PointerPassMultidimArrayRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001676 EXPECT_OK(validationPointerInterface->bar21([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001677 EXPECT_OK(pointerInterface->foo21(v));
1678 }));
1679}
1680TEST_F(HidlTest, PointerPassRefMultidimArrayTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001681 EXPECT_OK(validationPointerInterface->bar22([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001682 EXPECT_OK(pointerInterface->foo22(v));
1683 }));
1684}
1685TEST_F(HidlTest, PointerGiveOldBufferTest) {
1686 EXPECT_OK(pointerInterface->bar1([&](const auto& sptr, const auto& s) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001687 EXPECT_OK(validationPointerInterface->foo1(sptr, s));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001688 }));
1689}
1690TEST_F(HidlTest, PointerGiveOldBufferTest2) {
1691 EXPECT_OK(pointerInterface->bar2([&](const auto& s, const auto& a) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001692 EXPECT_OK(validationPointerInterface->foo2(s, a));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001693 }));
1694}
1695TEST_F(HidlTest, PointerGiveSameOldBufferPointerTest) {
1696 EXPECT_OK(pointerInterface->bar3([&](const auto& s, const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001697 EXPECT_OK(validationPointerInterface->foo3(s, a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001698 }));
1699}
1700TEST_F(HidlTest, PointerGiveOnlyTest) {
1701 EXPECT_OK(pointerInterface->bar4([&](const auto& s) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001702 EXPECT_OK(validationPointerInterface->foo4(s));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001703 }));
1704}
1705TEST_F(HidlTest, PointerGiveTwoEmbeddedTest) {
1706 EXPECT_OK(pointerInterface->bar5([&](const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001707 EXPECT_OK(validationPointerInterface->foo5(a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001708 }));
1709}
1710TEST_F(HidlTest, PointerGiveIndirectBufferHasDataTest) {
1711 EXPECT_OK(pointerInterface->bar6([&](const auto& a) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001712 EXPECT_OK(validationPointerInterface->foo6(a));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001713 }));
1714}
1715TEST_F(HidlTest, PointerGiveTwoIndirectBufferTest) {
1716 EXPECT_OK(pointerInterface->bar7([&](const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001717 EXPECT_OK(validationPointerInterface->foo7(a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001718 }));
1719}
1720TEST_F(HidlTest, PointerGiveDeeplyIndirectTest) {
1721 EXPECT_OK(pointerInterface->bar8([&](const auto& d) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001722 EXPECT_OK(validationPointerInterface->foo8(d));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001723 }));
1724}
1725TEST_F(HidlTest, PointerGiveStringRefTest) {
1726 EXPECT_OK(pointerInterface->bar9([&](const auto& str) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001727 EXPECT_OK(validationPointerInterface->foo9(str));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001728 }));
1729}
1730TEST_F(HidlTest, PointerGiveRefVecTest) {
1731 EXPECT_OK(pointerInterface->bar10([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001732 EXPECT_OK(validationPointerInterface->foo10(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001733 }));
1734}
1735TEST_F(HidlTest, PointerGiveVecRefTest) {
1736 EXPECT_OK(pointerInterface->bar11([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001737 EXPECT_OK(validationPointerInterface->foo11(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001738 }));
1739}
1740TEST_F(HidlTest, PointerGiveArrayRefTest) {
1741 EXPECT_OK(pointerInterface->bar12([&](const auto& array) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001742 EXPECT_OK(validationPointerInterface->foo12(array));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001743 }));
1744}
1745TEST_F(HidlTest, PointerGiveRefArrayTest) {
1746 EXPECT_OK(pointerInterface->bar13([&](const auto& array) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001747 EXPECT_OK(validationPointerInterface->foo13(array));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001748 }));
1749}
1750TEST_F(HidlTest, PointerGive3RefTest) {
1751 EXPECT_OK(pointerInterface->bar14([&](const auto& p3) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001752 EXPECT_OK(validationPointerInterface->foo14(p3));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001753 }));
1754}
1755TEST_F(HidlTest, PointerGiveInt3RefTest) {
1756 EXPECT_OK(pointerInterface->bar15([&](const auto& p3) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001757 EXPECT_OK(validationPointerInterface->foo15(p3));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001758 }));
1759}
1760TEST_F(HidlTest, PointerGiveEmbeddedPointersTest) {
1761 EXPECT_OK(pointerInterface->bar16([&](const auto& p) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001762 EXPECT_OK(validationPointerInterface->foo16(p));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001763 }));
1764}
1765TEST_F(HidlTest, PointerGiveEmbeddedPointers2Test) {
1766 EXPECT_OK(pointerInterface->bar17([&](const auto& p) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001767 EXPECT_OK(validationPointerInterface->foo17(p));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001768 }));
1769}
1770TEST_F(HidlTest, PointerGiveCopiedStringTest) {
1771 EXPECT_OK(pointerInterface->bar18([&](const auto& str_ref, const auto& str_ref2, const auto& str) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001772 EXPECT_OK(validationPointerInterface->foo18(str_ref, str_ref2, str));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001773 }));
1774}
1775TEST_F(HidlTest, PointerGiveCopiedVecTest) {
1776 EXPECT_OK(pointerInterface->bar19([&](const auto& a_vec_ref, const auto& a_vec, const auto& a_vec_ref2) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001777 EXPECT_OK(validationPointerInterface->foo19(a_vec_ref, a_vec, a_vec_ref2));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001778 }));
1779}
Yifan Hong84465902016-09-27 15:52:17 -07001780// This cannot be enabled until _hidl_error is not ignored when
1781// the remote writeEmbeddedReferencesToParcel.
1782// TEST_F(HidlTest, PointerGiveBigRefVecTest) {
1783// EXPECT_FAIL(pointerInterface->bar20([&](const auto& v) {
1784// }));
1785// }
Yifan Hongbf459bc2016-08-23 16:50:37 -07001786TEST_F(HidlTest, PointerGiveMultidimArrayRefTest) {
1787 EXPECT_OK(pointerInterface->bar21([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001788 EXPECT_OK(validationPointerInterface->foo21(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001789 }));
1790}
1791TEST_F(HidlTest, PointerGiveRefMultidimArrayTest) {
1792 EXPECT_OK(pointerInterface->bar22([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001793 EXPECT_OK(validationPointerInterface->foo22(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001794 }));
1795}
1796TEST_F(HidlTest, PointerReportErrorsTest) {
1797 Return<int32_t> ret = pointerInterface->getErrors();
1798 EXPECT_OK(ret);
1799 EXPECT_EQ(int32_t(ret), 0);
1800}
1801#endif
1802
Steven Moreland7b680eb2017-04-19 16:34:26 -07001803template <class T>
1804static void waitForServer(const std::string &serviceName) {
1805 ::android::hardware::details::waitForHwService(T::descriptor, serviceName);
1806}
1807
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001808int forkAndRunTests(TestMode mode, bool enableDelayMeasurementTests) {
Yifan Hongc70f0d82016-10-10 14:50:22 -07001809 pid_t child;
1810 int status;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001811
Yifan Hongc70f0d82016-10-10 14:50:22 -07001812 const char* modeText = (mode == BINDERIZED) ? "BINDERIZED" : "PASSTHROUGH";
1813 ALOGI("Start running tests in %s mode...", modeText);
1814 fprintf(stdout, "Start running tests in %s mode...\n", modeText);
1815 fflush(stdout);
1816
1817 if ((child = fork()) == 0) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001818 gHidlEnvironment = static_cast<HidlEnvironment *>(
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001819 ::testing::AddGlobalTestEnvironment(new HidlEnvironment(
1820 mode, enableDelayMeasurementTests)));
Yifan Hongc70f0d82016-10-10 14:50:22 -07001821 int testStatus = RUN_ALL_TESTS();
1822 if(testStatus == 0) {
1823 exit(0);
1824 }
1825 int failed = ::testing::UnitTest::GetInstance()->failed_test_count();
1826 if (failed == 0) {
1827 exit(-testStatus);
1828 }
1829 exit(failed);
1830 }
1831 waitpid(child, &status, 0 /* options */);
1832 ALOGI("All tests finished in %s mode.", modeText);
1833 fprintf(stdout, "All tests finished in %s mode.\n", modeText);
1834 fflush(stdout);
Yifan Hong1dc87932016-08-19 09:51:01 -07001835 return status;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001836}
Yifan Hongc70f0d82016-10-10 14:50:22 -07001837
1838void handleStatus(int status, const char *mode) {
1839 if (status != 0) {
1840 if (WIFEXITED(status)) {
1841 status = WEXITSTATUS(status);
1842 if (status < 0) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001843 fprintf(stdout, " RUN_ALL_TESTS returns %d for %s mode.\n", -status, mode);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001844 } else {
Yifan Hong48562fd2016-10-24 15:26:10 -07001845 fprintf(stdout, " %d test(s) failed for %s mode.\n", status, mode);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001846 }
1847 } else {
Yifan Hong48562fd2016-10-24 15:26:10 -07001848 fprintf(stdout, " ERROR: %s child process exited abnormally with %d\n", mode, status);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001849 }
1850 }
1851}
1852
Yifan Hongd12398d2016-10-13 11:05:29 -07001853static void usage(const char *me) {
1854 fprintf(stderr,
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001855 "usage: %s [-b] [-p] [-d] [GTEST_OPTIONS]\n",
Yifan Hongd12398d2016-10-13 11:05:29 -07001856 me);
1857
1858 fprintf(stderr, " -b binderized mode only\n");
1859 fprintf(stderr, " -p passthrough mode only\n");
1860 fprintf(stderr, " (if -b and -p are both missing or both present, "
1861 "both modes are tested.)\n");
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001862 fprintf(stderr, " -d Enable delay measurement tests\n");
Yifan Hongd12398d2016-10-13 11:05:29 -07001863}
1864
Yifan Hongc70f0d82016-10-10 14:50:22 -07001865int main(int argc, char **argv) {
Steven Morelandf9cf33b2017-05-18 13:58:54 -07001866 setenv("TREBLE_TESTING_OVERRIDE", "true", true);
1867
Yifan Hongd12398d2016-10-13 11:05:29 -07001868 const char *me = argv[0];
1869 bool b = false;
1870 bool p = false;
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001871 bool d = false;
Yifan Hongd12398d2016-10-13 11:05:29 -07001872 struct option longopts[] = {{0,0,0,0}};
1873 int res;
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001874 while ((res = getopt_long(argc, argv, "hbpd", longopts, NULL)) >= 0) {
Yifan Hongd12398d2016-10-13 11:05:29 -07001875 switch (res) {
1876 case 'h': {
1877 usage(me);
1878 exit(1);
1879 } break;
1880
1881 case 'b': {
1882 b = true;
1883 } break;
1884
1885 case 'p': {
1886 p = true;
1887 } break;
1888
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001889 case 'd': {
1890 d = true;
1891 } break;
1892
Yifan Hongd12398d2016-10-13 11:05:29 -07001893 case '?':
1894 default: {
1895 // ignore. pass to gTest.
1896 } break;
1897 }
1898 }
1899 if (!b && !p) {
1900 b = p = true;
1901 }
Yifan Hongc70f0d82016-10-10 14:50:22 -07001902
1903 ::testing::InitGoogleTest(&argc, argv);
1904 // put test in child process because RUN_ALL_TESTS
1905 // should not be run twice.
Hridya Valsarajud7899cd2017-03-30 16:36:48 -07001906 int pStatus = p ? forkAndRunTests(PASSTHROUGH, d) : 0;
1907 int bStatus = b ? forkAndRunTests(BINDERIZED, d) : 0;
Yifan Hongc70f0d82016-10-10 14:50:22 -07001908
Yifan Hong48562fd2016-10-24 15:26:10 -07001909 fprintf(stdout, "\n=========================================================\n\n"
1910 " Summary:\n\n");
Yifan Hongd12398d2016-10-13 11:05:29 -07001911 if (p) {
1912 ALOGI("PASSTHROUGH Test result = %d", pStatus);
1913 handleStatus(pStatus, "PASSTHROUGH");
1914 }
1915 if (b) {
Steven Moreland7b680eb2017-04-19 16:34:26 -07001916 EACH_SERVER(waitForServer);
Yifan Hongd12398d2016-10-13 11:05:29 -07001917 ALOGI("BINDERIZED Test result = %d", bStatus);
1918 handleStatus(bStatus, "BINDERIZED ");
1919 }
1920
Yifan Hongc70f0d82016-10-10 14:50:22 -07001921 if (pStatus == 0 && bStatus == 0) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001922 fprintf(stdout, " Hooray! All tests passed.\n");
Yifan Hongc70f0d82016-10-10 14:50:22 -07001923 }
Yifan Hong48562fd2016-10-24 15:26:10 -07001924 fprintf(stdout, "\n=========================================================\n\n");
Yifan Hongc70f0d82016-10-10 14:50:22 -07001925
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001926 return pStatus + bStatus != 0;
Yifan Hongc70f0d82016-10-10 14:50:22 -07001927}