blob: 392e23c9c1d75c3946cb85f6c742fa8dce2962dd [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) {
106 bool success;
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000107 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
108 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb, &success).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700109 ASSERT_TRUE(success);
110}
111
112TEST_P(ContextHubAidl, TestRegisterNullCallback) {
113 bool success;
114 ASSERT_TRUE(contextHub->registerCallback(getHubId(), nullptr, &success).isOk());
115}
116
117// Helper callback that puts the async appInfo callback data into a promise
118class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
119 public:
120 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
121 ALOGD("Got app info callback with %zu apps", appInfo.size());
122 promise.set_value(appInfo);
123 return Status::ok();
124 }
125
126 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
127 const std::vector<String16>& /* msgContentPerms */) override {
128 return Status::ok();
129 }
130
131 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
132
133 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
134 return Status::ok();
135 }
136
137 std::promise<std::vector<NanoappInfo>> promise;
138};
139
140// Calls queryApps() and checks the returned metadata
141TEST_P(ContextHubAidl, TestQueryApps) {
142 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
143 bool success;
144 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb, &success).isOk());
145 ASSERT_TRUE(success);
146
147 ASSERT_TRUE(contextHub->queryNanoapps(getHubId(), &success).isOk());
148 ASSERT_TRUE(success);
149
150 std::vector<NanoappInfo> appInfoList;
151 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
152 for (const NanoappInfo& appInfo : appInfoList) {
153 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
154 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
Arthur Ishiguro08103072021-12-09 18:30:49 +0000155
156 // Verify services are unique.
157 std::set<uint64_t> existingServiceIds;
158 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
159 EXPECT_NE(rpcService.id, UINT64_C(0));
160 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
161 existingServiceIds.insert(rpcService.id);
162 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700163 }
164}
165
166// Helper callback that puts the TransactionResult for the expectedTransactionId into a
167// promise
168class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
169 public:
170 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
171 return Status::ok();
172 }
173
174 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
175 const std::vector<String16>& /* msgContentPerms */) override {
176 return Status::ok();
177 }
178
179 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
180
181 Status handleTransactionResult(int32_t transactionId, bool success) override {
182 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
183 ") with success %d",
184 transactionId, expectedTransactionId, success);
185 if (transactionId == expectedTransactionId) {
186 promise.set_value(success);
187 }
188 return Status::ok();
189 }
190
191 uint32_t expectedTransactionId = 0;
192 std::promise<bool> promise;
193};
194
195// Parameterized fixture that sets the callback to TransactionResultCallback
196class ContextHubTransactionTest : public ContextHubAidl {
197 public:
198 virtual void SetUp() override {
199 ContextHubAidl::SetUp();
200 bool success;
201 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb, &success).isOk());
202 ASSERT_TRUE(success);
203 }
204
205 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
206};
207
208TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
209 ContextHubMessage message;
210 message.nanoappId = kNonExistentAppId;
211 message.messageType = 1;
212 message.messageBody.resize(4);
213 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
214
215 ALOGD("Sending message to non-existent nanoapp");
216 bool success;
217 ASSERT_TRUE(contextHub->sendMessageToHub(getHubId(), message, &success).isOk());
218 ASSERT_TRUE(success);
219}
220
221TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
222 cb->expectedTransactionId = 0123;
223 NanoappBinary emptyApp;
224
225 emptyApp.nanoappId = kNonExistentAppId;
226 emptyApp.nanoappVersion = 1;
227 emptyApp.flags = 0;
228 emptyApp.targetChreApiMajorVersion = 1;
229 emptyApp.targetChreApiMinorVersion = 0;
230
231 ALOGD("Loading empty nanoapp");
232 bool success;
233 ASSERT_TRUE(contextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId, &success)
234 .isOk());
235 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");
246 bool success;
247 ASSERT_TRUE(contextHub
248 ->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId,
249 &success)
250 .isOk());
251 if (success) {
252 bool transactionSuccess;
253 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
254 ASSERT_FALSE(transactionSuccess);
255 }
256}
257
258TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) {
259 cb->expectedTransactionId = 2345;
260
261 ALOGD("Enabling nonexistent nanoapp");
262 bool success;
263 ASSERT_TRUE(contextHub
264 ->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId,
265 &success)
266 .isOk());
267 if (success) {
268 bool transactionSuccess;
269 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
270 ASSERT_FALSE(transactionSuccess);
271 }
272}
273
274TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) {
275 cb->expectedTransactionId = 3456;
276
277 ALOGD("Disabling nonexistent nanoapp");
278 bool success;
279 ASSERT_TRUE(contextHub
280 ->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId,
281 &success)
282 .isOk());
283 if (success) {
284 bool transactionSuccess;
285 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
286 ASSERT_FALSE(transactionSuccess);
287 }
288}
289
290void ContextHubAidl::testSettingChanged(Setting setting) {
291 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
292 // verify the expected E2E behavior in CHRE
293 bool success;
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000294 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
295 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb, &success).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700296 ASSERT_TRUE(success);
297
298 ASSERT_TRUE(contextHub->onSettingChanged(setting, true /* enabled */).isOk());
299 ASSERT_TRUE(contextHub->onSettingChanged(setting, false /* enabled */).isOk());
300
301 ASSERT_TRUE(contextHub->registerCallback(getHubId(), nullptr, &success).isOk());
302 ASSERT_TRUE(success);
303}
304
305TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
306 testSettingChanged(Setting::LOCATION);
307}
308
309TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
310 testSettingChanged(Setting::WIFI_MAIN);
311}
312
313TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
314 testSettingChanged(Setting::WIFI_SCANNING);
315}
316
317TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
318 testSettingChanged(Setting::AIRPLANE_MODE);
319}
320
321TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
322 testSettingChanged(Setting::MICROPHONE);
323}
324
325std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
326 std::vector<std::tuple<std::string, int32_t>> tuples;
327 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
328 std::vector<ContextHubInfo> contextHubInfos;
329
330 for (int i = 0; i < contextHubAidlNames.size(); i++) {
331 auto contextHubName = contextHubAidlNames[i].c_str();
332 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
333 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
334 for (auto& info : contextHubInfos) {
335 tuples.push_back(std::make_tuple(contextHubName, info.id));
336 }
337 }
338 }
339
340 return tuples;
341}
342
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000343TEST_P(ContextHubAidl, TestHostConnection) {
344 constexpr char16_t kHostEndpointId = 1;
345 HostEndpointInfo hostEndpointInfo;
346 hostEndpointInfo.hostEndpointId = kHostEndpointId;
347
348 ASSERT_TRUE(contextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
349 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
350}
351
352TEST_P(ContextHubAidl, TestInvalidHostConnection) {
353 constexpr char16_t kHostEndpointId = 1;
354
355 Status status = contextHub->onHostEndpointDisconnected(kHostEndpointId);
356 ASSERT_EQ(status.exceptionCode(), android::binder::Status::EX_ILLEGAL_ARGUMENT);
357}
358
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700359std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
360 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
361}
362
363GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
364INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
365 PrintGeneratedTest);
366
367GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
368INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
369 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
370
371int main(int argc, char** argv) {
372 ::testing::InitGoogleTest(&argc, argv);
373 ProcessState::self()->setThreadPoolMaxThreadCount(1);
374 ProcessState::self()->startThreadPool();
375 return RUN_ALL_TESTS();
376}