blob: 742da7421ad1b0e5dea200a3b23bb750913dba7c [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"
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"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "rtc_base/asyncsocket.h"
17#include "rtc_base/asynctcpsocket.h"
18#include "rtc_base/asyncudpsocket.h"
19#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/nethelpers.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/socketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/testechoserver.h"
23#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
26using namespace rtc;
27
Mirko Bonadei675513b2017-11-09 11:09:25 +010028#define MAYBE_SKIP_IPV4 \
29 if (!HasIPv4Enabled()) { \
30 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
31 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070032 }
33
Mirko Bonadei675513b2017-11-09 11:09:25 +010034#define MAYBE_SKIP_IPV6 \
35 if (!HasIPv6Enabled()) { \
36 RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
37 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070038 }
39
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040void TestUdpInternal(const SocketAddress& loopback) {
Yves Gerey665174f2018-06-19 15:03:05 +020041 Thread* main = Thread::Current();
42 AsyncSocket* socket =
43 main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_DGRAM);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 socket->Bind(loopback);
45
Karl Wiberg918f50c2018-07-05 11:40:33 +020046 TestClient client(absl::make_unique<AsyncUDPSocket>(socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047 SocketAddress addr = client.address(), from;
48 EXPECT_EQ(3, client.SendTo("foo", 3, addr));
49 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
50 EXPECT_EQ(from, addr);
51 EXPECT_TRUE(client.CheckNoPacket());
52}
53
54void TestTcpInternal(const SocketAddress& loopback) {
Yves Gerey665174f2018-06-19 15:03:05 +020055 Thread* main = Thread::Current();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 TestEchoServer server(main, loopback);
57
Yves Gerey665174f2018-06-19 15:03:05 +020058 AsyncSocket* socket =
59 main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_STREAM);
Karl Wiberg918f50c2018-07-05 11:40:33 +020060 std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique(
61 AsyncTCPSocket::Create(socket, loopback, server.address()));
deadbeef37f5ecf2017-02-27 14:06:41 -080062 ASSERT_TRUE(tcp_socket != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
nisse32f25052017-05-08 01:57:18 -070064 TestClient client(std::move(tcp_socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 SocketAddress addr = client.address(), from;
66 EXPECT_TRUE(client.CheckConnected());
67 EXPECT_EQ(3, client.Send("foo", 3));
68 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
69 EXPECT_EQ(from, server.address());
70 EXPECT_TRUE(client.CheckNoPacket());
71}
72
73// Tests whether the TestClient can send UDP to itself.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000074TEST(TestClientTest, TestUdpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -070075 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 TestUdpInternal(SocketAddress("127.0.0.1", 0));
77}
78
minyue5d696482015-08-19 04:42:03 -070079#if defined(WEBRTC_LINUX)
80#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
81#else
82#define MAYBE_TestUdpIPv6 TestUdpIPv6
83#endif
84TEST(TestClientTest, MAYBE_TestUdpIPv6) {
deadbeef9a6f4d42017-05-15 19:43:33 -070085 MAYBE_SKIP_IPV6;
86 TestUdpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087}
88
89// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000090TEST(TestClientTest, TestTcpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -070091 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 TestTcpInternal(SocketAddress("127.0.0.1", 0));
93}
94
minyue5d696482015-08-19 04:42:03 -070095#if defined(WEBRTC_LINUX)
96#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
97#else
98#define MAYBE_TestTcpIPv6 TestTcpIPv6
99#endif
100TEST(TestClientTest, MAYBE_TestTcpIPv6) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700101 MAYBE_SKIP_IPV6;
102 TestTcpInternal(SocketAddress("::1", 0));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103}