blob: b3a6c8056389555a80655ca5009f4dc6f59ba7c5 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/testclient.h"
Karl Wiberg918f50c2018-07-05 11:40:33 +020012#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/gunit.h"
14#include "rtc_base/nethelpers.h"
15#include "rtc_base/physicalsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/testechoserver.h"
17#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19using namespace rtc;
20
Mirko Bonadei675513b2017-11-09 11:09:25 +010021#define MAYBE_SKIP_IPV4 \
22 if (!HasIPv4Enabled()) { \
23 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
24 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070025 }
26
Mirko Bonadei675513b2017-11-09 11:09:25 +010027#define MAYBE_SKIP_IPV6 \
28 if (!HasIPv6Enabled()) { \
29 RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
30 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070031 }
32
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033void TestUdpInternal(const SocketAddress& loopback) {
Yves Gerey665174f2018-06-19 15:03:05 +020034 Thread* main = Thread::Current();
35 AsyncSocket* socket =
36 main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_DGRAM);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037 socket->Bind(loopback);
38
Karl Wiberg918f50c2018-07-05 11:40:33 +020039 TestClient client(absl::make_unique<AsyncUDPSocket>(socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 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
47void TestTcpInternal(const SocketAddress& loopback) {
Yves Gerey665174f2018-06-19 15:03:05 +020048 Thread* main = Thread::Current();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 TestEchoServer server(main, loopback);
50
Yves Gerey665174f2018-06-19 15:03:05 +020051 AsyncSocket* socket =
52 main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_STREAM);
Karl Wiberg918f50c2018-07-05 11:40:33 +020053 std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
54 AsyncTCPSocket::Create(socket, loopback, server.address()));
deadbeef37f5ecf2017-02-27 14:06:41 -080055 ASSERT_TRUE(tcp_socket != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056
nisse32f25052017-05-08 01:57:18 -070057 TestClient client(std::move(tcp_socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 SocketAddress addr = client.address(), from;
59 EXPECT_TRUE(client.CheckConnected());
60 EXPECT_EQ(3, client.Send("foo", 3));
61 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
62 EXPECT_EQ(from, server.address());
63 EXPECT_TRUE(client.CheckNoPacket());
64}
65
66// Tests whether the TestClient can send UDP to itself.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000067TEST(TestClientTest, TestUdpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -070068 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 TestUdpInternal(SocketAddress("127.0.0.1", 0));
70}
71
minyue5d696482015-08-19 04:42:03 -070072#if defined(WEBRTC_LINUX)
73#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
74#else
75#define MAYBE_TestUdpIPv6 TestUdpIPv6
76#endif
77TEST(TestClientTest, MAYBE_TestUdpIPv6) {
deadbeef9a6f4d42017-05-15 19:43:33 -070078 MAYBE_SKIP_IPV6;
79 TestUdpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080}
81
82// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000083TEST(TestClientTest, TestTcpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -070084 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 TestTcpInternal(SocketAddress("127.0.0.1", 0));
86}
87
minyue5d696482015-08-19 04:42:03 -070088#if defined(WEBRTC_LINUX)
89#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
90#else
91#define MAYBE_TestTcpIPv6 TestTcpIPv6
92#endif
93TEST(TestClientTest, MAYBE_TestTcpIPv6) {
deadbeef9a6f4d42017-05-15 19:43:33 -070094 MAYBE_SKIP_IPV6;
95 TestTcpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096}