diff --git a/connections/implementation/bwu_manager_test.cc b/connections/implementation/bwu_manager_test.cc index bca45522..97cf9dbe 100644 --- a/connections/implementation/bwu_manager_test.cc +++ b/connections/implementation/bwu_manager_test.cc @@ -40,6 +40,7 @@ constexpr absl::string_view kServiceIdB = "ServiceB"; constexpr absl::string_view kEndpointId1 = "Endpoint1"; constexpr absl::string_view kEndpointId2 = "Endpoint2"; constexpr absl::string_view kEndpointId3 = "Endpoint3"; +constexpr absl::string_view kEndpointId4 = "Endpoint4"; class BwuManagerTest : public ::testing::Test { protected: @@ -49,14 +50,18 @@ class BwuManagerTest : public ::testing::Test { auto fake_web_rtc = std::make_unique(Medium::WEB_RTC); auto fake_wifi_lan = std::make_unique(Medium::WIFI_LAN); + auto fake_wifi_hotspot = + std::make_unique(Medium::WIFI_HOTSPOT); fake_web_rtc_bwu_handler_ = fake_web_rtc.get(); fake_wifi_lan_bwu_handler_ = fake_wifi_lan.get(); + fake_wifi_hotspot_bwu_handler_ = fake_wifi_hotspot.get(); handlers.emplace(Medium::WEB_RTC, std::move(fake_web_rtc)); handlers.emplace(Medium::WIFI_LAN, std::move(fake_wifi_lan)); + handlers.emplace(Medium::WIFI_HOTSPOT, std::move(fake_wifi_hotspot)); BwuManager::Config config; - config.allow_upgrade_to = - BooleanMediumSelector{.web_rtc = true, .wifi_lan = true}; + config.allow_upgrade_to = BooleanMediumSelector{ + .web_rtc = true, .wifi_lan = true, .wifi_hotspot = true}; bwu_manager_ = std::make_unique(mediums_, em_, ecm_, std::move(handlers), config); @@ -95,6 +100,9 @@ class BwuManagerTest : public ::testing::Test { case Medium::WIFI_LAN: handler = fake_wifi_lan_bwu_handler_; break; + case Medium::WIFI_HOTSPOT: + handler = fake_wifi_hotspot_bwu_handler_; + break; default: return nullptr; } @@ -129,6 +137,7 @@ class BwuManagerTest : public ::testing::Test { Mediums mediums_; FakeBwuHandler* fake_web_rtc_bwu_handler_ = nullptr; FakeBwuHandler* fake_wifi_lan_bwu_handler_ = nullptr; + FakeBwuHandler* fake_wifi_hotspot_bwu_handler_ = nullptr; std::unique_ptr bwu_manager_; }; @@ -154,6 +163,8 @@ TEST_P(BwuManagerTestParam, InitiateBwu_Success) { // The appropriate upgrade medium handler is informed of the BWU initiation. ASSERT_EQ(1u, fake_web_rtc_bwu_handler_->handle_initialize_calls().size()); EXPECT_TRUE(fake_wifi_lan_bwu_handler_->handle_initialize_calls().empty()); + EXPECT_TRUE( + fake_wifi_hotspot_bwu_handler_->handle_initialize_calls().empty()); EXPECT_EQ(WrapInitiatorUpgradeServiceId(kServiceIdA), fake_web_rtc_bwu_handler_->handle_initialize_calls()[0].service_id); EXPECT_EQ( @@ -222,6 +233,8 @@ TEST_P(BwuManagerTestParam, InitiateBwu_Error_NoMediumHandler) { // Make sure none of the other medium handlers are called. EXPECT_TRUE(fake_web_rtc_bwu_handler_->handle_initialize_calls().empty()); EXPECT_TRUE(fake_wifi_lan_bwu_handler_->handle_initialize_calls().empty()); + EXPECT_TRUE( + fake_wifi_hotspot_bwu_handler_->handle_initialize_calls().empty()); } TEST_P(BwuManagerTestParam, InitiateBwu_Error_UpgradeAlreadyInProgress) { @@ -237,6 +250,8 @@ TEST_P(BwuManagerTestParam, InitiateBwu_Error_UpgradeAlreadyInProgress) { Medium::WIFI_LAN); EXPECT_EQ(1u, fake_web_rtc_bwu_handler_->handle_initialize_calls().size()); EXPECT_TRUE(fake_wifi_lan_bwu_handler_->handle_initialize_calls().empty()); + EXPECT_TRUE( + fake_wifi_hotspot_bwu_handler_->handle_initialize_calls().empty()); } TEST_P(BwuManagerTestParam, @@ -475,12 +490,15 @@ TEST_F( CreateInitialEndpoint(kServiceIdA, kEndpointId1, Medium::BLUETOOTH); CreateInitialEndpoint(kServiceIdA, kEndpointId2, Medium::BLUETOOTH); CreateInitialEndpoint(kServiceIdB, kEndpointId3, Medium::BLUETOOTH); + CreateInitialEndpoint(kServiceIdB, kEndpointId4, Medium::BLUETOOTH); FullyUpgradeEndpoint(kEndpointId1, /*initial_medium=*/Medium::BLUETOOTH, /*upgrade_medium=*/Medium::WEB_RTC); FullyUpgradeEndpoint(kEndpointId2, /*initial_medium=*/Medium::BLUETOOTH, /*upgrade_medium=*/Medium::WIFI_LAN); FullyUpgradeEndpoint(kEndpointId3, /*initial_medium=*/Medium::BLUETOOTH, /*upgrade_medium=*/Medium::WIFI_LAN); + FullyUpgradeEndpoint(kEndpointId4, /*initial_medium=*/Medium::BLUETOOTH, + /*upgrade_medium=*/Medium::WIFI_HOTSPOT); std::string upgrade_service_id_A = WrapInitiatorUpgradeServiceId(kServiceIdA); std::string upgrade_service_id_B = WrapInitiatorUpgradeServiceId(kServiceIdB); @@ -490,8 +508,10 @@ TEST_F( // endpoint of that medium for the service is disconnected. EXPECT_TRUE(fake_web_rtc_bwu_handler_->disconnect_calls().empty()); EXPECT_TRUE(fake_wifi_lan_bwu_handler_->disconnect_calls().empty()); + EXPECT_TRUE(fake_wifi_hotspot_bwu_handler_->disconnect_calls().empty()); EXPECT_TRUE(fake_web_rtc_bwu_handler_->handle_revert_calls().empty()); EXPECT_TRUE(fake_wifi_lan_bwu_handler_->handle_revert_calls().empty()); + EXPECT_TRUE(fake_wifi_hotspot_bwu_handler_->handle_revert_calls().empty()); { CountDownLatch latch(1); ecm_.UnregisterChannelForEndpoint(std::string(kEndpointId1)); @@ -509,6 +529,8 @@ TEST_F( // We reverted a WebRTC channel; no WLAN calls expected. EXPECT_TRUE(fake_wifi_lan_bwu_handler_->disconnect_calls().empty()); EXPECT_TRUE(fake_wifi_lan_bwu_handler_->handle_revert_calls().empty()); + EXPECT_TRUE(fake_wifi_hotspot_bwu_handler_->disconnect_calls().empty()); + EXPECT_TRUE(fake_wifi_hotspot_bwu_handler_->handle_revert_calls().empty()); } { CountDownLatch latch(1); @@ -546,6 +568,26 @@ TEST_F( EXPECT_EQ(upgrade_service_id_B, fake_wifi_lan_bwu_handler_->handle_revert_calls()[1].service_id); } + { + CountDownLatch latch(1); + ecm_.UnregisterChannelForEndpoint(std::string(kEndpointId4)); + bwu_manager_->OnEndpointDisconnect(&client_, upgrade_service_id_B, + std::string(kEndpointId4), latch); + + // We reverted a Hotspot channel; no additional WebRTC calls expected. + EXPECT_EQ(1u, fake_web_rtc_bwu_handler_->disconnect_calls().size()); + EXPECT_EQ(1u, fake_web_rtc_bwu_handler_->handle_revert_calls().size()); + + // No more Hotspot channels for service B; expect revert call. + ASSERT_EQ(1u, fake_wifi_hotspot_bwu_handler_->disconnect_calls().size()); + EXPECT_EQ( + kEndpointId4, + fake_wifi_hotspot_bwu_handler_->disconnect_calls()[0].endpoint_id); + ASSERT_EQ(1u, fake_wifi_hotspot_bwu_handler_->handle_revert_calls().size()); + EXPECT_EQ( + upgrade_service_id_B, + fake_wifi_hotspot_bwu_handler_->handle_revert_calls()[0].service_id); + } } TEST_F(BwuManagerTest, InitiateBwu_Revert_OnUpgradeFailure_FlagEnabled) { diff --git a/connections/implementation/fake_bwu_handler.h b/connections/implementation/fake_bwu_handler.h index b9e5c3e9..5e8204da 100644 --- a/connections/implementation/fake_bwu_handler.h +++ b/connections/implementation/fake_bwu_handler.h @@ -139,6 +139,9 @@ class FakeBwuHandler : public BaseBwuHandler { case proto::connections::UNKNOWN_MEDIUM: case proto::connections::MDNS: case proto::connections::WIFI_HOTSPOT: + return parser::ForBwuWifiHotspotPathAvailable( + /*ssid=*/"Direct-357a2d8c", /*password=*/"b592f7d3", + /*port=*/1234, /*gateway=*/"123.234.23.1", false); case proto::connections::BLE: case proto::connections::WIFI_AWARE: case proto::connections::NFC: diff --git a/connections/implementation/mediums/wifi_hotspot_test.cc b/connections/implementation/mediums/wifi_hotspot_test.cc index 2193fa02..8a8d6a85 100644 --- a/connections/implementation/mediums/wifi_hotspot_test.cc +++ b/connections/implementation/mediums/wifi_hotspot_test.cc @@ -43,9 +43,9 @@ constexpr FeatureFlags kTestCases[] = { }; constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"}; -constexpr absl::string_view kSsid{"Direct_Nearby"}; +constexpr absl::string_view kSsid{"Direct-357a2d8c"}; constexpr absl::string_view kPassword{"12345678"}; -constexpr absl::string_view kIp = "123.234.23.2"; +constexpr absl::string_view kIp = "123.234.23.1"; constexpr const size_t kPort = 20; constexpr absl::Duration kWaitDuration = absl::Milliseconds(100); diff --git a/connections/implementation/wifi_hotspot_test.cc b/connections/implementation/wifi_hotspot_test.cc index 41296bc1..acc8cab2 100644 --- a/connections/implementation/wifi_hotspot_test.cc +++ b/connections/implementation/wifi_hotspot_test.cc @@ -12,43 +12,113 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/wifi_hotspot_bwu_handler.h" -#include "connections/implementation/wifi_hotspot_endpoint_channel.h" +#include #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "absl/time/clock.h" +#include "internal/platform/medium_environment.h" +#include "connections/implementation/bwu_handler.h" +#include "connections/implementation/endpoint_channel_manager.h" +#include "connections/implementation/wifi_hotspot_bwu_handler.h" +#include "connections/implementation/wifi_hotspot_endpoint_channel.h" namespace location { namespace nearby { namespace connections { -TEST(WifiHotspotTest, CanCreateBwuHandler) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_bwu_handler.cc +constexpr absl::Duration kWaitDuration_s = absl::Milliseconds(500); +constexpr absl::Duration kWaitDuration_l = absl::Milliseconds(1000); + +class WifiHotspotTest : public testing::Test { + protected: + WifiHotspotTest() { + env_.Stop(); + env_.Start(); + } + ~WifiHotspotTest() override { + env_.Stop(); + } + + MediumEnvironment& env_{MediumEnvironment::Instance()}; +}; + +TEST_F(WifiHotspotTest, CanCreateBwuHandler) { + BwuHandler::BwuNotifications notifications{ + .incoming_connection_cb = {} + }; + ClientProxy client; + Mediums mediums; + + auto handler = + std::make_unique(mediums, notifications); + + handler->InitializeUpgradedMediumForEndpoint(&client, /*service_id=*/"B", + /*endpoint_id=*/"2"); + handler->RevertInitiatorState(); + SUCCEED(); + absl::SleepFor(kWaitDuration_s); + handler.reset(); } -TEST(WifiHotspotTest, CanInitializeUpgradedMediumForEndpoint) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_bwu_handler.cc -} +TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) { + CountDownLatch accept_latch(1); -TEST(WifiHotspotTest, CanRevert) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_bwu_handler.cc -} + BwuHandler::BwuNotifications notifications_1{ + .incoming_connection_cb = + [&accept_latch](ClientProxy* client, + std::unique_ptr + mutable_connection) { + NEARBY_LOGS(WARNING) << "Server socket connection accept call back"; + accept_latch.CountDown(); + }, + }; + BwuHandler::BwuNotifications notifications_2{ + .incoming_connection_cb = {} + }; + ClientProxy client_1, client_2; + Mediums mediums_1, mediums_2; + ExceptionOr upgrade_frame; -TEST(WifiHotspotTest, CanCreateUpgradedEndpointChannel) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_bwu_handler.cc -} + auto handler_1 = + std::make_unique(mediums_1, notifications_1); -TEST(WifiHotspotTest, CanOnIncomingWifiHotspotConnection) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_bwu_handler.cc -} + // client_1 works as Hotspot SoftAP + SingleThreadExecutor server_executor; + server_executor.Execute([&handler_1, &client_1, &upgrade_frame]() { + ByteArray upgrade_path_available_frame = + handler_1->InitializeUpgradedMediumForEndpoint( + &client_1, + /*service_id=*/"A", + /*endpoint_id=*/"1"); + EXPECT_FALSE(upgrade_path_available_frame.Empty()); -TEST(WifiHotspotTest, CanCreateEndpointChannel) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_endpoint_channel.cc -} + upgrade_frame = parser::FromBytes(upgrade_path_available_frame); + }); -TEST(WifiHotspotTest, CanGetMedium) { - // TODO(b/227482970): Add test coverage for wifi_hotspot_endpoint_channel.cc + absl::SleepFor(kWaitDuration_l); + + // client_2 works as Hotspot STA which will connect to client_1 + SingleThreadExecutor client_executor; + std::unique_ptr handler_2 = + std::make_unique(mediums_2, notifications_2); + + client_executor.Execute([&handler_2, &client_2, &upgrade_frame, + &accept_latch]() { + auto bwu_frame = + upgrade_frame.result().v1().bandwidth_upgrade_negotiation(); + + std::unique_ptr new_channel = + handler_2->CreateUpgradedEndpointChannel(&client_2, /*service_id=*/"A", + /*endpoint_id=*/"1", + bwu_frame.upgrade_path_info()); + EXPECT_TRUE(accept_latch.Await(kWaitDuration_l).result()); + EXPECT_EQ(new_channel->GetMedium(), + proto::connections::Medium::WIFI_HOTSPOT); + }); + + absl::SleepFor(kWaitDuration_s); } } // namespace connections diff --git a/internal/platform/wifi_hotspot_test.cc b/internal/platform/wifi_hotspot_test.cc index 1b241999..2ef169a5 100644 --- a/internal/platform/wifi_hotspot_test.cc +++ b/internal/platform/wifi_hotspot_test.cc @@ -42,7 +42,7 @@ constexpr FeatureFlags kTestCases[] = { constexpr absl::string_view kSsid = "Direct-357a2d8c"; constexpr absl::string_view kPassword = "b592f7d3"; -constexpr absl::string_view kIp = "123.234.23.2"; +constexpr absl::string_view kIp = "123.234.23.1"; constexpr const size_t kPort = 20; constexpr absl::Duration kWaitDuration = absl::Milliseconds(100); constexpr absl::string_view kData = "ABCD";