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