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