blob: e780857f24905deaaa72b2664190f3dbbdc386ab [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;
Anthony Stange7fba1002023-03-02 21:45:20 +000045using ::android::hardware::contexthub::NanSessionRequest;
46using ::android::hardware::contexthub::NanSessionStateUpdate;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070047using ::android::hardware::contexthub::Setting;
48using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
49using ::android::hardware::contexthub::vts_utils::waitForCallback;
50
Lei Ju8787afa2023-06-28 09:36:19 -070051// 6612b522-b717-41c8-b48d-c0b1cc64e142
52const std::array<uint8_t, 16> kUuid = {0x66, 0x12, 0xb5, 0x22, 0xb7, 0x17, 0x41, 0xc8,
53 0xb4, 0x8d, 0xc0, 0xb1, 0xcc, 0x64, 0xe1, 0x42};
54
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070055class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> {
56 public:
57 virtual void SetUp() override {
58 contextHub = android::waitForDeclaredService<IContextHub>(
59 String16(std::get<0>(GetParam()).c_str()));
60 ASSERT_NE(contextHub, nullptr);
61 }
62
63 uint32_t getHubId() { return std::get<1>(GetParam()); }
64
65 void testSettingChanged(Setting setting);
66
67 sp<IContextHub> contextHub;
68};
69
70TEST_P(ContextHubAidl, TestGetHubs) {
71 std::vector<ContextHubInfo> hubs;
72 ASSERT_TRUE(contextHub->getContextHubs(&hubs).isOk());
73
74 ALOGD("System reports %zu hubs", hubs.size());
75
76 for (const ContextHubInfo& hub : hubs) {
77 ALOGD("Checking hub ID %" PRIu32, hub.id);
78
79 EXPECT_GT(hub.name.size(), 0);
80 EXPECT_GT(hub.vendor.size(), 0);
81 EXPECT_GT(hub.toolchain.size(), 0);
82 EXPECT_GT(hub.peakMips, 0);
83 EXPECT_GT(hub.chrePlatformId, 0);
84 EXPECT_GT(hub.chreApiMajorVersion, 0);
Arthur Ishiguro6471f612021-10-28 21:59:55 +000085 EXPECT_GE(hub.chreApiMinorVersion, 0);
86 EXPECT_GE(hub.chrePatchVersion, 0);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070087
88 // Minimum 128 byte MTU as required by CHRE API v1.0
89 EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128));
90 }
91}
92
Matthew Sedamc8ce4d52023-01-09 20:18:21 +000093TEST_P(ContextHubAidl, TestEnableTestMode) {
94 Status status = contextHub->setTestMode(true);
95 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
96 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +000097 GTEST_SKIP() << "Not supported -> old API; or not implemented";
98 } else {
99 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000100 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000101}
102
103TEST_P(ContextHubAidl, TestDisableTestMode) {
104 Status status = contextHub->setTestMode(false);
105 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
106 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000107 GTEST_SKIP() << "Not supported -> old API; or not implemented";
108 } else {
109 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000110 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000111}
112
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000113class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
114 public:
115 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
116 return Status::ok();
117 }
118
119 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
120 const std::vector<String16>& /* msgContentPerms */) override {
121 return Status::ok();
122 }
123
124 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
125
126 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
127 return Status::ok();
128 }
Anthony Stange7344af92022-12-22 14:21:31 +0000129
Anthony Stange7fba1002023-03-02 21:45:20 +0000130 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
131 return Status::ok();
132 }
Lei Ju8787afa2023-06-28 09:36:19 -0700133
134 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
135 *out_uuid = kUuid;
136 return Status::ok();
137 }
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000138};
139
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700140TEST_P(ContextHubAidl, TestRegisterCallback) {
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000141 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000142 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700143}
144
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700145// Helper callback that puts the async appInfo callback data into a promise
146class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
147 public:
148 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
149 ALOGD("Got app info callback with %zu apps", appInfo.size());
150 promise.set_value(appInfo);
151 return Status::ok();
152 }
153
154 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
155 const std::vector<String16>& /* msgContentPerms */) override {
156 return Status::ok();
157 }
158
159 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
160
161 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
162 return Status::ok();
163 }
164
Anthony Stange7fba1002023-03-02 21:45:20 +0000165 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
166 return Status::ok();
167 }
Anthony Stange7344af92022-12-22 14:21:31 +0000168
Lei Ju8787afa2023-06-28 09:36:19 -0700169 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
170 *out_uuid = kUuid;
171 return Status::ok();
172 }
173
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700174 std::promise<std::vector<NanoappInfo>> promise;
175};
176
177// Calls queryApps() and checks the returned metadata
178TEST_P(ContextHubAidl, TestQueryApps) {
179 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000180 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
181 ASSERT_TRUE(contextHub->queryNanoapps(getHubId()).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700182
183 std::vector<NanoappInfo> appInfoList;
184 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
185 for (const NanoappInfo& appInfo : appInfoList) {
186 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
187 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
Arthur Ishiguro08103072021-12-09 18:30:49 +0000188
189 // Verify services are unique.
190 std::set<uint64_t> existingServiceIds;
191 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
192 EXPECT_NE(rpcService.id, UINT64_C(0));
193 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
194 existingServiceIds.insert(rpcService.id);
195 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700196 }
197}
198
Matthew Sedamd70f84d2023-03-06 18:34:24 +0000199// Calls getPreloadedNanoappsIds() and verifies there are preloaded nanoapps
200TEST_P(ContextHubAidl, TestGetPreloadedNanoappIds) {
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000201 std::vector<int64_t> preloadedNanoappIds;
Matthew Sedamd70f84d2023-03-06 18:34:24 +0000202 Status status = contextHub->getPreloadedNanoappIds(getHubId(), &preloadedNanoappIds);
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000203 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
204 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000205 GTEST_SKIP() << "Not supported -> old API; or not implemented";
206 } else {
207 ASSERT_TRUE(status.isOk());
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000208 }
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000209}
210
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700211// Helper callback that puts the TransactionResult for the expectedTransactionId into a
212// promise
213class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
214 public:
215 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
216 return Status::ok();
217 }
218
219 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
220 const std::vector<String16>& /* msgContentPerms */) override {
221 return Status::ok();
222 }
223
224 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
225
226 Status handleTransactionResult(int32_t transactionId, bool success) override {
227 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
228 ") with success %d",
229 transactionId, expectedTransactionId, success);
230 if (transactionId == expectedTransactionId) {
231 promise.set_value(success);
232 }
233 return Status::ok();
234 }
235
Anthony Stange7fba1002023-03-02 21:45:20 +0000236 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
237 return Status::ok();
238 }
Anthony Stange7344af92022-12-22 14:21:31 +0000239
Lei Ju8787afa2023-06-28 09:36:19 -0700240 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
241 *out_uuid = kUuid;
242 return Status::ok();
243 }
244
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700245 uint32_t expectedTransactionId = 0;
246 std::promise<bool> promise;
247};
248
249// Parameterized fixture that sets the callback to TransactionResultCallback
250class ContextHubTransactionTest : public ContextHubAidl {
251 public:
252 virtual void SetUp() override {
253 ContextHubAidl::SetUp();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000254 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700255 }
256
257 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
258};
259
260TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
261 ContextHubMessage message;
262 message.nanoappId = kNonExistentAppId;
263 message.messageType = 1;
264 message.messageBody.resize(4);
265 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
266
267 ALOGD("Sending message to non-existent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000268 ASSERT_TRUE(contextHub->sendMessageToHub(getHubId(), message).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700269}
270
271TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
272 cb->expectedTransactionId = 0123;
273 NanoappBinary emptyApp;
274
275 emptyApp.nanoappId = kNonExistentAppId;
276 emptyApp.nanoappVersion = 1;
277 emptyApp.flags = 0;
278 emptyApp.targetChreApiMajorVersion = 1;
279 emptyApp.targetChreApiMinorVersion = 0;
280
281 ALOGD("Loading empty nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000282 bool success = contextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700283 if (success) {
284 bool transactionSuccess;
285 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
286 ASSERT_FALSE(transactionSuccess);
287 }
288}
289
290TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) {
291 cb->expectedTransactionId = 1234;
292
293 ALOGD("Unloading nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000294 bool success =
295 contextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
296 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700297 if (success) {
298 bool transactionSuccess;
299 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
300 ASSERT_FALSE(transactionSuccess);
301 }
302}
303
304TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) {
305 cb->expectedTransactionId = 2345;
306
307 ALOGD("Enabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000308 bool success =
309 contextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
310 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700311 if (success) {
312 bool transactionSuccess;
313 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
314 ASSERT_FALSE(transactionSuccess);
315 }
316}
317
318TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) {
319 cb->expectedTransactionId = 3456;
320
321 ALOGD("Disabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000322 bool success =
323 contextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
324 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700325 if (success) {
326 bool transactionSuccess;
327 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
328 ASSERT_FALSE(transactionSuccess);
329 }
330}
331
332void ContextHubAidl::testSettingChanged(Setting setting) {
333 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
334 // verify the expected E2E behavior in CHRE
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000335 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000336 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700337
338 ASSERT_TRUE(contextHub->onSettingChanged(setting, true /* enabled */).isOk());
339 ASSERT_TRUE(contextHub->onSettingChanged(setting, false /* enabled */).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700340}
341
342TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
343 testSettingChanged(Setting::LOCATION);
344}
345
346TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
347 testSettingChanged(Setting::WIFI_MAIN);
348}
349
350TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
351 testSettingChanged(Setting::WIFI_SCANNING);
352}
353
354TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
355 testSettingChanged(Setting::AIRPLANE_MODE);
356}
357
358TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
359 testSettingChanged(Setting::MICROPHONE);
360}
361
Anthonya6b65002022-01-20 20:49:10 +0000362TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) {
363 testSettingChanged(Setting::BT_MAIN);
364}
365
366TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) {
367 testSettingChanged(Setting::BT_SCANNING);
368}
369
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700370std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
371 std::vector<std::tuple<std::string, int32_t>> tuples;
372 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
373 std::vector<ContextHubInfo> contextHubInfos;
374
375 for (int i = 0; i < contextHubAidlNames.size(); i++) {
376 auto contextHubName = contextHubAidlNames[i].c_str();
377 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
378 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
379 for (auto& info : contextHubInfos) {
380 tuples.push_back(std::make_tuple(contextHubName, info.id));
381 }
382 }
383 }
384
385 return tuples;
386}
387
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000388TEST_P(ContextHubAidl, TestHostConnection) {
389 constexpr char16_t kHostEndpointId = 1;
390 HostEndpointInfo hostEndpointInfo;
Arthur Ishiguro16f40622023-02-10 18:11:12 +0000391 hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000392 hostEndpointInfo.hostEndpointId = kHostEndpointId;
393
394 ASSERT_TRUE(contextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
395 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
396}
397
398TEST_P(ContextHubAidl, TestInvalidHostConnection) {
399 constexpr char16_t kHostEndpointId = 1;
400
Arthur Ishiguro5dba9212022-02-01 17:03:32 +0000401 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000402}
403
Anthony Stange7344af92022-12-22 14:21:31 +0000404TEST_P(ContextHubAidl, TestNanSessionStateChange) {
Anthony Stange7fba1002023-03-02 21:45:20 +0000405 NanSessionStateUpdate update;
406 update.state = true;
Rocky Fang78140982023-06-16 00:42:41 +0000407 Status status = contextHub->onNanSessionStateChanged(update);
408 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
409 status.transactionError() == android::UNKNOWN_TRANSACTION) {
410 GTEST_SKIP() << "Not supported -> old API; or not implemented";
411 } else {
412 ASSERT_TRUE(status.isOk());
413 update.state = false;
414 ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk());
415 }
Anthony Stange7344af92022-12-22 14:21:31 +0000416}
417
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700418std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
419 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
420}
421
422GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
423INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
424 PrintGeneratedTest);
425
426GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
427INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
428 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
429
430int main(int argc, char** argv) {
431 ::testing::InitGoogleTest(&argc, argv);
432 ProcessState::self()->setThreadPoolMaxThreadCount(1);
433 ProcessState::self()->startThreadPool();
434 return RUN_ALL_TESTS();
435}