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