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