blob: b2866a2d349f06036207b3549a835c98cf0afd4a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/test_client.h"
Yves Gerey3e707812018-11-28 15:47:4912
13#include <utility>
14
Karl Wiberg918f50c2018-07-05 09:40:3315#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 17:11:0016#include "rtc_base/async_tcp_socket.h"
17#include "rtc_base/async_udp_socket.h"
Yves Gerey3e707812018-11-28 15:47:4918#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0019#include "rtc_base/net_helpers.h"
Mirko Bonadei0d8b79e2023-07-27 06:55:4920#include "rtc_base/net_test_helpers.h"
Niels Möller83830f32022-05-20 07:12:5721#include "rtc_base/physical_socket_server.h"
Niels Möllerd0b88792021-08-12 08:32:3022#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/test_echo_server.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 15:47:4925#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2626
Mirko Bonadeie10b1632018-12-11 17:43:4027namespace rtc {
28namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:2629
Mirko Bonadei675513b2017-11-09 10:09:2530#define MAYBE_SKIP_IPV4 \
31 if (!HasIPv4Enabled()) { \
32 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
33 return; \
deadbeef9a6f4d42017-05-16 02:43:3334 }
35
Mirko Bonadei675513b2017-11-09 10:09:2536#define MAYBE_SKIP_IPV6 \
37 if (!HasIPv6Enabled()) { \
38 RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
39 return; \
deadbeef9a6f4d42017-05-16 02:43:3340 }
41
henrike@webrtc.orgf0488722014-05-13 18:00:2642void TestUdpInternal(const SocketAddress& loopback) {
Niels Möller83830f32022-05-20 07:12:5743 rtc::PhysicalSocketServer socket_server;
44 rtc::AutoSocketServerThread main_thread(&socket_server);
45 Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_DGRAM);
henrike@webrtc.orgf0488722014-05-13 18:00:2646 socket->Bind(loopback);
47
Mirko Bonadei317a1f02019-09-17 15:06:1848 TestClient client(std::make_unique<AsyncUDPSocket>(socket));
henrike@webrtc.orgf0488722014-05-13 18:00:2649 SocketAddress addr = client.address(), from;
50 EXPECT_EQ(3, client.SendTo("foo", 3, addr));
51 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
52 EXPECT_EQ(from, addr);
53 EXPECT_TRUE(client.CheckNoPacket());
54}
55
56void TestTcpInternal(const SocketAddress& loopback) {
Niels Möller83830f32022-05-20 07:12:5757 rtc::PhysicalSocketServer socket_server;
58 rtc::AutoSocketServerThread main_thread(&socket_server);
59 TestEchoServer server(&main_thread, loopback);
henrike@webrtc.orgf0488722014-05-13 18:00:2660
Niels Möller83830f32022-05-20 07:12:5761 Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_STREAM);
Karl Wiberg918f50c2018-07-05 09:40:3362 std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
63 AsyncTCPSocket::Create(socket, loopback, server.address()));
deadbeef37f5ecf2017-02-27 22:06:4164 ASSERT_TRUE(tcp_socket != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:2665
nisse32f25052017-05-08 08:57:1866 TestClient client(std::move(tcp_socket));
henrike@webrtc.orgf0488722014-05-13 18:00:2667 SocketAddress addr = client.address(), from;
68 EXPECT_TRUE(client.CheckConnected());
69 EXPECT_EQ(3, client.Send("foo", 3));
70 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
71 EXPECT_EQ(from, server.address());
72 EXPECT_TRUE(client.CheckNoPacket());
73}
74
75// Tests whether the TestClient can send UDP to itself.
henrike@webrtc.orgc732a3e2014-10-09 22:08:1576TEST(TestClientTest, TestUdpIPv4) {
deadbeef9a6f4d42017-05-16 02:43:3377 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:2678 TestUdpInternal(SocketAddress("127.0.0.1", 0));
79}
80
minyue5d696482015-08-19 11:42:0381#if defined(WEBRTC_LINUX)
82#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
83#else
84#define MAYBE_TestUdpIPv6 TestUdpIPv6
85#endif
86TEST(TestClientTest, MAYBE_TestUdpIPv6) {
deadbeef9a6f4d42017-05-16 02:43:3387 MAYBE_SKIP_IPV6;
88 TestUdpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:2689}
90
91// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:1592TEST(TestClientTest, TestTcpIPv4) {
deadbeef9a6f4d42017-05-16 02:43:3393 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:2694 TestTcpInternal(SocketAddress("127.0.0.1", 0));
95}
96
minyue5d696482015-08-19 11:42:0397#if defined(WEBRTC_LINUX)
98#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
99#else
100#define MAYBE_TestTcpIPv6 TestTcpIPv6
101#endif
102TEST(TestClientTest, MAYBE_TestTcpIPv6) {
deadbeef9a6f4d42017-05-16 02:43:33103 MAYBE_SKIP_IPV6;
104 TestTcpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26105}
Mirko Bonadeie10b1632018-12-11 17:43:40106
107} // namespace
108} // namespace rtc