blob: bdd06b329a9f594dce57c8194297437c587aa466 [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
11#include "webrtc/base/gunit.h"
12#include "webrtc/base/nethelpers.h"
13#include "webrtc/base/physicalsocketserver.h"
14#include "webrtc/base/testclient.h"
15#include "webrtc/base/testechoserver.h"
16#include "webrtc/base/thread.h"
17
18using namespace rtc;
19
20void TestUdpInternal(const SocketAddress& loopback) {
21 Thread *main = Thread::Current();
22 AsyncSocket* socket = main->socketserver()
23 ->CreateAsyncSocket(loopback.family(), SOCK_DGRAM);
24 socket->Bind(loopback);
25
26 TestClient client(new AsyncUDPSocket(socket));
27 SocketAddress addr = client.address(), from;
28 EXPECT_EQ(3, client.SendTo("foo", 3, addr));
29 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
30 EXPECT_EQ(from, addr);
31 EXPECT_TRUE(client.CheckNoPacket());
32}
33
34void TestTcpInternal(const SocketAddress& loopback) {
35 Thread *main = Thread::Current();
36 TestEchoServer server(main, loopback);
37
38 AsyncSocket* socket = main->socketserver()
39 ->CreateAsyncSocket(loopback.family(), SOCK_STREAM);
40 AsyncTCPSocket* tcp_socket = AsyncTCPSocket::Create(
41 socket, loopback, server.address());
42 ASSERT_TRUE(tcp_socket != NULL);
43
44 TestClient client(tcp_socket);
45 SocketAddress addr = client.address(), from;
46 EXPECT_TRUE(client.CheckConnected());
47 EXPECT_EQ(3, client.Send("foo", 3));
48 EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from));
49 EXPECT_EQ(from, server.address());
50 EXPECT_TRUE(client.CheckNoPacket());
51}
52
53// Tests whether the TestClient can send UDP to itself.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000054TEST(TestClientTest, TestUdpIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 TestUdpInternal(SocketAddress("127.0.0.1", 0));
56}
57
minyue5d696482015-08-19 04:42:03 -070058#if defined(WEBRTC_LINUX)
59#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
60#else
61#define MAYBE_TestUdpIPv6 TestUdpIPv6
62#endif
63TEST(TestClientTest, MAYBE_TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064 if (HasIPv6Enabled()) {
65 TestUdpInternal(SocketAddress("::1", 0));
66 } else {
67 LOG(LS_INFO) << "Skipping IPv6 test.";
68 }
69}
70
71// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000072TEST(TestClientTest, TestTcpIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 TestTcpInternal(SocketAddress("127.0.0.1", 0));
74}
75
minyue5d696482015-08-19 04:42:03 -070076#if defined(WEBRTC_LINUX)
77#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
78#else
79#define MAYBE_TestTcpIPv6 TestTcpIPv6
80#endif
81TEST(TestClientTest, MAYBE_TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 if (HasIPv6Enabled()) {
83 TestTcpInternal(SocketAddress("::1", 0));
84 } else {
85 LOG(LS_INFO) << "Skipping IPv6 test.";
86 }
87}