| 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 <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | 19 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" |
| 19 #include "components/proximity_auth/fake_connection.h" | 20 #include "components/proximity_auth/fake_connection.h" |
| 20 #include "components/proximity_auth/proximity_auth_test_util.h" | 21 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 21 #include "components/proximity_auth/remote_device.h" | 22 #include "components/proximity_auth/remote_device.h" |
| 22 #include "components/proximity_auth/wire_message.h" | 23 #include "components/proximity_auth/wire_message.h" |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" | 24 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 FinderStrategy finder_strategy) | 63 FinderStrategy finder_strategy) |
| 63 : BluetoothLowEnergyConnectionFinder(CreateLERemoteDeviceForTest(), | 64 : BluetoothLowEnergyConnectionFinder(CreateLERemoteDeviceForTest(), |
| 64 kServiceUUID, | 65 kServiceUUID, |
| 65 finder_strategy, | 66 finder_strategy, |
| 66 device_whitelist, | 67 device_whitelist, |
| 67 nullptr, | 68 nullptr, |
| 68 kMaxNumberOfAttempts) {} | 69 kMaxNumberOfAttempts) {} |
| 69 | 70 |
| 70 ~MockBluetoothLowEnergyConnectionFinder() override {} | 71 ~MockBluetoothLowEnergyConnectionFinder() override {} |
| 71 | 72 |
| 72 // Mock methods don't support return type scoped_ptr<>. This is a possible | 73 // Mock methods don't support return type std::unique_ptr<>. This is a |
| 73 // workaround: mock a proxy method to be called by the target overrided method | 74 // possible workaround: mock a proxy method to be called by the target |
| 74 // (CreateConnection). | 75 // overridden method (CreateConnection). |
| 75 MOCK_METHOD0(CreateConnectionProxy, Connection*()); | 76 MOCK_METHOD0(CreateConnectionProxy, Connection*()); |
| 76 | 77 |
| 77 // Creates a mock connection and sets an expectation that the mock connection | 78 // Creates a mock connection and sets an expectation that the mock connection |
| 78 // finder's CreateConnection() method will be called and will return the | 79 // finder's CreateConnection() method will be called and will return the |
| 79 // created connection. Returns a reference to the created connection. | 80 // created connection. Returns a reference to the created connection. |
| 80 // NOTE: The returned connection's lifetime is managed by the connection | 81 // NOTE: The returned connection's lifetime is managed by the connection |
| 81 // finder. | 82 // finder. |
| 82 FakeConnection* ExpectCreateConnection() { | 83 FakeConnection* ExpectCreateConnection() { |
| 83 scoped_ptr<FakeConnection> connection( | 84 std::unique_ptr<FakeConnection> connection( |
| 84 new FakeConnection(CreateLERemoteDeviceForTest())); | 85 new FakeConnection(CreateLERemoteDeviceForTest())); |
| 85 FakeConnection* connection_alias = connection.get(); | 86 FakeConnection* connection_alias = connection.get(); |
| 86 EXPECT_CALL(*this, CreateConnectionProxy()) | 87 EXPECT_CALL(*this, CreateConnectionProxy()) |
| 87 .WillOnce(Return(connection.release())); | 88 .WillOnce(Return(connection.release())); |
| 88 return connection_alias; | 89 return connection_alias; |
| 89 } | 90 } |
| 90 | 91 |
| 91 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); | 92 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); |
| 92 | 93 |
| 93 protected: | 94 protected: |
| 94 scoped_ptr<Connection> CreateConnection( | 95 std::unique_ptr<Connection> CreateConnection( |
| 95 const std::string& device_address) override { | 96 const std::string& device_address) override { |
| 96 return make_scoped_ptr(CreateConnectionProxy()); | 97 return base::WrapUnique(CreateConnectionProxy()); |
| 97 } | 98 } |
| 98 | 99 |
| 99 private: | 100 private: |
| 100 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); | 101 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); |
| 101 }; | 102 }; |
| 102 | 103 |
| 103 } // namespace | 104 } // namespace |
| 104 | 105 |
| 105 class ProximityAuthBluetoothLowEnergyConnectionFinderTest | 106 class ProximityAuthBluetoothLowEnergyConnectionFinderTest |
| 106 : public testing::Test { | 107 : public testing::Test { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 125 std::vector<const device::BluetoothDevice*> devices; | 126 std::vector<const device::BluetoothDevice*> devices; |
| 126 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | 127 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); |
| 127 | 128 |
| 128 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 129 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
| 129 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 130 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| 130 | 131 |
| 131 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) | 132 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) |
| 132 .WillByDefault(Return(false)); | 133 .WillByDefault(Return(false)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 void OnConnectionFound(scoped_ptr<Connection> connection) { | 136 void OnConnectionFound(std::unique_ptr<Connection> connection) { |
| 136 last_found_connection_ = std::move(connection); | 137 last_found_connection_ = std::move(connection); |
| 137 } | 138 } |
| 138 | 139 |
| 139 void FindAndExpectStartDiscovery( | 140 void FindAndExpectStartDiscovery( |
| 140 BluetoothLowEnergyConnectionFinder& connection_finder) { | 141 BluetoothLowEnergyConnectionFinder& connection_finder) { |
| 141 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 142 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 142 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 143 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 143 new NiceMock<device::MockBluetoothDiscoverySession>()); | 144 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 144 last_discovery_session_alias_ = discovery_session.get(); | 145 last_discovery_session_alias_ = discovery_session.get(); |
| 145 | 146 |
| 146 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for | 147 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for |
| 147 // StartDiscoveryWithFilter. | 148 // StartDiscoveryWithFilter. |
| 148 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 149 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 149 .WillOnce(SaveArg<1>(&discovery_callback)); | 150 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 150 EXPECT_CALL(*adapter_, AddObserver(_)); | 151 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 151 ON_CALL(*last_discovery_session_alias_, IsActive()) | 152 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 152 .WillByDefault(Return(true)); | 153 .WillByDefault(Return(true)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 165 bool paired) { | 166 bool paired) { |
| 166 std::vector<device::BluetoothUUID> uuids; | 167 std::vector<device::BluetoothUUID> uuids; |
| 167 uuids.push_back(device::BluetoothUUID(uuid)); | 168 uuids.push_back(device::BluetoothUUID(uuid)); |
| 168 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); | 169 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); |
| 169 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); | 170 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); |
| 170 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); | 171 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); |
| 171 } | 172 } |
| 172 | 173 |
| 173 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 174 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 174 ConnectionFinder::ConnectionCallback connection_callback_; | 175 ConnectionFinder::ConnectionCallback connection_callback_; |
| 175 scoped_ptr<device::MockBluetoothDevice> device_; | 176 std::unique_ptr<device::MockBluetoothDevice> device_; |
| 176 scoped_ptr<Connection> last_found_connection_; | 177 std::unique_ptr<Connection> last_found_connection_; |
| 177 scoped_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_; | 178 std::unique_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_; |
| 178 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; | 179 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; |
| 179 | 180 |
| 180 private: | 181 private: |
| 181 base::MessageLoop message_loop_; | 182 base::MessageLoop message_loop_; |
| 182 }; | 183 }; |
| 183 | 184 |
| 184 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 185 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 185 ConstructAndDestroyDoesntCrash) { | 186 ConstructAndDestroyDoesntCrash) { |
| 186 // Destroying a BluetoothConnectionFinder for which Find() has not been called | 187 // Destroying a BluetoothConnectionFinder for which Find() has not been called |
| 187 // should not crash. | 188 // should not crash. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 204 } | 205 } |
| 205 | 206 |
| 206 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 207 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 207 Find_StopsDiscoverySessionBeforeDestroying) { | 208 Find_StopsDiscoverySessionBeforeDestroying) { |
| 208 BluetoothLowEnergyConnectionFinder connection_finder( | 209 BluetoothLowEnergyConnectionFinder connection_finder( |
| 209 CreateLERemoteDeviceForTest(), kServiceUUID, | 210 CreateLERemoteDeviceForTest(), kServiceUUID, |
| 210 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, | 211 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, |
| 211 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); | 212 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); |
| 212 | 213 |
| 213 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 214 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 214 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 215 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 215 new NiceMock<device::MockBluetoothDiscoverySession>()); | 216 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 216 device::MockBluetoothDiscoverySession* discovery_session_alias = | 217 device::MockBluetoothDiscoverySession* discovery_session_alias = |
| 217 discovery_session.get(); | 218 discovery_session.get(); |
| 218 | 219 |
| 219 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 220 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 220 .WillOnce(SaveArg<1>(&discovery_callback)); | 221 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 221 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); | 222 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); |
| 222 EXPECT_CALL(*adapter_, AddObserver(_)); | 223 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 223 connection_finder.Find(connection_callback_); | 224 connection_finder.Find(connection_callback_); |
| 224 | 225 |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 .WillOnce(SaveArg<1>(&discovery_callback)); | 407 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 407 | 408 |
| 408 // Connection fails. | 409 // Connection fails. |
| 409 { | 410 { |
| 410 base::RunLoop run_loop; | 411 base::RunLoop run_loop; |
| 411 connection->SetStatus(Connection::DISCONNECTED); | 412 connection->SetStatus(Connection::DISCONNECTED); |
| 412 run_loop.RunUntilIdle(); | 413 run_loop.RunUntilIdle(); |
| 413 } | 414 } |
| 414 | 415 |
| 415 // Restarting the discovery session. | 416 // Restarting the discovery session. |
| 416 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 417 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 417 new NiceMock<device::MockBluetoothDiscoverySession>()); | 418 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 418 last_discovery_session_alias_ = discovery_session.get(); | 419 last_discovery_session_alias_ = discovery_session.get(); |
| 419 ON_CALL(*last_discovery_session_alias_, IsActive()) | 420 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 420 .WillByDefault(Return(true)); | 421 .WillByDefault(Return(true)); |
| 421 ASSERT_FALSE(discovery_callback.is_null()); | 422 ASSERT_FALSE(discovery_callback.is_null()); |
| 422 discovery_callback.Run(std::move(discovery_session)); | 423 discovery_callback.Run(std::move(discovery_session)); |
| 423 | 424 |
| 424 // Preparing to create a GATT connection to the right device. | 425 // Preparing to create a GATT connection to the right device. |
| 425 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); | 426 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); |
| 426 connection = connection_finder.ExpectCreateConnection(); | 427 connection = connection_finder.ExpectCreateConnection(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 453 ON_CALL(*last_discovery_session_alias_, IsActive()) | 454 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 454 .WillByDefault(Return(false)); | 455 .WillByDefault(Return(false)); |
| 455 connection_finder.AdapterPoweredChanged(adapter_.get(), false); | 456 connection_finder.AdapterPoweredChanged(adapter_.get(), false); |
| 456 connection_finder.AdapterPresentChanged(adapter_.get(), false); | 457 connection_finder.AdapterPresentChanged(adapter_.get(), false); |
| 457 | 458 |
| 458 // Adding the adapter. | 459 // Adding the adapter. |
| 459 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 460 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
| 460 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 461 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| 461 | 462 |
| 462 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 463 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 463 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 464 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 464 new NiceMock<device::MockBluetoothDiscoverySession>()); | 465 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 465 last_discovery_session_alias_ = discovery_session.get(); | 466 last_discovery_session_alias_ = discovery_session.get(); |
| 466 | 467 |
| 467 // Restarting the discovery session. | 468 // Restarting the discovery session. |
| 468 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 469 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 469 .WillOnce(SaveArg<1>(&discovery_callback)); | 470 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 470 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 471 connection_finder.AdapterPresentChanged(adapter_.get(), true); |
| 471 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | 472 connection_finder.AdapterPoweredChanged(adapter_.get(), true); |
| 472 ON_CALL(*last_discovery_session_alias_, IsActive()) | 473 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 473 .WillByDefault(Return(true)); | 474 .WillByDefault(Return(true)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 485 // Completing the connection. | 486 // Completing the connection. |
| 486 base::RunLoop run_loop; | 487 base::RunLoop run_loop; |
| 487 ASSERT_FALSE(last_found_connection_); | 488 ASSERT_FALSE(last_found_connection_); |
| 488 connection->SetStatus(Connection::IN_PROGRESS); | 489 connection->SetStatus(Connection::IN_PROGRESS); |
| 489 connection->SetStatus(Connection::CONNECTED); | 490 connection->SetStatus(Connection::CONNECTED); |
| 490 run_loop.RunUntilIdle(); | 491 run_loop.RunUntilIdle(); |
| 491 EXPECT_TRUE(last_found_connection_); | 492 EXPECT_TRUE(last_found_connection_); |
| 492 } | 493 } |
| 493 | 494 |
| 494 } // namespace proximity_auth | 495 } // namespace proximity_auth |
| OLD | NEW |