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 <math.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | #include <stdint.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <time.h> |
| 16 | #if defined(WEBRTC_POSIX) |
| 17 | #include <netinet/in.h> |
| 18 | #endif |
| 19 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 21 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include <utility> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 23 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 24 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 26 | #include "rtc_base/async_packet_socket.h" |
| 27 | #include "rtc_base/async_socket.h" |
| 28 | #include "rtc_base/async_udp_socket.h" |
| 29 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 31 | #include "rtc_base/ip_address.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 32 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 34 | #include "rtc_base/message_handler.h" |
| 35 | #include "rtc_base/message_queue.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 36 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 37 | #include "rtc_base/socket_address.h" |
| 38 | #include "rtc_base/test_client.h" |
| 39 | #include "rtc_base/test_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 40 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 42 | #include "rtc_base/time_utils.h" |
| 43 | #include "rtc_base/virtual_socket_server.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 44 | #include "test/gtest.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 46 | namespace rtc { |
| 47 | namespace { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 49 | using webrtc::testing::SSE_CLOSE; |
| 50 | using webrtc::testing::SSE_ERROR; |
| 51 | using webrtc::testing::SSE_OPEN; |
| 52 | using webrtc::testing::SSE_READ; |
| 53 | using webrtc::testing::SSE_WRITE; |
| 54 | using webrtc::testing::StreamSink; |
| 55 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | // Sends at a constant rate but with random packet sizes. |
| 57 | struct Sender : public MessageHandler { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 58 | Sender(Thread* th, AsyncSocket* s, uint32_t rt) |
| 59 | : thread(th), |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 60 | socket(absl::make_unique<AsyncUDPSocket>(s)), |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 61 | done(false), |
| 62 | rate(rt), |
| 63 | count(0) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 64 | last_send = rtc::TimeMillis(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 65 | thread->PostDelayed(RTC_FROM_HERE, NextDelay(), this, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 68 | uint32_t NextDelay() { |
| 69 | uint32_t size = (rand() % 4096) + 1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | return 1000 * size / rate; |
| 71 | } |
| 72 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 73 | void OnMessage(Message* pmsg) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | ASSERT_EQ(1u, pmsg->message_id); |
| 75 | |
| 76 | if (done) |
| 77 | return; |
| 78 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 79 | int64_t cur_time = rtc::TimeMillis(); |
| 80 | int64_t delay = cur_time - last_send; |
| 81 | uint32_t size = static_cast<uint32_t>(rate * delay / 1000); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 82 | size = std::min<uint32_t>(size, 4096); |
| 83 | size = std::max<uint32_t>(size, sizeof(uint32_t)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | |
| 85 | count += size; |
| 86 | memcpy(dummy, &cur_time, sizeof(cur_time)); |
| 87 | socket->Send(dummy, size, options); |
| 88 | |
| 89 | last_send = cur_time; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 90 | thread->PostDelayed(RTC_FROM_HERE, NextDelay(), this, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | Thread* thread; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 94 | std::unique_ptr<AsyncUDPSocket> socket; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | rtc::PacketOptions options; |
| 96 | bool done; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 97 | uint32_t rate; // bytes per second |
| 98 | uint32_t count; |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 99 | int64_t last_send; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | char dummy[4096]; |
| 101 | }; |
| 102 | |
| 103 | struct Receiver : public MessageHandler, public sigslot::has_slots<> { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 104 | Receiver(Thread* th, AsyncSocket* s, uint32_t bw) |
| 105 | : thread(th), |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 106 | socket(absl::make_unique<AsyncUDPSocket>(s)), |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 107 | bandwidth(bw), |
| 108 | done(false), |
| 109 | count(0), |
| 110 | sec_count(0), |
| 111 | sum(0), |
| 112 | sum_sq(0), |
| 113 | samples(0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | socket->SignalReadPacket.connect(this, &Receiver::OnReadPacket); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 115 | thread->PostDelayed(RTC_FROM_HERE, 1000, this, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 118 | ~Receiver() override { thread->Clear(this); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | void OnReadPacket(AsyncPacketSocket* s, |
| 121 | const char* data, |
| 122 | size_t size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 124 | const int64_t& /* packet_time_us */) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | ASSERT_EQ(socket.get(), s); |
| 126 | ASSERT_GE(size, 4U); |
| 127 | |
| 128 | count += size; |
| 129 | sec_count += size; |
| 130 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 131 | uint32_t send_time = *reinterpret_cast<const uint32_t*>(data); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 132 | uint32_t recv_time = rtc::TimeMillis(); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 133 | uint32_t delay = recv_time - send_time; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 134 | sum += delay; |
| 135 | sum_sq += delay * delay; |
| 136 | samples += 1; |
| 137 | } |
| 138 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 139 | void OnMessage(Message* pmsg) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | ASSERT_EQ(1u, pmsg->message_id); |
| 141 | |
| 142 | if (done) |
| 143 | return; |
| 144 | |
| 145 | // It is always possible for us to receive more than expected because |
| 146 | // packets can be further delayed in delivery. |
| 147 | if (bandwidth > 0) |
| 148 | ASSERT_TRUE(sec_count <= 5 * bandwidth / 4); |
| 149 | sec_count = 0; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 150 | thread->PostDelayed(RTC_FROM_HERE, 1000, this, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | Thread* thread; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 154 | std::unique_ptr<AsyncUDPSocket> socket; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 155 | uint32_t bandwidth; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 156 | bool done; |
| 157 | size_t count; |
| 158 | size_t sec_count; |
| 159 | double sum; |
| 160 | double sum_sq; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 161 | uint32_t samples; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 164 | // Note: This test uses a fake clock in addition to a virtual network. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | class VirtualSocketServerTest : public testing::Test { |
| 166 | public: |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 167 | VirtualSocketServerTest() |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 168 | : ss_(&fake_clock_), |
| 169 | thread_(&ss_), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 170 | kIPv4AnyAddress(IPAddress(INADDR_ANY), 0), |
| 171 | kIPv6AnyAddress(IPAddress(in6addr_any), 0) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 +0000 | [diff] [blame] | 173 | void CheckPortIncrementalization(const SocketAddress& post, |
| 174 | const SocketAddress& pre) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | EXPECT_EQ(post.port(), pre.port() + 1); |
| 176 | IPAddress post_ip = post.ipaddr(); |
| 177 | IPAddress pre_ip = pre.ipaddr(); |
| 178 | EXPECT_EQ(pre_ip.family(), post_ip.family()); |
| 179 | if (post_ip.family() == AF_INET) { |
| 180 | in_addr pre_ipv4 = pre_ip.ipv4_address(); |
| 181 | in_addr post_ipv4 = post_ip.ipv4_address(); |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 +0000 | [diff] [blame] | 182 | EXPECT_EQ(post_ipv4.s_addr, pre_ipv4.s_addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | } else if (post_ip.family() == AF_INET6) { |
| 184 | in6_addr post_ip6 = post_ip.ipv6_address(); |
| 185 | in6_addr pre_ip6 = pre_ip.ipv6_address(); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 186 | uint32_t* post_as_ints = reinterpret_cast<uint32_t*>(&post_ip6.s6_addr); |
| 187 | uint32_t* pre_as_ints = reinterpret_cast<uint32_t*>(&pre_ip6.s6_addr); |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 +0000 | [diff] [blame] | 188 | EXPECT_EQ(post_as_ints[3], pre_as_ints[3]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 192 | // Test a client can bind to the any address, and all sent packets will have |
| 193 | // the default route as the source address. Also, it can receive packets sent |
| 194 | // to the default route. |
| 195 | void TestDefaultRoute(const IPAddress& default_route) { |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 196 | ss_.SetDefaultRoute(default_route); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 197 | |
| 198 | // Create client1 bound to the any address. |
| 199 | AsyncSocket* socket = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 200 | ss_.CreateAsyncSocket(default_route.family(), SOCK_DGRAM); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 201 | socket->Bind(EmptySocketAddressWithFamily(default_route.family())); |
| 202 | SocketAddress client1_any_addr = socket->GetLocalAddress(); |
| 203 | EXPECT_TRUE(client1_any_addr.IsAnyIP()); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 204 | auto client1 = absl::make_unique<TestClient>( |
| 205 | absl::make_unique<AsyncUDPSocket>(socket), &fake_clock_); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 206 | |
| 207 | // Create client2 bound to the default route. |
| 208 | AsyncSocket* socket2 = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 209 | ss_.CreateAsyncSocket(default_route.family(), SOCK_DGRAM); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 210 | socket2->Bind(SocketAddress(default_route, 0)); |
| 211 | SocketAddress client2_addr = socket2->GetLocalAddress(); |
| 212 | EXPECT_FALSE(client2_addr.IsAnyIP()); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 213 | auto client2 = absl::make_unique<TestClient>( |
| 214 | absl::make_unique<AsyncUDPSocket>(socket2), &fake_clock_); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 215 | |
| 216 | // Client1 sends to client2, client2 should see the default route as |
| 217 | // client1's address. |
| 218 | SocketAddress client1_addr; |
| 219 | EXPECT_EQ(6, client1->SendTo("bizbaz", 6, client2_addr)); |
| 220 | EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &client1_addr)); |
| 221 | EXPECT_EQ(client1_addr, |
| 222 | SocketAddress(default_route, client1_any_addr.port())); |
| 223 | |
| 224 | // Client2 can send back to client1's default route address. |
| 225 | EXPECT_EQ(3, client2->SendTo("foo", 3, client1_addr)); |
| 226 | EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &client2_addr)); |
| 227 | } |
| 228 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 229 | void BasicTest(const SocketAddress& initial_addr) { |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 230 | AsyncSocket* socket = |
| 231 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | socket->Bind(initial_addr); |
| 233 | SocketAddress server_addr = socket->GetLocalAddress(); |
| 234 | // Make sure VSS didn't switch families on us. |
| 235 | EXPECT_EQ(server_addr.family(), initial_addr.family()); |
| 236 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 237 | auto client1 = absl::make_unique<TestClient>( |
| 238 | absl::make_unique<AsyncUDPSocket>(socket), &fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | AsyncSocket* socket2 = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 240 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 241 | auto client2 = absl::make_unique<TestClient>( |
| 242 | absl::make_unique<AsyncUDPSocket>(socket2), &fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 243 | |
| 244 | SocketAddress client2_addr; |
| 245 | EXPECT_EQ(3, client2->SendTo("foo", 3, server_addr)); |
| 246 | EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &client2_addr)); |
| 247 | |
| 248 | SocketAddress client1_addr; |
| 249 | EXPECT_EQ(6, client1->SendTo("bizbaz", 6, client2_addr)); |
| 250 | EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &client1_addr)); |
| 251 | EXPECT_EQ(client1_addr, server_addr); |
| 252 | |
| 253 | SocketAddress empty = EmptySocketAddressWithFamily(initial_addr.family()); |
| 254 | for (int i = 0; i < 10; i++) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 255 | client2 = absl::make_unique<TestClient>( |
| 256 | absl::WrapUnique(AsyncUDPSocket::Create(&ss_, empty)), &fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 257 | |
| 258 | SocketAddress next_client2_addr; |
| 259 | EXPECT_EQ(3, client2->SendTo("foo", 3, server_addr)); |
| 260 | EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &next_client2_addr)); |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 +0000 | [diff] [blame] | 261 | CheckPortIncrementalization(next_client2_addr, client2_addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 262 | // EXPECT_EQ(next_client2_addr.port(), client2_addr.port() + 1); |
| 263 | |
| 264 | SocketAddress server_addr2; |
| 265 | EXPECT_EQ(6, client1->SendTo("bizbaz", 6, next_client2_addr)); |
| 266 | EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &server_addr2)); |
| 267 | EXPECT_EQ(server_addr2, server_addr); |
| 268 | |
| 269 | client2_addr = next_client2_addr; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // initial_addr should be made from either INADDR_ANY or in6addr_any. |
| 274 | void ConnectTest(const SocketAddress& initial_addr) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 275 | StreamSink sink; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 276 | SocketAddress accept_addr; |
| 277 | const SocketAddress kEmptyAddr = |
| 278 | EmptySocketAddressWithFamily(initial_addr.family()); |
| 279 | |
| 280 | // Create client |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 281 | std::unique_ptr<AsyncSocket> client = absl::WrapUnique( |
| 282 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 283 | sink.Monitor(client.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 284 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); |
| 285 | EXPECT_TRUE(client->GetLocalAddress().IsNil()); |
| 286 | |
| 287 | // Create server |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 288 | std::unique_ptr<AsyncSocket> server = absl::WrapUnique( |
| 289 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 290 | sink.Monitor(server.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 291 | EXPECT_NE(0, server->Listen(5)); // Bind required |
| 292 | EXPECT_EQ(0, server->Bind(initial_addr)); |
| 293 | EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); |
| 294 | EXPECT_EQ(0, server->Listen(5)); |
| 295 | EXPECT_EQ(server->GetState(), AsyncSocket::CS_CONNECTING); |
| 296 | |
| 297 | // No pending server connections |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 298 | EXPECT_FALSE(sink.Check(server.get(), SSE_READ)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 299 | EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 300 | EXPECT_EQ(AF_UNSPEC, accept_addr.family()); |
| 301 | |
| 302 | // Attempt connect to listening socket |
| 303 | EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 304 | EXPECT_NE(client->GetLocalAddress(), kEmptyAddr); // Implicit Bind |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 305 | EXPECT_NE(AF_UNSPEC, client->GetLocalAddress().family()); // Implicit Bind |
| 306 | EXPECT_NE(client->GetLocalAddress(), server->GetLocalAddress()); |
| 307 | |
| 308 | // Client is connecting |
| 309 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 310 | EXPECT_FALSE(sink.Check(client.get(), SSE_OPEN)); |
| 311 | EXPECT_FALSE(sink.Check(client.get(), SSE_CLOSE)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 312 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 313 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 314 | |
| 315 | // Client still connecting |
| 316 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 317 | EXPECT_FALSE(sink.Check(client.get(), SSE_OPEN)); |
| 318 | EXPECT_FALSE(sink.Check(client.get(), SSE_CLOSE)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | |
| 320 | // Server has pending connection |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 321 | EXPECT_TRUE(sink.Check(server.get(), SSE_READ)); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 322 | std::unique_ptr<Socket> accepted = |
| 323 | absl::WrapUnique(server->Accept(&accept_addr)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 324 | EXPECT_TRUE(nullptr != accepted); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 325 | EXPECT_NE(accept_addr, kEmptyAddr); |
| 326 | EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr); |
| 327 | |
| 328 | EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CONNECTED); |
| 329 | EXPECT_EQ(accepted->GetLocalAddress(), server->GetLocalAddress()); |
| 330 | EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress()); |
| 331 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 332 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 333 | |
| 334 | // Client has connected |
| 335 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTED); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 336 | EXPECT_TRUE(sink.Check(client.get(), SSE_OPEN)); |
| 337 | EXPECT_FALSE(sink.Check(client.get(), SSE_CLOSE)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 338 | EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); |
| 339 | EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); |
| 340 | } |
| 341 | |
| 342 | void ConnectToNonListenerTest(const SocketAddress& initial_addr) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 343 | StreamSink sink; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 344 | SocketAddress accept_addr; |
| 345 | const SocketAddress nil_addr; |
| 346 | const SocketAddress empty_addr = |
| 347 | EmptySocketAddressWithFamily(initial_addr.family()); |
| 348 | |
| 349 | // Create client |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 350 | std::unique_ptr<AsyncSocket> client = absl::WrapUnique( |
| 351 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 352 | sink.Monitor(client.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 353 | |
| 354 | // Create server |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 355 | std::unique_ptr<AsyncSocket> server = absl::WrapUnique( |
| 356 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 357 | sink.Monitor(server.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 358 | EXPECT_EQ(0, server->Bind(initial_addr)); |
| 359 | EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); |
| 360 | // Attempt connect to non-listening socket |
| 361 | EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); |
| 362 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 363 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 364 | |
| 365 | // No pending server connections |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 366 | EXPECT_FALSE(sink.Check(server.get(), SSE_READ)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 367 | EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 368 | EXPECT_EQ(accept_addr, nil_addr); |
| 369 | |
| 370 | // Connection failed |
| 371 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 372 | EXPECT_FALSE(sink.Check(client.get(), SSE_OPEN)); |
| 373 | EXPECT_TRUE(sink.Check(client.get(), SSE_ERROR)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 374 | EXPECT_EQ(client->GetRemoteAddress(), nil_addr); |
| 375 | } |
| 376 | |
| 377 | void CloseDuringConnectTest(const SocketAddress& initial_addr) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 378 | StreamSink sink; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 379 | SocketAddress accept_addr; |
| 380 | const SocketAddress empty_addr = |
| 381 | EmptySocketAddressWithFamily(initial_addr.family()); |
| 382 | |
| 383 | // Create client and server |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 384 | std::unique_ptr<AsyncSocket> client( |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 385 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 386 | sink.Monitor(client.get()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 387 | std::unique_ptr<AsyncSocket> server( |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 388 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 389 | sink.Monitor(server.get()); |
| 390 | |
| 391 | // Initiate connect |
| 392 | EXPECT_EQ(0, server->Bind(initial_addr)); |
| 393 | EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); |
| 394 | |
| 395 | EXPECT_EQ(0, server->Listen(5)); |
| 396 | EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); |
| 397 | |
| 398 | // Server close before socket enters accept queue |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 399 | EXPECT_FALSE(sink.Check(server.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 400 | server->Close(); |
| 401 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 402 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 403 | |
| 404 | // Result: connection failed |
| 405 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 406 | EXPECT_TRUE(sink.Check(client.get(), SSE_ERROR)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 407 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 408 | server.reset(ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 409 | sink.Monitor(server.get()); |
| 410 | |
| 411 | // Initiate connect |
| 412 | EXPECT_EQ(0, server->Bind(initial_addr)); |
| 413 | EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); |
| 414 | |
| 415 | EXPECT_EQ(0, server->Listen(5)); |
| 416 | EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); |
| 417 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 418 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 419 | |
| 420 | // Server close while socket is in accept queue |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 421 | EXPECT_TRUE(sink.Check(server.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 422 | server->Close(); |
| 423 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 424 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 425 | |
| 426 | // Result: connection failed |
| 427 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 428 | EXPECT_TRUE(sink.Check(client.get(), SSE_ERROR)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 429 | |
| 430 | // New server |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 431 | server.reset(ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 432 | sink.Monitor(server.get()); |
| 433 | |
| 434 | // Initiate connect |
| 435 | EXPECT_EQ(0, server->Bind(initial_addr)); |
| 436 | EXPECT_EQ(server->GetLocalAddress().family(), initial_addr.family()); |
| 437 | |
| 438 | EXPECT_EQ(0, server->Listen(5)); |
| 439 | EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); |
| 440 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 441 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 442 | |
| 443 | // Server accepts connection |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 444 | EXPECT_TRUE(sink.Check(server.get(), SSE_READ)); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 445 | std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 446 | ASSERT_TRUE(nullptr != accepted.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 447 | sink.Monitor(accepted.get()); |
| 448 | |
| 449 | // Client closes before connection complets |
| 450 | EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CONNECTED); |
| 451 | |
| 452 | // Connected message has not been processed yet. |
| 453 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CONNECTING); |
| 454 | client->Close(); |
| 455 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 456 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 457 | |
| 458 | // Result: accepted socket closes |
| 459 | EXPECT_EQ(accepted->GetState(), AsyncSocket::CS_CLOSED); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 460 | EXPECT_TRUE(sink.Check(accepted.get(), SSE_CLOSE)); |
| 461 | EXPECT_FALSE(sink.Check(client.get(), SSE_CLOSE)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void CloseTest(const SocketAddress& initial_addr) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 465 | StreamSink sink; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 466 | const SocketAddress kEmptyAddr; |
| 467 | |
| 468 | // Create clients |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 469 | std::unique_ptr<AsyncSocket> a = absl::WrapUnique( |
| 470 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 471 | sink.Monitor(a.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 472 | a->Bind(initial_addr); |
| 473 | EXPECT_EQ(a->GetLocalAddress().family(), initial_addr.family()); |
| 474 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 475 | std::unique_ptr<AsyncSocket> b = absl::WrapUnique( |
| 476 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 477 | sink.Monitor(b.get()); |
| 478 | b->Bind(initial_addr); |
| 479 | EXPECT_EQ(b->GetLocalAddress().family(), initial_addr.family()); |
| 480 | |
| 481 | EXPECT_EQ(0, a->Connect(b->GetLocalAddress())); |
| 482 | EXPECT_EQ(0, b->Connect(a->GetLocalAddress())); |
| 483 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 484 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 485 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 486 | EXPECT_TRUE(sink.Check(a.get(), SSE_OPEN)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 487 | EXPECT_EQ(a->GetState(), AsyncSocket::CS_CONNECTED); |
| 488 | EXPECT_EQ(a->GetRemoteAddress(), b->GetLocalAddress()); |
| 489 | |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 490 | EXPECT_TRUE(sink.Check(b.get(), SSE_OPEN)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 491 | EXPECT_EQ(b->GetState(), AsyncSocket::CS_CONNECTED); |
| 492 | EXPECT_EQ(b->GetRemoteAddress(), a->GetLocalAddress()); |
| 493 | |
| 494 | EXPECT_EQ(1, a->Send("a", 1)); |
| 495 | b->Close(); |
| 496 | EXPECT_EQ(1, a->Send("b", 1)); |
| 497 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 498 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 499 | |
| 500 | char buffer[10]; |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 501 | EXPECT_FALSE(sink.Check(b.get(), SSE_READ)); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 502 | EXPECT_EQ(-1, b->Recv(buffer, 10, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 503 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 504 | EXPECT_TRUE(sink.Check(a.get(), SSE_CLOSE)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 505 | EXPECT_EQ(a->GetState(), AsyncSocket::CS_CLOSED); |
| 506 | EXPECT_EQ(a->GetRemoteAddress(), kEmptyAddr); |
| 507 | |
| 508 | // No signal for Closer |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 509 | EXPECT_FALSE(sink.Check(b.get(), SSE_CLOSE)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 510 | EXPECT_EQ(b->GetState(), AsyncSocket::CS_CLOSED); |
| 511 | EXPECT_EQ(b->GetRemoteAddress(), kEmptyAddr); |
| 512 | } |
| 513 | |
| 514 | void TcpSendTest(const SocketAddress& initial_addr) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 515 | StreamSink sink; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 516 | const SocketAddress kEmptyAddr; |
| 517 | |
| 518 | // Connect two sockets |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 519 | std::unique_ptr<AsyncSocket> a = absl::WrapUnique( |
| 520 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 521 | sink.Monitor(a.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 522 | a->Bind(initial_addr); |
| 523 | EXPECT_EQ(a->GetLocalAddress().family(), initial_addr.family()); |
| 524 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 525 | std::unique_ptr<AsyncSocket> b = absl::WrapUnique( |
| 526 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 527 | sink.Monitor(b.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 528 | b->Bind(initial_addr); |
| 529 | EXPECT_EQ(b->GetLocalAddress().family(), initial_addr.family()); |
| 530 | |
| 531 | EXPECT_EQ(0, a->Connect(b->GetLocalAddress())); |
| 532 | EXPECT_EQ(0, b->Connect(a->GetLocalAddress())); |
| 533 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 534 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 535 | |
| 536 | const size_t kBufferSize = 2000; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 537 | ss_.set_send_buffer_capacity(kBufferSize); |
| 538 | ss_.set_recv_buffer_capacity(kBufferSize); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 539 | |
| 540 | const size_t kDataSize = 5000; |
| 541 | char send_buffer[kDataSize], recv_buffer[kDataSize]; |
| 542 | for (size_t i = 0; i < kDataSize; ++i) |
| 543 | send_buffer[i] = static_cast<char>(i % 256); |
| 544 | memset(recv_buffer, 0, sizeof(recv_buffer)); |
| 545 | size_t send_pos = 0, recv_pos = 0; |
| 546 | |
| 547 | // Can't send more than send buffer in one write |
| 548 | int result = a->Send(send_buffer + send_pos, kDataSize - send_pos); |
| 549 | EXPECT_EQ(static_cast<int>(kBufferSize), result); |
| 550 | send_pos += result; |
| 551 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 552 | ss_.ProcessMessagesUntilIdle(); |
| 553 | EXPECT_FALSE(sink.Check(a.get(), SSE_WRITE)); |
| 554 | EXPECT_TRUE(sink.Check(b.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 555 | |
| 556 | // Receive buffer is already filled, fill send buffer again |
| 557 | result = a->Send(send_buffer + send_pos, kDataSize - send_pos); |
| 558 | EXPECT_EQ(static_cast<int>(kBufferSize), result); |
| 559 | send_pos += result; |
| 560 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 561 | ss_.ProcessMessagesUntilIdle(); |
| 562 | EXPECT_FALSE(sink.Check(a.get(), SSE_WRITE)); |
| 563 | EXPECT_FALSE(sink.Check(b.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 564 | |
| 565 | // No more room in send or receive buffer |
| 566 | result = a->Send(send_buffer + send_pos, kDataSize - send_pos); |
| 567 | EXPECT_EQ(-1, result); |
| 568 | EXPECT_TRUE(a->IsBlocking()); |
| 569 | |
| 570 | // Read a subset of the data |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 571 | result = b->Recv(recv_buffer + recv_pos, 500, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 572 | EXPECT_EQ(500, result); |
| 573 | recv_pos += result; |
| 574 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 575 | ss_.ProcessMessagesUntilIdle(); |
| 576 | EXPECT_TRUE(sink.Check(a.get(), SSE_WRITE)); |
| 577 | EXPECT_TRUE(sink.Check(b.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 578 | |
| 579 | // Room for more on the sending side |
| 580 | result = a->Send(send_buffer + send_pos, kDataSize - send_pos); |
| 581 | EXPECT_EQ(500, result); |
| 582 | send_pos += result; |
| 583 | |
| 584 | // Empty the recv buffer |
| 585 | while (true) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 586 | result = b->Recv(recv_buffer + recv_pos, kDataSize - recv_pos, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 587 | if (result < 0) { |
| 588 | EXPECT_EQ(-1, result); |
| 589 | EXPECT_TRUE(b->IsBlocking()); |
| 590 | break; |
| 591 | } |
| 592 | recv_pos += result; |
| 593 | } |
| 594 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 595 | ss_.ProcessMessagesUntilIdle(); |
| 596 | EXPECT_TRUE(sink.Check(b.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 597 | |
| 598 | // Continue to empty the recv buffer |
| 599 | while (true) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 600 | result = b->Recv(recv_buffer + recv_pos, kDataSize - recv_pos, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 601 | if (result < 0) { |
| 602 | EXPECT_EQ(-1, result); |
| 603 | EXPECT_TRUE(b->IsBlocking()); |
| 604 | break; |
| 605 | } |
| 606 | recv_pos += result; |
| 607 | } |
| 608 | |
| 609 | // Send last of the data |
| 610 | result = a->Send(send_buffer + send_pos, kDataSize - send_pos); |
| 611 | EXPECT_EQ(500, result); |
| 612 | send_pos += result; |
| 613 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 614 | ss_.ProcessMessagesUntilIdle(); |
| 615 | EXPECT_TRUE(sink.Check(b.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 616 | |
| 617 | // Receive the last of the data |
| 618 | while (true) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 619 | result = b->Recv(recv_buffer + recv_pos, kDataSize - recv_pos, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 620 | if (result < 0) { |
| 621 | EXPECT_EQ(-1, result); |
| 622 | EXPECT_TRUE(b->IsBlocking()); |
| 623 | break; |
| 624 | } |
| 625 | recv_pos += result; |
| 626 | } |
| 627 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 628 | ss_.ProcessMessagesUntilIdle(); |
| 629 | EXPECT_FALSE(sink.Check(b.get(), SSE_READ)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 630 | |
| 631 | // The received data matches the sent data |
| 632 | EXPECT_EQ(kDataSize, send_pos); |
| 633 | EXPECT_EQ(kDataSize, recv_pos); |
| 634 | EXPECT_EQ(0, memcmp(recv_buffer, send_buffer, kDataSize)); |
| 635 | } |
| 636 | |
| 637 | void TcpSendsPacketsInOrderTest(const SocketAddress& initial_addr) { |
| 638 | const SocketAddress kEmptyAddr; |
| 639 | |
| 640 | // Connect two sockets |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 641 | std::unique_ptr<AsyncSocket> a = absl::WrapUnique( |
| 642 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
| 643 | std::unique_ptr<AsyncSocket> b = absl::WrapUnique( |
| 644 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_STREAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 645 | a->Bind(initial_addr); |
| 646 | EXPECT_EQ(a->GetLocalAddress().family(), initial_addr.family()); |
| 647 | |
| 648 | b->Bind(initial_addr); |
| 649 | EXPECT_EQ(b->GetLocalAddress().family(), initial_addr.family()); |
| 650 | |
| 651 | EXPECT_EQ(0, a->Connect(b->GetLocalAddress())); |
| 652 | EXPECT_EQ(0, b->Connect(a->GetLocalAddress())); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 653 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 654 | |
| 655 | // First, deliver all packets in 0 ms. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 656 | char buffer[2] = {0, 0}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 657 | const char cNumPackets = 10; |
| 658 | for (char i = 0; i < cNumPackets; ++i) { |
| 659 | buffer[0] = '0' + i; |
| 660 | EXPECT_EQ(1, a->Send(buffer, 1)); |
| 661 | } |
| 662 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 663 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 664 | |
| 665 | for (char i = 0; i < cNumPackets; ++i) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 666 | EXPECT_EQ(1, b->Recv(buffer, sizeof(buffer), nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 667 | EXPECT_EQ(static_cast<char>('0' + i), buffer[0]); |
| 668 | } |
| 669 | |
| 670 | // Next, deliver packets at random intervals |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 671 | const uint32_t mean = 50; |
| 672 | const uint32_t stddev = 50; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 673 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 674 | ss_.set_delay_mean(mean); |
| 675 | ss_.set_delay_stddev(stddev); |
| 676 | ss_.UpdateDelayDistribution(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 677 | |
| 678 | for (char i = 0; i < cNumPackets; ++i) { |
| 679 | buffer[0] = 'A' + i; |
| 680 | EXPECT_EQ(1, a->Send(buffer, 1)); |
| 681 | } |
| 682 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 683 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 684 | |
| 685 | for (char i = 0; i < cNumPackets; ++i) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 686 | EXPECT_EQ(1, b->Recv(buffer, sizeof(buffer), nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 687 | EXPECT_EQ(static_cast<char>('A' + i), buffer[0]); |
| 688 | } |
| 689 | } |
| 690 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 691 | // It is important that initial_addr's port has to be 0 such that the |
| 692 | // incremental port behavior could ensure the 2 Binds result in different |
| 693 | // address. |
| 694 | void BandwidthTest(const SocketAddress& initial_addr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 695 | AsyncSocket* send_socket = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 696 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 697 | AsyncSocket* recv_socket = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 698 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 699 | ASSERT_EQ(0, send_socket->Bind(initial_addr)); |
| 700 | ASSERT_EQ(0, recv_socket->Bind(initial_addr)); |
| 701 | EXPECT_EQ(send_socket->GetLocalAddress().family(), initial_addr.family()); |
| 702 | EXPECT_EQ(recv_socket->GetLocalAddress().family(), initial_addr.family()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 703 | ASSERT_EQ(0, send_socket->Connect(recv_socket->GetLocalAddress())); |
| 704 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 705 | uint32_t bandwidth = 64 * 1024; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 706 | ss_.set_bandwidth(bandwidth); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 707 | |
| 708 | Thread* pthMain = Thread::Current(); |
| 709 | Sender sender(pthMain, send_socket, 80 * 1024); |
| 710 | Receiver receiver(pthMain, recv_socket, bandwidth); |
| 711 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 712 | // Allow the sender to run for 5 (simulated) seconds, then be stopped for 5 |
| 713 | // seconds. |
| 714 | SIMULATED_WAIT(false, 5000, fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 715 | sender.done = true; |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 716 | SIMULATED_WAIT(false, 5000, fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 717 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 718 | // Ensure the observed bandwidth fell within a reasonable margin of error. |
| 719 | EXPECT_TRUE(receiver.count >= 5 * 3 * bandwidth / 4); |
| 720 | EXPECT_TRUE(receiver.count <= 6 * bandwidth); // queue could drain for 1s |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 721 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 722 | ss_.set_bandwidth(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 723 | } |
| 724 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 725 | // It is important that initial_addr's port has to be 0 such that the |
| 726 | // incremental port behavior could ensure the 2 Binds result in different |
| 727 | // address. |
| 728 | void DelayTest(const SocketAddress& initial_addr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 729 | time_t seed = ::time(nullptr); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 730 | RTC_LOG(LS_VERBOSE) << "seed = " << seed; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 731 | srand(static_cast<unsigned int>(seed)); |
| 732 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 733 | const uint32_t mean = 2000; |
| 734 | const uint32_t stddev = 500; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 735 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 736 | ss_.set_delay_mean(mean); |
| 737 | ss_.set_delay_stddev(stddev); |
| 738 | ss_.UpdateDelayDistribution(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 739 | |
| 740 | AsyncSocket* send_socket = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 741 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 742 | AsyncSocket* recv_socket = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 743 | ss_.CreateAsyncSocket(initial_addr.family(), SOCK_DGRAM); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 744 | ASSERT_EQ(0, send_socket->Bind(initial_addr)); |
| 745 | ASSERT_EQ(0, recv_socket->Bind(initial_addr)); |
| 746 | EXPECT_EQ(send_socket->GetLocalAddress().family(), initial_addr.family()); |
| 747 | EXPECT_EQ(recv_socket->GetLocalAddress().family(), initial_addr.family()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 748 | ASSERT_EQ(0, send_socket->Connect(recv_socket->GetLocalAddress())); |
| 749 | |
| 750 | Thread* pthMain = Thread::Current(); |
| 751 | // Avg packet size is 2K, so at 200KB/s for 10s, we should see about |
| 752 | // 1000 packets, which is necessary to get a good distribution. |
| 753 | Sender sender(pthMain, send_socket, 100 * 2 * 1024); |
| 754 | Receiver receiver(pthMain, recv_socket, 0); |
| 755 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 756 | // Simulate 10 seconds of packets being sent, then check the observed delay |
| 757 | // distribution. |
| 758 | SIMULATED_WAIT(false, 10000, fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 759 | sender.done = receiver.done = true; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 760 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 761 | |
| 762 | const double sample_mean = receiver.sum / receiver.samples; |
| 763 | double num = |
| 764 | receiver.samples * receiver.sum_sq - receiver.sum * receiver.sum; |
| 765 | double den = receiver.samples * (receiver.samples - 1); |
| 766 | const double sample_stddev = sqrt(num / den); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 767 | RTC_LOG(LS_VERBOSE) << "mean=" << sample_mean |
| 768 | << " stddev=" << sample_stddev; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 769 | |
| 770 | EXPECT_LE(500u, receiver.samples); |
| 771 | // We initially used a 0.1 fudge factor, but on the build machine, we |
| 772 | // have seen the value differ by as much as 0.13. |
| 773 | EXPECT_NEAR(mean, sample_mean, 0.15 * mean); |
| 774 | EXPECT_NEAR(stddev, sample_stddev, 0.15 * stddev); |
| 775 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 776 | ss_.set_delay_mean(0); |
| 777 | ss_.set_delay_stddev(0); |
| 778 | ss_.UpdateDelayDistribution(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | // Test cross-family communication between a client bound to client_addr and a |
| 782 | // server bound to server_addr. shouldSucceed indicates if communication is |
| 783 | // expected to work or not. |
| 784 | void CrossFamilyConnectionTest(const SocketAddress& client_addr, |
| 785 | const SocketAddress& server_addr, |
| 786 | bool shouldSucceed) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 787 | StreamSink sink; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 788 | SocketAddress accept_address; |
| 789 | const SocketAddress kEmptyAddr; |
| 790 | |
| 791 | // Client gets a IPv4 address |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 792 | std::unique_ptr<AsyncSocket> client = absl::WrapUnique( |
| 793 | ss_.CreateAsyncSocket(client_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 794 | sink.Monitor(client.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 795 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); |
| 796 | EXPECT_EQ(client->GetLocalAddress(), kEmptyAddr); |
| 797 | client->Bind(client_addr); |
| 798 | |
| 799 | // Server gets a non-mapped non-any IPv6 address. |
| 800 | // IPv4 sockets should not be able to connect to this. |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 801 | std::unique_ptr<AsyncSocket> server = absl::WrapUnique( |
| 802 | ss_.CreateAsyncSocket(server_addr.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 803 | sink.Monitor(server.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 804 | server->Bind(server_addr); |
| 805 | server->Listen(5); |
| 806 | |
| 807 | if (shouldSucceed) { |
| 808 | EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 809 | ss_.ProcessMessagesUntilIdle(); |
| 810 | EXPECT_TRUE(sink.Check(server.get(), SSE_READ)); |
| 811 | std::unique_ptr<Socket> accepted = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 812 | absl::WrapUnique(server->Accept(&accept_address)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 813 | EXPECT_TRUE(nullptr != accepted); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | EXPECT_NE(kEmptyAddr, accept_address); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 815 | ss_.ProcessMessagesUntilIdle(); |
| 816 | EXPECT_TRUE(sink.Check(client.get(), SSE_OPEN)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 817 | EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); |
| 818 | } else { |
| 819 | // Check that the connection failed. |
| 820 | EXPECT_EQ(-1, client->Connect(server->GetLocalAddress())); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 821 | ss_.ProcessMessagesUntilIdle(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 822 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 823 | EXPECT_FALSE(sink.Check(server.get(), SSE_READ)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 824 | EXPECT_TRUE(nullptr == server->Accept(&accept_address)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 825 | EXPECT_EQ(accept_address, kEmptyAddr); |
| 826 | EXPECT_EQ(client->GetState(), AsyncSocket::CS_CLOSED); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 827 | EXPECT_FALSE(sink.Check(client.get(), SSE_OPEN)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 828 | EXPECT_EQ(client->GetRemoteAddress(), kEmptyAddr); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // Test cross-family datagram sending between a client bound to client_addr |
| 833 | // and a server bound to server_addr. shouldSucceed indicates if sending is |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 834 | // expected to succeed or not. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 835 | void CrossFamilyDatagramTest(const SocketAddress& client_addr, |
| 836 | const SocketAddress& server_addr, |
| 837 | bool shouldSucceed) { |
Steve Anton | 31e5bf5 | 2018-05-07 10:42:55 -0700 | [diff] [blame] | 838 | AsyncSocket* socket = ss_.CreateAsyncSocket(AF_INET, SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 839 | socket->Bind(server_addr); |
| 840 | SocketAddress bound_server_addr = socket->GetLocalAddress(); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 841 | auto client1 = absl::make_unique<TestClient>( |
| 842 | absl::make_unique<AsyncUDPSocket>(socket), &fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 843 | |
Steve Anton | 31e5bf5 | 2018-05-07 10:42:55 -0700 | [diff] [blame] | 844 | AsyncSocket* socket2 = ss_.CreateAsyncSocket(AF_INET, SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 845 | socket2->Bind(client_addr); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 846 | auto client2 = absl::make_unique<TestClient>( |
| 847 | absl::make_unique<AsyncUDPSocket>(socket2), &fake_clock_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 848 | SocketAddress client2_addr; |
| 849 | |
| 850 | if (shouldSucceed) { |
| 851 | EXPECT_EQ(3, client2->SendTo("foo", 3, bound_server_addr)); |
| 852 | EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &client2_addr)); |
| 853 | SocketAddress client1_addr; |
| 854 | EXPECT_EQ(6, client1->SendTo("bizbaz", 6, client2_addr)); |
| 855 | EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &client1_addr)); |
| 856 | EXPECT_EQ(client1_addr, bound_server_addr); |
| 857 | } else { |
| 858 | EXPECT_EQ(-1, client2->SendTo("foo", 3, bound_server_addr)); |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 859 | EXPECT_TRUE(client1->CheckNoPacket()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | protected: |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 864 | rtc::ScopedFakeClock fake_clock_; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 865 | VirtualSocketServer ss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 866 | AutoSocketServerThread thread_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 867 | const SocketAddress kIPv4AnyAddress; |
| 868 | const SocketAddress kIPv6AnyAddress; |
| 869 | }; |
| 870 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 871 | TEST_F(VirtualSocketServerTest, basic_v4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 872 | SocketAddress ipv4_test_addr(IPAddress(INADDR_ANY), 5000); |
| 873 | BasicTest(ipv4_test_addr); |
| 874 | } |
| 875 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 876 | TEST_F(VirtualSocketServerTest, basic_v6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 877 | SocketAddress ipv6_test_addr(IPAddress(in6addr_any), 5000); |
| 878 | BasicTest(ipv6_test_addr); |
| 879 | } |
| 880 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 881 | TEST_F(VirtualSocketServerTest, TestDefaultRoute_v4) { |
| 882 | IPAddress ipv4_default_addr(0x01020304); |
| 883 | TestDefaultRoute(ipv4_default_addr); |
| 884 | } |
| 885 | |
| 886 | TEST_F(VirtualSocketServerTest, TestDefaultRoute_v6) { |
| 887 | IPAddress ipv6_default_addr; |
| 888 | EXPECT_TRUE( |
| 889 | IPFromString("2401:fa00:4:1000:be30:5bff:fee5:c3", &ipv6_default_addr)); |
| 890 | TestDefaultRoute(ipv6_default_addr); |
| 891 | } |
| 892 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 893 | TEST_F(VirtualSocketServerTest, connect_v4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 894 | ConnectTest(kIPv4AnyAddress); |
| 895 | } |
| 896 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 897 | TEST_F(VirtualSocketServerTest, connect_v6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 898 | ConnectTest(kIPv6AnyAddress); |
| 899 | } |
| 900 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 901 | TEST_F(VirtualSocketServerTest, connect_to_non_listener_v4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 902 | ConnectToNonListenerTest(kIPv4AnyAddress); |
| 903 | } |
| 904 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 905 | TEST_F(VirtualSocketServerTest, connect_to_non_listener_v6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 906 | ConnectToNonListenerTest(kIPv6AnyAddress); |
| 907 | } |
| 908 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 909 | TEST_F(VirtualSocketServerTest, close_during_connect_v4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 910 | CloseDuringConnectTest(kIPv4AnyAddress); |
| 911 | } |
| 912 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 913 | TEST_F(VirtualSocketServerTest, close_during_connect_v6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 914 | CloseDuringConnectTest(kIPv6AnyAddress); |
| 915 | } |
| 916 | |
| 917 | TEST_F(VirtualSocketServerTest, close_v4) { |
| 918 | CloseTest(kIPv4AnyAddress); |
| 919 | } |
| 920 | |
| 921 | TEST_F(VirtualSocketServerTest, close_v6) { |
| 922 | CloseTest(kIPv6AnyAddress); |
| 923 | } |
| 924 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 925 | TEST_F(VirtualSocketServerTest, tcp_send_v4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 926 | TcpSendTest(kIPv4AnyAddress); |
| 927 | } |
| 928 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 929 | TEST_F(VirtualSocketServerTest, tcp_send_v6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 930 | TcpSendTest(kIPv6AnyAddress); |
| 931 | } |
| 932 | |
| 933 | TEST_F(VirtualSocketServerTest, TcpSendsPacketsInOrder_v4) { |
| 934 | TcpSendsPacketsInOrderTest(kIPv4AnyAddress); |
| 935 | } |
| 936 | |
| 937 | TEST_F(VirtualSocketServerTest, TcpSendsPacketsInOrder_v6) { |
| 938 | TcpSendsPacketsInOrderTest(kIPv6AnyAddress); |
| 939 | } |
| 940 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 941 | TEST_F(VirtualSocketServerTest, bandwidth_v4) { |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 942 | BandwidthTest(kIPv4AnyAddress); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 943 | } |
| 944 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 945 | TEST_F(VirtualSocketServerTest, bandwidth_v6) { |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 946 | BandwidthTest(kIPv6AnyAddress); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 947 | } |
| 948 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 949 | TEST_F(VirtualSocketServerTest, delay_v4) { |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 950 | DelayTest(kIPv4AnyAddress); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 951 | } |
| 952 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 953 | TEST_F(VirtualSocketServerTest, delay_v6) { |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 954 | DelayTest(kIPv6AnyAddress); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | // Works, receiving socket sees 127.0.0.2. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 958 | TEST_F(VirtualSocketServerTest, CanConnectFromMappedIPv6ToIPv4Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 959 | CrossFamilyConnectionTest(SocketAddress("::ffff:127.0.0.2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 960 | SocketAddress("0.0.0.0", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | // Fails. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 964 | TEST_F(VirtualSocketServerTest, CantConnectFromUnMappedIPv6ToIPv4Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 965 | CrossFamilyConnectionTest(SocketAddress("::2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 966 | SocketAddress("0.0.0.0", 5000), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | // Fails. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 970 | TEST_F(VirtualSocketServerTest, CantConnectFromUnMappedIPv6ToMappedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 971 | CrossFamilyConnectionTest(SocketAddress("::2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 972 | SocketAddress("::ffff:127.0.0.1", 5000), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | // Works. receiving socket sees ::ffff:127.0.0.2. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 976 | TEST_F(VirtualSocketServerTest, CanConnectFromIPv4ToIPv6Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 977 | CrossFamilyConnectionTest(SocketAddress("127.0.0.2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 978 | SocketAddress("::", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | // Fails. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 982 | TEST_F(VirtualSocketServerTest, CantConnectFromIPv4ToUnMappedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 983 | CrossFamilyConnectionTest(SocketAddress("127.0.0.2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 984 | SocketAddress("::1", 5000), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | // Works. Receiving socket sees ::ffff:127.0.0.1. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 988 | TEST_F(VirtualSocketServerTest, CanConnectFromIPv4ToMappedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 989 | CrossFamilyConnectionTest(SocketAddress("127.0.0.1", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 990 | SocketAddress("::ffff:127.0.0.2", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // Works, receiving socket sees a result from GetNextIP. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 994 | TEST_F(VirtualSocketServerTest, CanConnectFromUnboundIPv6ToIPv4Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 995 | CrossFamilyConnectionTest(SocketAddress("::", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 996 | SocketAddress("0.0.0.0", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | // Works, receiving socket sees whatever GetNextIP gave the client. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1000 | TEST_F(VirtualSocketServerTest, CanConnectFromUnboundIPv4ToIPv6Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1001 | CrossFamilyConnectionTest(SocketAddress("0.0.0.0", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1002 | SocketAddress("::", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1005 | TEST_F(VirtualSocketServerTest, CanSendDatagramFromUnboundIPv4ToIPv6Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1006 | CrossFamilyDatagramTest(SocketAddress("0.0.0.0", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1007 | SocketAddress("::", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1010 | TEST_F(VirtualSocketServerTest, CanSendDatagramFromMappedIPv6ToIPv4Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1011 | CrossFamilyDatagramTest(SocketAddress("::ffff:127.0.0.1", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1012 | SocketAddress("0.0.0.0", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1015 | TEST_F(VirtualSocketServerTest, CantSendDatagramFromUnMappedIPv6ToIPv4Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1016 | CrossFamilyDatagramTest(SocketAddress("::2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1017 | SocketAddress("0.0.0.0", 5000), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1020 | TEST_F(VirtualSocketServerTest, CantSendDatagramFromUnMappedIPv6ToMappedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1021 | CrossFamilyDatagramTest(SocketAddress("::2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1022 | SocketAddress("::ffff:127.0.0.1", 5000), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1025 | TEST_F(VirtualSocketServerTest, CanSendDatagramFromIPv4ToIPv6Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1026 | CrossFamilyDatagramTest(SocketAddress("127.0.0.2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1027 | SocketAddress("::", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1030 | TEST_F(VirtualSocketServerTest, CantSendDatagramFromIPv4ToUnMappedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1031 | CrossFamilyDatagramTest(SocketAddress("127.0.0.2", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1032 | SocketAddress("::1", 5000), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1035 | TEST_F(VirtualSocketServerTest, CanSendDatagramFromIPv4ToMappedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1036 | CrossFamilyDatagramTest(SocketAddress("127.0.0.1", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1037 | SocketAddress("::ffff:127.0.0.2", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 1040 | TEST_F(VirtualSocketServerTest, CanSendDatagramFromUnboundIPv6ToIPv4Any) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1041 | CrossFamilyDatagramTest(SocketAddress("::", 0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1042 | SocketAddress("0.0.0.0", 5000), true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1045 | TEST_F(VirtualSocketServerTest, SetSendingBlockedWithUdpSocket) { |
| 1046 | AsyncSocket* socket1 = |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1047 | ss_.CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_DGRAM); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1048 | std::unique_ptr<AsyncSocket> socket2 = absl::WrapUnique( |
| 1049 | ss_.CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_DGRAM)); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1050 | socket1->Bind(kIPv4AnyAddress); |
| 1051 | socket2->Bind(kIPv4AnyAddress); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1052 | auto client1 = absl::make_unique<TestClient>( |
| 1053 | absl::make_unique<AsyncUDPSocket>(socket1), &fake_clock_); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1054 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1055 | ss_.SetSendingBlocked(true); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1056 | EXPECT_EQ(-1, client1->SendTo("foo", 3, socket2->GetLocalAddress())); |
| 1057 | EXPECT_TRUE(socket1->IsBlocking()); |
| 1058 | EXPECT_EQ(0, client1->ready_to_send_count()); |
| 1059 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1060 | ss_.SetSendingBlocked(false); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1061 | EXPECT_EQ(1, client1->ready_to_send_count()); |
| 1062 | EXPECT_EQ(3, client1->SendTo("foo", 3, socket2->GetLocalAddress())); |
| 1063 | } |
| 1064 | |
| 1065 | TEST_F(VirtualSocketServerTest, SetSendingBlockedWithTcpSocket) { |
| 1066 | constexpr size_t kBufferSize = 1024; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1067 | ss_.set_send_buffer_capacity(kBufferSize); |
| 1068 | ss_.set_recv_buffer_capacity(kBufferSize); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1069 | |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 1070 | StreamSink sink; |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1071 | std::unique_ptr<AsyncSocket> socket1 = absl::WrapUnique( |
| 1072 | ss_.CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_STREAM)); |
| 1073 | std::unique_ptr<AsyncSocket> socket2 = absl::WrapUnique( |
| 1074 | ss_.CreateAsyncSocket(kIPv4AnyAddress.family(), SOCK_STREAM)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1075 | sink.Monitor(socket1.get()); |
| 1076 | sink.Monitor(socket2.get()); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1077 | socket1->Bind(kIPv4AnyAddress); |
| 1078 | socket2->Bind(kIPv4AnyAddress); |
| 1079 | |
| 1080 | // Connect sockets. |
| 1081 | EXPECT_EQ(0, socket1->Connect(socket2->GetLocalAddress())); |
| 1082 | EXPECT_EQ(0, socket2->Connect(socket1->GetLocalAddress())); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1083 | ss_.ProcessMessagesUntilIdle(); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1084 | |
| 1085 | char data[kBufferSize] = {}; |
| 1086 | |
| 1087 | // First Send call will fill the send buffer but not send anything. |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1088 | ss_.SetSendingBlocked(true); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1089 | EXPECT_EQ(static_cast<int>(kBufferSize), socket1->Send(data, kBufferSize)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1090 | ss_.ProcessMessagesUntilIdle(); |
| 1091 | EXPECT_FALSE(sink.Check(socket1.get(), SSE_WRITE)); |
| 1092 | EXPECT_FALSE(sink.Check(socket2.get(), SSE_READ)); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1093 | EXPECT_FALSE(socket1->IsBlocking()); |
| 1094 | |
| 1095 | // Since the send buffer is full, next Send will result in EWOULDBLOCK. |
| 1096 | EXPECT_EQ(-1, socket1->Send(data, kBufferSize)); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1097 | EXPECT_FALSE(sink.Check(socket1.get(), SSE_WRITE)); |
| 1098 | EXPECT_FALSE(sink.Check(socket2.get(), SSE_READ)); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1099 | EXPECT_TRUE(socket1->IsBlocking()); |
| 1100 | |
| 1101 | // When sending is unblocked, the buffered data should be sent and |
| 1102 | // SignalWriteEvent should fire. |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 1103 | ss_.SetSendingBlocked(false); |
| 1104 | ss_.ProcessMessagesUntilIdle(); |
| 1105 | EXPECT_TRUE(sink.Check(socket1.get(), SSE_WRITE)); |
| 1106 | EXPECT_TRUE(sink.Check(socket2.get(), SSE_READ)); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1109 | TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1110 | const uint32_t kTestMean[] = {10, 100, 333, 1000}; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1111 | const double kTestDev[] = {0.25, 0.1, 0.01}; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1112 | // TODO(deadbeef): The current code only works for 1000 data points or more. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1113 | const uint32_t kTestSamples[] = {/*10, 100,*/ 1000}; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 1114 | for (size_t midx = 0; midx < arraysize(kTestMean); ++midx) { |
| 1115 | for (size_t didx = 0; didx < arraysize(kTestDev); ++didx) { |
| 1116 | for (size_t sidx = 0; sidx < arraysize(kTestSamples); ++sidx) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1117 | ASSERT_LT(0u, kTestSamples[sidx]); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1118 | const uint32_t kStdDev = |
| 1119 | static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1120 | VirtualSocketServer::Function* f = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1121 | VirtualSocketServer::CreateDistribution(kTestMean[midx], kStdDev, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1122 | kTestSamples[sidx]); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1123 | ASSERT_TRUE(nullptr != f); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1124 | ASSERT_EQ(kTestSamples[sidx], f->size()); |
| 1125 | double sum = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1126 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1127 | sum += (*f)[i].second; |
| 1128 | } |
| 1129 | const double mean = sum / f->size(); |
| 1130 | double sum_sq_dev = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1131 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1132 | double dev = (*f)[i].second - mean; |
| 1133 | sum_sq_dev += dev * dev; |
| 1134 | } |
| 1135 | const double stddev = sqrt(sum_sq_dev / f->size()); |
| 1136 | EXPECT_NEAR(kTestMean[midx], mean, 0.1 * kTestMean[midx]) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1137 | << "M=" << kTestMean[midx] << " SD=" << kStdDev |
| 1138 | << " N=" << kTestSamples[sidx]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1139 | EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1140 | << "M=" << kTestMean[midx] << " SD=" << kStdDev |
| 1141 | << " N=" << kTestSamples[sidx]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1142 | delete f; |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | } |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 1147 | |
| 1148 | } // namespace |
| 1149 | } // namespace rtc |