| 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> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 8 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 12 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 13 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | 16 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" |
| 14 #include "components/proximity_auth/bluetooth_throttler.h" | 17 #include "components/proximity_auth/bluetooth_throttler.h" |
| 15 #include "components/proximity_auth/connection_finder.h" | 18 #include "components/proximity_auth/connection_finder.h" |
| 16 #include "components/proximity_auth/proximity_auth_test_util.h" | 19 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 17 #include "components/proximity_auth/remote_device.h" | 20 #include "components/proximity_auth/remote_device.h" |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 265 |
| 263 EXPECT_EQ(connection->sub_status(), | 266 EXPECT_EQ(connection->sub_status(), |
| 264 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); | 267 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); |
| 265 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 268 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 266 } | 269 } |
| 267 | 270 |
| 268 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. | 271 // Transitions |connection| from WAITING_RESPONSE_SIGNAL to CONNECTED state. |
| 269 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { | 272 void ResponseSignalReceived(MockBluetoothLowEnergyConnection* connection) { |
| 270 // Written value contains only the | 273 // Written value contains only the |
| 271 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. | 274 // BluetoothLowEneryConnection::ControlSignal::kInviteToConnectSignal. |
| 272 const std::vector<uint8> kInviteToConnectSignal = ToByteVector(static_cast< | 275 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( |
| 273 uint32>( | 276 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: |
| 274 BluetoothLowEnergyConnection::ControlSignal::kInviteToConnectSignal)); | 277 kInviteToConnectSignal)); |
| 275 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | 278 EXPECT_EQ(last_value_written_on_to_peripheral_char_, |
| 276 kInviteToConnectSignal); | 279 kInviteToConnectSignal); |
| 277 | 280 |
| 278 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | 281 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); |
| 279 RunWriteCharacteristicSuccessCallback(); | 282 RunWriteCharacteristicSuccessCallback(); |
| 280 | 283 |
| 281 // Received the | 284 // Received the |
| 282 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. | 285 // BluetoothLowEneryConnection::ControlSignal::kInvitationResponseSignal. |
| 283 const std::vector<uint8> kInvitationResponseSignal = ToByteVector( | 286 const std::vector<uint8_t> kInvitationResponseSignal = ToByteVector( |
| 284 static_cast<uint32>(BluetoothLowEnergyConnection::ControlSignal:: | 287 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: |
| 285 kInvitationResponseSignal)); | 288 kInvitationResponseSignal)); |
| 286 connection->GattCharacteristicValueChanged( | 289 connection->GattCharacteristicValueChanged( |
| 287 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); | 290 adapter_.get(), from_peripheral_char_.get(), kInvitationResponseSignal); |
| 288 | 291 |
| 289 EXPECT_EQ(connection->sub_status(), | 292 EXPECT_EQ(connection->sub_status(), |
| 290 BluetoothLowEnergyConnection::SubStatus::CONNECTED); | 293 BluetoothLowEnergyConnection::SubStatus::CONNECTED); |
| 291 EXPECT_EQ(connection->status(), Connection::CONNECTED); | 294 EXPECT_EQ(connection->status(), Connection::CONNECTED); |
| 292 } | 295 } |
| 293 | 296 |
| 294 // Transitions |connection| to a DISCONNECTED state regardless of its initial | 297 // Transitions |connection| to a DISCONNECTED state regardless of its initial |
| 295 // state. | 298 // state. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 311 NotifySessionStarted(connection); | 314 NotifySessionStarted(connection); |
| 312 ResponseSignalReceived(connection); | 315 ResponseSignalReceived(connection); |
| 313 } | 316 } |
| 314 | 317 |
| 315 void RunWriteCharacteristicSuccessCallback() { | 318 void RunWriteCharacteristicSuccessCallback() { |
| 316 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); | 319 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 317 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); | 320 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 318 write_remote_characteristic_success_callback_.Run(); | 321 write_remote_characteristic_success_callback_.Run(); |
| 319 } | 322 } |
| 320 | 323 |
| 321 std::vector<uint8> CreateSendSignalWithSize(int message_size) { | 324 std::vector<uint8_t> CreateSendSignalWithSize(int message_size) { |
| 322 std::vector<uint8> value = ToByteVector(static_cast<uint32>( | 325 std::vector<uint8_t> value = ToByteVector(static_cast<uint32_t>( |
| 323 BluetoothLowEnergyConnection::ControlSignal::kSendSignal)); | 326 BluetoothLowEnergyConnection::ControlSignal::kSendSignal)); |
| 324 std::vector<uint8> size = ToByteVector(static_cast<uint32>(message_size)); | 327 std::vector<uint8_t> size = |
| 328 ToByteVector(static_cast<uint32_t>(message_size)); |
| 325 value.insert(value.end(), size.begin(), size.end()); | 329 value.insert(value.end(), size.begin(), size.end()); |
| 326 return value; | 330 return value; |
| 327 } | 331 } |
| 328 | 332 |
| 329 std::vector<uint8> CreateFirstCharacteristicValue(const std::string& message, | 333 std::vector<uint8_t> CreateFirstCharacteristicValue( |
| 330 int size) { | 334 const std::string& message, |
| 331 std::vector<uint8> value(CreateSendSignalWithSize(size)); | 335 int size) { |
| 332 std::vector<uint8> bytes(message.begin(), message.end()); | 336 std::vector<uint8_t> value(CreateSendSignalWithSize(size)); |
| 337 std::vector<uint8_t> bytes(message.begin(), message.end()); |
| 333 value.insert(value.end(), bytes.begin(), bytes.end()); | 338 value.insert(value.end(), bytes.begin(), bytes.end()); |
| 334 return value; | 339 return value; |
| 335 } | 340 } |
| 336 | 341 |
| 337 std::vector<uint8> ToByteVector(uint32 value) { | 342 std::vector<uint8_t> ToByteVector(uint32_t value) { |
| 338 std::vector<uint8> bytes(4, 0); | 343 std::vector<uint8_t> bytes(4, 0); |
| 339 bytes[0] = static_cast<uint8>(value); | 344 bytes[0] = static_cast<uint8_t>(value); |
| 340 bytes[1] = static_cast<uint8>(value >> 8); | 345 bytes[1] = static_cast<uint8_t>(value >> 8); |
| 341 bytes[2] = static_cast<uint8>(value >> 16); | 346 bytes[2] = static_cast<uint8_t>(value >> 16); |
| 342 bytes[3] = static_cast<uint8>(value >> 24); | 347 bytes[3] = static_cast<uint8_t>(value >> 24); |
| 343 return bytes; | 348 return bytes; |
| 344 } | 349 } |
| 345 | 350 |
| 346 protected: | 351 protected: |
| 347 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 352 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 348 RemoteDevice remote_device_; | 353 RemoteDevice remote_device_; |
| 349 device::BluetoothUUID service_uuid_; | 354 device::BluetoothUUID service_uuid_; |
| 350 device::BluetoothUUID to_peripheral_char_uuid_; | 355 device::BluetoothUUID to_peripheral_char_uuid_; |
| 351 device::BluetoothUUID from_peripheral_char_uuid_; | 356 device::BluetoothUUID from_peripheral_char_uuid_; |
| 352 scoped_ptr<device::MockBluetoothDevice> device_; | 357 scoped_ptr<device::MockBluetoothDevice> device_; |
| 353 scoped_ptr<device::MockBluetoothGattService> service_; | 358 scoped_ptr<device::MockBluetoothGattService> service_; |
| 354 scoped_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; | 359 scoped_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; |
| 355 scoped_ptr<device::MockBluetoothGattCharacteristic> from_peripheral_char_; | 360 scoped_ptr<device::MockBluetoothGattCharacteristic> from_peripheral_char_; |
| 356 std::vector<uint8> last_value_written_on_to_peripheral_char_; | 361 std::vector<uint8_t> last_value_written_on_to_peripheral_char_; |
| 357 device::MockBluetoothGattNotifySession* notify_session_alias_; | 362 device::MockBluetoothGattNotifySession* notify_session_alias_; |
| 358 scoped_ptr<MockBluetoothThrottler> bluetooth_throttler_; | 363 scoped_ptr<MockBluetoothThrottler> bluetooth_throttler_; |
| 359 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 364 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 360 base::MessageLoop message_loop_; | 365 base::MessageLoop message_loop_; |
| 361 | 366 |
| 362 // Callbacks | 367 // Callbacks |
| 363 device::BluetoothDevice::GattConnectionCallback | 368 device::BluetoothDevice::GattConnectionCallback |
| 364 create_gatt_connection_success_callback_; | 369 create_gatt_connection_success_callback_; |
| 365 device::BluetoothDevice::ConnectErrorCallback | 370 device::BluetoothDevice::ConnectErrorCallback |
| 366 create_gatt_connection_error_callback_; | 371 create_gatt_connection_error_callback_; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | 487 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. |
| 483 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | 488 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); |
| 484 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 489 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| 485 .Times(kMaxNumberOfTries - 1) | 490 .Times(kMaxNumberOfTries - 1) |
| 486 .WillRepeatedly( | 491 .WillRepeatedly( |
| 487 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 492 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
| 488 SaveArg<1>(&write_remote_characteristic_success_callback_), | 493 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 489 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 494 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 490 | 495 |
| 491 for (int i = 0; i < kMaxNumberOfTries; i++) { | 496 for (int i = 0; i < kMaxNumberOfTries; i++) { |
| 492 const std::vector<uint8> kInviteToConnectSignal = ToByteVector(static_cast< | 497 const std::vector<uint8_t> kInviteToConnectSignal = ToByteVector( |
| 493 uint32>( | 498 static_cast<uint32_t>(BluetoothLowEnergyConnection::ControlSignal:: |
| 494 BluetoothLowEnergyConnection::ControlSignal::kInviteToConnectSignal)); | 499 kInviteToConnectSignal)); |
| 495 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | 500 EXPECT_EQ(last_value_written_on_to_peripheral_char_, |
| 496 kInviteToConnectSignal); | 501 kInviteToConnectSignal); |
| 497 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); | 502 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 498 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); | 503 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 499 write_remote_characteristic_error_callback_.Run( | 504 write_remote_characteristic_error_callback_.Run( |
| 500 device::BluetoothGattService::GATT_ERROR_UNKNOWN); | 505 device::BluetoothGattService::GATT_ERROR_UNKNOWN); |
| 501 } | 506 } |
| 502 | 507 |
| 503 EXPECT_EQ(connection->sub_status(), | 508 EXPECT_EQ(connection->sub_status(), |
| 504 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | 509 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 // Sending the |kSendSignal| + |message_size| + |message| (truncated at | 547 // Sending the |kSendSignal| + |message_size| + |message| (truncated at |
| 543 // |chunk_size|). | 548 // |chunk_size|). |
| 544 int first_write_payload_size = | 549 int first_write_payload_size = |
| 545 chunk_size - CreateSendSignalWithSize(message_size).size(); | 550 chunk_size - CreateSendSignalWithSize(message_size).size(); |
| 546 connection->GattCharacteristicValueChanged( | 551 connection->GattCharacteristicValueChanged( |
| 547 adapter_.get(), from_peripheral_char_.get(), | 552 adapter_.get(), from_peripheral_char_.get(), |
| 548 CreateFirstCharacteristicValue( | 553 CreateFirstCharacteristicValue( |
| 549 message.substr(0, first_write_payload_size), message.size())); | 554 message.substr(0, first_write_payload_size), message.size())); |
| 550 | 555 |
| 551 // Sending the remaining bytes. | 556 // Sending the remaining bytes. |
| 552 std::vector<uint8> value; | 557 std::vector<uint8_t> value; |
| 553 value.push_back(0); | 558 value.push_back(0); |
| 554 value.insert(value.end(), message.begin() + first_write_payload_size, | 559 value.insert(value.end(), message.begin() + first_write_payload_size, |
| 555 message.end()); | 560 message.end()); |
| 556 connection->GattCharacteristicValueChanged( | 561 connection->GattCharacteristicValueChanged( |
| 557 adapter_.get(), from_peripheral_char_.get(), value); | 562 adapter_.get(), from_peripheral_char_.get(), value); |
| 558 | 563 |
| 559 EXPECT_EQ(received_bytes, message); | 564 EXPECT_EQ(received_bytes, message); |
| 560 } | 565 } |
| 561 | 566 |
| 562 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 567 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 605 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 601 | 606 |
| 602 // Message (bytes) that is going to be sent. | 607 // Message (bytes) that is going to be sent. |
| 603 int message_size = 600; | 608 int message_size = 600; |
| 604 std::string message(message_size, 'A'); | 609 std::string message(message_size, 'A'); |
| 605 message[0] = 'B'; | 610 message[0] = 'B'; |
| 606 connection->SendMessage(make_scoped_ptr(new FakeWireMessage(message))); | 611 connection->SendMessage(make_scoped_ptr(new FakeWireMessage(message))); |
| 607 | 612 |
| 608 // Expecting that |kSendSignal| + |message_size| was written in the first 8 | 613 // Expecting that |kSendSignal| + |message_size| was written in the first 8 |
| 609 // bytes. | 614 // bytes. |
| 610 std::vector<uint8> prefix( | 615 std::vector<uint8_t> prefix( |
| 611 last_value_written_on_to_peripheral_char_.begin(), | 616 last_value_written_on_to_peripheral_char_.begin(), |
| 612 last_value_written_on_to_peripheral_char_.begin() + 8); | 617 last_value_written_on_to_peripheral_char_.begin() + 8); |
| 613 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); | 618 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); |
| 614 std::vector<uint8> bytes_received( | 619 std::vector<uint8_t> bytes_received( |
| 615 last_value_written_on_to_peripheral_char_.begin() + 8, | 620 last_value_written_on_to_peripheral_char_.begin() + 8, |
| 616 last_value_written_on_to_peripheral_char_.end()); | 621 last_value_written_on_to_peripheral_char_.end()); |
| 617 | 622 |
| 618 // Expecting a second call of WriteRemoteCharacteristic, after success | 623 // Expecting a second call of WriteRemoteCharacteristic, after success |
| 619 // callback is called. | 624 // callback is called. |
| 620 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 625 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| 621 .WillOnce( | 626 .WillOnce( |
| 622 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 627 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
| 623 SaveArg<1>(&write_remote_characteristic_success_callback_), | 628 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 624 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 629 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 625 | 630 |
| 626 RunWriteCharacteristicSuccessCallback(); | 631 RunWriteCharacteristicSuccessCallback(); |
| 627 bytes_received.insert(bytes_received.end(), | 632 bytes_received.insert(bytes_received.end(), |
| 628 last_value_written_on_to_peripheral_char_.begin() + 1, | 633 last_value_written_on_to_peripheral_char_.begin() + 1, |
| 629 last_value_written_on_to_peripheral_char_.end()); | 634 last_value_written_on_to_peripheral_char_.end()); |
| 630 | 635 |
| 631 // Expecting that the message was written. | 636 // Expecting that the message was written. |
| 632 std::vector<uint8> expected_value(message.begin(), message.end()); | 637 std::vector<uint8_t> expected_value(message.begin(), message.end()); |
| 633 EXPECT_EQ(expected_value.size(), bytes_received.size()); | 638 EXPECT_EQ(expected_value.size(), bytes_received.size()); |
| 634 EXPECT_EQ(expected_value, bytes_received); | 639 EXPECT_EQ(expected_value, bytes_received); |
| 635 | 640 |
| 636 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | 641 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); |
| 637 RunWriteCharacteristicSuccessCallback(); | 642 RunWriteCharacteristicSuccessCallback(); |
| 638 } | 643 } |
| 639 | 644 |
| 640 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 645 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 641 Connect_AfterADelayWhenThrottled) { | 646 Connect_AfterADelayWhenThrottled) { |
| 642 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 647 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 670 create_gatt_connection_success_callback_.Run( | 675 create_gatt_connection_success_callback_.Run( |
| 671 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( | 676 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( |
| 672 adapter_, kTestRemoteDeviceBluetoothAddress))); | 677 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 673 | 678 |
| 674 CharacteristicsFound(connection.get()); | 679 CharacteristicsFound(connection.get()); |
| 675 NotifySessionStarted(connection.get()); | 680 NotifySessionStarted(connection.get()); |
| 676 ResponseSignalReceived(connection.get()); | 681 ResponseSignalReceived(connection.get()); |
| 677 } | 682 } |
| 678 | 683 |
| 679 } // namespace proximity_auth | 684 } // namespace proximity_auth |
| OLD | NEW |