2 * Copyright 2006 The WebRTC Project Authors. All rights reserved.
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.
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 #include "webrtc/test/testsupport/gtest_disable.h"
21 void TestUdpInternal(const SocketAddress& loopback) {
22 Thread *main = Thread::Current();
23 AsyncSocket* socket = main->socketserver()
24 ->CreateAsyncSocket(loopback.family(), SOCK_DGRAM);
25 socket->Bind(loopback);
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());
35 void TestTcpInternal(const SocketAddress& loopback) {
36 Thread *main = Thread::Current();
37 TestEchoServer server(main, loopback);
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);
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());
54 // Tests whether the TestClient can send UDP to itself.
55 TEST(TestClientTest, TestUdpIPv4) {
56 TestUdpInternal(SocketAddress("127.0.0.1", 0));
59 TEST(TestClientTest, TestUdpIPv6) {
60 if (HasIPv6Enabled()) {
61 TestUdpInternal(SocketAddress("::1", 0));
63 LOG(LS_INFO) << "Skipping IPv6 test.";
67 // Tests whether the TestClient can connect to a server and exchange data.
68 TEST(TestClientTest, TestTcpIPv4) {
69 TestTcpInternal(SocketAddress("127.0.0.1", 0));
72 TEST(TestClientTest, TestTcpIPv6) {
73 if (HasIPv6Enabled()) {
74 TestTcpInternal(SocketAddress("::1", 0));
76 LOG(LS_INFO) << "Skipping IPv6 test.";