ice_transport_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/ice_transport.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/location.h"
  9. #include "base/logging.h"
  10. #include "base/run_loop.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "base/time/time.h"
  15. #include "build/build_config.h"
  16. #include "components/webrtc/thread_wrapper.h"
  17. #include "net/url_request/url_request_context_getter.h"
  18. #include "remoting/base/url_request.h"
  19. #include "remoting/protocol/chromium_port_allocator_factory.h"
  20. #include "remoting/protocol/connection_tester.h"
  21. #include "remoting/protocol/fake_authenticator.h"
  22. #include "remoting/protocol/message_channel_factory.h"
  23. #include "remoting/protocol/message_pipe.h"
  24. #include "remoting/protocol/transport_context.h"
  25. #include "services/network/public/cpp/shared_url_loader_factory.h"
  26. #include "testing/gmock/include/gmock/gmock.h"
  27. #include "testing/gtest/include/gtest/gtest.h"
  28. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  29. using testing::_;
  30. namespace remoting {
  31. namespace protocol {
  32. namespace {
  33. // Send 100 messages 1024 bytes each. UDP messages are sent with 10ms delay
  34. // between messages (about 1 second for 100 messages).
  35. const int kMessageSize = 1024;
  36. const int kMessages = 100;
  37. const char kChannelName[] = "test_channel";
  38. ACTION_P2(QuitRunLoopOnCounter, run_loop, counter) {
  39. --(*counter);
  40. EXPECT_GE(*counter, 0);
  41. if (*counter == 0)
  42. run_loop->Quit();
  43. }
  44. class MockChannelCreatedCallback {
  45. public:
  46. MOCK_METHOD1(OnDone, void(MessagePipe* socket));
  47. };
  48. class TestTransportEventHandler : public IceTransport::EventHandler {
  49. public:
  50. typedef base::RepeatingCallback<void(ErrorCode error)> ErrorCallback;
  51. TestTransportEventHandler() = default;
  52. TestTransportEventHandler(const TestTransportEventHandler&) = delete;
  53. TestTransportEventHandler& operator=(const TestTransportEventHandler&) =
  54. delete;
  55. ~TestTransportEventHandler() = default;
  56. void set_error_callback(const ErrorCallback& callback) {
  57. error_callback_ = callback;
  58. }
  59. // IceTransport::EventHandler interface.
  60. void OnIceTransportRouteChange(const std::string& channel_name,
  61. const TransportRoute& route) override {}
  62. void OnIceTransportError(ErrorCode error) override {
  63. error_callback_.Run(error);
  64. }
  65. private:
  66. ErrorCallback error_callback_;
  67. };
  68. } // namespace
  69. class IceTransportTest : public testing::Test {
  70. public:
  71. IceTransportTest() {
  72. webrtc::ThreadWrapper::EnsureForCurrentMessageLoop();
  73. network_settings_ =
  74. NetworkSettings(NetworkSettings::NAT_TRAVERSAL_OUTGOING);
  75. }
  76. void TearDown() override {
  77. client_message_pipe_.reset();
  78. host_message_pipe_.reset();
  79. client_transport_.reset();
  80. host_transport_.reset();
  81. base::RunLoop().RunUntilIdle();
  82. }
  83. void ProcessTransportInfo(std::unique_ptr<IceTransport>* target_transport,
  84. std::unique_ptr<jingle_xmpp::XmlElement> transport_info) {
  85. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  86. FROM_HERE,
  87. base::BindOnce(&IceTransportTest::DeliverTransportInfo,
  88. base::Unretained(this), target_transport,
  89. std::move(transport_info)),
  90. transport_info_delay_);
  91. }
  92. void DeliverTransportInfo(std::unique_ptr<IceTransport>* target_transport,
  93. std::unique_ptr<jingle_xmpp::XmlElement> transport_info) {
  94. ASSERT_TRUE(target_transport);
  95. EXPECT_TRUE(
  96. (*target_transport)->ProcessTransportInfo(transport_info.get()));
  97. }
  98. void InitializeConnection() {
  99. webrtc::ThreadWrapper::EnsureForCurrentMessageLoop();
  100. rtc::SocketFactory* socket_factory =
  101. webrtc::ThreadWrapper::current()->SocketServer();
  102. host_transport_ = std::make_unique<IceTransport>(
  103. new TransportContext(std::make_unique<ChromiumPortAllocatorFactory>(),
  104. socket_factory, nullptr, nullptr,
  105. network_settings_, TransportRole::SERVER),
  106. &host_event_handler_);
  107. if (!host_authenticator_) {
  108. host_authenticator_ =
  109. std::make_unique<FakeAuthenticator>(FakeAuthenticator::ACCEPT);
  110. }
  111. client_transport_ = std::make_unique<IceTransport>(
  112. new TransportContext(std::make_unique<ChromiumPortAllocatorFactory>(),
  113. socket_factory, nullptr, nullptr,
  114. network_settings_, TransportRole::CLIENT),
  115. &client_event_handler_);
  116. if (!client_authenticator_) {
  117. client_authenticator_ =
  118. std::make_unique<FakeAuthenticator>(FakeAuthenticator::ACCEPT);
  119. }
  120. host_event_handler_.set_error_callback(base::BindRepeating(
  121. &IceTransportTest::OnTransportError, base::Unretained(this)));
  122. client_event_handler_.set_error_callback(base::BindRepeating(
  123. &IceTransportTest::OnTransportError, base::Unretained(this)));
  124. // Start both transports.
  125. host_transport_->Start(
  126. host_authenticator_.get(),
  127. base::BindRepeating(&IceTransportTest::ProcessTransportInfo,
  128. base::Unretained(this), &client_transport_));
  129. client_transport_->Start(
  130. client_authenticator_.get(),
  131. base::BindRepeating(&IceTransportTest::ProcessTransportInfo,
  132. base::Unretained(this), &host_transport_));
  133. }
  134. void WaitUntilConnected() {
  135. run_loop_ = std::make_unique<base::RunLoop>();
  136. int counter = 2;
  137. EXPECT_CALL(client_channel_callback_, OnDone(_))
  138. .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter));
  139. EXPECT_CALL(host_channel_callback_, OnDone(_))
  140. .WillOnce(QuitRunLoopOnCounter(run_loop_.get(), &counter));
  141. run_loop_->Run();
  142. EXPECT_TRUE(client_message_pipe_.get());
  143. EXPECT_TRUE(host_message_pipe_.get());
  144. }
  145. void OnClientChannelCreated(std::unique_ptr<MessagePipe> message_pipe) {
  146. client_message_pipe_ = std::move(message_pipe);
  147. client_channel_callback_.OnDone(client_message_pipe_.get());
  148. }
  149. void OnHostChannelCreated(std::unique_ptr<MessagePipe> message_pipe) {
  150. host_message_pipe_ = std::move(message_pipe);
  151. host_channel_callback_.OnDone(host_message_pipe_.get());
  152. }
  153. void OnTransportError(ErrorCode error) {
  154. LOG(ERROR) << "Transport Error";
  155. error_ = error;
  156. run_loop_->Quit();
  157. }
  158. protected:
  159. base::test::SingleThreadTaskEnvironment task_environment_{
  160. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  161. std::unique_ptr<base::RunLoop> run_loop_;
  162. NetworkSettings network_settings_;
  163. base::TimeDelta transport_info_delay_;
  164. std::unique_ptr<IceTransport> host_transport_;
  165. TestTransportEventHandler host_event_handler_;
  166. std::unique_ptr<FakeAuthenticator> host_authenticator_;
  167. std::unique_ptr<IceTransport> client_transport_;
  168. TestTransportEventHandler client_event_handler_;
  169. std::unique_ptr<FakeAuthenticator> client_authenticator_;
  170. MockChannelCreatedCallback client_channel_callback_;
  171. MockChannelCreatedCallback host_channel_callback_;
  172. std::unique_ptr<MessagePipe> client_message_pipe_;
  173. std::unique_ptr<MessagePipe> host_message_pipe_;
  174. ErrorCode error_ = OK;
  175. };
  176. // crbug.com/1224862: Tests are flaky on Mac.
  177. #if BUILDFLAG(IS_MAC)
  178. #define MAYBE_DataStream DISABLED_DataStream
  179. #else
  180. #define MAYBE_DataStream DataStream
  181. #endif
  182. TEST_F(IceTransportTest, MAYBE_DataStream) {
  183. InitializeConnection();
  184. client_transport_->GetChannelFactory()->CreateChannel(
  185. kChannelName, base::BindOnce(&IceTransportTest::OnClientChannelCreated,
  186. base::Unretained(this)));
  187. host_transport_->GetChannelFactory()->CreateChannel(
  188. kChannelName, base::BindOnce(&IceTransportTest::OnHostChannelCreated,
  189. base::Unretained(this)));
  190. WaitUntilConnected();
  191. MessagePipeConnectionTester tester(host_message_pipe_.get(),
  192. client_message_pipe_.get(), kMessageSize,
  193. kMessages);
  194. tester.RunAndCheckResults();
  195. }
  196. // crbug.com/1224862: Tests are flaky on Mac.
  197. #if BUILDFLAG(IS_MAC)
  198. #define MAYBE_MuxDataStream DISABLED_MuxDataStream
  199. #else
  200. #define MAYBE_MuxDataStream MuxDataStream
  201. #endif
  202. TEST_F(IceTransportTest, MAYBE_MuxDataStream) {
  203. InitializeConnection();
  204. client_transport_->GetMultiplexedChannelFactory()->CreateChannel(
  205. kChannelName, base::BindOnce(&IceTransportTest::OnClientChannelCreated,
  206. base::Unretained(this)));
  207. host_transport_->GetMultiplexedChannelFactory()->CreateChannel(
  208. kChannelName, base::BindOnce(&IceTransportTest::OnHostChannelCreated,
  209. base::Unretained(this)));
  210. WaitUntilConnected();
  211. MessagePipeConnectionTester tester(host_message_pipe_.get(),
  212. client_message_pipe_.get(), kMessageSize,
  213. kMessages);
  214. tester.RunAndCheckResults();
  215. }
  216. // crbug.com/1224862: Tests are flaky on Mac.
  217. #if BUILDFLAG(IS_MAC)
  218. #define MAYBE_FailedChannelAuth DISABLED_FailedChannelAuth
  219. #else
  220. #define MAYBE_FailedChannelAuth FailedChannelAuth
  221. #endif
  222. TEST_F(IceTransportTest, MAYBE_FailedChannelAuth) {
  223. // Use host authenticator with one that rejects channel authentication.
  224. host_authenticator_ =
  225. std::make_unique<FakeAuthenticator>(FakeAuthenticator::REJECT_CHANNEL);
  226. InitializeConnection();
  227. client_transport_->GetChannelFactory()->CreateChannel(
  228. kChannelName, base::BindOnce(&IceTransportTest::OnClientChannelCreated,
  229. base::Unretained(this)));
  230. host_transport_->GetChannelFactory()->CreateChannel(
  231. kChannelName, base::BindOnce(&IceTransportTest::OnHostChannelCreated,
  232. base::Unretained(this)));
  233. run_loop_ = std::make_unique<base::RunLoop>();
  234. // The callback should never be called.
  235. EXPECT_CALL(host_channel_callback_, OnDone(_)).Times(0);
  236. run_loop_->Run();
  237. EXPECT_FALSE(host_message_pipe_);
  238. EXPECT_EQ(CHANNEL_CONNECTION_ERROR, error_);
  239. client_transport_->GetChannelFactory()->CancelChannelCreation(
  240. kChannelName);
  241. }
  242. // Verify that channels are never marked connected if connection cannot be
  243. // established.
  244. TEST_F(IceTransportTest, TestBrokenTransport) {
  245. // Allow only incoming connections on both ends, which effectively renders
  246. // transport unusable. Also reduce connection timeout so the test finishes
  247. // quickly.
  248. network_settings_ = NetworkSettings(NetworkSettings::NAT_TRAVERSAL_DISABLED);
  249. network_settings_.ice_timeout = base::Seconds(1);
  250. network_settings_.ice_reconnect_attempts = 1;
  251. InitializeConnection();
  252. client_transport_->GetChannelFactory()->CreateChannel(
  253. kChannelName, base::BindOnce(&IceTransportTest::OnClientChannelCreated,
  254. base::Unretained(this)));
  255. host_transport_->GetChannelFactory()->CreateChannel(
  256. kChannelName, base::BindOnce(&IceTransportTest::OnHostChannelCreated,
  257. base::Unretained(this)));
  258. // The RunLoop should quit in OnTransportError().
  259. run_loop_ = std::make_unique<base::RunLoop>();
  260. run_loop_->Run();
  261. // Verify that neither of the two ends of the channel is connected.
  262. EXPECT_FALSE(client_message_pipe_);
  263. EXPECT_FALSE(host_message_pipe_);
  264. EXPECT_EQ(CHANNEL_CONNECTION_ERROR, error_);
  265. client_transport_->GetChannelFactory()->CancelChannelCreation(
  266. kChannelName);
  267. host_transport_->GetChannelFactory()->CancelChannelCreation(
  268. kChannelName);
  269. }
  270. TEST_F(IceTransportTest, TestCancelChannelCreation) {
  271. InitializeConnection();
  272. client_transport_->GetChannelFactory()->CreateChannel(
  273. kChannelName, base::BindOnce(&IceTransportTest::OnClientChannelCreated,
  274. base::Unretained(this)));
  275. client_transport_->GetChannelFactory()->CancelChannelCreation(
  276. kChannelName);
  277. EXPECT_TRUE(!client_message_pipe_.get());
  278. }
  279. // crbug.com/1224862: Tests are flaky on Mac.
  280. #if BUILDFLAG(IS_MAC)
  281. #define MAYBE_TestDelayedSignaling DISABLED_TestDelayedSignaling
  282. #else
  283. #define MAYBE_TestDelayedSignaling TestDelayedSignaling
  284. #endif
  285. // Verify that we can still connect even when there is a delay in signaling
  286. // messages delivery.
  287. TEST_F(IceTransportTest, MAYBE_TestDelayedSignaling) {
  288. transport_info_delay_ = base::Milliseconds(100);
  289. InitializeConnection();
  290. client_transport_->GetChannelFactory()->CreateChannel(
  291. kChannelName, base::BindOnce(&IceTransportTest::OnClientChannelCreated,
  292. base::Unretained(this)));
  293. host_transport_->GetChannelFactory()->CreateChannel(
  294. kChannelName, base::BindOnce(&IceTransportTest::OnHostChannelCreated,
  295. base::Unretained(this)));
  296. WaitUntilConnected();
  297. MessagePipeConnectionTester tester(host_message_pipe_.get(),
  298. client_message_pipe_.get(), kMessageSize,
  299. kMessages);
  300. tester.RunAndCheckResults();
  301. }
  302. } // namespace protocol
  303. } // namespace remoting