|
20 | 20 | #include "gtest/gtest.h" |
21 | 21 | #include "absl/strings/string_view.h" |
22 | 22 | #include "absl/time/time.h" |
23 | | -#include "connections/implementation/flags/nearby_connections_feature_flags.h" |
24 | | -#include "internal/flags/nearby_flags.h" |
25 | 23 | #include "internal/platform/awdl.h" |
26 | | -#include "internal/platform/base64_utils.h" |
27 | 24 | #include "internal/platform/cancellation_flag.h" |
28 | 25 | #include "internal/platform/count_down_latch.h" |
29 | 26 | #include "internal/platform/expected.h" |
30 | 27 | #include "internal/platform/feature_flags.h" |
| 28 | +#include "internal/platform/implementation/psk_info.h" |
31 | 29 | #include "internal/platform/logging.h" |
32 | 30 | #include "internal/platform/medium_environment.h" |
33 | 31 | #include "internal/platform/nsd_service_info.h" |
34 | | -#include "internal/platform/single_thread_executor.h" |
35 | 32 |
|
36 | 33 | namespace nearby { |
37 | 34 | namespace connections { |
@@ -113,6 +110,61 @@ TEST_P(AwdlTest, CanConnect) { |
113 | 110 | env_.Stop(); |
114 | 111 | } |
115 | 112 |
|
| 113 | +TEST_P(AwdlTest, CanConnectWithPsk) { |
| 114 | + FeatureFlags feature_flags = GetParam(); |
| 115 | + env_.SetFeatureFlags(feature_flags); |
| 116 | + env_.Start(); |
| 117 | + Awdl awdl_client; |
| 118 | + Awdl awdl_server; |
| 119 | + std::string service_id(kServiceID); |
| 120 | + std::string service_info_name(kServiceInfoName); |
| 121 | + std::string endpoint_info_name(kEndpointName); |
| 122 | + api::PskInfo psk_info; |
| 123 | + psk_info.password = "password"; |
| 124 | + CountDownLatch discovered_latch(1); |
| 125 | + CountDownLatch accept_latch(1); |
| 126 | + |
| 127 | + AwdlSocket socket_for_server; |
| 128 | + EXPECT_TRUE(awdl_server.StartAcceptingConnections( |
| 129 | + service_id, psk_info, |
| 130 | + [&](const std::string& service_id, AwdlSocket socket) { |
| 131 | + socket_for_server = std::move(socket); |
| 132 | + accept_latch.CountDown(); |
| 133 | + })); |
| 134 | + |
| 135 | + NsdServiceInfo nsd_service_info; |
| 136 | + nsd_service_info.SetServiceName(service_info_name); |
| 137 | + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), |
| 138 | + endpoint_info_name); |
| 139 | + awdl_server.StartAdvertising(service_id, nsd_service_info); |
| 140 | + |
| 141 | + NsdServiceInfo discovered_service_info; |
| 142 | + awdl_client.StartDiscovery( |
| 143 | + service_id, |
| 144 | + { |
| 145 | + .service_discovered_cb = |
| 146 | + [&discovered_latch, &discovered_service_info]( |
| 147 | + NsdServiceInfo service_info, const std::string& service_id) { |
| 148 | + LOG(INFO) << "Discovered service_info=" << &service_info; |
| 149 | + discovered_service_info = service_info; |
| 150 | + discovered_latch.CountDown(); |
| 151 | + }, |
| 152 | + }); |
| 153 | + discovered_latch.Await(kWaitDuration).result(); |
| 154 | + ASSERT_TRUE(discovered_service_info.IsValid()); |
| 155 | + |
| 156 | + CancellationFlag flag; |
| 157 | + ErrorOr<AwdlSocket> socket_for_client_result = |
| 158 | + awdl_client.Connect(service_id, discovered_service_info, psk_info, &flag); |
| 159 | + EXPECT_TRUE(accept_latch.Await(kWaitDuration).result()); |
| 160 | + EXPECT_TRUE(awdl_server.StopAcceptingConnections(service_id)); |
| 161 | + EXPECT_TRUE(awdl_server.StopAdvertising(service_id)); |
| 162 | + EXPECT_TRUE(socket_for_server.IsValid()); |
| 163 | + EXPECT_TRUE(socket_for_client_result.has_value()); |
| 164 | + EXPECT_TRUE(socket_for_client_result.value().IsValid()); |
| 165 | + env_.Stop(); |
| 166 | +} |
| 167 | + |
116 | 168 | TEST_P(AwdlTest, CanCancelConnect) { |
117 | 169 | FeatureFlags feature_flags = GetParam(); |
118 | 170 | env_.SetFeatureFlags(feature_flags); |
@@ -206,6 +258,115 @@ TEST_F(AwdlTest, CanStartAdvertising) { |
206 | 258 | env_.Stop(); |
207 | 259 | } |
208 | 260 |
|
| 261 | +TEST_F(AwdlTest, StartAdvertisingFailsWithInvalidNsdServiceInfo) { |
| 262 | + env_.Start(); |
| 263 | + Awdl awdl_a; |
| 264 | + std::string service_id(kServiceID); |
| 265 | + |
| 266 | + EXPECT_TRUE(awdl_a.StartAcceptingConnections(service_id, {})); |
| 267 | + |
| 268 | + NsdServiceInfo nsd_service_info; |
| 269 | + ErrorOr<bool> result = awdl_a.StartAdvertising(service_id, nsd_service_info); |
| 270 | + EXPECT_FALSE(result.has_value()); |
| 271 | + EXPECT_EQ(result.error().operation_result_code().value(), |
| 272 | + location::nearby::proto::connections::OperationResultCode:: |
| 273 | + MEDIUM_UNAVAILABLE_NSD_NOT_AVAILABLE); |
| 274 | + env_.Stop(); |
| 275 | +} |
| 276 | + |
| 277 | +TEST_F(AwdlTest, StopAdvertisingFailsIfNotAdvertising) { |
| 278 | + env_.Start(); |
| 279 | + Awdl awdl_a; |
| 280 | + std::string service_id(kServiceID); |
| 281 | + |
| 282 | + EXPECT_FALSE(awdl_a.StopAdvertising(service_id)); |
| 283 | + env_.Stop(); |
| 284 | +} |
| 285 | + |
| 286 | +TEST_F(AwdlTest, StartAdvertisingFailsIfAlreadyAdvertising) { |
| 287 | + env_.Start(); |
| 288 | + Awdl awdl_a; |
| 289 | + std::string service_id(kServiceID); |
| 290 | + std::string service_info_name(kServiceInfoName); |
| 291 | + std::string endpoint_info_name(kEndpointName); |
| 292 | + |
| 293 | + EXPECT_TRUE(awdl_a.StartAcceptingConnections(service_id, {})); |
| 294 | + |
| 295 | + NsdServiceInfo nsd_service_info; |
| 296 | + nsd_service_info.SetServiceName(service_info_name); |
| 297 | + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), |
| 298 | + endpoint_info_name); |
| 299 | + EXPECT_TRUE(awdl_a.StartAdvertising(service_id, nsd_service_info)); |
| 300 | + ErrorOr<bool> result = awdl_a.StartAdvertising(service_id, nsd_service_info); |
| 301 | + EXPECT_FALSE(result.has_value()); |
| 302 | + EXPECT_EQ(result.error().operation_result_code().value(), |
| 303 | + location::nearby::proto::connections::OperationResultCode:: |
| 304 | + CLIENT_AWDL_DUPLICATE_ADVERTISING); |
| 305 | + EXPECT_TRUE(awdl_a.StopAdvertising(service_id)); |
| 306 | + env_.Stop(); |
| 307 | +} |
| 308 | + |
| 309 | +TEST_F(AwdlTest, StartAdvertisingFailsIfNotAcceptingConnections) { |
| 310 | + env_.Start(); |
| 311 | + Awdl awdl_a; |
| 312 | + std::string service_id(kServiceID); |
| 313 | + std::string service_info_name(kServiceInfoName); |
| 314 | + std::string endpoint_info_name(kEndpointName); |
| 315 | + |
| 316 | + NsdServiceInfo nsd_service_info; |
| 317 | + nsd_service_info.SetServiceName(service_info_name); |
| 318 | + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), |
| 319 | + endpoint_info_name); |
| 320 | + ErrorOr<bool> result = awdl_a.StartAdvertising(service_id, nsd_service_info); |
| 321 | + EXPECT_FALSE(result.has_value()); |
| 322 | + EXPECT_EQ(result.error().operation_result_code().value(), |
| 323 | + location::nearby::proto::connections::OperationResultCode:: |
| 324 | + CLIENT_DUPLICATE_ACCEPTING_AWDL_CONNECTION_REQUEST); |
| 325 | + env_.Stop(); |
| 326 | +} |
| 327 | + |
| 328 | +TEST_F(AwdlTest, StartAdvertisingUpdatesNsdServiceInfo) { |
| 329 | + env_.Start(); |
| 330 | + Awdl awdl_a; |
| 331 | + std::string service_id(kServiceID); |
| 332 | + std::string service_info_name(kServiceInfoName); |
| 333 | + std::string endpoint_info_name(kEndpointName); |
| 334 | + |
| 335 | + EXPECT_TRUE(awdl_a.StartAcceptingConnections(service_id, {})); |
| 336 | + |
| 337 | + NsdServiceInfo nsd_service_info; |
| 338 | + nsd_service_info.SetServiceName(service_info_name); |
| 339 | + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), |
| 340 | + endpoint_info_name); |
| 341 | + EXPECT_TRUE(awdl_a.StartAdvertising(service_id, nsd_service_info)); |
| 342 | + EXPECT_FALSE(nsd_service_info.GetServiceType().empty()); |
| 343 | + EXPECT_FALSE(nsd_service_info.GetIPAddress().empty()); |
| 344 | + EXPECT_GT(nsd_service_info.GetPort(), 0); |
| 345 | + EXPECT_TRUE(awdl_a.StopAdvertising(service_id)); |
| 346 | + env_.Stop(); |
| 347 | +} |
| 348 | + |
| 349 | +TEST_F(AwdlTest, CanStartAcceptingConnectionsWithPsk) { |
| 350 | + env_.Start(); |
| 351 | + Awdl awdl_a; |
| 352 | + std::string service_id(kServiceID); |
| 353 | + std::string service_info_name(kServiceInfoName); |
| 354 | + std::string endpoint_info_name(kEndpointName); |
| 355 | + api::PskInfo psk_info; |
| 356 | + psk_info.password = "password"; |
| 357 | + |
| 358 | + EXPECT_TRUE(awdl_a.StartAcceptingConnections(service_id, psk_info, {})); |
| 359 | + |
| 360 | + NsdServiceInfo nsd_service_info; |
| 361 | + nsd_service_info.SetServiceName(service_info_name); |
| 362 | + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), |
| 363 | + endpoint_info_name); |
| 364 | + EXPECT_TRUE(awdl_a.StartAdvertising(service_id, nsd_service_info)); |
| 365 | + EXPECT_EQ(awdl_a.GetCredentials(service_id).password, "password"); |
| 366 | + EXPECT_TRUE(awdl_a.StopAdvertising(service_id)); |
| 367 | + env_.Stop(); |
| 368 | +} |
| 369 | + |
209 | 370 | TEST_F(AwdlTest, CanStartMultipleAdvertising) { |
210 | 371 | env_.Start(); |
211 | 372 | Awdl awdl_a; |
@@ -235,6 +396,32 @@ TEST_F(AwdlTest, CanStartMultipleAdvertising) { |
235 | 396 | env_.Stop(); |
236 | 397 | } |
237 | 398 |
|
| 399 | +TEST_F(AwdlTest, StartAcceptingConnectionsFailsWithEmptyServiceId) { |
| 400 | + env_.Start(); |
| 401 | + Awdl awdl_a; |
| 402 | + ErrorOr<bool> result = awdl_a.StartAcceptingConnections("", {}); |
| 403 | + EXPECT_FALSE(result.has_value()); |
| 404 | + EXPECT_EQ(result.error().operation_result_code().value(), |
| 405 | + location::nearby::proto::connections::OperationResultCode:: |
| 406 | + NEARBY_LOCAL_CLIENT_STATE_WRONG); |
| 407 | + env_.Stop(); |
| 408 | +} |
| 409 | + |
| 410 | +TEST_F(AwdlTest, StartAcceptingConnectionsFailsIfAlreadyAccepting) { |
| 411 | + env_.Start(); |
| 412 | + Awdl awdl_a; |
| 413 | + std::string service_id(kServiceID); |
| 414 | + |
| 415 | + EXPECT_TRUE(awdl_a.StartAcceptingConnections(service_id, {})); |
| 416 | + ErrorOr<bool> result = awdl_a.StartAcceptingConnections(service_id, {}); |
| 417 | + EXPECT_FALSE(result.has_value()); |
| 418 | + EXPECT_EQ(result.error().operation_result_code().value(), |
| 419 | + location::nearby::proto::connections::OperationResultCode:: |
| 420 | + CLIENT_DUPLICATE_ACCEPTING_AWDL_CONNECTION_REQUEST); |
| 421 | + awdl_a.StopAcceptingConnections(service_id); |
| 422 | + env_.Stop(); |
| 423 | +} |
| 424 | + |
238 | 425 | TEST_F(AwdlTest, CanStartDiscovery) { |
239 | 426 | env_.Start(); |
240 | 427 | Awdl awdl_a; |
|
0 commit comments