OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
khorimoto
2017/05/31 17:26:36
You don't have any tests here for when the operati
Ryan Hansberry
2017/05/31 21:19:04
Done. Also added a test for different response tim
| |
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 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 bool has_device_authenticated; | 86 bool has_device_authenticated; |
85 std::vector<std::shared_ptr<MessageWrapper>> received_messages; | 87 std::vector<std::shared_ptr<MessageWrapper>> received_messages; |
86 }; | 88 }; |
87 | 89 |
88 std::map<cryptauth::RemoteDevice, DeviceMapValue> device_map_; | 90 std::map<cryptauth::RemoteDevice, DeviceMapValue> device_map_; |
89 | 91 |
90 bool has_operation_started_; | 92 bool has_operation_started_; |
91 bool has_operation_finished_; | 93 bool has_operation_finished_; |
92 }; | 94 }; |
93 | 95 |
96 class TestTimerFactory : public TimerFactory { | |
97 public: | |
98 ~TestTimerFactory() override {} | |
99 | |
100 // TimerFactory: | |
101 std::unique_ptr<base::Timer> CreateOneShotTimer() override { | |
102 return base::MakeUnique<base::MockTimer>(false, false); | |
khorimoto
2017/05/31 17:26:36
/* parameter_names */
Ryan Hansberry
2017/05/31 21:19:04
Done.
| |
103 } | |
104 }; | |
105 | |
94 DeviceStatus CreateFakeDeviceStatus() { | 106 DeviceStatus CreateFakeDeviceStatus() { |
95 WifiStatus wifi_status; | 107 WifiStatus wifi_status; |
96 wifi_status.set_status_code( | 108 wifi_status.set_status_code( |
97 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); | 109 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); |
98 wifi_status.set_ssid("Google A"); | 110 wifi_status.set_ssid("Google A"); |
99 | 111 |
100 DeviceStatus device_status; | 112 DeviceStatus device_status; |
101 device_status.set_battery_percentage(75); | 113 device_status.set_battery_percentage(75); |
102 device_status.set_cell_provider("Google Fi"); | 114 device_status.set_cell_provider("Google Fi"); |
103 device_status.set_connection_strength(4); | 115 device_status.set_connection_strength(4); |
(...skipping 22 matching lines...) Expand all Loading... | |
126 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); | 138 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); |
127 } | 139 } |
128 | 140 |
129 void SetUp() override { | 141 void SetUp() override { |
130 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 142 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
131 } | 143 } |
132 | 144 |
133 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { | 145 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { |
134 operation_ = base::WrapUnique( | 146 operation_ = base::WrapUnique( |
135 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); | 147 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); |
148 operation_->SetTimerFactoryForTest(base::MakeUnique<TestTimerFactory>()); | |
136 VerifyOperationStartedAndFinished(false /* has_started */, | 149 VerifyOperationStartedAndFinished(false /* has_started */, |
137 false /* has_finished */); | 150 false /* has_finished */); |
138 } | 151 } |
139 | 152 |
140 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { | 153 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { |
141 DCHECK(operation_); | 154 DCHECK(operation_); |
142 return std::find(operation_->remote_devices_.begin(), | 155 return std::find(operation_->remote_devices_.begin(), |
143 operation_->remote_devices_.end(), | 156 operation_->remote_devices_.end(), |
144 remote_device) != operation_->remote_devices_.end(); | 157 remote_device) != operation_->remote_devices_.end(); |
145 } | 158 } |
146 | 159 |
147 void InitializeOperation() { | 160 void InitializeOperation() { |
148 VerifyOperationStartedAndFinished(false /* has_started */, | 161 VerifyOperationStartedAndFinished(false /* has_started */, |
149 false /* has_finished */); | 162 false /* has_finished */); |
150 operation_->Initialize(); | 163 operation_->Initialize(); |
151 VerifyOperationStartedAndFinished(true /* has_started */, | 164 VerifyOperationStartedAndFinished(true /* has_started */, |
152 false /* has_finished */); | 165 false /* has_finished */); |
153 } | 166 } |
154 | 167 |
155 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { | 168 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { |
156 EXPECT_EQ(has_started, operation_->has_operation_started()); | 169 EXPECT_EQ(has_started, operation_->has_operation_started()); |
157 EXPECT_EQ(has_finished, operation_->has_operation_finished()); | 170 EXPECT_EQ(has_finished, operation_->has_operation_finished()); |
158 } | 171 } |
159 | 172 |
173 void UnregisterDevice(const cryptauth::RemoteDevice& remote_device) { | |
174 operation_->UnregisterDevice(remote_device); | |
175 } | |
176 | |
177 base::MockTimer* GetResponseTimerForDevice( | |
178 const cryptauth::RemoteDevice& remote_device) { | |
179 return static_cast<base::MockTimer*>( | |
180 operation_->GetResponseTimerForDevice(remote_device)); | |
181 } | |
182 | |
160 const std::vector<cryptauth::RemoteDevice> test_devices_; | 183 const std::vector<cryptauth::RemoteDevice> test_devices_; |
161 | 184 |
162 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 185 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
163 std::unique_ptr<TestOperation> operation_; | 186 std::unique_ptr<TestOperation> operation_; |
164 | 187 |
165 private: | 188 private: |
166 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); | 189 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); |
167 }; | 190 }; |
168 | 191 |
169 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { | 192 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 fake_ble_connection_manager_->SetDeviceStatus( | 282 fake_ble_connection_manager_->SetDeviceStatus( |
260 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | 283 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); |
261 fake_ble_connection_manager_->SetDeviceStatus( | 284 fake_ble_connection_manager_->SetDeviceStatus( |
262 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | 285 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); |
263 fake_ble_connection_manager_->SetDeviceStatus( | 286 fake_ble_connection_manager_->SetDeviceStatus( |
264 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | 287 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); |
265 fake_ble_connection_manager_->SetDeviceStatus( | 288 fake_ble_connection_manager_->SetDeviceStatus( |
266 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | 289 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); |
267 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 290 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
268 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | 291 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); |
292 EXPECT_TRUE(GetResponseTimerForDevice(test_devices_[0])); | |
khorimoto
2017/05/31 17:26:36
Test that timers are created correctly in all test
Ryan Hansberry
2017/05/31 21:19:04
Done.
| |
269 | 293 |
270 fake_ble_connection_manager_->ReceiveMessage( | 294 fake_ble_connection_manager_->ReceiveMessage( |
271 test_devices_[0], | 295 test_devices_[0], |
272 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); | 296 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); |
273 | 297 |
274 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 298 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
275 std::shared_ptr<MessageWrapper> message = | 299 std::shared_ptr<MessageWrapper> message = |
276 operation_->GetReceivedMessages(test_devices_[0])[0]; | 300 operation_->GetReceivedMessages(test_devices_[0])[0]; |
277 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 301 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
278 message->GetMessageType()); | 302 message->GetMessageType()); |
279 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 303 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
280 message->GetProto()->SerializeAsString()); | 304 message->GetProto()->SerializeAsString()); |
305 | |
306 // Simulate how subclasses behave after a successful response: unregister the | |
khorimoto
2017/05/31 17:26:36
Instead of manually calling UnregisterDevice(), up
Ryan Hansberry
2017/05/31 21:19:04
Done.
| |
307 // device. | |
308 UnregisterDevice(test_devices_[0]); | |
309 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
310 } | |
311 | |
312 TEST_F(MessageTransferOperationTest, TestAuthenticatesButTimesOut) { | |
313 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | |
314 InitializeOperation(); | |
315 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
316 | |
317 fake_ble_connection_manager_->SetDeviceStatus( | |
318 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
319 fake_ble_connection_manager_->SetDeviceStatus( | |
320 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
321 fake_ble_connection_manager_->SetDeviceStatus( | |
322 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
323 fake_ble_connection_manager_->SetDeviceStatus( | |
324 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
325 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
326 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | |
327 | |
328 GetResponseTimerForDevice(test_devices_[0])->Fire(); | |
329 | |
330 EXPECT_FALSE(IsDeviceRegistered(test_devices_[0])); | |
331 EXPECT_TRUE(operation_->has_operation_finished()); | |
332 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
281 } | 333 } |
282 | 334 |
283 TEST_F(MessageTransferOperationTest, TestRepeatedInputDevice) { | 335 TEST_F(MessageTransferOperationTest, TestRepeatedInputDevice) { |
284 // Construct with two copies of the same device. | 336 // Construct with two copies of the same device. |
285 ConstructOperation( | 337 ConstructOperation( |
286 std::vector<cryptauth::RemoteDevice>{test_devices_[0], test_devices_[0]}); | 338 std::vector<cryptauth::RemoteDevice>{test_devices_[0], test_devices_[0]}); |
287 InitializeOperation(); | 339 InitializeOperation(); |
288 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 340 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
289 | 341 |
290 fake_ble_connection_manager_->SetDeviceStatus( | 342 fake_ble_connection_manager_->SetDeviceStatus( |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 | 426 |
375 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); | 427 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); |
376 std::shared_ptr<MessageWrapper> message = | 428 std::shared_ptr<MessageWrapper> message = |
377 operation_->GetReceivedMessages(test_devices_[0])[0]; | 429 operation_->GetReceivedMessages(test_devices_[0])[0]; |
378 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, | 430 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
379 message->GetMessageType()); | 431 message->GetMessageType()); |
380 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), | 432 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
381 message->GetProto()->SerializeAsString()); | 433 message->GetProto()->SerializeAsString()); |
382 } | 434 } |
383 | 435 |
436 TEST_F(MessageTransferOperationTest, | |
437 AuthenticatesButTimesOutWaitingForResponse) { | |
khorimoto
2017/05/31 17:26:36
nit: Change name to reflect the fact that this tes
Ryan Hansberry
2017/05/31 21:19:04
Done.
| |
438 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); | |
439 | |
440 // Simulate the authentication of |test_devices_[0]|'s channel before | |
441 // initialization. | |
442 fake_ble_connection_manager_->RegisterRemoteDevice( | |
443 test_devices_[0], MessageType::CONNECT_TETHERING_REQUEST); | |
444 fake_ble_connection_manager_->SetDeviceStatus( | |
445 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); | |
446 fake_ble_connection_manager_->SetDeviceStatus( | |
447 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); | |
448 fake_ble_connection_manager_->SetDeviceStatus( | |
449 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATING); | |
450 fake_ble_connection_manager_->SetDeviceStatus( | |
451 test_devices_[0], cryptauth::SecureChannel::Status::AUTHENTICATED); | |
452 | |
453 // Now initialize; the authentication handler should have been invoked. | |
454 InitializeOperation(); | |
455 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | |
456 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); | |
457 | |
458 GetResponseTimerForDevice(test_devices_[0])->Fire(); | |
459 | |
460 EXPECT_FALSE(IsDeviceRegistered(test_devices_[0])); | |
461 EXPECT_TRUE(operation_->has_operation_finished()); | |
462 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0])); | |
463 } | |
464 | |
384 TEST_F(MessageTransferOperationTest, MultipleDevices) { | 465 TEST_F(MessageTransferOperationTest, MultipleDevices) { |
385 ConstructOperation(test_devices_); | 466 ConstructOperation(test_devices_); |
386 InitializeOperation(); | 467 InitializeOperation(); |
387 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); | 468 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); |
388 EXPECT_TRUE(IsDeviceRegistered(test_devices_[1])); | 469 EXPECT_TRUE(IsDeviceRegistered(test_devices_[1])); |
389 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); | 470 EXPECT_TRUE(IsDeviceRegistered(test_devices_[2])); |
390 EXPECT_TRUE(IsDeviceRegistered(test_devices_[3])); | 471 EXPECT_TRUE(IsDeviceRegistered(test_devices_[3])); |
391 | 472 |
392 // Authenticate |test_devices_[0]|'s channel. | 473 // Authenticate |test_devices_[0]|'s channel. |
393 fake_ble_connection_manager_->RegisterRemoteDevice( | 474 fake_ble_connection_manager_->RegisterRemoteDevice( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATING); | 525 test_devices_[3], cryptauth::SecureChannel::Status::AUTHENTICATING); |
445 fake_ble_connection_manager_->SetDeviceStatus( | 526 fake_ble_connection_manager_->SetDeviceStatus( |
446 test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED); | 527 test_devices_[3], cryptauth::SecureChannel::Status::DISCONNECTED); |
447 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[3])); | 528 EXPECT_FALSE(operation_->HasDeviceAuthenticated(test_devices_[3])); |
448 EXPECT_FALSE(IsDeviceRegistered(test_devices_[3])); | 529 EXPECT_FALSE(IsDeviceRegistered(test_devices_[3])); |
449 } | 530 } |
450 | 531 |
451 } // namespace tether | 532 } // namespace tether |
452 | 533 |
453 } // namespace chromeos | 534 } // namespace chromeos |
OLD | NEW |