1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/quick_pair/scanning/fast_pair/fast_pair_scanner_impl.h"
#include <algorithm>
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/quick_pair/common/constants.h"
#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/fake_bluetooth_adapter.h"
#include "ash/quick_pair/common/protocol.h"
#include "ash/quick_pair/fast_pair_handshake/fake_fast_pair_handshake.h"
#include "ash/quick_pair/fast_pair_handshake/fake_fast_pair_handshake_lookup.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_gatt_service_client.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake_lookup.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_low_energy_scan_filter.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "device/bluetooth/test/mock_bluetooth_low_energy_scan_session.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// Below constants are used to construct MockBluetoothDevice for testing.
constexpr char kTestBleDeviceAddress1[] = "11:12:13:14:15:16";
constexpr char kTestBleDeviceName[] = "Test Device Name";
std::unique_ptr<device::MockBluetoothDevice> CreateTestBluetoothDevice(
const std::string& address) {
auto mock_device =
std::make_unique<testing::NiceMock<device::MockBluetoothDevice>>(
/*adapter=*/nullptr, /*bluetooth_class=*/0, kTestBleDeviceName,
address, /*paired=*/true, /*connected=*/true);
mock_device->AddUUID(ash::quick_pair::kFastPairBluetoothUuid);
mock_device->SetServiceDataForUUID(ash::quick_pair::kFastPairBluetoothUuid,
{1, 2, 3});
return mock_device;
}
class FastPairScannerObserver
: public ash::quick_pair::FastPairScanner::Observer {
public:
// FastPairScanner::Observer overrides
void OnDeviceFound(device::BluetoothDevice* device) override {
device_addreses_.push_back(device->GetAddress());
on_device_found_count_++;
}
void OnDeviceLost(device::BluetoothDevice* device) override {
device_addreses_.erase(
std::ranges::find(device_addreses_, device->GetAddress()));
}
bool DoesDeviceListContainTestDevice(const std::string& address) {
return base::Contains(device_addreses_, address);
}
int on_device_found_count() { return on_device_found_count_; }
private:
std::vector<std::string> device_addreses_;
int on_device_found_count_ = 0;
};
} // namespace
namespace ash {
namespace quick_pair {
class FastPairScannerImplTest : public testing::Test {
public:
void SetUp() override {
adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
ON_CALL(*adapter_, StartLowEnergyScanSession(testing::_, testing::_))
.WillByDefault(
Invoke(this, &FastPairScannerImplTest::StartLowEnergyScanSession));
device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
FastPairHandshakeLookup::UseFakeInstance();
scanner_ = base::MakeRefCounted<FastPairScannerImpl>();
scanner_observer_ = std::make_unique<FastPairScannerObserver>();
scanner().AddObserver(scanner_observer_.get());
task_environment_.RunUntilIdle();
}
void TearDown() override {
scanner().RemoveObserver(scanner_observer_.get());
FastPairHandshakeLookup::GetInstance()->Clear();
}
std::unique_ptr<device::BluetoothLowEnergyScanSession>
StartLowEnergyScanSession(
std::unique_ptr<device::BluetoothLowEnergyScanFilter> filter,
base::WeakPtr<device::BluetoothLowEnergyScanSession::Delegate> delegate) {
auto mock_scan_session =
std::make_unique<device::MockBluetoothLowEnergyScanSession>(
base::BindOnce(&FastPairScannerImplTest::OnScanSessionDestroyed,
weak_ptr_factory_.GetWeakPtr()));
mock_scan_session_ = mock_scan_session.get();
delegate_ = delegate;
return mock_scan_session;
}
void OnScanSessionDestroyed() {
delegate_ = nullptr;
mock_scan_session_ = nullptr;
}
FakeBluetoothAdapter& adapter() { return *(adapter_.get()); }
device::MockBluetoothLowEnergyScanSession* scan_session() {
return mock_scan_session_;
}
FastPairScannerObserver& scanner_observer() {
return *(scanner_observer_.get());
}
FastPairScanner& scanner() { return *(scanner_.get()); }
void TriggerOnDeviceFound(const std::string& address) {
if (!delegate_)
return;
delegate_->OnDeviceFound(mock_scan_session_,
CreateTestBluetoothDevice(address).get());
}
void TriggerOnDeviceLost(const std::string& address) {
if (!delegate_)
return;
delegate_->OnDeviceLost(mock_scan_session_,
CreateTestBluetoothDevice(address).get());
}
void AddConnectedHandshake(const std::string& address) {
FastPairHandshakeLookup::GetInstance()->Create(
adapter_,
base::MakeRefCounted<Device>("", address, Protocol::kFastPairInitial),
base::DoNothing());
}
std::unique_ptr<FastPairHandshake> CreateConnectedHandshake(
scoped_refptr<Device> device,
FastPairHandshakeLookup::OnCompleteCallback callback) {
return std::make_unique<FakeFastPairHandshake>(adapter_, std::move(device),
std::move(callback));
}
void SetUpFactoryScanner() {
scanner_.reset();
scanner_ = FastPairScannerImpl::Factory::Create();
scanner_->AddObserver(scanner_observer_.get());
task_environment_.RunUntilIdle();
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
scoped_refptr<FakeBluetoothAdapter> adapter_;
scoped_refptr<FastPairScanner> scanner_;
raw_ptr<device::MockBluetoothLowEnergyScanSession> mock_scan_session_ =
nullptr;
std::unique_ptr<FastPairScannerObserver> scanner_observer_;
base::WeakPtr<device::BluetoothLowEnergyScanSession::Delegate> delegate_;
base::WeakPtrFactory<FastPairScannerImplTest> weak_ptr_factory_{this};
};
TEST_F(FastPairScannerImplTest, FactoryCreate) {
SetUpFactoryScanner();
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_TRUE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, SessionStartedSuccessfully) {
delegate_->OnSessionStarted(mock_scan_session_,
/*error_code=*/std::nullopt);
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_TRUE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, SessionStartedFailure) {
delegate_->OnSessionStarted(
mock_scan_session_,
device::BluetoothLowEnergyScanSession::ErrorCode::kFailed);
delegate_ = nullptr;
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_FALSE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, SessionInvalidated) {
delegate_->OnSessionInvalidated(mock_scan_session_);
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_FALSE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, DeviceAddedNotifiesObservers) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_TRUE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, DeviceRemoved) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
auto mock_device = CreateTestBluetoothDevice(kTestBleDeviceAddress1);
auto* mock_device_ptr = mock_device.get();
mock_device->SetServiceDataForUUID(ash::quick_pair::kFastPairBluetoothUuid,
{4, 5, 6});
adapter().NotifyDeviceRemoved(mock_device_ptr);
adapter().NotifyDeviceChanged(mock_device_ptr);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
}
TEST_F(FastPairScannerImplTest, DevicePairedChanged) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
auto mock_device = CreateTestBluetoothDevice(kTestBleDeviceAddress1);
auto* mock_device_ptr = mock_device.get();
mock_device->SetServiceDataForUUID(ash::quick_pair::kFastPairBluetoothUuid,
{4, 5, 6});
adapter().NotifyDevicePairedChanged(mock_device_ptr, false);
adapter().NotifyDeviceChanged(mock_device_ptr);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
}
TEST_F(FastPairScannerImplTest, DeviceAddedAlreadyHasHandshake) {
AddConnectedHandshake(kTestBleDeviceAddress1);
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_FALSE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, DeviceLostNotifiesObservers) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_TRUE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
TriggerOnDeviceLost(kTestBleDeviceAddress1);
EXPECT_FALSE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
}
TEST_F(FastPairScannerImplTest, DeviceChangedNewServiceDataLength) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
auto mock_device = CreateTestBluetoothDevice(kTestBleDeviceAddress1);
auto* mock_device_ptr = mock_device.get();
// The length of the service data changes between Initial/Subsequent pairing
// which is used to detect if we should trigger OnDeviceFound or not.
mock_device->SetServiceDataForUUID(ash::quick_pair::kFastPairBluetoothUuid,
{4, 5, 6, 7});
adapter().NotifyDeviceChanged(mock_device_ptr);
EXPECT_EQ(scanner_observer().on_device_found_count(), 2);
}
TEST_F(FastPairScannerImplTest, DeviceChangedSameServiceDataLength) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
auto mock_device = CreateTestBluetoothDevice(kTestBleDeviceAddress1);
auto* mock_device_ptr = mock_device.get();
mock_device->SetServiceDataForUUID(ash::quick_pair::kFastPairBluetoothUuid,
{4, 5, 6});
// This simulates a change of service data within one of the ongoing pairing
// scenarios, in which case we do not notify observers.
adapter().NotifyDeviceChanged(mock_device_ptr);
EXPECT_EQ(scanner_observer().on_device_found_count(), 1);
}
TEST_F(FastPairScannerImplTest, DeviceAddedNoServiceData) {
auto mock_device =
std::make_unique<testing::NiceMock<device::MockBluetoothDevice>>(
/*adapter=*/nullptr, /*bluetooth_class=*/0, kTestBleDeviceName,
kTestBleDeviceAddress1, /*paired=*/true, /*connected=*/true);
delegate_->OnDeviceFound(mock_scan_session_, mock_device.get());
EXPECT_EQ(scanner_observer().on_device_found_count(), 0);
}
TEST_F(FastPairScannerImplTest, DeviceChangedNoServiceData) {
auto mock_device =
std::make_unique<testing::NiceMock<device::MockBluetoothDevice>>(
/*adapter=*/nullptr, /*bluetooth_class=*/0, kTestBleDeviceName,
kTestBleDeviceAddress1, /*paired=*/true, /*connected=*/true);
delegate_->OnDeviceFound(mock_scan_session_, mock_device.get());
EXPECT_EQ(scanner_observer().on_device_found_count(), 0);
auto* mock_device_ptr = mock_device.get();
mock_device->SetServiceDataForUUID(ash::quick_pair::kFastPairBluetoothUuid,
{4, 5, 6});
adapter().NotifyDeviceChanged(mock_device_ptr);
EXPECT_EQ(scanner_observer().on_device_found_count(), 0);
}
TEST_F(FastPairScannerImplTest, IgnoresEventDuringActiveHandshake) {
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_TRUE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
AddConnectedHandshake(kTestBleDeviceAddress1);
TriggerOnDeviceLost(kTestBleDeviceAddress1);
EXPECT_FALSE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_TRUE(scanner_observer().DoesDeviceListContainTestDevice(
kTestBleDeviceAddress1));
EXPECT_EQ(scanner_observer().on_device_found_count(), 2);
}
TEST_F(FastPairScannerImplTest, NoNotifyForPairedDevice) {
auto paired_device = base::MakeRefCounted<Device>(
"test_metadata_id", kTestBleDeviceAddress1, Protocol::kFastPairInitial);
paired_device->set_classic_address(kTestBleDeviceAddress1);
auto mock_device =
std::make_unique<testing::NiceMock<device::MockBluetoothDevice>>(
/*adapter=*/nullptr, /*bluetooth_class=*/0, kTestBleDeviceName,
kTestBleDeviceAddress1, /*paired=*/true, /*connected=*/true);
adapter_->AddMockDevice(std::move(mock_device));
scanner_->OnDevicePaired(paired_device);
TriggerOnDeviceFound(kTestBleDeviceAddress1);
EXPECT_EQ(scanner_observer().on_device_found_count(), 0);
}
} // namespace quick_pair
} // namespace ash
|