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