PageRenderTime 87ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/remoting/protocol/webrtc_transport_unittest.cc

https://gitlab.com/0072016/Facebook-SDK-
C++ | 328 lines | 254 code | 62 blank | 12 comment | 15 complexity | ce8c76db4973c8f505fb9fe87dc1caf8 MD5 | raw file
  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. #include "remoting/protocol/webrtc_transport.h"
  5. #include <utility>
  6. #include "base/macros.h"
  7. #include "base/message_loop/message_loop.h"
  8. #include "base/run_loop.h"
  9. #include "base/strings/string_util.h"
  10. #include "jingle/glue/thread_wrapper.h"
  11. #include "net/base/io_buffer.h"
  12. #include "net/url_request/url_request_context_getter.h"
  13. #include "remoting/protocol/connection_tester.h"
  14. #include "remoting/protocol/fake_authenticator.h"
  15. #include "remoting/protocol/message_channel_factory.h"
  16. #include "remoting/protocol/message_pipe.h"
  17. #include "remoting/protocol/network_settings.h"
  18. #include "remoting/protocol/transport_context.h"
  19. #include "remoting/signaling/fake_signal_strategy.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
  22. namespace remoting {
  23. namespace protocol {
  24. namespace {
  25. const char kChannelName[] = "test_channel";
  26. const char kAuthKey[] = "test_auth_key";
  27. class TestTransportEventHandler : public WebrtcTransport::EventHandler {
  28. public:
  29. typedef base::Callback<void(ErrorCode error)> ErrorCallback;
  30. TestTransportEventHandler() {}
  31. ~TestTransportEventHandler() {}
  32. // All callbacks must be set before the test handler is passed to a Transport
  33. // object.
  34. void set_connecting_callback(const base::Closure& callback) {
  35. connecting_callback_ = callback;
  36. }
  37. void set_connected_callback(const base::Closure& callback) {
  38. connected_callback_ = callback;
  39. }
  40. void set_error_callback(const ErrorCallback& callback) {
  41. error_callback_ = callback;
  42. }
  43. // WebrtcTransport::EventHandler interface.
  44. void OnWebrtcTransportConnecting() override {
  45. if (!connecting_callback_.is_null())
  46. connecting_callback_.Run();
  47. }
  48. void OnWebrtcTransportConnected() override {
  49. if (!connected_callback_.is_null())
  50. connected_callback_.Run();
  51. }
  52. void OnWebrtcTransportError(ErrorCode error) override {
  53. error_callback_.Run(error);
  54. }
  55. void OnWebrtcTransportMediaStreamAdded(
  56. scoped_refptr<webrtc::MediaStreamInterface> stream) override {}
  57. void OnWebrtcTransportMediaStreamRemoved(
  58. scoped_refptr<webrtc::MediaStreamInterface> stream) override {}
  59. private:
  60. base::Closure connecting_callback_;
  61. base::Closure connected_callback_;
  62. ErrorCallback error_callback_;
  63. DISALLOW_COPY_AND_ASSIGN(TestTransportEventHandler);
  64. };
  65. } // namespace
  66. class WebrtcTransportTest : public testing::Test {
  67. public:
  68. WebrtcTransportTest() {
  69. jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
  70. network_settings_ =
  71. NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING);
  72. }
  73. void TearDown() override {
  74. run_loop_.reset();
  75. client_message_pipe_.reset();
  76. client_transport_.reset();
  77. host_message_pipe_.reset();
  78. host_transport_.reset();
  79. base::RunLoop().RunUntilIdle();
  80. }
  81. void ProcessTransportInfo(std::unique_ptr<WebrtcTransport>* target_transport,
  82. bool normalize_line_endings,
  83. std::unique_ptr<buzz::XmlElement> transport_info) {
  84. ASSERT_TRUE(target_transport);
  85. // Reformat the message to normalize line endings by removing CR symbol.
  86. if (normalize_line_endings) {
  87. std::string xml = transport_info->Str();
  88. base::ReplaceChars(xml, "\r", std::string(), &xml);
  89. transport_info.reset(buzz::XmlElement::ForStr(xml));
  90. }
  91. EXPECT_TRUE(
  92. (*target_transport)->ProcessTransportInfo(transport_info.get()));
  93. }
  94. void InitializeConnection() {
  95. host_transport_.reset(
  96. new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(),
  97. TransportContext::ForTests(TransportRole::SERVER),
  98. &host_event_handler_));
  99. host_authenticator_.reset(new FakeAuthenticator(
  100. FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false));
  101. host_authenticator_->set_auth_key(kAuthKey);
  102. client_transport_.reset(
  103. new WebrtcTransport(jingle_glue::JingleThreadWrapper::current(),
  104. TransportContext::ForTests(TransportRole::CLIENT),
  105. &client_event_handler_));
  106. client_authenticator_.reset(new FakeAuthenticator(
  107. FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false));
  108. client_authenticator_->set_auth_key(kAuthKey);
  109. }
  110. void StartConnection() {
  111. host_event_handler_.set_connected_callback(base::Bind(&base::DoNothing));
  112. client_event_handler_.set_connected_callback(base::Bind(&base::DoNothing));
  113. host_event_handler_.set_error_callback(
  114. base::Bind(&WebrtcTransportTest::OnSessionError, base::Unretained(this),
  115. TransportRole::SERVER));
  116. client_event_handler_.set_error_callback(
  117. base::Bind(&WebrtcTransportTest::OnSessionError, base::Unretained(this),
  118. TransportRole::CLIENT));
  119. // Start both transports.
  120. host_transport_->Start(
  121. host_authenticator_.get(),
  122. base::Bind(&WebrtcTransportTest::ProcessTransportInfo,
  123. base::Unretained(this), &client_transport_, true));
  124. client_transport_->Start(
  125. client_authenticator_.get(),
  126. base::Bind(&WebrtcTransportTest::ProcessTransportInfo,
  127. base::Unretained(this), &host_transport_, false));
  128. }
  129. void WaitUntilConnected() {
  130. int counter = 2;
  131. host_event_handler_.set_connected_callback(
  132. base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter,
  133. base::Unretained(this), &counter));
  134. client_event_handler_.set_connected_callback(
  135. base::Bind(&WebrtcTransportTest::QuitRunLoopOnCounter,
  136. base::Unretained(this), &counter));
  137. run_loop_.reset(new base::RunLoop());
  138. run_loop_->Run();
  139. host_event_handler_.set_connected_callback(base::Closure());
  140. client_event_handler_.set_connected_callback(base::Closure());
  141. EXPECT_EQ(OK, client_error_);
  142. EXPECT_EQ(OK, host_error_);
  143. }
  144. void CreateClientDataStream() {
  145. client_transport_->incoming_channel_factory()->CreateChannel(
  146. kChannelName, base::Bind(&WebrtcTransportTest::OnClientChannelCreated,
  147. base::Unretained(this)));
  148. }
  149. void CreateHostDataStream() {
  150. host_transport_->outgoing_channel_factory()->CreateChannel(
  151. kChannelName, base::Bind(&WebrtcTransportTest::OnHostChannelCreated,
  152. base::Unretained(this)));
  153. }
  154. void OnClientChannelCreated(std::unique_ptr<MessagePipe> pipe) {
  155. client_message_pipe_ = std::move(pipe);
  156. if (run_loop_ && host_message_pipe_)
  157. run_loop_->Quit();
  158. }
  159. void OnHostChannelCreated(std::unique_ptr<MessagePipe> pipe) {
  160. host_message_pipe_ = std::move(pipe);
  161. if (run_loop_ && client_message_pipe_)
  162. run_loop_->Quit();
  163. }
  164. void OnSessionError(TransportRole role, ErrorCode error) {
  165. if (role == TransportRole::SERVER) {
  166. host_error_ = error;
  167. if (destroy_on_error_) {
  168. host_message_pipe_.reset();
  169. host_transport_.reset();
  170. }
  171. } else {
  172. CHECK(role == TransportRole::CLIENT);
  173. client_error_ = error;
  174. if (destroy_on_error_) {
  175. client_message_pipe_.reset();
  176. client_transport_.reset();
  177. }
  178. }
  179. run_loop_->Quit();
  180. }
  181. void QuitRunLoopOnCounter(int* counter) {
  182. --(*counter);
  183. if (*counter == 0)
  184. run_loop_->Quit();
  185. }
  186. protected:
  187. base::MessageLoopForIO message_loop_;
  188. std::unique_ptr<base::RunLoop> run_loop_;
  189. NetworkSettings network_settings_;
  190. std::unique_ptr<WebrtcTransport> host_transport_;
  191. TestTransportEventHandler host_event_handler_;
  192. std::unique_ptr<FakeAuthenticator> host_authenticator_;
  193. std::unique_ptr<WebrtcTransport> client_transport_;
  194. TestTransportEventHandler client_event_handler_;
  195. std::unique_ptr<FakeAuthenticator> client_authenticator_;
  196. std::unique_ptr<MessagePipe> client_message_pipe_;
  197. std::unique_ptr<MessagePipe> host_message_pipe_;
  198. ErrorCode client_error_ = OK;
  199. ErrorCode host_error_ = OK;
  200. bool destroy_on_error_ = false;
  201. };
  202. TEST_F(WebrtcTransportTest, Connects) {
  203. InitializeConnection();
  204. StartConnection();
  205. WaitUntilConnected();
  206. }
  207. TEST_F(WebrtcTransportTest, InvalidAuthKey) {
  208. InitializeConnection();
  209. client_authenticator_->set_auth_key("Incorrect Key");
  210. StartConnection();
  211. run_loop_.reset(new base::RunLoop());
  212. run_loop_->Run();
  213. EXPECT_EQ(AUTHENTICATION_FAILED, client_error_);
  214. }
  215. TEST_F(WebrtcTransportTest, DataStream) {
  216. client_event_handler_.set_connecting_callback(base::Bind(
  217. &WebrtcTransportTest::CreateClientDataStream, base::Unretained(this)));
  218. host_event_handler_.set_connecting_callback(base::Bind(
  219. &WebrtcTransportTest::CreateHostDataStream, base::Unretained(this)));
  220. InitializeConnection();
  221. StartConnection();
  222. run_loop_.reset(new base::RunLoop());
  223. run_loop_->Run();
  224. EXPECT_TRUE(client_message_pipe_);
  225. EXPECT_TRUE(host_message_pipe_);
  226. const int kMessageSize = 1024;
  227. const int kMessages = 100;
  228. MessagePipeConnectionTester tester(host_message_pipe_.get(),
  229. client_message_pipe_.get(), kMessageSize,
  230. kMessages);
  231. tester.RunAndCheckResults();
  232. }
  233. // Verify that data streams can be created after connection has been initiated.
  234. TEST_F(WebrtcTransportTest, DataStreamLate) {
  235. InitializeConnection();
  236. StartConnection();
  237. WaitUntilConnected();
  238. CreateClientDataStream();
  239. CreateHostDataStream();
  240. run_loop_.reset(new base::RunLoop());
  241. run_loop_->Run();
  242. EXPECT_TRUE(client_message_pipe_);
  243. EXPECT_TRUE(host_message_pipe_);
  244. }
  245. TEST_F(WebrtcTransportTest, TerminateDataChannel) {
  246. InitializeConnection();
  247. StartConnection();
  248. WaitUntilConnected();
  249. CreateClientDataStream();
  250. CreateHostDataStream();
  251. run_loop_.reset(new base::RunLoop());
  252. run_loop_->Run();
  253. EXPECT_TRUE(client_message_pipe_);
  254. EXPECT_TRUE(host_message_pipe_);
  255. destroy_on_error_ = true;
  256. // Destroy pipe on one side of the of the connection. It should get closed on
  257. // the other side.
  258. client_message_pipe_.reset();
  259. run_loop_.reset(new base::RunLoop());
  260. run_loop_->Run();
  261. // Check that OnSessionError() has been called.
  262. EXPECT_EQ(CHANNEL_CONNECTION_ERROR, host_error_);
  263. EXPECT_FALSE(host_transport_);
  264. }
  265. } // namespace protocol
  266. } // namespace remoting