xref: /aosp_15_r20/external/webrtc/rtc_base/test_client_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #include "rtc_base/test_client.h"
12 
13 #include <utility>
14 
15 #include "absl/memory/memory.h"
16 #include "rtc_base/async_tcp_socket.h"
17 #include "rtc_base/async_udp_socket.h"
18 #include "rtc_base/logging.h"
19 #include "rtc_base/net_helpers.h"
20 #include "rtc_base/physical_socket_server.h"
21 #include "rtc_base/socket.h"
22 #include "rtc_base/test_echo_server.h"
23 #include "rtc_base/thread.h"
24 #include "test/gtest.h"
25 
26 namespace rtc {
27 namespace {
28 
29 #define MAYBE_SKIP_IPV4                        \
30   if (!HasIPv4Enabled()) {                     \
31     RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
32     return;                                    \
33   }
34 
35 #define MAYBE_SKIP_IPV6                        \
36   if (!HasIPv6Enabled()) {                     \
37     RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
38     return;                                    \
39   }
40 
TestUdpInternal(const SocketAddress & loopback)41 void TestUdpInternal(const SocketAddress& loopback) {
42   rtc::PhysicalSocketServer socket_server;
43   rtc::AutoSocketServerThread main_thread(&socket_server);
44   Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_DGRAM);
45   socket->Bind(loopback);
46 
47   TestClient client(std::make_unique<AsyncUDPSocket>(socket));
48   SocketAddress addr = client.address(), from;
49   EXPECT_EQ(3, client.SendTo("foo", 3, addr));
50   EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
51   EXPECT_EQ(from, addr);
52   EXPECT_TRUE(client.CheckNoPacket());
53 }
54 
TestTcpInternal(const SocketAddress & loopback)55 void TestTcpInternal(const SocketAddress& loopback) {
56   rtc::PhysicalSocketServer socket_server;
57   rtc::AutoSocketServerThread main_thread(&socket_server);
58   TestEchoServer server(&main_thread, loopback);
59 
60   Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_STREAM);
61   std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
62       AsyncTCPSocket::Create(socket, loopback, server.address()));
63   ASSERT_TRUE(tcp_socket != nullptr);
64 
65   TestClient client(std::move(tcp_socket));
66   SocketAddress addr = client.address(), from;
67   EXPECT_TRUE(client.CheckConnected());
68   EXPECT_EQ(3, client.Send("foo", 3));
69   EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
70   EXPECT_EQ(from, server.address());
71   EXPECT_TRUE(client.CheckNoPacket());
72 }
73 
74 // Tests whether the TestClient can send UDP to itself.
TEST(TestClientTest,TestUdpIPv4)75 TEST(TestClientTest, TestUdpIPv4) {
76   MAYBE_SKIP_IPV4;
77   TestUdpInternal(SocketAddress("127.0.0.1", 0));
78 }
79 
80 #if defined(WEBRTC_LINUX)
81 #define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
82 #else
83 #define MAYBE_TestUdpIPv6 TestUdpIPv6
84 #endif
TEST(TestClientTest,MAYBE_TestUdpIPv6)85 TEST(TestClientTest, MAYBE_TestUdpIPv6) {
86   MAYBE_SKIP_IPV6;
87   TestUdpInternal(SocketAddress("::1", 0));
88 }
89 
90 // Tests whether the TestClient can connect to a server and exchange data.
TEST(TestClientTest,TestTcpIPv4)91 TEST(TestClientTest, TestTcpIPv4) {
92   MAYBE_SKIP_IPV4;
93   TestTcpInternal(SocketAddress("127.0.0.1", 0));
94 }
95 
96 #if defined(WEBRTC_LINUX)
97 #define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
98 #else
99 #define MAYBE_TestTcpIPv6 TestTcpIPv6
100 #endif
TEST(TestClientTest,MAYBE_TestTcpIPv6)101 TEST(TestClientTest, MAYBE_TestTcpIPv6) {
102   MAYBE_SKIP_IPV6;
103   TestTcpInternal(SocketAddress("::1", 0));
104 }
105 
106 }  // namespace
107 }  // namespace rtc
108