Refactor implementation:pcp_handler_test to improve test coverage.

PiperOrigin-RevId: 804491033
This commit is contained in:
hai007
2025-09-08 10:32:06 -07:00
committed by Copybara-Service
parent 6cd1575a64
commit 579a01e26f
4 changed files with 389 additions and 14 deletions
+2
View File
@@ -306,6 +306,7 @@ cc_test(
"//connections/implementation/analytics",
"//connections/implementation/flags:connections_flags",
"//connections/implementation/mediums",
"//connections/implementation/mediums:webrtc_utils",
"//connections/implementation/proto:offline_wire_formats_cc_proto",
"//connections/v3:v3_types",
"//internal/flags:nearby_flags",
@@ -320,6 +321,7 @@ cc_test(
"//proto:connections_enums_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
@@ -845,6 +845,8 @@ Status BasePcpHandler::RequestConnection(
const ConnectionRequestInfo& info,
const ConnectionOptions& connection_options) {
auto result = std::make_shared<Future<Status>>();
LOG(INFO) << "RequestConnection with supported mediums: "
<< GetStringValueOfSupportedMediums(connection_options);
RunOnPcpHandlerThread(
"request-connection",
[this, client, &info, connection_options, endpoint_id,
@@ -25,6 +25,7 @@
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/base/thread_annotations.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
@@ -41,10 +42,12 @@
#include "connections/implementation/endpoint_manager.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/mediums.h"
#include "connections/implementation/mediums/webrtc_peer_id.h"
#include "connections/implementation/mock_device.h"
#include "connections/implementation/offline_frames.h"
#include "connections/implementation/pcp.h"
#include "connections/implementation/proto/offline_wire_formats.pb.h"
#include "connections/implementation/webrtc_state.h"
#include "connections/listeners.h"
#include "connections/medium_selector.h"
#include "connections/out_of_band_connection_metadata.h"
@@ -114,6 +117,7 @@ constexpr BooleanMediumSelector kTestCases[] = {
BooleanMediumSelector{
.bluetooth = true,
.ble = true,
.web_rtc = true,
.wifi_lan = true,
},
};
@@ -202,17 +206,10 @@ class MockPcpHandler : public BasePcpHandler {
MOCK_METHOD(Strategy, GetStrategy, (), (const, override));
MOCK_METHOD(Pcp, GetPcp, (), (const, override));
MOCK_METHOD(bool, HasOutgoingConnections, (ClientProxy * client),
(const, override));
MOCK_METHOD(bool, HasIncomingConnections, (ClientProxy * client),
(const, override));
MOCK_METHOD(bool, CanSendOutgoingConnection, (ClientProxy * client),
(const, override));
MOCK_METHOD(bool, CanReceiveIncomingConnection, (ClientProxy * client),
(const, override));
MOCK_METHOD(StartOperationResult, StartAdvertisingImpl,
(ClientProxy * client, const std::string& service_id,
const std::string& local_endpoint_id,
@@ -362,6 +359,24 @@ class MockPcpHandler : public BasePcpHandler {
return BasePcpHandler::NeedsToTurnOffDiscoveryMedium(medium, old_options,
new_options);
}
void StripOutWifiHotspotMedium(ConnectionInfo& connection_info) {
BasePcpHandler::StripOutWifiHotspotMedium(connection_info);
}
mediums::WebrtcPeerId CreatePeerIdFromAdvertisement(
const std::string& service_id, const std::string& endpoint_id,
const ByteArray& endpoint_info) {
return BasePcpHandler::CreatePeerIdFromAdvertisement(
service_id, endpoint_id, endpoint_info);
}
bool HasOutgoingConnections(ClientProxy* client) const override {
return BasePcpHandler::HasOutgoingConnections(client);
}
bool HasIncomingConnections(ClientProxy* client) const override {
return BasePcpHandler::HasIncomingConnections(client);
}
};
class MockContext {
@@ -894,6 +909,8 @@ TEST_P(BasePcpHandlerTest, ConstructorDestructorWorks) {
}
TEST_P(BasePcpHandlerTest, StartAdvertisingChangesState) {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::kEnableDct, true);
env_.Start();
ClientProxy client;
Mediums m;
@@ -1153,6 +1170,8 @@ TEST_P(BasePcpHandlerTest, RequestConnectionChangesState) {
RequestConnection("1234", std::move(channel_a), channel_b.get(), &client,
&pcp_handler, connect_medium);
LOG(INFO) << "RequestConnection complete";
EXPECT_TRUE(pcp_handler.HasOutgoingConnections(&client));
EXPECT_FALSE(pcp_handler.HasIncomingConnections(&client));
channel_b->Close();
bwu.Shutdown();
pcp_handler.DisconnectFromEndpointManager();
@@ -1697,6 +1716,55 @@ TEST_P(BasePcpHandlerTest, MultipleMediumsProduceSingleEndpointLostEvent) {
INSTANTIATE_TEST_SUITE_P(ParameterizedBasePcpHandlerTest, BasePcpHandlerTest,
::testing::ValuesIn(kTestCases));
TEST_F(BasePcpHandlerTest, StripOutWifiHotspotMedium) {
env_.Start();
Mediums m;
EndpointChannelManager ecm;
EndpointManager em(&ecm);
BwuManager bwu(m, em, ecm, {}, {});
MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu);
ConnectionInfo connection_info;
connection_info.supported_mediums = {Medium::WIFI_LAN, Medium::WIFI_HOTSPOT,
Medium::BLUETOOTH};
pcp_handler.StripOutWifiHotspotMedium(connection_info);
EXPECT_THAT(
connection_info.supported_mediums,
::testing::UnorderedElementsAre(Medium::WIFI_LAN, Medium::BLUETOOTH));
ConnectionInfo connection_info2;
connection_info2.supported_mediums = {Medium::WIFI_HOTSPOT,
Medium::BLUETOOTH};
pcp_handler.StripOutWifiHotspotMedium(connection_info2);
EXPECT_THAT(
connection_info2.supported_mediums,
::testing::UnorderedElementsAre(Medium::WIFI_HOTSPOT, Medium::BLUETOOTH));
bwu.Shutdown();
env_.Stop();
}
TEST_F(BasePcpHandlerTest, CreatePeerIdFromAdvertisement) {
env_.Start();
Mediums m;
EndpointChannelManager ecm;
EndpointManager em(&ecm);
BwuManager bwu(m, em, ecm, {}, {});
MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu);
std::string service_id = "service";
std::string endpoint_id = "endpoint";
ByteArray endpoint_info("info");
mediums::WebrtcPeerId peer_id = pcp_handler.CreatePeerIdFromAdvertisement(
service_id, endpoint_id, endpoint_info);
std::string seed =
absl::StrCat(service_id, endpoint_id, std::string(endpoint_info));
mediums::WebrtcPeerId expected_peer_id =
mediums::WebrtcPeerId::FromSeed(ByteArray(std::move(seed)));
EXPECT_EQ(peer_id.GetId(), expected_peer_id.GetId());
bwu.Shutdown();
env_.Stop();
}
TEST_F(BasePcpHandlerTest, InjectEndpoint) {
env_.Start();
std::string service_id{"service"};
@@ -2239,6 +2307,8 @@ TEST_F(BasePcpHandlerTest, TestDeviceFilterForConnectionsWithUnknown) {
Medium::BLUETOOTH,
NearbyDevice::Type::kConnectionsDevice)
.Ok());
EXPECT_TRUE(pcp_handler.HasIncomingConnections(&client));
EXPECT_FALSE(pcp_handler.HasOutgoingConnections(&client));
env_.Stop();
}
@@ -2573,6 +2643,9 @@ TEST_F(BasePcpHandlerTest, TestNeedsToTurnOffAdvertisingMedium) {
TEST_F(BasePcpHandlerTest, TestUpdateAdvertisingOptionsWorks) {
env_.Start();
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::kUseStableEndpointId,
true);
AdvertisingOptions old_options{
{},
true, // auto_upgrade_bandwidth
@@ -61,6 +61,9 @@ constexpr BooleanMediumSelector kTestCases[] = {
BooleanMediumSelector{
.wifi_lan = true,
},
BooleanMediumSelector{
.awdl = true,
},
BooleanMediumSelector{
.bluetooth = true,
.ble = true,
@@ -76,6 +79,7 @@ constexpr BooleanMediumSelector kTestCases[] = {
BooleanMediumSelector{
.bluetooth = true,
.ble = true,
.web_rtc = true,
.wifi_lan = true,
},
};
@@ -264,6 +268,8 @@ class P2pClusterPcpHandlerTestWithParam
config_package_nearby::nearby_connections_feature::
kDisableBluetoothClassicScanning,
is_disable_bluetooth_scanning);
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::kEnableDct, true);
if (advertising_options_.allowed.ble) {
LOG(INFO) << "SetUp: BLE enabled";
}
@@ -276,6 +282,9 @@ class P2pClusterPcpHandlerTestWithParam
if (advertising_options_.allowed.web_rtc) {
LOG(INFO) << "SetUp: WebRTC enabled";
}
if (advertising_options_.allowed.awdl) {
LOG(INFO) << "SetUp: Awdl enabled";
}
LOG(INFO) << "SetUp: ble v2 enabled: " << ble_v2_enabled;
LOG(INFO) << "SetUp: is_disable_bluetooth_scanning: "
<< is_disable_bluetooth_scanning;
@@ -1081,12 +1090,143 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanConnect) {
mediums_b.GetWifi().GetInformation().ip_address_4_bytes);
}
handler_a.StopAdvertising(&client_a_);
handler_b.StopDiscovery(&client_b_);
bwu_a.Shutdown();
bwu_b.Shutdown();
env_.Stop();
}
TEST_P(P2pClusterPcpHandlerTestWithParam, CanConnectWithDctEnabled) {
env_.Start();
ByteArray endpoint_info_a{
"\x22\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b"
"\x54\x65\x73\x74\x20\x64\x65\x76\x69\x63\x65",
29};
ClientProxy client_a;
ClientProxy client_b;
Mediums mediums_a;
Mediums mediums_b;
BluetoothRadio& radio_a = mediums_a.GetBluetoothRadio();
BluetoothRadio& radio_b = mediums_b.GetBluetoothRadio();
radio_a.GetBluetoothAdapter().SetName("BT Device A");
radio_b.GetBluetoothAdapter().SetName("BT Device B");
EndpointChannelManager ecm_a;
EndpointChannelManager ecm_b;
EndpointManager em_a(&ecm_a);
EndpointManager em_b(&ecm_b);
BwuManager bwu_a(mediums_a, em_a, ecm_a, {},
{.allow_upgrade_to = {.bluetooth = true}});
BwuManager bwu_b(mediums_b, em_b, ecm_b, {},
{.allow_upgrade_to = {.bluetooth = true}});
InjectedBluetoothDeviceStore ibds_a;
InjectedBluetoothDeviceStore ibds_b;
P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a);
P2pClusterPcpHandler handler_b(&mediums_b, &em_b, &ecm_b, &bwu_b, ibds_b);
CountDownLatch discover_latch(1);
CountDownLatch connect_latch(2);
struct DiscoveredInfo {
std::string endpoint_id;
ByteArray endpoint_info;
std::string service_id;
} discovered;
EXPECT_EQ(
handler_a.StartAdvertising(
&client_a, service_id_, advertising_options_,
{
.endpoint_info = endpoint_info_a,
.listener =
{
.initiated_cb =
[&connect_latch](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
LOG(INFO)
<< "StartAdvertising: initiated_cb called";
connect_latch.CountDown();
},
},
}),
Status{Status::kSuccess});
EXPECT_EQ(handler_b.StartDiscovery(
&client_b, service_id_, discovery_options_,
{
.endpoint_found_cb =
[&discover_latch, &discovered](
const std::string& endpoint_id,
const ByteArray& endpoint_info,
const std::string& service_id) {
LOG(INFO) << "Device discovered: id=" << endpoint_id
<< ", endpoint_info="
<< std::string{endpoint_info};
discovered = {
.endpoint_id = endpoint_id,
.endpoint_info = endpoint_info,
.service_id = service_id,
};
discover_latch.CountDown();
},
}),
Status{Status::kSuccess});
EXPECT_TRUE(discover_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_EQ(endpoint_info_a, discovered.endpoint_info);
const std::string kBssid = "34:36:3B:C7:8C:71";
const std::int32_t kFreq = 5200;
constexpr char kIp4Bytes[] = {(char)192, (char)168, (char)1, (char)37, 0};
connection_options_.connection_info.supports_5_ghz = true;
connection_options_.connection_info.bssid = kBssid;
connection_options_.connection_info.ap_frequency = kFreq;
connection_options_.connection_info.ip_address.resize(4);
connection_options_.connection_info.ip_address = kIp4Bytes;
client_b.AddCancellationFlag(discovered.endpoint_id);
handler_b.RequestConnection(
&client_b, discovered.endpoint_id,
{.endpoint_info = discovered.endpoint_info,
.listener =
{
.initiated_cb =
[&connect_latch](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
LOG(INFO) << "RequestConnection: initiated_cb called";
connect_latch.CountDown();
},
}},
connection_options_);
std::string client_b_local_endpoint = client_b.GetLocalEndpointId();
EXPECT_TRUE(connect_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_TRUE(client_b.Is5GHzSupported(discovered.endpoint_id));
EXPECT_EQ(client_b.GetBssid(discovered.endpoint_id), kBssid);
EXPECT_EQ(client_b.GetApFrequency(discovered.endpoint_id), kFreq);
EXPECT_EQ(client_b.GetIPAddress(discovered.endpoint_id),
std::string(kIp4Bytes));
// When connection is established, EndpointManager will setup KeepAliveManager
// loop. When it fails, the connection will be dismantled. Since this a unit
// test, KeepAliveManager won't be really up. The disconnection may happen
// before the following check, which cause the check fail. So we check the
// connection status first.
if (client_b.IsConnectedToEndpoint(discovered.endpoint_id)) {
EXPECT_EQ(client_a.Is5GHzSupported(client_b_local_endpoint),
mediums_b.GetWifi().GetCapability().supports_5_ghz);
EXPECT_EQ(client_a.GetBssid(client_b_local_endpoint),
mediums_b.GetWifi().GetInformation().bssid);
EXPECT_EQ(client_a.GetApFrequency(client_b_local_endpoint),
mediums_b.GetWifi().GetInformation().ap_frequency);
EXPECT_EQ(client_a.GetIPAddress(client_b_local_endpoint),
mediums_b.GetWifi().GetInformation().ip_address_4_bytes);
}
handler_a.StopAdvertising(&client_a);
handler_b.StopDiscovery(&client_b);
bwu_a.Shutdown();
bwu_b.Shutdown();
env_.Stop();
}
TEST_P(P2pClusterPcpHandlerTestWithParam,
CanStartListeningForIncomingConnections) {
env_.Start();
@@ -1324,7 +1464,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateAwdlDiscoveryOptions) {
env_.Stop();
}
TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateAwdlAdvertisingOptions) {
TEST_F(P2pClusterPcpHandlerTest, CanUpdateAwdlAdvertisingOptions) {
env_.Start();
std::string endpoint_name{"endpoint_name"};
Mediums mediums_a;
@@ -1333,18 +1473,19 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateAwdlAdvertisingOptions) {
BwuManager bwu_a(mediums_a, em_a, ecm_a, {}, {});
InjectedBluetoothDeviceStore ibds_a;
P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a);
advertising_options_.allowed.wifi_lan = true;
AdvertisingOptions advertising_options{
{Strategy::kP2pCluster, BooleanMediumSelector{}}};
advertising_options.allowed.wifi_lan = true;
EXPECT_EQ(
handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_,
handler_a.StartAdvertising(&client_a_, service_id_, advertising_options,
{.endpoint_info = ByteArray{endpoint_name}}),
Status{Status::kSuccess});
// EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_));
EXPECT_TRUE(mediums_a.GetWifiLan().IsAdvertising(service_id_));
EXPECT_FALSE(mediums_a.GetAwdl().IsAdvertising(service_id_));
advertising_options_.allowed.wifi_lan = false;
advertising_options_.allowed.awdl = true;
advertising_options.allowed.wifi_lan = false;
advertising_options.allowed.awdl = true;
EXPECT_EQ(handler_a.UpdateAdvertisingOptions(&client_a_, service_id_,
advertising_options_),
advertising_options),
Status{Status::kSuccess});
EXPECT_FALSE(mediums_a.GetWifiLan().IsAdvertising(service_id_));
EXPECT_TRUE(mediums_a.GetAwdl().IsAdvertising(service_id_));
@@ -1544,11 +1685,168 @@ TEST_F(P2pClusterPcpHandlerTest, CanConnectToInjectedEndpoint) {
env_.Stop();
}
class P2pLostHandlerTestWithParam : public testing::TestWithParam<bool> {
protected:
void SetUp() override {
LOG(INFO) << "SetUp: begin";
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::kEnableAwdl, true);
env_.SetBleExtendedAdvertisementsAvailable(true);
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::
kDisableBluetoothClassicScanning,
false);
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::kEnableBleV2,
true);
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::kEnableInstantOnLost,
GetParam());
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_connections_feature::
kEnableAdvertisingForInstantOnLost,
true);
}
AdvertisingOptions GetBleOnlyAdvertisingOptions() {
return AdvertisingOptions{
{Strategy::kP2pCluster,
BooleanMediumSelector{
.ble = true,
}},
};
}
DiscoveryOptions GetBleOnlyDiscoveryOptions() {
return DiscoveryOptions{
{Strategy::kP2pCluster,
BooleanMediumSelector{
.ble = true,
}},
};
}
ClientProxy client_a_;
ClientProxy client_b_;
std::string service_id_{"service"};
MediumEnvironment& env_{MediumEnvironment::Instance()};
};
TEST_P(P2pLostHandlerTestWithParam, CanConnectWithInstantLostEnabled) {
env_.Start();
std::string endpoint_name_a{"endpoint_name"};
Mediums mediums_a;
Mediums mediums_b;
BluetoothRadio& radio_a = mediums_a.GetBluetoothRadio();
BluetoothRadio& radio_b = mediums_b.GetBluetoothRadio();
radio_a.GetBluetoothAdapter().SetName("BT Device A");
radio_b.GetBluetoothAdapter().SetName("BT Device B");
EndpointChannelManager ecm_a;
EndpointChannelManager ecm_b;
EndpointManager em_a(&ecm_a);
EndpointManager em_b(&ecm_b);
BwuManager bwu_a(mediums_a, em_a, ecm_a, {},
{.allow_upgrade_to = {.bluetooth = true}});
BwuManager bwu_b(mediums_b, em_b, ecm_b, {},
{.allow_upgrade_to = {.bluetooth = true}});
InjectedBluetoothDeviceStore ibds_a;
InjectedBluetoothDeviceStore ibds_b;
P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a);
P2pClusterPcpHandler handler_b(&mediums_b, &em_b, &ecm_b, &bwu_b, ibds_b);
CountDownLatch discover_latch(1);
CountDownLatch connect_latch(2);
CountDownLatch lost_latch(1);
struct DiscoveredInfo {
std::string endpoint_id;
ByteArray endpoint_info;
std::string service_id;
} discovered;
EXPECT_EQ(handler_b.StartDiscovery(
&client_b_, service_id_, GetBleOnlyDiscoveryOptions(),
{
.endpoint_found_cb =
[&discover_latch, &discovered](
const std::string& endpoint_id,
const ByteArray& endpoint_info,
const std::string& service_id) {
LOG(INFO) << "Device discovered: id=" << endpoint_id
<< ", endpoint_info="
<< std::string{endpoint_info};
discovered = {
.endpoint_id = endpoint_id,
.endpoint_info = endpoint_info,
.service_id = service_id,
};
discover_latch.CountDown();
},
.endpoint_lost_cb =
[&lost_latch](const std::string& endpoint_id) {
LOG(INFO) << "Device lost: id=" << endpoint_id;
lost_latch.CountDown();
},
}),
Status{Status::kSuccess});
EXPECT_EQ(
handler_a.StartAdvertising(
&client_a_, service_id_, GetBleOnlyAdvertisingOptions(),
{
.endpoint_info = ByteArray{endpoint_name_a},
.listener =
{
.initiated_cb =
[&connect_latch](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
LOG(INFO)
<< "StartAdvertising: initiated_cb called";
connect_latch.CountDown();
},
},
}),
Status{Status::kSuccess});
EXPECT_TRUE(discover_latch.Await(absl::Milliseconds(1000)).result());
EXPECT_EQ(endpoint_name_a, std::string{discovered.endpoint_info});
client_b_.AddCancellationFlag(discovered.endpoint_id);
handler_b.RequestConnection(
&client_b_, discovered.endpoint_id,
{.endpoint_info = discovered.endpoint_info,
.listener =
{
.initiated_cb =
[&connect_latch](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
LOG(INFO) << "RequestConnection: initiated_cb called";
connect_latch.CountDown();
},
}},
{});
std::string client_b_local_endpoint = client_b_.GetLocalEndpointId();
EXPECT_TRUE(connect_latch.Await(absl::Milliseconds(1000)).result());
handler_a.StopAdvertising(&client_a_);
EXPECT_TRUE(lost_latch.Await(absl::Milliseconds(3000)).result());
handler_b.StopDiscovery(&client_b_);
bwu_a.Shutdown();
bwu_b.Shutdown();
env_.Stop();
env_.SetBleExtendedAdvertisementsAvailable(false);
}
INSTANTIATE_TEST_SUITE_P(
ParametrisedPcpHandlerTest, P2pClusterPcpHandlerTestWithParam,
::testing::Combine(/*mediums=*/::testing::ValuesIn(kTestCases),
/*ble_v2_enabled=*/::testing::Bool(),
/*disable_bluetooth_scanning=*/::testing::Bool()));
INSTANTIATE_TEST_SUITE_P(ParametrisedP2pLostHandlerTest,
P2pLostHandlerTestWithParam, testing::Bool());
} // namespace
} // namespace connections