Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 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> |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 23 | #include <android/hardware/contexthub/BnEndpointCallback.h> |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 24 | #include <android/hardware/contexthub/IContextHub.h> |
| 25 | #include <android/hardware/contexthub/IContextHubCallback.h> |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 26 | #include <android/hardware/contexthub/IEndpointCallback.h> |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 27 | #include <android/hardware/contexthub/IEndpointCommunication.h> |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/ProcessState.h> |
| 30 | #include <log/log.h> |
| 31 | |
| 32 | #include <cinttypes> |
| 33 | #include <future> |
| 34 | |
| 35 | using ::android::ProcessState; |
| 36 | using ::android::sp; |
| 37 | using ::android::String16; |
| 38 | using ::android::binder::Status; |
| 39 | using ::android::hardware::contexthub::AsyncEventType; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 40 | using ::android::hardware::contexthub::BnEndpointCallback; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 41 | using ::android::hardware::contexthub::ContextHubInfo; |
| 42 | using ::android::hardware::contexthub::ContextHubMessage; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 43 | using ::android::hardware::contexthub::EndpointId; |
| 44 | using ::android::hardware::contexthub::EndpointInfo; |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 45 | using ::android::hardware::contexthub::ErrorCode; |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 46 | using ::android::hardware::contexthub::HostEndpointInfo; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 47 | using ::android::hardware::contexthub::HubInfo; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 48 | using ::android::hardware::contexthub::IContextHub; |
| 49 | using ::android::hardware::contexthub::IContextHubCallbackDefault; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 50 | using ::android::hardware::contexthub::IEndpointCommunication; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 51 | using ::android::hardware::contexthub::Message; |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 52 | using ::android::hardware::contexthub::MessageDeliveryStatus; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 53 | using ::android::hardware::contexthub::NanoappBinary; |
| 54 | using ::android::hardware::contexthub::NanoappInfo; |
Arthur Ishiguro | 0810307 | 2021-12-09 18:30:49 +0000 | [diff] [blame] | 55 | using ::android::hardware::contexthub::NanoappRpcService; |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 56 | using ::android::hardware::contexthub::NanSessionRequest; |
| 57 | using ::android::hardware::contexthub::NanSessionStateUpdate; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 58 | using ::android::hardware::contexthub::Reason; |
| 59 | using ::android::hardware::contexthub::Service; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 60 | using ::android::hardware::contexthub::Setting; |
| 61 | using ::android::hardware::contexthub::vts_utils::kNonExistentAppId; |
| 62 | using ::android::hardware::contexthub::vts_utils::waitForCallback; |
| 63 | |
Lei Ju | 8787afa | 2023-06-28 09:36:19 -0700 | [diff] [blame] | 64 | // 6612b522-b717-41c8-b48d-c0b1cc64e142 |
Lei Ju | 5b2ded4 | 2023-12-05 15:08:21 -0800 | [diff] [blame] | 65 | constexpr std::array<uint8_t, 16> kUuid = {0x66, 0x12, 0xb5, 0x22, 0xb7, 0x17, 0x41, 0xc8, |
| 66 | 0xb4, 0x8d, 0xc0, 0xb1, 0xcc, 0x64, 0xe1, 0x42}; |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 67 | |
Lei Ju | 5b2ded4 | 2023-12-05 15:08:21 -0800 | [diff] [blame] | 68 | const String16 kName{"VtsAidlHalContextHubTargetTest"}; |
Lei Ju | 8787afa | 2023-06-28 09:36:19 -0700 | [diff] [blame] | 69 | |
Arthur Ishiguro | 38712cf | 2024-12-13 11:40:47 -0800 | [diff] [blame] | 70 | const String16 kEchoServiceName{"android.hardware.contexthub.test.EchoService"}; |
| 71 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 72 | constexpr int64_t kDefaultHubId = 1; |
| 73 | |
| 74 | class TestEndpointCallback; |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 75 | class ContextHubAidl : public testing::TestWithParam<std::tuple<std::string, int32_t>> { |
| 76 | public: |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 77 | void SetUp() override { |
| 78 | mContextHub = android::waitForDeclaredService<IContextHub>( |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 79 | String16(std::get<0>(GetParam()).c_str())); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 80 | ASSERT_NE(mContextHub, nullptr); |
| 81 | mEndpointCb = sp<TestEndpointCallback>::make(); |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | uint32_t getHubId() { return std::get<1>(GetParam()); } |
| 85 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 86 | 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 Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 105 | |
| 106 | void testSettingChanged(Setting setting); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 107 | |
| 108 | sp<IContextHub> mContextHub; |
| 109 | sp<TestEndpointCallback> mEndpointCb; |
| 110 | sp<IEndpointCommunication> mHubInterface; |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | class ContextHubAidlWithTestMode : public ContextHubAidl { |
| 114 | public: |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 115 | void SetUp() override { |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 116 | ContextHubAidl::SetUp(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 117 | |
| 118 | // Best effort enable test mode - this may not be supported on older HALS, so we |
| 119 | // ignore the return value. |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 120 | mContextHub->setTestMode(/* enable= */ true); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 123 | void TearDown() override { |
| 124 | mContextHub->setTestMode(/* enable= */ false); |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 125 | ContextHubAidl::TearDown(); |
| 126 | } |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | TEST_P(ContextHubAidl, TestGetHubs) { |
| 130 | std::vector<ContextHubInfo> hubs; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 131 | ASSERT_TRUE(mContextHub->getContextHubs(&hubs).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 132 | |
| 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 Ishiguro | 6471f61 | 2021-10-28 21:59:55 +0000 | [diff] [blame] | 144 | EXPECT_GE(hub.chreApiMinorVersion, 0); |
| 145 | EXPECT_GE(hub.chrePatchVersion, 0); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 146 | |
| 147 | // Minimum 128 byte MTU as required by CHRE API v1.0 |
| 148 | EXPECT_GE(hub.maxSupportedMessageLengthBytes, UINT32_C(128)); |
| 149 | } |
| 150 | } |
| 151 | |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 152 | TEST_P(ContextHubAidl, TestEnableTestMode) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 153 | Status status = mContextHub->setTestMode(true); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 154 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 155 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
Matthew Sedam | 121b6d6 | 2023-01-19 19:04:53 +0000 | [diff] [blame] | 156 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 157 | } else { |
| 158 | ASSERT_TRUE(status.isOk()); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 159 | } |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST_P(ContextHubAidl, TestDisableTestMode) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 163 | Status status = mContextHub->setTestMode(false); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 164 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 165 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
Matthew Sedam | 121b6d6 | 2023-01-19 19:04:53 +0000 | [diff] [blame] | 166 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 167 | } else { |
| 168 | ASSERT_TRUE(status.isOk()); |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 169 | } |
Matthew Sedam | c8ce4d5 | 2023-01-09 20:18:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 172 | class 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 Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 188 | |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 189 | Status handleNanSessionRequest(const NanSessionRequest& /* request */) override { |
| 190 | return Status::ok(); |
| 191 | } |
Lei Ju | 8787afa | 2023-06-28 09:36:19 -0700 | [diff] [blame] | 192 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 193 | Status handleMessageDeliveryStatus( |
| 194 | char16_t /* hostEndPointId */, |
| 195 | const MessageDeliveryStatus& /* messageDeliveryStatus */) override { |
| 196 | return Status::ok(); |
| 197 | } |
| 198 | |
Lei Ju | 8787afa | 2023-06-28 09:36:19 -0700 | [diff] [blame] | 199 | Status getUuid(std::array<uint8_t, 16>* out_uuid) override { |
| 200 | *out_uuid = kUuid; |
| 201 | return Status::ok(); |
| 202 | } |
Lei Ju | 5b2ded4 | 2023-12-05 15:08:21 -0800 | [diff] [blame] | 203 | |
| 204 | Status getName(::android::String16* out_name) override { |
| 205 | *out_name = kName; |
| 206 | return Status::ok(); |
| 207 | } |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 210 | TEST_P(ContextHubAidl, TestRegisterCallback) { |
Arthur Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 211 | sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make(); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 212 | ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 215 | // Helper callback that puts the async appInfo callback data into a promise |
| 216 | class 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 Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 235 | Status handleNanSessionRequest(const NanSessionRequest& /* request */) override { |
| 236 | return Status::ok(); |
| 237 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 238 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 239 | Status handleMessageDeliveryStatus( |
| 240 | char16_t /* hostEndPointId */, |
| 241 | const MessageDeliveryStatus& /* messageDeliveryStatus */) override { |
| 242 | return Status::ok(); |
| 243 | } |
| 244 | |
Lei Ju | 8787afa | 2023-06-28 09:36:19 -0700 | [diff] [blame] | 245 | Status getUuid(std::array<uint8_t, 16>* out_uuid) override { |
| 246 | *out_uuid = kUuid; |
| 247 | return Status::ok(); |
| 248 | } |
| 249 | |
Lei Ju | 5b2ded4 | 2023-12-05 15:08:21 -0800 | [diff] [blame] | 250 | Status getName(::android::String16* out_name) override { |
| 251 | *out_name = kName; |
| 252 | return Status::ok(); |
| 253 | } |
| 254 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 255 | std::promise<std::vector<NanoappInfo>> promise; |
| 256 | }; |
| 257 | |
| 258 | // Calls queryApps() and checks the returned metadata |
| 259 | TEST_P(ContextHubAidl, TestQueryApps) { |
| 260 | sp<QueryAppsCallback> cb = sp<QueryAppsCallback>::make(); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 261 | ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk()); |
| 262 | ASSERT_TRUE(mContextHub->queryNanoapps(getHubId()).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 263 | |
| 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 Ishiguro | 0810307 | 2021-12-09 18:30:49 +0000 | [diff] [blame] | 269 | |
| 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 Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Matthew Sedam | d70f84d | 2023-03-06 18:34:24 +0000 | [diff] [blame] | 280 | // Calls getPreloadedNanoappsIds() and verifies there are preloaded nanoapps |
| 281 | TEST_P(ContextHubAidl, TestGetPreloadedNanoappIds) { |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 282 | std::vector<int64_t> preloadedNanoappIds; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 283 | Status status = mContextHub->getPreloadedNanoappIds(getHubId(), &preloadedNanoappIds); |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 284 | if (status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION || |
| 285 | status.transactionError() == android::UNKNOWN_TRANSACTION) { |
Matthew Sedam | 121b6d6 | 2023-01-19 19:04:53 +0000 | [diff] [blame] | 286 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 287 | } else { |
| 288 | ASSERT_TRUE(status.isOk()); |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 289 | } |
Arthur Ishiguro | fd5e65c | 2022-11-08 16:49:47 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 292 | // Helper callback that puts the TransactionResult for the expectedTransactionId into a |
| 293 | // promise |
| 294 | class 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 Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 317 | Status handleNanSessionRequest(const NanSessionRequest& /* request */) override { |
| 318 | return Status::ok(); |
| 319 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 320 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 321 | Status handleMessageDeliveryStatus( |
| 322 | char16_t /* hostEndPointId */, |
| 323 | const MessageDeliveryStatus& /* messageDeliveryStatus */) override { |
| 324 | return Status::ok(); |
| 325 | } |
| 326 | |
Lei Ju | 8787afa | 2023-06-28 09:36:19 -0700 | [diff] [blame] | 327 | Status getUuid(std::array<uint8_t, 16>* out_uuid) override { |
| 328 | *out_uuid = kUuid; |
| 329 | return Status::ok(); |
| 330 | } |
| 331 | |
Lei Ju | 5b2ded4 | 2023-12-05 15:08:21 -0800 | [diff] [blame] | 332 | Status getName(::android::String16* out_name) override { |
| 333 | *out_name = kName; |
| 334 | return Status::ok(); |
| 335 | } |
| 336 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 337 | uint32_t expectedTransactionId = 0; |
| 338 | std::promise<bool> promise; |
| 339 | }; |
| 340 | |
| 341 | // Parameterized fixture that sets the callback to TransactionResultCallback |
| 342 | class ContextHubTransactionTest : public ContextHubAidl { |
| 343 | public: |
| 344 | virtual void SetUp() override { |
| 345 | ContextHubAidl::SetUp(); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 346 | ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | sp<TransactionResultCallback> cb = sp<TransactionResultCallback>::make(); |
| 350 | }; |
| 351 | |
| 352 | TEST_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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 360 | ASSERT_TRUE(mContextHub->sendMessageToHub(getHubId(), message).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST_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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 374 | bool success = mContextHub->loadNanoapp(getHubId(), emptyApp, cb->expectedTransactionId).isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 375 | if (success) { |
| 376 | bool transactionSuccess; |
| 377 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 378 | ASSERT_FALSE(transactionSuccess); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | TEST_P(ContextHubTransactionTest, TestUnloadNonexistentNanoapp) { |
| 383 | cb->expectedTransactionId = 1234; |
| 384 | |
| 385 | ALOGD("Unloading nonexistent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 386 | bool success = |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 387 | mContextHub->unloadNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId) |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 388 | .isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 389 | if (success) { |
| 390 | bool transactionSuccess; |
| 391 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 392 | ASSERT_FALSE(transactionSuccess); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | TEST_P(ContextHubTransactionTest, TestEnableNonexistentNanoapp) { |
| 397 | cb->expectedTransactionId = 2345; |
| 398 | |
| 399 | ALOGD("Enabling nonexistent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 400 | bool success = |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 401 | mContextHub->enableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId) |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 402 | .isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 403 | if (success) { |
| 404 | bool transactionSuccess; |
| 405 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 406 | ASSERT_FALSE(transactionSuccess); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | TEST_P(ContextHubTransactionTest, TestDisableNonexistentNanoapp) { |
| 411 | cb->expectedTransactionId = 3456; |
| 412 | |
| 413 | ALOGD("Disabling nonexistent nanoapp"); |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 414 | bool success = |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 415 | mContextHub->disableNanoapp(getHubId(), kNonExistentAppId, cb->expectedTransactionId) |
Arthur Ishiguro | 070f47d | 2022-01-06 22:42:10 +0000 | [diff] [blame] | 416 | .isOk(); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 417 | if (success) { |
| 418 | bool transactionSuccess; |
| 419 | ASSERT_TRUE(waitForCallback(cb->promise.get_future(), &transactionSuccess)); |
| 420 | ASSERT_FALSE(transactionSuccess); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void 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 Ishiguro | e6b540d | 2021-10-29 16:01:35 +0000 | [diff] [blame] | 427 | sp<EmptyContextHubCallback> cb = sp<EmptyContextHubCallback>::make(); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 428 | ASSERT_TRUE(mContextHub->registerCallback(getHubId(), cb).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 429 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 430 | ASSERT_TRUE(mContextHub->onSettingChanged(setting, true /* enabled */).isOk()); |
| 431 | ASSERT_TRUE(mContextHub->onSettingChanged(setting, false /* enabled */).isOk()); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | TEST_P(ContextHubAidl, TestOnLocationSettingChanged) { |
| 435 | testSettingChanged(Setting::LOCATION); |
| 436 | } |
| 437 | |
| 438 | TEST_P(ContextHubAidl, TestOnWifiMainSettingChanged) { |
| 439 | testSettingChanged(Setting::WIFI_MAIN); |
| 440 | } |
| 441 | |
| 442 | TEST_P(ContextHubAidl, TestOnWifiScanningSettingChanged) { |
| 443 | testSettingChanged(Setting::WIFI_SCANNING); |
| 444 | } |
| 445 | |
| 446 | TEST_P(ContextHubAidl, TestOnAirplaneModeSettingChanged) { |
| 447 | testSettingChanged(Setting::AIRPLANE_MODE); |
| 448 | } |
| 449 | |
| 450 | TEST_P(ContextHubAidl, TestOnMicrophoneSettingChanged) { |
| 451 | testSettingChanged(Setting::MICROPHONE); |
| 452 | } |
| 453 | |
Anthony | a6b6500 | 2022-01-20 20:49:10 +0000 | [diff] [blame] | 454 | TEST_P(ContextHubAidl, TestOnBtMainSettingChanged) { |
| 455 | testSettingChanged(Setting::BT_MAIN); |
| 456 | } |
| 457 | |
| 458 | TEST_P(ContextHubAidl, TestOnBtScanningSettingChanged) { |
| 459 | testSettingChanged(Setting::BT_SCANNING); |
| 460 | } |
| 461 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 462 | std::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 Ju | bc6ead0 | 2024-02-05 18:15:08 -0800 | [diff] [blame] | 480 | TEST_P(ContextHubTransactionTest, TestHostConnection) { |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 481 | constexpr char16_t kHostEndpointId = 1; |
| 482 | HostEndpointInfo hostEndpointInfo; |
Arthur Ishiguro | 16f4062 | 2023-02-10 18:11:12 +0000 | [diff] [blame] | 483 | hostEndpointInfo.type = HostEndpointInfo::Type::NATIVE; |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 484 | hostEndpointInfo.hostEndpointId = kHostEndpointId; |
| 485 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 486 | ASSERT_TRUE(mContextHub->onHostEndpointConnected(hostEndpointInfo).isOk()); |
| 487 | ASSERT_TRUE(mContextHub->onHostEndpointDisconnected(kHostEndpointId).isOk()); |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Lei Ju | bc6ead0 | 2024-02-05 18:15:08 -0800 | [diff] [blame] | 490 | TEST_P(ContextHubTransactionTest, TestInvalidHostConnection) { |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 491 | constexpr char16_t kHostEndpointId = 1; |
| 492 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 493 | ASSERT_TRUE(mContextHub->onHostEndpointDisconnected(kHostEndpointId).isOk()); |
Arthur Ishiguro | 065a9a5 | 2021-11-19 00:24:45 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Lei Ju | bc6ead0 | 2024-02-05 18:15:08 -0800 | [diff] [blame] | 496 | TEST_P(ContextHubTransactionTest, TestNanSessionStateChange) { |
Anthony Stange | 7fba100 | 2023-03-02 21:45:20 +0000 | [diff] [blame] | 497 | NanSessionStateUpdate update; |
| 498 | update.state = true; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 499 | Status status = mContextHub->onNanSessionStateChanged(update); |
Rocky Fang | 7814098 | 2023-06-16 00:42:41 +0000 | [diff] [blame] | 500 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 506 | ASSERT_TRUE(mContextHub->onNanSessionStateChanged(update).isOk()); |
Rocky Fang | 7814098 | 2023-06-16 00:42:41 +0000 | [diff] [blame] | 507 | } |
Anthony Stange | 7344af9 | 2022-12-22 14:21:31 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 510 | TEST_P(ContextHubAidl, TestSendMessageDeliveryStatusToHub) { |
| 511 | MessageDeliveryStatus messageDeliveryStatus; |
| 512 | messageDeliveryStatus.messageSequenceNumber = 123; |
| 513 | messageDeliveryStatus.errorCode = ErrorCode::OK; |
| 514 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 515 | Status status = mContextHub->sendMessageDeliveryStatusToHub(getHubId(), messageDeliveryStatus); |
Matthew Sedam | adfd557 | 2023-11-21 05:53:00 -0800 | [diff] [blame] | 516 | 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 Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 524 | class 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 Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 536 | { |
| 537 | std::unique_lock<std::mutex> lock(mMutex); |
| 538 | mMessages.push_back(message); |
| 539 | } |
| 540 | mCondVar.notify_one(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 541 | 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 Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 561 | { |
| 562 | std::unique_lock<std::mutex> lock(mMutex); |
| 563 | mWasOnEndpointSessionOpenCompleteCalled = true; |
| 564 | } |
| 565 | mCondVar.notify_one(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 566 | return Status::ok(); |
| 567 | } |
| 568 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 569 | bool wasOnEndpointSessionOpenCompleteCalled() { |
| 570 | return mWasOnEndpointSessionOpenCompleteCalled; |
| 571 | } |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 572 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 573 | void resetWasOnEndpointSessionOpenCompleteCalled() { |
| 574 | mWasOnEndpointSessionOpenCompleteCalled = false; |
| 575 | } |
| 576 | |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 577 | std::mutex& getMutex() { return mMutex; } |
| 578 | std::condition_variable& getCondVar() { return mCondVar; } |
| 579 | std::vector<Message> getMessages() { return mMessages; } |
| 580 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 581 | private: |
| 582 | std::vector<Message> mMessages; |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 583 | std::mutex mMutex; |
| 584 | std::condition_variable mCondVar; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 585 | bool mWasOnEndpointSessionOpenCompleteCalled = false; |
| 586 | }; |
| 587 | |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 588 | TEST_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 Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 607 | TEST_P(ContextHubAidlWithTestMode, RegisterEndpoint) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 608 | if (!registerDefaultHub()) { |
| 609 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 610 | } |
| 611 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 612 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 619 | Status status = mHubInterface->registerEndpoint(endpointInfo); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 620 | 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 Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 628 | TEST_P(ContextHubAidlWithTestMode, RegisterEndpointSameNameFailure) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 629 | if (!registerDefaultHub()) { |
| 630 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 631 | } |
| 632 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 633 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 647 | Status status = mHubInterface->registerEndpoint(endpointInfo); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 648 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 655 | EXPECT_FALSE(mHubInterface->registerEndpoint(endpointInfo2).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 658 | TEST_P(ContextHubAidlWithTestMode, RegisterEndpointSameIdFailure) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 659 | if (!registerDefaultHub()) { |
| 660 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 661 | } |
| 662 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 663 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 677 | Status status = mHubInterface->registerEndpoint(endpointInfo); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 678 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 685 | EXPECT_FALSE(mHubInterface->registerEndpoint(endpointInfo2).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 688 | TEST_P(ContextHubAidlWithTestMode, UnregisterEndpoint) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 689 | if (!registerDefaultHub()) { |
| 690 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 691 | } |
| 692 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 693 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 700 | Status status = mHubInterface->registerEndpoint(endpointInfo); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 701 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 708 | EXPECT_TRUE(mHubInterface->unregisterEndpoint(endpointInfo).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 711 | TEST_P(ContextHubAidlWithTestMode, UnregisterEndpointNonexistent) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 712 | if (!registerDefaultHub()) { |
| 713 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
| 714 | } |
| 715 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 716 | 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 723 | Status status = mHubInterface->unregisterEndpoint(endpointInfo); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 724 | 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 Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 732 | TEST_P(ContextHubAidlWithTestMode, OpenEndpointSessionInvalidRange) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 733 | if (!registerDefaultHub()) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 734 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 735 | } |
| 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 744 | EXPECT_TRUE(mHubInterface->registerEndpoint(initiatorEndpoint).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 745 | |
| 746 | // Find the destination, if it exists |
| 747 | std::vector<EndpointInfo> endpoints; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 748 | EXPECT_TRUE(mContextHub->getEndpoints(&endpoints).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 749 | const EndpointInfo* destinationEndpoint = nullptr; |
| 750 | for (const EndpointInfo& endpoint : endpoints) { |
| 751 | for (const Service& service : endpoint.services) { |
Arthur Ishiguro | 38712cf | 2024-12-13 11:40:47 -0800 | [diff] [blame] | 752 | if (service.serviceDescriptor == kEchoServiceName) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 753 | 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 Zhang | cef8f66 | 2024-12-06 16:03:26 -0800 | [diff] [blame] | 764 | std::array<int32_t, 2> range; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 765 | ASSERT_TRUE(mHubInterface->requestSessionIdRange(requestedRange, &range).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 766 | EXPECT_EQ(range.size(), 2); |
| 767 | EXPECT_GE(range[1] - range[0] + 1, requestedRange); |
| 768 | |
| 769 | // Open the session |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 770 | int32_t sessionId = range[1] + 10; // invalid |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 771 | EXPECT_FALSE(mHubInterface |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 772 | ->openEndpointSession(sessionId, destinationEndpoint->id, |
| 773 | initiatorEndpoint.id, |
Arthur Ishiguro | 38712cf | 2024-12-13 11:40:47 -0800 | [diff] [blame] | 774 | /* in_serviceDescriptor= */ kEchoServiceName) |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 775 | .isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 778 | TEST_P(ContextHubAidlWithTestMode, OpenEndpointSessionAndSendMessageEchoesBack) { |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 779 | if (!registerDefaultHub()) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 780 | GTEST_SKIP() << "Not supported -> old API; or not implemented"; |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 781 | } |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 782 | std::unique_lock<std::mutex> lock(mEndpointCb->getMutex()); |
Matthew Sedam | 0ec290c | 2024-12-11 11:18:15 -0800 | [diff] [blame] | 783 | |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 784 | // 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 Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 791 | EXPECT_TRUE(mHubInterface->registerEndpoint(initiatorEndpoint).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 792 | |
| 793 | // Find the destination, if it exists |
| 794 | std::vector<EndpointInfo> endpoints; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 795 | EXPECT_TRUE(mContextHub->getEndpoints(&endpoints).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 796 | const EndpointInfo* destinationEndpoint = nullptr; |
| 797 | for (const EndpointInfo& endpoint : endpoints) { |
| 798 | for (const Service& service : endpoint.services) { |
Arthur Ishiguro | 38712cf | 2024-12-13 11:40:47 -0800 | [diff] [blame] | 799 | if (service.serviceDescriptor == kEchoServiceName) { |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 800 | 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 Zhang | cef8f66 | 2024-12-06 16:03:26 -0800 | [diff] [blame] | 811 | std::array<int32_t, 2> range; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 812 | ASSERT_TRUE(mHubInterface->requestSessionIdRange(requestedRange, &range).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 813 | EXPECT_EQ(range.size(), 2); |
| 814 | EXPECT_GE(range[1] - range[0] + 1, requestedRange); |
| 815 | |
| 816 | // Open the session |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 817 | mEndpointCb->resetWasOnEndpointSessionOpenCompleteCalled(); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 818 | int32_t sessionId = range[0]; |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 819 | ASSERT_TRUE(mHubInterface |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 820 | ->openEndpointSession(sessionId, destinationEndpoint->id, |
| 821 | initiatorEndpoint.id, |
Arthur Ishiguro | 38712cf | 2024-12-13 11:40:47 -0800 | [diff] [blame] | 822 | /* in_serviceDescriptor= */ kEchoServiceName) |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 823 | .isOk()); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 824 | mEndpointCb->getCondVar().wait(lock); |
| 825 | EXPECT_TRUE(mEndpointCb->wasOnEndpointSessionOpenCompleteCalled()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 826 | |
| 827 | // Send the message |
| 828 | Message message; |
| 829 | message.flags = 0; |
| 830 | message.sequenceNumber = 0; |
| 831 | message.content.push_back(42); |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 832 | ASSERT_TRUE(mHubInterface->sendMessageToEndpoint(sessionId, message).isOk()); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 833 | |
| 834 | // Check for echo |
Madhav Iyengar | ee25d07 | 2024-12-17 17:08:55 +0000 | [diff] [blame] | 835 | mEndpointCb->getCondVar().wait(lock); |
| 836 | EXPECT_FALSE(mEndpointCb->getMessages().empty()); |
| 837 | EXPECT_EQ(mEndpointCb->getMessages().back().content.back(), 42); |
Matthew Sedam | 92c2bd8 | 2024-11-11 19:52:30 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 840 | std::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 | |
| 844 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidl); |
| 845 | INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidl, testing::ValuesIn(generateContextHubMapping()), |
| 846 | PrintGeneratedTest); |
| 847 | |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 848 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubAidlWithTestMode); |
| 849 | INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubAidlWithTestMode, |
| 850 | testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest); |
| 851 | |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 852 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ContextHubTransactionTest); |
| 853 | INSTANTIATE_TEST_SUITE_P(ContextHub, ContextHubTransactionTest, |
| 854 | testing::ValuesIn(generateContextHubMapping()), PrintGeneratedTest); |
| 855 | |
| 856 | int main(int argc, char** argv) { |
| 857 | ::testing::InitGoogleTest(&argc, argv); |
Matthew Sedam | 41db093 | 2025-01-02 11:17:46 -0800 | [diff] [blame] | 858 | ProcessState::self()->setThreadPoolMaxThreadCount(2); |
Arthur Ishiguro | 4e916c3 | 2021-08-12 12:47:03 -0700 | [diff] [blame] | 859 | ProcessState::self()->startThreadPool(); |
| 860 | return RUN_ALL_TESTS(); |
| 861 | } |