chromium_socket_factory_unittest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2014 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/chromium_socket_factory.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/containers/flat_set.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/run_loop.h"
  12. #include "base/task/single_thread_task_runner.h"
  13. #include "base/test/task_environment.h"
  14. #include "testing/gmock/include/gmock/gmock.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "third_party/webrtc/rtc_base/async_packet_socket.h"
  17. #include "third_party/webrtc/rtc_base/socket_address.h"
  18. #include "third_party/webrtc/rtc_base/time_utils.h"
  19. namespace remoting {
  20. namespace protocol {
  21. namespace {
  22. class ConstantScopedFakeClock : public rtc::ClockInterface {
  23. public:
  24. ConstantScopedFakeClock() { prev_clock_ = rtc::SetClockForTesting(this); }
  25. ~ConstantScopedFakeClock() override { rtc::SetClockForTesting(prev_clock_); }
  26. int64_t TimeNanos() const override { return 1337L * 1000L * 1000L; }
  27. private:
  28. raw_ptr<ClockInterface> prev_clock_;
  29. };
  30. } // namespace
  31. class ChromiumSocketFactoryTest : public testing::Test,
  32. public sigslot::has_slots<> {
  33. public:
  34. void SetUp() override {
  35. socket_factory_ = std::make_unique<ChromiumPacketSocketFactory>(nullptr);
  36. socket_.reset(socket_factory_->CreateUdpSocket(
  37. rtc::SocketAddress("127.0.0.1", 0), 0, 0));
  38. ASSERT_TRUE(socket_.get() != nullptr);
  39. EXPECT_EQ(socket_->GetState(), rtc::AsyncPacketSocket::STATE_BOUND);
  40. socket_->SignalReadPacket.connect(
  41. this, &ChromiumSocketFactoryTest::OnPacket);
  42. }
  43. void OnPacket(rtc::AsyncPacketSocket* socket,
  44. const char* data,
  45. size_t size,
  46. const rtc::SocketAddress& address,
  47. const int64_t& packet_time) {
  48. EXPECT_EQ(socket, socket_.get());
  49. last_packet_.assign(data, data + size);
  50. last_address_ = address;
  51. last_packet_time_ = packet_time;
  52. run_loop_.Quit();
  53. }
  54. void OnSentPacket(rtc::AsyncPacketSocket* socket,
  55. const rtc::SentPacket& sent_packet) {
  56. // It is expected that send_packet was set using rtc::TimeMillis(),
  57. // which will use the fake clock set above, so the times will be equal
  58. int64_t fake_clock_ms = rtc::TimeMillis();
  59. EXPECT_EQ(fake_clock_ms, sent_packet.send_time_ms);
  60. }
  61. void VerifyCanSendAndReceive(rtc::AsyncPacketSocket* sender) {
  62. // UDP packets may be lost, so we have to retry sending it more than once.
  63. const int kMaxAttempts = 3;
  64. const base::TimeDelta kAttemptPeriod = base::Seconds(1);
  65. std::string test_packet("TEST PACKET");
  66. int attempts = 0;
  67. rtc::PacketOptions options;
  68. while (last_packet_.empty() && attempts++ < kMaxAttempts) {
  69. sender->SendTo(test_packet.data(), test_packet.size(),
  70. socket_->GetLocalAddress(), options);
  71. task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
  72. FROM_HERE, run_loop_.QuitClosure(), kAttemptPeriod);
  73. run_loop_.Run();
  74. }
  75. EXPECT_EQ(test_packet, last_packet_);
  76. EXPECT_EQ(sender->GetLocalAddress(), last_address_);
  77. }
  78. protected:
  79. base::test::SingleThreadTaskEnvironment task_environment_{
  80. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  81. base::RunLoop run_loop_;
  82. std::unique_ptr<rtc::PacketSocketFactory> socket_factory_;
  83. std::unique_ptr<rtc::AsyncPacketSocket> socket_;
  84. std::string last_packet_;
  85. rtc::SocketAddress last_address_;
  86. int64_t last_packet_time_;
  87. ConstantScopedFakeClock fake_clock_;
  88. };
  89. TEST_F(ChromiumSocketFactoryTest, SendAndReceive) {
  90. std::unique_ptr<rtc::AsyncPacketSocket> sending_socket(
  91. socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0,
  92. 0));
  93. ASSERT_TRUE(sending_socket.get() != nullptr);
  94. EXPECT_EQ(sending_socket->GetState(),
  95. rtc::AsyncPacketSocket::STATE_BOUND);
  96. VerifyCanSendAndReceive(sending_socket.get());
  97. }
  98. TEST_F(ChromiumSocketFactoryTest, SetOptions) {
  99. EXPECT_EQ(0, socket_->SetOption(rtc::Socket::OPT_SNDBUF, 4096));
  100. EXPECT_EQ(0, socket_->SetOption(rtc::Socket::OPT_RCVBUF, 4096));
  101. }
  102. TEST_F(ChromiumSocketFactoryTest, PortRange) {
  103. constexpr uint16_t kMinPort = 12400;
  104. constexpr uint16_t kMaxPort = 12410;
  105. socket_.reset(socket_factory_->CreateUdpSocket(
  106. rtc::SocketAddress("127.0.0.1", 0), kMinPort, kMaxPort));
  107. ASSERT_TRUE(socket_.get() != nullptr);
  108. EXPECT_EQ(socket_->GetState(), rtc::AsyncPacketSocket::STATE_BOUND);
  109. EXPECT_GE(socket_->GetLocalAddress().port(), kMinPort);
  110. EXPECT_LE(socket_->GetLocalAddress().port(), kMaxPort);
  111. }
  112. TEST_F(ChromiumSocketFactoryTest, CreateMultiplePortsFromPortRange) {
  113. constexpr uint16_t kPortCount = 5;
  114. constexpr uint16_t kMinPort = 12400;
  115. constexpr uint16_t kMaxPort = kMinPort + kPortCount - 1;
  116. std::vector<std::unique_ptr<rtc::AsyncPacketSocket>> sockets;
  117. for (int i = 0; i < kPortCount; i++) {
  118. sockets.push_back(std::unique_ptr<rtc::AsyncPacketSocket>(
  119. socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0),
  120. kMinPort, kMaxPort)));
  121. }
  122. base::flat_set<uint16_t> assigned_ports;
  123. for (auto& socket : sockets) {
  124. ASSERT_TRUE(socket.get() != nullptr);
  125. EXPECT_EQ(socket->GetState(), rtc::AsyncPacketSocket::STATE_BOUND);
  126. uint16_t port = socket->GetLocalAddress().port();
  127. EXPECT_GE(port, kMinPort);
  128. EXPECT_LE(port, kMaxPort);
  129. ASSERT_EQ(assigned_ports.end(), assigned_ports.find(port));
  130. assigned_ports.insert(port);
  131. }
  132. // Try to create another socket, which should fail because no more port is
  133. // available.
  134. auto* extra_socket = socket_factory_->CreateUdpSocket(
  135. rtc::SocketAddress("127.0.0.1", 0), kMinPort, kMaxPort);
  136. ASSERT_EQ(nullptr, extra_socket);
  137. }
  138. TEST_F(ChromiumSocketFactoryTest, TransientError) {
  139. std::unique_ptr<rtc::AsyncPacketSocket> sending_socket(
  140. socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0,
  141. 0));
  142. std::string test_packet("TEST");
  143. // Try sending a packet to an IPv6 address from a socket that's bound to an
  144. // IPv4 address. This send is expected to fail, but the socket should still be
  145. // functional.
  146. sending_socket->SendTo(test_packet.data(), test_packet.size(),
  147. rtc::SocketAddress("::1", 0),
  148. rtc::PacketOptions());
  149. // Verify that socket is still usable.
  150. VerifyCanSendAndReceive(sending_socket.get());
  151. }
  152. TEST_F(ChromiumSocketFactoryTest, CheckSendTime) {
  153. std::unique_ptr<rtc::AsyncPacketSocket> sending_socket(
  154. socket_factory_->CreateUdpSocket(rtc::SocketAddress("127.0.0.1", 0), 0,
  155. 0));
  156. sending_socket->SignalSentPacket.connect(
  157. static_cast<ChromiumSocketFactoryTest*>(this),
  158. &ChromiumSocketFactoryTest::OnSentPacket);
  159. VerifyCanSendAndReceive(sending_socket.get());
  160. // Check receive time is from rtc clock as well
  161. ASSERT_EQ(last_packet_time_, rtc::TimeMicros());
  162. }
  163. } // namespace protocol
  164. } // namespace remoting