OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "remoting/protocol/fake_connection_to_host.h" |
| 9 #include "remoting/test/test_chromoting_client.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 const char kTestUserName[] = "test_user@faux_address.com"; |
| 14 const char kAccessToken[] = "faux_access_token"; |
| 15 } |
| 16 |
| 17 namespace remoting { |
| 18 namespace test { |
| 19 |
| 20 using testing::_; |
| 21 |
| 22 // Provides base functionality for the TestChromotingClient Tests below. This |
| 23 // fixture also creates an IO MessageLoop for use by the TestChromotingClient. |
| 24 // Overrides a subset of the RemoteConnectionObserver interface to track |
| 25 // connection status changes for result verification. |
| 26 class TestChromotingClientTest : public ::testing::Test, |
| 27 public RemoteConnectionObserver { |
| 28 public: |
| 29 TestChromotingClientTest(); |
| 30 ~TestChromotingClientTest() override; |
| 31 |
| 32 protected: |
| 33 // testing::Test interface. |
| 34 void SetUp() override; |
| 35 void TearDown() override; |
| 36 |
| 37 // Used for result verification. |
| 38 bool is_connected_to_host_; |
| 39 protocol::ConnectionToHost::State connection_state_; |
| 40 protocol::ErrorCode error_code_; |
| 41 |
| 42 // Used for simulating different conditions for the TestChromotingClient. |
| 43 RemoteHostInfo remote_host_info_; |
| 44 FakeConnectionToHost* fake_connection_to_host_; |
| 45 |
| 46 scoped_ptr<TestChromotingClient> test_chromoting_client_; |
| 47 |
| 48 private: |
| 49 // RemoteConnectionObserver interface. |
| 50 void ConnectionStateChanged(protocol::ConnectionToHost::State state, |
| 51 protocol::ErrorCode error_code) override; |
| 52 void ConnectionReady(bool ready) override; |
| 53 |
| 54 base::MessageLoopForIO message_loop_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(TestChromotingClientTest); |
| 57 }; |
| 58 |
| 59 TestChromotingClientTest::TestChromotingClientTest() |
| 60 : is_connected_to_host_(false), |
| 61 connection_state_(protocol::ConnectionToHost::INITIALIZING), |
| 62 error_code_(protocol::OK), |
| 63 fake_connection_to_host_(nullptr) { |
| 64 } |
| 65 |
| 66 TestChromotingClientTest::~TestChromotingClientTest() { |
| 67 } |
| 68 |
| 69 void TestChromotingClientTest::SetUp() { |
| 70 test_chromoting_client_.reset(new TestChromotingClient()); |
| 71 test_chromoting_client_->AddRemoteConnectionObserver(this); |
| 72 |
| 73 // Pass ownership of the FakeConnectionToHost to the chromoting instance but |
| 74 // keep the ptr around so we can use it to simulate state changes. It will |
| 75 // remain valid until |test_chromoting_client_| is destroyed. |
| 76 fake_connection_to_host_ = new FakeConnectionToHost(); |
| 77 test_chromoting_client_->SetConnectionToHostForTests( |
| 78 make_scoped_ptr(fake_connection_to_host_)); |
| 79 |
| 80 remote_host_info_.remote_host_status = kRemoteHostStatusReady; |
| 81 } |
| 82 |
| 83 void TestChromotingClientTest::TearDown() { |
| 84 test_chromoting_client_->RemoveRemoteConnectionObserver(this); |
| 85 fake_connection_to_host_ = nullptr; |
| 86 |
| 87 // The chromoting instance must be destroyed before the message loop. |
| 88 test_chromoting_client_.reset(); |
| 89 |
| 90 // The LibjingleTransportFactory destroys the PortAllocator via a DeleteSoon |
| 91 // operation. If we do not allow the message loop to run here, we run the |
| 92 // risk of the DeleteSoon task being dropped and incurring a memory leak. |
| 93 message_loop_.RunUntilIdle(); |
| 94 } |
| 95 |
| 96 void TestChromotingClientTest::ConnectionStateChanged( |
| 97 protocol::ConnectionToHost::State state, |
| 98 protocol::ErrorCode error_code) { |
| 99 connection_state_ = state; |
| 100 error_code_ = error_code; |
| 101 |
| 102 if (state != protocol::ConnectionToHost::State::CONNECTED || |
| 103 error_code != protocol::OK) { |
| 104 is_connected_to_host_ = false; |
| 105 } |
| 106 } |
| 107 |
| 108 void TestChromotingClientTest::ConnectionReady(bool ready) { |
| 109 if (ready) { |
| 110 is_connected_to_host_ = true; |
| 111 } |
| 112 } |
| 113 |
| 114 TEST_F(TestChromotingClientTest, StartConnectionAndDisconnect) { |
| 115 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken, |
| 116 remote_host_info_); |
| 117 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_); |
| 118 EXPECT_EQ(protocol::OK, error_code_); |
| 119 EXPECT_FALSE(is_connected_to_host_); |
| 120 |
| 121 // Simulate an AUTHENTICATED message being sent from the Jingle session. |
| 122 fake_connection_to_host_->SignalStateChange(protocol::Session::AUTHENTICATED, |
| 123 protocol::OK); |
| 124 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED, |
| 125 connection_state_); |
| 126 EXPECT_EQ(protocol::OK, error_code_); |
| 127 EXPECT_FALSE(is_connected_to_host_); |
| 128 |
| 129 // Simulate a CONNECTED message being sent from the Jingle session. |
| 130 fake_connection_to_host_->SignalStateChange(protocol::Session::CONNECTED, |
| 131 protocol::OK); |
| 132 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); |
| 133 EXPECT_EQ(protocol::OK, error_code_); |
| 134 EXPECT_FALSE(is_connected_to_host_); |
| 135 |
| 136 fake_connection_to_host_->SignalConnectionReady(true); |
| 137 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); |
| 138 EXPECT_EQ(protocol::OK, error_code_); |
| 139 EXPECT_TRUE(is_connected_to_host_); |
| 140 |
| 141 test_chromoting_client_->EndConnection(); |
| 142 EXPECT_EQ(protocol::ConnectionToHost::State::CLOSED, connection_state_); |
| 143 EXPECT_EQ(protocol::OK, error_code_); |
| 144 EXPECT_FALSE(is_connected_to_host_); |
| 145 } |
| 146 |
| 147 TEST_F(TestChromotingClientTest, |
| 148 StartConnectionThenFailWithAuthenticationError) { |
| 149 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken, |
| 150 remote_host_info_); |
| 151 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_); |
| 152 EXPECT_EQ(protocol::OK, error_code_); |
| 153 EXPECT_FALSE(is_connected_to_host_); |
| 154 |
| 155 fake_connection_to_host_->SignalStateChange(protocol::Session::FAILED, |
| 156 protocol::AUTHENTICATION_FAILED); |
| 157 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_); |
| 158 EXPECT_EQ(protocol::ErrorCode::AUTHENTICATION_FAILED, error_code_); |
| 159 EXPECT_FALSE(is_connected_to_host_); |
| 160 |
| 161 // Close the connection via the TestChromotingClient and verify the error |
| 162 // state is persisted. |
| 163 test_chromoting_client_->EndConnection(); |
| 164 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_); |
| 165 EXPECT_EQ(protocol::ErrorCode::AUTHENTICATION_FAILED, error_code_); |
| 166 EXPECT_FALSE(is_connected_to_host_); |
| 167 } |
| 168 |
| 169 TEST_F(TestChromotingClientTest, StartConnectionThenFailWithUnknownError) { |
| 170 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken, |
| 171 remote_host_info_); |
| 172 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_); |
| 173 EXPECT_EQ(protocol::OK, error_code_); |
| 174 EXPECT_FALSE(is_connected_to_host_); |
| 175 |
| 176 // Simulate an AUTHENTICATED message being sent from the Jingle session. |
| 177 fake_connection_to_host_->SignalStateChange(protocol::Session::AUTHENTICATED, |
| 178 protocol::OK); |
| 179 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED, |
| 180 connection_state_); |
| 181 EXPECT_EQ(protocol::OK, error_code_); |
| 182 EXPECT_FALSE(is_connected_to_host_); |
| 183 |
| 184 // Simulate a CONNECTED message being sent from the Jingle session. |
| 185 fake_connection_to_host_->SignalStateChange(protocol::Session::CONNECTED, |
| 186 protocol::OK); |
| 187 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); |
| 188 EXPECT_EQ(protocol::OK, error_code_); |
| 189 EXPECT_FALSE(is_connected_to_host_); |
| 190 |
| 191 fake_connection_to_host_->SignalConnectionReady(true); |
| 192 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); |
| 193 EXPECT_EQ(protocol::OK, error_code_); |
| 194 EXPECT_TRUE(is_connected_to_host_); |
| 195 |
| 196 fake_connection_to_host_->SignalStateChange(protocol::Session::FAILED, |
| 197 protocol::UNKNOWN_ERROR); |
| 198 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_); |
| 199 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_); |
| 200 EXPECT_FALSE(is_connected_to_host_); |
| 201 |
| 202 // Close the connection via the TestChromotingClient and verify the error |
| 203 // state is persisted. |
| 204 test_chromoting_client_->EndConnection(); |
| 205 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_); |
| 206 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_); |
| 207 EXPECT_FALSE(is_connected_to_host_); |
| 208 } |
| 209 |
| 210 } // namespace test |
| 211 } // namespace remoting |
OLD | NEW |