| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "base/strings/utf_string_conversions.h" | |
| 18 #include "base/time/time.h" | |
| 19 #include "components/cryptauth/connection.h" | |
| 20 #include "components/cryptauth/cryptauth_test_util.h" | |
| 21 #include "components/cryptauth/fake_connection.h" | |
| 22 #include "components/cryptauth/remote_device.h" | |
| 23 #include "components/cryptauth/wire_message.h" | |
| 24 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | |
| 25 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 26 #include "device/bluetooth/bluetooth_uuid.h" | |
| 27 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
| 28 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
| 29 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | |
| 30 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
| 31 #include "testing/gmock/include/gmock/gmock.h" | |
| 32 #include "testing/gtest/include/gtest/gtest.h" | |
| 33 | |
| 34 using testing::_; | |
| 35 using testing::AtLeast; | |
| 36 using testing::NiceMock; | |
| 37 using testing::Return; | |
| 38 using testing::StrictMock; | |
| 39 using testing::SaveArg; | |
| 40 | |
| 41 using device::BluetoothDevice; | |
| 42 | |
| 43 namespace proximity_auth { | |
| 44 namespace { | |
| 45 | |
| 46 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | |
| 47 const char kOtherUUID[] = "AAAAAAAA-AAAA-AAAA-AAAA-D15EA5EBEEEF"; | |
| 48 | |
| 49 const int kMaxNumberOfAttempts = 2; | |
| 50 | |
| 51 class MockBluetoothLowEnergyDeviceWhitelist | |
| 52 : public BluetoothLowEnergyDeviceWhitelist { | |
| 53 public: | |
| 54 MockBluetoothLowEnergyDeviceWhitelist() | |
| 55 : BluetoothLowEnergyDeviceWhitelist(nullptr) {} | |
| 56 ~MockBluetoothLowEnergyDeviceWhitelist() override {} | |
| 57 | |
| 58 MOCK_CONST_METHOD1(HasDeviceWithAddress, bool(const std::string&)); | |
| 59 }; | |
| 60 | |
| 61 class MockBluetoothLowEnergyConnectionFinder | |
| 62 : public BluetoothLowEnergyConnectionFinder { | |
| 63 public: | |
| 64 MockBluetoothLowEnergyConnectionFinder( | |
| 65 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, | |
| 66 FinderStrategy finder_strategy) | |
| 67 : BluetoothLowEnergyConnectionFinder( | |
| 68 cryptauth::CreateLERemoteDeviceForTest(), | |
| 69 kServiceUUID, | |
| 70 finder_strategy, | |
| 71 device_whitelist, | |
| 72 nullptr, | |
| 73 kMaxNumberOfAttempts) {} | |
| 74 | |
| 75 ~MockBluetoothLowEnergyConnectionFinder() override {} | |
| 76 | |
| 77 // Mock methods don't support return type std::unique_ptr<>. This is a | |
| 78 // possible workaround: mock a proxy method to be called by the target | |
| 79 // overridden method (CreateConnection). | |
| 80 MOCK_METHOD0(CreateConnectionProxy, cryptauth::Connection*()); | |
| 81 | |
| 82 // Creates a mock connection and sets an expectation that the mock connection | |
| 83 // finder's CreateConnection() method will be called and will return the | |
| 84 // created connection. Returns a reference to the created connection. | |
| 85 // NOTE: The returned connection's lifetime is managed by the connection | |
| 86 // finder. | |
| 87 cryptauth::FakeConnection* ExpectCreateConnection() { | |
| 88 std::unique_ptr<cryptauth::FakeConnection> connection( | |
| 89 new cryptauth::FakeConnection( | |
| 90 cryptauth::CreateLERemoteDeviceForTest())); | |
| 91 cryptauth::FakeConnection* connection_alias = connection.get(); | |
| 92 EXPECT_CALL(*this, CreateConnectionProxy()) | |
| 93 .WillOnce(Return(connection.release())); | |
| 94 return connection_alias; | |
| 95 } | |
| 96 | |
| 97 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); | |
| 98 | |
| 99 protected: | |
| 100 std::unique_ptr<cryptauth::Connection> CreateConnection( | |
| 101 const std::string& device_address) override { | |
| 102 return base::WrapUnique(CreateConnectionProxy()); | |
| 103 } | |
| 104 | |
| 105 private: | |
| 106 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); | |
| 107 }; | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 class ProximityAuthBluetoothLowEnergyConnectionFinderTest | |
| 112 : public testing::Test { | |
| 113 protected: | |
| 114 ProximityAuthBluetoothLowEnergyConnectionFinderTest() | |
| 115 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | |
| 116 connection_callback_( | |
| 117 base::Bind(&ProximityAuthBluetoothLowEnergyConnectionFinderTest:: | |
| 118 OnConnectionFound, | |
| 119 base::Unretained(this))), | |
| 120 device_(new NiceMock<device::MockBluetoothDevice>( | |
| 121 adapter_.get(), | |
| 122 0, | |
| 123 cryptauth::kTestRemoteDeviceName, | |
| 124 cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 125 false, | |
| 126 false)), | |
| 127 device_whitelist_(new MockBluetoothLowEnergyDeviceWhitelist()), | |
| 128 last_discovery_session_alias_(nullptr) { | |
| 129 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
| 130 | |
| 131 std::vector<const device::BluetoothDevice*> devices; | |
| 132 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | |
| 133 | |
| 134 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | |
| 135 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | |
| 136 | |
| 137 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) | |
| 138 .WillByDefault(Return(false)); | |
| 139 } | |
| 140 | |
| 141 void OnConnectionFound(std::unique_ptr<cryptauth::Connection> connection) { | |
| 142 last_found_connection_ = std::move(connection); | |
| 143 } | |
| 144 | |
| 145 void FindAndExpectStartDiscovery( | |
| 146 BluetoothLowEnergyConnectionFinder& connection_finder) { | |
| 147 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | |
| 148 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( | |
| 149 new NiceMock<device::MockBluetoothDiscoverySession>()); | |
| 150 last_discovery_session_alias_ = discovery_session.get(); | |
| 151 | |
| 152 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for | |
| 153 // StartDiscoveryWithFilter. | |
| 154 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | |
| 155 .WillOnce(SaveArg<1>(&discovery_callback)); | |
| 156 EXPECT_CALL(*adapter_, AddObserver(_)); | |
| 157 ON_CALL(*last_discovery_session_alias_, IsActive()) | |
| 158 .WillByDefault(Return(true)); | |
| 159 connection_finder.Find(connection_callback_); | |
| 160 ASSERT_FALSE(discovery_callback.is_null()); | |
| 161 discovery_callback.Run(std::move(discovery_session)); | |
| 162 } | |
| 163 | |
| 164 void ExpectRemoveObserver() { | |
| 165 EXPECT_CALL(*adapter_, RemoveObserver(_)).Times(AtLeast(1)); | |
| 166 } | |
| 167 | |
| 168 // Prepare |device_| with |uuid|. | |
| 169 void PrepareDevice(const std::string& uuid, | |
| 170 const std::string& address, | |
| 171 bool paired) { | |
| 172 BluetoothDevice::UUIDSet uuids = {device::BluetoothUUID(uuid)}; | |
| 173 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); | |
| 174 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); | |
| 175 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); | |
| 176 } | |
| 177 | |
| 178 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
| 179 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; | |
| 180 std::unique_ptr<device::MockBluetoothDevice> device_; | |
| 181 std::unique_ptr<cryptauth::Connection> last_found_connection_; | |
| 182 std::unique_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_; | |
| 183 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; | |
| 184 | |
| 185 private: | |
| 186 base::MessageLoop message_loop_; | |
| 187 }; | |
| 188 | |
| 189 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 190 ConstructAndDestroyDoesntCrash) { | |
| 191 // Destroying a BluetoothConnectionFinder for which Find() has not been called | |
| 192 // should not crash. | |
| 193 BluetoothLowEnergyConnectionFinder connection_finder( | |
| 194 cryptauth::CreateLERemoteDeviceForTest(), kServiceUUID, | |
| 195 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, | |
| 196 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); | |
| 197 } | |
| 198 | |
| 199 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 200 Find_StartsDiscoverySession) { | |
| 201 BluetoothLowEnergyConnectionFinder connection_finder( | |
| 202 cryptauth::CreateLERemoteDeviceForTest(), kServiceUUID, | |
| 203 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, | |
| 204 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); | |
| 205 | |
| 206 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)); | |
| 207 EXPECT_CALL(*adapter_, AddObserver(_)); | |
| 208 connection_finder.Find(connection_callback_); | |
| 209 } | |
| 210 | |
| 211 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 212 Find_StopsDiscoverySessionBeforeDestroying) { | |
| 213 BluetoothLowEnergyConnectionFinder connection_finder( | |
| 214 cryptauth::CreateLERemoteDeviceForTest(), kServiceUUID, | |
| 215 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, | |
| 216 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); | |
| 217 | |
| 218 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | |
| 219 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( | |
| 220 new NiceMock<device::MockBluetoothDiscoverySession>()); | |
| 221 device::MockBluetoothDiscoverySession* discovery_session_alias = | |
| 222 discovery_session.get(); | |
| 223 | |
| 224 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | |
| 225 .WillOnce(SaveArg<1>(&discovery_callback)); | |
| 226 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); | |
| 227 EXPECT_CALL(*adapter_, AddObserver(_)); | |
| 228 connection_finder.Find(connection_callback_); | |
| 229 | |
| 230 ASSERT_FALSE(discovery_callback.is_null()); | |
| 231 discovery_callback.Run(std::move(discovery_session)); | |
| 232 | |
| 233 EXPECT_CALL(*adapter_, RemoveObserver(_)); | |
| 234 } | |
| 235 | |
| 236 // TODO(sacomoto): Remove it when ProximityAuthBleSystem is not needed anymore. | |
| 237 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 238 Find_CreatesConnectionWhenWhitelistedDeviceIsAdded) { | |
| 239 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 240 device_whitelist_.get(), | |
| 241 BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 242 FindAndExpectStartDiscovery(connection_finder); | |
| 243 ExpectRemoveObserver(); | |
| 244 | |
| 245 BluetoothDevice::UUIDSet uuids; | |
| 246 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); | |
| 247 ON_CALL(*device_, IsPaired()).WillByDefault(Return(true)); | |
| 248 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) | |
| 249 .WillByDefault(Return(true)); | |
| 250 | |
| 251 connection_finder.ExpectCreateConnection(); | |
| 252 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 253 } | |
| 254 | |
| 255 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 256 Find_CreatesConnectionWhenRightDeviceIsAdded_NoPublicAddress) { | |
| 257 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 258 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 259 | |
| 260 FindAndExpectStartDiscovery(connection_finder); | |
| 261 ExpectRemoveObserver(); | |
| 262 | |
| 263 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 264 false); | |
| 265 ON_CALL(*device_, GetName()) | |
| 266 .WillByDefault(Return(std::string(cryptauth::kTestRemoteDeviceName))); | |
| 267 | |
| 268 connection_finder.ExpectCreateConnection(); | |
| 269 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 270 } | |
| 271 | |
| 272 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 273 Find_DoesntCreatesConnectionWhenWrongDeviceIsAdded_NoPublicAddress) { | |
| 274 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 275 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 276 | |
| 277 FindAndExpectStartDiscovery(connection_finder); | |
| 278 ExpectRemoveObserver(); | |
| 279 | |
| 280 PrepareDevice(kOtherUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 281 false); | |
| 282 ON_CALL(*device_, GetName()).WillByDefault(Return(std::string("Other name"))); | |
| 283 | |
| 284 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 285 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 286 } | |
| 287 | |
| 288 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 289 Find_CreatesConnectionWhenRightDeviceIsAdded_HasPublicAddress) { | |
| 290 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 291 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 292 | |
| 293 FindAndExpectStartDiscovery(connection_finder); | |
| 294 ExpectRemoveObserver(); | |
| 295 | |
| 296 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 297 true); | |
| 298 connection_finder.ExpectCreateConnection(); | |
| 299 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 300 } | |
| 301 | |
| 302 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 303 Find_DoesntCreateConnectionWhenWrongDeviceIsAdded_HasPublicAddress) { | |
| 304 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 305 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 306 FindAndExpectStartDiscovery(connection_finder); | |
| 307 ExpectRemoveObserver(); | |
| 308 | |
| 309 PrepareDevice(kOtherUUID, "", true); | |
| 310 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 311 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 312 } | |
| 313 | |
| 314 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 315 Find_CreatesConnectionWhenRightDeviceIsChanged_HasPublicAddress) { | |
| 316 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 317 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 318 | |
| 319 FindAndExpectStartDiscovery(connection_finder); | |
| 320 ExpectRemoveObserver(); | |
| 321 | |
| 322 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 323 true); | |
| 324 connection_finder.ExpectCreateConnection(); | |
| 325 connection_finder.DeviceChanged(adapter_.get(), device_.get()); | |
| 326 } | |
| 327 | |
| 328 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 329 Find_DoesntCreateConnectionWhenWrongDeviceIsChanged_HasPublicAddress) { | |
| 330 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 331 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 332 | |
| 333 FindAndExpectStartDiscovery(connection_finder); | |
| 334 ExpectRemoveObserver(); | |
| 335 | |
| 336 PrepareDevice(kOtherUUID, "", true); | |
| 337 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | |
| 338 connection_finder.DeviceChanged(adapter_.get(), device_.get()); | |
| 339 } | |
| 340 | |
| 341 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 342 Find_CreatesOnlyOneConnection) { | |
| 343 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 344 nullptr, BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | |
| 345 FindAndExpectStartDiscovery(connection_finder); | |
| 346 ExpectRemoveObserver(); | |
| 347 | |
| 348 // Prepare to add |device_|. | |
| 349 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 350 true); | |
| 351 | |
| 352 // Prepare to add |other_device|. | |
| 353 NiceMock<device::MockBluetoothDevice> other_device( | |
| 354 adapter_.get(), 0, cryptauth::kTestRemoteDeviceName, | |
| 355 cryptauth::kTestRemoteDeviceBluetoothAddress, false, false); | |
| 356 BluetoothDevice::UUIDSet uuids = {device::BluetoothUUID(kServiceUUID)}; | |
| 357 ON_CALL(other_device, GetAddress()) | |
| 358 .WillByDefault(Return(cryptauth::kTestRemoteDeviceBluetoothAddress)); | |
| 359 ON_CALL(other_device, IsPaired()).WillByDefault(Return(true)); | |
| 360 ON_CALL(other_device, GetUUIDs()).WillByDefault((Return(uuids))); | |
| 361 | |
| 362 // Only one connection should be created. | |
| 363 connection_finder.ExpectCreateConnection(); | |
| 364 | |
| 365 // Add the devices. | |
| 366 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 367 connection_finder.DeviceAdded(adapter_.get(), &other_device); | |
| 368 } | |
| 369 | |
| 370 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 371 Find_ConnectionSucceeds_WithRemoteDevice) { | |
| 372 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 373 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); | |
| 374 // Starting discovery. | |
| 375 FindAndExpectStartDiscovery(connection_finder); | |
| 376 ExpectRemoveObserver(); | |
| 377 | |
| 378 // Finding and creating a connection to the right device. | |
| 379 cryptauth::FakeConnection* connection = | |
| 380 connection_finder.ExpectCreateConnection(); | |
| 381 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 382 true); | |
| 383 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 384 | |
| 385 // Creating a connection. | |
| 386 base::RunLoop run_loop; | |
| 387 EXPECT_FALSE(last_found_connection_); | |
| 388 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); | |
| 389 connection->SetStatus(cryptauth::Connection::CONNECTED); | |
| 390 run_loop.RunUntilIdle(); | |
| 391 EXPECT_TRUE(last_found_connection_); | |
| 392 } | |
| 393 | |
| 394 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 395 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) { | |
| 396 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 397 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); | |
| 398 | |
| 399 // Starting discovery. | |
| 400 FindAndExpectStartDiscovery(connection_finder); | |
| 401 | |
| 402 // Preparing to create a GATT connection to the right device. | |
| 403 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 404 true); | |
| 405 cryptauth::FakeConnection* connection = | |
| 406 connection_finder.ExpectCreateConnection(); | |
| 407 | |
| 408 // Trying to create a connection. | |
| 409 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 410 ASSERT_FALSE(last_found_connection_); | |
| 411 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); | |
| 412 | |
| 413 // Preparing to restart the discovery session. | |
| 414 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | |
| 415 std::vector<const device::BluetoothDevice*> devices; | |
| 416 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | |
| 417 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | |
| 418 .WillOnce(SaveArg<1>(&discovery_callback)); | |
| 419 | |
| 420 // Connection fails. | |
| 421 { | |
| 422 base::RunLoop run_loop; | |
| 423 connection->SetStatus(cryptauth::Connection::DISCONNECTED); | |
| 424 run_loop.RunUntilIdle(); | |
| 425 } | |
| 426 | |
| 427 // Restarting the discovery session. | |
| 428 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( | |
| 429 new NiceMock<device::MockBluetoothDiscoverySession>()); | |
| 430 last_discovery_session_alias_ = discovery_session.get(); | |
| 431 ON_CALL(*last_discovery_session_alias_, IsActive()) | |
| 432 .WillByDefault(Return(true)); | |
| 433 ASSERT_FALSE(discovery_callback.is_null()); | |
| 434 discovery_callback.Run(std::move(discovery_session)); | |
| 435 | |
| 436 // Preparing to create a GATT connection to the right device. | |
| 437 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 438 true); | |
| 439 connection = connection_finder.ExpectCreateConnection(); | |
| 440 | |
| 441 // Trying to create a connection. | |
| 442 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 443 | |
| 444 // Completing the connection. | |
| 445 { | |
| 446 base::RunLoop run_loop; | |
| 447 EXPECT_FALSE(last_found_connection_); | |
| 448 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); | |
| 449 connection->SetStatus(cryptauth::Connection::CONNECTED); | |
| 450 run_loop.RunUntilIdle(); | |
| 451 } | |
| 452 EXPECT_TRUE(last_found_connection_); | |
| 453 } | |
| 454 | |
| 455 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | |
| 456 Find_AdapterRemoved_RestartDiscoveryAndConnectionSucceeds) { | |
| 457 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | |
| 458 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); | |
| 459 | |
| 460 // Starting discovery. | |
| 461 FindAndExpectStartDiscovery(connection_finder); | |
| 462 | |
| 463 // Removing the adapter. | |
| 464 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | |
| 465 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); | |
| 466 ON_CALL(*last_discovery_session_alias_, IsActive()) | |
| 467 .WillByDefault(Return(false)); | |
| 468 connection_finder.AdapterPoweredChanged(adapter_.get(), false); | |
| 469 connection_finder.AdapterPresentChanged(adapter_.get(), false); | |
| 470 | |
| 471 // Adding the adapter. | |
| 472 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | |
| 473 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | |
| 474 | |
| 475 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | |
| 476 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( | |
| 477 new NiceMock<device::MockBluetoothDiscoverySession>()); | |
| 478 last_discovery_session_alias_ = discovery_session.get(); | |
| 479 | |
| 480 // Restarting the discovery session. | |
| 481 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | |
| 482 .WillOnce(SaveArg<1>(&discovery_callback)); | |
| 483 connection_finder.AdapterPresentChanged(adapter_.get(), true); | |
| 484 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | |
| 485 ON_CALL(*last_discovery_session_alias_, IsActive()) | |
| 486 .WillByDefault(Return(true)); | |
| 487 | |
| 488 ASSERT_FALSE(discovery_callback.is_null()); | |
| 489 discovery_callback.Run(std::move(discovery_session)); | |
| 490 | |
| 491 // Preparing to create a GATT connection to the right device. | |
| 492 PrepareDevice(kServiceUUID, cryptauth::kTestRemoteDeviceBluetoothAddress, | |
| 493 true); | |
| 494 cryptauth::FakeConnection* connection = | |
| 495 connection_finder.ExpectCreateConnection(); | |
| 496 | |
| 497 // Trying to create a connection. | |
| 498 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | |
| 499 | |
| 500 // Completing the connection. | |
| 501 base::RunLoop run_loop; | |
| 502 ASSERT_FALSE(last_found_connection_); | |
| 503 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); | |
| 504 connection->SetStatus(cryptauth::Connection::CONNECTED); | |
| 505 run_loop.RunUntilIdle(); | |
| 506 EXPECT_TRUE(last_found_connection_); | |
| 507 } | |
| 508 | |
| 509 } // namespace proximity_auth | |
| OLD | NEW |