OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/ice_transport.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "jingle/glue/thread_wrapper.h" |
| 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "remoting/protocol/chromium_port_allocator.h" |
| 16 #include "remoting/protocol/connection_tester.h" |
| 17 #include "remoting/protocol/fake_authenticator.h" |
| 18 #include "remoting/protocol/p2p_stream_socket.h" |
| 19 #include "remoting/protocol/stream_channel_factory.h" |
| 20 #include "remoting/signaling/fake_signal_strategy.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 24 |
| 25 using testing::_; |
| 26 |
| 27 namespace remoting { |
| 28 namespace protocol { |
| 29 |
| 30 namespace { |
| 31 |
| 32 // Send 100 messages 1024 bytes each. UDP messages are sent with 10ms delay |
| 33 // between messages (about 1 second for 100 messages). |
| 34 const int kMessageSize = 1024; |
| 35 const int kMessages = 100; |
| 36 const char kChannelName[] = "test_channel"; |
| 37 |
| 38 ACTION_P(QuitRunLoop, run_loop) { |
| 39 run_loop->Quit(); |
| 40 } |
| 41 |
| 42 ACTION_P2(QuitRunLoopOnCounter, run_loop, counter) { |
| 43 --(*counter); |
| 44 EXPECT_GE(*counter, 0); |
| 45 if (*counter == 0) |
| 46 run_loop->Quit(); |
| 47 } |
| 48 |
| 49 class MockChannelCreatedCallback { |
| 50 public: |
| 51 MOCK_METHOD1(OnDone, void(P2PStreamSocket* socket)); |
| 52 }; |
| 53 |
| 54 class TestTransportEventHandler : public Transport::EventHandler { |
| 55 public: |
| 56 typedef base::Callback<void(scoped_ptr<buzz::XmlElement> message)> |
| 57 TransportInfoCallback; |
| 58 typedef base::Callback<void(ErrorCode error)> ErrorCallback; |
| 59 |
| 60 TestTransportEventHandler() {} |
| 61 ~TestTransportEventHandler() {} |
| 62 |
| 63 // Both callback must be set before the test handler is passed to a Transport |
| 64 // object. |
| 65 void set_transport_info_callback(const TransportInfoCallback& callback) { |
| 66 transport_info_callback_ = callback; |
| 67 } |
| 68 void set_connected_callback(const base::Closure& callback) { |
| 69 connected_callback_ = callback; |
| 70 } |
| 71 void set_error_callback(const ErrorCallback& callback) { |
| 72 error_callback_ = callback; |
| 73 } |
| 74 |
| 75 // Transport::EventHandler interface. |
| 76 void OnOutgoingTransportInfo(scoped_ptr<buzz::XmlElement> message) override { |
| 77 transport_info_callback_.Run(message.Pass()); |
| 78 } |
| 79 void OnTransportRouteChange(const std::string& channel_name, |
| 80 const TransportRoute& route) override {} |
| 81 void OnTransportConnected() override { |
| 82 connected_callback_.Run(); |
| 83 } |
| 84 void OnTransportError(ErrorCode error) override { |
| 85 error_callback_.Run(error); |
| 86 } |
| 87 |
| 88 private: |
| 89 TransportInfoCallback transport_info_callback_; |
| 90 base::Closure connected_callback_; |
| 91 ErrorCallback error_callback_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); |
| 94 }; |
| 95 |
| 96 } // namespace |
| 97 |
| 98 class IceTransportTest : public testing::Test { |
| 99 public: |
| 100 IceTransportTest() { |
| 101 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 102 network_settings_ = |
| 103 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
| 104 } |
| 105 |
| 106 void TearDown() override { |
| 107 client_socket_.reset(); |
| 108 host_socket_.reset(); |
| 109 client_transport_.reset(); |
| 110 host_transport_.reset(); |
| 111 message_loop_.RunUntilIdle(); |
| 112 } |
| 113 |
| 114 void ProcessTransportInfo(scoped_ptr<IceTransport>* target_transport, |
| 115 scoped_ptr<buzz::XmlElement> transport_info) { |
| 116 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 117 FROM_HERE, base::Bind(&IceTransportTest::DeliverTransportInfo, |
| 118 base::Unretained(this), target_transport, |
| 119 base::Passed(&transport_info)), |
| 120 transport_info_delay_); |
| 121 } |
| 122 |
| 123 void DeliverTransportInfo(scoped_ptr<IceTransport>* target_transport, |
| 124 scoped_ptr<buzz::XmlElement> transport_info) { |
| 125 ASSERT_TRUE(target_transport); |
| 126 EXPECT_TRUE( |
| 127 (*target_transport)->ProcessTransportInfo(transport_info.get())); |
| 128 } |
| 129 |
| 130 void InitializeConnection() { |
| 131 port_allocator_ = ChromiumPortAllocator::Create(nullptr, network_settings_); |
| 132 |
| 133 host_transport_.reset(new IceTransport( |
| 134 port_allocator_.get(), network_settings_, TransportRole::SERVER)); |
| 135 host_transport_->GetCanStartClosure().Run(); |
| 136 |
| 137 if (!host_authenticator_) { |
| 138 host_authenticator_.reset(new FakeAuthenticator( |
| 139 FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, true)); |
| 140 } |
| 141 |
| 142 client_transport_.reset(new IceTransport( |
| 143 port_allocator_.get(), network_settings_, TransportRole::CLIENT)); |
| 144 client_transport_->GetCanStartClosure().Run(); |
| 145 if (!client_authenticator_) { |
| 146 client_authenticator_.reset(new FakeAuthenticator( |
| 147 FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, true)); |
| 148 } |
| 149 |
| 150 // Connect signaling between the two IceTransport objects. |
| 151 host_event_handler_.set_transport_info_callback( |
| 152 base::Bind(&IceTransportTest::ProcessTransportInfo, |
| 153 base::Unretained(this), &client_transport_)); |
| 154 client_event_handler_.set_transport_info_callback( |
| 155 base::Bind(&IceTransportTest::ProcessTransportInfo, |
| 156 base::Unretained(this), &host_transport_)); |
| 157 |
| 158 host_event_handler_.set_connected_callback(base::Bind(&base::DoNothing)); |
| 159 host_event_handler_.set_error_callback(base::Bind( |
| 160 &IceTransportTest::OnTransportError, base::Unretained(this))); |
| 161 |
| 162 client_event_handler_.set_connected_callback(base::Bind(&base::DoNothing)); |
| 163 client_event_handler_.set_error_callback(base::Bind( |
| 164 &IceTransportTest::OnTransportError, base::Unretained(this))); |
| 165 |
| 166 host_transport_->Start(&host_event_handler_, host_authenticator_.get()); |
| 167 client_transport_->Start(&client_event_handler_, |
| 168 client_authenticator_.get()); |
| 169 } |
| 170 |
| 171 void WaitUntilConnected() { |
| 172 run_loop_.reset(new base::RunLoop()); |
| 173 |
| 174 int counter = 2; |
| 175 EXPECT_CALL(client_channel_callback_, OnDone(_)) |
| 176 .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter)); |
| 177 EXPECT_CALL(host_channel_callback_, OnDone(_)) |
| 178 .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter)); |
| 179 |
| 180 run_loop_->Run(); |
| 181 |
| 182 EXPECT_TRUE(client_socket_.get()); |
| 183 EXPECT_TRUE(host_socket_.get()); |
| 184 } |
| 185 |
| 186 void OnClientChannelCreated(scoped_ptr<P2PStreamSocket> socket) { |
| 187 client_socket_ = socket.Pass(); |
| 188 client_channel_callback_.OnDone(client_socket_.get()); |
| 189 } |
| 190 |
| 191 void OnHostChannelCreated(scoped_ptr<P2PStreamSocket> socket) { |
| 192 host_socket_ = socket.Pass(); |
| 193 host_channel_callback_.OnDone(host_socket_.get()); |
| 194 } |
| 195 |
| 196 void OnTransportError(ErrorCode error) { |
| 197 error_ = error; |
| 198 run_loop_->Quit(); |
| 199 } |
| 200 |
| 201 protected: |
| 202 base::MessageLoopForIO message_loop_; |
| 203 scoped_ptr<base::RunLoop> run_loop_; |
| 204 |
| 205 NetworkSettings network_settings_; |
| 206 |
| 207 scoped_ptr<FakeSignalStrategy> signal_strategy_; |
| 208 |
| 209 base::TimeDelta transport_info_delay_; |
| 210 |
| 211 scoped_ptr<cricket::PortAllocator> port_allocator_; |
| 212 |
| 213 // scoped_ptr<IceTransportFactory> host_transport_factory_; |
| 214 scoped_ptr<IceTransport> host_transport_; |
| 215 TestTransportEventHandler host_event_handler_; |
| 216 scoped_ptr<FakeAuthenticator> host_authenticator_; |
| 217 |
| 218 // scoped_ptr<IceTransportFactory> client_transport_factory_; |
| 219 scoped_ptr<IceTransport> client_transport_; |
| 220 TestTransportEventHandler client_event_handler_; |
| 221 scoped_ptr<FakeAuthenticator> client_authenticator_; |
| 222 |
| 223 MockChannelCreatedCallback client_channel_callback_; |
| 224 MockChannelCreatedCallback host_channel_callback_; |
| 225 |
| 226 scoped_ptr<P2PStreamSocket> client_socket_; |
| 227 scoped_ptr<P2PStreamSocket> host_socket_; |
| 228 |
| 229 ErrorCode error_ = OK; |
| 230 }; |
| 231 |
| 232 TEST_F(IceTransportTest, DataStream) { |
| 233 InitializeConnection(); |
| 234 |
| 235 client_transport_->GetStreamChannelFactory()->CreateChannel( |
| 236 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 237 base::Unretained(this))); |
| 238 host_transport_->GetStreamChannelFactory()->CreateChannel( |
| 239 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 240 base::Unretained(this))); |
| 241 |
| 242 WaitUntilConnected(); |
| 243 |
| 244 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 245 kMessageSize, kMessages); |
| 246 tester.Start(); |
| 247 message_loop_.Run(); |
| 248 tester.CheckResults(); |
| 249 } |
| 250 |
| 251 TEST_F(IceTransportTest, MuxDataStream) { |
| 252 InitializeConnection(); |
| 253 |
| 254 client_transport_->GetMultiplexedChannelFactory()->CreateChannel( |
| 255 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 256 base::Unretained(this))); |
| 257 host_transport_->GetMultiplexedChannelFactory()->CreateChannel( |
| 258 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 259 base::Unretained(this))); |
| 260 |
| 261 WaitUntilConnected(); |
| 262 |
| 263 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 264 kMessageSize, kMessages); |
| 265 tester.Start(); |
| 266 message_loop_.Run(); |
| 267 tester.CheckResults(); |
| 268 } |
| 269 |
| 270 TEST_F(IceTransportTest, FailedChannelAuth) { |
| 271 // Use host authenticator with one that rejects channel authentication. |
| 272 host_authenticator_.reset(new FakeAuthenticator( |
| 273 FakeAuthenticator::HOST, 0, FakeAuthenticator::REJECT_CHANNEL, true)); |
| 274 |
| 275 InitializeConnection(); |
| 276 |
| 277 client_transport_->GetStreamChannelFactory()->CreateChannel( |
| 278 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 279 base::Unretained(this))); |
| 280 host_transport_->GetStreamChannelFactory()->CreateChannel( |
| 281 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 282 base::Unretained(this))); |
| 283 |
| 284 run_loop_.reset(new base::RunLoop()); |
| 285 |
| 286 EXPECT_CALL(host_channel_callback_, OnDone(nullptr)) |
| 287 .WillOnce(QuitRunLoop(run_loop_.get())); |
| 288 |
| 289 run_loop_->Run(); |
| 290 |
| 291 EXPECT_FALSE(host_socket_); |
| 292 |
| 293 client_transport_->GetStreamChannelFactory()->CancelChannelCreation( |
| 294 kChannelName); |
| 295 } |
| 296 |
| 297 // Verify that channels are never marked connected if connection cannot be |
| 298 // established. |
| 299 TEST_F(IceTransportTest, TestBrokenTransport) { |
| 300 // Allow only incoming connections on both ends, which effectively renders |
| 301 // transport unusable. |
| 302 network_settings_ = NetworkSettings(NetworkSettings::NAT_TRAVERSAL_DISABLED); |
| 303 |
| 304 InitializeConnection(); |
| 305 |
| 306 client_transport_->GetStreamChannelFactory()->CreateChannel( |
| 307 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 308 base::Unretained(this))); |
| 309 host_transport_->GetStreamChannelFactory()->CreateChannel( |
| 310 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 311 base::Unretained(this))); |
| 312 |
| 313 message_loop_.RunUntilIdle(); |
| 314 |
| 315 // Verify that neither of the two ends of the channel is connected. |
| 316 EXPECT_FALSE(client_socket_); |
| 317 EXPECT_FALSE(host_socket_); |
| 318 |
| 319 client_transport_->GetStreamChannelFactory()->CancelChannelCreation( |
| 320 kChannelName); |
| 321 host_transport_->GetStreamChannelFactory()->CancelChannelCreation( |
| 322 kChannelName); |
| 323 } |
| 324 |
| 325 TEST_F(IceTransportTest, TestCancelChannelCreation) { |
| 326 InitializeConnection(); |
| 327 |
| 328 client_transport_->GetStreamChannelFactory()->CreateChannel( |
| 329 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 330 base::Unretained(this))); |
| 331 client_transport_->GetStreamChannelFactory()->CancelChannelCreation( |
| 332 kChannelName); |
| 333 |
| 334 EXPECT_TRUE(!client_socket_.get()); |
| 335 } |
| 336 |
| 337 // Verify that we can still connect even when there is a delay in signaling |
| 338 // messages delivery. |
| 339 TEST_F(IceTransportTest, TestDelayedSignaling) { |
| 340 transport_info_delay_ = base::TimeDelta::FromMilliseconds(100); |
| 341 |
| 342 InitializeConnection(); |
| 343 |
| 344 client_transport_->GetStreamChannelFactory()->CreateChannel( |
| 345 kChannelName, base::Bind(&IceTransportTest::OnClientChannelCreated, |
| 346 base::Unretained(this))); |
| 347 host_transport_->GetStreamChannelFactory()->CreateChannel( |
| 348 kChannelName, base::Bind(&IceTransportTest::OnHostChannelCreated, |
| 349 base::Unretained(this))); |
| 350 |
| 351 WaitUntilConnected(); |
| 352 |
| 353 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 354 kMessageSize, kMessages); |
| 355 tester.Start(); |
| 356 message_loop_.Run(); |
| 357 tester.CheckResults(); |
| 358 } |
| 359 |
| 360 |
| 361 } // namespace protocol |
| 362 } // namespace remoting |
OLD | NEW |