You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2006 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/test_client.h"
  11. #include <utility>
  12. #include "absl/memory/memory.h"
  13. #include "rtc_base/async_socket.h"
  14. #include "rtc_base/async_tcp_socket.h"
  15. #include "rtc_base/async_udp_socket.h"
  16. #include "rtc_base/logging.h"
  17. #include "rtc_base/net_helpers.h"
  18. #include "rtc_base/socket_server.h"
  19. #include "rtc_base/test_echo_server.h"
  20. #include "rtc_base/thread.h"
  21. #include "test/gtest.h"
  22. namespace rtc {
  23. namespace {
  24. #define MAYBE_SKIP_IPV4 \
  25. if (!HasIPv4Enabled()) { \
  26. RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
  27. return; \
  28. }
  29. #define MAYBE_SKIP_IPV6 \
  30. if (!HasIPv6Enabled()) { \
  31. RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
  32. return; \
  33. }
  34. void TestUdpInternal(const SocketAddress& loopback) {
  35. Thread* main = Thread::Current();
  36. AsyncSocket* socket =
  37. main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_DGRAM);
  38. socket->Bind(loopback);
  39. TestClient client(std::make_unique<AsyncUDPSocket>(socket));
  40. SocketAddress addr = client.address(), from;
  41. EXPECT_EQ(3, client.SendTo("foo", 3, addr));
  42. EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
  43. EXPECT_EQ(from, addr);
  44. EXPECT_TRUE(client.CheckNoPacket());
  45. }
  46. void TestTcpInternal(const SocketAddress& loopback) {
  47. Thread* main = Thread::Current();
  48. TestEchoServer server(main, loopback);
  49. AsyncSocket* socket =
  50. main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_STREAM);
  51. std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
  52. AsyncTCPSocket::Create(socket, loopback, server.address()));
  53. ASSERT_TRUE(tcp_socket != nullptr);
  54. TestClient client(std::move(tcp_socket));
  55. SocketAddress addr = client.address(), from;
  56. EXPECT_TRUE(client.CheckConnected());
  57. EXPECT_EQ(3, client.Send("foo", 3));
  58. EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
  59. EXPECT_EQ(from, server.address());
  60. EXPECT_TRUE(client.CheckNoPacket());
  61. }
  62. // Tests whether the TestClient can send UDP to itself.
  63. TEST(TestClientTest, TestUdpIPv4) {
  64. MAYBE_SKIP_IPV4;
  65. TestUdpInternal(SocketAddress("127.0.0.1", 0));
  66. }
  67. #if defined(WEBRTC_LINUX)
  68. #define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
  69. #else
  70. #define MAYBE_TestUdpIPv6 TestUdpIPv6
  71. #endif
  72. TEST(TestClientTest, MAYBE_TestUdpIPv6) {
  73. MAYBE_SKIP_IPV6;
  74. TestUdpInternal(SocketAddress("::1", 0));
  75. }
  76. // Tests whether the TestClient can connect to a server and exchange data.
  77. TEST(TestClientTest, TestTcpIPv4) {
  78. MAYBE_SKIP_IPV4;
  79. TestTcpInternal(SocketAddress("127.0.0.1", 0));
  80. }
  81. #if defined(WEBRTC_LINUX)
  82. #define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
  83. #else
  84. #define MAYBE_TestTcpIPv6 TestTcpIPv6
  85. #endif
  86. TEST(TestClientTest, MAYBE_TestTcpIPv6) {
  87. MAYBE_SKIP_IPV6;
  88. TestTcpInternal(SocketAddress("::1", 0));
  89. }
  90. } // namespace
  91. } // namespace rtc