3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
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"
40 class PhysicalSocketTest : public SocketTest {
43 TEST_F(PhysicalSocketTest, TestConnectIPv4) {
44 SocketTest::TestConnectIPv4();
47 TEST_F(PhysicalSocketTest, TestConnectIPv6) {
48 SocketTest::TestConnectIPv6();
51 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
52 SocketTest::TestConnectWithDnsLookupIPv4();
55 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
56 SocketTest::TestConnectWithDnsLookupIPv6();
59 TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
60 SocketTest::TestConnectFailIPv4();
63 TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
64 SocketTest::TestConnectFailIPv6();
67 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
68 SocketTest::TestConnectWithDnsLookupFailIPv4();
72 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
73 SocketTest::TestConnectWithDnsLookupFailIPv6();
78 // This test crashes the OS X kernel on 10.6 (at bsd/netinet/tcp_subr.c:2118).
79 TEST_F(PhysicalSocketTest, DISABLED_TestConnectWithClosedSocketIPv4) {
81 TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
83 SocketTest::TestConnectWithClosedSocketIPv4();
87 // This test crashes the OS X kernel on 10.6 (at bsd/netinet/tcp_subr.c:2118).
88 TEST_F(PhysicalSocketTest, DISABLED_TestConnectWithClosedSocketIPv6) {
90 TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
92 SocketTest::TestConnectWithClosedSocketIPv6();
95 TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
96 SocketTest::TestConnectWhileNotClosedIPv4();
99 TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
100 SocketTest::TestConnectWhileNotClosedIPv6();
103 TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
104 SocketTest::TestServerCloseDuringConnectIPv4();
107 TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
108 SocketTest::TestServerCloseDuringConnectIPv6();
111 TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
112 SocketTest::TestClientCloseDuringConnectIPv4();
115 TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
116 SocketTest::TestClientCloseDuringConnectIPv6();
119 TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
120 SocketTest::TestServerCloseIPv4();
123 TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
124 SocketTest::TestServerCloseIPv6();
127 TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
128 SocketTest::TestCloseInClosedCallbackIPv4();
131 TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
132 SocketTest::TestCloseInClosedCallbackIPv6();
135 TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
136 SocketTest::TestSocketServerWaitIPv4();
139 TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
140 SocketTest::TestSocketServerWaitIPv6();
143 TEST_F(PhysicalSocketTest, TestTcpIPv4) {
144 SocketTest::TestTcpIPv4();
147 TEST_F(PhysicalSocketTest, TestTcpIPv6) {
148 SocketTest::TestTcpIPv6();
151 TEST_F(PhysicalSocketTest, TestUdpIPv4) {
152 SocketTest::TestUdpIPv4();
155 TEST_F(PhysicalSocketTest, TestUdpIPv6) {
156 SocketTest::TestUdpIPv6();
159 TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv4) {
160 SocketTest::TestUdpReadyToSendIPv4();
163 TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) {
164 SocketTest::TestUdpReadyToSendIPv6();
167 TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
168 SocketTest::TestGetSetOptionsIPv4();
171 TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
172 SocketTest::TestGetSetOptionsIPv6();
177 class PosixSignalDeliveryTest : public testing::Test {
179 static void RecordSignal(int signum) {
180 signals_received_.push_back(signum);
181 signaled_thread_ = Thread::Current();
186 ss_.reset(new PhysicalSocketServer());
191 signals_received_.clear();
192 signaled_thread_ = NULL;
195 bool ExpectSignal(int signum) {
196 if (signals_received_.empty()) {
197 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
200 if (signals_received_[0] != signum) {
201 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
202 signals_received_[0] << ", expected " << signum;
205 signals_received_.erase(signals_received_.begin());
210 bool ret = signals_received_.empty();
212 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
213 << ", expected none";
218 static std::vector<int> signals_received_;
219 static Thread *signaled_thread_;
221 scoped_ptr<PhysicalSocketServer> ss_;
224 std::vector<int> PosixSignalDeliveryTest::signals_received_;
225 Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
227 // Test receiving a synchronous signal while not in Wait() and then entering
228 // Wait() afterwards.
229 TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
230 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
232 EXPECT_TRUE(ss_->Wait(0, true));
233 EXPECT_TRUE(ExpectSignal(SIGTERM));
234 EXPECT_TRUE(ExpectNone());
237 // Test that we can handle getting tons of repeated signals and that we see all
238 // the different ones.
239 TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
240 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
241 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
242 for (int i = 0; i < 10000; ++i) {
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());
253 // Test that a signal during a Wait() call is detected.
254 TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
255 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
257 EXPECT_TRUE(ss_->Wait(1500, true));
258 EXPECT_TRUE(ExpectSignal(SIGALRM));
259 EXPECT_TRUE(ExpectNone());
262 class RaiseSigTermRunnable : public Runnable {
263 void Run(Thread *thread) {
264 thread->socketserver()->Wait(1000, false);
266 // Allow SIGTERM. This will be the only thread with it not masked so it will
267 // be delivered to us.
270 pthread_sigmask(SIG_SETMASK, &mask, NULL);
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).
279 TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
280 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
281 // Mask out SIGTERM so that it can't be delivered to this thread.
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 scoped_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
291 thread->Start(runnable.get());
292 EXPECT_TRUE(ss_->Wait(1500, true));
293 EXPECT_TRUE(ExpectSignal(SIGTERM));
294 EXPECT_EQ(Thread::Current(), signaled_thread_);
295 EXPECT_TRUE(ExpectNone());
300 } // namespace talk_base