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. |
| 54 | TEST(TestClientTest, TestUdpIPv4) { |
| 55 | TestUdpInternal(SocketAddress("127.0.0.1", 0)); |
| 56 | } |
| 57 | |
| 58 | TEST(TestClientTest, TestUdpIPv6) { |
| 59 | if (HasIPv6Enabled()) { |
| 60 | TestUdpInternal(SocketAddress("::1", 0)); |
| 61 | } else { |
| 62 | LOG(LS_INFO) << "Skipping IPv6 test."; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Tests whether the TestClient can connect to a server and exchange data. |
| 67 | TEST(TestClientTest, TestTcpIPv4) { |
| 68 | TestTcpInternal(SocketAddress("127.0.0.1", 0)); |
| 69 | } |
| 70 | |
| 71 | TEST(TestClientTest, TestTcpIPv6) { |
| 72 | if (HasIPv6Enabled()) { |
| 73 | TestTcpInternal(SocketAddress("::1", 0)); |
| 74 | } else { |
| 75 | LOG(LS_INFO) << "Skipping IPv6 test."; |
| 76 | } |
| 77 | } |