diff --git a/connections/implementation/mediums/wifi_lan_bwu_handler.cc b/connections/implementation/mediums/wifi_lan_bwu_handler.cc index fca9ecef..a25cd81c 100644 --- a/connections/implementation/mediums/wifi_lan_bwu_handler.cc +++ b/connections/implementation/mediums/wifi_lan_bwu_handler.cc @@ -124,6 +124,7 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel( std::string WifiLanBwuHandler::HandleInitializeUpgradedMediumForEndpoint( ClientProxy* client, const std::string& upgrade_service_id, const std::string& endpoint_id) { + bool started_accepting = false; if (!wifi_lan_medium_.IsAcceptingConnections(upgrade_service_id)) { if (!wifi_lan_medium_.StartAcceptingConnections( upgrade_service_id, @@ -140,6 +141,7 @@ std::string WifiLanBwuHandler::HandleInitializeUpgradedMediumForEndpoint( << "WifiLanBwuHandler successfully started listening for incoming " "WifiLan connections while upgrading endpoint " << endpoint_id; + started_accepting = true; } // Address candidates are not populated until StartAcceptingConnections() is @@ -151,6 +153,9 @@ std::string WifiLanBwuHandler::HandleInitializeUpgradedMediumForEndpoint( LOG(INFO) << "WifiLanBwuHandler couldn't initiate the wifi_lan upgrade for " << "service " << upgrade_service_id << " and endpoint " << endpoint_id << " because there are no available ip addresses."; + if (started_accepting) { + wifi_lan_medium_.StopAcceptingConnections(upgrade_service_id); + } return {}; } client->GetAnalyticsRecorder().UpdateBwUpgradeNetworkInfo( diff --git a/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc b/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc index 24074701..600c8474 100644 --- a/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc +++ b/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc @@ -39,6 +39,7 @@ #include "internal/platform/mock_wifi_lan_server_socket.h" #include "internal/platform/mock_wifi_lan_socket.h" #include "internal/platform/service_address.h" +#include "internal/platform/wifi_lan.h" #include "internal/proto/analytics/connections_log.pb.h" namespace nearby { @@ -344,6 +345,71 @@ TEST_F(WifiLanBwuHandlerTest, InitializeUpgradedMediumForEndpoint_Success) { client.GetAnalyticsRecorder().LogSession(); } +TEST_F(WifiLanBwuHandlerTest, + InitializeUpgradedMediumForEndpoint_EmptyCandidates_StopsAccepting) { + MediumEnvironment::Instance().Start({.use_simulated_clock = true}); + ClientProxy client(&mock_event_logger_); + client.AddCancellationFlag(std::string(kEndpointId)); + + auto mock_server_socket = std::make_unique(); + MockWifiLanServerSocket* raw_server_socket = mock_server_socket.get(); + + EXPECT_CALL(*raw_server_socket, GetPort()).WillRepeatedly(Return(8080)); + EXPECT_CALL(*wifi_lan_medium, IsNetworkConnected()) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*wifi_lan_medium, ListenForService(_)) + .WillOnce(Return(ByMove(std::move(mock_server_socket)))); + + EXPECT_CALL(*wifi_lan_medium, GetUpgradeAddressCandidates(_)) + .WillOnce(Return(api::UpgradeAddressInfo{.num_interfaces = 0, + .num_ipv6_only_interfaces = 0, + .address_candidates = {}})); + + std::string result = handler_.InitializeUpgradedMediumForEndpoint( + &client, std::string(kServiceId), std::string(kEndpointId)); + + EXPECT_TRUE(result.empty()); + EXPECT_FALSE( + mediums_.GetWifiLan().IsAcceptingConnections("service_id_UPGRADE")); +} + +TEST_F( + WifiLanBwuHandlerTest, + InitializeUpgradedMediumForEndpoint_AlreadyAccepting_KeepAccepting) { + MediumEnvironment::Instance().Start({.use_simulated_clock = true}); + ClientProxy client(&mock_event_logger_); + client.AddCancellationFlag(std::string(kEndpointId)); + + auto mock_server_socket = std::make_unique(); + MockWifiLanServerSocket* raw_server_socket = mock_server_socket.get(); + + EXPECT_CALL(*raw_server_socket, GetPort()).WillRepeatedly(Return(8080)); + EXPECT_CALL(*wifi_lan_medium, IsNetworkConnected()) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*wifi_lan_medium, ListenForService(_)) + .WillOnce(Return(ByMove(std::move(mock_server_socket)))); + + EXPECT_TRUE( + mediums_.GetWifiLan() + .StartAcceptingConnections("service_id_UPGRADE", + [](const std::string&, WifiLanSocket) {}) + .has_value()); + EXPECT_TRUE( + mediums_.GetWifiLan().IsAcceptingConnections("service_id_UPGRADE")); + + EXPECT_CALL(*wifi_lan_medium, GetUpgradeAddressCandidates(_)) + .WillOnce(Return(api::UpgradeAddressInfo{.num_interfaces = 0, + .num_ipv6_only_interfaces = 0, + .address_candidates = {}})); + + std::string result = handler_.InitializeUpgradedMediumForEndpoint( + &client, std::string(kServiceId), std::string(kEndpointId)); + + EXPECT_TRUE(result.empty()); + EXPECT_TRUE( + mediums_.GetWifiLan().IsAcceptingConnections("service_id_UPGRADE")); +} + } // namespace } // namespace connections namespace api {