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 |
| 25 const uint32_t kTestResponseTimeoutSeconds = 5; |
| 26 |
23 // A test double for MessageTransferOperation is needed because | 27 // A test double for MessageTransferOperation is needed because |
24 // MessageTransferOperation has pure virtual methods which must be overridden in | 28 // MessageTransferOperation has pure virtual methods which must be overridden in |
25 // order to create a concrete instantiation of the class. | 29 // order to create a concrete instantiation of the class. |
26 class TestOperation : public MessageTransferOperation { | 30 class TestOperation : public MessageTransferOperation { |
27 public: | 31 public: |
28 TestOperation(const std::vector<cryptauth::RemoteDevice>& devices_to_connect, | 32 TestOperation(const std::vector<cryptauth::RemoteDevice>& devices_to_connect, |
29 BleConnectionManager* connection_manager) | 33 BleConnectionManager* connection_manager) |
30 : MessageTransferOperation(devices_to_connect, connection_manager), | 34 : MessageTransferOperation(devices_to_connect, connection_manager) {} |
31 has_operation_started_(false), | |
32 has_operation_finished_(false) {} | |
33 ~TestOperation() override {} | 35 ~TestOperation() override {} |
34 | 36 |
35 bool HasDeviceAuthenticated(const cryptauth::RemoteDevice& remote_device) { | 37 bool HasDeviceAuthenticated(const cryptauth::RemoteDevice& remote_device) { |
36 const auto iter = device_map_.find(remote_device); | 38 const auto iter = device_map_.find(remote_device); |
37 if (iter == device_map_.end()) { | 39 if (iter == device_map_.end()) { |
38 return false; | 40 return false; |
39 } | 41 } |
40 | 42 |
41 return iter->second.has_device_authenticated; | 43 return iter->second.has_device_authenticated; |
42 } | 44 } |
(...skipping 12 matching lines...) Expand all Loading... |
55 void OnDeviceAuthenticated( | 57 void OnDeviceAuthenticated( |
56 const cryptauth::RemoteDevice& remote_device) override { | 58 const cryptauth::RemoteDevice& remote_device) override { |
57 device_map_[remote_device].has_device_authenticated = true; | 59 device_map_[remote_device].has_device_authenticated = true; |
58 } | 60 } |
59 | 61 |
60 void OnMessageReceived( | 62 void OnMessageReceived( |
61 std::unique_ptr<MessageWrapper> message_wrapper, | 63 std::unique_ptr<MessageWrapper> message_wrapper, |
62 const cryptauth::RemoteDevice& remote_device) override { | 64 const cryptauth::RemoteDevice& remote_device) override { |
63 device_map_[remote_device].received_messages.push_back( | 65 device_map_[remote_device].received_messages.push_back( |
64 std::move(message_wrapper)); | 66 std::move(message_wrapper)); |
| 67 |
| 68 if (should_unregister_device_on_message_received_) |
| 69 UnregisterDevice(remote_device); |
65 } | 70 } |
66 | 71 |
67 void OnOperationStarted() override { has_operation_started_ = true; } | 72 void OnOperationStarted() override { has_operation_started_ = true; } |
68 | 73 |
69 void OnOperationFinished() override { has_operation_finished_ = true; } | 74 void OnOperationFinished() override { has_operation_finished_ = true; } |
70 | 75 |
71 MessageType GetMessageTypeForConnection() override { | 76 MessageType GetMessageTypeForConnection() override { |
72 return kTestMessageType; | 77 return kTestMessageType; |
73 } | 78 } |
74 | 79 |
| 80 bool ShouldWaitForResponse() override { return should_wait_for_response_; } |
| 81 |
| 82 uint32_t GetResponseTimeoutSeconds() override { |
| 83 return response_timeout_seconds_; |
| 84 } |
| 85 |
| 86 void set_should_wait_for_response(bool should_wait_for_response) { |
| 87 should_wait_for_response_ = should_wait_for_response; |
| 88 } |
| 89 |
| 90 void set_response_timeout_seconds(uint32_t response_timeout_seconds) { |
| 91 response_timeout_seconds_ = response_timeout_seconds; |
| 92 } |
| 93 |
| 94 void set_should_unregister_device_on_message_received( |
| 95 bool should_unregister_device_on_message_received) { |
| 96 should_unregister_device_on_message_received_ = |
| 97 should_unregister_device_on_message_received; |
| 98 } |
| 99 |
75 bool has_operation_started() { return has_operation_started_; } | 100 bool has_operation_started() { return has_operation_started_; } |
76 | 101 |
77 bool has_operation_finished() { return has_operation_finished_; } | 102 bool has_operation_finished() { return has_operation_finished_; } |
78 | 103 |
79 private: | 104 private: |
80 struct DeviceMapValue { | 105 struct DeviceMapValue { |
81 DeviceMapValue() {} | 106 DeviceMapValue() {} |
82 ~DeviceMapValue() {} | 107 ~DeviceMapValue() {} |
83 | 108 |
84 bool has_device_authenticated; | 109 bool has_device_authenticated; |
85 std::vector<std::shared_ptr<MessageWrapper>> received_messages; | 110 std::vector<std::shared_ptr<MessageWrapper>> received_messages; |
86 }; | 111 }; |
87 | 112 |
88 std::map<cryptauth::RemoteDevice, DeviceMapValue> device_map_; | 113 std::map<cryptauth::RemoteDevice, DeviceMapValue> device_map_; |
89 | 114 |
90 bool has_operation_started_; | 115 bool should_wait_for_response_ = true; |
91 bool has_operation_finished_; | 116 uint32_t response_timeout_seconds_ = kTestResponseTimeoutSeconds; |
| 117 bool should_unregister_device_on_message_received_ = false; |
| 118 bool has_operation_started_ = false; |
| 119 bool has_operation_finished_ = false; |
| 120 }; |
| 121 |
| 122 class TestTimerFactory : public TimerFactory { |
| 123 public: |
| 124 ~TestTimerFactory() override {} |
| 125 |
| 126 // TimerFactory: |
| 127 std::unique_ptr<base::Timer> CreateOneShotTimer() override { |
| 128 EXPECT_FALSE(device_id_for_next_timer_.empty()); |
| 129 base::MockTimer* mock_timer = new base::MockTimer( |
| 130 false /* retain_user_task */, false /* is_repeating */); |
| 131 device_id_to_timer_map_[device_id_for_next_timer_] = mock_timer; |
| 132 return base::WrapUnique(mock_timer); |
| 133 } |
| 134 |
| 135 base::MockTimer* GetResponseTimerForDeviceId(const std::string& device_id) { |
| 136 return device_id_to_timer_map_[device_id_for_next_timer_]; |
| 137 } |
| 138 |
| 139 void set_device_id_for_next_timer( |
| 140 const std::string& device_id_for_next_timer) { |
| 141 device_id_for_next_timer_ = device_id_for_next_timer; |
| 142 } |
| 143 |
| 144 private: |
| 145 std::string device_id_for_next_timer_; |
| 146 std::unordered_map<std::string, base::MockTimer*> device_id_to_timer_map_; |
92 }; | 147 }; |
93 | 148 |
94 DeviceStatus CreateFakeDeviceStatus() { | 149 DeviceStatus CreateFakeDeviceStatus() { |
95 WifiStatus wifi_status; | 150 WifiStatus wifi_status; |
96 wifi_status.set_status_code( | 151 wifi_status.set_status_code( |
97 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); | 152 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); |
98 wifi_status.set_ssid("Google A"); | 153 wifi_status.set_ssid("Google A"); |
99 | 154 |
100 DeviceStatus device_status; | 155 DeviceStatus device_status; |
101 device_status.set_battery_percentage(75); | 156 device_status.set_battery_percentage(75); |
(...skipping 22 matching lines...) Expand all Loading... |
124 // These tests are written under the assumption that there are a maximum of | 179 // These tests are written under the assumption that there are a maximum of |
125 // 3 connection attempts; they need to be edited if this value changes. | 180 // 3 connection attempts; they need to be edited if this value changes. |
126 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); | 181 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); |
127 } | 182 } |
128 | 183 |
129 void SetUp() override { | 184 void SetUp() override { |
130 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 185 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
131 } | 186 } |
132 | 187 |
133 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { | 188 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { |
| 189 test_timer_factory_ = new TestTimerFactory(); |
134 operation_ = base::WrapUnique( | 190 operation_ = base::WrapUnique( |
135 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); | 191 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); |
| 192 operation_->SetTimerFactoryForTest(base::WrapUnique(test_timer_factory_)); |
136 VerifyOperationStartedAndFinished(false /* has_started */, | 193 VerifyOperationStartedAndFinished(false /* has_started */, |
137 false /* has_finished */); | 194 false /* has_finished */); |
138 } | 195 } |
139 | 196 |
140 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { | 197 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { |
141 DCHECK(operation_); | 198 DCHECK(operation_); |
142 return std::find(operation_->remote_devices_.begin(), | 199 return std::find(operation_->remote_devices_.begin(), |
143 operation_->remote_devices_.end(), | 200 operation_->remote_devices_.end(), |
144 remote_device) != operation_->remote_devices_.end(); | 201 remote_device) != operation_->remote_devices_.end(); |
145 } | 202 } |
146 | 203 |
147 void InitializeOperation() { | 204 void InitializeOperation() { |
148 VerifyOperationStartedAndFinished(false /* has_started */, | 205 VerifyOperationStartedAndFinished(false /* has_started */, |
149 false /* has_finished */); | 206 false /* has_finished */); |
150 operation_->Initialize(); | 207 operation_->Initialize(); |
151 VerifyOperationStartedAndFinished(true /* has_started */, | 208 VerifyOperationStartedAndFinished(true /* has_started */, |
152 false /* has_finished */); | 209 false /* has_finished */); |
153 } | 210 } |
154 | 211 |
155 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { | 212 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { |
156 EXPECT_EQ(has_started, operation_->has_operation_started()); | 213 EXPECT_EQ(has_started, operation_->has_operation_started()); |
157 EXPECT_EQ(has_finished, operation_->has_operation_finished()); | 214 EXPECT_EQ(has_finished, operation_->has_operation_finished()); |
158 } | 215 } |
159 | 216 |
| 217 void TransitionDeviceStatusFromDisconnectedToAuthenticated( |
| 218 const cryptauth::RemoteDevice& remote_device) { |
| 219 test_timer_factory_->set_device_id_for_next_timer( |
| 220 remote_device.GetDeviceId()); |
| 221 |
| 222 fake_ble_connection_manager_->SetDeviceStatus( |
| 223 remote_device, cryptauth::SecureChannel::Status::CONNECTING); |
| 224 fake_ble_connection_manager_->SetDeviceStatus( |
| 225 remote_device, cryptauth::SecureChannel::Status::CONNECTED); |
| 226 fake_ble_connection_manager_->SetDeviceStatus( |
| 227 remote_device, cryptauth::SecureChannel::Status::AUTHENTICATING); |
| 228 fake_ble_connection_manager_->SetDeviceStatus( |
| 229 remote_device, cryptauth::SecureChannel::Status::AUTHENTICATED); |
| 230 } |
| 231 |
| 232 base::MockTimer* GetResponseTimerForDevice( |
| 233 const cryptauth::RemoteDevice& remote_device) { |
| 234 return test_timer_factory_->GetResponseTimerForDeviceId( |
| 235 remote_device.GetDeviceId()); |
| 236 } |
| 237 |
| 238 void VerifyDefaultTimerCreatedForDevice( |
| 239 const cryptauth::RemoteDevice& remote_device) { |
| 240 VerifyTimerCreatedForDevice(remote_device, kTestResponseTimeoutSeconds); |
| 241 } |
| 242 |
| 243 void VerifyTimerCreatedForDevice(const cryptauth::RemoteDevice& remote_device, |
| 244 uint32_t response_timeout_seconds) { |
| 245 EXPECT_TRUE(GetResponseTimerForDevice(remote_device)); |
| 246 EXPECT_EQ(base::TimeDelta::FromSeconds(response_timeout_seconds), |
| 247 GetResponseTimerForDevice(remote_device)->GetCurrentDelay()); |
| 248 } |
| 249 |
160 const std::vector<cryptauth::RemoteDevice> test_devices_; | 250 const std::vector<cryptauth::RemoteDevice> test_devices_; |
161 | 251 |
162 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 252 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
| 253 TestTimerFactory* test_timer_factory_; |
163 std::unique_ptr<TestOperation> operation_; | 254 std::unique_ptr<TestOperation> operation_; |
164 | 255 |
165 private: | 256 private: |
166 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); | 257 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); |
167 }; | 258 }; |
168 | 259 |
169 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { | 260 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { |
170 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | 261 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
171 InitializeOperation(); | 262 InitializeOperation(); |
172 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 263 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 320 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
230 | 321 |
231 // Try to connect and fail. The device should still be registered. | 322 // Try to connect and fail. The device should still be registered. |
232 fake_ble_connection_manager_->SetDeviceStatus( | 323 fake_ble_connection_manager_->SetDeviceStatus( |
233 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 324 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); |
234 fake_ble_connection_manager_->SetDeviceStatus( | 325 fake_ble_connection_manager_->SetDeviceStatus( |
235 test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED); | 326 test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED); |
236 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 327 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
237 | 328 |
238 // Try again and succeed. | 329 // Try again and succeed. |
239 fake_ble_connection_manager_->SetDeviceStatus( | 330 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
240 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
241 fake_ble_connection_manager_->SetDeviceStatus( | |
242 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
243 fake_ble_connection_manager_->SetDeviceStatus( | |
244 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
245 fake_ble_connection_manager_->SetDeviceStatus( | |
246 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
247 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 331 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
248 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 332 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 333 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
249 | 334 |
250 EXPECT_TRUE(operation_->GetReceivedMessages(test_devices_[0]).empty()); | 335 EXPECT_TRUE(operation_->GetReceivedMessages(test_devices_[0]).empty()); |
251 } | 336 } |
252 | 337 |
253 TEST_F(MessageTransferOperationTest, | 338 TEST_F(MessageTransferOperationTest, |
254 TestSuccessfulConnectionAndReceiveMessage) { | 339 TestSuccessfulConnectionAndReceiveMessage) { |
255 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | 340 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
256 InitializeOperation(); | 341 InitializeOperation(); |
257 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 342 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
258 | 343 |
259 fake_ble_connection_manager_->SetDeviceStatus( | 344 // Simulate how subclasses behave after a successful response: unregister the |
260 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 345 // device. |
261 fake_ble_connection_manager_->SetDeviceStatus( | 346 operation_->set_should_unregister_device_on_message_received(true); |
262 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 347 |
263 fake_ble_connection_manager_->SetDeviceStatus( | 348 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
264 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
265 fake_ble_connection_manager_->SetDeviceStatus( | |
266 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
267 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 349 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
268 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 350 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 351 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
269 | 352 |
270 fake_ble_connection_manager_->ReceiveMessage( | 353 fake_ble_connection_manager_->ReceiveMessage( |
271 test_devices_[0], | 354 test_devices_[0], |
272 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 355 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
| 356 |
| 357 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
| 358 std::shared_ptr<MessageWrapper> message = |
| 359 operation_->GetReceivedMessages(test_devices_[0])[0]; |
| 360 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
| 361 message->GetMessageType()); |
| 362 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
| 363 message->GetProto()->SerializeAsString()); |
| 364 } |
| 365 |
| 366 TEST_F(MessageTransferOperationTest, |
| 367 TestSuccessfulConnectionAndReceiveMessage_ResponseTimeoutSeconds) { |
| 368 const uint32_t response_timeout_seconds = 90; |
| 369 |
| 370 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
| 371 InitializeOperation(); |
| 372 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 373 |
| 374 operation_->set_response_timeout_seconds(response_timeout_seconds); |
| 375 |
| 376 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
| 377 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 378 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 379 VerifyTimerCreatedForDevice(test_devices_[0], response_timeout_seconds); |
| 380 |
| 381 EXPECT_EQ(base::TimeDelta::FromSeconds(response_timeout_seconds), |
| 382 GetResponseTimerForDevice(test_devices_[0])->GetCurrentDelay()); |
| 383 |
| 384 fake_ble_connection_manager_->ReceiveMessage( |
| 385 test_devices_[0], |
| 386 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
| 387 |
| 388 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
| 389 std::shared_ptr<MessageWrapper> message = |
| 390 operation_->GetReceivedMessages(test_devices_[0])[0]; |
| 391 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
| 392 message->GetMessageType()); |
| 393 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
| 394 message->GetProto()->SerializeAsString()); |
| 395 } |
| 396 |
| 397 TEST_F(MessageTransferOperationTest, |
| 398 TestSuccessfulConnectionAndReceiveMessage_ShouldNotWaitForResponse) { |
| 399 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
| 400 InitializeOperation(); |
| 401 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 402 |
| 403 operation_->set_should_wait_for_response(false); |
| 404 |
| 405 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
| 406 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 407 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 408 |
| 409 // A Timer should not have been created because the operation should not wait |
| 410 // for a response. |
| 411 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); |
| 412 |
| 413 fake_ble_connection_manager_->ReceiveMessage( |
| 414 test_devices_[0], |
| 415 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
273 | 416 |
274 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 417 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
275 std::shared_ptr<MessageWrapper> message = | 418 std::shared_ptr<MessageWrapper> message = |
276 operation_->GetReceivedMessages(test_devices_[0])[0]; | 419 operation_->GetReceivedMessages(test_devices_[0])[0]; |
277 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 420 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
278 message->GetMessageType()); | 421 message->GetMessageType()); |
279 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 422 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
280 message->GetProto()->SerializeAsString()); | 423 message->GetProto()->SerializeAsString()); |
281 } | 424 } |
282 | 425 |
| 426 TEST_F(MessageTransferOperationTest, TestAuthenticatesButTimesOut) { |
| 427 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
| 428 InitializeOperation(); |
| 429 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 430 |
| 431 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
| 432 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 433 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 434 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
| 435 |
| 436 GetResponseTimerForDevice(test_devices_[0])->Fire(); |
| 437 |
| 438 EXPECT_FALSE(IsDeviceRegistered(test_devices_[0])); |
| 439 EXPECT_TRUE(operation_->has_operation_finished()); |
| 440 } |
| 441 |
283 TEST_F(MessageTransferOperationTest, TestRepeatedInputDevice) { | 442 TEST_F(MessageTransferOperationTest, TestRepeatedInputDevice) { |
284 // Construct with two copies of the same device. | 443 // Construct with two copies of the same device. |
285 ConstructOperation( | 444 ConstructOperation( |
286 std::vector<cryptauth::RemoteDevice>{test_devices_[0], test_devices_[0]}); | 445 std::vector<cryptauth::RemoteDevice>{test_devices_[0], test_devices_[0]}); |
287 InitializeOperation(); | 446 InitializeOperation(); |
288 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 447 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
289 | 448 |
290 fake_ble_connection_manager_->SetDeviceStatus( | 449 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
291 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
292 fake_ble_connection_manager_->SetDeviceStatus( | |
293 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
294 fake_ble_connection_manager_->SetDeviceStatus( | |
295 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
296 fake_ble_connection_manager_->SetDeviceStatus( | |
297 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
298 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 450 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
299 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 451 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 452 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
300 | 453 |
301 fake_ble_connection_manager_->ReceiveMessage( | 454 fake_ble_connection_manager_->ReceiveMessage( |
302 test_devices_[0], | 455 test_devices_[0], |
303 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 456 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
304 | 457 |
305 // Should still have received only one message even though the device was | 458 // Should still have received only one message even though the device was |
306 // repeated twice in the constructor. | 459 // repeated twice in the constructor. |
307 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 460 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
308 std::shared_ptr<MessageWrapper> message = | 461 std::shared_ptr<MessageWrapper> message = |
309 operation_->GetReceivedMessages(test_devices_[0])[0]; | 462 operation_->GetReceivedMessages(test_devices_[0])[0]; |
310 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 463 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
311 message->GetMessageType()); | 464 message->GetMessageType()); |
312 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 465 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
313 message->GetProto()->SerializeAsString()); | 466 message->GetProto()->SerializeAsString()); |
314 } | 467 } |
315 | 468 |
316 TEST_F(MessageTransferOperationTest, TestReceiveEventForOtherDevice) { | 469 TEST_F(MessageTransferOperationTest, TestReceiveEventForOtherDevice) { |
317 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | 470 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
318 InitializeOperation(); | 471 InitializeOperation(); |
319 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 472 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
320 | 473 |
321 // Simulate the authentication of |test_devices_[1]|'s channel. Since the | 474 // Simulate the authentication of |test_devices_[1]|'s channel. Since the |
322 // operation was only constructed with |test_devices_[0]|, this operation | 475 // operation was only constructed with |test_devices_[0]|, this operation |
323 // should not be affected. | 476 // should not be affected. |
324 fake_ble_connection_manager_->RegisterRemoteDevice( | 477 fake_ble_connection_manager_->RegisterRemoteDevice( |
325 test_devices_[1], MessageType::CONNECT_TETHERING_REQUEST); | 478 test_devices_[1], MessageType::CONNECT_TETHERING_REQUEST); |
326 fake_ble_connection_manager_->SetDeviceStatus( | 479 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[1]); |
327 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | |
328 fake_ble_connection_manager_->SetDeviceStatus( | |
329 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTED); | |
330 fake_ble_connection_manager_->SetDeviceStatus( | |
331 test_devices_[1], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
332 fake_ble_connection_manager_->SetDeviceStatus( | |
333 test_devices_[1], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
334 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 480 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
335 EXPECT_FALSE(IsDeviceRegistered(test_devices_[1])); | 481 EXPECT_FALSE(IsDeviceRegistered(test_devices_[1])); |
336 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 482 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
337 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[1])); | 483 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[1])); |
338 | 484 |
339 // Now, receive a message for |test_devices[1]|. Likewise, this operation | 485 // Now, receive a message for |test_devices[1]|. Likewise, this operation |
340 // should not be affected. | 486 // should not be affected. |
341 fake_ble_connection_manager_->ReceiveMessage( | 487 fake_ble_connection_manager_->ReceiveMessage( |
342 test_devices_[1], | 488 test_devices_[1], |
343 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 489 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
344 | 490 |
345 EXPECT_FALSE(operation_->GetReceivedMessages(test_devices_[0]).size()); | 491 EXPECT_FALSE(operation_->GetReceivedMessages(test_devices_[0]).size()); |
346 } | 492 } |
347 | 493 |
348 TEST_F(MessageTransferOperationTest, | 494 TEST_F(MessageTransferOperationTest, |
349 TestAlreadyAuthenticatedBeforeInitialization) { | 495 TestAlreadyAuthenticatedBeforeInitialization) { |
350 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | 496 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
351 | 497 |
352 // Simulate the authentication of |test_devices_[0]|'s channel before | 498 // Simulate the authentication of |test_devices_[0]|'s channel before |
353 // initialization. | 499 // initialization. |
354 fake_ble_connection_manager_->RegisterRemoteDevice( | 500 fake_ble_connection_manager_->RegisterRemoteDevice( |
355 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); | 501 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); |
356 fake_ble_connection_manager_->SetDeviceStatus( | 502 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
357 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
358 fake_ble_connection_manager_->SetDeviceStatus( | |
359 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
360 fake_ble_connection_manager_->SetDeviceStatus( | |
361 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
362 fake_ble_connection_manager_->SetDeviceStatus( | |
363 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
364 | 503 |
365 // Now initialize; the authentication handler should have been invoked. | 504 // Now initialize; the authentication handler should have been invoked. |
366 InitializeOperation(); | 505 InitializeOperation(); |
367 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 506 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
368 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 507 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 508 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
369 | 509 |
370 // Receiving a message should work at this point. | 510 // Receiving a message should work at this point. |
371 fake_ble_connection_manager_->ReceiveMessage( | 511 fake_ble_connection_manager_->ReceiveMessage( |
372 test_devices_[0], | 512 test_devices_[0], |
373 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 513 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
374 | 514 |
375 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 515 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
376 std::shared_ptr<MessageWrapper> message = | 516 std::shared_ptr<MessageWrapper> message = |
377 operation_->GetReceivedMessages(test_devices_[0])[0]; | 517 operation_->GetReceivedMessages(test_devices_[0])[0]; |
378 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 518 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
379 message->GetMessageType()); | 519 message->GetMessageType()); |
380 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 520 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
381 message->GetProto()->SerializeAsString()); | 521 message->GetProto()->SerializeAsString()); |
382 } | 522 } |
383 | 523 |
| 524 TEST_F(MessageTransferOperationTest, |
| 525 AlreadyAuthenticatedBeforeInitialization_TimesOutWaitingForResponse) { |
| 526 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); |
| 527 |
| 528 // Simulate the authentication of |test_devices_[0]|'s channel before |
| 529 // initialization. |
| 530 fake_ble_connection_manager_->RegisterRemoteDevice( |
| 531 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); |
| 532 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
| 533 |
| 534 // Now initialize; the authentication handler should have been invoked. |
| 535 InitializeOperation(); |
| 536 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 537 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
| 538 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
| 539 |
| 540 GetResponseTimerForDevice(test_devices_[0])->Fire(); |
| 541 |
| 542 EXPECT_FALSE(IsDeviceRegistered(test_devices_[0])); |
| 543 EXPECT_TRUE(operation_->has_operation_finished()); |
| 544 } |
| 545 |
384 TEST_F(MessageTransferOperationTest, MultipleDevices) { | 546 TEST_F(MessageTransferOperationTest, MultipleDevices) { |
385 ConstructOperation(test_devices_); | 547 ConstructOperation(test_devices_); |
386 InitializeOperation(); | 548 InitializeOperation(); |
387 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 549 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
388 EXPECT_TRUE(IsDeviceRegistered(test_devices_[1])); | 550 EXPECT_TRUE(IsDeviceRegistered(test_devices_[1])); |
389 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); | 551 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); |
390 EXPECT_TRUE(IsDeviceRegistered(test_devices_[3])); | 552 EXPECT_TRUE(IsDeviceRegistered(test_devices_[3])); |
391 | 553 |
392 // Authenticate |test_devices_[0]|'s channel. | 554 // Authenticate |test_devices_[0]|'s channel. |
393 fake_ble_connection_manager_->RegisterRemoteDevice( | 555 fake_ble_connection_manager_->RegisterRemoteDevice( |
394 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); | 556 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); |
395 fake_ble_connection_manager_->SetDeviceStatus( | 557 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]); |
396 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
397 fake_ble_connection_manager_->SetDeviceStatus( | |
398 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
399 fake_ble_connection_manager_->SetDeviceStatus( | |
400 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
401 fake_ble_connection_manager_->SetDeviceStatus( | |
402 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
403 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 558 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
404 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 559 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
| 560 VerifyDefaultTimerCreatedForDevice(test_devices_[0]); |
405 | 561 |
406 // Fail 3 times to connect to |test_devices_[1]|. | 562 // Fail 3 times to connect to |test_devices_[1]|. |
| 563 test_timer_factory_->set_device_id_for_next_timer( |
| 564 test_devices_[1].GetDeviceId()); |
407 fake_ble_connection_manager_->SetDeviceStatus( | 565 fake_ble_connection_manager_->SetDeviceStatus( |
408 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | 566 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); |
409 fake_ble_connection_manager_->SetDeviceStatus( | 567 fake_ble_connection_manager_->SetDeviceStatus( |
410 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); | 568 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); |
411 fake_ble_connection_manager_->SetDeviceStatus( | 569 fake_ble_connection_manager_->SetDeviceStatus( |
412 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | 570 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); |
413 fake_ble_connection_manager_->SetDeviceStatus( | 571 fake_ble_connection_manager_->SetDeviceStatus( |
414 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); | 572 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); |
415 fake_ble_connection_manager_->SetDeviceStatus( | 573 fake_ble_connection_manager_->SetDeviceStatus( |
416 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); | 574 test_devices_[1], cryptauth::SecureChannel::Status::CONNECTING); |
417 fake_ble_connection_manager_->SetDeviceStatus( | 575 fake_ble_connection_manager_->SetDeviceStatus( |
418 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); | 576 test_devices_[1], cryptauth::SecureChannel::Status::DISCONNECTED); |
419 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[1])); | 577 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[1])); |
420 EXPECT_FALSE(IsDeviceRegistered(test_devices_[1])); | 578 EXPECT_FALSE(IsDeviceRegistered(test_devices_[1])); |
| 579 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[1])); |
421 | 580 |
422 // Authenticate |test_devices_[2]|'s channel. | 581 // Authenticate |test_devices_[2]|'s channel. |
423 fake_ble_connection_manager_->RegisterRemoteDevice( | 582 fake_ble_connection_manager_->RegisterRemoteDevice( |
424 test_devices_[2], MessageType::CONNECT_TETHERING_REQUEST); | 583 test_devices_[2], MessageType::CONNECT_TETHERING_REQUEST); |
425 fake_ble_connection_manager_->SetDeviceStatus( | 584 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[2]); |
426 test_devices_[2], cryptauth::SecureChannel::Status::CONNECTING); | |
427 fake_ble_connection_manager_->SetDeviceStatus( | |
428 test_devices_[2], cryptauth::SecureChannel::Status::CONNECTED); | |
429 fake_ble_connection_manager_->SetDeviceStatus( | |
430 test_devices_[2], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
431 fake_ble_connection_manager_->SetDeviceStatus( | |
432 test_devices_[2], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
433 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[2])); | 585 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[2])); |
434 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); | 586 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); |
| 587 VerifyDefaultTimerCreatedForDevice(test_devices_[2]); |
435 | 588 |
436 // Fail to authenticate |test_devices_[3]|'s channel. | 589 // Fail to authenticate |test_devices_[3]|'s channel. |
| 590 test_timer_factory_->set_device_id_for_next_timer( |
| 591 test_devices_[3].GetDeviceId()); |
437 fake_ble_connection_manager_->RegisterRemoteDevice( | 592 fake_ble_connection_manager_->RegisterRemoteDevice( |
438 test_devices_[3], MessageType::CONNECT_TETHERING_REQUEST); | 593 test_devices_[3], MessageType::CONNECT_TETHERING_REQUEST); |
439 fake_ble_connection_manager_->SetDeviceStatus( | 594 fake_ble_connection_manager_->SetDeviceStatus( |
440 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTING); | 595 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTING); |
441 fake_ble_connection_manager_->SetDeviceStatus( | 596 fake_ble_connection_manager_->SetDeviceStatus( |
442 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTED); | 597 test_devices_[3], cryptauth::SecureChannel::Status::CONNECTED); |
443 fake_ble_connection_manager_->SetDeviceStatus( | 598 fake_ble_connection_manager_->SetDeviceStatus( |
444 test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATING); | 599 test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATING); |
445 fake_ble_connection_manager_->SetDeviceStatus( | 600 fake_ble_connection_manager_->SetDeviceStatus( |
446 test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED); | 601 test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED); |
447 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[3])); | 602 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[3])); |
448 EXPECT_FALSE(IsDeviceRegistered(test_devices_[3])); | 603 EXPECT_FALSE(IsDeviceRegistered(test_devices_[3])); |
| 604 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[3])); |
449 } | 605 } |
450 | 606 |
451 } // namespace tether | 607 } // namespace tether |
452 | 608 |
453 } // namespace chromeos | 609 } // namespace chromeos |
OLD | NEW |