This CL includes additional tests for Ble, LostEntityTracker, BluetoothRadio, WebRtcPeerId, and WifiLan.

PiperOrigin-RevId: 788272883
This commit is contained in:
Mingshiou Wu
2025-07-28 20:31:22 -07:00
committed by Copybara-Service
parent d4edb7663d
commit 164065e0b0
5 changed files with 116 additions and 7 deletions
@@ -275,6 +275,37 @@ TEST_F(BleTest, CanStartAndStopLegacyAdvertising) {
env_.Stop();
}
TEST_F(BleTest, CanStartLegacyAdvertisingWithEmptyServiceUuid) {
env_.Start();
BluetoothRadio radio_a;
Ble ble_a{radio_a};
radio_a.Enable();
std::string service_id(kServiceID);
std::string legacy_service_id(std::string{kServiceID} + "-Legacy");
std::string device_a_endpoint_id{"1A1A"};
EXPECT_TRUE(
ble_a.StartLegacyAdvertising(service_id, device_a_endpoint_id,
/*fast_advertisement_service_uuid=*/""));
EXPECT_FALSE(ble_a.IsAdvertising(service_id));
EXPECT_TRUE(ble_a.IsAdvertising(legacy_service_id));
EXPECT_TRUE(ble_a.StopLegacyAdvertising(service_id));
EXPECT_FALSE(ble_a.IsAdvertising(legacy_service_id));
env_.Stop();
}
TEST_F(BleTest, ConnectWithEmptyServiceId) {
env_.Start();
BluetoothRadio radio_a;
Ble ble_a{radio_a};
radio_a.Enable();
BlePeripheral peripheral;
CancellationFlag flag;
ErrorOr<BleSocket> socket_result =
ble_a.Connect(peripheral, /*service_id=*/"", &flag);
EXPECT_TRUE(socket_result.has_error());
env_.Stop();
}
} // namespace
} // namespace connections
} // namespace nearby
@@ -17,6 +17,7 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "internal/platform/bluetooth_adapter.h"
namespace nearby {
namespace connections {
@@ -45,6 +46,17 @@ TEST(BluetoothRadioTest, CanDisable) {
EXPECT_FALSE(radio.IsEnabled());
}
TEST(BluetoothRadioTest, DestructorRestoresState) {
BluetoothAdapter adapter;
bool initial_state = adapter.IsEnabled();
{
BluetoothRadio radio;
EXPECT_TRUE(radio.IsAdapterValid());
radio.Disable();
}
EXPECT_EQ(initial_state, adapter.IsEnabled());
}
} // namespace
} // namespace connections
} // namespace nearby
@@ -129,6 +129,30 @@ TEST(LostEntityTrackerTest, SameEntityMultipleCopies) {
EXPECT_TRUE(lost_entities.find(entity_1_copy) != lost_entities.end());
}
TEST(LostEntityTrackerTest, LostEntitiesAreClearedAfterComputation) {
LostEntityTracker<TestEntity> lost_entity_tracker;
TestEntity entity_1{1};
TestEntity entity_2{2};
TestEntity entity_3{3};
// Discover some entities.
lost_entity_tracker.RecordFoundEntity(entity_1);
lost_entity_tracker.RecordFoundEntity(entity_2);
lost_entity_tracker.RecordFoundEntity(entity_3);
// Make sure none are lost on the first round.
EXPECT_TRUE(lost_entity_tracker.ComputeLostEntities().empty());
// Go through a round without rediscovering any entities.
typename LostEntityTracker<TestEntity>::EntitySet lost_entities =
lost_entity_tracker.ComputeLostEntities();
EXPECT_EQ(lost_entities.size(), 3);
// Make sure we don't get any more lost entities on the next round if we don't
// discover any more entities.
EXPECT_TRUE(lost_entity_tracker.ComputeLostEntities().empty());
}
} // namespace
} // namespace mediums
} // namespace connections
@@ -14,18 +14,17 @@
#include "connections/implementation/mediums/webrtc_peer_id.h"
#include <memory>
#include <string>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/crypto.h"
namespace nearby {
namespace connections {
namespace mediums {
constexpr char kTestPeerId[] = "this_is_a_test";
TEST(WebrtcPeerIdTest, GenerateRandomPeerId) {
WebrtcPeerId peer_id = WebrtcPeerId::FromRandom();
EXPECT_EQ(64, peer_id.GetId().size());
@@ -45,9 +44,16 @@ TEST(WebrtcPeerIdTest, GenerateFromSeed) {
}
TEST(WebrtcPeerIdTest, GetId) {
const std::string id = "this_is_a_test";
WebrtcPeerId peer_id(id);
EXPECT_EQ(id, peer_id.GetId());
WebrtcPeerId peer_id(kTestPeerId);
EXPECT_EQ(kTestPeerId, peer_id.GetId());
}
TEST(WebrtcPeerIdTest, IsValid) {
WebrtcPeerId peer_id(kTestPeerId);
EXPECT_TRUE(peer_id.IsValid());
WebrtcPeerId peer_id_empty;
EXPECT_FALSE(peer_id_empty.IsValid());
}
} // namespace mediums
@@ -252,6 +252,42 @@ TEST_P(WifiLanTest, CanCancelConnect) {
env_.Stop();
}
TEST_P(WifiLanTest, CanConnectWithIpAddressAndPort) {
FeatureFlags feature_flags = GetParam();
env_.SetFeatureFlags(feature_flags);
env_.Start();
WifiLan wifi_lan_client;
WifiLan wifi_lan_server;
std::string service_id(kServiceID);
CountDownLatch accept_latch(1);
WifiLanSocket socket_for_server;
EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections(
service_id, [&](const std::string& service_id, WifiLanSocket socket) {
socket_for_server = std::move(socket);
accept_latch.CountDown();
}));
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceName(std::string(kServiceInfoName));
EXPECT_TRUE(wifi_lan_server.StartAdvertising(service_id, nsd_service_info));
auto server_credentials = wifi_lan_server.GetCredentials(service_id);
ASSERT_FALSE(server_credentials.first.empty());
ASSERT_NE(server_credentials.second, 0);
CancellationFlag flag;
ErrorOr<WifiLanSocket> socket_for_client_result = wifi_lan_client.Connect(
service_id, server_credentials.first, server_credentials.second, &flag);
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
EXPECT_TRUE(wifi_lan_server.StopAcceptingConnections(service_id));
EXPECT_TRUE(wifi_lan_server.StopAdvertising(service_id));
EXPECT_TRUE(socket_for_server.IsValid());
ASSERT_TRUE(socket_for_client_result.has_value());
EXPECT_TRUE(socket_for_client_result.value().IsValid());
env_.Stop();
}
INSTANTIATE_TEST_SUITE_P(ParametrisedWifiLanTest, WifiLanTest,
::testing::ValuesIn(kTestCases));