OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/run_loop.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" | |
23 #include "components/cryptauth/remote_device.h" | |
24 #include "components/cryptauth/wire_message.h" | |
25 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | |
27 #include "device/bluetooth/bluetooth_uuid.h" | |
28 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
29 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
30 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | |
31 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" | |
32 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
33 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" | |
34 #include "testing/gmock/include/gmock/gmock.h" | |
35 #include "testing/gtest/include/gtest/gtest.h" | |
36 | |
37 using testing::_; | |
38 using testing::AtLeast; | |
39 using testing::NiceMock; | |
40 using testing::Return; | |
41 using testing::StrictMock; | |
42 using testing::SaveArg; | |
43 | |
44 namespace proximity_auth { | |
45 namespace { | |
46 | |
47 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; | |
48 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | |
49 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | |
50 | |
51 const char kServiceID[] = "service id"; | |
52 const char kToPeripheralCharID[] = "to peripheral char id"; | |
53 const char kFromPeripheralCharID[] = "from peripheral char id"; | |
54 | |
55 const char kTestFeature[] = "testFeature"; | |
56 | |
57 const device::BluetoothRemoteGattCharacteristic::Properties | |
58 kCharacteristicProperties = | |
59 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | | |
60 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | | |
61 device::BluetoothRemoteGattCharacteristic:: | |
62 PROPERTY_WRITE_WITHOUT_RESPONSE | | |
63 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; | |
64 | |
65 const int kMaxNumberOfTries = 3; | |
66 | |
67 class MockBluetoothThrottler : public cryptauth::BluetoothThrottler { | |
68 public: | |
69 MockBluetoothThrottler() {} | |
70 ~MockBluetoothThrottler() override {} | |
71 | |
72 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | |
73 MOCK_METHOD1(OnConnection, void(cryptauth::Connection* connection)); | |
74 | |
75 private: | |
76 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | |
77 }; | |
78 | |
79 class MockBluetoothLowEnergyCharacteristicsFinder | |
80 : public cryptauth::BluetoothLowEnergyCharacteristicsFinder { | |
81 public: | |
82 MockBluetoothLowEnergyCharacteristicsFinder() {} | |
83 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} | |
84 | |
85 private: | |
86 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); | |
87 }; | |
88 | |
89 class MockBluetoothLowEnergyConnection : public BluetoothLowEnergyConnection { | |
90 public: | |
91 MockBluetoothLowEnergyConnection( | |
92 const cryptauth::RemoteDevice& remote_device, | |
93 scoped_refptr<device::BluetoothAdapter> adapter, | |
94 const device::BluetoothUUID remote_service_uuid, | |
95 cryptauth::BluetoothThrottler* bluetooth_throttler, | |
96 int max_number_of_write_attempts) | |
97 : BluetoothLowEnergyConnection(remote_device, | |
98 adapter, | |
99 remote_service_uuid, | |
100 bluetooth_throttler, | |
101 max_number_of_write_attempts) {} | |
102 | |
103 ~MockBluetoothLowEnergyConnection() override {} | |
104 | |
105 MOCK_METHOD2(CreateCharacteristicsFinder, | |
106 cryptauth::BluetoothLowEnergyCharacteristicsFinder*( | |
107 const cryptauth::BluetoothLowEnergyCharacteristicsFinder:: | |
108 SuccessCallback& success, | |
109 const cryptauth::BluetoothLowEnergyCharacteristicsFinder:: | |
110 ErrorCallback& error)); | |
111 | |
112 MOCK_METHOD2(OnDidSendMessage, | |
113 void(const cryptauth::WireMessage& message, bool success)); | |
114 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); | |
115 | |
116 // Exposing inherited protected methods for testing. | |
117 using BluetoothLowEnergyConnection::GattCharacteristicValueChanged; | |
118 using BluetoothLowEnergyConnection::SetTaskRunnerForTesting; | |
119 | |
120 // Exposing inherited protected fields for testing. | |
121 using BluetoothLowEnergyConnection::status; | |
122 using BluetoothLowEnergyConnection::sub_status; | |
123 | |
124 private: | |
125 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnection); | |
126 }; | |
127 | |
128 } // namespace | |
129 | |
130 class ProximityAuthBluetoothLowEnergyConnectionTest : public testing::Test { | |
131 public: | |
132 ProximityAuthBluetoothLowEnergyConnectionTest() | |
133 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | |
134 remote_device_(cryptauth::CreateLERemoteDeviceForTest()), | |
135 service_uuid_(device::BluetoothUUID(kServiceUUID)), | |
136 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), | |
137 from_peripheral_char_uuid_( | |
138 device::BluetoothUUID(kFromPeripheralCharUUID)), | |
139 notify_session_alias_(NULL), | |
140 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), | |
141 task_runner_(new base::TestSimpleTaskRunner) {} | |
142 | |
143 void SetUp() override { | |
144 device_ = base::MakeUnique<NiceMock<device::MockBluetoothDevice>>( | |
145 adapter_.get(), 0, cryptauth::kTestRemoteDeviceName, | |
146 cryptauth::kTestRemoteDeviceBluetoothAddress, false, false); | |
147 | |
148 service_ = base::MakeUnique<NiceMock<device::MockBluetoothGattService>>( | |
149 device_.get(), kServiceID, service_uuid_, true, false); | |
150 to_peripheral_char_ = | |
151 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>( | |
152 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, | |
153 false, kCharacteristicProperties, | |
154 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE); | |
155 | |
156 from_peripheral_char_ = | |
157 base::MakeUnique<NiceMock<device::MockBluetoothGattCharacteristic>>( | |
158 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, | |
159 false, kCharacteristicProperties, | |
160 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE); | |
161 | |
162 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | |
163 | |
164 std::vector<const device::BluetoothDevice*> devices; | |
165 devices.push_back(device_.get()); | |
166 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | |
167 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress)) | |
168 .WillByDefault(Return(device_.get())); | |
169 ON_CALL(*device_, GetGattService(kServiceID)) | |
170 .WillByDefault(Return(service_.get())); | |
171 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) | |
172 .WillByDefault(Return(from_peripheral_char_.get())); | |
173 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) | |
174 .WillByDefault(Return(to_peripheral_char_.get())); | |
175 } | |
176 | |
177 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED | |
178 // state. | |
179 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { | |
180 EXPECT_CALL(*adapter_, AddObserver(_)); | |
181 EXPECT_CALL(*adapter_, RemoveObserver(_)); | |
182 | |
183 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
184 new MockBluetoothLowEnergyConnection( | |
185 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
186 kMaxNumberOfTries)); | |
187 | |
188 EXPECT_EQ(connection->sub_status(), | |
189 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
190 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED); | |
191 | |
192 connection->SetTaskRunnerForTesting(task_runner_); | |
193 | |
194 return connection; | |
195 } | |
196 | |
197 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS | |
198 // state, without an existing GATT connection. | |
199 void ConnectGatt(MockBluetoothLowEnergyConnection* connection) { | |
200 // Preparing |connection| for a CreateGattConnection call. | |
201 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
202 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
203 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
204 | |
205 // No throttling by default | |
206 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
207 .WillOnce(Return(base::TimeDelta())); | |
208 | |
209 connection->Connect(); | |
210 | |
211 EXPECT_EQ(connection->sub_status(), | |
212 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); | |
213 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS); | |
214 | |
215 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
216 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
217 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
218 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
219 .WillOnce(DoAll( | |
220 SaveArg<0>(&characteristics_finder_success_callback_), | |
221 SaveArg<1>(&characteristics_finder_error_callback_), | |
222 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
223 | |
224 create_gatt_connection_success_callback_.Run( | |
225 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( | |
226 adapter_, cryptauth::kTestRemoteDeviceBluetoothAddress)); | |
227 | |
228 EXPECT_EQ(connection->sub_status(), | |
229 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS); | |
230 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS); | |
231 } | |
232 | |
233 // Transitions |connection| from WAITING_CHARACTERISTICS to | |
234 // WAITING_NOTIFY_SESSION state. | |
235 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { | |
236 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)) | |
237 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), | |
238 SaveArg<1>(¬ify_session_error_callback_))); | |
239 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); | |
240 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); | |
241 | |
242 characteristics_finder_success_callback_.Run( | |
243 {service_uuid_, kServiceID}, | |
244 {to_peripheral_char_uuid_, kToPeripheralCharID}, | |
245 {from_peripheral_char_uuid_, kFromPeripheralCharID}); | |
246 | |
247 EXPECT_EQ(connection->sub_status(), | |
248 BluetoothLowEnergyConnection::SubStatus::WAITING_NOTIFY_SESSION); | |
249 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS); | |
250 } | |
251 | |
252 // Transitions |connection| from WAITING_NOTIFY_SESSION to | |
253 // WAITING_RESPONSE_SIGNAL state. | |
254 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { | |
255 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
256 .WillOnce( | |
257 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | |
258 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
259 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
260 EXPECT_FALSE(notify_session_error_callback_.is_null()); | |
261 ASSERT_FALSE(notify_session_success_callback_.is_null()); | |
262 | |
263 // Store an alias for the notify session passed |connection|. | |
264 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( | |
265 new NiceMock<device::MockBluetoothGattNotifySession>( | |
266 to_peripheral_char_->GetWeakPtr())); | |
267 notify_session_alias_ = notify_session.get(); | |
268 | |
269 notify_session_success_callback_.Run(std::move(notify_session)); | |
270 task_runner_->RunUntilIdle(); | |
271 | |
272 EXPECT_EQ(connection->sub_status(), | |
273 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); | |
274 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS); | |
275 } | |
276 | |
277 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. | |
278 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { | |
279 // Written value contains only the | |
280 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. | |
281 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( | |
282 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: | |
283 kInviteToConnectSignal)); | |
284 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | |
285 kInviteToConnectSignal); | |
286 | |
287 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | |
288 RunWriteCharacteristicSuccessCallback(); | |
289 | |
290 // Received the | |
291 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. | |
292 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector( | |
293 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: | |
294 kInvitationResponseSignal)); | |
295 connection->GattCharacteristicValueChanged( | |
296 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); | |
297 | |
298 EXPECT_EQ(connection->sub_status(), | |
299 BluetoothLowEnergyConnection::SubStatus::CONNECTED); | |
300 EXPECT_EQ(connection->status(), cryptauth::Connection::CONNECTED); | |
301 } | |
302 | |
303 // Transitions |connection| to a DISCONNECTED state regardless of its initial | |
304 // state. | |
305 void Disconnect(MockBluetoothLowEnergyConnection* connection) { | |
306 // A notify session was previously set. | |
307 if (notify_session_alias_) | |
308 EXPECT_CALL(*notify_session_alias_, Stop(_)); | |
309 | |
310 connection->Disconnect(); | |
311 | |
312 EXPECT_EQ(connection->sub_status(), | |
313 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
314 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED); | |
315 } | |
316 | |
317 void InitializeConnection(MockBluetoothLowEnergyConnection* connection) { | |
318 ConnectGatt(connection); | |
319 CharacteristicsFound(connection); | |
320 NotifySessionStarted(connection); | |
321 ResponseSignalReceived(connection); | |
322 } | |
323 | |
324 void RunWriteCharacteristicSuccessCallback() { | |
325 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
326 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
327 write_remote_characteristic_success_callback_.Run(); | |
328 } | |
329 | |
330 std::vector<uint8_t> CreateSendSignalWithSize(int message_size) { | |
331 std::vector<uint8_t> value = ToByteVector(static_cast<uint32_t>( | |
332 BluetoothLowEnergyConnection::ControlSignal::kSendSignal)); | |
333 std::vector<uint8_t> size = | |
334 ToByteVector(static_cast<uint32_t>(message_size)); | |
335 value.insert(value.end(), size.begin(), size.end()); | |
336 return value; | |
337 } | |
338 | |
339 std::vector<uint8_t> CreateFirstCharacteristicValue( | |
340 const std::string& message, | |
341 int size) { | |
342 std::vector<uint8_t> value(CreateSendSignalWithSize(size)); | |
343 std::vector<uint8_t> bytes(message.begin(), message.end()); | |
344 value.insert(value.end(), bytes.begin(), bytes.end()); | |
345 return value; | |
346 } | |
347 | |
348 std::vector<uint8_t> ToByteVector(uint32_t value) { | |
349 std::vector<uint8_t> bytes(4, 0); | |
350 bytes[0] = static_cast<uint8_t>(value); | |
351 bytes[1] = static_cast<uint8_t>(value >> 8); | |
352 bytes[2] = static_cast<uint8_t>(value >> 16); | |
353 bytes[3] = static_cast<uint8_t>(value >> 24); | |
354 return bytes; | |
355 } | |
356 | |
357 protected: | |
358 scoped_refptr<device::MockBluetoothAdapter> adapter_; | |
359 cryptauth::RemoteDevice remote_device_; | |
360 device::BluetoothUUID service_uuid_; | |
361 device::BluetoothUUID to_peripheral_char_uuid_; | |
362 device::BluetoothUUID from_peripheral_char_uuid_; | |
363 std::unique_ptr<device::MockBluetoothDevice> device_; | |
364 std::unique_ptr<device::MockBluetoothGattService> service_; | |
365 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; | |
366 std::unique_ptr<device::MockBluetoothGattCharacteristic> | |
367 from_peripheral_char_; | |
368 std::vector<uint8_t> last_value_written_on_to_peripheral_char_; | |
369 device::MockBluetoothGattNotifySession* notify_session_alias_; | |
370 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; | |
371 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
372 base::MessageLoop message_loop_; | |
373 | |
374 // Callbacks | |
375 device::BluetoothDevice::GattConnectionCallback | |
376 create_gatt_connection_success_callback_; | |
377 device::BluetoothDevice::ConnectErrorCallback | |
378 create_gatt_connection_error_callback_; | |
379 | |
380 cryptauth::BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | |
381 characteristics_finder_success_callback_; | |
382 cryptauth::BluetoothLowEnergyCharacteristicsFinder::ErrorCallback | |
383 characteristics_finder_error_callback_; | |
384 | |
385 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback | |
386 notify_session_success_callback_; | |
387 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
388 notify_session_error_callback_; | |
389 | |
390 base::Closure write_remote_characteristic_success_callback_; | |
391 device::BluetoothRemoteGattCharacteristic::ErrorCallback | |
392 write_remote_characteristic_error_callback_; | |
393 }; | |
394 | |
395 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
396 CreateAndDestroyWithouthConnectCallDoesntCrash) { | |
397 BluetoothLowEnergyConnection connection( | |
398 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | |
399 kMaxNumberOfTries); | |
400 } | |
401 | |
402 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
403 Disconect_WithoutConnectDoesntCrash) { | |
404 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
405 CreateConnection()); | |
406 Disconnect(connection.get()); | |
407 } | |
408 | |
409 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, Connect_Success) { | |
410 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
411 CreateConnection()); | |
412 ConnectGatt(connection.get()); | |
413 CharacteristicsFound(connection.get()); | |
414 NotifySessionStarted(connection.get()); | |
415 ResponseSignalReceived(connection.get()); | |
416 } | |
417 | |
418 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
419 Connect_Success_Disconnect) { | |
420 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
421 CreateConnection()); | |
422 InitializeConnection(connection.get()); | |
423 Disconnect(connection.get()); | |
424 } | |
425 | |
426 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
427 Connect_Incomplete_Disconnect_FromWaitingCharacteristicsState) { | |
428 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
429 CreateConnection()); | |
430 ConnectGatt(connection.get()); | |
431 Disconnect(connection.get()); | |
432 } | |
433 | |
434 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
435 Connect_Incomplete_Disconnect_FromWaitingNotifySessionState) { | |
436 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
437 CreateConnection()); | |
438 ConnectGatt(connection.get()); | |
439 CharacteristicsFound(connection.get()); | |
440 Disconnect(connection.get()); | |
441 } | |
442 | |
443 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
444 Connect_Incomplete_Disconnect_FromWaitingResponseSignalState) { | |
445 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
446 CreateConnection()); | |
447 ConnectGatt(connection.get()); | |
448 CharacteristicsFound(connection.get()); | |
449 NotifySessionStarted(connection.get()); | |
450 Disconnect(connection.get()); | |
451 } | |
452 | |
453 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
454 Connect_Fails_CharacteristicsNotFound) { | |
455 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
456 CreateConnection()); | |
457 ConnectGatt(connection.get()); | |
458 | |
459 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); | |
460 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); | |
461 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); | |
462 | |
463 characteristics_finder_error_callback_.Run( | |
464 {to_peripheral_char_uuid_, kToPeripheralCharID}, | |
465 {from_peripheral_char_uuid_, kFromPeripheralCharID}); | |
466 | |
467 EXPECT_EQ(connection->sub_status(), | |
468 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
469 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED); | |
470 } | |
471 | |
472 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
473 Connect_Fails_NotifySessionError) { | |
474 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
475 CreateConnection()); | |
476 ConnectGatt(connection.get()); | |
477 CharacteristicsFound(connection.get()); | |
478 | |
479 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
480 .Times(0); | |
481 EXPECT_FALSE(notify_session_success_callback_.is_null()); | |
482 ASSERT_FALSE(notify_session_error_callback_.is_null()); | |
483 | |
484 notify_session_error_callback_.Run( | |
485 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
486 | |
487 EXPECT_EQ(connection->sub_status(), | |
488 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
489 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED); | |
490 } | |
491 | |
492 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
493 Connect_Fails_ErrorSendingInviteToConnectSignal) { | |
494 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
495 CreateConnection()); | |
496 ConnectGatt(connection.get()); | |
497 CharacteristicsFound(connection.get()); | |
498 NotifySessionStarted(connection.get()); | |
499 | |
500 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the | |
501 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for | |
502 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use | |
503 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | |
504 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | |
505 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
506 .Times(kMaxNumberOfTries - 1) | |
507 .WillRepeatedly( | |
508 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | |
509 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
510 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
511 | |
512 for (int i = 0; i < kMaxNumberOfTries; i++) { | |
513 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( | |
514 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: | |
515 kInviteToConnectSignal)); | |
516 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | |
517 kInviteToConnectSignal); | |
518 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | |
519 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | |
520 write_remote_characteristic_error_callback_.Run( | |
521 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); | |
522 } | |
523 | |
524 EXPECT_EQ(connection->sub_status(), | |
525 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | |
526 EXPECT_EQ(connection->status(), cryptauth::Connection::DISCONNECTED); | |
527 } | |
528 | |
529 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
530 Receive_MessageSmallerThanCharacteristicSize) { | |
531 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
532 CreateConnection()); | |
533 InitializeConnection(connection.get()); | |
534 | |
535 std::string received_bytes; | |
536 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
537 .WillOnce(SaveArg<0>(&received_bytes)); | |
538 | |
539 // Message (bytes) that is going to be received. | |
540 std::string message(100, 'A'); | |
541 | |
542 // Sending the |kSendSignal| + |message_size| + |message|. | |
543 connection->GattCharacteristicValueChanged( | |
544 adapter_.get(), from_peripheral_char_.get(), | |
545 CreateFirstCharacteristicValue(message, message.size())); | |
546 | |
547 EXPECT_EQ(received_bytes, message); | |
548 } | |
549 | |
550 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
551 Receive_MessageLargerThanCharacteristicSize) { | |
552 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
553 CreateConnection()); | |
554 InitializeConnection(connection.get()); | |
555 | |
556 std::string received_bytes; | |
557 int chunk_size = 500; | |
558 EXPECT_CALL(*connection, OnBytesReceived(_)) | |
559 .WillOnce(SaveArg<0>(&received_bytes)); | |
560 | |
561 // Message (bytes) that is going to be received. | |
562 int message_size = 600; | |
563 std::string message(message_size, 'A'); | |
564 | |
565 // Sending the |kSendSignal| + |message_size| + |message| (truncated at | |
566 // |chunk_size|). | |
567 int first_write_payload_size = | |
568 chunk_size - CreateSendSignalWithSize(message_size).size(); | |
569 connection->GattCharacteristicValueChanged( | |
570 adapter_.get(), from_peripheral_char_.get(), | |
571 CreateFirstCharacteristicValue( | |
572 message.substr(0, first_write_payload_size), message.size())); | |
573 | |
574 // Sending the remaining bytes. | |
575 std::vector<uint8_t> value; | |
576 value.push_back(0); | |
577 value.insert(value.end(), message.begin() + first_write_payload_size, | |
578 message.end()); | |
579 connection->GattCharacteristicValueChanged( | |
580 adapter_.get(), from_peripheral_char_.get(), value); | |
581 | |
582 EXPECT_EQ(received_bytes, message); | |
583 } | |
584 | |
585 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
586 SendMessage_SmallerThanCharacteristicSize) { | |
587 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
588 CreateConnection()); | |
589 InitializeConnection(connection.get()); | |
590 | |
591 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
592 // called. | |
593 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
594 .WillOnce( | |
595 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | |
596 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
597 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
598 | |
599 // Message (bytes) that is going to be sent. | |
600 int message_size = 100; | |
601 std::string message(message_size, 'A'); | |
602 message[0] = 'B'; | |
603 | |
604 std::unique_ptr<cryptauth::FakeWireMessage> wire_message = | |
605 base::MakeUnique<cryptauth::FakeWireMessage>(message, | |
606 std::string(kTestFeature)); | |
607 std::string serialized_message = wire_message->Serialize(); | |
608 | |
609 connection->SendMessage(std::move(wire_message)); | |
610 | |
611 // Expecting that |kSendSignal| + |message_size| + |message| was written. | |
612 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | |
613 CreateFirstCharacteristicValue(serialized_message, | |
614 serialized_message.size())); | |
615 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | |
616 | |
617 RunWriteCharacteristicSuccessCallback(); | |
618 } | |
619 | |
620 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
621 SendMessage_LargerThanCharacteristicSize) { | |
622 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
623 CreateConnection()); | |
624 InitializeConnection(connection.get()); | |
625 | |
626 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | |
627 // called. | |
628 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
629 .WillOnce( | |
630 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | |
631 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
632 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
633 | |
634 // Message (bytes) that is going to be sent. | |
635 int message_size = 600; | |
636 std::string message(message_size, 'A'); | |
637 message[0] = 'B'; | |
638 | |
639 std::unique_ptr<cryptauth::FakeWireMessage> wire_message = | |
640 base::MakeUnique<cryptauth::FakeWireMessage>(message, | |
641 std::string(kTestFeature)); | |
642 std::string serialized_message = wire_message->Serialize(); | |
643 | |
644 // Send the message. | |
645 connection->SendMessage(std::move(wire_message)); | |
646 | |
647 // Expecting that |kSendSignal| + |serialized_message| was written in the | |
648 // first 8 bytes. | |
649 std::vector<uint8_t> prefix( | |
650 last_value_written_on_to_peripheral_char_.begin(), | |
651 last_value_written_on_to_peripheral_char_.begin() + 8); | |
652 EXPECT_EQ(prefix, CreateSendSignalWithSize(serialized_message.size())); | |
653 std::vector<uint8_t> bytes_received( | |
654 last_value_written_on_to_peripheral_char_.begin() + 8, | |
655 last_value_written_on_to_peripheral_char_.end()); | |
656 | |
657 // Expecting a second call of WriteRemoteCharacteristic, after success | |
658 // callback is called. | |
659 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | |
660 .WillOnce( | |
661 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | |
662 SaveArg<1>(&write_remote_characteristic_success_callback_), | |
663 SaveArg<2>(&write_remote_characteristic_error_callback_))); | |
664 | |
665 RunWriteCharacteristicSuccessCallback(); | |
666 bytes_received.insert(bytes_received.end(), | |
667 last_value_written_on_to_peripheral_char_.begin() + 1, | |
668 last_value_written_on_to_peripheral_char_.end()); | |
669 | |
670 // Expecting that the message was written. | |
671 std::vector<uint8_t> expected_value(serialized_message.begin(), | |
672 serialized_message.end()); | |
673 EXPECT_EQ(expected_value.size(), bytes_received.size()); | |
674 EXPECT_EQ(expected_value, bytes_received); | |
675 | |
676 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | |
677 RunWriteCharacteristicSuccessCallback(); | |
678 } | |
679 | |
680 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | |
681 Connect_AfterADelayWhenThrottled) { | |
682 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( | |
683 CreateConnection()); | |
684 | |
685 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | |
686 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); | |
687 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | |
688 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | |
689 SaveArg<1>(&create_gatt_connection_error_callback_))); | |
690 | |
691 // No GATT connection should be created before the delay. | |
692 connection->Connect(); | |
693 EXPECT_EQ(connection->sub_status(), | |
694 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); | |
695 EXPECT_EQ(connection->status(), cryptauth::Connection::IN_PROGRESS); | |
696 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); | |
697 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); | |
698 | |
699 // A GATT connection should be created after the delay. | |
700 task_runner_->RunUntilIdle(); | |
701 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | |
702 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | |
703 | |
704 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | |
705 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | |
706 .WillOnce(DoAll( | |
707 SaveArg<0>(&characteristics_finder_success_callback_), | |
708 SaveArg<1>(&characteristics_finder_error_callback_), | |
709 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | |
710 | |
711 create_gatt_connection_success_callback_.Run( | |
712 base::MakeUnique<NiceMock<device::MockBluetoothGattConnection>>( | |
713 adapter_, cryptauth::kTestRemoteDeviceBluetoothAddress)); | |
714 | |
715 CharacteristicsFound(connection.get()); | |
716 NotifySessionStarted(connection.get()); | |
717 ResponseSignalReceived(connection.get()); | |
718 } | |
719 | |
720 } // namespace proximity_auth | |
OLD | NEW |