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

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

Issue 2561203002: Migrate weave-related classes from proximity_auth/ble to cryptauth/ble. (Closed)
Patch Set: Rebase. Created 3 years, 12 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 <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 "
20 #include "components/cryptauth/bluetooth_throttler.h"
21 #include "components/cryptauth/connection_finder.h"
22 #include "components/cryptauth/cryptauth_test_util.h"
19 #include "components/cryptauth/remote_device.h" 23 #include "components/cryptauth/remote_device.h"
20 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" 24 #include "components/cryptauth/wire_message.h"
21 #include "components/proximity_auth/bluetooth_throttler.h"
22 #include "components/proximity_auth/connection_finder.h"
23 #include "components/proximity_auth/proximity_auth_test_util.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"
31 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" 31 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
32 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" 32 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
33 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" 33 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
34 #include "testing/gmock/include/gmock/gmock.h" 34 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 20 matching lines...) Expand all
55 const device::BluetoothRemoteGattCharacteristic::Properties 55 const device::BluetoothRemoteGattCharacteristic::Properties
56 kCharacteristicProperties = 56 kCharacteristicProperties =
57 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | 57 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST |
58 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | 58 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ |
59 device::BluetoothRemoteGattCharacteristic:: 59 device::BluetoothRemoteGattCharacteristic::
60 PROPERTY_WRITE_WITHOUT_RESPONSE | 60 PROPERTY_WRITE_WITHOUT_RESPONSE |
61 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; 61 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE;
62 62
63 const int kMaxNumberOfTries = 3; 63 const int kMaxNumberOfTries = 3;
64 64
65 class MockBluetoothThrottler : public BluetoothThrottler { 65 class MockBluetoothThrottler : public cryptauth::BluetoothThrottler {
66 public: 66 public:
67 MockBluetoothThrottler() {} 67 MockBluetoothThrottler() {}
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(cryptauth::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 cryptauth::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 cryptauth::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.
119 using BluetoothLowEnergyConnection::status; 119 using BluetoothLowEnergyConnection::status;
120 using BluetoothLowEnergyConnection::sub_status; 120 using BluetoothLowEnergyConnection::sub_status;
121 121
122 private: 122 private:
123 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection); 123 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection);
124 }; 124 };
125 125
126 } // namespace 126 } // namespace
127 127
128 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test { 128 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test {
129 public: 129 public:
130 ProximityAuthBluetoothLowEnergyConnectionTest() 130 ProximityAuthBluetoothLowEnergyConnectionTest()
131 : adapter_(new NiceMock<device::MockBluetoothAdapter>), 131 : adapter_(new NiceMock<device::MockBluetoothAdapter>),
132 remote_device_(CreateLERemoteDeviceForTest()), 132 remote_device_(cryptauth::CreateLERemoteDeviceForTest()),
133 service_uuid_(device::BluetoothUUID(kServiceUUID)), 133 service_uuid_(device::BluetoothUUID(kServiceUUID)),
134 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), 134 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)),
135 from_peripheral_char_uuid_( 135 from_peripheral_char_uuid_(
136 device::BluetoothUUID(kFromPeripheralCharUUID)), 136 device::BluetoothUUID(kFromPeripheralCharUUID)),
137 notify_session_alias_(NULL), 137 notify_session_alias_(NULL),
138 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), 138 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>),
139 task_runner_(new base::TestSimpleTaskRunner) {} 139 task_runner_(new base::TestSimpleTaskRunner) {}
140 140
141 void SetUp() override { 141 void SetUp() override {
142 device_ = base::MakeUnique<NiceMock<device::MockBluetoothDevice>>( 142 device_ = base::MakeUnique<NiceMock<device::MockBluetoothDevice>>(
143 adapter_.get(), 0, kTestRemoteDeviceName, 143 adapter_.get(), 0, cryptauth::kTestRemoteDeviceName,
144 kTestRemoteDeviceBluetoothAddress, false, false); 144 cryptauth::kTestRemoteDeviceBluetoothAddress, false, false);
145 145
146 service_ = base::MakeUnique<NiceMock<device::MockBluetoothGattService>>( 146 service_ = base::MakeUnique<NiceMock<device::MockBluetoothGattService>>(
147 device_.get(), kServiceID, service_uuid_, true, false); 147 device_.get(), kServiceID, service_uuid_, true, false);
148 to_peripheral_char_ = 148 to_peripheral_char_ =
149 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>( 149 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>(
150 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, 150 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_,
151 false, kCharacteristicProperties, 151 false, kCharacteristicProperties,
152 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE); 152 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE);
153 153
154 from_peripheral_char_ = 154 from_peripheral_char_ =
155 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>( 155 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>(
156 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, 156 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_,
157 false, kCharacteristicProperties, 157 false, kCharacteristicProperties,
158 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE); 158 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE);
159 159
160 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 160 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
161 161
162 std::vector<const device::BluetoothDevice*> devices; 162 std::vector<const device::BluetoothDevice*> devices;
163 devices.push_back(device_.get()); 163 devices.push_back(device_.get());
164 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); 164 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices));
165 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) 165 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress))
166 .WillByDefault(Return(device_.get())); 166 .WillByDefault(Return(device_.get()));
167 ON_CALL(*device_, GetGattService(kServiceID)) 167 ON_CALL(*device_, GetGattService(kServiceID))
168 .WillByDefault(Return(service_.get())); 168 .WillByDefault(Return(service_.get()));
169 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) 169 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID))
170 .WillByDefault(Return(from_peripheral_char_.get())); 170 .WillByDefault(Return(from_peripheral_char_.get()));
171 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) 171 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID))
172 .WillByDefault(Return(to_peripheral_char_.get())); 172 .WillByDefault(Return(to_peripheral_char_.get()));
173 } 173 }
174 174
175 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED 175 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED
176 // state. 176 // state.
177 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { 177 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() {
178 EXPECT_CALL(*adapter_, AddObserver(_)); 178 EXPECT_CALL(*adapter_, AddObserver(_));
179 EXPECT_CALL(*adapter_, RemoveObserver(_)); 179 EXPECT_CALL(*adapter_, RemoveObserver(_));
180 180
181 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 181 std::unique_ptr<MockBluetoothLowEnergyConnection> connection(
182 new MockBluetoothLowEnergyConnection( 182 new MockBluetoothLowEnergyConnection(
183 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), 183 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(),
184 kMaxNumberOfTries)); 184 kMaxNumberOfTries));
185 185
186 EXPECT_EQ(connection->sub_status(), 186 EXPECT_EQ(connection->sub_status(),
187 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 187 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
188 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 188 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED);
189 189
190 connection->SetTaskRunnerForTesting(task_runner_); 190 connection->SetTaskRunnerForTesting(task_runner_);
191 191
192 return connection; 192 return connection;
193 } 193 }
194 194
195 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS 195 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS
196 // state, without an existing GATT connection. 196 // state, without an existing GATT connection.
197 void ConnectGatt(MockBluetoothLowEnergyConnection* connection) { 197 void ConnectGatt(MockBluetoothLowEnergyConnection* connection) {
198 // Preparing |connection| for a CreateGattConnection call. 198 // Preparing |connection| for a CreateGattConnection call.
199 EXPECT_CALL(*device_, CreateGattConnection(_, _)) 199 EXPECT_CALL(*device_, CreateGattConnection(_, _))
200 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), 200 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_),
201 SaveArg<1>(&create_gatt_connection_error_callback_))); 201 SaveArg<1>(&create_gatt_connection_error_callback_)));
202 202
203 // No throttling by default 203 // No throttling by default
204 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) 204 EXPECT_CALL(*bluetooth_throttler_, GetDelay())
205 .WillOnce(Return(base::TimeDelta())); 205 .WillOnce(Return(base::TimeDelta()));
206 206
207 connection->Connect(); 207 connection->Connect();
208 208
209 EXPECT_EQ(connection->sub_status(), 209 EXPECT_EQ(connection->sub_status(),
210 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); 210 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION);
211 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 211 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS);
212 212
213 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 213 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
214 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 214 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
215 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 215 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
216 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 216 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
217 .WillOnce(DoAll( 217 .WillOnce(DoAll(
218 SaveArg<0>(&characteristics_finder_success_callback_), 218 SaveArg<0>(&characteristics_finder_success_callback_),
219 SaveArg<1>(&characteristics_finder_error_callback_), 219 SaveArg<1>(&characteristics_finder_error_callback_),
220 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 220 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
221 221
222 create_gatt_connection_success_callback_.Run( 222 create_gatt_connection_success_callback_.Run(
223 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( 223 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>(
224 adapter_, kTestRemoteDeviceBluetoothAddress)); 224 adapter_, cryptauth::kTestRemoteDeviceBluetoothAddress));
225 225
226 EXPECT_EQ(connection->sub_status(), 226 EXPECT_EQ(connection->sub_status(),
227 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS); 227 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS);
228 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 228 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS);
229 } 229 }
230 230
231 // Transitions |connection| from WAITING_CHARACTERISTICS to 231 // Transitions |connection| from WAITING_CHARACTERISTICS to
232 // WAITING_NOTIFY_SESSION state. 232 // WAITING_NOTIFY_SESSION state.
233 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { 233 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) {
234 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)) 234 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _))
235 .WillOnce(DoAll(SaveArg<0>(&notify_session_success_callback_), 235 .WillOnce(DoAll(SaveArg<0>(&notify_session_success_callback_),
236 SaveArg<1>(&notify_session_error_callback_))); 236 SaveArg<1>(&notify_session_error_callback_)));
237 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); 237 EXPECT_FALSE(characteristics_finder_error_callback_.is_null());
238 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); 238 ASSERT_FALSE(characteristics_finder_success_callback_.is_null());
239 239
240 characteristics_finder_success_callback_.Run( 240 characteristics_finder_success_callback_.Run(
241 {service_uuid_, kServiceID}, 241 {service_uuid_, kServiceID},
242 {to_peripheral_char_uuid_, kToPeripheralCharID}, 242 {to_peripheral_char_uuid_, kToPeripheralCharID},
243 {from_peripheral_char_uuid_, kFromPeripheralCharID}); 243 {from_peripheral_char_uuid_, kFromPeripheralCharID});
244 244
245 EXPECT_EQ(connection->sub_status(), 245 EXPECT_EQ(connection->sub_status(),
246 BluetoothLowEnergyConnection::SubStatus::WAITING_NOTIFY_SESSION); 246 BluetoothLowEnergyConnection::SubStatus::WAITING_NOTIFY_SESSION);
247 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 247 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS);
248 } 248 }
249 249
250 // Transitions |connection| from WAITING_NOTIFY_SESSION to 250 // Transitions |connection| from WAITING_NOTIFY_SESSION to
251 // WAITING_RESPONSE_SIGNAL state. 251 // WAITING_RESPONSE_SIGNAL state.
252 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { 252 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) {
253 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 253 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _))
254 .WillOnce( 254 .WillOnce(
255 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), 255 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_),
256 SaveArg<1>(&write_remote_characteristic_success_callback_), 256 SaveArg<1>(&write_remote_characteristic_success_callback_),
257 SaveArg<2>(&write_remote_characteristic_error_callback_))); 257 SaveArg<2>(&write_remote_characteristic_error_callback_)));
258 EXPECT_FALSE(notify_session_error_callback_.is_null()); 258 EXPECT_FALSE(notify_session_error_callback_.is_null());
259 ASSERT_FALSE(notify_session_success_callback_.is_null()); 259 ASSERT_FALSE(notify_session_success_callback_.is_null());
260 260
261 // Store an alias for the notify session passed |connection|. 261 // Store an alias for the notify session passed |connection|.
262 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( 262 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session(
263 new NiceMock<device::MockBluetoothGattNotifySession>( 263 new NiceMock<device::MockBluetoothGattNotifySession>(
264 to_peripheral_char_->GetWeakPtr())); 264 to_peripheral_char_->GetWeakPtr()));
265 notify_session_alias_ = notify_session.get(); 265 notify_session_alias_ = notify_session.get();
266 266
267 notify_session_success_callback_.Run(std::move(notify_session)); 267 notify_session_success_callback_.Run(std::move(notify_session));
268 task_runner_->RunUntilIdle(); 268 task_runner_->RunUntilIdle();
269 269
270 EXPECT_EQ(connection->sub_status(), 270 EXPECT_EQ(connection->sub_status(),
271 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); 271 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL);
272 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 272 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS);
273 } 273 }
274 274
275 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. 275 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state.
276 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { 276 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) {
277 // Written value contains only the 277 // Written value contains only the
278 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. 278 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal.
279 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( 279 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector(
280 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 280 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal::
281 kInviteToConnectSignal)); 281 kInviteToConnectSignal));
282 EXPECT_EQ(last_value_written_on_to_peripheral_char_, 282 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
283 kInviteToConnectSignal); 283 kInviteToConnectSignal);
284 284
285 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); 285 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0);
286 RunWriteCharacteristicSuccessCallback(); 286 RunWriteCharacteristicSuccessCallback();
287 287
288 // Received the 288 // Received the
289 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. 289 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal.
290 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector( 290 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector(
291 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: 291 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal::
292 kInvitationResponseSignal)); 292 kInvitationResponseSignal));
293 connection->GattCharacteristicValueChanged( 293 connection->GattCharacteristicValueChanged(
294 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); 294 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal);
295 295
296 EXPECT_EQ(connection->sub_status(), 296 EXPECT_EQ(connection->sub_status(),
297 BluetoothLowEnergyConnection::SubStatus::CONNECTED); 297 BluetoothLowEnergyConnection::SubStatus::CONNECTED);
298 EXPECT_EQ(connection->status(), Connection::CONNECTED); 298 EXPECT_EQ(connection->status(), cryptauth::Connection::CONNECTED);
299 } 299 }
300 300
301 // Transitions |connection| to a DISCONNECTED state regardless of its initial 301 // Transitions |connection| to a DISCONNECTED state regardless of its initial
302 // state. 302 // state.
303 void Disconnect(MockBluetoothLowEnergyConnection* connection) { 303 void Disconnect(MockBluetoothLowEnergyConnection* connection) {
304 // A notify session was previously set. 304 // A notify session was previously set.
305 if (notify_session_alias_) 305 if (notify_session_alias_)
306 EXPECT_CALL(*notify_session_alias_, Stop(_)); 306 EXPECT_CALL(*notify_session_alias_, Stop(_));
307 307
308 connection->Disconnect(); 308 connection->Disconnect();
309 309
310 EXPECT_EQ(connection->sub_status(), 310 EXPECT_EQ(connection->sub_status(),
311 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 311 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
312 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 312 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED);
313 } 313 }
314 314
315 void InitializeConnection(MockBluetoothLowEnergyConnection* connection) { 315 void InitializeConnection(MockBluetoothLowEnergyConnection* connection) {
316 ConnectGatt(connection); 316 ConnectGatt(connection);
317 CharacteristicsFound(connection); 317 CharacteristicsFound(connection);
318 NotifySessionStarted(connection); 318 NotifySessionStarted(connection);
319 ResponseSignalReceived(connection); 319 ResponseSignalReceived(connection);
320 } 320 }
321 321
322 void RunWriteCharacteristicSuccessCallback() { 322 void RunWriteCharacteristicSuccessCallback() {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); 457 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0);
458 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); 458 EXPECT_FALSE(characteristics_finder_success_callback_.is_null());
459 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); 459 ASSERT_FALSE(characteristics_finder_error_callback_.is_null());
460 460
461 characteristics_finder_error_callback_.Run( 461 characteristics_finder_error_callback_.Run(
462 {to_peripheral_char_uuid_, kToPeripheralCharID}, 462 {to_peripheral_char_uuid_, kToPeripheralCharID},
463 {from_peripheral_char_uuid_, kFromPeripheralCharID}); 463 {from_peripheral_char_uuid_, kFromPeripheralCharID});
464 464
465 EXPECT_EQ(connection->sub_status(), 465 EXPECT_EQ(connection->sub_status(),
466 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 466 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
467 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 467 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED);
468 } 468 }
469 469
470 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 470 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest,
471 Connect_Fails_NotifySessionError) { 471 Connect_Fails_NotifySessionError) {
472 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 472 std::unique_ptr<MockBluetoothLowEnergyConnection> connection(
473 CreateConnection()); 473 CreateConnection());
474 ConnectGatt(connection.get()); 474 ConnectGatt(connection.get());
475 CharacteristicsFound(connection.get()); 475 CharacteristicsFound(connection.get());
476 476
477 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) 477 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _))
478 .Times(0); 478 .Times(0);
479 EXPECT_FALSE(notify_session_success_callback_.is_null()); 479 EXPECT_FALSE(notify_session_success_callback_.is_null());
480 ASSERT_FALSE(notify_session_error_callback_.is_null()); 480 ASSERT_FALSE(notify_session_error_callback_.is_null());
481 481
482 notify_session_error_callback_.Run( 482 notify_session_error_callback_.Run(
483 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 483 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
484 484
485 EXPECT_EQ(connection->sub_status(), 485 EXPECT_EQ(connection->sub_status(),
486 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 486 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
487 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 487 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED);
488 } 488 }
489 489
490 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 490 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest,
491 Connect_Fails_ErrorSendingInviteToConnectSignal) { 491 Connect_Fails_ErrorSendingInviteToConnectSignal) {
492 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 492 std::unique_ptr<MockBluetoothLowEnergyConnection> connection(
493 CreateConnection()); 493 CreateConnection());
494 ConnectGatt(connection.get()); 494 ConnectGatt(connection.get());
495 CharacteristicsFound(connection.get()); 495 CharacteristicsFound(connection.get());
496 NotifySessionStarted(connection.get()); 496 NotifySessionStarted(connection.get());
497 497
(...skipping 16 matching lines...) Expand all
514 EXPECT_EQ(last_value_written_on_to_peripheral_char_, 514 EXPECT_EQ(last_value_written_on_to_peripheral_char_,
515 kInviteToConnectSignal); 515 kInviteToConnectSignal);
516 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); 516 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null());
517 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); 517 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null());
518 write_remote_characteristic_error_callback_.Run( 518 write_remote_characteristic_error_callback_.Run(
519 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); 519 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
520 } 520 }
521 521
522 EXPECT_EQ(connection->sub_status(), 522 EXPECT_EQ(connection->sub_status(),
523 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); 523 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED);
524 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); 524 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED);
525 } 525 }
526 526
527 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, 527 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest,
528 Receive_MessageSmallerThanCharacteristicSize) { 528 Receive_MessageSmallerThanCharacteristicSize) {
529 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( 529 std::unique_ptr<MockBluetoothLowEnergyConnection> connection(
530 CreateConnection()); 530 CreateConnection());
531 InitializeConnection(connection.get()); 531 InitializeConnection(connection.get());
532 532
533 std::string received_bytes; 533 std::string received_bytes;
534 EXPECT_CALL(*connection, OnBytesReceived(_)) 534 EXPECT_CALL(*connection, OnBytesReceived(_))
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 28 matching lines...) Expand all
668 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) 670 EXPECT_CALL(*bluetooth_throttler_, GetDelay())
669 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); 671 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1))));
670 EXPECT_CALL(*device_, CreateGattConnection(_, _)) 672 EXPECT_CALL(*device_, CreateGattConnection(_, _))
671 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), 673 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_),
672 SaveArg<1>(&create_gatt_connection_error_callback_))); 674 SaveArg<1>(&create_gatt_connection_error_callback_)));
673 675
674 // No GATT connection should be created before the delay. 676 // No GATT connection should be created before the delay.
675 connection->Connect(); 677 connection->Connect();
676 EXPECT_EQ(connection->sub_status(), 678 EXPECT_EQ(connection->sub_status(),
677 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); 679 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION);
678 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); 680 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS);
679 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); 681 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null());
680 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); 682 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null());
681 683
682 // A GATT connection should be created after the delay. 684 // A GATT connection should be created after the delay.
683 task_runner_->RunUntilIdle(); 685 task_runner_->RunUntilIdle();
684 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); 686 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null());
685 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); 687 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null());
686 688
687 // Preparing |connection| to run |create_gatt_connection_success_callback_|. 689 // Preparing |connection| to run |create_gatt_connection_success_callback_|.
688 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) 690 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _))
689 .WillOnce(DoAll( 691 .WillOnce(DoAll(
690 SaveArg<0>(&characteristics_finder_success_callback_), 692 SaveArg<0>(&characteristics_finder_success_callback_),
691 SaveArg<1>(&characteristics_finder_error_callback_), 693 SaveArg<1>(&characteristics_finder_error_callback_),
692 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); 694 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>)));
693 695
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_, cryptauth::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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698