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 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 11 | #include "webrtc/base/testclient.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | #include "webrtc/base/gunit.h" |
| 13 | #include "webrtc/base/nethelpers.h" |
| 14 | #include "webrtc/base/physicalsocketserver.h" |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 15 | #include "webrtc/base/ptr_util.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include "webrtc/base/testechoserver.h" |
| 17 | #include "webrtc/base/thread.h" |
| 18 | |
| 19 | using namespace rtc; |
| 20 | |
| 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 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 27 | TestClient client(MakeUnique<AsyncUDPSocket>(socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
| 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); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 41 | std::unique_ptr<AsyncTCPSocket> tcp_socket = |
| 42 | WrapUnique(AsyncTCPSocket::Create(socket, loopback, server.address())); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 43 | ASSERT_TRUE(tcp_socket != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 45 | TestClient client(std::move(tcp_socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 55 | TEST(TestClientTest, TestUdpIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | TestUdpInternal(SocketAddress("127.0.0.1", 0)); |
| 57 | } |
| 58 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 59 | #if defined(WEBRTC_LINUX) |
| 60 | #define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6 |
| 61 | #else |
| 62 | #define MAYBE_TestUdpIPv6 TestUdpIPv6 |
| 63 | #endif |
| 64 | TEST(TestClientTest, MAYBE_TestUdpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | if (HasIPv6Enabled()) { |
| 66 | TestUdpInternal(SocketAddress("::1", 0)); |
| 67 | } else { |
| 68 | LOG(LS_INFO) << "Skipping IPv6 test."; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // 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] | 73 | TEST(TestClientTest, TestTcpIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | TestTcpInternal(SocketAddress("127.0.0.1", 0)); |
| 75 | } |
| 76 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 77 | #if defined(WEBRTC_LINUX) |
| 78 | #define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6 |
| 79 | #else |
| 80 | #define MAYBE_TestTcpIPv6 TestTcpIPv6 |
| 81 | #endif |
| 82 | TEST(TestClientTest, MAYBE_TestTcpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | if (HasIPv6Enabled()) { |
| 84 | TestTcpInternal(SocketAddress("::1", 0)); |
| 85 | } else { |
| 86 | LOG(LS_INFO) << "Skipping IPv6 test."; |
| 87 | } |
| 88 | } |