blob: ad0d4657e880c4728b8076d63edadb0347810b80 [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>
12#include <stdarg.h>
13
14#include "webrtc/base/gunit.h"
15#include "webrtc/base/logging.h"
16#include "webrtc/base/physicalsocketserver.h"
17#include "webrtc/base/scoped_ptr.h"
18#include "webrtc/base/socket_unittest.h"
19#include "webrtc/base/testutils.h"
20#include "webrtc/base/thread.h"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000021#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
23namespace rtc {
24
25class PhysicalSocketTest : public SocketTest {
26};
27
28TEST_F(PhysicalSocketTest, TestConnectIPv4) {
29 SocketTest::TestConnectIPv4();
30}
31
minyue5d696482015-08-19 04:42:03 -070032// Crashes on Linux. See webrtc:4923.
33#if defined(WEBRTC_LINUX)
34#define MAYBE_TestConnectIPv6 DISABLED_TestConnectIPv6
35#else
36#define MAYBE_TestConnectIPv6 TestConnectIPv6
37#endif
38TEST_F(PhysicalSocketTest, MAYBE_TestConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 SocketTest::TestConnectIPv6();
40}
41
42TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
43 SocketTest::TestConnectWithDnsLookupIPv4();
44}
45
46TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
47 SocketTest::TestConnectWithDnsLookupIPv6();
48}
49
50TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
51 SocketTest::TestConnectFailIPv4();
52}
53
minyue5d696482015-08-19 04:42:03 -070054// Crashes on Linux. See webrtc:4923.
55#if defined(WEBRTC_LINUX)
56#define MAYBE_TestConnectFailIPv6 DISABLED_TestConnectFailIPv6
57#else
58#define MAYBE_TestConnectFailIPv6 TestConnectFailIPv6
59#endif
60TEST_F(PhysicalSocketTest, MAYBE_TestConnectFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061 SocketTest::TestConnectFailIPv6();
62}
63
64TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
65 SocketTest::TestConnectWithDnsLookupFailIPv4();
66}
67
minyue5d696482015-08-19 04:42:03 -070068// Crashes on Linux. See webrtc:4923.
69#if defined(WEBRTC_LINUX)
70#define MAYBE_TestConnectWithDnsLookupFailIPv6 \
71 DISABLED_TestConnectWithDnsLookupFailIPv6
72#else
73#define MAYBE_TestConnectWithDnsLookupFailIPv6 \
74 TestConnectWithDnsLookupFailIPv6
75#endif
76TEST_F(PhysicalSocketTest, MAYBE_TestConnectWithDnsLookupFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 SocketTest::TestConnectWithDnsLookupFailIPv6();
78}
79
80
81TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
82 SocketTest::TestConnectWithClosedSocketIPv4();
83}
84
minyue5d696482015-08-19 04:42:03 -070085// Crashes on Linux. See webrtc:4923.
86#if defined(WEBRTC_LINUX)
87#define MAYBE_TestConnectWithClosedSocketIPv6 \
88 DISABLED_TestConnectWithClosedSocketIPv6
89#else
90#define MAYBE_TestConnectWithClosedSocketIPv6 TestConnectWithClosedSocketIPv6
91#endif
92TEST_F(PhysicalSocketTest, MAYBE_TestConnectWithClosedSocketIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 SocketTest::TestConnectWithClosedSocketIPv6();
94}
95
96TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
97 SocketTest::TestConnectWhileNotClosedIPv4();
98}
99
minyue5d696482015-08-19 04:42:03 -0700100// Crashes on Linux. See webrtc:4923.
101#if defined(WEBRTC_LINUX)
102#define MAYBE_TestConnectWhileNotClosedIPv6 \
103 DISABLED_TestConnectWhileNotClosedIPv6
104#else
105#define MAYBE_TestConnectWhileNotClosedIPv6 TestConnectWhileNotClosedIPv6
106#endif
107TEST_F(PhysicalSocketTest, MAYBE_TestConnectWhileNotClosedIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 SocketTest::TestConnectWhileNotClosedIPv6();
109}
110
111TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
112 SocketTest::TestServerCloseDuringConnectIPv4();
113}
114
minyue5d696482015-08-19 04:42:03 -0700115// Crashes on Linux. See webrtc:4923.
116#if defined(WEBRTC_LINUX)
117#define MAYBE_TestServerCloseDuringConnectIPv6 \
118 DISABLED_TestServerCloseDuringConnectIPv6
119#else
120#define MAYBE_TestServerCloseDuringConnectIPv6 TestServerCloseDuringConnectIPv6
121#endif
122TEST_F(PhysicalSocketTest, MAYBE_TestServerCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 SocketTest::TestServerCloseDuringConnectIPv6();
124}
125
126TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
127 SocketTest::TestClientCloseDuringConnectIPv4();
128}
129
minyue5d696482015-08-19 04:42:03 -0700130// Crashes on Linux. See webrtc:4923.
131#if defined(WEBRTC_LINUX)
132#define MAYBE_TestClientCloseDuringConnectIPv6 \
133 DISABLED_TestClientCloseDuringConnectIPv6
134#else
135#define MAYBE_TestClientCloseDuringConnectIPv6 TestClientCloseDuringConnectIPv6
136#endif
137TEST_F(PhysicalSocketTest, MAYBE_TestClientCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 SocketTest::TestClientCloseDuringConnectIPv6();
139}
140
141TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
142 SocketTest::TestServerCloseIPv4();
143}
144
minyue5d696482015-08-19 04:42:03 -0700145// Crashes on Linux. See webrtc:4923.
146#if defined(WEBRTC_LINUX)
147#define MAYBE_TestServerCloseIPv6 DISABLED_TestServerCloseIPv6
148#else
149#define MAYBE_TestServerCloseIPv6 TestServerCloseIPv6
150#endif
151TEST_F(PhysicalSocketTest, MAYBE_TestServerCloseIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 SocketTest::TestServerCloseIPv6();
153}
154
155TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
156 SocketTest::TestCloseInClosedCallbackIPv4();
157}
158
minyue5d696482015-08-19 04:42:03 -0700159// Crashes on Linux. See webrtc:4923.
160#if defined(WEBRTC_LINUX)
161#define MAYBE_TestCloseInClosedCallbackIPv6 \
162 DISABLED_TestCloseInClosedCallbackIPv6
163#else
164#define MAYBE_TestCloseInClosedCallbackIPv6 TestCloseInClosedCallbackIPv6
165#endif
166TEST_F(PhysicalSocketTest, MAYBE_TestCloseInClosedCallbackIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167 SocketTest::TestCloseInClosedCallbackIPv6();
168}
169
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000170TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 SocketTest::TestSocketServerWaitIPv4();
172}
173
minyue5d696482015-08-19 04:42:03 -0700174// Crashes on Linux. See webrtc:4923.
175#if defined(WEBRTC_LINUX)
176#define MAYBE_TestSocketServerWaitIPv6 DISABLED_TestSocketServerWaitIPv6
177#else
178#define MAYBE_TestSocketServerWaitIPv6 TestSocketServerWaitIPv6
179#endif
180TEST_F(PhysicalSocketTest, MAYBE_TestSocketServerWaitIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000181 SocketTest::TestSocketServerWaitIPv6();
182}
183
184TEST_F(PhysicalSocketTest, TestTcpIPv4) {
185 SocketTest::TestTcpIPv4();
186}
187
minyue5d696482015-08-19 04:42:03 -0700188// Crashes on Linux. See webrtc:4923.
189#if defined(WEBRTC_LINUX)
190#define MAYBE_TestTcpIPv6 DISABLED_TestTcpIPv6
191#else
192#define MAYBE_TestTcpIPv6 TestTcpIPv6
193#endif
194TEST_F(PhysicalSocketTest, MAYBE_TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 SocketTest::TestTcpIPv6();
196}
197
198TEST_F(PhysicalSocketTest, TestUdpIPv4) {
199 SocketTest::TestUdpIPv4();
200}
201
minyue5d696482015-08-19 04:42:03 -0700202// Crashes on Linux. See webrtc:4923.
203#if defined(WEBRTC_LINUX)
204#define MAYBE_TestUdpIPv6 DISABLED_TestUdpIPv6
205#else
206#define MAYBE_TestUdpIPv6 TestUdpIPv6
207#endif
208TEST_F(PhysicalSocketTest, MAYBE_TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209 SocketTest::TestUdpIPv6();
210}
211
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000212// Disable for TSan v2, see
213// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
deadbeefc97be6a2015-09-25 11:00:38 -0700214// Also disable for MSan, see:
215// https://code.google.com/p/webrtc/issues/detail?id=4958
216// TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
minyuedf200d12015-10-17 13:10:46 -0700217// Also disable for ASan.
Henrik Kjellander0be3e042015-10-30 21:21:03 +0100218// Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
ivocf399f212015-11-19 06:44:32 -0800219// Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
minyuedf200d12015-10-17 13:10:46 -0700220#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
ivocf399f212015-11-19 06:44:32 -0800221 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
222 defined(WEBRTC_LINUX)
minyuedf200d12015-10-17 13:10:46 -0700223#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
224#else
225#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
226#endif
227TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 SocketTest::TestUdpReadyToSendIPv4();
229}
230
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000231TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232 SocketTest::TestUdpReadyToSendIPv6();
233}
234
235TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
236 SocketTest::TestGetSetOptionsIPv4();
237}
238
239TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
240 SocketTest::TestGetSetOptionsIPv6();
241}
242
243#if defined(WEBRTC_POSIX)
244
245class PosixSignalDeliveryTest : public testing::Test {
246 public:
247 static void RecordSignal(int signum) {
248 signals_received_.push_back(signum);
249 signaled_thread_ = Thread::Current();
250 }
251
252 protected:
253 void SetUp() {
254 ss_.reset(new PhysicalSocketServer());
255 }
256
257 void TearDown() {
258 ss_.reset(NULL);
259 signals_received_.clear();
260 signaled_thread_ = NULL;
261 }
262
263 bool ExpectSignal(int signum) {
264 if (signals_received_.empty()) {
265 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
266 return false;
267 }
268 if (signals_received_[0] != signum) {
269 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
270 signals_received_[0] << ", expected " << signum;
271 return false;
272 }
273 signals_received_.erase(signals_received_.begin());
274 return true;
275 }
276
277 bool ExpectNone() {
278 bool ret = signals_received_.empty();
279 if (!ret) {
280 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
281 << ", expected none";
282 }
283 return ret;
284 }
285
286 static std::vector<int> signals_received_;
287 static Thread *signaled_thread_;
288
289 scoped_ptr<PhysicalSocketServer> ss_;
290};
291
292std::vector<int> PosixSignalDeliveryTest::signals_received_;
293Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
294
295// Test receiving a synchronous signal while not in Wait() and then entering
296// Wait() afterwards.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000297TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
299 raise(SIGTERM);
300 EXPECT_TRUE(ss_->Wait(0, true));
301 EXPECT_TRUE(ExpectSignal(SIGTERM));
302 EXPECT_TRUE(ExpectNone());
303}
304
305// Test that we can handle getting tons of repeated signals and that we see all
306// the different ones.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000307TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
309 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
310 for (int i = 0; i < 10000; ++i) {
311 raise(SIGTERM);
312 }
313 raise(SIGINT);
314 EXPECT_TRUE(ss_->Wait(0, true));
315 // Order will be lowest signal numbers first.
316 EXPECT_TRUE(ExpectSignal(SIGINT));
317 EXPECT_TRUE(ExpectSignal(SIGTERM));
318 EXPECT_TRUE(ExpectNone());
319}
320
321// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000322TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
324 alarm(1);
325 EXPECT_TRUE(ss_->Wait(1500, true));
326 EXPECT_TRUE(ExpectSignal(SIGALRM));
327 EXPECT_TRUE(ExpectNone());
328}
329
330class RaiseSigTermRunnable : public Runnable {
331 void Run(Thread *thread) {
332 thread->socketserver()->Wait(1000, false);
333
334 // Allow SIGTERM. This will be the only thread with it not masked so it will
335 // be delivered to us.
336 sigset_t mask;
337 sigemptyset(&mask);
338 pthread_sigmask(SIG_SETMASK, &mask, NULL);
339
340 // Raise it.
341 raise(SIGTERM);
342 }
343};
344
345// Test that it works no matter what thread the kernel chooses to give the
346// signal to (since it's not guaranteed to be the one that Wait() runs on).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000347TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000348 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
349 // Mask out SIGTERM so that it can't be delivered to this thread.
350 sigset_t mask;
351 sigemptyset(&mask);
352 sigaddset(&mask, SIGTERM);
353 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
354 // Start a new thread that raises it. It will have to be delivered to that
355 // thread. Our implementation should safely handle it and dispatch
356 // RecordSignal() on this thread.
357 scoped_ptr<Thread> thread(new Thread());
358 scoped_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
359 thread->Start(runnable.get());
360 EXPECT_TRUE(ss_->Wait(1500, true));
361 EXPECT_TRUE(ExpectSignal(SIGTERM));
362 EXPECT_EQ(Thread::Current(), signaled_thread_);
363 EXPECT_TRUE(ExpectNone());
364}
365
366#endif
367
368} // namespace rtc