blob: 685499448f34ae31e84cac3057a33932640b48de [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
nisse32f25052017-05-08 01:57:18 -070011#include "webrtc/base/testclient.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include "webrtc/base/gunit.h"
13#include "webrtc/base/nethelpers.h"
14#include "webrtc/base/physicalsocketserver.h"
nisse32f25052017-05-08 01:57:18 -070015#include "webrtc/base/ptr_util.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/testechoserver.h"
17#include "webrtc/base/thread.h"
18
19using namespace rtc;
20
21void TestUdpInternal(const SocketAddress& loopback) {
22 Thread *main = Thread::Current();
23 AsyncSocket* socket = main->socketserver()
24 ->CreateAsyncSocket(loopback.family(), SOCK_DGRAM);
25 socket->Bind(loopback);
26
nisse32f25052017-05-08 01:57:18 -070027 TestClient client(MakeUnique<AsyncUDPSocket>(socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028 SocketAddress addr = client.address(), from;
29 EXPECT_EQ(3, client.SendTo("foo", 3, addr));
30 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
31 EXPECT_EQ(from, addr);
32 EXPECT_TRUE(client.CheckNoPacket());
33}
34
35void TestTcpInternal(const SocketAddress& loopback) {
36 Thread *main = Thread::Current();
37 TestEchoServer server(main, loopback);
38
39 AsyncSocket* socket = main->socketserver()
40 ->CreateAsyncSocket(loopback.family(), SOCK_STREAM);
nisse32f25052017-05-08 01:57:18 -070041 std::unique_ptr<AsyncTCPSocket> tcp_socket =
42 WrapUnique(AsyncTCPSocket::Create(socket, loopback, server.address()));
deadbeef37f5ecf2017-02-27 14:06:41 -080043 ASSERT_TRUE(tcp_socket != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
nisse32f25052017-05-08 01:57:18 -070045 TestClient client(std::move(tcp_socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046 SocketAddress addr = client.address(), from;
47 EXPECT_TRUE(client.CheckConnected());
48 EXPECT_EQ(3, client.Send("foo", 3));
49 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
50 EXPECT_EQ(from, server.address());
51 EXPECT_TRUE(client.CheckNoPacket());
52}
53
54// Tests whether the TestClient can send UDP to itself.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000055TEST(TestClientTest, TestUdpIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 TestUdpInternal(SocketAddress("127.0.0.1", 0));
57}
58
minyue5d696482015-08-19 04:42:03 -070059#if defined(WEBRTC_LINUX)
60#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
61#else
62#define MAYBE_TestUdpIPv6 TestUdpIPv6
63#endif
64TEST(TestClientTest, MAYBE_TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 if (HasIPv6Enabled()) {
66 TestUdpInternal(SocketAddress("::1", 0));
67 } else {
68 LOG(LS_INFO) << "Skipping IPv6 test.";
69 }
70}
71
72// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000073TEST(TestClientTest, TestTcpIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 TestTcpInternal(SocketAddress("127.0.0.1", 0));
75}
76
minyue5d696482015-08-19 04:42:03 -070077#if defined(WEBRTC_LINUX)
78#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
79#else
80#define MAYBE_TestTcpIPv6 TestTcpIPv6
81#endif
82TEST(TestClientTest, MAYBE_TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 if (HasIPv6Enabled()) {
84 TestTcpInternal(SocketAddress("::1", 0));
85 } else {
86 LOG(LS_INFO) << "Skipping IPv6 test.";
87 }
88}