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