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