Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/components/tether/message_transfer_operation.h" | 5 #include "chromeos/components/tether/message_transfer_operation.h" |
| 6 | 6 |
| 7 #include "base/timer/mock_timer.h" | |
| 7 #include "chromeos/components/tether/fake_ble_connection_manager.h" | 8 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
| 8 #include "chromeos/components/tether/message_wrapper.h" | 9 #include "chromeos/components/tether/message_wrapper.h" |
| 10 #include "chromeos/components/tether/timer_factory.h" | |
| 9 #include "components/cryptauth/remote_device_test_util.h" | 11 #include "components/cryptauth/remote_device_test_util.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 14 |
| 13 namespace chromeos { | 15 namespace chromeos { |
| 14 | 16 |
| 15 namespace tether { | 17 namespace tether { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 // Arbitrarily chosen value. The MessageType used in this test does not matter | 21 // Arbitrarily chosen value. The MessageType used in this test does not matter |
| 20 // except that it must be consistent throughout the test. | 22 // except that it must be consistent throughout the test. |
| 21 const MessageType kTestMessageType = MessageType::TETHER_AVAILABILITY_REQUEST; | 23 const MessageType kTestMessageType = MessageType::TETHER_AVAILABILITY_REQUEST; |
| 22 | 24 |
| 23 // A test double for MessageTransferOperation is needed because | 25 // A test double for MessageTransferOperation is needed because |
| 24 // MessageTransferOperation has pure virtual methods which must be overridden in | 26 // MessageTransferOperation has pure virtual methods which must be overridden in |
| 25 // order to create a concrete instantiation of the class. | 27 // order to create a concrete instantiation of the class. |
| 26 class TestOperation : public MessageTransferOperation { | 28 class TestOperation : public MessageTransferOperation { |
| 27 public: | 29 public: |
| 28 TestOperation(const std::vector<cryptauth::RemoteDevice>& devices_to_connect, | 30 TestOperation(const std::vector<cryptauth::RemoteDevice>& devices_to_connect, |
| 29 BleConnectionManager* connection_manager) | 31 BleConnectionManager* connection_manager) |
| 30 : MessageTransferOperation(devices_to_connect, connection_manager), | 32 : MessageTransferOperation(devices_to_connect, connection_manager) {} |
| 31 has_operation_started_(false), | |
| 32 has_operation_finished_(false) {} | |
| 33 ~TestOperation() override {} | 33 ~TestOperation() override {} |
| 34 | 34 |
| 35 bool HasDeviceAuthenticated(const cryptauth::RemoteDevice& remote_device) { | 35 bool HasDeviceAuthenticated(const cryptauth::RemoteDevice& remote_device) { |
| 36 const auto iter = device_map_.find(remote_device); | 36 const auto iter = device_map_.find(remote_device); |
| 37 if (iter == device_map_.end()) { | 37 if (iter == device_map_.end()) { |
| 38 return false; | 38 return false; |
| 39 } | 39 } |
| 40 | 40 |
| 41 return iter->second.has_device_authenticated; | 41 return iter->second.has_device_authenticated; |
| 42 } | 42 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 55 void OnDeviceAuthenticated( | 55 void OnDeviceAuthenticated( |
| 56 const cryptauth::RemoteDevice& remote_device) override { | 56 const cryptauth::RemoteDevice& remote_device) override { |
| 57 device_map_[remote_device].has_device_authenticated = true; | 57 device_map_[remote_device].has_device_authenticated = true; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void OnMessageReceived( | 60 void OnMessageReceived( |
| 61 std::unique_ptr<MessageWrapper> message_wrapper, | 61 std::unique_ptr<MessageWrapper> message_wrapper, |
| 62 const cryptauth::RemoteDevice& remote_device) override { | 62 const cryptauth::RemoteDevice& remote_device) override { |
| 63 device_map_[remote_device].received_messages.push_back( | 63 device_map_[remote_device].received_messages.push_back( |
| 64 std::move(message_wrapper)); | 64 std::move(message_wrapper)); |
| 65 | |
| 66 if (should_unregister_device_on_message_received_) | |
| 67 UnregisterDevice(remote_device); | |
| 65 } | 68 } |
| 66 | 69 |
| 67 void OnOperationStarted() override { has_operation_started_ = true; } | 70 void OnOperationStarted() override { has_operation_started_ = true; } |
| 68 | 71 |
| 69 void OnOperationFinished() override { has_operation_finished_ = true; } | 72 void OnOperationFinished() override { has_operation_finished_ = true; } |
| 70 | 73 |
| 71 MessageType GetMessageTypeForConnection() override { | 74 MessageType GetMessageTypeForConnection() override { |
| 72 return kTestMessageType; | 75 return kTestMessageType; |
| 73 } | 76 } |
| 74 | 77 |
| 78 bool ShouldWaitForResponse() override { return should_wait_for_response_; } | |
| 79 | |
| 80 uint32_t GetResponseTimeoutSeconds() override { | |
| 81 return response_timeout_seconds_; | |
| 82 } | |
| 83 | |
| 84 void set_should_wait_for_response(bool should_wait_for_response) { | |
| 85 should_wait_for_response_ = should_wait_for_response; | |
| 86 } | |
| 87 | |
| 88 void set_response_timeout_seconds(uint32_t response_timeout_seconds) { | |
| 89 response_timeout_seconds_ = response_timeout_seconds; | |
| 90 } | |
| 91 | |
| 92 void set_should_unregister_device_on_message_received( | |
| 93 bool should_unregister_device_on_message_received) { | |
| 94 should_unregister_device_on_message_received_ = | |
| 95 should_unregister_device_on_message_received; | |
| 96 } | |
| 97 | |
| 75 bool has_operation_started() { return has_operation_started_; } | 98 bool has_operation_started() { return has_operation_started_; } |
| 76 | 99 |
| 77 bool has_operation_finished() { return has_operation_finished_; } | 100 bool has_operation_finished() { return has_operation_finished_; } |
| 78 | 101 |
| 79 private: | 102 private: |
| 80 struct DeviceMapValue { | 103 struct DeviceMapValue { |
| 81 DeviceMapValue() {} | 104 DeviceMapValue() {} |
| 82 ~DeviceMapValue() {} | 105 ~DeviceMapValue() {} |
| 83 | 106 |
| 84 bool has_device_authenticated; | 107 bool has_device_authenticated; |
| 85 std::vector<std::shared_ptr<MessageWrapper>> received_messages; | 108 std::vector<std::shared_ptr<MessageWrapper>> received_messages; |
| 86 }; | 109 }; |
| 87 | 110 |
| 88 std::map<cryptauth::RemoteDevice, DeviceMapValue> device_map_; | 111 std::map<cryptauth::RemoteDevice, DeviceMapValue> device_map_; |
| 89 | 112 |
| 90 bool has_operation_started_; | 113 bool should_wait_for_response_ = true; |
| 91 bool has_operation_finished_; | 114 uint32_t response_timeout_seconds_ = 5; |
| 115 bool should_unregister_device_on_message_received_ = false; | |
| 116 bool has_operation_started_ = false; | |
| 117 bool has_operation_finished_ = false; | |
| 118 }; | |
| 119 | |
| 120 class TestTimerFactory : public TimerFactory { | |
| 121 public: | |
| 122 ~TestTimerFactory() override {} | |
| 123 | |
| 124 // TimerFactory: | |
| 125 std::unique_ptr<base::Timer> CreateOneShotTimer() override { | |
| 126 return base::MakeUnique<base::MockTimer>(false /* retain_user_task */, | |
| 127 false /* is_repeating */); | |
| 128 } | |
| 92 }; | 129 }; |
| 93 | 130 |
| 94 DeviceStatus CreateFakeDeviceStatus() { | 131 DeviceStatus CreateFakeDeviceStatus() { |
| 95 WifiStatus wifi_status; | 132 WifiStatus wifi_status; |
| 96 wifi_status.set_status_code( | 133 wifi_status.set_status_code( |
| 97 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); | 134 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); |
| 98 wifi_status.set_ssid("Google A"); | 135 wifi_status.set_ssid("Google A"); |
| 99 | 136 |
| 100 DeviceStatus device_status; | 137 DeviceStatus device_status; |
| 101 device_status.set_battery_percentage(75); | 138 device_status.set_battery_percentage(75); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 126 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); | 163 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); |
| 127 } | 164 } |
| 128 | 165 |
| 129 void SetUp() override { | 166 void SetUp() override { |
| 130 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 167 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
| 131 } | 168 } |
| 132 | 169 |
| 133 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { | 170 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { |
| 134 operation_ = base::WrapUnique( | 171 operation_ = base::WrapUnique( |
| 135 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); | 172 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); |
| 173 operation_->SetTimerFactoryForTest(base::MakeUnique<TestTimerFactory>()); | |
| 136 VerifyOperationStartedAndFinished(false /* has_started */, | 174 VerifyOperationStartedAndFinished(false /* has_started */, |
| 137 false /* has_finished */); | 175 false /* has_finished */); |
| 138 } | 176 } |
| 139 | 177 |
| 140 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { | 178 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { |
| 141 DCHECK(operation_); | 179 DCHECK(operation_); |
| 142 return std::find(operation_->remote_devices_.begin(), | 180 return std::find(operation_->remote_devices_.begin(), |
| 143 operation_->remote_devices_.end(), | 181 operation_->remote_devices_.end(), |
| 144 remote_device) != operation_->remote_devices_.end(); | 182 remote_device) != operation_->remote_devices_.end(); |
| 145 } | 183 } |
| 146 | 184 |
| 147 void InitializeOperation() { | 185 void InitializeOperation() { |
| 148 VerifyOperationStartedAndFinished(false /* has_started */, | 186 VerifyOperationStartedAndFinished(false /* has_started */, |
| 149 false /* has_finished */); | 187 false /* has_finished */); |
| 150 operation_->Initialize(); | 188 operation_->Initialize(); |
| 151 VerifyOperationStartedAndFinished(true /* has_started */, | 189 VerifyOperationStartedAndFinished(true /* has_started */, |
| 152 false /* has_finished */); | 190 false /* has_finished */); |
| 153 } | 191 } |
| 154 | 192 |
| 155 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { | 193 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { |
| 156 EXPECT_EQ(has_started, operation_->has_operation_started()); | 194 EXPECT_EQ(has_started, operation_->has_operation_started()); |
| 157 EXPECT_EQ(has_finished, operation_->has_operation_finished()); | 195 EXPECT_EQ(has_finished, operation_->has_operation_finished()); |
| 158 } | 196 } |
| 159 | 197 |
| 198 base::MockTimer* GetResponseTimerForDevice( | |
| 199 const cryptauth::RemoteDevice& remote_device) { | |
| 200 return static_cast<base::MockTimer*>( | |
| 201 operation_->GetResponseTimerForDevice(remote_device)); | |
| 202 } | |
| 203 | |
| 160 const std::vector<cryptauth::RemoteDevice> test_devices_; | 204 const std::vector<cryptauth::RemoteDevice> test_devices_; |
| 161 | 205 |
| 162 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 206 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
| 163 std::unique_ptr<TestOperation> operation_; | 207 std::unique_ptr<TestOperation> operation_; |
| 164 | 208 |
| 165 private: | 209 private: |
| 166 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); | 210 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); |
| 167 }; | 211 }; |
| 168 | 212 |
| 169 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { | 213 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 fake_ble_connection_manager_->SetDeviceStatus( | 283 fake_ble_connection_manager_->SetDeviceStatus( |
| 240 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 284 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); |
| 241 fake_ble_connection_manager_->SetDeviceStatus( | 285 fake_ble_connection_manager_->SetDeviceStatus( |
| 242 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 286 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); |
| 243 fake_ble_connection_manager_->SetDeviceStatus( | 287 fake_ble_connection_manager_->SetDeviceStatus( |
| 244 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | 288 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 245 fake_ble_connection_manager_->SetDeviceStatus( | 289 fake_ble_connection_manager_->SetDeviceStatus( |
| 246 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | 290 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 247 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 291 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 248 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 292 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 293 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 249 | 294 |
| 250 EXPECT_TRUE(operation_->GetReceivedMessages(test_devices_[0]).empty()); | 295 EXPECT_TRUE(operation_->GetReceivedMessages(test_devices_[0]).empty()); |
| 251 } | 296 } |
| 252 | 297 |
| 253 TEST_F(MessageTransferOperationTest, | 298 TEST_F(MessageTransferOperationTest, |
| 254 TestSuccessfulConnectionAndReceiveMessage) { | 299 TestSuccessfulConnectionAndReceiveMessage) { |
| 255 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | 300 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
| 256 InitializeOperation(); | 301 InitializeOperation(); |
| 257 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 302 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 258 | 303 |
| 304 // Simulate how subclasses behave after a successful response: unregister the | |
| 305 // device. | |
| 306 operation_->set_should_unregister_device_on_message_received(true); | |
| 307 | |
| 259 fake_ble_connection_manager_->SetDeviceStatus( | 308 fake_ble_connection_manager_->SetDeviceStatus( |
| 260 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 309 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); |
| 261 fake_ble_connection_manager_->SetDeviceStatus( | 310 fake_ble_connection_manager_->SetDeviceStatus( |
| 311 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
| 312 fake_ble_connection_manager_->SetDeviceStatus( | |
| 313 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
| 314 fake_ble_connection_manager_->SetDeviceStatus( | |
| 315 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
| 316 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 317 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | |
| 318 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 319 | |
| 320 fake_ble_connection_manager_->ReceiveMessage( | |
| 321 test_devices_[0], | |
| 322 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | |
| 323 | |
| 324 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | |
| 325 std::shared_ptr<MessageWrapper> message = | |
| 326 operation_->GetReceivedMessages(test_devices_[0])[0]; | |
| 327 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | |
| 328 message->GetMessageType()); | |
| 329 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | |
| 330 message->GetProto()->SerializeAsString()); | |
| 331 | |
| 332 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
| 333 } | |
| 334 | |
| 335 TEST_F(MessageTransferOperationTest, | |
| 336 TestSuccessfulConnectionAndReceiveMessage_ResponseTimeoutSeconds) { | |
| 337 uint32_t response_timeout_seconds = 90; | |
| 338 | |
| 339 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | |
| 340 InitializeOperation(); | |
| 341 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 342 | |
| 343 operation_->set_response_timeout_seconds(response_timeout_seconds); | |
| 344 | |
| 345 fake_ble_connection_manager_->SetDeviceStatus( | |
| 346 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
| 347 fake_ble_connection_manager_->SetDeviceStatus( | |
| 262 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 348 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); |
| 263 fake_ble_connection_manager_->SetDeviceStatus( | 349 fake_ble_connection_manager_->SetDeviceStatus( |
| 264 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | 350 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 265 fake_ble_connection_manager_->SetDeviceStatus( | 351 fake_ble_connection_manager_->SetDeviceStatus( |
| 266 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | 352 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 267 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 353 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 268 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 354 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 355 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
|
Kyle Horimoto
2017/05/31 21:55:33
You can combine the checks for having a response t
Ryan Hansberry
2017/06/01 00:31:58
Done.
| |
| 356 | |
| 357 EXPECT_EQ(base::TimeDelta::FromSeconds(response_timeout_seconds), | |
| 358 GetResponseTimerForDevice(test_devices_[0])->GetCurrentDelay()); | |
| 269 | 359 |
| 270 fake_ble_connection_manager_->ReceiveMessage( | 360 fake_ble_connection_manager_->ReceiveMessage( |
| 271 test_devices_[0], | 361 test_devices_[0], |
| 272 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 362 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
| 273 | 363 |
| 274 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 364 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
| 275 std::shared_ptr<MessageWrapper> message = | 365 std::shared_ptr<MessageWrapper> message = |
| 276 operation_->GetReceivedMessages(test_devices_[0])[0]; | 366 operation_->GetReceivedMessages(test_devices_[0])[0]; |
| 277 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 367 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
| 278 message->GetMessageType()); | 368 message->GetMessageType()); |
| 279 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 369 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
| 280 message->GetProto()->SerializeAsString()); | 370 message->GetProto()->SerializeAsString()); |
| 281 } | 371 } |
| 282 | 372 |
| 373 TEST_F(MessageTransferOperationTest, | |
| 374 TestSuccessfulConnectionAndReceiveMessage_ShouldNotWaitForResponse) { | |
| 375 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | |
| 376 InitializeOperation(); | |
| 377 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 378 | |
| 379 operation_->set_should_wait_for_response(false); | |
| 380 | |
| 381 fake_ble_connection_manager_->SetDeviceStatus( | |
| 382 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
| 383 fake_ble_connection_manager_->SetDeviceStatus( | |
| 384 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
| 385 fake_ble_connection_manager_->SetDeviceStatus( | |
| 386 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
| 387 fake_ble_connection_manager_->SetDeviceStatus( | |
| 388 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
| 389 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 390 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | |
| 391 | |
| 392 // A Timer should not have been created because the operation should not wait | |
| 393 // for a response. | |
| 394 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
| 395 | |
| 396 fake_ble_connection_manager_->ReceiveMessage( | |
| 397 test_devices_[0], | |
| 398 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | |
| 399 | |
| 400 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | |
| 401 std::shared_ptr<MessageWrapper> message = | |
| 402 operation_->GetReceivedMessages(test_devices_[0])[0]; | |
| 403 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | |
| 404 message->GetMessageType()); | |
| 405 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | |
| 406 message->GetProto()->SerializeAsString()); | |
| 407 } | |
| 408 | |
| 409 TEST_F(MessageTransferOperationTest, TestAuthenticatesButTimesOut) { | |
| 410 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | |
| 411 InitializeOperation(); | |
| 412 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 413 | |
| 414 fake_ble_connection_manager_->SetDeviceStatus( | |
| 415 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
| 416 fake_ble_connection_manager_->SetDeviceStatus( | |
| 417 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
| 418 fake_ble_connection_manager_->SetDeviceStatus( | |
| 419 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
| 420 fake_ble_connection_manager_->SetDeviceStatus( | |
| 421 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
| 422 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 423 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | |
| 424 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 425 | |
| 426 GetResponseTimerForDevice(test_devices_[0])->Fire(); | |
| 427 | |
| 428 EXPECT_FALSE(IsDeviceRegistered(test_devices_[0])); | |
| 429 EXPECT_TRUE(operation_->has_operation_finished()); | |
| 430 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
| 431 } | |
| 432 | |
| 283 TEST_F(MessageTransferOperationTest, TestRepeatedInputDevice) { | 433 TEST_F(MessageTransferOperationTest, TestRepeatedInputDevice) { |
| 284 // Construct with two copies of the same device. | 434 // Construct with two copies of the same device. |
| 285 ConstructOperation( | 435 ConstructOperation( |
| 286 std::vector<cryptauth::RemoteDevice>{test_devices_[0], test_devices_[0]}); | 436 std::vector<cryptauth::RemoteDevice>{test_devices_[0], test_devices_[0]}); |
| 287 InitializeOperation(); | 437 InitializeOperation(); |
| 288 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 438 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 289 | 439 |
| 290 fake_ble_connection_manager_->SetDeviceStatus( | 440 fake_ble_connection_manager_->SetDeviceStatus( |
| 291 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 441 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); |
| 292 fake_ble_connection_manager_->SetDeviceStatus( | 442 fake_ble_connection_manager_->SetDeviceStatus( |
| 293 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 443 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); |
| 294 fake_ble_connection_manager_->SetDeviceStatus( | 444 fake_ble_connection_manager_->SetDeviceStatus( |
| 295 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | 445 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 296 fake_ble_connection_manager_->SetDeviceStatus( | 446 fake_ble_connection_manager_->SetDeviceStatus( |
| 297 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | 447 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 298 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 448 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 299 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 449 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 450 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 300 | 451 |
| 301 fake_ble_connection_manager_->ReceiveMessage( | 452 fake_ble_connection_manager_->ReceiveMessage( |
| 302 test_devices_[0], | 453 test_devices_[0], |
| 303 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 454 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
| 304 | 455 |
| 305 // Should still have received only one message even though the device was | 456 // Should still have received only one message even though the device was |
| 306 // repeated twice in the constructor. | 457 // repeated twice in the constructor. |
| 307 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 458 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
| 308 std::shared_ptr<MessageWrapper> message = | 459 std::shared_ptr<MessageWrapper> message = |
| 309 operation_->GetReceivedMessages(test_devices_[0])[0]; | 460 operation_->GetReceivedMessages(test_devices_[0])[0]; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 510 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); |
| 360 fake_ble_connection_manager_->SetDeviceStatus( | 511 fake_ble_connection_manager_->SetDeviceStatus( |
| 361 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | 512 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 362 fake_ble_connection_manager_->SetDeviceStatus( | 513 fake_ble_connection_manager_->SetDeviceStatus( |
| 363 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | 514 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 364 | 515 |
| 365 // Now initialize; the authentication handler should have been invoked. | 516 // Now initialize; the authentication handler should have been invoked. |
| 366 InitializeOperation(); | 517 InitializeOperation(); |
| 367 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 518 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 368 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 519 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 520 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 369 | 521 |
| 370 // Receiving a message should work at this point. | 522 // Receiving a message should work at this point. |
| 371 fake_ble_connection_manager_->ReceiveMessage( | 523 fake_ble_connection_manager_->ReceiveMessage( |
| 372 test_devices_[0], | 524 test_devices_[0], |
| 373 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 525 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
| 374 | 526 |
| 375 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 527 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
| 376 std::shared_ptr<MessageWrapper> message = | 528 std::shared_ptr<MessageWrapper> message = |
| 377 operation_->GetReceivedMessages(test_devices_[0])[0]; | 529 operation_->GetReceivedMessages(test_devices_[0])[0]; |
| 378 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 530 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
| 379 message->GetMessageType()); | 531 message->GetMessageType()); |
| 380 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 532 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
| 381 message->GetProto()->SerializeAsString()); | 533 message->GetProto()->SerializeAsString()); |
| 382 } | 534 } |
| 383 | 535 |
| 536 TEST_F(MessageTransferOperationTest, | |
| 537 AlreadyAuthenticatedBeforeInitialization_TimesOutWaitingForResponse) { | |
| 538 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | |
| 539 | |
| 540 // Simulate the authentication of |test_devices_[0]|'s channel before | |
| 541 // initialization. | |
| 542 fake_ble_connection_manager_->RegisterRemoteDevice( | |
| 543 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); | |
| 544 fake_ble_connection_manager_->SetDeviceStatus( | |
| 545 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
| 546 fake_ble_connection_manager_->SetDeviceStatus( | |
| 547 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
| 548 fake_ble_connection_manager_->SetDeviceStatus( | |
| 549 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
| 550 fake_ble_connection_manager_->SetDeviceStatus( | |
| 551 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
| 552 | |
| 553 // Now initialize; the authentication handler should have been invoked. | |
| 554 InitializeOperation(); | |
| 555 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
| 556 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | |
| 557 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 558 | |
| 559 GetResponseTimerForDevice(test_devices_[0])->Fire(); | |
| 560 | |
| 561 EXPECT_FALSE(IsDeviceRegistered(test_devices_[0])); | |
| 562 EXPECT_TRUE(operation_->has_operation_finished()); | |
| 563 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
| 564 } | |
| 565 | |
| 384 TEST_F(MessageTransferOperationTest, MultipleDevices) { | 566 TEST_F(MessageTransferOperationTest, MultipleDevices) { |
| 385 ConstructOperation(test_devices_); | 567 ConstructOperation(test_devices_); |
| 386 InitializeOperation(); | 568 InitializeOperation(); |
| 387 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 569 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 388 EXPECT_TRUE(IsDeviceRegistered(test_devices_[1])); | 570 EXPECT_TRUE(IsDeviceRegistered(test_devices_[1])); |
| 389 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); | 571 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); |
| 390 EXPECT_TRUE(IsDeviceRegistered(test_devices_[3])); | 572 EXPECT_TRUE(IsDeviceRegistered(test_devices_[3])); |
| 391 | 573 |
| 392 // Authenticate |test_devices_[0]|'s channel. | 574 // Authenticate |test_devices_[0]|'s channel. |
| 393 fake_ble_connection_manager_->RegisterRemoteDevice( | 575 fake_ble_connection_manager_->RegisterRemoteDevice( |
| 394 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); | 576 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); |
| 395 fake_ble_connection_manager_->SetDeviceStatus( | 577 fake_ble_connection_manager_->SetDeviceStatus( |
| 396 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 578 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); |
| 397 fake_ble_connection_manager_->SetDeviceStatus( | 579 fake_ble_connection_manager_->SetDeviceStatus( |
| 398 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 580 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); |
| 399 fake_ble_connection_manager_->SetDeviceStatus( | 581 fake_ble_connection_manager_->SetDeviceStatus( |
| 400 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | 582 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 401 fake_ble_connection_manager_->SetDeviceStatus( | 583 fake_ble_connection_manager_->SetDeviceStatus( |
| 402 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | 584 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 403 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 585 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 404 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 586 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 587 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
| 405 | 588 |
| 406 // Fail 3 times to connect to |test_devices_[1]|. | 589 // Fail 3 times to connect to |test_devices_[1]|. |
| 407 fake_ble_connection_manager_->SetDeviceStatus( | 590 fake_ble_connection_manager_->SetDeviceStatus( |
| 408 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | 591 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); |
| 409 fake_ble_connection_manager_->SetDeviceStatus( | 592 fake_ble_connection_manager_->SetDeviceStatus( |
| 410 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); | 593 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); |
| 411 fake_ble_connection_manager_->SetDeviceStatus( | 594 fake_ble_connection_manager_->SetDeviceStatus( |
| 412 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | 595 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); |
| 413 fake_ble_connection_manager_->SetDeviceStatus( | 596 fake_ble_connection_manager_->SetDeviceStatus( |
| 414 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); | 597 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); |
| 415 fake_ble_connection_manager_->SetDeviceStatus( | 598 fake_ble_connection_manager_->SetDeviceStatus( |
| 416 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | 599 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); |
| 417 fake_ble_connection_manager_->SetDeviceStatus( | 600 fake_ble_connection_manager_->SetDeviceStatus( |
| 418 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); | 601 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); |
| 419 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[1])); | 602 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[1])); |
| 420 EXPECT_FALSE(IsDeviceRegistered(test_devices_[1])); | 603 EXPECT_FALSE(IsDeviceRegistered(test_devices_[1])); |
| 604 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[1])); | |
| 421 | 605 |
| 422 // Authenticate |test_devices_[2]|'s channel. | 606 // Authenticate |test_devices_[2]|'s channel. |
| 423 fake_ble_connection_manager_->RegisterRemoteDevice( | 607 fake_ble_connection_manager_->RegisterRemoteDevice( |
| 424 test_devices_[2], MessageType::CONNECT_TETHERING_REQUEST); | 608 test_devices_[2], MessageType::CONNECT_TETHERING_REQUEST); |
| 425 fake_ble_connection_manager_->SetDeviceStatus( | 609 fake_ble_connection_manager_->SetDeviceStatus( |
| 426 test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING); | 610 test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING); |
| 427 fake_ble_connection_manager_->SetDeviceStatus( | 611 fake_ble_connection_manager_->SetDeviceStatus( |
| 428 test_devices_[2], cryptauth::SecureChannel::Status::CONNECTED); | 612 test_devices_[2], cryptauth::SecureChannel::Status::CONNECTED); |
| 429 fake_ble_connection_manager_->SetDeviceStatus( | 613 fake_ble_connection_manager_->SetDeviceStatus( |
| 430 test_devices_[2], cryptauth::SecureChannel::Status::AUTHENTICATING); | 614 test_devices_[2], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 431 fake_ble_connection_manager_->SetDeviceStatus( | 615 fake_ble_connection_manager_->SetDeviceStatus( |
| 432 test_devices_[2], cryptauth::SecureChannel::Status::AUTHENTICATED); | 616 test_devices_[2], cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 433 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[2])); | 617 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[2])); |
| 434 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); | 618 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); |
| 619 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[2])); | |
| 435 | 620 |
| 436 // Fail to authenticate |test_devices_[3]|'s channel. | 621 // Fail to authenticate |test_devices_[3]|'s channel. |
| 437 fake_ble_connection_manager_->RegisterRemoteDevice( | 622 fake_ble_connection_manager_->RegisterRemoteDevice( |
| 438 test_devices_[3], MessageType::CONNECT_TETHERING_REQUEST); | 623 test_devices_[3], MessageType::CONNECT_TETHERING_REQUEST); |
| 439 fake_ble_connection_manager_->SetDeviceStatus( | 624 fake_ble_connection_manager_->SetDeviceStatus( |
| 440 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTING); | 625 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTING); |
| 441 fake_ble_connection_manager_->SetDeviceStatus( | 626 fake_ble_connection_manager_->SetDeviceStatus( |
| 442 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTED); | 627 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTED); |
| 443 fake_ble_connection_manager_->SetDeviceStatus( | 628 fake_ble_connection_manager_->SetDeviceStatus( |
| 444 test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATING); | 629 test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 445 fake_ble_connection_manager_->SetDeviceStatus( | 630 fake_ble_connection_manager_->SetDeviceStatus( |
| 446 test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED); | 631 test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED); |
| 447 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[3])); | 632 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[3])); |
| 448 EXPECT_FALSE(IsDeviceRegistered(test_devices_[3])); | 633 EXPECT_FALSE(IsDeviceRegistered(test_devices_[3])); |
| 634 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[3])); | |
| 449 } | 635 } |
| 450 | 636 |
| 451 } // namespace tether | 637 } // namespace tether |
| 452 | 638 |
| 453 } // namespace chromeos | 639 } // namespace chromeos |
| OLD | NEW |