henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
| 18 | using namespace rtc; |
| 19 | |
| 20 | void 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 | |
| 34 | void 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.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 54 | TEST(TestClientTest, TestUdpIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | TestUdpInternal(SocketAddress("127.0.0.1", 0)); |
| 56 | } |
| 57 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 58 | #if defined(WEBRTC_LINUX) |
| 59 | #define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6 |
| 60 | #else |
| 61 | #define MAYBE_TestUdpIPv6 TestUdpIPv6 |
| 62 | #endif |
| 63 | TEST(TestClientTest, MAYBE_TestUdpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | 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.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 72 | TEST(TestClientTest, TestTcpIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | TestTcpInternal(SocketAddress("127.0.0.1", 0)); |
| 74 | } |
| 75 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 76 | #if defined(WEBRTC_LINUX) |
| 77 | #define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6 |
| 78 | #else |
| 79 | #define MAYBE_TestTcpIPv6 TestTcpIPv6 |
| 80 | #endif |
| 81 | TEST(TestClientTest, MAYBE_TestTcpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | if (HasIPv6Enabled()) { |
| 83 | TestTcpInternal(SocketAddress("::1", 0)); |
| 84 | } else { |
| 85 | LOG(LS_INFO) << "Skipping IPv6 test."; |
| 86 | } |
| 87 | } |