blob: e2f05e977f15095818c9f97a45beacf14d01fc90 [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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include <signal.h>
13#include <stdarg.h>
14
15#include "webrtc/base/gunit.h"
16#include "webrtc/base/logging.h"
deadbeefc874d122017-02-13 15:41:59 -080017#include "webrtc/base/networkmonitor.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/physicalsocketserver.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#include "webrtc/base/socket_unittest.h"
20#include "webrtc/base/testutils.h"
21#include "webrtc/base/thread.h"
22
23namespace rtc {
24
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070025#define MAYBE_SKIP_IPV6 \
26 if (!HasIPv6Enabled()) { \
27 LOG(LS_INFO) << "No IPv6... skipping"; \
28 return; \
29 }
30
jbauch095ae152015-12-18 01:39:55 -080031class PhysicalSocketTest;
32
33class FakeSocketDispatcher : public SocketDispatcher {
34 public:
35 explicit FakeSocketDispatcher(PhysicalSocketServer* ss)
36 : SocketDispatcher(ss) {
37 }
38
jbauchf2a2bf42016-02-03 16:45:32 -080039 FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss)
40 : SocketDispatcher(s, ss) {
41 }
42
jbauch095ae152015-12-18 01:39:55 -080043 protected:
44 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override;
jbauchf2a2bf42016-02-03 16:45:32 -080045 int DoSend(SOCKET socket, const char* buf, int len, int flags) override;
46 int DoSendTo(SOCKET socket, const char* buf, int len, int flags,
47 const struct sockaddr* dest_addr, socklen_t addrlen) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048};
49
jbauch095ae152015-12-18 01:39:55 -080050class FakePhysicalSocketServer : public PhysicalSocketServer {
51 public:
52 explicit FakePhysicalSocketServer(PhysicalSocketTest* test)
53 : test_(test) {
54 }
55
56 AsyncSocket* CreateAsyncSocket(int type) override {
57 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
jbauchf2a2bf42016-02-03 16:45:32 -080058 if (!dispatcher->Create(type)) {
jbauch095ae152015-12-18 01:39:55 -080059 delete dispatcher;
60 return nullptr;
61 }
jbauchf2a2bf42016-02-03 16:45:32 -080062 return dispatcher;
jbauch095ae152015-12-18 01:39:55 -080063 }
64
65 AsyncSocket* CreateAsyncSocket(int family, int type) override {
66 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
jbauchf2a2bf42016-02-03 16:45:32 -080067 if (!dispatcher->Create(family, type)) {
jbauch095ae152015-12-18 01:39:55 -080068 delete dispatcher;
69 return nullptr;
70 }
jbauchf2a2bf42016-02-03 16:45:32 -080071 return dispatcher;
72 }
73
74 AsyncSocket* WrapSocket(SOCKET s) override {
75 SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this);
76 if (!dispatcher->Initialize()) {
77 delete dispatcher;
78 return nullptr;
79 }
80 return dispatcher;
jbauch095ae152015-12-18 01:39:55 -080081 }
82
83 PhysicalSocketTest* GetTest() const { return test_; }
84
85 private:
86 PhysicalSocketTest* test_;
87};
88
deadbeefc874d122017-02-13 15:41:59 -080089class FakeNetworkBinder : public NetworkBinderInterface {
90 public:
91 NetworkBindingResult BindSocketToNetwork(int, const IPAddress&) override {
deadbeef9ffa13f2017-02-21 16:18:00 -080092 ++num_binds_;
deadbeefc874d122017-02-13 15:41:59 -080093 return result_;
94 }
95
96 void set_result(NetworkBindingResult result) { result_ = result; }
97
deadbeef9ffa13f2017-02-21 16:18:00 -080098 int num_binds() { return num_binds_; }
99
deadbeefc874d122017-02-13 15:41:59 -0800100 private:
101 NetworkBindingResult result_ = NetworkBindingResult::SUCCESS;
deadbeef9ffa13f2017-02-21 16:18:00 -0800102 int num_binds_ = 0;
deadbeefc874d122017-02-13 15:41:59 -0800103};
104
jbauch095ae152015-12-18 01:39:55 -0800105class PhysicalSocketTest : public SocketTest {
106 public:
107 // Set flag to simluate failures when calling "::accept" on a AsyncSocket.
108 void SetFailAccept(bool fail) { fail_accept_ = fail; }
109 bool FailAccept() const { return fail_accept_; }
110
jbauchf2a2bf42016-02-03 16:45:32 -0800111 // Maximum size to ::send to a socket. Set to < 0 to disable limiting.
112 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; }
113 int MaxSendSize() const { return max_send_size_; }
114
jbauch095ae152015-12-18 01:39:55 -0800115 protected:
116 PhysicalSocketTest()
117 : server_(new FakePhysicalSocketServer(this)),
nisse7eaa4ea2017-05-08 05:25:41 -0700118 thread_(server_.get()),
jbauchf2a2bf42016-02-03 16:45:32 -0800119 fail_accept_(false),
nisse7eaa4ea2017-05-08 05:25:41 -0700120 max_send_size_(-1) {}
jbauch095ae152015-12-18 01:39:55 -0800121
122 void ConnectInternalAcceptError(const IPAddress& loopback);
jbauchf2a2bf42016-02-03 16:45:32 -0800123 void WritableAfterPartialWrite(const IPAddress& loopback);
jbauch095ae152015-12-18 01:39:55 -0800124
jbauch555604a2016-04-26 03:13:22 -0700125 std::unique_ptr<FakePhysicalSocketServer> server_;
nisse7eaa4ea2017-05-08 05:25:41 -0700126 rtc::AutoSocketServerThread thread_;
jbauch095ae152015-12-18 01:39:55 -0800127 bool fail_accept_;
jbauchf2a2bf42016-02-03 16:45:32 -0800128 int max_send_size_;
jbauch095ae152015-12-18 01:39:55 -0800129};
130
131SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
132 sockaddr* addr,
133 socklen_t* addrlen) {
134 FakePhysicalSocketServer* ss =
135 static_cast<FakePhysicalSocketServer*>(socketserver());
136 if (ss->GetTest()->FailAccept()) {
137 return INVALID_SOCKET;
138 }
139
140 return SocketDispatcher::DoAccept(socket, addr, addrlen);
141}
142
jbauchf2a2bf42016-02-03 16:45:32 -0800143int FakeSocketDispatcher::DoSend(SOCKET socket, const char* buf, int len,
144 int flags) {
145 FakePhysicalSocketServer* ss =
146 static_cast<FakePhysicalSocketServer*>(socketserver());
147 if (ss->GetTest()->MaxSendSize() >= 0) {
148 len = std::min(len, ss->GetTest()->MaxSendSize());
149 }
150
151 return SocketDispatcher::DoSend(socket, buf, len, flags);
152}
153
154int FakeSocketDispatcher::DoSendTo(SOCKET socket, const char* buf, int len,
155 int flags, const struct sockaddr* dest_addr, socklen_t addrlen) {
156 FakePhysicalSocketServer* ss =
157 static_cast<FakePhysicalSocketServer*>(socketserver());
158 if (ss->GetTest()->MaxSendSize() >= 0) {
159 len = std::min(len, ss->GetTest()->MaxSendSize());
160 }
161
162 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
163 addrlen);
164}
165
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166TEST_F(PhysicalSocketTest, TestConnectIPv4) {
167 SocketTest::TestConnectIPv4();
168}
169
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700170TEST_F(PhysicalSocketTest, TestConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 SocketTest::TestConnectIPv6();
172}
173
174TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
175 SocketTest::TestConnectWithDnsLookupIPv4();
176}
177
178TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
179 SocketTest::TestConnectWithDnsLookupIPv6();
180}
181
182TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
183 SocketTest::TestConnectFailIPv4();
184}
185
jbauch095ae152015-12-18 01:39:55 -0800186void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) {
kwibergd0d81482017-04-18 03:18:22 -0700187 webrtc::testing::StreamSink sink;
jbauch095ae152015-12-18 01:39:55 -0800188 SocketAddress accept_addr;
189
190 // Create two clients.
jbauch555604a2016-04-26 03:13:22 -0700191 std::unique_ptr<AsyncSocket> client1(
192 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800193 sink.Monitor(client1.get());
194 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState());
195 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr());
196
jbauch555604a2016-04-26 03:13:22 -0700197 std::unique_ptr<AsyncSocket> client2(
198 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800199 sink.Monitor(client2.get());
200 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState());
201 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr());
202
203 // Create server and listen.
jbauch555604a2016-04-26 03:13:22 -0700204 std::unique_ptr<AsyncSocket> server(
jbauch095ae152015-12-18 01:39:55 -0800205 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
206 sink.Monitor(server.get());
207 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
208 EXPECT_EQ(0, server->Listen(5));
209 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState());
210
211 // Ensure no pending server connections, since we haven't done anything yet.
kwibergd0d81482017-04-18 03:18:22 -0700212 EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ));
jbauch095ae152015-12-18 01:39:55 -0800213 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
214 EXPECT_TRUE(accept_addr.IsNil());
215
216 // Attempt first connect to listening socket.
217 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress()));
218 EXPECT_FALSE(client1->GetLocalAddress().IsNil());
219 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress());
220
221 // Client is connecting, outcome not yet determined.
222 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState());
kwibergd0d81482017-04-18 03:18:22 -0700223 EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_OPEN));
224 EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_CLOSE));
jbauch095ae152015-12-18 01:39:55 -0800225
226 // Server has pending connection, try to accept it (will fail).
kwibergd0d81482017-04-18 03:18:22 -0700227 EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)),
228 kTimeout);
jbauch095ae152015-12-18 01:39:55 -0800229 // Simulate "::accept" returning an error.
230 SetFailAccept(true);
jbauch555604a2016-04-26 03:13:22 -0700231 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800232 EXPECT_FALSE(accepted);
233 ASSERT_TRUE(accept_addr.IsNil());
234
235 // Ensure no more pending server connections.
kwibergd0d81482017-04-18 03:18:22 -0700236 EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ));
jbauch095ae152015-12-18 01:39:55 -0800237 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
238 EXPECT_TRUE(accept_addr.IsNil());
239
240 // Attempt second connect to listening socket.
241 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress()));
242 EXPECT_FALSE(client2->GetLocalAddress().IsNil());
243 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress());
244
245 // Client is connecting, outcome not yet determined.
246 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState());
kwibergd0d81482017-04-18 03:18:22 -0700247 EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_OPEN));
248 EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_CLOSE));
jbauch095ae152015-12-18 01:39:55 -0800249
250 // Server has pending connection, try to accept it (will succeed).
kwibergd0d81482017-04-18 03:18:22 -0700251 EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)),
252 kTimeout);
jbauch095ae152015-12-18 01:39:55 -0800253 SetFailAccept(false);
jbauch555604a2016-04-26 03:13:22 -0700254 std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800255 ASSERT_TRUE(accepted2);
256 EXPECT_FALSE(accept_addr.IsNil());
257 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr);
258}
259
260TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) {
261 ConnectInternalAcceptError(kIPv4Loopback);
262}
263
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700264TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) {
265 MAYBE_SKIP_IPV6;
jbauch095ae152015-12-18 01:39:55 -0800266 ConnectInternalAcceptError(kIPv6Loopback);
267}
268
jbauchf2a2bf42016-02-03 16:45:32 -0800269void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
270 // Simulate a really small maximum send size.
271 const int kMaxSendSize = 128;
272 SetMaxSendSize(kMaxSendSize);
273
274 // Run the default send/receive socket tests with a smaller amount of data
275 // to avoid long running times due to the small maximum send size.
276 const size_t kDataSize = 128 * 1024;
277 TcpInternal(loopback, kDataSize, kMaxSendSize);
278}
279
danilchapb7b9dca2016-08-05 05:55:43 -0700280// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
281#if defined(WEBRTC_WIN)
282#define MAYBE_TestWritableAfterPartialWriteIPv4 DISABLED_TestWritableAfterPartialWriteIPv4
283#else
284#define MAYBE_TestWritableAfterPartialWriteIPv4 TestWritableAfterPartialWriteIPv4
285#endif
286TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) {
jbauchf2a2bf42016-02-03 16:45:32 -0800287 WritableAfterPartialWrite(kIPv4Loopback);
288}
289
danilchapb7b9dca2016-08-05 05:55:43 -0700290// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
291#if defined(WEBRTC_WIN)
292#define MAYBE_TestWritableAfterPartialWriteIPv6 DISABLED_TestWritableAfterPartialWriteIPv6
293#else
294#define MAYBE_TestWritableAfterPartialWriteIPv6 TestWritableAfterPartialWriteIPv6
295#endif
296TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700297 MAYBE_SKIP_IPV6;
jbauchf2a2bf42016-02-03 16:45:32 -0800298 WritableAfterPartialWrite(kIPv6Loopback);
299}
300
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700301TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 SocketTest::TestConnectFailIPv6();
303}
304
305TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
306 SocketTest::TestConnectWithDnsLookupFailIPv4();
307}
308
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700309TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000310 SocketTest::TestConnectWithDnsLookupFailIPv6();
311}
312
313
314TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
315 SocketTest::TestConnectWithClosedSocketIPv4();
316}
317
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700318TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000319 SocketTest::TestConnectWithClosedSocketIPv6();
320}
321
322TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
323 SocketTest::TestConnectWhileNotClosedIPv4();
324}
325
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700326TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327 SocketTest::TestConnectWhileNotClosedIPv6();
328}
329
330TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
331 SocketTest::TestServerCloseDuringConnectIPv4();
332}
333
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700334TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000335 SocketTest::TestServerCloseDuringConnectIPv6();
336}
337
338TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
339 SocketTest::TestClientCloseDuringConnectIPv4();
340}
341
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700342TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 SocketTest::TestClientCloseDuringConnectIPv6();
344}
345
346TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
347 SocketTest::TestServerCloseIPv4();
348}
349
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700350TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000351 SocketTest::TestServerCloseIPv6();
352}
353
354TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
355 SocketTest::TestCloseInClosedCallbackIPv4();
356}
357
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700358TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000359 SocketTest::TestCloseInClosedCallbackIPv6();
360}
361
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000362TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363 SocketTest::TestSocketServerWaitIPv4();
364}
365
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700366TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000367 SocketTest::TestSocketServerWaitIPv6();
368}
369
370TEST_F(PhysicalSocketTest, TestTcpIPv4) {
371 SocketTest::TestTcpIPv4();
372}
373
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700374TEST_F(PhysicalSocketTest, TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000375 SocketTest::TestTcpIPv6();
376}
377
378TEST_F(PhysicalSocketTest, TestUdpIPv4) {
379 SocketTest::TestUdpIPv4();
380}
381
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700382TEST_F(PhysicalSocketTest, TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383 SocketTest::TestUdpIPv6();
384}
385
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000386// Disable for TSan v2, see
387// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
deadbeefc97be6a2015-09-25 11:00:38 -0700388// Also disable for MSan, see:
389// https://code.google.com/p/webrtc/issues/detail?id=4958
390// TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
minyuedf200d12015-10-17 13:10:46 -0700391// Also disable for ASan.
Henrik Kjellander0be3e042015-10-30 21:21:03 +0100392// Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
ivocf399f212015-11-19 06:44:32 -0800393// Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
minyuedf200d12015-10-17 13:10:46 -0700394#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
ivocf399f212015-11-19 06:44:32 -0800395 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
396 defined(WEBRTC_LINUX)
minyuedf200d12015-10-17 13:10:46 -0700397#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
398#else
399#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
400#endif
401TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 SocketTest::TestUdpReadyToSendIPv4();
403}
404
danilchapb7b9dca2016-08-05 05:55:43 -0700405// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
406#if defined(WEBRTC_WIN)
407#define MAYBE_TestUdpReadyToSendIPv6 DISABLED_TestUdpReadyToSendIPv6
408#else
409#define MAYBE_TestUdpReadyToSendIPv6 TestUdpReadyToSendIPv6
410#endif
411TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000412 SocketTest::TestUdpReadyToSendIPv6();
413}
414
415TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
416 SocketTest::TestGetSetOptionsIPv4();
417}
418
419TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
420 SocketTest::TestGetSetOptionsIPv6();
421}
422
423#if defined(WEBRTC_POSIX)
424
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700425// We don't get recv timestamps on Mac.
Stefan Holmer9131efd2016-05-23 18:19:26 +0200426#if !defined(WEBRTC_MAC)
427TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) {
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700428 SocketTest::TestSocketRecvTimestampIPv4();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200429}
430
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700431TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) {
432 SocketTest::TestSocketRecvTimestampIPv6();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200433}
434#endif
435
deadbeefc874d122017-02-13 15:41:59 -0800436// Verify that if the socket was unable to be bound to a real network interface
437// (not loopback), Bind will return an error.
438TEST_F(PhysicalSocketTest,
439 BindFailsIfNetworkBinderFailsForNonLoopbackInterface) {
440 FakeNetworkBinder fake_network_binder;
441 server_->set_network_binder(&fake_network_binder);
442 std::unique_ptr<AsyncSocket> socket(
443 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
444 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
445 EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0)));
446 server_->set_network_binder(nullptr);
447}
448
deadbeef9ffa13f2017-02-21 16:18:00 -0800449// Network binder shouldn't be used if the socket is bound to the "any" IP.
450TEST_F(PhysicalSocketTest,
451 NetworkBinderIsNotUsedForAnyIp) {
452 FakeNetworkBinder fake_network_binder;
453 server_->set_network_binder(&fake_network_binder);
454 std::unique_ptr<AsyncSocket> socket(
455 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
456 EXPECT_EQ(0, socket->Bind(SocketAddress("0.0.0.0", 0)));
457 EXPECT_EQ(0, fake_network_binder.num_binds());
458 server_->set_network_binder(nullptr);
459}
460
deadbeefc874d122017-02-13 15:41:59 -0800461// For a loopback interface, failures to bind to the interface should be
462// tolerated.
463TEST_F(PhysicalSocketTest,
464 BindSucceedsIfNetworkBinderFailsForLoopbackInterface) {
465 FakeNetworkBinder fake_network_binder;
466 server_->set_network_binder(&fake_network_binder);
467 std::unique_ptr<AsyncSocket> socket(
468 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
469 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
470 EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0)));
471 server_->set_network_binder(nullptr);
472}
473
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000474class PosixSignalDeliveryTest : public testing::Test {
475 public:
476 static void RecordSignal(int signum) {
477 signals_received_.push_back(signum);
478 signaled_thread_ = Thread::Current();
479 }
480
481 protected:
482 void SetUp() {
483 ss_.reset(new PhysicalSocketServer());
484 }
485
486 void TearDown() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800487 ss_.reset(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000488 signals_received_.clear();
deadbeef37f5ecf2017-02-27 14:06:41 -0800489 signaled_thread_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490 }
491
492 bool ExpectSignal(int signum) {
493 if (signals_received_.empty()) {
494 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
495 return false;
496 }
497 if (signals_received_[0] != signum) {
498 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
499 signals_received_[0] << ", expected " << signum;
500 return false;
501 }
502 signals_received_.erase(signals_received_.begin());
503 return true;
504 }
505
506 bool ExpectNone() {
507 bool ret = signals_received_.empty();
508 if (!ret) {
509 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
510 << ", expected none";
511 }
512 return ret;
513 }
514
515 static std::vector<int> signals_received_;
516 static Thread *signaled_thread_;
517
jbauch555604a2016-04-26 03:13:22 -0700518 std::unique_ptr<PhysicalSocketServer> ss_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000519};
520
521std::vector<int> PosixSignalDeliveryTest::signals_received_;
deadbeef37f5ecf2017-02-27 14:06:41 -0800522Thread* PosixSignalDeliveryTest::signaled_thread_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000523
524// Test receiving a synchronous signal while not in Wait() and then entering
525// Wait() afterwards.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000526TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000527 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
528 raise(SIGTERM);
529 EXPECT_TRUE(ss_->Wait(0, true));
530 EXPECT_TRUE(ExpectSignal(SIGTERM));
531 EXPECT_TRUE(ExpectNone());
532}
533
534// Test that we can handle getting tons of repeated signals and that we see all
535// the different ones.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000536TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000537 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
538 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
539 for (int i = 0; i < 10000; ++i) {
540 raise(SIGTERM);
541 }
542 raise(SIGINT);
543 EXPECT_TRUE(ss_->Wait(0, true));
544 // Order will be lowest signal numbers first.
545 EXPECT_TRUE(ExpectSignal(SIGINT));
546 EXPECT_TRUE(ExpectSignal(SIGTERM));
547 EXPECT_TRUE(ExpectNone());
548}
549
550// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000551TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000552 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
553 alarm(1);
554 EXPECT_TRUE(ss_->Wait(1500, true));
555 EXPECT_TRUE(ExpectSignal(SIGALRM));
556 EXPECT_TRUE(ExpectNone());
557}
558
559class RaiseSigTermRunnable : public Runnable {
560 void Run(Thread *thread) {
561 thread->socketserver()->Wait(1000, false);
562
563 // Allow SIGTERM. This will be the only thread with it not masked so it will
564 // be delivered to us.
565 sigset_t mask;
566 sigemptyset(&mask);
deadbeef37f5ecf2017-02-27 14:06:41 -0800567 pthread_sigmask(SIG_SETMASK, &mask, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000568
569 // Raise it.
570 raise(SIGTERM);
571 }
572};
573
574// Test that it works no matter what thread the kernel chooses to give the
575// signal to (since it's not guaranteed to be the one that Wait() runs on).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000576TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000577 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
578 // Mask out SIGTERM so that it can't be delivered to this thread.
579 sigset_t mask;
580 sigemptyset(&mask);
581 sigaddset(&mask, SIGTERM);
deadbeef37f5ecf2017-02-27 14:06:41 -0800582 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, nullptr));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000583 // Start a new thread that raises it. It will have to be delivered to that
584 // thread. Our implementation should safely handle it and dispatch
585 // RecordSignal() on this thread.
jbauch555604a2016-04-26 03:13:22 -0700586 std::unique_ptr<Thread> thread(new Thread());
587 std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000588 thread->Start(runnable.get());
589 EXPECT_TRUE(ss_->Wait(1500, true));
590 EXPECT_TRUE(ExpectSignal(SIGTERM));
591 EXPECT_EQ(Thread::Current(), signaled_thread_);
592 EXPECT_TRUE(ExpectNone());
593}
594
595#endif
596
597} // namespace rtc