blob: 63d2f0d92c80a14fad1965ef77be2de851094ce6 [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)),
118 scope_(server_.get()),
jbauchf2a2bf42016-02-03 16:45:32 -0800119 fail_accept_(false),
120 max_send_size_(-1) {
jbauch095ae152015-12-18 01:39:55 -0800121 }
122
123 void ConnectInternalAcceptError(const IPAddress& loopback);
jbauchf2a2bf42016-02-03 16:45:32 -0800124 void WritableAfterPartialWrite(const IPAddress& loopback);
jbauch095ae152015-12-18 01:39:55 -0800125
jbauch555604a2016-04-26 03:13:22 -0700126 std::unique_ptr<FakePhysicalSocketServer> server_;
jbauch095ae152015-12-18 01:39:55 -0800127 SocketServerScope scope_;
128 bool fail_accept_;
jbauchf2a2bf42016-02-03 16:45:32 -0800129 int max_send_size_;
jbauch095ae152015-12-18 01:39:55 -0800130};
131
132SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
133 sockaddr* addr,
134 socklen_t* addrlen) {
135 FakePhysicalSocketServer* ss =
136 static_cast<FakePhysicalSocketServer*>(socketserver());
137 if (ss->GetTest()->FailAccept()) {
138 return INVALID_SOCKET;
139 }
140
141 return SocketDispatcher::DoAccept(socket, addr, addrlen);
142}
143
jbauchf2a2bf42016-02-03 16:45:32 -0800144int FakeSocketDispatcher::DoSend(SOCKET socket, const char* buf, int len,
145 int flags) {
146 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
155int FakeSocketDispatcher::DoSendTo(SOCKET socket, const char* buf, int len,
156 int flags, const struct sockaddr* dest_addr, socklen_t addrlen) {
157 FakePhysicalSocketServer* ss =
158 static_cast<FakePhysicalSocketServer*>(socketserver());
159 if (ss->GetTest()->MaxSendSize() >= 0) {
160 len = std::min(len, ss->GetTest()->MaxSendSize());
161 }
162
163 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
164 addrlen);
165}
166
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167TEST_F(PhysicalSocketTest, TestConnectIPv4) {
168 SocketTest::TestConnectIPv4();
169}
170
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700171TEST_F(PhysicalSocketTest, TestConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 SocketTest::TestConnectIPv6();
173}
174
175TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
176 SocketTest::TestConnectWithDnsLookupIPv4();
177}
178
179TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
180 SocketTest::TestConnectWithDnsLookupIPv6();
181}
182
183TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
184 SocketTest::TestConnectFailIPv4();
185}
186
jbauch095ae152015-12-18 01:39:55 -0800187void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) {
188 testing::StreamSink sink;
189 SocketAddress accept_addr;
190
191 // Create two clients.
jbauch555604a2016-04-26 03:13:22 -0700192 std::unique_ptr<AsyncSocket> client1(
193 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800194 sink.Monitor(client1.get());
195 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState());
196 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr());
197
jbauch555604a2016-04-26 03:13:22 -0700198 std::unique_ptr<AsyncSocket> client2(
199 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800200 sink.Monitor(client2.get());
201 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState());
202 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr());
203
204 // Create server and listen.
jbauch555604a2016-04-26 03:13:22 -0700205 std::unique_ptr<AsyncSocket> server(
jbauch095ae152015-12-18 01:39:55 -0800206 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
207 sink.Monitor(server.get());
208 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
209 EXPECT_EQ(0, server->Listen(5));
210 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState());
211
212 // Ensure no pending server connections, since we haven't done anything yet.
213 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
214 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
215 EXPECT_TRUE(accept_addr.IsNil());
216
217 // Attempt first connect to listening socket.
218 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress()));
219 EXPECT_FALSE(client1->GetLocalAddress().IsNil());
220 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress());
221
222 // Client is connecting, outcome not yet determined.
223 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState());
224 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_OPEN));
225 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_CLOSE));
226
227 // Server has pending connection, try to accept it (will fail).
228 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
229 // 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.
236 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
237 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());
247 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_OPEN));
248 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_CLOSE));
249
250 // Server has pending connection, try to accept it (will succeed).
251 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
252 SetFailAccept(false);
jbauch555604a2016-04-26 03:13:22 -0700253 std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800254 ASSERT_TRUE(accepted2);
255 EXPECT_FALSE(accept_addr.IsNil());
256 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr);
257}
258
259TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) {
260 ConnectInternalAcceptError(kIPv4Loopback);
261}
262
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700263TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) {
264 MAYBE_SKIP_IPV6;
jbauch095ae152015-12-18 01:39:55 -0800265 ConnectInternalAcceptError(kIPv6Loopback);
266}
267
jbauchf2a2bf42016-02-03 16:45:32 -0800268void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
269 // Simulate a really small maximum send size.
270 const int kMaxSendSize = 128;
271 SetMaxSendSize(kMaxSendSize);
272
273 // Run the default send/receive socket tests with a smaller amount of data
274 // to avoid long running times due to the small maximum send size.
275 const size_t kDataSize = 128 * 1024;
276 TcpInternal(loopback, kDataSize, kMaxSendSize);
277}
278
danilchapb7b9dca2016-08-05 05:55:43 -0700279// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
280#if defined(WEBRTC_WIN)
281#define MAYBE_TestWritableAfterPartialWriteIPv4 DISABLED_TestWritableAfterPartialWriteIPv4
282#else
283#define MAYBE_TestWritableAfterPartialWriteIPv4 TestWritableAfterPartialWriteIPv4
284#endif
285TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) {
jbauchf2a2bf42016-02-03 16:45:32 -0800286 WritableAfterPartialWrite(kIPv4Loopback);
287}
288
danilchapb7b9dca2016-08-05 05:55:43 -0700289// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
290#if defined(WEBRTC_WIN)
291#define MAYBE_TestWritableAfterPartialWriteIPv6 DISABLED_TestWritableAfterPartialWriteIPv6
292#else
293#define MAYBE_TestWritableAfterPartialWriteIPv6 TestWritableAfterPartialWriteIPv6
294#endif
295TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700296 MAYBE_SKIP_IPV6;
jbauchf2a2bf42016-02-03 16:45:32 -0800297 WritableAfterPartialWrite(kIPv6Loopback);
298}
299
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700300TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301 SocketTest::TestConnectFailIPv6();
302}
303
304TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
305 SocketTest::TestConnectWithDnsLookupFailIPv4();
306}
307
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700308TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 SocketTest::TestConnectWithDnsLookupFailIPv6();
310}
311
312
313TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
314 SocketTest::TestConnectWithClosedSocketIPv4();
315}
316
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700317TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318 SocketTest::TestConnectWithClosedSocketIPv6();
319}
320
321TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
322 SocketTest::TestConnectWhileNotClosedIPv4();
323}
324
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700325TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000326 SocketTest::TestConnectWhileNotClosedIPv6();
327}
328
329TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
330 SocketTest::TestServerCloseDuringConnectIPv4();
331}
332
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700333TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334 SocketTest::TestServerCloseDuringConnectIPv6();
335}
336
337TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
338 SocketTest::TestClientCloseDuringConnectIPv4();
339}
340
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700341TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342 SocketTest::TestClientCloseDuringConnectIPv6();
343}
344
345TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
346 SocketTest::TestServerCloseIPv4();
347}
348
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700349TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000350 SocketTest::TestServerCloseIPv6();
351}
352
353TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
354 SocketTest::TestCloseInClosedCallbackIPv4();
355}
356
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700357TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358 SocketTest::TestCloseInClosedCallbackIPv6();
359}
360
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000361TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000362 SocketTest::TestSocketServerWaitIPv4();
363}
364
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700365TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 SocketTest::TestSocketServerWaitIPv6();
367}
368
369TEST_F(PhysicalSocketTest, TestTcpIPv4) {
370 SocketTest::TestTcpIPv4();
371}
372
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700373TEST_F(PhysicalSocketTest, TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374 SocketTest::TestTcpIPv6();
375}
376
377TEST_F(PhysicalSocketTest, TestUdpIPv4) {
378 SocketTest::TestUdpIPv4();
379}
380
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700381TEST_F(PhysicalSocketTest, TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000382 SocketTest::TestUdpIPv6();
383}
384
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000385// Disable for TSan v2, see
386// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
deadbeefc97be6a2015-09-25 11:00:38 -0700387// Also disable for MSan, see:
388// https://code.google.com/p/webrtc/issues/detail?id=4958
389// TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
minyuedf200d12015-10-17 13:10:46 -0700390// Also disable for ASan.
Henrik Kjellander0be3e042015-10-30 21:21:03 +0100391// Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
ivocf399f212015-11-19 06:44:32 -0800392// Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
minyuedf200d12015-10-17 13:10:46 -0700393#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
ivocf399f212015-11-19 06:44:32 -0800394 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
395 defined(WEBRTC_LINUX)
minyuedf200d12015-10-17 13:10:46 -0700396#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
397#else
398#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
399#endif
400TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000401 SocketTest::TestUdpReadyToSendIPv4();
402}
403
danilchapb7b9dca2016-08-05 05:55:43 -0700404// https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
405#if defined(WEBRTC_WIN)
406#define MAYBE_TestUdpReadyToSendIPv6 DISABLED_TestUdpReadyToSendIPv6
407#else
408#define MAYBE_TestUdpReadyToSendIPv6 TestUdpReadyToSendIPv6
409#endif
410TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000411 SocketTest::TestUdpReadyToSendIPv6();
412}
413
414TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
415 SocketTest::TestGetSetOptionsIPv4();
416}
417
418TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
419 SocketTest::TestGetSetOptionsIPv6();
420}
421
422#if defined(WEBRTC_POSIX)
423
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700424// We don't get recv timestamps on Mac.
Stefan Holmer9131efd2016-05-23 18:19:26 +0200425#if !defined(WEBRTC_MAC)
426TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) {
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700427 SocketTest::TestSocketRecvTimestampIPv4();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200428}
429
Taylor Brandstetter6f825352016-08-11 15:38:28 -0700430TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) {
431 SocketTest::TestSocketRecvTimestampIPv6();
Stefan Holmer9131efd2016-05-23 18:19:26 +0200432}
433#endif
434
deadbeefc874d122017-02-13 15:41:59 -0800435// Verify that if the socket was unable to be bound to a real network interface
436// (not loopback), Bind will return an error.
437TEST_F(PhysicalSocketTest,
438 BindFailsIfNetworkBinderFailsForNonLoopbackInterface) {
439 FakeNetworkBinder fake_network_binder;
440 server_->set_network_binder(&fake_network_binder);
441 std::unique_ptr<AsyncSocket> socket(
442 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
443 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
444 EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0)));
445 server_->set_network_binder(nullptr);
446}
447
deadbeef9ffa13f2017-02-21 16:18:00 -0800448// Network binder shouldn't be used if the socket is bound to the "any" IP.
449TEST_F(PhysicalSocketTest,
450 NetworkBinderIsNotUsedForAnyIp) {
451 FakeNetworkBinder fake_network_binder;
452 server_->set_network_binder(&fake_network_binder);
453 std::unique_ptr<AsyncSocket> socket(
454 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
455 EXPECT_EQ(0, socket->Bind(SocketAddress("0.0.0.0", 0)));
456 EXPECT_EQ(0, fake_network_binder.num_binds());
457 server_->set_network_binder(nullptr);
458}
459
deadbeefc874d122017-02-13 15:41:59 -0800460// For a loopback interface, failures to bind to the interface should be
461// tolerated.
462TEST_F(PhysicalSocketTest,
463 BindSucceedsIfNetworkBinderFailsForLoopbackInterface) {
464 FakeNetworkBinder fake_network_binder;
465 server_->set_network_binder(&fake_network_binder);
466 std::unique_ptr<AsyncSocket> socket(
467 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM));
468 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
469 EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0)));
470 server_->set_network_binder(nullptr);
471}
472
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000473class PosixSignalDeliveryTest : public testing::Test {
474 public:
475 static void RecordSignal(int signum) {
476 signals_received_.push_back(signum);
477 signaled_thread_ = Thread::Current();
478 }
479
480 protected:
481 void SetUp() {
482 ss_.reset(new PhysicalSocketServer());
483 }
484
485 void TearDown() {
486 ss_.reset(NULL);
487 signals_received_.clear();
488 signaled_thread_ = NULL;
489 }
490
491 bool ExpectSignal(int signum) {
492 if (signals_received_.empty()) {
493 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
494 return false;
495 }
496 if (signals_received_[0] != signum) {
497 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
498 signals_received_[0] << ", expected " << signum;
499 return false;
500 }
501 signals_received_.erase(signals_received_.begin());
502 return true;
503 }
504
505 bool ExpectNone() {
506 bool ret = signals_received_.empty();
507 if (!ret) {
508 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
509 << ", expected none";
510 }
511 return ret;
512 }
513
514 static std::vector<int> signals_received_;
515 static Thread *signaled_thread_;
516
jbauch555604a2016-04-26 03:13:22 -0700517 std::unique_ptr<PhysicalSocketServer> ss_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000518};
519
520std::vector<int> PosixSignalDeliveryTest::signals_received_;
521Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
522
523// Test receiving a synchronous signal while not in Wait() and then entering
524// Wait() afterwards.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000525TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000526 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
527 raise(SIGTERM);
528 EXPECT_TRUE(ss_->Wait(0, true));
529 EXPECT_TRUE(ExpectSignal(SIGTERM));
530 EXPECT_TRUE(ExpectNone());
531}
532
533// Test that we can handle getting tons of repeated signals and that we see all
534// the different ones.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000535TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000536 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
537 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
538 for (int i = 0; i < 10000; ++i) {
539 raise(SIGTERM);
540 }
541 raise(SIGINT);
542 EXPECT_TRUE(ss_->Wait(0, true));
543 // Order will be lowest signal numbers first.
544 EXPECT_TRUE(ExpectSignal(SIGINT));
545 EXPECT_TRUE(ExpectSignal(SIGTERM));
546 EXPECT_TRUE(ExpectNone());
547}
548
549// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000550TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000551 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
552 alarm(1);
553 EXPECT_TRUE(ss_->Wait(1500, true));
554 EXPECT_TRUE(ExpectSignal(SIGALRM));
555 EXPECT_TRUE(ExpectNone());
556}
557
558class RaiseSigTermRunnable : public Runnable {
559 void Run(Thread *thread) {
560 thread->socketserver()->Wait(1000, false);
561
562 // Allow SIGTERM. This will be the only thread with it not masked so it will
563 // be delivered to us.
564 sigset_t mask;
565 sigemptyset(&mask);
566 pthread_sigmask(SIG_SETMASK, &mask, NULL);
567
568 // Raise it.
569 raise(SIGTERM);
570 }
571};
572
573// Test that it works no matter what thread the kernel chooses to give the
574// signal to (since it's not guaranteed to be the one that Wait() runs on).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000575TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000576 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
577 // Mask out SIGTERM so that it can't be delivered to this thread.
578 sigset_t mask;
579 sigemptyset(&mask);
580 sigaddset(&mask, SIGTERM);
581 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
582 // Start a new thread that raises it. It will have to be delivered to that
583 // thread. Our implementation should safely handle it and dispatch
584 // RecordSignal() on this thread.
jbauch555604a2016-04-26 03:13:22 -0700585 std::unique_ptr<Thread> thread(new Thread());
586 std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000587 thread->Start(runnable.get());
588 EXPECT_TRUE(ss_->Wait(1500, true));
589 EXPECT_TRUE(ExpectSignal(SIGTERM));
590 EXPECT_EQ(Thread::Current(), signaled_thread_);
591 EXPECT_TRUE(ExpectNone());
592}
593
594#endif
595
596} // namespace rtc