[email protected] | b5a6afe | 2012-01-07 05:48:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [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 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 5 | #include "remoting/protocol/jingle_session.h" |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 6 | |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 7 | #include <memory> |
sergeyu | 89d088b | 2015-12-24 00:22:44 | [diff] [blame] | 8 | #include <utility> |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 9 | #include <vector> |
sergeyu | 89d088b | 2015-12-24 00:22:44 | [diff] [blame] | 10 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 11 | #include "base/bind.h" |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 12 | #include "base/memory/ptr_util.h" |
[email protected] | eda79ed | 2014-01-24 15:30:16 | [diff] [blame] | 13 | #include "base/run_loop.h" |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
sergeyu | 428e893 | 2016-12-19 23:53:19 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 16 | #include "base/test/task_environment.h" |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 17 | #include "base/test/test_timeouts.h" |
[email protected] | 5d7eb86 | 2013-06-28 15:21:24 | [diff] [blame] | 18 | #include "base/time/time.h" |
[email protected] | ad48b7f | 2012-02-21 21:20:02 | [diff] [blame] | 19 | #include "net/socket/socket.h" |
| 20 | #include "net/socket/stream_socket.h" |
[email protected] | 09c19045 | 2013-12-12 21:18:14 | [diff] [blame] | 21 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 22 | #include "remoting/base/constants.h" |
| 23 | #include "remoting/protocol/authenticator.h" |
| 24 | #include "remoting/protocol/channel_authenticator.h" |
sergeyu | c3c43e8 | 2016-02-11 23:47:01 | [diff] [blame] | 25 | #include "remoting/protocol/chromium_port_allocator_factory.h" |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 26 | #include "remoting/protocol/connection_tester.h" |
| 27 | #include "remoting/protocol/fake_authenticator.h" |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 28 | #include "remoting/protocol/jingle_session_manager.h" |
[email protected] | fd0ca11 | 2014-07-17 06:10:20 | [diff] [blame] | 29 | #include "remoting/protocol/network_settings.h" |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 30 | #include "remoting/protocol/session_plugin.h" |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 31 | #include "remoting/protocol/transport.h" |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 32 | #include "remoting/protocol/transport_context.h" |
[email protected] | fd0ca11 | 2014-07-17 06:10:20 | [diff] [blame] | 33 | #include "remoting/signaling/fake_signal_strategy.h" |
Sergey Ulanov | 137ebe10 | 2022-01-26 18:36:18 | [diff] [blame] | 34 | #include "remoting/signaling/xmpp_constants.h" |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 35 | #include "testing/gmock/include/gmock/gmock.h" |
| 36 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 37 | |
| 38 | using testing::_; |
[email protected] | 7033be5 | 2012-07-24 01:12:45 | [diff] [blame] | 39 | using testing::AtLeast; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 40 | using testing::AtMost; |
| 41 | using testing::DeleteArg; |
| 42 | using testing::DoAll; |
| 43 | using testing::InSequence; |
| 44 | using testing::Invoke; |
| 45 | using testing::InvokeWithoutArgs; |
| 46 | using testing::Return; |
| 47 | using testing::SaveArg; |
thestig | e147d68 | 2017-05-05 07:31:31 | [diff] [blame] | 48 | using testing::SetArgPointee; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 49 | using testing::WithArg; |
| 50 | |
| 51 | namespace remoting { |
| 52 | namespace protocol { |
| 53 | |
| 54 | namespace { |
| 55 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 56 | const char kHostJid[] = "[email protected]/123"; |
| 57 | const char kClientJid[] = "[email protected]/321"; |
| 58 | |
| 59 | // kHostJid the way it would be stored in the directory. |
| 60 | const char kNormalizedHostJid[] = "[email protected]/123"; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 61 | |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 62 | class MockSessionManagerListener { |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 63 | public: |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 64 | MOCK_METHOD2(OnIncomingSession, |
| 65 | void(Session*, |
| 66 | SessionManager::IncomingSessionResponse*)); |
| 67 | }; |
| 68 | |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 69 | class MockSessionEventHandler : public Session::EventHandler { |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 70 | public: |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 71 | MOCK_METHOD1(OnSessionStateChange, void(Session::State)); |
| 72 | MOCK_METHOD2(OnSessionRouteChange, void(const std::string& channel_name, |
| 73 | const TransportRoute& route)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 74 | }; |
| 75 | |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 76 | class FakeTransport : public Transport { |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 77 | public: |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 78 | SendTransportInfoCallback send_transport_info_callback() { |
| 79 | return send_transport_info_callback_; |
| 80 | } |
| 81 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 82 | const std::vector<std::unique_ptr<jingle_xmpp::XmlElement>>& received_messages() { |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 83 | return received_messages_; |
| 84 | } |
| 85 | |
Evan Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 86 | void set_on_message_callback( |
| 87 | const base::RepeatingClosure& on_message_callback) { |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 88 | on_message_callback_ = on_message_callback; |
| 89 | } |
| 90 | |
| 91 | // Transport interface. |
| 92 | void Start(Authenticator* authenticator, |
| 93 | SendTransportInfoCallback send_transport_info_callback) override { |
| 94 | send_transport_info_callback_ = send_transport_info_callback; |
| 95 | } |
| 96 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 97 | bool ProcessTransportInfo(jingle_xmpp::XmlElement* transport_info) override { |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 98 | received_messages_.push_back( |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 99 | std::make_unique<jingle_xmpp::XmlElement>(*transport_info)); |
Evan Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 100 | if (on_message_callback_) |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 101 | on_message_callback_.Run(); |
| 102 | return true; |
| 103 | } |
| 104 | |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 105 | private: |
| 106 | SendTransportInfoCallback send_transport_info_callback_; |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 107 | std::vector<std::unique_ptr<jingle_xmpp::XmlElement>> received_messages_; |
Evan Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 108 | base::RepeatingClosure on_message_callback_; |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 109 | }; |
| 110 | |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 111 | class FakePlugin : public SessionPlugin { |
| 112 | public: |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 113 | std::unique_ptr<jingle_xmpp::XmlElement> GetNextMessage() override { |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 114 | std::string tag_name = "test-tag-"; |
Raul Tambre | 319abecb2 | 2019-02-08 07:23:37 | [diff] [blame] | 115 | tag_name += base::NumberToString(outgoing_messages_.size()); |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 116 | std::unique_ptr<jingle_xmpp::XmlElement> new_message(new jingle_xmpp::XmlElement( |
| 117 | jingle_xmpp::QName("test-namespace", tag_name))); |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 118 | outgoing_messages_.push_back(*new_message); |
| 119 | return new_message; |
| 120 | } |
| 121 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 122 | void OnIncomingMessage(const jingle_xmpp::XmlElement& attachments) override { |
| 123 | for (const jingle_xmpp::XmlElement* it = attachments.FirstElement(); |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 124 | it != nullptr; |
| 125 | it = it->NextElement()) { |
| 126 | incoming_messages_.push_back(*it); |
| 127 | } |
| 128 | } |
| 129 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 130 | const std::vector<jingle_xmpp::XmlElement>& outgoing_messages() const { |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 131 | return outgoing_messages_; |
| 132 | } |
| 133 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 134 | const std::vector<jingle_xmpp::XmlElement>& incoming_messages() const { |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 135 | return incoming_messages_; |
| 136 | } |
| 137 | |
| 138 | void Clear() { |
| 139 | outgoing_messages_.clear(); |
| 140 | incoming_messages_.clear(); |
| 141 | } |
| 142 | |
| 143 | private: |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 144 | std::vector<jingle_xmpp::XmlElement> outgoing_messages_; |
| 145 | std::vector<jingle_xmpp::XmlElement> incoming_messages_; |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 146 | }; |
| 147 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 148 | std::unique_ptr<jingle_xmpp::XmlElement> CreateTransportInfo(const std::string& id) { |
| 149 | std::unique_ptr<jingle_xmpp::XmlElement> result( |
| 150 | jingle_xmpp::XmlElement::ForStr("<transport xmlns='google:remoting:ice'/>")); |
Sergey Ulanov | 137ebe10 | 2022-01-26 18:36:18 | [diff] [blame] | 151 | result->AddAttr(kQNameId, id); |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 152 | return result; |
| 153 | } |
| 154 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 155 | } // namespace |
| 156 | |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 157 | class JingleSessionTest : public testing::Test { |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 158 | public: |
[email protected] | 836c897 | 2012-03-01 18:35:08 | [diff] [blame] | 159 | JingleSessionTest() { |
sergeyu | a1760f1 | 2015-10-14 20:24:23 | [diff] [blame] | 160 | network_settings_ = |
| 161 | NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Helper method that handles OnIncomingSession(). |
| 165 | void SetHostSession(Session* session) { |
| 166 | DCHECK(session); |
| 167 | host_session_.reset(session); |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 168 | host_session_->SetEventHandler(&host_session_event_handler_); |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 169 | host_session_->SetTransport(&host_transport_); |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 170 | host_session_->AddPlugin(&host_plugin_); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 171 | } |
| 172 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 173 | void DeleteHostSession() { host_session_.reset(); } |
| 174 | |
| 175 | void DeleteClientSession() { client_session_.reset(); } |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 176 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 177 | protected: |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 178 | void TearDown() override { |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 179 | CloseSessions(); |
| 180 | CloseSessionManager(); |
[email protected] | eda79ed | 2014-01-24 15:30:16 | [diff] [blame] | 181 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void CloseSessions() { |
| 185 | host_session_.reset(); |
| 186 | client_session_.reset(); |
| 187 | } |
| 188 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 189 | void CreateSessionManagers(FakeAuthenticator::Config auth_config, |
| 190 | int messages_till_start) { |
| 191 | host_signal_strategy_ = |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 192 | std::make_unique<FakeSignalStrategy>(SignalingAddress(kHostJid)); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 193 | client_signal_strategy_ = |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 194 | std::make_unique<FakeSignalStrategy>(SignalingAddress(kClientJid)); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 195 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 196 | FakeSignalStrategy::Connect(host_signal_strategy_.get(), |
| 197 | client_signal_strategy_.get()); |
| 198 | |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 199 | host_server_ = |
| 200 | std::make_unique<JingleSessionManager>(host_signal_strategy_.get()); |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 201 | host_server_->AcceptIncoming( |
Evan Stade | c7ae0190 | 2020-07-06 16:50:40 | [diff] [blame] | 202 | base::BindRepeating(&MockSessionManagerListener::OnIncomingSession, |
| 203 | base::Unretained(&host_server_listener_))); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 204 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 205 | std::unique_ptr<AuthenticatorFactory> factory( |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 206 | new FakeHostAuthenticatorFactory(messages_till_start, auth_config)); |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 207 | host_server_->set_authenticator_factory(std::move(factory)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 208 | |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 209 | client_server_ = |
| 210 | std::make_unique<JingleSessionManager>(client_signal_strategy_.get()); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 211 | } |
| 212 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 213 | void CreateSessionManagers(FakeAuthenticator::Config auth_config) { |
| 214 | CreateSessionManagers(auth_config, 0); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 217 | void CloseSessionManager() { |
sergeyu | 1b03268 | 2015-12-17 03:17:36 | [diff] [blame] | 218 | host_server_.reset(); |
| 219 | client_server_.reset(); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 220 | host_signal_strategy_.reset(); |
| 221 | client_signal_strategy_.reset(); |
| 222 | } |
| 223 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 224 | void SetHostExpectation(bool expect_fail) { |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 225 | EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 226 | .WillOnce( |
| 227 | DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), |
thestig | e147d68 | 2017-05-05 07:31:31 | [diff] [blame] | 228 | SetArgPointee<1>(protocol::SessionManager::ACCEPT))); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 229 | |
| 230 | { |
| 231 | InSequence dummy; |
| 232 | |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 233 | EXPECT_CALL(host_session_event_handler_, |
sergeyu | c0fddfa8 | 2015-11-27 21:51:03 | [diff] [blame] | 234 | OnSessionStateChange(Session::ACCEPTED)) |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 235 | .Times(AtMost(1)); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 236 | EXPECT_CALL(host_session_event_handler_, |
| 237 | OnSessionStateChange(Session::AUTHENTICATING)) |
| 238 | .Times(AtMost(1)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 239 | if (expect_fail) { |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 240 | EXPECT_CALL(host_session_event_handler_, |
| 241 | OnSessionStateChange(Session::FAILED)) |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 242 | .Times(1); |
| 243 | } else { |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 244 | EXPECT_CALL(host_session_event_handler_, |
| 245 | OnSessionStateChange(Session::AUTHENTICATED)) |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 246 | .Times(1); |
| 247 | // Expect that the connection will be closed eventually. |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 248 | EXPECT_CALL(host_session_event_handler_, |
| 249 | OnSessionStateChange(Session::CLOSED)) |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 250 | .Times(AtMost(1)); |
| 251 | } |
| 252 | } |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 253 | } |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 254 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 255 | void SetClientExpectation(bool expect_fail) { |
| 256 | InSequence dummy; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 257 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 258 | EXPECT_CALL(client_session_event_handler_, |
| 259 | OnSessionStateChange(Session::ACCEPTED)) |
| 260 | .Times(AtMost(1)); |
| 261 | EXPECT_CALL(client_session_event_handler_, |
| 262 | OnSessionStateChange(Session::AUTHENTICATING)) |
| 263 | .Times(AtMost(1)); |
| 264 | if (expect_fail) { |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 265 | EXPECT_CALL(client_session_event_handler_, |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 266 | OnSessionStateChange(Session::FAILED)) |
| 267 | .Times(1); |
| 268 | } else { |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 269 | EXPECT_CALL(client_session_event_handler_, |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 270 | OnSessionStateChange(Session::AUTHENTICATED)) |
| 271 | .Times(1); |
| 272 | // Expect that the connection will be closed eventually. |
| 273 | EXPECT_CALL(client_session_event_handler_, |
| 274 | OnSessionStateChange(Session::CLOSED)) |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 275 | .Times(AtMost(1)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 276 | } |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 277 | } |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 278 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 279 | void ConnectClient(std::unique_ptr<Authenticator> authenticator) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 280 | client_session_ = client_server_->Connect( |
| 281 | SignalingAddress(kNormalizedHostJid), std::move(authenticator)); |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 282 | client_session_->SetEventHandler(&client_session_event_handler_); |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 283 | client_session_->SetTransport(&client_transport_); |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 284 | client_session_->AddPlugin(&client_plugin_); |
[email protected] | eda79ed | 2014-01-24 15:30:16 | [diff] [blame] | 285 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 286 | } |
| 287 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 288 | void ConnectClient(FakeAuthenticator::Config auth_config) { |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 289 | ConnectClient(std::make_unique<FakeAuthenticator>( |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 290 | FakeAuthenticator::CLIENT, auth_config, |
| 291 | client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid)); |
| 292 | } |
| 293 | |
| 294 | void InitiateConnection(FakeAuthenticator::Config auth_config, |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 295 | bool expect_fail) { |
| 296 | SetHostExpectation(expect_fail); |
| 297 | SetClientExpectation(expect_fail); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 298 | ConnectClient(auth_config); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 301 | void ExpectRouteChange(const std::string& channel_name) { |
[email protected] | 7033be5 | 2012-07-24 01:12:45 | [diff] [blame] | 302 | EXPECT_CALL(host_session_event_handler_, |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 303 | OnSessionRouteChange(channel_name, _)) |
[email protected] | 7033be5 | 2012-07-24 01:12:45 | [diff] [blame] | 304 | .Times(AtLeast(1)); |
| 305 | EXPECT_CALL(client_session_event_handler_, |
[email protected] | 3e9187d0 | 2012-08-18 04:17:23 | [diff] [blame] | 306 | OnSessionRouteChange(channel_name, _)) |
[email protected] | 7033be5 | 2012-07-24 01:12:45 | [diff] [blame] | 307 | .Times(AtLeast(1)); |
| 308 | } |
| 309 | |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 310 | void ExpectPluginMessagesEqual() const { |
| 311 | ASSERT_EQ(client_plugin_.outgoing_messages().size(), |
| 312 | host_plugin_.incoming_messages().size()); |
| 313 | for (size_t i = 0; i < client_plugin_.outgoing_messages().size(); i++) { |
| 314 | ASSERT_EQ(client_plugin_.outgoing_messages()[i].Str(), |
| 315 | host_plugin_.incoming_messages()[i].Str()); |
| 316 | } |
| 317 | |
| 318 | ASSERT_EQ(client_plugin_.incoming_messages().size(), |
| 319 | host_plugin_.outgoing_messages().size()); |
| 320 | for (size_t i = 0; i < client_plugin_.incoming_messages().size(); i++) { |
| 321 | ASSERT_EQ(client_plugin_.incoming_messages()[i].Str(), |
| 322 | host_plugin_.outgoing_messages()[i].Str()); |
| 323 | } |
| 324 | } |
| 325 | |
Gabriel Charette | df0a744 | 2019-09-05 17:32:35 | [diff] [blame] | 326 | base::test::SingleThreadTaskEnvironment task_environment_{ |
| 327 | base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 328 | |
sergeyu | a1760f1 | 2015-10-14 20:24:23 | [diff] [blame] | 329 | NetworkSettings network_settings_; |
| 330 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 331 | std::unique_ptr<FakeSignalStrategy> host_signal_strategy_; |
| 332 | std::unique_ptr<FakeSignalStrategy> client_signal_strategy_; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 333 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 334 | std::unique_ptr<JingleSessionManager> host_server_; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 335 | MockSessionManagerListener host_server_listener_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 336 | std::unique_ptr<JingleSessionManager> client_server_; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 337 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 338 | std::unique_ptr<Session> host_session_; |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 339 | MockSessionEventHandler host_session_event_handler_; |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 340 | FakeTransport host_transport_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 341 | std::unique_ptr<Session> client_session_; |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 342 | MockSessionEventHandler client_session_event_handler_; |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 343 | FakeTransport client_transport_; |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 344 | |
| 345 | FakePlugin host_plugin_; |
| 346 | FakePlugin client_plugin_; |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | |
| 350 | // Verify that we can create and destroy session managers without a |
| 351 | // connection. |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 352 | TEST_F(JingleSessionTest, CreateAndDestoy) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 353 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // Verify that an incoming session can be rejected, and that the |
| 357 | // status of the connection is set to FAILED in this case. |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 358 | TEST_F(JingleSessionTest, RejectConnection) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 359 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 360 | |
| 361 | // Reject incoming session. |
| 362 | EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
thestig | e147d68 | 2017-05-05 07:31:31 | [diff] [blame] | 363 | .WillOnce(SetArgPointee<1>(protocol::SessionManager::DECLINE)); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 364 | |
| 365 | { |
| 366 | InSequence dummy; |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 367 | EXPECT_CALL(client_session_event_handler_, |
| 368 | OnSessionStateChange(Session::FAILED)) |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 369 | .Times(1); |
| 370 | } |
| 371 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 372 | ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
[email protected] | b1f94ee | 2012-07-12 21:56:41 | [diff] [blame] | 373 | client_session_->SetEventHandler(&client_session_event_handler_); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 374 | |
[email protected] | eda79ed | 2014-01-24 15:30:16 | [diff] [blame] | 375 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Verify that we can connect two endpoints with single-step authentication. |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 379 | TEST_F(JingleSessionTest, Connect) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 380 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
| 381 | InitiateConnection(FakeAuthenticator::Config(), false); |
[email protected] | a05c0d82a | 2012-02-29 01:19:25 | [diff] [blame] | 382 | |
| 383 | // Verify that the client specified correct initiator value. |
| 384 | ASSERT_GT(host_signal_strategy_->received_messages().size(), 0U); |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 385 | const jingle_xmpp::XmlElement* initiate_xml = |
Sergey Ulanov | dec73fbd | 2017-06-16 06:14:42 | [diff] [blame] | 386 | host_signal_strategy_->received_messages().front().get(); |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 387 | const jingle_xmpp::XmlElement* jingle_element = |
| 388 | initiate_xml->FirstNamed(jingle_xmpp::QName("urn:xmpp:jingle:1", "jingle")); |
[email protected] | a05c0d82a | 2012-02-29 01:19:25 | [diff] [blame] | 389 | ASSERT_TRUE(jingle_element); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 390 | ASSERT_EQ(client_signal_strategy_->GetLocalAddress().id(), |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 391 | jingle_element->Attr(jingle_xmpp::QName(std::string(), "initiator"))); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Verify that we can connect two endpoints with multi-step authentication. |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 395 | TEST_F(JingleSessionTest, ConnectWithMultistep) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 396 | const int kAuthRoundtrips = 3; |
| 397 | FakeAuthenticator::Config auth_config(kAuthRoundtrips, |
| 398 | FakeAuthenticator::ACCEPT, true); |
| 399 | CreateSessionManagers(auth_config); |
| 400 | InitiateConnection(auth_config, false); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 401 | } |
| 402 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 403 | TEST_F(JingleSessionTest, ConnectWithOutOfOrderIqs) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 404 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
| 405 | InitiateConnection(FakeAuthenticator::Config(), false); |
sergeyu | 68d8a74 | 2016-11-02 22:55:01 | [diff] [blame] | 406 | client_signal_strategy_->SimulateMessageReordering(); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 407 | |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 408 | // Verify that out of order transport messages are received correctly. |
| 409 | host_transport_.send_transport_info_callback().Run(CreateTransportInfo("1")); |
| 410 | host_transport_.send_transport_info_callback().Run(CreateTransportInfo("2")); |
| 411 | base::RunLoop().RunUntilIdle(); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 412 | |
| 413 | ASSERT_EQ(client_transport_.received_messages().size(), 2U); |
Sergey Ulanov | 137ebe10 | 2022-01-26 18:36:18 | [diff] [blame] | 414 | EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId)); |
| 415 | EXPECT_EQ("2", client_transport_.received_messages()[1]->Attr(kQNameId)); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Verify that out-of-order messages are handled correctly when the session is |
| 419 | // torn down after the first message. |
| 420 | TEST_F(JingleSessionTest, ConnectWithOutOfOrderIqsDestroyOnFirstMessage) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 421 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
| 422 | InitiateConnection(FakeAuthenticator::Config(), false); |
sergeyu | 68d8a74 | 2016-11-02 22:55:01 | [diff] [blame] | 423 | client_signal_strategy_->SimulateMessageReordering(); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 424 | |
| 425 | // Verify that out of order transport messages are received correctly. |
| 426 | host_transport_.send_transport_info_callback().Run(CreateTransportInfo("1")); |
| 427 | host_transport_.send_transport_info_callback().Run(CreateTransportInfo("2")); |
| 428 | |
| 429 | // Destroy the session as soon as the first message is received. |
Evan Stade | c7ae0190 | 2020-07-06 16:50:40 | [diff] [blame] | 430 | client_transport_.set_on_message_callback(base::BindRepeating( |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 431 | &JingleSessionTest::DeleteClientSession, base::Unretained(this))); |
| 432 | |
| 433 | base::RunLoop().RunUntilIdle(); |
| 434 | |
| 435 | ASSERT_EQ(client_transport_.received_messages().size(), 1U); |
Sergey Ulanov | 137ebe10 | 2022-01-26 18:36:18 | [diff] [blame] | 436 | EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId)); |
kelvinp | 897dbcb | 2016-10-22 00:40:12 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 439 | // Verify that connection is terminated when single-step auth fails. |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 440 | TEST_F(JingleSessionTest, ConnectWithBadAuth) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 441 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::REJECT)); |
| 442 | InitiateConnection(FakeAuthenticator::Config(), true); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Verify that connection is terminated when multi-step auth fails. |
[email protected] | 6c44b53 | 2012-02-24 21:57:44 | [diff] [blame] | 446 | TEST_F(JingleSessionTest, ConnectWithBadMultistepAuth) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 447 | const int kAuthRoundtrips = 3; |
| 448 | CreateSessionManagers(FakeAuthenticator::Config( |
| 449 | kAuthRoundtrips, FakeAuthenticator::REJECT, false)); |
| 450 | InitiateConnection(FakeAuthenticator::Config( |
| 451 | kAuthRoundtrips, FakeAuthenticator::ACCEPT, false), |
| 452 | true); |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 453 | } |
| 454 | |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 455 | // Verify that incompatible protocol configuration is handled properly. |
| 456 | TEST_F(JingleSessionTest, TestIncompatibleProtocol) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 457 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 458 | |
| 459 | EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)).Times(0); |
| 460 | |
| 461 | EXPECT_CALL(client_session_event_handler_, |
| 462 | OnSessionStateChange(Session::FAILED)) |
| 463 | .Times(1); |
| 464 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 465 | std::unique_ptr<CandidateSessionConfig> config = |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 466 | CandidateSessionConfig::CreateDefault(); |
sergeyu | 2a64040 | 2015-08-14 19:52:18 | [diff] [blame] | 467 | // Disable all video codecs so the host will reject connection. |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 468 | config->mutable_video_configs()->clear(); |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 469 | client_server_->set_protocol_config(std::move(config)); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 470 | ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 471 | |
| 472 | EXPECT_EQ(INCOMPATIBLE_PROTOCOL, client_session_->error()); |
| 473 | EXPECT_FALSE(host_session_); |
| 474 | } |
| 475 | |
sergeyu | 562defa | 2015-07-27 21:48:04 | [diff] [blame] | 476 | // Verify that GICE-only client is rejected with an appropriate error code. |
sergeyu | b4b0927 | 2015-04-23 18:59:12 | [diff] [blame] | 477 | TEST_F(JingleSessionTest, TestLegacyIceConnection) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 478 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 479 | |
sergeyu | 562defa | 2015-07-27 21:48:04 | [diff] [blame] | 480 | EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)).Times(0); |
| 481 | |
| 482 | EXPECT_CALL(client_session_event_handler_, |
| 483 | OnSessionStateChange(Session::FAILED)) |
| 484 | .Times(1); |
| 485 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 486 | std::unique_ptr<CandidateSessionConfig> config = |
sergeyu | 7d23c4f | 2015-07-27 20:24:08 | [diff] [blame] | 487 | CandidateSessionConfig::CreateDefault(); |
sergeyu | 76391f0 | 2015-12-02 21:50:19 | [diff] [blame] | 488 | config->set_ice_supported(false); |
sergeyu | aa6fa234 | 2015-12-22 23:26:48 | [diff] [blame] | 489 | client_server_->set_protocol_config(std::move(config)); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 490 | ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT)); |
sergeyu | b4b0927 | 2015-04-23 18:59:12 | [diff] [blame] | 491 | |
sergeyu | 562defa | 2015-07-27 21:48:04 | [diff] [blame] | 492 | EXPECT_EQ(INCOMPATIBLE_PROTOCOL, client_session_->error()); |
| 493 | EXPECT_FALSE(host_session_); |
sergeyu | b4b0927 | 2015-04-23 18:59:12 | [diff] [blame] | 494 | } |
| 495 | |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 496 | TEST_F(JingleSessionTest, DeleteSessionOnIncomingConnection) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 497 | const int kAuthRoundtrips = 3; |
| 498 | FakeAuthenticator::Config auth_config(kAuthRoundtrips, |
| 499 | FakeAuthenticator::ACCEPT, true); |
| 500 | CreateSessionManagers(auth_config); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 501 | |
| 502 | EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
thestig | e147d68 | 2017-05-05 07:31:31 | [diff] [blame] | 503 | .WillOnce( |
| 504 | DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), |
| 505 | SetArgPointee<1>(protocol::SessionManager::ACCEPT))); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 506 | |
| 507 | EXPECT_CALL(host_session_event_handler_, |
sergeyu | c0fddfa8 | 2015-11-27 21:51:03 | [diff] [blame] | 508 | OnSessionStateChange(Session::ACCEPTED)) |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 509 | .Times(AtMost(1)); |
| 510 | |
| 511 | EXPECT_CALL(host_session_event_handler_, |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 512 | OnSessionStateChange(Session::AUTHENTICATING)) |
| 513 | .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteHostSession)); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 514 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 515 | ConnectClient(auth_config); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | TEST_F(JingleSessionTest, DeleteSessionOnAuth) { |
| 519 | // Same as the previous test, but set messages_till_started to 2 in |
| 520 | // CreateSessionManagers so that the session will goes into the |
| 521 | // AUTHENTICATING state after two message exchanges. |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 522 | const int kMessagesTillStarted = 2; |
| 523 | |
| 524 | const int kAuthRoundtrips = 3; |
| 525 | FakeAuthenticator::Config auth_config(kAuthRoundtrips, |
| 526 | FakeAuthenticator::ACCEPT, true); |
| 527 | CreateSessionManagers(auth_config, kMessagesTillStarted); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 528 | |
| 529 | EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 530 | .WillOnce( |
| 531 | DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), |
thestig | e147d68 | 2017-05-05 07:31:31 | [diff] [blame] | 532 | SetArgPointee<1>(protocol::SessionManager::ACCEPT))); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 533 | |
| 534 | EXPECT_CALL(host_session_event_handler_, |
sergeyu | c0fddfa8 | 2015-11-27 21:51:03 | [diff] [blame] | 535 | OnSessionStateChange(Session::ACCEPTED)) |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 536 | .Times(AtMost(1)); |
| 537 | |
| 538 | EXPECT_CALL(host_session_event_handler_, |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 539 | OnSessionStateChange(Session::AUTHENTICATING)) |
| 540 | .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteHostSession)); |
[email protected] | 064128c | 2014-04-07 22:33:28 | [diff] [blame] | 541 | |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 542 | ConnectClient(auth_config); |
sergeyu | 60afdae4 | 2015-04-15 22:04:58 | [diff] [blame] | 543 | } |
| 544 | |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 545 | // Verify that incoming transport-info messages are handled correctly while in |
| 546 | // AUTHENTICATING state. |
| 547 | TEST_F(JingleSessionTest, TransportInfoDuringAuthentication) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 548 | const int kAuthRoundtrips = 2; |
| 549 | FakeAuthenticator::Config auth_config(kAuthRoundtrips, |
| 550 | FakeAuthenticator::ACCEPT, true); |
| 551 | |
| 552 | CreateSessionManagers(auth_config); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 553 | |
| 554 | SetHostExpectation(false); |
| 555 | { |
| 556 | InSequence dummy; |
| 557 | |
| 558 | EXPECT_CALL(client_session_event_handler_, |
| 559 | OnSessionStateChange(Session::ACCEPTED)) |
| 560 | .Times(AtMost(1)); |
| 561 | EXPECT_CALL(client_session_event_handler_, |
| 562 | OnSessionStateChange(Session::AUTHENTICATING)) |
| 563 | .Times(AtMost(1)); |
| 564 | } |
| 565 | |
| 566 | // Create connection and pause it before authentication is finished. |
| 567 | FakeAuthenticator* authenticator = new FakeAuthenticator( |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 568 | FakeAuthenticator::CLIENT, auth_config, |
| 569 | client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 570 | authenticator->set_pause_message_index(4); |
| 571 | ConnectClient(base::WrapUnique(authenticator)); |
| 572 | |
| 573 | // Send 2 transport messages. |
| 574 | host_transport_.send_transport_info_callback().Run(CreateTransportInfo("1")); |
| 575 | host_transport_.send_transport_info_callback().Run(CreateTransportInfo("2")); |
| 576 | |
| 577 | base::RunLoop().RunUntilIdle(); |
| 578 | |
| 579 | // The transport-info messages should not be received here because |
| 580 | // authentication hasn't finished. |
| 581 | EXPECT_TRUE(client_transport_.received_messages().empty()); |
| 582 | |
| 583 | // Destroy the session as soon as the first message is received. |
Evan Stade | c7ae0190 | 2020-07-06 16:50:40 | [diff] [blame] | 584 | client_transport_.set_on_message_callback(base::BindRepeating( |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 585 | &JingleSessionTest::DeleteClientSession, base::Unretained(this))); |
| 586 | |
| 587 | // Resume authentication. |
| 588 | authenticator->Resume(); |
| 589 | base::RunLoop().RunUntilIdle(); |
| 590 | |
| 591 | // Verify that transport-info that the first transport-info message was |
| 592 | // received. |
| 593 | ASSERT_EQ(client_transport_.received_messages().size(), 1U); |
Sergey Ulanov | 137ebe10 | 2022-01-26 18:36:18 | [diff] [blame] | 594 | EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId)); |
sergeyu | 04e7784 | 2016-10-31 19:28:46 | [diff] [blame] | 595 | } |
| 596 | |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 597 | TEST_F(JingleSessionTest, TestSessionPlugin) { |
| 598 | host_plugin_.Clear(); |
| 599 | client_plugin_.Clear(); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 600 | |
| 601 | const int kAuthRoundtrips = 3; |
| 602 | FakeAuthenticator::Config auth_config(kAuthRoundtrips, |
| 603 | FakeAuthenticator::ACCEPT, true); |
| 604 | |
| 605 | CreateSessionManagers(auth_config); |
| 606 | ASSERT_NO_FATAL_FAILURE(InitiateConnection(auth_config, false)); |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 607 | ExpectPluginMessagesEqual(); |
| 608 | } |
| 609 | |
| 610 | TEST_F(JingleSessionTest, SessionPluginShouldNotBeInvolvedInSessionTerminate) { |
| 611 | host_plugin_.Clear(); |
| 612 | client_plugin_.Clear(); |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 613 | CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::REJECT)); |
| 614 | InitiateConnection(FakeAuthenticator::Config(), true); |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 615 | // It's expected the client sends one more plugin message than host, the host |
| 616 | // won't send plugin message in the SESSION_TERMINATE message. |
| 617 | ASSERT_EQ(client_plugin_.outgoing_messages().size() - 1, |
| 618 | client_plugin_.incoming_messages().size()); |
| 619 | ExpectPluginMessagesEqual(); |
| 620 | } |
| 621 | |
| 622 | TEST_F(JingleSessionTest, ImmediatelyCloseSessionAfterConnect) { |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 623 | const int kAuthRoundtrips = 3; |
| 624 | FakeAuthenticator::Config auth_config(kAuthRoundtrips, |
| 625 | FakeAuthenticator::ACCEPT, true); |
| 626 | CreateSessionManagers(auth_config); |
sergeyu | 9fe3bc8 | 2017-04-11 19:57:12 | [diff] [blame] | 627 | client_session_ = client_server_->Connect( |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 628 | SignalingAddress(kNormalizedHostJid), |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 629 | std::make_unique<FakeAuthenticator>( |
sergeyu | 65aaa85 | 2017-04-11 23:26:20 | [diff] [blame] | 630 | FakeAuthenticator::CLIENT, auth_config, |
| 631 | client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid)); |
| 632 | |
zijiehe | cf233ee | 2016-12-29 23:43:28 | [diff] [blame] | 633 | client_session_->Close(HOST_OVERLOAD); |
| 634 | base::RunLoop().RunUntilIdle(); |
| 635 | // We should only send a SESSION_TERMINATE message if the session has been |
| 636 | // closed before SESSION_INITIATE message. |
| 637 | ASSERT_EQ(1U, host_signal_strategy_->received_messages().size()); |
| 638 | } |
| 639 | |
[email protected] | 1bc9c7c | 2011-12-14 00:13:39 | [diff] [blame] | 640 | } // namespace protocol |
| 641 | } // namespace remoting |