OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_weave_client_connec
tion.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "base/test/test_simple_task_runner.h" |
| 17 #include "components/proximity_auth/bluetooth_throttler.h" |
| 18 #include "components/proximity_auth/connection_finder.h" |
| 19 #include "components/proximity_auth/connection_observer.h" |
| 20 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 21 #include "components/proximity_auth/remote_device.h" |
| 22 #include "components/proximity_auth/wire_message.h" |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 24 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 25 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 26 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| 27 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" |
| 28 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 29 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 |
| 33 using testing::_; |
| 34 using testing::AtLeast; |
| 35 using testing::NiceMock; |
| 36 using testing::Return; |
| 37 using testing::StrictMock; |
| 38 using testing::SaveArg; |
| 39 |
| 40 namespace proximity_auth { |
| 41 namespace { |
| 42 |
| 43 class MockBluetoothThrottler : public BluetoothThrottler { |
| 44 public: |
| 45 MockBluetoothThrottler() {} |
| 46 ~MockBluetoothThrottler() override {} |
| 47 |
| 48 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); |
| 49 MOCK_METHOD1(OnConnection, void(Connection* connection)); |
| 50 |
| 51 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); |
| 53 }; |
| 54 |
| 55 class MockBluetoothLowEnergyCharacteristicsFinder |
| 56 : public BluetoothLowEnergyCharacteristicsFinder { |
| 57 public: |
| 58 MockBluetoothLowEnergyCharacteristicsFinder() {} |
| 59 ~MockBluetoothLowEnergyCharacteristicsFinder() override {} |
| 60 |
| 61 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyCharacteristicsFinder); |
| 63 }; |
| 64 |
| 65 class MockConnectionObserver : public ConnectionObserver { |
| 66 public: |
| 67 MockConnectionObserver() : num_send_completed_(0) {} |
| 68 |
| 69 void OnConnectionStatusChanged(Connection* connection, |
| 70 Connection::Status old_status, |
| 71 Connection::Status new_status) override {} |
| 72 |
| 73 void OnMessageReceived(const Connection& connection, |
| 74 const WireMessage& message) override {} |
| 75 |
| 76 void OnSendCompleted(const Connection& conenction, |
| 77 const WireMessage& message, |
| 78 bool success) override { |
| 79 last_deserialized_message_ = message.payload(); |
| 80 last_send_success_ = success; |
| 81 num_send_completed_++; |
| 82 } |
| 83 |
| 84 std::string GetLastDeserializedMessage() { |
| 85 return last_deserialized_message_; |
| 86 } |
| 87 |
| 88 bool GetLastSendSuccess() { return last_send_success_; } |
| 89 |
| 90 int GetNumSendCompleted() { return num_send_completed_; } |
| 91 |
| 92 private: |
| 93 std::string last_deserialized_message_; |
| 94 bool last_send_success_; |
| 95 int num_send_completed_; |
| 96 }; |
| 97 |
| 98 } // namespace |
| 99 |
| 100 namespace weave { |
| 101 namespace { |
| 102 |
| 103 typedef BluetoothLowEnergyWeaveClientConnection::SubStatus SubStatus; |
| 104 typedef BluetoothLowEnergyWeavePacketReceiver::State ReceiverState; |
| 105 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverError ReceiverError; |
| 106 typedef BluetoothLowEnergyWeavePacketReceiver::ReceiverType ReceiverType; |
| 107 |
| 108 const char kServiceUUID[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF"; |
| 109 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; |
| 110 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; |
| 111 |
| 112 const char kServiceID[] = "service id"; |
| 113 const char kTXCharacteristicID[] = "TX characteristic id"; |
| 114 const char kRXCharacteristicID[] = "RX characteristic id"; |
| 115 |
| 116 const device::BluetoothRemoteGattCharacteristic::Properties |
| 117 kCharacteristicProperties = |
| 118 device::BluetoothRemoteGattCharacteristic::PROPERTY_BROADCAST | |
| 119 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ | |
| 120 device::BluetoothRemoteGattCharacteristic:: |
| 121 PROPERTY_WRITE_WITHOUT_RESPONSE | |
| 122 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE; |
| 123 |
| 124 const int kMaxNumberOfTries = 3; |
| 125 const uint16_t kLargeMaxPacketSize = 30; |
| 126 |
| 127 const uint8_t kDataHeader = 0; |
| 128 const uint8_t kConnectionRequestHeader = 1; |
| 129 const uint8_t kSmallConnectionResponseHeader = 2; |
| 130 const uint8_t kLargeConnectionResponseHeader = 3; |
| 131 const uint8_t kConnectionCloseHeader = 4; |
| 132 const uint8_t kErroneousHeader = 5; |
| 133 |
| 134 const std::string kSmallMessage = "bb"; |
| 135 const std::string kLargeMessage = "aaabbb"; |
| 136 const std::string kLargeMessage0 = "aaa"; |
| 137 const std::string kLargeMessage1 = "bbb"; |
| 138 |
| 139 const Packet kConnectionRequest{kConnectionRequestHeader}; |
| 140 const Packet kSmallConnectionResponse{kSmallConnectionResponseHeader}; |
| 141 const Packet kLargeConnectionResponse{kLargeConnectionResponseHeader}; |
| 142 const Packet kConnectionCloseSuccess{kConnectionCloseHeader, |
| 143 ReasonForClose::CLOSE_WITHOUT_ERROR}; |
| 144 const Packet kConnectionCloseUnknownError{kConnectionCloseHeader, |
| 145 ReasonForClose::UNKNOWN_ERROR}; |
| 146 const Packet kConnectionCloseApplicationError{ |
| 147 kConnectionCloseHeader, ReasonForClose::APPLICATION_ERROR}; |
| 148 |
| 149 const Packet kSmallPackets0 = Packet{kDataHeader, 'b', 'b'}; |
| 150 const Packet kLargePackets0 = Packet{kDataHeader, 'a', 'a', 'a'}; |
| 151 const Packet kLargePackets1 = Packet{kDataHeader, 'b', 'b', 'b'}; |
| 152 const Packet kErroneousPacket = Packet{kErroneousHeader}; |
| 153 |
| 154 const std::vector<Packet> kSmallPackets{kSmallPackets0}; |
| 155 const std::vector<Packet> kLargePackets{kLargePackets0, kLargePackets1}; |
| 156 |
| 157 class MockBluetoothLowEnergyWeavePacketGenerator |
| 158 : public BluetoothLowEnergyWeavePacketGenerator { |
| 159 public: |
| 160 MockBluetoothLowEnergyWeavePacketGenerator() |
| 161 : max_packet_size_(kDefaultMaxPacketSize) {} |
| 162 |
| 163 Packet CreateConnectionRequest() override { return kConnectionRequest; } |
| 164 |
| 165 Packet CreateConnectionResponse() override { |
| 166 NOTIMPLEMENTED(); |
| 167 return Packet(); |
| 168 } |
| 169 |
| 170 Packet CreateConnectionClose(ReasonForClose reason_for_close) override { |
| 171 return Packet{kConnectionCloseHeader, |
| 172 static_cast<uint8_t>(reason_for_close)}; |
| 173 } |
| 174 |
| 175 void SetMaxPacketSize(uint16_t size) override { max_packet_size_ = size; } |
| 176 |
| 177 std::vector<Packet> EncodeDataMessage(std::string message) override { |
| 178 if (message == kSmallMessage && max_packet_size_ == kDefaultMaxPacketSize) { |
| 179 return kSmallPackets; |
| 180 } else if (message == kLargeMessage && |
| 181 max_packet_size_ == kLargeMaxPacketSize) { |
| 182 return kLargePackets; |
| 183 } else { |
| 184 NOTREACHED(); |
| 185 return std::vector<Packet>(); |
| 186 } |
| 187 } |
| 188 |
| 189 uint16_t GetMaxPacketSize() { return max_packet_size_; } |
| 190 |
| 191 private: |
| 192 uint16_t max_packet_size_; |
| 193 }; |
| 194 |
| 195 class MockBluetoothLowEnergyWeavePacketReceiver |
| 196 : public BluetoothLowEnergyWeavePacketReceiver { |
| 197 public: |
| 198 MockBluetoothLowEnergyWeavePacketReceiver() |
| 199 : BluetoothLowEnergyWeavePacketReceiver(ReceiverType::CLIENT), |
| 200 state_(State::CONNECTING), |
| 201 max_packet_size_(kDefaultMaxPacketSize), |
| 202 reason_for_close_(ReasonForClose::CLOSE_WITHOUT_ERROR), |
| 203 reason_to_close_(ReasonForClose::CLOSE_WITHOUT_ERROR) {} |
| 204 |
| 205 ReceiverState GetState() override { return state_; } |
| 206 |
| 207 uint16_t GetMaxPacketSize() override { return max_packet_size_; } |
| 208 |
| 209 ReasonForClose GetReasonForClose() override { return reason_for_close_; } |
| 210 |
| 211 ReasonForClose GetReasonToClose() override { return reason_to_close_; } |
| 212 |
| 213 std::string GetDataMessage() override { |
| 214 if (max_packet_size_ == kDefaultMaxPacketSize) { |
| 215 return kSmallMessage; |
| 216 } else { |
| 217 return kLargeMessage; |
| 218 } |
| 219 } |
| 220 |
| 221 ReceiverError GetReceiverError() override { |
| 222 return ReceiverError::NO_ERROR_DETECTED; |
| 223 } |
| 224 |
| 225 ReceiverState ReceivePacket(const Packet& packet) override { |
| 226 switch (packet[0]) { |
| 227 case kSmallConnectionResponseHeader: |
| 228 max_packet_size_ = kDefaultMaxPacketSize; |
| 229 state_ = ReceiverState::WAITING; |
| 230 break; |
| 231 case kLargeConnectionResponseHeader: |
| 232 max_packet_size_ = kLargeMaxPacketSize; |
| 233 state_ = ReceiverState::WAITING; |
| 234 break; |
| 235 case kConnectionCloseHeader: |
| 236 state_ = ReceiverState::CONNECTION_CLOSED; |
| 237 reason_for_close_ = static_cast<ReasonForClose>(packet[1]); |
| 238 break; |
| 239 case kDataHeader: |
| 240 if (packet == kSmallPackets0 || packet == kLargePackets1) { |
| 241 state_ = ReceiverState::DATA_READY; |
| 242 } else { |
| 243 state_ = ReceiverState::RECEIVING_DATA; |
| 244 } |
| 245 break; |
| 246 default: |
| 247 reason_to_close_ = ReasonForClose::APPLICATION_ERROR; |
| 248 state_ = ReceiverState::ERROR_DETECTED; |
| 249 } |
| 250 return state_; |
| 251 } |
| 252 |
| 253 private: |
| 254 ReceiverState state_; |
| 255 uint16_t max_packet_size_; |
| 256 ReasonForClose reason_for_close_; |
| 257 ReasonForClose reason_to_close_; |
| 258 }; |
| 259 |
| 260 class MockBluetoothLowEnergyWeavePacketGeneratorFactory |
| 261 : public BluetoothLowEnergyWeavePacketGenerator::Factory { |
| 262 public: |
| 263 // most_recent_instance_ will be obsolete after the connection class |
| 264 // destructs. Do not use if that's the case. |
| 265 MockBluetoothLowEnergyWeavePacketGenerator* GetMostRecentInstance() { |
| 266 return most_recent_instance_; |
| 267 } |
| 268 |
| 269 private: |
| 270 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> BuildInstance() |
| 271 override { |
| 272 most_recent_instance_ = new MockBluetoothLowEnergyWeavePacketGenerator(); |
| 273 return std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator>( |
| 274 most_recent_instance_); |
| 275 } |
| 276 |
| 277 MockBluetoothLowEnergyWeavePacketGenerator* most_recent_instance_; |
| 278 }; |
| 279 |
| 280 class MockBluetoothLowEnergyWeavePacketReceiverFactory |
| 281 : public BluetoothLowEnergyWeavePacketReceiver::Factory { |
| 282 public: |
| 283 // most_recent_instance_ will be obsolete after the connection class |
| 284 // destructs. Do not use if that's the case. |
| 285 MockBluetoothLowEnergyWeavePacketReceiver* GetMostRecentInstance() { |
| 286 return most_recent_instance_; |
| 287 } |
| 288 |
| 289 private: |
| 290 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> BuildInstance( |
| 291 ReceiverType receiver_type) override { |
| 292 most_recent_instance_ = new MockBluetoothLowEnergyWeavePacketReceiver(); |
| 293 return std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver>( |
| 294 most_recent_instance_); |
| 295 } |
| 296 |
| 297 MockBluetoothLowEnergyWeavePacketReceiver* most_recent_instance_; |
| 298 }; |
| 299 |
| 300 class TestBluetoothLowEnergyWeaveClientConnection |
| 301 : public BluetoothLowEnergyWeaveClientConnection { |
| 302 public: |
| 303 TestBluetoothLowEnergyWeaveClientConnection( |
| 304 const RemoteDevice& remote_device, |
| 305 scoped_refptr<device::BluetoothAdapter> adapter, |
| 306 const device::BluetoothUUID remote_service_uuid, |
| 307 BluetoothThrottler* bluetooth_throttler, |
| 308 int max_number_of_write_attempts) |
| 309 : BluetoothLowEnergyWeaveClientConnection(remote_device, |
| 310 adapter, |
| 311 remote_service_uuid, |
| 312 bluetooth_throttler, |
| 313 max_number_of_write_attempts) {} |
| 314 |
| 315 ~TestBluetoothLowEnergyWeaveClientConnection() override {} |
| 316 |
| 317 MOCK_METHOD2( |
| 318 CreateCharacteristicsFinder, |
| 319 BluetoothLowEnergyCharacteristicsFinder*( |
| 320 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
| 321 success, |
| 322 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& error)); |
| 323 |
| 324 MOCK_METHOD1(OnBytesReceived, void(const std::string& bytes)); |
| 325 |
| 326 // Exposing inherited protected methods for testing. |
| 327 using BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged; |
| 328 using BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting; |
| 329 using BluetoothLowEnergyWeaveClientConnection::DestroyConnection; |
| 330 |
| 331 // Exposing inherited protected fields for testing. |
| 332 using BluetoothLowEnergyWeaveClientConnection::status; |
| 333 using BluetoothLowEnergyWeaveClientConnection::sub_status; |
| 334 |
| 335 private: |
| 336 DISALLOW_COPY_AND_ASSIGN(TestBluetoothLowEnergyWeaveClientConnection); |
| 337 }; |
| 338 |
| 339 } // namespace |
| 340 |
| 341 class ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest |
| 342 : public testing::Test { |
| 343 public: |
| 344 ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest() |
| 345 : adapter_(new NiceMock<device::MockBluetoothAdapter>), |
| 346 remote_device_(CreateLERemoteDeviceForTest()), |
| 347 service_uuid_(device::BluetoothUUID(kServiceUUID)), |
| 348 tx_characteristic_uuid_(device::BluetoothUUID(kTXCharacteristicUUID)), |
| 349 rx_characteristic_uuid_(device::BluetoothUUID(kRXCharacteristicUUID)), |
| 350 notify_session_alias_(NULL), |
| 351 bluetooth_throttler_(new NiceMock<MockBluetoothThrottler>), |
| 352 task_runner_(new base::TestSimpleTaskRunner), |
| 353 last_completed_wire_message_(""), |
| 354 generator_factory_( |
| 355 new MockBluetoothLowEnergyWeavePacketGeneratorFactory()), |
| 356 receiver_factory_( |
| 357 new MockBluetoothLowEnergyWeavePacketReceiverFactory()) { |
| 358 BluetoothLowEnergyWeavePacketGenerator::Factory::SetInstanceForTesting( |
| 359 generator_factory_); |
| 360 BluetoothLowEnergyWeavePacketReceiver::Factory::SetInstanceForTesting( |
| 361 receiver_factory_); |
| 362 } |
| 363 |
| 364 void SetUp() override { |
| 365 device_ = base::WrapUnique(new NiceMock<device::MockBluetoothDevice>( |
| 366 adapter_.get(), 0, kTestRemoteDeviceName, |
| 367 kTestRemoteDeviceBluetoothAddress, false, false)); |
| 368 |
| 369 service_ = base::WrapUnique(new NiceMock<device::MockBluetoothGattService>( |
| 370 device_.get(), kServiceID, service_uuid_, true, false)); |
| 371 tx_characteristic_ = |
| 372 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( |
| 373 service_.get(), kTXCharacteristicID, tx_characteristic_uuid_, false, |
| 374 kCharacteristicProperties, |
| 375 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); |
| 376 |
| 377 rx_characteristic_ = |
| 378 base::WrapUnique(new NiceMock<device::MockBluetoothGattCharacteristic>( |
| 379 service_.get(), kRXCharacteristicID, rx_characteristic_uuid_, false, |
| 380 kCharacteristicProperties, |
| 381 device::BluetoothRemoteGattCharacteristic::PERMISSION_NONE)); |
| 382 |
| 383 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); |
| 384 |
| 385 std::vector<const device::BluetoothDevice*> devices; |
| 386 devices.push_back(device_.get()); |
| 387 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); |
| 388 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) |
| 389 .WillByDefault(Return(device_.get())); |
| 390 ON_CALL(*device_, GetGattService(kServiceID)) |
| 391 .WillByDefault(Return(service_.get())); |
| 392 ON_CALL(*service_, GetCharacteristic(kRXCharacteristicID)) |
| 393 .WillByDefault(Return(rx_characteristic_.get())); |
| 394 ON_CALL(*service_, GetCharacteristic(kTXCharacteristicID)) |
| 395 .WillByDefault(Return(tx_characteristic_.get())); |
| 396 } |
| 397 |
| 398 // Creates a BluetoothLowEnergyWeaveClientConnection and verifies it's in |
| 399 // DISCONNECTED state. |
| 400 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> |
| 401 CreateConnection() { |
| 402 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 403 EXPECT_CALL(*adapter_, RemoveObserver(_)); |
| 404 |
| 405 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 406 new TestBluetoothLowEnergyWeaveClientConnection( |
| 407 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), |
| 408 kMaxNumberOfTries)); |
| 409 |
| 410 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 411 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 412 |
| 413 // Add the mock observer to observe on OnDidMessageSend. |
| 414 connection->AddObserver(&connection_observer_); |
| 415 |
| 416 connection->SetTaskRunnerForTesting(task_runner_); |
| 417 |
| 418 return connection; |
| 419 } |
| 420 |
| 421 // Transitions |connection| from DISCONNECTED to WAITING_CHARACTERISTICS |
| 422 // state, without an existing GATT connection. |
| 423 void ConnectGatt(TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 424 // Preparing |connection| for a CreateGattConnection call. |
| 425 EXPECT_CALL(*device_, CreateGattConnection(_, _)) |
| 426 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), |
| 427 SaveArg<1>(&create_gatt_connection_error_callback_))); |
| 428 |
| 429 // No throttling by default |
| 430 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) |
| 431 .WillOnce(Return(base::TimeDelta())); |
| 432 |
| 433 connection->Connect(); |
| 434 |
| 435 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); |
| 436 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 437 |
| 438 // Preparing |connection| to run |create_gatt_connection_success_callback_|. |
| 439 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); |
| 440 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); |
| 441 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) |
| 442 .WillOnce(DoAll( |
| 443 SaveArg<0>(&characteristics_finder_success_callback_), |
| 444 SaveArg<1>(&characteristics_finder_error_callback_), |
| 445 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); |
| 446 |
| 447 create_gatt_connection_success_callback_.Run( |
| 448 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( |
| 449 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 450 |
| 451 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CHARACTERISTICS); |
| 452 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 453 } |
| 454 |
| 455 // Transitions |connection| from WAITING_CHARACTERISTICS to |
| 456 // WAITING_NOTIFY_SESSION state. |
| 457 void CharacteristicsFound( |
| 458 TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 459 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)) |
| 460 .WillOnce(DoAll(SaveArg<0>(¬ify_session_success_callback_), |
| 461 SaveArg<1>(¬ify_session_error_callback_))); |
| 462 EXPECT_FALSE(characteristics_finder_error_callback_.is_null()); |
| 463 ASSERT_FALSE(characteristics_finder_success_callback_.is_null()); |
| 464 |
| 465 characteristics_finder_success_callback_.Run( |
| 466 {service_uuid_, kServiceID}, |
| 467 {tx_characteristic_uuid_, kTXCharacteristicID}, |
| 468 {rx_characteristic_uuid_, kRXCharacteristicID}); |
| 469 |
| 470 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_NOTIFY_SESSION); |
| 471 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 472 } |
| 473 |
| 474 // Transitions |connection| from WAITING_NOTIFY_SESSION to |
| 475 // WAITING_CONNECTION_RESPONSE state. |
| 476 void NotifySessionStarted( |
| 477 TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 478 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 479 .WillOnce( |
| 480 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 481 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 482 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 483 EXPECT_FALSE(notify_session_error_callback_.is_null()); |
| 484 ASSERT_FALSE(notify_session_success_callback_.is_null()); |
| 485 |
| 486 // Store an alias for the notify session passed |connection|. |
| 487 std::unique_ptr<device::MockBluetoothGattNotifySession> notify_session( |
| 488 new NiceMock<device::MockBluetoothGattNotifySession>( |
| 489 kTXCharacteristicID)); |
| 490 notify_session_alias_ = notify_session.get(); |
| 491 |
| 492 notify_session_success_callback_.Run(std::move(notify_session)); |
| 493 task_runner_->RunUntilIdle(); |
| 494 |
| 495 // Written value contains only the mock Connection Request. |
| 496 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); |
| 497 |
| 498 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_CONNECTION_RESPONSE); |
| 499 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 500 } |
| 501 |
| 502 // Transitions |connection| from WAITING_CONNECTION_RESPONSE to CONNECTED. |
| 503 void ConnectionResponseReceived( |
| 504 TestBluetoothLowEnergyWeaveClientConnection* connection, |
| 505 uint16_t selected_packet_size) { |
| 506 // Written value contains only the mock Connection Request. |
| 507 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); |
| 508 |
| 509 // OnDidSendMessage is not called. |
| 510 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); |
| 511 |
| 512 RunWriteCharacteristicSuccessCallback(); |
| 513 |
| 514 // Received Connection Response. |
| 515 if (selected_packet_size == kDefaultMaxPacketSize) { |
| 516 connection->GattCharacteristicValueChanged( |
| 517 adapter_.get(), rx_characteristic_.get(), kSmallConnectionResponse); |
| 518 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetMaxPacketSize(), |
| 519 kDefaultMaxPacketSize); |
| 520 EXPECT_EQ(generator_factory_->GetMostRecentInstance()->GetMaxPacketSize(), |
| 521 kDefaultMaxPacketSize); |
| 522 } else if (selected_packet_size == kLargeMaxPacketSize) { |
| 523 connection->GattCharacteristicValueChanged( |
| 524 adapter_.get(), rx_characteristic_.get(), kLargeConnectionResponse); |
| 525 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetMaxPacketSize(), |
| 526 kLargeMaxPacketSize); |
| 527 EXPECT_EQ(generator_factory_->GetMostRecentInstance()->GetMaxPacketSize(), |
| 528 kLargeMaxPacketSize); |
| 529 } else { |
| 530 NOTREACHED(); |
| 531 } |
| 532 |
| 533 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); |
| 534 EXPECT_EQ(connection->status(), Connection::CONNECTED); |
| 535 } |
| 536 |
| 537 // Transitions |connection| to a DISCONNECTED state regardless of its initial |
| 538 // state. |
| 539 void Disconnect(TestBluetoothLowEnergyWeaveClientConnection* connection) { |
| 540 // A notify session was previously set. |
| 541 if (notify_session_alias_) |
| 542 EXPECT_CALL(*notify_session_alias_, Stop(_)); |
| 543 |
| 544 if (connection->sub_status() == SubStatus::CONNECTED) { |
| 545 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 546 .WillOnce( |
| 547 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 548 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 549 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 550 } |
| 551 |
| 552 connection->Disconnect(); |
| 553 |
| 554 if (connection->sub_status() == SubStatus::CONNECTED) { |
| 555 connection->DestroyConnection(); |
| 556 EXPECT_EQ(last_value_written_on_tx_characteristic_, |
| 557 kConnectionCloseSuccess); |
| 558 RunWriteCharacteristicSuccessCallback(); |
| 559 } |
| 560 |
| 561 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 562 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 563 } |
| 564 |
| 565 void InitializeConnection( |
| 566 TestBluetoothLowEnergyWeaveClientConnection* connection, |
| 567 uint32_t selected_packet_size) { |
| 568 ConnectGatt(connection); |
| 569 CharacteristicsFound(connection); |
| 570 NotifySessionStarted(connection); |
| 571 ConnectionResponseReceived(connection, selected_packet_size); |
| 572 } |
| 573 |
| 574 void RunWriteCharacteristicSuccessCallback() { |
| 575 EXPECT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 576 ASSERT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 577 write_remote_characteristic_success_callback_.Run(); |
| 578 } |
| 579 |
| 580 protected: |
| 581 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 582 RemoteDevice remote_device_; |
| 583 device::BluetoothUUID service_uuid_; |
| 584 device::BluetoothUUID tx_characteristic_uuid_; |
| 585 device::BluetoothUUID rx_characteristic_uuid_; |
| 586 std::unique_ptr<device::MockBluetoothDevice> device_; |
| 587 std::unique_ptr<device::MockBluetoothGattService> service_; |
| 588 std::unique_ptr<device::MockBluetoothGattCharacteristic> tx_characteristic_; |
| 589 std::unique_ptr<device::MockBluetoothGattCharacteristic> rx_characteristic_; |
| 590 std::vector<uint8_t> last_value_written_on_tx_characteristic_; |
| 591 device::MockBluetoothGattNotifySession* notify_session_alias_; |
| 592 std::unique_ptr<MockBluetoothThrottler> bluetooth_throttler_; |
| 593 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 594 base::MessageLoop message_loop_; |
| 595 WireMessage last_completed_wire_message_; |
| 596 bool last_wire_message_success_; |
| 597 std::shared_ptr<MockBluetoothLowEnergyWeavePacketGeneratorFactory> |
| 598 generator_factory_; |
| 599 std::shared_ptr<MockBluetoothLowEnergyWeavePacketReceiverFactory> |
| 600 receiver_factory_; |
| 601 MockConnectionObserver connection_observer_; |
| 602 |
| 603 // Callbacks |
| 604 device::BluetoothDevice::GattConnectionCallback |
| 605 create_gatt_connection_success_callback_; |
| 606 device::BluetoothDevice::ConnectErrorCallback |
| 607 create_gatt_connection_error_callback_; |
| 608 |
| 609 BluetoothLowEnergyCharacteristicsFinder::SuccessCallback |
| 610 characteristics_finder_success_callback_; |
| 611 BluetoothLowEnergyCharacteristicsFinder::ErrorCallback |
| 612 characteristics_finder_error_callback_; |
| 613 |
| 614 device::BluetoothRemoteGattCharacteristic::NotifySessionCallback |
| 615 notify_session_success_callback_; |
| 616 device::BluetoothRemoteGattCharacteristic::ErrorCallback |
| 617 notify_session_error_callback_; |
| 618 |
| 619 base::Closure write_remote_characteristic_success_callback_; |
| 620 device::BluetoothRemoteGattCharacteristic::ErrorCallback |
| 621 write_remote_characteristic_error_callback_; |
| 622 }; |
| 623 |
| 624 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 625 CreateAndDestroyWithoutConnectCallDoesntCrash) { |
| 626 BluetoothLowEnergyWeaveClientConnection connection( |
| 627 remote_device_, adapter_, service_uuid_, bluetooth_throttler_.get(), |
| 628 kMaxNumberOfTries); |
| 629 } |
| 630 |
| 631 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 632 DisconnectWithoutConnectDoesntCrash) { |
| 633 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 634 CreateConnection()); |
| 635 Disconnect(connection.get()); |
| 636 } |
| 637 |
| 638 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 639 ConnectSuccess) { |
| 640 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 641 CreateConnection()); |
| 642 ConnectGatt(connection.get()); |
| 643 CharacteristicsFound(connection.get()); |
| 644 NotifySessionStarted(connection.get()); |
| 645 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); |
| 646 } |
| 647 |
| 648 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 649 ConnectSuccessDisconnect) { |
| 650 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 651 CreateConnection()); |
| 652 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 653 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); |
| 654 Disconnect(connection.get()); |
| 655 } |
| 656 |
| 657 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 658 ConnectIncompleteDisconnectFromWaitingCharacteristicsState) { |
| 659 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 660 CreateConnection()); |
| 661 ConnectGatt(connection.get()); |
| 662 Disconnect(connection.get()); |
| 663 } |
| 664 |
| 665 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 666 ConnectIncompleteDisconnectFromWaitingNotifySessionState) { |
| 667 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 668 CreateConnection()); |
| 669 ConnectGatt(connection.get()); |
| 670 CharacteristicsFound(connection.get()); |
| 671 Disconnect(connection.get()); |
| 672 } |
| 673 |
| 674 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 675 ConnectIncompleteDisconnectFromWaitingConnectionResponseState) { |
| 676 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 677 CreateConnection()); |
| 678 ConnectGatt(connection.get()); |
| 679 CharacteristicsFound(connection.get()); |
| 680 NotifySessionStarted(connection.get()); |
| 681 Disconnect(connection.get()); |
| 682 } |
| 683 |
| 684 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 685 ConnectFailsCharacteristicsNotFound) { |
| 686 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 687 CreateConnection()); |
| 688 ConnectGatt(connection.get()); |
| 689 |
| 690 EXPECT_CALL(*rx_characteristic_, StartNotifySession(_, _)).Times(0); |
| 691 EXPECT_FALSE(characteristics_finder_success_callback_.is_null()); |
| 692 ASSERT_FALSE(characteristics_finder_error_callback_.is_null()); |
| 693 |
| 694 characteristics_finder_error_callback_.Run( |
| 695 {tx_characteristic_uuid_, kTXCharacteristicID}, |
| 696 {rx_characteristic_uuid_, kRXCharacteristicID}); |
| 697 |
| 698 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 699 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 700 } |
| 701 |
| 702 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 703 ConnectFailsNotifySessionError) { |
| 704 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 705 CreateConnection()); |
| 706 ConnectGatt(connection.get()); |
| 707 CharacteristicsFound(connection.get()); |
| 708 |
| 709 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)).Times(0); |
| 710 EXPECT_FALSE(notify_session_success_callback_.is_null()); |
| 711 ASSERT_FALSE(notify_session_error_callback_.is_null()); |
| 712 |
| 713 notify_session_error_callback_.Run( |
| 714 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); |
| 715 |
| 716 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 717 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 718 } |
| 719 |
| 720 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 721 ConnectFailsErrorSendingConnectionRequest) { |
| 722 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 723 CreateConnection()); |
| 724 ConnectGatt(connection.get()); |
| 725 CharacteristicsFound(connection.get()); |
| 726 NotifySessionStarted(connection.get()); |
| 727 |
| 728 // |connection| will call WriteRemoteCharacteristics(_,_) to try to send the |
| 729 // message |kMaxNumberOfTries| times. There is alredy one EXPECTA_CALL for |
| 730 // WriteRemoteCharacteristic(_,_,_) in NotifySessionStated, that's why we use |
| 731 // |kMaxNumberOfTries-1| in the EXPECT_CALL statement. |
| 732 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); |
| 733 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 734 .Times(kMaxNumberOfTries - 1) |
| 735 .WillRepeatedly( |
| 736 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 737 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 738 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 739 |
| 740 for (int i = 0; i < kMaxNumberOfTries; i++) { |
| 741 EXPECT_EQ(last_value_written_on_tx_characteristic_, kConnectionRequest); |
| 742 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 743 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 744 write_remote_characteristic_error_callback_.Run( |
| 745 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); |
| 746 } |
| 747 |
| 748 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 749 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 750 } |
| 751 |
| 752 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 753 ReceiveMessageSmallerThanCharacteristicSize) { |
| 754 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 755 CreateConnection()); |
| 756 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 757 |
| 758 std::string received_bytes; |
| 759 EXPECT_CALL(*connection, OnBytesReceived(_)) |
| 760 .WillOnce(SaveArg<0>(&received_bytes)); |
| 761 |
| 762 connection->GattCharacteristicValueChanged( |
| 763 adapter_.get(), rx_characteristic_.get(), kSmallPackets0); |
| 764 |
| 765 EXPECT_EQ(received_bytes, kSmallMessage); |
| 766 } |
| 767 |
| 768 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 769 ReceiveMessageLargerThanCharacteristicSize) { |
| 770 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 771 CreateConnection()); |
| 772 |
| 773 InitializeConnection(connection.get(), kLargeMaxPacketSize); |
| 774 |
| 775 std::string received_bytes; |
| 776 EXPECT_CALL(*connection, OnBytesReceived(_)) |
| 777 .WillOnce(SaveArg<0>(&received_bytes)); |
| 778 |
| 779 std::vector<Packet> packets = kLargePackets; |
| 780 |
| 781 for (auto packet : packets) { |
| 782 connection->GattCharacteristicValueChanged( |
| 783 adapter_.get(), rx_characteristic_.get(), packet); |
| 784 } |
| 785 EXPECT_EQ(received_bytes, kLargeMessage); |
| 786 } |
| 787 |
| 788 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 789 SendMessageSmallerThanCharacteristicSize) { |
| 790 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 791 CreateConnection()); |
| 792 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 793 |
| 794 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
| 795 // called. |
| 796 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 797 .WillOnce( |
| 798 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 799 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 800 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 801 |
| 802 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage))); |
| 803 |
| 804 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); |
| 805 |
| 806 RunWriteCharacteristicSuccessCallback(); |
| 807 |
| 808 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); |
| 809 EXPECT_EQ(kSmallMessage, connection_observer_.GetLastDeserializedMessage()); |
| 810 EXPECT_TRUE(connection_observer_.GetLastSendSuccess()); |
| 811 } |
| 812 |
| 813 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 814 SendMessageLargerThanCharacteristicSize) { |
| 815 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 816 CreateConnection()); |
| 817 |
| 818 InitializeConnection(connection.get(), kLargeMaxPacketSize); |
| 819 |
| 820 // Expecting a first call of WriteRemoteCharacteristic, after SendMessage is |
| 821 // called. |
| 822 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 823 .WillOnce( |
| 824 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 825 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 826 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 827 |
| 828 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage))); |
| 829 |
| 830 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); |
| 831 std::vector<uint8_t> bytes_received( |
| 832 last_value_written_on_tx_characteristic_.begin() + 1, |
| 833 last_value_written_on_tx_characteristic_.end()); |
| 834 |
| 835 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 836 .WillOnce( |
| 837 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 838 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 839 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 840 |
| 841 RunWriteCharacteristicSuccessCallback(); |
| 842 bytes_received.insert(bytes_received.end(), |
| 843 last_value_written_on_tx_characteristic_.begin() + 1, |
| 844 last_value_written_on_tx_characteristic_.end()); |
| 845 |
| 846 std::vector<uint8_t> expected(kLargeMessage.begin(), kLargeMessage.end()); |
| 847 EXPECT_EQ(expected, bytes_received); |
| 848 |
| 849 RunWriteCharacteristicSuccessCallback(); |
| 850 |
| 851 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); |
| 852 EXPECT_EQ(kLargeMessage, connection_observer_.GetLastDeserializedMessage()); |
| 853 EXPECT_TRUE(connection_observer_.GetLastSendSuccess()); |
| 854 } |
| 855 |
| 856 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 857 SendMessageKeepsFailing) { |
| 858 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 859 CreateConnection()); |
| 860 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 861 |
| 862 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 863 .Times(kMaxNumberOfTries) |
| 864 .WillRepeatedly( |
| 865 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 866 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 867 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 868 |
| 869 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kSmallMessage))); |
| 870 |
| 871 for (int i = 0; i < kMaxNumberOfTries; i++) { |
| 872 EXPECT_EQ(last_value_written_on_tx_characteristic_, kSmallPackets0); |
| 873 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 874 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 875 write_remote_characteristic_error_callback_.Run( |
| 876 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); |
| 877 if (i == kMaxNumberOfTries - 1) { |
| 878 EXPECT_EQ(1, connection_observer_.GetNumSendCompleted()); |
| 879 EXPECT_EQ(kSmallMessage, |
| 880 connection_observer_.GetLastDeserializedMessage()); |
| 881 EXPECT_FALSE(connection_observer_.GetLastSendSuccess()); |
| 882 } else { |
| 883 EXPECT_EQ(0, connection_observer_.GetNumSendCompleted()); |
| 884 } |
| 885 } |
| 886 |
| 887 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 888 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 889 } |
| 890 |
| 891 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 892 ReceiveCloseConnectionTest) { |
| 893 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 894 CreateConnection()); |
| 895 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 896 |
| 897 connection->GattCharacteristicValueChanged( |
| 898 adapter_.get(), rx_characteristic_.get(), kConnectionCloseUnknownError); |
| 899 |
| 900 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetReasonForClose(), |
| 901 ReasonForClose::UNKNOWN_ERROR); |
| 902 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 903 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 904 } |
| 905 |
| 906 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 907 ReceiverErrorTest) { |
| 908 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 909 CreateConnection()); |
| 910 |
| 911 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 912 |
| 913 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 914 .WillOnce( |
| 915 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 916 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 917 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 918 |
| 919 connection->GattCharacteristicValueChanged( |
| 920 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); |
| 921 |
| 922 EXPECT_EQ(last_value_written_on_tx_characteristic_, |
| 923 kConnectionCloseApplicationError); |
| 924 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetReasonToClose(), |
| 925 ReasonForClose::APPLICATION_ERROR); |
| 926 |
| 927 RunWriteCharacteristicSuccessCallback(); |
| 928 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 929 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 930 } |
| 931 |
| 932 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 933 ReceiverErrorWithPendingWritesTest) { |
| 934 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 935 CreateConnection()); |
| 936 |
| 937 InitializeConnection(connection.get(), kLargeMaxPacketSize); |
| 938 |
| 939 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 940 .WillOnce( |
| 941 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 942 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 943 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 944 |
| 945 connection->SendMessage(base::WrapUnique(new FakeWireMessage(kLargeMessage))); |
| 946 |
| 947 connection->GattCharacteristicValueChanged( |
| 948 adapter_.get(), rx_characteristic_.get(), kErroneousPacket); |
| 949 |
| 950 EXPECT_EQ(last_value_written_on_tx_characteristic_, kLargePackets0); |
| 951 |
| 952 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 953 .WillOnce( |
| 954 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 955 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 956 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 957 |
| 958 RunWriteCharacteristicSuccessCallback(); |
| 959 |
| 960 EXPECT_EQ(last_value_written_on_tx_characteristic_, |
| 961 kConnectionCloseApplicationError); |
| 962 EXPECT_EQ(receiver_factory_->GetMostRecentInstance()->GetReasonToClose(), |
| 963 ReasonForClose::APPLICATION_ERROR); |
| 964 |
| 965 RunWriteCharacteristicSuccessCallback(); |
| 966 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 967 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 968 } |
| 969 |
| 970 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 971 WriteConnectionCloseMaxNumberOfTimes) { |
| 972 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 973 CreateConnection()); |
| 974 |
| 975 InitializeConnection(connection.get(), kDefaultMaxPacketSize); |
| 976 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); |
| 977 |
| 978 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 979 .WillOnce( |
| 980 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 981 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 982 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 983 connection->Disconnect(); |
| 984 EXPECT_EQ(connection->sub_status(), SubStatus::CONNECTED); |
| 985 |
| 986 for (int i = 0; i < kMaxNumberOfTries; i++) { |
| 987 EXPECT_EQ(last_value_written_on_tx_characteristic_, |
| 988 kConnectionCloseSuccess); |
| 989 ASSERT_FALSE(write_remote_characteristic_error_callback_.is_null()); |
| 990 EXPECT_FALSE(write_remote_characteristic_success_callback_.is_null()); |
| 991 |
| 992 if (i != kMaxNumberOfTries - 1) { |
| 993 EXPECT_CALL(*tx_characteristic_, WriteRemoteCharacteristic(_, _, _)) |
| 994 .WillOnce( |
| 995 DoAll(SaveArg<0>(&last_value_written_on_tx_characteristic_), |
| 996 SaveArg<1>(&write_remote_characteristic_success_callback_), |
| 997 SaveArg<2>(&write_remote_characteristic_error_callback_))); |
| 998 } |
| 999 |
| 1000 write_remote_characteristic_error_callback_.Run( |
| 1001 device::BluetoothRemoteGattService::GATT_ERROR_UNKNOWN); |
| 1002 } |
| 1003 |
| 1004 EXPECT_EQ(connection->sub_status(), SubStatus::DISCONNECTED); |
| 1005 EXPECT_EQ(connection->status(), Connection::DISCONNECTED); |
| 1006 } |
| 1007 |
| 1008 TEST_F(ProximityAuthBluetoothLowEnergyWeaveClientConnectionTest, |
| 1009 ConnectAfterADelayWhenThrottled) { |
| 1010 std::unique_ptr<TestBluetoothLowEnergyWeaveClientConnection> connection( |
| 1011 CreateConnection()); |
| 1012 |
| 1013 EXPECT_CALL(*bluetooth_throttler_, GetDelay()) |
| 1014 .WillOnce(Return(base::TimeDelta(base::TimeDelta::FromSeconds(1)))); |
| 1015 EXPECT_CALL(*device_, CreateGattConnection(_, _)) |
| 1016 .WillOnce(DoAll(SaveArg<0>(&create_gatt_connection_success_callback_), |
| 1017 SaveArg<1>(&create_gatt_connection_error_callback_))); |
| 1018 |
| 1019 // No GATT connection should be created before the delay. |
| 1020 connection->Connect(); |
| 1021 EXPECT_EQ(connection->sub_status(), SubStatus::WAITING_GATT_CONNECTION); |
| 1022 EXPECT_EQ(connection->status(), Connection::IN_PROGRESS); |
| 1023 EXPECT_TRUE(create_gatt_connection_error_callback_.is_null()); |
| 1024 EXPECT_TRUE(create_gatt_connection_success_callback_.is_null()); |
| 1025 |
| 1026 // A GATT connection should be created after the delay. |
| 1027 task_runner_->RunUntilIdle(); |
| 1028 EXPECT_FALSE(create_gatt_connection_error_callback_.is_null()); |
| 1029 ASSERT_FALSE(create_gatt_connection_success_callback_.is_null()); |
| 1030 |
| 1031 // Preparing |connection| to run |create_gatt_connection_success_callback_|. |
| 1032 EXPECT_CALL(*connection, CreateCharacteristicsFinder(_, _)) |
| 1033 .WillOnce(DoAll( |
| 1034 SaveArg<0>(&characteristics_finder_success_callback_), |
| 1035 SaveArg<1>(&characteristics_finder_error_callback_), |
| 1036 Return(new NiceMock<MockBluetoothLowEnergyCharacteristicsFinder>))); |
| 1037 |
| 1038 create_gatt_connection_success_callback_.Run( |
| 1039 base::WrapUnique(new NiceMock<device::MockBluetoothGattConnection>( |
| 1040 adapter_, kTestRemoteDeviceBluetoothAddress))); |
| 1041 |
| 1042 CharacteristicsFound(connection.get()); |
| 1043 NotifySessionStarted(connection.get()); |
| 1044 ConnectionResponseReceived(connection.get(), kDefaultMaxPacketSize); |
| 1045 } |
| 1046 |
| 1047 } // namespace weave |
| 1048 |
| 1049 } // namespace proximity_auth |
OLD | NEW |