From d28ce4d36b250a6ca69e374df61aba0fa1a70c82 Mon Sep 17 00:00:00 2001 From: Mingshiou Wu Date: Sun, 27 Jul 2025 20:06:39 -0700 Subject: [PATCH] Add unit tests cases for implementation/mediums/ble_v2.cc PiperOrigin-RevId: 787826632 --- connections/implementation/mediums/BUILD | 1 + .../implementation/mediums/ble_v2_test.cc | 137 ++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/connections/implementation/mediums/BUILD b/connections/implementation/mediums/BUILD index 7862d371..8f9894e0 100644 --- a/connections/implementation/mediums/BUILD +++ b/connections/implementation/mediums/BUILD @@ -171,6 +171,7 @@ cc_test( "//internal/platform:comm", "//internal/platform:test_util", "//internal/platform:types", + "//internal/platform/implementation:platform", "//internal/platform/implementation:types", "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", diff --git a/connections/implementation/mediums/ble_v2_test.cc b/connections/implementation/mediums/ble_v2_test.cc index 2372023d..f6a04307 100644 --- a/connections/implementation/mediums/ble_v2_test.cc +++ b/connections/implementation/mediums/ble_v2_test.cc @@ -33,6 +33,7 @@ #include "internal/platform/count_down_latch.h" #include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" +#include "internal/platform/implementation/system_clock.h" #include "internal/platform/logging.h" #include "internal/platform/medium_environment.h" @@ -221,6 +222,68 @@ TEST_F(BleV2Test, CanConstructValidObject) { env_.Stop(); } +TEST_F(BleV2Test, CanNotStartAdvertisingWithEmptyAdvertisementBytes) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Enable(); + ByteArray advertisement_bytes; + + EXPECT_FALSE(ble_a.StartAdvertising( + std::string(kServiceIDA), PowerLevel::kHighPower, + BleV2::AdvertisingType::kRegular, advertisement_bytes)); + env_.Stop(); +} + +TEST_F(BleV2Test, CanNotStartAdvertisingWhenRadioNotEnabled) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Disable(); + ByteArray advertisement_bytes((std::string(kAdvertisementString))); + + EXPECT_FALSE(ble_a.StartAdvertising( + std::string(kServiceIDA), PowerLevel::kHighPower, + BleV2::AdvertisingType::kRegular, advertisement_bytes)); + env_.Stop(); +} + +TEST_F(BleV2Test, CanStartAdvertisingWithDifferentPowerLevels) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Enable(); + ByteArray advertisement_bytes((std::string(kAdvertisementString))); + + EXPECT_TRUE(ble_a.StartAdvertising( + std::string(kServiceIDA), PowerLevel::kLowPower, + BleV2::AdvertisingType::kRegular, advertisement_bytes)); + EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); + EXPECT_TRUE(ble_a.StartAdvertising( + std::string(kServiceIDA), PowerLevel::kHighPower, + BleV2::AdvertisingType::kRegular, advertisement_bytes)); + EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); + env_.Stop(); +} + +TEST_F(BleV2Test, CanStartAdvertisingWithDifferentAdvertisingTypes) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Enable(); + ByteArray advertisement_bytes((std::string(kAdvertisementString))); + + EXPECT_TRUE(ble_a.StartAdvertising( + std::string(kServiceIDA), PowerLevel::kHighPower, + BleV2::AdvertisingType::kRegular, advertisement_bytes)); + EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); + EXPECT_TRUE(ble_a.StartAdvertising( + std::string(kServiceIDA), PowerLevel::kHighPower, + BleV2::AdvertisingType::kFast, advertisement_bytes)); + EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); + env_.Stop(); +} + TEST_F(BleV2Test, CanStartFastAdvertising) { env_.Start(); BluetoothRadio radio_a; @@ -357,6 +420,80 @@ TEST_F(BleV2Test, CanStartScanning) { env_.Stop(); } +TEST_F(BleV2Test, CanNotStartScanningWhenRadioNotEnabled) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Disable(); + + EXPECT_FALSE(ble_a.StartScanning( + std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, + /*include_dct_advertisement=*/false, + mediums::DiscoveredPeripheralCallback{})); + env_.Stop(); +} + +TEST_F(BleV2Test, CanNotStartAcceptingConnectionsWhenRadioNotEnabled) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Disable(); + + EXPECT_FALSE(ble_a.StartAcceptingConnections( + std::string(kServiceIDA), + [&](BleV2Socket socket, const std::string&) {})); + env_.Stop(); +} + +TEST_F(BleV2Test, CanStartScanningWithDifferentPowerLevels) { + env_.Start(); + BluetoothRadio radio_a; + BleV2 ble_a(radio_a); + radio_a.Enable(); + + EXPECT_TRUE(ble_a.StartScanning(std::string(kServiceIDA), + Pcp::kP2pPointToPoint, PowerLevel::kLowPower, + /*include_dct_advertisement=*/false, + mediums::DiscoveredPeripheralCallback{})); + EXPECT_TRUE(ble_a.StopScanning(std::string(kServiceIDA))); + EXPECT_TRUE(ble_a.StartScanning(std::string(kServiceIDA), + Pcp::kP2pPointToPoint, PowerLevel::kHighPower, + /*include_dct_advertisement=*/false, + mediums::DiscoveredPeripheralCallback{})); + EXPECT_TRUE(ble_a.StopScanning(std::string(kServiceIDA))); + env_.Stop(); +} + +TEST_F(BleV2Test, CanNotConnectWithInvalidPeripheral) { + env_.Start(); + BluetoothRadio radio_client; + BleV2 ble_client{radio_client}; + radio_client.Enable(); + std::string service_id(kServiceIDA); + BleV2Peripheral invalid_peripheral; + CancellationFlag flag; + + ErrorOr socket_for_client_result = + ble_client.Connect(service_id, invalid_peripheral, &flag); + EXPECT_TRUE(socket_for_client_result.has_error()); + env_.Stop(); +} + +TEST_F(BleV2Test, CanNotConnectWithEmptyServiceId) { + env_.Start(); + BluetoothRadio radio_client; + BleV2 ble_client{radio_client}; + radio_client.Enable(); + std::string empty_service_id; + BleV2Peripheral peripheral; + CancellationFlag flag; + + ErrorOr socket_for_client_result = + ble_client.Connect(empty_service_id, peripheral, &flag); + EXPECT_TRUE(socket_for_client_result.has_error()); + env_.Stop(); +} + TEST_F(BleV2Test, CanStartStopMultipleScanningWithDifferentServiceIds) { env_.Start(); BluetoothRadio radio;