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
19 #include "ContexthubCallbackBase.h"
20 #include "ContexthubHidlTestBase.h"
21 #include "VtsHalContexthubUtilsCommon.h"
22 #include "VtsHalContexthubUtilsHidl.h"
23
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>
29 #include <gtest/gtest.h>
30 #include <hidl/GtestPrinter.h>
31 #include <log/log.h>
32
33 #include <cinttypes>
34 #include <future>
35 #include <utility>
36
37 using ::android::sp;
38 using ::android::hardware::hidl_string;
39 using ::android::hardware::hidl_vec;
40 using ::android::hardware::Return;
41 using ::android::hardware::Void;
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;
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;
56 using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
57 using ::android::hardware::contexthub::vts_utils::waitForCallback;
58
59 namespace {
60
61 const std::vector<std::tuple<std::string, std::string>> kTestParameters =
62 getHalAndHubIdList<IContexthub>();
63
64 class ContexthubHidlTest : public ContexthubHidlTestBase<IContexthub> {};
65
66 class ContexthubCallbackV1_0 : public ContexthubCallbackBase<IContexthubCallback> {};
67
68 // Ensures that the metadata reported in getHubs() is sane
TEST_P(ContexthubHidlTest,TestGetHubs)69 TEST_P(ContexthubHidlTest, TestGetHubs) {
70 hidl_vec<ContextHub> hubs = getHubsSync(hubApi.get());
71 ALOGD("System reports %zu hubs", hubs.size());
72
73 for (const ContextHub& hub : hubs) {
74 ALOGD("Checking hub ID %" PRIu32, hub.hubId);
75
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);
83
84 // Minimum 128 byte MTU as required by CHRE API v1.0
85 EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128));
86 }
87 }
88
TEST_P(ContexthubHidlTest,TestRegisterCallback)89 TEST_P(ContexthubHidlTest, TestRegisterCallback) {
90 ALOGD("TestRegisterCallback called, hubId %" PRIu32, getHubId());
91 ASSERT_OK(registerCallback(new ContexthubCallbackV1_0()));
92 }
93
TEST_P(ContexthubHidlTest,TestRegisterNullCallback)94 TEST_P(ContexthubHidlTest, TestRegisterNullCallback) {
95 ALOGD("TestRegisterNullCallback called, hubId %" PRIu32, getHubId());
96 ASSERT_OK(registerCallback(nullptr));
97 }
98
99 // Helper callback that puts the async appInfo callback data into a promise
100 class QueryAppsCallback : public ContexthubCallbackV1_0 {
101 public:
handleAppsInfo(const hidl_vec<HubAppInfo> & appInfo)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 }
107
108 std::promise<hidl_vec<HubAppInfo>> promise;
109 };
110
111 // Calls queryApps() and checks the returned metadata
TEST_P(ContexthubHidlTest,TestQueryApps)112 TEST_P(ContexthubHidlTest, TestQueryApps) {
113 ALOGD("TestQueryApps called, hubId %u", getHubId());
114 sp<QueryAppsCallback> cb = new QueryAppsCallback();
115 ASSERT_OK(registerCallback(cb));
116
117 Result result = hubApi->queryApps(getHubId());
118 ASSERT_OK(result);
119
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 }
127 }
128
129 // Helper callback that puts the TransactionResult for the expectedTxnId into a
130 // promise
131 class TxnResultCallback : public ContexthubCallbackV1_0 {
132 public:
handleTxnResult(uint32_t txnId,TransactionResult result)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();
141 }
142
143 uint32_t expectedTxnId = 0;
144 std::promise<TransactionResult> promise;
145 };
146
147 // Parameterized fixture that sets the callback to TxnResultCallback
148 class ContexthubTxnTest : public ContexthubHidlTest {
149 public:
SetUp()150 virtual void SetUp() override {
151 ContexthubHidlTest::SetUp();
152 ASSERT_OK(registerCallback(cb));
153 }
154
155 sp<TxnResultCallback> cb = new TxnResultCallback();
156 };
157
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.
checkFailureSyncOrAsync(Result result,Result allowedSyncResult,std::future<TransactionResult> && future)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);
182 }
183
184 return ::testing::AssertionSuccess();
185 }
186
TEST_P(ContexthubTxnTest,TestSendMessageToNonExistentNanoApp)187 TEST_P(ContexthubTxnTest, TestSendMessageToNonExistentNanoApp) {
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);
193
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 }
201 }
202
TEST_P(ContexthubTxnTest,TestLoadEmptyNanoApp)203 TEST_P(ContexthubTxnTest, TestLoadEmptyNanoApp) {
204 cb->expectedTxnId = 0123;
205 NanoAppBinary emptyApp;
206
207 emptyApp.appId = kNonExistentAppId;
208 emptyApp.appVersion = 1;
209 emptyApp.flags = 0;
210 emptyApp.targetChreApiMajorVersion = 1;
211 emptyApp.targetChreApiMinorVersion = 0;
212
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()));
216 }
217
TEST_P(ContexthubTxnTest,TestUnloadNonexistentNanoApp)218 TEST_P(ContexthubTxnTest, TestUnloadNonexistentNanoApp) {
219 cb->expectedTxnId = 1234;
220
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()));
224 }
225
TEST_P(ContexthubTxnTest,TestEnableNonexistentNanoApp)226 TEST_P(ContexthubTxnTest, TestEnableNonexistentNanoApp) {
227 cb->expectedTxnId = 2345;
228
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()));
232 }
233
TEST_P(ContexthubTxnTest,TestDisableNonexistentNanoApp)234 TEST_P(ContexthubTxnTest, TestDisableNonexistentNanoApp) {
235 cb->expectedTxnId = 3456;
236
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()));
240 }
241
242 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContexthubHidlTest);
243 INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubHidlTest, testing::ValuesIn(kTestParameters),
244 android::hardware::PrintInstanceTupleNameToString<>);
245
246 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContexthubTxnTest);
247 INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubTxnTest, testing::ValuesIn(kTestParameters),
248 android::hardware::PrintInstanceTupleNameToString<>);
249
250 } // anonymous namespace
251