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