henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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.org | fded02c | 2014-09-19 13:10:10 +0000 | [diff] [blame] | 21 | #include "webrtc/test/testsupport/gtest_disable.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
| 25 | class PhysicalSocketTest : public SocketTest { |
| 26 | }; |
| 27 | |
| 28 | TEST_F(PhysicalSocketTest, TestConnectIPv4) { |
| 29 | SocketTest::TestConnectIPv4(); |
| 30 | } |
| 31 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 32 | // 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 |
| 38 | TEST_F(PhysicalSocketTest, MAYBE_TestConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | SocketTest::TestConnectIPv6(); |
| 40 | } |
| 41 | |
| 42 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) { |
| 43 | SocketTest::TestConnectWithDnsLookupIPv4(); |
| 44 | } |
| 45 | |
| 46 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) { |
| 47 | SocketTest::TestConnectWithDnsLookupIPv6(); |
| 48 | } |
| 49 | |
| 50 | TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { |
| 51 | SocketTest::TestConnectFailIPv4(); |
| 52 | } |
| 53 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 54 | // 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 |
| 60 | TEST_F(PhysicalSocketTest, MAYBE_TestConnectFailIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | SocketTest::TestConnectFailIPv6(); |
| 62 | } |
| 63 | |
| 64 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) { |
| 65 | SocketTest::TestConnectWithDnsLookupFailIPv4(); |
| 66 | } |
| 67 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 68 | // 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 |
| 76 | TEST_F(PhysicalSocketTest, MAYBE_TestConnectWithDnsLookupFailIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | SocketTest::TestConnectWithDnsLookupFailIPv6(); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) { |
| 82 | SocketTest::TestConnectWithClosedSocketIPv4(); |
| 83 | } |
| 84 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 85 | // 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 |
| 92 | TEST_F(PhysicalSocketTest, MAYBE_TestConnectWithClosedSocketIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | SocketTest::TestConnectWithClosedSocketIPv6(); |
| 94 | } |
| 95 | |
| 96 | TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) { |
| 97 | SocketTest::TestConnectWhileNotClosedIPv4(); |
| 98 | } |
| 99 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 100 | // 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 |
| 107 | TEST_F(PhysicalSocketTest, MAYBE_TestConnectWhileNotClosedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | SocketTest::TestConnectWhileNotClosedIPv6(); |
| 109 | } |
| 110 | |
| 111 | TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) { |
| 112 | SocketTest::TestServerCloseDuringConnectIPv4(); |
| 113 | } |
| 114 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 115 | // 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 |
| 122 | TEST_F(PhysicalSocketTest, MAYBE_TestServerCloseDuringConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | SocketTest::TestServerCloseDuringConnectIPv6(); |
| 124 | } |
| 125 | |
| 126 | TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) { |
| 127 | SocketTest::TestClientCloseDuringConnectIPv4(); |
| 128 | } |
| 129 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 130 | // 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 |
| 137 | TEST_F(PhysicalSocketTest, MAYBE_TestClientCloseDuringConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 138 | SocketTest::TestClientCloseDuringConnectIPv6(); |
| 139 | } |
| 140 | |
| 141 | TEST_F(PhysicalSocketTest, TestServerCloseIPv4) { |
| 142 | SocketTest::TestServerCloseIPv4(); |
| 143 | } |
| 144 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 145 | // 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 |
| 151 | TEST_F(PhysicalSocketTest, MAYBE_TestServerCloseIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | SocketTest::TestServerCloseIPv6(); |
| 153 | } |
| 154 | |
| 155 | TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) { |
| 156 | SocketTest::TestCloseInClosedCallbackIPv4(); |
| 157 | } |
| 158 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 159 | // 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 |
| 166 | TEST_F(PhysicalSocketTest, MAYBE_TestCloseInClosedCallbackIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 167 | SocketTest::TestCloseInClosedCallbackIPv6(); |
| 168 | } |
| 169 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 170 | TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | SocketTest::TestSocketServerWaitIPv4(); |
| 172 | } |
| 173 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 174 | // 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 |
| 180 | TEST_F(PhysicalSocketTest, MAYBE_TestSocketServerWaitIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 181 | SocketTest::TestSocketServerWaitIPv6(); |
| 182 | } |
| 183 | |
| 184 | TEST_F(PhysicalSocketTest, TestTcpIPv4) { |
| 185 | SocketTest::TestTcpIPv4(); |
| 186 | } |
| 187 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 188 | // 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 |
| 194 | TEST_F(PhysicalSocketTest, MAYBE_TestTcpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 195 | SocketTest::TestTcpIPv6(); |
| 196 | } |
| 197 | |
| 198 | TEST_F(PhysicalSocketTest, TestUdpIPv4) { |
| 199 | SocketTest::TestUdpIPv4(); |
| 200 | } |
| 201 | |
minyue | 5d69648 | 2015-08-19 04:42:03 -0700 | [diff] [blame] | 202 | // 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 |
| 208 | TEST_F(PhysicalSocketTest, MAYBE_TestUdpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 209 | SocketTest::TestUdpIPv6(); |
| 210 | } |
| 211 | |
henrike@webrtc.org | 6f833c3 | 2014-06-27 16:21:49 +0000 | [diff] [blame] | 212 | // Disable for TSan v2, see |
| 213 | // https://code.google.com/p/webrtc/issues/detail?id=3498 for details. |
deadbeef | c97be6a | 2015-09-25 11:00:38 -0700 | [diff] [blame] | 214 | // Also disable for MSan, see: |
| 215 | // https://code.google.com/p/webrtc/issues/detail?id=4958 |
| 216 | // TODO(deadbeef): Enable again once test is reimplemented to be unflaky. |
minyue | df200d1 | 2015-10-17 13:10:46 -0700 | [diff] [blame] | 217 | // Also disable for ASan. |
Henrik Kjellander | 0be3e04 | 2015-10-30 21:21:03 +0100 | [diff] [blame] | 218 | // Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364 |
ivoc | f399f21 | 2015-11-19 06:44:32 -0800 | [diff] [blame^] | 219 | // Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233 |
minyue | df200d1 | 2015-10-17 13:10:46 -0700 | [diff] [blame] | 220 | #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
ivoc | f399f21 | 2015-11-19 06:44:32 -0800 | [diff] [blame^] | 221 | defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \ |
| 222 | defined(WEBRTC_LINUX) |
minyue | df200d1 | 2015-10-17 13:10:46 -0700 | [diff] [blame] | 223 | #define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4 |
| 224 | #else |
| 225 | #define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4 |
| 226 | #endif |
| 227 | TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 228 | SocketTest::TestUdpReadyToSendIPv4(); |
| 229 | } |
| 230 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 231 | TEST_F(PhysicalSocketTest, TestUdpReadyToSendIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | SocketTest::TestUdpReadyToSendIPv6(); |
| 233 | } |
| 234 | |
| 235 | TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) { |
| 236 | SocketTest::TestGetSetOptionsIPv4(); |
| 237 | } |
| 238 | |
| 239 | TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) { |
| 240 | SocketTest::TestGetSetOptionsIPv6(); |
| 241 | } |
| 242 | |
| 243 | #if defined(WEBRTC_POSIX) |
| 244 | |
| 245 | class PosixSignalDeliveryTest : public testing::Test { |
| 246 | public: |
| 247 | static void RecordSignal(int signum) { |
| 248 | signals_received_.push_back(signum); |
| 249 | signaled_thread_ = Thread::Current(); |
| 250 | } |
| 251 | |
| 252 | protected: |
| 253 | void SetUp() { |
| 254 | ss_.reset(new PhysicalSocketServer()); |
| 255 | } |
| 256 | |
| 257 | void TearDown() { |
| 258 | ss_.reset(NULL); |
| 259 | signals_received_.clear(); |
| 260 | signaled_thread_ = NULL; |
| 261 | } |
| 262 | |
| 263 | bool ExpectSignal(int signum) { |
| 264 | if (signals_received_.empty()) { |
| 265 | LOG(LS_ERROR) << "ExpectSignal(): No signal received"; |
| 266 | return false; |
| 267 | } |
| 268 | if (signals_received_[0] != signum) { |
| 269 | LOG(LS_ERROR) << "ExpectSignal(): Received signal " << |
| 270 | signals_received_[0] << ", expected " << signum; |
| 271 | return false; |
| 272 | } |
| 273 | signals_received_.erase(signals_received_.begin()); |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | bool ExpectNone() { |
| 278 | bool ret = signals_received_.empty(); |
| 279 | if (!ret) { |
| 280 | LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0] |
| 281 | << ", expected none"; |
| 282 | } |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static std::vector<int> signals_received_; |
| 287 | static Thread *signaled_thread_; |
| 288 | |
| 289 | scoped_ptr<PhysicalSocketServer> ss_; |
| 290 | }; |
| 291 | |
| 292 | std::vector<int> PosixSignalDeliveryTest::signals_received_; |
| 293 | Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL; |
| 294 | |
| 295 | // Test receiving a synchronous signal while not in Wait() and then entering |
| 296 | // Wait() afterwards. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 297 | TEST_F(PosixSignalDeliveryTest, RaiseThenWait) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 298 | ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal)); |
| 299 | raise(SIGTERM); |
| 300 | EXPECT_TRUE(ss_->Wait(0, true)); |
| 301 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 302 | EXPECT_TRUE(ExpectNone()); |
| 303 | } |
| 304 | |
| 305 | // Test that we can handle getting tons of repeated signals and that we see all |
| 306 | // the different ones. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 307 | TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 308 | ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
| 309 | ss_->SetPosixSignalHandler(SIGINT, &RecordSignal); |
| 310 | for (int i = 0; i < 10000; ++i) { |
| 311 | raise(SIGTERM); |
| 312 | } |
| 313 | raise(SIGINT); |
| 314 | EXPECT_TRUE(ss_->Wait(0, true)); |
| 315 | // Order will be lowest signal numbers first. |
| 316 | EXPECT_TRUE(ExpectSignal(SIGINT)); |
| 317 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 318 | EXPECT_TRUE(ExpectNone()); |
| 319 | } |
| 320 | |
| 321 | // Test that a signal during a Wait() call is detected. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 322 | TEST_F(PosixSignalDeliveryTest, SignalDuringWait) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 323 | ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal); |
| 324 | alarm(1); |
| 325 | EXPECT_TRUE(ss_->Wait(1500, true)); |
| 326 | EXPECT_TRUE(ExpectSignal(SIGALRM)); |
| 327 | EXPECT_TRUE(ExpectNone()); |
| 328 | } |
| 329 | |
| 330 | class RaiseSigTermRunnable : public Runnable { |
| 331 | void Run(Thread *thread) { |
| 332 | thread->socketserver()->Wait(1000, false); |
| 333 | |
| 334 | // Allow SIGTERM. This will be the only thread with it not masked so it will |
| 335 | // be delivered to us. |
| 336 | sigset_t mask; |
| 337 | sigemptyset(&mask); |
| 338 | pthread_sigmask(SIG_SETMASK, &mask, NULL); |
| 339 | |
| 340 | // Raise it. |
| 341 | raise(SIGTERM); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | // Test that it works no matter what thread the kernel chooses to give the |
| 346 | // signal to (since it's not guaranteed to be the one that Wait() runs on). |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 347 | TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 348 | ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
| 349 | // Mask out SIGTERM so that it can't be delivered to this thread. |
| 350 | sigset_t mask; |
| 351 | sigemptyset(&mask); |
| 352 | sigaddset(&mask, SIGTERM); |
| 353 | EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL)); |
| 354 | // Start a new thread that raises it. It will have to be delivered to that |
| 355 | // thread. Our implementation should safely handle it and dispatch |
| 356 | // RecordSignal() on this thread. |
| 357 | scoped_ptr<Thread> thread(new Thread()); |
| 358 | scoped_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable()); |
| 359 | thread->Start(runnable.get()); |
| 360 | EXPECT_TRUE(ss_->Wait(1500, true)); |
| 361 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 362 | EXPECT_EQ(Thread::Current(), signaled_thread_); |
| 363 | EXPECT_TRUE(ExpectNone()); |
| 364 | } |
| 365 | |
| 366 | #endif |
| 367 | |
| 368 | } // namespace rtc |