blob: 1cb9a1a09911c34afc89aea462b3f6c53074c1be [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"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000017#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
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
27 TestClient client(new AsyncUDPSocket(socket));
28 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);
41 AsyncTCPSocket* tcp_socket = AsyncTCPSocket::Create(
42 socket, loopback, server.address());
43 ASSERT_TRUE(tcp_socket != NULL);
44
45 TestClient client(tcp_socket);
46 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
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000059TEST(TestClientTest, TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 if (HasIPv6Enabled()) {
61 TestUdpInternal(SocketAddress("::1", 0));
62 } else {
63 LOG(LS_INFO) << "Skipping IPv6 test.";
64 }
65}
66
67// Tests whether the TestClient can connect to a server and exchange data.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000068TEST(TestClientTest, TestTcpIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 TestTcpInternal(SocketAddress("127.0.0.1", 0));
70}
71
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000072TEST(TestClientTest, TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 if (HasIPv6Enabled()) {
74 TestTcpInternal(SocketAddress("::1", 0));
75 } else {
76 LOG(LS_INFO) << "Skipping IPv6 test.";
77 }
78}