| 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 "jingle/glue/thread_wrapper.h" | 12 #include "jingle/glue/thread_wrapper.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "remoting/protocol/chromium_port_allocator.h" | 15 #include "remoting/protocol/chromium_port_allocator.h" |
| 16 #include "remoting/protocol/connection_tester.h" | 16 #include "remoting/protocol/connection_tester.h" |
| 17 #include "remoting/protocol/fake_authenticator.h" | 17 #include "remoting/protocol/fake_authenticator.h" |
| 18 #include "remoting/protocol/network_settings.h" | 18 #include "remoting/protocol/network_settings.h" |
| 19 #include "remoting/protocol/p2p_stream_socket.h" | 19 #include "remoting/protocol/p2p_stream_socket.h" |
| 20 #include "remoting/protocol/transport_context.h" | 20 #include "remoting/protocol/transport_context.h" |
| 21 #include "remoting/signaling/fake_signal_strategy.h" | 21 #include "remoting/signaling/fake_signal_strategy.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 23 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 24 | 24 |
| 25 namespace remoting { | 25 namespace remoting { |
| 26 namespace protocol { | 26 namespace protocol { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const char kTestJid[] = "client@gmail.com/321"; | |
| 31 const char kChannelName[] = "test_channel"; | 30 const char kChannelName[] = "test_channel"; |
| 32 | 31 |
| 33 class TestTransportEventHandler : public Transport::EventHandler { | 32 class TestTransportEventHandler : public WebrtcTransport::EventHandler { |
| 34 public: | 33 public: |
| 35 typedef base::Callback<void(scoped_ptr<buzz::XmlElement> message)> | |
| 36 TransportInfoCallback; | |
| 37 typedef base::Callback<void(ErrorCode error)> ErrorCallback; | 34 typedef base::Callback<void(ErrorCode error)> ErrorCallback; |
| 38 | 35 |
| 39 TestTransportEventHandler() {} | 36 TestTransportEventHandler() {} |
| 40 ~TestTransportEventHandler() {} | 37 ~TestTransportEventHandler() {} |
| 41 | 38 |
| 42 // Both callback must be set before the test handler is passed to a Transport | 39 // Both callbacks must be set before the test handler is passed to a Transport |
| 43 // object. | 40 // object. |
| 44 void set_transport_info_callback(const TransportInfoCallback& callback) { | |
| 45 transport_info_callback_ = callback; | |
| 46 } | |
| 47 void set_connected_callback(const base::Closure& callback) { | 41 void set_connected_callback(const base::Closure& callback) { |
| 48 connected_callback_ = callback; | 42 connected_callback_ = callback; |
| 49 } | 43 } |
| 50 void set_error_callback(const ErrorCallback& callback) { | 44 void set_error_callback(const ErrorCallback& callback) { |
| 51 error_callback_ = callback; | 45 error_callback_ = callback; |
| 52 } | 46 } |
| 53 | 47 |
| 54 // Transport::EventHandler interface. | 48 // WebrtcTransport::EventHandler interface. |
| 55 void OnOutgoingTransportInfo(scoped_ptr<buzz::XmlElement> message) override { | 49 void OnWebrtcTransportConnected() override { |
| 56 transport_info_callback_.Run(std::move(message)); | |
| 57 } | |
| 58 void OnTransportRouteChange(const std::string& channel_name, | |
| 59 const TransportRoute& route) override {} | |
| 60 void OnTransportConnected() override { | |
| 61 connected_callback_.Run(); | 50 connected_callback_.Run(); |
| 62 } | 51 } |
| 63 void OnTransportError(ErrorCode error) override { | 52 void OnWebrtcTransportError(ErrorCode error) override { |
| 64 error_callback_.Run(error); | 53 error_callback_.Run(error); |
| 65 } | 54 } |
| 66 | 55 |
| 67 private: | 56 private: |
| 68 TransportInfoCallback transport_info_callback_; | |
| 69 base::Closure connected_callback_; | 57 base::Closure connected_callback_; |
| 70 ErrorCallback error_callback_; | 58 ErrorCallback error_callback_; |
| 71 | 59 |
| 72 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); | 60 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); |
| 73 }; | 61 }; |
| 74 | 62 |
| 75 } // namespace | 63 } // namespace |
| 76 | 64 |
| 77 class WebrtcTransportTest : public testing::Test { | 65 class WebrtcTransportTest : public testing::Test { |
| 78 public: | 66 public: |
| 79 WebrtcTransportTest() { | 67 WebrtcTransportTest() { |
| 80 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 68 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 81 network_settings_ = | 69 network_settings_ = |
| 82 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); | 70 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
| 83 } | 71 } |
| 84 | 72 |
| 85 void ProcessTransportInfo(scoped_ptr<Transport>* target_transport, | 73 void ProcessTransportInfo(scoped_ptr<WebrtcTransport>* target_transport, |
| 86 scoped_ptr<buzz::XmlElement> transport_info) { | 74 scoped_ptr<buzz::XmlElement> transport_info) { |
| 87 ASSERT_TRUE(target_transport); | 75 ASSERT_TRUE(target_transport); |
| 88 EXPECT_TRUE((*target_transport) | 76 EXPECT_TRUE((*target_transport) |
| 89 ->ProcessTransportInfo(transport_info.get())); | 77 ->ProcessTransportInfo(transport_info.get())); |
| 90 } | 78 } |
| 91 | 79 |
| 92 protected: | 80 protected: |
| 93 void InitializeConnection() { | 81 void InitializeConnection() { |
| 94 signal_strategy_.reset(new FakeSignalStrategy(kTestJid)); | 82 host_transport_.reset( |
| 95 | 83 new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(), |
| 96 host_transport_factory_.reset(new WebrtcTransportFactory( | 84 TransportContext::ForTests(TransportRole::SERVER), |
| 97 jingle_glue::JingleThreadWrapper::current(), | 85 &host_event_handler_)); |
| 98 new TransportContext( | |
| 99 signal_strategy_.get(), | |
| 100 make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), | |
| 101 network_settings_, TransportRole::SERVER))); | |
| 102 host_transport_ = host_transport_factory_->CreateTransport(); | |
| 103 host_authenticator_.reset(new FakeAuthenticator( | 86 host_authenticator_.reset(new FakeAuthenticator( |
| 104 FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false)); | 87 FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false)); |
| 105 | 88 |
| 106 client_transport_factory_.reset(new WebrtcTransportFactory( | 89 client_transport_.reset( |
| 107 jingle_glue::JingleThreadWrapper::current(), | 90 new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(), |
| 108 new TransportContext( | 91 TransportContext::ForTests(TransportRole::CLIENT), |
| 109 signal_strategy_.get(), | 92 &client_event_handler_)); |
| 110 make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), | 93 client_authenticator_.reset(new FakeAuthenticator( |
| 111 network_settings_, TransportRole::CLIENT))); | |
| 112 client_transport_ = client_transport_factory_->CreateTransport(); | |
| 113 host_authenticator_.reset(new FakeAuthenticator( | |
| 114 FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false)); | 94 FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false)); |
| 115 | |
| 116 // Connect signaling between the two WebrtcTransport objects. | |
| 117 host_event_handler_.set_transport_info_callback( | |
| 118 base::Bind(&WebrtcTransportTest::ProcessTransportInfo, | |
| 119 base::Unretained(this), &client_transport_)); | |
| 120 client_event_handler_.set_transport_info_callback( | |
| 121 base::Bind(&WebrtcTransportTest::ProcessTransportInfo, | |
| 122 base::Unretained(this), &host_transport_)); | |
| 123 } | 95 } |
| 124 | 96 |
| 125 void StartConnection() { | 97 void StartConnection() { |
| 126 host_event_handler_.set_connected_callback(base::Bind(&base::DoNothing)); | 98 host_event_handler_.set_connected_callback(base::Bind(&base::DoNothing)); |
| 127 client_event_handler_.set_connected_callback(base::Bind(&base::DoNothing)); | 99 client_event_handler_.set_connected_callback(base::Bind(&base::DoNothing)); |
| 128 | 100 |
| 129 host_event_handler_.set_error_callback(base::Bind( | 101 host_event_handler_.set_error_callback(base::Bind( |
| 130 &WebrtcTransportTest::OnSessionError, base::Unretained(this))); | 102 &WebrtcTransportTest::OnSessionError, base::Unretained(this))); |
| 131 client_event_handler_.set_error_callback(base::Bind( | 103 client_event_handler_.set_error_callback(base::Bind( |
| 132 &WebrtcTransportTest::OnSessionError, base::Unretained(this))); | 104 &WebrtcTransportTest::OnSessionError, base::Unretained(this))); |
| 133 | 105 |
| 134 host_transport_->Start(&host_event_handler_, host_authenticator_.get()); | 106 // Start both transports. |
| 135 client_transport_->Start(&client_event_handler_, | 107 host_transport_->Start( |
| 136 client_authenticator_.get()); | 108 host_authenticator_.get(), |
| 109 base::Bind(&WebrtcTransportTest::ProcessTransportInfo, |
| 110 base::Unretained(this), &client_transport_)); |
| 111 client_transport_->Start( |
| 112 client_authenticator_.get(), |
| 113 base::Bind(&WebrtcTransportTest::ProcessTransportInfo, |
| 114 base::Unretained(this), &host_transport_)); |
| 137 } | 115 } |
| 138 | 116 |
| 139 void WaitUntilConnected() { | 117 void WaitUntilConnected() { |
| 140 int counter = 2; | 118 int counter = 2; |
| 141 host_event_handler_.set_connected_callback( | 119 host_event_handler_.set_connected_callback( |
| 142 base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter, | 120 base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter, |
| 143 base::Unretained(this), &counter)); | 121 base::Unretained(this), &counter)); |
| 144 client_event_handler_.set_connected_callback( | 122 client_event_handler_.set_connected_callback( |
| 145 base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter, | 123 base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter, |
| 146 base::Unretained(this), &counter)); | 124 base::Unretained(this), &counter)); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 if (*counter == 0) | 160 if (*counter == 0) |
| 183 run_loop_->Quit(); | 161 run_loop_->Quit(); |
| 184 } | 162 } |
| 185 | 163 |
| 186 protected: | 164 protected: |
| 187 base::MessageLoopForIO message_loop_; | 165 base::MessageLoopForIO message_loop_; |
| 188 scoped_ptr<base::RunLoop> run_loop_; | 166 scoped_ptr<base::RunLoop> run_loop_; |
| 189 | 167 |
| 190 NetworkSettings network_settings_; | 168 NetworkSettings network_settings_; |
| 191 | 169 |
| 192 scoped_ptr< FakeSignalStrategy> signal_strategy_; | 170 scoped_ptr<WebrtcTransport> host_transport_; |
| 193 | |
| 194 scoped_ptr<WebrtcTransportFactory> host_transport_factory_; | |
| 195 scoped_ptr<Transport> host_transport_; | |
| 196 TestTransportEventHandler host_event_handler_; | 171 TestTransportEventHandler host_event_handler_; |
| 197 scoped_ptr<FakeAuthenticator> host_authenticator_; | 172 scoped_ptr<FakeAuthenticator> host_authenticator_; |
| 198 | 173 |
| 199 scoped_ptr<WebrtcTransportFactory> client_transport_factory_; | 174 scoped_ptr<WebrtcTransport> client_transport_; |
| 200 scoped_ptr<Transport> client_transport_; | |
| 201 TestTransportEventHandler client_event_handler_; | 175 TestTransportEventHandler client_event_handler_; |
| 202 scoped_ptr<FakeAuthenticator> client_authenticator_; | 176 scoped_ptr<FakeAuthenticator> client_authenticator_; |
| 203 | 177 |
| 204 scoped_ptr<P2PStreamSocket> client_socket_; | 178 scoped_ptr<P2PStreamSocket> client_socket_; |
| 205 scoped_ptr<P2PStreamSocket> host_socket_; | 179 scoped_ptr<P2PStreamSocket> host_socket_; |
| 206 | 180 |
| 207 ErrorCode error_ = OK; | 181 ErrorCode error_ = OK; |
| 208 }; | 182 }; |
| 209 | 183 |
| 210 TEST_F(WebrtcTransportTest, Connects) { | 184 TEST_F(WebrtcTransportTest, Connects) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 225 const int kMessages = 100; | 199 const int kMessages = 100; |
| 226 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), | 200 StreamConnectionTester tester(host_socket_.get(), client_socket_.get(), |
| 227 kMessageSize, kMessages); | 201 kMessageSize, kMessages); |
| 228 tester.Start(); | 202 tester.Start(); |
| 229 message_loop_.Run(); | 203 message_loop_.Run(); |
| 230 tester.CheckResults(); | 204 tester.CheckResults(); |
| 231 } | 205 } |
| 232 | 206 |
| 233 } // namespace protocol | 207 } // namespace protocol |
| 234 } // namespace remoting | 208 } // namespace remoting |
| OLD | NEW |