| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | 16 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" |
| 17 #include "components/proximity_auth/connection.h" | 17 #include "components/proximity_auth/fake_connection.h" |
| 18 #include "components/proximity_auth/remote_device.h" | 18 #include "components/proximity_auth/remote_device.h" |
| 19 #include "components/proximity_auth/wire_message.h" | 19 #include "components/proximity_auth/wire_message.h" |
| 20 #include "device/bluetooth/bluetooth_adapter_factory.h" | 20 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 21 #include "device/bluetooth/bluetooth_uuid.h" | 21 #include "device/bluetooth/bluetooth_uuid.h" |
| 22 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 22 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 23 #include "device/bluetooth/test/mock_bluetooth_device.h" | 23 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 24 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 24 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| 25 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 25 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 44 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | 44 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; |
| 45 const char kOtherUUID[] = "AAAAAAAA-AAAA-AAAA-AAAA-D15EA5EBEEEF"; | 45 const char kOtherUUID[] = "AAAAAAAA-AAAA-AAAA-AAAA-D15EA5EBEEEF"; |
| 46 | 46 |
| 47 const int kMaxNumberOfAttempts = 2; | 47 const int kMaxNumberOfAttempts = 2; |
| 48 | 48 |
| 49 RemoteDevice CreateRemoteDevice() { | 49 RemoteDevice CreateRemoteDevice() { |
| 50 return RemoteDevice(kDeviceName, kPublicKey, kBluetoothAddress, | 50 return RemoteDevice(kDeviceName, kPublicKey, kBluetoothAddress, |
| 51 kPersistentSymmetricKey); | 51 kPersistentSymmetricKey); |
| 52 } | 52 } |
| 53 | 53 |
| 54 class MockConnection : public Connection { | |
| 55 public: | |
| 56 MockConnection() : Connection(CreateRemoteDevice()) {} | |
| 57 ~MockConnection() override {} | |
| 58 | |
| 59 MOCK_METHOD0(Connect, void()); | |
| 60 | |
| 61 using Connection::SetStatus; | |
| 62 | |
| 63 private: | |
| 64 void Disconnect() override {} | |
| 65 void SendMessageImpl(scoped_ptr<WireMessage> message) override {} | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(MockConnection); | |
| 68 }; | |
| 69 | |
| 70 class MockBluetoothLowEnergyDeviceWhitelist | 54 class MockBluetoothLowEnergyDeviceWhitelist |
| 71 : public BluetoothLowEnergyDeviceWhitelist { | 55 : public BluetoothLowEnergyDeviceWhitelist { |
| 72 public: | 56 public: |
| 73 MockBluetoothLowEnergyDeviceWhitelist() | 57 MockBluetoothLowEnergyDeviceWhitelist() |
| 74 : BluetoothLowEnergyDeviceWhitelist(nullptr) {} | 58 : BluetoothLowEnergyDeviceWhitelist(nullptr) {} |
| 75 ~MockBluetoothLowEnergyDeviceWhitelist() override {} | 59 ~MockBluetoothLowEnergyDeviceWhitelist() override {} |
| 76 | 60 |
| 77 MOCK_CONST_METHOD1(HasDeviceWithAddress, bool(const std::string&)); | 61 MOCK_CONST_METHOD1(HasDeviceWithAddress, bool(const std::string&)); |
| 78 }; | 62 }; |
| 79 | 63 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 95 // Mock methods don't support return type scoped_ptr<>. This is a possible | 79 // Mock methods don't support return type scoped_ptr<>. This is a possible |
| 96 // workaround: mock a proxy method to be called by the target overrided method | 80 // workaround: mock a proxy method to be called by the target overrided method |
| 97 // (CreateConnection). | 81 // (CreateConnection). |
| 98 MOCK_METHOD0(CreateConnectionProxy, Connection*()); | 82 MOCK_METHOD0(CreateConnectionProxy, Connection*()); |
| 99 | 83 |
| 100 // Creates a mock connection and sets an expectation that the mock connection | 84 // Creates a mock connection and sets an expectation that the mock connection |
| 101 // finder's CreateConnection() method will be called and will return the | 85 // finder's CreateConnection() method will be called and will return the |
| 102 // created connection. Returns a reference to the created connection. | 86 // created connection. Returns a reference to the created connection. |
| 103 // NOTE: The returned connection's lifetime is managed by the connection | 87 // NOTE: The returned connection's lifetime is managed by the connection |
| 104 // finder. | 88 // finder. |
| 105 MockConnection* ExpectCreateConnection() { | 89 FakeConnection* ExpectCreateConnection() { |
| 106 scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>()); | 90 scoped_ptr<FakeConnection> connection( |
| 107 MockConnection* connection_alias = connection.get(); | 91 new FakeConnection(CreateRemoteDevice())); |
| 92 FakeConnection* connection_alias = connection.get(); |
| 108 EXPECT_CALL(*this, CreateConnectionProxy()) | 93 EXPECT_CALL(*this, CreateConnectionProxy()) |
| 109 .WillOnce(Return(connection.release())); | 94 .WillOnce(Return(connection.release())); |
| 110 return connection_alias; | 95 return connection_alias; |
| 111 } | 96 } |
| 112 | 97 |
| 113 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); | 98 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); |
| 114 | 99 |
| 115 protected: | 100 protected: |
| 116 scoped_ptr<Connection> CreateConnection( | 101 scoped_ptr<Connection> CreateConnection( |
| 117 const std::string& device_address) override { | 102 const std::string& device_address) override { |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 | 367 |
| 383 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 368 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 384 Find_ConnectionSucceeds_WithRemoteDevice) { | 369 Find_ConnectionSucceeds_WithRemoteDevice) { |
| 385 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | 370 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( |
| 386 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); | 371 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); |
| 387 // Starting discovery. | 372 // Starting discovery. |
| 388 FindAndExpectStartDiscovery(connection_finder); | 373 FindAndExpectStartDiscovery(connection_finder); |
| 389 ExpectStopDiscoveryAndRemoveObserver(); | 374 ExpectStopDiscoveryAndRemoveObserver(); |
| 390 | 375 |
| 391 // Finding and creating a connection to the right device. | 376 // Finding and creating a connection to the right device. |
| 392 MockConnection* connection = connection_finder.ExpectCreateConnection(); | 377 FakeConnection* connection = connection_finder.ExpectCreateConnection(); |
| 393 PrepareDevice(kServiceUUID, kBluetoothAddress, true); | 378 PrepareDevice(kServiceUUID, kBluetoothAddress, true); |
| 394 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | 379 connection_finder.DeviceAdded(adapter_.get(), device_.get()); |
| 395 | 380 |
| 396 // Creating a connection. | 381 // Creating a connection. |
| 397 base::RunLoop run_loop; | 382 base::RunLoop run_loop; |
| 398 EXPECT_FALSE(last_found_connection_); | 383 EXPECT_FALSE(last_found_connection_); |
| 399 connection->SetStatus(Connection::IN_PROGRESS); | 384 connection->SetStatus(Connection::IN_PROGRESS); |
| 400 connection->SetStatus(Connection::CONNECTED); | 385 connection->SetStatus(Connection::CONNECTED); |
| 401 run_loop.RunUntilIdle(); | 386 run_loop.RunUntilIdle(); |
| 402 EXPECT_TRUE(last_found_connection_); | 387 EXPECT_TRUE(last_found_connection_); |
| 403 } | 388 } |
| 404 | 389 |
| 405 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 390 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 406 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) { | 391 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) { |
| 407 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | 392 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( |
| 408 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); | 393 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); |
| 409 | 394 |
| 410 // Starting discovery. | 395 // Starting discovery. |
| 411 FindAndExpectStartDiscovery(connection_finder); | 396 FindAndExpectStartDiscovery(connection_finder); |
| 412 base::Closure stop_discovery_session_callback; | 397 base::Closure stop_discovery_session_callback; |
| 413 EXPECT_CALL(*last_discovery_session_alias_, Stop(_, _)) | 398 EXPECT_CALL(*last_discovery_session_alias_, Stop(_, _)) |
| 414 .WillOnce(SaveArg<0>(&stop_discovery_session_callback)); | 399 .WillOnce(SaveArg<0>(&stop_discovery_session_callback)); |
| 415 | 400 |
| 416 // Preparing to create a GATT connection to the right device. | 401 // Preparing to create a GATT connection to the right device. |
| 417 PrepareDevice(kServiceUUID, kBluetoothAddress, true); | 402 PrepareDevice(kServiceUUID, kBluetoothAddress, true); |
| 418 MockConnection* connection = connection_finder.ExpectCreateConnection(); | 403 FakeConnection* connection = connection_finder.ExpectCreateConnection(); |
| 419 | 404 |
| 420 // Trying to create a connection. | 405 // Trying to create a connection. |
| 421 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | 406 connection_finder.DeviceAdded(adapter_.get(), device_.get()); |
| 422 ASSERT_FALSE(last_found_connection_); | 407 ASSERT_FALSE(last_found_connection_); |
| 423 connection->SetStatus(Connection::IN_PROGRESS); | 408 connection->SetStatus(Connection::IN_PROGRESS); |
| 424 | 409 |
| 425 // Stopping the discovery session. | 410 // Stopping the discovery session. |
| 426 ASSERT_FALSE(stop_discovery_session_callback.is_null()); | 411 ASSERT_FALSE(stop_discovery_session_callback.is_null()); |
| 427 stop_discovery_session_callback.Run(); | 412 stop_discovery_session_callback.Run(); |
| 428 | 413 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 478 connection_finder.AdapterPresentChanged(adapter_.get(), true); |
| 494 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | 479 connection_finder.AdapterPoweredChanged(adapter_.get(), true); |
| 495 ON_CALL(*last_discovery_session_alias_, IsActive()) | 480 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 496 .WillByDefault(Return(true)); | 481 .WillByDefault(Return(true)); |
| 497 | 482 |
| 498 ASSERT_FALSE(discovery_callback.is_null()); | 483 ASSERT_FALSE(discovery_callback.is_null()); |
| 499 discovery_callback.Run(discovery_session.Pass()); | 484 discovery_callback.Run(discovery_session.Pass()); |
| 500 | 485 |
| 501 // Preparing to create a GATT connection to the right device. | 486 // Preparing to create a GATT connection to the right device. |
| 502 PrepareDevice(kServiceUUID, kBluetoothAddress, true); | 487 PrepareDevice(kServiceUUID, kBluetoothAddress, true); |
| 503 MockConnection* connection = connection_finder.ExpectCreateConnection(); | 488 FakeConnection* connection = connection_finder.ExpectCreateConnection(); |
| 504 | 489 |
| 505 // Trying to create a connection. | 490 // Trying to create a connection. |
| 506 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | 491 connection_finder.DeviceAdded(adapter_.get(), device_.get()); |
| 507 EXPECT_CALL(*last_discovery_session_alias_, Stop(_, _)).Times(AtLeast(1)); | 492 EXPECT_CALL(*last_discovery_session_alias_, Stop(_, _)).Times(AtLeast(1)); |
| 508 | 493 |
| 509 // Completing the connection. | 494 // Completing the connection. |
| 510 base::RunLoop run_loop; | 495 base::RunLoop run_loop; |
| 511 ASSERT_FALSE(last_found_connection_); | 496 ASSERT_FALSE(last_found_connection_); |
| 512 connection->SetStatus(Connection::IN_PROGRESS); | 497 connection->SetStatus(Connection::IN_PROGRESS); |
| 513 connection->SetStatus(Connection::CONNECTED); | 498 connection->SetStatus(Connection::CONNECTED); |
| 514 run_loop.RunUntilIdle(); | 499 run_loop.RunUntilIdle(); |
| 515 EXPECT_TRUE(last_found_connection_); | 500 EXPECT_TRUE(last_found_connection_); |
| 516 } | 501 } |
| 517 | 502 |
| 518 } // namespace proximity_auth | 503 } // namespace proximity_auth |
| OLD | NEW |