Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "contexthub_hidl_hal_test" |
| 18 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame^] | 19 | #include "ContexthubCallbackBase.h" |
| 20 | #include "ContexthubHidlTestBase.h" |
| 21 | #include "VtsHalContexthubUtils.h" |
| 22 | |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | #include <android/hardware/contexthub/1.0/IContexthub.h> |
| 25 | #include <android/hardware/contexthub/1.0/IContexthubCallback.h> |
| 26 | #include <android/hardware/contexthub/1.0/types.h> |
| 27 | #include <android/log.h> |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 28 | #include <gtest/gtest.h> |
| 29 | #include <hidl/GtestPrinter.h> |
Steven Moreland | 3eb7df7 | 2017-04-06 12:15:23 -0700 | [diff] [blame] | 30 | #include <log/log.h> |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 31 | |
| 32 | #include <cinttypes> |
| 33 | #include <future> |
| 34 | #include <utility> |
| 35 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 36 | using ::android::sp; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 37 | using ::android::hardware::hidl_string; |
| 38 | using ::android::hardware::hidl_vec; |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 39 | using ::android::hardware::Return; |
| 40 | using ::android::hardware::Void; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 41 | using ::android::hardware::contexthub::V1_0::AsyncEventType; |
| 42 | using ::android::hardware::contexthub::V1_0::ContextHub; |
| 43 | using ::android::hardware::contexthub::V1_0::ContextHubMsg; |
| 44 | using ::android::hardware::contexthub::V1_0::HubAppInfo; |
| 45 | using ::android::hardware::contexthub::V1_0::IContexthub; |
| 46 | using ::android::hardware::contexthub::V1_0::IContexthubCallback; |
| 47 | using ::android::hardware::contexthub::V1_0::NanoAppBinary; |
| 48 | using ::android::hardware::contexthub::V1_0::Result; |
| 49 | using ::android::hardware::contexthub::V1_0::TransactionResult; |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame^] | 50 | using ::android::hardware::contexthub::vts_utils::asBaseType; |
| 51 | using ::android::hardware::contexthub::vts_utils::ContexthubCallbackBase; |
| 52 | using ::android::hardware::contexthub::vts_utils::ContexthubHidlTestBase; |
| 53 | using ::android::hardware::contexthub::vts_utils::getHalAndHubIdList; |
| 54 | using ::android::hardware::contexthub::vts_utils::getHubsSync; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 55 | |
| 56 | namespace { |
| 57 | |
| 58 | // App ID with vendor "GoogT" (Google Testing), app identifier 0x555555. This |
| 59 | // app ID is reserved and must never appear in the list of loaded apps. |
| 60 | constexpr uint64_t kNonExistentAppId = 0x476f6f6754555555; |
| 61 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame^] | 62 | const std::vector<std::tuple<std::string, std::string>> kTestParameters = |
| 63 | getHalAndHubIdList<IContexthub>(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 64 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame^] | 65 | class ContexthubHidlTest : public ContexthubHidlTestBase<IContexthub> {}; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 66 | |
| 67 | // Wait for a callback to occur (signaled by the given future) up to the |
| 68 | // provided timeout. If the future is invalid or the callback does not come |
| 69 | // within the given time, returns false. |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 70 | template <class ReturnType> |
| 71 | bool waitForCallback(std::future<ReturnType> future, ReturnType* result, |
| 72 | std::chrono::milliseconds timeout = std::chrono::seconds(5)) { |
| 73 | auto expiration = std::chrono::system_clock::now() + timeout; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 74 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 75 | EXPECT_NE(result, nullptr); |
| 76 | EXPECT_TRUE(future.valid()); |
| 77 | if (result != nullptr && future.valid()) { |
| 78 | std::future_status status = future.wait_until(expiration); |
| 79 | EXPECT_NE(status, std::future_status::timeout) << "Timed out waiting for callback"; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 80 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 81 | if (status == std::future_status::ready) { |
| 82 | *result = future.get(); |
| 83 | return true; |
| 84 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 85 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 86 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 87 | return false; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Ensures that the metadata reported in getHubs() is sane |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 91 | TEST_P(ContexthubHidlTest, TestGetHubs) { |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame^] | 92 | hidl_vec<ContextHub> hubs = getHubsSync(hubApi.get()); |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 93 | ALOGD("System reports %zu hubs", hubs.size()); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 94 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 95 | for (const ContextHub& hub : hubs) { |
| 96 | ALOGD("Checking hub ID %" PRIu32, hub.hubId); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 97 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 98 | EXPECT_FALSE(hub.name.empty()); |
| 99 | EXPECT_FALSE(hub.vendor.empty()); |
| 100 | EXPECT_FALSE(hub.toolchain.empty()); |
| 101 | EXPECT_GT(hub.peakMips, 0); |
| 102 | EXPECT_GE(hub.stoppedPowerDrawMw, 0); |
| 103 | EXPECT_GE(hub.sleepPowerDrawMw, 0); |
| 104 | EXPECT_GT(hub.peakPowerDrawMw, 0); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 105 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 106 | // Minimum 128 byte MTU as required by CHRE API v1.0 |
| 107 | EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128)); |
| 108 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST_P(ContexthubHidlTest, TestRegisterCallback) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 112 | ALOGD("TestRegisterCallback called, hubId %" PRIu32, getHubId()); |
| 113 | ASSERT_OK(registerCallback(new ContexthubCallbackBase())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_P(ContexthubHidlTest, TestRegisterNullCallback) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 117 | ALOGD("TestRegisterNullCallback called, hubId %" PRIu32, getHubId()); |
| 118 | ASSERT_OK(registerCallback(nullptr)); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Helper callback that puts the async appInfo callback data into a promise |
| 122 | class QueryAppsCallback : public ContexthubCallbackBase { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 123 | public: |
| 124 | virtual Return<void> handleAppsInfo(const hidl_vec<HubAppInfo>& appInfo) override { |
| 125 | ALOGD("Got app info callback with %zu apps", appInfo.size()); |
| 126 | promise.set_value(appInfo); |
| 127 | return Void(); |
| 128 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 129 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 130 | std::promise<hidl_vec<HubAppInfo>> promise; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | // Calls queryApps() and checks the returned metadata |
| 134 | TEST_P(ContexthubHidlTest, TestQueryApps) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 135 | ALOGD("TestQueryApps called, hubId %u", getHubId()); |
| 136 | sp<QueryAppsCallback> cb = new QueryAppsCallback(); |
| 137 | ASSERT_OK(registerCallback(cb)); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 138 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 139 | Result result = hubApi->queryApps(getHubId()); |
| 140 | ASSERT_OK(result); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 141 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 142 | ALOGD("Waiting for app info callback"); |
| 143 | hidl_vec<HubAppInfo> appList; |
| 144 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appList)); |
| 145 | for (const HubAppInfo& appInfo : appList) { |
| 146 | EXPECT_NE(appInfo.appId, UINT64_C(0)); |
| 147 | EXPECT_NE(appInfo.appId, kNonExistentAppId); |
| 148 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Helper callback that puts the TransactionResult for the expectedTxnId into a |
| 152 | // promise |
| 153 | class TxnResultCallback : public ContexthubCallbackBase { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 154 | public: |
| 155 | virtual Return<void> handleTxnResult(uint32_t txnId, TransactionResult result) override { |
| 156 | ALOGD("Got transaction result callback for txnId %" PRIu32 " (expecting %" PRIu32 |
| 157 | ") with result %" PRId32, |
| 158 | txnId, expectedTxnId, result); |
| 159 | if (txnId == expectedTxnId) { |
| 160 | promise.set_value(result); |
| 161 | } |
| 162 | return Void(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 163 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 164 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 165 | uint32_t expectedTxnId = 0; |
| 166 | std::promise<TransactionResult> promise; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | // Parameterized fixture that sets the callback to TxnResultCallback |
| 170 | class ContexthubTxnTest : public ContexthubHidlTest { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 171 | public: |
| 172 | virtual void SetUp() override { |
| 173 | ContexthubHidlTest::SetUp(); |
| 174 | ASSERT_OK(registerCallback(cb)); |
| 175 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 176 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 177 | sp<TxnResultCallback> cb = new TxnResultCallback(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 180 | // Checks cases where the hub implementation is expected to return an error, but |
| 181 | // that error can be returned either synchronously or in the asynchronous |
| 182 | // transaction callback. Returns an AssertionResult that can be used in |
| 183 | // ASSERT/EXPECT_TRUE. Allows checking the sync result against 1 additional |
| 184 | // allowed error code apart from OK and TRANSACTION_FAILED, which are always |
| 185 | // allowed. |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 186 | ::testing::AssertionResult checkFailureSyncOrAsync(Result result, Result allowedSyncResult, |
| 187 | std::future<TransactionResult>&& future) { |
| 188 | if (result == Result::OK) { |
| 189 | // No error reported synchronously - this is OK, but then we should get an |
| 190 | // async callback with a failure status |
| 191 | TransactionResult asyncResult; |
| 192 | if (!waitForCallback(std::forward<std::future<TransactionResult>>(future), &asyncResult)) { |
| 193 | return ::testing::AssertionFailure() |
| 194 | << "Got successful sync result, then failed to receive async cb"; |
| 195 | } else if (asyncResult == TransactionResult::SUCCESS) { |
| 196 | return ::testing::AssertionFailure() |
| 197 | << "Got successful sync result, then unexpected successful async " |
| 198 | "result"; |
| 199 | } |
| 200 | } else if (result != allowedSyncResult && result != Result::TRANSACTION_FAILED) { |
| 201 | return ::testing::AssertionFailure() |
| 202 | << "Got sync result " << asBaseType(result) << ", expected TRANSACTION_FAILED or " |
| 203 | << asBaseType(allowedSyncResult); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 204 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 205 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 206 | return ::testing::AssertionSuccess(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_P(ContexthubTxnTest, TestSendMessageToNonExistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 210 | ContextHubMsg msg; |
| 211 | msg.appName = kNonExistentAppId; |
| 212 | msg.msgType = 1; |
| 213 | msg.msg.resize(4); |
| 214 | std::fill(msg.msg.begin(), msg.msg.end(), 0); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 215 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 216 | ALOGD("Sending message to non-existent nanoapp"); |
| 217 | Result result = hubApi->sendMessageToHub(getHubId(), msg); |
| 218 | if (result != Result::OK && result != Result::BAD_PARAMS && |
| 219 | result != Result::TRANSACTION_FAILED) { |
| 220 | FAIL() << "Got result " << asBaseType(result) << ", expected OK, BAD_PARAMS" |
| 221 | << ", or TRANSACTION_FAILED"; |
| 222 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_P(ContexthubTxnTest, TestLoadEmptyNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 226 | cb->expectedTxnId = 0123; |
| 227 | NanoAppBinary emptyApp; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 228 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 229 | emptyApp.appId = kNonExistentAppId; |
| 230 | emptyApp.appVersion = 1; |
| 231 | emptyApp.flags = 0; |
| 232 | emptyApp.targetChreApiMajorVersion = 1; |
| 233 | emptyApp.targetChreApiMinorVersion = 0; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 234 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 235 | ALOGD("Loading empty nanoapp"); |
| 236 | Result result = hubApi->loadNanoApp(getHubId(), emptyApp, cb->expectedTxnId); |
| 237 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST_P(ContexthubTxnTest, TestUnloadNonexistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 241 | cb->expectedTxnId = 1234; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 242 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 243 | ALOGD("Unloading nonexistent nanoapp"); |
| 244 | Result result = hubApi->unloadNanoApp(getHubId(), kNonExistentAppId, cb->expectedTxnId); |
| 245 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TEST_P(ContexthubTxnTest, TestEnableNonexistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 249 | cb->expectedTxnId = 2345; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 250 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 251 | ALOGD("Enabling nonexistent nanoapp"); |
| 252 | Result result = hubApi->enableNanoApp(getHubId(), kNonExistentAppId, cb->expectedTxnId); |
| 253 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST_P(ContexthubTxnTest, TestDisableNonexistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 257 | cb->expectedTxnId = 3456; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 258 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 259 | ALOGD("Disabling nonexistent nanoapp"); |
| 260 | Result result = hubApi->disableNanoApp(getHubId(), kNonExistentAppId, cb->expectedTxnId); |
| 261 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 264 | INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubHidlTest, testing::ValuesIn(kTestParameters), |
| 265 | android::hardware::PrintInstanceTupleNameToString<>); |
| 266 | |
| 267 | INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubTxnTest, testing::ValuesIn(kTestParameters), |
| 268 | android::hardware::PrintInstanceTupleNameToString<>); |
| 269 | |
| 270 | } // anonymous namespace |