OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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/keep_alive_operation.h" | 5 #include "chromeos/components/tether/keep_alive_operation.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "chromeos/components/tether/fake_ble_connection_manager.h" | 10 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
11 #include "chromeos/components/tether/message_wrapper.h" | 11 #include "chromeos/components/tether/message_wrapper.h" |
12 #include "chromeos/components/tether/proto/tether.pb.h" | 12 #include "chromeos/components/tether/proto/tether.pb.h" |
| 13 #include "chromeos/components/tether/proto_test_util.h" |
13 #include "components/cryptauth/remote_device_test_util.h" | 14 #include "components/cryptauth/remote_device_test_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace chromeos { | 17 namespace chromeos { |
17 | 18 |
18 namespace tether { | 19 namespace tether { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 class TestObserver : public KeepAliveOperation::Observer { | 23 class TestObserver : public KeepAliveOperation::Observer { |
23 public: | 24 public: |
24 TestObserver() : has_run_callback_(false) {} | 25 TestObserver() : has_run_callback_(false) {} |
25 | 26 |
26 virtual ~TestObserver() {} | 27 virtual ~TestObserver() {} |
27 | 28 |
28 bool has_run_callback() { return has_run_callback_; } | 29 bool has_run_callback() { return has_run_callback_; } |
29 | 30 |
30 void OnOperationFinished() override { has_run_callback_ = true; } | 31 cryptauth::RemoteDevice last_remote_device_received() { |
| 32 return last_remote_device_received_; |
| 33 } |
| 34 |
| 35 DeviceStatus* last_device_status_received() { |
| 36 return last_device_status_received_.get(); |
| 37 } |
| 38 |
| 39 void OnOperationFinished( |
| 40 const cryptauth::RemoteDevice& remote_device, |
| 41 std::unique_ptr<DeviceStatus> device_status) override { |
| 42 has_run_callback_ = true; |
| 43 last_remote_device_received_ = remote_device; |
| 44 last_device_status_received_ = std::move(device_status); |
| 45 } |
31 | 46 |
32 private: | 47 private: |
33 bool has_run_callback_; | 48 bool has_run_callback_; |
| 49 cryptauth::RemoteDevice last_remote_device_received_; |
| 50 std::unique_ptr<DeviceStatus> last_device_status_received_; |
34 }; | 51 }; |
35 | 52 |
36 std::string CreateKeepAliveTickleString() { | 53 std::string CreateKeepAliveTickleString() { |
37 KeepAliveTickle tickle; | 54 KeepAliveTickle tickle; |
38 return MessageWrapper(tickle).ToRawMessage(); | 55 return MessageWrapper(tickle).ToRawMessage(); |
39 } | 56 } |
40 | 57 |
| 58 std::string CreateKeepAliveTickleResponseString() { |
| 59 KeepAliveTickleResponse response; |
| 60 response.mutable_device_status()->CopyFrom( |
| 61 CreateDeviceStatusWithFakeFields()); |
| 62 return MessageWrapper(response).ToRawMessage(); |
| 63 } |
| 64 |
41 } // namespace | 65 } // namespace |
42 | 66 |
43 class KeepAliveOperationTest : public testing::Test { | 67 class KeepAliveOperationTest : public testing::Test { |
44 protected: | 68 protected: |
45 KeepAliveOperationTest() | 69 KeepAliveOperationTest() |
46 : keep_alive_tickle_string_(CreateKeepAliveTickleString()), | 70 : keep_alive_tickle_string_(CreateKeepAliveTickleString()), |
47 test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {} | 71 test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {} |
48 | 72 |
49 void SetUp() override { | 73 void SetUp() override { |
50 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 74 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
(...skipping 23 matching lines...) Expand all Loading... |
74 | 98 |
75 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 99 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
76 std::unique_ptr<TestObserver> test_observer_; | 100 std::unique_ptr<TestObserver> test_observer_; |
77 | 101 |
78 std::unique_ptr<KeepAliveOperation> operation_; | 102 std::unique_ptr<KeepAliveOperation> operation_; |
79 | 103 |
80 private: | 104 private: |
81 DISALLOW_COPY_AND_ASSIGN(KeepAliveOperationTest); | 105 DISALLOW_COPY_AND_ASSIGN(KeepAliveOperationTest); |
82 }; | 106 }; |
83 | 107 |
84 TEST_F(KeepAliveOperationTest, TestSendsKeepAliveTickle) { | 108 TEST_F(KeepAliveOperationTest, TestSendsKeepAliveTickleAndReceivesResponse) { |
85 EXPECT_FALSE(test_observer_->has_run_callback()); | 109 EXPECT_FALSE(test_observer_->has_run_callback()); |
| 110 |
86 SimulateDeviceAuthenticationAndVerifyMessageSent(); | 111 SimulateDeviceAuthenticationAndVerifyMessageSent(); |
| 112 EXPECT_FALSE(test_observer_->has_run_callback()); |
| 113 |
| 114 fake_ble_connection_manager_->ReceiveMessage( |
| 115 test_device_, CreateKeepAliveTickleResponseString()); |
87 EXPECT_TRUE(test_observer_->has_run_callback()); | 116 EXPECT_TRUE(test_observer_->has_run_callback()); |
| 117 EXPECT_EQ(test_device_, test_observer_->last_remote_device_received()); |
| 118 ASSERT_TRUE(test_observer_->last_device_status_received()); |
| 119 EXPECT_EQ(CreateDeviceStatusWithFakeFields().SerializeAsString(), |
| 120 test_observer_->last_device_status_received()->SerializeAsString()); |
| 121 } |
| 122 |
| 123 TEST_F(KeepAliveOperationTest, TestCannotConnect) { |
| 124 // Simulate the device failing to connect. |
| 125 fake_ble_connection_manager_->SetDeviceStatus( |
| 126 test_device_, cryptauth::SecureChannel::Status::CONNECTING); |
| 127 fake_ble_connection_manager_->SetDeviceStatus( |
| 128 test_device_, cryptauth::SecureChannel::Status::DISCONNECTED); |
| 129 fake_ble_connection_manager_->SetDeviceStatus( |
| 130 test_device_, cryptauth::SecureChannel::Status::CONNECTING); |
| 131 fake_ble_connection_manager_->SetDeviceStatus( |
| 132 test_device_, cryptauth::SecureChannel::Status::DISCONNECTED); |
| 133 fake_ble_connection_manager_->SetDeviceStatus( |
| 134 test_device_, cryptauth::SecureChannel::Status::CONNECTING); |
| 135 fake_ble_connection_manager_->SetDeviceStatus( |
| 136 test_device_, cryptauth::SecureChannel::Status::DISCONNECTED); |
| 137 |
| 138 // The maximum number of connection failures has occurred. |
| 139 EXPECT_TRUE(test_observer_->has_run_callback()); |
| 140 EXPECT_EQ(test_device_, test_observer_->last_remote_device_received()); |
| 141 EXPECT_FALSE(test_observer_->last_device_status_received()); |
88 } | 142 } |
89 | 143 |
90 } // namespace tether | 144 } // namespace tether |
91 | 145 |
92 } // namespace cryptauth | 146 } // namespace cryptauth |
OLD | NEW |