| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 16 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/test_simple_task_runner.h" |
| 17 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | 19 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" |
| 18 #include "components/proximity_auth/bluetooth_throttler.h" | 20 #include "components/proximity_auth/bluetooth_throttler.h" |
| 19 #include "components/proximity_auth/connection_finder.h" | 21 #include "components/proximity_auth/connection_finder.h" |
| 20 #include "components/proximity_auth/proximity_auth_test_util.h" | 22 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 21 #include "components/proximity_auth/remote_device.h" | 23 #include "components/proximity_auth/remote_device.h" |
| 22 #include "components/proximity_auth/wire_message.h" | 24 #include "components/proximity_auth/wire_message.h" |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" | 25 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 remote_device_(CreateLERemoteDeviceForTest()), | 131 remote_device_(CreateLERemoteDeviceForTest()), |
| 130 service_uuid_(device::BluetoothUUID(kServiceUUID)), | 132 service_uuid_(device::BluetoothUUID(kServiceUUID)), |
| 131 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), | 133 to_peripheral_char_uuid_(device::BluetoothUUID(kToPeripheralCharUUID)), |
| 132 from_peripheral_char_uuid_( | 134 from_peripheral_char_uuid_( |
| 133 device::BluetoothUUID(kFromPeripheralCharUUID)), | 135 device::BluetoothUUID(kFromPeripheralCharUUID)), |
| 134 notify_session_alias_(NULL), | 136 notify_session_alias_(NULL), |
| 135 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), | 137 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), |
| 136 task_runner_(new base::TestSimpleTaskRunner) {} | 138 task_runner_(new base::TestSimpleTaskRunner) {} |
| 137 | 139 |
| 138 void SetUp() override { | 140 void SetUp() override { |
| 139 device_ = make_scoped_ptr(new NiceMock<device::MockBluetoothDevice>( | 141 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( |
| 140 adapter_.get(), 0, kTestRemoteDeviceName, | 142 adapter_.get(), 0, kTestRemoteDeviceName, |
| 141 kTestRemoteDeviceBluetoothAddress, false, false)); | 143 kTestRemoteDeviceBluetoothAddress, false, false)); |
| 142 | 144 |
| 143 service_ = make_scoped_ptr(new NiceMock<device::MockBluetoothGattService>( | 145 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( |
| 144 device_.get(), kServiceID, service_uuid_, true, false)); | 146 device_.get(), kServiceID, service_uuid_, true, false)); |
| 145 to_peripheral_char_ = | 147 to_peripheral_char_ = |
| 146 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>( | 148 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( |
| 147 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, | 149 service_.get(), kToPeripheralCharID, to_peripheral_char_uuid_, |
| 148 false, kCharacteristicProperties, | 150 false, kCharacteristicProperties, |
| 149 device::BluetoothGattCharacteristic::PERMISSION_NONE)); | 151 device::BluetoothGattCharacteristic::PERMISSION_NONE)); |
| 150 | 152 |
| 151 from_peripheral_char_ = | 153 from_peripheral_char_ = |
| 152 make_scoped_ptr(new NiceMock<device::MockBluetoothGattCharacteristic>( | 154 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( |
| 153 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, | 155 service_.get(), kFromPeripheralCharID, from_peripheral_char_uuid_, |
| 154 false, kCharacteristicProperties, | 156 false, kCharacteristicProperties, |
| 155 device::BluetoothGattCharacteristic::PERMISSION_NONE)); | 157 device::BluetoothGattCharacteristic::PERMISSION_NONE)); |
| 156 | 158 |
| 157 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | 159 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); |
| 158 | 160 |
| 159 std::vector<const device::BluetoothDevice*> devices; | 161 std::vector<const device::BluetoothDevice*> devices; |
| 160 devices.push_back(device_.get()); | 162 devices.push_back(device_.get()); |
| 161 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | 163 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); |
| 162 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 164 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) |
| 163 .WillByDefault(Return(device_.get())); | 165 .WillByDefault(Return(device_.get())); |
| 164 ON_CALL(*device_, GetGattService(kServiceID)) | 166 ON_CALL(*device_, GetGattService(kServiceID)) |
| 165 .WillByDefault(Return(service_.get())); | 167 .WillByDefault(Return(service_.get())); |
| 166 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) | 168 ON_CALL(*service_, GetCharacteristic(kFromPeripheralCharID)) |
| 167 .WillByDefault(Return(from_peripheral_char_.get())); | 169 .WillByDefault(Return(from_peripheral_char_.get())); |
| 168 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) | 170 ON_CALL(*service_, GetCharacteristic(kToPeripheralCharID)) |
| 169 .WillByDefault(Return(to_peripheral_char_.get())); | 171 .WillByDefault(Return(to_peripheral_char_.get())); |
| 170 } | 172 } |
| 171 | 173 |
| 172 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED | 174 // Creates a BluetoothLowEnergyConnection and verifies it's in DISCONNECTED |
| 173 // state. | 175 // state. |
| 174 scoped_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { | 176 std::unique_ptr<MockBluetoothLowEnergyConnection> CreateConnection() { |
| 175 EXPECT_CALL(*adapter_, AddObserver(_)); | 177 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 176 EXPECT_CALL(*adapter_, RemoveObserver(_)); | 178 EXPECT_CALL(*adapter_, RemoveObserver(_)); |
| 177 | 179 |
| 178 scoped_ptr<MockBluetoothLowEnergyConnection> connection( | 180 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 179 new MockBluetoothLowEnergyConnection( | 181 new MockBluetoothLowEnergyConnection( |
| 180 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | 182 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), |
| 181 kMaxNumberOfTries)); | 183 kMaxNumberOfTries)); |
| 182 | 184 |
| 183 EXPECT_EQ(connection->sub_status(), | 185 EXPECT_EQ(connection->sub_status(), |
| 184 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | 186 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); |
| 185 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 187 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 186 | 188 |
| 187 connection->SetTaskRunnerForTesting(task_runner_); | 189 connection->SetTaskRunnerForTesting(task_runner_); |
| 188 | 190 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 210 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | 212 // Preparing |connection| to run |create_gatt_connection_success_callback_|. |
| 211 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | 213 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); |
| 212 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | 214 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); |
| 213 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | 215 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) |
| 214 .WillOnce(DoAll( | 216 .WillOnce(DoAll( |
| 215 SaveArg<0>(&characteristics_finder_success_callback_), | 217 SaveArg<0>(&characteristics_finder_success_callback_), |
| 216 SaveArg<1>(&characteristics_finder_error_callback_), | 218 SaveArg<1>(&characteristics_finder_error_callback_), |
| 217 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | 219 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); |
| 218 | 220 |
| 219 create_gatt_connection_success_callback_.Run( | 221 create_gatt_connection_success_callback_.Run( |
| 220 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( | 222 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( |
| 221 adapter_, kTestRemoteDeviceBluetoothAddress))); | 223 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 222 | 224 |
| 223 EXPECT_EQ(connection->sub_status(), | 225 EXPECT_EQ(connection->sub_status(), |
| 224 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS); | 226 BluetoothLowEnergyConnection::SubStatus::WAITING_CHARACTERISTICS); |
| 225 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 227 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 226 } | 228 } |
| 227 | 229 |
| 228 // Transitions |connection| from WAITING_CHARACTERISTICS to | 230 // Transitions |connection| from WAITING_CHARACTERISTICS to |
| 229 // WAITING_NOTIFY_SESSION state. | 231 // WAITING_NOTIFY_SESSION state. |
| 230 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { | 232 void CharacteristicsFound(MockBluetoothLowEnergyConnection* connection) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 249 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { | 251 void NotifySessionStarted(MockBluetoothLowEnergyConnection* connection) { |
| 250 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 252 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| 251 .WillOnce( | 253 .WillOnce( |
| 252 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 254 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
| 253 SaveArg<1>(&write_remote_characteristic_success_callback_), | 255 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 254 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 256 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 255 EXPECT_FALSE(notify_session_error_callback_.is_null()); | 257 EXPECT_FALSE(notify_session_error_callback_.is_null()); |
| 256 ASSERT_FALSE(notify_session_success_callback_.is_null()); | 258 ASSERT_FALSE(notify_session_success_callback_.is_null()); |
| 257 | 259 |
| 258 // Store an alias for the notify session passed |connection|. | 260 // Store an alias for the notify session passed |connection|. |
| 259 scoped_ptr<device::MockBluetoothGattNotifySession> notify_session( | 261 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( |
| 260 new NiceMock<device::MockBluetoothGattNotifySession>( | 262 new NiceMock<device::MockBluetoothGattNotifySession>( |
| 261 kToPeripheralCharID)); | 263 kToPeripheralCharID)); |
| 262 notify_session_alias_ = notify_session.get(); | 264 notify_session_alias_ = notify_session.get(); |
| 263 | 265 |
| 264 notify_session_success_callback_.Run(std::move(notify_session)); | 266 notify_session_success_callback_.Run(std::move(notify_session)); |
| 265 task_runner_->RunUntilIdle(); | 267 task_runner_->RunUntilIdle(); |
| 266 | 268 |
| 267 EXPECT_EQ(connection->sub_status(), | 269 EXPECT_EQ(connection->sub_status(), |
| 268 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); | 270 BluetoothLowEnergyConnection::SubStatus::WAITING_RESPONSE_SIGNAL); |
| 269 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 271 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 bytes[3] = static_cast<uint8_t>(value >> 24); | 350 bytes[3] = static_cast<uint8_t>(value >> 24); |
| 349 return bytes; | 351 return bytes; |
| 350 } | 352 } |
| 351 | 353 |
| 352 protected: | 354 protected: |
| 353 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 355 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 354 RemoteDevice remote_device_; | 356 RemoteDevice remote_device_; |
| 355 device::BluetoothUUID service_uuid_; | 357 device::BluetoothUUID service_uuid_; |
| 356 device::BluetoothUUID to_peripheral_char_uuid_; | 358 device::BluetoothUUID to_peripheral_char_uuid_; |
| 357 device::BluetoothUUID from_peripheral_char_uuid_; | 359 device::BluetoothUUID from_peripheral_char_uuid_; |
| 358 scoped_ptr<device::MockBluetoothDevice> device_; | 360 std::unique_ptr<device::MockBluetoothDevice> device_; |
| 359 scoped_ptr<device::MockBluetoothGattService> service_; | 361 std::unique_ptr<device::MockBluetoothGattService> service_; |
| 360 scoped_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; | 362 std::unique_ptr<device::MockBluetoothGattCharacteristic> to_peripheral_char_; |
| 361 scoped_ptr<device::MockBluetoothGattCharacteristic> from_peripheral_char_; | 363 std::unique_ptr<device::MockBluetoothGattCharacteristic> |
| 364 from_peripheral_char_; |
| 362 std::vector<uint8_t> last_value_written_on_to_peripheral_char_; | 365 std::vector<uint8_t> last_value_written_on_to_peripheral_char_; |
| 363 device::MockBluetoothGattNotifySession* notify_session_alias_; | 366 device::MockBluetoothGattNotifySession* notify_session_alias_; |
| 364 scoped_ptr<MockBluetoothThrottler> bluetooth_throttler_; | 367 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; |
| 365 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 368 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 366 base::MessageLoop message_loop_; | 369 base::MessageLoop message_loop_; |
| 367 | 370 |
| 368 // Callbacks | 371 // Callbacks |
| 369 device::BluetoothDevice::GattConnectionCallback | 372 device::BluetoothDevice::GattConnectionCallback |
| 370 create_gatt_connection_success_callback_; | 373 create_gatt_connection_success_callback_; |
| 371 device::BluetoothDevice::ConnectErrorCallback | 374 device::BluetoothDevice::ConnectErrorCallback |
| 372 create_gatt_connection_error_callback_; | 375 create_gatt_connection_error_callback_; |
| 373 | 376 |
| 374 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback | 377 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback |
| (...skipping 13 matching lines...) Expand all Loading... |
| 388 | 391 |
| 389 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 392 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 390 CreateAndDestroyWithouthConnectCallDoesntCrash) { | 393 CreateAndDestroyWithouthConnectCallDoesntCrash) { |
| 391 BluetoothLowEnergyConnection connection( | 394 BluetoothLowEnergyConnection connection( |
| 392 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), | 395 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), |
| 393 kMaxNumberOfTries); | 396 kMaxNumberOfTries); |
| 394 } | 397 } |
| 395 | 398 |
| 396 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 399 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 397 Disconect_WithoutConnectDoesntCrash) { | 400 Disconect_WithoutConnectDoesntCrash) { |
| 398 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 401 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 402 CreateConnection()); |
| 399 Disconnect(connection.get()); | 403 Disconnect(connection.get()); |
| 400 } | 404 } |
| 401 | 405 |
| 402 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, Connect_Success) { | 406 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, Connect_Success) { |
| 403 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 407 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 408 CreateConnection()); |
| 404 ConnectGatt(connection.get()); | 409 ConnectGatt(connection.get()); |
| 405 CharacteristicsFound(connection.get()); | 410 CharacteristicsFound(connection.get()); |
| 406 NotifySessionStarted(connection.get()); | 411 NotifySessionStarted(connection.get()); |
| 407 ResponseSignalReceived(connection.get()); | 412 ResponseSignalReceived(connection.get()); |
| 408 } | 413 } |
| 409 | 414 |
| 410 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 415 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 411 Connect_Success_Disconnect) { | 416 Connect_Success_Disconnect) { |
| 412 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 417 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 418 CreateConnection()); |
| 413 InitializeConnection(connection.get()); | 419 InitializeConnection(connection.get()); |
| 414 Disconnect(connection.get()); | 420 Disconnect(connection.get()); |
| 415 } | 421 } |
| 416 | 422 |
| 417 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 423 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 418 Connect_Incomplete_Disconnect_FromWaitingCharacteristicsState) { | 424 Connect_Incomplete_Disconnect_FromWaitingCharacteristicsState) { |
| 419 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 425 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 426 CreateConnection()); |
| 420 ConnectGatt(connection.get()); | 427 ConnectGatt(connection.get()); |
| 421 Disconnect(connection.get()); | 428 Disconnect(connection.get()); |
| 422 } | 429 } |
| 423 | 430 |
| 424 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 431 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 425 Connect_Incomplete_Disconnect_FromWaitingNotifySessionState) { | 432 Connect_Incomplete_Disconnect_FromWaitingNotifySessionState) { |
| 426 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 433 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 434 CreateConnection()); |
| 427 ConnectGatt(connection.get()); | 435 ConnectGatt(connection.get()); |
| 428 CharacteristicsFound(connection.get()); | 436 CharacteristicsFound(connection.get()); |
| 429 Disconnect(connection.get()); | 437 Disconnect(connection.get()); |
| 430 } | 438 } |
| 431 | 439 |
| 432 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 440 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 433 Connect_Incomplete_Disconnect_FromWaitingResponseSignalState) { | 441 Connect_Incomplete_Disconnect_FromWaitingResponseSignalState) { |
| 434 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 442 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 443 CreateConnection()); |
| 435 ConnectGatt(connection.get()); | 444 ConnectGatt(connection.get()); |
| 436 CharacteristicsFound(connection.get()); | 445 CharacteristicsFound(connection.get()); |
| 437 NotifySessionStarted(connection.get()); | 446 NotifySessionStarted(connection.get()); |
| 438 Disconnect(connection.get()); | 447 Disconnect(connection.get()); |
| 439 } | 448 } |
| 440 | 449 |
| 441 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 450 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 442 Connect_Fails_CharacteristicsNotFound) { | 451 Connect_Fails_CharacteristicsNotFound) { |
| 443 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 452 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 453 CreateConnection()); |
| 444 ConnectGatt(connection.get()); | 454 ConnectGatt(connection.get()); |
| 445 | 455 |
| 446 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); | 456 EXPECT_CALL(*from_peripheral_char_, StartNotifySession(_, _)).Times(0); |
| 447 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); | 457 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); |
| 448 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); | 458 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); |
| 449 | 459 |
| 450 characteristics_finder_error_callback_.Run( | 460 characteristics_finder_error_callback_.Run( |
| 451 {to_peripheral_char_uuid_, kToPeripheralCharID}, | 461 {to_peripheral_char_uuid_, kToPeripheralCharID}, |
| 452 {from_peripheral_char_uuid_, kFromPeripheralCharID}); | 462 {from_peripheral_char_uuid_, kFromPeripheralCharID}); |
| 453 | 463 |
| 454 EXPECT_EQ(connection->sub_status(), | 464 EXPECT_EQ(connection->sub_status(), |
| 455 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | 465 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); |
| 456 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 466 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 457 } | 467 } |
| 458 | 468 |
| 459 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 469 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 460 Connect_Fails_NotifySessionError) { | 470 Connect_Fails_NotifySessionError) { |
| 461 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 471 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 472 CreateConnection()); |
| 462 ConnectGatt(connection.get()); | 473 ConnectGatt(connection.get()); |
| 463 CharacteristicsFound(connection.get()); | 474 CharacteristicsFound(connection.get()); |
| 464 | 475 |
| 465 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 476 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| 466 .Times(0); | 477 .Times(0); |
| 467 EXPECT_FALSE(notify_session_success_callback_.is_null()); | 478 EXPECT_FALSE(notify_session_success_callback_.is_null()); |
| 468 ASSERT_FALSE(notify_session_error_callback_.is_null()); | 479 ASSERT_FALSE(notify_session_error_callback_.is_null()); |
| 469 | 480 |
| 470 notify_session_error_callback_.Run( | 481 notify_session_error_callback_.Run( |
| 471 device::BluetoothGattService::GATT_ERROR_UNKNOWN); | 482 device::BluetoothGattService::GATT_ERROR_UNKNOWN); |
| 472 | 483 |
| 473 EXPECT_EQ(connection->sub_status(), | 484 EXPECT_EQ(connection->sub_status(), |
| 474 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | 485 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); |
| 475 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 486 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 476 } | 487 } |
| 477 | 488 |
| 478 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 489 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 479 Connect_Fails_ErrorSendingInviteToConnectSignal) { | 490 Connect_Fails_ErrorSendingInviteToConnectSignal) { |
| 480 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 491 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 492 CreateConnection()); |
| 481 ConnectGatt(connection.get()); | 493 ConnectGatt(connection.get()); |
| 482 CharacteristicsFound(connection.get()); | 494 CharacteristicsFound(connection.get()); |
| 483 NotifySessionStarted(connection.get()); | 495 NotifySessionStarted(connection.get()); |
| 484 | 496 |
| 485 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the | 497 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the |
| 486 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for | 498 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for |
| 487 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use | 499 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use |
| 488 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. | 500 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. |
| 489 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); | 501 EXPECT_CALL(*connection, OnDidSendMessage(_, _)).Times(0); |
| 490 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 502 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 506 device::BluetoothGattService::GATT_ERROR_UNKNOWN); | 518 device::BluetoothGattService::GATT_ERROR_UNKNOWN); |
| 507 } | 519 } |
| 508 | 520 |
| 509 EXPECT_EQ(connection->sub_status(), | 521 EXPECT_EQ(connection->sub_status(), |
| 510 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); | 522 BluetoothLowEnergyConnection::SubStatus::DISCONNECTED); |
| 511 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); | 523 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 512 } | 524 } |
| 513 | 525 |
| 514 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 526 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 515 Receive_MessageSmallerThanCharacteristicSize) { | 527 Receive_MessageSmallerThanCharacteristicSize) { |
| 516 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 528 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 529 CreateConnection()); |
| 517 InitializeConnection(connection.get()); | 530 InitializeConnection(connection.get()); |
| 518 | 531 |
| 519 std::string received_bytes; | 532 std::string received_bytes; |
| 520 EXPECT_CALL(*connection, OnBytesReceived(_)) | 533 EXPECT_CALL(*connection, OnBytesReceived(_)) |
| 521 .WillOnce(SaveArg<0>(&received_bytes)); | 534 .WillOnce(SaveArg<0>(&received_bytes)); |
| 522 | 535 |
| 523 // Message (bytes) that is going to be received. | 536 // Message (bytes) that is going to be received. |
| 524 std::string message(100, 'A'); | 537 std::string message(100, 'A'); |
| 525 | 538 |
| 526 // Sending the |kSendSignal| + |message_size| + |message|. | 539 // Sending the |kSendSignal| + |message_size| + |message|. |
| 527 connection->GattCharacteristicValueChanged( | 540 connection->GattCharacteristicValueChanged( |
| 528 adapter_.get(), from_peripheral_char_.get(), | 541 adapter_.get(), from_peripheral_char_.get(), |
| 529 CreateFirstCharacteristicValue(message, message.size())); | 542 CreateFirstCharacteristicValue(message, message.size())); |
| 530 | 543 |
| 531 EXPECT_EQ(received_bytes, message); | 544 EXPECT_EQ(received_bytes, message); |
| 532 } | 545 } |
| 533 | 546 |
| 534 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 547 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 535 Receive_MessageLargerThanCharacteristicSize) { | 548 Receive_MessageLargerThanCharacteristicSize) { |
| 536 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 549 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 550 CreateConnection()); |
| 537 InitializeConnection(connection.get()); | 551 InitializeConnection(connection.get()); |
| 538 | 552 |
| 539 std::string received_bytes; | 553 std::string received_bytes; |
| 540 int chunk_size = 500; | 554 int chunk_size = 500; |
| 541 EXPECT_CALL(*connection, OnBytesReceived(_)) | 555 EXPECT_CALL(*connection, OnBytesReceived(_)) |
| 542 .WillOnce(SaveArg<0>(&received_bytes)); | 556 .WillOnce(SaveArg<0>(&received_bytes)); |
| 543 | 557 |
| 544 // Message (bytes) that is going to be received. | 558 // Message (bytes) that is going to be received. |
| 545 int message_size = 600; | 559 int message_size = 600; |
| 546 std::string message(message_size, 'A'); | 560 std::string message(message_size, 'A'); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 560 value.insert(value.end(), message.begin() + first_write_payload_size, | 574 value.insert(value.end(), message.begin() + first_write_payload_size, |
| 561 message.end()); | 575 message.end()); |
| 562 connection->GattCharacteristicValueChanged( | 576 connection->GattCharacteristicValueChanged( |
| 563 adapter_.get(), from_peripheral_char_.get(), value); | 577 adapter_.get(), from_peripheral_char_.get(), value); |
| 564 | 578 |
| 565 EXPECT_EQ(received_bytes, message); | 579 EXPECT_EQ(received_bytes, message); |
| 566 } | 580 } |
| 567 | 581 |
| 568 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 582 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 569 SendMessage_SmallerThanCharacteristicSize) { | 583 SendMessage_SmallerThanCharacteristicSize) { |
| 570 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 584 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 585 CreateConnection()); |
| 571 InitializeConnection(connection.get()); | 586 InitializeConnection(connection.get()); |
| 572 | 587 |
| 573 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | 588 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
| 574 // called. | 589 // called. |
| 575 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 590 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| 576 .WillOnce( | 591 .WillOnce( |
| 577 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 592 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
| 578 SaveArg<1>(&write_remote_characteristic_success_callback_), | 593 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 579 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 594 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 580 | 595 |
| 581 // Message (bytes) that is going to be sent. | 596 // Message (bytes) that is going to be sent. |
| 582 int message_size = 100; | 597 int message_size = 100; |
| 583 std::string message(message_size, 'A'); | 598 std::string message(message_size, 'A'); |
| 584 message[0] = 'B'; | 599 message[0] = 'B'; |
| 585 connection->SendMessage(make_scoped_ptr(new FakeWireMessage(message))); | 600 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message))); |
| 586 | 601 |
| 587 // Expecting that |kSendSignal| + |message_size| + |message| was written. | 602 // Expecting that |kSendSignal| + |message_size| + |message| was written. |
| 588 EXPECT_EQ(last_value_written_on_to_peripheral_char_, | 603 EXPECT_EQ(last_value_written_on_to_peripheral_char_, |
| 589 CreateFirstCharacteristicValue(message, message.size())); | 604 CreateFirstCharacteristicValue(message, message.size())); |
| 590 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | 605 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); |
| 591 | 606 |
| 592 RunWriteCharacteristicSuccessCallback(); | 607 RunWriteCharacteristicSuccessCallback(); |
| 593 } | 608 } |
| 594 | 609 |
| 595 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 610 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 596 SendMessage_LagerThanCharacteristicSize) { | 611 SendMessage_LagerThanCharacteristicSize) { |
| 597 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 612 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 613 CreateConnection()); |
| 598 InitializeConnection(connection.get()); | 614 InitializeConnection(connection.get()); |
| 599 | 615 |
| 600 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is | 616 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
| 601 // called. | 617 // called. |
| 602 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) | 618 EXPECT_CALL(*to_peripheral_char_, WriteRemoteCharacteristic(_, _, _)) |
| 603 .WillOnce( | 619 .WillOnce( |
| 604 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), | 620 DoAll(SaveArg<0>(&last_value_written_on_to_peripheral_char_), |
| 605 SaveArg<1>(&write_remote_characteristic_success_callback_), | 621 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 606 SaveArg<2>(&write_remote_characteristic_error_callback_))); | 622 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 607 | 623 |
| 608 // Message (bytes) that is going to be sent. | 624 // Message (bytes) that is going to be sent. |
| 609 int message_size = 600; | 625 int message_size = 600; |
| 610 std::string message(message_size, 'A'); | 626 std::string message(message_size, 'A'); |
| 611 message[0] = 'B'; | 627 message[0] = 'B'; |
| 612 connection->SendMessage(make_scoped_ptr(new FakeWireMessage(message))); | 628 connection->SendMessage(base::WrapUnique(new FakeWireMessage(message))); |
| 613 | 629 |
| 614 // Expecting that |kSendSignal| + |message_size| was written in the first 8 | 630 // Expecting that |kSendSignal| + |message_size| was written in the first 8 |
| 615 // bytes. | 631 // bytes. |
| 616 std::vector<uint8_t> prefix( | 632 std::vector<uint8_t> prefix( |
| 617 last_value_written_on_to_peripheral_char_.begin(), | 633 last_value_written_on_to_peripheral_char_.begin(), |
| 618 last_value_written_on_to_peripheral_char_.begin() + 8); | 634 last_value_written_on_to_peripheral_char_.begin() + 8); |
| 619 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); | 635 EXPECT_EQ(prefix, CreateSendSignalWithSize(message_size)); |
| 620 std::vector<uint8_t> bytes_received( | 636 std::vector<uint8_t> bytes_received( |
| 621 last_value_written_on_to_peripheral_char_.begin() + 8, | 637 last_value_written_on_to_peripheral_char_.begin() + 8, |
| 622 last_value_written_on_to_peripheral_char_.end()); | 638 last_value_written_on_to_peripheral_char_.end()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 638 std::vector<uint8_t> expected_value(message.begin(), message.end()); | 654 std::vector<uint8_t> expected_value(message.begin(), message.end()); |
| 639 EXPECT_EQ(expected_value.size(), bytes_received.size()); | 655 EXPECT_EQ(expected_value.size(), bytes_received.size()); |
| 640 EXPECT_EQ(expected_value, bytes_received); | 656 EXPECT_EQ(expected_value, bytes_received); |
| 641 | 657 |
| 642 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); | 658 EXPECT_CALL(*connection, OnDidSendMessage(_, _)); |
| 643 RunWriteCharacteristicSuccessCallback(); | 659 RunWriteCharacteristicSuccessCallback(); |
| 644 } | 660 } |
| 645 | 661 |
| 646 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, | 662 TEST_F(ProximityAuthBluetoothLowEnergyConnectionTest, |
| 647 Connect_AfterADelayWhenThrottled) { | 663 Connect_AfterADelayWhenThrottled) { |
| 648 scoped_ptr<MockBluetoothLowEnergyConnection> connection(CreateConnection()); | 664 std::unique_ptr<MockBluetoothLowEnergyConnection> connection( |
| 665 CreateConnection()); |
| 649 | 666 |
| 650 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) | 667 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) |
| 651 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); | 668 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); |
| 652 EXPECT_CALL(*device_, CreateGattConnection(_, _)) | 669 EXPECT_CALL(*device_, CreateGattConnection(_, _)) |
| 653 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), | 670 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), |
| 654 SaveArg<1>(&create_gatt_connection_error_callback_))); | 671 SaveArg<1>(&create_gatt_connection_error_callback_))); |
| 655 | 672 |
| 656 // No GATT connection should be created before the delay. | 673 // No GATT connection should be created before the delay. |
| 657 connection->Connect(); | 674 connection->Connect(); |
| 658 EXPECT_EQ(connection->sub_status(), | 675 EXPECT_EQ(connection->sub_status(), |
| 659 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); | 676 BluetoothLowEnergyConnection::SubStatus::WAITING_GATT_CONNECTION); |
| 660 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); | 677 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 661 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); | 678 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); |
| 662 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); | 679 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); |
| 663 | 680 |
| 664 // A GATT connection should be created after the delay. | 681 // A GATT connection should be created after the delay. |
| 665 task_runner_->RunUntilIdle(); | 682 task_runner_->RunUntilIdle(); |
| 666 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); | 683 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); |
| 667 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); | 684 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); |
| 668 | 685 |
| 669 // Preparing |connection| to run |create_gatt_connection_success_callback_|. | 686 // Preparing |connection| to run |create_gatt_connection_success_callback_|. |
| 670 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) | 687 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) |
| 671 .WillOnce(DoAll( | 688 .WillOnce(DoAll( |
| 672 SaveArg<0>(&characteristics_finder_success_callback_), | 689 SaveArg<0>(&characteristics_finder_success_callback_), |
| 673 SaveArg<1>(&characteristics_finder_error_callback_), | 690 SaveArg<1>(&characteristics_finder_error_callback_), |
| 674 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); | 691 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); |
| 675 | 692 |
| 676 create_gatt_connection_success_callback_.Run( | 693 create_gatt_connection_success_callback_.Run( |
| 677 make_scoped_ptr(new NiceMock<device::MockBluetoothGattConnection>( | 694 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( |
| 678 adapter_, kTestRemoteDeviceBluetoothAddress))); | 695 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 679 | 696 |
| 680 CharacteristicsFound(connection.get()); | 697 CharacteristicsFound(connection.get()); |
| 681 NotifySessionStarted(connection.get()); | 698 NotifySessionStarted(connection.get()); |
| 682 ResponseSignalReceived(connection.get()); | 699 ResponseSignalReceived(connection.get()); |
| 683 } | 700 } |
| 684 | 701 |
| 685 } // namespace proximity_auth | 702 } // namespace proximity_auth |
| OLD | NEW |