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 "chromeos/components/tether/ble_connection_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/timer/mock_timer.h" |
| 9 #include "chromeos/components/tether/ble_constants.h" |
| 10 #include "chromeos/components/tether/proto/tether.pb.h" |
| 11 #include "components/cryptauth/ble/bluetooth_low_energy_weave_client_connection.
h" |
| 12 #include "components/cryptauth/bluetooth_throttler.h" |
| 13 #include "components/cryptauth/connection.h" |
| 14 #include "components/cryptauth/fake_connection.h" |
| 15 #include "components/cryptauth/fake_secure_channel.h" |
| 16 #include "components/cryptauth/fake_secure_message_delegate.h" |
| 17 #include "components/cryptauth/remote_device_test_util.h" |
| 18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 using testing::_; |
| 23 using testing::NiceMock; |
| 24 using testing::Return; |
| 25 |
| 26 namespace chromeos { |
| 27 |
| 28 namespace tether { |
| 29 |
| 30 namespace { |
| 31 |
| 32 const char kTetherFeature[] = "magic_tether"; |
| 33 |
| 34 const char kUserId[] = "userId"; |
| 35 |
| 36 const char kBluetoothAddress1[] = "11:22:33:44:55:66"; |
| 37 const char kBluetoothAddress2[] = "22:33:44:55:66:77"; |
| 38 const char kBluetoothAddress3[] = "33:44:55:66:77:88"; |
| 39 |
| 40 class FakeSecureChannelDelegate : public cryptauth::SecureChannel::Delegate { |
| 41 public: |
| 42 FakeSecureChannelDelegate() {} |
| 43 ~FakeSecureChannelDelegate() override {} |
| 44 |
| 45 std::unique_ptr<cryptauth::SecureMessageDelegate> |
| 46 CreateSecureMessageDelegate() override { |
| 47 return base::MakeUnique<cryptauth::FakeSecureMessageDelegate>(); |
| 48 } |
| 49 }; |
| 50 |
| 51 class TestDelegate : public BleConnectionManager::Delegate { |
| 52 public: |
| 53 TestDelegate() {} |
| 54 ~TestDelegate() {} |
| 55 |
| 56 std::unique_ptr<cryptauth::SecureChannel::Delegate> |
| 57 CreateSecureChannelDelegate() override { |
| 58 return base::WrapUnique(new FakeSecureChannelDelegate()); |
| 59 } |
| 60 }; |
| 61 |
| 62 struct SecureChannelStatusChange { |
| 63 SecureChannelStatusChange(const cryptauth::RemoteDevice& remote_device, |
| 64 const cryptauth::SecureChannel::Status& old_status, |
| 65 const cryptauth::SecureChannel::Status& new_status) |
| 66 : remote_device(remote_device), |
| 67 old_status(old_status), |
| 68 new_status(new_status) {} |
| 69 |
| 70 cryptauth::RemoteDevice remote_device; |
| 71 cryptauth::SecureChannel::Status old_status; |
| 72 cryptauth::SecureChannel::Status new_status; |
| 73 }; |
| 74 |
| 75 struct ReceivedMessage { |
| 76 ReceivedMessage(const cryptauth::RemoteDevice& remote_device, |
| 77 const std::string& payload) |
| 78 : remote_device(remote_device), payload(payload) {} |
| 79 |
| 80 cryptauth::RemoteDevice remote_device; |
| 81 std::string payload; |
| 82 }; |
| 83 |
| 84 class TestObserver : public BleConnectionManager::Observer { |
| 85 public: |
| 86 TestObserver() {} |
| 87 |
| 88 // BleConnectionManager::Observer: |
| 89 void OnSecureChannelStatusChanged( |
| 90 const cryptauth::RemoteDevice& remote_device, |
| 91 const cryptauth::SecureChannel::Status& old_status, |
| 92 const cryptauth::SecureChannel::Status& new_status) override { |
| 93 connection_status_changes_.push_back( |
| 94 SecureChannelStatusChange(remote_device, old_status, new_status)); |
| 95 } |
| 96 |
| 97 void OnMessageReceived(const cryptauth::RemoteDevice& remote_device, |
| 98 const std::string& payload) override { |
| 99 received_messages_.push_back(ReceivedMessage(remote_device, payload)); |
| 100 } |
| 101 |
| 102 std::vector<SecureChannelStatusChange>& connection_status_changes() { |
| 103 return connection_status_changes_; |
| 104 } |
| 105 |
| 106 std::vector<ReceivedMessage>& received_messages() { |
| 107 return received_messages_; |
| 108 } |
| 109 |
| 110 private: |
| 111 std::vector<SecureChannelStatusChange> connection_status_changes_; |
| 112 std::vector<ReceivedMessage> received_messages_; |
| 113 }; |
| 114 |
| 115 class MockBleScanner : public BleScanner { |
| 116 public: |
| 117 MockBleScanner() : BleScanner(nullptr) {} |
| 118 ~MockBleScanner() override {} |
| 119 |
| 120 MOCK_METHOD1(RegisterScanFilterForDevice, |
| 121 bool(const cryptauth::RemoteDevice&)); |
| 122 MOCK_METHOD1(UnregisterScanFilterForDevice, |
| 123 bool(const cryptauth::RemoteDevice&)); |
| 124 |
| 125 void SimulateScanResults(const std::string& device_address, |
| 126 const cryptauth::RemoteDevice& remote_device) { |
| 127 for (auto& observer : observer_list_) { |
| 128 observer.OnReceivedAdvertisementFromDevice(device_address, remote_device); |
| 129 } |
| 130 } |
| 131 }; |
| 132 |
| 133 class MockBleAdvertiser : public BleAdvertiser { |
| 134 public: |
| 135 MockBleAdvertiser() : BleAdvertiser(nullptr, nullptr, nullptr) {} |
| 136 ~MockBleAdvertiser() override {} |
| 137 |
| 138 MOCK_METHOD1(StartAdvertisingToDevice, bool(const cryptauth::RemoteDevice&)); |
| 139 MOCK_METHOD1(StopAdvertisingToDevice, bool(const cryptauth::RemoteDevice&)); |
| 140 }; |
| 141 |
| 142 class MockBluetoothThrottler : public cryptauth::BluetoothThrottler { |
| 143 public: |
| 144 MockBluetoothThrottler() {} |
| 145 virtual ~MockBluetoothThrottler() {} |
| 146 |
| 147 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); |
| 148 MOCK_METHOD1(OnConnection, void(cryptauth::Connection*)); |
| 149 }; |
| 150 |
| 151 class FakeConnectionWithAddress : public cryptauth::FakeConnection { |
| 152 public: |
| 153 FakeConnectionWithAddress(const cryptauth::RemoteDevice& remote_device, |
| 154 const std::string& device_address) |
| 155 : FakeConnection(remote_device, false /* should_auto_connect */), |
| 156 device_address_(device_address) {} |
| 157 |
| 158 // cryptauth::Connection: |
| 159 std::string GetDeviceAddress() override { return device_address_; } |
| 160 |
| 161 private: |
| 162 const std::string device_address_; |
| 163 }; |
| 164 |
| 165 class FakeConnectionFactory |
| 166 : public cryptauth::weave::BluetoothLowEnergyWeaveClientConnection:: |
| 167 Factory { |
| 168 public: |
| 169 FakeConnectionFactory( |
| 170 scoped_refptr<device::BluetoothAdapter> expected_adapter, |
| 171 const device::BluetoothUUID expected_remote_service_uuid, |
| 172 cryptauth::BluetoothThrottler* expected_bluetooth_throttler) |
| 173 : expected_adapter_(expected_adapter), |
| 174 expected_remote_service_uuid_(expected_remote_service_uuid), |
| 175 expected_bluetooth_throttler_(expected_bluetooth_throttler) {} |
| 176 |
| 177 std::unique_ptr<cryptauth::Connection> BuildInstance( |
| 178 const cryptauth::RemoteDevice& remote_device, |
| 179 const std::string& device_address, |
| 180 scoped_refptr<device::BluetoothAdapter> adapter, |
| 181 const device::BluetoothUUID remote_service_uuid, |
| 182 cryptauth::BluetoothThrottler* bluetooth_throttler) override { |
| 183 EXPECT_EQ(expected_adapter_, adapter); |
| 184 EXPECT_EQ(expected_remote_service_uuid_, remote_service_uuid); |
| 185 EXPECT_EQ(expected_bluetooth_throttler_, bluetooth_throttler); |
| 186 |
| 187 return base::WrapUnique<FakeConnectionWithAddress>( |
| 188 new FakeConnectionWithAddress(remote_device, device_address)); |
| 189 } |
| 190 |
| 191 private: |
| 192 scoped_refptr<device::BluetoothAdapter> expected_adapter_; |
| 193 const device::BluetoothUUID expected_remote_service_uuid_; |
| 194 cryptauth::BluetoothThrottler* expected_bluetooth_throttler_; |
| 195 }; |
| 196 |
| 197 std::vector<cryptauth::RemoteDevice> CreateTestDevices(size_t num_to_create) { |
| 198 std::vector<cryptauth::RemoteDevice> test_devices = |
| 199 cryptauth::GenerateTestRemoteDevices(num_to_create); |
| 200 for (auto& device : test_devices) { |
| 201 device.user_id = std::string(kUserId); |
| 202 } |
| 203 return test_devices; |
| 204 } |
| 205 |
| 206 } // namespace |
| 207 |
| 208 class BleConnectionManagerTest : public testing::Test { |
| 209 protected: |
| 210 class FakeSecureChannel : public cryptauth::FakeSecureChannel { |
| 211 public: |
| 212 FakeSecureChannel( |
| 213 std::unique_ptr<cryptauth::Connection> connection, |
| 214 std::unique_ptr<cryptauth::SecureChannel::Delegate> delegate) |
| 215 : cryptauth::FakeSecureChannel(std::move(connection), |
| 216 std::move(delegate)) {} |
| 217 ~FakeSecureChannel() override {} |
| 218 |
| 219 void AddObserver(Observer* observer) override { |
| 220 cryptauth::FakeSecureChannel::AddObserver(observer); |
| 221 |
| 222 EXPECT_EQ(static_cast<size_t>(1), observers().size()); |
| 223 } |
| 224 |
| 225 void RemoveObserver(Observer* observer) override { |
| 226 cryptauth::FakeSecureChannel::RemoveObserver(observer); |
| 227 EXPECT_EQ(static_cast<size_t>(0), observers().size()); |
| 228 } |
| 229 }; |
| 230 |
| 231 class FakeSecureChannelFactory : public cryptauth::SecureChannel::Factory { |
| 232 public: |
| 233 FakeSecureChannelFactory() {} |
| 234 |
| 235 void SetExpectedDeviceAddress(const std::string& expected_device_address) { |
| 236 expected_device_address_ = expected_device_address; |
| 237 } |
| 238 |
| 239 std::unique_ptr<cryptauth::SecureChannel> BuildInstance( |
| 240 std::unique_ptr<cryptauth::Connection> connection, |
| 241 std::unique_ptr<cryptauth::SecureChannel::Delegate> delegate) override { |
| 242 FakeConnectionWithAddress* fake_connection = |
| 243 static_cast<FakeConnectionWithAddress*>(connection.get()); |
| 244 EXPECT_EQ(expected_device_address_, fake_connection->GetDeviceAddress()); |
| 245 return base::WrapUnique( |
| 246 new FakeSecureChannel(std::move(connection), std::move(delegate))); |
| 247 } |
| 248 |
| 249 private: |
| 250 std::string expected_device_address_; |
| 251 }; |
| 252 |
| 253 class MockTimerFactory : public BleConnectionManager::TimerFactory { |
| 254 public: |
| 255 std::unique_ptr<base::Timer> CreateTimer() override { |
| 256 return base::MakeUnique<base::MockTimer>(false /* retains_user_task */, |
| 257 false /* is_repeating */); |
| 258 } |
| 259 }; |
| 260 |
| 261 BleConnectionManagerTest() : test_devices_(CreateTestDevices(4)) { |
| 262 // These tests assume a maximum of two concurrent advertisers. Some of the |
| 263 // multi-device tests would need to be re-written if this constant changes. |
| 264 EXPECT_EQ(2, kMaxConcurrentAdvertisements); |
| 265 } |
| 266 |
| 267 void SetUp() override { |
| 268 verified_status_changes_.clear(); |
| 269 verified_received_messages_.clear(); |
| 270 |
| 271 delegate_ = new TestDelegate(); |
| 272 mock_adapter_ = |
| 273 make_scoped_refptr(new NiceMock<device::MockBluetoothAdapter>()); |
| 274 |
| 275 mock_ble_scanner_ = new MockBleScanner(); |
| 276 ON_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(_)) |
| 277 .WillByDefault(Return(true)); |
| 278 ON_CALL(*mock_ble_scanner_, UnregisterScanFilterForDevice(_)) |
| 279 .WillByDefault(Return(true)); |
| 280 |
| 281 mock_ble_advertiser_ = new MockBleAdvertiser(); |
| 282 ON_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(_)) |
| 283 .WillByDefault(Return(true)); |
| 284 ON_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(_)) |
| 285 .WillByDefault(Return(true)); |
| 286 |
| 287 device_queue_ = new BleAdvertisementDeviceQueue(); |
| 288 mock_timer_factory_ = new MockTimerFactory(); |
| 289 mock_bluetooth_throttler_ = base::WrapUnique(new MockBluetoothThrottler()); |
| 290 |
| 291 fake_connection_factory_ = base::WrapUnique(new FakeConnectionFactory( |
| 292 mock_adapter_, device::BluetoothUUID(std::string(kGattServerUuid)), |
| 293 mock_bluetooth_throttler_.get())); |
| 294 cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory:: |
| 295 SetInstanceForTesting(std::move(fake_connection_factory_)); |
| 296 |
| 297 fake_secure_channel_factory_ = |
| 298 base::WrapUnique(new FakeSecureChannelFactory()); |
| 299 cryptauth::SecureChannel::Factory::SetInstanceForTesting( |
| 300 fake_secure_channel_factory_.get()); |
| 301 |
| 302 manager_ = base::WrapUnique(new BleConnectionManager( |
| 303 base::WrapUnique(delegate_), mock_adapter_, |
| 304 base::WrapUnique(mock_ble_scanner_), |
| 305 base::WrapUnique(mock_ble_advertiser_), base::WrapUnique(device_queue_), |
| 306 base::WrapUnique(mock_timer_factory_), |
| 307 mock_bluetooth_throttler_.get())); |
| 308 test_observer_ = base::WrapUnique(new TestObserver()); |
| 309 manager_->AddObserver(test_observer_.get()); |
| 310 } |
| 311 |
| 312 void TearDown() override { |
| 313 // All state changes should have already been verified. This ensures that |
| 314 // no test has missed one. |
| 315 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>()); |
| 316 |
| 317 // Same with received messages. |
| 318 VerifyReceivedMessages(std::vector<ReceivedMessage>()); |
| 319 } |
| 320 |
| 321 void VerifyConnectionStateChanges( |
| 322 const std::vector<SecureChannelStatusChange>& expected_changes) { |
| 323 verified_status_changes_.insert(verified_status_changes_.end(), |
| 324 expected_changes.begin(), |
| 325 expected_changes.end()); |
| 326 |
| 327 ASSERT_EQ(verified_status_changes_.size(), |
| 328 test_observer_->connection_status_changes().size()); |
| 329 |
| 330 for (size_t i = 0; i < verified_status_changes_.size(); i++) { |
| 331 EXPECT_EQ(verified_status_changes_[i].remote_device, |
| 332 test_observer_->connection_status_changes()[i].remote_device); |
| 333 EXPECT_EQ(verified_status_changes_[i].old_status, |
| 334 test_observer_->connection_status_changes()[i].old_status); |
| 335 EXPECT_EQ(verified_status_changes_[i].new_status, |
| 336 test_observer_->connection_status_changes()[i].new_status); |
| 337 } |
| 338 } |
| 339 |
| 340 void VerifyReceivedMessages( |
| 341 const std::vector<ReceivedMessage>& expected_messages) { |
| 342 verified_received_messages_.insert(verified_received_messages_.end(), |
| 343 expected_messages.begin(), |
| 344 expected_messages.end()); |
| 345 |
| 346 ASSERT_EQ(verified_received_messages_.size(), |
| 347 test_observer_->received_messages().size()); |
| 348 |
| 349 for (size_t i = 0; i < verified_received_messages_.size(); i++) { |
| 350 EXPECT_EQ(verified_received_messages_[i].remote_device, |
| 351 test_observer_->received_messages()[i].remote_device); |
| 352 EXPECT_EQ(verified_received_messages_[i].payload, |
| 353 test_observer_->received_messages()[i].payload); |
| 354 } |
| 355 } |
| 356 |
| 357 void VerifyNoTimeoutSet(const cryptauth::RemoteDevice& remote_device) { |
| 358 std::shared_ptr<BleConnectionManager::ConnectionMetadata> |
| 359 connection_metadata = manager_->GetConnectionMetadata(remote_device); |
| 360 EXPECT_TRUE(connection_metadata); |
| 361 EXPECT_FALSE( |
| 362 connection_metadata->connection_attempt_timeout_timer_->IsRunning()); |
| 363 } |
| 364 |
| 365 void VerifyFailImmediatelyTimeoutSet( |
| 366 const cryptauth::RemoteDevice& remote_device) { |
| 367 VerifyTimeoutSet(remote_device, |
| 368 BleConnectionManager::kFailImmediatelyTimeoutMillis); |
| 369 } |
| 370 |
| 371 void VerifyAdvertisingTimeoutSet( |
| 372 const cryptauth::RemoteDevice& remote_device) { |
| 373 VerifyTimeoutSet(remote_device, |
| 374 BleConnectionManager::kAdvertisingTimeoutMillis); |
| 375 } |
| 376 |
| 377 void VerifyTimeoutSet(const cryptauth::RemoteDevice& remote_device, |
| 378 int64_t expected_num_millis) { |
| 379 std::shared_ptr<BleConnectionManager::ConnectionMetadata> |
| 380 connection_metadata = manager_->GetConnectionMetadata(remote_device); |
| 381 EXPECT_TRUE(connection_metadata); |
| 382 EXPECT_TRUE( |
| 383 connection_metadata->connection_attempt_timeout_timer_->IsRunning()); |
| 384 EXPECT_EQ(base::TimeDelta::FromMilliseconds(expected_num_millis), |
| 385 connection_metadata->connection_attempt_timeout_timer_ |
| 386 ->GetCurrentDelay()); |
| 387 } |
| 388 |
| 389 void FireTimerForDevice(const cryptauth::RemoteDevice& remote_device) { |
| 390 std::shared_ptr<BleConnectionManager::ConnectionMetadata> |
| 391 connection_metadata = manager_->GetConnectionMetadata(remote_device); |
| 392 EXPECT_TRUE(connection_metadata); |
| 393 EXPECT_TRUE( |
| 394 connection_metadata->connection_attempt_timeout_timer_->IsRunning()); |
| 395 static_cast<base::MockTimer*>( |
| 396 connection_metadata->connection_attempt_timeout_timer_.get()) |
| 397 ->Fire(); |
| 398 } |
| 399 |
| 400 FakeSecureChannel* GetChannelForDevice( |
| 401 const cryptauth::RemoteDevice& remote_device) { |
| 402 std::shared_ptr<BleConnectionManager::ConnectionMetadata> |
| 403 connection_metadata = manager_->GetConnectionMetadata(remote_device); |
| 404 EXPECT_TRUE(connection_metadata); |
| 405 EXPECT_TRUE(connection_metadata->secure_channel_); |
| 406 return static_cast<FakeSecureChannel*>( |
| 407 connection_metadata->secure_channel_.get()); |
| 408 } |
| 409 |
| 410 void VerifyDeviceRegistered(const cryptauth::RemoteDevice& remote_device) { |
| 411 std::shared_ptr<BleConnectionManager::ConnectionMetadata> |
| 412 connection_metadata = manager_->GetConnectionMetadata(remote_device); |
| 413 EXPECT_TRUE(connection_metadata); |
| 414 } |
| 415 |
| 416 void VerifyDeviceNotRegistered(const cryptauth::RemoteDevice& remote_device) { |
| 417 std::shared_ptr<BleConnectionManager::ConnectionMetadata> |
| 418 connection_metadata = manager_->GetConnectionMetadata(remote_device); |
| 419 EXPECT_FALSE(connection_metadata); |
| 420 } |
| 421 |
| 422 // Registers |remote_device|, creates a connection to that device at |
| 423 // |bluetooth_address|, and authenticates the resulting channel. |
| 424 FakeSecureChannel* ConnectSuccessfully( |
| 425 const cryptauth::RemoteDevice& remote_device, |
| 426 const std::string& bluetooth_address, |
| 427 const MessageType connection_reason) { |
| 428 manager_->RegisterRemoteDevice(remote_device, connection_reason); |
| 429 VerifyAdvertisingTimeoutSet(remote_device); |
| 430 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 431 {remote_device, cryptauth::SecureChannel::Status::DISCONNECTED, |
| 432 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 433 |
| 434 FakeSecureChannel* channel = |
| 435 ConnectChannel(remote_device, bluetooth_address); |
| 436 AuthenticateChannel(remote_device); |
| 437 return channel; |
| 438 } |
| 439 |
| 440 // Creates a connection to |remote_device| at |bluetooth_address|. The device |
| 441 // must be registered before calling this function. |
| 442 FakeSecureChannel* ConnectChannel( |
| 443 const cryptauth::RemoteDevice& remote_device, |
| 444 const std::string& bluetooth_address) { |
| 445 VerifyDeviceRegistered(remote_device); |
| 446 |
| 447 fake_secure_channel_factory_->SetExpectedDeviceAddress(bluetooth_address); |
| 448 mock_ble_scanner_->SimulateScanResults(bluetooth_address, remote_device); |
| 449 return GetChannelForDevice(remote_device); |
| 450 } |
| 451 |
| 452 // Authenticates the SecureChannel associated with |remote_device|. The device |
| 453 // must be registered and already have an associated channel before calling |
| 454 // this function. |
| 455 void AuthenticateChannel(const cryptauth::RemoteDevice& remote_device) { |
| 456 VerifyDeviceRegistered(remote_device); |
| 457 |
| 458 FakeSecureChannel* channel = GetChannelForDevice(remote_device); |
| 459 DCHECK(channel); |
| 460 |
| 461 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTING); |
| 462 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTED); |
| 463 channel->ChangeStatus(cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 464 channel->ChangeStatus(cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 465 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 466 {remote_device, cryptauth::SecureChannel::Status::CONNECTING, |
| 467 cryptauth::SecureChannel::Status::CONNECTED}, |
| 468 {remote_device, cryptauth::SecureChannel::Status::CONNECTED, |
| 469 cryptauth::SecureChannel::Status::AUTHENTICATING}, |
| 470 {remote_device, cryptauth::SecureChannel::Status::AUTHENTICATING, |
| 471 cryptauth::SecureChannel::Status::AUTHENTICATED}}); |
| 472 } |
| 473 |
| 474 void VerifyLastMessageSent(FakeSecureChannel* channel, |
| 475 const std::string& payload, |
| 476 size_t expected_size) { |
| 477 ASSERT_EQ(expected_size, channel->sent_messages().size()); |
| 478 cryptauth::FakeSecureChannel::SentMessage sent_message = |
| 479 channel->sent_messages()[expected_size - 1]; |
| 480 EXPECT_EQ(std::string(kTetherFeature), sent_message.feature); |
| 481 EXPECT_EQ(payload, sent_message.payload); |
| 482 } |
| 483 |
| 484 const std::vector<cryptauth::RemoteDevice> test_devices_; |
| 485 |
| 486 BleConnectionManager::Delegate* delegate_; |
| 487 scoped_refptr<NiceMock<device::MockBluetoothAdapter>> mock_adapter_; |
| 488 MockBleScanner* mock_ble_scanner_; |
| 489 MockBleAdvertiser* mock_ble_advertiser_; |
| 490 BleAdvertisementDeviceQueue* device_queue_; |
| 491 MockTimerFactory* mock_timer_factory_; |
| 492 std::unique_ptr<MockBluetoothThrottler> mock_bluetooth_throttler_; |
| 493 std::unique_ptr<FakeConnectionFactory> fake_connection_factory_; |
| 494 std::unique_ptr<FakeSecureChannelFactory> fake_secure_channel_factory_; |
| 495 std::unique_ptr<TestObserver> test_observer_; |
| 496 |
| 497 std::vector<SecureChannelStatusChange> verified_status_changes_; |
| 498 std::vector<ReceivedMessage> verified_received_messages_; |
| 499 |
| 500 std::unique_ptr<BleConnectionManager> manager_; |
| 501 |
| 502 private: |
| 503 DISALLOW_COPY_AND_ASSIGN(BleConnectionManagerTest); |
| 504 }; |
| 505 |
| 506 TEST_F(BleConnectionManagerTest, TestCannotScan) { |
| 507 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0])) |
| 508 .WillOnce(Return(false)); |
| 509 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 510 .Times(0); |
| 511 |
| 512 manager_->RegisterRemoteDevice(test_devices_[0], |
| 513 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 514 VerifyFailImmediatelyTimeoutSet(test_devices_[0]); |
| 515 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 516 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 517 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 518 } |
| 519 |
| 520 TEST_F(BleConnectionManagerTest, TestCannotAdvertise) { |
| 521 EXPECT_CALL(*mock_ble_scanner_, |
| 522 RegisterScanFilterForDevice(test_devices_[0])); |
| 523 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 524 .WillOnce(Return(false)); |
| 525 |
| 526 manager_->RegisterRemoteDevice(test_devices_[0], |
| 527 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 528 VerifyFailImmediatelyTimeoutSet(test_devices_[0]); |
| 529 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 530 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 531 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 532 } |
| 533 |
| 534 TEST_F(BleConnectionManagerTest, TestRegistersButNoResult) { |
| 535 EXPECT_CALL(*mock_ble_scanner_, |
| 536 RegisterScanFilterForDevice(test_devices_[0])); |
| 537 EXPECT_CALL(*mock_ble_advertiser_, |
| 538 StartAdvertisingToDevice(test_devices_[0])); |
| 539 |
| 540 manager_->RegisterRemoteDevice(test_devices_[0], |
| 541 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 542 VerifyAdvertisingTimeoutSet(test_devices_[0]); |
| 543 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 544 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 545 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 546 } |
| 547 |
| 548 TEST_F(BleConnectionManagerTest, TestRegistersAndUnregister_NoConnection) { |
| 549 // Expected to start a connection attempt after the device is registered and |
| 550 // to stop the attempt once the device is unregistered. |
| 551 EXPECT_CALL(*mock_ble_scanner_, |
| 552 RegisterScanFilterForDevice(test_devices_[0])); |
| 553 EXPECT_CALL(*mock_ble_advertiser_, |
| 554 StartAdvertisingToDevice(test_devices_[0])); |
| 555 EXPECT_CALL(*mock_ble_scanner_, |
| 556 UnregisterScanFilterForDevice(test_devices_[0])); |
| 557 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 558 |
| 559 manager_->RegisterRemoteDevice(test_devices_[0], |
| 560 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 561 VerifyAdvertisingTimeoutSet(test_devices_[0]); |
| 562 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 563 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 564 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 565 |
| 566 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 567 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 568 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 569 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING, |
| 570 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 571 } |
| 572 |
| 573 TEST_F(BleConnectionManagerTest, TestRegisterWithNoConnection_TimeoutOccurs) { |
| 574 // Expected to start a connection attempt, then stop once the timer fires, |
| 575 // then start again, then stop when the device is unregistered; in total, 2 |
| 576 // starts and 2 stops. |
| 577 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0])) |
| 578 .Times(2); |
| 579 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 580 .Times(2); |
| 581 EXPECT_CALL(*mock_ble_scanner_, |
| 582 UnregisterScanFilterForDevice(test_devices_[0])) |
| 583 .Times(2); |
| 584 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])) |
| 585 .Times(2); |
| 586 |
| 587 manager_->RegisterRemoteDevice(test_devices_[0], |
| 588 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 589 VerifyAdvertisingTimeoutSet(test_devices_[0]); |
| 590 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 591 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 592 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 593 |
| 594 FireTimerForDevice(test_devices_[0]); |
| 595 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 596 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING, |
| 597 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 598 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 599 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 600 |
| 601 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 602 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 603 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 604 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING, |
| 605 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 606 } |
| 607 |
| 608 TEST_F(BleConnectionManagerTest, TestSuccessfulConnection_FailsAuthentication) { |
| 609 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0])) |
| 610 .Times(2); |
| 611 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 612 .Times(2); |
| 613 EXPECT_CALL(*mock_ble_scanner_, |
| 614 UnregisterScanFilterForDevice(test_devices_[0])); |
| 615 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 616 |
| 617 manager_->RegisterRemoteDevice(test_devices_[0], |
| 618 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 619 VerifyAdvertisingTimeoutSet(test_devices_[0]); |
| 620 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 621 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 622 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 623 |
| 624 fake_secure_channel_factory_->SetExpectedDeviceAddress( |
| 625 std::string(kBluetoothAddress1)); |
| 626 mock_ble_scanner_->SimulateScanResults(std::string(kBluetoothAddress1), |
| 627 test_devices_[0]); |
| 628 FakeSecureChannel* channel = GetChannelForDevice(test_devices_[0]); |
| 629 |
| 630 // Should not result in an additional "disconnected => connecting" broadcast. |
| 631 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTING); |
| 632 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>()); |
| 633 |
| 634 channel->ChangeStatus(cryptauth::SecureChannel::Status::CONNECTED); |
| 635 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 636 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING, |
| 637 cryptauth::SecureChannel::Status::CONNECTED}}); |
| 638 |
| 639 channel->ChangeStatus(cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 640 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 641 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED, |
| 642 cryptauth::SecureChannel::Status::AUTHENTICATING}}); |
| 643 |
| 644 // Fail authentication, which should automatically start a retry. |
| 645 channel->ChangeStatus(cryptauth::SecureChannel::Status::DISCONNECTED); |
| 646 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 647 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING, |
| 648 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 649 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 650 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 651 } |
| 652 |
| 653 TEST_F(BleConnectionManagerTest, TestSuccessfulConnection_SendAndReceive) { |
| 654 EXPECT_CALL(*mock_ble_scanner_, |
| 655 RegisterScanFilterForDevice(test_devices_[0])); |
| 656 EXPECT_CALL(*mock_ble_advertiser_, |
| 657 StartAdvertisingToDevice(test_devices_[0])); |
| 658 EXPECT_CALL(*mock_ble_scanner_, |
| 659 UnregisterScanFilterForDevice(test_devices_[0])); |
| 660 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 661 |
| 662 FakeSecureChannel* channel = |
| 663 ConnectSuccessfully(test_devices_[0], std::string(kBluetoothAddress1), |
| 664 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 665 |
| 666 manager_->SendMessage(test_devices_[0], "request1"); |
| 667 VerifyLastMessageSent(channel, "request1", 1); |
| 668 |
| 669 channel->ReceiveMessage(std::string(kTetherFeature), "response1"); |
| 670 VerifyReceivedMessages( |
| 671 std::vector<ReceivedMessage>{{test_devices_[0], "response1"}}); |
| 672 |
| 673 manager_->SendMessage(test_devices_[0], "request2"); |
| 674 VerifyLastMessageSent(channel, "request2", 2); |
| 675 |
| 676 channel->ReceiveMessage(std::string(kTetherFeature), "response2"); |
| 677 VerifyReceivedMessages( |
| 678 std::vector<ReceivedMessage>{{test_devices_[0], "response2"}}); |
| 679 |
| 680 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 681 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 682 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 683 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 684 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 685 VerifyDeviceNotRegistered(test_devices_[0]); |
| 686 } |
| 687 |
| 688 TEST_F(BleConnectionManagerTest, |
| 689 TestSuccessfulConnection_MultipleConnectionReasons) { |
| 690 EXPECT_CALL(*mock_ble_scanner_, |
| 691 RegisterScanFilterForDevice(test_devices_[0])); |
| 692 EXPECT_CALL(*mock_ble_advertiser_, |
| 693 StartAdvertisingToDevice(test_devices_[0])); |
| 694 EXPECT_CALL(*mock_ble_scanner_, |
| 695 UnregisterScanFilterForDevice(test_devices_[0])); |
| 696 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 697 |
| 698 ConnectSuccessfully(test_devices_[0], std::string(kBluetoothAddress1), |
| 699 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 700 |
| 701 // Now, register a different connection reason. |
| 702 manager_->RegisterRemoteDevice(test_devices_[0], |
| 703 MessageType::CONNECT_TETHERING_REQUEST); |
| 704 |
| 705 // Unregister the |TETHER_AVAILABILITY_REQUEST| reason, but leave the |
| 706 // |CONNECT_TETHERING_REQUEST| registered. |
| 707 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 708 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 709 VerifyDeviceRegistered(test_devices_[0]); |
| 710 |
| 711 // Now, unregister the other reason; this should cause the device to be |
| 712 // fully unregistered. |
| 713 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 714 MessageType::CONNECT_TETHERING_REQUEST); |
| 715 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 716 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 717 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 718 VerifyDeviceNotRegistered(test_devices_[0]); |
| 719 } |
| 720 |
| 721 TEST_F(BleConnectionManagerTest, |
| 722 TestSuccessfulConnection_DisconnectsAfterConnection) { |
| 723 // A reconnection attempt is expected once the disconnection occurs, meaning |
| 724 // two separate connection attempts. |
| 725 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0])) |
| 726 .Times(2); |
| 727 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 728 .Times(2); |
| 729 EXPECT_CALL(*mock_ble_scanner_, |
| 730 UnregisterScanFilterForDevice(test_devices_[0])); |
| 731 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 732 |
| 733 FakeSecureChannel* channel = |
| 734 ConnectSuccessfully(test_devices_[0], std::string(kBluetoothAddress1), |
| 735 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 736 |
| 737 channel->ChangeStatus(cryptauth::SecureChannel::Status::DISCONNECTED); |
| 738 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 739 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 740 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 741 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 742 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 743 } |
| 744 |
| 745 TEST_F(BleConnectionManagerTest, TwoDevices_NeitherCanScan) { |
| 746 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0])) |
| 747 .WillOnce(Return(false)); |
| 748 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 749 .Times(0); |
| 750 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1])) |
| 751 .WillOnce(Return(false)); |
| 752 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1])) |
| 753 .Times(0); |
| 754 |
| 755 manager_->RegisterRemoteDevice(test_devices_[0], |
| 756 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 757 VerifyFailImmediatelyTimeoutSet(test_devices_[0]); |
| 758 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 759 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 760 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 761 |
| 762 manager_->RegisterRemoteDevice(test_devices_[1], |
| 763 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 764 VerifyFailImmediatelyTimeoutSet(test_devices_[1]); |
| 765 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 766 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 767 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 768 } |
| 769 |
| 770 TEST_F(BleConnectionManagerTest, TwoDevices_NeitherCanAdvertise) { |
| 771 EXPECT_CALL(*mock_ble_scanner_, |
| 772 RegisterScanFilterForDevice(test_devices_[0])); |
| 773 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 774 .WillOnce(Return(false)); |
| 775 EXPECT_CALL(*mock_ble_scanner_, |
| 776 RegisterScanFilterForDevice(test_devices_[1])); |
| 777 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1])) |
| 778 .WillOnce(Return(false)); |
| 779 |
| 780 manager_->RegisterRemoteDevice(test_devices_[0], |
| 781 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 782 VerifyFailImmediatelyTimeoutSet(test_devices_[0]); |
| 783 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 784 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 785 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 786 |
| 787 manager_->RegisterRemoteDevice(test_devices_[1], |
| 788 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 789 VerifyFailImmediatelyTimeoutSet(test_devices_[1]); |
| 790 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 791 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 792 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 793 } |
| 794 |
| 795 TEST_F(BleConnectionManagerTest, |
| 796 TwoDevices_RegisterWithNoConnection_TimerFires) { |
| 797 // Expected to start a connection attempt, then stop once the timer fires, |
| 798 // then start again, then stop when the device is unregistered; in total, 2 |
| 799 // starts and 2 stops. |
| 800 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[0])) |
| 801 .Times(2); |
| 802 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[0])) |
| 803 .Times(2); |
| 804 EXPECT_CALL(*mock_ble_scanner_, |
| 805 UnregisterScanFilterForDevice(test_devices_[0])) |
| 806 .Times(2); |
| 807 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])) |
| 808 .Times(2); |
| 809 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1])) |
| 810 .Times(2); |
| 811 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1])) |
| 812 .Times(2); |
| 813 EXPECT_CALL(*mock_ble_scanner_, |
| 814 UnregisterScanFilterForDevice(test_devices_[1])) |
| 815 .Times(2); |
| 816 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1])) |
| 817 .Times(2); |
| 818 |
| 819 // Register device 0. |
| 820 manager_->RegisterRemoteDevice(test_devices_[0], |
| 821 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 822 VerifyAdvertisingTimeoutSet(test_devices_[0]); |
| 823 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 824 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 825 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 826 |
| 827 // Register device 1. |
| 828 manager_->RegisterRemoteDevice(test_devices_[1], |
| 829 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 830 VerifyAdvertisingTimeoutSet(test_devices_[1]); |
| 831 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 832 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 833 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 834 |
| 835 // Simulate timeout for device 0 by firing timeout. |
| 836 FireTimerForDevice(test_devices_[0]); |
| 837 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 838 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING, |
| 839 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 840 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 841 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 842 |
| 843 // Simulate timeout for device 1 by firing timeout. |
| 844 FireTimerForDevice(test_devices_[1]); |
| 845 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 846 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
| 847 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 848 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 849 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 850 |
| 851 // Unregister device 0. |
| 852 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 853 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 854 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 855 {test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING, |
| 856 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 857 |
| 858 // Unregister device 1. |
| 859 manager_->UnregisterRemoteDevice(test_devices_[1], |
| 860 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 861 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 862 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
| 863 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 864 } |
| 865 |
| 866 TEST_F(BleConnectionManagerTest, TwoDevices_OneConnects) { |
| 867 // Device 0 is expected to start the attempt and stop once a connection has |
| 868 // been achieved. Device 1 is expected to start, then stop once the tiemr |
| 869 // fires, then start again. |
| 870 EXPECT_CALL(*mock_ble_scanner_, |
| 871 RegisterScanFilterForDevice(test_devices_[0])); |
| 872 EXPECT_CALL(*mock_ble_advertiser_, |
| 873 StartAdvertisingToDevice(test_devices_[0])); |
| 874 EXPECT_CALL(*mock_ble_scanner_, |
| 875 UnregisterScanFilterForDevice(test_devices_[0])); |
| 876 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 877 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1])) |
| 878 .Times(2); |
| 879 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1])) |
| 880 .Times(2); |
| 881 EXPECT_CALL(*mock_ble_scanner_, |
| 882 UnregisterScanFilterForDevice(test_devices_[1])) |
| 883 .Times(2); |
| 884 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1])) |
| 885 .Times(2); |
| 886 |
| 887 // Successfully connect to device 0. |
| 888 ConnectSuccessfully(test_devices_[0], std::string(kBluetoothAddress1), |
| 889 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 890 |
| 891 // Register device 1. |
| 892 manager_->RegisterRemoteDevice(test_devices_[1], |
| 893 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 894 VerifyAdvertisingTimeoutSet(test_devices_[1]); |
| 895 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 896 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 897 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 898 |
| 899 // Simulate timeout for device 1 by firing timeout. |
| 900 FireTimerForDevice(test_devices_[1]); |
| 901 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 902 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
| 903 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 904 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 905 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 906 |
| 907 // Unregister device 0. |
| 908 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 909 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 910 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 911 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 912 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 913 |
| 914 // Unregister device 1. |
| 915 manager_->UnregisterRemoteDevice(test_devices_[1], |
| 916 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 917 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 918 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
| 919 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 920 } |
| 921 |
| 922 TEST_F(BleConnectionManagerTest, TwoDevices_BothConnectSendAndReceive) { |
| 923 // Device 0 is expected to start the attempt and stop once a connection has |
| 924 // been achieved. Device 1 is expected to start, then stop once the timer |
| 925 // fires, then start again. |
| 926 EXPECT_CALL(*mock_ble_scanner_, |
| 927 RegisterScanFilterForDevice(test_devices_[0])); |
| 928 EXPECT_CALL(*mock_ble_advertiser_, |
| 929 StartAdvertisingToDevice(test_devices_[0])); |
| 930 EXPECT_CALL(*mock_ble_scanner_, |
| 931 UnregisterScanFilterForDevice(test_devices_[0])); |
| 932 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 933 EXPECT_CALL(*mock_ble_scanner_, |
| 934 RegisterScanFilterForDevice(test_devices_[1])); |
| 935 EXPECT_CALL(*mock_ble_advertiser_, |
| 936 StartAdvertisingToDevice(test_devices_[1])); |
| 937 EXPECT_CALL(*mock_ble_scanner_, |
| 938 UnregisterScanFilterForDevice(test_devices_[1])); |
| 939 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1])); |
| 940 |
| 941 FakeSecureChannel* channel0 = |
| 942 ConnectSuccessfully(test_devices_[0], std::string(kBluetoothAddress1), |
| 943 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 944 |
| 945 FakeSecureChannel* channel1 = |
| 946 ConnectSuccessfully(test_devices_[1], std::string(kBluetoothAddress2), |
| 947 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 948 |
| 949 manager_->SendMessage(test_devices_[0], "request1_device0"); |
| 950 VerifyLastMessageSent(channel0, "request1_device0", 1); |
| 951 |
| 952 manager_->SendMessage(test_devices_[1], "request1_device1"); |
| 953 VerifyLastMessageSent(channel1, "request1_device1", 1); |
| 954 |
| 955 channel0->ReceiveMessage(std::string(kTetherFeature), "response1_device0"); |
| 956 VerifyReceivedMessages( |
| 957 std::vector<ReceivedMessage>{{test_devices_[0], "response1_device0"}}); |
| 958 |
| 959 channel1->ReceiveMessage(std::string(kTetherFeature), "response1_device1"); |
| 960 VerifyReceivedMessages( |
| 961 std::vector<ReceivedMessage>{{test_devices_[1], "response1_device1"}}); |
| 962 |
| 963 manager_->SendMessage(test_devices_[0], "request2_device0"); |
| 964 VerifyLastMessageSent(channel0, "request2_device0", 2); |
| 965 |
| 966 manager_->SendMessage(test_devices_[1], "request2_device1"); |
| 967 VerifyLastMessageSent(channel1, "request2_device1", 2); |
| 968 |
| 969 channel0->ReceiveMessage(std::string(kTetherFeature), "response2_device0"); |
| 970 VerifyReceivedMessages( |
| 971 std::vector<ReceivedMessage>{{test_devices_[0], "response2_device0"}}); |
| 972 |
| 973 channel1->ReceiveMessage(std::string(kTetherFeature), "response2_device1"); |
| 974 VerifyReceivedMessages( |
| 975 std::vector<ReceivedMessage>{{test_devices_[1], "response2_device1"}}); |
| 976 |
| 977 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 978 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 979 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 980 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 981 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 982 VerifyDeviceNotRegistered(test_devices_[0]); |
| 983 |
| 984 manager_->UnregisterRemoteDevice(test_devices_[1], |
| 985 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 986 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 987 {test_devices_[1], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 988 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 989 VerifyDeviceNotRegistered(test_devices_[1]); |
| 990 } |
| 991 |
| 992 TEST_F(BleConnectionManagerTest, FourDevices_ComprehensiveTest) { |
| 993 EXPECT_CALL(*mock_ble_scanner_, |
| 994 RegisterScanFilterForDevice(test_devices_[0])); |
| 995 EXPECT_CALL(*mock_ble_advertiser_, |
| 996 StartAdvertisingToDevice(test_devices_[0])); |
| 997 EXPECT_CALL(*mock_ble_scanner_, |
| 998 UnregisterScanFilterForDevice(test_devices_[0])); |
| 999 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[0])); |
| 1000 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[1])) |
| 1001 .Times(2); |
| 1002 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[1])) |
| 1003 .Times(2); |
| 1004 EXPECT_CALL(*mock_ble_scanner_, |
| 1005 UnregisterScanFilterForDevice(test_devices_[1])) |
| 1006 .Times(2); |
| 1007 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[1])) |
| 1008 .Times(2); |
| 1009 EXPECT_CALL(*mock_ble_scanner_, RegisterScanFilterForDevice(test_devices_[2])) |
| 1010 .Times(2); |
| 1011 EXPECT_CALL(*mock_ble_advertiser_, StartAdvertisingToDevice(test_devices_[2])) |
| 1012 .Times(2); |
| 1013 EXPECT_CALL(*mock_ble_scanner_, |
| 1014 UnregisterScanFilterForDevice(test_devices_[2])) |
| 1015 .Times(2); |
| 1016 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[2])) |
| 1017 .Times(2); |
| 1018 EXPECT_CALL(*mock_ble_scanner_, |
| 1019 RegisterScanFilterForDevice(test_devices_[3])); |
| 1020 EXPECT_CALL(*mock_ble_advertiser_, |
| 1021 StartAdvertisingToDevice(test_devices_[3])); |
| 1022 EXPECT_CALL(*mock_ble_scanner_, |
| 1023 UnregisterScanFilterForDevice(test_devices_[3])); |
| 1024 EXPECT_CALL(*mock_ble_advertiser_, StopAdvertisingToDevice(test_devices_[3])); |
| 1025 |
| 1026 // Register all devices. Since the maximum number of simultaneous connection |
| 1027 // attempts is 2, only devices 0 and 1 should actually start connecting. |
| 1028 manager_->RegisterRemoteDevice(test_devices_[0], |
| 1029 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1030 manager_->RegisterRemoteDevice(test_devices_[1], |
| 1031 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1032 manager_->RegisterRemoteDevice(test_devices_[2], |
| 1033 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1034 manager_->RegisterRemoteDevice(test_devices_[3], |
| 1035 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1036 |
| 1037 // Devices 0 and 1 should be advertising; devices 2 and 3 should not be. |
| 1038 VerifyAdvertisingTimeoutSet(test_devices_[0]); |
| 1039 VerifyAdvertisingTimeoutSet(test_devices_[1]); |
| 1040 VerifyNoTimeoutSet(test_devices_[2]); |
| 1041 VerifyNoTimeoutSet(test_devices_[3]); |
| 1042 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1043 {test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 1044 cryptauth::SecureChannel::Status::CONNECTING}, |
| 1045 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 1046 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 1047 |
| 1048 // Device 0 connects successfully. |
| 1049 FakeSecureChannel* channel0 = |
| 1050 ConnectChannel(test_devices_[0], std::string(kBluetoothAddress1)); |
| 1051 |
| 1052 // Since device 0 has connected, advertising to that device is no longer |
| 1053 // necessary. Device 2 should have filled up that advertising slot. |
| 1054 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1055 {test_devices_[2], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 1056 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 1057 |
| 1058 // Meanwhile, device 1 fails to connect, so the timeout fires. The advertising |
| 1059 // slot left by device 1 creates space for device 3 to start connecting. |
| 1060 FireTimerForDevice(test_devices_[1]); |
| 1061 VerifyAdvertisingTimeoutSet(test_devices_[3]); |
| 1062 VerifyNoTimeoutSet(test_devices_[1]); |
| 1063 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1064 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
| 1065 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 1066 {test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 1067 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 1068 |
| 1069 // Now, device 0 authenticates and sends and receives a message. |
| 1070 AuthenticateChannel(test_devices_[0]); |
| 1071 manager_->SendMessage(test_devices_[0], "request1"); |
| 1072 VerifyLastMessageSent(channel0, "request1", 1); |
| 1073 |
| 1074 channel0->ReceiveMessage(std::string(kTetherFeature), "response1"); |
| 1075 VerifyReceivedMessages( |
| 1076 std::vector<ReceivedMessage>{{test_devices_[0], "response1"}}); |
| 1077 |
| 1078 // Now, device 0 is unregistered. |
| 1079 manager_->UnregisterRemoteDevice(test_devices_[0], |
| 1080 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1081 VerifyDeviceNotRegistered(test_devices_[0]); |
| 1082 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1083 {test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 1084 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 1085 |
| 1086 // Device 2 fails to connect, so the timeout fires. Device 1 takes its spot. |
| 1087 FireTimerForDevice(test_devices_[2]); |
| 1088 VerifyAdvertisingTimeoutSet(test_devices_[1]); |
| 1089 VerifyNoTimeoutSet(test_devices_[2]); |
| 1090 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1091 {test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING, |
| 1092 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 1093 {test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 1094 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 1095 |
| 1096 // Device 3 connects successfully. |
| 1097 FakeSecureChannel* channel3 = |
| 1098 ConnectChannel(test_devices_[3], std::string(kBluetoothAddress3)); |
| 1099 |
| 1100 // Since device 3 has connected, device 2 starts connecting again. |
| 1101 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1102 {test_devices_[2], cryptauth::SecureChannel::Status::DISCONNECTED, |
| 1103 cryptauth::SecureChannel::Status::CONNECTING}}); |
| 1104 |
| 1105 // Now, device 3 authenticates and sends and receives a message. |
| 1106 AuthenticateChannel(test_devices_[3]); |
| 1107 manager_->SendMessage(test_devices_[3], "request3"); |
| 1108 VerifyLastMessageSent(channel3, "request3", 1); |
| 1109 |
| 1110 channel3->ReceiveMessage(std::string(kTetherFeature), "response3"); |
| 1111 VerifyReceivedMessages( |
| 1112 std::vector<ReceivedMessage>{{test_devices_[3], "response3"}}); |
| 1113 |
| 1114 // Assume that none of the other devices can connect, and unregister the |
| 1115 // remaining 3 devices. |
| 1116 manager_->UnregisterRemoteDevice(test_devices_[3], |
| 1117 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1118 VerifyDeviceNotRegistered(test_devices_[3]); |
| 1119 manager_->UnregisterRemoteDevice(test_devices_[1], |
| 1120 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1121 VerifyDeviceNotRegistered(test_devices_[1]); |
| 1122 manager_->UnregisterRemoteDevice(test_devices_[2], |
| 1123 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 1124 VerifyDeviceNotRegistered(test_devices_[2]); |
| 1125 |
| 1126 VerifyConnectionStateChanges(std::vector<SecureChannelStatusChange>{ |
| 1127 {test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATED, |
| 1128 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 1129 {test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING, |
| 1130 cryptauth::SecureChannel::Status::DISCONNECTED}, |
| 1131 {test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING, |
| 1132 cryptauth::SecureChannel::Status::DISCONNECTED}}); |
| 1133 } |
| 1134 |
| 1135 } // namespace tether |
| 1136 |
| 1137 } // namespace cryptauth |
OLD | NEW |