Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: chromeos/components/tether/message_transfer_operation_unittest.cc

Issue 2915713002: Tether MessageTransferOperation: Only wait for a response from a host for a certain amount of time … (Closed)
Patch Set: khorimoto@ comments. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 EXPECT_FALSE(device_id_for_next_timer_.empty());
127 base::MockTimer* mock_timer = new base::MockTimer(
128 false /* retain_user_task */, false /* is_repeating */);
129 device_id_to_timer_map_[device_id_for_next_timer_] = mock_timer;
130 return base::WrapUnique(mock_timer);
131 }
132
133 base::MockTimer* GetResponseTimerForDeviceId(const std::string& device_id) {
134 return device_id_to_timer_map_[device_id_for_next_timer_];
135 }
136
137 void set_device_id_for_next_timer(
138 const std::string& device_id_for_next_timer) {
139 device_id_for_next_timer_ = device_id_for_next_timer;
140 }
141
142 private:
143 std::string device_id_for_next_timer_ = std::string();
Kyle Horimoto 2017/06/01 18:18:58 You don't have to initialize this to anything. The
Ryan Hansberry 2017/06/01 18:44:09 Oops :)
144 std::unordered_map<std::string, base::MockTimer*> device_id_to_timer_map_;
92 }; 145 };
93 146
94 DeviceStatus CreateFakeDeviceStatus() { 147 DeviceStatus CreateFakeDeviceStatus() {
95 WifiStatus wifi_status; 148 WifiStatus wifi_status;
96 wifi_status.set_status_code( 149 wifi_status.set_status_code(
97 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); 150 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED);
98 wifi_status.set_ssid("Google A"); 151 wifi_status.set_ssid("Google A");
99 152
100 DeviceStatus device_status; 153 DeviceStatus device_status;
101 device_status.set_battery_percentage(75); 154 device_status.set_battery_percentage(75);
(...skipping 22 matching lines...) Expand all
124 // These tests are written under the assumption that there are a maximum of 177 // 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. 178 // 3 connection attempts; they need to be edited if this value changes.
126 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice); 179 EXPECT_EQ(3u, MessageTransferOperation::kMaxConnectionAttemptsPerDevice);
127 } 180 }
128 181
129 void SetUp() override { 182 void SetUp() override {
130 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); 183 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
131 } 184 }
132 185
133 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) { 186 void ConstructOperation(std::vector<cryptauth::RemoteDevice> remote_devices) {
187 test_timer_factory_ = new TestTimerFactory();
134 operation_ = base::WrapUnique( 188 operation_ = base::WrapUnique(
135 new TestOperation(remote_devices, fake_ble_connection_manager_.get())); 189 new TestOperation(remote_devices, fake_ble_connection_manager_.get()));
190 operation_->SetTimerFactoryForTest(base::WrapUnique(test_timer_factory_));
136 VerifyOperationStartedAndFinished(false /* has_started */, 191 VerifyOperationStartedAndFinished(false /* has_started */,
137 false /* has_finished */); 192 false /* has_finished */);
138 } 193 }
139 194
140 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const { 195 bool IsDeviceRegistered(const cryptauth::RemoteDevice& remote_device) const {
141 DCHECK(operation_); 196 DCHECK(operation_);
142 return std::find(operation_->remote_devices_.begin(), 197 return std::find(operation_->remote_devices_.begin(),
143 operation_->remote_devices_.end(), 198 operation_->remote_devices_.end(),
144 remote_device) != operation_->remote_devices_.end(); 199 remote_device) != operation_->remote_devices_.end();
145 } 200 }
146 201
147 void InitializeOperation() { 202 void InitializeOperation() {
148 VerifyOperationStartedAndFinished(false /* has_started */, 203 VerifyOperationStartedAndFinished(false /* has_started */,
149 false /* has_finished */); 204 false /* has_finished */);
150 operation_->Initialize(); 205 operation_->Initialize();
151 VerifyOperationStartedAndFinished(true /* has_started */, 206 VerifyOperationStartedAndFinished(true /* has_started */,
152 false /* has_finished */); 207 false /* has_finished */);
153 } 208 }
154 209
155 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) { 210 void VerifyOperationStartedAndFinished(bool has_started, bool has_finished) {
156 EXPECT_EQ(has_started, operation_->has_operation_started()); 211 EXPECT_EQ(has_started, operation_->has_operation_started());
157 EXPECT_EQ(has_finished, operation_->has_operation_finished()); 212 EXPECT_EQ(has_finished, operation_->has_operation_finished());
158 } 213 }
159 214
215 void TransitionDeviceStatusFromDisconnectedToAuthenticated(
216 const cryptauth::RemoteDevice& remote_device) {
217 test_timer_factory_->set_device_id_for_next_timer(
218 remote_device.GetDeviceId());
219
220 fake_ble_connection_manager_->SetDeviceStatus(
221 remote_device, cryptauth::SecureChannel::Status::CONNECTING);
222 fake_ble_connection_manager_->SetDeviceStatus(
223 remote_device, cryptauth::SecureChannel::Status::CONNECTED);
224 fake_ble_connection_manager_->SetDeviceStatus(
225 remote_device, cryptauth::SecureChannel::Status::AUTHENTICATING);
226 fake_ble_connection_manager_->SetDeviceStatus(
227 remote_device, cryptauth::SecureChannel::Status::AUTHENTICATED);
228 }
229
230 base::MockTimer* GetResponseTimerForDevice(
231 const cryptauth::RemoteDevice& remote_device) {
232 return test_timer_factory_->GetResponseTimerForDeviceId(
233 remote_device.GetDeviceId());
234 }
235
236 void VerifyTimerCreatedForDevice(
Kyle Horimoto 2017/06/01 18:18:58 nit: Change this to VerifyDefaultTimeoutTimerCreat
Ryan Hansberry 2017/06/01 18:44:10 Done.
237 const cryptauth::RemoteDevice& remote_device) {
238 VerifyTimerCreatedForDevice(remote_device,
239 operation_->GetResponseTimeoutSeconds());
240 }
241
242 void VerifyTimerCreatedForDevice(const cryptauth::RemoteDevice& remote_device,
243 uint32_t response_timeout_seconds) {
244 EXPECT_TRUE(GetResponseTimerForDevice(remote_device));
245 EXPECT_EQ(base::TimeDelta::FromSeconds(response_timeout_seconds),
246 GetResponseTimerForDevice(remote_device)->GetCurrentDelay());
247 }
248
160 const std::vector<cryptauth::RemoteDevice> test_devices_; 249 const std::vector<cryptauth::RemoteDevice> test_devices_;
161 250
162 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; 251 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
252 TestTimerFactory* test_timer_factory_;
163 std::unique_ptr<TestOperation> operation_; 253 std::unique_ptr<TestOperation> operation_;
164 254
165 private: 255 private:
166 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest); 256 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperationTest);
167 }; 257 };
168 258
169 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) { 259 TEST_F(MessageTransferOperationTest, TestCannotConnectAndReachesRetryLimit) {
170 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); 260 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]});
171 InitializeOperation(); 261 InitializeOperation();
172 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); 262 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); 319 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
230 320
231 // Try to connect and fail. The device should still be registered. 321 // Try to connect and fail. The device should still be registered.
232 fake_ble_connection_manager_->SetDeviceStatus( 322 fake_ble_connection_manager_->SetDeviceStatus(
233 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); 323 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING);
234 fake_ble_connection_manager_->SetDeviceStatus( 324 fake_ble_connection_manager_->SetDeviceStatus(
235 test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED); 325 test_devices_[0], cryptauth::SecureChannel::Status::DISCONNECTED);
236 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); 326 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
237 327
238 // Try again and succeed. 328 // Try again and succeed.
239 fake_ble_connection_manager_->SetDeviceStatus( 329 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])); 330 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
248 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); 331 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0]));
332 VerifyTimerCreatedForDevice(test_devices_[0]);
249 333
250 EXPECT_TRUE(operation_->GetReceivedMessages(test_devices_[0]).empty()); 334 EXPECT_TRUE(operation_->GetReceivedMessages(test_devices_[0]).empty());
251 } 335 }
252 336
253 TEST_F(MessageTransferOperationTest, 337 TEST_F(MessageTransferOperationTest,
254 TestSuccessfulConnectionAndReceiveMessage) { 338 TestSuccessfulConnectionAndReceiveMessage) {
255 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]}); 339 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]});
256 InitializeOperation(); 340 InitializeOperation();
257 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0])); 341 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
258 342
259 fake_ble_connection_manager_->SetDeviceStatus( 343 // Simulate how subclasses behave after a successful response: unregister the
260 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTING); 344 // device.
261 fake_ble_connection_manager_->SetDeviceStatus( 345 operation_->set_should_unregister_device_on_message_received(true);
262 test_devices_[0], cryptauth::SecureChannel::Status::CONNECTED); 346
263 fake_ble_connection_manager_->SetDeviceStatus( 347 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])); 348 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
268 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0])); 349 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0]));
350 VerifyTimerCreatedForDevice(test_devices_[0]);
269 351
270 fake_ble_connection_manager_->ReceiveMessage( 352 fake_ble_connection_manager_->ReceiveMessage(
271 test_devices_[0], 353 test_devices_[0],
354 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage());
355
356 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size());
357 std::shared_ptr<MessageWrapper> message =
358 operation_->GetReceivedMessages(test_devices_[0])[0];
359 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE,
360 message->GetMessageType());
361 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(),
362 message->GetProto()->SerializeAsString());
363 }
364
365 TEST_F(MessageTransferOperationTest,
366 TestSuccessfulConnectionAndReceiveMessage_ResponseTimeoutSeconds) {
367 uint32_t response_timeout_seconds = 90;
Kyle Horimoto 2017/06/01 18:18:59 const uint32_t kResponseTimeoutSeconds
Ryan Hansberry 2017/06/01 18:44:09 Done.
368
369 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]});
370 InitializeOperation();
371 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
372
373 operation_->set_response_timeout_seconds(response_timeout_seconds);
374
375 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]);
376 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
377 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0]));
378 VerifyTimerCreatedForDevice(test_devices_[0], response_timeout_seconds);
379
380 EXPECT_EQ(base::TimeDelta::FromSeconds(response_timeout_seconds),
381 GetResponseTimerForDevice(test_devices_[0])->GetCurrentDelay());
382
383 fake_ble_connection_manager_->ReceiveMessage(
384 test_devices_[0],
272 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage()); 385 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage());
273 386
274 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size()); 387 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size());
275 std::shared_ptr<MessageWrapper> message = 388 std::shared_ptr<MessageWrapper> message =
276 operation_->GetReceivedMessages(test_devices_[0])[0]; 389 operation_->GetReceivedMessages(test_devices_[0])[0];
277 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, 390 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE,
278 message->GetMessageType()); 391 message->GetMessageType());
279 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), 392 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(),
280 message->GetProto()->SerializeAsString()); 393 message->GetProto()->SerializeAsString());
281 } 394 }
282 395
396 TEST_F(MessageTransferOperationTest,
397 TestSuccessfulConnectionAndReceiveMessage_ShouldNotWaitForResponse) {
398 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]});
399 InitializeOperation();
400 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
401
402 operation_->set_should_wait_for_response(false);
403
404 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]);
405 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
406 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0]));
407
408 // A Timer should not have been created because the operation should not wait
409 // for a response.
410 EXPECT_FALSE(GetResponseTimerForDevice(test_devices_[0]));
411
412 fake_ble_connection_manager_->ReceiveMessage(
413 test_devices_[0],
414 MessageWrapper(CreateTetherAvailabilityResponse()).ToRawMessage());
415
416 EXPECT_EQ(1u, operation_->GetReceivedMessages(test_devices_[0]).size());
417 std::shared_ptr<MessageWrapper> message =
418 operation_->GetReceivedMessages(test_devices_[0])[0];
419 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE,
420 message->GetMessageType());
421 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(),
422 message->GetProto()->SerializeAsString());
423 }
424
425 TEST_F(MessageTransferOperationTest, TestAuthenticatesButTimesOut) {
426 ConstructOperation(std::vector<cryptauth::RemoteDevice>{test_devices_[0]});
427 InitializeOperation();
428 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
429
430 TransitionDeviceStatusFromDisconnectedToAuthenticated(test_devices_[0]);
431 ;
Kyle Horimoto 2017/06/01 18:18:58 Remove.
Ryan Hansberry 2017/06/01 18:44:10 Done.
432 EXPECT_TRUE(IsDeviceRegistered(test_devices_[0]));
433 EXPECT_TRUE(operation_->HasDeviceAuthenticated(test_devices_[0]));
434 VerifyTimerCreatedForDevice(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 VerifyTimerCreatedForDevice(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_[0]);
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 VerifyTimerCreatedForDevice(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 VerifyTimerCreatedForDevice(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 VerifyTimerCreatedForDevice(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 VerifyTimerCreatedForDevice(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698