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/webrtc_transport.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "jingle/glue/thread_wrapper.h" |
| 10 #include "net/url_request/url_request_context_getter.h" |
| 11 #include "remoting/protocol/chromium_port_allocator_factory.h" |
| 12 #include "remoting/protocol/fake_authenticator.h" |
| 13 #include "remoting/protocol/network_settings.h" |
| 14 #include "remoting/signaling/fake_signal_strategy.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 17 |
| 18 namespace remoting { |
| 19 namespace protocol { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kTestJid[] = "client@gmail.com/321"; |
| 24 |
| 25 class TestTransportEventHandler : public Transport::EventHandler { |
| 26 public: |
| 27 typedef base::Callback<void(scoped_ptr<buzz::XmlElement> message)> |
| 28 TransportInfoCallback; |
| 29 typedef base::Callback<void(ErrorCode error)> ErrorCallback; |
| 30 |
| 31 TestTransportEventHandler() {} |
| 32 ~TestTransportEventHandler() {} |
| 33 |
| 34 // Both callback must be set before the test handler is passed to a Transport |
| 35 // object. |
| 36 void set_transport_info_callback(const TransportInfoCallback& callback) { |
| 37 transport_info_callback_ = callback; |
| 38 } |
| 39 void set_connected_callback(const base::Closure& callback) { |
| 40 connected_callback_ = callback; |
| 41 } |
| 42 void set_error_callback(const ErrorCallback& callback) { |
| 43 error_callback_ = callback; |
| 44 } |
| 45 |
| 46 // Transport::EventHandler interface. |
| 47 void OnOutgoingTransportInfo(scoped_ptr<buzz::XmlElement> message) override { |
| 48 transport_info_callback_.Run(message.Pass()); |
| 49 } |
| 50 void OnTransportRouteChange(const std::string& channel_name, |
| 51 const TransportRoute& route) override {} |
| 52 void OnTransportConnected() override { |
| 53 connected_callback_.Run(); |
| 54 } |
| 55 void OnTransportError(ErrorCode error) override { |
| 56 error_callback_.Run(error); |
| 57 } |
| 58 |
| 59 private: |
| 60 TransportInfoCallback transport_info_callback_; |
| 61 base::Closure connected_callback_; |
| 62 ErrorCallback error_callback_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler); |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 class WebrtcTransportTest : public testing::Test { |
| 70 public: |
| 71 WebrtcTransportTest() { |
| 72 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 73 network_settings_ = |
| 74 NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); |
| 75 } |
| 76 |
| 77 void ProcessTransportInfo(scoped_ptr<Transport>* target_transport, |
| 78 scoped_ptr<buzz::XmlElement> transport_info) { |
| 79 ASSERT_TRUE(target_transport); |
| 80 EXPECT_TRUE((*target_transport) |
| 81 ->ProcessTransportInfo(transport_info.get())); |
| 82 } |
| 83 |
| 84 protected: |
| 85 void WaitUntilConnected() { |
| 86 host_event_handler_.set_error_callback(base::Bind( |
| 87 &WebrtcTransportTest::QuitRunLoopOnError, base::Unretained(this))); |
| 88 client_event_handler_.set_error_callback(base::Bind( |
| 89 &WebrtcTransportTest::QuitRunLoopOnError, base::Unretained(this))); |
| 90 |
| 91 int counter = 2; |
| 92 host_event_handler_.set_connected_callback( |
| 93 base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter, |
| 94 base::Unretained(this), &counter)); |
| 95 client_event_handler_.set_connected_callback( |
| 96 base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter, |
| 97 base::Unretained(this), &counter)); |
| 98 |
| 99 run_loop_.reset(new base::RunLoop()); |
| 100 run_loop_->Run(); |
| 101 |
| 102 EXPECT_EQ(OK, error_); |
| 103 } |
| 104 |
| 105 void QuitRunLoopOnError(ErrorCode error) { |
| 106 error_ = error; |
| 107 run_loop_->Quit(); |
| 108 } |
| 109 |
| 110 void QuitRunLoopOnCounter(int* counter) { |
| 111 --(*counter); |
| 112 if (*counter == 0) |
| 113 run_loop_->Quit(); |
| 114 } |
| 115 |
| 116 protected: |
| 117 base::MessageLoopForIO message_loop_; |
| 118 scoped_ptr<base::RunLoop> run_loop_; |
| 119 |
| 120 NetworkSettings network_settings_; |
| 121 |
| 122 scoped_ptr< FakeSignalStrategy> signal_strategy_; |
| 123 |
| 124 scoped_ptr<WebrtcTransportFactory> host_transport_factory_; |
| 125 scoped_ptr<Transport> host_transport_; |
| 126 TestTransportEventHandler host_event_handler_; |
| 127 scoped_ptr<FakeAuthenticator> host_authenticator_; |
| 128 |
| 129 scoped_ptr<WebrtcTransportFactory> client_transport_factory_; |
| 130 scoped_ptr<Transport> client_transport_; |
| 131 TestTransportEventHandler client_event_handler_; |
| 132 scoped_ptr<FakeAuthenticator> client_authenticator_; |
| 133 |
| 134 ErrorCode error_ = OK; |
| 135 }; |
| 136 |
| 137 TEST_F(WebrtcTransportTest, Connects) { |
| 138 signal_strategy_.reset(new FakeSignalStrategy(kTestJid)); |
| 139 |
| 140 host_transport_factory_.reset(new WebrtcTransportFactory( |
| 141 signal_strategy_.get(), |
| 142 ChromiumPortAllocatorFactory::Create(network_settings_, nullptr), |
| 143 TransportRole::SERVER)); |
| 144 host_transport_ = host_transport_factory_->CreateTransport(); |
| 145 host_authenticator_.reset(new FakeAuthenticator( |
| 146 FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false)); |
| 147 |
| 148 client_transport_factory_.reset(new WebrtcTransportFactory( |
| 149 signal_strategy_.get(), |
| 150 ChromiumPortAllocatorFactory::Create(network_settings_, nullptr), |
| 151 TransportRole::CLIENT)); |
| 152 client_transport_ = client_transport_factory_->CreateTransport(); |
| 153 host_authenticator_.reset(new FakeAuthenticator( |
| 154 FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false)); |
| 155 |
| 156 // Connect signaling between the two WebrtcTransport objects. |
| 157 host_event_handler_.set_transport_info_callback( |
| 158 base::Bind(&WebrtcTransportTest::ProcessTransportInfo, |
| 159 base::Unretained(this), &client_transport_)); |
| 160 client_event_handler_.set_transport_info_callback( |
| 161 base::Bind(&WebrtcTransportTest::ProcessTransportInfo, |
| 162 base::Unretained(this), &host_transport_)); |
| 163 |
| 164 host_transport_->Start(&host_event_handler_, host_authenticator_.get()); |
| 165 client_transport_->Start(&client_event_handler_, client_authenticator_.get()); |
| 166 |
| 167 WaitUntilConnected(); |
| 168 } |
| 169 |
| 170 } // namespace protocol |
| 171 } // namespace remoting |
OLD | NEW |