blob: b069baaeae8b2f3939b0e50c5b98ca14c4234036 [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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include <signal.h>
13#include <stdarg.h>
14
15#include "webrtc/base/gunit.h"
16#include "webrtc/base/logging.h"
17#include "webrtc/base/physicalsocketserver.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/socket_unittest.h"
19#include "webrtc/base/testutils.h"
20#include "webrtc/base/thread.h"
21
22namespace rtc {
23
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070024#define MAYBE_SKIP_IPV6 \
25 if (!HasIPv6Enabled()) { \
26 LOG(LS_INFO) << "No IPv6... skipping"; \
27 return; \
28 }
29
jbauch095ae152015-12-18 01:39:55 -080030class PhysicalSocketTest;
31
32class FakeSocketDispatcher : public SocketDispatcher {
33 public:
34 explicit FakeSocketDispatcher(PhysicalSocketServer* ss)
35 : SocketDispatcher(ss) {
36 }
37
jbauchf2a2bf42016-02-03 16:45:32 -080038 FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss)
39 : SocketDispatcher(s, ss) {
40 }
41
jbauch095ae152015-12-18 01:39:55 -080042 protected:
43 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override;
jbauchf2a2bf42016-02-03 16:45:32 -080044 int DoSend(SOCKET socket, const char* buf, int len, int flags) override;
45 int DoSendTo(SOCKET socket, const char* buf, int len, int flags,
46 const struct sockaddr* dest_addr, socklen_t addrlen) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047};
48
jbauch095ae152015-12-18 01:39:55 -080049class FakePhysicalSocketServer : public PhysicalSocketServer {
50 public:
51 explicit FakePhysicalSocketServer(PhysicalSocketTest* test)
52 : test_(test) {
53 }
54
55 AsyncSocket* CreateAsyncSocket(int type) override {
56 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
jbauchf2a2bf42016-02-03 16:45:32 -080057 if (!dispatcher->Create(type)) {
jbauch095ae152015-12-18 01:39:55 -080058 delete dispatcher;
59 return nullptr;
60 }
jbauchf2a2bf42016-02-03 16:45:32 -080061 return dispatcher;
jbauch095ae152015-12-18 01:39:55 -080062 }
63
64 AsyncSocket* CreateAsyncSocket(int family, int type) override {
65 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
jbauchf2a2bf42016-02-03 16:45:32 -080066 if (!dispatcher->Create(family, type)) {
jbauch095ae152015-12-18 01:39:55 -080067 delete dispatcher;
68 return nullptr;
69 }
jbauchf2a2bf42016-02-03 16:45:32 -080070 return dispatcher;
71 }
72
73 AsyncSocket* WrapSocket(SOCKET s) override {
74 SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this);
75 if (!dispatcher->Initialize()) {
76 delete dispatcher;
77 return nullptr;
78 }
79 return dispatcher;
jbauch095ae152015-12-18 01:39:55 -080080 }
81
82 PhysicalSocketTest* GetTest() const { return test_; }
83
84 private:
85 PhysicalSocketTest* test_;
86};
87
88class PhysicalSocketTest : public SocketTest {
89 public:
90 // Set flag to simluate failures when calling "::accept" on a AsyncSocket.
91 void SetFailAccept(bool fail) { fail_accept_ = fail; }
92 bool FailAccept() const { return fail_accept_; }
93
jbauchf2a2bf42016-02-03 16:45:32 -080094 // Maximum size to ::send to a socket. Set to < 0 to disable limiting.
95 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; }
96 int MaxSendSize() const { return max_send_size_; }
97
jbauch095ae152015-12-18 01:39:55 -080098 protected:
99 PhysicalSocketTest()
100 : server_(new FakePhysicalSocketServer(this)),
101 scope_(server_.get()),
jbauchf2a2bf42016-02-03 16:45:32 -0800102 fail_accept_(false),
103 max_send_size_(-1) {
jbauch095ae152015-12-18 01:39:55 -0800104 }
105
106 void ConnectInternalAcceptError(const IPAddress& loopback);
jbauchf2a2bf42016-02-03 16:45:32 -0800107 void WritableAfterPartialWrite(const IPAddress& loopback);
jbauch095ae152015-12-18 01:39:55 -0800108
jbauch555604a2016-04-26 03:13:22 -0700109 std::unique_ptr<FakePhysicalSocketServer> server_;
jbauch095ae152015-12-18 01:39:55 -0800110 SocketServerScope scope_;
111 bool fail_accept_;
jbauchf2a2bf42016-02-03 16:45:32 -0800112 int max_send_size_;
jbauch095ae152015-12-18 01:39:55 -0800113};
114
115SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
116 sockaddr* addr,
117 socklen_t* addrlen) {
118 FakePhysicalSocketServer* ss =
119 static_cast<FakePhysicalSocketServer*>(socketserver());
120 if (ss->GetTest()->FailAccept()) {
121 return INVALID_SOCKET;
122 }
123
124 return SocketDispatcher::DoAccept(socket, addr, addrlen);
125}
126
jbauchf2a2bf42016-02-03 16:45:32 -0800127int FakeSocketDispatcher::DoSend(SOCKET socket, const char* buf, int len,
128 int flags) {
129 FakePhysicalSocketServer* ss =
130 static_cast<FakePhysicalSocketServer*>(socketserver());
131 if (ss->GetTest()->MaxSendSize() >= 0) {
132 len = std::min(len, ss->GetTest()->MaxSendSize());
133 }
134
135 return SocketDispatcher::DoSend(socket, buf, len, flags);
136}
137
138int FakeSocketDispatcher::DoSendTo(SOCKET socket, const char* buf, int len,
139 int flags, const struct sockaddr* dest_addr, socklen_t addrlen) {
140 FakePhysicalSocketServer* ss =
141 static_cast<FakePhysicalSocketServer*>(socketserver());
142 if (ss->GetTest()->MaxSendSize() >= 0) {
143 len = std::min(len, ss->GetTest()->MaxSendSize());
144 }
145
146 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
147 addrlen);
148}
149
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150TEST_F(PhysicalSocketTest, TestConnectIPv4) {
151 SocketTest::TestConnectIPv4();
152}
153
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700154TEST_F(PhysicalSocketTest, TestConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155 SocketTest::TestConnectIPv6();
156}
157
158TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
159 SocketTest::TestConnectWithDnsLookupIPv4();
160}
161
162TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
163 SocketTest::TestConnectWithDnsLookupIPv6();
164}
165
166TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
167 SocketTest::TestConnectFailIPv4();
168}
169
jbauch095ae152015-12-18 01:39:55 -0800170void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) {
171 testing::StreamSink sink;
172 SocketAddress accept_addr;
173
174 // Create two clients.
jbauch555604a2016-04-26 03:13:22 -0700175 std::unique_ptr<AsyncSocket> client1(
176 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800177 sink.Monitor(client1.get());
178 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState());
179 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr());
180
jbauch555604a2016-04-26 03:13:22 -0700181 std::unique_ptr<AsyncSocket> client2(
182 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
jbauch095ae152015-12-18 01:39:55 -0800183 sink.Monitor(client2.get());
184 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState());
185 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr());
186
187 // Create server and listen.
jbauch555604a2016-04-26 03:13:22 -0700188 std::unique_ptr<AsyncSocket> server(
jbauch095ae152015-12-18 01:39:55 -0800189 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
190 sink.Monitor(server.get());
191 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
192 EXPECT_EQ(0, server->Listen(5));
193 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState());
194
195 // Ensure no pending server connections, since we haven't done anything yet.
196 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
197 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
198 EXPECT_TRUE(accept_addr.IsNil());
199
200 // Attempt first connect to listening socket.
201 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress()));
202 EXPECT_FALSE(client1->GetLocalAddress().IsNil());
203 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress());
204
205 // Client is connecting, outcome not yet determined.
206 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState());
207 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_OPEN));
208 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_CLOSE));
209
210 // Server has pending connection, try to accept it (will fail).
211 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
212 // Simulate "::accept" returning an error.
213 SetFailAccept(true);
jbauch555604a2016-04-26 03:13:22 -0700214 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800215 EXPECT_FALSE(accepted);
216 ASSERT_TRUE(accept_addr.IsNil());
217
218 // Ensure no more pending server connections.
219 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
220 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
221 EXPECT_TRUE(accept_addr.IsNil());
222
223 // Attempt second connect to listening socket.
224 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress()));
225 EXPECT_FALSE(client2->GetLocalAddress().IsNil());
226 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress());
227
228 // Client is connecting, outcome not yet determined.
229 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState());
230 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_OPEN));
231 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_CLOSE));
232
233 // Server has pending connection, try to accept it (will succeed).
234 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
235 SetFailAccept(false);
jbauch555604a2016-04-26 03:13:22 -0700236 std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr));
jbauch095ae152015-12-18 01:39:55 -0800237 ASSERT_TRUE(accepted2);
238 EXPECT_FALSE(accept_addr.IsNil());
239 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr);
240}
241
242TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) {
243 ConnectInternalAcceptError(kIPv4Loopback);
244}
245
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700246TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) {
247 MAYBE_SKIP_IPV6;
jbauch095ae152015-12-18 01:39:55 -0800248 ConnectInternalAcceptError(kIPv6Loopback);
249}
250
jbauchf2a2bf42016-02-03 16:45:32 -0800251void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
252 // Simulate a really small maximum send size.
253 const int kMaxSendSize = 128;
254 SetMaxSendSize(kMaxSendSize);
255
256 // Run the default send/receive socket tests with a smaller amount of data
257 // to avoid long running times due to the small maximum send size.
258 const size_t kDataSize = 128 * 1024;
259 TcpInternal(loopback, kDataSize, kMaxSendSize);
260}
261
262TEST_F(PhysicalSocketTest, TestWritableAfterPartialWriteIPv4) {
263 WritableAfterPartialWrite(kIPv4Loopback);
264}
265
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700266TEST_F(PhysicalSocketTest, TestWritableAfterPartialWriteIPv6) {
267 MAYBE_SKIP_IPV6;
jbauchf2a2bf42016-02-03 16:45:32 -0800268 WritableAfterPartialWrite(kIPv6Loopback);
269}
270
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700271TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 SocketTest::TestConnectFailIPv6();
273}
274
275TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
276 SocketTest::TestConnectWithDnsLookupFailIPv4();
277}
278
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700279TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280 SocketTest::TestConnectWithDnsLookupFailIPv6();
281}
282
283
284TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
285 SocketTest::TestConnectWithClosedSocketIPv4();
286}
287
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700288TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 SocketTest::TestConnectWithClosedSocketIPv6();
290}
291
292TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
293 SocketTest::TestConnectWhileNotClosedIPv4();
294}
295
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700296TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 SocketTest::TestConnectWhileNotClosedIPv6();
298}
299
300TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
301 SocketTest::TestServerCloseDuringConnectIPv4();
302}
303
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700304TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305 SocketTest::TestServerCloseDuringConnectIPv6();
306}
307
308TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
309 SocketTest::TestClientCloseDuringConnectIPv4();
310}
311
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700312TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000313 SocketTest::TestClientCloseDuringConnectIPv6();
314}
315
316TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
317 SocketTest::TestServerCloseIPv4();
318}
319
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700320TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321 SocketTest::TestServerCloseIPv6();
322}
323
324TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
325 SocketTest::TestCloseInClosedCallbackIPv4();
326}
327
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700328TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000329 SocketTest::TestCloseInClosedCallbackIPv6();
330}
331
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000332TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333 SocketTest::TestSocketServerWaitIPv4();
334}
335
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700336TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000337 SocketTest::TestSocketServerWaitIPv6();
338}
339
340TEST_F(PhysicalSocketTest, TestTcpIPv4) {
341 SocketTest::TestTcpIPv4();
342}
343
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700344TEST_F(PhysicalSocketTest, TestTcpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345 SocketTest::TestTcpIPv6();
346}
347
348TEST_F(PhysicalSocketTest, TestUdpIPv4) {
349 SocketTest::TestUdpIPv4();
350}
351
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700352TEST_F(PhysicalSocketTest, TestUdpIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000353 SocketTest::TestUdpIPv6();
354}
355
henrike@webrtc.org6f833c32014-06-27 16:21:49 +0000356// Disable for TSan v2, see
357// https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
deadbeefc97be6a2015-09-25 11:00:38 -0700358// Also disable for MSan, see:
359// https://code.google.com/p/webrtc/issues/detail?id=4958
360// TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
minyuedf200d12015-10-17 13:10:46 -0700361// Also disable for ASan.
Henrik Kjellander0be3e042015-10-30 21:21:03 +0100362// Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
ivocf399f212015-11-19 06:44:32 -0800363// Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
minyuedf200d12015-10-17 13:10:46 -0700364#if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
ivocf399f212015-11-19 06:44:32 -0800365 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
366 defined(WEBRTC_LINUX)
minyuedf200d12015-10-17 13:10:46 -0700367#define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
368#else
369#define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
370#endif
371TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000372 SocketTest::TestUdpReadyToSendIPv4();
373}
374
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000375TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000376 SocketTest::TestUdpReadyToSendIPv6();
377}
378
379TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
380 SocketTest::TestGetSetOptionsIPv4();
381}
382
383TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
384 SocketTest::TestGetSetOptionsIPv6();
385}
386
387#if defined(WEBRTC_POSIX)
388
389class PosixSignalDeliveryTest : public testing::Test {
390 public:
391 static void RecordSignal(int signum) {
392 signals_received_.push_back(signum);
393 signaled_thread_ = Thread::Current();
394 }
395
396 protected:
397 void SetUp() {
398 ss_.reset(new PhysicalSocketServer());
399 }
400
401 void TearDown() {
402 ss_.reset(NULL);
403 signals_received_.clear();
404 signaled_thread_ = NULL;
405 }
406
407 bool ExpectSignal(int signum) {
408 if (signals_received_.empty()) {
409 LOG(LS_ERROR) << "ExpectSignal(): No signal received";
410 return false;
411 }
412 if (signals_received_[0] != signum) {
413 LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
414 signals_received_[0] << ", expected " << signum;
415 return false;
416 }
417 signals_received_.erase(signals_received_.begin());
418 return true;
419 }
420
421 bool ExpectNone() {
422 bool ret = signals_received_.empty();
423 if (!ret) {
424 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
425 << ", expected none";
426 }
427 return ret;
428 }
429
430 static std::vector<int> signals_received_;
431 static Thread *signaled_thread_;
432
jbauch555604a2016-04-26 03:13:22 -0700433 std::unique_ptr<PhysicalSocketServer> ss_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434};
435
436std::vector<int> PosixSignalDeliveryTest::signals_received_;
437Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
438
439// Test receiving a synchronous signal while not in Wait() and then entering
440// Wait() afterwards.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000441TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000442 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal));
443 raise(SIGTERM);
444 EXPECT_TRUE(ss_->Wait(0, true));
445 EXPECT_TRUE(ExpectSignal(SIGTERM));
446 EXPECT_TRUE(ExpectNone());
447}
448
449// Test that we can handle getting tons of repeated signals and that we see all
450// the different ones.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000451TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000452 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
453 ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
454 for (int i = 0; i < 10000; ++i) {
455 raise(SIGTERM);
456 }
457 raise(SIGINT);
458 EXPECT_TRUE(ss_->Wait(0, true));
459 // Order will be lowest signal numbers first.
460 EXPECT_TRUE(ExpectSignal(SIGINT));
461 EXPECT_TRUE(ExpectSignal(SIGTERM));
462 EXPECT_TRUE(ExpectNone());
463}
464
465// Test that a signal during a Wait() call is detected.
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000466TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000467 ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
468 alarm(1);
469 EXPECT_TRUE(ss_->Wait(1500, true));
470 EXPECT_TRUE(ExpectSignal(SIGALRM));
471 EXPECT_TRUE(ExpectNone());
472}
473
474class RaiseSigTermRunnable : public Runnable {
475 void Run(Thread *thread) {
476 thread->socketserver()->Wait(1000, false);
477
478 // Allow SIGTERM. This will be the only thread with it not masked so it will
479 // be delivered to us.
480 sigset_t mask;
481 sigemptyset(&mask);
482 pthread_sigmask(SIG_SETMASK, &mask, NULL);
483
484 // Raise it.
485 raise(SIGTERM);
486 }
487};
488
489// Test that it works no matter what thread the kernel chooses to give the
490// signal to (since it's not guaranteed to be the one that Wait() runs on).
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000491TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000492 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
493 // Mask out SIGTERM so that it can't be delivered to this thread.
494 sigset_t mask;
495 sigemptyset(&mask);
496 sigaddset(&mask, SIGTERM);
497 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
498 // Start a new thread that raises it. It will have to be delivered to that
499 // thread. Our implementation should safely handle it and dispatch
500 // RecordSignal() on this thread.
jbauch555604a2016-04-26 03:13:22 -0700501 std::unique_ptr<Thread> thread(new Thread());
502 std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000503 thread->Start(runnable.get());
504 EXPECT_TRUE(ss_->Wait(1500, true));
505 EXPECT_TRUE(ExpectSignal(SIGTERM));
506 EXPECT_EQ(Thread::Current(), signaled_thread_);
507 EXPECT_TRUE(ExpectNone());
508}
509
510#endif
511
512} // namespace rtc