blob: 3987a59d00dcfa34cba873fa917a66e770b673d5 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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 <signal.h>
Yves Gerey3e707812018-11-28 16:47:49 +010012#include <algorithm>
Yves Gerey665174f2018-06-19 15:03:05 +020013#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/ip_address.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/network_monitor.h"
19#include "rtc_base/physical_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/socket_unittest.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
25namespace rtc {
26
Mirko Bonadei675513b2017-11-09 11:09:25 +010027#define MAYBE_SKIP_IPV4 \
28 if (!HasIPv4Enabled()) { \
29 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
30 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070031 }
32
Mirko Bonadei675513b2017-11-09 11:09:25 +010033#define MAYBE_SKIP_IPV6 \
34 if (!HasIPv6Enabled()) { \
35 RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
36 return; \
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070037 }
38
jbauch095ae152015-12-18 01:39:55 -080039class PhysicalSocketTest;
40
41class FakeSocketDispatcher : public SocketDispatcher {
42 public:
43 explicit FakeSocketDispatcher(PhysicalSocketServer* ss)
Yves Gerey665174f2018-06-19 15:03:05 +020044 : SocketDispatcher(ss) {}
jbauch095ae152015-12-18 01:39:55 -080045
jbauchf2a2bf42016-02-03 16:45:32 -080046 FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss)
Yves Gerey665174f2018-06-19 15:03:05 +020047 : SocketDispatcher(s, ss) {}
jbauchf2a2bf42016-02-03 16:45:32 -080048
jbauch095ae152015-12-18 01:39:55 -080049 protected:
50 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override;
jbauchf2a2bf42016-02-03 16:45:32 -080051 int DoSend(SOCKET socket, const char* buf, int len, int flags) override;
Yves Gerey665174f2018-06-19 15:03:05 +020052 int DoSendTo(SOCKET socket,
53 const char* buf,
54 int len,
55 int flags,
56 const struct sockaddr* dest_addr,
57 socklen_t addrlen) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058};
59
jbauch095ae152015-12-18 01:39:55 -080060class FakePhysicalSocketServer : public PhysicalSocketServer {
61 public:
Yves Gerey665174f2018-06-19 15:03:05 +020062 explicit FakePhysicalSocketServer(PhysicalSocketTest* test) : test_(test) {}
jbauch095ae152015-12-18 01:39:55 -080063
jbauch095ae152015-12-18 01:39:55 -080064 AsyncSocket* CreateAsyncSocket(int family, int type) override {
65 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
jbauchf2a2bf42016-02-03 16:45:32 -080066 if (!dispatcher->Create(family, type)) {
jbauch095ae152015-12-18 01:39:55 -080067 delete dispatcher;
68 return nullptr;
69 }
jbauchf2a2bf42016-02-03 16:45:32 -080070 return dispatcher;
71 }
72
73 AsyncSocket* WrapSocket(SOCKET s) override {
74 SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this);
75 if (!dispatcher->Initialize()) {
76 delete dispatcher;
77 return nullptr;
78 }
79 return dispatcher;
jbauch095ae152015-12-18 01:39:55 -080080 }
81
82 PhysicalSocketTest* GetTest() const { return test_; }
83
84 private:
85 PhysicalSocketTest* test_;
86};
87
deadbeefc874d122017-02-13 15:41:59 -080088class FakeNetworkBinder : public NetworkBinderInterface {
89 public:
90 NetworkBindingResult BindSocketToNetwork(int, const IPAddress&) override {
deadbeef9ffa13f2017-02-21 16:18:00 -080091 ++num_binds_;
deadbeefc874d122017-02-13 15:41:59 -080092 return result_;
93 }
94
95 void set_result(NetworkBindingResult result) { result_ = result; }
96
deadbeef9ffa13f2017-02-21 16:18:00 -080097 int num_binds() { return num_binds_; }
98
deadbeefc874d122017-02-13 15:41:59 -080099 private:
100 NetworkBindingResult result_ = NetworkBindingResult::SUCCESS;
deadbeef9ffa13f2017-02-21 16:18:00 -0800101 int num_binds_ = 0;
deadbeefc874d122017-02-13 15:41:59 -0800102};
103
jbauch095ae152015-12-18 01:39:55 -0800104class PhysicalSocketTest : public SocketTest {
105 public:
106 // Set flag to simluate failures when calling "::accept" on a AsyncSocket.
107 void SetFailAccept(bool fail) { fail_accept_ = fail; }
108 bool FailAccept() const { return fail_accept_; }
109
jbauchf2a2bf42016-02-03 16:45:32 -0800110 // Maximum size to ::send to a socket. Set to < 0 to disable limiting.
111 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; }
112 int MaxSendSize() const { return max_send_size_; }
113
jbauch095ae152015-12-18 01:39:55 -0800114 protected:
115 PhysicalSocketTest()
Yves Gerey665174f2018-06-19 15:03:05 +0200116 : server_(new FakePhysicalSocketServer(this)),
117 thread_(server_.get()),
118 fail_accept_(false),
119 max_send_size_(-1) {}
jbauch095ae152015-12-18 01:39:55 -0800120
121 void ConnectInternalAcceptError(const IPAddress& loopback);
jbauchf2a2bf42016-02-03 16:45:32 -0800122 void WritableAfterPartialWrite(const IPAddress& loopback);
jbauch095ae152015-12-18 01:39:55 -0800123
jbauch555604a2016-04-26 03:13:22 -0700124 std::unique_ptr<FakePhysicalSocketServer> server_;
nisse7eaa4ea2017-05-08 05:25:41 -0700125 rtc::AutoSocketServerThread thread_;
jbauch095ae152015-12-18 01:39:55 -0800126 bool fail_accept_;
jbauchf2a2bf42016-02-03 16:45:32 -0800127 int max_send_size_;
jbauch095ae152015-12-18 01:39:55 -0800128};
129
130SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
131 sockaddr* addr,
132 socklen_t* addrlen) {
133 FakePhysicalSocketServer* ss =
134 static_cast<FakePhysicalSocketServer*>(socketserver());
135 if (ss->GetTest()->FailAccept()) {
136 return INVALID_SOCKET;
137 }
138
139 return SocketDispatcher::DoAccept(socket, addr, addrlen);
140}
141
Yves Gerey665174f2018-06-19 15:03:05 +0200142int FakeSocketDispatcher::DoSend(SOCKET socket,
143 const char* buf,
144 int len,
145 int flags) {
jbauchf2a2bf42016-02-03 16:45:32 -0800146 FakePhysicalSocketServer* ss =
147 static_cast<FakePhysicalSocketServer*>(socketserver());
148 if (ss->GetTest()->MaxSendSize() >= 0) {
149 len = std::min(len, ss->GetTest()->MaxSendSize());
150 }
151
152 return SocketDispatcher::DoSend(socket, buf, len, flags);
153}
154
Yves Gerey665174f2018-06-19 15:03:05 +0200155int FakeSocketDispatcher::DoSendTo(SOCKET socket,
156 const char* buf,
157 int len,
158 int flags,
159 const struct sockaddr* dest_addr,
160 socklen_t addrlen) {
jbauchf2a2bf42016-02-03 16:45:32 -0800161 FakePhysicalSocketServer* ss =
162 static_cast<FakePhysicalSocketServer*>(socketserver());
163 if (ss->GetTest()->MaxSendSize() >= 0) {
164 len = std::min(len, ss->GetTest()->MaxSendSize());
165 }
166
167 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
Yves Gerey665174f2018-06-19 15:03:05 +0200168 addrlen);
jbauchf2a2bf42016-02-03 16:45:32 -0800169}
170
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171TEST_F(PhysicalSocketTest, TestConnectIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700172 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 SocketTest::TestConnectIPv4();
174}
175
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700176TEST_F(PhysicalSocketTest, TestConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 SocketTest::TestConnectIPv6();
178}
179
180TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700181 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182 SocketTest::TestConnectWithDnsLookupIPv4();
183}
184
185TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
186 SocketTest::TestConnectWithDnsLookupIPv6();
187}
188
189TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700190 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 SocketTest::TestConnectFailIPv4();
192}
193
jbauch095ae152015-12-18 01:39:55 -0800194void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) {
kwibergd0d81482017-04-18 03:18:22 -0700195 webrtc::testing::StreamSink sink;
jbauch095ae152015-12-18 01:39:55 -0800196 SocketAddress accept_addr;
197
198 // Create two clients.
jbauch555604a2016-04-26 03:13:22 -0700199 std::unique_ptr<AsyncSocket> client1(
200 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800201 sink.Monitor(client1.get());
202 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200203 EXPECT_TRUE(IsUnspecOrEmptyIP(client1->GetLocalAddress().ipaddr()));
jbauch095ae152015-12-18 01:39:55 -0800204
jbauch555604a2016-04-26 03:13:22 -0700205 std::unique_ptr<AsyncSocket> client2(
206 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800207 sink.Monitor(client2.get());
208 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200209 EXPECT_TRUE(IsUnspecOrEmptyIP(client2->GetLocalAddress().ipaddr()));
jbauch095ae152015-12-18 01:39:55 -0800210
211 // Create server and listen.
jbauch555604a2016-04-26 03:13:22 -0700212 std::unique_ptr<AsyncSocket> server(
jbauch095ae152015-12-18 01:39:55 -0800213 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
214 sink.Monitor(server.get());
215 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
216 EXPECT_EQ(0, server->Listen(5));
217 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState());
218
219 // Ensure no pending server connections, since we haven't done anything yet.
kwibergd0d81482017-04-18 03:18:22 -0700220 EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ));
jbauch095ae152015-12-18 01:39:55 -0800221 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
222 EXPECT_TRUE(accept_addr.IsNil());
223
224 // Attempt first connect to listening socket.
225 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress()));
226 EXPECT_FALSE(client1->GetLocalAddress().IsNil());
227 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress());
228
229 // Client is connecting, outcome not yet determined.
230 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState());
kwibergd0d81482017-04-18 03:18:22 -0700231 EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_OPEN));
232 EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_CLOSE));
jbauch095ae152015-12-18 01:39:55 -0800233
234 // Server has pending connection, try to accept it (will fail).
kwibergd0d81482017-04-18 03:18:22 -0700235 EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)),
236 kTimeout);
jbauch095ae152015-12-18 01:39:55 -0800237 // Simulate "::accept" returning an error.
238 SetFailAccept(true);
jbauch555604a2016-04-26 03:13:22 -0700239 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800240 EXPECT_FALSE(accepted);
241 ASSERT_TRUE(accept_addr.IsNil());
242
243 // Ensure no more pending server connections.
kwibergd0d81482017-04-18 03:18:22 -0700244 EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ));
jbauch095ae152015-12-18 01:39:55 -0800245 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
246 EXPECT_TRUE(accept_addr.IsNil());
247
248 // Attempt second connect to listening socket.
249 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress()));
250 EXPECT_FALSE(client2->GetLocalAddress().IsNil());
251 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress());
252
253 // Client is connecting, outcome not yet determined.
254 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState());
kwibergd0d81482017-04-18 03:18:22 -0700255 EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_OPEN));
256 EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_CLOSE));
jbauch095ae152015-12-18 01:39:55 -0800257
258 // Server has pending connection, try to accept it (will succeed).
kwibergd0d81482017-04-18 03:18:22 -0700259 EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)),
260 kTimeout);
jbauch095ae152015-12-18 01:39:55 -0800261 SetFailAccept(false);
jbauch555604a2016-04-26 03:13:22 -0700262 std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800263 ASSERT_TRUE(accepted2);
264 EXPECT_FALSE(accept_addr.IsNil());
265 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr);
266}
267
268TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700269 MAYBE_SKIP_IPV4;
jbauch095ae152015-12-18 01:39:55 -0800270 ConnectInternalAcceptError(kIPv4Loopback);
271}
272
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700273TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) {
274 MAYBE_SKIP_IPV6;
jbauch095ae152015-12-18 01:39:55 -0800275 ConnectInternalAcceptError(kIPv6Loopback);
276}
277
jbauchf2a2bf42016-02-03 16:45:32 -0800278void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
279 // Simulate a really small maximum send size.
280 const int kMaxSendSize = 128;
281 SetMaxSendSize(kMaxSendSize);
282
283 // Run the default send/receive socket tests with a smaller amount of data
284 // to avoid long running times due to the small maximum send size.
285 const size_t kDataSize = 128 * 1024;
286 TcpInternal(loopback, kDataSize, kMaxSendSize);
287}
288
danilchapb7b9dca2016-08-05 05:55:43 -0700289// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
290#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200291#define MAYBE_TestWritableAfterPartialWriteIPv4 \
292 DISABLED_TestWritableAfterPartialWriteIPv4
danilchapb7b9dca2016-08-05 05:55:43 -0700293#else
Yves Gerey665174f2018-06-19 15:03:05 +0200294#define MAYBE_TestWritableAfterPartialWriteIPv4 \
295 TestWritableAfterPartialWriteIPv4
danilchapb7b9dca2016-08-05 05:55:43 -0700296#endif
297TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700298 MAYBE_SKIP_IPV4;
jbauchf2a2bf42016-02-03 16:45:32 -0800299 WritableAfterPartialWrite(kIPv4Loopback);
300}
301
danilchapb7b9dca2016-08-05 05:55:43 -0700302// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
303#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200304#define MAYBE_TestWritableAfterPartialWriteIPv6 \
305 DISABLED_TestWritableAfterPartialWriteIPv6
danilchapb7b9dca2016-08-05 05:55:43 -0700306#else
Yves Gerey665174f2018-06-19 15:03:05 +0200307#define MAYBE_TestWritableAfterPartialWriteIPv6 \
308 TestWritableAfterPartialWriteIPv6
danilchapb7b9dca2016-08-05 05:55:43 -0700309#endif
310TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700311 MAYBE_SKIP_IPV6;
jbauchf2a2bf42016-02-03 16:45:32 -0800312 WritableAfterPartialWrite(kIPv6Loopback);
313}
314
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700315TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 SocketTest::TestConnectFailIPv6();
317}
318
319TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700320 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321 SocketTest::TestConnectWithDnsLookupFailIPv4();
322}
323
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700324TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325 SocketTest::TestConnectWithDnsLookupFailIPv6();
326}
327
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700329 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330 SocketTest::TestConnectWithClosedSocketIPv4();
331}
332
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700333TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334 SocketTest::TestConnectWithClosedSocketIPv6();
335}
336
337TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700338 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339 SocketTest::TestConnectWhileNotClosedIPv4();
340}
341
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700342TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 SocketTest::TestConnectWhileNotClosedIPv6();
344}
345
346TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700347 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000348 SocketTest::TestServerCloseDuringConnectIPv4();
349}
350
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700351TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000352 SocketTest::TestServerCloseDuringConnectIPv6();
353}
354
355TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700356 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000357 SocketTest::TestClientCloseDuringConnectIPv4();
358}
359
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700360TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000361 SocketTest::TestClientCloseDuringConnectIPv6();
362}
363
364TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700365 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 SocketTest::TestServerCloseIPv4();
367}
368
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700369TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000370 SocketTest::TestServerCloseIPv6();
371}
372
373TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700374 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000375 SocketTest::TestCloseInClosedCallbackIPv4();
376}
377
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700378TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379 SocketTest::TestCloseInClosedCallbackIPv6();
380}
381
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000382TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700383 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384 SocketTest::TestSocketServerWaitIPv4();
385}
386
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700387TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000388 SocketTest::TestSocketServerWaitIPv6();
389}
390
391TEST_F(PhysicalSocketTest, TestTcpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700392 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000393 SocketTest::TestTcpIPv4();
394}
395
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700396TEST_F(PhysicalSocketTest, TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000397 SocketTest::TestTcpIPv6();
398}
399
400TEST_F(PhysicalSocketTest, TestUdpIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700401 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 SocketTest::TestUdpIPv4();
403}
404
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700405TEST_F(PhysicalSocketTest, TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000406 SocketTest::TestUdpIPv6();
407}
408
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000409// Disable for TSan v2, see
410// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
deadbeefc97be6a2015-09-25 11:00:38 -0700411// Also disable for MSan, see:
412// https://code.google.com/p/webrtc/issues/detail?id=4958
413// TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
minyuedf200d12015-10-17 13:10:46 -0700414// Also disable for ASan.
Henrik Kjellander0be3e042015-10-30 21:21:03 +0100415// Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
ivocf399f212015-11-19 06:44:32 -0800416// Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
minyuedf200d12015-10-17 13:10:46 -0700417#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
ivocf399f212015-11-19 06:44:32 -0800418 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
419 defined(WEBRTC_LINUX)
minyuedf200d12015-10-17 13:10:46 -0700420#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
421#else
422#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
423#endif
424TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700425 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000426 SocketTest::TestUdpReadyToSendIPv4();
427}
428
danilchapb7b9dca2016-08-05 05:55:43 -0700429// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
430#if defined(WEBRTC_WIN)
431#define MAYBE_TestUdpReadyToSendIPv6 DISABLED_TestUdpReadyToSendIPv6
432#else
433#define MAYBE_TestUdpReadyToSendIPv6 TestUdpReadyToSendIPv6
434#endif
435TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000436 SocketTest::TestUdpReadyToSendIPv6();
437}
438
439TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700440 MAYBE_SKIP_IPV4;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000441 SocketTest::TestGetSetOptionsIPv4();
442}
443
444TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
445 SocketTest::TestGetSetOptionsIPv6();
446}
447
448#if defined(WEBRTC_POSIX)
449
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700450// We don't get recv timestamps on Mac.
Stefan Holmer9131efd2016-05-23 18:19:26 +0200451#if !defined(WEBRTC_MAC)
452TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700453 MAYBE_SKIP_IPV4;
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700454 SocketTest::TestSocketRecvTimestampIPv4();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200455}
456
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700457TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) {
458 SocketTest::TestSocketRecvTimestampIPv6();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200459}
460#endif
461
deadbeefc874d122017-02-13 15:41:59 -0800462// Verify that if the socket was unable to be bound to a real network interface
463// (not loopback), Bind will return an error.
464TEST_F(PhysicalSocketTest,
465 BindFailsIfNetworkBinderFailsForNonLoopbackInterface) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700466 MAYBE_SKIP_IPV4;
deadbeefc874d122017-02-13 15:41:59 -0800467 FakeNetworkBinder fake_network_binder;
468 server_->set_network_binder(&fake_network_binder);
469 std::unique_ptr<AsyncSocket> socket(
470 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
471 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
472 EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0)));
473 server_->set_network_binder(nullptr);
474}
475
deadbeef9ffa13f2017-02-21 16:18:00 -0800476// Network binder shouldn't be used if the socket is bound to the "any" IP.
Yves Gerey665174f2018-06-19 15:03:05 +0200477TEST_F(PhysicalSocketTest, NetworkBinderIsNotUsedForAnyIp) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700478 MAYBE_SKIP_IPV4;
deadbeef9ffa13f2017-02-21 16:18:00 -0800479 FakeNetworkBinder fake_network_binder;
480 server_->set_network_binder(&fake_network_binder);
481 std::unique_ptr<AsyncSocket> socket(
482 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
483 EXPECT_EQ(0, socket->Bind(SocketAddress("0.0.0.0", 0)));
484 EXPECT_EQ(0, fake_network_binder.num_binds());
485 server_->set_network_binder(nullptr);
486}
487
deadbeefc874d122017-02-13 15:41:59 -0800488// For a loopback interface, failures to bind to the interface should be
489// tolerated.
490TEST_F(PhysicalSocketTest,
491 BindSucceedsIfNetworkBinderFailsForLoopbackInterface) {
deadbeef9a6f4d42017-05-15 19:43:33 -0700492 MAYBE_SKIP_IPV4;
deadbeefc874d122017-02-13 15:41:59 -0800493 FakeNetworkBinder fake_network_binder;
494 server_->set_network_binder(&fake_network_binder);
495 std::unique_ptr<AsyncSocket> socket(
496 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
497 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
498 EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0)));
499 server_->set_network_binder(nullptr);
500}
501
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200502class PosixSignalDeliveryTest : public ::testing::Test {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000503 public:
504 static void RecordSignal(int signum) {
505 signals_received_.push_back(signum);
506 signaled_thread_ = Thread::Current();
507 }
508
509 protected:
Steve Anton9de3aac2017-10-24 10:08:26 -0700510 void SetUp() override { ss_.reset(new PhysicalSocketServer()); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000511
Steve Anton9de3aac2017-10-24 10:08:26 -0700512 void TearDown() override {
deadbeef37f5ecf2017-02-27 14:06:41 -0800513 ss_.reset(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000514 signals_received_.clear();
deadbeef37f5ecf2017-02-27 14:06:41 -0800515 signaled_thread_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000516 }
517
518 bool ExpectSignal(int signum) {
519 if (signals_received_.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100520 RTC_LOG(LS_ERROR) << "ExpectSignal(): No signal received";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000521 return false;
522 }
523 if (signals_received_[0] != signum) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100524 RTC_LOG(LS_ERROR) << "ExpectSignal(): Received signal "
525 << signals_received_[0] << ", expected " << signum;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000526 return false;
527 }
528 signals_received_.erase(signals_received_.begin());
529 return true;
530 }
531
532 bool ExpectNone() {
533 bool ret = signals_received_.empty();
534 if (!ret) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100535 RTC_LOG(LS_ERROR) << "ExpectNone(): Received signal "
536 << signals_received_[0] << ", expected none";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000537 }
538 return ret;
539 }
540
541 static std::vector<int> signals_received_;
Yves Gerey665174f2018-06-19 15:03:05 +0200542 static Thread* signaled_thread_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543
jbauch555604a2016-04-26 03:13:22 -0700544 std::unique_ptr<PhysicalSocketServer> ss_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000545};
546
547std::vector<int> PosixSignalDeliveryTest::signals_received_;
deadbeef37f5ecf2017-02-27 14:06:41 -0800548Thread* PosixSignalDeliveryTest::signaled_thread_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000549
550// Test receiving a synchronous signal while not in Wait() and then entering
551// Wait() afterwards.
Artem Titarenko34fc3462018-11-06 12:29:29 +0100552// TODO(webrtc:7864): Fails on real iOS devices
553#if defined(WEBRTC_IOS) && defined(WEBRTC_ARCH_ARM_FAMILY)
554#define MAYBE_RaiseThenWait DISABLED_RaiseThenWait
555#else
556#define MAYBE_RaiseThenWait RaiseThenWait
557#endif
558TEST_F(PosixSignalDeliveryTest, MAYBE_RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000559 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
560 raise(SIGTERM);
561 EXPECT_TRUE(ss_->Wait(0, true));
562 EXPECT_TRUE(ExpectSignal(SIGTERM));
563 EXPECT_TRUE(ExpectNone());
564}
565
566// Test that we can handle getting tons of repeated signals and that we see all
567// the different ones.
Artem Titarenko34fc3462018-11-06 12:29:29 +0100568// TODO(webrtc:7864): Fails on real iOS devices
569#if defined(WEBRTC_IOS) && defined(WEBRTC_ARCH_ARM_FAMILY)
570#define MAYBE_InsanelyManySignals DISABLED_InsanelyManySignals
571#else
572#define MAYBE_InsanelyManySignals InsanelyManySignals
573#endif
574TEST_F(PosixSignalDeliveryTest, MAYBE_InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000575 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
576 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
577 for (int i = 0; i < 10000; ++i) {
578 raise(SIGTERM);
579 }
580 raise(SIGINT);
581 EXPECT_TRUE(ss_->Wait(0, true));
582 // Order will be lowest signal numbers first.
583 EXPECT_TRUE(ExpectSignal(SIGINT));
584 EXPECT_TRUE(ExpectSignal(SIGTERM));
585 EXPECT_TRUE(ExpectNone());
586}
587
588// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000589TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000590 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
591 alarm(1);
592 EXPECT_TRUE(ss_->Wait(1500, true));
593 EXPECT_TRUE(ExpectSignal(SIGALRM));
594 EXPECT_TRUE(ExpectNone());
595}
596
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000597// Test that it works no matter what thread the kernel chooses to give the
598// signal to (since it's not guaranteed to be the one that Wait() runs on).
Artem Titarenko34fc3462018-11-06 12:29:29 +0100599// TODO(webrtc:7864): Fails on real iOS devices
600#if defined(WEBRTC_IOS) && defined(WEBRTC_ARCH_ARM_FAMILY)
601#define MAYBE_SignalOnDifferentThread DISABLED_SignalOnDifferentThread
602#else
603#define MAYBE_SignalOnDifferentThread SignalOnDifferentThread
604#endif
605TEST_F(PosixSignalDeliveryTest, DISABLED_SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000606 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
607 // Mask out SIGTERM so that it can't be delivered to this thread.
608 sigset_t mask;
609 sigemptyset(&mask);
610 sigaddset(&mask, SIGTERM);
deadbeef37f5ecf2017-02-27 14:06:41 -0800611 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000612 // Start a new thread that raises it. It will have to be delivered to that
613 // thread. Our implementation should safely handle it and dispatch
614 // RecordSignal() on this thread.
tommie7251592017-07-14 14:44:46 -0700615 std::unique_ptr<Thread> thread(Thread::CreateWithSocketServer());
Niels Möllerd2e50132019-06-11 09:24:14 +0200616 thread->Start();
617 thread->PostTask(RTC_FROM_HERE, [&thread]() {
618 thread->socketserver()->Wait(1000, false);
619 // Allow SIGTERM. This will be the only thread with it not masked so it will
620 // be delivered to us.
621 sigset_t mask;
622 sigemptyset(&mask);
623 pthread_sigmask(SIG_SETMASK, &mask, nullptr);
624
625 // Raise it.
626 raise(SIGTERM);
627 });
628
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000629 EXPECT_TRUE(ss_->Wait(1500, true));
630 EXPECT_TRUE(ExpectSignal(SIGTERM));
631 EXPECT_EQ(Thread::Current(), signaled_thread_);
632 EXPECT_TRUE(ExpectNone());
633}
634
635#endif
636
637} // namespace rtc