blob: 9707ecf7282586aef0d92b6858574803603ff9e2 [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
51class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> {
52 public:
53 virtual void SetUp() override {
54 contextHub = android::waitForDeclaredService<IContextHub>(
55 String16(std::get<0>(GetParam()).c_str()));
56 ASSERT_NE(contextHub, nullptr);
57 }
58
59 uint32_t getHubId() { return std::get<1>(GetParam()); }
60
61 void testSettingChanged(Setting setting);
62
63 sp<IContextHub> contextHub;
64};
65
66TEST_P(ContextHubAidl, TestGetHubs) {
67 std::vector<ContextHubInfo> hubs;
68 ASSERT_TRUE(contextHub->getContextHubs(&hubs).isOk());
69
70 ALOGD("System reports %zu hubs", hubs.size());
71
72 for (const ContextHubInfo& hub : hubs) {
73 ALOGD("Checking hub ID %" PRIu32, hub.id);
74
75 EXPECT_GT(hub.name.size(), 0);
76 EXPECT_GT(hub.vendor.size(), 0);
77 EXPECT_GT(hub.toolchain.size(), 0);
78 EXPECT_GT(hub.peakMips, 0);
79 EXPECT_GT(hub.chrePlatformId, 0);
80 EXPECT_GT(hub.chreApiMajorVersion, 0);
Arthur Ishiguro6471f612021-10-28 21:59:55 +000081 EXPECT_GE(hub.chreApiMinorVersion, 0);
82 EXPECT_GE(hub.chrePatchVersion, 0);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070083
84 // Minimum 128 byte MTU as required by CHRE API v1.0
85 EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128));
86 }
87}
88
Matthew Sedamc8ce4d52023-01-09 20:18:21 +000089TEST_P(ContextHubAidl, TestEnableTestMode) {
90 Status status = contextHub->setTestMode(true);
91 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
92 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +000093 GTEST_SKIP() << "Not supported -> old API; or not implemented";
94 } else {
95 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +000096 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +000097}
98
99TEST_P(ContextHubAidl, TestDisableTestMode) {
100 Status status = contextHub->setTestMode(false);
101 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
102 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000103 GTEST_SKIP() << "Not supported -> old API; or not implemented";
104 } else {
105 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000106 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000107}
108
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000109class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
110 public:
111 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
112 return Status::ok();
113 }
114
115 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
116 const std::vector<String16>& /* msgContentPerms */) override {
117 return Status::ok();
118 }
119
120 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
121
122 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
123 return Status::ok();
124 }
Anthony Stange7344af92022-12-22 14:21:31 +0000125
Anthony Stange7fba1002023-03-02 21:45:20 +0000126 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
127 return Status::ok();
128 }
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000129};
130
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700131TEST_P(ContextHubAidl, TestRegisterCallback) {
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000132 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000133 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700134}
135
136TEST_P(ContextHubAidl, TestRegisterNullCallback) {
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000137 ASSERT_TRUE(contextHub->registerCallback(getHubId(), nullptr).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700138}
139
140// Helper callback that puts the async appInfo callback data into a promise
141class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
142 public:
143 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
144 ALOGD("Got app info callback with %zu apps", appInfo.size());
145 promise.set_value(appInfo);
146 return Status::ok();
147 }
148
149 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
150 const std::vector<String16>& /* msgContentPerms */) override {
151 return Status::ok();
152 }
153
154 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
155
156 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
157 return Status::ok();
158 }
159
Anthony Stange7fba1002023-03-02 21:45:20 +0000160 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
161 return Status::ok();
162 }
Anthony Stange7344af92022-12-22 14:21:31 +0000163
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700164 std::promise<std::vector<NanoappInfo>> promise;
165};
166
167// Calls queryApps() and checks the returned metadata
168TEST_P(ContextHubAidl, TestQueryApps) {
169 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000170 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
171 ASSERT_TRUE(contextHub->queryNanoapps(getHubId()).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700172
173 std::vector<NanoappInfo> appInfoList;
174 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
175 for (const NanoappInfo& appInfo : appInfoList) {
176 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
177 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
Arthur Ishiguro08103072021-12-09 18:30:49 +0000178
179 // Verify services are unique.
180 std::set<uint64_t> existingServiceIds;
181 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
182 EXPECT_NE(rpcService.id, UINT64_C(0));
183 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
184 existingServiceIds.insert(rpcService.id);
185 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700186 }
187}
188
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000189// Calls getPreloadedNanoapps() and verifies there are preloaded nanoapps
190TEST_P(ContextHubAidl, TestGetPreloadedNanoapps) {
191 std::vector<int64_t> preloadedNanoappIds;
192 Status status = contextHub->getPreloadedNanoappIds(&preloadedNanoappIds);
193 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
194 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000195 GTEST_SKIP() << "Not supported -> old API; or not implemented";
196 } else {
197 ASSERT_TRUE(status.isOk());
198 ASSERT_FALSE(preloadedNanoappIds.empty());
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000199 }
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000200}
201
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700202// Helper callback that puts the TransactionResult for the expectedTransactionId into a
203// promise
204class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
205 public:
206 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
207 return Status::ok();
208 }
209
210 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
211 const std::vector<String16>& /* msgContentPerms */) override {
212 return Status::ok();
213 }
214
215 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
216
217 Status handleTransactionResult(int32_t transactionId, bool success) override {
218 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
219 ") with success %d",
220 transactionId, expectedTransactionId, success);
221 if (transactionId == expectedTransactionId) {
222 promise.set_value(success);
223 }
224 return Status::ok();
225 }
226
Anthony Stange7fba1002023-03-02 21:45:20 +0000227 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
228 return Status::ok();
229 }
Anthony Stange7344af92022-12-22 14:21:31 +0000230
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700231 uint32_t expectedTransactionId = 0;
232 std::promise<bool> promise;
233};
234
235// Parameterized fixture that sets the callback to TransactionResultCallback
236class ContextHubTransactionTest : public ContextHubAidl {
237 public:
238 virtual void SetUp() override {
239 ContextHubAidl::SetUp();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000240 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700241 }
242
243 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
244};
245
246TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
247 ContextHubMessage message;
248 message.nanoappId = kNonExistentAppId;
249 message.messageType = 1;
250 message.messageBody.resize(4);
251 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
252
253 ALOGD("Sending message to non-existent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000254 ASSERT_TRUE(contextHub->sendMessageToHub(getHubId(), message).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700255}
256
257TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
258 cb->expectedTransactionId = 0123;
259 NanoappBinary emptyApp;
260
261 emptyApp.nanoappId = kNonExistentAppId;
262 emptyApp.nanoappVersion = 1;
263 emptyApp.flags = 0;
264 emptyApp.targetChreApiMajorVersion = 1;
265 emptyApp.targetChreApiMinorVersion = 0;
266
267 ALOGD("Loading empty nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000268 bool success = contextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700269 if (success) {
270 bool transactionSuccess;
271 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
272 ASSERT_FALSE(transactionSuccess);
273 }
274}
275
276TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) {
277 cb->expectedTransactionId = 1234;
278
279 ALOGD("Unloading nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000280 bool success =
281 contextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
282 .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, TestEnableNonexistentNanoapp) {
291 cb->expectedTransactionId = 2345;
292
293 ALOGD("Enabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000294 bool success =
295 contextHub->enableNanoapp(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, TestDisableNonexistentNanoapp) {
305 cb->expectedTransactionId = 3456;
306
307 ALOGD("Disabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000308 bool success =
309 contextHub->disableNanoapp(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
318void ContextHubAidl::testSettingChanged(Setting setting) {
319 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
320 // verify the expected E2E behavior in CHRE
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000321 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000322 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700323
324 ASSERT_TRUE(contextHub->onSettingChanged(setting, true /* enabled */).isOk());
325 ASSERT_TRUE(contextHub->onSettingChanged(setting, false /* enabled */).isOk());
326
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000327 ASSERT_TRUE(contextHub->registerCallback(getHubId(), nullptr).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700328}
329
330TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
331 testSettingChanged(Setting::LOCATION);
332}
333
334TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
335 testSettingChanged(Setting::WIFI_MAIN);
336}
337
338TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
339 testSettingChanged(Setting::WIFI_SCANNING);
340}
341
342TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
343 testSettingChanged(Setting::AIRPLANE_MODE);
344}
345
346TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
347 testSettingChanged(Setting::MICROPHONE);
348}
349
Anthonya6b65002022-01-20 20:49:10 +0000350TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) {
351 testSettingChanged(Setting::BT_MAIN);
352}
353
354TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) {
355 testSettingChanged(Setting::BT_SCANNING);
356}
357
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700358std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
359 std::vector<std::tuple<std::string, int32_t>> tuples;
360 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
361 std::vector<ContextHubInfo> contextHubInfos;
362
363 for (int i = 0; i < contextHubAidlNames.size(); i++) {
364 auto contextHubName = contextHubAidlNames[i].c_str();
365 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
366 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
367 for (auto& info : contextHubInfos) {
368 tuples.push_back(std::make_tuple(contextHubName, info.id));
369 }
370 }
371 }
372
373 return tuples;
374}
375
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000376TEST_P(ContextHubAidl, TestHostConnection) {
377 constexpr char16_t kHostEndpointId = 1;
378 HostEndpointInfo hostEndpointInfo;
Arthur Ishiguro16f40622023-02-10 18:11:12 +0000379 hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000380 hostEndpointInfo.hostEndpointId = kHostEndpointId;
381
382 ASSERT_TRUE(contextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
383 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
384}
385
386TEST_P(ContextHubAidl, TestInvalidHostConnection) {
387 constexpr char16_t kHostEndpointId = 1;
388
Arthur Ishiguro5dba9212022-02-01 17:03:32 +0000389 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000390}
391
Anthony Stange7344af92022-12-22 14:21:31 +0000392TEST_P(ContextHubAidl, TestNanSessionStateChange) {
Anthony Stange7fba1002023-03-02 21:45:20 +0000393 NanSessionStateUpdate update;
394 update.state = true;
395 ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk());
396 update.state = false;
397 ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk());
Anthony Stange7344af92022-12-22 14:21:31 +0000398}
399
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700400std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
401 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
402}
403
404GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
405INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
406 PrintGeneratedTest);
407
408GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
409INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
410 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
411
412int main(int argc, char** argv) {
413 ::testing::InitGoogleTest(&argc, argv);
414 ProcessState::self()->setThreadPoolMaxThreadCount(1);
415 ProcessState::self()->startThreadPool();
416 return RUN_ALL_TESTS();
417}