Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 1 | #define LOG_TAG "hidl_test" |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 2 | #include <android-base/logging.h> |
| 3 | |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 4 | #include <android/hardware/tests/foo/1.0/BnFoo.h> |
| 5 | #include <android/hardware/tests/foo/1.0/BnFooCallback.h> |
| 6 | #include <android/hardware/tests/bar/1.0/BnBar.h> |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 7 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 9 | #include <inttypes.h> |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 10 | #if GTEST_IS_THREADSAFE |
| 11 | #include <sys/types.h> |
| 12 | #include <signal.h> |
| 13 | #include <errno.h> |
| 14 | #include <pthread.h> |
| 15 | #else |
| 16 | #error "GTest did not detect pthread library." |
| 17 | #endif |
| 18 | |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 19 | #include <hidl/IServiceManager.h> |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 20 | #include <hidl/Status.h> |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 21 | #include <hwbinder/IPCThreadState.h> |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 22 | #include <hwbinder/ProcessState.h> |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 23 | |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 24 | #include <utils/Condition.h> |
| 25 | #include <utils/Timers.h> |
| 26 | |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 27 | using ::android::hardware::tests::foo::V1_0::BnFoo; |
| 28 | using ::android::hardware::tests::foo::V1_0::BnFooCallback; |
| 29 | using ::android::hardware::tests::bar::V1_0::BnBar; |
| 30 | using ::android::hardware::tests::foo::V1_0::IFoo; |
| 31 | using ::android::hardware::tests::foo::V1_0::IFooCallback; |
| 32 | using ::android::hardware::tests::bar::V1_0::IBar; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 33 | using ::android::hardware::tests::bar::V1_0::IHwBar; |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 34 | using ::android::hardware::tests::foo::V1_0::Abc; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 35 | using ::android::hardware::Return; |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 36 | using ::android::hardware::Void; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 37 | using ::android::hardware::hidl_vec; |
| 38 | using ::android::hardware::hidl_string; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 39 | using ::android::sp; |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 40 | using ::android::Mutex; |
| 41 | using ::android::Condition; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 42 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 43 | struct FooCallback : public IFooCallback { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 44 | FooCallback() : invokeInfo{}, mLock{}, mCond{} {} |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 45 | Return<void> heyItsYou(const sp<IFooCallback> &cb) override; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 46 | Return<bool> heyItsYouIsntIt(const sp<IFooCallback> &cb) override; |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 47 | Return<void> heyItsTheMeaningOfLife(uint8_t tmol) override; |
| 48 | Return<void> reportResults(int64_t ns, reportResults_cb cb) override; |
| 49 | Return<void> youBlockedMeFor(const int64_t ns[3]) override; |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 50 | |
Steven Moreland | 60bce22 | 2016-09-07 10:17:43 -0700 | [diff] [blame] | 51 | static constexpr nsecs_t DELAY_S = 1; |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 52 | static constexpr nsecs_t DELAY_NS = seconds_to_nanoseconds(DELAY_S); |
| 53 | static constexpr nsecs_t TOLERANCE_NS = milliseconds_to_nanoseconds(10); |
| 54 | static constexpr nsecs_t ONEWAY_TOLERANCE_NS = milliseconds_to_nanoseconds(1); |
| 55 | |
| 56 | InvokeInfo invokeInfo[3]; |
| 57 | Mutex mLock; |
| 58 | Condition mCond; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 61 | Return<void> FooCallback::heyItsYou( |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 62 | const sp<IFooCallback> &_cb) { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 63 | nsecs_t start = systemTime(); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 64 | ALOGI("SERVER(FooCallback) heyItsYou cb = %p", _cb.get()); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 65 | mLock.lock(); |
| 66 | invokeInfo[0].invoked = true; |
| 67 | invokeInfo[0].timeNs = systemTime() - start; |
| 68 | mCond.signal(); |
| 69 | mLock.unlock(); |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 70 | return Void(); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 71 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 72 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 73 | Return<bool> FooCallback::heyItsYouIsntIt(const sp<IFooCallback> &_cb) { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 74 | nsecs_t start = systemTime(); |
| 75 | ALOGI("SERVER(FooCallback) heyItsYouIsntIt cb = %p sleeping for %" PRId64 " seconds", _cb.get(), DELAY_S); |
| 76 | sleep(DELAY_S); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 77 | ALOGI("SERVER(FooCallback) heyItsYouIsntIt cb = %p responding", _cb.get()); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 78 | mLock.lock(); |
| 79 | invokeInfo[1].invoked = true; |
| 80 | invokeInfo[1].timeNs = systemTime() - start; |
| 81 | mCond.signal(); |
| 82 | mLock.unlock(); |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 83 | return true; |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 86 | Return<void> FooCallback::heyItsTheMeaningOfLife(uint8_t tmol) { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 87 | nsecs_t start = systemTime(); |
| 88 | ALOGI("SERVER(FooCallback) heyItsTheMeaningOfLife = %d sleeping for %" PRId64 " seconds", tmol, DELAY_S); |
| 89 | sleep(DELAY_S); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 90 | ALOGI("SERVER(FooCallback) heyItsTheMeaningOfLife = %d done sleeping", tmol); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 91 | mLock.lock(); |
| 92 | invokeInfo[2].invoked = true; |
| 93 | invokeInfo[2].timeNs = systemTime() - start; |
| 94 | mCond.signal(); |
| 95 | mLock.unlock(); |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 96 | return Void(); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 99 | Return<void> FooCallback::reportResults(int64_t ns, reportResults_cb cb) { |
Andreas Huber | 03866f5 | 2016-08-30 14:19:52 -0700 | [diff] [blame] | 100 | ALOGI("SERVER(FooCallback) reportResults(%" PRId64 " seconds)", nanoseconds_to_seconds(ns)); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 101 | nsecs_t leftToWaitNs = ns; |
| 102 | mLock.lock(); |
| 103 | while (!(invokeInfo[0].invoked && invokeInfo[1].invoked && invokeInfo[2].invoked) && |
| 104 | leftToWaitNs > 0) { |
| 105 | nsecs_t start = systemTime(); |
| 106 | ::android::status_t rc = mCond.waitRelative(mLock, leftToWaitNs); |
| 107 | if (rc != ::android::OK) { |
Andreas Huber | 03866f5 | 2016-08-30 14:19:52 -0700 | [diff] [blame] | 108 | ALOGI("SERVER(FooCallback)::reportResults(%" PRId64 " ns) Condition::waitRelative(%" PRId64 ") returned error (%d)", ns, leftToWaitNs, rc); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
Andreas Huber | 03866f5 | 2016-08-30 14:19:52 -0700 | [diff] [blame] | 111 | ALOGI("SERVER(FooCallback)::reportResults(%" PRId64 " ns) Condition::waitRelative was signalled", ns); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 112 | leftToWaitNs -= systemTime() - start; |
| 113 | } |
| 114 | mLock.unlock(); |
| 115 | cb(leftToWaitNs, invokeInfo); |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 116 | return Void(); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 119 | Return<void> FooCallback::youBlockedMeFor(const int64_t ns[3]) { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 120 | for (size_t i = 0; i < 3; i++) { |
| 121 | invokeInfo[i].callerBlockedNs = ns[i]; |
| 122 | } |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 123 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 126 | struct Bar : public IBar { |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 127 | Return<void> doThis(float param) override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 128 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 129 | Return<int32_t> doThatAndReturnSomething(int64_t param) override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 130 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 131 | Return<double> doQuiteABit( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 132 | int32_t a, |
| 133 | int64_t b, |
| 134 | float c, |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 135 | double d) override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 136 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 137 | Return<void> doSomethingElse( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 138 | const int32_t param[15], doSomethingElse_cb _cb) override; |
| 139 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 140 | Return<void> doStuffAndReturnAString( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 141 | doStuffAndReturnAString_cb _cb) override; |
| 142 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 143 | Return<void> mapThisVector( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 144 | const hidl_vec<int32_t> ¶m, mapThisVector_cb _cb) override; |
| 145 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 146 | Return<void> callMe( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 147 | const sp<IFooCallback> &cb) override; |
| 148 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 149 | Return<SomeEnum> useAnEnum(SomeEnum param) override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 150 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 151 | Return<void> haveAGooberVec(const hidl_vec<Goober>& param) override; |
| 152 | Return<void> haveAGoober(const Goober &g) override; |
| 153 | Return<void> haveAGooberArray(const Goober lots[20]) override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 154 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 155 | Return<void> haveATypeFromAnotherFile(const Abc &def) override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 156 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 157 | Return<void> haveSomeStrings( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 158 | const hidl_string array[3], |
| 159 | haveSomeStrings_cb _cb) override; |
| 160 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 161 | Return<void> haveAStringVec( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 162 | const hidl_vec<hidl_string> &vector, |
| 163 | haveAStringVec_cb _cb) override; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 164 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 165 | Return<void> thisIsNew() override; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 168 | Return<void> Bar::doThis(float param) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 169 | ALOGI("SERVER(Bar) doThis(%.2f)", param); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 170 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 171 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 174 | Return<int32_t> Bar::doThatAndReturnSomething( |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 175 | int64_t param) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 176 | ALOGI("SERVER(Bar) doThatAndReturnSomething(%ld)", param); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 177 | |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 178 | return 666; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 181 | Return<double> Bar::doQuiteABit( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 182 | int32_t a, |
| 183 | int64_t b, |
| 184 | float c, |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 185 | double d) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 186 | ALOGI("SERVER(Bar) doQuiteABit(%d, %ld, %.2f, %.2f)", a, b, c, d); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 187 | |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 188 | return 666.5; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 191 | Return<void> Bar::doSomethingElse( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 192 | const int32_t param[15], doSomethingElse_cb _cb) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 193 | ALOGI("SERVER(Bar) doSomethingElse(...)"); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 194 | |
| 195 | int32_t result[32] = { 0 }; |
| 196 | for (size_t i = 0; i < 15; ++i) { |
| 197 | result[i] = 2 * param[i]; |
| 198 | result[15 + i] = param[i]; |
| 199 | } |
| 200 | result[30] = 1; |
| 201 | result[31] = 2; |
| 202 | |
| 203 | _cb(result); |
| 204 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 205 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 208 | Return<void> Bar::doStuffAndReturnAString( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 209 | doStuffAndReturnAString_cb _cb) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 210 | ALOGI("SERVER(Bar) doStuffAndReturnAString"); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 211 | |
| 212 | hidl_string s; |
| 213 | s = "Hello, world"; |
| 214 | |
| 215 | _cb(s); |
| 216 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 217 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 220 | Return<void> Bar::mapThisVector( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 221 | const hidl_vec<int32_t> ¶m, mapThisVector_cb _cb) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 222 | ALOGI("SERVER(Bar) mapThisVector"); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 223 | |
| 224 | hidl_vec<int32_t> out; |
Andreas Huber | 899b063 | 2016-08-15 12:21:19 -0700 | [diff] [blame] | 225 | out.resize(param.size()); |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 226 | |
| 227 | for (size_t i = 0; i < out.size(); ++i) { |
| 228 | out[i] = param[i] * 2; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | _cb(out); |
| 232 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 233 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 236 | Return<void> Bar::callMe( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 237 | const sp<IFooCallback> &cb) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 238 | ALOGI("SERVER(Bar) callMe %p", cb.get()); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 239 | |
| 240 | if (cb != NULL) { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 241 | |
| 242 | nsecs_t c[3]; |
| 243 | ALOGI("SERVER(Bar) callMe %p calling IFooCallback::heyItsYou, " \ |
| 244 | "should return immediately", cb.get()); |
| 245 | c[0] = systemTime(); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 246 | cb->heyItsYou(cb); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 247 | c[0] = systemTime() - c[0]; |
| 248 | ALOGI("SERVER(Bar) callMe %p calling IFooCallback::heyItsYou " \ |
| 249 | "returned after %" PRId64 "ns", cb.get(), c[0]); |
| 250 | |
| 251 | ALOGI("SERVER(Bar) callMe %p calling IFooCallback::heyItsYouIsntIt, " \ |
| 252 | "should block for %" PRId64 " seconds", cb.get(), |
| 253 | FooCallback::DELAY_S); |
| 254 | c[1] = systemTime(); |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 255 | bool answer = cb->heyItsYouIsntIt(cb); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 256 | c[1] = systemTime() - c[1]; |
| 257 | ALOGI("SERVER(Bar) callMe %p IFooCallback::heyItsYouIsntIt " \ |
| 258 | "responded with %d after %" PRId64 "ns", cb.get(), answer, c[1]); |
| 259 | |
| 260 | ALOGI("SERVER(Bar) callMe %p calling " \ |
| 261 | "IFooCallback::heyItsTheMeaningOfLife, " \ |
| 262 | "should return immediately ", cb.get()); |
| 263 | c[2] = systemTime(); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 264 | cb->heyItsTheMeaningOfLife(42); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 265 | c[2] = systemTime() - c[2]; |
| 266 | ALOGI("SERVER(Bar) callMe %p After call to " \ |
| 267 | "IFooCallback::heyItsTheMeaningOfLife " \ |
| 268 | "responded after %" PRId64 "ns", cb.get(), c[2]); |
| 269 | |
| 270 | ALOGI("SERVER(Bar) callMe %p calling IFooCallback::youBlockedMeFor " \ |
| 271 | "to report times", cb.get()); |
| 272 | cb->youBlockedMeFor(c); |
| 273 | ALOGI("SERVER(Bar) callMe %p After call to " \ |
| 274 | "IFooCallback::heyYouBlockedMeFor", cb.get()); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 277 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 280 | Return<Bar::SomeEnum> Bar::useAnEnum(SomeEnum param) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 281 | ALOGI("SERVER(Bar) useAnEnum %d", (int)param); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 282 | |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 283 | return SomeEnum::goober; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 286 | Return<void> Bar::haveAGooberVec(const hidl_vec<Goober>& param) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 287 | ALOGI("SERVER(Bar) haveAGooberVec ¶m = %p", ¶m); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 288 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 289 | return Void(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 292 | Return<void> Bar::haveAGoober(const Goober &g) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 293 | ALOGI("SERVER(Bar) haveaGoober g=%p", &g); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 294 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 295 | return Void(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 298 | Return<void> Bar::haveAGooberArray(const Goober lots[20]) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 299 | ALOGI("SERVER(Bar) haveAGooberArray lots = %p", lots); |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 300 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 301 | return Void(); |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 304 | Return<void> Bar::haveATypeFromAnotherFile(const Abc &def) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 305 | ALOGI("SERVER(Bar) haveATypeFromAnotherFile def=%p", &def); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 306 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 307 | return Void(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 310 | Return<void> Bar::haveSomeStrings( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 311 | const hidl_string array[3], |
| 312 | haveSomeStrings_cb _cb) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 313 | ALOGI("SERVER(Bar) haveSomeStrings([\"%s\", \"%s\", \"%s\"])", |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 314 | array[0].c_str(), |
| 315 | array[1].c_str(), |
| 316 | array[2].c_str()); |
| 317 | |
| 318 | hidl_string result[2]; |
| 319 | result[0] = "Hello"; |
| 320 | result[1] = "World"; |
| 321 | |
| 322 | _cb(result); |
| 323 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 324 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 327 | Return<void> Bar::haveAStringVec( |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 328 | const hidl_vec<hidl_string> &vector, |
| 329 | haveAStringVec_cb _cb) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 330 | ALOGI("SERVER(Bar) haveAStringVec([\"%s\", \"%s\", \"%s\"])", |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 331 | vector[0].c_str(), |
| 332 | vector[1].c_str(), |
| 333 | vector[2].c_str()); |
| 334 | |
| 335 | hidl_vec<hidl_string> result; |
| 336 | result.resize(2); |
| 337 | |
| 338 | result[0] = "Hello"; |
| 339 | result[1] = "World"; |
| 340 | |
| 341 | _cb(result); |
| 342 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 343 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 346 | Return<void> Bar::thisIsNew() { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 347 | ALOGI("SERVER(Bar) thisIsNew"); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 348 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 349 | return Void(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 352 | template<typename I> |
| 353 | static std::string arraylikeToString(const I data, size_t size) { |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 354 | std::string out = "["; |
| 355 | for (size_t i = 0; i < size; ++i) { |
| 356 | if (i > 0) { |
| 357 | out += ", "; |
| 358 | } |
| 359 | |
| 360 | out += ::android::String8::format("%d", data[i]).string(); |
| 361 | } |
| 362 | out += "]"; |
| 363 | |
| 364 | return out; |
| 365 | } |
| 366 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 367 | |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 368 | static std::string vecToString(const hidl_vec<int32_t> &vec) { |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 369 | return arraylikeToString(vec, vec.size()); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 372 | template <typename T> |
| 373 | static inline void EXPECT_OK(::android::hardware::Return<T> ret) { |
| 374 | EXPECT_TRUE(ret.getStatus().isOk()); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | template<typename T, typename S> |
| 378 | static inline bool EXPECT_ARRAYEQ(const T arr1, const S arr2, size_t size) { |
| 379 | for(size_t i = 0; i < size; i++) |
| 380 | if(arr1[i] != arr2[i]) |
| 381 | return false; |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | template <class T> |
| 387 | static void startServer(T server, const android::hardware::hidl_version kVersion, |
| 388 | const char *serviceName, |
| 389 | const char *tag) { |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 390 | using namespace android::hardware; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 391 | using android::String16; |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 392 | ALOGI("SERVER(%s) registering", tag); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 393 | server->registerAsService(String16(serviceName), kVersion); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 394 | ALOGI("SERVER(%s) starting", tag); |
| 395 | ProcessState::self()->setThreadPoolMaxThreadCount(0); |
| 396 | ProcessState::self()->startThreadPool(); |
| 397 | IPCThreadState::self()->joinThreadPool(); // never ends. needs kill(). |
| 398 | ALOGI("SERVER(%s) ends.", tag); |
| 399 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 400 | |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 401 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 402 | class HidlTest : public ::testing::Test { |
| 403 | public: |
| 404 | sp<::android::hardware::IBinder> service; |
| 405 | sp<IFoo> foo; |
| 406 | sp<IBar> bar; |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 407 | sp<IFooCallback> fooCb; |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 408 | sp<::android::hardware::IBinder> cbService; |
| 409 | virtual void SetUp() override { |
| 410 | ALOGI("Test setup beginning..."); |
| 411 | using namespace android::hardware; |
| 412 | using android::String16; |
| 413 | const hidl_version kVersion = make_hidl_version(1, 0); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 414 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 415 | foo = IFoo::getService(String16("foo"), kVersion); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 416 | CHECK(foo != NULL); |
| 417 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 418 | bar = IBar::getService(String16("foo"), kVersion); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 419 | CHECK(bar != NULL); |
| 420 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 421 | fooCb = IFooCallback::getService(String16("foo callback"), kVersion); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 422 | CHECK(fooCb != NULL); |
| 423 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 424 | ALOGI("Test setup complete"); |
| 425 | } |
| 426 | virtual void TearDown() override { |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | class HidlEnvironment : public ::testing::Environment { |
| 431 | private: |
| 432 | pid_t fooCallbackServerPid, barServerPid; |
| 433 | public: |
| 434 | virtual void SetUp() { |
| 435 | ALOGI("Environment setup beginning..."); |
| 436 | // use fork to create and kill to destroy server processes. |
| 437 | if ((barServerPid = fork()) == 0) { |
| 438 | // Fear me, I am a child. |
| 439 | startServer(new Bar, android::hardware::make_hidl_version(1, 0), |
| 440 | "foo", "Bar"); // never returns |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | if ((fooCallbackServerPid = fork()) == 0) { |
| 445 | // Fear me, I am a second child. |
| 446 | startServer(new FooCallback, android::hardware::make_hidl_version(1, 0), |
| 447 | "foo callback", "FooCalback"); // never returns |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | // Fear you not, I am parent. |
| 452 | sleep(1); |
| 453 | ALOGI("Environment setup complete."); |
| 454 | } |
| 455 | |
| 456 | virtual void TearDown() { |
| 457 | // clean up by killing server processes. |
| 458 | ALOGI("Environment tear-down beginning..."); |
| 459 | ALOGI("Killing servers..."); |
| 460 | if(kill(barServerPid, SIGTERM)) { |
| 461 | ALOGE("Could not kill barServer; errno = %d", errno); |
| 462 | } else { |
| 463 | int status; |
| 464 | ALOGI("Waiting for barServer to exit..."); |
| 465 | waitpid(barServerPid, &status, 0); |
| 466 | ALOGI("Continuing..."); |
| 467 | } |
| 468 | if(kill(fooCallbackServerPid, SIGTERM)) { |
| 469 | ALOGE("Could not kill fooCallbackServer; errno = %d", errno); |
| 470 | } else { |
| 471 | int status; |
| 472 | ALOGI("Waiting for fooCallbackServer to exit..."); |
| 473 | waitpid(barServerPid, &status, 0); |
| 474 | ALOGI("Continuing..."); |
| 475 | } |
| 476 | ALOGI("Servers all killed."); |
| 477 | ALOGI("Environment tear-down complete."); |
| 478 | } |
| 479 | }; |
| 480 | |
| 481 | TEST_F(HidlTest, FooDoThisTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 482 | ALOGI("CLIENT call doThis."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 483 | EXPECT_OK(foo->doThis(1.0f)); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 484 | ALOGI("CLIENT doThis returned."); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 485 | EXPECT_EQ(true, true); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 486 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 487 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 488 | TEST_F(HidlTest, FooDoThatAndReturnSomethingTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 489 | ALOGI("CLIENT call doThatAndReturnSomething."); |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 490 | int32_t result = foo->doThatAndReturnSomething(2.0f); |
| 491 | ALOGI("CLIENT doThatAndReturnSomething returned %d.", result); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 492 | EXPECT_EQ(result, 666); |
| 493 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 494 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 495 | TEST_F(HidlTest, FooDoQuiteABitTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 496 | ALOGI("CLIENT call doQuiteABit"); |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 497 | double something = foo->doQuiteABit(1, 2, 3.0f, 4.0); |
| 498 | ALOGI("CLIENT doQuiteABit returned %f.", something); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 499 | EXPECT_DOUBLE_EQ(something, 666.5); |
| 500 | } |
| 501 | |
| 502 | TEST_F(HidlTest, FooDoSomethingElseTest) { |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 503 | |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 504 | ALOGI("CLIENT call doSomethingElse"); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 505 | int32_t param[15]; |
| 506 | for (size_t i = 0; i < sizeof(param) / sizeof(param[0]); ++i) { |
| 507 | param[i] = i; |
| 508 | } |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 509 | EXPECT_OK(foo->doSomethingElse(param, [&](const auto &something) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 510 | ALOGI("CLIENT doSomethingElse returned %s.", |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 511 | arraylikeToString(something, 32).c_str()); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 512 | int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, |
| 513 | 26, 28, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2}; |
| 514 | EXPECT_ARRAYEQ(something, expect, 32); |
| 515 | })); |
| 516 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 517 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 518 | TEST_F(HidlTest, FooDoStuffAndReturnAStringTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 519 | ALOGI("CLIENT call doStuffAndReturnAString"); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 520 | EXPECT_OK(foo->doStuffAndReturnAString([&](const auto &something) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 521 | ALOGI("CLIENT doStuffAndReturnAString returned '%s'.", |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 522 | something.c_str()); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 523 | EXPECT_STREQ(something.c_str(), "Hello, world"); |
| 524 | })); |
| 525 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 526 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 527 | TEST_F(HidlTest, FooMapThisVectorTest) { |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 528 | hidl_vec<int32_t> vecParam; |
| 529 | vecParam.resize(10); |
| 530 | for (size_t i = 0; i < 10; ++i) { |
| 531 | vecParam[i] = i; |
| 532 | } |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 533 | EXPECT_OK(foo->mapThisVector(vecParam, [&](const auto &something) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 534 | ALOGI("CLIENT mapThisVector returned %s.", |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 535 | vecToString(something).c_str()); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 536 | int32_t expect[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}; |
| 537 | EXPECT_ARRAYEQ(something, expect, something.size()); |
| 538 | })); |
| 539 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 540 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 541 | TEST_F(HidlTest, FooCallMeTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 542 | ALOGI("CLIENT call callMe."); |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 543 | // callMe is oneway, should return instantly. |
| 544 | nsecs_t now; |
| 545 | now = systemTime(); |
| 546 | EXPECT_OK(foo->callMe(fooCb)); |
| 547 | EXPECT_TRUE(systemTime() - now < FooCallback::ONEWAY_TOLERANCE_NS); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 548 | ALOGI("CLIENT callMe returned."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 549 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 550 | |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 551 | TEST_F(HidlTest, ForReportResultsTest) { |
| 552 | |
| 553 | // Bar::callMe will invoke three methods on FooCallback; one will return |
| 554 | // right away (even though it is a two-way method); the second one will |
| 555 | // block Bar for FooCallback::DELAY_S seconds, and the third one will return |
| 556 | // to Bar right away (is oneway) but will itself block for DELAY_S seconds. |
| 557 | // We need a way to make sure that these three things have happened within |
| 558 | // 2*DELAY_S seconds plus some small tolerance. |
| 559 | // |
| 560 | // Method FooCallback::reportResults() takes a timeout parameter. It blocks for |
| 561 | // that length of time, while waiting for the three methods above to |
| 562 | // complete. It returns the information of whether each method was invoked, |
| 563 | // as well as how long the body of the method took to execute. We verify |
| 564 | // the information returned by reportResults() against the timeout we pass (which |
| 565 | // is long enough for the method bodies to execute, plus tolerance), and |
| 566 | // verify that eachof them executed, as expected, and took the length of |
| 567 | // time to execute that we also expect. |
| 568 | |
| 569 | const nsecs_t reportResultsNs = |
Andreas Huber | 03866f5 | 2016-08-30 14:19:52 -0700 | [diff] [blame] | 570 | 2 * FooCallback::DELAY_NS + FooCallback::TOLERANCE_NS; |
| 571 | |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 572 | ALOGI("CLIENT: Waiting for up to %" PRId64 " seconds.", |
| 573 | nanoseconds_to_seconds(reportResultsNs)); |
| 574 | |
| 575 | fooCb->reportResults(reportResultsNs, |
| 576 | [&](int64_t timeLeftNs, |
| 577 | const IFooCallback::InvokeInfo invokeResults[3]) { |
| 578 | ALOGI("CLIENT: FooCallback::reportResults() is returning data."); |
| 579 | ALOGI("CLIENT: Waited for %" PRId64 " milliseconds.", |
| 580 | nanoseconds_to_milliseconds(reportResultsNs - timeLeftNs)); |
| 581 | |
| 582 | EXPECT_TRUE(0 <= timeLeftNs && timeLeftNs <= reportResultsNs); |
| 583 | |
| 584 | // two-way method, was supposed to return right away |
| 585 | EXPECT_TRUE(invokeResults[0].invoked); |
| 586 | EXPECT_TRUE(invokeResults[0].timeNs <= invokeResults[0].callerBlockedNs); |
| 587 | EXPECT_TRUE(invokeResults[0].callerBlockedNs <= FooCallback::TOLERANCE_NS); |
| 588 | // two-way method, was supposed to block caller for DELAY_NS |
| 589 | EXPECT_TRUE(invokeResults[1].invoked); |
| 590 | EXPECT_TRUE(invokeResults[1].timeNs <= invokeResults[1].callerBlockedNs); |
| 591 | EXPECT_TRUE(invokeResults[1].callerBlockedNs <= |
| 592 | FooCallback::DELAY_NS + FooCallback::TOLERANCE_NS); |
| 593 | // one-way method, do not block caller, but body was supposed to block for DELAY_NS |
| 594 | EXPECT_TRUE(invokeResults[2].invoked); |
| 595 | EXPECT_TRUE(invokeResults[2].callerBlockedNs <= FooCallback::ONEWAY_TOLERANCE_NS); |
| 596 | EXPECT_TRUE(invokeResults[2].timeNs <= FooCallback::DELAY_NS + FooCallback::TOLERANCE_NS); |
| 597 | }); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 602 | TEST_F(HidlTest, FooUseAnEnumTest) { |
| 603 | ALOGI("CLIENT call useAnEnum."); |
Iliyan Malchev | d856b05 | 2016-08-16 22:25:27 -0700 | [diff] [blame] | 604 | IFoo::SomeEnum sleepy = foo->useAnEnum(IFoo::SomeEnum::quux); |
| 605 | ALOGI("CLIENT useAnEnum returned %u", (unsigned)sleepy); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 606 | EXPECT_EQ(sleepy, IFoo::SomeEnum::goober); |
| 607 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 608 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 609 | TEST_F(HidlTest, FooHaveAGooberTest) { |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 610 | hidl_vec<IFoo::Goober> gooberVecParam; |
| 611 | gooberVecParam.resize(2); |
| 612 | gooberVecParam[0].name = "Hello"; |
| 613 | gooberVecParam[1].name = "World"; |
| 614 | |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 615 | ALOGI("CLIENT call haveAGooberVec."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 616 | EXPECT_OK(foo->haveAGooberVec(gooberVecParam)); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 617 | ALOGI("CLIENT haveAGooberVec returned."); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 618 | |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 619 | ALOGI("CLIENT call haveaGoober."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 620 | EXPECT_OK(foo->haveAGoober(gooberVecParam[0])); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 621 | ALOGI("CLIENT haveaGoober returned."); |
| 622 | |
| 623 | ALOGI("CLIENT call haveAGooberArray."); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 624 | IFoo::Goober gooberArrayParam[20]; |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 625 | EXPECT_OK(foo->haveAGooberArray(gooberArrayParam)); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 626 | ALOGI("CLIENT haveAGooberArray returned."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 627 | } |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 628 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 629 | TEST_F(HidlTest, FooHaveATypeFromAnotherFileTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 630 | ALOGI("CLIENT call haveATypeFromAnotherFile."); |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 631 | Abc abcParam{}; |
| 632 | abcParam.x = "alphabet"; |
| 633 | abcParam.y = 3.14f; |
| 634 | abcParam.z = new native_handle_t(); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 635 | EXPECT_OK(foo->haveATypeFromAnotherFile(abcParam)); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 636 | ALOGI("CLIENT haveATypeFromAnotherFile returned."); |
Steven Moreland | 88ca451 | 2016-08-11 11:24:10 -0700 | [diff] [blame] | 637 | delete abcParam.z; |
| 638 | abcParam.z = NULL; |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 639 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 640 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 641 | TEST_F(HidlTest, FooHaveSomeStringsTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 642 | ALOGI("CLIENT call haveSomeStrings."); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 643 | hidl_string stringArrayParam[3]; |
| 644 | stringArrayParam[0] = "What"; |
| 645 | stringArrayParam[1] = "a"; |
| 646 | stringArrayParam[2] = "disaster"; |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 647 | EXPECT_OK(foo->haveSomeStrings(stringArrayParam)); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 648 | ALOGI("CLIENT haveSomeStrings returned."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 649 | } |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 650 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 651 | TEST_F(HidlTest, FooHaveAStringVecTest) { |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 652 | ALOGI("CLIENT call haveAStringVec."); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 653 | hidl_vec<hidl_string> stringVecParam; |
| 654 | stringVecParam.resize(3); |
| 655 | stringVecParam[0] = "What"; |
| 656 | stringVecParam[1] = "a"; |
| 657 | stringVecParam[2] = "disaster"; |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 658 | EXPECT_OK(foo->haveAStringVec(stringVecParam)); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 659 | ALOGI("CLIENT haveAStringVec returned."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 660 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 661 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 662 | TEST_F(HidlTest, BarThisIsNewTest) { |
Iliyan Malchev | 0acf419 | 2016-08-22 19:33:20 -0700 | [diff] [blame] | 663 | // Now the tricky part, get access to the derived interface. |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 664 | ALOGI("CLIENT call thisIsNew."); |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 665 | EXPECT_OK(bar->thisIsNew()); |
Iliyan Malchev | b31e10c | 2016-08-13 23:03:25 -0700 | [diff] [blame] | 666 | ALOGI("CLIENT thisIsNew returned."); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 669 | int main(int argc, char **argv) { |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 670 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 671 | ::testing::AddGlobalTestEnvironment(new HidlEnvironment); |
| 672 | ::testing::InitGoogleTest(&argc, argv); |
| 673 | int status = RUN_ALL_TESTS(); |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 674 | |
Yifan Hong | 1dc8793 | 2016-08-19 09:51:01 -0700 | [diff] [blame] | 675 | ALOGI("Test result = %d", status); |
| 676 | return status; |
Andreas Huber | 9cd48d0 | 2016-08-03 14:25:59 -0700 | [diff] [blame] | 677 | } |