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