blob: b8d11e31847d0ee4209dd0fc454913de53345dff [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"
4
Andreas Huber9cd48d02016-08-03 14:25:59 -07005#include <android-base/logging.h>
6
Steven Moreland01bcb772016-11-08 15:57:25 -08007#include <android/hidl/manager/1.0/IServiceManager.h>
Steven Moreland0693f312016-11-09 15:06:14 -08008#include <android/hidl/manager/1.0/IServiceNotification.h>
Steven Moreland01bcb772016-11-08 15:57:25 -08009
Steven Morelandffc4e512017-02-16 19:22:34 -080010#include <android/hidl/allocator/1.0/IAllocator.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010011#include <android/hidl/memory/1.0/IMemory.h>
12
Steven Morelandaa2b83a2016-12-21 15:52:11 -080013#include <android/hidl/token/1.0/ITokenManager.h>
14
Steven Morelandb48a7da2016-11-11 14:12:46 -080015#include <android/hardware/tests/foo/1.0/IFoo.h>
Yifan Hong01e7cde2017-01-09 17:45:45 -080016#include <android/hardware/tests/foo/1.0/BnHwSimple.h>
Yifan Hongdef2cfb2016-12-14 16:00:24 -080017#include <android/hardware/tests/foo/1.0/BsSimple.h>
Yifan Hong01e7cde2017-01-09 17:45:45 -080018#include <android/hardware/tests/foo/1.0/BpHwSimple.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080019#include <android/hardware/tests/bar/1.0/IBar.h>
Yifan Hong5749b2c2016-11-28 12:52:36 -080020#include <android/hardware/tests/bar/1.0/IComplicated.h>
Yifan Hong87ff8232017-01-09 12:07:05 -080021#include <android/hardware/tests/bar/1.0/IImportRules.h>
Hridya Valsaraju179379a2017-02-09 16:38:12 -080022#include <android/hardware/tests/baz/1.0/IBaz.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080023#include <android/hardware/tests/inheritance/1.0/IFetcher.h>
24#include <android/hardware/tests/inheritance/1.0/IGrandparent.h>
25#include <android/hardware/tests/inheritance/1.0/IParent.h>
26#include <android/hardware/tests/inheritance/1.0/IChild.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010027#include <android/hardware/tests/memory/1.0/IMemoryTest.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080028#include <android/hardware/tests/pointer/1.0/IGraph.h>
29#include <android/hardware/tests/pointer/1.0/IPointer.h>
Andreas Huber9cd48d02016-08-03 14:25:59 -070030
Yifan Hong1dc87932016-08-19 09:51:01 -070031#include <gtest/gtest.h>
32#if GTEST_IS_THREADSAFE
33#include <sys/types.h>
Yifan Hongc70f0d82016-10-10 14:50:22 -070034#include <sys/wait.h>
Yifan Hong1dc87932016-08-19 09:51:01 -070035#include <signal.h>
36#include <errno.h>
37#include <pthread.h>
38#else
39#error "GTest did not detect pthread library."
40#endif
41
Steven Moreland01bcb772016-11-08 15:57:25 -080042#include <algorithm>
Steven Moreland0693f312016-11-09 15:06:14 -080043#include <condition_variable>
Yifan Hongd12398d2016-10-13 11:05:29 -070044#include <getopt.h>
45#include <inttypes.h>
Steven Moreland0693f312016-11-09 15:06:14 -080046#include <mutex>
Steven Moreland01bcb772016-11-08 15:57:25 -080047#include <set>
Yifan Hongbf459bc2016-08-23 16:50:37 -070048#include <sstream>
Yifan Hongccd782b2016-11-28 09:41:46 -080049#include <utility>
Yifan Hongd12398d2016-10-13 11:05:29 -070050#include <vector>
Yifan Hongbf459bc2016-08-23 16:50:37 -070051
Yifan Hong398e6fb2016-10-17 11:38:09 -070052#include <hidl-test/FooHelper.h>
53#include <hidl-test/PointerHelper.h>
54
Martijn Coenen93915102016-09-01 01:35:52 +020055#include <hidl/Status.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010056#include <hidlmemory/mapping.h>
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010057
Iliyan Malchev0acf4192016-08-22 19:33:20 -070058#include <utils/Condition.h>
59#include <utils/Timers.h>
60
Yifan Hongbf459bc2016-08-23 16:50:37 -070061#define EXPECT_OK(__ret__) EXPECT_TRUE(isOk(__ret__))
Yifan Hong84465902016-09-27 15:52:17 -070062#define EXPECT_FAIL(__ret__) EXPECT_FALSE(isOk(__ret__))
Yifan Hong8c48ad72016-10-14 11:34:59 -070063#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hongbf459bc2016-08-23 16:50:37 -070064
65// TODO uncomment this when kernel is patched with pointer changes.
66//#define HIDL_RUN_POINTER_TESTS 1
67
Yifan Hongca890522016-10-12 10:03:41 -070068// forward declarations.
Hridya Valsaraju179379a2017-02-09 16:38:12 -080069class HidlEnvironment;
Yifan Hongca890522016-10-12 10:03:41 -070070
Yifan Hongc70f0d82016-10-10 14:50:22 -070071// static storage
Hridya Valsaraju179379a2017-02-09 16:38:12 -080072enum TestMode {
Yifan Hongc70f0d82016-10-10 14:50:22 -070073 BINDERIZED,
74 PASSTHROUGH
Hridya Valsaraju179379a2017-02-09 16:38:12 -080075};
76
77static HidlEnvironment *gHidlEnvironment = nullptr;
Yifan Hongc70f0d82016-10-10 14:50:22 -070078
Andreas Huber86a112b2016-10-19 14:25:16 -070079using ::android::hardware::tests::foo::V1_0::Abc;
Steven Moreland88ca4512016-08-11 11:24:10 -070080using ::android::hardware::tests::foo::V1_0::IFoo;
81using ::android::hardware::tests::foo::V1_0::IFooCallback;
Andreas Huber86a112b2016-10-19 14:25:16 -070082using ::android::hardware::tests::foo::V1_0::ISimple;
Yifan Hong11992a62016-11-09 18:07:40 -080083using ::android::hardware::tests::foo::V1_0::implementation::FooCallback;
Steven Moreland88ca4512016-08-11 11:24:10 -070084using ::android::hardware::tests::bar::V1_0::IBar;
Yifan Hong5749b2c2016-11-28 12:52:36 -080085using ::android::hardware::tests::bar::V1_0::IComplicated;
Hridya Valsaraju179379a2017-02-09 16:38:12 -080086using ::android::hardware::tests::baz::V1_0::IBaz;
Yifan Hong1e81c532016-10-18 18:43:46 -070087using ::android::hardware::tests::inheritance::V1_0::IFetcher;
88using ::android::hardware::tests::inheritance::V1_0::IGrandparent;
89using ::android::hardware::tests::inheritance::V1_0::IParent;
90using ::android::hardware::tests::inheritance::V1_0::IChild;
Yifan Hongbf459bc2016-08-23 16:50:37 -070091using ::android::hardware::tests::pointer::V1_0::IGraph;
92using ::android::hardware::tests::pointer::V1_0::IPointer;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010093using ::android::hardware::tests::memory::V1_0::IMemoryTest;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -070094using ::android::hardware::Return;
Iliyan Malchevd57066f2016-09-08 13:59:38 -070095using ::android::hardware::Void;
Andreas Huberf03332a2016-09-22 15:35:43 -070096using ::android::hardware::hidl_array;
Martijn Coenen115d4282016-12-19 05:14:04 +010097using ::android::hardware::hidl_death_recipient;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010098using ::android::hardware::hidl_memory;
Andreas Huber8a82ff72016-08-04 10:29:39 -070099using ::android::hardware::hidl_string;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100100using ::android::hardware::hidl_vec;
Steven Morelandffc4e512017-02-16 19:22:34 -0800101using ::android::hidl::allocator::V1_0::IAllocator;
Martijn Coenen115d4282016-12-19 05:14:04 +0100102using ::android::hidl::base::V1_0::IBase;
Steven Moreland01bcb772016-11-08 15:57:25 -0800103using ::android::hidl::manager::V1_0::IServiceManager;
Steven Moreland0693f312016-11-09 15:06:14 -0800104using ::android::hidl::manager::V1_0::IServiceNotification;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100105using ::android::hidl::memory::V1_0::IMemory;
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800106using ::android::hidl::token::V1_0::ITokenManager;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700107using ::android::sp;
Martijn Coenen115d4282016-12-19 05:14:04 +0100108using ::android::wp;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700109using ::android::to_string;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700110using ::android::Mutex;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700111using ::android::MultiDimensionalToString;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700112using ::android::Condition;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700113using ::android::DELAY_S;
114using ::android::DELAY_NS;
115using ::android::TOLERANCE_NS;
116using ::android::ONEWAY_TOLERANCE_NS;
117using std::to_string;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700118
Yifan Hongbf459bc2016-08-23 16:50:37 -0700119template <typename T>
Yifan Honga3e8b232017-02-22 10:56:09 -0800120static inline ::testing::AssertionResult isOk(const ::android::hardware::Return<T> &ret) {
Steven Morelandcd00b9b2016-12-29 10:34:03 -0800121 return ret.isOk()
122 ? (::testing::AssertionSuccess() << ret.description())
123 : (::testing::AssertionFailure() << ret.description());
Yifan Hongbf459bc2016-08-23 16:50:37 -0700124}
125
126template<typename T, typename S>
127static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
128 for(size_t i = 0; i < size; i++)
129 if(arr1[i] != arr2[i])
130 return false;
131 return true;
132}
133
Steven Moreland01bcb772016-11-08 15:57:25 -0800134template<typename T>
135std::string to_string(std::set<T> set) {
136 std::stringstream ss;
137 ss << "{";
138
139 bool first = true;
140 for (const T &item : set) {
141 if (first) {
142 first = false;
143 } else {
144 ss << ", ";
145 }
146
147 ss << to_string(item);
148 }
149
150 ss << "}";
151
152 return ss.str();
153}
154
Andreas Huber86a112b2016-10-19 14:25:16 -0700155struct Simple : public ISimple {
156 Simple(int32_t cookie)
157 : mCookie(cookie) {
158 }
159
160 Return<int32_t> getCookie() override {
161 return mCookie;
162 }
163
Yifan Hong5749b2c2016-11-28 12:52:36 -0800164 Return<void> customVecInt(customVecInt_cb _cb) override {
165 _cb(hidl_vec<int32_t>());
166 return Void();
167 }
168
169 Return<void> customVecStr(customVecStr_cb _cb) override {
170 hidl_vec<hidl_string> vec;
171 vec.resize(2);
172 _cb(vec);
173 return Void();
174 }
175
176 Return<void> mystr(mystr_cb _cb) override {
177 _cb(hidl_string());
178 return Void();
179 }
180
181 Return<void> myhandle(myhandle_cb _cb) override {
182 auto h = native_handle_create(0, 1);
183 _cb(h);
184 native_handle_delete(h);
185 return Void();
186 }
187
188private:
189 int32_t mCookie;
190};
191
Steven Moreland21aa43c2017-03-07 17:15:24 -0800192struct SimpleParent : public IParent {
193 Return<void> doGrandparent() override {
194 return Void();
195 }
196 Return<void> doParent() override {
197 return Void();
198 }
199};
200
201struct SimpleChild : public IChild {
202 Return<void> doGrandparent() override {
203 return Void();
204 }
205 Return <void> doParent() override {
206 return Void();
207 }
208 Return <void> doChild() override {
209 return Void();
210 }
211};
212
Yifan Hong5749b2c2016-11-28 12:52:36 -0800213struct Complicated : public IComplicated {
214 Complicated(int32_t cookie)
215 : mCookie(cookie) {
216 }
217
218 Return<int32_t> getCookie() override {
219 return mCookie;
220 }
221
222 Return<void> customVecInt(customVecInt_cb _cb) override {
223 _cb(hidl_vec<int32_t>());
224 return Void();
225 }
226 Return<void> customVecStr(customVecStr_cb _cb) override {
227 hidl_vec<hidl_string> vec;
228 vec.resize(2);
229 _cb(vec);
230 return Void();
231 }
232
233 Return<void> mystr(mystr_cb _cb) override {
234 _cb(hidl_string());
235 return Void();
236 }
237
238 Return<void> myhandle(myhandle_cb _cb) override {
239 auto h = native_handle_create(0, 1);
240 _cb(h);
241 native_handle_delete(h);
242 return Void();
243 }
244
Andreas Huber86a112b2016-10-19 14:25:16 -0700245private:
246 int32_t mCookie;
247};
Steven Moreland0693f312016-11-09 15:06:14 -0800248
Yifan Hong87ff8232017-01-09 12:07:05 -0800249// Ensure (statically) that the types in IImportRules resolves to the correct types by
250// overriding the methods with fully namespaced types as arguments.
251struct MyImportRules : public ::android::hardware::tests::bar::V1_0::IImportRules {
252 Return<void> rule0a(
253 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer&) override {
254 return Void();
255 }
256
257 Return<void> rule0a1(
258 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer&) override {
259 return Void();
260 }
261
262 Return<void> rule0b(
263 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer&) override {
264 return Void();
265 }
266
267 Return<void> rule0c(const ::android::hardware::tests::foo::V1_0::Outer&) override {
268 return Void();
269 }
270
271 Return<void> rule0d(const ::android::hardware::tests::foo::V1_0::Outer&) override {
272 return Void();
273 }
274
275 Return<void> rule0e(
276 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer::Inner&) override {
277 return Void();
278 }
279
280 Return<void> rule0f(
281 const ::android::hardware::tests::bar::V1_0::IImportRules::Outer::Inner&) override {
282 return Void();
283 }
284
285 Return<void> rule0g(const ::android::hardware::tests::foo::V1_0::Outer::Inner&) override {
286 return Void();
287 }
288
289 Return<void> rule0h(const ::android::hardware::tests::foo::V1_0::Outer::Inner&) override {
290 return Void();
291 }
292
293 Return<void> rule1a(const ::android::hardware::tests::bar::V1_0::Def&) override {
294 return Void();
295 }
296
297 Return<void> rule1b(const ::android::hardware::tests::foo::V1_0::Def&) override {
298 return Void();
299 }
300
301 Return<void> rule2a(const ::android::hardware::tests::foo::V1_0::Unrelated&) override {
302 return Void();
303 }
304
305 Return<void> rule2b(const sp<::android::hardware::tests::foo::V1_0::IFooCallback>&) override {
306 return Void();
307 }
308};
309
Steven Moreland0693f312016-11-09 15:06:14 -0800310struct ServiceNotification : public IServiceNotification {
311 std::mutex mutex;
312 std::condition_variable condition;
313
314 Return<void> onRegistration(const hidl_string &fqName,
315 const hidl_string &name,
316 bool preexisting) override {
317 if (preexisting) {
318 // not interested in things registered from previous runs of hidl_test
319 return Void();
320 }
321
322 std::unique_lock<std::mutex> lock(mutex);
323
324 mRegistered.push_back(std::string(fqName.c_str()) + "/" + name.c_str());
325
326 lock.unlock();
327 condition.notify_one();
328
329 return Void();
330 }
331
332 const std::vector<std::string> &getRegistrations() const {
333 return mRegistered;
334 }
335
336private:
337 std::vector<std::string> mRegistered{};
338};
339
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800340class HidlEnvironment : public ::testing::Environment {
Yifan Hong1dc87932016-08-19 09:51:01 -0700341public:
Steven Moreland01bcb772016-11-08 15:57:25 -0800342 sp<IServiceManager> manager;
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800343 sp<ITokenManager> tokenManager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100344 sp<IAllocator> ashmemAllocator;
345 sp<IMemoryTest> memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700346 sp<IFetcher> fetcher;
Yifan Hong1dc87932016-08-19 09:51:01 -0700347 sp<IFoo> foo;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800348 sp<IBaz> dyingBaz;
Yifan Hong1dc87932016-08-19 09:51:01 -0700349 sp<IBar> bar;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700350 sp<IGraph> graphInterface;
351 sp<IPointer> pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700352 sp<IPointer> validationPointerInterface;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800353 TestMode mode;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700354
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800355 HidlEnvironment(TestMode mode) : mode(mode) {};
Yifan Hongccd782b2016-11-28 09:41:46 -0800356
Yifan Hongca890522016-10-12 10:03:41 -0700357 void getServices() {
Yifan Hong8b98d8d2017-02-24 17:06:06 -0800358 manager = IServiceManager::getService();
Steven Moreland01bcb772016-11-08 15:57:25 -0800359
360 // alternatively:
361 // manager = defaultServiceManager()
362
363 ASSERT_NE(manager, nullptr);
364 ASSERT_TRUE(manager->isRemote()); // manager is always remote
365
Yifan Hong8b98d8d2017-02-24 17:06:06 -0800366 tokenManager = ITokenManager::getService();
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800367 ASSERT_NE(tokenManager, nullptr);
368 ASSERT_TRUE(tokenManager->isRemote()); // tokenManager is always remote
369
Steven Moreland2a753532016-12-15 12:37:49 -0800370 ashmemAllocator = IAllocator::getService("ashmem");
371 ASSERT_NE(ashmemAllocator, nullptr);
372 ASSERT_TRUE(ashmemAllocator->isRemote()); // allocator is always remote
373
Yifan Hongc70f0d82016-10-10 14:50:22 -0700374 // getStub is true if we are in passthrough mode to skip checking
375 // binderized server, false for binderized mode.
Yifan Hong1e81c532016-10-18 18:43:46 -0700376
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800377 memoryTest = IMemoryTest::getService("memory", mode == PASSTHROUGH /* getStub */);
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100378 ASSERT_NE(memoryTest, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800379 ASSERT_EQ(memoryTest->isRemote(), mode == BINDERIZED);
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100380
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800381 fetcher = IFetcher::getService("fetcher", mode == PASSTHROUGH /* getStub */);
Yifan Hong1e81c532016-10-18 18:43:46 -0700382 ASSERT_NE(fetcher, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800383 ASSERT_EQ(fetcher->isRemote(), mode == BINDERIZED);
Yifan Hong1e81c532016-10-18 18:43:46 -0700384
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800385 foo = IFoo::getService("foo", mode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700386 ASSERT_NE(foo, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800387 ASSERT_EQ(foo->isRemote(), mode == BINDERIZED);
Yifan Hong1dc87932016-08-19 09:51:01 -0700388
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800389 dyingBaz = IBaz::getService("dyingBaz", mode == PASSTHROUGH /* getStub */);
Martijn Coenen115d4282016-12-19 05:14:04 +0100390 ASSERT_NE(foo, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800391 ASSERT_EQ(foo->isRemote(), mode == BINDERIZED);
Martijn Coenen115d4282016-12-19 05:14:04 +0100392
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800393 bar = IBar::getService("foo", mode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700394 ASSERT_NE(bar, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800395 ASSERT_EQ(bar->isRemote(), mode == BINDERIZED);
Yifan Hong1dc87932016-08-19 09:51:01 -0700396
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800397 graphInterface = IGraph::getService("graph", mode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700398 ASSERT_NE(graphInterface, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800399 ASSERT_EQ(graphInterface->isRemote(), mode == BINDERIZED);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700400
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800401 pointerInterface = IPointer::getService("pointer", mode == PASSTHROUGH /* getStub */);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700402 ASSERT_NE(pointerInterface, nullptr);
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800403 ASSERT_EQ(pointerInterface->isRemote(), mode == BINDERIZED);
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700404
405 // use passthrough mode as the validation object.
406 validationPointerInterface = IPointer::getService("pointer", true /* getStub */);
407 ASSERT_NE(validationPointerInterface, nullptr);
Yifan Hong1dc87932016-08-19 09:51:01 -0700408 }
Yifan Hong1e81c532016-10-18 18:43:46 -0700409
Yifan Hongca890522016-10-12 10:03:41 -0700410 virtual void SetUp() {
411 ALOGI("Environment setup beginning...");
Yifan Hongca890522016-10-12 10:03:41 -0700412 getServices();
Yifan Hong1dc87932016-08-19 09:51:01 -0700413 ALOGI("Environment setup complete.");
414 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700415};
416
Yifan Hongca890522016-10-12 10:03:41 -0700417class HidlTest : public ::testing::Test {
418public:
Steven Moreland01bcb772016-11-08 15:57:25 -0800419 sp<IServiceManager> manager;
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800420 sp<ITokenManager> tokenManager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100421 sp<IAllocator> ashmemAllocator;
422 sp<IMemoryTest> memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700423 sp<IFetcher> fetcher;
Yifan Hongca890522016-10-12 10:03:41 -0700424 sp<IFoo> foo;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800425 sp<IBaz> dyingBaz;
Yifan Hongca890522016-10-12 10:03:41 -0700426 sp<IBar> bar;
Yifan Hongca890522016-10-12 10:03:41 -0700427 sp<IGraph> graphInterface;
428 sp<IPointer> pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700429 sp<IPointer> validationPointerInterface;
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800430 TestMode mode = TestMode::PASSTHROUGH;
Martijn Coenen115d4282016-12-19 05:14:04 +0100431
Yifan Hongca890522016-10-12 10:03:41 -0700432 virtual void SetUp() override {
433 ALOGI("Test setup beginning...");
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800434 manager = gHidlEnvironment->manager;
435 tokenManager = gHidlEnvironment->tokenManager;
436 ashmemAllocator = gHidlEnvironment->ashmemAllocator;
437 memoryTest = gHidlEnvironment->memoryTest;
438 fetcher = gHidlEnvironment->fetcher;
439 foo = gHidlEnvironment->foo;
440 dyingBaz = gHidlEnvironment->dyingBaz;
441 bar = gHidlEnvironment->bar;
442 graphInterface = gHidlEnvironment->graphInterface;
443 pointerInterface = gHidlEnvironment->pointerInterface;
444 validationPointerInterface = gHidlEnvironment->validationPointerInterface;
445 mode = gHidlEnvironment->mode;
Yifan Hongca890522016-10-12 10:03:41 -0700446 ALOGI("Test setup complete");
447 }
448};
449
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800450TEST_F(HidlTest, ToStringTest) {
451 using namespace android::hardware;
452
453 LOG(INFO) << toString(IFoo::Everything{});
454
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100455 // Note that handles don't need to be deleted because MQDescriptor takes ownership
456 // and deletes them when destructed.
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800457 auto handle = native_handle_create(0, 1);
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100458 auto handle2 = native_handle_create(0, 1);
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800459 handle->data[0] = 5;
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100460 handle2->data[0] = 6;
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800461 IFoo::Everything e {
462 .u = {.p = reinterpret_cast<void *>(0x5)},
463 .number = 10,
464 .h = handle,
465 .descSync = {std::vector<GrantorDescriptor>(), handle, 5},
Martijn Coenenf7b596b2017-01-13 14:07:59 +0100466 .descUnsync = {std::vector<GrantorDescriptor>(), handle2, 6},
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800467 .mem = hidl_memory("mymem", handle, 5),
468 .p = reinterpret_cast<void *>(0x6),
469 .vs = {"hello", "world"},
470 .multidimArray = hidl_vec<hidl_string>{"hello", "great", "awesome", "nice"}.data(),
471 .sArray = hidl_vec<hidl_string>{"awesome", "thanks", "you're welcome"}.data(),
472 .anotherStruct = {.first = "first", .last = "last"},
473 .bf = IFoo::BitField::V0 | IFoo::BitField::V2
474 };
475 LOG(INFO) << toString(e);
476 LOG(INFO) << toString(foo);
477 // toString is for debugging purposes only; no good EXPECT
478 // statement can be written here.
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800479}
480
Yifan Hong870d1a72017-02-22 14:24:17 -0800481TEST_F(HidlTest, EnumToStringTest) {
482 using namespace std::string_literals;
483 using ::android::hardware::tests::foo::V1_0::toString;
484 // toString for enum
485 EXPECT_EQ(toString(IFoo::BitField::V0), "V0"s);
486 EXPECT_EQ(toString(static_cast<IFoo::BitField>(0)), "0"s)
487 << "Invalid enum isn't stringified correctly.";
488 EXPECT_EQ(toString(static_cast<IFoo::BitField>(IFoo::BitField::V0 | IFoo::BitField::V2)), "0x5"s)
489 << "Invalid enum isn't stringified correctly.";
490 // dump bitfields
491 EXPECT_EQ(toString<IFoo::BitField>(0 | IFoo::BitField::V0), "V0 (0x1)"s);
492 EXPECT_EQ(toString<IFoo::BitField>(0 | IFoo::BitField::V0 | IFoo::BitField::V2), "V0 | V2 (0x5)"s);
493 EXPECT_EQ(toString<IFoo::BitField>(0xF), "V0 | V1 | V2 | V3 | VALL (0xf)"s);
494 EXPECT_EQ(toString<IFoo::BitField>(0xFF), "V0 | V1 | V2 | V3 | VALL | 0xf0 (0xff)"s);
495}
496
Steven Moreland424a9482017-02-13 19:20:40 -0800497TEST_F(HidlTest, PingTest) {
498 EXPECT_OK(manager->ping());
499}
500
Steven Moreland01bcb772016-11-08 15:57:25 -0800501TEST_F(HidlTest, ServiceListTest) {
502 static const std::set<std::string> binderizedSet = {
503 "android.hardware.tests.pointer@1.0::IPointer/pointer",
504 "android.hardware.tests.bar@1.0::IBar/foo",
505 "android.hardware.tests.inheritance@1.0::IFetcher/fetcher",
Steven Moreland01bcb772016-11-08 15:57:25 -0800506 "android.hardware.tests.inheritance@1.0::IParent/parent",
507 "android.hardware.tests.inheritance@1.0::IParent/child",
508 "android.hardware.tests.inheritance@1.0::IChild/child",
509 "android.hardware.tests.pointer@1.0::IGraph/graph",
510 "android.hardware.tests.inheritance@1.0::IGrandparent/child",
511 "android.hardware.tests.foo@1.0::IFoo/foo",
Martijn Coenenc20e35b2017-03-02 14:59:41 +0100512 "android.hidl.manager@1.0::IServiceManager/default",
Steven Moreland01bcb772016-11-08 15:57:25 -0800513 };
514
515 static const std::set<std::string> passthroughSet = {
Martijn Coenenc20e35b2017-03-02 14:59:41 +0100516 "android.hidl.manager@1.0::IServiceManager/default"
Steven Moreland01bcb772016-11-08 15:57:25 -0800517 };
518
519 std::set<std::string> activeSet;
520
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800521 switch(mode) {
Steven Moreland01bcb772016-11-08 15:57:25 -0800522 case BINDERIZED: {
523 activeSet = binderizedSet;
524 } break;
525
526 case PASSTHROUGH: {
527 activeSet = passthroughSet;
528 } break;
529 default:
530 EXPECT_TRUE(false) << "unrecognized mode";
531 }
532
533 EXPECT_OK(manager->list([&activeSet](const hidl_vec<hidl_string> &registered){
534 std::set<std::string> registeredSet;
535
536 for (size_t i = 0; i < registered.size(); i++) {
537 registeredSet.insert(registered[i]);
538 }
539
540 std::set<std::string> difference;
541 std::set_difference(activeSet.begin(), activeSet.end(),
542 registeredSet.begin(), registeredSet.end(),
543 std::inserter(difference, difference.begin()));
544
545 EXPECT_EQ(difference.size(), 0u) << "service(s) not registered " << to_string(difference);
546 }));
547}
548
549// passthrough TODO(b/32747392)
550TEST_F(HidlTest, ServiceListByInterfaceTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800551 if (mode == BINDERIZED) {
Steven Morelandd39133b2016-11-11 12:30:08 -0800552 EXPECT_OK(manager->listByInterface(IParent::descriptor,
Steven Moreland01bcb772016-11-08 15:57:25 -0800553 [](const hidl_vec<hidl_string> &registered) {
554 std::set<std::string> registeredSet;
555
556 for (size_t i = 0; i < registered.size(); i++) {
557 registeredSet.insert(registered[i]);
558 }
559
560 std::set<std::string> activeSet = {
561 "parent", "child"
562 };
563 std::set<std::string> difference;
564 std::set_difference(activeSet.begin(), activeSet.end(),
565 registeredSet.begin(), registeredSet.end(),
566 std::inserter(difference, difference.begin()));
567
568 EXPECT_EQ(difference.size(), 0u) << "service(s) not registered " << to_string(difference);
569 }));
570 }
571}
572
573// passthrough TODO(b/32747392)
574TEST_F(HidlTest, ServiceParentTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800575 if (mode == BINDERIZED) {
Steven Moreland01bcb772016-11-08 15:57:25 -0800576 sp<IParent> parent = IParent::getService("child");
577
578 EXPECT_NE(parent, nullptr);
579 }
580}
581
Steven Moreland0693f312016-11-09 15:06:14 -0800582// passthrough TODO(b/32747392)
583TEST_F(HidlTest, ServiceNotificationTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800584 if (mode == BINDERIZED) {
Steven Moreland0693f312016-11-09 15:06:14 -0800585 ServiceNotification *notification = new ServiceNotification();
586
587 std::string instanceName = "test-instance";
Steven Moreland21aa43c2017-03-07 17:15:24 -0800588 EXPECT_TRUE(IParent::registerForNotifications(instanceName, notification));
Steven Moreland0693f312016-11-09 15:06:14 -0800589
Steven Moreland21aa43c2017-03-07 17:15:24 -0800590 EXPECT_EQ(::android::OK, (new SimpleChild())->registerAsService(instanceName));
591 EXPECT_EQ(::android::OK, (new SimpleParent())->registerAsService(instanceName));
Steven Moreland0693f312016-11-09 15:06:14 -0800592
593 std::unique_lock<std::mutex> lock(notification->mutex);
594
595 notification->condition.wait_for(
596 lock,
597 std::chrono::milliseconds(2),
598 [&notification]() {
Steven Moreland21aa43c2017-03-07 17:15:24 -0800599 return notification->getRegistrations().size() >= 2;
Steven Moreland0693f312016-11-09 15:06:14 -0800600 });
601
602 std::vector<std::string> registrations = notification->getRegistrations();
603
Steven Moreland21aa43c2017-03-07 17:15:24 -0800604 EXPECT_EQ(registrations.size(), 2u);
Steven Moreland0693f312016-11-09 15:06:14 -0800605
606 EXPECT_EQ(to_string(registrations.data(), registrations.size()),
Steven Moreland21aa43c2017-03-07 17:15:24 -0800607 std::string("['") + IParent::descriptor + "/" + instanceName +
608 "', '" + IParent::descriptor + "/" + instanceName + "']");
Steven Moreland0693f312016-11-09 15:06:14 -0800609 }
610}
611
612// passthrough TODO(b/32747392)
613TEST_F(HidlTest, ServiceAllNotificationTest) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800614 if (mode == BINDERIZED) {
Steven Moreland0693f312016-11-09 15:06:14 -0800615 ServiceNotification *notification = new ServiceNotification();
616
617 std::string instanceOne = "test-instance-one";
618 std::string instanceTwo = "test-instance-two";
619 EXPECT_TRUE(ISimple::registerForNotifications("", notification));
620
Steven Moreland0693f312016-11-09 15:06:14 -0800621 Simple* instanceA = new Simple(1);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800622 EXPECT_EQ(::android::OK, instanceA->registerAsService(instanceOne));
Steven Moreland0693f312016-11-09 15:06:14 -0800623 Simple* instanceB = new Simple(2);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800624 EXPECT_EQ(::android::OK, instanceB->registerAsService(instanceTwo));
Steven Moreland0693f312016-11-09 15:06:14 -0800625
626 std::unique_lock<std::mutex> lock(notification->mutex);
627
628 notification->condition.wait_for(
629 lock,
630 std::chrono::milliseconds(2),
631 [&notification]() {
632 return notification->getRegistrations().size() >= 2;
633 });
634
635 std::vector<std::string> registrations = notification->getRegistrations();
636 std::sort(registrations.begin(), registrations.end());
637
638 EXPECT_EQ(registrations.size(), 2u);
639
Steven Morelandd39133b2016-11-11 12:30:08 -0800640 std::string descriptor = ISimple::descriptor;
Steven Moreland0693f312016-11-09 15:06:14 -0800641
642 EXPECT_EQ(to_string(registrations.data(), registrations.size()),
643 "['" + descriptor + "/" + instanceOne + "', '"
644 + descriptor + "/" + instanceTwo + "']");
645 }
646}
647
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800648TEST_F(HidlTest, TestToken) {
649 Return<uint64_t> ret = tokenManager->createToken(manager);
650 EXPECT_OK(ret);
651 uint64_t token = ret;
652
Martijn Coenenb40ef022017-01-02 15:21:46 +0100653 Return<sp<IBase>> retService = tokenManager->get(token);
654 EXPECT_OK(retService);
655 if (retService.isOk()) {
656 sp<IBase> service = retService;
657 EXPECT_NE(nullptr, service.get());
658 sp<IServiceManager> retManager = IServiceManager::castFrom(service);
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800659
660 // TODO(b/33818800): should have only one Bp per process
661 // EXPECT_EQ(manager, retManager);
662
663 EXPECT_NE(nullptr, retManager.get());
Martijn Coenenb40ef022017-01-02 15:21:46 +0100664 }
Steven Morelandaa2b83a2016-12-21 15:52:11 -0800665
666 Return<bool> unregisterRet = tokenManager->unregister(token);
667
668 EXPECT_OK(unregisterRet);
669 if (unregisterRet.isOk()) {
670 EXPECT_TRUE(ret);
671 }
672}
673
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100674TEST_F(HidlTest, TestSharedMemory) {
675 const uint8_t kValue = 0xCA;
676 hidl_memory mem_copy;
677 EXPECT_OK(ashmemAllocator->allocate(1024, [&](bool success, const hidl_memory& mem) {
678 EXPECT_EQ(success, true);
679
680 sp<IMemory> memory = mapMemory(mem);
681
682 EXPECT_NE(memory, nullptr);
683
684 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
685 EXPECT_NE(data, nullptr);
686
687 EXPECT_EQ(memory->getSize(), mem.size());
688
689 memory->update();
690 memset(data, 0, memory->getSize());
691 memory->commit();
692
693 mem_copy = mem;
694 memoryTest->fillMemory(mem, kValue);
695 for (size_t i = 0; i < mem.size(); i++) {
696 EXPECT_EQ(kValue, data[i]);
697 }
698 }));
699
700 // Test the memory persists after the call
701 sp<IMemory> memory = mapMemory(mem_copy);
702
703 EXPECT_NE(memory, nullptr);
704
705 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
706 EXPECT_NE(data, nullptr);
707
708 for (size_t i = 0; i < mem_copy.size(); i++) {
709 EXPECT_EQ(kValue, data[i]);
710 }
Hridya Valsaraju8951e552017-02-27 12:37:20 -0800711
712 hidl_memory mem_move(std::move(mem_copy));
713 ASSERT_EQ(nullptr, mem_copy.handle());
714 ASSERT_EQ(0UL, mem_copy.size());
715 ASSERT_EQ("", mem_copy.name());
716
717 memory.clear();
718 memory = mapMemory(mem_move);
719
720 EXPECT_NE(memory, nullptr);
721
722 data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
723 EXPECT_NE(data, nullptr);
724
725 for (size_t i = 0; i < mem_move.size(); i++) {
726 EXPECT_EQ(kValue, data[i]);
727 }
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100728}
729
Steven Morelandf31f1652017-01-06 18:33:42 -0800730TEST_F(HidlTest, NullSharedMemory) {
731 hidl_memory memory{};
732
733 EXPECT_EQ(nullptr, memory.handle());
734
735 EXPECT_OK(memoryTest->haveSomeMemory(memory, [&](const hidl_memory &mem) {
736 EXPECT_EQ(nullptr, mem.handle());
737 }));
738}
739
Yifan Hongc75fd472017-01-11 12:37:31 -0800740TEST_F(HidlTest, FooGetDescriptorTest) {
741 EXPECT_OK(foo->interfaceDescriptor([&] (const auto &desc) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -0800742 EXPECT_EQ(desc, mode == BINDERIZED
Yifan Hongc75fd472017-01-11 12:37:31 -0800743 ? IBar::descriptor // service is actually IBar in binderized mode
744 : IFoo::descriptor); // dlopened, so service is IFoo
745 }));
746}
747
Yifan Hong1dc87932016-08-19 09:51:01 -0700748TEST_F(HidlTest, FooDoThisTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700749 ALOGI("CLIENT call doThis.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700750 EXPECT_OK(foo->doThis(1.0f));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700751 ALOGI("CLIENT doThis returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700752}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700753
Yifan Hong1dc87932016-08-19 09:51:01 -0700754TEST_F(HidlTest, FooDoThatAndReturnSomethingTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700755 ALOGI("CLIENT call doThatAndReturnSomething.");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000756 int32_t result = foo->doThatAndReturnSomething(2.0f);
757 ALOGI("CLIENT doThatAndReturnSomething returned %d.", result);
758 EXPECT_EQ(result, 666);
Yifan Hong1dc87932016-08-19 09:51:01 -0700759}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700760
Yifan Hong1dc87932016-08-19 09:51:01 -0700761TEST_F(HidlTest, FooDoQuiteABitTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700762 ALOGI("CLIENT call doQuiteABit");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000763 double something = foo->doQuiteABit(1, 2, 3.0f, 4.0);
764 ALOGI("CLIENT doQuiteABit returned %f.", something);
765 EXPECT_DOUBLE_EQ(something, 666.5);
Yifan Hong1dc87932016-08-19 09:51:01 -0700766}
767
768TEST_F(HidlTest, FooDoSomethingElseTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700769
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700770 ALOGI("CLIENT call doSomethingElse");
Andreas Huberf03332a2016-09-22 15:35:43 -0700771 hidl_array<int32_t, 15> param;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700772 for (size_t i = 0; i < sizeof(param) / sizeof(param[0]); ++i) {
773 param[i] = i;
774 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700775 EXPECT_OK(foo->doSomethingElse(param, [&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700776 ALOGI("CLIENT doSomethingElse returned %s.",
Andreas Huber5e44a292016-09-27 14:52:39 -0700777 to_string(something).c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700778 int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
779 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 -0700780 EXPECT_TRUE(isArrayEqual(something, expect, 32));
Yifan Hong1dc87932016-08-19 09:51:01 -0700781 }));
782}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700783
Yifan Hong1dc87932016-08-19 09:51:01 -0700784TEST_F(HidlTest, FooDoStuffAndReturnAStringTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700785 ALOGI("CLIENT call doStuffAndReturnAString");
Yifan Hong1dc87932016-08-19 09:51:01 -0700786 EXPECT_OK(foo->doStuffAndReturnAString([&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700787 ALOGI("CLIENT doStuffAndReturnAString returned '%s'.",
Andreas Huber9cd48d02016-08-03 14:25:59 -0700788 something.c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700789 EXPECT_STREQ(something.c_str(), "Hello, world");
Steven Moreland6e3f9f42017-01-30 11:41:49 -0800790 EXPECT_EQ(strlen("Hello, world"), something.size());
Yifan Hong1dc87932016-08-19 09:51:01 -0700791 }));
792}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700793
Yifan Hong1dc87932016-08-19 09:51:01 -0700794TEST_F(HidlTest, FooMapThisVectorTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700795 hidl_vec<int32_t> vecParam;
796 vecParam.resize(10);
797 for (size_t i = 0; i < 10; ++i) {
798 vecParam[i] = i;
799 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700800 EXPECT_OK(foo->mapThisVector(vecParam, [&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700801 ALOGI("CLIENT mapThisVector returned %s.",
Andreas Huber5e44a292016-09-27 14:52:39 -0700802 to_string(something).c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700803 int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
Steven Morelande70455b2016-09-14 15:46:36 -0700804 EXPECT_TRUE(isArrayEqual(something, expect, something.size()));
Yifan Hong1dc87932016-08-19 09:51:01 -0700805 }));
806}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700807
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800808TEST_F(HidlTest, WrapTest) {
Yifan Hong01e7cde2017-01-09 17:45:45 -0800809 using ::android::hardware::tests::foo::V1_0::BnHwSimple;
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800810 using ::android::hardware::tests::foo::V1_0::BsSimple;
Yifan Hong01e7cde2017-01-09 17:45:45 -0800811 using ::android::hardware::tests::foo::V1_0::BpHwSimple;
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000812 using ::android::hardware::details::HidlInstrumentor;
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800813 nsecs_t now;
814 int i = 0;
815
816 now = systemTime();
Yifan Hong01e7cde2017-01-09 17:45:45 -0800817 new BnHwSimple(new Simple(1));
818 EXPECT_LT(systemTime() - now, 2000000) << " for BnHwSimple(nonnull)";
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800819
820 now = systemTime();
Yifan Hong01e7cde2017-01-09 17:45:45 -0800821 new BnHwSimple(nullptr);
822 EXPECT_LT(systemTime() - now, 2000000) << " for BnHwSimple(null)";
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800823
824 now = systemTime();
825 new BsSimple(new Simple(1));
826 EXPECT_LT(systemTime() - now, 2000000) << " for BsSimple(nonnull)";
827
828 now = systemTime();
829 new BsSimple(nullptr);
830 EXPECT_LT(systemTime() - now, 2000000) << " for BsSimple(null)";
831
832 now = systemTime();
Yifan Hong01e7cde2017-01-09 17:45:45 -0800833 new BpHwSimple(nullptr);
834 EXPECT_LT(systemTime() - now, 2000000) << " for BpHwSimple(null)";
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800835
836 now = systemTime();
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000837 new ::android::hardware::details::HidlInstrumentor("", "");
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800838 EXPECT_LT(systemTime() - now, 2000000) << " for HidlInstrumentor";
839
840 now = systemTime();
841 i++;
842 EXPECT_LT(systemTime() - now, 1000) << " for nothing";
843}
844
Yifan Hong1dc87932016-08-19 09:51:01 -0700845TEST_F(HidlTest, FooCallMeTest) {
Yifan Hong11992a62016-11-09 18:07:40 -0800846
847 sp<IFooCallback> fooCb = new FooCallback();
848
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700849 ALOGI("CLIENT call callMe.");
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700850 // callMe is oneway, should return instantly.
851 nsecs_t now;
852 now = systemTime();
853 EXPECT_OK(foo->callMe(fooCb));
Yifan Hong3eac8a32016-10-11 10:02:59 -0700854 EXPECT_LT(systemTime() - now, ONEWAY_TOLERANCE_NS);
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700855 ALOGI("CLIENT callMe returned.");
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700856
857 // Bar::callMe will invoke three methods on FooCallback; one will return
858 // right away (even though it is a two-way method); the second one will
Yifan Hong3eac8a32016-10-11 10:02:59 -0700859 // block Bar for DELAY_S seconds, and the third one will return
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700860 // to Bar right away (is oneway) but will itself block for DELAY_S seconds.
861 // We need a way to make sure that these three things have happened within
862 // 2*DELAY_S seconds plus some small tolerance.
863 //
864 // Method FooCallback::reportResults() takes a timeout parameter. It blocks for
865 // that length of time, while waiting for the three methods above to
866 // complete. It returns the information of whether each method was invoked,
867 // as well as how long the body of the method took to execute. We verify
868 // the information returned by reportResults() against the timeout we pass (which
869 // is long enough for the method bodies to execute, plus tolerance), and
870 // verify that eachof them executed, as expected, and took the length of
871 // time to execute that we also expect.
872
Yifan Hongfcf94e42016-12-07 15:49:51 -0800873 const nsecs_t waitNs =
874 3 * DELAY_NS + TOLERANCE_NS;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700875 const nsecs_t reportResultsNs =
Yifan Hong3eac8a32016-10-11 10:02:59 -0700876 2 * DELAY_NS + TOLERANCE_NS;
Andreas Huber03866f52016-08-30 14:19:52 -0700877
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700878 ALOGI("CLIENT: Waiting for up to %" PRId64 " seconds.",
Yifan Hongfcf94e42016-12-07 15:49:51 -0800879 nanoseconds_to_seconds(waitNs));
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700880
Yifan Hongfcf94e42016-12-07 15:49:51 -0800881 fooCb->reportResults(waitNs,
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700882 [&](int64_t timeLeftNs,
Andreas Huberf03332a2016-09-22 15:35:43 -0700883 const hidl_array<IFooCallback::InvokeInfo, 3> &invokeResults) {
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700884 ALOGI("CLIENT: FooCallback::reportResults() is returning data.");
885 ALOGI("CLIENT: Waited for %" PRId64 " milliseconds.",
Yifan Hongfcf94e42016-12-07 15:49:51 -0800886 nanoseconds_to_milliseconds(waitNs - timeLeftNs));
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700887
Yifan Hongfcf94e42016-12-07 15:49:51 -0800888 EXPECT_LE(waitNs - timeLeftNs, reportResultsNs)
889 << "waited for "
890 << (timeLeftNs >= 0 ? "" : "more than ")
891 << (timeLeftNs >= 0 ? (waitNs - timeLeftNs) : waitNs)
892 << "ns, expect to finish in "
893 << reportResultsNs << " ns";
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700894
895 // two-way method, was supposed to return right away
896 EXPECT_TRUE(invokeResults[0].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700897 EXPECT_LE(invokeResults[0].timeNs, invokeResults[0].callerBlockedNs);
898 EXPECT_LE(invokeResults[0].callerBlockedNs, TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700899 // two-way method, was supposed to block caller for DELAY_NS
900 EXPECT_TRUE(invokeResults[1].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700901 EXPECT_LE(invokeResults[1].timeNs, invokeResults[1].callerBlockedNs);
902 EXPECT_LE(invokeResults[1].callerBlockedNs,
903 DELAY_NS + TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700904 // one-way method, do not block caller, but body was supposed to block for DELAY_NS
905 EXPECT_TRUE(invokeResults[2].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700906 EXPECT_LE(invokeResults[2].callerBlockedNs, ONEWAY_TOLERANCE_NS);
907 EXPECT_LE(invokeResults[2].timeNs, DELAY_NS + TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700908 });
909}
910
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000911
912
Yifan Hong1dc87932016-08-19 09:51:01 -0700913TEST_F(HidlTest, FooUseAnEnumTest) {
914 ALOGI("CLIENT call useAnEnum.");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000915 IFoo::SomeEnum sleepy = foo->useAnEnum(IFoo::SomeEnum::quux);
916 ALOGI("CLIENT useAnEnum returned %u", (unsigned)sleepy);
917 EXPECT_EQ(sleepy, IFoo::SomeEnum::goober);
Yifan Hong1dc87932016-08-19 09:51:01 -0700918}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700919
Yifan Hong1dc87932016-08-19 09:51:01 -0700920TEST_F(HidlTest, FooHaveAGooberTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700921 hidl_vec<IFoo::Goober> gooberVecParam;
922 gooberVecParam.resize(2);
923 gooberVecParam[0].name = "Hello";
924 gooberVecParam[1].name = "World";
925
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700926 ALOGI("CLIENT call haveAGooberVec.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700927 EXPECT_OK(foo->haveAGooberVec(gooberVecParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700928 ALOGI("CLIENT haveAGooberVec returned.");
Andreas Huber9cd48d02016-08-03 14:25:59 -0700929
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700930 ALOGI("CLIENT call haveaGoober.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700931 EXPECT_OK(foo->haveAGoober(gooberVecParam[0]));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700932 ALOGI("CLIENT haveaGoober returned.");
933
934 ALOGI("CLIENT call haveAGooberArray.");
Andreas Huberf03332a2016-09-22 15:35:43 -0700935 hidl_array<IFoo::Goober, 20> gooberArrayParam;
Yifan Hong1dc87932016-08-19 09:51:01 -0700936 EXPECT_OK(foo->haveAGooberArray(gooberArrayParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700937 ALOGI("CLIENT haveAGooberArray returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700938}
Steven Moreland88ca4512016-08-11 11:24:10 -0700939
Yifan Hong1dc87932016-08-19 09:51:01 -0700940TEST_F(HidlTest, FooHaveATypeFromAnotherFileTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700941 ALOGI("CLIENT call haveATypeFromAnotherFile.");
Steven Moreland88ca4512016-08-11 11:24:10 -0700942 Abc abcParam{};
943 abcParam.x = "alphabet";
944 abcParam.y = 3.14f;
Yifan Honga65bb2c2016-10-27 13:17:14 -0700945 native_handle_t *handle = native_handle_create(0, 0);
946 abcParam.z = handle;
Yifan Hong1dc87932016-08-19 09:51:01 -0700947 EXPECT_OK(foo->haveATypeFromAnotherFile(abcParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700948 ALOGI("CLIENT haveATypeFromAnotherFile returned.");
Yifan Honga65bb2c2016-10-27 13:17:14 -0700949 native_handle_delete(handle);
Steven Moreland88ca4512016-08-11 11:24:10 -0700950 abcParam.z = NULL;
Yifan Hong1dc87932016-08-19 09:51:01 -0700951}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700952
Yifan Hong1dc87932016-08-19 09:51:01 -0700953TEST_F(HidlTest, FooHaveSomeStringsTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700954 ALOGI("CLIENT call haveSomeStrings.");
Andreas Huberf03332a2016-09-22 15:35:43 -0700955 hidl_array<hidl_string, 3> stringArrayParam;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700956 stringArrayParam[0] = "What";
957 stringArrayParam[1] = "a";
958 stringArrayParam[2] = "disaster";
Steven Moreland67f67b42016-09-29 08:59:02 -0700959 EXPECT_OK(foo->haveSomeStrings(
960 stringArrayParam,
961 [&](const auto &out) {
962 ALOGI("CLIENT haveSomeStrings returned %s.",
963 to_string(out).c_str());
964
965 EXPECT_EQ(to_string(out), "['Hello', 'World']");
966 }));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700967 ALOGI("CLIENT haveSomeStrings returned.");
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, FooHaveAStringVecTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700971 ALOGI("CLIENT call haveAStringVec.");
Andreas Huber9cd48d02016-08-03 14:25:59 -0700972 hidl_vec<hidl_string> stringVecParam;
973 stringVecParam.resize(3);
974 stringVecParam[0] = "What";
975 stringVecParam[1] = "a";
976 stringVecParam[2] = "disaster";
Steven Moreland67f67b42016-09-29 08:59:02 -0700977 EXPECT_OK(foo->haveAStringVec(
978 stringVecParam,
979 [&](const auto &out) {
980 ALOGI("CLIENT haveAStringVec returned %s.",
981 to_string(out).c_str());
982
983 EXPECT_EQ(to_string(out), "['Hello', 'World']");
984 }));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700985 ALOGI("CLIENT haveAStringVec returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700986}
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700987
Andreas Huberf9d49f12016-09-12 14:58:36 -0700988TEST_F(HidlTest, FooTransposeMeTest) {
Andreas Huberf03332a2016-09-22 15:35:43 -0700989 hidl_array<float, 3, 5> in;
Andreas Huberf9d49f12016-09-12 14:58:36 -0700990 float k = 1.0f;
991 for (size_t i = 0; i < 3; ++i) {
992 for (size_t j = 0; j < 5; ++j, ++k) {
993 in[i][j] = k;
994 }
995 }
996
Andreas Huberf03332a2016-09-22 15:35:43 -0700997 ALOGI("CLIENT call transposeMe(%s).", to_string(in).c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -0700998
999 EXPECT_OK(foo->transposeMe(
Andreas Huberf03332a2016-09-22 15:35:43 -07001000 in,
Andreas Huberf9d49f12016-09-12 14:58:36 -07001001 [&](const auto &out) {
1002 ALOGI("CLIENT transposeMe returned %s.",
Andreas Huberf03332a2016-09-22 15:35:43 -07001003 to_string(out).c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001004
1005 for (size_t i = 0; i < 3; ++i) {
1006 for (size_t j = 0; j < 5; ++j) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001007 EXPECT_EQ(out[j][i], in[i][j]);
Andreas Huberf9d49f12016-09-12 14:58:36 -07001008 }
1009 }
1010 }));
1011}
1012
1013TEST_F(HidlTest, FooCallingDrWhoTest) {
1014 IFoo::MultiDimensional in;
1015
1016 size_t k = 0;
1017 for (size_t i = 0; i < 5; ++i) {
1018 for (size_t j = 0; j < 3; ++j, ++k) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001019 in.quuxMatrix[i][j].first = ("First " + std::to_string(k)).c_str();
1020 in.quuxMatrix[i][j].last = ("Last " + std::to_string(15-k)).c_str();
Andreas Huberf9d49f12016-09-12 14:58:36 -07001021 }
1022 }
1023
1024 ALOGI("CLIENT call callingDrWho(%s).",
1025 MultiDimensionalToString(in).c_str());
1026
1027 EXPECT_OK(foo->callingDrWho(
1028 in,
1029 [&](const auto &out) {
1030 ALOGI("CLIENT callingDrWho returned %s.",
1031 MultiDimensionalToString(out).c_str());
1032
Andreas Huber709b62d2016-09-19 11:21:18 -07001033 size_t k = 0;
Andreas Huberf9d49f12016-09-12 14:58:36 -07001034 for (size_t i = 0; i < 5; ++i) {
Andreas Huber709b62d2016-09-19 11:21:18 -07001035 for (size_t j = 0; j < 3; ++j, ++k) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001036 EXPECT_STREQ(
1037 out.quuxMatrix[i][j].first.c_str(),
1038 in.quuxMatrix[4 - i][2 - j].last.c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001039
1040 EXPECT_STREQ(
Andreas Huberf03332a2016-09-22 15:35:43 -07001041 out.quuxMatrix[i][j].last.c_str(),
1042 in.quuxMatrix[4 - i][2 - j].first.c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -07001043 }
1044 }
1045 }));
1046}
1047
Andreas Huber709b62d2016-09-19 11:21:18 -07001048static std::string numberToEnglish(int x) {
1049 static const char *const kDigits[] = {
1050 "zero",
1051 "one",
1052 "two",
1053 "three",
1054 "four",
1055 "five",
1056 "six",
1057 "seven",
1058 "eight",
1059 "nine",
1060 };
1061
1062 if (x < 0) {
1063 return "negative " + numberToEnglish(-x);
1064 }
1065
1066 if (x < 10) {
1067 return kDigits[x];
1068 }
1069
1070 if (x <= 15) {
1071 static const char *const kSpecialTens[] = {
1072 "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
1073 };
1074
1075 return kSpecialTens[x - 10];
1076 }
1077
1078 if (x < 20) {
1079 return std::string(kDigits[x % 10]) + "teen";
1080 }
1081
1082 if (x < 100) {
1083 static const char *const kDecades[] = {
1084 "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
1085 "eighty", "ninety",
1086 };
1087
1088 return std::string(kDecades[x / 10 - 2]) + kDigits[x % 10];
1089 }
1090
1091 return "positively huge!";
1092}
1093
1094TEST_F(HidlTest, FooTransposeTest) {
1095 IFoo::StringMatrix5x3 in;
1096
Andreas Huber709b62d2016-09-19 11:21:18 -07001097 for (int i = 0; i < 5; ++i) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001098 for (int j = 0; j < 3; ++j) {
1099 in.s[i][j] = numberToEnglish(3 * i + j + 1).c_str();
Andreas Huber709b62d2016-09-19 11:21:18 -07001100 }
1101 }
1102
1103 EXPECT_OK(foo->transpose(
1104 in,
1105 [&](const auto &out) {
1106 EXPECT_EQ(
1107 to_string(out),
1108 "[['one', 'four', 'seven', 'ten', 'thirteen'], "
1109 "['two', 'five', 'eight', 'eleven', 'fourteen'], "
1110 "['three', 'six', 'nine', 'twelve', 'fifteen']]");
1111 }));
1112}
1113
1114TEST_F(HidlTest, FooTranspose2Test) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001115 hidl_array<hidl_string, 5, 3> in;
Andreas Huber709b62d2016-09-19 11:21:18 -07001116
Andreas Huber709b62d2016-09-19 11:21:18 -07001117 for (int i = 0; i < 5; ++i) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001118 for (int j = 0; j < 3; ++j) {
1119 in[i][j] = numberToEnglish(3 * i + j + 1).c_str();
Andreas Huber709b62d2016-09-19 11:21:18 -07001120 }
1121 }
1122
1123 EXPECT_OK(foo->transpose2(
1124 in,
1125 [&](const auto &out) {
1126 EXPECT_EQ(
Andreas Huberf03332a2016-09-22 15:35:43 -07001127 to_string(out),
Andreas Huber709b62d2016-09-19 11:21:18 -07001128 "[['one', 'four', 'seven', 'ten', 'thirteen'], "
1129 "['two', 'five', 'eight', 'eleven', 'fourteen'], "
1130 "['three', 'six', 'nine', 'twelve', 'fifteen']]");
1131 }));
1132}
1133
Yifan Hongb725d672016-10-10 10:14:15 -07001134TEST_F(HidlTest, FooNullNativeHandleTest) {
Martijn Coenen9d97da22017-01-12 17:29:47 +01001135 Abc xyz;
1136 xyz.z = nullptr;
1137 EXPECT_OK(bar->expectNullHandle(nullptr, xyz, [](bool hIsNull, bool xyzHasNull) {
1138 EXPECT_TRUE(hIsNull);
1139 EXPECT_TRUE(xyzHasNull);
1140 }));
Yifan Hongb725d672016-10-10 10:14:15 -07001141}
Steven Moreland67f67b42016-09-29 08:59:02 -07001142
Martijn Coenene1638232016-10-26 12:51:34 +02001143TEST_F(HidlTest, FooNullCallbackTest) {
1144 EXPECT_OK(foo->echoNullInterface(nullptr,
1145 [](const auto receivedNull, const auto &intf) {
1146 EXPECT_TRUE(receivedNull);
1147 EXPECT_EQ(intf, nullptr);
1148 }));
1149}
1150
Steven Moreland67f67b42016-09-29 08:59:02 -07001151TEST_F(HidlTest, FooNonNullCallbackTest) {
1152 hidl_array<hidl_string, 5, 3> in;
1153
1154 EXPECT_FAIL(foo->transpose2(in, nullptr /* _hidl_cb */));
1155}
1156
Andreas Huber5e44a292016-09-27 14:52:39 -07001157TEST_F(HidlTest, FooSendVecTest) {
1158 hidl_vec<uint8_t> in;
1159 in.resize(16);
1160 for (size_t i = 0; i < in.size(); ++i) {
1161 in[i] = i;
1162 }
1163
1164 EXPECT_OK(foo->sendVec(
1165 in,
1166 [&](const auto &out) {
1167 EXPECT_EQ(to_string(in), to_string(out));
1168 }));
1169}
1170
Martijn Coenenafb30cb2017-01-13 00:24:02 +01001171TEST_F(HidlTest, FooSendEmptyVecTest) {
1172 hidl_vec<uint8_t> in;
1173 EXPECT_OK(foo->sendVec(
1174 in,
1175 [&](const auto &out) {
1176 EXPECT_EQ(out.size(), 0u);
1177 EXPECT_EQ(to_string(in), to_string(out));
1178 }));
1179}
1180
Andreas Huber86a112b2016-10-19 14:25:16 -07001181TEST_F(HidlTest, FooHaveAVectorOfInterfacesTest) {
1182 hidl_vec<sp<ISimple> > in;
1183 in.resize(16);
1184 for (size_t i = 0; i < in.size(); ++i) {
1185 in[i] = new Simple(i);
1186 }
1187
1188 EXPECT_OK(foo->haveAVectorOfInterfaces(
1189 in,
1190 [&](const auto &out) {
1191 EXPECT_EQ(in.size(), out.size());
1192 for (size_t i = 0; i < in.size(); ++i) {
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001193 int32_t inCookie = in[i]->getCookie();
1194 int32_t outCookie = out[i]->getCookie();
1195 EXPECT_EQ(inCookie, outCookie);
Andreas Huber86a112b2016-10-19 14:25:16 -07001196 }
1197 }));
1198}
1199
1200TEST_F(HidlTest, FooHaveAVectorOfGenericInterfacesTest) {
Andreas Huber86a112b2016-10-19 14:25:16 -07001201
Yifan Hongc8934042016-11-17 17:10:52 -08001202 hidl_vec<sp<::android::hidl::base::V1_0::IBase> > in;
Andreas Huber86a112b2016-10-19 14:25:16 -07001203 in.resize(16);
1204 for (size_t i = 0; i < in.size(); ++i) {
Yifan Hongc8934042016-11-17 17:10:52 -08001205 sp<ISimple> s = new Simple(i);
1206 in[i] = s;
Andreas Huber86a112b2016-10-19 14:25:16 -07001207 }
1208
1209 EXPECT_OK(foo->haveAVectorOfGenericInterfaces(
1210 in,
1211 [&](const auto &out) {
1212 EXPECT_EQ(in.size(), out.size());
Yifan Hongc8934042016-11-17 17:10:52 -08001213
Yifan Hongfbcdc802017-02-22 18:12:48 -08001214 EXPECT_OK(out[0]->interfaceDescriptor([](const auto &name) {
1215 ASSERT_STREQ(name.c_str(), ISimple::descriptor);
Yifan Hongc8934042016-11-17 17:10:52 -08001216 }));
Andreas Huber86a112b2016-10-19 14:25:16 -07001217 for (size_t i = 0; i < in.size(); ++i) {
Yifan Hongc8934042016-11-17 17:10:52 -08001218 sp<ISimple> inSimple = ISimple::castFrom(in[i]);
1219 sp<ISimple> outSimple = ISimple::castFrom(out[i]);
1220
1221 ASSERT_NE(inSimple.get(), nullptr);
1222 ASSERT_NE(outSimple.get(), nullptr);
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001223 EXPECT_EQ(in[i], inSimple.get()); // pointers must be equal!
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001224 int32_t inCookie = inSimple->getCookie();
1225 int32_t outCookie = outSimple->getCookie();
1226 EXPECT_EQ(inCookie, outCookie);
Andreas Huber86a112b2016-10-19 14:25:16 -07001227 }
1228 }));
1229}
1230
Yifan Hong8c48ad72016-10-14 11:34:59 -07001231TEST_F(HidlTest, FooStructEmbeddedHandleTest) {
1232 EXPECT_OK(foo->createMyHandle([&](const auto &myHandle) {
1233 EXPECT_EQ(myHandle.guard, 666);
Martijn Coenen2f69a5b2016-11-18 15:26:38 +01001234 const native_handle_t* handle = myHandle.h.getNativeHandle();
1235 EXPECT_EQ(handle->numInts, 10);
1236 EXPECT_EQ(handle->numFds, 0);
Yifan Hong8c48ad72016-10-14 11:34:59 -07001237 int data[] = {2,3,5,7,11,13,17,19,21,23};
Martijn Coenen2f69a5b2016-11-18 15:26:38 +01001238 EXPECT_ARRAYEQ(handle->data, data, 10);
Yifan Hong8c48ad72016-10-14 11:34:59 -07001239 }));
1240
1241 EXPECT_OK(foo->closeHandles());
1242}
1243
1244TEST_F(HidlTest, FooHandleVecTest) {
1245 EXPECT_OK(foo->createHandles(3, [&](const auto &handles) {
1246 EXPECT_EQ(handles.size(), 3ull);
1247 int data[] = {2,3,5,7,11,13,17,19,21,23};
1248 for (size_t i = 0; i < 3; i++) {
1249 const native_handle_t *h = handles[i];
1250 EXPECT_EQ(h->numInts, 10) << " for element " << i;
1251 EXPECT_EQ(h->numFds, 0) << " for element " << i;
1252 EXPECT_ARRAYEQ(h->data, data, 10);
1253 }
1254 }));
1255
1256 EXPECT_OK(foo->closeHandles());
1257}
1258
Martijn Coenen115d4282016-12-19 05:14:04 +01001259struct HidlDeathRecipient : hidl_death_recipient {
1260 std::mutex mutex;
1261 std::condition_variable condition;
1262 wp<IBase> who;
1263 bool fired = false;
1264 uint64_t cookie = 0;
1265
1266 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who) {
1267 std::unique_lock<std::mutex> lock(mutex);
1268 fired = true;
1269 this->cookie = cookie;
1270 this->who = who;
1271 condition.notify_one();
1272 };
1273};
1274
1275TEST_F(HidlTest, DeathRecipientTest) {
Martijn Coenen115d4282016-12-19 05:14:04 +01001276 sp<HidlDeathRecipient> recipient = new HidlDeathRecipient();
1277 sp<HidlDeathRecipient> recipient2 = new HidlDeathRecipient();
1278
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001279 EXPECT_TRUE(dyingBaz->linkToDeath(recipient, 0x1481));
1280 EXPECT_TRUE(dyingBaz->linkToDeath(recipient2, 0x2592));
1281 EXPECT_TRUE(dyingBaz->unlinkToDeath(recipient2));
Martijn Coenen115d4282016-12-19 05:14:04 +01001282
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001283 if (mode != BINDERIZED) {
Martijn Coenen115d4282016-12-19 05:14:04 +01001284 // Passthrough doesn't fire, nor does it keep state of
1285 // registered death recipients (so it won't fail unlinking
1286 // the same recipient twice).
1287 return;
1288 }
1289
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001290 EXPECT_FALSE(dyingBaz->unlinkToDeath(recipient2));
1291 auto ret = dyingBaz->dieNow();
1292 if (!ret.isOk()) {
1293 //do nothing, this is expected
1294 }
Martijn Coenen115d4282016-12-19 05:14:04 +01001295
1296 std::unique_lock<std::mutex> lock(recipient->mutex);
1297 recipient->condition.wait_for(lock, std::chrono::milliseconds(1000), [&recipient]() {
1298 return recipient->fired;
1299 });
1300 EXPECT_TRUE(recipient->fired);
1301 EXPECT_EQ(recipient->cookie, 0x1481u);
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001302 EXPECT_EQ(recipient->who, dyingBaz);
Martijn Coenen115d4282016-12-19 05:14:04 +01001303 std::unique_lock<std::mutex> lock2(recipient2->mutex);
1304 recipient2->condition.wait_for(lock2, std::chrono::milliseconds(1000), [&recipient2]() {
1305 return recipient2->fired;
1306 });
1307 EXPECT_FALSE(recipient2->fired);
1308
1309 // Verify servicemanager dropped its reference too
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001310 sp<IBaz> deadBaz = IBaz::getService("dyingBaz", false);
1311 if (deadBaz != nullptr) {
Martijn Coenen115d4282016-12-19 05:14:04 +01001312 // Got a passthrough
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001313 EXPECT_FALSE(deadBaz->isRemote());
Martijn Coenen115d4282016-12-19 05:14:04 +01001314 }
1315}
1316
Yifan Hong1dc87932016-08-19 09:51:01 -07001317TEST_F(HidlTest, BarThisIsNewTest) {
Iliyan Malchev0acf4192016-08-22 19:33:20 -07001318 // Now the tricky part, get access to the derived interface.
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001319 ALOGI("CLIENT call thisIsNew.");
Yifan Hong1dc87932016-08-19 09:51:01 -07001320 EXPECT_OK(bar->thisIsNew());
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001321 ALOGI("CLIENT thisIsNew returned.");
Andreas Huber9cd48d02016-08-03 14:25:59 -07001322}
1323
Yifan Hong1e81c532016-10-18 18:43:46 -07001324static void expectGoodChild(const sp<IChild> &child) {
1325 ASSERT_NE(child.get(), nullptr);
1326 EXPECT_OK(child->doGrandparent());
1327 EXPECT_OK(child->doParent());
1328 EXPECT_OK(child->doChild());
1329}
1330
1331static void expectGoodParent(const sp<IParent> &parent) {
1332 ASSERT_NE(parent.get(), nullptr);
1333 EXPECT_OK(parent->doGrandparent());
1334 EXPECT_OK(parent->doParent());
1335 sp<IChild> child = IChild::castFrom(parent);
1336 expectGoodChild(child);
1337}
1338
1339static void expectGoodGrandparent(const sp<IGrandparent> &grandparent) {
1340 ASSERT_NE(grandparent.get(), nullptr);
1341 EXPECT_OK(grandparent->doGrandparent());
1342 sp<IParent> parent = IParent::castFrom(grandparent);
1343 expectGoodParent(parent);
1344}
1345
Yifan Hong5749b2c2016-11-28 12:52:36 -08001346TEST_F(HidlTest, FooHaveAnInterfaceTest) {
1347
1348 sp<ISimple> in = new Complicated(42);
Martijn Coenenb40ef022017-01-02 15:21:46 +01001349 Return<sp<ISimple>> ret = bar->haveAInterface(in);
1350 EXPECT_OK(ret);
1351 sp<ISimple> out = ret;
1352 ASSERT_NE(out.get(), nullptr);
1353 EXPECT_EQ(out->getCookie(), 42);
Yifan Hongfbcdc802017-02-22 18:12:48 -08001354 EXPECT_OK(out->customVecInt([](const auto &) { }));
1355 EXPECT_OK(out->customVecStr([](const auto &) { }));
1356 EXPECT_OK(out->ping());
1357 EXPECT_OK(out->mystr([](const auto &) { }));
1358 EXPECT_OK(out->myhandle([](const auto &) { }));
Yifan Hong5749b2c2016-11-28 12:52:36 -08001359}
1360
Yifan Hong1e81c532016-10-18 18:43:46 -07001361TEST_F(HidlTest, InheritRemoteGrandparentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001362 Return<sp<IGrandparent>> ret = fetcher->getGrandparent(true);
1363 EXPECT_OK(ret);
1364 expectGoodGrandparent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001365}
1366
1367TEST_F(HidlTest, InheritLocalGrandparentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001368 Return<sp<IGrandparent>> ret = fetcher->getGrandparent(false);
1369 EXPECT_OK(ret);
1370 expectGoodGrandparent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001371}
1372
Yifan Hong4b0214d2016-12-07 14:57:57 -08001373TEST_F(HidlTest, InheritRemoteParentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001374 Return<sp<IParent>> ret = fetcher->getParent(true);
1375 EXPECT_OK(ret);
1376 expectGoodParent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001377}
1378
Yifan Hong4b0214d2016-12-07 14:57:57 -08001379TEST_F(HidlTest, InheritLocalParentTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001380 Return<sp<IParent>> ret = fetcher->getParent(false);
1381 EXPECT_OK(ret);
1382 expectGoodParent(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001383}
1384
1385TEST_F(HidlTest, InheritRemoteChildTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001386 Return<sp<IChild>> ret = fetcher->getChild(true);
1387 EXPECT_OK(ret);
1388 expectGoodChild(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001389}
1390
1391TEST_F(HidlTest, InheritLocalChildTest) {
Martijn Coenenb40ef022017-01-02 15:21:46 +01001392 Return<sp<IChild>> ret = fetcher->getChild(false);
1393 EXPECT_OK(ret);
1394 expectGoodChild(ret);
Yifan Hong1e81c532016-10-18 18:43:46 -07001395}
1396
Andreas Huber5d034772016-09-28 14:23:51 -07001397TEST_F(HidlTest, TestArrayDimensionality) {
1398 hidl_array<int, 2> oneDim;
1399 hidl_array<int, 2, 3> twoDim;
1400 hidl_array<int, 2, 3, 4> threeDim;
1401
1402 EXPECT_EQ(oneDim.size(), 2u);
1403 EXPECT_EQ(twoDim.size(), std::make_tuple(2u, 3u));
1404 EXPECT_EQ(threeDim.size(), std::make_tuple(2u, 3u, 4u));
1405}
1406
Yifan Hongc6752dc2016-12-20 14:00:14 -08001407TEST_F(HidlTest, StructEqualTest) {
1408 using G = IFoo::Goober;
1409 using F = IFoo::Fumble;
1410 G g1{
1411 .q = 42,
1412 .name = "The Ultimate Question of Life, the Universe, and Everything",
1413 .address = "North Pole",
1414 .numbers = std::array<double, 10>{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} },
1415 .fumble = F{.data = {.data = 50}},
1416 .gumble = F{.data = {.data = 60}}
1417 };
1418 G g2{
1419 .q = 42,
1420 .name = "The Ultimate Question of Life, the Universe, and Everything",
1421 .address = "North Pole",
1422 .numbers = std::array<double, 10>{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} },
1423 .fumble = F{.data = {.data = 50}},
1424 .gumble = F{.data = {.data = 60}}
1425 };
1426 G g3{
1427 .q = 42,
1428 .name = "The Ultimate Question of Life, the Universe, and Everything",
1429 .address = "North Pole",
1430 .numbers = std::array<double, 10>{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} },
1431 .fumble = F{.data = {.data = 50}},
1432 .gumble = F{.data = {.data = 61}}
1433 };
1434 // explicitly invoke operator== here.
1435 EXPECT_TRUE(g1 == g2);
1436 EXPECT_TRUE(g1 != g3);
1437}
1438
1439TEST_F(HidlTest, EnumEqualTest) {
1440 using E = IFoo::SomeEnum;
1441 E e1 = E::quux;
1442 E e2 = E::quux;
1443 E e3 = E::goober;
1444 // explicitly invoke operator== here.
1445 EXPECT_TRUE(e1 == e2);
1446 EXPECT_TRUE(e1 != e3);
1447}
1448
Yifan Hongbf459bc2016-08-23 16:50:37 -07001449#if HIDL_RUN_POINTER_TESTS
Andreas Huber9cd48d02016-08-03 14:25:59 -07001450
Yifan Hongbf459bc2016-08-23 16:50:37 -07001451TEST_F(HidlTest, PassAGraphTest) {
1452 IGraph::Graph g;
Yifan Hong398e6fb2016-10-17 11:38:09 -07001453 ::android::simpleGraph(g);
1454 ::android::logSimpleGraph("CLIENT", g);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001455 ALOGI("CLIENT call passAGraph");
1456 EXPECT_OK(graphInterface->passAGraph(g));
1457}
1458
1459TEST_F(HidlTest, GiveAGraphTest) {
1460 EXPECT_OK(graphInterface->giveAGraph([&](const auto &newGraph) {
Yifan Hong398e6fb2016-10-17 11:38:09 -07001461 ::android::logSimpleGraph("CLIENT", newGraph);
1462 EXPECT_TRUE(::android::isSimpleGraph(newGraph));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001463 }));
1464}
1465TEST_F(HidlTest, PassANodeTest) {
1466 IGraph::Node node; node.data = 10;
1467 EXPECT_OK(graphInterface->passANode(node));
1468}
1469TEST_F(HidlTest, PassTwoGraphsTest) {
1470 IGraph::Graph g;
Yifan Hong398e6fb2016-10-17 11:38:09 -07001471 ::android::simpleGraph(g);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001472 EXPECT_OK(graphInterface->passTwoGraphs(&g, &g));
1473}
1474TEST_F(HidlTest, PassAGammaTest) {
1475 IGraph::Theta s; s.data = 500;
1476 IGraph::Alpha a; a.s_ptr = &s;
1477 IGraph::Beta b; b.s_ptr = &s;
1478 IGraph::Gamma c; c.a_ptr = &a; c.b_ptr = &b;
1479 ALOGI("CLIENT calling passAGamma: c.a = %p, c.b = %p, c.a->s = %p, c.b->s = %p",
1480 c.a_ptr, c.b_ptr, c.a_ptr->s_ptr, c.b_ptr->s_ptr);
1481 EXPECT_OK(graphInterface->passAGamma(c));
1482}
1483TEST_F(HidlTest, PassNullTest) {
1484 IGraph::Gamma c;
1485 c.a_ptr = nullptr;
1486 c.b_ptr = nullptr;
1487 EXPECT_OK(graphInterface->passAGamma(c));
1488}
1489TEST_F(HidlTest, PassASimpleRefTest) {
1490 IGraph::Theta s;
1491 s.data = 500;
1492 IGraph::Alpha a;
1493 a.s_ptr = &s;
1494 EXPECT_OK(graphInterface->passASimpleRef(&a));
1495}
1496TEST_F(HidlTest, PassASimpleRefSTest) {
1497 IGraph::Theta s;
1498 s.data = 500;
1499 ALOGI("CLIENT call passASimpleRefS with %p", &s);
1500 EXPECT_OK(graphInterface->passASimpleRefS(&s));
1501}
1502TEST_F(HidlTest, GiveASimpleRefTest) {
1503 EXPECT_OK(graphInterface->giveASimpleRef([&](const auto & a_ptr) {
1504 EXPECT_EQ(a_ptr->s_ptr->data, 500);
1505 }));
1506}
1507TEST_F(HidlTest, GraphReportErrorsTest) {
1508 Return<int32_t> ret = graphInterface->getErrors();
1509 EXPECT_OK(ret);
1510 EXPECT_EQ(int32_t(ret), 0);
1511}
1512
1513TEST_F(HidlTest, PointerPassOldBufferTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001514 EXPECT_OK(validationPointerInterface->bar1([&](const auto& sptr, const auto& s) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001515 EXPECT_OK(pointerInterface->foo1(sptr, s));
1516 }));
1517}
1518TEST_F(HidlTest, PointerPassOldBufferTest2) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001519 EXPECT_OK(validationPointerInterface->bar2([&](const auto& s, const auto& a) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001520 EXPECT_OK(pointerInterface->foo2(s, a));
1521 }));
1522}
1523TEST_F(HidlTest, PointerPassSameOldBufferPointerTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001524 EXPECT_OK(validationPointerInterface->bar3([&](const auto& s, const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001525 EXPECT_OK(pointerInterface->foo3(s, a, b));
1526 }));
1527}
1528TEST_F(HidlTest, PointerPassOnlyTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001529 EXPECT_OK(validationPointerInterface->bar4([&](const auto& s) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001530 EXPECT_OK(pointerInterface->foo4(s));
1531 }));
1532}
1533TEST_F(HidlTest, PointerPassTwoEmbeddedTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001534 EXPECT_OK(validationPointerInterface->bar5([&](const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001535 EXPECT_OK(pointerInterface->foo5(a, b));
1536 }));
1537}
1538TEST_F(HidlTest, PointerPassIndirectBufferHasDataTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001539 EXPECT_OK(validationPointerInterface->bar6([&](const auto& a) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001540 EXPECT_OK(pointerInterface->foo6(a));
1541 }));
1542}
1543TEST_F(HidlTest, PointerPassTwoIndirectBufferTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001544 EXPECT_OK(validationPointerInterface->bar7([&](const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001545 EXPECT_OK(pointerInterface->foo7(a, b));
1546 }));
1547}
1548TEST_F(HidlTest, PointerPassDeeplyIndirectTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001549 EXPECT_OK(validationPointerInterface->bar8([&](const auto& d) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001550 EXPECT_OK(pointerInterface->foo8(d));
1551 }));
1552}
1553TEST_F(HidlTest, PointerPassStringRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001554 EXPECT_OK(validationPointerInterface->bar9([&](const auto& str) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001555 EXPECT_OK(pointerInterface->foo9(str));
1556 }));
1557}
1558TEST_F(HidlTest, PointerPassRefVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001559 EXPECT_OK(validationPointerInterface->bar10([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001560 EXPECT_OK(pointerInterface->foo10(v));
1561 }));
1562}
1563TEST_F(HidlTest, PointerPassVecRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001564 EXPECT_OK(validationPointerInterface->bar11([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001565 EXPECT_OK(pointerInterface->foo11(v));
1566 }));
1567}
1568TEST_F(HidlTest, PointerPassArrayRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001569 EXPECT_OK(validationPointerInterface->bar12([&](const auto& array) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001570 EXPECT_OK(pointerInterface->foo12(array));
1571 }));
1572}
1573TEST_F(HidlTest, PointerPassRefArrayTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001574 EXPECT_OK(validationPointerInterface->bar13([&](const auto& array) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001575 EXPECT_OK(pointerInterface->foo13(array));
1576 }));
1577}
1578TEST_F(HidlTest, PointerPass3RefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001579 EXPECT_OK(validationPointerInterface->bar14([&](const auto& p3) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001580 EXPECT_OK(pointerInterface->foo14(p3));
1581 }));
1582}
1583TEST_F(HidlTest, PointerPassInt3RefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001584 EXPECT_OK(validationPointerInterface->bar15([&](const auto& p3) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001585 EXPECT_OK(pointerInterface->foo15(p3));
1586 }));
1587}
1588TEST_F(HidlTest, PointerPassEmbeddedPointersTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001589 EXPECT_OK(validationPointerInterface->bar16([&](const auto& p) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001590 EXPECT_OK(pointerInterface->foo16(p));
1591 }));
1592}
1593TEST_F(HidlTest, PointerPassEmbeddedPointers2Test) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001594 EXPECT_OK(validationPointerInterface->bar17([&](const auto& p) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001595 EXPECT_OK(pointerInterface->foo17(p));
1596 }));
1597}
1598TEST_F(HidlTest, PointerPassCopiedStringTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001599 EXPECT_OK(validationPointerInterface->bar18([&](const auto& str_ref, const auto& str_ref2, const auto& str) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001600 EXPECT_OK(pointerInterface->foo18(str_ref, str_ref2, str));
1601 }));
1602}
1603TEST_F(HidlTest, PointerPassCopiedVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001604 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 -07001605 EXPECT_OK(pointerInterface->foo19(a_vec_ref, a_vec, a_vec_ref2));
1606 }));
1607}
Yifan Hong84465902016-09-27 15:52:17 -07001608TEST_F(HidlTest, PointerPassBigRefVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001609 EXPECT_OK(validationPointerInterface->bar20([&](const auto& v) {
Yifan Hong84465902016-09-27 15:52:17 -07001610 EXPECT_FAIL(pointerInterface->foo20(v));
1611 }));
1612}
Yifan Hongbf459bc2016-08-23 16:50:37 -07001613TEST_F(HidlTest, PointerPassMultidimArrayRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001614 EXPECT_OK(validationPointerInterface->bar21([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001615 EXPECT_OK(pointerInterface->foo21(v));
1616 }));
1617}
1618TEST_F(HidlTest, PointerPassRefMultidimArrayTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001619 EXPECT_OK(validationPointerInterface->bar22([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001620 EXPECT_OK(pointerInterface->foo22(v));
1621 }));
1622}
1623TEST_F(HidlTest, PointerGiveOldBufferTest) {
1624 EXPECT_OK(pointerInterface->bar1([&](const auto& sptr, const auto& s) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001625 EXPECT_OK(validationPointerInterface->foo1(sptr, s));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001626 }));
1627}
1628TEST_F(HidlTest, PointerGiveOldBufferTest2) {
1629 EXPECT_OK(pointerInterface->bar2([&](const auto& s, const auto& a) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001630 EXPECT_OK(validationPointerInterface->foo2(s, a));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001631 }));
1632}
1633TEST_F(HidlTest, PointerGiveSameOldBufferPointerTest) {
1634 EXPECT_OK(pointerInterface->bar3([&](const auto& s, const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001635 EXPECT_OK(validationPointerInterface->foo3(s, a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001636 }));
1637}
1638TEST_F(HidlTest, PointerGiveOnlyTest) {
1639 EXPECT_OK(pointerInterface->bar4([&](const auto& s) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001640 EXPECT_OK(validationPointerInterface->foo4(s));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001641 }));
1642}
1643TEST_F(HidlTest, PointerGiveTwoEmbeddedTest) {
1644 EXPECT_OK(pointerInterface->bar5([&](const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001645 EXPECT_OK(validationPointerInterface->foo5(a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001646 }));
1647}
1648TEST_F(HidlTest, PointerGiveIndirectBufferHasDataTest) {
1649 EXPECT_OK(pointerInterface->bar6([&](const auto& a) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001650 EXPECT_OK(validationPointerInterface->foo6(a));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001651 }));
1652}
1653TEST_F(HidlTest, PointerGiveTwoIndirectBufferTest) {
1654 EXPECT_OK(pointerInterface->bar7([&](const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001655 EXPECT_OK(validationPointerInterface->foo7(a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001656 }));
1657}
1658TEST_F(HidlTest, PointerGiveDeeplyIndirectTest) {
1659 EXPECT_OK(pointerInterface->bar8([&](const auto& d) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001660 EXPECT_OK(validationPointerInterface->foo8(d));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001661 }));
1662}
1663TEST_F(HidlTest, PointerGiveStringRefTest) {
1664 EXPECT_OK(pointerInterface->bar9([&](const auto& str) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001665 EXPECT_OK(validationPointerInterface->foo9(str));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001666 }));
1667}
1668TEST_F(HidlTest, PointerGiveRefVecTest) {
1669 EXPECT_OK(pointerInterface->bar10([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001670 EXPECT_OK(validationPointerInterface->foo10(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001671 }));
1672}
1673TEST_F(HidlTest, PointerGiveVecRefTest) {
1674 EXPECT_OK(pointerInterface->bar11([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001675 EXPECT_OK(validationPointerInterface->foo11(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001676 }));
1677}
1678TEST_F(HidlTest, PointerGiveArrayRefTest) {
1679 EXPECT_OK(pointerInterface->bar12([&](const auto& array) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001680 EXPECT_OK(validationPointerInterface->foo12(array));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001681 }));
1682}
1683TEST_F(HidlTest, PointerGiveRefArrayTest) {
1684 EXPECT_OK(pointerInterface->bar13([&](const auto& array) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001685 EXPECT_OK(validationPointerInterface->foo13(array));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001686 }));
1687}
1688TEST_F(HidlTest, PointerGive3RefTest) {
1689 EXPECT_OK(pointerInterface->bar14([&](const auto& p3) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001690 EXPECT_OK(validationPointerInterface->foo14(p3));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001691 }));
1692}
1693TEST_F(HidlTest, PointerGiveInt3RefTest) {
1694 EXPECT_OK(pointerInterface->bar15([&](const auto& p3) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001695 EXPECT_OK(validationPointerInterface->foo15(p3));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001696 }));
1697}
1698TEST_F(HidlTest, PointerGiveEmbeddedPointersTest) {
1699 EXPECT_OK(pointerInterface->bar16([&](const auto& p) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001700 EXPECT_OK(validationPointerInterface->foo16(p));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001701 }));
1702}
1703TEST_F(HidlTest, PointerGiveEmbeddedPointers2Test) {
1704 EXPECT_OK(pointerInterface->bar17([&](const auto& p) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001705 EXPECT_OK(validationPointerInterface->foo17(p));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001706 }));
1707}
1708TEST_F(HidlTest, PointerGiveCopiedStringTest) {
1709 EXPECT_OK(pointerInterface->bar18([&](const auto& str_ref, const auto& str_ref2, const auto& str) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001710 EXPECT_OK(validationPointerInterface->foo18(str_ref, str_ref2, str));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001711 }));
1712}
1713TEST_F(HidlTest, PointerGiveCopiedVecTest) {
1714 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 -07001715 EXPECT_OK(validationPointerInterface->foo19(a_vec_ref, a_vec, a_vec_ref2));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001716 }));
1717}
Yifan Hong84465902016-09-27 15:52:17 -07001718// This cannot be enabled until _hidl_error is not ignored when
1719// the remote writeEmbeddedReferencesToParcel.
1720// TEST_F(HidlTest, PointerGiveBigRefVecTest) {
1721// EXPECT_FAIL(pointerInterface->bar20([&](const auto& v) {
1722// }));
1723// }
Yifan Hongbf459bc2016-08-23 16:50:37 -07001724TEST_F(HidlTest, PointerGiveMultidimArrayRefTest) {
1725 EXPECT_OK(pointerInterface->bar21([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001726 EXPECT_OK(validationPointerInterface->foo21(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001727 }));
1728}
1729TEST_F(HidlTest, PointerGiveRefMultidimArrayTest) {
1730 EXPECT_OK(pointerInterface->bar22([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001731 EXPECT_OK(validationPointerInterface->foo22(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001732 }));
1733}
1734TEST_F(HidlTest, PointerReportErrorsTest) {
1735 Return<int32_t> ret = pointerInterface->getErrors();
1736 EXPECT_OK(ret);
1737 EXPECT_EQ(int32_t(ret), 0);
1738}
1739#endif
1740
Yifan Hongc70f0d82016-10-10 14:50:22 -07001741int forkAndRunTests(TestMode mode) {
1742 pid_t child;
1743 int status;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001744
Yifan Hongc70f0d82016-10-10 14:50:22 -07001745 const char* modeText = (mode == BINDERIZED) ? "BINDERIZED" : "PASSTHROUGH";
1746 ALOGI("Start running tests in %s mode...", modeText);
1747 fprintf(stdout, "Start running tests in %s mode...\n", modeText);
1748 fflush(stdout);
1749
1750 if ((child = fork()) == 0) {
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001751 gHidlEnvironment = static_cast<HidlEnvironment *>(
1752 ::testing::AddGlobalTestEnvironment(new HidlEnvironment(mode)));
Yifan Hongc70f0d82016-10-10 14:50:22 -07001753 int testStatus = RUN_ALL_TESTS();
1754 if(testStatus == 0) {
1755 exit(0);
1756 }
1757 int failed = ::testing::UnitTest::GetInstance()->failed_test_count();
1758 if (failed == 0) {
1759 exit(-testStatus);
1760 }
1761 exit(failed);
1762 }
1763 waitpid(child, &status, 0 /* options */);
1764 ALOGI("All tests finished in %s mode.", modeText);
1765 fprintf(stdout, "All tests finished in %s mode.\n", modeText);
1766 fflush(stdout);
Yifan Hong1dc87932016-08-19 09:51:01 -07001767 return status;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001768}
Yifan Hongc70f0d82016-10-10 14:50:22 -07001769
1770void handleStatus(int status, const char *mode) {
1771 if (status != 0) {
1772 if (WIFEXITED(status)) {
1773 status = WEXITSTATUS(status);
1774 if (status < 0) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001775 fprintf(stdout, " RUN_ALL_TESTS returns %d for %s mode.\n", -status, mode);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001776 } else {
Yifan Hong48562fd2016-10-24 15:26:10 -07001777 fprintf(stdout, " %d test(s) failed for %s mode.\n", status, mode);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001778 }
1779 } else {
Yifan Hong48562fd2016-10-24 15:26:10 -07001780 fprintf(stdout, " ERROR: %s child process exited abnormally with %d\n", mode, status);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001781 }
1782 }
1783}
1784
Yifan Hongd12398d2016-10-13 11:05:29 -07001785static void usage(const char *me) {
1786 fprintf(stderr,
1787 "usage: %s [-b] [-p] [GTEST_OPTIONS]\n",
1788 me);
1789
1790 fprintf(stderr, " -b binderized mode only\n");
1791 fprintf(stderr, " -p passthrough mode only\n");
1792 fprintf(stderr, " (if -b and -p are both missing or both present, "
1793 "both modes are tested.)\n");
1794}
1795
Yifan Hongc70f0d82016-10-10 14:50:22 -07001796int main(int argc, char **argv) {
Yifan Hongd12398d2016-10-13 11:05:29 -07001797 const char *me = argv[0];
1798 bool b = false;
1799 bool p = false;
1800 struct option longopts[] = {{0,0,0,0}};
1801 int res;
1802 while ((res = getopt_long(argc, argv, "hbp", longopts, NULL)) >= 0) {
1803 switch (res) {
1804 case 'h': {
1805 usage(me);
1806 exit(1);
1807 } break;
1808
1809 case 'b': {
1810 b = true;
1811 } break;
1812
1813 case 'p': {
1814 p = true;
1815 } break;
1816
1817 case '?':
1818 default: {
1819 // ignore. pass to gTest.
1820 } break;
1821 }
1822 }
1823 if (!b && !p) {
1824 b = p = true;
1825 }
Yifan Hongc70f0d82016-10-10 14:50:22 -07001826
1827 ::testing::InitGoogleTest(&argc, argv);
1828 // put test in child process because RUN_ALL_TESTS
1829 // should not be run twice.
Yifan Hongd12398d2016-10-13 11:05:29 -07001830 int pStatus = p ? forkAndRunTests(PASSTHROUGH) : 0;
1831 int bStatus = b ? forkAndRunTests(BINDERIZED) : 0;
Yifan Hongc70f0d82016-10-10 14:50:22 -07001832
Yifan Hong48562fd2016-10-24 15:26:10 -07001833 fprintf(stdout, "\n=========================================================\n\n"
1834 " Summary:\n\n");
Yifan Hongd12398d2016-10-13 11:05:29 -07001835 if (p) {
1836 ALOGI("PASSTHROUGH Test result = %d", pStatus);
1837 handleStatus(pStatus, "PASSTHROUGH");
1838 }
1839 if (b) {
1840 ALOGI("BINDERIZED Test result = %d", bStatus);
1841 handleStatus(bStatus, "BINDERIZED ");
1842 }
1843
Yifan Hongc70f0d82016-10-10 14:50:22 -07001844 if (pStatus == 0 && bStatus == 0) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001845 fprintf(stdout, " Hooray! All tests passed.\n");
Yifan Hongc70f0d82016-10-10 14:50:22 -07001846 }
Yifan Hong48562fd2016-10-24 15:26:10 -07001847 fprintf(stdout, "\n=========================================================\n\n");
Yifan Hongc70f0d82016-10-10 14:50:22 -07001848
Hridya Valsaraju179379a2017-02-09 16:38:12 -08001849 return pStatus + bStatus != 0;
Yifan Hongc70f0d82016-10-10 14:50:22 -07001850}