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 "components/cryptauth/remote_device_test_util.h" | 13 #include "components/cryptauth/remote_device_test_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace chromeos { | 16 namespace chromeos { |
17 | 17 |
18 namespace tether { | 18 namespace tether { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 class TestObserver : public KeepAliveOperation::Observer { | 22 class TestObserver : public KeepAliveOperation::Observer { |
23 public: | 23 public: |
24 TestObserver() : has_run_callback_(false) {} | 24 TestObserver() : has_run_callback_(false) {} |
25 | 25 |
26 virtual ~TestObserver() {} | 26 virtual ~TestObserver() {} |
27 | 27 |
28 bool has_run_callback() { return has_run_callback_; } | 28 bool has_run_callback() { return has_run_callback_; } |
29 | 29 |
30 void OnOperationFinished() override { has_run_callback_ = true; } | 30 cryptauth::RemoteDevice last_remote_device_received() { |
31 return last_remote_device_received_; | |
32 } | |
33 | |
34 DeviceStatus* last_device_status_received() { | |
35 return last_device_status_received_.get(); | |
36 } | |
37 | |
38 void OnOperationFinished( | |
39 const cryptauth::RemoteDevice& remote_device, | |
40 std::unique_ptr<DeviceStatus> device_status) override { | |
41 has_run_callback_ = true; | |
42 last_remote_device_received_ = remote_device; | |
43 last_device_status_received_ = std::move(device_status); | |
44 } | |
31 | 45 |
32 private: | 46 private: |
33 bool has_run_callback_; | 47 bool has_run_callback_; |
48 cryptauth::RemoteDevice last_remote_device_received_; | |
49 std::unique_ptr<DeviceStatus> last_device_status_received_; | |
34 }; | 50 }; |
35 | 51 |
36 std::string CreateKeepAliveTickleString() { | 52 std::string CreateKeepAliveTickleString() { |
37 KeepAliveTickle tickle; | 53 KeepAliveTickle tickle; |
38 return MessageWrapper(tickle).ToRawMessage(); | 54 return MessageWrapper(tickle).ToRawMessage(); |
39 } | 55 } |
40 | 56 |
57 DeviceStatus CreateFakeDeviceStatus() { | |
Ryan Hansberry
2017/06/01 23:45:52
Has this been copied from somewhere? If so, can we
Kyle Horimoto
2017/06/06 18:30:33
Done.
| |
58 WifiStatus wifi_status; | |
59 wifi_status.set_status_code( | |
60 WifiStatus_StatusCode::WifiStatus_StatusCode_NOT_CONNECTED); | |
61 | |
62 DeviceStatus device_status; | |
63 device_status.set_battery_percentage(75); | |
64 device_status.set_cell_provider("Google Fi"); | |
65 device_status.set_connection_strength(4); | |
66 device_status.mutable_wifi_status()->CopyFrom(wifi_status); | |
67 | |
68 return device_status; | |
69 } | |
70 | |
71 std::string CreateKeepAliveTickleResponseString() { | |
72 KeepAliveTickleResponse response; | |
73 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus()); | |
74 return MessageWrapper(response).ToRawMessage(); | |
75 } | |
76 | |
41 } // namespace | 77 } // namespace |
42 | 78 |
43 class KeepAliveOperationTest : public testing::Test { | 79 class KeepAliveOperationTest : public testing::Test { |
44 protected: | 80 protected: |
45 KeepAliveOperationTest() | 81 KeepAliveOperationTest() |
46 : keep_alive_tickle_string_(CreateKeepAliveTickleString()), | 82 : keep_alive_tickle_string_(CreateKeepAliveTickleString()), |
47 test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {} | 83 test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {} |
48 | 84 |
49 void SetUp() override { | 85 void SetUp() override { |
50 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); | 86 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); |
(...skipping 23 matching lines...) Expand all Loading... | |
74 | 110 |
75 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; | 111 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; |
76 std::unique_ptr<TestObserver> test_observer_; | 112 std::unique_ptr<TestObserver> test_observer_; |
77 | 113 |
78 std::unique_ptr<KeepAliveOperation> operation_; | 114 std::unique_ptr<KeepAliveOperation> operation_; |
79 | 115 |
80 private: | 116 private: |
81 DISALLOW_COPY_AND_ASSIGN(KeepAliveOperationTest); | 117 DISALLOW_COPY_AND_ASSIGN(KeepAliveOperationTest); |
82 }; | 118 }; |
83 | 119 |
84 TEST_F(KeepAliveOperationTest, TestSendsKeepAliveTickle) { | 120 TEST_F(KeepAliveOperationTest, TestSendsKeepAliveTickleAndReceivesResponse) { |
85 EXPECT_FALSE(test_observer_->has_run_callback()); | 121 EXPECT_FALSE(test_observer_->has_run_callback()); |
122 | |
86 SimulateDeviceAuthenticationAndVerifyMessageSent(); | 123 SimulateDeviceAuthenticationAndVerifyMessageSent(); |
124 EXPECT_FALSE(test_observer_->has_run_callback()); | |
125 | |
126 fake_ble_connection_manager_->ReceiveMessage( | |
127 test_device_, CreateKeepAliveTickleResponseString()); | |
87 EXPECT_TRUE(test_observer_->has_run_callback()); | 128 EXPECT_TRUE(test_observer_->has_run_callback()); |
129 EXPECT_EQ(test_device_, test_observer_->last_remote_device_received()); | |
130 ASSERT_TRUE(test_observer_->last_device_status_received()); | |
131 EXPECT_EQ(CreateFakeDeviceStatus().SerializeAsString(), | |
132 test_observer_->last_device_status_received()->SerializeAsString()); | |
133 } | |
134 | |
135 TEST_F(KeepAliveOperationTest, TestCannotConnect) { | |
136 // Simulate the device failing to connect. | |
137 fake_ble_connection_manager_->SetDeviceStatus( | |
138 test_device_, cryptauth::SecureChannel::Status::CONNECTING); | |
139 fake_ble_connection_manager_->SetDeviceStatus( | |
140 test_device_, cryptauth::SecureChannel::Status::DISCONNECTED); | |
141 fake_ble_connection_manager_->SetDeviceStatus( | |
142 test_device_, cryptauth::SecureChannel::Status::CONNECTING); | |
143 fake_ble_connection_manager_->SetDeviceStatus( | |
144 test_device_, cryptauth::SecureChannel::Status::DISCONNECTED); | |
145 fake_ble_connection_manager_->SetDeviceStatus( | |
146 test_device_, cryptauth::SecureChannel::Status::CONNECTING); | |
147 fake_ble_connection_manager_->SetDeviceStatus( | |
148 test_device_, cryptauth::SecureChannel::Status::DISCONNECTED); | |
149 | |
150 // The maximum number of connection failures has occurred. | |
151 EXPECT_TRUE(test_observer_->has_run_callback()); | |
152 EXPECT_EQ(test_device_, test_observer_->last_remote_device_received()); | |
153 EXPECT_FALSE(test_observer_->last_device_status_received()); | |
88 } | 154 } |
89 | 155 |
90 } // namespace tether | 156 } // namespace tether |
91 | 157 |
92 } // namespace cryptauth | 158 } // namespace cryptauth |
OLD | NEW |