blob: d0083bdcfcfc5771b8c1d3268febdf1a3434ff6b [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 {
92 return result_;
93 }
94
95 void set_result(NetworkBindingResult result) { result_ = result; }
96
97 private:
98 NetworkBindingResult result_ = NetworkBindingResult::SUCCESS;
99};
100
jbauch095ae152015-12-18 01:39:55 -0800101class PhysicalSocketTest : public SocketTest {
102 public:
103 // Set flag to simluate failures when calling "::accept" on a AsyncSocket.
104 void SetFailAccept(bool fail) { fail_accept_ = fail; }
105 bool FailAccept() const { return fail_accept_; }
106
jbauchf2a2bf42016-02-03 16:45:32 -0800107 // Maximum size to ::send to a socket. Set to < 0 to disable limiting.
108 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; }
109 int MaxSendSize() const { return max_send_size_; }
110
jbauch095ae152015-12-18 01:39:55 -0800111 protected:
112 PhysicalSocketTest()
113 : server_(new FakePhysicalSocketServer(this)),
114 scope_(server_.get()),
jbauchf2a2bf42016-02-03 16:45:32 -0800115 fail_accept_(false),
116 max_send_size_(-1) {
jbauch095ae152015-12-18 01:39:55 -0800117 }
118
119 void ConnectInternalAcceptError(const IPAddress& loopback);
jbauchf2a2bf42016-02-03 16:45:32 -0800120 void WritableAfterPartialWrite(const IPAddress& loopback);
jbauch095ae152015-12-18 01:39:55 -0800121
jbauch555604a2016-04-26 03:13:22 -0700122 std::unique_ptr<FakePhysicalSocketServer> server_;
jbauch095ae152015-12-18 01:39:55 -0800123 SocketServerScope scope_;
124 bool fail_accept_;
jbauchf2a2bf42016-02-03 16:45:32 -0800125 int max_send_size_;
jbauch095ae152015-12-18 01:39:55 -0800126};
127
128SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
129 sockaddr* addr,
130 socklen_t* addrlen) {
131 FakePhysicalSocketServer* ss =
132 static_cast<FakePhysicalSocketServer*>(socketserver());
133 if (ss->GetTest()->FailAccept()) {
134 return INVALID_SOCKET;
135 }
136
137 return SocketDispatcher::DoAccept(socket, addr, addrlen);
138}
139
jbauchf2a2bf42016-02-03 16:45:32 -0800140int FakeSocketDispatcher::DoSend(SOCKET socket, const char* buf, int len,
141 int flags) {
142 FakePhysicalSocketServer* ss =
143 static_cast<FakePhysicalSocketServer*>(socketserver());
144 if (ss->GetTest()->MaxSendSize() >= 0) {
145 len = std::min(len, ss->GetTest()->MaxSendSize());
146 }
147
148 return SocketDispatcher::DoSend(socket, buf, len, flags);
149}
150
151int FakeSocketDispatcher::DoSendTo(SOCKET socket, const char* buf, int len,
152 int flags, const struct sockaddr* dest_addr, socklen_t addrlen) {
153 FakePhysicalSocketServer* ss =
154 static_cast<FakePhysicalSocketServer*>(socketserver());
155 if (ss->GetTest()->MaxSendSize() >= 0) {
156 len = std::min(len, ss->GetTest()->MaxSendSize());
157 }
158
159 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
160 addrlen);
161}
162
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163TEST_F(PhysicalSocketTest, TestConnectIPv4) {
164 SocketTest::TestConnectIPv4();
165}
166
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700167TEST_F(PhysicalSocketTest, TestConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 SocketTest::TestConnectIPv6();
169}
170
171TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
172 SocketTest::TestConnectWithDnsLookupIPv4();
173}
174
175TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
176 SocketTest::TestConnectWithDnsLookupIPv6();
177}
178
179TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
180 SocketTest::TestConnectFailIPv4();
181}
182
jbauch095ae152015-12-18 01:39:55 -0800183void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) {
184 testing::StreamSink sink;
185 SocketAddress accept_addr;
186
187 // Create two clients.
jbauch555604a2016-04-26 03:13:22 -0700188 std::unique_ptr<AsyncSocket> client1(
189 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800190 sink.Monitor(client1.get());
191 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState());
192 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr());
193
jbauch555604a2016-04-26 03:13:22 -0700194 std::unique_ptr<AsyncSocket> client2(
195 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800196 sink.Monitor(client2.get());
197 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState());
198 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr());
199
200 // Create server and listen.
jbauch555604a2016-04-26 03:13:22 -0700201 std::unique_ptr<AsyncSocket> server(
jbauch095ae152015-12-18 01:39:55 -0800202 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
203 sink.Monitor(server.get());
204 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
205 EXPECT_EQ(0, server->Listen(5));
206 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState());
207
208 // Ensure no pending server connections, since we haven't done anything yet.
209 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
210 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
211 EXPECT_TRUE(accept_addr.IsNil());
212
213 // Attempt first connect to listening socket.
214 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress()));
215 EXPECT_FALSE(client1->GetLocalAddress().IsNil());
216 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress());
217
218 // Client is connecting, outcome not yet determined.
219 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState());
220 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_OPEN));
221 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_CLOSE));
222
223 // Server has pending connection, try to accept it (will fail).
224 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
225 // Simulate "::accept" returning an error.
226 SetFailAccept(true);
jbauch555604a2016-04-26 03:13:22 -0700227 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800228 EXPECT_FALSE(accepted);
229 ASSERT_TRUE(accept_addr.IsNil());
230
231 // Ensure no more pending server connections.
232 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
233 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
234 EXPECT_TRUE(accept_addr.IsNil());
235
236 // Attempt second connect to listening socket.
237 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress()));
238 EXPECT_FALSE(client2->GetLocalAddress().IsNil());
239 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress());
240
241 // Client is connecting, outcome not yet determined.
242 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState());
243 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_OPEN));
244 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_CLOSE));
245
246 // Server has pending connection, try to accept it (will succeed).
247 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
248 SetFailAccept(false);
jbauch555604a2016-04-26 03:13:22 -0700249 std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800250 ASSERT_TRUE(accepted2);
251 EXPECT_FALSE(accept_addr.IsNil());
252 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr);
253}
254
255TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) {
256 ConnectInternalAcceptError(kIPv4Loopback);
257}
258
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700259TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) {
260 MAYBE_SKIP_IPV6;
jbauch095ae152015-12-18 01:39:55 -0800261 ConnectInternalAcceptError(kIPv6Loopback);
262}
263
jbauchf2a2bf42016-02-03 16:45:32 -0800264void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
265 // Simulate a really small maximum send size.
266 const int kMaxSendSize = 128;
267 SetMaxSendSize(kMaxSendSize);
268
269 // Run the default send/receive socket tests with a smaller amount of data
270 // to avoid long running times due to the small maximum send size.
271 const size_t kDataSize = 128 * 1024;
272 TcpInternal(loopback, kDataSize, kMaxSendSize);
273}
274
danilchapb7b9dca2016-08-05 05:55:43 -0700275// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
276#if defined(WEBRTC_WIN)
277#define MAYBE_TestWritableAfterPartialWriteIPv4 DISABLED_TestWritableAfterPartialWriteIPv4
278#else
279#define MAYBE_TestWritableAfterPartialWriteIPv4 TestWritableAfterPartialWriteIPv4
280#endif
281TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) {
jbauchf2a2bf42016-02-03 16:45:32 -0800282 WritableAfterPartialWrite(kIPv4Loopback);
283}
284
danilchapb7b9dca2016-08-05 05:55:43 -0700285// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
286#if defined(WEBRTC_WIN)
287#define MAYBE_TestWritableAfterPartialWriteIPv6 DISABLED_TestWritableAfterPartialWriteIPv6
288#else
289#define MAYBE_TestWritableAfterPartialWriteIPv6 TestWritableAfterPartialWriteIPv6
290#endif
291TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700292 MAYBE_SKIP_IPV6;
jbauchf2a2bf42016-02-03 16:45:32 -0800293 WritableAfterPartialWrite(kIPv6Loopback);
294}
295
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700296TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 SocketTest::TestConnectFailIPv6();
298}
299
300TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
301 SocketTest::TestConnectWithDnsLookupFailIPv4();
302}
303
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700304TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305 SocketTest::TestConnectWithDnsLookupFailIPv6();
306}
307
308
309TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
310 SocketTest::TestConnectWithClosedSocketIPv4();
311}
312
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700313TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314 SocketTest::TestConnectWithClosedSocketIPv6();
315}
316
317TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
318 SocketTest::TestConnectWhileNotClosedIPv4();
319}
320
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700321TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322 SocketTest::TestConnectWhileNotClosedIPv6();
323}
324
325TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
326 SocketTest::TestServerCloseDuringConnectIPv4();
327}
328
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700329TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330 SocketTest::TestServerCloseDuringConnectIPv6();
331}
332
333TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
334 SocketTest::TestClientCloseDuringConnectIPv4();
335}
336
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700337TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000338 SocketTest::TestClientCloseDuringConnectIPv6();
339}
340
341TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
342 SocketTest::TestServerCloseIPv4();
343}
344
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700345TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000346 SocketTest::TestServerCloseIPv6();
347}
348
349TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
350 SocketTest::TestCloseInClosedCallbackIPv4();
351}
352
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700353TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000354 SocketTest::TestCloseInClosedCallbackIPv6();
355}
356
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000357TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358 SocketTest::TestSocketServerWaitIPv4();
359}
360
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700361TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000362 SocketTest::TestSocketServerWaitIPv6();
363}
364
365TEST_F(PhysicalSocketTest, TestTcpIPv4) {
366 SocketTest::TestTcpIPv4();
367}
368
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700369TEST_F(PhysicalSocketTest, TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000370 SocketTest::TestTcpIPv6();
371}
372
373TEST_F(PhysicalSocketTest, TestUdpIPv4) {
374 SocketTest::TestUdpIPv4();
375}
376
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700377TEST_F(PhysicalSocketTest, TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000378 SocketTest::TestUdpIPv6();
379}
380
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000381// Disable for TSan v2, see
382// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
deadbeefc97be6a2015-09-25 11:00:38 -0700383// Also disable for MSan, see:
384// https://code.google.com/p/webrtc/issues/detail?id=4958
385// TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
minyuedf200d12015-10-17 13:10:46 -0700386// Also disable for ASan.
Henrik Kjellander0be3e042015-10-30 21:21:03 +0100387// Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
ivocf399f212015-11-19 06:44:32 -0800388// Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
minyuedf200d12015-10-17 13:10:46 -0700389#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
ivocf399f212015-11-19 06:44:32 -0800390 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
391 defined(WEBRTC_LINUX)
minyuedf200d12015-10-17 13:10:46 -0700392#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
393#else
394#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
395#endif
396TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000397 SocketTest::TestUdpReadyToSendIPv4();
398}
399
danilchapb7b9dca2016-08-05 05:55:43 -0700400// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
401#if defined(WEBRTC_WIN)
402#define MAYBE_TestUdpReadyToSendIPv6 DISABLED_TestUdpReadyToSendIPv6
403#else
404#define MAYBE_TestUdpReadyToSendIPv6 TestUdpReadyToSendIPv6
405#endif
406TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 SocketTest::TestUdpReadyToSendIPv6();
408}
409
410TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
411 SocketTest::TestGetSetOptionsIPv4();
412}
413
414TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
415 SocketTest::TestGetSetOptionsIPv6();
416}
417
418#if defined(WEBRTC_POSIX)
419
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700420// We don't get recv timestamps on Mac.
Stefan Holmer9131efd2016-05-23 18:19:26 +0200421#if !defined(WEBRTC_MAC)
422TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) {
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700423 SocketTest::TestSocketRecvTimestampIPv4();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200424}
425
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700426TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) {
427 SocketTest::TestSocketRecvTimestampIPv6();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200428}
429#endif
430
deadbeefc874d122017-02-13 15:41:59 -0800431// Verify that if the socket was unable to be bound to a real network interface
432// (not loopback), Bind will return an error.
433TEST_F(PhysicalSocketTest,
434 BindFailsIfNetworkBinderFailsForNonLoopbackInterface) {
435 FakeNetworkBinder fake_network_binder;
436 server_->set_network_binder(&fake_network_binder);
437 std::unique_ptr<AsyncSocket> socket(
438 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
439 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
440 EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0)));
441 server_->set_network_binder(nullptr);
442}
443
444// For a loopback interface, failures to bind to the interface should be
445// tolerated.
446TEST_F(PhysicalSocketTest,
447 BindSucceedsIfNetworkBinderFailsForLoopbackInterface) {
448 FakeNetworkBinder fake_network_binder;
449 server_->set_network_binder(&fake_network_binder);
450 std::unique_ptr<AsyncSocket> socket(
451 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
452 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
453 EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0)));
454 server_->set_network_binder(nullptr);
455}
456
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000457class PosixSignalDeliveryTest : public testing::Test {
458 public:
459 static void RecordSignal(int signum) {
460 signals_received_.push_back(signum);
461 signaled_thread_ = Thread::Current();
462 }
463
464 protected:
465 void SetUp() {
466 ss_.reset(new PhysicalSocketServer());
467 }
468
469 void TearDown() {
470 ss_.reset(NULL);
471 signals_received_.clear();
472 signaled_thread_ = NULL;
473 }
474
475 bool ExpectSignal(int signum) {
476 if (signals_received_.empty()) {
477 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
478 return false;
479 }
480 if (signals_received_[0] != signum) {
481 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
482 signals_received_[0] << ", expected " << signum;
483 return false;
484 }
485 signals_received_.erase(signals_received_.begin());
486 return true;
487 }
488
489 bool ExpectNone() {
490 bool ret = signals_received_.empty();
491 if (!ret) {
492 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
493 << ", expected none";
494 }
495 return ret;
496 }
497
498 static std::vector<int> signals_received_;
499 static Thread *signaled_thread_;
500
jbauch555604a2016-04-26 03:13:22 -0700501 std::unique_ptr<PhysicalSocketServer> ss_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000502};
503
504std::vector<int> PosixSignalDeliveryTest::signals_received_;
505Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
506
507// Test receiving a synchronous signal while not in Wait() and then entering
508// Wait() afterwards.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000509TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000510 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
511 raise(SIGTERM);
512 EXPECT_TRUE(ss_->Wait(0, true));
513 EXPECT_TRUE(ExpectSignal(SIGTERM));
514 EXPECT_TRUE(ExpectNone());
515}
516
517// Test that we can handle getting tons of repeated signals and that we see all
518// the different ones.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000519TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000520 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
521 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
522 for (int i = 0; i < 10000; ++i) {
523 raise(SIGTERM);
524 }
525 raise(SIGINT);
526 EXPECT_TRUE(ss_->Wait(0, true));
527 // Order will be lowest signal numbers first.
528 EXPECT_TRUE(ExpectSignal(SIGINT));
529 EXPECT_TRUE(ExpectSignal(SIGTERM));
530 EXPECT_TRUE(ExpectNone());
531}
532
533// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000534TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000535 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
536 alarm(1);
537 EXPECT_TRUE(ss_->Wait(1500, true));
538 EXPECT_TRUE(ExpectSignal(SIGALRM));
539 EXPECT_TRUE(ExpectNone());
540}
541
542class RaiseSigTermRunnable : public Runnable {
543 void Run(Thread *thread) {
544 thread->socketserver()->Wait(1000, false);
545
546 // Allow SIGTERM. This will be the only thread with it not masked so it will
547 // be delivered to us.
548 sigset_t mask;
549 sigemptyset(&mask);
550 pthread_sigmask(SIG_SETMASK, &mask, NULL);
551
552 // Raise it.
553 raise(SIGTERM);
554 }
555};
556
557// Test that it works no matter what thread the kernel chooses to give the
558// signal to (since it's not guaranteed to be the one that Wait() runs on).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000559TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000560 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
561 // Mask out SIGTERM so that it can't be delivered to this thread.
562 sigset_t mask;
563 sigemptyset(&mask);
564 sigaddset(&mask, SIGTERM);
565 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
566 // Start a new thread that raises it. It will have to be delivered to that
567 // thread. Our implementation should safely handle it and dispatch
568 // RecordSignal() on this thread.
jbauch555604a2016-04-26 03:13:22 -0700569 std::unique_ptr<Thread> thread(new Thread());
570 std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000571 thread->Start(runnable.get());
572 EXPECT_TRUE(ss_->Wait(1500, true));
573 EXPECT_TRUE(ExpectSignal(SIGTERM));
574 EXPECT_EQ(Thread::Current(), signaled_thread_);
575 EXPECT_TRUE(ExpectNone());
576}
577
578#endif
579
580} // namespace rtc