blob: b7f68481a70ddbd707bd060caa4633a87e75a1cf [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <signal.h>
29#include <stdarg.h>
30
31#include "talk/base/gunit.h"
32#include "talk/base/logging.h"
33#include "talk/base/physicalsocketserver.h"
34#include "talk/base/scoped_ptr.h"
35#include "talk/base/socket_unittest.h"
36#include "talk/base/thread.h"
37
38namespace talk_base {
39
40class PhysicalSocketTest : public SocketTest {
41};
42
43TEST_F(PhysicalSocketTest, TestConnectIPv4) {
44 SocketTest::TestConnectIPv4();
45}
46
47TEST_F(PhysicalSocketTest, TestConnectIPv6) {
48 SocketTest::TestConnectIPv6();
49}
50
51TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
52 SocketTest::TestConnectWithDnsLookupIPv4();
53}
54
55TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
56 SocketTest::TestConnectWithDnsLookupIPv6();
57}
58
59TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
60 SocketTest::TestConnectFailIPv4();
61}
62
63TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
64 SocketTest::TestConnectFailIPv6();
65}
66
67TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
68 SocketTest::TestConnectWithDnsLookupFailIPv4();
69}
70
71
72TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
73 SocketTest::TestConnectWithDnsLookupFailIPv6();
74}
75
76
77#ifdef OSX
78// This test crashes the OS X kernel on 10.6 (at bsd/netinet/tcp_subr.c:2118).
79TEST_F(PhysicalSocketTest, DISABLED_TestConnectWithClosedSocketIPv4) {
80#else
81TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
82#endif
83 SocketTest::TestConnectWithClosedSocketIPv4();
84}
85
86#ifdef OSX
87// This test crashes the OS X kernel on 10.6 (at bsd/netinet/tcp_subr.c:2118).
88TEST_F(PhysicalSocketTest, DISABLED_TestConnectWithClosedSocketIPv6) {
89#else
90TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
91#endif
92 SocketTest::TestConnectWithClosedSocketIPv6();
93}
94
95TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
96 SocketTest::TestConnectWhileNotClosedIPv4();
97}
98
99TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
100 SocketTest::TestConnectWhileNotClosedIPv6();
101}
102
103TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
104 SocketTest::TestServerCloseDuringConnectIPv4();
105}
106
107TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
108 SocketTest::TestServerCloseDuringConnectIPv6();
109}
110
111TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
112 SocketTest::TestClientCloseDuringConnectIPv4();
113}
114
115TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
116 SocketTest::TestClientCloseDuringConnectIPv6();
117}
118
119TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
120 SocketTest::TestServerCloseIPv4();
121}
122
123TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
124 SocketTest::TestServerCloseIPv6();
125}
126
127TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
128 SocketTest::TestCloseInClosedCallbackIPv4();
129}
130
131TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
132 SocketTest::TestCloseInClosedCallbackIPv6();
133}
134
135TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
136 SocketTest::TestSocketServerWaitIPv4();
137}
138
139TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
140 SocketTest::TestSocketServerWaitIPv6();
141}
142
143TEST_F(PhysicalSocketTest, TestTcpIPv4) {
144 SocketTest::TestTcpIPv4();
145}
146
147TEST_F(PhysicalSocketTest, TestTcpIPv6) {
148 SocketTest::TestTcpIPv6();
149}
150
151TEST_F(PhysicalSocketTest, TestUdpIPv4) {
152 SocketTest::TestUdpIPv4();
153}
154
155TEST_F(PhysicalSocketTest, TestUdpIPv6) {
156 SocketTest::TestUdpIPv6();
157}
158
159TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv4) {
160 SocketTest::TestUdpReadyToSendIPv4();
161}
162
163TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) {
164 SocketTest::TestUdpReadyToSendIPv6();
165}
166
167TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
168 SocketTest::TestGetSetOptionsIPv4();
169}
170
171TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
172 SocketTest::TestGetSetOptionsIPv6();
173}
174
175#ifdef POSIX
176
177class PosixSignalDeliveryTest : public testing::Test {
178 public:
179 static void RecordSignal(int signum) {
180 signals_received_.push_back(signum);
181 signaled_thread_ = Thread::Current();
182 }
183
184 protected:
185 void SetUp() {
186 ss_.reset(new PhysicalSocketServer());
187 }
188
189 void TearDown() {
190 ss_.reset(NULL);
191 signals_received_.clear();
192 signaled_thread_ = NULL;
193 }
194
195 bool ExpectSignal(int signum) {
196 if (signals_received_.empty()) {
197 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
198 return false;
199 }
200 if (signals_received_[0] != signum) {
201 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
202 signals_received_[0] << ", expected " << signum;
203 return false;
204 }
205 signals_received_.erase(signals_received_.begin());
206 return true;
207 }
208
209 bool ExpectNone() {
210 bool ret = signals_received_.empty();
211 if (!ret) {
212 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
213 << ", expected none";
214 }
215 return ret;
216 }
217
218 static std::vector<int> signals_received_;
219 static Thread *signaled_thread_;
220
221 scoped_ptr<PhysicalSocketServer> ss_;
222};
223
224std::vector<int> PosixSignalDeliveryTest::signals_received_;
225Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
226
227// Test receiving a synchronous signal while not in Wait() and then entering
228// Wait() afterwards.
229TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
230 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
231 raise(SIGTERM);
232 EXPECT_TRUE(ss_->Wait(0, true));
233 EXPECT_TRUE(ExpectSignal(SIGTERM));
234 EXPECT_TRUE(ExpectNone());
235}
236
237// Test that we can handle getting tons of repeated signals and that we see all
238// the different ones.
239TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
240 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
241 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
242 for (int i = 0; i < 10000; ++i) {
243 raise(SIGTERM);
244 }
245 raise(SIGINT);
246 EXPECT_TRUE(ss_->Wait(0, true));
247 // Order will be lowest signal numbers first.
248 EXPECT_TRUE(ExpectSignal(SIGINT));
249 EXPECT_TRUE(ExpectSignal(SIGTERM));
250 EXPECT_TRUE(ExpectNone());
251}
252
253// Test that a signal during a Wait() call is detected.
254TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
255 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
256 alarm(1);
257 EXPECT_TRUE(ss_->Wait(1500, true));
258 EXPECT_TRUE(ExpectSignal(SIGALRM));
259 EXPECT_TRUE(ExpectNone());
260}
261
262class RaiseSigTermRunnable : public Runnable {
263 void Run(Thread *thread) {
264 thread->socketserver()->Wait(1000, false);
265
266 // Allow SIGTERM. This will be the only thread with it not masked so it will
267 // be delivered to us.
268 sigset_t mask;
269 sigemptyset(&mask);
270 pthread_sigmask(SIG_SETMASK, &mask, NULL);
271
272 // Raise it.
273 raise(SIGTERM);
274 }
275};
276
277// Test that it works no matter what thread the kernel chooses to give the
278// signal to (since it's not guaranteed to be the one that Wait() runs on).
279TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
280 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
281 // Mask out SIGTERM so that it can't be delivered to this thread.
282 sigset_t mask;
283 sigemptyset(&mask);
284 sigaddset(&mask, SIGTERM);
285 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
286 // Start a new thread that raises it. It will have to be delivered to that
287 // thread. Our implementation should safely handle it and dispatch
288 // RecordSignal() on this thread.
289 scoped_ptr<Thread> thread(new Thread());
290 thread->Start(new RaiseSigTermRunnable());
291 EXPECT_TRUE(ss_->Wait(1500, true));
292 EXPECT_TRUE(ExpectSignal(SIGTERM));
293 EXPECT_EQ(Thread::Current(), signaled_thread_);
294 EXPECT_TRUE(ExpectNone());
295}
296
297#endif
298
299} // namespace talk_base