blob: a0c8b88e626bca6c7fb0131016fc7acf5d028833 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#include "rtc_base/test_client.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <utility>
14
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/async_tcp_socket.h"
17#include "rtc_base/async_udp_socket.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/net_helpers.h"
Niels Möller83830f32022-05-20 09:12:57 +020020#include "rtc_base/physical_socket_server.h"
Niels Möllerd0b88792021-08-12 10:32:30 +020021#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/test_echo_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
Mirko Bonadeie10b1632018-12-11 18:43:40 +010026namespace rtc {
27namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
Mirko Bonadei675513b2017-11-09 11:09:25 +010029#define MAYBE_SKIP_IPV4 \
30 if (!HasIPv4Enabled()) { \
31 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
32 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070033 }
34
Mirko Bonadei675513b2017-11-09 11:09:25 +010035#define MAYBE_SKIP_IPV6 \
36 if (!HasIPv6Enabled()) { \
37 RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
38 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070039 }
40
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041void TestUdpInternal(const SocketAddress& loopback) {
Niels Möller83830f32022-05-20 09:12:57 +020042 rtc::PhysicalSocketServer socket_server;
43 rtc::AutoSocketServerThread main_thread(&socket_server);
44 Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_DGRAM);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045 socket->Bind(loopback);
46
Mirko Bonadei317a1f02019-09-17 17:06:18 +020047 TestClient client(std::make_unique<AsyncUDPSocket>(socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 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
55void TestTcpInternal(const SocketAddress& loopback) {
Niels Möller83830f32022-05-20 09:12:57 +020056 rtc::PhysicalSocketServer socket_server;
57 rtc::AutoSocketServerThread main_thread(&socket_server);
58 TestEchoServer server(&main_thread, loopback);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
Niels Möller83830f32022-05-20 09:12:57 +020060 Socket* socket = socket_server.CreateSocket(loopback.family(), SOCK_STREAM);
Karl Wiberg918f50c2018-07-05 11:40:33 +020061 std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
62 AsyncTCPSocket::Create(socket, loopback, server.address()));
deadbeef37f5ecf2017-02-27 14:06:41 -080063 ASSERT_TRUE(tcp_socket != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
nisse32f25052017-05-08 01:57:18 -070065 TestClient client(std::move(tcp_socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 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.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000075TEST(TestClientTest, TestUdpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -070076 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 TestUdpInternal(SocketAddress("127.0.0.1", 0));
78}
79
minyue5d696482015-08-19 04:42:03 -070080#if defined(WEBRTC_LINUX)
81#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
82#else
83#define MAYBE_TestUdpIPv6 TestUdpIPv6
84#endif
85TEST(TestClientTest, MAYBE_TestUdpIPv6) {
deadbeef9a6f4d42017-05-15 19:43:33 -070086 MAYBE_SKIP_IPV6;
87 TestUdpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088}
89
90// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000091TEST(TestClientTest, TestTcpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -070092 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 TestTcpInternal(SocketAddress("127.0.0.1", 0));
94}
95
minyue5d696482015-08-19 04:42:03 -070096#if defined(WEBRTC_LINUX)
97#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
98#else
99#define MAYBE_TestTcpIPv6 TestTcpIPv6
100#endif
101TEST(TestClientTest, MAYBE_TestTcpIPv6) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700102 MAYBE_SKIP_IPV6;
103 TestTcpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104}
Mirko Bonadeie10b1632018-12-11 18:43:40 +0100105
106} // namespace
107} // namespace rtc