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 "VtsHalContexthubUtils.h"
22
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>
28 #include <gtest/gtest.h>
29 #include <hidl/GtestPrinter.h>
30 #include <log/log.h>
31
32 #include <cinttypes>
33 #include <future>
34 #include <utility>
35
36 using ::android::sp;
37 using ::android::hardware::hidl_string;
38 using ::android::hardware::hidl_vec;
39 using ::android::hardware::Return;
40 using ::android::hardware::Void;
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;
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;
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
62 const std::vector<std::tuple<std::string, std::string>> kTestParameters =
63 getHalAndHubIdList<IContexthub>();
64
65 class ContexthubHidlTest : public ContexthubHidlTestBase<IContexthub> {};
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.
70 template <class ReturnType>
waitForCallback(std::future<ReturnType> future,ReturnType * result,std::chrono::milliseconds timeout=std::chrono::seconds (5))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;
74
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";
80
81 if (status == std::future_status::ready) {
82 *result = future.get();
83 return true;
84 }
85 }
86
87 return false;
88 }
89
90 // Ensures that the metadata reported in getHubs() is sane
TEST_P(ContexthubHidlTest,TestGetHubs)91 TEST_P(ContexthubHidlTest, TestGetHubs) {
92 hidl_vec<ContextHub> hubs = getHubsSync(hubApi.get());
93 ALOGD("System reports %zu hubs", hubs.size());
94
95 for (const ContextHub& hub : hubs) {
96 ALOGD("Checking hub ID %" PRIu32, hub.hubId);
97
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);
105
106 // Minimum 128 byte MTU as required by CHRE API v1.0
107 EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128));
108 }
109 }
110
TEST_P(ContexthubHidlTest,TestRegisterCallback)111 TEST_P(ContexthubHidlTest, TestRegisterCallback) {
112 ALOGD("TestRegisterCallback called, hubId %" PRIu32, getHubId());
113 ASSERT_OK(registerCallback(new ContexthubCallbackBase()));
114 }
115
TEST_P(ContexthubHidlTest,TestRegisterNullCallback)116 TEST_P(ContexthubHidlTest, TestRegisterNullCallback) {
117 ALOGD("TestRegisterNullCallback called, hubId %" PRIu32, getHubId());
118 ASSERT_OK(registerCallback(nullptr));
119 }
120
121 // Helper callback that puts the async appInfo callback data into a promise
122 class QueryAppsCallback : public ContexthubCallbackBase {
123 public:
handleAppsInfo(const hidl_vec<HubAppInfo> & appInfo)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 }
129
130 std::promise<hidl_vec<HubAppInfo>> promise;
131 };
132
133 // Calls queryApps() and checks the returned metadata
TEST_P(ContexthubHidlTest,TestQueryApps)134 TEST_P(ContexthubHidlTest, TestQueryApps) {
135 ALOGD("TestQueryApps called, hubId %u", getHubId());
136 sp<QueryAppsCallback> cb = new QueryAppsCallback();
137 ASSERT_OK(registerCallback(cb));
138
139 Result result = hubApi->queryApps(getHubId());
140 ASSERT_OK(result);
141
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 }
149 }
150
151 // Helper callback that puts the TransactionResult for the expectedTxnId into a
152 // promise
153 class TxnResultCallback : public ContexthubCallbackBase {
154 public:
handleTxnResult(uint32_t txnId,TransactionResult result)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();
163 }
164
165 uint32_t expectedTxnId = 0;
166 std::promise<TransactionResult> promise;
167 };
168
169 // Parameterized fixture that sets the callback to TxnResultCallback
170 class ContexthubTxnTest : public ContexthubHidlTest {
171 public:
SetUp()172 virtual void SetUp() override {
173 ContexthubHidlTest::SetUp();
174 ASSERT_OK(registerCallback(cb));
175 }
176
177 sp<TxnResultCallback> cb = new TxnResultCallback();
178 };
179
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.
checkFailureSyncOrAsync(Result result,Result allowedSyncResult,std::future<TransactionResult> && future)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);
204 }
205
206 return ::testing::AssertionSuccess();
207 }
208
TEST_P(ContexthubTxnTest,TestSendMessageToNonExistentNanoApp)209 TEST_P(ContexthubTxnTest, TestSendMessageToNonExistentNanoApp) {
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);
215
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 }
223 }
224
TEST_P(ContexthubTxnTest,TestLoadEmptyNanoApp)225 TEST_P(ContexthubTxnTest, TestLoadEmptyNanoApp) {
226 cb->expectedTxnId = 0123;
227 NanoAppBinary emptyApp;
228
229 emptyApp.appId = kNonExistentAppId;
230 emptyApp.appVersion = 1;
231 emptyApp.flags = 0;
232 emptyApp.targetChreApiMajorVersion = 1;
233 emptyApp.targetChreApiMinorVersion = 0;
234
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()));
238 }
239
TEST_P(ContexthubTxnTest,TestUnloadNonexistentNanoApp)240 TEST_P(ContexthubTxnTest, TestUnloadNonexistentNanoApp) {
241 cb->expectedTxnId = 1234;
242
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()));
246 }
247
TEST_P(ContexthubTxnTest,TestEnableNonexistentNanoApp)248 TEST_P(ContexthubTxnTest, TestEnableNonexistentNanoApp) {
249 cb->expectedTxnId = 2345;
250
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()));
254 }
255
TEST_P(ContexthubTxnTest,TestDisableNonexistentNanoApp)256 TEST_P(ContexthubTxnTest, TestDisableNonexistentNanoApp) {
257 cb->expectedTxnId = 3456;
258
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()));
262 }
263
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
271