blob: fbc819011c72eb3952a11527033547086ad483a9 [file] [log] [blame]
[email protected]b5a6afe2012-01-07 05:48:201// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1bc9c7c2011-12-14 00:13:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6c44b532012-02-24 21:57:445#include "remoting/protocol/jingle_session.h"
[email protected]1bc9c7c2011-12-14 00:13:396
Jinho Bang138fde32018-01-18 23:13:427#include <memory>
sergeyu89d088b2015-12-24 00:22:448#include <utility>
kelvinp897dbcb2016-10-22 00:40:129#include <vector>
sergeyu89d088b2015-12-24 00:22:4410
[email protected]1bc9c7c2011-12-14 00:13:3911#include "base/bind.h"
kelvinp897dbcb2016-10-22 00:40:1212#include "base/memory/ptr_util.h"
[email protected]eda79ed2014-01-24 15:30:1613#include "base/run_loop.h"
zijiehecf233ee2016-12-29 23:43:2814#include "base/strings/string_number_conversions.h"
sergeyu428e8932016-12-19 23:53:1915#include "base/strings/string_util.h"
Gabriel Charettec7108742019-08-23 03:31:4016#include "base/test/task_environment.h"
[email protected]1bc9c7c2011-12-14 00:13:3917#include "base/test/test_timeouts.h"
[email protected]5d7eb862013-06-28 15:21:2418#include "base/time/time.h"
[email protected]ad48b7f2012-02-21 21:20:0219#include "net/socket/socket.h"
20#include "net/socket/stream_socket.h"
[email protected]09c190452013-12-12 21:18:1421#include "net/url_request/url_request_context_getter.h"
[email protected]1bc9c7c2011-12-14 00:13:3922#include "remoting/base/constants.h"
23#include "remoting/protocol/authenticator.h"
24#include "remoting/protocol/channel_authenticator.h"
sergeyuc3c43e82016-02-11 23:47:0125#include "remoting/protocol/chromium_port_allocator_factory.h"
[email protected]1bc9c7c2011-12-14 00:13:3926#include "remoting/protocol/connection_tester.h"
27#include "remoting/protocol/fake_authenticator.h"
[email protected]6c44b532012-02-24 21:57:4428#include "remoting/protocol/jingle_session_manager.h"
[email protected]fd0ca112014-07-17 06:10:2029#include "remoting/protocol/network_settings.h"
zijiehecf233ee2016-12-29 23:43:2830#include "remoting/protocol/session_plugin.h"
sergeyue5767602015-12-28 19:49:0331#include "remoting/protocol/transport.h"
sergeyu86030f382015-12-16 22:45:0032#include "remoting/protocol/transport_context.h"
[email protected]fd0ca112014-07-17 06:10:2033#include "remoting/signaling/fake_signal_strategy.h"
Sergey Ulanov137ebe102022-01-26 18:36:1834#include "remoting/signaling/xmpp_constants.h"
[email protected]1bc9c7c2011-12-14 00:13:3935#include "testing/gmock/include/gmock/gmock.h"
36#include "testing/gtest/include/gtest/gtest.h"
[email protected]1bc9c7c2011-12-14 00:13:3937
38using testing::_;
[email protected]7033be52012-07-24 01:12:4539using testing::AtLeast;
[email protected]1bc9c7c2011-12-14 00:13:3940using testing::AtMost;
41using testing::DeleteArg;
42using testing::DoAll;
43using testing::InSequence;
44using testing::Invoke;
45using testing::InvokeWithoutArgs;
46using testing::Return;
47using testing::SaveArg;
thestige147d682017-05-05 07:31:3148using testing::SetArgPointee;
[email protected]1bc9c7c2011-12-14 00:13:3949using testing::WithArg;
50
51namespace remoting {
52namespace protocol {
53
54namespace {
55
sergeyu65aaa852017-04-11 23:26:2056const char kHostJid[] = "[email protected]/123";
57const char kClientJid[] = "[email protected]/321";
58
59// kHostJid the way it would be stored in the directory.
60const char kNormalizedHostJid[] = "[email protected]/123";
[email protected]1bc9c7c2011-12-14 00:13:3961
sergeyu1b032682015-12-17 03:17:3662class MockSessionManagerListener {
[email protected]1bc9c7c2011-12-14 00:13:3963 public:
[email protected]1bc9c7c2011-12-14 00:13:3964 MOCK_METHOD2(OnIncomingSession,
65 void(Session*,
66 SessionManager::IncomingSessionResponse*));
67};
68
[email protected]b1f94ee2012-07-12 21:56:4169class MockSessionEventHandler : public Session::EventHandler {
[email protected]1bc9c7c2011-12-14 00:13:3970 public:
[email protected]b1f94ee2012-07-12 21:56:4171 MOCK_METHOD1(OnSessionStateChange, void(Session::State));
72 MOCK_METHOD2(OnSessionRouteChange, void(const std::string& channel_name,
73 const TransportRoute& route));
[email protected]1bc9c7c2011-12-14 00:13:3974};
75
kelvinp897dbcb2016-10-22 00:40:1276class FakeTransport : public Transport {
sergeyue5767602015-12-28 19:49:0377 public:
kelvinp897dbcb2016-10-22 00:40:1278 SendTransportInfoCallback send_transport_info_callback() {
79 return send_transport_info_callback_;
80 }
81
Mirko Bonadei80d1cea2019-01-18 22:22:1782 const std::vector<std::unique_ptr<jingle_xmpp::XmlElement>>& received_messages() {
kelvinp897dbcb2016-10-22 00:40:1283 return received_messages_;
84 }
85
Evan Stade86dadf152020-03-16 20:06:3386 void set_on_message_callback(
87 const base::RepeatingClosure& on_message_callback) {
sergeyu04e77842016-10-31 19:28:4688 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 Bonadei80d1cea2019-01-18 22:22:1797 bool ProcessTransportInfo(jingle_xmpp::XmlElement* transport_info) override {
sergeyu04e77842016-10-31 19:28:4698 received_messages_.push_back(
Mirko Bonadei80d1cea2019-01-18 22:22:1799 std::make_unique<jingle_xmpp::XmlElement>(*transport_info));
Evan Stade86dadf152020-03-16 20:06:33100 if (on_message_callback_)
sergeyu04e77842016-10-31 19:28:46101 on_message_callback_.Run();
102 return true;
103 }
104
kelvinp897dbcb2016-10-22 00:40:12105 private:
106 SendTransportInfoCallback send_transport_info_callback_;
Mirko Bonadei80d1cea2019-01-18 22:22:17107 std::vector<std::unique_ptr<jingle_xmpp::XmlElement>> received_messages_;
Evan Stade86dadf152020-03-16 20:06:33108 base::RepeatingClosure on_message_callback_;
sergeyue5767602015-12-28 19:49:03109};
110
zijiehecf233ee2016-12-29 23:43:28111class FakePlugin : public SessionPlugin {
112 public:
Mirko Bonadei80d1cea2019-01-18 22:22:17113 std::unique_ptr<jingle_xmpp::XmlElement> GetNextMessage() override {
zijiehecf233ee2016-12-29 23:43:28114 std::string tag_name = "test-tag-";
Raul Tambre319abecb22019-02-08 07:23:37115 tag_name += base::NumberToString(outgoing_messages_.size());
Mirko Bonadei80d1cea2019-01-18 22:22:17116 std::unique_ptr<jingle_xmpp::XmlElement> new_message(new jingle_xmpp::XmlElement(
117 jingle_xmpp::QName("test-namespace", tag_name)));
zijiehecf233ee2016-12-29 23:43:28118 outgoing_messages_.push_back(*new_message);
119 return new_message;
120 }
121
Mirko Bonadei80d1cea2019-01-18 22:22:17122 void OnIncomingMessage(const jingle_xmpp::XmlElement& attachments) override {
123 for (const jingle_xmpp::XmlElement* it = attachments.FirstElement();
zijiehecf233ee2016-12-29 23:43:28124 it != nullptr;
125 it = it->NextElement()) {
126 incoming_messages_.push_back(*it);
127 }
128 }
129
Mirko Bonadei80d1cea2019-01-18 22:22:17130 const std::vector<jingle_xmpp::XmlElement>& outgoing_messages() const {
zijiehecf233ee2016-12-29 23:43:28131 return outgoing_messages_;
132 }
133
Mirko Bonadei80d1cea2019-01-18 22:22:17134 const std::vector<jingle_xmpp::XmlElement>& incoming_messages() const {
zijiehecf233ee2016-12-29 23:43:28135 return incoming_messages_;
136 }
137
138 void Clear() {
139 outgoing_messages_.clear();
140 incoming_messages_.clear();
141 }
142
143 private:
Mirko Bonadei80d1cea2019-01-18 22:22:17144 std::vector<jingle_xmpp::XmlElement> outgoing_messages_;
145 std::vector<jingle_xmpp::XmlElement> incoming_messages_;
zijiehecf233ee2016-12-29 23:43:28146};
147
Mirko Bonadei80d1cea2019-01-18 22:22:17148std::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 Ulanov137ebe102022-01-26 18:36:18151 result->AddAttr(kQNameId, id);
kelvinp897dbcb2016-10-22 00:40:12152 return result;
153}
154
[email protected]1bc9c7c2011-12-14 00:13:39155} // namespace
156
[email protected]6c44b532012-02-24 21:57:44157class JingleSessionTest : public testing::Test {
[email protected]1bc9c7c2011-12-14 00:13:39158 public:
[email protected]836c8972012-03-01 18:35:08159 JingleSessionTest() {
sergeyua1760f12015-10-14 20:24:23160 network_settings_ =
161 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING);
[email protected]1bc9c7c2011-12-14 00:13:39162 }
163
164 // Helper method that handles OnIncomingSession().
165 void SetHostSession(Session* session) {
166 DCHECK(session);
167 host_session_.reset(session);
[email protected]b1f94ee2012-07-12 21:56:41168 host_session_->SetEventHandler(&host_session_event_handler_);
sergeyue5767602015-12-28 19:49:03169 host_session_->SetTransport(&host_transport_);
zijiehecf233ee2016-12-29 23:43:28170 host_session_->AddPlugin(&host_plugin_);
[email protected]1bc9c7c2011-12-14 00:13:39171 }
172
sergeyu04e77842016-10-31 19:28:46173 void DeleteHostSession() { host_session_.reset(); }
174
175 void DeleteClientSession() { client_session_.reset(); }
[email protected]064128c2014-04-07 22:33:28176
[email protected]1bc9c7c2011-12-14 00:13:39177 protected:
dcheng440d8e1c2014-10-28 01:23:15178 void TearDown() override {
[email protected]1bc9c7c2011-12-14 00:13:39179 CloseSessions();
180 CloseSessionManager();
[email protected]eda79ed2014-01-24 15:30:16181 base::RunLoop().RunUntilIdle();
[email protected]1bc9c7c2011-12-14 00:13:39182 }
183
184 void CloseSessions() {
185 host_session_.reset();
186 client_session_.reset();
187 }
188
sergeyu65aaa852017-04-11 23:26:20189 void CreateSessionManagers(FakeAuthenticator::Config auth_config,
190 int messages_till_start) {
191 host_signal_strategy_ =
Jinho Bang138fde32018-01-18 23:13:42192 std::make_unique<FakeSignalStrategy>(SignalingAddress(kHostJid));
sergeyu65aaa852017-04-11 23:26:20193 client_signal_strategy_ =
Jinho Bang138fde32018-01-18 23:13:42194 std::make_unique<FakeSignalStrategy>(SignalingAddress(kClientJid));
sergeyu65aaa852017-04-11 23:26:20195
[email protected]1bc9c7c2011-12-14 00:13:39196 FakeSignalStrategy::Connect(host_signal_strategy_.get(),
197 client_signal_strategy_.get());
198
Peter Boström42afa23f2021-04-02 22:10:46199 host_server_ =
200 std::make_unique<JingleSessionManager>(host_signal_strategy_.get());
sergeyu1b032682015-12-17 03:17:36201 host_server_->AcceptIncoming(
Evan Stadec7ae01902020-07-06 16:50:40202 base::BindRepeating(&MockSessionManagerListener::OnIncomingSession,
203 base::Unretained(&host_server_listener_)));
[email protected]1bc9c7c2011-12-14 00:13:39204
dcheng0765c492016-04-06 22:41:53205 std::unique_ptr<AuthenticatorFactory> factory(
sergeyu65aaa852017-04-11 23:26:20206 new FakeHostAuthenticatorFactory(messages_till_start, auth_config));
sergeyuaa6fa2342015-12-22 23:26:48207 host_server_->set_authenticator_factory(std::move(factory));
[email protected]1bc9c7c2011-12-14 00:13:39208
Peter Boström42afa23f2021-04-02 22:10:46209 client_server_ =
210 std::make_unique<JingleSessionManager>(client_signal_strategy_.get());
[email protected]1bc9c7c2011-12-14 00:13:39211 }
212
sergeyu65aaa852017-04-11 23:26:20213 void CreateSessionManagers(FakeAuthenticator::Config auth_config) {
214 CreateSessionManagers(auth_config, 0);
[email protected]064128c2014-04-07 22:33:28215 }
216
[email protected]1bc9c7c2011-12-14 00:13:39217 void CloseSessionManager() {
sergeyu1b032682015-12-17 03:17:36218 host_server_.reset();
219 client_server_.reset();
[email protected]1bc9c7c2011-12-14 00:13:39220 host_signal_strategy_.reset();
221 client_signal_strategy_.reset();
222 }
223
sergeyu04e77842016-10-31 19:28:46224 void SetHostExpectation(bool expect_fail) {
[email protected]1bc9c7c2011-12-14 00:13:39225 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
sergeyu04e77842016-10-31 19:28:46226 .WillOnce(
227 DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
thestige147d682017-05-05 07:31:31228 SetArgPointee<1>(protocol::SessionManager::ACCEPT)));
[email protected]1bc9c7c2011-12-14 00:13:39229
230 {
231 InSequence dummy;
232
[email protected]b1f94ee2012-07-12 21:56:41233 EXPECT_CALL(host_session_event_handler_,
sergeyuc0fddfa82015-11-27 21:51:03234 OnSessionStateChange(Session::ACCEPTED))
[email protected]1bc9c7c2011-12-14 00:13:39235 .Times(AtMost(1));
[email protected]064128c2014-04-07 22:33:28236 EXPECT_CALL(host_session_event_handler_,
237 OnSessionStateChange(Session::AUTHENTICATING))
238 .Times(AtMost(1));
[email protected]1bc9c7c2011-12-14 00:13:39239 if (expect_fail) {
[email protected]b1f94ee2012-07-12 21:56:41240 EXPECT_CALL(host_session_event_handler_,
241 OnSessionStateChange(Session::FAILED))
[email protected]1bc9c7c2011-12-14 00:13:39242 .Times(1);
243 } else {
[email protected]b1f94ee2012-07-12 21:56:41244 EXPECT_CALL(host_session_event_handler_,
245 OnSessionStateChange(Session::AUTHENTICATED))
[email protected]1bc9c7c2011-12-14 00:13:39246 .Times(1);
247 // Expect that the connection will be closed eventually.
[email protected]b1f94ee2012-07-12 21:56:41248 EXPECT_CALL(host_session_event_handler_,
249 OnSessionStateChange(Session::CLOSED))
[email protected]1bc9c7c2011-12-14 00:13:39250 .Times(AtMost(1));
251 }
252 }
sergeyu04e77842016-10-31 19:28:46253 }
[email protected]1bc9c7c2011-12-14 00:13:39254
sergeyu04e77842016-10-31 19:28:46255 void SetClientExpectation(bool expect_fail) {
256 InSequence dummy;
[email protected]1bc9c7c2011-12-14 00:13:39257
sergeyu04e77842016-10-31 19:28:46258 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]b1f94ee2012-07-12 21:56:41265 EXPECT_CALL(client_session_event_handler_,
sergeyu04e77842016-10-31 19:28:46266 OnSessionStateChange(Session::FAILED))
267 .Times(1);
268 } else {
[email protected]064128c2014-04-07 22:33:28269 EXPECT_CALL(client_session_event_handler_,
sergeyu04e77842016-10-31 19:28:46270 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]064128c2014-04-07 22:33:28275 .Times(AtMost(1));
[email protected]1bc9c7c2011-12-14 00:13:39276 }
sergeyu04e77842016-10-31 19:28:46277 }
[email protected]1bc9c7c2011-12-14 00:13:39278
sergeyu04e77842016-10-31 19:28:46279 void ConnectClient(std::unique_ptr<Authenticator> authenticator) {
sergeyu65aaa852017-04-11 23:26:20280 client_session_ = client_server_->Connect(
281 SignalingAddress(kNormalizedHostJid), std::move(authenticator));
[email protected]b1f94ee2012-07-12 21:56:41282 client_session_->SetEventHandler(&client_session_event_handler_);
sergeyue5767602015-12-28 19:49:03283 client_session_->SetTransport(&client_transport_);
zijiehecf233ee2016-12-29 23:43:28284 client_session_->AddPlugin(&client_plugin_);
[email protected]eda79ed2014-01-24 15:30:16285 base::RunLoop().RunUntilIdle();
[email protected]1bc9c7c2011-12-14 00:13:39286 }
287
sergeyu65aaa852017-04-11 23:26:20288 void ConnectClient(FakeAuthenticator::Config auth_config) {
Jinho Bang138fde32018-01-18 23:13:42289 ConnectClient(std::make_unique<FakeAuthenticator>(
sergeyu65aaa852017-04-11 23:26:20290 FakeAuthenticator::CLIENT, auth_config,
291 client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid));
292 }
293
294 void InitiateConnection(FakeAuthenticator::Config auth_config,
sergeyu04e77842016-10-31 19:28:46295 bool expect_fail) {
296 SetHostExpectation(expect_fail);
297 SetClientExpectation(expect_fail);
sergeyu65aaa852017-04-11 23:26:20298 ConnectClient(auth_config);
sergeyu04e77842016-10-31 19:28:46299 }
300
[email protected]3e9187d02012-08-18 04:17:23301 void ExpectRouteChange(const std::string& channel_name) {
[email protected]7033be52012-07-24 01:12:45302 EXPECT_CALL(host_session_event_handler_,
[email protected]3e9187d02012-08-18 04:17:23303 OnSessionRouteChange(channel_name, _))
[email protected]7033be52012-07-24 01:12:45304 .Times(AtLeast(1));
305 EXPECT_CALL(client_session_event_handler_,
[email protected]3e9187d02012-08-18 04:17:23306 OnSessionRouteChange(channel_name, _))
[email protected]7033be52012-07-24 01:12:45307 .Times(AtLeast(1));
308 }
309
zijiehecf233ee2016-12-29 23:43:28310 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 Charettedf0a7442019-09-05 17:32:35326 base::test::SingleThreadTaskEnvironment task_environment_{
327 base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
[email protected]1bc9c7c2011-12-14 00:13:39328
sergeyua1760f12015-10-14 20:24:23329 NetworkSettings network_settings_;
330
dcheng0765c492016-04-06 22:41:53331 std::unique_ptr<FakeSignalStrategy> host_signal_strategy_;
332 std::unique_ptr<FakeSignalStrategy> client_signal_strategy_;
[email protected]1bc9c7c2011-12-14 00:13:39333
dcheng0765c492016-04-06 22:41:53334 std::unique_ptr<JingleSessionManager> host_server_;
[email protected]1bc9c7c2011-12-14 00:13:39335 MockSessionManagerListener host_server_listener_;
dcheng0765c492016-04-06 22:41:53336 std::unique_ptr<JingleSessionManager> client_server_;
[email protected]1bc9c7c2011-12-14 00:13:39337
dcheng0765c492016-04-06 22:41:53338 std::unique_ptr<Session> host_session_;
[email protected]b1f94ee2012-07-12 21:56:41339 MockSessionEventHandler host_session_event_handler_;
kelvinp897dbcb2016-10-22 00:40:12340 FakeTransport host_transport_;
dcheng0765c492016-04-06 22:41:53341 std::unique_ptr<Session> client_session_;
[email protected]b1f94ee2012-07-12 21:56:41342 MockSessionEventHandler client_session_event_handler_;
kelvinp897dbcb2016-10-22 00:40:12343 FakeTransport client_transport_;
zijiehecf233ee2016-12-29 23:43:28344
345 FakePlugin host_plugin_;
346 FakePlugin client_plugin_;
[email protected]1bc9c7c2011-12-14 00:13:39347};
348
349
350// Verify that we can create and destroy session managers without a
351// connection.
[email protected]6c44b532012-02-24 21:57:44352TEST_F(JingleSessionTest, CreateAndDestoy) {
sergeyu65aaa852017-04-11 23:26:20353 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
[email protected]1bc9c7c2011-12-14 00:13:39354}
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]6c44b532012-02-24 21:57:44358TEST_F(JingleSessionTest, RejectConnection) {
sergeyu65aaa852017-04-11 23:26:20359 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
[email protected]1bc9c7c2011-12-14 00:13:39360
361 // Reject incoming session.
362 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
thestige147d682017-05-05 07:31:31363 .WillOnce(SetArgPointee<1>(protocol::SessionManager::DECLINE));
[email protected]1bc9c7c2011-12-14 00:13:39364
365 {
366 InSequence dummy;
[email protected]b1f94ee2012-07-12 21:56:41367 EXPECT_CALL(client_session_event_handler_,
368 OnSessionStateChange(Session::FAILED))
[email protected]1bc9c7c2011-12-14 00:13:39369 .Times(1);
370 }
371
sergeyu65aaa852017-04-11 23:26:20372 ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
[email protected]b1f94ee2012-07-12 21:56:41373 client_session_->SetEventHandler(&client_session_event_handler_);
[email protected]1bc9c7c2011-12-14 00:13:39374
[email protected]eda79ed2014-01-24 15:30:16375 base::RunLoop().RunUntilIdle();
[email protected]1bc9c7c2011-12-14 00:13:39376}
377
378// Verify that we can connect two endpoints with single-step authentication.
[email protected]6c44b532012-02-24 21:57:44379TEST_F(JingleSessionTest, Connect) {
sergeyu65aaa852017-04-11 23:26:20380 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
381 InitiateConnection(FakeAuthenticator::Config(), false);
[email protected]a05c0d82a2012-02-29 01:19:25382
383 // Verify that the client specified correct initiator value.
384 ASSERT_GT(host_signal_strategy_->received_messages().size(), 0U);
Mirko Bonadei80d1cea2019-01-18 22:22:17385 const jingle_xmpp::XmlElement* initiate_xml =
Sergey Ulanovdec73fbd2017-06-16 06:14:42386 host_signal_strategy_->received_messages().front().get();
Mirko Bonadei80d1cea2019-01-18 22:22:17387 const jingle_xmpp::XmlElement* jingle_element =
388 initiate_xml->FirstNamed(jingle_xmpp::QName("urn:xmpp:jingle:1", "jingle"));
[email protected]a05c0d82a2012-02-29 01:19:25389 ASSERT_TRUE(jingle_element);
sergeyu65aaa852017-04-11 23:26:20390 ASSERT_EQ(client_signal_strategy_->GetLocalAddress().id(),
Mirko Bonadei80d1cea2019-01-18 22:22:17391 jingle_element->Attr(jingle_xmpp::QName(std::string(), "initiator")));
[email protected]1bc9c7c2011-12-14 00:13:39392}
393
394// Verify that we can connect two endpoints with multi-step authentication.
[email protected]6c44b532012-02-24 21:57:44395TEST_F(JingleSessionTest, ConnectWithMultistep) {
sergeyu65aaa852017-04-11 23:26:20396 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]1bc9c7c2011-12-14 00:13:39401}
402
sergeyu04e77842016-10-31 19:28:46403TEST_F(JingleSessionTest, ConnectWithOutOfOrderIqs) {
sergeyu65aaa852017-04-11 23:26:20404 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
405 InitiateConnection(FakeAuthenticator::Config(), false);
sergeyu68d8a742016-11-02 22:55:01406 client_signal_strategy_->SimulateMessageReordering();
sergeyu04e77842016-10-31 19:28:46407
kelvinp897dbcb2016-10-22 00:40:12408 // 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();
sergeyu04e77842016-10-31 19:28:46412
413 ASSERT_EQ(client_transport_.received_messages().size(), 2U);
Sergey Ulanov137ebe102022-01-26 18:36:18414 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId));
415 EXPECT_EQ("2", client_transport_.received_messages()[1]->Attr(kQNameId));
sergeyu04e77842016-10-31 19:28:46416}
417
418// Verify that out-of-order messages are handled correctly when the session is
419// torn down after the first message.
420TEST_F(JingleSessionTest, ConnectWithOutOfOrderIqsDestroyOnFirstMessage) {
sergeyu65aaa852017-04-11 23:26:20421 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
422 InitiateConnection(FakeAuthenticator::Config(), false);
sergeyu68d8a742016-11-02 22:55:01423 client_signal_strategy_->SimulateMessageReordering();
sergeyu04e77842016-10-31 19:28:46424
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 Stadec7ae01902020-07-06 16:50:40430 client_transport_.set_on_message_callback(base::BindRepeating(
sergeyu04e77842016-10-31 19:28:46431 &JingleSessionTest::DeleteClientSession, base::Unretained(this)));
432
433 base::RunLoop().RunUntilIdle();
434
435 ASSERT_EQ(client_transport_.received_messages().size(), 1U);
Sergey Ulanov137ebe102022-01-26 18:36:18436 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId));
kelvinp897dbcb2016-10-22 00:40:12437}
438
[email protected]1bc9c7c2011-12-14 00:13:39439// Verify that connection is terminated when single-step auth fails.
[email protected]6c44b532012-02-24 21:57:44440TEST_F(JingleSessionTest, ConnectWithBadAuth) {
sergeyu65aaa852017-04-11 23:26:20441 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::REJECT));
442 InitiateConnection(FakeAuthenticator::Config(), true);
[email protected]1bc9c7c2011-12-14 00:13:39443}
444
445// Verify that connection is terminated when multi-step auth fails.
[email protected]6c44b532012-02-24 21:57:44446TEST_F(JingleSessionTest, ConnectWithBadMultistepAuth) {
sergeyu65aaa852017-04-11 23:26:20447 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]1bc9c7c2011-12-14 00:13:39453}
454
sergeyu7d23c4f2015-07-27 20:24:08455// Verify that incompatible protocol configuration is handled properly.
456TEST_F(JingleSessionTest, TestIncompatibleProtocol) {
sergeyu65aaa852017-04-11 23:26:20457 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
sergeyu7d23c4f2015-07-27 20:24:08458
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
dcheng0765c492016-04-06 22:41:53465 std::unique_ptr<CandidateSessionConfig> config =
sergeyu7d23c4f2015-07-27 20:24:08466 CandidateSessionConfig::CreateDefault();
sergeyu2a640402015-08-14 19:52:18467 // Disable all video codecs so the host will reject connection.
sergeyu7d23c4f2015-07-27 20:24:08468 config->mutable_video_configs()->clear();
sergeyuaa6fa2342015-12-22 23:26:48469 client_server_->set_protocol_config(std::move(config));
sergeyu65aaa852017-04-11 23:26:20470 ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
sergeyu7d23c4f2015-07-27 20:24:08471
472 EXPECT_EQ(INCOMPATIBLE_PROTOCOL, client_session_->error());
473 EXPECT_FALSE(host_session_);
474}
475
sergeyu562defa2015-07-27 21:48:04476// Verify that GICE-only client is rejected with an appropriate error code.
sergeyub4b09272015-04-23 18:59:12477TEST_F(JingleSessionTest, TestLegacyIceConnection) {
sergeyu65aaa852017-04-11 23:26:20478 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
sergeyu7d23c4f2015-07-27 20:24:08479
sergeyu562defa2015-07-27 21:48:04480 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)).Times(0);
481
482 EXPECT_CALL(client_session_event_handler_,
483 OnSessionStateChange(Session::FAILED))
484 .Times(1);
485
dcheng0765c492016-04-06 22:41:53486 std::unique_ptr<CandidateSessionConfig> config =
sergeyu7d23c4f2015-07-27 20:24:08487 CandidateSessionConfig::CreateDefault();
sergeyu76391f02015-12-02 21:50:19488 config->set_ice_supported(false);
sergeyuaa6fa2342015-12-22 23:26:48489 client_server_->set_protocol_config(std::move(config));
sergeyu65aaa852017-04-11 23:26:20490 ConnectClient(FakeAuthenticator::Config(FakeAuthenticator::ACCEPT));
sergeyub4b09272015-04-23 18:59:12491
sergeyu562defa2015-07-27 21:48:04492 EXPECT_EQ(INCOMPATIBLE_PROTOCOL, client_session_->error());
493 EXPECT_FALSE(host_session_);
sergeyub4b09272015-04-23 18:59:12494}
495
[email protected]064128c2014-04-07 22:33:28496TEST_F(JingleSessionTest, DeleteSessionOnIncomingConnection) {
sergeyu65aaa852017-04-11 23:26:20497 const int kAuthRoundtrips = 3;
498 FakeAuthenticator::Config auth_config(kAuthRoundtrips,
499 FakeAuthenticator::ACCEPT, true);
500 CreateSessionManagers(auth_config);
[email protected]064128c2014-04-07 22:33:28501
502 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
thestige147d682017-05-05 07:31:31503 .WillOnce(
504 DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
505 SetArgPointee<1>(protocol::SessionManager::ACCEPT)));
[email protected]064128c2014-04-07 22:33:28506
507 EXPECT_CALL(host_session_event_handler_,
sergeyuc0fddfa82015-11-27 21:51:03508 OnSessionStateChange(Session::ACCEPTED))
[email protected]064128c2014-04-07 22:33:28509 .Times(AtMost(1));
510
511 EXPECT_CALL(host_session_event_handler_,
sergeyu04e77842016-10-31 19:28:46512 OnSessionStateChange(Session::AUTHENTICATING))
513 .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteHostSession));
[email protected]064128c2014-04-07 22:33:28514
sergeyu65aaa852017-04-11 23:26:20515 ConnectClient(auth_config);
[email protected]064128c2014-04-07 22:33:28516}
517
518TEST_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.
sergeyu65aaa852017-04-11 23:26:20522 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]064128c2014-04-07 22:33:28528
529 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
sergeyu04e77842016-10-31 19:28:46530 .WillOnce(
531 DoAll(WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
thestige147d682017-05-05 07:31:31532 SetArgPointee<1>(protocol::SessionManager::ACCEPT)));
[email protected]064128c2014-04-07 22:33:28533
534 EXPECT_CALL(host_session_event_handler_,
sergeyuc0fddfa82015-11-27 21:51:03535 OnSessionStateChange(Session::ACCEPTED))
[email protected]064128c2014-04-07 22:33:28536 .Times(AtMost(1));
537
538 EXPECT_CALL(host_session_event_handler_,
sergeyu04e77842016-10-31 19:28:46539 OnSessionStateChange(Session::AUTHENTICATING))
540 .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteHostSession));
[email protected]064128c2014-04-07 22:33:28541
sergeyu65aaa852017-04-11 23:26:20542 ConnectClient(auth_config);
sergeyu60afdae42015-04-15 22:04:58543}
544
sergeyu04e77842016-10-31 19:28:46545// Verify that incoming transport-info messages are handled correctly while in
546// AUTHENTICATING state.
547TEST_F(JingleSessionTest, TransportInfoDuringAuthentication) {
sergeyu65aaa852017-04-11 23:26:20548 const int kAuthRoundtrips = 2;
549 FakeAuthenticator::Config auth_config(kAuthRoundtrips,
550 FakeAuthenticator::ACCEPT, true);
551
552 CreateSessionManagers(auth_config);
sergeyu04e77842016-10-31 19:28:46553
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(
sergeyu65aaa852017-04-11 23:26:20568 FakeAuthenticator::CLIENT, auth_config,
569 client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid);
sergeyu04e77842016-10-31 19:28:46570 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 Stadec7ae01902020-07-06 16:50:40584 client_transport_.set_on_message_callback(base::BindRepeating(
sergeyu04e77842016-10-31 19:28:46585 &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 Ulanov137ebe102022-01-26 18:36:18594 EXPECT_EQ("1", client_transport_.received_messages()[0]->Attr(kQNameId));
sergeyu04e77842016-10-31 19:28:46595}
596
zijiehecf233ee2016-12-29 23:43:28597TEST_F(JingleSessionTest, TestSessionPlugin) {
598 host_plugin_.Clear();
599 client_plugin_.Clear();
sergeyu65aaa852017-04-11 23:26:20600
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));
zijiehecf233ee2016-12-29 23:43:28607 ExpectPluginMessagesEqual();
608}
609
610TEST_F(JingleSessionTest, SessionPluginShouldNotBeInvolvedInSessionTerminate) {
611 host_plugin_.Clear();
612 client_plugin_.Clear();
sergeyu65aaa852017-04-11 23:26:20613 CreateSessionManagers(FakeAuthenticator::Config(FakeAuthenticator::REJECT));
614 InitiateConnection(FakeAuthenticator::Config(), true);
zijiehecf233ee2016-12-29 23:43:28615 // 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
622TEST_F(JingleSessionTest, ImmediatelyCloseSessionAfterConnect) {
sergeyu65aaa852017-04-11 23:26:20623 const int kAuthRoundtrips = 3;
624 FakeAuthenticator::Config auth_config(kAuthRoundtrips,
625 FakeAuthenticator::ACCEPT, true);
626 CreateSessionManagers(auth_config);
sergeyu9fe3bc82017-04-11 19:57:12627 client_session_ = client_server_->Connect(
sergeyu65aaa852017-04-11 23:26:20628 SignalingAddress(kNormalizedHostJid),
Jinho Bang138fde32018-01-18 23:13:42629 std::make_unique<FakeAuthenticator>(
sergeyu65aaa852017-04-11 23:26:20630 FakeAuthenticator::CLIENT, auth_config,
631 client_signal_strategy_->GetLocalAddress().id(), kNormalizedHostJid));
632
zijiehecf233ee2016-12-29 23:43:28633 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]1bc9c7c2011-12-14 00:13:39640} // namespace protocol
641} // namespace remoting