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; |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 55 | using ::android::hardware::contexthub::vts_utils::kNonExistentAppId; |
| 56 | using ::android::hardware::contexthub::vts_utils::waitForCallback; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 57 | |
| 58 | namespace { |
| 59 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 60 | const std::vector<std::tuple<std::string, std::string>> kTestParameters = |
| 61 | getHalAndHubIdList<IContexthub>(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 62 | |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 63 | class ContexthubHidlTest : public ContexthubHidlTestBase<IContexthub> {}; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 64 | |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 65 | class ContexthubCallbackV1_0 : public ContexthubCallbackBase<IContexthubCallback> {}; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 66 | |
| 67 | // Ensures that the metadata reported in getHubs() is sane |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 68 | TEST_P(ContexthubHidlTest, TestGetHubs) { |
Brian Duddie | 01eb01a | 2020-02-14 15:35:57 -0800 | [diff] [blame] | 69 | hidl_vec<ContextHub> hubs = getHubsSync(hubApi.get()); |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 70 | ALOGD("System reports %zu hubs", hubs.size()); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 71 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 72 | for (const ContextHub& hub : hubs) { |
| 73 | ALOGD("Checking hub ID %" PRIu32, hub.hubId); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 74 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 75 | EXPECT_FALSE(hub.name.empty()); |
| 76 | EXPECT_FALSE(hub.vendor.empty()); |
| 77 | EXPECT_FALSE(hub.toolchain.empty()); |
| 78 | EXPECT_GT(hub.peakMips, 0); |
| 79 | EXPECT_GE(hub.stoppedPowerDrawMw, 0); |
| 80 | EXPECT_GE(hub.sleepPowerDrawMw, 0); |
| 81 | EXPECT_GT(hub.peakPowerDrawMw, 0); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 82 | |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 83 | // Minimum 128 byte MTU as required by CHRE API v1.0 |
| 84 | EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128)); |
| 85 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST_P(ContexthubHidlTest, TestRegisterCallback) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 89 | ALOGD("TestRegisterCallback called, hubId %" PRIu32, getHubId()); |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 90 | ASSERT_OK(registerCallback(new ContexthubCallbackV1_0())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_P(ContexthubHidlTest, TestRegisterNullCallback) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 94 | ALOGD("TestRegisterNullCallback called, hubId %" PRIu32, getHubId()); |
| 95 | ASSERT_OK(registerCallback(nullptr)); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Helper callback that puts the async appInfo callback data into a promise |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 99 | class QueryAppsCallback : public ContexthubCallbackV1_0 { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 100 | public: |
| 101 | virtual Return<void> handleAppsInfo(const hidl_vec<HubAppInfo>& appInfo) override { |
| 102 | ALOGD("Got app info callback with %zu apps", appInfo.size()); |
| 103 | promise.set_value(appInfo); |
| 104 | return Void(); |
| 105 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 106 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 107 | std::promise<hidl_vec<HubAppInfo>> promise; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | // Calls queryApps() and checks the returned metadata |
| 111 | TEST_P(ContexthubHidlTest, TestQueryApps) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 112 | ALOGD("TestQueryApps called, hubId %u", getHubId()); |
| 113 | sp<QueryAppsCallback> cb = new QueryAppsCallback(); |
| 114 | ASSERT_OK(registerCallback(cb)); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 115 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 116 | Result result = hubApi->queryApps(getHubId()); |
| 117 | ASSERT_OK(result); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 118 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 119 | ALOGD("Waiting for app info callback"); |
| 120 | hidl_vec<HubAppInfo> appList; |
| 121 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appList)); |
| 122 | for (const HubAppInfo& appInfo : appList) { |
| 123 | EXPECT_NE(appInfo.appId, UINT64_C(0)); |
| 124 | EXPECT_NE(appInfo.appId, kNonExistentAppId); |
| 125 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Helper callback that puts the TransactionResult for the expectedTxnId into a |
| 129 | // promise |
Anthony Stange | a2c3db1 | 2020-12-16 15:04:02 -0500 | [diff] [blame] | 130 | class TxnResultCallback : public ContexthubCallbackV1_0 { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 131 | public: |
| 132 | virtual Return<void> handleTxnResult(uint32_t txnId, TransactionResult result) override { |
| 133 | ALOGD("Got transaction result callback for txnId %" PRIu32 " (expecting %" PRIu32 |
| 134 | ") with result %" PRId32, |
| 135 | txnId, expectedTxnId, result); |
| 136 | if (txnId == expectedTxnId) { |
| 137 | promise.set_value(result); |
| 138 | } |
| 139 | return Void(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 140 | } |
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 | uint32_t expectedTxnId = 0; |
| 143 | std::promise<TransactionResult> promise; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | // Parameterized fixture that sets the callback to TxnResultCallback |
| 147 | class ContexthubTxnTest : public ContexthubHidlTest { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 148 | public: |
| 149 | virtual void SetUp() override { |
| 150 | ContexthubHidlTest::SetUp(); |
| 151 | ASSERT_OK(registerCallback(cb)); |
| 152 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 153 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 154 | sp<TxnResultCallback> cb = new TxnResultCallback(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 157 | // Checks cases where the hub implementation is expected to return an error, but |
| 158 | // that error can be returned either synchronously or in the asynchronous |
| 159 | // transaction callback. Returns an AssertionResult that can be used in |
| 160 | // ASSERT/EXPECT_TRUE. Allows checking the sync result against 1 additional |
| 161 | // allowed error code apart from OK and TRANSACTION_FAILED, which are always |
| 162 | // allowed. |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 163 | ::testing::AssertionResult checkFailureSyncOrAsync(Result result, Result allowedSyncResult, |
| 164 | std::future<TransactionResult>&& future) { |
| 165 | if (result == Result::OK) { |
| 166 | // No error reported synchronously - this is OK, but then we should get an |
| 167 | // async callback with a failure status |
| 168 | TransactionResult asyncResult; |
| 169 | if (!waitForCallback(std::forward<std::future<TransactionResult>>(future), &asyncResult)) { |
| 170 | return ::testing::AssertionFailure() |
| 171 | << "Got successful sync result, then failed to receive async cb"; |
| 172 | } else if (asyncResult == TransactionResult::SUCCESS) { |
| 173 | return ::testing::AssertionFailure() |
| 174 | << "Got successful sync result, then unexpected successful async " |
| 175 | "result"; |
| 176 | } |
| 177 | } else if (result != allowedSyncResult && result != Result::TRANSACTION_FAILED) { |
| 178 | return ::testing::AssertionFailure() |
| 179 | << "Got sync result " << asBaseType(result) << ", expected TRANSACTION_FAILED or " |
| 180 | << asBaseType(allowedSyncResult); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 181 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 182 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 183 | return ::testing::AssertionSuccess(); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_P(ContexthubTxnTest, TestSendMessageToNonExistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 187 | ContextHubMsg msg; |
| 188 | msg.appName = kNonExistentAppId; |
| 189 | msg.msgType = 1; |
| 190 | msg.msg.resize(4); |
| 191 | std::fill(msg.msg.begin(), msg.msg.end(), 0); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 192 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 193 | ALOGD("Sending message to non-existent nanoapp"); |
| 194 | Result result = hubApi->sendMessageToHub(getHubId(), msg); |
| 195 | if (result != Result::OK && result != Result::BAD_PARAMS && |
| 196 | result != Result::TRANSACTION_FAILED) { |
| 197 | FAIL() << "Got result " << asBaseType(result) << ", expected OK, BAD_PARAMS" |
| 198 | << ", or TRANSACTION_FAILED"; |
| 199 | } |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST_P(ContexthubTxnTest, TestLoadEmptyNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 203 | cb->expectedTxnId = 0123; |
| 204 | NanoAppBinary emptyApp; |
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 | emptyApp.appId = kNonExistentAppId; |
| 207 | emptyApp.appVersion = 1; |
| 208 | emptyApp.flags = 0; |
| 209 | emptyApp.targetChreApiMajorVersion = 1; |
| 210 | emptyApp.targetChreApiMinorVersion = 0; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 211 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 212 | ALOGD("Loading empty nanoapp"); |
| 213 | Result result = hubApi->loadNanoApp(getHubId(), emptyApp, cb->expectedTxnId); |
| 214 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_P(ContexthubTxnTest, TestUnloadNonexistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 218 | cb->expectedTxnId = 1234; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 219 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 220 | ALOGD("Unloading nonexistent nanoapp"); |
| 221 | Result result = hubApi->unloadNanoApp(getHubId(), kNonExistentAppId, cb->expectedTxnId); |
| 222 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_P(ContexthubTxnTest, TestEnableNonexistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 226 | cb->expectedTxnId = 2345; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 227 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 228 | ALOGD("Enabling nonexistent nanoapp"); |
| 229 | Result result = hubApi->enableNanoApp(getHubId(), kNonExistentAppId, cb->expectedTxnId); |
| 230 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST_P(ContexthubTxnTest, TestDisableNonexistentNanoApp) { |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 234 | cb->expectedTxnId = 3456; |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 235 | |
Brian Duddie | af8a2f9 | 2020-02-14 15:39:19 -0800 | [diff] [blame] | 236 | ALOGD("Disabling nonexistent nanoapp"); |
| 237 | Result result = hubApi->disableNanoApp(getHubId(), kNonExistentAppId, cb->expectedTxnId); |
| 238 | EXPECT_TRUE(checkFailureSyncOrAsync(result, Result::BAD_PARAMS, cb->promise.get_future())); |
Brian Duddie | cd3a43f | 2016-12-07 16:53:11 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Dan Shi | ff985a8 | 2020-07-29 09:45:03 -0700 | [diff] [blame] | 241 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContexthubHidlTest); |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 242 | INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubHidlTest, testing::ValuesIn(kTestParameters), |
| 243 | android::hardware::PrintInstanceTupleNameToString<>); |
| 244 | |
Dan Shi | ff985a8 | 2020-07-29 09:45:03 -0700 | [diff] [blame] | 245 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContexthubTxnTest); |
Dan Shi | e10b1d6 | 2019-12-12 10:12:52 -0800 | [diff] [blame] | 246 | INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubTxnTest, testing::ValuesIn(kTestParameters), |
| 247 | android::hardware::PrintInstanceTupleNameToString<>); |
| 248 | |
| 249 | } // anonymous namespace |