1 /*
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 #include "webrtc/test/testsupport/gtest_disable.h"
18
19 using namespace rtc;
20
TestUdpInternal(const SocketAddress & loopback)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);
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
TestTcpInternal(const SocketAddress & loopback)35 void 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.
TEST(TestClientTest,DISABLED_ON_MAC (TestUdpIPv4))55 TEST(TestClientTest, DISABLED_ON_MAC(TestUdpIPv4)) {
56 TestUdpInternal(SocketAddress("127.0.0.1", 0));
57 }
58
TEST(TestClientTest,DISABLED_ON_MAC (TestUdpIPv6))59 TEST(TestClientTest, DISABLED_ON_MAC(TestUdpIPv6)) {
60 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.
TEST(TestClientTest,DISABLED_ON_MAC (TestTcpIPv4))68 TEST(TestClientTest, DISABLED_ON_MAC(TestTcpIPv4)) {
69 TestTcpInternal(SocketAddress("127.0.0.1", 0));
70 }
71
TEST(TestClientTest,DISABLED_ON_MAC (TestTcpIPv6))72 TEST(TestClientTest, DISABLED_ON_MAC(TestTcpIPv6)) {
73 if (HasIPv6Enabled()) {
74 TestTcpInternal(SocketAddress("::1", 0));
75 } else {
76 LOG(LS_INFO) << "Skipping IPv6 test.";
77 }
78 }
79