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