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