blob: df750adbef678a5290089615a4ddfcf774c406ab [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"
21
22namespace rtc {
23
24class PhysicalSocketTest : public SocketTest {
25};
26
27TEST_F(PhysicalSocketTest, TestConnectIPv4) {
28 SocketTest::TestConnectIPv4();
29}
30
31TEST_F(PhysicalSocketTest, TestConnectIPv6) {
32 SocketTest::TestConnectIPv6();
33}
34
35TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
36 SocketTest::TestConnectWithDnsLookupIPv4();
37}
38
39TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
40 SocketTest::TestConnectWithDnsLookupIPv6();
41}
42
43TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
44 SocketTest::TestConnectFailIPv4();
45}
46
47TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
48 SocketTest::TestConnectFailIPv6();
49}
50
51TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
52 SocketTest::TestConnectWithDnsLookupFailIPv4();
53}
54
55
56TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
57 SocketTest::TestConnectWithDnsLookupFailIPv6();
58}
59
60
61TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
62 SocketTest::TestConnectWithClosedSocketIPv4();
63}
64
65TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
66 SocketTest::TestConnectWithClosedSocketIPv6();
67}
68
69TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
70 SocketTest::TestConnectWhileNotClosedIPv4();
71}
72
73TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
74 SocketTest::TestConnectWhileNotClosedIPv6();
75}
76
77TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
78 SocketTest::TestServerCloseDuringConnectIPv4();
79}
80
81TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
82 SocketTest::TestServerCloseDuringConnectIPv6();
83}
84
85TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
86 SocketTest::TestClientCloseDuringConnectIPv4();
87}
88
89TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
90 SocketTest::TestClientCloseDuringConnectIPv6();
91}
92
93TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
94 SocketTest::TestServerCloseIPv4();
95}
96
97TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
98 SocketTest::TestServerCloseIPv6();
99}
100
101TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
102 SocketTest::TestCloseInClosedCallbackIPv4();
103}
104
105TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
106 SocketTest::TestCloseInClosedCallbackIPv6();
107}
108
109TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
110 SocketTest::TestSocketServerWaitIPv4();
111}
112
113TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
114 SocketTest::TestSocketServerWaitIPv6();
115}
116
117TEST_F(PhysicalSocketTest, TestTcpIPv4) {
118 SocketTest::TestTcpIPv4();
119}
120
121TEST_F(PhysicalSocketTest, TestTcpIPv6) {
122 SocketTest::TestTcpIPv6();
123}
124
125TEST_F(PhysicalSocketTest, TestUdpIPv4) {
126 SocketTest::TestUdpIPv4();
127}
128
129TEST_F(PhysicalSocketTest, TestUdpIPv6) {
130 SocketTest::TestUdpIPv6();
131}
132
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000133// Disable for TSan v2, see
134// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
135#if !defined(THREAD_SANITIZER)
136
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv4) {
138 SocketTest::TestUdpReadyToSendIPv4();
139}
140
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000141#endif // if !defined(THREAD_SANITIZER)
142
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) {
144 SocketTest::TestUdpReadyToSendIPv6();
145}
146
147TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
148 SocketTest::TestGetSetOptionsIPv4();
149}
150
151TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
152 SocketTest::TestGetSetOptionsIPv6();
153}
154
155#if defined(WEBRTC_POSIX)
156
157class PosixSignalDeliveryTest : public testing::Test {
158 public:
159 static void RecordSignal(int signum) {
160 signals_received_.push_back(signum);
161 signaled_thread_ = Thread::Current();
162 }
163
164 protected:
165 void SetUp() {
166 ss_.reset(new PhysicalSocketServer());
167 }
168
169 void TearDown() {
170 ss_.reset(NULL);
171 signals_received_.clear();
172 signaled_thread_ = NULL;
173 }
174
175 bool ExpectSignal(int signum) {
176 if (signals_received_.empty()) {
177 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
178 return false;
179 }
180 if (signals_received_[0] != signum) {
181 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
182 signals_received_[0] << ", expected " << signum;
183 return false;
184 }
185 signals_received_.erase(signals_received_.begin());
186 return true;
187 }
188
189 bool ExpectNone() {
190 bool ret = signals_received_.empty();
191 if (!ret) {
192 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
193 << ", expected none";
194 }
195 return ret;
196 }
197
198 static std::vector<int> signals_received_;
199 static Thread *signaled_thread_;
200
201 scoped_ptr<PhysicalSocketServer> ss_;
202};
203
204std::vector<int> PosixSignalDeliveryTest::signals_received_;
205Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
206
207// Test receiving a synchronous signal while not in Wait() and then entering
208// Wait() afterwards.
209TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
210 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
211 raise(SIGTERM);
212 EXPECT_TRUE(ss_->Wait(0, true));
213 EXPECT_TRUE(ExpectSignal(SIGTERM));
214 EXPECT_TRUE(ExpectNone());
215}
216
217// Test that we can handle getting tons of repeated signals and that we see all
218// the different ones.
219TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
220 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
221 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
222 for (int i = 0; i < 10000; ++i) {
223 raise(SIGTERM);
224 }
225 raise(SIGINT);
226 EXPECT_TRUE(ss_->Wait(0, true));
227 // Order will be lowest signal numbers first.
228 EXPECT_TRUE(ExpectSignal(SIGINT));
229 EXPECT_TRUE(ExpectSignal(SIGTERM));
230 EXPECT_TRUE(ExpectNone());
231}
232
233// Test that a signal during a Wait() call is detected.
234TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
235 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
236 alarm(1);
237 EXPECT_TRUE(ss_->Wait(1500, true));
238 EXPECT_TRUE(ExpectSignal(SIGALRM));
239 EXPECT_TRUE(ExpectNone());
240}
241
242class RaiseSigTermRunnable : public Runnable {
243 void Run(Thread *thread) {
244 thread->socketserver()->Wait(1000, false);
245
246 // Allow SIGTERM. This will be the only thread with it not masked so it will
247 // be delivered to us.
248 sigset_t mask;
249 sigemptyset(&mask);
250 pthread_sigmask(SIG_SETMASK, &mask, NULL);
251
252 // Raise it.
253 raise(SIGTERM);
254 }
255};
256
257// Test that it works no matter what thread the kernel chooses to give the
258// signal to (since it's not guaranteed to be the one that Wait() runs on).
259TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
260 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
261 // Mask out SIGTERM so that it can't be delivered to this thread.
262 sigset_t mask;
263 sigemptyset(&mask);
264 sigaddset(&mask, SIGTERM);
265 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
266 // Start a new thread that raises it. It will have to be delivered to that
267 // thread. Our implementation should safely handle it and dispatch
268 // RecordSignal() on this thread.
269 scoped_ptr<Thread> thread(new Thread());
270 scoped_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
271 thread->Start(runnable.get());
272 EXPECT_TRUE(ss_->Wait(1500, true));
273 EXPECT_TRUE(ExpectSignal(SIGTERM));
274 EXPECT_EQ(Thread::Current(), signaled_thread_);
275 EXPECT_TRUE(ExpectNone());
276}
277
278#endif
279
280} // namespace rtc