| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ice_transport.h" | 5 #include "remoting/protocol/ice_transport.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "jingle/glue/thread_wrapper.h" | 16 #include "jingle/glue/thread_wrapper.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 18 #include "remoting/protocol/chromium_port_allocator.h" | 18 #include "remoting/protocol/chromium_port_allocator.h" |
| 19 #include "remoting/protocol/connection_tester.h" | 19 #include "remoting/protocol/connection_tester.h" |
| 20 #include "remoting/protocol/fake_authenticator.h" | 20 #include "remoting/protocol/fake_authenticator.h" |
| 21 #include "remoting/protocol/p2p_stream_socket.h" | 21 #include "remoting/protocol/message_channel_factory.h" |
| 22 #include "remoting/protocol/stream_channel_factory.h" | 22 #include "remoting/protocol/message_pipe.h" |
| 23 #include "remoting/protocol/transport_context.h" | 23 #include "remoting/protocol/transport_context.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 26 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 27 | 27 |
| 28 using testing::_; | 28 using testing::_; |
| 29 | 29 |
| 30 namespace remoting { | 30 namespace remoting { |
| 31 namespace protocol { | 31 namespace protocol { |
| 32 | 32 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 44 | 44 |
| 45 ACTION_P2(QuitRunLoopOnCounter, run_loop, counter) { | 45 ACTION_P2(QuitRunLoopOnCounter, run_loop, counter) { |
| 46 --(*counter); | 46 --(*counter); |
| 47 EXPECT_GE(*counter, 0); | 47 EXPECT_GE(*counter, 0); |
| 48 if (*counter == 0) | 48 if (*counter == 0) |
| 49 run_loop->Quit(); | 49 run_loop->Quit(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 class MockChannelCreatedCallback { | 52 class MockChannelCreatedCallback { |
| 53 public: | 53 public: |
| 54 MOCK_METHOD1(OnDone, void(P2PStreamSocket* socket)); | 54 MOCK_METHOD1(OnDone, void(MessagePipe* socket)); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 class TestTransportEventHandler : public IceTransport::EventHandler { | 57 class TestTransportEventHandler : public IceTransport::EventHandler { |
| 58 public: | 58 public: |
| 59 typedef base::Callback<void(ErrorCode error)> ErrorCallback; | 59 typedef base::Callback<void(ErrorCode error)> ErrorCallback; |
| 60 | 60 |
| 61 TestTransportEventHandler() {} | 61 TestTransportEventHandler() {} |
| 62 ~TestTransportEventHandler() {} | 62 ~TestTransportEventHandler() {} |
| 63 | 63 |
| 64 void set_error_callback(const ErrorCallback& callback) { | 64 void set_error_callback(const ErrorCallback& callback) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 82 | 82 |
| 83 class IceTransportTest : public testing::Test { | 83 class IceTransportTest : public testing::Test { |
| 84 public: | 84 public: |
| 85 IceTransportTest() { | 85 IceTransportTest() { |
| 86 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 86 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 87 network_settings_ = | 87 network_settings_ = |
| 88 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 88 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void TearDown() override { | 91 void TearDown() override { |
| 92 client_socket_.reset(); | 92 client_message_pipe_.reset(); |
| 93 host_socket_.reset(); | 93 host_message_pipe_.reset(); |
| 94 client_transport_.reset(); | 94 client_transport_.reset(); |
| 95 host_transport_.reset(); | 95 host_transport_.reset(); |
| 96 message_loop_.RunUntilIdle(); | 96 message_loop_.RunUntilIdle(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void ProcessTransportInfo(scoped_ptr<IceTransport>* target_transport, | 99 void ProcessTransportInfo(scoped_ptr<IceTransport>* target_transport, |
| 100 scoped_ptr<buzz::XmlElement> transport_info) { | 100 scoped_ptr<buzz::XmlElement> transport_info) { |
| 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 102 FROM_HERE, base::Bind(&IceTransportTest::DeliverTransportInfo, | 102 FROM_HERE, base::Bind(&IceTransportTest::DeliverTransportInfo, |
| 103 base::Unretained(this), target_transport, | 103 base::Unretained(this), target_transport, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 run_loop_.reset(new base::RunLoop()); | 149 run_loop_.reset(new base::RunLoop()); |
| 150 | 150 |
| 151 int counter = 2; | 151 int counter = 2; |
| 152 EXPECT_CALL(client_channel_callback_, OnDone(_)) | 152 EXPECT_CALL(client_channel_callback_, OnDone(_)) |
| 153 .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter)); | 153 .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter)); |
| 154 EXPECT_CALL(host_channel_callback_, OnDone(_)) | 154 EXPECT_CALL(host_channel_callback_, OnDone(_)) |
| 155 .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter)); | 155 .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter)); |
| 156 | 156 |
| 157 run_loop_->Run(); | 157 run_loop_->Run(); |
| 158 | 158 |
| 159 EXPECT_TRUE(client_socket_.get()); | 159 EXPECT_TRUE(client_message_pipe_.get()); |
| 160 EXPECT_TRUE(host_socket_.get()); | 160 EXPECT_TRUE(host_message_pipe_.get()); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void OnClientChannelCreated(scoped_ptr<P2PStreamSocket> socket) { | 163 void OnClientChannelCreated(scoped_ptr<MessagePipe> message_pipe) { |
| 164 client_socket_ = std::move(socket); | 164 client_message_pipe_ = std::move(message_pipe); |
| 165 client_channel_callback_.OnDone(client_socket_.get()); | 165 client_channel_callback_.OnDone(client_message_pipe_.get()); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void OnHostChannelCreated(scoped_ptr<P2PStreamSocket> socket) { | 168 void OnHostChannelCreated(scoped_ptr<MessagePipe> message_pipe) { |
| 169 host_socket_ = std::move(socket); | 169 host_message_pipe_ = std::move(message_pipe); |
| 170 host_channel_callback_.OnDone(host_socket_.get()); | 170 host_channel_callback_.OnDone(host_message_pipe_.get()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void OnTransportError(ErrorCode error) { | 173 void OnTransportError(ErrorCode error) { |
| 174 LOG(ERROR) << "Transport Error"; |
| 174 error_ = error; | 175 error_ = error; |
| 175 run_loop_->Quit(); | 176 run_loop_->Quit(); |
| 176 } | 177 } |
| 177 | 178 |
| 178 protected: | 179 protected: |
| 179 base::MessageLoopForIO message_loop_; | 180 base::MessageLoopForIO message_loop_; |
| 180 scoped_ptr<base::RunLoop> run_loop_; | 181 scoped_ptr<base::RunLoop> run_loop_; |
| 181 | 182 |
| 182 NetworkSettings network_settings_; | 183 NetworkSettings network_settings_; |
| 183 | 184 |
| 184 base::TimeDelta transport_info_delay_; | 185 base::TimeDelta transport_info_delay_; |
| 185 | 186 |
| 186 scoped_ptr<IceTransport> host_transport_; | 187 scoped_ptr<IceTransport> host_transport_; |
| 187 TestTransportEventHandler host_event_handler_; | 188 TestTransportEventHandler host_event_handler_; |
| 188 scoped_ptr<FakeAuthenticator> host_authenticator_; | 189 scoped_ptr<FakeAuthenticator> host_authenticator_; |
| 189 | 190 |
| 190 scoped_ptr<IceTransport> client_transport_; | 191 scoped_ptr<IceTransport> client_transport_; |
| 191 TestTransportEventHandler client_event_handler_; | 192 TestTransportEventHandler client_event_handler_; |
| 192 scoped_ptr<FakeAuthenticator> client_authenticator_; | 193 scoped_ptr<FakeAuthenticator> client_authenticator_; |
| 193 | 194 |
| 194 MockChannelCreatedCallback client_channel_callback_; | 195 MockChannelCreatedCallback client_channel_callback_; |
| 195 MockChannelCreatedCallback host_channel_callback_; | 196 MockChannelCreatedCallback host_channel_callback_; |
| 196 | 197 |
| 197 scoped_ptr<P2PStreamSocket> client_socket_; | 198 scoped_ptr<MessagePipe> client_message_pipe_; |
| 198 scoped_ptr<P2PStreamSocket> host_socket_; | 199 scoped_ptr<MessagePipe> host_message_pipe_; |
| 199 | 200 |
| 200 ErrorCode error_ = OK; | 201 ErrorCode error_ = OK; |
| 201 }; | 202 }; |
| 202 | 203 |
| 203 TEST_F(IceTransportTest, DataStream) { | 204 TEST_F(IceTransportTest, DataStream) { |
| 204 InitializeConnection(); | 205 InitializeConnection(); |
| 205 | 206 |
| 206 client_transport_->GetStreamChannelFactory()->CreateChannel( | 207 client_transport_->GetChannelFactory()->CreateChannel( |
| 207 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, | 208 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 208 base::Unretained(this))); | 209 base::Unretained(this))); |
| 209 host_transport_->GetStreamChannelFactory()->CreateChannel( | 210 host_transport_->GetChannelFactory()->CreateChannel( |
| 210 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, | 211 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 211 base::Unretained(this))); | 212 base::Unretained(this))); |
| 212 | 213 |
| 213 WaitUntilConnected(); | 214 WaitUntilConnected(); |
| 214 | 215 |
| 215 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 216 MessagePipeConnectionTester tester(host_message_pipe_.get(), |
| 216 kMessageSize, kMessages); | 217 client_message_pipe_.get(), kMessageSize, |
| 217 tester.Start(); | 218 kMessages); |
| 218 message_loop_.Run(); | 219 tester.RunAndCheckResults(); |
| 219 tester.CheckResults(); | |
| 220 } | 220 } |
| 221 | 221 |
| 222 TEST_F(IceTransportTest, MuxDataStream) { | 222 TEST_F(IceTransportTest, MuxDataStream) { |
| 223 InitializeConnection(); | 223 InitializeConnection(); |
| 224 | 224 |
| 225 client_transport_->GetMultiplexedChannelFactory()->CreateChannel( | 225 client_transport_->GetMultiplexedChannelFactory()->CreateChannel( |
| 226 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, | 226 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 227 base::Unretained(this))); | 227 base::Unretained(this))); |
| 228 host_transport_->GetMultiplexedChannelFactory()->CreateChannel( | 228 host_transport_->GetMultiplexedChannelFactory()->CreateChannel( |
| 229 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, | 229 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 230 base::Unretained(this))); | 230 base::Unretained(this))); |
| 231 | 231 |
| 232 WaitUntilConnected(); | 232 WaitUntilConnected(); |
| 233 | 233 |
| 234 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 234 MessagePipeConnectionTester tester(host_message_pipe_.get(), |
| 235 kMessageSize, kMessages); | 235 client_message_pipe_.get(), kMessageSize, |
| 236 tester.Start(); | 236 kMessages); |
| 237 message_loop_.Run(); | 237 tester.RunAndCheckResults(); |
| 238 tester.CheckResults(); | |
| 239 } | 238 } |
| 240 | 239 |
| 241 TEST_F(IceTransportTest, FailedChannelAuth) { | 240 TEST_F(IceTransportTest, FailedChannelAuth) { |
| 242 // Use host authenticator with one that rejects channel authentication. | 241 // Use host authenticator with one that rejects channel authentication. |
| 243 host_authenticator_.reset(new FakeAuthenticator( | 242 host_authenticator_.reset(new FakeAuthenticator( |
| 244 FakeAuthenticator::HOST, 0, FakeAuthenticator::REJECT_CHANNEL, true)); | 243 FakeAuthenticator::HOST, 0, FakeAuthenticator::REJECT_CHANNEL, true)); |
| 245 | 244 |
| 246 InitializeConnection(); | 245 InitializeConnection(); |
| 247 | 246 |
| 248 client_transport_->GetStreamChannelFactory()->CreateChannel( | 247 client_transport_->GetChannelFactory()->CreateChannel( |
| 249 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, | 248 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 250 base::Unretained(this))); | 249 base::Unretained(this))); |
| 251 host_transport_->GetStreamChannelFactory()->CreateChannel( | 250 host_transport_->GetChannelFactory()->CreateChannel( |
| 252 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, | 251 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 253 base::Unretained(this))); | 252 base::Unretained(this))); |
| 254 | 253 |
| 255 run_loop_.reset(new base::RunLoop()); | 254 run_loop_.reset(new base::RunLoop()); |
| 256 | 255 |
| 257 EXPECT_CALL(host_channel_callback_, OnDone(nullptr)) | 256 // The callback should never be called. |
| 258 .WillOnce(QuitRunLoop(run_loop_.get())); | 257 EXPECT_CALL(host_channel_callback_, OnDone(_)).Times(0); |
| 259 | 258 |
| 260 run_loop_->Run(); | 259 run_loop_->Run(); |
| 261 | 260 |
| 262 EXPECT_FALSE(host_socket_); | 261 EXPECT_FALSE(host_message_pipe_); |
| 262 EXPECT_EQ(CHANNEL_CONNECTION_ERROR, error_); |
| 263 | 263 |
| 264 client_transport_->GetStreamChannelFactory()->CancelChannelCreation( | 264 client_transport_->GetChannelFactory()->CancelChannelCreation( |
| 265 kChannelName); | 265 kChannelName); |
| 266 } | 266 } |
| 267 | 267 |
| 268 // Verify that channels are never marked connected if connection cannot be | 268 // Verify that channels are never marked connected if connection cannot be |
| 269 // established. | 269 // established. |
| 270 TEST_F(IceTransportTest, TestBrokenTransport) { | 270 TEST_F(IceTransportTest, TestBrokenTransport) { |
| 271 // Allow only incoming connections on both ends, which effectively renders | 271 // Allow only incoming connections on both ends, which effectively renders |
| 272 // transport unusable. | 272 // transport unusable. |
| 273 network_settings_ = NetworkSettings(NetworkSettings::NAT_TRAVERSAL_DISABLED); | 273 network_settings_ = NetworkSettings(NetworkSettings::NAT_TRAVERSAL_DISABLED); |
| 274 | 274 |
| 275 InitializeConnection(); | 275 InitializeConnection(); |
| 276 | 276 |
| 277 client_transport_->GetStreamChannelFactory()->CreateChannel( | 277 client_transport_->GetChannelFactory()->CreateChannel( |
| 278 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, | 278 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 279 base::Unretained(this))); | 279 base::Unretained(this))); |
| 280 host_transport_->GetStreamChannelFactory()->CreateChannel( | 280 host_transport_->GetChannelFactory()->CreateChannel( |
| 281 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, | 281 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 282 base::Unretained(this))); | 282 base::Unretained(this))); |
| 283 | 283 |
| 284 message_loop_.RunUntilIdle(); | 284 message_loop_.RunUntilIdle(); |
| 285 | 285 |
| 286 // Verify that neither of the two ends of the channel is connected. | 286 // Verify that neither of the two ends of the channel is connected. |
| 287 EXPECT_FALSE(client_socket_); | 287 EXPECT_FALSE(client_message_pipe_); |
| 288 EXPECT_FALSE(host_socket_); | 288 EXPECT_FALSE(host_message_pipe_); |
| 289 | 289 |
| 290 client_transport_->GetStreamChannelFactory()->CancelChannelCreation( | 290 client_transport_->GetChannelFactory()->CancelChannelCreation( |
| 291 kChannelName); | 291 kChannelName); |
| 292 host_transport_->GetStreamChannelFactory()->CancelChannelCreation( | 292 host_transport_->GetChannelFactory()->CancelChannelCreation( |
| 293 kChannelName); | 293 kChannelName); |
| 294 } | 294 } |
| 295 | 295 |
| 296 TEST_F(IceTransportTest, TestCancelChannelCreation) { | 296 TEST_F(IceTransportTest, TestCancelChannelCreation) { |
| 297 InitializeConnection(); | 297 InitializeConnection(); |
| 298 | 298 |
| 299 client_transport_->GetStreamChannelFactory()->CreateChannel( | 299 client_transport_->GetChannelFactory()->CreateChannel( |
| 300 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, | 300 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 301 base::Unretained(this))); | 301 base::Unretained(this))); |
| 302 client_transport_->GetStreamChannelFactory()->CancelChannelCreation( | 302 client_transport_->GetChannelFactory()->CancelChannelCreation( |
| 303 kChannelName); | 303 kChannelName); |
| 304 | 304 |
| 305 EXPECT_TRUE(!client_socket_.get()); | 305 EXPECT_TRUE(!client_message_pipe_.get()); |
| 306 } | 306 } |
| 307 | 307 |
| 308 // Verify that we can still connect even when there is a delay in signaling | 308 // Verify that we can still connect even when there is a delay in signaling |
| 309 // messages delivery. | 309 // messages delivery. |
| 310 TEST_F(IceTransportTest, TestDelayedSignaling) { | 310 TEST_F(IceTransportTest, TestDelayedSignaling) { |
| 311 transport_info_delay_ = base::TimeDelta::FromMilliseconds(100); | 311 transport_info_delay_ = base::TimeDelta::FromMilliseconds(100); |
| 312 | 312 |
| 313 InitializeConnection(); | 313 InitializeConnection(); |
| 314 | 314 |
| 315 client_transport_->GetStreamChannelFactory()->CreateChannel( | 315 client_transport_->GetChannelFactory()->CreateChannel( |
| 316 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, | 316 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 317 base::Unretained(this))); | 317 base::Unretained(this))); |
| 318 host_transport_->GetStreamChannelFactory()->CreateChannel( | 318 host_transport_->GetChannelFactory()->CreateChannel( |
| 319 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, | 319 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 320 base::Unretained(this))); | 320 base::Unretained(this))); |
| 321 | 321 |
| 322 WaitUntilConnected(); | 322 WaitUntilConnected(); |
| 323 | 323 |
| 324 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 324 MessagePipeConnectionTester tester(host_message_pipe_.get(), |
| 325 kMessageSize, kMessages); | 325 client_message_pipe_.get(), kMessageSize, |
| 326 tester.Start(); | 326 kMessages); |
| 327 message_loop_.Run(); | 327 tester.RunAndCheckResults(); |
| 328 tester.CheckResults(); | |
| 329 } | 328 } |
| 330 | 329 |
| 331 | 330 |
| 332 } // namespace protocol | 331 } // namespace protocol |
| 333 } // namespace remoting | 332 } // namespace remoting |
| OLD | NEW |