blob: fb0e773be353824d282a888eb979b665d83af27d [file] [log] [blame]
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001#define LOG_TAG "hidl_test"
Andreas Huber9cd48d02016-08-03 14:25:59 -07002#include <android-base/logging.h>
3
Steven Moreland01bcb772016-11-08 15:57:25 -08004#include <android/hidl/manager/1.0/IServiceManager.h>
Steven Moreland0693f312016-11-09 15:06:14 -08005#include <android/hidl/manager/1.0/IServiceNotification.h>
Steven Moreland01bcb772016-11-08 15:57:25 -08006
Martijn Coenen99e6beb2016-12-01 15:48:42 +01007#include <android/hidl/memory/1.0/IAllocator.h>
8#include <android/hidl/memory/1.0/IMemory.h>
9
Steven Morelandb48a7da2016-11-11 14:12:46 -080010#include <android/hardware/tests/foo/1.0/IFoo.h>
11#include <android/hardware/tests/foo/1.0/IFooCallback.h>
Yifan Hongdef2cfb2016-12-14 16:00:24 -080012#include <android/hardware/tests/foo/1.0/BnSimple.h>
13// TODO(b/33669138): remove
14#include <cutils/trace.h>
15#include <android/hardware/tests/foo/1.0/BsSimple.h>
16#include <android/hardware/tests/foo/1.0/BpSimple.h>
17
Steven Morelandb48a7da2016-11-11 14:12:46 -080018#include <android/hardware/tests/bar/1.0/IBar.h>
Yifan Hong5749b2c2016-11-28 12:52:36 -080019#include <android/hardware/tests/bar/1.0/IComplicated.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080020#include <android/hardware/tests/inheritance/1.0/IFetcher.h>
21#include <android/hardware/tests/inheritance/1.0/IGrandparent.h>
22#include <android/hardware/tests/inheritance/1.0/IParent.h>
23#include <android/hardware/tests/inheritance/1.0/IChild.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010024#include <android/hardware/tests/memory/1.0/IMemoryTest.h>
Steven Morelandb48a7da2016-11-11 14:12:46 -080025#include <android/hardware/tests/pointer/1.0/IGraph.h>
26#include <android/hardware/tests/pointer/1.0/IPointer.h>
Andreas Huber9cd48d02016-08-03 14:25:59 -070027
Yifan Hong1dc87932016-08-19 09:51:01 -070028#include <gtest/gtest.h>
29#if GTEST_IS_THREADSAFE
30#include <sys/types.h>
Yifan Hongc70f0d82016-10-10 14:50:22 -070031#include <sys/wait.h>
Yifan Hong1dc87932016-08-19 09:51:01 -070032#include <signal.h>
33#include <errno.h>
34#include <pthread.h>
35#else
36#error "GTest did not detect pthread library."
37#endif
38
Steven Moreland01bcb772016-11-08 15:57:25 -080039#include <algorithm>
Steven Moreland0693f312016-11-09 15:06:14 -080040#include <condition_variable>
Yifan Hongd12398d2016-10-13 11:05:29 -070041#include <getopt.h>
42#include <inttypes.h>
Steven Moreland0693f312016-11-09 15:06:14 -080043#include <mutex>
Steven Moreland01bcb772016-11-08 15:57:25 -080044#include <set>
Yifan Hongbf459bc2016-08-23 16:50:37 -070045#include <sstream>
Yifan Hongccd782b2016-11-28 09:41:46 -080046#include <utility>
Yifan Hongd12398d2016-10-13 11:05:29 -070047#include <vector>
Yifan Hongbf459bc2016-08-23 16:50:37 -070048
Yifan Hong398e6fb2016-10-17 11:38:09 -070049#include <hidl-test/FooHelper.h>
50#include <hidl-test/PointerHelper.h>
51
Martijn Coenen93915102016-09-01 01:35:52 +020052#include <hidl/Status.h>
Martijn Coenen99e6beb2016-12-01 15:48:42 +010053#include <hidlmemory/mapping.h>
Andreas Huber9cd48d02016-08-03 14:25:59 -070054#include <hwbinder/IPCThreadState.h>
Andreas Huber9cd48d02016-08-03 14:25:59 -070055#include <hwbinder/ProcessState.h>
Andreas Huber9cd48d02016-08-03 14:25:59 -070056
Iliyan Malchev0acf4192016-08-22 19:33:20 -070057#include <utils/Condition.h>
58#include <utils/Timers.h>
59
Yifan Hongbf459bc2016-08-23 16:50:37 -070060#define EXPECT_OK(__ret__) EXPECT_TRUE(isOk(__ret__))
Yifan Hong84465902016-09-27 15:52:17 -070061#define EXPECT_FAIL(__ret__) EXPECT_FALSE(isOk(__ret__))
Yifan Hong8c48ad72016-10-14 11:34:59 -070062#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hongbf459bc2016-08-23 16:50:37 -070063
64// TODO uncomment this when kernel is patched with pointer changes.
65//#define HIDL_RUN_POINTER_TESTS 1
66
Yifan Hongca890522016-10-12 10:03:41 -070067// forward declarations.
68class PassthroughEnvironment;
69class BinderizedEnvironment;
70
Yifan Hongc70f0d82016-10-10 14:50:22 -070071// static storage
72static enum TestMode {
73 BINDERIZED,
74 PASSTHROUGH
75} gMode;
Yifan Hongca890522016-10-12 10:03:41 -070076static PassthroughEnvironment *gPassthroughEnvironment = nullptr;
77static BinderizedEnvironment *gBinderizedEnvironment = nullptr;
Yifan Hong35d66002016-10-13 12:47:44 -070078// per process tag
79static std::string gServiceName;
Yifan Hongc70f0d82016-10-10 14:50:22 -070080// end static storage
81
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;
Steven Moreland88ca4512016-08-11 11:24:10 -070086using ::android::hardware::tests::bar::V1_0::IBar;
Yifan Hong5749b2c2016-11-28 12:52:36 -080087using ::android::hardware::tests::bar::V1_0::IComplicated;
Yifan Hong1e81c532016-10-18 18:43:46 -070088using ::android::hardware::tests::inheritance::V1_0::IFetcher;
89using ::android::hardware::tests::inheritance::V1_0::IGrandparent;
90using ::android::hardware::tests::inheritance::V1_0::IParent;
91using ::android::hardware::tests::inheritance::V1_0::IChild;
Yifan Hongbf459bc2016-08-23 16:50:37 -070092using ::android::hardware::tests::pointer::V1_0::IGraph;
93using ::android::hardware::tests::pointer::V1_0::IPointer;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010094using ::android::hardware::tests::memory::V1_0::IMemoryTest;
Yifan Hong35d66002016-10-13 12:47:44 -070095using ::android::hardware::IPCThreadState;
96using ::android::hardware::ProcessState;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -070097using ::android::hardware::Return;
Iliyan Malchevd57066f2016-09-08 13:59:38 -070098using ::android::hardware::Void;
Andreas Huberf03332a2016-09-22 15:35:43 -070099using ::android::hardware::hidl_array;
Martijn Coenen115d4282016-12-19 05:14:04 +0100100using ::android::hardware::hidl_death_recipient;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100101using ::android::hardware::hidl_memory;
Andreas Huber8a82ff72016-08-04 10:29:39 -0700102using ::android::hardware::hidl_string;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100103using ::android::hardware::hidl_vec;
Martijn Coenen115d4282016-12-19 05:14:04 +0100104using ::android::hidl::base::V1_0::IBase;
Steven Moreland01bcb772016-11-08 15:57:25 -0800105using ::android::hidl::manager::V1_0::IServiceManager;
Steven Moreland0693f312016-11-09 15:06:14 -0800106using ::android::hidl::manager::V1_0::IServiceNotification;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100107using ::android::hidl::memory::V1_0::IAllocator;
108using ::android::hidl::memory::V1_0::IMemory;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700109using ::android::sp;
Martijn Coenen115d4282016-12-19 05:14:04 +0100110using ::android::wp;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700111using ::android::to_string;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700112using ::android::Mutex;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700113using ::android::MultiDimensionalToString;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700114using ::android::Condition;
Yifan Hong398e6fb2016-10-17 11:38:09 -0700115using ::android::DELAY_S;
116using ::android::DELAY_NS;
117using ::android::TOLERANCE_NS;
118using ::android::ONEWAY_TOLERANCE_NS;
119using std::to_string;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700120
Yifan Hongbf459bc2016-08-23 16:50:37 -0700121template <typename T>
122static inline ::testing::AssertionResult isOk(::android::hardware::Return<T> ret) {
123 return ret.getStatus().isOk()
124 ? (::testing::AssertionSuccess() << ret.getStatus())
125 : (::testing::AssertionFailure() << ret.getStatus());
126}
127
128template<typename T, typename S>
129static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
130 for(size_t i = 0; i < size; i++)
131 if(arr1[i] != arr2[i])
132 return false;
133 return true;
134}
135
Steven Moreland01bcb772016-11-08 15:57:25 -0800136template<typename T>
137std::string to_string(std::set<T> set) {
138 std::stringstream ss;
139 ss << "{";
140
141 bool first = true;
142 for (const T &item : set) {
143 if (first) {
144 first = false;
145 } else {
146 ss << ", ";
147 }
148
149 ss << to_string(item);
150 }
151
152 ss << "}";
153
154 return ss.str();
155}
156
Andreas Huber86a112b2016-10-19 14:25:16 -0700157struct Simple : public ISimple {
158 Simple(int32_t cookie)
159 : mCookie(cookie) {
160 }
161
162 Return<int32_t> getCookie() override {
163 return mCookie;
164 }
165
Yifan Hong5749b2c2016-11-28 12:52:36 -0800166 Return<void> customVecInt(customVecInt_cb _cb) override {
167 _cb(hidl_vec<int32_t>());
168 return Void();
169 }
170
171 Return<void> customVecStr(customVecStr_cb _cb) override {
172 hidl_vec<hidl_string> vec;
173 vec.resize(2);
174 _cb(vec);
175 return Void();
176 }
177
178 Return<void> mystr(mystr_cb _cb) override {
179 _cb(hidl_string());
180 return Void();
181 }
182
183 Return<void> myhandle(myhandle_cb _cb) override {
184 auto h = native_handle_create(0, 1);
185 _cb(h);
186 native_handle_delete(h);
187 return Void();
188 }
189
190private:
191 int32_t mCookie;
192};
193
194struct Complicated : public IComplicated {
195 Complicated(int32_t cookie)
196 : mCookie(cookie) {
197 }
198
199 Return<int32_t> getCookie() override {
200 return mCookie;
201 }
202
203 Return<void> customVecInt(customVecInt_cb _cb) override {
204 _cb(hidl_vec<int32_t>());
205 return Void();
206 }
207 Return<void> customVecStr(customVecStr_cb _cb) override {
208 hidl_vec<hidl_string> vec;
209 vec.resize(2);
210 _cb(vec);
211 return Void();
212 }
213
214 Return<void> mystr(mystr_cb _cb) override {
215 _cb(hidl_string());
216 return Void();
217 }
218
219 Return<void> myhandle(myhandle_cb _cb) override {
220 auto h = native_handle_create(0, 1);
221 _cb(h);
222 native_handle_delete(h);
223 return Void();
224 }
225
Andreas Huber86a112b2016-10-19 14:25:16 -0700226private:
227 int32_t mCookie;
228};
Steven Moreland0693f312016-11-09 15:06:14 -0800229
230struct ServiceNotification : public IServiceNotification {
231 std::mutex mutex;
232 std::condition_variable condition;
233
234 Return<void> onRegistration(const hidl_string &fqName,
235 const hidl_string &name,
236 bool preexisting) override {
237 if (preexisting) {
238 // not interested in things registered from previous runs of hidl_test
239 return Void();
240 }
241
242 std::unique_lock<std::mutex> lock(mutex);
243
244 mRegistered.push_back(std::string(fqName.c_str()) + "/" + name.c_str());
245
246 lock.unlock();
247 condition.notify_one();
248
249 return Void();
250 }
251
252 const std::vector<std::string> &getRegistrations() const {
253 return mRegistered;
254 }
255
256private:
257 std::vector<std::string> mRegistered{};
258};
259
Yifan Hong35d66002016-10-13 12:47:44 -0700260void signal_handler(int signal)
261{
262 if (signal == SIGTERM) {
263 ALOGD("SERVER %s shutting down...", gServiceName.c_str());
264 IPCThreadState::shutdown();
265 ALOGD("SERVER %s shutdown.", gServiceName.c_str());
266 exit(0);
267 }
268}
269
Yifan Hong1dc87932016-08-19 09:51:01 -0700270template <class T>
Yifan Hongccd782b2016-11-28 09:41:46 -0800271static pid_t forkServer(const std::string &serviceName) {
Yifan Hong35d66002016-10-13 12:47:44 -0700272 pid_t pid;
Steven Morelandc7167ca2016-11-28 11:29:55 -0800273
Yifan Hong35d66002016-10-13 12:47:44 -0700274 // use fork to create and kill to destroy server processes.
275 // getStub = true to get the passthrough version as the backend for the
276 // binderized service.
277 if ((pid = fork()) == 0) {
278 // in child process
279 sp<T> server = T::getService(serviceName, true);
280 gServiceName = serviceName;
281 signal(SIGTERM, signal_handler);
Yifan Hongccd782b2016-11-28 09:41:46 -0800282 ALOGD("SERVER registering %s", serviceName.c_str());
Steven Morelandc7167ca2016-11-28 11:29:55 -0800283 ::android::status_t status = server->registerAsService(serviceName);
284 if (status != ::android::OK) {
Yifan Hongccd782b2016-11-28 09:41:46 -0800285 ALOGE("SERVER could not register %s", serviceName.c_str());
Steven Morelandc7167ca2016-11-28 11:29:55 -0800286 exit(-1);
287 }
Yifan Hongccd782b2016-11-28 09:41:46 -0800288 ALOGD("SERVER starting %s", serviceName.c_str());
Yifan Hong35d66002016-10-13 12:47:44 -0700289 ProcessState::self()->setThreadPoolMaxThreadCount(0);
290 ProcessState::self()->startThreadPool();
291 IPCThreadState::self()->joinThreadPool();
Yifan Hongccd782b2016-11-28 09:41:46 -0800292 ALOGD("SERVER %s ends.", serviceName.c_str());
Yifan Hong35d66002016-10-13 12:47:44 -0700293 exit(0);
294 }
Steven Morelandc7167ca2016-11-28 11:29:55 -0800295
Yifan Hong35d66002016-10-13 12:47:44 -0700296 // in main process
297 return pid;
Yifan Hong1dc87932016-08-19 09:51:01 -0700298}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700299
Yifan Hongbf459bc2016-08-23 16:50:37 -0700300static void killServer(pid_t pid, const char *serverName) {
301 if(kill(pid, SIGTERM)) {
302 ALOGE("Could not kill %s; errno = %d", serverName, errno);
303 } else {
304 int status;
Yifan Hong35d66002016-10-13 12:47:44 -0700305 ALOGD("Waiting for %s to exit...", serverName);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700306 waitpid(pid, &status, 0);
Yifan Hong35d66002016-10-13 12:47:44 -0700307 if (status != 0) {
308 ALOGE("%s terminates abnormally with status %d", serverName, status);
309 }
310 ALOGD("Continuing...");
Yifan Hongbf459bc2016-08-23 16:50:37 -0700311 }
312}
313
Yifan Hongca890522016-10-12 10:03:41 -0700314class HidlEnvironmentBase : public ::testing::Environment {
Yifan Hong1e81c532016-10-18 18:43:46 -0700315protected:
Yifan Hongccd782b2016-11-28 09:41:46 -0800316 std::vector<std::pair<std::string, pid_t>> mPids;
317
Yifan Hong1dc87932016-08-19 09:51:01 -0700318public:
Steven Moreland01bcb772016-11-08 15:57:25 -0800319 sp<IServiceManager> manager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100320 sp<IAllocator> ashmemAllocator;
321 sp<IMemoryTest> memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700322 sp<IFetcher> fetcher;
Yifan Hong1dc87932016-08-19 09:51:01 -0700323 sp<IFoo> foo;
Martijn Coenen115d4282016-12-19 05:14:04 +0100324 sp<IFoo> dyingFoo;
Yifan Hong1dc87932016-08-19 09:51:01 -0700325 sp<IBar> bar;
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700326 sp<IFooCallback> fooCb;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700327 sp<IGraph> graphInterface;
328 sp<IPointer> pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700329 sp<IPointer> validationPointerInterface;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700330
Yifan Hongccd782b2016-11-28 09:41:46 -0800331 template <class T>
332 void addServer(const std::string &name) {
333 mPids.push_back({name, forkServer<T>(name)});
334 }
335
Yifan Hongca890522016-10-12 10:03:41 -0700336 void getServices() {
Steven Moreland01bcb772016-11-08 15:57:25 -0800337
338 manager = IServiceManager::getService("manager");
339
340 // alternatively:
341 // manager = defaultServiceManager()
342
343 ASSERT_NE(manager, nullptr);
344 ASSERT_TRUE(manager->isRemote()); // manager is always remote
345
Yifan Hongc70f0d82016-10-10 14:50:22 -0700346 // getStub is true if we are in passthrough mode to skip checking
347 // binderized server, false for binderized mode.
Yifan Hong1e81c532016-10-18 18:43:46 -0700348
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100349 ashmemAllocator = IAllocator::getService("ashmem", gMode == PASSTHROUGH /* getStub */);
350 ASSERT_NE(ashmemAllocator, nullptr);
351 ASSERT_EQ(ashmemAllocator->isRemote(), gMode == BINDERIZED);
352
353 memoryTest = IMemoryTest::getService("memory", gMode == PASSTHROUGH /* getStub */);
354 ASSERT_NE(memoryTest, nullptr);
355 ASSERT_EQ(memoryTest->isRemote(), gMode == BINDERIZED);
356
Yifan Hong1e81c532016-10-18 18:43:46 -0700357 fetcher = IFetcher::getService("fetcher", gMode == PASSTHROUGH /* getStub */);
358 ASSERT_NE(fetcher, nullptr);
359 ASSERT_EQ(fetcher->isRemote(), gMode == BINDERIZED);
360
Yifan Hongc70f0d82016-10-10 14:50:22 -0700361 foo = IFoo::getService("foo", gMode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700362 ASSERT_NE(foo, nullptr);
Yifan Hongc70f0d82016-10-10 14:50:22 -0700363 ASSERT_EQ(foo->isRemote(), gMode == BINDERIZED);
Yifan Hong1dc87932016-08-19 09:51:01 -0700364
Martijn Coenen115d4282016-12-19 05:14:04 +0100365 dyingFoo = IFoo::getService("dyingFoo", gMode == PASSTHROUGH /* getStub */);
366 ASSERT_NE(foo, nullptr);
367 ASSERT_EQ(foo->isRemote(), gMode == BINDERIZED);
368
Yifan Hongc70f0d82016-10-10 14:50:22 -0700369 bar = IBar::getService("foo", gMode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700370 ASSERT_NE(bar, nullptr);
Yifan Hongc70f0d82016-10-10 14:50:22 -0700371 ASSERT_EQ(bar->isRemote(), gMode == BINDERIZED);
Yifan Hong1dc87932016-08-19 09:51:01 -0700372
Yifan Hongc70f0d82016-10-10 14:50:22 -0700373 fooCb = IFooCallback::getService("foo callback", gMode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700374 ASSERT_NE(fooCb, nullptr);
Yifan Hongc70f0d82016-10-10 14:50:22 -0700375 ASSERT_EQ(fooCb->isRemote(), gMode == BINDERIZED);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700376
Yifan Hongc70f0d82016-10-10 14:50:22 -0700377 graphInterface = IGraph::getService("graph", gMode == PASSTHROUGH /* getStub */);
Yifan Hong7a827722016-10-06 17:20:12 -0700378 ASSERT_NE(graphInterface, nullptr);
Yifan Hongc70f0d82016-10-10 14:50:22 -0700379 ASSERT_EQ(graphInterface->isRemote(), gMode == BINDERIZED);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700380
Yifan Hongc70f0d82016-10-10 14:50:22 -0700381 pointerInterface = IPointer::getService("pointer", gMode == PASSTHROUGH /* getStub */);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700382 ASSERT_NE(pointerInterface, nullptr);
Yifan Hongc70f0d82016-10-10 14:50:22 -0700383 ASSERT_EQ(pointerInterface->isRemote(), gMode == BINDERIZED);
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700384
385 // use passthrough mode as the validation object.
386 validationPointerInterface = IPointer::getService("pointer", true /* getStub */);
387 ASSERT_NE(validationPointerInterface, nullptr);
Yifan Hong1dc87932016-08-19 09:51:01 -0700388 }
Yifan Hong1e81c532016-10-18 18:43:46 -0700389
Martijn Coenen115d4282016-12-19 05:14:04 +0100390 void killServer(const char *serverName) {
391 for (const auto &pair : mPids) {
392 if (pair.first == serverName) {
393 ::killServer(pair.second, pair.first.c_str());
394 }
395 }
396 }
397
Yifan Hong1e81c532016-10-18 18:43:46 -0700398 virtual void TearDown() {
399 // clean up by killing server processes.
400 ALOGI("Environment tear-down beginning...");
401 ALOGI("Killing servers...");
402 size_t i = 0;
Yifan Hongccd782b2016-11-28 09:41:46 -0800403 for (const auto &pair : mPids) {
Martijn Coenen115d4282016-12-19 05:14:04 +0100404 ::killServer(pair.second, pair.first.c_str());
Yifan Hong1e81c532016-10-18 18:43:46 -0700405 }
406 ALOGI("Servers all killed.");
407 ALOGI("Environment tear-down complete.");
408 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700409};
410
Yifan Hongca890522016-10-12 10:03:41 -0700411class PassthroughEnvironment : public HidlEnvironmentBase {
412private:
Yifan Hongca890522016-10-12 10:03:41 -0700413 virtual void SetUp() {
414 ALOGI("Environment setup beginning...");
Yifan Hong1e81c532016-10-18 18:43:46 -0700415 // starts this even for passthrough mode.
416 // this is used in Bar's default implementation
Yifan Hongccd782b2016-11-28 09:41:46 -0800417 addServer<IChild>("child");
Yifan Hong1e81c532016-10-18 18:43:46 -0700418 sleep(1);
Yifan Hongca890522016-10-12 10:03:41 -0700419 getServices();
420 ALOGI("Environment setup complete.");
421 }
422};
423
424class BinderizedEnvironment : public HidlEnvironmentBase {
Yifan Hong1dc87932016-08-19 09:51:01 -0700425public:
426 virtual void SetUp() {
427 ALOGI("Environment setup beginning...");
Yifan Hongca890522016-10-12 10:03:41 -0700428
Yifan Hong1e81c532016-10-18 18:43:46 -0700429 size_t i = 0;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100430 addServer<IAllocator>("ashmem");
431 addServer<IMemoryTest>("memory");
Yifan Hongccd782b2016-11-28 09:41:46 -0800432 addServer<IChild>("child");
433 addServer<IParent>("parent");
434 addServer<IFetcher>("fetcher");
435 addServer<IBar>("foo");
Martijn Coenen115d4282016-12-19 05:14:04 +0100436 addServer<IFoo>("dyingFoo");
Yifan Hongccd782b2016-11-28 09:41:46 -0800437 addServer<IFooCallback>("foo callback");
438 addServer<IGraph>("graph");
439 addServer<IPointer>("pointer");
Yifan Hong1dc87932016-08-19 09:51:01 -0700440
Yifan Hong1dc87932016-08-19 09:51:01 -0700441 sleep(1);
Yifan Hongca890522016-10-12 10:03:41 -0700442 getServices();
Yifan Hong1dc87932016-08-19 09:51:01 -0700443 ALOGI("Environment setup complete.");
444 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700445};
446
Yifan Hongca890522016-10-12 10:03:41 -0700447class HidlTest : public ::testing::Test {
448public:
Steven Moreland01bcb772016-11-08 15:57:25 -0800449 sp<IServiceManager> manager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100450 sp<IAllocator> ashmemAllocator;
451 sp<IMemoryTest> memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700452 sp<IFetcher> fetcher;
Yifan Hongca890522016-10-12 10:03:41 -0700453 sp<IFoo> foo;
Martijn Coenen115d4282016-12-19 05:14:04 +0100454 sp<IFoo> dyingFoo;
Yifan Hongca890522016-10-12 10:03:41 -0700455 sp<IBar> bar;
456 sp<IFooCallback> fooCb;
457 sp<IGraph> graphInterface;
458 sp<IPointer> pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700459 sp<IPointer> validationPointerInterface;
Yifan Hongca890522016-10-12 10:03:41 -0700460
Martijn Coenen115d4282016-12-19 05:14:04 +0100461 void killServer(const char *serverName) {
462 HidlEnvironmentBase *env;
463 if (gMode == BINDERIZED) {
464 env = gBinderizedEnvironment;
465 } else {
466 env = gPassthroughEnvironment;
467 }
468 env->killServer(serverName);
469 }
470
Yifan Hongca890522016-10-12 10:03:41 -0700471 virtual void SetUp() override {
472 ALOGI("Test setup beginning...");
473 HidlEnvironmentBase *env;
474 if (gMode == BINDERIZED) {
475 env = gBinderizedEnvironment;
476 } else {
477 env = gPassthroughEnvironment;
478 }
Steven Moreland01bcb772016-11-08 15:57:25 -0800479 manager = env->manager;
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100480 ashmemAllocator = env->ashmemAllocator;
481 memoryTest = env->memoryTest;
Yifan Hong1e81c532016-10-18 18:43:46 -0700482 fetcher = env->fetcher;
Yifan Hongca890522016-10-12 10:03:41 -0700483 foo = env->foo;
Martijn Coenen115d4282016-12-19 05:14:04 +0100484 dyingFoo = env->dyingFoo;
Yifan Hongca890522016-10-12 10:03:41 -0700485 bar = env->bar;
486 fooCb = env->fooCb;
487 graphInterface = env->graphInterface;
488 pointerInterface = env->pointerInterface;
Yifan Hong3cccc0f2016-10-13 10:12:28 -0700489 validationPointerInterface = env->validationPointerInterface;
Yifan Hongca890522016-10-12 10:03:41 -0700490 ALOGI("Test setup complete");
491 }
492};
493
Steven Moreland01bcb772016-11-08 15:57:25 -0800494TEST_F(HidlTest, ServiceListTest) {
495 static const std::set<std::string> binderizedSet = {
496 "android.hardware.tests.pointer@1.0::IPointer/pointer",
497 "android.hardware.tests.bar@1.0::IBar/foo",
498 "android.hardware.tests.inheritance@1.0::IFetcher/fetcher",
499 "android.hardware.tests.foo@1.0::IFooCallback/foo callback",
500 "android.hardware.tests.inheritance@1.0::IParent/parent",
501 "android.hardware.tests.inheritance@1.0::IParent/child",
502 "android.hardware.tests.inheritance@1.0::IChild/child",
503 "android.hardware.tests.pointer@1.0::IGraph/graph",
504 "android.hardware.tests.inheritance@1.0::IGrandparent/child",
505 "android.hardware.tests.foo@1.0::IFoo/foo",
506 "android.hidl.manager@1.0::IServiceManager/manager",
507 };
508
509 static const std::set<std::string> passthroughSet = {
510 "android.hidl.manager@1.0::IServiceManager/manager"
511 };
512
513 std::set<std::string> activeSet;
514
515 switch(gMode) {
516 case BINDERIZED: {
517 activeSet = binderizedSet;
518 } break;
519
520 case PASSTHROUGH: {
521 activeSet = passthroughSet;
522 } break;
523 default:
524 EXPECT_TRUE(false) << "unrecognized mode";
525 }
526
527 EXPECT_OK(manager->list([&activeSet](const hidl_vec<hidl_string> &registered){
528 std::set<std::string> registeredSet;
529
530 for (size_t i = 0; i < registered.size(); i++) {
531 registeredSet.insert(registered[i]);
532 }
533
534 std::set<std::string> difference;
535 std::set_difference(activeSet.begin(), activeSet.end(),
536 registeredSet.begin(), registeredSet.end(),
537 std::inserter(difference, difference.begin()));
538
539 EXPECT_EQ(difference.size(), 0u) << "service(s) not registered " << to_string(difference);
540 }));
541}
542
543// passthrough TODO(b/32747392)
544TEST_F(HidlTest, ServiceListByInterfaceTest) {
545 if (gMode == BINDERIZED) {
Steven Morelandd39133b2016-11-11 12:30:08 -0800546 EXPECT_OK(manager->listByInterface(IParent::descriptor,
Steven Moreland01bcb772016-11-08 15:57:25 -0800547 [](const hidl_vec<hidl_string> &registered) {
548 std::set<std::string> registeredSet;
549
550 for (size_t i = 0; i < registered.size(); i++) {
551 registeredSet.insert(registered[i]);
552 }
553
554 std::set<std::string> activeSet = {
555 "parent", "child"
556 };
557 std::set<std::string> difference;
558 std::set_difference(activeSet.begin(), activeSet.end(),
559 registeredSet.begin(), registeredSet.end(),
560 std::inserter(difference, difference.begin()));
561
562 EXPECT_EQ(difference.size(), 0u) << "service(s) not registered " << to_string(difference);
563 }));
564 }
565}
566
567// passthrough TODO(b/32747392)
568TEST_F(HidlTest, ServiceParentTest) {
569 if (gMode == BINDERIZED) {
570 sp<IParent> parent = IParent::getService("child");
571
572 EXPECT_NE(parent, nullptr);
573 }
574}
575
Steven Moreland0693f312016-11-09 15:06:14 -0800576// passthrough TODO(b/32747392)
577TEST_F(HidlTest, ServiceNotificationTest) {
578 if (gMode == BINDERIZED) {
579 ServiceNotification *notification = new ServiceNotification();
580
581 std::string instanceName = "test-instance";
582 EXPECT_TRUE(ISimple::registerForNotifications(instanceName, notification));
583
584 ProcessState::self()->setThreadPoolMaxThreadCount(0);
585 ProcessState::self()->startThreadPool();
586
587 Simple* instance = new Simple(1);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800588 EXPECT_EQ(::android::OK, instance->registerAsService(instanceName));
Steven Moreland0693f312016-11-09 15:06:14 -0800589
590 std::unique_lock<std::mutex> lock(notification->mutex);
591
592 notification->condition.wait_for(
593 lock,
594 std::chrono::milliseconds(2),
595 [&notification]() {
596 return notification->getRegistrations().size() >= 1;
597 });
598
599 std::vector<std::string> registrations = notification->getRegistrations();
600
601 EXPECT_EQ(registrations.size(), 1u);
602
603 EXPECT_EQ(to_string(registrations.data(), registrations.size()),
Steven Morelandd39133b2016-11-11 12:30:08 -0800604 std::string("['") + Simple::descriptor + "/" + instanceName + "']");
Steven Moreland0693f312016-11-09 15:06:14 -0800605 }
606}
607
608// passthrough TODO(b/32747392)
609TEST_F(HidlTest, ServiceAllNotificationTest) {
610 if (gMode == BINDERIZED) {
611 ServiceNotification *notification = new ServiceNotification();
612
613 std::string instanceOne = "test-instance-one";
614 std::string instanceTwo = "test-instance-two";
615 EXPECT_TRUE(ISimple::registerForNotifications("", notification));
616
617 ProcessState::self()->setThreadPoolMaxThreadCount(0);
618 ProcessState::self()->startThreadPool();
619
620 Simple* instanceA = new Simple(1);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800621 EXPECT_EQ(::android::OK, instanceA->registerAsService(instanceOne));
Steven Moreland0693f312016-11-09 15:06:14 -0800622 Simple* instanceB = new Simple(2);
Steven Morelandc7167ca2016-11-28 11:29:55 -0800623 EXPECT_EQ(::android::OK, instanceB->registerAsService(instanceTwo));
Steven Moreland0693f312016-11-09 15:06:14 -0800624
625 std::unique_lock<std::mutex> lock(notification->mutex);
626
627 notification->condition.wait_for(
628 lock,
629 std::chrono::milliseconds(2),
630 [&notification]() {
631 return notification->getRegistrations().size() >= 2;
632 });
633
634 std::vector<std::string> registrations = notification->getRegistrations();
635 std::sort(registrations.begin(), registrations.end());
636
637 EXPECT_EQ(registrations.size(), 2u);
638
Steven Morelandd39133b2016-11-11 12:30:08 -0800639 std::string descriptor = ISimple::descriptor;
Steven Moreland0693f312016-11-09 15:06:14 -0800640
641 EXPECT_EQ(to_string(registrations.data(), registrations.size()),
642 "['" + descriptor + "/" + instanceOne + "', '"
643 + descriptor + "/" + instanceTwo + "']");
644 }
645}
646
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100647TEST_F(HidlTest, TestSharedMemory) {
648 const uint8_t kValue = 0xCA;
649 hidl_memory mem_copy;
650 EXPECT_OK(ashmemAllocator->allocate(1024, [&](bool success, const hidl_memory& mem) {
651 EXPECT_EQ(success, true);
652
653 sp<IMemory> memory = mapMemory(mem);
654
655 EXPECT_NE(memory, nullptr);
656
657 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
658 EXPECT_NE(data, nullptr);
659
660 EXPECT_EQ(memory->getSize(), mem.size());
661
662 memory->update();
663 memset(data, 0, memory->getSize());
664 memory->commit();
665
666 mem_copy = mem;
667 memoryTest->fillMemory(mem, kValue);
668 for (size_t i = 0; i < mem.size(); i++) {
669 EXPECT_EQ(kValue, data[i]);
670 }
671 }));
672
673 // Test the memory persists after the call
674 sp<IMemory> memory = mapMemory(mem_copy);
675
676 EXPECT_NE(memory, nullptr);
677
678 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer()));
679 EXPECT_NE(data, nullptr);
680
681 for (size_t i = 0; i < mem_copy.size(); i++) {
682 EXPECT_EQ(kValue, data[i]);
683 }
684}
685
Yifan Hong1dc87932016-08-19 09:51:01 -0700686TEST_F(HidlTest, FooDoThisTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700687 ALOGI("CLIENT call doThis.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700688 EXPECT_OK(foo->doThis(1.0f));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700689 ALOGI("CLIENT doThis returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700690}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700691
Yifan Hong1dc87932016-08-19 09:51:01 -0700692TEST_F(HidlTest, FooDoThatAndReturnSomethingTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700693 ALOGI("CLIENT call doThatAndReturnSomething.");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000694 int32_t result = foo->doThatAndReturnSomething(2.0f);
695 ALOGI("CLIENT doThatAndReturnSomething returned %d.", result);
696 EXPECT_EQ(result, 666);
Yifan Hong1dc87932016-08-19 09:51:01 -0700697}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700698
Yifan Hong1dc87932016-08-19 09:51:01 -0700699TEST_F(HidlTest, FooDoQuiteABitTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700700 ALOGI("CLIENT call doQuiteABit");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000701 double something = foo->doQuiteABit(1, 2, 3.0f, 4.0);
702 ALOGI("CLIENT doQuiteABit returned %f.", something);
703 EXPECT_DOUBLE_EQ(something, 666.5);
Yifan Hong1dc87932016-08-19 09:51:01 -0700704}
705
706TEST_F(HidlTest, FooDoSomethingElseTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700707
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700708 ALOGI("CLIENT call doSomethingElse");
Andreas Huberf03332a2016-09-22 15:35:43 -0700709 hidl_array<int32_t, 15> param;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700710 for (size_t i = 0; i < sizeof(param) / sizeof(param[0]); ++i) {
711 param[i] = i;
712 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700713 EXPECT_OK(foo->doSomethingElse(param, [&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700714 ALOGI("CLIENT doSomethingElse returned %s.",
Andreas Huber5e44a292016-09-27 14:52:39 -0700715 to_string(something).c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700716 int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
717 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 -0700718 EXPECT_TRUE(isArrayEqual(something, expect, 32));
Yifan Hong1dc87932016-08-19 09:51:01 -0700719 }));
720}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700721
Yifan Hong1dc87932016-08-19 09:51:01 -0700722TEST_F(HidlTest, FooDoStuffAndReturnAStringTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700723 ALOGI("CLIENT call doStuffAndReturnAString");
Yifan Hong1dc87932016-08-19 09:51:01 -0700724 EXPECT_OK(foo->doStuffAndReturnAString([&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700725 ALOGI("CLIENT doStuffAndReturnAString returned '%s'.",
Andreas Huber9cd48d02016-08-03 14:25:59 -0700726 something.c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700727 EXPECT_STREQ(something.c_str(), "Hello, world");
728 }));
729}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700730
Yifan Hong1dc87932016-08-19 09:51:01 -0700731TEST_F(HidlTest, FooMapThisVectorTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700732 hidl_vec<int32_t> vecParam;
733 vecParam.resize(10);
734 for (size_t i = 0; i < 10; ++i) {
735 vecParam[i] = i;
736 }
Yifan Hong1dc87932016-08-19 09:51:01 -0700737 EXPECT_OK(foo->mapThisVector(vecParam, [&](const auto &something) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700738 ALOGI("CLIENT mapThisVector returned %s.",
Andreas Huber5e44a292016-09-27 14:52:39 -0700739 to_string(something).c_str());
Yifan Hong1dc87932016-08-19 09:51:01 -0700740 int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
Steven Morelande70455b2016-09-14 15:46:36 -0700741 EXPECT_TRUE(isArrayEqual(something, expect, something.size()));
Yifan Hong1dc87932016-08-19 09:51:01 -0700742 }));
743}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700744
Yifan Hongdef2cfb2016-12-14 16:00:24 -0800745TEST_F(HidlTest, WrapTest) {
746 using ::android::hardware::tests::foo::V1_0::BnSimple;
747 using ::android::hardware::tests::foo::V1_0::BsSimple;
748 using ::android::hardware::tests::foo::V1_0::BpSimple;
749 using ::android::hardware::HidlInstrumentor;
750 nsecs_t now;
751 int i = 0;
752
753 now = systemTime();
754 new BnSimple(new Simple(1));
755 EXPECT_LT(systemTime() - now, 2000000) << " for BnSimple(nonnull)";
756
757 now = systemTime();
758 new BnSimple(nullptr);
759 EXPECT_LT(systemTime() - now, 2000000) << " for BnSimple(null)";
760
761 now = systemTime();
762 new BsSimple(new Simple(1));
763 EXPECT_LT(systemTime() - now, 2000000) << " for BsSimple(nonnull)";
764
765 now = systemTime();
766 new BsSimple(nullptr);
767 EXPECT_LT(systemTime() - now, 2000000) << " for BsSimple(null)";
768
769 now = systemTime();
770 new BpSimple(nullptr);
771 EXPECT_LT(systemTime() - now, 2000000) << " for BpSimple(null)";
772
773 now = systemTime();
774 new ::android::hardware::HidlInstrumentor("");
775 EXPECT_LT(systemTime() - now, 2000000) << " for HidlInstrumentor";
776
777 now = systemTime();
778 i++;
779 EXPECT_LT(systemTime() - now, 1000) << " for nothing";
780}
781
Yifan Hong48562fd2016-10-24 15:26:10 -0700782// TODO: b/31819198
Yifan Hong1dc87932016-08-19 09:51:01 -0700783TEST_F(HidlTest, FooCallMeTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700784 ALOGI("CLIENT call callMe.");
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700785 // callMe is oneway, should return instantly.
786 nsecs_t now;
787 now = systemTime();
788 EXPECT_OK(foo->callMe(fooCb));
Yifan Hong3eac8a32016-10-11 10:02:59 -0700789 EXPECT_LT(systemTime() - now, ONEWAY_TOLERANCE_NS);
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700790 ALOGI("CLIENT callMe returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700791}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700792
Yifan Hong48562fd2016-10-24 15:26:10 -0700793// TODO: b/31819198
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700794TEST_F(HidlTest, ForReportResultsTest) {
795
796 // Bar::callMe will invoke three methods on FooCallback; one will return
797 // right away (even though it is a two-way method); the second one will
Yifan Hong3eac8a32016-10-11 10:02:59 -0700798 // block Bar for DELAY_S seconds, and the third one will return
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700799 // to Bar right away (is oneway) but will itself block for DELAY_S seconds.
800 // We need a way to make sure that these three things have happened within
801 // 2*DELAY_S seconds plus some small tolerance.
802 //
803 // Method FooCallback::reportResults() takes a timeout parameter. It blocks for
804 // that length of time, while waiting for the three methods above to
805 // complete. It returns the information of whether each method was invoked,
806 // as well as how long the body of the method took to execute. We verify
807 // the information returned by reportResults() against the timeout we pass (which
808 // is long enough for the method bodies to execute, plus tolerance), and
809 // verify that eachof them executed, as expected, and took the length of
810 // time to execute that we also expect.
811
812 const nsecs_t reportResultsNs =
Yifan Hong3eac8a32016-10-11 10:02:59 -0700813 2 * DELAY_NS + TOLERANCE_NS;
Andreas Huber03866f52016-08-30 14:19:52 -0700814
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700815 ALOGI("CLIENT: Waiting for up to %" PRId64 " seconds.",
816 nanoseconds_to_seconds(reportResultsNs));
817
818 fooCb->reportResults(reportResultsNs,
819 [&](int64_t timeLeftNs,
Andreas Huberf03332a2016-09-22 15:35:43 -0700820 const hidl_array<IFooCallback::InvokeInfo, 3> &invokeResults) {
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700821 ALOGI("CLIENT: FooCallback::reportResults() is returning data.");
822 ALOGI("CLIENT: Waited for %" PRId64 " milliseconds.",
823 nanoseconds_to_milliseconds(reportResultsNs - timeLeftNs));
824
Yifan Hong3eac8a32016-10-11 10:02:59 -0700825 EXPECT_LE(0, timeLeftNs);
826 EXPECT_LE(timeLeftNs, reportResultsNs);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700827
828 // two-way method, was supposed to return right away
829 EXPECT_TRUE(invokeResults[0].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700830 EXPECT_LE(invokeResults[0].timeNs, invokeResults[0].callerBlockedNs);
831 EXPECT_LE(invokeResults[0].callerBlockedNs, TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700832 // two-way method, was supposed to block caller for DELAY_NS
833 EXPECT_TRUE(invokeResults[1].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700834 EXPECT_LE(invokeResults[1].timeNs, invokeResults[1].callerBlockedNs);
835 EXPECT_LE(invokeResults[1].callerBlockedNs,
836 DELAY_NS + TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700837 // one-way method, do not block caller, but body was supposed to block for DELAY_NS
838 EXPECT_TRUE(invokeResults[2].invoked);
Yifan Hong3eac8a32016-10-11 10:02:59 -0700839 EXPECT_LE(invokeResults[2].callerBlockedNs, ONEWAY_TOLERANCE_NS);
840 EXPECT_LE(invokeResults[2].timeNs, DELAY_NS + TOLERANCE_NS);
Iliyan Malchev0acf4192016-08-22 19:33:20 -0700841 });
842}
843
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000844
845
Yifan Hong1dc87932016-08-19 09:51:01 -0700846TEST_F(HidlTest, FooUseAnEnumTest) {
847 ALOGI("CLIENT call useAnEnum.");
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000848 IFoo::SomeEnum sleepy = foo->useAnEnum(IFoo::SomeEnum::quux);
849 ALOGI("CLIENT useAnEnum returned %u", (unsigned)sleepy);
850 EXPECT_EQ(sleepy, IFoo::SomeEnum::goober);
Yifan Hong1dc87932016-08-19 09:51:01 -0700851}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700852
Yifan Hong1dc87932016-08-19 09:51:01 -0700853TEST_F(HidlTest, FooHaveAGooberTest) {
Andreas Huber9cd48d02016-08-03 14:25:59 -0700854 hidl_vec<IFoo::Goober> gooberVecParam;
855 gooberVecParam.resize(2);
856 gooberVecParam[0].name = "Hello";
857 gooberVecParam[1].name = "World";
858
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700859 ALOGI("CLIENT call haveAGooberVec.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700860 EXPECT_OK(foo->haveAGooberVec(gooberVecParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700861 ALOGI("CLIENT haveAGooberVec returned.");
Andreas Huber9cd48d02016-08-03 14:25:59 -0700862
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700863 ALOGI("CLIENT call haveaGoober.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700864 EXPECT_OK(foo->haveAGoober(gooberVecParam[0]));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700865 ALOGI("CLIENT haveaGoober returned.");
866
867 ALOGI("CLIENT call haveAGooberArray.");
Andreas Huberf03332a2016-09-22 15:35:43 -0700868 hidl_array<IFoo::Goober, 20> gooberArrayParam;
Yifan Hong1dc87932016-08-19 09:51:01 -0700869 EXPECT_OK(foo->haveAGooberArray(gooberArrayParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700870 ALOGI("CLIENT haveAGooberArray returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700871}
Steven Moreland88ca4512016-08-11 11:24:10 -0700872
Yifan Hong1dc87932016-08-19 09:51:01 -0700873TEST_F(HidlTest, FooHaveATypeFromAnotherFileTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700874 ALOGI("CLIENT call haveATypeFromAnotherFile.");
Steven Moreland88ca4512016-08-11 11:24:10 -0700875 Abc abcParam{};
876 abcParam.x = "alphabet";
877 abcParam.y = 3.14f;
Yifan Honga65bb2c2016-10-27 13:17:14 -0700878 native_handle_t *handle = native_handle_create(0, 0);
879 abcParam.z = handle;
Yifan Hong1dc87932016-08-19 09:51:01 -0700880 EXPECT_OK(foo->haveATypeFromAnotherFile(abcParam));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700881 ALOGI("CLIENT haveATypeFromAnotherFile returned.");
Yifan Honga65bb2c2016-10-27 13:17:14 -0700882 native_handle_delete(handle);
Steven Moreland88ca4512016-08-11 11:24:10 -0700883 abcParam.z = NULL;
Yifan Hong1dc87932016-08-19 09:51:01 -0700884}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700885
Yifan Hong1dc87932016-08-19 09:51:01 -0700886TEST_F(HidlTest, FooHaveSomeStringsTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700887 ALOGI("CLIENT call haveSomeStrings.");
Andreas Huberf03332a2016-09-22 15:35:43 -0700888 hidl_array<hidl_string, 3> stringArrayParam;
Andreas Huber9cd48d02016-08-03 14:25:59 -0700889 stringArrayParam[0] = "What";
890 stringArrayParam[1] = "a";
891 stringArrayParam[2] = "disaster";
Steven Moreland67f67b42016-09-29 08:59:02 -0700892 EXPECT_OK(foo->haveSomeStrings(
893 stringArrayParam,
894 [&](const auto &out) {
895 ALOGI("CLIENT haveSomeStrings returned %s.",
896 to_string(out).c_str());
897
898 EXPECT_EQ(to_string(out), "['Hello', 'World']");
899 }));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700900 ALOGI("CLIENT haveSomeStrings returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700901}
Andreas Huber9cd48d02016-08-03 14:25:59 -0700902
Yifan Hong1dc87932016-08-19 09:51:01 -0700903TEST_F(HidlTest, FooHaveAStringVecTest) {
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700904 ALOGI("CLIENT call haveAStringVec.");
Andreas Huber9cd48d02016-08-03 14:25:59 -0700905 hidl_vec<hidl_string> stringVecParam;
906 stringVecParam.resize(3);
907 stringVecParam[0] = "What";
908 stringVecParam[1] = "a";
909 stringVecParam[2] = "disaster";
Steven Moreland67f67b42016-09-29 08:59:02 -0700910 EXPECT_OK(foo->haveAStringVec(
911 stringVecParam,
912 [&](const auto &out) {
913 ALOGI("CLIENT haveAStringVec returned %s.",
914 to_string(out).c_str());
915
916 EXPECT_EQ(to_string(out), "['Hello', 'World']");
917 }));
Iliyan Malchevb31e10c2016-08-13 23:03:25 -0700918 ALOGI("CLIENT haveAStringVec returned.");
Yifan Hong1dc87932016-08-19 09:51:01 -0700919}
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700920
Andreas Huberf9d49f12016-09-12 14:58:36 -0700921TEST_F(HidlTest, FooTransposeMeTest) {
Andreas Huberf03332a2016-09-22 15:35:43 -0700922 hidl_array<float, 3, 5> in;
Andreas Huberf9d49f12016-09-12 14:58:36 -0700923 float k = 1.0f;
924 for (size_t i = 0; i < 3; ++i) {
925 for (size_t j = 0; j < 5; ++j, ++k) {
926 in[i][j] = k;
927 }
928 }
929
Andreas Huberf03332a2016-09-22 15:35:43 -0700930 ALOGI("CLIENT call transposeMe(%s).", to_string(in).c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -0700931
932 EXPECT_OK(foo->transposeMe(
Andreas Huberf03332a2016-09-22 15:35:43 -0700933 in,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700934 [&](const auto &out) {
935 ALOGI("CLIENT transposeMe returned %s.",
Andreas Huberf03332a2016-09-22 15:35:43 -0700936 to_string(out).c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -0700937
938 for (size_t i = 0; i < 3; ++i) {
939 for (size_t j = 0; j < 5; ++j) {
Andreas Huberf03332a2016-09-22 15:35:43 -0700940 EXPECT_EQ(out[j][i], in[i][j]);
Andreas Huberf9d49f12016-09-12 14:58:36 -0700941 }
942 }
943 }));
944}
945
946TEST_F(HidlTest, FooCallingDrWhoTest) {
947 IFoo::MultiDimensional in;
948
949 size_t k = 0;
950 for (size_t i = 0; i < 5; ++i) {
951 for (size_t j = 0; j < 3; ++j, ++k) {
Andreas Huberf03332a2016-09-22 15:35:43 -0700952 in.quuxMatrix[i][j].first = ("First " + std::to_string(k)).c_str();
953 in.quuxMatrix[i][j].last = ("Last " + std::to_string(15-k)).c_str();
Andreas Huberf9d49f12016-09-12 14:58:36 -0700954 }
955 }
956
957 ALOGI("CLIENT call callingDrWho(%s).",
958 MultiDimensionalToString(in).c_str());
959
960 EXPECT_OK(foo->callingDrWho(
961 in,
962 [&](const auto &out) {
963 ALOGI("CLIENT callingDrWho returned %s.",
964 MultiDimensionalToString(out).c_str());
965
Andreas Huber709b62d2016-09-19 11:21:18 -0700966 size_t k = 0;
Andreas Huberf9d49f12016-09-12 14:58:36 -0700967 for (size_t i = 0; i < 5; ++i) {
Andreas Huber709b62d2016-09-19 11:21:18 -0700968 for (size_t j = 0; j < 3; ++j, ++k) {
Andreas Huberf03332a2016-09-22 15:35:43 -0700969 EXPECT_STREQ(
970 out.quuxMatrix[i][j].first.c_str(),
971 in.quuxMatrix[4 - i][2 - j].last.c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -0700972
973 EXPECT_STREQ(
Andreas Huberf03332a2016-09-22 15:35:43 -0700974 out.quuxMatrix[i][j].last.c_str(),
975 in.quuxMatrix[4 - i][2 - j].first.c_str());
Andreas Huberf9d49f12016-09-12 14:58:36 -0700976 }
977 }
978 }));
979}
980
Andreas Huber709b62d2016-09-19 11:21:18 -0700981static std::string numberToEnglish(int x) {
982 static const char *const kDigits[] = {
983 "zero",
984 "one",
985 "two",
986 "three",
987 "four",
988 "five",
989 "six",
990 "seven",
991 "eight",
992 "nine",
993 };
994
995 if (x < 0) {
996 return "negative " + numberToEnglish(-x);
997 }
998
999 if (x < 10) {
1000 return kDigits[x];
1001 }
1002
1003 if (x <= 15) {
1004 static const char *const kSpecialTens[] = {
1005 "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
1006 };
1007
1008 return kSpecialTens[x - 10];
1009 }
1010
1011 if (x < 20) {
1012 return std::string(kDigits[x % 10]) + "teen";
1013 }
1014
1015 if (x < 100) {
1016 static const char *const kDecades[] = {
1017 "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
1018 "eighty", "ninety",
1019 };
1020
1021 return std::string(kDecades[x / 10 - 2]) + kDigits[x % 10];
1022 }
1023
1024 return "positively huge!";
1025}
1026
1027TEST_F(HidlTest, FooTransposeTest) {
1028 IFoo::StringMatrix5x3 in;
1029
Andreas Huber709b62d2016-09-19 11:21:18 -07001030 for (int i = 0; i < 5; ++i) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001031 for (int j = 0; j < 3; ++j) {
1032 in.s[i][j] = numberToEnglish(3 * i + j + 1).c_str();
Andreas Huber709b62d2016-09-19 11:21:18 -07001033 }
1034 }
1035
1036 EXPECT_OK(foo->transpose(
1037 in,
1038 [&](const auto &out) {
1039 EXPECT_EQ(
1040 to_string(out),
1041 "[['one', 'four', 'seven', 'ten', 'thirteen'], "
1042 "['two', 'five', 'eight', 'eleven', 'fourteen'], "
1043 "['three', 'six', 'nine', 'twelve', 'fifteen']]");
1044 }));
1045}
1046
1047TEST_F(HidlTest, FooTranspose2Test) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001048 hidl_array<hidl_string, 5, 3> in;
Andreas Huber709b62d2016-09-19 11:21:18 -07001049
Andreas Huber709b62d2016-09-19 11:21:18 -07001050 for (int i = 0; i < 5; ++i) {
Andreas Huberf03332a2016-09-22 15:35:43 -07001051 for (int j = 0; j < 3; ++j) {
1052 in[i][j] = numberToEnglish(3 * i + j + 1).c_str();
Andreas Huber709b62d2016-09-19 11:21:18 -07001053 }
1054 }
1055
1056 EXPECT_OK(foo->transpose2(
1057 in,
1058 [&](const auto &out) {
1059 EXPECT_EQ(
Andreas Huberf03332a2016-09-22 15:35:43 -07001060 to_string(out),
Andreas Huber709b62d2016-09-19 11:21:18 -07001061 "[['one', 'four', 'seven', 'ten', 'thirteen'], "
1062 "['two', 'five', 'eight', 'eleven', 'fourteen'], "
1063 "['three', 'six', 'nine', 'twelve', 'fifteen']]");
1064 }));
1065}
1066
Yifan Hong48562fd2016-10-24 15:26:10 -07001067// TODO: enable for passthrough mode after b/30814137
Yifan Hongb725d672016-10-10 10:14:15 -07001068TEST_F(HidlTest, FooNullNativeHandleTest) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001069 if (gMode == BINDERIZED) {
1070 Abc xyz;
1071 xyz.z = nullptr;
Yifan Honga65bb2c2016-10-27 13:17:14 -07001072 EXPECT_FAIL(bar->expectNullHandle(nullptr, xyz, [](bool hIsNull, bool xyzHasNull) {
1073 EXPECT_TRUE(hIsNull);
1074 EXPECT_TRUE(xyzHasNull);
1075 }));
Yifan Hong48562fd2016-10-24 15:26:10 -07001076 }
Yifan Hongb725d672016-10-10 10:14:15 -07001077}
Steven Moreland67f67b42016-09-29 08:59:02 -07001078
Martijn Coenene1638232016-10-26 12:51:34 +02001079TEST_F(HidlTest, FooNullCallbackTest) {
1080 EXPECT_OK(foo->echoNullInterface(nullptr,
1081 [](const auto receivedNull, const auto &intf) {
1082 EXPECT_TRUE(receivedNull);
1083 EXPECT_EQ(intf, nullptr);
1084 }));
1085}
1086
Steven Moreland67f67b42016-09-29 08:59:02 -07001087TEST_F(HidlTest, FooNonNullCallbackTest) {
1088 hidl_array<hidl_string, 5, 3> in;
1089
1090 EXPECT_FAIL(foo->transpose2(in, nullptr /* _hidl_cb */));
1091}
1092
Andreas Huber5e44a292016-09-27 14:52:39 -07001093TEST_F(HidlTest, FooSendVecTest) {
1094 hidl_vec<uint8_t> in;
1095 in.resize(16);
1096 for (size_t i = 0; i < in.size(); ++i) {
1097 in[i] = i;
1098 }
1099
1100 EXPECT_OK(foo->sendVec(
1101 in,
1102 [&](const auto &out) {
1103 EXPECT_EQ(to_string(in), to_string(out));
1104 }));
1105}
1106
Andreas Huber86a112b2016-10-19 14:25:16 -07001107TEST_F(HidlTest, FooHaveAVectorOfInterfacesTest) {
1108 hidl_vec<sp<ISimple> > in;
1109 in.resize(16);
1110 for (size_t i = 0; i < in.size(); ++i) {
1111 in[i] = new Simple(i);
1112 }
1113
1114 EXPECT_OK(foo->haveAVectorOfInterfaces(
1115 in,
1116 [&](const auto &out) {
1117 EXPECT_EQ(in.size(), out.size());
1118 for (size_t i = 0; i < in.size(); ++i) {
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001119 int32_t inCookie = in[i]->getCookie();
1120 int32_t outCookie = out[i]->getCookie();
1121 EXPECT_EQ(inCookie, outCookie);
Andreas Huber86a112b2016-10-19 14:25:16 -07001122 }
1123 }));
1124}
1125
1126TEST_F(HidlTest, FooHaveAVectorOfGenericInterfacesTest) {
Andreas Huber86a112b2016-10-19 14:25:16 -07001127
Yifan Hongc8934042016-11-17 17:10:52 -08001128 hidl_vec<sp<::android::hidl::base::V1_0::IBase> > in;
Andreas Huber86a112b2016-10-19 14:25:16 -07001129 in.resize(16);
1130 for (size_t i = 0; i < in.size(); ++i) {
Yifan Hongc8934042016-11-17 17:10:52 -08001131 sp<ISimple> s = new Simple(i);
1132 in[i] = s;
Andreas Huber86a112b2016-10-19 14:25:16 -07001133 }
1134
1135 EXPECT_OK(foo->haveAVectorOfGenericInterfaces(
1136 in,
1137 [&](const auto &out) {
1138 EXPECT_EQ(in.size(), out.size());
Yifan Hongc8934042016-11-17 17:10:52 -08001139
1140 EXPECT_OK(out[0]->interfaceChain([](const auto &names) {
1141 ASSERT_GT(names.size(), 0u);
1142 ASSERT_STREQ(names[0].c_str(), ISimple::descriptor);
1143 }));
Andreas Huber86a112b2016-10-19 14:25:16 -07001144 for (size_t i = 0; i < in.size(); ++i) {
Yifan Hongc8934042016-11-17 17:10:52 -08001145 sp<ISimple> inSimple = ISimple::castFrom(in[i]);
1146 sp<ISimple> outSimple = ISimple::castFrom(out[i]);
1147
1148 ASSERT_NE(inSimple.get(), nullptr);
1149 ASSERT_NE(outSimple.get(), nullptr);
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001150 EXPECT_EQ(in[i], inSimple.get()); // pointers must be equal!
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001151 int32_t inCookie = inSimple->getCookie();
1152 int32_t outCookie = outSimple->getCookie();
1153 EXPECT_EQ(inCookie, outCookie);
Andreas Huber86a112b2016-10-19 14:25:16 -07001154 }
1155 }));
1156}
1157
Yifan Hong8c48ad72016-10-14 11:34:59 -07001158TEST_F(HidlTest, FooStructEmbeddedHandleTest) {
1159 EXPECT_OK(foo->createMyHandle([&](const auto &myHandle) {
1160 EXPECT_EQ(myHandle.guard, 666);
Martijn Coenen2f69a5b2016-11-18 15:26:38 +01001161 const native_handle_t* handle = myHandle.h.getNativeHandle();
1162 EXPECT_EQ(handle->numInts, 10);
1163 EXPECT_EQ(handle->numFds, 0);
Yifan Hong8c48ad72016-10-14 11:34:59 -07001164 int data[] = {2,3,5,7,11,13,17,19,21,23};
Martijn Coenen2f69a5b2016-11-18 15:26:38 +01001165 EXPECT_ARRAYEQ(handle->data, data, 10);
Yifan Hong8c48ad72016-10-14 11:34:59 -07001166 }));
1167
1168 EXPECT_OK(foo->closeHandles());
1169}
1170
1171TEST_F(HidlTest, FooHandleVecTest) {
1172 EXPECT_OK(foo->createHandles(3, [&](const auto &handles) {
1173 EXPECT_EQ(handles.size(), 3ull);
1174 int data[] = {2,3,5,7,11,13,17,19,21,23};
1175 for (size_t i = 0; i < 3; i++) {
1176 const native_handle_t *h = handles[i];
1177 EXPECT_EQ(h->numInts, 10) << " for element " << i;
1178 EXPECT_EQ(h->numFds, 0) << " for element " << i;
1179 EXPECT_ARRAYEQ(h->data, data, 10);
1180 }
1181 }));
1182
1183 EXPECT_OK(foo->closeHandles());
1184}
1185
Martijn Coenen115d4282016-12-19 05:14:04 +01001186struct HidlDeathRecipient : hidl_death_recipient {
1187 std::mutex mutex;
1188 std::condition_variable condition;
1189 wp<IBase> who;
1190 bool fired = false;
1191 uint64_t cookie = 0;
1192
1193 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who) {
1194 std::unique_lock<std::mutex> lock(mutex);
1195 fired = true;
1196 this->cookie = cookie;
1197 this->who = who;
1198 condition.notify_one();
1199 };
1200};
1201
1202TEST_F(HidlTest, DeathRecipientTest) {
1203 // Need a threadpool to receive death calls from the kernel
1204 ProcessState::self()->setThreadPoolMaxThreadCount(0);
1205 ProcessState::self()->startThreadPool();
1206
1207 sp<HidlDeathRecipient> recipient = new HidlDeathRecipient();
1208 sp<HidlDeathRecipient> recipient2 = new HidlDeathRecipient();
1209
1210 EXPECT_TRUE(dyingFoo->linkToDeath(recipient, 0x1481));
1211 EXPECT_TRUE(dyingFoo->linkToDeath(recipient2, 0x2592));
1212 EXPECT_TRUE(dyingFoo->unlinkToDeath(recipient2));
1213
1214 if (gMode != BINDERIZED) {
1215 // Passthrough doesn't fire, nor does it keep state of
1216 // registered death recipients (so it won't fail unlinking
1217 // the same recipient twice).
1218 return;
1219 }
1220
1221 EXPECT_FALSE(dyingFoo->unlinkToDeath(recipient2));
1222 killServer("dyingFoo");
1223
1224 std::unique_lock<std::mutex> lock(recipient->mutex);
1225 recipient->condition.wait_for(lock, std::chrono::milliseconds(1000), [&recipient]() {
1226 return recipient->fired;
1227 });
1228 EXPECT_TRUE(recipient->fired);
1229 EXPECT_EQ(recipient->cookie, 0x1481u);
1230 EXPECT_EQ(recipient->who, dyingFoo);
1231 std::unique_lock<std::mutex> lock2(recipient2->mutex);
1232 recipient2->condition.wait_for(lock2, std::chrono::milliseconds(1000), [&recipient2]() {
1233 return recipient2->fired;
1234 });
1235 EXPECT_FALSE(recipient2->fired);
1236
1237 // Verify servicemanager dropped its reference too
1238 sp<IFoo> deadFoo = IFoo::getService("dyingFoo", false);
1239 if (deadFoo != nullptr) {
1240 // Got a passthrough
1241 EXPECT_FALSE(deadFoo->isRemote());
1242 }
1243}
1244
Yifan Hong1dc87932016-08-19 09:51:01 -07001245TEST_F(HidlTest, BarThisIsNewTest) {
Iliyan Malchev0acf4192016-08-22 19:33:20 -07001246 // Now the tricky part, get access to the derived interface.
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001247 ALOGI("CLIENT call thisIsNew.");
Yifan Hong1dc87932016-08-19 09:51:01 -07001248 EXPECT_OK(bar->thisIsNew());
Iliyan Malchevb31e10c2016-08-13 23:03:25 -07001249 ALOGI("CLIENT thisIsNew returned.");
Andreas Huber9cd48d02016-08-03 14:25:59 -07001250}
1251
Yifan Hong1e81c532016-10-18 18:43:46 -07001252static void expectGoodChild(const sp<IChild> &child) {
1253 ASSERT_NE(child.get(), nullptr);
1254 EXPECT_OK(child->doGrandparent());
1255 EXPECT_OK(child->doParent());
1256 EXPECT_OK(child->doChild());
1257}
1258
1259static void expectGoodParent(const sp<IParent> &parent) {
1260 ASSERT_NE(parent.get(), nullptr);
1261 EXPECT_OK(parent->doGrandparent());
1262 EXPECT_OK(parent->doParent());
1263 sp<IChild> child = IChild::castFrom(parent);
1264 expectGoodChild(child);
1265}
1266
1267static void expectGoodGrandparent(const sp<IGrandparent> &grandparent) {
1268 ASSERT_NE(grandparent.get(), nullptr);
1269 EXPECT_OK(grandparent->doGrandparent());
1270 sp<IParent> parent = IParent::castFrom(grandparent);
1271 expectGoodParent(parent);
1272}
1273
Yifan Hong5749b2c2016-11-28 12:52:36 -08001274TEST_F(HidlTest, FooHaveAnInterfaceTest) {
1275
1276 sp<ISimple> in = new Complicated(42);
1277
1278 EXPECT_OK(bar->haveAInterface(
1279 in,
1280 [&](const auto &out) {
1281 ASSERT_NE(out.get(), nullptr);
1282 EXPECT_EQ(out->getCookie(), 42);
1283 EXPECT_OK(out->customVecInt([&](const auto &) { }));
1284 EXPECT_OK(out->customVecStr([&](const auto &) { }));
1285 EXPECT_OK(out->interfaceChain([&](const auto &) { }));
1286 EXPECT_OK(out->mystr([&](const auto &) { }));
1287 EXPECT_OK(out->myhandle([&](const auto &) { }));
1288 }));
1289}
1290
Yifan Hong1e81c532016-10-18 18:43:46 -07001291TEST_F(HidlTest, InheritRemoteGrandparentTest) {
1292 EXPECT_OK(fetcher->getGrandparent(true, [&](const sp<IGrandparent>& grandparent) {
1293 expectGoodGrandparent(grandparent);
1294 }));
1295}
1296
1297TEST_F(HidlTest, InheritLocalGrandparentTest) {
1298 EXPECT_OK(fetcher->getGrandparent(false, [&](const sp<IGrandparent>& grandparent) {
1299 expectGoodGrandparent(grandparent);
1300 }));
1301}
1302
Yifan Hong4b0214d2016-12-07 14:57:57 -08001303TEST_F(HidlTest, InheritRemoteParentTest) {
Yifan Hong1e81c532016-10-18 18:43:46 -07001304 EXPECT_OK(fetcher->getParent(true, [&](const sp<IParent>& parent) {
1305 expectGoodParent(parent);
1306 }));
1307}
1308
Yifan Hong4b0214d2016-12-07 14:57:57 -08001309TEST_F(HidlTest, InheritLocalParentTest) {
Yifan Hong1e81c532016-10-18 18:43:46 -07001310 EXPECT_OK(fetcher->getParent(false, [&](const sp<IParent>& parent) {
1311 expectGoodParent(parent);
1312 }));
1313}
1314
1315TEST_F(HidlTest, InheritRemoteChildTest) {
1316 EXPECT_OK(fetcher->getChild(true, [&](const sp<IChild>& child) {
1317 expectGoodChild(child);
1318 }));
1319}
1320
1321TEST_F(HidlTest, InheritLocalChildTest) {
1322 EXPECT_OK(fetcher->getChild(false, [&](const sp<IChild>& child) {
1323 expectGoodChild(child);
1324 }));
1325}
1326
Andreas Huber5d034772016-09-28 14:23:51 -07001327TEST_F(HidlTest, TestArrayDimensionality) {
1328 hidl_array<int, 2> oneDim;
1329 hidl_array<int, 2, 3> twoDim;
1330 hidl_array<int, 2, 3, 4> threeDim;
1331
1332 EXPECT_EQ(oneDim.size(), 2u);
1333 EXPECT_EQ(twoDim.size(), std::make_tuple(2u, 3u));
1334 EXPECT_EQ(threeDim.size(), std::make_tuple(2u, 3u, 4u));
1335}
1336
Yifan Hongbf459bc2016-08-23 16:50:37 -07001337#if HIDL_RUN_POINTER_TESTS
Andreas Huber9cd48d02016-08-03 14:25:59 -07001338
Yifan Hongbf459bc2016-08-23 16:50:37 -07001339TEST_F(HidlTest, PassAGraphTest) {
1340 IGraph::Graph g;
Yifan Hong398e6fb2016-10-17 11:38:09 -07001341 ::android::simpleGraph(g);
1342 ::android::logSimpleGraph("CLIENT", g);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001343 ALOGI("CLIENT call passAGraph");
1344 EXPECT_OK(graphInterface->passAGraph(g));
1345}
1346
1347TEST_F(HidlTest, GiveAGraphTest) {
1348 EXPECT_OK(graphInterface->giveAGraph([&](const auto &newGraph) {
Yifan Hong398e6fb2016-10-17 11:38:09 -07001349 ::android::logSimpleGraph("CLIENT", newGraph);
1350 EXPECT_TRUE(::android::isSimpleGraph(newGraph));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001351 }));
1352}
1353TEST_F(HidlTest, PassANodeTest) {
1354 IGraph::Node node; node.data = 10;
1355 EXPECT_OK(graphInterface->passANode(node));
1356}
1357TEST_F(HidlTest, PassTwoGraphsTest) {
1358 IGraph::Graph g;
Yifan Hong398e6fb2016-10-17 11:38:09 -07001359 ::android::simpleGraph(g);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001360 EXPECT_OK(graphInterface->passTwoGraphs(&g, &g));
1361}
1362TEST_F(HidlTest, PassAGammaTest) {
1363 IGraph::Theta s; s.data = 500;
1364 IGraph::Alpha a; a.s_ptr = &s;
1365 IGraph::Beta b; b.s_ptr = &s;
1366 IGraph::Gamma c; c.a_ptr = &a; c.b_ptr = &b;
1367 ALOGI("CLIENT calling passAGamma: c.a = %p, c.b = %p, c.a->s = %p, c.b->s = %p",
1368 c.a_ptr, c.b_ptr, c.a_ptr->s_ptr, c.b_ptr->s_ptr);
1369 EXPECT_OK(graphInterface->passAGamma(c));
1370}
1371TEST_F(HidlTest, PassNullTest) {
1372 IGraph::Gamma c;
1373 c.a_ptr = nullptr;
1374 c.b_ptr = nullptr;
1375 EXPECT_OK(graphInterface->passAGamma(c));
1376}
1377TEST_F(HidlTest, PassASimpleRefTest) {
1378 IGraph::Theta s;
1379 s.data = 500;
1380 IGraph::Alpha a;
1381 a.s_ptr = &s;
1382 EXPECT_OK(graphInterface->passASimpleRef(&a));
1383}
1384TEST_F(HidlTest, PassASimpleRefSTest) {
1385 IGraph::Theta s;
1386 s.data = 500;
1387 ALOGI("CLIENT call passASimpleRefS with %p", &s);
1388 EXPECT_OK(graphInterface->passASimpleRefS(&s));
1389}
1390TEST_F(HidlTest, GiveASimpleRefTest) {
1391 EXPECT_OK(graphInterface->giveASimpleRef([&](const auto & a_ptr) {
1392 EXPECT_EQ(a_ptr->s_ptr->data, 500);
1393 }));
1394}
1395TEST_F(HidlTest, GraphReportErrorsTest) {
1396 Return<int32_t> ret = graphInterface->getErrors();
1397 EXPECT_OK(ret);
1398 EXPECT_EQ(int32_t(ret), 0);
1399}
1400
1401TEST_F(HidlTest, PointerPassOldBufferTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001402 EXPECT_OK(validationPointerInterface->bar1([&](const auto& sptr, const auto& s) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001403 EXPECT_OK(pointerInterface->foo1(sptr, s));
1404 }));
1405}
1406TEST_F(HidlTest, PointerPassOldBufferTest2) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001407 EXPECT_OK(validationPointerInterface->bar2([&](const auto& s, const auto& a) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001408 EXPECT_OK(pointerInterface->foo2(s, a));
1409 }));
1410}
1411TEST_F(HidlTest, PointerPassSameOldBufferPointerTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001412 EXPECT_OK(validationPointerInterface->bar3([&](const auto& s, const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001413 EXPECT_OK(pointerInterface->foo3(s, a, b));
1414 }));
1415}
1416TEST_F(HidlTest, PointerPassOnlyTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001417 EXPECT_OK(validationPointerInterface->bar4([&](const auto& s) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001418 EXPECT_OK(pointerInterface->foo4(s));
1419 }));
1420}
1421TEST_F(HidlTest, PointerPassTwoEmbeddedTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001422 EXPECT_OK(validationPointerInterface->bar5([&](const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001423 EXPECT_OK(pointerInterface->foo5(a, b));
1424 }));
1425}
1426TEST_F(HidlTest, PointerPassIndirectBufferHasDataTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001427 EXPECT_OK(validationPointerInterface->bar6([&](const auto& a) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001428 EXPECT_OK(pointerInterface->foo6(a));
1429 }));
1430}
1431TEST_F(HidlTest, PointerPassTwoIndirectBufferTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001432 EXPECT_OK(validationPointerInterface->bar7([&](const auto& a, const auto& b) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001433 EXPECT_OK(pointerInterface->foo7(a, b));
1434 }));
1435}
1436TEST_F(HidlTest, PointerPassDeeplyIndirectTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001437 EXPECT_OK(validationPointerInterface->bar8([&](const auto& d) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001438 EXPECT_OK(pointerInterface->foo8(d));
1439 }));
1440}
1441TEST_F(HidlTest, PointerPassStringRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001442 EXPECT_OK(validationPointerInterface->bar9([&](const auto& str) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001443 EXPECT_OK(pointerInterface->foo9(str));
1444 }));
1445}
1446TEST_F(HidlTest, PointerPassRefVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001447 EXPECT_OK(validationPointerInterface->bar10([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001448 EXPECT_OK(pointerInterface->foo10(v));
1449 }));
1450}
1451TEST_F(HidlTest, PointerPassVecRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001452 EXPECT_OK(validationPointerInterface->bar11([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001453 EXPECT_OK(pointerInterface->foo11(v));
1454 }));
1455}
1456TEST_F(HidlTest, PointerPassArrayRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001457 EXPECT_OK(validationPointerInterface->bar12([&](const auto& array) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001458 EXPECT_OK(pointerInterface->foo12(array));
1459 }));
1460}
1461TEST_F(HidlTest, PointerPassRefArrayTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001462 EXPECT_OK(validationPointerInterface->bar13([&](const auto& array) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001463 EXPECT_OK(pointerInterface->foo13(array));
1464 }));
1465}
1466TEST_F(HidlTest, PointerPass3RefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001467 EXPECT_OK(validationPointerInterface->bar14([&](const auto& p3) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001468 EXPECT_OK(pointerInterface->foo14(p3));
1469 }));
1470}
1471TEST_F(HidlTest, PointerPassInt3RefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001472 EXPECT_OK(validationPointerInterface->bar15([&](const auto& p3) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001473 EXPECT_OK(pointerInterface->foo15(p3));
1474 }));
1475}
1476TEST_F(HidlTest, PointerPassEmbeddedPointersTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001477 EXPECT_OK(validationPointerInterface->bar16([&](const auto& p) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001478 EXPECT_OK(pointerInterface->foo16(p));
1479 }));
1480}
1481TEST_F(HidlTest, PointerPassEmbeddedPointers2Test) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001482 EXPECT_OK(validationPointerInterface->bar17([&](const auto& p) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001483 EXPECT_OK(pointerInterface->foo17(p));
1484 }));
1485}
1486TEST_F(HidlTest, PointerPassCopiedStringTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001487 EXPECT_OK(validationPointerInterface->bar18([&](const auto& str_ref, const auto& str_ref2, const auto& str) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001488 EXPECT_OK(pointerInterface->foo18(str_ref, str_ref2, str));
1489 }));
1490}
1491TEST_F(HidlTest, PointerPassCopiedVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001492 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 -07001493 EXPECT_OK(pointerInterface->foo19(a_vec_ref, a_vec, a_vec_ref2));
1494 }));
1495}
Yifan Hong84465902016-09-27 15:52:17 -07001496TEST_F(HidlTest, PointerPassBigRefVecTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001497 EXPECT_OK(validationPointerInterface->bar20([&](const auto& v) {
Yifan Hong84465902016-09-27 15:52:17 -07001498 EXPECT_FAIL(pointerInterface->foo20(v));
1499 }));
1500}
Yifan Hongbf459bc2016-08-23 16:50:37 -07001501TEST_F(HidlTest, PointerPassMultidimArrayRefTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001502 EXPECT_OK(validationPointerInterface->bar21([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001503 EXPECT_OK(pointerInterface->foo21(v));
1504 }));
1505}
1506TEST_F(HidlTest, PointerPassRefMultidimArrayTest) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001507 EXPECT_OK(validationPointerInterface->bar22([&](const auto& v) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001508 EXPECT_OK(pointerInterface->foo22(v));
1509 }));
1510}
1511TEST_F(HidlTest, PointerGiveOldBufferTest) {
1512 EXPECT_OK(pointerInterface->bar1([&](const auto& sptr, const auto& s) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001513 EXPECT_OK(validationPointerInterface->foo1(sptr, s));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001514 }));
1515}
1516TEST_F(HidlTest, PointerGiveOldBufferTest2) {
1517 EXPECT_OK(pointerInterface->bar2([&](const auto& s, const auto& a) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001518 EXPECT_OK(validationPointerInterface->foo2(s, a));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001519 }));
1520}
1521TEST_F(HidlTest, PointerGiveSameOldBufferPointerTest) {
1522 EXPECT_OK(pointerInterface->bar3([&](const auto& s, const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001523 EXPECT_OK(validationPointerInterface->foo3(s, a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001524 }));
1525}
1526TEST_F(HidlTest, PointerGiveOnlyTest) {
1527 EXPECT_OK(pointerInterface->bar4([&](const auto& s) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001528 EXPECT_OK(validationPointerInterface->foo4(s));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001529 }));
1530}
1531TEST_F(HidlTest, PointerGiveTwoEmbeddedTest) {
1532 EXPECT_OK(pointerInterface->bar5([&](const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001533 EXPECT_OK(validationPointerInterface->foo5(a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001534 }));
1535}
1536TEST_F(HidlTest, PointerGiveIndirectBufferHasDataTest) {
1537 EXPECT_OK(pointerInterface->bar6([&](const auto& a) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001538 EXPECT_OK(validationPointerInterface->foo6(a));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001539 }));
1540}
1541TEST_F(HidlTest, PointerGiveTwoIndirectBufferTest) {
1542 EXPECT_OK(pointerInterface->bar7([&](const auto& a, const auto& b) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001543 EXPECT_OK(validationPointerInterface->foo7(a, b));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001544 }));
1545}
1546TEST_F(HidlTest, PointerGiveDeeplyIndirectTest) {
1547 EXPECT_OK(pointerInterface->bar8([&](const auto& d) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001548 EXPECT_OK(validationPointerInterface->foo8(d));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001549 }));
1550}
1551TEST_F(HidlTest, PointerGiveStringRefTest) {
1552 EXPECT_OK(pointerInterface->bar9([&](const auto& str) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001553 EXPECT_OK(validationPointerInterface->foo9(str));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001554 }));
1555}
1556TEST_F(HidlTest, PointerGiveRefVecTest) {
1557 EXPECT_OK(pointerInterface->bar10([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001558 EXPECT_OK(validationPointerInterface->foo10(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001559 }));
1560}
1561TEST_F(HidlTest, PointerGiveVecRefTest) {
1562 EXPECT_OK(pointerInterface->bar11([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001563 EXPECT_OK(validationPointerInterface->foo11(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001564 }));
1565}
1566TEST_F(HidlTest, PointerGiveArrayRefTest) {
1567 EXPECT_OK(pointerInterface->bar12([&](const auto& array) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001568 EXPECT_OK(validationPointerInterface->foo12(array));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001569 }));
1570}
1571TEST_F(HidlTest, PointerGiveRefArrayTest) {
1572 EXPECT_OK(pointerInterface->bar13([&](const auto& array) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001573 EXPECT_OK(validationPointerInterface->foo13(array));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001574 }));
1575}
1576TEST_F(HidlTest, PointerGive3RefTest) {
1577 EXPECT_OK(pointerInterface->bar14([&](const auto& p3) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001578 EXPECT_OK(validationPointerInterface->foo14(p3));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001579 }));
1580}
1581TEST_F(HidlTest, PointerGiveInt3RefTest) {
1582 EXPECT_OK(pointerInterface->bar15([&](const auto& p3) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001583 EXPECT_OK(validationPointerInterface->foo15(p3));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001584 }));
1585}
1586TEST_F(HidlTest, PointerGiveEmbeddedPointersTest) {
1587 EXPECT_OK(pointerInterface->bar16([&](const auto& p) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001588 EXPECT_OK(validationPointerInterface->foo16(p));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001589 }));
1590}
1591TEST_F(HidlTest, PointerGiveEmbeddedPointers2Test) {
1592 EXPECT_OK(pointerInterface->bar17([&](const auto& p) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001593 EXPECT_OK(validationPointerInterface->foo17(p));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001594 }));
1595}
1596TEST_F(HidlTest, PointerGiveCopiedStringTest) {
1597 EXPECT_OK(pointerInterface->bar18([&](const auto& str_ref, const auto& str_ref2, const auto& str) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001598 EXPECT_OK(validationPointerInterface->foo18(str_ref, str_ref2, str));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001599 }));
1600}
1601TEST_F(HidlTest, PointerGiveCopiedVecTest) {
1602 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 -07001603 EXPECT_OK(validationPointerInterface->foo19(a_vec_ref, a_vec, a_vec_ref2));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001604 }));
1605}
Yifan Hong84465902016-09-27 15:52:17 -07001606// This cannot be enabled until _hidl_error is not ignored when
1607// the remote writeEmbeddedReferencesToParcel.
1608// TEST_F(HidlTest, PointerGiveBigRefVecTest) {
1609// EXPECT_FAIL(pointerInterface->bar20([&](const auto& v) {
1610// }));
1611// }
Yifan Hongbf459bc2016-08-23 16:50:37 -07001612TEST_F(HidlTest, PointerGiveMultidimArrayRefTest) {
1613 EXPECT_OK(pointerInterface->bar21([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001614 EXPECT_OK(validationPointerInterface->foo21(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001615 }));
1616}
1617TEST_F(HidlTest, PointerGiveRefMultidimArrayTest) {
1618 EXPECT_OK(pointerInterface->bar22([&](const auto& v) {
Yifan Hong3cccc0f2016-10-13 10:12:28 -07001619 EXPECT_OK(validationPointerInterface->foo22(v));
Yifan Hongbf459bc2016-08-23 16:50:37 -07001620 }));
1621}
1622TEST_F(HidlTest, PointerReportErrorsTest) {
1623 Return<int32_t> ret = pointerInterface->getErrors();
1624 EXPECT_OK(ret);
1625 EXPECT_EQ(int32_t(ret), 0);
1626}
1627#endif
1628
Yifan Hongc70f0d82016-10-10 14:50:22 -07001629int forkAndRunTests(TestMode mode) {
1630 pid_t child;
1631 int status;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001632
Yifan Hongc70f0d82016-10-10 14:50:22 -07001633 const char* modeText = (mode == BINDERIZED) ? "BINDERIZED" : "PASSTHROUGH";
1634 ALOGI("Start running tests in %s mode...", modeText);
1635 fprintf(stdout, "Start running tests in %s mode...\n", modeText);
1636 fflush(stdout);
1637
1638 if ((child = fork()) == 0) {
1639 gMode = mode;
1640 if (gMode == PASSTHROUGH) {
Yifan Hongca890522016-10-12 10:03:41 -07001641 gPassthroughEnvironment = static_cast<PassthroughEnvironment *>(
1642 ::testing::AddGlobalTestEnvironment(new PassthroughEnvironment));
Yifan Hongc70f0d82016-10-10 14:50:22 -07001643 } else if (gMode == BINDERIZED) {
Yifan Hongca890522016-10-12 10:03:41 -07001644 gBinderizedEnvironment = static_cast<BinderizedEnvironment *>(
1645 ::testing::AddGlobalTestEnvironment(new BinderizedEnvironment));
Yifan Hongc70f0d82016-10-10 14:50:22 -07001646 }
1647 int testStatus = RUN_ALL_TESTS();
1648 if(testStatus == 0) {
1649 exit(0);
1650 }
1651 int failed = ::testing::UnitTest::GetInstance()->failed_test_count();
1652 if (failed == 0) {
1653 exit(-testStatus);
1654 }
1655 exit(failed);
1656 }
1657 waitpid(child, &status, 0 /* options */);
1658 ALOGI("All tests finished in %s mode.", modeText);
1659 fprintf(stdout, "All tests finished in %s mode.\n", modeText);
1660 fflush(stdout);
Yifan Hong1dc87932016-08-19 09:51:01 -07001661 return status;
Andreas Huber9cd48d02016-08-03 14:25:59 -07001662}
Yifan Hongc70f0d82016-10-10 14:50:22 -07001663
1664void handleStatus(int status, const char *mode) {
1665 if (status != 0) {
1666 if (WIFEXITED(status)) {
1667 status = WEXITSTATUS(status);
1668 if (status < 0) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001669 fprintf(stdout, " RUN_ALL_TESTS returns %d for %s mode.\n", -status, mode);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001670 } else {
Yifan Hong48562fd2016-10-24 15:26:10 -07001671 fprintf(stdout, " %d test(s) failed for %s mode.\n", status, mode);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001672 }
1673 } else {
Yifan Hong48562fd2016-10-24 15:26:10 -07001674 fprintf(stdout, " ERROR: %s child process exited abnormally with %d\n", mode, status);
Yifan Hongc70f0d82016-10-10 14:50:22 -07001675 }
1676 }
1677}
1678
Yifan Hongd12398d2016-10-13 11:05:29 -07001679static void usage(const char *me) {
1680 fprintf(stderr,
1681 "usage: %s [-b] [-p] [GTEST_OPTIONS]\n",
1682 me);
1683
1684 fprintf(stderr, " -b binderized mode only\n");
1685 fprintf(stderr, " -p passthrough mode only\n");
1686 fprintf(stderr, " (if -b and -p are both missing or both present, "
1687 "both modes are tested.)\n");
1688}
1689
Yifan Hongc70f0d82016-10-10 14:50:22 -07001690int main(int argc, char **argv) {
Yifan Hongd12398d2016-10-13 11:05:29 -07001691 const char *me = argv[0];
1692 bool b = false;
1693 bool p = false;
1694 struct option longopts[] = {{0,0,0,0}};
1695 int res;
1696 while ((res = getopt_long(argc, argv, "hbp", longopts, NULL)) >= 0) {
1697 switch (res) {
1698 case 'h': {
1699 usage(me);
1700 exit(1);
1701 } break;
1702
1703 case 'b': {
1704 b = true;
1705 } break;
1706
1707 case 'p': {
1708 p = true;
1709 } break;
1710
1711 case '?':
1712 default: {
1713 // ignore. pass to gTest.
1714 } break;
1715 }
1716 }
1717 if (!b && !p) {
1718 b = p = true;
1719 }
Yifan Hongc70f0d82016-10-10 14:50:22 -07001720
1721 ::testing::InitGoogleTest(&argc, argv);
1722 // put test in child process because RUN_ALL_TESTS
1723 // should not be run twice.
Yifan Hongd12398d2016-10-13 11:05:29 -07001724 int pStatus = p ? forkAndRunTests(PASSTHROUGH) : 0;
1725 int bStatus = b ? forkAndRunTests(BINDERIZED) : 0;
Yifan Hongc70f0d82016-10-10 14:50:22 -07001726
Yifan Hong48562fd2016-10-24 15:26:10 -07001727 fprintf(stdout, "\n=========================================================\n\n"
1728 " Summary:\n\n");
Yifan Hongd12398d2016-10-13 11:05:29 -07001729 if (p) {
1730 ALOGI("PASSTHROUGH Test result = %d", pStatus);
1731 handleStatus(pStatus, "PASSTHROUGH");
1732 }
1733 if (b) {
1734 ALOGI("BINDERIZED Test result = %d", bStatus);
1735 handleStatus(bStatus, "BINDERIZED ");
1736 }
1737
Yifan Hongc70f0d82016-10-10 14:50:22 -07001738 if (pStatus == 0 && bStatus == 0) {
Yifan Hong48562fd2016-10-24 15:26:10 -07001739 fprintf(stdout, " Hooray! All tests passed.\n");
Yifan Hongc70f0d82016-10-10 14:50:22 -07001740 }
Yifan Hong48562fd2016-10-24 15:26:10 -07001741 fprintf(stdout, "\n=========================================================\n\n");
Yifan Hongc70f0d82016-10-10 14:50:22 -07001742
1743 return pStatus + bStatus;
1744}