| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/jingle_session.h" | 5 #include "remoting/protocol/jingle_session.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 98 |
| 99 // Helper method that handles OnIncomingSession(). | 99 // Helper method that handles OnIncomingSession(). |
| 100 void SetHostSession(Session* session) { | 100 void SetHostSession(Session* session) { |
| 101 DCHECK(session); | 101 DCHECK(session); |
| 102 host_session_.reset(session); | 102 host_session_.reset(session); |
| 103 host_session_->SetEventHandler(&host_session_event_handler_); | 103 host_session_->SetEventHandler(&host_session_event_handler_); |
| 104 | 104 |
| 105 session->set_config(SessionConfig::ForTest()); | 105 session->set_config(SessionConfig::ForTest()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void DeleteSession() { |
| 109 host_session_.reset(); |
| 110 } |
| 111 |
| 108 void OnClientChannelCreated(scoped_ptr<net::StreamSocket> socket) { | 112 void OnClientChannelCreated(scoped_ptr<net::StreamSocket> socket) { |
| 109 client_channel_callback_.OnDone(socket.get()); | 113 client_channel_callback_.OnDone(socket.get()); |
| 110 client_socket_ = socket.Pass(); | 114 client_socket_ = socket.Pass(); |
| 111 } | 115 } |
| 112 | 116 |
| 113 void OnHostChannelCreated(scoped_ptr<net::StreamSocket> socket) { | 117 void OnHostChannelCreated(scoped_ptr<net::StreamSocket> socket) { |
| 114 host_channel_callback_.OnDone(socket.get()); | 118 host_channel_callback_.OnDone(socket.get()); |
| 115 host_socket_ = socket.Pass(); | 119 host_socket_ = socket.Pass(); |
| 116 } | 120 } |
| 117 | 121 |
| 118 protected: | 122 protected: |
| 119 virtual void SetUp() { | 123 virtual void SetUp() { |
| 120 } | 124 } |
| 121 | 125 |
| 122 virtual void TearDown() { | 126 virtual void TearDown() { |
| 123 CloseSessions(); | 127 CloseSessions(); |
| 124 CloseSessionManager(); | 128 CloseSessionManager(); |
| 125 base::RunLoop().RunUntilIdle(); | 129 base::RunLoop().RunUntilIdle(); |
| 126 } | 130 } |
| 127 | 131 |
| 128 void CloseSessions() { | 132 void CloseSessions() { |
| 129 host_socket_.reset(); | 133 host_socket_.reset(); |
| 130 host_session_.reset(); | 134 host_session_.reset(); |
| 131 client_socket_.reset(); | 135 client_socket_.reset(); |
| 132 client_session_.reset(); | 136 client_session_.reset(); |
| 133 } | 137 } |
| 134 | 138 |
| 135 void CreateSessionManagers(int auth_round_trips, | 139 void CreateSessionManagers(int auth_round_trips, int messages_till_start, |
| 136 FakeAuthenticator::Action auth_action) { | 140 FakeAuthenticator::Action auth_action) { |
| 137 host_signal_strategy_.reset(new FakeSignalStrategy(kHostJid)); | 141 host_signal_strategy_.reset(new FakeSignalStrategy(kHostJid)); |
| 138 client_signal_strategy_.reset(new FakeSignalStrategy(kClientJid)); | 142 client_signal_strategy_.reset(new FakeSignalStrategy(kClientJid)); |
| 139 FakeSignalStrategy::Connect(host_signal_strategy_.get(), | 143 FakeSignalStrategy::Connect(host_signal_strategy_.get(), |
| 140 client_signal_strategy_.get()); | 144 client_signal_strategy_.get()); |
| 141 | 145 |
| 142 EXPECT_CALL(host_server_listener_, OnSessionManagerReady()) | 146 EXPECT_CALL(host_server_listener_, OnSessionManagerReady()) |
| 143 .Times(1); | 147 .Times(1); |
| 144 | 148 |
| 145 NetworkSettings network_settings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 149 NetworkSettings network_settings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
| 146 | 150 |
| 147 scoped_ptr<TransportFactory> host_transport(new LibjingleTransportFactory( | 151 scoped_ptr<TransportFactory> host_transport(new LibjingleTransportFactory( |
| 148 NULL, | 152 NULL, |
| 149 ChromiumPortAllocator::Create(NULL, network_settings) | 153 ChromiumPortAllocator::Create(NULL, network_settings) |
| 150 .PassAs<cricket::HttpPortAllocatorBase>(), | 154 .PassAs<cricket::HttpPortAllocatorBase>(), |
| 151 network_settings)); | 155 network_settings)); |
| 152 host_server_.reset(new JingleSessionManager(host_transport.Pass())); | 156 host_server_.reset(new JingleSessionManager(host_transport.Pass())); |
| 153 host_server_->Init(host_signal_strategy_.get(), &host_server_listener_); | 157 host_server_->Init(host_signal_strategy_.get(), &host_server_listener_); |
| 154 | 158 |
| 155 scoped_ptr<AuthenticatorFactory> factory( | 159 scoped_ptr<AuthenticatorFactory> factory( |
| 156 new FakeHostAuthenticatorFactory(auth_round_trips, auth_action, true)); | 160 new FakeHostAuthenticatorFactory(auth_round_trips, |
| 161 messages_till_start, auth_action, true)); |
| 157 host_server_->set_authenticator_factory(factory.Pass()); | 162 host_server_->set_authenticator_factory(factory.Pass()); |
| 158 | 163 |
| 159 EXPECT_CALL(client_server_listener_, OnSessionManagerReady()) | 164 EXPECT_CALL(client_server_listener_, OnSessionManagerReady()) |
| 160 .Times(1); | 165 .Times(1); |
| 161 scoped_ptr<TransportFactory> client_transport(new LibjingleTransportFactory( | 166 scoped_ptr<TransportFactory> client_transport(new LibjingleTransportFactory( |
| 162 NULL, | 167 NULL, |
| 163 ChromiumPortAllocator::Create(NULL, network_settings) | 168 ChromiumPortAllocator::Create(NULL, network_settings) |
| 164 .PassAs<cricket::HttpPortAllocatorBase>(), | 169 .PassAs<cricket::HttpPortAllocatorBase>(), |
| 165 network_settings)); | 170 network_settings)); |
| 166 client_server_.reset( | 171 client_server_.reset( |
| 167 new JingleSessionManager(client_transport.Pass())); | 172 new JingleSessionManager(client_transport.Pass())); |
| 168 client_server_->Init(client_signal_strategy_.get(), | 173 client_server_->Init(client_signal_strategy_.get(), |
| 169 &client_server_listener_); | 174 &client_server_listener_); |
| 170 } | 175 } |
| 171 | 176 |
| 177 void CreateSessionManagers(int auth_round_trips, |
| 178 FakeAuthenticator::Action auth_action) { |
| 179 CreateSessionManagers(auth_round_trips, 0, auth_action); |
| 180 } |
| 181 |
| 172 void CloseSessionManager() { | 182 void CloseSessionManager() { |
| 173 if (host_server_.get()) { | 183 if (host_server_.get()) { |
| 174 host_server_->Close(); | 184 host_server_->Close(); |
| 175 host_server_.reset(); | 185 host_server_.reset(); |
| 176 } | 186 } |
| 177 if (client_server_.get()) { | 187 if (client_server_.get()) { |
| 178 client_server_->Close(); | 188 client_server_->Close(); |
| 179 client_server_.reset(); | 189 client_server_.reset(); |
| 180 } | 190 } |
| 181 host_signal_strategy_.reset(); | 191 host_signal_strategy_.reset(); |
| 182 client_signal_strategy_.reset(); | 192 client_signal_strategy_.reset(); |
| 183 } | 193 } |
| 184 | 194 |
| 185 void InitiateConnection(int auth_round_trips, | 195 void InitiateConnection(int auth_round_trips, |
| 186 FakeAuthenticator::Action auth_action, | 196 FakeAuthenticator::Action auth_action, |
| 187 bool expect_fail) { | 197 bool expect_fail) { |
| 188 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) | 198 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
| 189 .WillOnce(DoAll( | 199 .WillOnce(DoAll( |
| 190 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), | 200 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), |
| 191 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT))); | 201 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT))); |
| 192 | 202 |
| 193 { | 203 { |
| 194 InSequence dummy; | 204 InSequence dummy; |
| 195 | 205 |
| 196 EXPECT_CALL(host_session_event_handler_, | 206 EXPECT_CALL(host_session_event_handler_, |
| 197 OnSessionStateChange(Session::CONNECTED)) | 207 OnSessionStateChange(Session::CONNECTED)) |
| 198 .Times(AtMost(1)); | 208 .Times(AtMost(1)); |
| 209 EXPECT_CALL(host_session_event_handler_, |
| 210 OnSessionStateChange(Session::AUTHENTICATING)) |
| 211 .Times(AtMost(1)); |
| 199 if (expect_fail) { | 212 if (expect_fail) { |
| 200 EXPECT_CALL(host_session_event_handler_, | 213 EXPECT_CALL(host_session_event_handler_, |
| 201 OnSessionStateChange(Session::FAILED)) | 214 OnSessionStateChange(Session::FAILED)) |
| 202 .Times(1); | 215 .Times(1); |
| 203 } else { | 216 } else { |
| 204 EXPECT_CALL(host_session_event_handler_, | 217 EXPECT_CALL(host_session_event_handler_, |
| 205 OnSessionStateChange(Session::AUTHENTICATED)) | 218 OnSessionStateChange(Session::AUTHENTICATED)) |
| 206 .Times(1); | 219 .Times(1); |
| 207 // Expect that the connection will be closed eventually. | 220 // Expect that the connection will be closed eventually. |
| 208 EXPECT_CALL(host_session_event_handler_, | 221 EXPECT_CALL(host_session_event_handler_, |
| 209 OnSessionStateChange(Session::CLOSED)) | 222 OnSessionStateChange(Session::CLOSED)) |
| 210 .Times(AtMost(1)); | 223 .Times(AtMost(1)); |
| 211 } | 224 } |
| 212 } | 225 } |
| 213 | 226 |
| 214 { | 227 { |
| 215 InSequence dummy; | 228 InSequence dummy; |
| 216 | 229 |
| 217 EXPECT_CALL(client_session_event_handler_, | 230 EXPECT_CALL(client_session_event_handler_, |
| 218 OnSessionStateChange(Session::CONNECTED)) | 231 OnSessionStateChange(Session::CONNECTED)) |
| 219 .Times(AtMost(1)); | 232 .Times(AtMost(1)); |
| 233 EXPECT_CALL(client_session_event_handler_, |
| 234 OnSessionStateChange(Session::AUTHENTICATING)) |
| 235 .Times(AtMost(1)); |
| 220 if (expect_fail) { | 236 if (expect_fail) { |
| 221 EXPECT_CALL(client_session_event_handler_, | 237 EXPECT_CALL(client_session_event_handler_, |
| 222 OnSessionStateChange(Session::FAILED)) | 238 OnSessionStateChange(Session::FAILED)) |
| 223 .Times(1); | 239 .Times(1); |
| 224 } else { | 240 } else { |
| 225 EXPECT_CALL(client_session_event_handler_, | 241 EXPECT_CALL(client_session_event_handler_, |
| 226 OnSessionStateChange(Session::AUTHENTICATED)) | 242 OnSessionStateChange(Session::AUTHENTICATED)) |
| 227 .Times(1); | 243 .Times(1); |
| 228 // Expect that the connection will be closed eventually. | 244 // Expect that the connection will be closed eventually. |
| 229 EXPECT_CALL(client_session_event_handler_, | 245 EXPECT_CALL(client_session_event_handler_, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 | 384 |
| 369 ASSERT_NO_FATAL_FAILURE(CreateChannel()); | 385 ASSERT_NO_FATAL_FAILURE(CreateChannel()); |
| 370 | 386 |
| 371 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 387 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 372 kMessageSize, kMessages); | 388 kMessageSize, kMessages); |
| 373 tester.Start(); | 389 tester.Start(); |
| 374 message_loop_->Run(); | 390 message_loop_->Run(); |
| 375 tester.CheckResults(); | 391 tester.CheckResults(); |
| 376 } | 392 } |
| 377 | 393 |
| 394 TEST_F(JingleSessionTest, DeleteSessionOnIncomingConnection) { |
| 395 CreateSessionManagers(3, FakeAuthenticator::ACCEPT); |
| 396 |
| 397 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
| 398 .WillOnce(DoAll( |
| 399 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), |
| 400 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT))); |
| 401 |
| 402 EXPECT_CALL(host_session_event_handler_, |
| 403 OnSessionStateChange(Session::CONNECTED)) |
| 404 .Times(AtMost(1)); |
| 405 |
| 406 EXPECT_CALL(host_session_event_handler_, |
| 407 OnSessionStateChange(Session::AUTHENTICATING)) |
| 408 .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteSession)); |
| 409 |
| 410 scoped_ptr<Authenticator> authenticator(new FakeAuthenticator( |
| 411 FakeAuthenticator::CLIENT, 3, FakeAuthenticator::ACCEPT, true)); |
| 412 |
| 413 client_session_ = client_server_->Connect( |
| 414 kHostJid, authenticator.Pass(), |
| 415 CandidateSessionConfig::CreateDefault()); |
| 416 |
| 417 base::RunLoop().RunUntilIdle(); |
| 418 } |
| 419 |
| 420 TEST_F(JingleSessionTest, DeleteSessionOnAuth) { |
| 421 // Same as the previous test, but set messages_till_started to 2 in |
| 422 // CreateSessionManagers so that the session will goes into the |
| 423 // AUTHENTICATING state after two message exchanges. |
| 424 CreateSessionManagers(3, 2, FakeAuthenticator::ACCEPT); |
| 425 |
| 426 EXPECT_CALL(host_server_listener_, OnIncomingSession(_, _)) |
| 427 .WillOnce(DoAll( |
| 428 WithArg<0>(Invoke(this, &JingleSessionTest::SetHostSession)), |
| 429 SetArgumentPointee<1>(protocol::SessionManager::ACCEPT))); |
| 430 |
| 431 EXPECT_CALL(host_session_event_handler_, |
| 432 OnSessionStateChange(Session::CONNECTED)) |
| 433 .Times(AtMost(1)); |
| 434 |
| 435 EXPECT_CALL(host_session_event_handler_, |
| 436 OnSessionStateChange(Session::AUTHENTICATING)) |
| 437 .WillOnce(InvokeWithoutArgs(this, &JingleSessionTest::DeleteSession)); |
| 438 |
| 439 scoped_ptr<Authenticator> authenticator(new FakeAuthenticator( |
| 440 FakeAuthenticator::CLIENT, 3, FakeAuthenticator::ACCEPT, true)); |
| 441 |
| 442 client_session_ = client_server_->Connect( |
| 443 kHostJid, authenticator.Pass(), |
| 444 CandidateSessionConfig::CreateDefault()); |
| 445 base::RunLoop().RunUntilIdle(); |
| 446 } |
| 447 |
| 378 // Verify that data can be sent over a multiplexed channel. | 448 // Verify that data can be sent over a multiplexed channel. |
| 379 TEST_F(JingleSessionTest, TestMuxStreamChannel) { | 449 TEST_F(JingleSessionTest, TestMuxStreamChannel) { |
| 380 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); | 450 CreateSessionManagers(1, FakeAuthenticator::ACCEPT); |
| 381 ASSERT_NO_FATAL_FAILURE( | 451 ASSERT_NO_FATAL_FAILURE( |
| 382 InitiateConnection(1, FakeAuthenticator::ACCEPT, false)); | 452 InitiateConnection(1, FakeAuthenticator::ACCEPT, false)); |
| 383 | 453 |
| 384 client_session_->GetMultiplexedChannelFactory()->CreateStreamChannel( | 454 client_session_->GetMultiplexedChannelFactory()->CreateStreamChannel( |
| 385 kChannelName, base::Bind(&JingleSessionTest::OnClientChannelCreated, | 455 kChannelName, base::Bind(&JingleSessionTest::OnClientChannelCreated, |
| 386 base::Unretained(this))); | 456 base::Unretained(this))); |
| 387 host_session_->GetMultiplexedChannelFactory()->CreateStreamChannel( | 457 host_session_->GetMultiplexedChannelFactory()->CreateStreamChannel( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 .Times(AtMost(1)); | 512 .Times(AtMost(1)); |
| 443 ExpectRouteChange(kChannelName); | 513 ExpectRouteChange(kChannelName); |
| 444 | 514 |
| 445 message_loop_->Run(); | 515 message_loop_->Run(); |
| 446 | 516 |
| 447 EXPECT_TRUE(!host_socket_.get()); | 517 EXPECT_TRUE(!host_socket_.get()); |
| 448 } | 518 } |
| 449 | 519 |
| 450 } // namespace protocol | 520 } // namespace protocol |
| 451 } // namespace remoting | 521 } // namespace remoting |
| OLD | NEW |