Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection_finder_unittest.cc

Issue 2561203002: Migrate weave-related classes from proximity_auth/ble to cryptauth/ble. (Closed)
Patch Set: Moved all general classes from proximity_auth to cryptauth. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/fake_connection.h"
19 #include "components/cryptauth/remote_device.h" 21 #include "components/cryptauth/remote_device.h"
22 #include "components/cryptauth/wire_message.h"
20 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" 23 #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" 24 #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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 finder_strategy, 69 finder_strategy,
69 device_whitelist, 70 device_whitelist,
70 nullptr, 71 nullptr,
71 kMaxNumberOfAttempts) {} 72 kMaxNumberOfAttempts) {}
72 73
73 ~MockBluetoothLowEnergyConnectionFinder() override {} 74 ~MockBluetoothLowEnergyConnectionFinder() override {}
74 75
75 // Mock methods don't support return type std::unique_ptr<>. This is a 76 // 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 77 // possible workaround: mock a proxy method to be called by the target
77 // overridden method (CreateConnection). 78 // overridden method (CreateConnection).
78 MOCK_METHOD0(CreateConnectionProxy, Connection*()); 79 MOCK_METHOD0(CreateConnectionProxy, cryptauth::Connection*());
79 80
80 // Creates a mock connection and sets an expectation that the mock connection 81 // Creates a mock connection and sets an expectation that the mock connection
81 // finder's CreateConnection() method will be called and will return the 82 // finder's CreateConnection() method will be called and will return the
82 // created connection. Returns a reference to the created connection. 83 // created connection. Returns a reference to the created connection.
83 // NOTE: The returned connection's lifetime is managed by the connection 84 // NOTE: The returned connection's lifetime is managed by the connection
84 // finder. 85 // finder.
85 FakeConnection* ExpectCreateConnection() { 86 cryptauth::FakeConnection* ExpectCreateConnection() {
86 std::unique_ptr<FakeConnection> connection( 87 std::unique_ptr<cryptauth::FakeConnection> connection(
87 new FakeConnection(CreateLERemoteDeviceForTest())); 88 new FakeConnection(CreateLERemoteDeviceForTest()));
88 FakeConnection* connection_alias = connection.get(); 89 cryptauth::FakeConnection* connection_alias = connection.get();
89 EXPECT_CALL(*this, CreateConnectionProxy()) 90 EXPECT_CALL(*this, CreateConnectionProxy())
90 .WillOnce(Return(connection.release())); 91 .WillOnce(Return(connection.release()));
91 return connection_alias; 92 return connection_alias;
92 } 93 }
93 94
94 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); 95 MOCK_METHOD0(CloseGattConnectionProxy, void(void));
95 96
96 protected: 97 protected:
97 std::unique_ptr<Connection> CreateConnection( 98 std::unique_ptr<cryptauth::Connection> CreateConnection(
98 const std::string& device_address) override { 99 const std::string& device_address) override {
99 return base::WrapUnique(CreateConnectionProxy()); 100 return base::WrapUnique(CreateConnectionProxy());
100 } 101 }
101 102
102 private: 103 private:
103 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); 104 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder);
104 }; 105 };
105 106
106 } // namespace 107 } // namespace
107 108
(...skipping 20 matching lines...) Expand all
128 std::vector<const device::BluetoothDevice*> devices; 129 std::vector<const device::BluetoothDevice*> devices;
129 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 130 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
130 131
131 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); 132 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
132 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); 133 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
133 134
134 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) 135 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_))
135 .WillByDefault(Return(false)); 136 .WillByDefault(Return(false));
136 } 137 }
137 138
138 void OnConnectionFound(std::unique_ptr<Connection> connection) { 139 void OnConnectionFound(std::unique_ptr<cryptauth::Connection> connection) {
139 last_found_connection_ = std::move(connection); 140 last_found_connection_ = std::move(connection);
140 } 141 }
141 142
142 void FindAndExpectStartDiscovery( 143 void FindAndExpectStartDiscovery(
143 BluetoothLowEnergyConnectionFinder& connection_finder) { 144 BluetoothLowEnergyConnectionFinder& connection_finder) {
144 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; 145 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
145 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( 146 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session(
146 new NiceMock<device::MockBluetoothDiscoverySession>()); 147 new NiceMock<device::MockBluetoothDiscoverySession>());
147 last_discovery_session_alias_ = discovery_session.get(); 148 last_discovery_session_alias_ = discovery_session.get();
148 149
(...skipping 19 matching lines...) Expand all
168 bool paired) { 169 bool paired) {
169 BluetoothDevice::UUIDSet uuids = {device::BluetoothUUID(uuid)}; 170 BluetoothDevice::UUIDSet uuids = {device::BluetoothUUID(uuid)};
170 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); 171 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids));
171 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); 172 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address));
172 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); 173 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired));
173 } 174 }
174 175
175 scoped_refptr<device::MockBluetoothAdapter> adapter_; 176 scoped_refptr<device::MockBluetoothAdapter> adapter_;
176 ConnectionFinder::ConnectionCallback connection_callback_; 177 ConnectionFinder::ConnectionCallback connection_callback_;
177 std::unique_ptr<device::MockBluetoothDevice> device_; 178 std::unique_ptr<device::MockBluetoothDevice> device_;
178 std::unique_ptr<Connection> last_found_connection_; 179 std::unique_ptr<cryptauth::Connection> last_found_connection_;
179 std::unique_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_; 180 std::unique_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_;
180 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; 181 device::MockBluetoothDiscoverySession* last_discovery_session_alias_;
181 182
182 private: 183 private:
183 base::MessageLoop message_loop_; 184 base::MessageLoop message_loop_;
184 }; 185 };
185 186
186 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 187 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
187 ConstructAndDestroyDoesntCrash) { 188 ConstructAndDestroyDoesntCrash) {
188 // Destroying a BluetoothConnectionFinder for which Find() has not been called 189 // Destroying a BluetoothConnectionFinder for which Find() has not been called
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 362
362 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 363 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
363 Find_ConnectionSucceeds_WithRemoteDevice) { 364 Find_ConnectionSucceeds_WithRemoteDevice) {
364 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 365 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
365 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); 366 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE);
366 // Starting discovery. 367 // Starting discovery.
367 FindAndExpectStartDiscovery(connection_finder); 368 FindAndExpectStartDiscovery(connection_finder);
368 ExpectRemoveObserver(); 369 ExpectRemoveObserver();
369 370
370 // Finding and creating a connection to the right device. 371 // Finding and creating a connection to the right device.
371 FakeConnection* connection = connection_finder.ExpectCreateConnection(); 372 cryptauth::FakeConnection* connection =
373 connection_finder.ExpectCreateConnection();
372 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); 374 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true);
373 connection_finder.DeviceAdded(adapter_.get(), device_.get()); 375 connection_finder.DeviceAdded(adapter_.get(), device_.get());
374 376
375 // Creating a connection. 377 // Creating a connection.
376 base::RunLoop run_loop; 378 base::RunLoop run_loop;
377 EXPECT_FALSE(last_found_connection_); 379 EXPECT_FALSE(last_found_connection_);
378 connection->SetStatus(Connection::IN_PROGRESS); 380 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
379 connection->SetStatus(Connection::CONNECTED); 381 connection->SetStatus(cryptauth::Connection::CONNECTED);
380 run_loop.RunUntilIdle(); 382 run_loop.RunUntilIdle();
381 EXPECT_TRUE(last_found_connection_); 383 EXPECT_TRUE(last_found_connection_);
382 } 384 }
383 385
384 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 386 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
385 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) { 387 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds) {
386 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 388 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
387 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); 389 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE);
388 390
389 // Starting discovery. 391 // Starting discovery.
390 FindAndExpectStartDiscovery(connection_finder); 392 FindAndExpectStartDiscovery(connection_finder);
391 393
392 // Preparing to create a GATT connection to the right device. 394 // Preparing to create a GATT connection to the right device.
393 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); 395 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true);
394 FakeConnection* connection = connection_finder.ExpectCreateConnection(); 396 cryptauth::FakeConnection* connection =
397 connection_finder.ExpectCreateConnection();
395 398
396 // Trying to create a connection. 399 // Trying to create a connection.
397 connection_finder.DeviceAdded(adapter_.get(), device_.get()); 400 connection_finder.DeviceAdded(adapter_.get(), device_.get());
398 ASSERT_FALSE(last_found_connection_); 401 ASSERT_FALSE(last_found_connection_);
399 connection->SetStatus(Connection::IN_PROGRESS); 402 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
400 403
401 // Preparing to restart the discovery session. 404 // Preparing to restart the discovery session.
402 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; 405 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback;
403 std::vector<const device::BluetoothDevice*> devices; 406 std::vector<const device::BluetoothDevice*> devices;
404 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 407 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
405 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) 408 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _))
406 .WillOnce(SaveArg<1>(&discovery_callback)); 409 .WillOnce(SaveArg<1>(&discovery_callback));
407 410
408 // Connection fails. 411 // Connection fails.
409 { 412 {
410 base::RunLoop run_loop; 413 base::RunLoop run_loop;
411 connection->SetStatus(Connection::DISCONNECTED); 414 connection->SetStatus(cryptauth::Connection::DISCONNECTED);
412 run_loop.RunUntilIdle(); 415 run_loop.RunUntilIdle();
413 } 416 }
414 417
415 // Restarting the discovery session. 418 // Restarting the discovery session.
416 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( 419 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session(
417 new NiceMock<device::MockBluetoothDiscoverySession>()); 420 new NiceMock<device::MockBluetoothDiscoverySession>());
418 last_discovery_session_alias_ = discovery_session.get(); 421 last_discovery_session_alias_ = discovery_session.get();
419 ON_CALL(*last_discovery_session_alias_, IsActive()) 422 ON_CALL(*last_discovery_session_alias_, IsActive())
420 .WillByDefault(Return(true)); 423 .WillByDefault(Return(true));
421 ASSERT_FALSE(discovery_callback.is_null()); 424 ASSERT_FALSE(discovery_callback.is_null());
422 discovery_callback.Run(std::move(discovery_session)); 425 discovery_callback.Run(std::move(discovery_session));
423 426
424 // Preparing to create a GATT connection to the right device. 427 // Preparing to create a GATT connection to the right device.
425 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); 428 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true);
426 connection = connection_finder.ExpectCreateConnection(); 429 connection = connection_finder.ExpectCreateConnection();
427 430
428 // Trying to create a connection. 431 // Trying to create a connection.
429 connection_finder.DeviceAdded(adapter_.get(), device_.get()); 432 connection_finder.DeviceAdded(adapter_.get(), device_.get());
430 433
431 // Completing the connection. 434 // Completing the connection.
432 { 435 {
433 base::RunLoop run_loop; 436 base::RunLoop run_loop;
434 EXPECT_FALSE(last_found_connection_); 437 EXPECT_FALSE(last_found_connection_);
435 connection->SetStatus(Connection::IN_PROGRESS); 438 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
436 connection->SetStatus(Connection::CONNECTED); 439 connection->SetStatus(cryptauth::Connection::CONNECTED);
437 run_loop.RunUntilIdle(); 440 run_loop.RunUntilIdle();
438 } 441 }
439 EXPECT_TRUE(last_found_connection_); 442 EXPECT_TRUE(last_found_connection_);
440 } 443 }
441 444
442 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, 445 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest,
443 Find_AdapterRemoved_RestartDiscoveryAndConnectionSucceeds) { 446 Find_AdapterRemoved_RestartDiscoveryAndConnectionSucceeds) {
444 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( 447 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder(
445 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE); 448 nullptr, BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE);
446 449
(...skipping 23 matching lines...) Expand all
470 connection_finder.AdapterPresentChanged(adapter_.get(), true); 473 connection_finder.AdapterPresentChanged(adapter_.get(), true);
471 connection_finder.AdapterPoweredChanged(adapter_.get(), true); 474 connection_finder.AdapterPoweredChanged(adapter_.get(), true);
472 ON_CALL(*last_discovery_session_alias_, IsActive()) 475 ON_CALL(*last_discovery_session_alias_, IsActive())
473 .WillByDefault(Return(true)); 476 .WillByDefault(Return(true));
474 477
475 ASSERT_FALSE(discovery_callback.is_null()); 478 ASSERT_FALSE(discovery_callback.is_null());
476 discovery_callback.Run(std::move(discovery_session)); 479 discovery_callback.Run(std::move(discovery_session));
477 480
478 // Preparing to create a GATT connection to the right device. 481 // Preparing to create a GATT connection to the right device.
479 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); 482 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true);
480 FakeConnection* connection = connection_finder.ExpectCreateConnection(); 483 cryptauth::FakeConnection* connection =
484 connection_finder.ExpectCreateConnection();
481 485
482 // Trying to create a connection. 486 // Trying to create a connection.
483 connection_finder.DeviceAdded(adapter_.get(), device_.get()); 487 connection_finder.DeviceAdded(adapter_.get(), device_.get());
484 488
485 // Completing the connection. 489 // Completing the connection.
486 base::RunLoop run_loop; 490 base::RunLoop run_loop;
487 ASSERT_FALSE(last_found_connection_); 491 ASSERT_FALSE(last_found_connection_);
488 connection->SetStatus(Connection::IN_PROGRESS); 492 connection->SetStatus(cryptauth::Connection::IN_PROGRESS);
489 connection->SetStatus(Connection::CONNECTED); 493 connection->SetStatus(cryptauth::Connection::CONNECTED);
490 run_loop.RunUntilIdle(); 494 run_loop.RunUntilIdle();
491 EXPECT_TRUE(last_found_connection_); 495 EXPECT_TRUE(last_found_connection_);
492 } 496 }
493 497
494 } // namespace proximity_auth 498 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698