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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/testclient.h" |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 12 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/gunit.h" |
| 14 | #include "rtc_base/nethelpers.h" |
| 15 | #include "rtc_base/physicalsocketserver.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/testechoserver.h" |
| 17 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace rtc; |
| 20 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 21 | #define MAYBE_SKIP_IPV4 \ |
| 22 | if (!HasIPv4Enabled()) { \ |
| 23 | RTC_LOG(LS_INFO) << "No IPv4... skipping"; \ |
| 24 | return; \ |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 27 | #define MAYBE_SKIP_IPV6 \ |
| 28 | if (!HasIPv6Enabled()) { \ |
| 29 | RTC_LOG(LS_INFO) << "No IPv6... skipping"; \ |
| 30 | return; \ |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 31 | } |
| 32 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | void TestUdpInternal(const SocketAddress& loopback) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 34 | Thread* main = Thread::Current(); |
| 35 | AsyncSocket* socket = |
| 36 | main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | socket->Bind(loopback); |
| 38 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 39 | TestClient client(absl::make_unique<AsyncUDPSocket>(socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | SocketAddress addr = client.address(), from; |
| 41 | EXPECT_EQ(3, client.SendTo("foo", 3, addr)); |
| 42 | EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from)); |
| 43 | EXPECT_EQ(from, addr); |
| 44 | EXPECT_TRUE(client.CheckNoPacket()); |
| 45 | } |
| 46 | |
| 47 | void TestTcpInternal(const SocketAddress& loopback) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | Thread* main = Thread::Current(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | TestEchoServer server(main, loopback); |
| 50 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 51 | AsyncSocket* socket = |
| 52 | main->socketserver()->CreateAsyncSocket(loopback.family(), SOCK_STREAM); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 53 | std::unique_ptr<AsyncTCPSocket> tcp_socket = absl::WrapUnique( |
| 54 | AsyncTCPSocket::Create(socket, loopback, server.address())); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 55 | ASSERT_TRUE(tcp_socket != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 57 | TestClient client(std::move(tcp_socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | SocketAddress addr = client.address(), from; |
| 59 | EXPECT_TRUE(client.CheckConnected()); |
| 60 | EXPECT_EQ(3, client.Send("foo", 3)); |
| 61 | EXPECT_TRUE(client.CheckNextPacket("foo", 3, &from)); |
| 62 | EXPECT_EQ(from, server.address()); |
| 63 | EXPECT_TRUE(client.CheckNoPacket()); |
| 64 | } |
| 65 | |
| 66 | // Tests whether the TestClient can send UDP to itself. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 67 | TEST(TestClientTest, TestUdpIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 68 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | TestUdpInternal(SocketAddress("127.0.0.1", 0)); |
| 70 | } |
| 71 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 72 | #if defined(WEBRTC_LINUX) |
| 73 | #define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6 |
| 74 | #else |
| 75 | #define MAYBE_TestUdpIPv6 TestUdpIPv6 |
| 76 | #endif |
| 77 | TEST(TestClientTest, MAYBE_TestUdpIPv6) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 78 | MAYBE_SKIP_IPV6; |
| 79 | TestUdpInternal(SocketAddress("::1", 0)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // 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] | 83 | TEST(TestClientTest, TestTcpIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 84 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | TestTcpInternal(SocketAddress("127.0.0.1", 0)); |
| 86 | } |
| 87 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 88 | #if defined(WEBRTC_LINUX) |
| 89 | #define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6 |
| 90 | #else |
| 91 | #define MAYBE_TestTcpIPv6 TestTcpIPv6 |
| 92 | #endif |
| 93 | TEST(TestClientTest, MAYBE_TestTcpIPv6) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 94 | MAYBE_SKIP_IPV6; |
| 95 | TestTcpInternal(SocketAddress("::1", 0)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | } |