1 /*
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/BnEndpointCallback.h>
24 #include <android/hardware/contexthub/IContextHub.h>
25 #include <android/hardware/contexthub/IContextHubCallback.h>
26 #include <android/hardware/contexthub/IEndpointCallback.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/ProcessState.h>
29 #include <log/log.h>
30
31 #include <cinttypes>
32 #include <future>
33
34 using ::android::ProcessState;
35 using ::android::sp;
36 using ::android::String16;
37 using ::android::binder::Status;
38 using ::android::hardware::contexthub::AsyncEventType;
39 using ::android::hardware::contexthub::BnEndpointCallback;
40 using ::android::hardware::contexthub::ContextHubInfo;
41 using ::android::hardware::contexthub::ContextHubMessage;
42 using ::android::hardware::contexthub::EndpointId;
43 using ::android::hardware::contexthub::EndpointInfo;
44 using ::android::hardware::contexthub::ErrorCode;
45 using ::android::hardware::contexthub::HostEndpointInfo;
46 using ::android::hardware::contexthub::HubInfo;
47 using ::android::hardware::contexthub::IContextHub;
48 using ::android::hardware::contexthub::IContextHubCallbackDefault;
49 using ::android::hardware::contexthub::Message;
50 using ::android::hardware::contexthub::MessageDeliveryStatus;
51 using ::android::hardware::contexthub::NanoappBinary;
52 using ::android::hardware::contexthub::NanoappInfo;
53 using ::android::hardware::contexthub::NanoappRpcService;
54 using ::android::hardware::contexthub::NanSessionRequest;
55 using ::android::hardware::contexthub::NanSessionStateUpdate;
56 using ::android::hardware::contexthub::Reason;
57 using ::android::hardware::contexthub::Service;
58 using ::android::hardware::contexthub::Setting;
59 using ::android::hardware::contexthub::vts_utils::kNonExistentAppId;
60 using ::android::hardware::contexthub::vts_utils::waitForCallback;
61
62 // 6612b522-b717-41c8-b48d-c0b1cc64e142
63 constexpr std::array<uint8_t, 16> kUuid = {0x66, 0x12, 0xb5, 0x22, 0xb7, 0x17, 0x41, 0xc8,
64 0xb4, 0x8d, 0xc0, 0xb1, 0xcc, 0x64, 0xe1, 0x42};
65 const String16 kName{"VtsAidlHalContextHubTargetTest"};
66
67 class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> {
68 public:
SetUp()69 virtual void SetUp() override {
70 contextHub = android::waitForDeclaredService<IContextHub>(
71 String16(std::get<0>(GetParam()).c_str()));
72 ASSERT_NE(contextHub, nullptr);
73
74 // Best effort enable test mode - this may not be supported on older HALS, so we
75 // ignore the return value.
76 contextHub->setTestMode(/* enable= */ true);
77 }
78
TearDown()79 virtual void TearDown() override { contextHub->setTestMode(/* enable= */ false); }
80
getHubId()81 uint32_t getHubId() { return std::get<1>(GetParam()); }
82
83 void testSettingChanged(Setting setting);
84
85 sp<IContextHub> contextHub;
86 };
87
TEST_P(ContextHubAidl,TestGetHubs)88 TEST_P(ContextHubAidl, TestGetHubs) {
89 std::vector<ContextHubInfo> hubs;
90 ASSERT_TRUE(contextHub->getContextHubs(&hubs).isOk());
91
92 ALOGD("System reports %zu hubs", hubs.size());
93
94 for (const ContextHubInfo& hub : hubs) {
95 ALOGD("Checking hub ID %" PRIu32, hub.id);
96
97 EXPECT_GT(hub.name.size(), 0);
98 EXPECT_GT(hub.vendor.size(), 0);
99 EXPECT_GT(hub.toolchain.size(), 0);
100 EXPECT_GT(hub.peakMips, 0);
101 EXPECT_GT(hub.chrePlatformId, 0);
102 EXPECT_GT(hub.chreApiMajorVersion, 0);
103 EXPECT_GE(hub.chreApiMinorVersion, 0);
104 EXPECT_GE(hub.chrePatchVersion, 0);
105
106 // Minimum 128 byte MTU as required by CHRE API v1.0
107 EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128));
108 }
109 }
110
TEST_P(ContextHubAidl,TestEnableTestMode)111 TEST_P(ContextHubAidl, TestEnableTestMode) {
112 Status status = contextHub->setTestMode(true);
113 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
114 status.transactionError() == android::UNKNOWN_TRANSACTION) {
115 GTEST_SKIP() << "Not supported -> old API; or not implemented";
116 } else {
117 ASSERT_TRUE(status.isOk());
118 }
119 }
120
TEST_P(ContextHubAidl,TestDisableTestMode)121 TEST_P(ContextHubAidl, TestDisableTestMode) {
122 Status status = contextHub->setTestMode(false);
123 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
124 status.transactionError() == android::UNKNOWN_TRANSACTION) {
125 GTEST_SKIP() << "Not supported -> old API; or not implemented";
126 } else {
127 ASSERT_TRUE(status.isOk());
128 }
129 }
130
131 class EmptyContextHubCallback : public android::hardware::contexthub::BnContextHubCallback {
132 public:
handleNanoappInfo(const std::vector<NanoappInfo> &)133 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
134 return Status::ok();
135 }
136
handleContextHubMessage(const ContextHubMessage &,const std::vector<String16> &)137 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
138 const std::vector<String16>& /* msgContentPerms */) override {
139 return Status::ok();
140 }
141
handleContextHubAsyncEvent(AsyncEventType)142 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
143
handleTransactionResult(int32_t,bool)144 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
145 return Status::ok();
146 }
147
handleNanSessionRequest(const NanSessionRequest &)148 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
149 return Status::ok();
150 }
151
handleMessageDeliveryStatus(char16_t,const MessageDeliveryStatus &)152 Status handleMessageDeliveryStatus(
153 char16_t /* hostEndPointId */,
154 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
155 return Status::ok();
156 }
157
getUuid(std::array<uint8_t,16> * out_uuid)158 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
159 *out_uuid = kUuid;
160 return Status::ok();
161 }
162
getName(::android::String16 * out_name)163 Status getName(::android::String16* out_name) override {
164 *out_name = kName;
165 return Status::ok();
166 }
167 };
168
TEST_P(ContextHubAidl,TestRegisterCallback)169 TEST_P(ContextHubAidl, TestRegisterCallback) {
170 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
171 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
172 }
173
174 // Helper callback that puts the async appInfo callback data into a promise
175 class QueryAppsCallback : public android::hardware::contexthub::BnContextHubCallback {
176 public:
handleNanoappInfo(const std::vector<NanoappInfo> & appInfo)177 Status handleNanoappInfo(const std::vector<NanoappInfo>& appInfo) override {
178 ALOGD("Got app info callback with %zu apps", appInfo.size());
179 promise.set_value(appInfo);
180 return Status::ok();
181 }
182
handleContextHubMessage(const ContextHubMessage &,const std::vector<String16> &)183 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
184 const std::vector<String16>& /* msgContentPerms */) override {
185 return Status::ok();
186 }
187
handleContextHubAsyncEvent(AsyncEventType)188 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
189
handleTransactionResult(int32_t,bool)190 Status handleTransactionResult(int32_t /* transactionId */, bool /* success */) override {
191 return Status::ok();
192 }
193
handleNanSessionRequest(const NanSessionRequest &)194 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
195 return Status::ok();
196 }
197
handleMessageDeliveryStatus(char16_t,const MessageDeliveryStatus &)198 Status handleMessageDeliveryStatus(
199 char16_t /* hostEndPointId */,
200 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
201 return Status::ok();
202 }
203
getUuid(std::array<uint8_t,16> * out_uuid)204 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
205 *out_uuid = kUuid;
206 return Status::ok();
207 }
208
getName(::android::String16 * out_name)209 Status getName(::android::String16* out_name) override {
210 *out_name = kName;
211 return Status::ok();
212 }
213
214 std::promise<std::vector<NanoappInfo>> promise;
215 };
216
217 // Calls queryApps() and checks the returned metadata
TEST_P(ContextHubAidl,TestQueryApps)218 TEST_P(ContextHubAidl, TestQueryApps) {
219 sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make();
220 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
221 ASSERT_TRUE(contextHub->queryNanoapps(getHubId()).isOk());
222
223 std::vector<NanoappInfo> appInfoList;
224 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &appInfoList));
225 for (const NanoappInfo& appInfo : appInfoList) {
226 EXPECT_NE(appInfo.nanoappId, UINT64_C(0));
227 EXPECT_NE(appInfo.nanoappId, kNonExistentAppId);
228
229 // Verify services are unique.
230 std::set<uint64_t> existingServiceIds;
231 for (const NanoappRpcService& rpcService : appInfo.rpcServices) {
232 EXPECT_NE(rpcService.id, UINT64_C(0));
233 EXPECT_EQ(existingServiceIds.count(rpcService.id), 0);
234 existingServiceIds.insert(rpcService.id);
235 }
236 }
237 }
238
239 // Calls getPreloadedNanoappsIds() and verifies there are preloaded nanoapps
TEST_P(ContextHubAidl,TestGetPreloadedNanoappIds)240 TEST_P(ContextHubAidl, TestGetPreloadedNanoappIds) {
241 std::vector<int64_t> preloadedNanoappIds;
242 Status status = contextHub->getPreloadedNanoappIds(getHubId(), &preloadedNanoappIds);
243 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
244 status.transactionError() == android::UNKNOWN_TRANSACTION) {
245 GTEST_SKIP() << "Not supported -> old API; or not implemented";
246 } else {
247 ASSERT_TRUE(status.isOk());
248 }
249 }
250
251 // Helper callback that puts the TransactionResult for the expectedTransactionId into a
252 // promise
253 class TransactionResultCallback : public android::hardware::contexthub::BnContextHubCallback {
254 public:
handleNanoappInfo(const std::vector<NanoappInfo> &)255 Status handleNanoappInfo(const std::vector<NanoappInfo>& /* appInfo */) override {
256 return Status::ok();
257 }
258
handleContextHubMessage(const ContextHubMessage &,const std::vector<String16> &)259 Status handleContextHubMessage(const ContextHubMessage& /* msg */,
260 const std::vector<String16>& /* msgContentPerms */) override {
261 return Status::ok();
262 }
263
handleContextHubAsyncEvent(AsyncEventType)264 Status handleContextHubAsyncEvent(AsyncEventType /* evt */) override { return Status::ok(); }
265
handleTransactionResult(int32_t transactionId,bool success)266 Status handleTransactionResult(int32_t transactionId, bool success) override {
267 ALOGD("Got transaction result callback for transactionId %" PRIu32 " (expecting %" PRIu32
268 ") with success %d",
269 transactionId, expectedTransactionId, success);
270 if (transactionId == expectedTransactionId) {
271 promise.set_value(success);
272 }
273 return Status::ok();
274 }
275
handleNanSessionRequest(const NanSessionRequest &)276 Status handleNanSessionRequest(const NanSessionRequest& /* request */) override {
277 return Status::ok();
278 }
279
handleMessageDeliveryStatus(char16_t,const MessageDeliveryStatus &)280 Status handleMessageDeliveryStatus(
281 char16_t /* hostEndPointId */,
282 const MessageDeliveryStatus& /* messageDeliveryStatus */) override {
283 return Status::ok();
284 }
285
getUuid(std::array<uint8_t,16> * out_uuid)286 Status getUuid(std::array<uint8_t, 16>* out_uuid) override {
287 *out_uuid = kUuid;
288 return Status::ok();
289 }
290
getName(::android::String16 * out_name)291 Status getName(::android::String16* out_name) override {
292 *out_name = kName;
293 return Status::ok();
294 }
295
296 uint32_t expectedTransactionId = 0;
297 std::promise<bool> promise;
298 };
299
300 // Parameterized fixture that sets the callback to TransactionResultCallback
301 class ContextHubTransactionTest : public ContextHubAidl {
302 public:
SetUp()303 virtual void SetUp() override {
304 ContextHubAidl::SetUp();
305 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
306 }
307
308 sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make();
309 };
310
TEST_P(ContextHubTransactionTest,TestSendMessageToNonExistentNanoapp)311 TEST_P(ContextHubTransactionTest, TestSendMessageToNonExistentNanoapp) {
312 ContextHubMessage message;
313 message.nanoappId = kNonExistentAppId;
314 message.messageType = 1;
315 message.messageBody.resize(4);
316 std::fill(message.messageBody.begin(), message.messageBody.end(), 0);
317
318 ALOGD("Sending message to non-existent nanoapp");
319 ASSERT_TRUE(contextHub->sendMessageToHub(getHubId(), message).isOk());
320 }
321
TEST_P(ContextHubTransactionTest,TestLoadEmptyNanoapp)322 TEST_P(ContextHubTransactionTest, TestLoadEmptyNanoapp) {
323 cb->expectedTransactionId = 0123;
324 NanoappBinary emptyApp;
325
326 emptyApp.nanoappId = kNonExistentAppId;
327 emptyApp.nanoappVersion = 1;
328 emptyApp.flags = 0;
329 emptyApp.targetChreApiMajorVersion = 1;
330 emptyApp.targetChreApiMinorVersion = 0;
331
332 ALOGD("Loading empty nanoapp");
333 bool success = contextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk();
334 if (success) {
335 bool transactionSuccess;
336 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
337 ASSERT_FALSE(transactionSuccess);
338 }
339 }
340
TEST_P(ContextHubTransactionTest,TestUnloadNonexistentNanoapp)341 TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) {
342 cb->expectedTransactionId = 1234;
343
344 ALOGD("Unloading nonexistent nanoapp");
345 bool success =
346 contextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
347 .isOk();
348 if (success) {
349 bool transactionSuccess;
350 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
351 ASSERT_FALSE(transactionSuccess);
352 }
353 }
354
TEST_P(ContextHubTransactionTest,TestEnableNonexistentNanoapp)355 TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) {
356 cb->expectedTransactionId = 2345;
357
358 ALOGD("Enabling nonexistent nanoapp");
359 bool success =
360 contextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
361 .isOk();
362 if (success) {
363 bool transactionSuccess;
364 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
365 ASSERT_FALSE(transactionSuccess);
366 }
367 }
368
TEST_P(ContextHubTransactionTest,TestDisableNonexistentNanoapp)369 TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) {
370 cb->expectedTransactionId = 3456;
371
372 ALOGD("Disabling nonexistent nanoapp");
373 bool success =
374 contextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId)
375 .isOk();
376 if (success) {
377 bool transactionSuccess;
378 ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess));
379 ASSERT_FALSE(transactionSuccess);
380 }
381 }
382
testSettingChanged(Setting setting)383 void ContextHubAidl::testSettingChanged(Setting setting) {
384 // In VTS, we only test that sending the values doesn't cause things to blow up - GTS tests
385 // verify the expected E2E behavior in CHRE
386 sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make();
387 ASSERT_TRUE(contextHub->registerCallback(getHubId(), cb).isOk());
388
389 ASSERT_TRUE(contextHub->onSettingChanged(setting, true /* enabled */).isOk());
390 ASSERT_TRUE(contextHub->onSettingChanged(setting, false /* enabled */).isOk());
391 }
392
TEST_P(ContextHubAidl,TestOnLocationSettingChanged)393 TEST_P(ContextHubAidl, TestOnLocationSettingChanged) {
394 testSettingChanged(Setting::LOCATION);
395 }
396
TEST_P(ContextHubAidl,TestOnWifiMainSettingChanged)397 TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) {
398 testSettingChanged(Setting::WIFI_MAIN);
399 }
400
TEST_P(ContextHubAidl,TestOnWifiScanningSettingChanged)401 TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) {
402 testSettingChanged(Setting::WIFI_SCANNING);
403 }
404
TEST_P(ContextHubAidl,TestOnAirplaneModeSettingChanged)405 TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) {
406 testSettingChanged(Setting::AIRPLANE_MODE);
407 }
408
TEST_P(ContextHubAidl,TestOnMicrophoneSettingChanged)409 TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) {
410 testSettingChanged(Setting::MICROPHONE);
411 }
412
TEST_P(ContextHubAidl,TestOnBtMainSettingChanged)413 TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) {
414 testSettingChanged(Setting::BT_MAIN);
415 }
416
TEST_P(ContextHubAidl,TestOnBtScanningSettingChanged)417 TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) {
418 testSettingChanged(Setting::BT_SCANNING);
419 }
420
generateContextHubMapping()421 std::vector<std::tuple<std::string, int32_t>> generateContextHubMapping() {
422 std::vector<std::tuple<std::string, int32_t>> tuples;
423 auto contextHubAidlNames = android::getAidlHalInstanceNames(IContextHub::descriptor);
424 std::vector<ContextHubInfo> contextHubInfos;
425
426 for (int i = 0; i < contextHubAidlNames.size(); i++) {
427 auto contextHubName = contextHubAidlNames[i].c_str();
428 auto contextHub = android::waitForDeclaredService<IContextHub>(String16(contextHubName));
429 if (contextHub->getContextHubs(&contextHubInfos).isOk()) {
430 for (auto& info : contextHubInfos) {
431 tuples.push_back(std::make_tuple(contextHubName, info.id));
432 }
433 }
434 }
435
436 return tuples;
437 }
438
TEST_P(ContextHubTransactionTest,TestHostConnection)439 TEST_P(ContextHubTransactionTest, TestHostConnection) {
440 constexpr char16_t kHostEndpointId = 1;
441 HostEndpointInfo hostEndpointInfo;
442 hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE;
443 hostEndpointInfo.hostEndpointId = kHostEndpointId;
444
445 ASSERT_TRUE(contextHub->onHostEndpointConnected(hostEndpointInfo).isOk());
446 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
447 }
448
TEST_P(ContextHubTransactionTest,TestInvalidHostConnection)449 TEST_P(ContextHubTransactionTest, TestInvalidHostConnection) {
450 constexpr char16_t kHostEndpointId = 1;
451
452 ASSERT_TRUE(contextHub->onHostEndpointDisconnected(kHostEndpointId).isOk());
453 }
454
TEST_P(ContextHubTransactionTest,TestNanSessionStateChange)455 TEST_P(ContextHubTransactionTest, TestNanSessionStateChange) {
456 NanSessionStateUpdate update;
457 update.state = true;
458 Status status = contextHub->onNanSessionStateChanged(update);
459 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
460 status.transactionError() == android::UNKNOWN_TRANSACTION) {
461 GTEST_SKIP() << "Not supported -> old API; or not implemented";
462 } else {
463 ASSERT_TRUE(status.isOk());
464 update.state = false;
465 ASSERT_TRUE(contextHub->onNanSessionStateChanged(update).isOk());
466 }
467 }
468
TEST_P(ContextHubAidl,TestSendMessageDeliveryStatusToHub)469 TEST_P(ContextHubAidl, TestSendMessageDeliveryStatusToHub) {
470 MessageDeliveryStatus messageDeliveryStatus;
471 messageDeliveryStatus.messageSequenceNumber = 123;
472 messageDeliveryStatus.errorCode = ErrorCode::OK;
473
474 Status status = contextHub->sendMessageDeliveryStatusToHub(getHubId(), messageDeliveryStatus);
475 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
476 status.transactionError() == android::UNKNOWN_TRANSACTION) {
477 GTEST_SKIP() << "Not supported -> old API; or not implemented";
478 } else {
479 EXPECT_TRUE(status.isOk());
480 }
481 }
482
483 class TestEndpointCallback : public BnEndpointCallback {
484 public:
onEndpointStarted(const std::vector<EndpointInfo> &)485 Status onEndpointStarted(const std::vector<EndpointInfo>& /* endpointInfos */) override {
486 return Status::ok();
487 }
488
onEndpointStopped(const std::vector<EndpointId> &,Reason)489 Status onEndpointStopped(const std::vector<EndpointId>& /* endpointIds */,
490 Reason /* reason */) override {
491 return Status::ok();
492 }
493
onMessageReceived(int32_t,const Message & message)494 Status onMessageReceived(int32_t /* sessionId */, const Message& message) override {
495 mMessages.push_back(message);
496 return Status::ok();
497 }
498
onMessageDeliveryStatusReceived(int32_t,const MessageDeliveryStatus &)499 Status onMessageDeliveryStatusReceived(int32_t /* sessionId */,
500 const MessageDeliveryStatus& /* msgStatus */) override {
501 return Status::ok();
502 }
503
onEndpointSessionOpenRequest(int32_t,const EndpointId &,const EndpointId &,const std::optional<String16> &)504 Status onEndpointSessionOpenRequest(
505 int32_t /* sessionId */, const EndpointId& /* destination */,
506 const EndpointId& /* initiator */,
507 const std::optional<String16>& /* serviceDescriptor */) override {
508 return Status::ok();
509 }
510
onCloseEndpointSession(int32_t,Reason)511 Status onCloseEndpointSession(int32_t /* sessionId */, Reason /* reason */) override {
512 return Status::ok();
513 }
514
onEndpointSessionOpenComplete(int32_t)515 Status onEndpointSessionOpenComplete(int32_t /* sessionId */) override {
516 mWasOnEndpointSessionOpenCompleteCalled = true;
517 return Status::ok();
518 }
519
getMessages()520 std::vector<Message> getMessages() { return mMessages; }
521
wasOnEndpointSessionOpenCompleteCalled()522 bool wasOnEndpointSessionOpenCompleteCalled() {
523 return mWasOnEndpointSessionOpenCompleteCalled;
524 }
resetWasOnEndpointSessionOpenCompleteCalled()525 void resetWasOnEndpointSessionOpenCompleteCalled() {
526 mWasOnEndpointSessionOpenCompleteCalled = false;
527 }
528
529 private:
530 std::vector<Message> mMessages;
531 bool mWasOnEndpointSessionOpenCompleteCalled = false;
532 };
533
TEST_P(ContextHubAidl,RegisterEndpoint)534 TEST_P(ContextHubAidl, RegisterEndpoint) {
535 EndpointInfo endpointInfo;
536 endpointInfo.id.id = 1;
537 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
538 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
539 endpointInfo.name = String16("Test host endpoint 1");
540 endpointInfo.version = 42;
541
542 Status status = contextHub->registerEndpoint(endpointInfo);
543 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
544 status.transactionError() == android::UNKNOWN_TRANSACTION) {
545 GTEST_SKIP() << "Not supported -> old API; or not implemented";
546 } else {
547 EXPECT_TRUE(status.isOk());
548 }
549 }
550
TEST_P(ContextHubAidl,RegisterEndpointSameNameFailure)551 TEST_P(ContextHubAidl, RegisterEndpointSameNameFailure) {
552 EndpointInfo endpointInfo;
553 endpointInfo.id.id = 2;
554 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
555 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
556 endpointInfo.name = String16("Test host endpoint 2");
557 endpointInfo.version = 42;
558
559 EndpointInfo endpointInfo2;
560 endpointInfo2.id.id = 3;
561 endpointInfo2.id.hubId = 0xCAFECAFECAFECAFE;
562 endpointInfo2.type = EndpointInfo::EndpointType::NATIVE;
563 endpointInfo2.name = String16("Test host endpoint 2");
564 endpointInfo2.version = 42;
565
566 Status status = contextHub->registerEndpoint(endpointInfo);
567 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
568 status.transactionError() == android::UNKNOWN_TRANSACTION) {
569 GTEST_SKIP() << "Not supported -> old API; or not implemented";
570 } else {
571 EXPECT_TRUE(status.isOk());
572 }
573
574 EXPECT_FALSE(contextHub->registerEndpoint(endpointInfo2).isOk());
575 }
576
TEST_P(ContextHubAidl,RegisterEndpointSameIdFailure)577 TEST_P(ContextHubAidl, RegisterEndpointSameIdFailure) {
578 EndpointInfo endpointInfo;
579 endpointInfo.id.id = 4;
580 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
581 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
582 endpointInfo.name = String16("Test host endpoint 4");
583 endpointInfo.version = 42;
584
585 EndpointInfo endpointInfo2;
586 endpointInfo2.id.id = 4;
587 endpointInfo2.id.hubId = 0xCAFECAFECAFECAFE;
588 endpointInfo2.type = EndpointInfo::EndpointType::NATIVE;
589 endpointInfo2.name = String16("Test host endpoint - same ID test");
590 endpointInfo2.version = 42;
591
592 Status status = contextHub->registerEndpoint(endpointInfo);
593 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
594 status.transactionError() == android::UNKNOWN_TRANSACTION) {
595 GTEST_SKIP() << "Not supported -> old API; or not implemented";
596 } else {
597 EXPECT_TRUE(status.isOk());
598 }
599
600 EXPECT_FALSE(contextHub->registerEndpoint(endpointInfo2).isOk());
601 }
602
TEST_P(ContextHubAidl,UnregisterEndpoint)603 TEST_P(ContextHubAidl, UnregisterEndpoint) {
604 EndpointInfo endpointInfo;
605 endpointInfo.id.id = 6;
606 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
607 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
608 endpointInfo.name = String16("Test host endpoint 6");
609 endpointInfo.version = 42;
610
611 Status status = contextHub->registerEndpoint(endpointInfo);
612 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
613 status.transactionError() == android::UNKNOWN_TRANSACTION) {
614 GTEST_SKIP() << "Not supported -> old API; or not implemented";
615 } else {
616 EXPECT_TRUE(status.isOk());
617 }
618
619 EXPECT_TRUE(contextHub->unregisterEndpoint(endpointInfo).isOk());
620 }
621
TEST_P(ContextHubAidl,UnregisterEndpointNonexistent)622 TEST_P(ContextHubAidl, UnregisterEndpointNonexistent) {
623 EndpointInfo endpointInfo;
624 endpointInfo.id.id = 100;
625 endpointInfo.id.hubId = 0xCAFECAFECAFECAFE;
626 endpointInfo.type = EndpointInfo::EndpointType::NATIVE;
627 endpointInfo.name = String16("Test host endpoint 100");
628 endpointInfo.version = 42;
629
630 Status status = contextHub->unregisterEndpoint(endpointInfo);
631 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_FALSE(status.isOk());
636 }
637 }
638
TEST_P(ContextHubAidl,RegisterCallback)639 TEST_P(ContextHubAidl, RegisterCallback) {
640 auto cb = sp<TestEndpointCallback>::make();
641 Status status = contextHub->registerEndpointCallback(cb);
642 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
643 status.transactionError() == android::UNKNOWN_TRANSACTION) {
644 GTEST_SKIP() << "Not supported -> old API; or not implemented";
645 } else {
646 EXPECT_TRUE(status.isOk());
647 }
648 }
649
TEST_P(ContextHubAidl,OpenEndpointSessionInvalidRange)650 TEST_P(ContextHubAidl, OpenEndpointSessionInvalidRange) {
651 auto cb = sp<TestEndpointCallback>::make();
652 Status status = contextHub->registerEndpointCallback(cb);
653 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
654 status.transactionError() == android::UNKNOWN_TRANSACTION) {
655 GTEST_SKIP() << "Not supported -> old API; or not implemented";
656 } else {
657 EXPECT_TRUE(status.isOk());
658 }
659
660 // Register the endpoint
661 EndpointInfo initiatorEndpoint;
662 initiatorEndpoint.id.id = 7;
663 initiatorEndpoint.id.hubId = 0xCAFECAFECAFECAFE;
664 initiatorEndpoint.type = EndpointInfo::EndpointType::NATIVE;
665 initiatorEndpoint.name = String16("Test host endpoint 7");
666 initiatorEndpoint.version = 42;
667 EXPECT_TRUE(contextHub->registerEndpoint(initiatorEndpoint).isOk());
668
669 // Find the destination, if it exists
670 std::vector<EndpointInfo> endpoints;
671 EXPECT_TRUE(contextHub->getEndpoints(&endpoints).isOk());
672 const EndpointInfo* destinationEndpoint = nullptr;
673 for (const EndpointInfo& endpoint : endpoints) {
674 for (const Service& service : endpoint.services) {
675 if (service.serviceDescriptor == String16("ECHO")) {
676 destinationEndpoint = &endpoint;
677 break;
678 }
679 }
680 }
681 if (destinationEndpoint == nullptr) {
682 return; // no echo service endpoint -> just return
683 }
684
685 // Request the range
686 constexpr int32_t requestedRange = 100;
687 std::vector<int32_t> range;
688 ASSERT_TRUE(contextHub->requestSessionIdRange(requestedRange, &range).isOk());
689 EXPECT_EQ(range.size(), 2);
690 EXPECT_GE(range[1] - range[0] + 1, requestedRange);
691
692 // Open the session
693 cb->resetWasOnEndpointSessionOpenCompleteCalled();
694 int32_t sessionId = range[1] + 10; // invalid
695 EXPECT_FALSE(contextHub
696 ->openEndpointSession(sessionId, destinationEndpoint->id,
697 initiatorEndpoint.id,
698 /* in_serviceDescriptor= */ String16("ECHO"))
699 .isOk());
700 EXPECT_FALSE(cb->wasOnEndpointSessionOpenCompleteCalled());
701 }
702
TEST_P(ContextHubAidl,OpenEndpointSessionAndSendMessageEchoesBack)703 TEST_P(ContextHubAidl, OpenEndpointSessionAndSendMessageEchoesBack) {
704 auto cb = sp<TestEndpointCallback>::make();
705 Status status = contextHub->registerEndpointCallback(cb);
706 if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
707 status.transactionError() == android::UNKNOWN_TRANSACTION) {
708 GTEST_SKIP() << "Not supported -> old API; or not implemented";
709 } else {
710 EXPECT_TRUE(status.isOk());
711 }
712
713 // Register the endpoint
714 EndpointInfo initiatorEndpoint;
715 initiatorEndpoint.id.id = 8;
716 initiatorEndpoint.id.hubId = 0xCAFECAFECAFECAFE;
717 initiatorEndpoint.type = EndpointInfo::EndpointType::NATIVE;
718 initiatorEndpoint.name = String16("Test host endpoint 7");
719 initiatorEndpoint.version = 42;
720 EXPECT_TRUE(contextHub->registerEndpoint(initiatorEndpoint).isOk());
721
722 // Find the destination, if it exists
723 std::vector<EndpointInfo> endpoints;
724 EXPECT_TRUE(contextHub->getEndpoints(&endpoints).isOk());
725 const EndpointInfo* destinationEndpoint = nullptr;
726 for (const EndpointInfo& endpoint : endpoints) {
727 for (const Service& service : endpoint.services) {
728 if (service.serviceDescriptor == String16("ECHO")) {
729 destinationEndpoint = &endpoint;
730 break;
731 }
732 }
733 }
734 if (destinationEndpoint == nullptr) {
735 return; // no echo service endpoint -> just return
736 }
737
738 // Request the range
739 constexpr int32_t requestedRange = 100;
740 std::vector<int32_t> range;
741 ASSERT_TRUE(contextHub->requestSessionIdRange(requestedRange, &range).isOk());
742 EXPECT_EQ(range.size(), 2);
743 EXPECT_GE(range[1] - range[0] + 1, requestedRange);
744
745 // Open the session
746 cb->resetWasOnEndpointSessionOpenCompleteCalled();
747 int32_t sessionId = range[0];
748 ASSERT_TRUE(contextHub
749 ->openEndpointSession(sessionId, destinationEndpoint->id,
750 initiatorEndpoint.id,
751 /* in_serviceDescriptor= */ String16("ECHO"))
752 .isOk());
753 EXPECT_TRUE(cb->wasOnEndpointSessionOpenCompleteCalled());
754
755 // Send the message
756 Message message;
757 message.flags = 0;
758 message.sequenceNumber = 0;
759 message.content.push_back(42);
760 ASSERT_TRUE(contextHub->sendMessageToEndpoint(sessionId, message).isOk());
761
762 // Check for echo
763 EXPECT_FALSE(cb->getMessages().empty());
764 EXPECT_EQ(cb->getMessages().back().content.back(), 42);
765 }
766
PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType> & info)767 std::string PrintGeneratedTest(const testing::TestParamInfo<ContextHubAidl::ParamType>& info) {
768 return std::string("CONTEXT_HUB_ID_") + std::to_string(std::get<1>(info.param));
769 }
770
771 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl);
772 INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()),
773 PrintGeneratedTest);
774
775 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest);
776 INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest,
777 testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest);
778
main(int argc,char ** argv)779 int main(int argc, char** argv) {
780 ::testing::InitGoogleTest(&argc, argv);
781 ProcessState::self()->setThreadPoolMaxThreadCount(1);
782 ProcessState::self()->startThreadPool();
783 return RUN_ALL_TESTS();
784 }
785