blob: d7859d9920147a1d8ca50fb7a75097c65f6f5877 [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 Ishiguro4e916c32021-08-12 12:47:03 -070075class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> {
76 public:
Madhav Iyengaree25d072024-12-17 17:08:55 +000077 void SetUp() override {
78 mContextHub = android::waitForDeclaredService<IContextHub>(
Arthur Ishiguro4e916c32021-08-12 12:47:03 -070079 String16(std::get<0>(GetParam()).c_str()));
Madhav Iyengaree25d072024-12-17 17:08:55 +000080 ASSERT_NE(mContextHub, nullptr);
81 mEndpointCb = sp<TestEndpointCallback>::make();
Matthew Sedam41db0932025-01-02 11:17:46 -080082 }
83
84 uint32_t getHubId() { return std::get<1>(GetParam()); }
85
Madhav Iyengaree25d072024-12-17 17:08:55 +000086 Status registerHub(int64_t id, sp<IEndpointCommunication>* hubInterface) {
87 HubInfo info;
88 info.hubId = id;
89 return mContextHub->registerEndpointHub(mEndpointCb, info, hubInterface);
90 }
91
92 bool registerDefaultHub() {
93 Status status = registerHub(kDefaultHubId, &mHubInterface);
94 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
95 status.transactionError() == android::UNKNOWN_TRANSACTION) {
96 return false;
97 }
98 EXPECT_TRUE(status.isOk());
99 EXPECT_NE(mHubInterface, nullptr);
100 if (!mHubInterface) {
101 return false;
102 }
103 return true;
104 }
Matthew Sedam41db0932025-01-02 11:17:46 -0800105
106 void testSettingChanged(Setting setting);
Madhav Iyengaree25d072024-12-17 17:08:55 +0000107
108 sp<IContextHub> mContextHub;
109 sp<TestEndpointCallback> mEndpointCb;
110 sp<IEndpointCommunication> mHubInterface;
Matthew Sedam41db0932025-01-02 11:17:46 -0800111};
112
113class ContextHubAidlWithTestMode : public ContextHubAidl {
114 public:
Madhav Iyengaree25d072024-12-17 17:08:55 +0000115 void SetUp() override {
Matthew Sedam41db0932025-01-02 11:17:46 -0800116 ContextHubAidl::SetUp();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000117
118 // Best effort enable test mode - this may not be supported on older HALS, so we
119 // ignore the return value.
Madhav Iyengaree25d072024-12-17 17:08:55 +0000120 mContextHub->setTestMode(/* enable= */ true);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700121 }
122
Madhav Iyengaree25d072024-12-17 17:08:55 +0000123 void TearDown() override {
124 mContextHub->setTestMode(/* enable= */ false);
Matthew Sedam41db0932025-01-02 11:17:46 -0800125 ContextHubAidl::TearDown();
126 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700127};
128
129TEST_P(ContextHubAidl, TestGetHubs) {
130 std::vector<ContextHubInfo> hubs;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000131 ASSERT_TRUE(mContextHub->getContextHubs(&hubs).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700132
133 ALOGD("System reports %zu hubs", hubs.size());
134
135 for (const ContextHubInfo& hub : hubs) {
136 ALOGD("Checking hub ID %" PRIu32, hub.id);
137
138 EXPECT_GT(hub.name.size(), 0);
139 EXPECT_GT(hub.vendor.size(), 0);
140 EXPECT_GT(hub.toolchain.size(), 0);
141 EXPECT_GT(hub.peakMips, 0);
142 EXPECT_GT(hub.chrePlatformId, 0);
143 EXPECT_GT(hub.chreApiMajorVersion, 0);
Arthur Ishiguro6471f612021-10-28 21:59:55 +0000144 EXPECT_GE(hub.chreApiMinorVersion, 0);
145 EXPECT_GE(hub.chrePatchVersion, 0);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700146
147 // Minimum 128 byte MTU as required by CHRE API v1.0
148 EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128));
149 }
150}
151
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000152TEST_P(ContextHubAidl, TestEnableTestMode) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000153 Status status = mContextHub->setTestMode(true);
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000154 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
155 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000156 GTEST_SKIP() << "Not supported -> old API; or not implemented";
157 } else {
158 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000159 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000160}
161
162TEST_P(ContextHubAidl, TestDisableTestMode) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000163 Status status = mContextHub->setTestMode(false);
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000164 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
165 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000166 GTEST_SKIP() << "Not supported -> old API; or not implemented";
167 } else {
168 ASSERT_TRUE(status.isOk());
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000169 }
Matthew Sedamc8ce4d52023-01-09 20:18:21 +0000170}
171
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000172class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
173 public:
174 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
175 return Status::ok();
176 }
177
178 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
179 const std::vector<String16>& /* msgContentPerms */) override {
180 return Status::ok();
181 }
182
183 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
184
185 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
186 return Status::ok();
187 }
Anthony Stange7344af92022-12-22 14:21:31 +0000188
Anthony Stange7fba1002023-03-02 21:45:20 +0000189 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
190 return Status::ok();
191 }
Lei Ju8787afa2023-06-28 09:36:19 -0700192
Matthew Sedamadfd5572023-11-21 05:53:00 -0800193 Status handleMessageDeliveryStatus(
194 char16_t /* hostEndPointId */,
195 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
196 return Status::ok();
197 }
198
Lei Ju8787afa2023-06-28 09:36:19 -0700199 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
200 *out_uuid = kUuid;
201 return Status::ok();
202 }
Lei Ju5b2ded42023-12-05 15:08:21 -0800203
204 Status getName(::android::String16* out_name) override {
205 *out_name = kName;
206 return Status::ok();
207 }
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000208};
209
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700210TEST_P(ContextHubAidl, TestRegisterCallback) {
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000211 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000212 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700213}
214
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700215// Helper callback that puts the async appInfo callback data into a promise
216class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
217 public:
218 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
219 ALOGD("Got app info callback with %zu apps", appInfo.size());
220 promise.set_value(appInfo);
221 return Status::ok();
222 }
223
224 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
225 const std::vector<String16>& /* msgContentPerms */) override {
226 return Status::ok();
227 }
228
229 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
230
231 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
232 return Status::ok();
233 }
234
Anthony Stange7fba1002023-03-02 21:45:20 +0000235 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
236 return Status::ok();
237 }
Anthony Stange7344af92022-12-22 14:21:31 +0000238
Matthew Sedamadfd5572023-11-21 05:53:00 -0800239 Status handleMessageDeliveryStatus(
240 char16_t /* hostEndPointId */,
241 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
242 return Status::ok();
243 }
244
Lei Ju8787afa2023-06-28 09:36:19 -0700245 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
246 *out_uuid = kUuid;
247 return Status::ok();
248 }
249
Lei Ju5b2ded42023-12-05 15:08:21 -0800250 Status getName(::android::String16* out_name) override {
251 *out_name = kName;
252 return Status::ok();
253 }
254
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700255 std::promise<std::vector<NanoappInfo>> promise;
256};
257
258// Calls queryApps() and checks the returned metadata
259TEST_P(ContextHubAidl, TestQueryApps) {
260 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000261 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
262 ASSERT_TRUE(mContextHub->queryNanoapps(getHubId()).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700263
264 std::vector<NanoappInfo> appInfoList;
265 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
266 for (const NanoappInfo& appInfo : appInfoList) {
267 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
268 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
Arthur Ishiguro08103072021-12-09 18:30:49 +0000269
270 // Verify services are unique.
271 std::set<uint64_t> existingServiceIds;
272 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
273 EXPECT_NE(rpcService.id, UINT64_C(0));
274 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
275 existingServiceIds.insert(rpcService.id);
276 }
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700277 }
278}
279
Matthew Sedamd70f84d2023-03-06 18:34:24 +0000280// Calls getPreloadedNanoappsIds() and verifies there are preloaded nanoapps
281TEST_P(ContextHubAidl, TestGetPreloadedNanoappIds) {
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000282 std::vector<int64_t> preloadedNanoappIds;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000283 Status status = mContextHub->getPreloadedNanoappIds(getHubId(), &preloadedNanoappIds);
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000284 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
285 status.transactionError() == android::UNKNOWN_TRANSACTION) {
Matthew Sedam121b6d62023-01-19 19:04:53 +0000286 GTEST_SKIP() << "Not supported -> old API; or not implemented";
287 } else {
288 ASSERT_TRUE(status.isOk());
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000289 }
Arthur Ishigurofd5e65c2022-11-08 16:49:47 +0000290}
291
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700292// Helper callback that puts the TransactionResult for the expectedTransactionId into a
293// promise
294class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
295 public:
296 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
297 return Status::ok();
298 }
299
300 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
301 const std::vector<String16>& /* msgContentPerms */) override {
302 return Status::ok();
303 }
304
305 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
306
307 Status handleTransactionResult(int32_t transactionId, bool success) override {
308 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
309 ") with success %d",
310 transactionId, expectedTransactionId, success);
311 if (transactionId == expectedTransactionId) {
312 promise.set_value(success);
313 }
314 return Status::ok();
315 }
316
Anthony Stange7fba1002023-03-02 21:45:20 +0000317 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
318 return Status::ok();
319 }
Anthony Stange7344af92022-12-22 14:21:31 +0000320
Matthew Sedamadfd5572023-11-21 05:53:00 -0800321 Status handleMessageDeliveryStatus(
322 char16_t /* hostEndPointId */,
323 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
324 return Status::ok();
325 }
326
Lei Ju8787afa2023-06-28 09:36:19 -0700327 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
328 *out_uuid = kUuid;
329 return Status::ok();
330 }
331
Lei Ju5b2ded42023-12-05 15:08:21 -0800332 Status getName(::android::String16* out_name) override {
333 *out_name = kName;
334 return Status::ok();
335 }
336
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700337 uint32_t expectedTransactionId = 0;
338 std::promise<bool> promise;
339};
340
341// Parameterized fixture that sets the callback to TransactionResultCallback
342class ContextHubTransactionTest : public ContextHubAidl {
343 public:
344 virtual void SetUp() override {
345 ContextHubAidl::SetUp();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000346 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700347 }
348
349 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
350};
351
352TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
353 ContextHubMessage message;
354 message.nanoappId = kNonExistentAppId;
355 message.messageType = 1;
356 message.messageBody.resize(4);
357 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
358
359 ALOGD("Sending message to non-existent nanoapp");
Madhav Iyengaree25d072024-12-17 17:08:55 +0000360 ASSERT_TRUE(mContextHub->sendMessageToHub(getHubId(), message).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700361}
362
363TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
364 cb->expectedTransactionId = 0123;
365 NanoappBinary emptyApp;
366
367 emptyApp.nanoappId = kNonExistentAppId;
368 emptyApp.nanoappVersion = 1;
369 emptyApp.flags = 0;
370 emptyApp.targetChreApiMajorVersion = 1;
371 emptyApp.targetChreApiMinorVersion = 0;
372
373 ALOGD("Loading empty nanoapp");
Madhav Iyengaree25d072024-12-17 17:08:55 +0000374 bool success = mContextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700375 if (success) {
376 bool transactionSuccess;
377 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
378 ASSERT_FALSE(transactionSuccess);
379 }
380}
381
382TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) {
383 cb->expectedTransactionId = 1234;
384
385 ALOGD("Unloading nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000386 bool success =
Madhav Iyengaree25d072024-12-17 17:08:55 +0000387 mContextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000388 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700389 if (success) {
390 bool transactionSuccess;
391 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
392 ASSERT_FALSE(transactionSuccess);
393 }
394}
395
396TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) {
397 cb->expectedTransactionId = 2345;
398
399 ALOGD("Enabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000400 bool success =
Madhav Iyengaree25d072024-12-17 17:08:55 +0000401 mContextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000402 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700403 if (success) {
404 bool transactionSuccess;
405 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
406 ASSERT_FALSE(transactionSuccess);
407 }
408}
409
410TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) {
411 cb->expectedTransactionId = 3456;
412
413 ALOGD("Disabling nonexistent nanoapp");
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000414 bool success =
Madhav Iyengaree25d072024-12-17 17:08:55 +0000415 mContextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
Arthur Ishiguro070f47d2022-01-06 22:42:10 +0000416 .isOk();
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700417 if (success) {
418 bool transactionSuccess;
419 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
420 ASSERT_FALSE(transactionSuccess);
421 }
422}
423
424void ContextHubAidl::testSettingChanged(Setting setting) {
425 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
426 // verify the expected E2E behavior in CHRE
Arthur Ishiguroe6b540d2021-10-29 16:01:35 +0000427 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
Madhav Iyengaree25d072024-12-17 17:08:55 +0000428 ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700429
Madhav Iyengaree25d072024-12-17 17:08:55 +0000430 ASSERT_TRUE(mContextHub->onSettingChanged(setting, true /* enabled */).isOk());
431 ASSERT_TRUE(mContextHub->onSettingChanged(setting, false /* enabled */).isOk());
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700432}
433
434TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
435 testSettingChanged(Setting::LOCATION);
436}
437
438TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
439 testSettingChanged(Setting::WIFI_MAIN);
440}
441
442TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
443 testSettingChanged(Setting::WIFI_SCANNING);
444}
445
446TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
447 testSettingChanged(Setting::AIRPLANE_MODE);
448}
449
450TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
451 testSettingChanged(Setting::MICROPHONE);
452}
453
Anthonya6b65002022-01-20 20:49:10 +0000454TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) {
455 testSettingChanged(Setting::BT_MAIN);
456}
457
458TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) {
459 testSettingChanged(Setting::BT_SCANNING);
460}
461
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700462std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
463 std::vector<std::tuple<std::string, int32_t>> tuples;
464 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
465 std::vector<ContextHubInfo> contextHubInfos;
466
467 for (int i = 0; i < contextHubAidlNames.size(); i++) {
468 auto contextHubName = contextHubAidlNames[i].c_str();
469 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
470 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
471 for (auto& info : contextHubInfos) {
472 tuples.push_back(std::make_tuple(contextHubName, info.id));
473 }
474 }
475 }
476
477 return tuples;
478}
479
Lei Jubc6ead02024-02-05 18:15:08 -0800480TEST_P(ContextHubTransactionTest, TestHostConnection) {
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000481 constexpr char16_t kHostEndpointId = 1;
482 HostEndpointInfo hostEndpointInfo;
Arthur Ishiguro16f40622023-02-10 18:11:12 +0000483 hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE;
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000484 hostEndpointInfo.hostEndpointId = kHostEndpointId;
485
Madhav Iyengaree25d072024-12-17 17:08:55 +0000486 ASSERT_TRUE(mContextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
487 ASSERT_TRUE(mContextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000488}
489
Lei Jubc6ead02024-02-05 18:15:08 -0800490TEST_P(ContextHubTransactionTest, TestInvalidHostConnection) {
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000491 constexpr char16_t kHostEndpointId = 1;
492
Madhav Iyengaree25d072024-12-17 17:08:55 +0000493 ASSERT_TRUE(mContextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
Arthur Ishiguro065a9a52021-11-19 00:24:45 +0000494}
495
Lei Jubc6ead02024-02-05 18:15:08 -0800496TEST_P(ContextHubTransactionTest, TestNanSessionStateChange) {
Anthony Stange7fba1002023-03-02 21:45:20 +0000497 NanSessionStateUpdate update;
498 update.state = true;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000499 Status status = mContextHub->onNanSessionStateChanged(update);
Rocky Fang78140982023-06-16 00:42:41 +0000500 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
501 status.transactionError() == android::UNKNOWN_TRANSACTION) {
502 GTEST_SKIP() << "Not supported -> old API; or not implemented";
503 } else {
504 ASSERT_TRUE(status.isOk());
505 update.state = false;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000506 ASSERT_TRUE(mContextHub->onNanSessionStateChanged(update).isOk());
Rocky Fang78140982023-06-16 00:42:41 +0000507 }
Anthony Stange7344af92022-12-22 14:21:31 +0000508}
509
Matthew Sedamadfd5572023-11-21 05:53:00 -0800510TEST_P(ContextHubAidl, TestSendMessageDeliveryStatusToHub) {
511 MessageDeliveryStatus messageDeliveryStatus;
512 messageDeliveryStatus.messageSequenceNumber = 123;
513 messageDeliveryStatus.errorCode = ErrorCode::OK;
514
Madhav Iyengaree25d072024-12-17 17:08:55 +0000515 Status status = mContextHub->sendMessageDeliveryStatusToHub(getHubId(), messageDeliveryStatus);
Matthew Sedamadfd5572023-11-21 05:53:00 -0800516 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
517 status.transactionError() == android::UNKNOWN_TRANSACTION) {
518 GTEST_SKIP() << "Not supported -> old API; or not implemented";
519 } else {
520 EXPECT_TRUE(status.isOk());
521 }
522}
523
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000524class TestEndpointCallback : public BnEndpointCallback {
525 public:
526 Status onEndpointStarted(const std::vector<EndpointInfo>& /* endpointInfos */) override {
527 return Status::ok();
528 }
529
530 Status onEndpointStopped(const std::vector<EndpointId>& /* endpointIds */,
531 Reason /* reason */) override {
532 return Status::ok();
533 }
534
535 Status onMessageReceived(int32_t /* sessionId */, const Message& message) override {
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800536 {
537 std::unique_lock<std::mutex> lock(mMutex);
538 mMessages.push_back(message);
539 }
540 mCondVar.notify_one();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000541 return Status::ok();
542 }
543
544 Status onMessageDeliveryStatusReceived(int32_t /* sessionId */,
545 const MessageDeliveryStatus& /* msgStatus */) override {
546 return Status::ok();
547 }
548
549 Status onEndpointSessionOpenRequest(
550 int32_t /* sessionId */, const EndpointId& /* destination */,
551 const EndpointId& /* initiator */,
552 const std::optional<String16>& /* serviceDescriptor */) override {
553 return Status::ok();
554 }
555
556 Status onCloseEndpointSession(int32_t /* sessionId */, Reason /* reason */) override {
557 return Status::ok();
558 }
559
560 Status onEndpointSessionOpenComplete(int32_t /* sessionId */) override {
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800561 {
562 std::unique_lock<std::mutex> lock(mMutex);
563 mWasOnEndpointSessionOpenCompleteCalled = true;
564 }
565 mCondVar.notify_one();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000566 return Status::ok();
567 }
568
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000569 bool wasOnEndpointSessionOpenCompleteCalled() {
570 return mWasOnEndpointSessionOpenCompleteCalled;
571 }
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800572
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000573 void resetWasOnEndpointSessionOpenCompleteCalled() {
574 mWasOnEndpointSessionOpenCompleteCalled = false;
575 }
576
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800577 std::mutex& getMutex() { return mMutex; }
578 std::condition_variable& getCondVar() { return mCondVar; }
579 std::vector<Message> getMessages() { return mMessages; }
580
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000581 private:
582 std::vector<Message> mMessages;
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800583 std::mutex mMutex;
584 std::condition_variable mCondVar;
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000585 bool mWasOnEndpointSessionOpenCompleteCalled = false;
586};
587
Madhav Iyengaree25d072024-12-17 17:08:55 +0000588TEST_P(ContextHubAidlWithTestMode, RegisterHub) {
589 if (!registerDefaultHub()) {
590 GTEST_SKIP() << "Not supported -> old API; or not implemented";
591 }
592
593 sp<IEndpointCommunication> hub2;
594 Status status = registerHub(kDefaultHubId + 1, &hub2);
595 EXPECT_TRUE(status.isOk());
596
597 sp<IEndpointCommunication> hub3;
598 status = registerHub(kDefaultHubId + 1, &hub3);
599 ASSERT_FALSE(status.isOk());
600 EXPECT_EQ(status.exceptionCode(), Status::EX_ILLEGAL_STATE);
601
602 hub2->unregister();
603 status = registerHub(kDefaultHubId + 1, &hub3);
604 EXPECT_TRUE(status.isOk());
605}
606
Matthew Sedam41db0932025-01-02 11:17:46 -0800607TEST_P(ContextHubAidlWithTestMode, RegisterEndpoint) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000608 if (!registerDefaultHub()) {
609 GTEST_SKIP() << "Not supported -> old API; or not implemented";
610 }
611
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000612 EndpointInfo endpointInfo;
613 endpointInfo.id.id = 1;
614 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
615 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
616 endpointInfo.name = String16("Test host endpoint 1");
617 endpointInfo.version = 42;
618
Madhav Iyengaree25d072024-12-17 17:08:55 +0000619 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000620 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
621 status.transactionError() == android::UNKNOWN_TRANSACTION) {
622 GTEST_SKIP() << "Not supported -> old API; or not implemented";
623 } else {
624 EXPECT_TRUE(status.isOk());
625 }
626}
627
Matthew Sedam41db0932025-01-02 11:17:46 -0800628TEST_P(ContextHubAidlWithTestMode, RegisterEndpointSameNameFailure) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000629 if (!registerDefaultHub()) {
630 GTEST_SKIP() << "Not supported -> old API; or not implemented";
631 }
632
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000633 EndpointInfo endpointInfo;
634 endpointInfo.id.id = 2;
635 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
636 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
637 endpointInfo.name = String16("Test host endpoint 2");
638 endpointInfo.version = 42;
639
640 EndpointInfo endpointInfo2;
641 endpointInfo2.id.id = 3;
642 endpointInfo2.id.hubId = 0xCAFECAFECAFECAFE;
643 endpointInfo2.type = EndpointInfo::EndpointType::NATIVE;
644 endpointInfo2.name = String16("Test host endpoint 2");
645 endpointInfo2.version = 42;
646
Madhav Iyengaree25d072024-12-17 17:08:55 +0000647 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000648 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
649 status.transactionError() == android::UNKNOWN_TRANSACTION) {
650 GTEST_SKIP() << "Not supported -> old API; or not implemented";
651 } else {
652 EXPECT_TRUE(status.isOk());
653 }
654
Madhav Iyengaree25d072024-12-17 17:08:55 +0000655 EXPECT_FALSE(mHubInterface->registerEndpoint(endpointInfo2).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000656}
657
Matthew Sedam41db0932025-01-02 11:17:46 -0800658TEST_P(ContextHubAidlWithTestMode, RegisterEndpointSameIdFailure) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000659 if (!registerDefaultHub()) {
660 GTEST_SKIP() << "Not supported -> old API; or not implemented";
661 }
662
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000663 EndpointInfo endpointInfo;
664 endpointInfo.id.id = 4;
665 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
666 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
667 endpointInfo.name = String16("Test host endpoint 4");
668 endpointInfo.version = 42;
669
670 EndpointInfo endpointInfo2;
671 endpointInfo2.id.id = 4;
672 endpointInfo2.id.hubId = 0xCAFECAFECAFECAFE;
673 endpointInfo2.type = EndpointInfo::EndpointType::NATIVE;
674 endpointInfo2.name = String16("Test host endpoint - same ID test");
675 endpointInfo2.version = 42;
676
Madhav Iyengaree25d072024-12-17 17:08:55 +0000677 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000678 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
679 status.transactionError() == android::UNKNOWN_TRANSACTION) {
680 GTEST_SKIP() << "Not supported -> old API; or not implemented";
681 } else {
682 EXPECT_TRUE(status.isOk());
683 }
684
Madhav Iyengaree25d072024-12-17 17:08:55 +0000685 EXPECT_FALSE(mHubInterface->registerEndpoint(endpointInfo2).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000686}
687
Matthew Sedam41db0932025-01-02 11:17:46 -0800688TEST_P(ContextHubAidlWithTestMode, UnregisterEndpoint) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000689 if (!registerDefaultHub()) {
690 GTEST_SKIP() << "Not supported -> old API; or not implemented";
691 }
692
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000693 EndpointInfo endpointInfo;
694 endpointInfo.id.id = 6;
695 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
696 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
697 endpointInfo.name = String16("Test host endpoint 6");
698 endpointInfo.version = 42;
699
Madhav Iyengaree25d072024-12-17 17:08:55 +0000700 Status status = mHubInterface->registerEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000701 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
702 status.transactionError() == android::UNKNOWN_TRANSACTION) {
703 GTEST_SKIP() << "Not supported -> old API; or not implemented";
704 } else {
705 EXPECT_TRUE(status.isOk());
706 }
707
Madhav Iyengaree25d072024-12-17 17:08:55 +0000708 EXPECT_TRUE(mHubInterface->unregisterEndpoint(endpointInfo).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000709}
710
Matthew Sedam41db0932025-01-02 11:17:46 -0800711TEST_P(ContextHubAidlWithTestMode, UnregisterEndpointNonexistent) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000712 if (!registerDefaultHub()) {
713 GTEST_SKIP() << "Not supported -> old API; or not implemented";
714 }
715
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000716 EndpointInfo endpointInfo;
717 endpointInfo.id.id = 100;
718 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
719 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
720 endpointInfo.name = String16("Test host endpoint 100");
721 endpointInfo.version = 42;
722
Madhav Iyengaree25d072024-12-17 17:08:55 +0000723 Status status = mHubInterface->unregisterEndpoint(endpointInfo);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000724 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
725 status.transactionError() == android::UNKNOWN_TRANSACTION) {
726 GTEST_SKIP() << "Not supported -> old API; or not implemented";
727 } else {
728 EXPECT_FALSE(status.isOk());
729 }
730}
731
Matthew Sedam41db0932025-01-02 11:17:46 -0800732TEST_P(ContextHubAidlWithTestMode, OpenEndpointSessionInvalidRange) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000733 if (!registerDefaultHub()) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000734 GTEST_SKIP() << "Not supported -> old API; or not implemented";
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000735 }
736
737 // Register the endpoint
738 EndpointInfo initiatorEndpoint;
739 initiatorEndpoint.id.id = 7;
740 initiatorEndpoint.id.hubId = 0xCAFECAFECAFECAFE;
741 initiatorEndpoint.type = EndpointInfo::EndpointType::NATIVE;
742 initiatorEndpoint.name = String16("Test host endpoint 7");
743 initiatorEndpoint.version = 42;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000744 EXPECT_TRUE(mHubInterface->registerEndpoint(initiatorEndpoint).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000745
746 // Find the destination, if it exists
747 std::vector<EndpointInfo> endpoints;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000748 EXPECT_TRUE(mContextHub->getEndpoints(&endpoints).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000749 const EndpointInfo* destinationEndpoint = nullptr;
750 for (const EndpointInfo& endpoint : endpoints) {
751 for (const Service& service : endpoint.services) {
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800752 if (service.serviceDescriptor == kEchoServiceName) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000753 destinationEndpoint = &endpoint;
754 break;
755 }
756 }
757 }
758 if (destinationEndpoint == nullptr) {
759 return; // no echo service endpoint -> just return
760 }
761
762 // Request the range
763 constexpr int32_t requestedRange = 100;
Yifei Zhangcef8f662024-12-06 16:03:26 -0800764 std::array<int32_t, 2> range;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000765 ASSERT_TRUE(mHubInterface->requestSessionIdRange(requestedRange, &range).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000766 EXPECT_EQ(range.size(), 2);
767 EXPECT_GE(range[1] - range[0] + 1, requestedRange);
768
769 // Open the session
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000770 int32_t sessionId = range[1] + 10; // invalid
Madhav Iyengaree25d072024-12-17 17:08:55 +0000771 EXPECT_FALSE(mHubInterface
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000772 ->openEndpointSession(sessionId, destinationEndpoint->id,
773 initiatorEndpoint.id,
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800774 /* in_serviceDescriptor= */ kEchoServiceName)
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000775 .isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000776}
777
Matthew Sedam41db0932025-01-02 11:17:46 -0800778TEST_P(ContextHubAidlWithTestMode, OpenEndpointSessionAndSendMessageEchoesBack) {
Madhav Iyengaree25d072024-12-17 17:08:55 +0000779 if (!registerDefaultHub()) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000780 GTEST_SKIP() << "Not supported -> old API; or not implemented";
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000781 }
Madhav Iyengaree25d072024-12-17 17:08:55 +0000782 std::unique_lock<std::mutex> lock(mEndpointCb->getMutex());
Matthew Sedam0ec290c2024-12-11 11:18:15 -0800783
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000784 // Register the endpoint
785 EndpointInfo initiatorEndpoint;
786 initiatorEndpoint.id.id = 8;
787 initiatorEndpoint.id.hubId = 0xCAFECAFECAFECAFE;
788 initiatorEndpoint.type = EndpointInfo::EndpointType::NATIVE;
789 initiatorEndpoint.name = String16("Test host endpoint 7");
790 initiatorEndpoint.version = 42;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000791 EXPECT_TRUE(mHubInterface->registerEndpoint(initiatorEndpoint).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000792
793 // Find the destination, if it exists
794 std::vector<EndpointInfo> endpoints;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000795 EXPECT_TRUE(mContextHub->getEndpoints(&endpoints).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000796 const EndpointInfo* destinationEndpoint = nullptr;
797 for (const EndpointInfo& endpoint : endpoints) {
798 for (const Service& service : endpoint.services) {
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800799 if (service.serviceDescriptor == kEchoServiceName) {
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000800 destinationEndpoint = &endpoint;
801 break;
802 }
803 }
804 }
805 if (destinationEndpoint == nullptr) {
806 return; // no echo service endpoint -> just return
807 }
808
809 // Request the range
810 constexpr int32_t requestedRange = 100;
Yifei Zhangcef8f662024-12-06 16:03:26 -0800811 std::array<int32_t, 2> range;
Madhav Iyengaree25d072024-12-17 17:08:55 +0000812 ASSERT_TRUE(mHubInterface->requestSessionIdRange(requestedRange, &range).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000813 EXPECT_EQ(range.size(), 2);
814 EXPECT_GE(range[1] - range[0] + 1, requestedRange);
815
816 // Open the session
Madhav Iyengaree25d072024-12-17 17:08:55 +0000817 mEndpointCb->resetWasOnEndpointSessionOpenCompleteCalled();
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000818 int32_t sessionId = range[0];
Madhav Iyengaree25d072024-12-17 17:08:55 +0000819 ASSERT_TRUE(mHubInterface
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000820 ->openEndpointSession(sessionId, destinationEndpoint->id,
821 initiatorEndpoint.id,
Arthur Ishiguro38712cf2024-12-13 11:40:47 -0800822 /* in_serviceDescriptor= */ kEchoServiceName)
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000823 .isOk());
Madhav Iyengaree25d072024-12-17 17:08:55 +0000824 mEndpointCb->getCondVar().wait(lock);
825 EXPECT_TRUE(mEndpointCb->wasOnEndpointSessionOpenCompleteCalled());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000826
827 // Send the message
828 Message message;
829 message.flags = 0;
830 message.sequenceNumber = 0;
831 message.content.push_back(42);
Madhav Iyengaree25d072024-12-17 17:08:55 +0000832 ASSERT_TRUE(mHubInterface->sendMessageToEndpoint(sessionId, message).isOk());
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000833
834 // Check for echo
Madhav Iyengaree25d072024-12-17 17:08:55 +0000835 mEndpointCb->getCondVar().wait(lock);
836 EXPECT_FALSE(mEndpointCb->getMessages().empty());
837 EXPECT_EQ(mEndpointCb->getMessages().back().content.back(), 42);
Matthew Sedam92c2bd82024-11-11 19:52:30 +0000838}
839
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700840std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
841 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
842}
843
844GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
845INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
846 PrintGeneratedTest);
847
Matthew Sedam41db0932025-01-02 11:17:46 -0800848GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidlWithTestMode);
849INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidlWithTestMode,
850 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
851
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700852GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
853INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
854 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
855
856int main(int argc, char** argv) {
857 ::testing::InitGoogleTest(&argc, argv);
Matthew Sedam41db0932025-01-02 11:17:46 -0800858 ProcessState::self()->setThreadPoolMaxThreadCount(2);
Arthur Ishiguro4e916c32021-08-12 12:47:03 -0700859 ProcessState::self()->startThreadPool();
860 return RUN_ALL_TESTS();
861}