Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / remoting / protocol / jingle_session_unittest.cc
blob9eb829dd93bdf978da3261c4b1ec6014bb706eef
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/protocol/jingle_session.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/time/time.h"
12 #include "jingle/glue/thread_wrapper.h"
13 #include "net/socket/socket.h"
14 #include "net/socket/stream_socket.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "remoting/base/constants.h"
17 #include "remoting/protocol/authenticator.h"
18 #include "remoting/protocol/channel_authenticator.h"
19 #include "remoting/protocol/chromium_port_allocator.h"
20 #include "remoting/protocol/connection_tester.h"
21 #include "remoting/protocol/fake_authenticator.h"
22 #include "remoting/protocol/jingle_session_manager.h"
23 #include "remoting/protocol/libjingle_transport_factory.h"
24 #include "remoting/protocol/network_settings.h"
25 #include "remoting/protocol/stream_channel_factory.h"
26 #include "remoting/signaling/fake_signal_strategy.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 using testing::_;
31 using testing::AtLeast;
32 using testing::AtMost;
33 using testing::DeleteArg;
34 using testing::DoAll;
35 using testing::InSequence;
36 using testing::Invoke;
37 using testing::InvokeWithoutArgs;
38 using testing::Return;
39 using testing::SaveArg;
40 using testing::SetArgumentPointee;
41 using testing::WithArg;
43 namespace remoting {
44 namespace protocol {
46 namespace {
48 const char kHostJid[] = "host1@gmail.com/123";
49 const char kClientJid[] = "host2@gmail.com/321";
51 // Send 100 messages 1024 bytes each. UDP messages are sent with 10ms delay
52 // between messages (about 1 second for 100 messages).
53 const int kMessageSize = 1024;
54 const int kMessages = 100;
55 const char kChannelName[] = "test_channel";
57 void QuitCurrentThread() {
58 base::MessageLoop::current()->PostTask(FROM_HERE,
59 base::MessageLoop::QuitClosure());
62 ACTION(QuitThread) {
63 QuitCurrentThread();
66 ACTION_P(QuitThreadOnCounter, counter) {
67 --(*counter);
68 EXPECT_GE(*counter, 0);
69 if (*counter == 0)
70 QuitCurrentThread();
73 class MockSessionManagerListener : public SessionManager::Listener {
74 public:
75 MOCK_METHOD0(OnSessionManagerReady, void());
76 MOCK_METHOD2(OnIncomingSession,
77 void(Session*,
78 SessionManager::IncomingSessionResponse*));
81 class MockSessionEventHandler : public Session::EventHandler {
82 public:
83 MOCK_METHOD1(OnSessionStateChange, void(Session::State));
84 MOCK_METHOD2(OnSessionRouteChange, void(const std::string& channel_name,
85 const TransportRoute& route));
88 class MockChannelCreatedCallback {
89 public:
90 MOCK_METHOD1(OnDone, void(net::StreamSocket* socket));
93 } // namespace
95 class JingleSessionTest : public testing::Test {
96 public:
97 JingleSessionTest() {
98 message_loop_.reset(new base::MessageLoopForIO());
99 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
102 // Helper method that handles OnIncomingSession().
103 void SetHostSession(Session* session) {
104 DCHECK(session);
105 host_session_.reset(session);
106 host_session_->SetEventHandler(&host_session_event_handler_);
108 session->set_config(standard_ice_ ? SessionConfig::ForTest()
109 : SessionConfig::WithLegacyIceForTest());
112 void DeleteSession() {
113 host_session_.reset();
116 void OnClientChannelCreated(scoped_ptr<net::StreamSocket> socket) {
117 client_channel_callback_.OnDone(socket.get());
118 client_socket_ = socket.Pass();
121 void OnHostChannelCreated(scoped_ptr<net::StreamSocket> socket) {
122 host_channel_callback_.OnDone(socket.get());
123 host_socket_ = socket.Pass();
126 protected:
127 void SetUp() override {}
129 void TearDown() override {
130 CloseSessions();
131 CloseSessionManager();
132 base::RunLoop().RunUntilIdle();
135 void CloseSessions() {
136 host_socket_.reset();
137 host_session_.reset();
138 client_socket_.reset();
139 client_session_.reset();
142 void CreateSessionManagers(int auth_round_trips, int messages_till_start,
143 FakeAuthenticator::Action auth_action) {
144 host_signal_strategy_.reset(new FakeSignalStrategy(kHostJid));
145 client_signal_strategy_.reset(new FakeSignalStrategy(kClientJid));
146 FakeSignalStrategy::Connect(host_signal_strategy_.get(),
147 client_signal_strategy_.get());
149 EXPECT_CALL(host_server_listener_, OnSessionManagerReady())
150 .Times(1);
152 NetworkSettings network_settings(NetworkSettings::NAT_TRAVERSAL_OUTGOING);
154 scoped_ptr<TransportFactory> host_transport(new LibjingleTransportFactory(
155 nullptr,
156 ChromiumPortAllocator::Create(nullptr, network_settings).Pass(),
157 network_settings, TransportRole::SERVER));
158 host_server_.reset(new JingleSessionManager(host_transport.Pass()));
159 host_server_->Init(host_signal_strategy_.get(), &host_server_listener_);
161 scoped_ptr<AuthenticatorFactory> factory(
162 new FakeHostAuthenticatorFactory(auth_round_trips,
163 messages_till_start, auth_action, true));
164 host_server_->set_authenticator_factory(factory.Pass());
166 EXPECT_CALL(client_server_listener_, OnSessionManagerReady())
167 .Times(1);
168 scoped_ptr<TransportFactory> client_transport(new LibjingleTransportFactory(
169 nullptr,
170 ChromiumPortAllocator::Create(nullptr, network_settings).Pass(),
171 network_settings, TransportRole::CLIENT));
172 client_server_.reset(
173 new JingleSessionManager(client_transport.Pass()));
174 client_server_->Init(client_signal_strategy_.get(),
175 &client_server_listener_);
178 void CreateSessionManagers(int auth_round_trips,
179 FakeAuthenticator::Action auth_action) {
180 CreateSessionManagers(auth_round_trips, 0, auth_action);
183 void CloseSessionManager() {
184 if (host_server_.get()) {
185 host_server_->Close();
186 host_server_.reset();
188 if (client_server_.get()) {
189 client_server_->Close();
190 client_server_.reset();
192 host_signal_strategy_.reset();
193 client_signal_strategy_.reset();
196 void InitiateConnection(int auth_round_trips,
197 FakeAuthenticator::Action auth_action,
198 bool expect_fail) {
199 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
200 .WillOnce(DoAll(
201 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
202 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT)));
205 InSequence dummy;
207 EXPECT_CALL(host_session_event_handler_,
208 OnSessionStateChange(Session::CONNECTED))
209 .Times(AtMost(1));
210 EXPECT_CALL(host_session_event_handler_,
211 OnSessionStateChange(Session::AUTHENTICATING))
212 .Times(AtMost(1));
213 if (expect_fail) {
214 EXPECT_CALL(host_session_event_handler_,
215 OnSessionStateChange(Session::FAILED))
216 .Times(1);
217 } else {
218 EXPECT_CALL(host_session_event_handler_,
219 OnSessionStateChange(Session::AUTHENTICATED))
220 .Times(1);
221 // Expect that the connection will be closed eventually.
222 EXPECT_CALL(host_session_event_handler_,
223 OnSessionStateChange(Session::CLOSED))
224 .Times(AtMost(1));
229 InSequence dummy;
231 EXPECT_CALL(client_session_event_handler_,
232 OnSessionStateChange(Session::CONNECTED))
233 .Times(AtMost(1));
234 EXPECT_CALL(client_session_event_handler_,
235 OnSessionStateChange(Session::AUTHENTICATING))
236 .Times(AtMost(1));
237 if (expect_fail) {
238 EXPECT_CALL(client_session_event_handler_,
239 OnSessionStateChange(Session::FAILED))
240 .Times(1);
241 } else {
242 EXPECT_CALL(client_session_event_handler_,
243 OnSessionStateChange(Session::AUTHENTICATED))
244 .Times(1);
245 // Expect that the connection will be closed eventually.
246 EXPECT_CALL(client_session_event_handler_,
247 OnSessionStateChange(Session::CLOSED))
248 .Times(AtMost(1));
252 scoped_ptr<Authenticator> authenticator(new FakeAuthenticator(
253 FakeAuthenticator::CLIENT, auth_round_trips, auth_action, true));
255 client_session_ = client_server_->Connect(
256 kHostJid, authenticator.Pass(),
257 CandidateSessionConfig::CreateDefault());
258 client_session_->SetEventHandler(&client_session_event_handler_);
260 base::RunLoop().RunUntilIdle();
263 void CreateChannel() {
264 client_session_->GetTransportChannelFactory()->CreateChannel(
265 kChannelName, base::Bind(&JingleSessionTest::OnClientChannelCreated,
266 base::Unretained(this)));
267 host_session_->GetTransportChannelFactory()->CreateChannel(
268 kChannelName, base::Bind(&JingleSessionTest::OnHostChannelCreated,
269 base::Unretained(this)));
271 int counter = 2;
272 ExpectRouteChange(kChannelName);
273 EXPECT_CALL(client_channel_callback_, OnDone(_))
274 .WillOnce(QuitThreadOnCounter(&counter));
275 EXPECT_CALL(host_channel_callback_, OnDone(_))
276 .WillOnce(QuitThreadOnCounter(&counter));
277 message_loop_->Run();
279 EXPECT_TRUE(client_socket_.get());
280 EXPECT_TRUE(host_socket_.get());
283 void ExpectRouteChange(const std::string& channel_name) {
284 EXPECT_CALL(host_session_event_handler_,
285 OnSessionRouteChange(channel_name, _))
286 .Times(AtLeast(1));
287 EXPECT_CALL(client_session_event_handler_,
288 OnSessionRouteChange(channel_name, _))
289 .Times(AtLeast(1));
292 scoped_ptr<base::MessageLoopForIO> message_loop_;
294 bool standard_ice_ = true;
296 scoped_ptr<FakeSignalStrategy> host_signal_strategy_;
297 scoped_ptr<FakeSignalStrategy> client_signal_strategy_;
299 scoped_ptr<JingleSessionManager> host_server_;
300 MockSessionManagerListener host_server_listener_;
301 scoped_ptr<JingleSessionManager> client_server_;
302 MockSessionManagerListener client_server_listener_;
304 scoped_ptr<Session> host_session_;
305 MockSessionEventHandler host_session_event_handler_;
306 scoped_ptr<Session> client_session_;
307 MockSessionEventHandler client_session_event_handler_;
309 MockChannelCreatedCallback client_channel_callback_;
310 MockChannelCreatedCallback host_channel_callback_;
312 scoped_ptr<net::StreamSocket> client_socket_;
313 scoped_ptr<net::StreamSocket> host_socket_;
317 // Verify that we can create and destroy session managers without a
318 // connection.
319 TEST_F(JingleSessionTest, CreateAndDestoy) {
320 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
323 // Verify that an incoming session can be rejected, and that the
324 // status of the connection is set to FAILED in this case.
325 TEST_F(JingleSessionTest, RejectConnection) {
326 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
328 // Reject incoming session.
329 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
330 .WillOnce(SetArgumentPointee<1>(protocol::SessionManager::DECLINE));
333 InSequence dummy;
334 EXPECT_CALL(client_session_event_handler_,
335 OnSessionStateChange(Session::FAILED))
336 .Times(1);
339 scoped_ptr<Authenticator> authenticator(new FakeAuthenticator(
340 FakeAuthenticator::CLIENT, 1, FakeAuthenticator::ACCEPT, true));
341 client_session_ = client_server_->Connect(
342 kHostJid, authenticator.Pass(), CandidateSessionConfig::CreateDefault());
343 client_session_->SetEventHandler(&client_session_event_handler_);
345 base::RunLoop().RunUntilIdle();
348 // Verify that we can connect two endpoints with single-step authentication.
349 TEST_F(JingleSessionTest, Connect) {
350 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
351 InitiateConnection(1, FakeAuthenticator::ACCEPT, false);
353 // Verify that the client specified correct initiator value.
354 ASSERT_GT(host_signal_strategy_->received_messages().size(), 0U);
355 const buzz::XmlElement* initiate_xml =
356 host_signal_strategy_->received_messages().front();
357 const buzz::XmlElement* jingle_element =
358 initiate_xml->FirstNamed(buzz::QName("urn:xmpp:jingle:1", "jingle"));
359 ASSERT_TRUE(jingle_element);
360 ASSERT_EQ(kClientJid,
361 jingle_element->Attr(buzz::QName(std::string(), "initiator")));
364 // Verify that we can connect two endpoints with multi-step authentication.
365 TEST_F(JingleSessionTest, ConnectWithMultistep) {
366 CreateSessionManagers(3, FakeAuthenticator::ACCEPT);
367 InitiateConnection(3, FakeAuthenticator::ACCEPT, false);
370 // Verify that connection is terminated when single-step auth fails.
371 TEST_F(JingleSessionTest, ConnectWithBadAuth) {
372 CreateSessionManagers(1, FakeAuthenticator::REJECT);
373 InitiateConnection(1, FakeAuthenticator::ACCEPT, true);
376 // Verify that connection is terminated when multi-step auth fails.
377 TEST_F(JingleSessionTest, ConnectWithBadMultistepAuth) {
378 CreateSessionManagers(3, FakeAuthenticator::REJECT);
379 InitiateConnection(3, FakeAuthenticator::ACCEPT, true);
382 // Verify that data can be sent over stream channel.
383 TEST_F(JingleSessionTest, TestStreamChannel) {
384 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
385 ASSERT_NO_FATAL_FAILURE(
386 InitiateConnection(1, FakeAuthenticator::ACCEPT, false));
388 ASSERT_NO_FATAL_FAILURE(CreateChannel());
390 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
391 kMessageSize, kMessages);
392 tester.Start();
393 message_loop_->Run();
394 tester.CheckResults();
397 // Verify that we can still connect using legacy GICE transport.
398 TEST_F(JingleSessionTest, TestLegacyIceConnection) {
399 standard_ice_ = false;
401 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
402 ASSERT_NO_FATAL_FAILURE(
403 InitiateConnection(1, FakeAuthenticator::ACCEPT, false));
405 ASSERT_NO_FATAL_FAILURE(CreateChannel());
407 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
408 kMessageSize, kMessages);
409 tester.Start();
410 message_loop_->Run();
411 tester.CheckResults();
414 TEST_F(JingleSessionTest, DeleteSessionOnIncomingConnection) {
415 CreateSessionManagers(3, FakeAuthenticator::ACCEPT);
417 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
418 .WillOnce(DoAll(
419 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
420 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT)));
422 EXPECT_CALL(host_session_event_handler_,
423 OnSessionStateChange(Session::CONNECTED))
424 .Times(AtMost(1));
426 EXPECT_CALL(host_session_event_handler_,
427 OnSessionStateChange(Session::AUTHENTICATING))
428 .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteSession));
430 scoped_ptr<Authenticator> authenticator(new FakeAuthenticator(
431 FakeAuthenticator::CLIENT, 3, FakeAuthenticator::ACCEPT, true));
433 client_session_ = client_server_->Connect(
434 kHostJid, authenticator.Pass(),
435 CandidateSessionConfig::CreateDefault());
437 base::RunLoop().RunUntilIdle();
440 TEST_F(JingleSessionTest, DeleteSessionOnAuth) {
441 // Same as the previous test, but set messages_till_started to 2 in
442 // CreateSessionManagers so that the session will goes into the
443 // AUTHENTICATING state after two message exchanges.
444 CreateSessionManagers(3, 2, FakeAuthenticator::ACCEPT);
446 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _))
447 .WillOnce(DoAll(
448 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)),
449 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT)));
451 EXPECT_CALL(host_session_event_handler_,
452 OnSessionStateChange(Session::CONNECTED))
453 .Times(AtMost(1));
455 EXPECT_CALL(host_session_event_handler_,
456 OnSessionStateChange(Session::AUTHENTICATING))
457 .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteSession));
459 scoped_ptr<Authenticator> authenticator(new FakeAuthenticator(
460 FakeAuthenticator::CLIENT, 3, FakeAuthenticator::ACCEPT, true));
462 client_session_ = client_server_->Connect(
463 kHostJid, authenticator.Pass(),
464 CandidateSessionConfig::CreateDefault());
465 base::RunLoop().RunUntilIdle();
468 // Verify that data can be sent over a multiplexed channel.
469 TEST_F(JingleSessionTest, TestMuxStreamChannel) {
470 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
471 ASSERT_NO_FATAL_FAILURE(
472 InitiateConnection(1, FakeAuthenticator::ACCEPT, false));
474 client_session_->GetMultiplexedChannelFactory()->CreateChannel(
475 kChannelName, base::Bind(&JingleSessionTest::OnClientChannelCreated,
476 base::Unretained(this)));
477 host_session_->GetMultiplexedChannelFactory()->CreateChannel(
478 kChannelName, base::Bind(&JingleSessionTest::OnHostChannelCreated,
479 base::Unretained(this)));
481 int counter = 2;
482 ExpectRouteChange("mux");
483 EXPECT_CALL(client_channel_callback_, OnDone(_))
484 .WillOnce(QuitThreadOnCounter(&counter));
485 EXPECT_CALL(host_channel_callback_, OnDone(_))
486 .WillOnce(QuitThreadOnCounter(&counter));
487 message_loop_->Run();
489 EXPECT_TRUE(client_socket_.get());
490 EXPECT_TRUE(host_socket_.get());
492 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
493 kMessageSize, kMessages);
494 tester.Start();
495 message_loop_->Run();
496 tester.CheckResults();
499 // Verify that we can connect channels with multistep auth.
500 TEST_F(JingleSessionTest, TestMultistepAuthStreamChannel) {
501 CreateSessionManagers(3, FakeAuthenticator::ACCEPT);
502 ASSERT_NO_FATAL_FAILURE(
503 InitiateConnection(3, FakeAuthenticator::ACCEPT, false));
505 ASSERT_NO_FATAL_FAILURE(CreateChannel());
507 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
508 kMessageSize, kMessages);
509 tester.Start();
510 message_loop_->Run();
511 tester.CheckResults();
514 // Verify that we shutdown properly when channel authentication fails.
515 TEST_F(JingleSessionTest, TestFailedChannelAuth) {
516 CreateSessionManagers(1, FakeAuthenticator::REJECT_CHANNEL);
517 ASSERT_NO_FATAL_FAILURE(
518 InitiateConnection(1, FakeAuthenticator::ACCEPT, false));
520 client_session_->GetTransportChannelFactory()->CreateChannel(
521 kChannelName, base::Bind(&JingleSessionTest::OnClientChannelCreated,
522 base::Unretained(this)));
523 host_session_->GetTransportChannelFactory()->CreateChannel(
524 kChannelName, base::Bind(&JingleSessionTest::OnHostChannelCreated,
525 base::Unretained(this)));
527 // Terminate the message loop when we get rejection notification
528 // from the host.
529 EXPECT_CALL(host_channel_callback_, OnDone(nullptr))
530 .WillOnce(QuitThread());
531 ExpectRouteChange(kChannelName);
533 message_loop_->Run();
535 client_session_->GetTransportChannelFactory()->CancelChannelCreation(
536 kChannelName);
538 EXPECT_TRUE(!host_socket_.get());
541 TEST_F(JingleSessionTest, TestCancelChannelCreation) {
542 CreateSessionManagers(1, FakeAuthenticator::REJECT_CHANNEL);
543 ASSERT_NO_FATAL_FAILURE(
544 InitiateConnection(1, FakeAuthenticator::ACCEPT, false));
546 client_session_->GetTransportChannelFactory()->CreateChannel(
547 kChannelName, base::Bind(&JingleSessionTest::OnClientChannelCreated,
548 base::Unretained(this)));
549 client_session_->GetTransportChannelFactory()->CancelChannelCreation(
550 kChannelName);
552 EXPECT_TRUE(!client_socket_.get());
555 // Verify that we can still connect even when there is a delay in signaling
556 // messages delivery.
557 TEST_F(JingleSessionTest, TestDelayedSignaling) {
558 CreateSessionManagers(1, FakeAuthenticator::ACCEPT);
559 ASSERT_NO_FATAL_FAILURE(
560 InitiateConnection(1, FakeAuthenticator::ACCEPT, false));
562 host_signal_strategy_->set_send_delay(
563 base::TimeDelta::FromMilliseconds(100));
565 ASSERT_NO_FATAL_FAILURE(CreateChannel());
567 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(),
568 kMessageSize, 1);
569 tester.Start();
570 message_loop_->Run();
571 tester.CheckResults();
574 } // namespace protocol
575 } // namespace remoting