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

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

Issue 1356943004: Add RemoteDeviceLoader to create RemoteDevice from CryptAuth data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webui
Patch Set: move RemoteDevice creation in tests to util function Created 5 years, 2 months 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.h" 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/test/test_simple_task_runner.h" 12 #include "base/test/test_simple_task_runner.h"
13 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" 13 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h"
14 #include "components/proximity_auth/bluetooth_throttler.h" 14 #include "components/proximity_auth/bluetooth_throttler.h"
15 #include "components/proximity_auth/connection_finder.h" 15 #include "components/proximity_auth/connection_finder.h"
16 #include "components/proximity_auth/proximity_auth_test_util.h"
16 #include "components/proximity_auth/remote_device.h" 17 #include "components/proximity_auth/remote_device.h"
17 #include "components/proximity_auth/wire_message.h" 18 #include "components/proximity_auth/wire_message.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h" 19 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "device/bluetooth/bluetooth_gatt_characteristic.h" 20 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
20 #include "device/bluetooth/bluetooth_uuid.h" 21 #include "device/bluetooth/bluetooth_uuid.h"
21 #include "device/bluetooth/test/mock_bluetooth_adapter.h" 22 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
22 #include "device/bluetooth/test/mock_bluetooth_device.h" 23 #include "device/bluetooth/test/mock_bluetooth_device.h"
23 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" 24 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
24 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" 25 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
25 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" 26 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
26 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" 27 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
27 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 30
30 using testing::_; 31 using testing::_;
31 using testing::AtLeast; 32 using testing::AtLeast;
32 using testing::NiceMock; 33 using testing::NiceMock;
33 using testing::Return; 34 using testing::Return;
34 using testing::StrictMock; 35 using testing::StrictMock;
35 using testing::SaveArg; 36 using testing::SaveArg;
36 37
37 namespace proximity_auth { 38 namespace proximity_auth {
38 namespace { 39 namespace {
39 40
40 const char kDeviceName[] = "Device name";
41 const char kPublicKey[] = "Public key";
42 const char kBluetoothAddress[] = "11:22:33:44:55:66";
43 const char kPersistentSymmetricKey[] = "PSK";
44
45 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; 41 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF";
46 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; 42 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
47 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; 43 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb";
48 44
49 const char kServiceID[] = "service id"; 45 const char kServiceID[] = "service id";
50 const char kToPeripheralCharID[] = "to peripheral char id"; 46 const char kToPeripheralCharID[] = "to peripheral char id";
51 const char kFromPeripheralCharID[] = "from peripheral char id"; 47 const char kFromPeripheralCharID[] = "from peripheral char id";
52 48
53 const device::BluetoothGattCharacteristic::Properties 49 const device::BluetoothGattCharacteristic::Properties
54 kCharacteristicProperties = 50 kCharacteristicProperties =
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 private: 115 private:
120 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection); 116 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection);
121 }; 117 };
122 118
123 } // namespace 119 } // namespace
124 120
125 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test { 121 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test {
126 public: 122 public:
127 ProximityAuthBluetoothLowEnergyConnectionTest() 123 ProximityAuthBluetoothLowEnergyConnectionTest()
128 : adapter_(new NiceMock<device::MockBluetoothAdapter>), 124 : adapter_(new NiceMock<device::MockBluetoothAdapter>),
129 remote_device_(kDeviceName, 125 remote_device_(CreateLERemoteDeviceForTest()),
130 kPublicKey,
131 kBluetoothAddress,
132 kPersistentSymmetricKey),
133 service_uuid_(device::BluetoothUUID(kServiceUUID)), 126 service_uuid_(device::BluetoothUUID(kServiceUUID)),
134 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), 127 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)),
135 from_peripheral_char_uuid_( 128 from_peripheral_char_uuid_(
136 device::BluetoothUUID(kFromPeripheralCharUUID)), 129 device::BluetoothUUID(kFromPeripheralCharUUID)),
137 notify_session_alias_(NULL), 130 notify_session_alias_(NULL),
138 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), 131 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>),
139 task_runner_(new base::TestSimpleTaskRunner) {} 132 task_runner_(new base::TestSimpleTaskRunner) {}
140 133
141 void SetUp() override { 134 void SetUp() override {
142 device_ = make_scoped_ptr(new NiceMock<device::MockBluetoothDevice>( 135 device_ = make_scoped_ptr(new NiceMock<device::MockBluetoothDevice>(
143 adapter_.get(), 0, kDeviceName, kBluetoothAddress, false, false)); 136 adapter_.get(), 0, kTestRemoteDeviceName,
137 kTestRemoteDeviceBluetoothAddress, false, false));
144 138
145 service_ = make_scoped_ptr(new NiceMock<device::MockBluetoothGattService>( 139 service_ = make_scoped_ptr(new NiceMock<device::MockBluetoothGattService>(
146 device_.get(), kServiceID, service_uuid_, true, false)); 140 device_.get(), kServiceID, service_uuid_, true, false));
147 to_peripheral_char_ = 141 to_peripheral_char_ =
148 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>( 142 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>(
149 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, 143 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_,
150 false, kCharacteristicProperties, 144 false, kCharacteristicProperties,
151 device::BluetoothGattCharacteristic::PERMISSION_NONE)); 145 device::BluetoothGattCharacteristic::PERMISSION_NONE));
152 146
153 from_peripheral_char_ = 147 from_peripheral_char_ =
154 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>( 148 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>(
155 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, 149 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_,
156 false, kCharacteristicProperties, 150 false, kCharacteristicProperties,
157 device::BluetoothGattCharacteristic::PERMISSION_NONE)); 151 device::BluetoothGattCharacteristic::PERMISSION_NONE));
158 152
159 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 153 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
160 154
161 std::vector<const device::BluetoothDevice*> devices; 155 std::vector<const device::BluetoothDevice*> devices;
162 devices.push_back(device_.get()); 156 devices.push_back(device_.get());
163 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 157 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
164 ON_CALL(*adapter_, GetDevice(kBluetoothAddress)) 158 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress))
165 .WillByDefault(Return(device_.get())); 159 .WillByDefault(Return(device_.get()));
166 ON_CALL(*device_, GetGattService(kServiceID)) 160 ON_CALL(*device_, GetGattService(kServiceID))
167 .WillByDefault(Return(service_.get())); 161 .WillByDefault(Return(service_.get()));
168 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) 162 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID))
169 .WillByDefault(Return(from_peripheral_char_.get())); 163 .WillByDefault(Return(from_peripheral_char_.get()));
170 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) 164 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID))
171 .WillByDefault(Return(to_peripheral_char_.get())); 165 .WillByDefault(Return(to_peripheral_char_.get()));
172 } 166 }
173 167
174 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED 168 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 207 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
214 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 208 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
215 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 209 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
216 .WillOnce(DoAll( 210 .WillOnce(DoAll(
217 SaveArg<0>(&characteristics_finder_success_callback_), 211 SaveArg<0>(&characteristics_finder_success_callback_),
218 SaveArg<1>(&characteristics_finder_error_callback_), 212 SaveArg<1>(&characteristics_finder_error_callback_),
219 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 213 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
220 214
221 create_gatt_connection_success_callback_.Run( 215 create_gatt_connection_success_callback_.Run(
222 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( 216 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>(
223 adapter_, kBluetoothAddress))); 217 adapter_, kTestRemoteDeviceBluetoothAddress)));
224 218
225 EXPECT_EQ(connection->sub_status(), 219 EXPECT_EQ(connection->sub_status(),
226 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS); 220 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS);
227 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 221 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS);
228 } 222 }
229 223
230 // Transitions |connection| from WAITING_CHARACTERISTICS to 224 // Transitions |connection| from WAITING_CHARACTERISTICS to
231 // WAITING_NOTIFY_SESSION state. 225 // WAITING_NOTIFY_SESSION state.
232 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { 226 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) {
233 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)) 227 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _))
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 662
669 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 663 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
670 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 664 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
671 .WillOnce(DoAll( 665 .WillOnce(DoAll(
672 SaveArg<0>(&characteristics_finder_success_callback_), 666 SaveArg<0>(&characteristics_finder_success_callback_),
673 SaveArg<1>(&characteristics_finder_error_callback_), 667 SaveArg<1>(&characteristics_finder_error_callback_),
674 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 668 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
675 669
676 create_gatt_connection_success_callback_.Run( 670 create_gatt_connection_success_callback_.Run(
677 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( 671 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>(
678 adapter_, kBluetoothAddress))); 672 adapter_, kTestRemoteDeviceBluetoothAddress)));
679 673
680 CharacteristicsFound(connection.get()); 674 CharacteristicsFound(connection.get());
681 NotifySessionStarted(connection.get()); 675 NotifySessionStarted(connection.get());
682 ResponseSignalReceived(connection.get()); 676 ResponseSignalReceived(connection.get());
683 } 677 }
684 678
685 } // namespace proximity_auth 679 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698