blob: 8104f27f5b7c36efeef6435ffbbcb3a575b974a1 [file] [log] [blame]
Arthur Ishiguro4e916c32021-08-12 12:47:03 -07001/*
2 * Copyright (C) 2021 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#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18
19#include "VtsHalContexthubUtilsCommon.h"
20
21#include <android/hardware/contexthub/BnContextHub.h>
22#include <android/hardware/contexthub/BnContextHubCallback.h>
23#include <android/hardware/contexthub/IContextHub.h>
24#include <android/hardware/contexthub/IContextHubCallback.h>
25#include <binder/IServiceManager.h>
26#include <binder/ProcessState.h>
27#include <log/log.h>
28
29#include <cinttypes>
30#include <future>
31
32using ::android::ProcessState;
33using ::android::sp;
34using ::android::String16;
35using ::android::binder::Status;
36using ::android::hardware::contexthub::AsyncEventType;
37using ::android::hardware::contexthub::ContextHubInfo;
38using ::android::hardware::contexthub::ContextHubMessage;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +000039using ::android::hardware::contexthub::HostEndpointInfo;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070040using ::android::hardware::contexthub::IContextHub;
41using ::android::hardware::contexthub::IContextHubCallbackDefault;
42using ::android::hardware::contexthub::NanoappBinary;
43using ::android::hardware::contexthub::NanoappInfo;
Arthur Ishiguro08103072021-12-09 18:30:49 +000044using ::android::hardware::contexthub::NanoappRpcService;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070045using ::android::hardware::contexthub::Setting;
46using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
47using ::android::hardware::contexthub::vts_utils::waitForCallback;
48
49class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> {
50 public:
51 virtual void SetUp() override {
52 contextHub = android::waitForDeclaredService<IContextHub>(
53 String16(std::get<0>(GetParam()).c_str()));
54 ASSERT_NE(contextHub, nullptr);
55 }
56
57 uint32_t getHubId() { return std::get<1>(GetParam()); }
58
59 void testSettingChanged(Setting setting);
60
61 sp<IContextHub> contextHub;
62};
63
64TEST_P(ContextHubAidl, TestGetHubs) {
65 std::vector<ContextHubInfo> hubs;
66 ASSERT_TRUE(contextHub->getContextHubs(&hubs).isOk());
67
68 ALOGD("System reports %zu hubs", hubs.size());
69
70 for (const ContextHubInfo& hub : hubs) {
71 ALOGD("Checking hub ID %" PRIu32, hub.id);
72
73 EXPECT_GT(hub.name.size(), 0);
74 EXPECT_GT(hub.vendor.size(), 0);
75 EXPECT_GT(hub.toolchain.size(), 0);
76 EXPECT_GT(hub.peakMips, 0);
77 EXPECT_GT(hub.chrePlatformId, 0);
78 EXPECT_GT(hub.chreApiMajorVersion, 0);
Arthur Ishiguro6471f612021-10-28 21:59:55 +000079 EXPECT_GE(hub.chreApiMinorVersion, 0);
80 EXPECT_GE(hub.chrePatchVersion, 0);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070081
82 // Minimum 128 byte MTU as required by CHRE API v1.0
83 EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128));
84 }
85}
86
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +000087class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
88 public:
89 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
90 return Status::ok();
91 }
92
93 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
94 const std::vector<String16>& /* msgContentPerms */) override {
95 return Status::ok();
96 }
97
98 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
99
100 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
101 return Status::ok();
102 }
103};
104
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700105TEST_P(ContextHubAidl, TestRegisterCallback) {
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000106 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000107 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700108}
109
110TEST_P(ContextHubAidl, TestRegisterNullCallback) {
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000111 ASSERT_TRUE(contextHub->registerCallback(getHubId(), nullptr).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700112}
113
114// Helper callback that puts the async appInfo callback data into a promise
115class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
116 public:
117 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
118 ALOGD("Got app info callback with %zu apps", appInfo.size());
119 promise.set_value(appInfo);
120 return Status::ok();
121 }
122
123 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
124 const std::vector<String16>& /* msgContentPerms */) override {
125 return Status::ok();
126 }
127
128 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
129
130 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
131 return Status::ok();
132 }
133
134 std::promise<std::vector<NanoappInfo>> promise;
135};
136
137// Calls queryApps() and checks the returned metadata
138TEST_P(ContextHubAidl, TestQueryApps) {
139 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000140 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
141 ASSERT_TRUE(contextHub->queryNanoapps(getHubId()).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700142
143 std::vector<NanoappInfo> appInfoList;
144 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
145 for (const NanoappInfo& appInfo : appInfoList) {
146 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
147 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
Arthur Ishiguro08103072021-12-09 18:30:49 +0000148
149 // Verify services are unique.
150 std::set<uint64_t> existingServiceIds;
151 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
152 EXPECT_NE(rpcService.id, UINT64_C(0));
153 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
154 existingServiceIds.insert(rpcService.id);
155 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700156 }
157}
158
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000159// Calls getPreloadedNanoapps() and verifies there are preloaded nanoapps
160TEST_P(ContextHubAidl, TestGetPreloadedNanoapps) {
161 std::vector<int64_t> preloadedNanoappIds;
162 Status status = contextHub->getPreloadedNanoappIds(&preloadedNanoappIds);
163 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
164 status.transactionError() == android::UNKNOWN_TRANSACTION) {
165 return; // not supported -> old API; or not implemented
166 }
167
168 ASSERT_TRUE(status.isOk());
169 ASSERT_FALSE(preloadedNanoappIds.empty());
170}
171
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700172// Helper callback that puts the TransactionResult for the expectedTransactionId into a
173// promise
174class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
175 public:
176 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
177 return Status::ok();
178 }
179
180 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
181 const std::vector<String16>& /* msgContentPerms */) override {
182 return Status::ok();
183 }
184
185 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
186
187 Status handleTransactionResult(int32_t transactionId, bool success) override {
188 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
189 ") with success %d",
190 transactionId, expectedTransactionId, success);
191 if (transactionId == expectedTransactionId) {
192 promise.set_value(success);
193 }
194 return Status::ok();
195 }
196
197 uint32_t expectedTransactionId = 0;
198 std::promise<bool> promise;
199};
200
201// Parameterized fixture that sets the callback to TransactionResultCallback
202class ContextHubTransactionTest : public ContextHubAidl {
203 public:
204 virtual void SetUp() override {
205 ContextHubAidl::SetUp();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000206 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700207 }
208
209 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
210};
211
212TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
213 ContextHubMessage message;
214 message.nanoappId = kNonExistentAppId;
215 message.messageType = 1;
216 message.messageBody.resize(4);
217 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
218
219 ALOGD("Sending message to non-existent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000220 ASSERT_TRUE(contextHub->sendMessageToHub(getHubId(), message).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700221}
222
223TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
224 cb->expectedTransactionId = 0123;
225 NanoappBinary emptyApp;
226
227 emptyApp.nanoappId = kNonExistentAppId;
228 emptyApp.nanoappVersion = 1;
229 emptyApp.flags = 0;
230 emptyApp.targetChreApiMajorVersion = 1;
231 emptyApp.targetChreApiMinorVersion = 0;
232
233 ALOGD("Loading empty nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000234 bool success = contextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700235 if (success) {
236 bool transactionSuccess;
237 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
238 ASSERT_FALSE(transactionSuccess);
239 }
240}
241
242TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) {
243 cb->expectedTransactionId = 1234;
244
245 ALOGD("Unloading nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000246 bool success =
247 contextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
248 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700249 if (success) {
250 bool transactionSuccess;
251 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
252 ASSERT_FALSE(transactionSuccess);
253 }
254}
255
256TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) {
257 cb->expectedTransactionId = 2345;
258
259 ALOGD("Enabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000260 bool success =
261 contextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
262 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700263 if (success) {
264 bool transactionSuccess;
265 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
266 ASSERT_FALSE(transactionSuccess);
267 }
268}
269
270TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) {
271 cb->expectedTransactionId = 3456;
272
273 ALOGD("Disabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000274 bool success =
275 contextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
276 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700277 if (success) {
278 bool transactionSuccess;
279 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
280 ASSERT_FALSE(transactionSuccess);
281 }
282}
283
284void ContextHubAidl::testSettingChanged(Setting setting) {
285 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
286 // verify the expected E2E behavior in CHRE
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000287 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000288 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700289
290 ASSERT_TRUE(contextHub->onSettingChanged(setting, true /* enabled */).isOk());
291 ASSERT_TRUE(contextHub->onSettingChanged(setting, false /* enabled */).isOk());
292
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000293 ASSERT_TRUE(contextHub->registerCallback(getHubId(), nullptr).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700294}
295
296TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
297 testSettingChanged(Setting::LOCATION);
298}
299
300TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
301 testSettingChanged(Setting::WIFI_MAIN);
302}
303
304TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
305 testSettingChanged(Setting::WIFI_SCANNING);
306}
307
308TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
309 testSettingChanged(Setting::AIRPLANE_MODE);
310}
311
312TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
313 testSettingChanged(Setting::MICROPHONE);
314}
315
Anthonya6b65002022-01-20 20:49:10 +0000316TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) {
317 testSettingChanged(Setting::BT_MAIN);
318}
319
320TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) {
321 testSettingChanged(Setting::BT_SCANNING);
322}
323
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700324std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
325 std::vector<std::tuple<std::string, int32_t>> tuples;
326 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
327 std::vector<ContextHubInfo> contextHubInfos;
328
329 for (int i = 0; i < contextHubAidlNames.size(); i++) {
330 auto contextHubName = contextHubAidlNames[i].c_str();
331 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
332 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
333 for (auto& info : contextHubInfos) {
334 tuples.push_back(std::make_tuple(contextHubName, info.id));
335 }
336 }
337 }
338
339 return tuples;
340}
341
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000342TEST_P(ContextHubAidl, TestHostConnection) {
343 constexpr char16_t kHostEndpointId = 1;
344 HostEndpointInfo hostEndpointInfo;
345 hostEndpointInfo.hostEndpointId = kHostEndpointId;
346
347 ASSERT_TRUE(contextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
348 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
349}
350
351TEST_P(ContextHubAidl, TestInvalidHostConnection) {
352 constexpr char16_t kHostEndpointId = 1;
353
Arthur Ishiguro5dba9212022-02-01 17:03:32 +0000354 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000355}
356
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700357std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
358 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
359}
360
361GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
362INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
363 PrintGeneratedTest);
364
365GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
366INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
367 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
368
369int main(int argc, char** argv) {
370 ::testing::InitGoogleTest(&argc, argv);
371 ProcessState::self()->setThreadPoolMaxThreadCount(1);
372 ProcessState::self()->startThreadPool();
373 return RUN_ALL_TESTS();
374}