blob: 900e6c93660e657ebca35b55f77f66303d5ddbd5 [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>
Matthew Sedam92c2bd82024-11-11 19:52:30 +000023#include <android/hardware/contexthub/BnEndpointCallback.h>
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070024#include <android/hardware/contexthub/IContextHub.h>
25#include <android/hardware/contexthub/IContextHubCallback.h>
Matthew Sedam92c2bd82024-11-11 19:52:30 +000026#include <android/hardware/contexthub/IEndpointCallback.h>
Madhav Iyengaree25d072024-12-17 17:08:55 +000027#include <android/hardware/contexthub/IEndpointCommunication.h>
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070028#include <binder/IServiceManager.h>
29#include <binder/ProcessState.h>
30#include <log/log.h>
31
32#include <cinttypes>
33#include <future>
34
35using ::android::ProcessState;
36using ::android::sp;
37using ::android::String16;
38using ::android::binder::Status;
39using ::android::hardware::contexthub::AsyncEventType;
Matthew Sedam92c2bd82024-11-11 19:52:30 +000040using ::android::hardware::contexthub::BnEndpointCallback;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070041using ::android::hardware::contexthub::ContextHubInfo;
42using ::android::hardware::contexthub::ContextHubMessage;
Matthew Sedam92c2bd82024-11-11 19:52:30 +000043using ::android::hardware::contexthub::EndpointId;
44using ::android::hardware::contexthub::EndpointInfo;
Matthew Sedamadfd5572023-11-21 05:53:00 -080045using ::android::hardware::contexthub::ErrorCode;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +000046using ::android::hardware::contexthub::HostEndpointInfo;
Matthew Sedam92c2bd82024-11-11 19:52:30 +000047using ::android::hardware::contexthub::HubInfo;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070048using ::android::hardware::contexthub::IContextHub;
49using ::android::hardware::contexthub::IContextHubCallbackDefault;
Madhav Iyengaree25d072024-12-17 17:08:55 +000050using ::android::hardware::contexthub::IEndpointCommunication;
Matthew Sedam92c2bd82024-11-11 19:52:30 +000051using ::android::hardware::contexthub::Message;
Matthew Sedamadfd5572023-11-21 05:53:00 -080052using ::android::hardware::contexthub::MessageDeliveryStatus;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070053using ::android::hardware::contexthub::NanoappBinary;
54using ::android::hardware::contexthub::NanoappInfo;
Arthur Ishiguro08103072021-12-09 18:30:49 +000055using ::android::hardware::contexthub::NanoappRpcService;
Anthony Stange7fba1002023-03-02 21:45:20 +000056using ::android::hardware::contexthub::NanSessionRequest;
57using ::android::hardware::contexthub::NanSessionStateUpdate;
Matthew Sedam92c2bd82024-11-11 19:52:30 +000058using ::android::hardware::contexthub::Reason;
59using ::android::hardware::contexthub::Service;
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070060using ::android::hardware::contexthub::Setting;
61using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
62using ::android::hardware::contexthub::vts_utils::waitForCallback;
63
Lei Ju8787afa2023-06-28 09:36:19 -070064// 6612b522-b717-41c8-b48d-c0b1cc64e142
Lei Ju5b2ded42023-12-05 15:08:21 -080065constexpr std::array<uint8_t, 16> kUuid = {0x66, 0x12, 0xb5, 0x22, 0xb7, 0x17, 0x41, 0xc8,
66 0xb4, 0x8d, 0xc0, 0xb1, 0xcc, 0x64, 0xe1, 0x42};
Matthew Sedam41db0932025-01-02 11:17:46 -080067
Lei Ju5b2ded42023-12-05 15:08:21 -080068const String16 kName{"VtsAidlHalContextHubTargetTest"};
Lei Ju8787afa2023-06-28 09:36:19 -070069
Arthur Ishiguro38712cf2024-12-13 11:40:47 -080070const String16 kEchoServiceName{"android.hardware.contexthub.test.EchoService"};
71
Madhav Iyengaree25d072024-12-17 17:08:55 +000072constexpr int64_t kDefaultHubId = 1;
73
74class TestEndpointCallback;
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +000075
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070076class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> {
77 public:
Madhav Iyengaree25d072024-12-17 17:08:55 +000078 void SetUp() override {
79 mContextHub = android::waitForDeclaredService<IContextHub>(
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070080 String16(std::get<0>(GetParam()).c_str()));
Madhav Iyengaree25d072024-12-17 17:08:55 +000081 ASSERT_NE(mContextHub, nullptr);
Matthew Sedam41db0932025-01-02 11:17:46 -080082 }
83
84 uint32_t getHubId() { return std::get<1>(GetParam()); }
85
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +000086 void testSettingChanged(Setting setting);
87
88 sp<IContextHub> mContextHub;
89};
90
91class ContextHubEndpointAidl : public testing::TestWithParam<std::string> {
92 public:
93 void SetUp() override {
94 mContextHub = android::waitForDeclaredService<IContextHub>(String16(GetParam().c_str()));
95 ASSERT_NE(mContextHub, nullptr);
96 mEndpointCb = sp<TestEndpointCallback>::make();
97 }
98
Madhav Iyengaree25d072024-12-17 17:08:55 +000099 Status registerHub(int64_t id, sp<IEndpointCommunication>* hubInterface) {
100 HubInfo info;
101 info.hubId = id;
102 return mContextHub->registerEndpointHub(mEndpointCb, info, hubInterface);
103 }
104
105 bool registerDefaultHub() {
106 Status status = registerHub(kDefaultHubId, &mHubInterface);
107 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
108 status.transactionError() == android::UNKNOWN_TRANSACTION) {
109 return false;
110 }
111 EXPECT_TRUE(status.isOk());
112 EXPECT_NE(mHubInterface, nullptr);
113 if (!mHubInterface) {
114 return false;
115 }
116 return true;
117 }
Matthew Sedam41db0932025-01-02 11:17:46 -0800118
Madhav Iyengaree25d072024-12-17 17:08:55 +0000119 sp<IContextHub> mContextHub;
120 sp<TestEndpointCallback> mEndpointCb;
121 sp<IEndpointCommunication> mHubInterface;
Matthew Sedam41db0932025-01-02 11:17:46 -0800122};
123
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000124class ContextHubEndpointAidlWithTestMode : public ContextHubEndpointAidl {
Matthew Sedam41db0932025-01-02 11:17:46 -0800125 public:
Madhav Iyengaree25d072024-12-17 17:08:55 +0000126 void SetUp() override {
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000127 ContextHubEndpointAidl::SetUp();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000128
129 // Best effort enable test mode - this may not be supported on older HALS, so we
130 // ignore the return value.
Madhav Iyengaree25d072024-12-17 17:08:55 +0000131 mContextHub->setTestMode(/* enable= */ true);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700132 }
133
Madhav Iyengaree25d072024-12-17 17:08:55 +0000134 void TearDown() override {
135 mContextHub->setTestMode(/* enable= */ false);
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000136 ContextHubEndpointAidl::TearDown();
Matthew Sedam41db0932025-01-02 11:17:46 -0800137 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700138};
139
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000140TEST_P(ContextHubEndpointAidl, TestGetHubs) {
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700141 std::vector<ContextHubInfo> hubs;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000142 ASSERT_TRUE(mContextHub->getContextHubs(&hubs).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700143
144 ALOGD("System reports %zu hubs", hubs.size());
145
146 for (const ContextHubInfo& hub : hubs) {
147 ALOGD("Checking hub ID %" PRIu32, hub.id);
148
149 EXPECT_GT(hub.name.size(), 0);
150 EXPECT_GT(hub.vendor.size(), 0);
151 EXPECT_GT(hub.toolchain.size(), 0);
152 EXPECT_GT(hub.peakMips, 0);
153 EXPECT_GT(hub.chrePlatformId, 0);
154 EXPECT_GT(hub.chreApiMajorVersion, 0);
Arthur Ishiguro6471f612021-10-28 21:59:55 +0000155 EXPECT_GE(hub.chreApiMinorVersion, 0);
156 EXPECT_GE(hub.chrePatchVersion, 0);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700157
158 // Minimum 128 byte MTU as required by CHRE API v1.0
159 EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128));
160 }
161}
162
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000163TEST_P(ContextHubEndpointAidl, TestEnableTestMode) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000164 Status status = mContextHub->setTestMode(true);
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000165 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
166 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000167 GTEST_SKIP() << "Not supported -> old API; or not implemented";
168 } else {
169 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000170 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000171}
172
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000173TEST_P(ContextHubEndpointAidl, TestDisableTestMode) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000174 Status status = mContextHub->setTestMode(false);
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000175 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
176 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000177 GTEST_SKIP() << "Not supported -> old API; or not implemented";
178 } else {
179 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000180 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000181}
182
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000183class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
184 public:
185 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
186 return Status::ok();
187 }
188
189 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
190 const std::vector<String16>& /* msgContentPerms */) override {
191 return Status::ok();
192 }
193
194 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
195
196 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
197 return Status::ok();
198 }
Anthony Stange7344af92022-12-22 14:21:31 +0000199
Anthony Stange7fba1002023-03-02 21:45:20 +0000200 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
201 return Status::ok();
202 }
Lei Ju8787afa2023-06-28 09:36:19 -0700203
Matthew Sedamadfd5572023-11-21 05:53:00 -0800204 Status handleMessageDeliveryStatus(
205 char16_t /* hostEndPointId */,
206 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
207 return Status::ok();
208 }
209
Lei Ju8787afa2023-06-28 09:36:19 -0700210 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
211 *out_uuid = kUuid;
212 return Status::ok();
213 }
Lei Ju5b2ded42023-12-05 15:08:21 -0800214
215 Status getName(::android::String16* out_name) override {
216 *out_name = kName;
217 return Status::ok();
218 }
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000219};
220
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700221TEST_P(ContextHubAidl, TestRegisterCallback) {
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000222 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000223 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700224}
225
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700226// Helper callback that puts the async appInfo callback data into a promise
227class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
228 public:
229 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
230 ALOGD("Got app info callback with %zu apps", appInfo.size());
231 promise.set_value(appInfo);
232 return Status::ok();
233 }
234
235 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
236 const std::vector<String16>& /* msgContentPerms */) override {
237 return Status::ok();
238 }
239
240 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
241
242 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
243 return Status::ok();
244 }
245
Anthony Stange7fba1002023-03-02 21:45:20 +0000246 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
247 return Status::ok();
248 }
Anthony Stange7344af92022-12-22 14:21:31 +0000249
Matthew Sedamadfd5572023-11-21 05:53:00 -0800250 Status handleMessageDeliveryStatus(
251 char16_t /* hostEndPointId */,
252 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
253 return Status::ok();
254 }
255
Lei Ju8787afa2023-06-28 09:36:19 -0700256 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
257 *out_uuid = kUuid;
258 return Status::ok();
259 }
260
Lei Ju5b2ded42023-12-05 15:08:21 -0800261 Status getName(::android::String16* out_name) override {
262 *out_name = kName;
263 return Status::ok();
264 }
265
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700266 std::promise<std::vector<NanoappInfo>> promise;
267};
268
269// Calls queryApps() and checks the returned metadata
270TEST_P(ContextHubAidl, TestQueryApps) {
271 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000272 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
273 ASSERT_TRUE(mContextHub->queryNanoapps(getHubId()).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700274
275 std::vector<NanoappInfo> appInfoList;
276 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
277 for (const NanoappInfo& appInfo : appInfoList) {
278 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
279 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
Arthur Ishiguro08103072021-12-09 18:30:49 +0000280
281 // Verify services are unique.
282 std::set<uint64_t> existingServiceIds;
283 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
284 EXPECT_NE(rpcService.id, UINT64_C(0));
285 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
286 existingServiceIds.insert(rpcService.id);
287 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700288 }
289}
290
Matthew Sedamd70f84d2023-03-06 18:34:24 +0000291// Calls getPreloadedNanoappsIds() and verifies there are preloaded nanoapps
292TEST_P(ContextHubAidl, TestGetPreloadedNanoappIds) {
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000293 std::vector<int64_t> preloadedNanoappIds;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000294 Status status = mContextHub->getPreloadedNanoappIds(getHubId(), &preloadedNanoappIds);
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000295 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
296 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000297 GTEST_SKIP() << "Not supported -> old API; or not implemented";
298 } else {
299 ASSERT_TRUE(status.isOk());
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000300 }
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000301}
302
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700303// Helper callback that puts the TransactionResult for the expectedTransactionId into a
304// promise
305class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
306 public:
307 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
308 return Status::ok();
309 }
310
311 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
312 const std::vector<String16>& /* msgContentPerms */) override {
313 return Status::ok();
314 }
315
316 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
317
318 Status handleTransactionResult(int32_t transactionId, bool success) override {
319 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
320 ") with success %d",
321 transactionId, expectedTransactionId, success);
322 if (transactionId == expectedTransactionId) {
323 promise.set_value(success);
324 }
325 return Status::ok();
326 }
327
Anthony Stange7fba1002023-03-02 21:45:20 +0000328 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
329 return Status::ok();
330 }
Anthony Stange7344af92022-12-22 14:21:31 +0000331
Matthew Sedamadfd5572023-11-21 05:53:00 -0800332 Status handleMessageDeliveryStatus(
333 char16_t /* hostEndPointId */,
334 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
335 return Status::ok();
336 }
337
Lei Ju8787afa2023-06-28 09:36:19 -0700338 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
339 *out_uuid = kUuid;
340 return Status::ok();
341 }
342
Lei Ju5b2ded42023-12-05 15:08:21 -0800343 Status getName(::android::String16* out_name) override {
344 *out_name = kName;
345 return Status::ok();
346 }
347
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700348 uint32_t expectedTransactionId = 0;
349 std::promise<bool> promise;
350};
351
352// Parameterized fixture that sets the callback to TransactionResultCallback
353class ContextHubTransactionTest : public ContextHubAidl {
354 public:
355 virtual void SetUp() override {
356 ContextHubAidl::SetUp();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000357 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700358 }
359
360 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
361};
362
363TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
364 ContextHubMessage message;
365 message.nanoappId = kNonExistentAppId;
366 message.messageType = 1;
367 message.messageBody.resize(4);
368 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
369
370 ALOGD("Sending message to non-existent nanoapp");
Madhav Iyengaree25d072024-12-17 17:08:55 +0000371 ASSERT_TRUE(mContextHub->sendMessageToHub(getHubId(), message).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700372}
373
374TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
375 cb->expectedTransactionId = 0123;
376 NanoappBinary emptyApp;
377
378 emptyApp.nanoappId = kNonExistentAppId;
379 emptyApp.nanoappVersion = 1;
380 emptyApp.flags = 0;
381 emptyApp.targetChreApiMajorVersion = 1;
382 emptyApp.targetChreApiMinorVersion = 0;
383
384 ALOGD("Loading empty nanoapp");
Madhav Iyengaree25d072024-12-17 17:08:55 +0000385 bool success = mContextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700386 if (success) {
387 bool transactionSuccess;
388 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
389 ASSERT_FALSE(transactionSuccess);
390 }
391}
392
393TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) {
394 cb->expectedTransactionId = 1234;
395
396 ALOGD("Unloading nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000397 bool success =
Madhav Iyengaree25d072024-12-17 17:08:55 +0000398 mContextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000399 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700400 if (success) {
401 bool transactionSuccess;
402 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
403 ASSERT_FALSE(transactionSuccess);
404 }
405}
406
407TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) {
408 cb->expectedTransactionId = 2345;
409
410 ALOGD("Enabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000411 bool success =
Madhav Iyengaree25d072024-12-17 17:08:55 +0000412 mContextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000413 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700414 if (success) {
415 bool transactionSuccess;
416 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
417 ASSERT_FALSE(transactionSuccess);
418 }
419}
420
421TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) {
422 cb->expectedTransactionId = 3456;
423
424 ALOGD("Disabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000425 bool success =
Madhav Iyengaree25d072024-12-17 17:08:55 +0000426 mContextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000427 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700428 if (success) {
429 bool transactionSuccess;
430 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
431 ASSERT_FALSE(transactionSuccess);
432 }
433}
434
435void ContextHubAidl::testSettingChanged(Setting setting) {
436 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
437 // verify the expected E2E behavior in CHRE
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000438 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000439 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700440
Madhav Iyengaree25d072024-12-17 17:08:55 +0000441 ASSERT_TRUE(mContextHub->onSettingChanged(setting, true /* enabled */).isOk());
442 ASSERT_TRUE(mContextHub->onSettingChanged(setting, false /* enabled */).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700443}
444
445TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
446 testSettingChanged(Setting::LOCATION);
447}
448
449TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
450 testSettingChanged(Setting::WIFI_MAIN);
451}
452
453TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
454 testSettingChanged(Setting::WIFI_SCANNING);
455}
456
457TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
458 testSettingChanged(Setting::AIRPLANE_MODE);
459}
460
461TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
462 testSettingChanged(Setting::MICROPHONE);
463}
464
Anthonya6b65002022-01-20 20:49:10 +0000465TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) {
466 testSettingChanged(Setting::BT_MAIN);
467}
468
469TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) {
470 testSettingChanged(Setting::BT_SCANNING);
471}
472
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700473std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
474 std::vector<std::tuple<std::string, int32_t>> tuples;
475 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
476 std::vector<ContextHubInfo> contextHubInfos;
477
478 for (int i = 0; i < contextHubAidlNames.size(); i++) {
479 auto contextHubName = contextHubAidlNames[i].c_str();
480 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
481 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
482 for (auto& info : contextHubInfos) {
483 tuples.push_back(std::make_tuple(contextHubName, info.id));
484 }
485 }
486 }
487
488 return tuples;
489}
490
Lei Jubc6ead02024-02-05 18:15:08 -0800491TEST_P(ContextHubTransactionTest, TestHostConnection) {
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000492 constexpr char16_t kHostEndpointId = 1;
493 HostEndpointInfo hostEndpointInfo;
Arthur Ishiguro16f40622023-02-10 18:11:12 +0000494 hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000495 hostEndpointInfo.hostEndpointId = kHostEndpointId;
496
Madhav Iyengaree25d072024-12-17 17:08:55 +0000497 ASSERT_TRUE(mContextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
498 ASSERT_TRUE(mContextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000499}
500
Lei Jubc6ead02024-02-05 18:15:08 -0800501TEST_P(ContextHubTransactionTest, TestInvalidHostConnection) {
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000502 constexpr char16_t kHostEndpointId = 1;
503
Madhav Iyengaree25d072024-12-17 17:08:55 +0000504 ASSERT_TRUE(mContextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000505}
506
Lei Jubc6ead02024-02-05 18:15:08 -0800507TEST_P(ContextHubTransactionTest, TestNanSessionStateChange) {
Anthony Stange7fba1002023-03-02 21:45:20 +0000508 NanSessionStateUpdate update;
509 update.state = true;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000510 Status status = mContextHub->onNanSessionStateChanged(update);
Rocky Fang78140982023-06-16 00:42:41 +0000511 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
512 status.transactionError() == android::UNKNOWN_TRANSACTION) {
513 GTEST_SKIP() << "Not supported -> old API; or not implemented";
514 } else {
515 ASSERT_TRUE(status.isOk());
516 update.state = false;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000517 ASSERT_TRUE(mContextHub->onNanSessionStateChanged(update).isOk());
Rocky Fang78140982023-06-16 00:42:41 +0000518 }
Anthony Stange7344af92022-12-22 14:21:31 +0000519}
520
Matthew Sedamadfd5572023-11-21 05:53:00 -0800521TEST_P(ContextHubAidl, TestSendMessageDeliveryStatusToHub) {
522 MessageDeliveryStatus messageDeliveryStatus;
523 messageDeliveryStatus.messageSequenceNumber = 123;
524 messageDeliveryStatus.errorCode = ErrorCode::OK;
525
Madhav Iyengaree25d072024-12-17 17:08:55 +0000526 Status status = mContextHub->sendMessageDeliveryStatusToHub(getHubId(), messageDeliveryStatus);
Matthew Sedamadfd5572023-11-21 05:53:00 -0800527 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
528 status.transactionError() == android::UNKNOWN_TRANSACTION) {
529 GTEST_SKIP() << "Not supported -> old API; or not implemented";
530 } else {
531 EXPECT_TRUE(status.isOk());
532 }
533}
534
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000535class TestEndpointCallback : public BnEndpointCallback {
536 public:
537 Status onEndpointStarted(const std::vector<EndpointInfo>& /* endpointInfos */) override {
538 return Status::ok();
539 }
540
541 Status onEndpointStopped(const std::vector<EndpointId>& /* endpointIds */,
542 Reason /* reason */) override {
543 return Status::ok();
544 }
545
546 Status onMessageReceived(int32_t /* sessionId */, const Message& message) override {
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800547 {
548 std::unique_lock<std::mutex> lock(mMutex);
549 mMessages.push_back(message);
550 }
551 mCondVar.notify_one();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000552 return Status::ok();
553 }
554
555 Status onMessageDeliveryStatusReceived(int32_t /* sessionId */,
556 const MessageDeliveryStatus& /* msgStatus */) override {
557 return Status::ok();
558 }
559
560 Status onEndpointSessionOpenRequest(
561 int32_t /* sessionId */, const EndpointId& /* destination */,
562 const EndpointId& /* initiator */,
563 const std::optional<String16>& /* serviceDescriptor */) override {
564 return Status::ok();
565 }
566
567 Status onCloseEndpointSession(int32_t /* sessionId */, Reason /* reason */) override {
568 return Status::ok();
569 }
570
571 Status onEndpointSessionOpenComplete(int32_t /* sessionId */) override {
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800572 {
573 std::unique_lock<std::mutex> lock(mMutex);
574 mWasOnEndpointSessionOpenCompleteCalled = true;
575 }
576 mCondVar.notify_one();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000577 return Status::ok();
578 }
579
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000580 bool wasOnEndpointSessionOpenCompleteCalled() {
581 return mWasOnEndpointSessionOpenCompleteCalled;
582 }
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800583
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000584 void resetWasOnEndpointSessionOpenCompleteCalled() {
585 mWasOnEndpointSessionOpenCompleteCalled = false;
586 }
587
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800588 std::mutex& getMutex() { return mMutex; }
589 std::condition_variable& getCondVar() { return mCondVar; }
590 std::vector<Message> getMessages() { return mMessages; }
591
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000592 private:
593 std::vector<Message> mMessages;
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800594 std::mutex mMutex;
595 std::condition_variable mCondVar;
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000596 bool mWasOnEndpointSessionOpenCompleteCalled = false;
597};
598
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000599TEST_P(ContextHubEndpointAidlWithTestMode, RegisterHub) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000600 if (!registerDefaultHub()) {
601 GTEST_SKIP() << "Not supported -> old API; or not implemented";
602 }
603
604 sp<IEndpointCommunication> hub2;
605 Status status = registerHub(kDefaultHubId + 1, &hub2);
606 EXPECT_TRUE(status.isOk());
607
608 sp<IEndpointCommunication> hub3;
609 status = registerHub(kDefaultHubId + 1, &hub3);
610 ASSERT_FALSE(status.isOk());
611 EXPECT_EQ(status.exceptionCode(), Status::EX_ILLEGAL_STATE);
612
613 hub2->unregister();
614 status = registerHub(kDefaultHubId + 1, &hub3);
615 EXPECT_TRUE(status.isOk());
616}
617
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000618TEST_P(ContextHubEndpointAidlWithTestMode, RegisterEndpoint) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000619 if (!registerDefaultHub()) {
620 GTEST_SKIP() << "Not supported -> old API; or not implemented";
621 }
622
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000623 EndpointInfo endpointInfo;
624 endpointInfo.id.id = 1;
625 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
626 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
627 endpointInfo.name = String16("Test host endpoint 1");
628 endpointInfo.version = 42;
629
Madhav Iyengaree25d072024-12-17 17:08:55 +0000630 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000631 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
632 status.transactionError() == android::UNKNOWN_TRANSACTION) {
633 GTEST_SKIP() << "Not supported -> old API; or not implemented";
634 } else {
635 EXPECT_TRUE(status.isOk());
636 }
637}
638
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000639TEST_P(ContextHubEndpointAidlWithTestMode, RegisterEndpointSameNameFailure) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000640 if (!registerDefaultHub()) {
641 GTEST_SKIP() << "Not supported -> old API; or not implemented";
642 }
643
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000644 EndpointInfo endpointInfo;
645 endpointInfo.id.id = 2;
646 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
647 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
648 endpointInfo.name = String16("Test host endpoint 2");
649 endpointInfo.version = 42;
650
651 EndpointInfo endpointInfo2;
652 endpointInfo2.id.id = 3;
653 endpointInfo2.id.hubId = 0xCAFECAFECAFECAFE;
654 endpointInfo2.type = EndpointInfo::EndpointType::NATIVE;
655 endpointInfo2.name = String16("Test host endpoint 2");
656 endpointInfo2.version = 42;
657
Madhav Iyengaree25d072024-12-17 17:08:55 +0000658 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000659 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
660 status.transactionError() == android::UNKNOWN_TRANSACTION) {
661 GTEST_SKIP() << "Not supported -> old API; or not implemented";
662 } else {
663 EXPECT_TRUE(status.isOk());
664 }
665
Madhav Iyengaree25d072024-12-17 17:08:55 +0000666 EXPECT_FALSE(mHubInterface->registerEndpoint(endpointInfo2).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000667}
668
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000669TEST_P(ContextHubEndpointAidlWithTestMode, RegisterEndpointSameIdFailure) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000670 if (!registerDefaultHub()) {
671 GTEST_SKIP() << "Not supported -> old API; or not implemented";
672 }
673
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000674 EndpointInfo endpointInfo;
675 endpointInfo.id.id = 4;
676 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
677 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
678 endpointInfo.name = String16("Test host endpoint 4");
679 endpointInfo.version = 42;
680
681 EndpointInfo endpointInfo2;
682 endpointInfo2.id.id = 4;
683 endpointInfo2.id.hubId = 0xCAFECAFECAFECAFE;
684 endpointInfo2.type = EndpointInfo::EndpointType::NATIVE;
685 endpointInfo2.name = String16("Test host endpoint - same ID test");
686 endpointInfo2.version = 42;
687
Madhav Iyengaree25d072024-12-17 17:08:55 +0000688 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000689 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
690 status.transactionError() == android::UNKNOWN_TRANSACTION) {
691 GTEST_SKIP() << "Not supported -> old API; or not implemented";
692 } else {
693 EXPECT_TRUE(status.isOk());
694 }
695
Madhav Iyengaree25d072024-12-17 17:08:55 +0000696 EXPECT_FALSE(mHubInterface->registerEndpoint(endpointInfo2).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000697}
698
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000699TEST_P(ContextHubEndpointAidlWithTestMode, UnregisterEndpoint) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000700 if (!registerDefaultHub()) {
701 GTEST_SKIP() << "Not supported -> old API; or not implemented";
702 }
703
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000704 EndpointInfo endpointInfo;
705 endpointInfo.id.id = 6;
706 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
707 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
708 endpointInfo.name = String16("Test host endpoint 6");
709 endpointInfo.version = 42;
710
Madhav Iyengaree25d072024-12-17 17:08:55 +0000711 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000712 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
713 status.transactionError() == android::UNKNOWN_TRANSACTION) {
714 GTEST_SKIP() << "Not supported -> old API; or not implemented";
715 } else {
716 EXPECT_TRUE(status.isOk());
717 }
718
Madhav Iyengaree25d072024-12-17 17:08:55 +0000719 EXPECT_TRUE(mHubInterface->unregisterEndpoint(endpointInfo).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000720}
721
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000722TEST_P(ContextHubEndpointAidlWithTestMode, UnregisterEndpointNonexistent) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000723 if (!registerDefaultHub()) {
724 GTEST_SKIP() << "Not supported -> old API; or not implemented";
725 }
726
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000727 EndpointInfo endpointInfo;
728 endpointInfo.id.id = 100;
729 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
730 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
731 endpointInfo.name = String16("Test host endpoint 100");
732 endpointInfo.version = 42;
733
Madhav Iyengaree25d072024-12-17 17:08:55 +0000734 Status status = mHubInterface->unregisterEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000735 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
736 status.transactionError() == android::UNKNOWN_TRANSACTION) {
737 GTEST_SKIP() << "Not supported -> old API; or not implemented";
738 } else {
739 EXPECT_FALSE(status.isOk());
740 }
741}
742
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000743TEST_P(ContextHubEndpointAidlWithTestMode, OpenEndpointSessionInvalidRange) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000744 if (!registerDefaultHub()) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000745 GTEST_SKIP() << "Not supported -> old API; or not implemented";
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000746 }
747
748 // Register the endpoint
749 EndpointInfo initiatorEndpoint;
750 initiatorEndpoint.id.id = 7;
751 initiatorEndpoint.id.hubId = 0xCAFECAFECAFECAFE;
752 initiatorEndpoint.type = EndpointInfo::EndpointType::NATIVE;
753 initiatorEndpoint.name = String16("Test host endpoint 7");
754 initiatorEndpoint.version = 42;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000755 EXPECT_TRUE(mHubInterface->registerEndpoint(initiatorEndpoint).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000756
757 // Find the destination, if it exists
758 std::vector<EndpointInfo> endpoints;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000759 EXPECT_TRUE(mContextHub->getEndpoints(&endpoints).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000760 const EndpointInfo* destinationEndpoint = nullptr;
761 for (const EndpointInfo& endpoint : endpoints) {
762 for (const Service& service : endpoint.services) {
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800763 if (service.serviceDescriptor == kEchoServiceName) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000764 destinationEndpoint = &endpoint;
765 break;
766 }
767 }
768 }
769 if (destinationEndpoint == nullptr) {
770 return; // no echo service endpoint -> just return
771 }
772
773 // Request the range
774 constexpr int32_t requestedRange = 100;
Yifei Zhangcef8f662024-12-06 16:03:26 -0800775 std::array<int32_t, 2> range;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000776 ASSERT_TRUE(mHubInterface->requestSessionIdRange(requestedRange, &range).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000777 EXPECT_EQ(range.size(), 2);
778 EXPECT_GE(range[1] - range[0] + 1, requestedRange);
779
780 // Open the session
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000781 int32_t sessionId = range[1] + 10; // invalid
Madhav Iyengaree25d072024-12-17 17:08:55 +0000782 EXPECT_FALSE(mHubInterface
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000783 ->openEndpointSession(sessionId, destinationEndpoint->id,
784 initiatorEndpoint.id,
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800785 /* in_serviceDescriptor= */ kEchoServiceName)
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000786 .isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000787}
788
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000789TEST_P(ContextHubEndpointAidlWithTestMode, OpenEndpointSessionAndSendMessageEchoesBack) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000790 if (!registerDefaultHub()) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000791 GTEST_SKIP() << "Not supported -> old API; or not implemented";
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000792 }
Madhav Iyengaree25d072024-12-17 17:08:55 +0000793 std::unique_lock<std::mutex> lock(mEndpointCb->getMutex());
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800794
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000795 // Register the endpoint
796 EndpointInfo initiatorEndpoint;
797 initiatorEndpoint.id.id = 8;
798 initiatorEndpoint.id.hubId = 0xCAFECAFECAFECAFE;
799 initiatorEndpoint.type = EndpointInfo::EndpointType::NATIVE;
800 initiatorEndpoint.name = String16("Test host endpoint 7");
801 initiatorEndpoint.version = 42;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000802 EXPECT_TRUE(mHubInterface->registerEndpoint(initiatorEndpoint).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000803
804 // Find the destination, if it exists
805 std::vector<EndpointInfo> endpoints;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000806 EXPECT_TRUE(mContextHub->getEndpoints(&endpoints).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000807 const EndpointInfo* destinationEndpoint = nullptr;
808 for (const EndpointInfo& endpoint : endpoints) {
809 for (const Service& service : endpoint.services) {
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800810 if (service.serviceDescriptor == kEchoServiceName) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000811 destinationEndpoint = &endpoint;
812 break;
813 }
814 }
815 }
816 if (destinationEndpoint == nullptr) {
817 return; // no echo service endpoint -> just return
818 }
819
820 // Request the range
821 constexpr int32_t requestedRange = 100;
Yifei Zhangcef8f662024-12-06 16:03:26 -0800822 std::array<int32_t, 2> range;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000823 ASSERT_TRUE(mHubInterface->requestSessionIdRange(requestedRange, &range).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000824 EXPECT_EQ(range.size(), 2);
825 EXPECT_GE(range[1] - range[0] + 1, requestedRange);
826
827 // Open the session
Madhav Iyengaree25d072024-12-17 17:08:55 +0000828 mEndpointCb->resetWasOnEndpointSessionOpenCompleteCalled();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000829 int32_t sessionId = range[0];
Madhav Iyengaree25d072024-12-17 17:08:55 +0000830 ASSERT_TRUE(mHubInterface
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000831 ->openEndpointSession(sessionId, destinationEndpoint->id,
832 initiatorEndpoint.id,
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800833 /* in_serviceDescriptor= */ kEchoServiceName)
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000834 .isOk());
Madhav Iyengaree25d072024-12-17 17:08:55 +0000835 mEndpointCb->getCondVar().wait(lock);
836 EXPECT_TRUE(mEndpointCb->wasOnEndpointSessionOpenCompleteCalled());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000837
838 // Send the message
839 Message message;
840 message.flags = 0;
841 message.sequenceNumber = 0;
842 message.content.push_back(42);
Madhav Iyengaree25d072024-12-17 17:08:55 +0000843 ASSERT_TRUE(mHubInterface->sendMessageToEndpoint(sessionId, message).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000844
845 // Check for echo
Madhav Iyengaree25d072024-12-17 17:08:55 +0000846 mEndpointCb->getCondVar().wait(lock);
847 EXPECT_FALSE(mEndpointCb->getMessages().empty());
848 EXPECT_EQ(mEndpointCb->getMessages().back().content.back(), 42);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000849}
850
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700851std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
852 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
853}
854
855GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
856INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
857 PrintGeneratedTest);
858
Arthur Ishiguro4e3314a2025-01-17 18:12:11 +0000859GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubEndpointAidl);
860INSTANTIATE_TEST_SUITE_P(
861 ContextHub, ContextHubEndpointAidl,
862 testing::ValuesIn(android::getAidlHalInstanceNames(IContextHub::descriptor)),
863 android::PrintInstanceNameToString);
864
865GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubEndpointAidlWithTestMode);
866INSTANTIATE_TEST_SUITE_P(
867 ContextHub, ContextHubEndpointAidlWithTestMode,
868 testing::ValuesIn(android::getAidlHalInstanceNames(IContextHub::descriptor)),
869 android::PrintInstanceNameToString);
Matthew Sedam41db0932025-01-02 11:17:46 -0800870
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700871GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
872INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
873 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
874
875int main(int argc, char** argv) {
876 ::testing::InitGoogleTest(&argc, argv);
Matthew Sedam41db0932025-01-02 11:17:46 -0800877 ProcessState::self()->setThreadPoolMaxThreadCount(2);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700878 ProcessState::self()->startThreadPool();
879 return RUN_ALL_TESTS();
880}