| 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/protocol/connection_tester.h" | 17 #include "remoting/protocol/connection_tester.h" |
| 17 #include "remoting/protocol/fake_authenticator.h" | 18 #include "remoting/protocol/fake_authenticator.h" |
| 18 #include "remoting/protocol/message_channel_factory.h" | 19 #include "remoting/protocol/message_channel_factory.h" |
| 19 #include "remoting/protocol/message_pipe.h" | 20 #include "remoting/protocol/message_pipe.h" |
| 20 #include "remoting/protocol/network_settings.h" | 21 #include "remoting/protocol/network_settings.h" |
| 21 #include "remoting/protocol/transport_context.h" | 22 #include "remoting/protocol/transport_context.h" |
| 22 #include "remoting/signaling/fake_signal_strategy.h" | 23 #include "remoting/signaling/fake_signal_strategy.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 25 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 25 | 26 |
| 26 namespace remoting { | 27 namespace remoting { |
| 27 namespace protocol { | 28 namespace protocol { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 const char kChannelName[] = "test_channel"; | 32 const char kChannelName[] = "test_channel"; |
| 32 const char kAuthKey[] = "test_auth_key"; | 33 const char kAuthKey[] = "test_auth_key"; |
| 33 | 34 |
| 34 class TestTransportEventHandler : public WebrtcTransport::EventHandler { | 35 class TestTransportEventHandler : public WebrtcTransport::EventHandler { |
| 35 public: | 36 public: |
| 36 typedef base::Callback<void(ErrorCode error)> ErrorCallback; | 37 typedef base::Callback<void(ErrorCode error)> ErrorCallback; |
| 38 typedef base::Callback<void(const std::string& name, |
| 39 std::unique_ptr<MessagePipe> pipe)> |
| 40 IncomingChannelCallback; |
| 37 | 41 |
| 38 TestTransportEventHandler() {} | 42 TestTransportEventHandler() {} |
| 39 ~TestTransportEventHandler() {} | 43 ~TestTransportEventHandler() override {} |
| 40 | 44 |
| 41 // All callbacks must be set before the test handler is passed to a Transport | 45 // All callbacks must be set before the test handler is passed to a Transport |
| 42 // object. | 46 // object. |
| 43 void set_connecting_callback(const base::Closure& callback) { | 47 void set_connecting_callback(const base::Closure& callback) { |
| 44 connecting_callback_ = callback; | 48 connecting_callback_ = callback; |
| 45 } | 49 } |
| 46 void set_connected_callback(const base::Closure& callback) { | 50 void set_connected_callback(const base::Closure& callback) { |
| 47 connected_callback_ = callback; | 51 connected_callback_ = callback; |
| 48 } | 52 } |
| 49 void set_error_callback(const ErrorCallback& callback) { | 53 void set_error_callback(const ErrorCallback& callback) { |
| 50 error_callback_ = callback; | 54 error_callback_ = callback; |
| 51 } | 55 } |
| 56 void set_incoming_channel_callback(const IncomingChannelCallback& callback) { |
| 57 incoming_channel_callback_ = callback; |
| 58 } |
| 52 | 59 |
| 53 // WebrtcTransport::EventHandler interface. | 60 // WebrtcTransport::EventHandler interface. |
| 54 void OnWebrtcTransportConnecting() override { | 61 void OnWebrtcTransportConnecting() override { |
| 55 if (!connecting_callback_.is_null()) | 62 if (!connecting_callback_.is_null()) |
| 56 connecting_callback_.Run(); | 63 connecting_callback_.Run(); |
| 57 } | 64 } |
| 58 void OnWebrtcTransportConnected() override { | 65 void OnWebrtcTransportConnected() override { |
| 59 if (!connected_callback_.is_null()) | 66 if (!connected_callback_.is_null()) |
| 60 connected_callback_.Run(); | 67 connected_callback_.Run(); |
| 61 } | 68 } |
| 62 void OnWebrtcTransportError(ErrorCode error) override { | 69 void OnWebrtcTransportError(ErrorCode error) override { |
| 63 error_callback_.Run(error); | 70 error_callback_.Run(error); |
| 64 } | 71 } |
| 72 void OnWebrtcTransportIncomingDataChannel( |
| 73 const std::string& name, |
| 74 std::unique_ptr<MessagePipe> pipe) override { |
| 75 if (!incoming_channel_callback_.is_null()) { |
| 76 incoming_channel_callback_.Run(name, std::move(pipe)); |
| 77 } else { |
| 78 FAIL() << "Received unexpected incoming channel."; |
| 79 } |
| 80 } |
| 65 void OnWebrtcTransportMediaStreamAdded( | 81 void OnWebrtcTransportMediaStreamAdded( |
| 66 scoped_refptr<webrtc::MediaStreamInterface> stream) override {} | 82 scoped_refptr<webrtc::MediaStreamInterface> stream) override {} |
| 67 void OnWebrtcTransportMediaStreamRemoved( | 83 void OnWebrtcTransportMediaStreamRemoved( |
| 68 scoped_refptr<webrtc::MediaStreamInterface> stream) override {} | 84 scoped_refptr<webrtc::MediaStreamInterface> stream) override {} |
| 69 | 85 |
| 70 private: | 86 private: |
| 71 base::Closure connecting_callback_; | 87 base::Closure connecting_callback_; |
| 72 base::Closure connected_callback_; | 88 base::Closure connected_callback_; |
| 73 ErrorCallback error_callback_; | 89 ErrorCallback error_callback_; |
| 90 IncomingChannelCallback incoming_channel_callback_; |
| 74 | 91 |
| 75 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); | 92 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); |
| 76 }; | 93 }; |
| 77 | 94 |
| 95 class TestMessagePipeEventHandler : public MessagePipe::EventHandler { |
| 96 public: |
| 97 TestMessagePipeEventHandler() {} |
| 98 ~TestMessagePipeEventHandler() override {} |
| 99 |
| 100 void set_closed_callback(const base::Closure& callback) { |
| 101 closed_callback_ = callback; |
| 102 } |
| 103 |
| 104 // MessagePipe::EventHandler interface. |
| 105 void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override { |
| 106 NOTREACHED(); |
| 107 } |
| 108 |
| 109 void OnMessagePipeClosed() override { |
| 110 if (!closed_callback_.is_null()) { |
| 111 closed_callback_.Run(); |
| 112 } else { |
| 113 FAIL() << "Channel closed unexpectedly."; |
| 114 } |
| 115 } |
| 116 |
| 117 private: |
| 118 base::Closure closed_callback_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(TestMessagePipeEventHandler); |
| 121 }; |
| 122 |
| 78 } // namespace | 123 } // namespace |
| 79 | 124 |
| 80 class WebrtcTransportTest : public testing::Test { | 125 class WebrtcTransportTest : public testing::Test { |
| 81 public: | 126 public: |
| 82 WebrtcTransportTest() { | 127 WebrtcTransportTest() { |
| 83 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 128 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 84 network_settings_ = | 129 network_settings_ = |
| 85 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 130 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
| 86 } | 131 } |
| 87 | 132 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 run_loop_.reset(new base::RunLoop()); | 207 run_loop_.reset(new base::RunLoop()); |
| 163 run_loop_->Run(); | 208 run_loop_->Run(); |
| 164 | 209 |
| 165 host_event_handler_.set_connected_callback(base::Closure()); | 210 host_event_handler_.set_connected_callback(base::Closure()); |
| 166 client_event_handler_.set_connected_callback(base::Closure()); | 211 client_event_handler_.set_connected_callback(base::Closure()); |
| 167 | 212 |
| 168 EXPECT_EQ(OK, client_error_); | 213 EXPECT_EQ(OK, client_error_); |
| 169 EXPECT_EQ(OK, host_error_); | 214 EXPECT_EQ(OK, host_error_); |
| 170 } | 215 } |
| 171 | 216 |
| 172 void CreateClientDataStream() { | 217 void ExpectClientDataStream() { |
| 173 client_transport_->incoming_channel_factory()->CreateChannel( | 218 client_event_handler_.set_incoming_channel_callback(base::Bind( |
| 174 kChannelName, base::Bind(&WebrtcTransportTest::OnClientChannelCreated, | 219 &WebrtcTransportTest::OnIncomingChannel, base::Unretained(this))); |
| 175 base::Unretained(this))); | |
| 176 } | 220 } |
| 177 | 221 |
| 178 void CreateHostDataStream() { | 222 void CreateHostDataStream() { |
| 179 host_transport_->outgoing_channel_factory()->CreateChannel( | 223 host_transport_->outgoing_channel_factory()->CreateChannel( |
| 180 kChannelName, base::Bind(&WebrtcTransportTest::OnHostChannelCreated, | 224 kChannelName, base::Bind(&WebrtcTransportTest::OnHostChannelCreated, |
| 181 base::Unretained(this))); | 225 base::Unretained(this))); |
| 182 } | 226 } |
| 183 | 227 |
| 184 void OnClientChannelCreated(std::unique_ptr<MessagePipe> pipe) { | 228 void OnIncomingChannel(const std::string& name, |
| 229 std::unique_ptr<MessagePipe> pipe) { |
| 230 EXPECT_EQ(kChannelName, name); |
| 185 client_message_pipe_ = std::move(pipe); | 231 client_message_pipe_ = std::move(pipe); |
| 186 if (run_loop_ && host_message_pipe_) | 232 if (run_loop_ && host_message_pipe_) |
| 187 run_loop_->Quit(); | 233 run_loop_->Quit(); |
| 188 } | 234 } |
| 189 | 235 |
| 190 void OnHostChannelCreated(std::unique_ptr<MessagePipe> pipe) { | 236 void OnHostChannelCreated(std::unique_ptr<MessagePipe> pipe) { |
| 191 host_message_pipe_ = std::move(pipe); | 237 host_message_pipe_ = std::move(pipe); |
| 192 if (run_loop_ && client_message_pipe_) | 238 if (run_loop_ && client_message_pipe_) |
| 193 run_loop_->Quit(); | 239 run_loop_->Quit(); |
| 194 } | 240 } |
| 195 | 241 |
| 196 void OnSessionError(TransportRole role, ErrorCode error) { | 242 void OnSessionError(TransportRole role, ErrorCode error) { |
| 197 if (role == TransportRole::SERVER) { | 243 if (role == TransportRole::SERVER) { |
| 198 host_error_ = error; | 244 host_error_ = error; |
| 199 if (destroy_on_error_) { | 245 if (destroy_on_error_) { |
| 200 host_message_pipe_.reset(); | 246 host_message_pipe_.reset(); |
| 201 host_transport_.reset(); | 247 host_transport_.reset(); |
| 202 } | 248 } |
| 203 } else { | 249 } else { |
| 204 CHECK(role == TransportRole::CLIENT); | 250 CHECK(role == TransportRole::CLIENT); |
| 205 client_error_ = error; | 251 client_error_ = error; |
| 206 if (destroy_on_error_) { | 252 if (destroy_on_error_) { |
| 207 client_message_pipe_.reset(); | 253 client_message_pipe_.reset(); |
| 208 client_transport_.reset(); | 254 client_transport_.reset(); |
| 209 } | 255 } |
| 210 } | 256 } |
| 211 run_loop_->Quit(); | 257 run_loop_->Quit(); |
| 212 } | 258 } |
| 213 | 259 |
| 260 void OnHostChannelClosed() { |
| 261 host_message_pipe_.reset(); |
| 262 run_loop_->Quit(); |
| 263 } |
| 264 |
| 214 void QuitRunLoopOnCounter(int* counter) { | 265 void QuitRunLoopOnCounter(int* counter) { |
| 215 --(*counter); | 266 --(*counter); |
| 216 if (*counter == 0) | 267 if (*counter == 0) |
| 217 run_loop_->Quit(); | 268 run_loop_->Quit(); |
| 218 } | 269 } |
| 219 | 270 |
| 220 protected: | 271 protected: |
| 221 base::MessageLoopForIO message_loop_; | 272 base::MessageLoopForIO message_loop_; |
| 222 std::unique_ptr<base::RunLoop> run_loop_; | 273 std::unique_ptr<base::RunLoop> run_loop_; |
| 223 | 274 |
| 224 NetworkSettings network_settings_; | 275 NetworkSettings network_settings_; |
| 225 | 276 |
| 226 std::unique_ptr<WebrtcTransport> host_transport_; | 277 std::unique_ptr<WebrtcTransport> host_transport_; |
| 227 TestTransportEventHandler host_event_handler_; | 278 TestTransportEventHandler host_event_handler_; |
| 228 std::unique_ptr<FakeAuthenticator> host_authenticator_; | 279 std::unique_ptr<FakeAuthenticator> host_authenticator_; |
| 229 | 280 |
| 230 std::unique_ptr<WebrtcTransport> client_transport_; | 281 std::unique_ptr<WebrtcTransport> client_transport_; |
| 231 TestTransportEventHandler client_event_handler_; | 282 TestTransportEventHandler client_event_handler_; |
| 232 std::unique_ptr<FakeAuthenticator> client_authenticator_; | 283 std::unique_ptr<FakeAuthenticator> client_authenticator_; |
| 233 | 284 |
| 234 std::unique_ptr<MessagePipe> client_message_pipe_; | 285 std::unique_ptr<MessagePipe> client_message_pipe_; |
| 235 std::unique_ptr<MessagePipe> host_message_pipe_; | 286 std::unique_ptr<MessagePipe> host_message_pipe_; |
| 287 TestMessagePipeEventHandler host_message_pipe_event_handler_; |
| 236 | 288 |
| 237 ErrorCode client_error_ = OK; | 289 ErrorCode client_error_ = OK; |
| 238 ErrorCode host_error_ = OK; | 290 ErrorCode host_error_ = OK; |
| 239 | 291 |
| 240 bool destroy_on_error_ = false; | 292 bool destroy_on_error_ = false; |
| 241 }; | 293 }; |
| 242 | 294 |
| 243 TEST_F(WebrtcTransportTest, Connects) { | 295 TEST_F(WebrtcTransportTest, Connects) { |
| 244 InitializeConnection(); | 296 InitializeConnection(); |
| 245 StartConnection(); | 297 StartConnection(); |
| 246 WaitUntilConnected(); | 298 WaitUntilConnected(); |
| 247 } | 299 } |
| 248 | 300 |
| 249 TEST_F(WebrtcTransportTest, InvalidAuthKey) { | 301 TEST_F(WebrtcTransportTest, InvalidAuthKey) { |
| 250 InitializeConnection(); | 302 InitializeConnection(); |
| 251 client_authenticator_->set_auth_key("Incorrect Key"); | 303 client_authenticator_->set_auth_key("Incorrect Key"); |
| 252 StartConnection(); | 304 StartConnection(); |
| 253 | 305 |
| 254 run_loop_.reset(new base::RunLoop()); | 306 run_loop_.reset(new base::RunLoop()); |
| 255 run_loop_->Run(); | 307 run_loop_->Run(); |
| 256 | 308 |
| 257 EXPECT_EQ(AUTHENTICATION_FAILED, client_error_); | 309 EXPECT_EQ(AUTHENTICATION_FAILED, client_error_); |
| 258 } | 310 } |
| 259 | 311 |
| 260 TEST_F(WebrtcTransportTest, DataStream) { | 312 TEST_F(WebrtcTransportTest, DataStream) { |
| 261 client_event_handler_.set_connecting_callback(base::Bind( | 313 client_event_handler_.set_connecting_callback(base::Bind( |
| 262 &WebrtcTransportTest::CreateClientDataStream, base::Unretained(this))); | 314 &WebrtcTransportTest::ExpectClientDataStream, base::Unretained(this))); |
| 263 host_event_handler_.set_connecting_callback(base::Bind( | 315 host_event_handler_.set_connecting_callback(base::Bind( |
| 264 &WebrtcTransportTest::CreateHostDataStream, base::Unretained(this))); | 316 &WebrtcTransportTest::CreateHostDataStream, base::Unretained(this))); |
| 265 | 317 |
| 266 InitializeConnection(); | 318 InitializeConnection(); |
| 267 StartConnection(); | 319 StartConnection(); |
| 268 | 320 |
| 269 run_loop_.reset(new base::RunLoop()); | 321 run_loop_.reset(new base::RunLoop()); |
| 270 run_loop_->Run(); | 322 run_loop_->Run(); |
| 271 | 323 |
| 272 EXPECT_TRUE(client_message_pipe_); | 324 EXPECT_TRUE(client_message_pipe_); |
| 273 EXPECT_TRUE(host_message_pipe_); | 325 EXPECT_TRUE(host_message_pipe_); |
| 274 | 326 |
| 275 const int kMessageSize = 1024; | 327 const int kMessageSize = 1024; |
| 276 const int kMessages = 100; | 328 const int kMessages = 100; |
| 277 MessagePipeConnectionTester tester(host_message_pipe_.get(), | 329 MessagePipeConnectionTester tester(host_message_pipe_.get(), |
| 278 client_message_pipe_.get(), kMessageSize, | 330 client_message_pipe_.get(), kMessageSize, |
| 279 kMessages); | 331 kMessages); |
| 280 tester.RunAndCheckResults(); | 332 tester.RunAndCheckResults(); |
| 281 } | 333 } |
| 282 | 334 |
| 283 // Verify that data streams can be created after connection has been initiated. | 335 // Verify that data streams can be created after connection has been initiated. |
| 284 TEST_F(WebrtcTransportTest, DataStreamLate) { | 336 TEST_F(WebrtcTransportTest, DataStreamLate) { |
| 285 InitializeConnection(); | 337 InitializeConnection(); |
| 286 StartConnection(); | 338 StartConnection(); |
| 287 WaitUntilConnected(); | 339 WaitUntilConnected(); |
| 288 | 340 |
| 289 CreateClientDataStream(); | 341 ExpectClientDataStream(); |
| 290 CreateHostDataStream(); | 342 CreateHostDataStream(); |
| 291 | 343 |
| 292 run_loop_.reset(new base::RunLoop()); | 344 run_loop_.reset(new base::RunLoop()); |
| 293 run_loop_->Run(); | 345 run_loop_->Run(); |
| 294 | 346 |
| 295 EXPECT_TRUE(client_message_pipe_); | 347 EXPECT_TRUE(client_message_pipe_); |
| 296 EXPECT_TRUE(host_message_pipe_); | 348 EXPECT_TRUE(host_message_pipe_); |
| 297 } | 349 } |
| 298 | 350 |
| 299 TEST_F(WebrtcTransportTest, TerminateDataChannel) { | 351 TEST_F(WebrtcTransportTest, TerminateDataChannel) { |
| 300 InitializeConnection(); | 352 InitializeConnection(); |
| 301 StartConnection(); | 353 StartConnection(); |
| 302 WaitUntilConnected(); | 354 WaitUntilConnected(); |
| 303 | 355 |
| 304 CreateClientDataStream(); | 356 ExpectClientDataStream(); |
| 305 CreateHostDataStream(); | 357 CreateHostDataStream(); |
| 306 | 358 |
| 307 run_loop_.reset(new base::RunLoop()); | 359 run_loop_.reset(new base::RunLoop()); |
| 308 run_loop_->Run(); | 360 run_loop_->Run(); |
| 309 | 361 |
| 310 EXPECT_TRUE(client_message_pipe_); | 362 EXPECT_TRUE(client_message_pipe_); |
| 311 EXPECT_TRUE(host_message_pipe_); | 363 EXPECT_TRUE(host_message_pipe_); |
| 312 | 364 |
| 313 destroy_on_error_ = true; | 365 destroy_on_error_ = true; |
| 314 | 366 |
| 367 // Expect that the channel is closed on the host side once the client closes |
| 368 // the channel. |
| 369 host_message_pipe_->Start(&host_message_pipe_event_handler_); |
| 370 host_message_pipe_event_handler_.set_closed_callback(base::Bind( |
| 371 &WebrtcTransportTest::OnHostChannelClosed, base::Unretained(this))); |
| 372 |
| 315 // Destroy pipe on one side of the of the connection. It should get closed on | 373 // Destroy pipe on one side of the of the connection. It should get closed on |
| 316 // the other side. | 374 // the other side. |
| 317 client_message_pipe_.reset(); | 375 client_message_pipe_.reset(); |
| 318 | 376 |
| 319 run_loop_.reset(new base::RunLoop()); | 377 run_loop_.reset(new base::RunLoop()); |
| 320 run_loop_->Run(); | 378 run_loop_->Run(); |
| 321 | 379 |
| 322 // Check that OnSessionError() has been called. | 380 // Check that OnHostChannelClosed() has been called. |
| 323 EXPECT_EQ(CHANNEL_CONNECTION_ERROR, host_error_); | 381 EXPECT_EQ(OK, host_error_); |
| 324 EXPECT_FALSE(host_transport_); | 382 EXPECT_FALSE(host_message_pipe_); |
| 325 } | 383 } |
| 326 | 384 |
| 327 } // namespace protocol | 385 } // namespace protocol |
| 328 } // namespace remoting | 386 } // namespace remoting |
| OLD | NEW |