| 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/webrtc_transport.h" | 5 #include "remoting/protocol/webrtc_transport.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "jingle/glue/thread_wrapper.h" | 13 #include "jingle/glue/thread_wrapper.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | 15 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "remoting/base/compound_buffer.h" | 16 #include "remoting/base/compound_buffer.h" |
| 17 #include "remoting/protocol/connection_tester.h" | 17 #include "remoting/proto/event.pb.h" |
| 18 #include "remoting/protocol/fake_authenticator.h" | 18 #include "remoting/protocol/fake_authenticator.h" |
| 19 #include "remoting/protocol/message_channel_factory.h" | 19 #include "remoting/protocol/message_channel_factory.h" |
| 20 #include "remoting/protocol/message_pipe.h" | 20 #include "remoting/protocol/message_pipe.h" |
| 21 #include "remoting/protocol/message_serialization.h" |
| 21 #include "remoting/protocol/network_settings.h" | 22 #include "remoting/protocol/network_settings.h" |
| 22 #include "remoting/protocol/transport_context.h" | 23 #include "remoting/protocol/transport_context.h" |
| 23 #include "remoting/signaling/fake_signal_strategy.h" | 24 #include "remoting/signaling/fake_signal_strategy.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 26 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 26 | 27 |
| 27 namespace remoting { | 28 namespace remoting { |
| 28 namespace protocol { | 29 namespace protocol { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 IncomingChannelCallback incoming_channel_callback_; | 91 IncomingChannelCallback incoming_channel_callback_; |
| 91 | 92 |
| 92 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); | 93 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 class TestMessagePipeEventHandler : public MessagePipe::EventHandler { | 96 class TestMessagePipeEventHandler : public MessagePipe::EventHandler { |
| 96 public: | 97 public: |
| 97 TestMessagePipeEventHandler() {} | 98 TestMessagePipeEventHandler() {} |
| 98 ~TestMessagePipeEventHandler() override {} | 99 ~TestMessagePipeEventHandler() override {} |
| 99 | 100 |
| 101 void set_open_callback(const base::Closure& callback) { |
| 102 open_callback_ = callback; |
| 103 } |
| 104 void set_message_callback(const base::Closure& callback) { |
| 105 message_callback_ = callback; |
| 106 } |
| 100 void set_closed_callback(const base::Closure& callback) { | 107 void set_closed_callback(const base::Closure& callback) { |
| 101 closed_callback_ = callback; | 108 closed_callback_ = callback; |
| 102 } | 109 } |
| 103 | 110 |
| 104 // MessagePipe::EventHandler interface. | 111 bool is_open() { return is_open_; } |
| 105 void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override { | 112 const std::list<std::unique_ptr<CompoundBuffer>>& received_messages() { |
| 106 NOTREACHED(); | 113 return received_messages_; |
| 107 } | 114 } |
| 108 | 115 |
| 116 // MessagePipe::EventHandler interface. |
| 117 void OnMessagePipeOpen() override { |
| 118 is_open_ = true; |
| 119 if (!open_callback_.is_null()) |
| 120 open_callback_.Run(); |
| 121 } |
| 122 void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override { |
| 123 received_messages_.push_back(std::move(message)); |
| 124 if (!message_callback_.is_null()) |
| 125 message_callback_.Run(); |
| 126 } |
| 109 void OnMessagePipeClosed() override { | 127 void OnMessagePipeClosed() override { |
| 110 if (!closed_callback_.is_null()) { | 128 if (!closed_callback_.is_null()) { |
| 111 closed_callback_.Run(); | 129 closed_callback_.Run(); |
| 112 } else { | 130 } else { |
| 113 FAIL() << "Channel closed unexpectedly."; | 131 FAIL() << "Channel closed unexpectedly."; |
| 114 } | 132 } |
| 115 } | 133 } |
| 116 | 134 |
| 117 private: | 135 private: |
| 136 bool is_open_ = false; |
| 137 base::Closure open_callback_; |
| 138 base::Closure message_callback_; |
| 118 base::Closure closed_callback_; | 139 base::Closure closed_callback_; |
| 119 | 140 |
| 141 std::list<std::unique_ptr<CompoundBuffer>> received_messages_; |
| 142 |
| 120 DISALLOW_COPY_AND_ASSIGN(TestMessagePipeEventHandler); | 143 DISALLOW_COPY_AND_ASSIGN(TestMessagePipeEventHandler); |
| 121 }; | 144 }; |
| 122 | 145 |
| 123 } // namespace | 146 } // namespace |
| 124 | 147 |
| 125 class WebrtcTransportTest : public testing::Test { | 148 class WebrtcTransportTest : public testing::Test { |
| 126 public: | 149 public: |
| 127 WebrtcTransportTest() { | 150 WebrtcTransportTest() { |
| 128 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 151 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 129 network_settings_ = | 152 network_settings_ = |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 EXPECT_EQ(OK, client_error_); | 236 EXPECT_EQ(OK, client_error_); |
| 214 EXPECT_EQ(OK, host_error_); | 237 EXPECT_EQ(OK, host_error_); |
| 215 } | 238 } |
| 216 | 239 |
| 217 void ExpectClientDataStream() { | 240 void ExpectClientDataStream() { |
| 218 client_event_handler_.set_incoming_channel_callback(base::Bind( | 241 client_event_handler_.set_incoming_channel_callback(base::Bind( |
| 219 &WebrtcTransportTest::OnIncomingChannel, base::Unretained(this))); | 242 &WebrtcTransportTest::OnIncomingChannel, base::Unretained(this))); |
| 220 } | 243 } |
| 221 | 244 |
| 222 void CreateHostDataStream() { | 245 void CreateHostDataStream() { |
| 223 host_transport_->outgoing_channel_factory()->CreateChannel( | 246 host_message_pipe_ = host_transport_->CreateOutgoingChannel(kChannelName); |
| 224 kChannelName, base::Bind(&WebrtcTransportTest::OnHostChannelCreated, | 247 host_message_pipe_->Start(&host_message_pipe_event_handler_); |
| 225 base::Unretained(this))); | 248 host_message_pipe_event_handler_.set_open_callback(base::Bind( |
| 249 &WebrtcTransportTest::OnHostChannelConnected, base::Unretained(this))); |
| 226 } | 250 } |
| 227 | 251 |
| 228 void OnIncomingChannel(const std::string& name, | 252 void OnIncomingChannel(const std::string& name, |
| 229 std::unique_ptr<MessagePipe> pipe) { | 253 std::unique_ptr<MessagePipe> pipe) { |
| 230 EXPECT_EQ(kChannelName, name); | 254 EXPECT_EQ(kChannelName, name); |
| 231 client_message_pipe_ = std::move(pipe); | 255 client_message_pipe_ = std::move(pipe); |
| 232 if (run_loop_ && host_message_pipe_) | 256 client_message_pipe_->Start(&client_message_pipe_event_handler_); |
| 257 |
| 258 if (run_loop_ && host_message_pipe_event_handler_.is_open()) |
| 233 run_loop_->Quit(); | 259 run_loop_->Quit(); |
| 234 } | 260 } |
| 235 | 261 |
| 236 void OnHostChannelCreated(std::unique_ptr<MessagePipe> pipe) { | 262 void OnHostChannelConnected() { |
| 237 host_message_pipe_ = std::move(pipe); | 263 if (run_loop_ && client_message_pipe_event_handler_.is_open()) |
| 238 if (run_loop_ && client_message_pipe_) | |
| 239 run_loop_->Quit(); | 264 run_loop_->Quit(); |
| 240 } | 265 } |
| 241 | 266 |
| 242 void OnSessionError(TransportRole role, ErrorCode error) { | 267 void OnSessionError(TransportRole role, ErrorCode error) { |
| 243 if (role == TransportRole::SERVER) { | 268 if (role == TransportRole::SERVER) { |
| 244 host_error_ = error; | 269 host_error_ = error; |
| 245 if (destroy_on_error_) { | 270 if (destroy_on_error_) { |
| 246 host_message_pipe_.reset(); | 271 host_message_pipe_.reset(); |
| 247 host_transport_.reset(); | 272 host_transport_.reset(); |
| 248 } | 273 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 276 | 301 |
| 277 std::unique_ptr<WebrtcTransport> host_transport_; | 302 std::unique_ptr<WebrtcTransport> host_transport_; |
| 278 TestTransportEventHandler host_event_handler_; | 303 TestTransportEventHandler host_event_handler_; |
| 279 std::unique_ptr<FakeAuthenticator> host_authenticator_; | 304 std::unique_ptr<FakeAuthenticator> host_authenticator_; |
| 280 | 305 |
| 281 std::unique_ptr<WebrtcTransport> client_transport_; | 306 std::unique_ptr<WebrtcTransport> client_transport_; |
| 282 TestTransportEventHandler client_event_handler_; | 307 TestTransportEventHandler client_event_handler_; |
| 283 std::unique_ptr<FakeAuthenticator> client_authenticator_; | 308 std::unique_ptr<FakeAuthenticator> client_authenticator_; |
| 284 | 309 |
| 285 std::unique_ptr<MessagePipe> client_message_pipe_; | 310 std::unique_ptr<MessagePipe> client_message_pipe_; |
| 311 TestMessagePipeEventHandler client_message_pipe_event_handler_; |
| 286 std::unique_ptr<MessagePipe> host_message_pipe_; | 312 std::unique_ptr<MessagePipe> host_message_pipe_; |
| 287 TestMessagePipeEventHandler host_message_pipe_event_handler_; | 313 TestMessagePipeEventHandler host_message_pipe_event_handler_; |
| 288 | 314 |
| 289 ErrorCode client_error_ = OK; | 315 ErrorCode client_error_ = OK; |
| 290 ErrorCode host_error_ = OK; | 316 ErrorCode host_error_ = OK; |
| 291 | 317 |
| 292 bool destroy_on_error_ = false; | 318 bool destroy_on_error_ = false; |
| 293 }; | 319 }; |
| 294 | 320 |
| 295 TEST_F(WebrtcTransportTest, Connects) { | 321 TEST_F(WebrtcTransportTest, Connects) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 317 | 343 |
| 318 InitializeConnection(); | 344 InitializeConnection(); |
| 319 StartConnection(); | 345 StartConnection(); |
| 320 | 346 |
| 321 run_loop_.reset(new base::RunLoop()); | 347 run_loop_.reset(new base::RunLoop()); |
| 322 run_loop_->Run(); | 348 run_loop_->Run(); |
| 323 | 349 |
| 324 EXPECT_TRUE(client_message_pipe_); | 350 EXPECT_TRUE(client_message_pipe_); |
| 325 EXPECT_TRUE(host_message_pipe_); | 351 EXPECT_TRUE(host_message_pipe_); |
| 326 | 352 |
| 327 const int kMessageSize = 1024; | 353 TextEvent message; |
| 328 const int kMessages = 100; | 354 message.set_text("Hello"); |
| 329 MessagePipeConnectionTester tester(host_message_pipe_.get(), | 355 host_message_pipe_->Send(&message, base::Closure()); |
| 330 client_message_pipe_.get(), kMessageSize, | 356 |
| 331 kMessages); | 357 run_loop_.reset(new base::RunLoop()); |
| 332 tester.RunAndCheckResults(); | 358 client_message_pipe_event_handler_.set_message_callback( |
| 359 base::Bind(&base::RunLoop::Quit, base::Unretained(run_loop_.get()))); |
| 360 run_loop_->Run(); |
| 361 |
| 362 ASSERT_EQ(1U, client_message_pipe_event_handler_.received_messages().size()); |
| 363 |
| 364 std::unique_ptr<TextEvent> received_message = ParseMessage<TextEvent>( |
| 365 client_message_pipe_event_handler_.received_messages().front().get()); |
| 366 EXPECT_EQ(message.text(), received_message->text()); |
| 333 } | 367 } |
| 334 | 368 |
| 335 // Verify that data streams can be created after connection has been initiated. | 369 // Verify that data streams can be created after connection has been initiated. |
| 336 TEST_F(WebrtcTransportTest, DataStreamLate) { | 370 TEST_F(WebrtcTransportTest, DataStreamLate) { |
| 337 InitializeConnection(); | 371 InitializeConnection(); |
| 338 StartConnection(); | 372 StartConnection(); |
| 339 WaitUntilConnected(); | 373 WaitUntilConnected(); |
| 340 | 374 |
| 341 ExpectClientDataStream(); | 375 ExpectClientDataStream(); |
| 342 CreateHostDataStream(); | 376 CreateHostDataStream(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 359 run_loop_.reset(new base::RunLoop()); | 393 run_loop_.reset(new base::RunLoop()); |
| 360 run_loop_->Run(); | 394 run_loop_->Run(); |
| 361 | 395 |
| 362 EXPECT_TRUE(client_message_pipe_); | 396 EXPECT_TRUE(client_message_pipe_); |
| 363 EXPECT_TRUE(host_message_pipe_); | 397 EXPECT_TRUE(host_message_pipe_); |
| 364 | 398 |
| 365 destroy_on_error_ = true; | 399 destroy_on_error_ = true; |
| 366 | 400 |
| 367 // Expect that the channel is closed on the host side once the client closes | 401 // Expect that the channel is closed on the host side once the client closes |
| 368 // the channel. | 402 // the channel. |
| 369 host_message_pipe_->Start(&host_message_pipe_event_handler_); | |
| 370 host_message_pipe_event_handler_.set_closed_callback(base::Bind( | 403 host_message_pipe_event_handler_.set_closed_callback(base::Bind( |
| 371 &WebrtcTransportTest::OnHostChannelClosed, base::Unretained(this))); | 404 &WebrtcTransportTest::OnHostChannelClosed, base::Unretained(this))); |
| 372 | 405 |
| 373 // Destroy pipe on one side of the of the connection. It should get closed on | 406 // Destroy pipe on one side of the of the connection. It should get closed on |
| 374 // the other side. | 407 // the other side. |
| 375 client_message_pipe_.reset(); | 408 client_message_pipe_.reset(); |
| 376 | 409 |
| 377 run_loop_.reset(new base::RunLoop()); | 410 run_loop_.reset(new base::RunLoop()); |
| 378 run_loop_->Run(); | 411 run_loop_->Run(); |
| 379 | 412 |
| 380 // Check that OnHostChannelClosed() has been called. | 413 // Check that OnHostChannelClosed() has been called. |
| 381 EXPECT_EQ(OK, host_error_); | 414 EXPECT_EQ(OK, host_error_); |
| 382 EXPECT_FALSE(host_message_pipe_); | 415 EXPECT_FALSE(host_message_pipe_); |
| 383 } | 416 } |
| 384 | 417 |
| 385 } // namespace protocol | 418 } // namespace protocol |
| 386 } // namespace remoting | 419 } // namespace remoting |
| OLD | NEW |