blob: 6c7be9f63c624c04e9734fd9f8410fbf8c473932 [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.
218#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
219 defined(ADDRESS_SANITIZER)
220#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
221#else
222#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
223#endif
224TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225 SocketTest::TestUdpReadyToSendIPv4();
226}
227
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000228TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229 SocketTest::TestUdpReadyToSendIPv6();
230}
231
232TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
233 SocketTest::TestGetSetOptionsIPv4();
234}
235
236TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
237 SocketTest::TestGetSetOptionsIPv6();
238}
239
240#if defined(WEBRTC_POSIX)
241
242class PosixSignalDeliveryTest : public testing::Test {
243 public:
244 static void RecordSignal(int signum) {
245 signals_received_.push_back(signum);
246 signaled_thread_ = Thread::Current();
247 }
248
249 protected:
250 void SetUp() {
251 ss_.reset(new PhysicalSocketServer());
252 }
253
254 void TearDown() {
255 ss_.reset(NULL);
256 signals_received_.clear();
257 signaled_thread_ = NULL;
258 }
259
260 bool ExpectSignal(int signum) {
261 if (signals_received_.empty()) {
262 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
263 return false;
264 }
265 if (signals_received_[0] != signum) {
266 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
267 signals_received_[0] << ", expected " << signum;
268 return false;
269 }
270 signals_received_.erase(signals_received_.begin());
271 return true;
272 }
273
274 bool ExpectNone() {
275 bool ret = signals_received_.empty();
276 if (!ret) {
277 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
278 << ", expected none";
279 }
280 return ret;
281 }
282
283 static std::vector<int> signals_received_;
284 static Thread *signaled_thread_;
285
286 scoped_ptr<PhysicalSocketServer> ss_;
287};
288
289std::vector<int> PosixSignalDeliveryTest::signals_received_;
290Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
291
292// Test receiving a synchronous signal while not in Wait() and then entering
293// Wait() afterwards.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000294TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
296 raise(SIGTERM);
297 EXPECT_TRUE(ss_->Wait(0, true));
298 EXPECT_TRUE(ExpectSignal(SIGTERM));
299 EXPECT_TRUE(ExpectNone());
300}
301
302// Test that we can handle getting tons of repeated signals and that we see all
303// the different ones.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000304TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
306 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
307 for (int i = 0; i < 10000; ++i) {
308 raise(SIGTERM);
309 }
310 raise(SIGINT);
311 EXPECT_TRUE(ss_->Wait(0, true));
312 // Order will be lowest signal numbers first.
313 EXPECT_TRUE(ExpectSignal(SIGINT));
314 EXPECT_TRUE(ExpectSignal(SIGTERM));
315 EXPECT_TRUE(ExpectNone());
316}
317
318// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000319TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000320 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
321 alarm(1);
322 EXPECT_TRUE(ss_->Wait(1500, true));
323 EXPECT_TRUE(ExpectSignal(SIGALRM));
324 EXPECT_TRUE(ExpectNone());
325}
326
327class RaiseSigTermRunnable : public Runnable {
328 void Run(Thread *thread) {
329 thread->socketserver()->Wait(1000, false);
330
331 // Allow SIGTERM. This will be the only thread with it not masked so it will
332 // be delivered to us.
333 sigset_t mask;
334 sigemptyset(&mask);
335 pthread_sigmask(SIG_SETMASK, &mask, NULL);
336
337 // Raise it.
338 raise(SIGTERM);
339 }
340};
341
342// Test that it works no matter what thread the kernel chooses to give the
343// signal to (since it's not guaranteed to be the one that Wait() runs on).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000344TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
346 // Mask out SIGTERM so that it can't be delivered to this thread.
347 sigset_t mask;
348 sigemptyset(&mask);
349 sigaddset(&mask, SIGTERM);
350 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
351 // Start a new thread that raises it. It will have to be delivered to that
352 // thread. Our implementation should safely handle it and dispatch
353 // RecordSignal() on this thread.
354 scoped_ptr<Thread> thread(new Thread());
355 scoped_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
356 thread->Start(runnable.get());
357 EXPECT_TRUE(ss_->Wait(1500, true));
358 EXPECT_TRUE(ExpectSignal(SIGTERM));
359 EXPECT_EQ(Thread::Current(), signaled_thread_);
360 EXPECT_TRUE(ExpectNone());
361}
362
363#endif
364
365} // namespace rtc