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