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.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/test_simple_task_runner.h" |
| 19 #include "components/cryptauth/ble/bluetooth_low_energy_characteristics_finder.h
" |
19 #include "components/cryptauth/remote_device.h" | 20 #include "components/cryptauth/remote_device.h" |
20 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | |
21 #include "components/proximity_auth/bluetooth_throttler.h" | 21 #include "components/proximity_auth/bluetooth_throttler.h" |
22 #include "components/proximity_auth/connection_finder.h" | 22 #include "components/proximity_auth/connection_finder.h" |
23 #include "components/proximity_auth/proximity_auth_test_util.h" | 23 #include "components/proximity_auth/proximity_auth_test_util.h" |
24 #include "components/proximity_auth/wire_message.h" | 24 #include "components/proximity_auth/wire_message.h" |
25 #include "device/bluetooth/bluetooth_adapter_factory.h" | 25 #include "device/bluetooth/bluetooth_adapter_factory.h" |
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
27 #include "device/bluetooth/bluetooth_uuid.h" | 27 #include "device/bluetooth/bluetooth_uuid.h" |
28 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 28 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
29 #include "device/bluetooth/test/mock_bluetooth_device.h" | 29 #include "device/bluetooth/test/mock_bluetooth_device.h" |
30 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 30 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 ~MockBluetoothThrottler() override {} | 68 ~MockBluetoothThrottler() override {} |
69 | 69 |
70 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | 70 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); |
71 MOCK_METHOD1(OnConnection, void(Connection* connection)); | 71 MOCK_METHOD1(OnConnection, void(Connection* connection)); |
72 | 72 |
73 private: | 73 private: |
74 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | 74 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); |
75 }; | 75 }; |
76 | 76 |
77 class MockBluetoothLowEnergyCharacteristicsFinder | 77 class MockBluetoothLowEnergyCharacteristicsFinder |
78 : public BluetoothLowEnergyCharacteristicsFinder { | 78 : public cryptauth::BluetoothLowEnergyCharacteristicsFinder { |
79 public: | 79 public: |
80 MockBluetoothLowEnergyCharacteristicsFinder() {} | 80 MockBluetoothLowEnergyCharacteristicsFinder() {} |
81 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} | 81 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} |
82 | 82 |
83 private: | 83 private: |
84 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); | 84 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); |
85 }; | 85 }; |
86 | 86 |
87 class MockBluetoothLowEnergyConnection : public BluetoothLowEnergyConnection { | 87 class MockBluetoothLowEnergyConnection : public BluetoothLowEnergyConnection { |
88 public: | 88 public: |
89 MockBluetoothLowEnergyConnection( | 89 MockBluetoothLowEnergyConnection( |
90 const cryptauth::RemoteDevice& remote_device, | 90 const cryptauth::RemoteDevice& remote_device, |
91 scoped_refptr<device::BluetoothAdapter> adapter, | 91 scoped_refptr<device::BluetoothAdapter> adapter, |
92 const device::BluetoothUUID remote_service_uuid, | 92 const device::BluetoothUUID remote_service_uuid, |
93 BluetoothThrottler* bluetooth_throttler, | 93 BluetoothThrottler* bluetooth_throttler, |
94 int max_number_of_write_attempts) | 94 int max_number_of_write_attempts) |
95 : BluetoothLowEnergyConnection(remote_device, | 95 : BluetoothLowEnergyConnection(remote_device, |
96 adapter, | 96 adapter, |
97 remote_service_uuid, | 97 remote_service_uuid, |
98 bluetooth_throttler, | 98 bluetooth_throttler, |
99 max_number_of_write_attempts) {} | 99 max_number_of_write_attempts) {} |
100 | 100 |
101 ~MockBluetoothLowEnergyConnection() override {} | 101 ~MockBluetoothLowEnergyConnection() override {} |
102 | 102 |
103 MOCK_METHOD2( | 103 MOCK_METHOD2(CreateCharacteristicsFinder, |
104 CreateCharacteristicsFinder, | 104 cryptauth::BluetoothLowEnergyCharacteristicsFinder*( |
105 BluetoothLowEnergyCharacteristicsFinder*( | 105 const cryptauth::BluetoothLowEnergyCharacteristicsFinder:: |
106 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | 106 SuccessCallback& success, |
107 success, | 107 const cryptauth::BluetoothLowEnergyCharacteristicsFinder:: |
108 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); | 108 ErrorCallback& error)); |
109 | 109 |
110 MOCK_METHOD2(OnDidSendMessage, | 110 MOCK_METHOD2(OnDidSendMessage, |
111 void(const WireMessage& message, bool success)); | 111 void(const WireMessage& message, bool success)); |
112 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | 112 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); |
113 | 113 |
114 // Exposing inherited protected methods for testing. | 114 // Exposing inherited protected methods for testing. |
115 using BluetoothLowEnergyConnection::GattCharacteristicValueChanged; | 115 using BluetoothLowEnergyConnection::GattCharacteristicValueChanged; |
116 using BluetoothLowEnergyConnection::SetTaskRunnerForTesting; | 116 using BluetoothLowEnergyConnection::SetTaskRunnerForTesting; |
117 | 117 |
118 // Exposing inherited protected fields for testing. | 118 // Exposing inherited protected fields for testing. |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; | 368 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; |
369 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 369 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
370 base::MessageLoop message_loop_; | 370 base::MessageLoop message_loop_; |
371 | 371 |
372 // Callbacks | 372 // Callbacks |
373 device::BluetoothDevice::GattConnectionCallback | 373 device::BluetoothDevice::GattConnectionCallback |
374 create_gatt_connection_success_callback_; | 374 create_gatt_connection_success_callback_; |
375 device::BluetoothDevice::ConnectErrorCallback | 375 device::BluetoothDevice::ConnectErrorCallback |
376 create_gatt_connection_error_callback_; | 376 create_gatt_connection_error_callback_; |
377 | 377 |
378 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | 378 cryptauth::BluetoothLowEnergyCharacteristicsFinder::SuccessCallback |
379 characteristics_finder_success_callback_; | 379 characteristics_finder_success_callback_; |
380 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback | 380 cryptauth::BluetoothLowEnergyCharacteristicsFinder::ErrorCallback |
381 characteristics_finder_error_callback_; | 381 characteristics_finder_error_callback_; |
382 | 382 |
383 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback | 383 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback |
384 notify_session_success_callback_; | 384 notify_session_success_callback_; |
385 device::BluetoothRemoteGattCharacteristic::ErrorCallback | 385 device::BluetoothRemoteGattCharacteristic::ErrorCallback |
386 notify_session_error_callback_; | 386 notify_session_error_callback_; |
387 | 387 |
388 base::Closure write_remote_characteristic_success_callback_; | 388 base::Closure write_remote_characteristic_success_callback_; |
389 device::BluetoothRemoteGattCharacteristic::ErrorCallback | 389 device::BluetoothRemoteGattCharacteristic::ErrorCallback |
390 write_remote_characteristic_error_callback_; | 390 write_remote_characteristic_error_callback_; |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 591 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
592 .WillOnce( | 592 .WillOnce( |
593 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 593 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
594 SaveArg<1>(&write_remote_characteristic_success_callback_), | 594 SaveArg<1>(&write_remote_characteristic_success_callback_), |
595 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 595 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
596 | 596 |
597 // Message (bytes) that is going to be sent. | 597 // Message (bytes) that is going to be sent. |
598 int message_size = 100; | 598 int message_size = 100; |
599 std::string message(message_size, 'A'); | 599 std::string message(message_size, 'A'); |
600 message[0] = 'B'; | 600 message[0] = 'B'; |
601 connection->SendMessage(base::MakeUnique<FakeWireMessage>(message)); | 601 connection->SendMessage( |
| 602 base::MakeUnique<cryptauth::FakeWireMessage>(message)); |
602 | 603 |
603 // Expecting that |kSendSignal| + |message_size| + |message| was written. | 604 // Expecting that |kSendSignal| + |message_size| + |message| was written. |
604 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | 605 EXPECT_EQ(last_value_written_on_to_peripheral_char_, |
605 CreateFirstCharacteristicValue(message, message.size())); | 606 CreateFirstCharacteristicValue(message, message.size())); |
606 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | 607 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); |
607 | 608 |
608 RunWriteCharacteristicSuccessCallback(); | 609 RunWriteCharacteristicSuccessCallback(); |
609 } | 610 } |
610 | 611 |
611 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 612 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
612 SendMessage_LagerThanCharacteristicSize) { | 613 SendMessage_LagerThanCharacteristicSize) { |
613 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | 614 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
614 CreateConnection()); | 615 CreateConnection()); |
615 InitializeConnection(connection.get()); | 616 InitializeConnection(connection.get()); |
616 | 617 |
617 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | 618 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
618 // called. | 619 // called. |
619 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 620 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
620 .WillOnce( | 621 .WillOnce( |
621 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 622 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
622 SaveArg<1>(&write_remote_characteristic_success_callback_), | 623 SaveArg<1>(&write_remote_characteristic_success_callback_), |
623 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 624 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
624 | 625 |
625 // Message (bytes) that is going to be sent. | 626 // Message (bytes) that is going to be sent. |
626 int message_size = 600; | 627 int message_size = 600; |
627 std::string message(message_size, 'A'); | 628 std::string message(message_size, 'A'); |
628 message[0] = 'B'; | 629 message[0] = 'B'; |
629 connection->SendMessage(base::MakeUnique<FakeWireMessage>(message)); | 630 connection->SendMessage( |
| 631 base::MakeUnique<cryptauth::FakeWireMessage>(message)); |
630 | 632 |
631 // Expecting that |kSendSignal| + |message_size| was written in the first 8 | 633 // Expecting that |kSendSignal| + |message_size| was written in the first 8 |
632 // bytes. | 634 // bytes. |
633 std::vector<uint8_t> prefix( | 635 std::vector<uint8_t> prefix( |
634 last_value_written_on_to_peripheral_char_.begin(), | 636 last_value_written_on_to_peripheral_char_.begin(), |
635 last_value_written_on_to_peripheral_char_.begin() + 8); | 637 last_value_written_on_to_peripheral_char_.begin() + 8); |
636 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); | 638 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); |
637 std::vector<uint8_t> bytes_received( | 639 std::vector<uint8_t> bytes_received( |
638 last_value_written_on_to_peripheral_char_.begin() + 8, | 640 last_value_written_on_to_peripheral_char_.begin() + 8, |
639 last_value_written_on_to_peripheral_char_.end()); | 641 last_value_written_on_to_peripheral_char_.end()); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 create_gatt_connection_success_callback_.Run( | 696 create_gatt_connection_success_callback_.Run( |
695 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( | 697 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( |
696 adapter_, kTestRemoteDeviceBluetoothAddress)); | 698 adapter_, kTestRemoteDeviceBluetoothAddress)); |
697 | 699 |
698 CharacteristicsFound(connection.get()); | 700 CharacteristicsFound(connection.get()); |
699 NotifySessionStarted(connection.get()); | 701 NotifySessionStarted(connection.get()); |
700 ResponseSignalReceived(connection.get()); | 702 ResponseSignalReceived(connection.get()); |
701 } | 703 } |
702 | 704 |
703 } // namespace proximity_auth | 705 } // namespace proximity_auth |
OLD | NEW |