[email protected] | 988dfc3c | 2012-01-04 01:10:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 5 | #include "remoting/host/chromoting_host.h" |
| 6 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 7 | #include <memory> |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 5485707 | 2012-11-15 06:34:22 | [diff] [blame] | 11 | #include "base/bind_helpers.h" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 12 | #include "base/memory/ptr_util.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
fdoray | 6d056ff | 2016-07-04 21:56:42 | [diff] [blame] | 14 | #include "base/run_loop.h" |
Yuwei Huang | 1132c58 | 2019-01-10 01:05:06 | [diff] [blame] | 15 | #include "base/test/scoped_task_environment.h" |
| 16 | #include "base/threading/thread_task_runner_handle.h" |
| 17 | #include "net/base/network_change_notifier.h" |
[email protected] | 170cba4 | 2012-09-12 22:28:39 | [diff] [blame] | 18 | #include "remoting/base/auto_thread_task_runner.h" |
[email protected] | 677bb0a | 2012-07-13 19:38:07 | [diff] [blame] | 19 | #include "remoting/host/audio_capturer.h" |
[email protected] | 420a5e4 | 2012-12-18 21:42:12 | [diff] [blame] | 20 | #include "remoting/host/chromoting_host_context.h" |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 21 | #include "remoting/host/fake_desktop_environment.h" |
[email protected] | 2001320 | 2014-08-08 06:33:50 | [diff] [blame] | 22 | #include "remoting/host/fake_mouse_cursor_monitor.h" |
[email protected] | 0a071a3 | 2011-02-08 00:18:24 | [diff] [blame] | 23 | #include "remoting/host/host_mock_objects.h" |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 24 | #include "remoting/proto/video.pb.h" |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 25 | #include "remoting/protocol/errors.h" |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 26 | #include "remoting/protocol/fake_connection_to_client.h" |
sergeyu | 4d20800 | 2015-11-23 22:27:43 | [diff] [blame] | 27 | #include "remoting/protocol/fake_desktop_capturer.h" |
[email protected] | 0a071a3 | 2011-02-08 00:18:24 | [diff] [blame] | 28 | #include "remoting/protocol/protocol_mock_objects.h" |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 29 | #include "remoting/protocol/session_config.h" |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 30 | #include "remoting/protocol/transport_context.h" |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 31 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 497d8a9 | 2013-01-28 23:54:03 | [diff] [blame] | 32 | #include "testing/gmock_mutant.h" |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 33 | #include "testing/gtest/include/gtest/gtest.h" |
| 34 | |
[email protected] | f0a9d1b | 2011-03-04 21:31:44 | [diff] [blame] | 35 | using ::remoting::protocol::MockClientStub; |
[email protected] | f0a9d1b | 2011-03-04 21:31:44 | [diff] [blame] | 36 | using ::remoting::protocol::MockConnectionToClientEventHandler; |
| 37 | using ::remoting::protocol::MockHostStub; |
[email protected] | f0a9d1b | 2011-03-04 21:31:44 | [diff] [blame] | 38 | using ::remoting::protocol::MockSession; |
| 39 | using ::remoting::protocol::MockVideoStub; |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 40 | using ::remoting::protocol::Session; |
[email protected] | f0a9d1b | 2011-03-04 21:31:44 | [diff] [blame] | 41 | using ::remoting::protocol::SessionConfig; |
| 42 | |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 43 | using testing::_; |
| 44 | using testing::AnyNumber; |
[email protected] | 37961b1 | 2011-03-30 12:39:00 | [diff] [blame] | 45 | using testing::AtLeast; |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 46 | using testing::AtMost; |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 47 | using testing::DeleteArg; |
| 48 | using testing::DoAll; |
[email protected] | db63831 | 2012-06-20 00:41:32 | [diff] [blame] | 49 | using testing::Expectation; |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 50 | using testing::InSequence; |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 51 | using testing::Invoke; |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 52 | using testing::InvokeArgument; |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 53 | using testing::InvokeWithoutArgs; |
| 54 | using testing::Return; |
[email protected] | f41095f8 | 2011-06-07 16:57:44 | [diff] [blame] | 55 | using testing::ReturnRef; |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 56 | using testing::SaveArg; |
[email protected] | 37961b1 | 2011-03-30 12:39:00 | [diff] [blame] | 57 | using testing::Sequence; |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 58 | |
| 59 | namespace remoting { |
| 60 | |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 61 | const size_t kNumFailuresIgnored = 5; |
| 62 | |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 63 | class ChromotingHostTest : public testing::Test { |
| 64 | public: |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 65 | ChromotingHostTest() = default; |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 66 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 67 | void SetUp() override { |
Yuwei Huang | 1132c58 | 2019-01-10 01:05:06 | [diff] [blame] | 68 | network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
| 69 | |
| 70 | task_runner_ = new AutoThreadTaskRunner(base::ThreadTaskRunnerHandle::Get(), |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 71 | base::DoNothing()); |
[email protected] | 3c8cfbe7 | 2012-07-03 00:24:15 | [diff] [blame] | 72 | |
sergeyu | 3c62d53 | 2016-09-27 00:40:33 | [diff] [blame] | 73 | desktop_environment_factory_.reset( |
Yuwei Huang | 1132c58 | 2019-01-10 01:05:06 | [diff] [blame] | 74 | new FakeDesktopEnvironmentFactory(base::ThreadTaskRunnerHandle::Get())); |
[email protected] | 46c6139 | 2012-06-16 00:40:11 | [diff] [blame] | 75 | session_manager_ = new protocol::MockSessionManager(); |
[email protected] | 1be2ddb | 2011-05-11 01:36:36 | [diff] [blame] | 76 | |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 77 | host_.reset(new ChromotingHost( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 78 | desktop_environment_factory_.get(), base::WrapUnique(session_manager_), |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 79 | protocol::TransportContext::ForTests(protocol::TransportRole::SERVER), |
Sergey Ulanov | cc04df9 | 2017-07-07 18:33:41 | [diff] [blame] | 80 | task_runner_, // Audio |
zijiehe | ccab8bd7 | 2016-11-23 00:57:31 | [diff] [blame] | 81 | task_runner_, |
| 82 | DesktopEnvironmentOptions::CreateDefault())); // Video encode |
Sergey Ulanov | cc04df9 | 2017-07-07 18:33:41 | [diff] [blame] | 83 | host_->status_monitor()->AddStatusObserver(&host_status_observer_); |
[email protected] | 856dec5 | 2011-12-01 04:25:26 | [diff] [blame] | 84 | |
[email protected] | dc22637d6 | 2012-08-31 16:26:24 | [diff] [blame] | 85 | xmpp_login_ = "host@domain"; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 86 | session1_ = new MockSession(); |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 87 | session2_ = new MockSession(); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 88 | session_unowned1_.reset(new MockSession()); |
| 89 | session_unowned2_.reset(new MockSession()); |
[email protected] | cdd1c9e | 2012-10-26 21:14:04 | [diff] [blame] | 90 | session_config1_ = SessionConfig::ForTest(); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 91 | session_jid1_ = "user@domain/rest-of-jid"; |
[email protected] | cdd1c9e | 2012-10-26 21:14:04 | [diff] [blame] | 92 | session_config2_ = SessionConfig::ForTest(); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 93 | session_jid2_ = "user2@domain/rest-of-jid"; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 94 | session_unowned_jid1_ = "user3@doman/rest-of-jid"; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 95 | session_unowned_jid2_ = "user4@doman/rest-of-jid"; |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 96 | |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 97 | EXPECT_CALL(*session1_, jid()) |
| 98 | .WillRepeatedly(ReturnRef(session_jid1_)); |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 99 | EXPECT_CALL(*session2_, jid()) |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 100 | .WillRepeatedly(ReturnRef(session_jid2_)); |
| 101 | EXPECT_CALL(*session_unowned1_, jid()) |
| 102 | .WillRepeatedly(ReturnRef(session_unowned_jid1_)); |
| 103 | EXPECT_CALL(*session_unowned2_, jid()) |
| 104 | .WillRepeatedly(ReturnRef(session_unowned_jid2_)); |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 105 | EXPECT_CALL(*session_unowned1_, SetEventHandler(_)) |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 106 | .Times(AnyNumber()) |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 107 | .WillRepeatedly(SaveArg<0>(&session_unowned1_event_handler_)); |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 108 | EXPECT_CALL(*session_unowned2_, SetEventHandler(_)) |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 109 | .Times(AnyNumber()) |
| 110 | .WillRepeatedly(SaveArg<0>(&session_unowned2_event_handler_)); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 111 | EXPECT_CALL(*session1_, config()) |
sergeyu | b4b0927 | 2015-04-23 18:59:12 | [diff] [blame] | 112 | .WillRepeatedly(ReturnRef(*session_config1_)); |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 113 | EXPECT_CALL(*session2_, config()) |
sergeyu | b4b0927 | 2015-04-23 18:59:12 | [diff] [blame] | 114 | .WillRepeatedly(ReturnRef(*session_config2_)); |
sergeyu | de15c037 | 2015-12-10 23:08:08 | [diff] [blame] | 115 | EXPECT_CALL(*session_unowned1_, config()) |
| 116 | .WillRepeatedly(ReturnRef(*session_config1_)); |
| 117 | EXPECT_CALL(*session_unowned2_, config()) |
| 118 | .WillRepeatedly(ReturnRef(*session_config2_)); |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 119 | |
sergeyu | b031cd2 | 2015-11-19 22:17:13 | [diff] [blame] | 120 | owned_connection1_.reset( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 121 | new protocol::FakeConnectionToClient(base::WrapUnique(session1_))); |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 122 | owned_connection1_->set_host_stub(&host_stub1_); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 123 | connection1_ = owned_connection1_.get(); |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 124 | connection1_->set_client_stub(&client_stub1_); |
| 125 | |
sergeyu | b031cd2 | 2015-11-19 22:17:13 | [diff] [blame] | 126 | owned_connection2_.reset( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 127 | new protocol::FakeConnectionToClient(base::WrapUnique(session2_))); |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 128 | owned_connection2_->set_host_stub(&host_stub2_); |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 129 | connection2_ = owned_connection2_.get(); |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 130 | connection2_->set_client_stub(&client_stub2_); |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 133 | // Helper method to pretend a client is connected to ChromotingHost. |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 134 | void SimulateClientConnection(int connection_index, bool authenticate, |
| 135 | bool reject) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 136 | std::unique_ptr<protocol::ConnectionToClient> connection = std::move( |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 137 | (connection_index == 0) ? owned_connection1_ : owned_connection2_); |
[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 138 | protocol::ConnectionToClient* connection_ptr = connection.get(); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 139 | std::unique_ptr<ClientSession> client(new ClientSession( |
sergeyu | cd16e206 | 2016-09-12 19:28:35 | [diff] [blame] | 140 | host_.get(), std::move(connection), desktop_environment_factory_.get(), |
zijiehe | 4aa6ea4 | 2016-11-12 01:28:16 | [diff] [blame] | 141 | DesktopEnvironmentOptions::CreateDefault(), base::TimeDelta(), nullptr, |
zijiehe | b7da6242 | 2017-07-11 01:17:15 | [diff] [blame] | 142 | std::vector<HostExtension*>())); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 143 | ClientSession* client_ptr = client.get(); |
[email protected] | f19f09ca | 2013-03-13 11:10:29 | [diff] [blame] | 144 | |
[email protected] | 22f3d24 | 2013-04-30 20:46:16 | [diff] [blame] | 145 | connection_ptr->set_host_stub(client.get()); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 146 | get_client(connection_index) = client_ptr; |
[email protected] | 22f3d24 | 2013-04-30 20:46:16 | [diff] [blame] | 147 | |
| 148 | // |host| is responsible for deleting |client| from now on. |
sergeyu | b047307a | 2016-10-18 17:19:29 | [diff] [blame] | 149 | host_->clients_.push_back(std::move(client)); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 150 | |
| 151 | if (authenticate) { |
sergeyu | 4e1f4cd | 2016-09-27 00:42:52 | [diff] [blame] | 152 | client_ptr->OnConnectionAuthenticated(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 153 | if (!reject) |
sergeyu | 4e1f4cd | 2016-09-27 00:42:52 | [diff] [blame] | 154 | client_ptr->OnConnectionChannelsConnected(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 155 | } else { |
sergeyu | 4e1f4cd | 2016-09-27 00:42:52 | [diff] [blame] | 156 | client_ptr->OnConnectionClosed(protocol::AUTHENTICATION_FAILED); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 157 | } |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 158 | } |
| 159 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 160 | void TearDown() override { |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 161 | if (host_) |
| 162 | ShutdownHost(); |
| 163 | task_runner_ = nullptr; |
[email protected] | b01b306 | 2012-09-13 20:26:11 | [diff] [blame] | 164 | |
fdoray | 6d056ff | 2016-07-04 21:56:42 | [diff] [blame] | 165 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2001320 | 2014-08-08 06:33:50 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 168 | void NotifyConnectionClosed1() { |
| 169 | if (session_unowned1_event_handler_) { |
| 170 | session_unowned1_event_handler_->OnSessionStateChange(Session::CLOSED); |
| 171 | } |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 174 | void NotifyConnectionClosed2() { |
| 175 | if (session_unowned2_event_handler_) { |
| 176 | session_unowned2_event_handler_->OnSessionStateChange(Session::CLOSED); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
[email protected] | c5319f503 | 2013-04-25 23:06:43 | [diff] [blame] | 180 | void ShutdownHost() { |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 181 | EXPECT_CALL(host_status_observer_, OnShutdown()); |
[email protected] | 22f3d24 | 2013-04-30 20:46:16 | [diff] [blame] | 182 | host_.reset(); |
[email protected] | ce404ca | 2013-01-16 17:23:53 | [diff] [blame] | 183 | desktop_environment_factory_.reset(); |
[email protected] | 970fb67 | 2011-06-20 20:54:47 | [diff] [blame] | 184 | } |
| 185 | |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 186 | // Starts the host. |
| 187 | void StartHost() { |
[email protected] | dc22637d6 | 2012-08-31 16:26:24 | [diff] [blame] | 188 | EXPECT_CALL(host_status_observer_, OnStart(xmpp_login_)); |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 189 | EXPECT_CALL(*session_manager_, AcceptIncoming(_)); |
| 190 | host_->Start(xmpp_login_); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Expect a client to connect. |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 194 | // Return an expectation that a session has started. |
| 195 | Expectation ExpectClientConnected(int connection_index) { |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 196 | const std::string& session_jid = get_session_jid(connection_index); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 197 | |
| 198 | Expectation client_authenticated = |
| 199 | EXPECT_CALL(host_status_observer_, OnClientAuthenticated(session_jid)); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 200 | return EXPECT_CALL(host_status_observer_, OnClientConnected(session_jid)) |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 201 | .After(client_authenticated); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 202 | } |
| 203 | |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 204 | // Expect that a client is disconnected. The given action will be done after |
| 205 | // the status observer is notified that the session has finished. |
| 206 | Expectation ExpectClientDisconnected(int connection_index) { |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 207 | return EXPECT_CALL(host_status_observer_, |
| 208 | OnClientDisconnected(get_session_jid(connection_index))) |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 209 | .RetiresOnSaturation(); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 212 | protected: |
Yuwei Huang | 1132c58 | 2019-01-10 01:05:06 | [diff] [blame] | 213 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 214 | std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
[email protected] | 22f3d24 | 2013-04-30 20:46:16 | [diff] [blame] | 215 | scoped_refptr<AutoThreadTaskRunner> task_runner_; |
[email protected] | f0a9d1b | 2011-03-04 21:31:44 | [diff] [blame] | 216 | MockConnectionToClientEventHandler handler_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 217 | std::unique_ptr<FakeDesktopEnvironmentFactory> desktop_environment_factory_; |
[email protected] | 8bc6b8b5 | 2012-06-21 02:49:24 | [diff] [blame] | 218 | MockHostStatusObserver host_status_observer_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 219 | std::unique_ptr<ChromotingHost> host_; |
[email protected] | 46c6139 | 2012-06-16 00:40:11 | [diff] [blame] | 220 | protocol::MockSessionManager* session_manager_; |
[email protected] | dc22637d6 | 2012-08-31 16:26:24 | [diff] [blame] | 221 | std::string xmpp_login_; |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 222 | protocol::FakeConnectionToClient* connection1_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 223 | std::unique_ptr<protocol::FakeConnectionToClient> owned_connection1_; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 224 | ClientSession* client1_; |
| 225 | std::string session_jid1_; |
| 226 | MockSession* session1_; // Owned by |connection_|. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 227 | std::unique_ptr<SessionConfig> session_config1_; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 228 | MockClientStub client_stub1_; |
| 229 | MockHostStub host_stub1_; |
sergeyu | 60082fbf | 2015-11-24 21:12:48 | [diff] [blame] | 230 | protocol::FakeConnectionToClient* connection2_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 231 | std::unique_ptr<protocol::FakeConnectionToClient> owned_connection2_; |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 232 | ClientSession* client2_; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 233 | std::string session_jid2_; |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 234 | MockSession* session2_; // Owned by |connection2_|. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 235 | std::unique_ptr<SessionConfig> session_config2_; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 236 | MockClientStub client_stub2_; |
[email protected] | 844a372 | 2011-05-13 00:32:02 | [diff] [blame] | 237 | MockHostStub host_stub2_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 238 | std::unique_ptr<MockSession> session_unowned1_; // Not owned by a connection. |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 239 | std::string session_unowned_jid1_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 240 | std::unique_ptr<MockSession> session_unowned2_; // Not owned by a connection. |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 241 | std::string session_unowned_jid2_; |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 242 | protocol::Session::EventHandler* session_unowned1_event_handler_; |
| 243 | protocol::Session::EventHandler* session_unowned2_event_handler_; |
[email protected] | 844a372 | 2011-05-13 00:32:02 | [diff] [blame] | 244 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 245 | // Returns the cached client pointers client1_ or client2_. |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 246 | ClientSession*& get_client(int connection_index) { |
| 247 | return (connection_index == 0) ? client1_ : client2_; |
| 248 | } |
| 249 | |
| 250 | const std::string& get_session_jid(int connection_index) { |
| 251 | return (connection_index == 0) ? session_jid1_ : session_jid2_; |
| 252 | } |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 253 | }; |
| 254 | |
[email protected] | 46c6139 | 2012-06-16 00:40:11 | [diff] [blame] | 255 | TEST_F(ChromotingHostTest, StartAndShutdown) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 256 | StartHost(); |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | 46c6139 | 2012-06-16 00:40:11 | [diff] [blame] | 259 | TEST_F(ChromotingHostTest, Connect) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 260 | StartHost(); |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 261 | |
| 262 | // Shut down the host when the first video packet is received. |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 263 | ExpectClientConnected(0); |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 264 | SimulateClientConnection(0, true, false); |
[email protected] | 66bb70d | 2012-06-26 20:16:54 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 267 | TEST_F(ChromotingHostTest, AuthenticationFailed) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 268 | StartHost(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 269 | |
| 270 | EXPECT_CALL(host_status_observer_, OnAccessDenied(session_jid1_)); |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 271 | SimulateClientConnection(0, false, false); |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 46c6139 | 2012-06-16 00:40:11 | [diff] [blame] | 274 | TEST_F(ChromotingHostTest, Reconnect) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 275 | StartHost(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 276 | |
| 277 | // Connect first client. |
| 278 | ExpectClientConnected(0); |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 279 | SimulateClientConnection(0, true, false); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 280 | |
| 281 | // Disconnect first client. |
| 282 | ExpectClientDisconnected(0); |
sergeyu | 4e1f4cd | 2016-09-27 00:42:52 | [diff] [blame] | 283 | client1_->OnConnectionClosed(protocol::OK); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 284 | |
| 285 | // Connect second client. |
| 286 | ExpectClientConnected(1); |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 287 | SimulateClientConnection(1, true, false); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 288 | |
| 289 | // Disconnect second client. |
| 290 | ExpectClientDisconnected(1); |
sergeyu | 4e1f4cd | 2016-09-27 00:42:52 | [diff] [blame] | 291 | client2_->OnConnectionClosed(protocol::OK); |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | 46c6139 | 2012-06-16 00:40:11 | [diff] [blame] | 294 | TEST_F(ChromotingHostTest, ConnectWhenAnotherClientIsConnected) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 295 | StartHost(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 296 | |
| 297 | // Connect first client. |
| 298 | ExpectClientConnected(0); |
[email protected] | d317a25a | 2012-06-26 22:58:30 | [diff] [blame] | 299 | SimulateClientConnection(0, true, false); |
[email protected] | 37961b1 | 2011-03-30 12:39:00 | [diff] [blame] | 300 | |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 301 | // Connect second client. First client should be disconnected automatically. |
| 302 | { |
| 303 | InSequence s; |
| 304 | ExpectClientDisconnected(0); |
| 305 | ExpectClientConnected(1); |
| 306 | } |
| 307 | SimulateClientConnection(1, true, false); |
[email protected] | da5665e | 2012-09-13 02:11:33 | [diff] [blame] | 308 | |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 309 | // Disconnect second client. |
| 310 | ExpectClientDisconnected(1); |
sergeyu | 4e1f4cd | 2016-09-27 00:42:52 | [diff] [blame] | 311 | client2_->OnConnectionClosed(protocol::OK); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 314 | TEST_F(ChromotingHostTest, IncomingSessionAccepted) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 315 | StartHost(); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 316 | |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 317 | MockSession* session = session_unowned1_.get(); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 318 | protocol::SessionManager::IncomingSessionResponse response = |
| 319 | protocol::SessionManager::DECLINE; |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 320 | host_->OnIncomingSession(session_unowned1_.release(), &response); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 321 | EXPECT_EQ(protocol::SessionManager::ACCEPT, response); |
| 322 | |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 323 | EXPECT_CALL(*session, Close(_)).WillOnce(InvokeWithoutArgs( |
| 324 | this, &ChromotingHostTest::NotifyConnectionClosed1)); |
[email protected] | da5665e | 2012-09-13 02:11:33 | [diff] [blame] | 325 | ShutdownHost(); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 326 | } |
| 327 | |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 328 | TEST_F(ChromotingHostTest, LoginBackOffTriggersIfClientsDoNotAuthenticate) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 329 | StartHost(); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 330 | |
| 331 | protocol::SessionManager::IncomingSessionResponse response = |
| 332 | protocol::SessionManager::DECLINE; |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 333 | protocol::Session::EventHandler* |
| 334 | session_event_handlers[kNumFailuresIgnored + 1]; |
| 335 | for (size_t i = 0; i < kNumFailuresIgnored + 1; ++i) { |
| 336 | // Set expectations and responses for the new session. |
| 337 | auto session = std::make_unique<MockSession>(); |
| 338 | EXPECT_CALL(*session, jid()) |
| 339 | .WillRepeatedly(ReturnRef(session_jid1_)); |
| 340 | EXPECT_CALL(*session, config()) |
| 341 | .WillRepeatedly(ReturnRef(*session_config1_)); |
| 342 | EXPECT_CALL(*session, SetEventHandler(_)) |
| 343 | .Times(AnyNumber()) |
| 344 | .WillRepeatedly(SaveArg<0>(&session_event_handlers[i])); |
| 345 | EXPECT_CALL(*session, Close(_)).WillOnce( |
| 346 | InvokeWithoutArgs( |
| 347 | [&session_event_handlers, i]() { |
| 348 | session_event_handlers[i]->OnSessionStateChange(Session::CLOSED); |
| 349 | })); |
| 350 | // Simulate the incoming connection. |
| 351 | host_->OnIncomingSession(session.release(), &response); |
| 352 | EXPECT_EQ(protocol::SessionManager::ACCEPT, response); |
| 353 | // Begin authentication; this will increase the backoff count, and since |
| 354 | // OnSessionAuthenticated is never called, the host should only allow |
| 355 | // kNumFailuresIgnored + 1 connections before beginning the backoff. |
| 356 | host_->OnSessionAuthenticating( |
| 357 | host_->client_sessions_for_tests().front().get()); |
| 358 | } |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 359 | |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 360 | // As this is connection kNumFailuresIgnored + 2, it should be rejected. |
[email protected] | 7aaaa8d | 2012-07-03 19:26:23 | [diff] [blame] | 361 | host_->OnIncomingSession(session_unowned2_.get(), &response); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 362 | EXPECT_EQ(protocol::SessionManager::OVERLOAD, response); |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 363 | EXPECT_EQ(host_->client_sessions_for_tests().size(), kNumFailuresIgnored + 1); |
| 364 | |
| 365 | // Shut down host while objects owned by this test are still in scope. |
| 366 | ShutdownHost(); |
[email protected] | d2a6c96f | 2012-06-29 19:19:19 | [diff] [blame] | 367 | } |
| 368 | |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 369 | TEST_F(ChromotingHostTest, LoginBackOffResetsIfClientsAuthenticate) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 370 | StartHost(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 371 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 372 | protocol::SessionManager::IncomingSessionResponse response = |
| 373 | protocol::SessionManager::DECLINE; |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 374 | protocol::Session::EventHandler* |
| 375 | session_event_handlers[kNumFailuresIgnored + 1]; |
| 376 | for (size_t i = 0; i < kNumFailuresIgnored + 1; ++i) { |
| 377 | // Set expectations and responses for the new session. |
| 378 | auto session = std::make_unique<MockSession>(); |
| 379 | EXPECT_CALL(*session, jid()) |
| 380 | .WillRepeatedly(ReturnRef(session_jid1_)); |
| 381 | EXPECT_CALL(*session, config()) |
| 382 | .WillRepeatedly(ReturnRef(*session_config1_)); |
| 383 | EXPECT_CALL(*session, SetEventHandler(_)) |
| 384 | .Times(AnyNumber()) |
| 385 | .WillRepeatedly(SaveArg<0>(&session_event_handlers[i])); |
| 386 | EXPECT_CALL(*session, Close(_)).WillOnce( |
| 387 | InvokeWithoutArgs( |
| 388 | [&session_event_handlers, i]() { |
| 389 | session_event_handlers[i]->OnSessionStateChange(Session::CLOSED); |
| 390 | })); |
| 391 | // Simulate the incoming connection. |
| 392 | host_->OnIncomingSession(session.release(), &response); |
| 393 | EXPECT_EQ(protocol::SessionManager::ACCEPT, response); |
| 394 | // Begin authentication; this will increase the backoff count |
| 395 | host_->OnSessionAuthenticating( |
| 396 | host_->client_sessions_for_tests().front().get()); |
| 397 | } |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 398 | |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 399 | // Simulate successful authentication for one of the previous connections. |
| 400 | // This should reset the backoff and disconnect all the other connections. |
| 401 | host_->OnSessionAuthenticated( |
sergeyu | b047307a | 2016-10-18 17:19:29 | [diff] [blame] | 402 | host_->client_sessions_for_tests().front().get()); |
sergeyu | b047307a | 2016-10-18 17:19:29 | [diff] [blame] | 403 | EXPECT_EQ(host_->client_sessions_for_tests().size(), 1U); |
Jamie Walch | 55d02bd | 2018-05-23 22:48:29 | [diff] [blame] | 404 | |
| 405 | // This is connection kNumFailuresIgnored + 2, but since we now have a |
| 406 | // successful authentication it should not be rejected. |
| 407 | auto session = std::make_unique<MockSession>(); |
| 408 | protocol::Session::EventHandler* session_event_handler; |
| 409 | EXPECT_CALL(*session, jid()) |
| 410 | .WillRepeatedly(ReturnRef(session_jid1_)); |
| 411 | EXPECT_CALL(*session, config()) |
| 412 | .WillRepeatedly(ReturnRef(*session_config1_)); |
| 413 | EXPECT_CALL(*session, SetEventHandler(_)) |
| 414 | .Times(AnyNumber()) |
| 415 | .WillRepeatedly(SaveArg<0>(&session_event_handler)); |
| 416 | EXPECT_CALL(*session, Close(_)).WillOnce( |
| 417 | InvokeWithoutArgs( |
| 418 | [&session_event_handler]() { |
| 419 | session_event_handler->OnSessionStateChange(Session::CLOSED); |
| 420 | })); |
| 421 | host_->OnIncomingSession(session.release(), &response); |
| 422 | EXPECT_EQ(protocol::SessionManager::ACCEPT, response); |
| 423 | |
| 424 | // Shut down host while objects owned by this test are still in scope. |
| 425 | ShutdownHost(); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 426 | } |
| 427 | |
[email protected] | e5ecce0 | 2012-07-03 20:34:23 | [diff] [blame] | 428 | TEST_F(ChromotingHostTest, OnSessionRouteChange) { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 429 | StartHost(); |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 430 | |
| 431 | |
| 432 | ExpectClientConnected(0); |
| 433 | SimulateClientConnection(0, true, false); |
| 434 | |
[email protected] | e5ecce0 | 2012-07-03 20:34:23 | [diff] [blame] | 435 | std::string channel_name("ChannelName"); |
| 436 | protocol::TransportRoute route; |
sergeyu | c18df2ca | 2015-11-27 03:33:35 | [diff] [blame] | 437 | EXPECT_CALL(host_status_observer_, |
| 438 | OnClientRouteChange(session_jid1_, channel_name, _)); |
| 439 | host_->OnSessionRouteChange(get_client(0), channel_name, route); |
[email protected] | e5ecce0 | 2012-07-03 20:34:23 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | 1e72daa | 2011-01-28 21:25:42 | [diff] [blame] | 442 | } // namespace remoting |