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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | #include <signal.h> |
| 13 | #include <stdarg.h> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/gunit.h" |
| 16 | #include "rtc_base/logging.h" |
| 17 | #include "rtc_base/networkmonitor.h" |
| 18 | #include "rtc_base/physicalsocketserver.h" |
| 19 | #include "rtc_base/socket_unittest.h" |
| 20 | #include "rtc_base/testutils.h" |
| 21 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 25 | #define MAYBE_SKIP_IPV4 \ |
| 26 | if (!HasIPv4Enabled()) { \ |
| 27 | RTC_LOG(LS_INFO) << "No IPv4... skipping"; \ |
| 28 | return; \ |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 31 | #define MAYBE_SKIP_IPV6 \ |
| 32 | if (!HasIPv6Enabled()) { \ |
| 33 | RTC_LOG(LS_INFO) << "No IPv6... skipping"; \ |
| 34 | return; \ |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 35 | } |
| 36 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 37 | class PhysicalSocketTest; |
| 38 | |
| 39 | class FakeSocketDispatcher : public SocketDispatcher { |
| 40 | public: |
| 41 | explicit FakeSocketDispatcher(PhysicalSocketServer* ss) |
| 42 | : SocketDispatcher(ss) { |
| 43 | } |
| 44 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 45 | FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss) |
| 46 | : SocketDispatcher(s, ss) { |
| 47 | } |
| 48 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 49 | protected: |
| 50 | SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 51 | int DoSend(SOCKET socket, const char* buf, int len, int flags) override; |
| 52 | int DoSendTo(SOCKET socket, const char* buf, int len, int flags, |
| 53 | const struct sockaddr* dest_addr, socklen_t addrlen) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 56 | class FakePhysicalSocketServer : public PhysicalSocketServer { |
| 57 | public: |
| 58 | explicit FakePhysicalSocketServer(PhysicalSocketTest* test) |
| 59 | : test_(test) { |
| 60 | } |
| 61 | |
| 62 | AsyncSocket* CreateAsyncSocket(int type) override { |
| 63 | SocketDispatcher* dispatcher = new FakeSocketDispatcher(this); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 64 | if (!dispatcher->Create(type)) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 65 | delete dispatcher; |
| 66 | return nullptr; |
| 67 | } |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 68 | return dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | AsyncSocket* CreateAsyncSocket(int family, int type) override { |
| 72 | SocketDispatcher* dispatcher = new FakeSocketDispatcher(this); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 73 | if (!dispatcher->Create(family, type)) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 74 | delete dispatcher; |
| 75 | return nullptr; |
| 76 | } |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 77 | return dispatcher; |
| 78 | } |
| 79 | |
| 80 | AsyncSocket* WrapSocket(SOCKET s) override { |
| 81 | SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this); |
| 82 | if (!dispatcher->Initialize()) { |
| 83 | delete dispatcher; |
| 84 | return nullptr; |
| 85 | } |
| 86 | return dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | PhysicalSocketTest* GetTest() const { return test_; } |
| 90 | |
| 91 | private: |
| 92 | PhysicalSocketTest* test_; |
| 93 | }; |
| 94 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 95 | class FakeNetworkBinder : public NetworkBinderInterface { |
| 96 | public: |
| 97 | NetworkBindingResult BindSocketToNetwork(int, const IPAddress&) override { |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 98 | ++num_binds_; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 99 | return result_; |
| 100 | } |
| 101 | |
| 102 | void set_result(NetworkBindingResult result) { result_ = result; } |
| 103 | |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 104 | int num_binds() { return num_binds_; } |
| 105 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 106 | private: |
| 107 | NetworkBindingResult result_ = NetworkBindingResult::SUCCESS; |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 108 | int num_binds_ = 0; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 111 | class PhysicalSocketTest : public SocketTest { |
| 112 | public: |
| 113 | // Set flag to simluate failures when calling "::accept" on a AsyncSocket. |
| 114 | void SetFailAccept(bool fail) { fail_accept_ = fail; } |
| 115 | bool FailAccept() const { return fail_accept_; } |
| 116 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 117 | // Maximum size to ::send to a socket. Set to < 0 to disable limiting. |
| 118 | void SetMaxSendSize(int max_size) { max_send_size_ = max_size; } |
| 119 | int MaxSendSize() const { return max_send_size_; } |
| 120 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 121 | protected: |
| 122 | PhysicalSocketTest() |
| 123 | : server_(new FakePhysicalSocketServer(this)), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 124 | thread_(server_.get()), |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 125 | fail_accept_(false), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 126 | max_send_size_(-1) {} |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 127 | |
| 128 | void ConnectInternalAcceptError(const IPAddress& loopback); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 129 | void WritableAfterPartialWrite(const IPAddress& loopback); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 130 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 131 | std::unique_ptr<FakePhysicalSocketServer> server_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 132 | rtc::AutoSocketServerThread thread_; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 133 | bool fail_accept_; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 134 | int max_send_size_; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket, |
| 138 | sockaddr* addr, |
| 139 | socklen_t* addrlen) { |
| 140 | FakePhysicalSocketServer* ss = |
| 141 | static_cast<FakePhysicalSocketServer*>(socketserver()); |
| 142 | if (ss->GetTest()->FailAccept()) { |
| 143 | return INVALID_SOCKET; |
| 144 | } |
| 145 | |
| 146 | return SocketDispatcher::DoAccept(socket, addr, addrlen); |
| 147 | } |
| 148 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 149 | int FakeSocketDispatcher::DoSend(SOCKET socket, const char* buf, int len, |
| 150 | int flags) { |
| 151 | FakePhysicalSocketServer* ss = |
| 152 | static_cast<FakePhysicalSocketServer*>(socketserver()); |
| 153 | if (ss->GetTest()->MaxSendSize() >= 0) { |
| 154 | len = std::min(len, ss->GetTest()->MaxSendSize()); |
| 155 | } |
| 156 | |
| 157 | return SocketDispatcher::DoSend(socket, buf, len, flags); |
| 158 | } |
| 159 | |
| 160 | int FakeSocketDispatcher::DoSendTo(SOCKET socket, const char* buf, int len, |
| 161 | int flags, const struct sockaddr* dest_addr, socklen_t addrlen) { |
| 162 | FakePhysicalSocketServer* ss = |
| 163 | static_cast<FakePhysicalSocketServer*>(socketserver()); |
| 164 | if (ss->GetTest()->MaxSendSize() >= 0) { |
| 165 | len = std::min(len, ss->GetTest()->MaxSendSize()); |
| 166 | } |
| 167 | |
| 168 | return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr, |
| 169 | addrlen); |
| 170 | } |
| 171 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | TEST_F(PhysicalSocketTest, TestConnectIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 173 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 174 | SocketTest::TestConnectIPv4(); |
| 175 | } |
| 176 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 177 | TEST_F(PhysicalSocketTest, TestConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | SocketTest::TestConnectIPv6(); |
| 179 | } |
| 180 | |
| 181 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 182 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | SocketTest::TestConnectWithDnsLookupIPv4(); |
| 184 | } |
| 185 | |
| 186 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) { |
| 187 | SocketTest::TestConnectWithDnsLookupIPv6(); |
| 188 | } |
| 189 | |
| 190 | TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 191 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | SocketTest::TestConnectFailIPv4(); |
| 193 | } |
| 194 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 195 | void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 196 | webrtc::testing::StreamSink sink; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 197 | SocketAddress accept_addr; |
| 198 | |
| 199 | // Create two clients. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 200 | std::unique_ptr<AsyncSocket> client1( |
| 201 | server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 202 | sink.Monitor(client1.get()); |
| 203 | EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState()); |
| 204 | EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr()); |
| 205 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 206 | std::unique_ptr<AsyncSocket> client2( |
| 207 | server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 208 | sink.Monitor(client2.get()); |
| 209 | EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState()); |
| 210 | EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr()); |
| 211 | |
| 212 | // Create server and listen. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 213 | std::unique_ptr<AsyncSocket> server( |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 214 | server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
| 215 | sink.Monitor(server.get()); |
| 216 | EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); |
| 217 | EXPECT_EQ(0, server->Listen(5)); |
| 218 | EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState()); |
| 219 | |
| 220 | // Ensure no pending server connections, since we haven't done anything yet. |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 221 | EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 222 | EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
| 223 | EXPECT_TRUE(accept_addr.IsNil()); |
| 224 | |
| 225 | // Attempt first connect to listening socket. |
| 226 | EXPECT_EQ(0, client1->Connect(server->GetLocalAddress())); |
| 227 | EXPECT_FALSE(client1->GetLocalAddress().IsNil()); |
| 228 | EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress()); |
| 229 | |
| 230 | // Client is connecting, outcome not yet determined. |
| 231 | EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState()); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 232 | EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_OPEN)); |
| 233 | EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_CLOSE)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 234 | |
| 235 | // Server has pending connection, try to accept it (will fail). |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 236 | EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)), |
| 237 | kTimeout); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 238 | // Simulate "::accept" returning an error. |
| 239 | SetFailAccept(true); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 240 | std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 241 | EXPECT_FALSE(accepted); |
| 242 | ASSERT_TRUE(accept_addr.IsNil()); |
| 243 | |
| 244 | // Ensure no more pending server connections. |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 245 | EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 246 | EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
| 247 | EXPECT_TRUE(accept_addr.IsNil()); |
| 248 | |
| 249 | // Attempt second connect to listening socket. |
| 250 | EXPECT_EQ(0, client2->Connect(server->GetLocalAddress())); |
| 251 | EXPECT_FALSE(client2->GetLocalAddress().IsNil()); |
| 252 | EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress()); |
| 253 | |
| 254 | // Client is connecting, outcome not yet determined. |
| 255 | EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState()); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 256 | EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_OPEN)); |
| 257 | EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_CLOSE)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 258 | |
| 259 | // Server has pending connection, try to accept it (will succeed). |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 260 | EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)), |
| 261 | kTimeout); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 262 | SetFailAccept(false); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 263 | std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 264 | ASSERT_TRUE(accepted2); |
| 265 | EXPECT_FALSE(accept_addr.IsNil()); |
| 266 | EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr); |
| 267 | } |
| 268 | |
| 269 | TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 270 | MAYBE_SKIP_IPV4; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 271 | ConnectInternalAcceptError(kIPv4Loopback); |
| 272 | } |
| 273 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 274 | TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) { |
| 275 | MAYBE_SKIP_IPV6; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 276 | ConnectInternalAcceptError(kIPv6Loopback); |
| 277 | } |
| 278 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 279 | void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) { |
| 280 | // Simulate a really small maximum send size. |
| 281 | const int kMaxSendSize = 128; |
| 282 | SetMaxSendSize(kMaxSendSize); |
| 283 | |
| 284 | // Run the default send/receive socket tests with a smaller amount of data |
| 285 | // to avoid long running times due to the small maximum send size. |
| 286 | const size_t kDataSize = 128 * 1024; |
| 287 | TcpInternal(loopback, kDataSize, kMaxSendSize); |
| 288 | } |
| 289 | |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 290 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167 |
| 291 | #if defined(WEBRTC_WIN) |
| 292 | #define MAYBE_TestWritableAfterPartialWriteIPv4 DISABLED_TestWritableAfterPartialWriteIPv4 |
| 293 | #else |
| 294 | #define MAYBE_TestWritableAfterPartialWriteIPv4 TestWritableAfterPartialWriteIPv4 |
| 295 | #endif |
| 296 | TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 297 | MAYBE_SKIP_IPV4; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 298 | WritableAfterPartialWrite(kIPv4Loopback); |
| 299 | } |
| 300 | |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 301 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167 |
| 302 | #if defined(WEBRTC_WIN) |
| 303 | #define MAYBE_TestWritableAfterPartialWriteIPv6 DISABLED_TestWritableAfterPartialWriteIPv6 |
| 304 | #else |
| 305 | #define MAYBE_TestWritableAfterPartialWriteIPv6 TestWritableAfterPartialWriteIPv6 |
| 306 | #endif |
| 307 | TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) { |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 308 | MAYBE_SKIP_IPV6; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 309 | WritableAfterPartialWrite(kIPv6Loopback); |
| 310 | } |
| 311 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 312 | TEST_F(PhysicalSocketTest, TestConnectFailIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | SocketTest::TestConnectFailIPv6(); |
| 314 | } |
| 315 | |
| 316 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 317 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 318 | SocketTest::TestConnectWithDnsLookupFailIPv4(); |
| 319 | } |
| 320 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 321 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 322 | SocketTest::TestConnectWithDnsLookupFailIPv6(); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 327 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 328 | SocketTest::TestConnectWithClosedSocketIPv4(); |
| 329 | } |
| 330 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 331 | TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 332 | SocketTest::TestConnectWithClosedSocketIPv6(); |
| 333 | } |
| 334 | |
| 335 | TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 336 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 337 | SocketTest::TestConnectWhileNotClosedIPv4(); |
| 338 | } |
| 339 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 340 | TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 341 | SocketTest::TestConnectWhileNotClosedIPv6(); |
| 342 | } |
| 343 | |
| 344 | TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 345 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 346 | SocketTest::TestServerCloseDuringConnectIPv4(); |
| 347 | } |
| 348 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 349 | TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 350 | SocketTest::TestServerCloseDuringConnectIPv6(); |
| 351 | } |
| 352 | |
| 353 | TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 354 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 355 | SocketTest::TestClientCloseDuringConnectIPv4(); |
| 356 | } |
| 357 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 358 | TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 359 | SocketTest::TestClientCloseDuringConnectIPv6(); |
| 360 | } |
| 361 | |
| 362 | TEST_F(PhysicalSocketTest, TestServerCloseIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 363 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 364 | SocketTest::TestServerCloseIPv4(); |
| 365 | } |
| 366 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 367 | TEST_F(PhysicalSocketTest, TestServerCloseIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 368 | SocketTest::TestServerCloseIPv6(); |
| 369 | } |
| 370 | |
| 371 | TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 372 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 373 | SocketTest::TestCloseInClosedCallbackIPv4(); |
| 374 | } |
| 375 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 376 | TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 377 | SocketTest::TestCloseInClosedCallbackIPv6(); |
| 378 | } |
| 379 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 380 | TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 381 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 382 | SocketTest::TestSocketServerWaitIPv4(); |
| 383 | } |
| 384 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 385 | TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 386 | SocketTest::TestSocketServerWaitIPv6(); |
| 387 | } |
| 388 | |
| 389 | TEST_F(PhysicalSocketTest, TestTcpIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 390 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 391 | SocketTest::TestTcpIPv4(); |
| 392 | } |
| 393 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 394 | TEST_F(PhysicalSocketTest, TestTcpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 395 | SocketTest::TestTcpIPv6(); |
| 396 | } |
| 397 | |
| 398 | TEST_F(PhysicalSocketTest, TestUdpIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 399 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 400 | SocketTest::TestUdpIPv4(); |
| 401 | } |
| 402 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 403 | TEST_F(PhysicalSocketTest, TestUdpIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 404 | SocketTest::TestUdpIPv6(); |
| 405 | } |
| 406 | |
henrike@webrtc.org | 6f833c3 | 2014-06-27 16:21:49 +0000 | [diff] [blame] | 407 | // Disable for TSan v2, see |
| 408 | // https://code.google.com/p/webrtc/issues/detail?id=3498 for details. |
deadbeef | c97be6a | 2015-09-25 11:00:38 -0700 | [diff] [blame] | 409 | // Also disable for MSan, see: |
| 410 | // https://code.google.com/p/webrtc/issues/detail?id=4958 |
| 411 | // TODO(deadbeef): Enable again once test is reimplemented to be unflaky. |
minyue | df200d1 | 2015-10-17 13:10:46 -0700 | [diff] [blame] | 412 | // Also disable for ASan. |
Henrik Kjellander | 0be3e04 | 2015-10-30 21:21:03 +0100 | [diff] [blame] | 413 | // Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364 |
ivoc | f399f21 | 2015-11-19 06:44:32 -0800 | [diff] [blame] | 414 | // Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233 |
minyue | df200d1 | 2015-10-17 13:10:46 -0700 | [diff] [blame] | 415 | #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
ivoc | f399f21 | 2015-11-19 06:44:32 -0800 | [diff] [blame] | 416 | defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \ |
| 417 | defined(WEBRTC_LINUX) |
minyue | df200d1 | 2015-10-17 13:10:46 -0700 | [diff] [blame] | 418 | #define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4 |
| 419 | #else |
| 420 | #define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4 |
| 421 | #endif |
| 422 | TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 423 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 424 | SocketTest::TestUdpReadyToSendIPv4(); |
| 425 | } |
| 426 | |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 427 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167 |
| 428 | #if defined(WEBRTC_WIN) |
| 429 | #define MAYBE_TestUdpReadyToSendIPv6 DISABLED_TestUdpReadyToSendIPv6 |
| 430 | #else |
| 431 | #define MAYBE_TestUdpReadyToSendIPv6 TestUdpReadyToSendIPv6 |
| 432 | #endif |
| 433 | TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 434 | SocketTest::TestUdpReadyToSendIPv6(); |
| 435 | } |
| 436 | |
| 437 | TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 438 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 439 | SocketTest::TestGetSetOptionsIPv4(); |
| 440 | } |
| 441 | |
| 442 | TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) { |
| 443 | SocketTest::TestGetSetOptionsIPv6(); |
| 444 | } |
| 445 | |
| 446 | #if defined(WEBRTC_POSIX) |
| 447 | |
Taylor Brandstetter | 6f82535 | 2016-08-11 15:38:28 -0700 | [diff] [blame] | 448 | // We don't get recv timestamps on Mac. |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 449 | #if !defined(WEBRTC_MAC) |
| 450 | TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 451 | MAYBE_SKIP_IPV4; |
Taylor Brandstetter | 6f82535 | 2016-08-11 15:38:28 -0700 | [diff] [blame] | 452 | SocketTest::TestSocketRecvTimestampIPv4(); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Taylor Brandstetter | 6f82535 | 2016-08-11 15:38:28 -0700 | [diff] [blame] | 455 | TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) { |
| 456 | SocketTest::TestSocketRecvTimestampIPv6(); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 457 | } |
| 458 | #endif |
| 459 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 460 | // Verify that if the socket was unable to be bound to a real network interface |
| 461 | // (not loopback), Bind will return an error. |
| 462 | TEST_F(PhysicalSocketTest, |
| 463 | BindFailsIfNetworkBinderFailsForNonLoopbackInterface) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 464 | MAYBE_SKIP_IPV4; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 465 | FakeNetworkBinder fake_network_binder; |
| 466 | server_->set_network_binder(&fake_network_binder); |
| 467 | std::unique_ptr<AsyncSocket> socket( |
| 468 | server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 469 | fake_network_binder.set_result(NetworkBindingResult::FAILURE); |
| 470 | EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0))); |
| 471 | server_->set_network_binder(nullptr); |
| 472 | } |
| 473 | |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 474 | // Network binder shouldn't be used if the socket is bound to the "any" IP. |
| 475 | TEST_F(PhysicalSocketTest, |
| 476 | NetworkBinderIsNotUsedForAnyIp) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 477 | MAYBE_SKIP_IPV4; |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 478 | FakeNetworkBinder fake_network_binder; |
| 479 | server_->set_network_binder(&fake_network_binder); |
| 480 | std::unique_ptr<AsyncSocket> socket( |
| 481 | server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 482 | EXPECT_EQ(0, socket->Bind(SocketAddress("0.0.0.0", 0))); |
| 483 | EXPECT_EQ(0, fake_network_binder.num_binds()); |
| 484 | server_->set_network_binder(nullptr); |
| 485 | } |
| 486 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 487 | // For a loopback interface, failures to bind to the interface should be |
| 488 | // tolerated. |
| 489 | TEST_F(PhysicalSocketTest, |
| 490 | BindSucceedsIfNetworkBinderFailsForLoopbackInterface) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 491 | MAYBE_SKIP_IPV4; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 492 | FakeNetworkBinder fake_network_binder; |
| 493 | server_->set_network_binder(&fake_network_binder); |
| 494 | std::unique_ptr<AsyncSocket> socket( |
| 495 | server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 496 | fake_network_binder.set_result(NetworkBindingResult::FAILURE); |
| 497 | EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0))); |
| 498 | server_->set_network_binder(nullptr); |
| 499 | } |
| 500 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 501 | class PosixSignalDeliveryTest : public testing::Test { |
| 502 | public: |
| 503 | static void RecordSignal(int signum) { |
| 504 | signals_received_.push_back(signum); |
| 505 | signaled_thread_ = Thread::Current(); |
| 506 | } |
| 507 | |
| 508 | protected: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 509 | void SetUp() override { ss_.reset(new PhysicalSocketServer()); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 510 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 511 | void TearDown() override { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 512 | ss_.reset(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 513 | signals_received_.clear(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 514 | signaled_thread_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | bool ExpectSignal(int signum) { |
| 518 | if (signals_received_.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 519 | RTC_LOG(LS_ERROR) << "ExpectSignal(): No signal received"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 520 | return false; |
| 521 | } |
| 522 | if (signals_received_[0] != signum) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 523 | RTC_LOG(LS_ERROR) << "ExpectSignal(): Received signal " |
| 524 | << signals_received_[0] << ", expected " << signum; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | signals_received_.erase(signals_received_.begin()); |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | bool ExpectNone() { |
| 532 | bool ret = signals_received_.empty(); |
| 533 | if (!ret) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 534 | RTC_LOG(LS_ERROR) << "ExpectNone(): Received signal " |
| 535 | << signals_received_[0] << ", expected none"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 536 | } |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static std::vector<int> signals_received_; |
| 541 | static Thread *signaled_thread_; |
| 542 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 543 | std::unique_ptr<PhysicalSocketServer> ss_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | std::vector<int> PosixSignalDeliveryTest::signals_received_; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 547 | Thread* PosixSignalDeliveryTest::signaled_thread_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 548 | |
| 549 | // Test receiving a synchronous signal while not in Wait() and then entering |
| 550 | // Wait() afterwards. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 551 | TEST_F(PosixSignalDeliveryTest, RaiseThenWait) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 552 | ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal)); |
| 553 | raise(SIGTERM); |
| 554 | EXPECT_TRUE(ss_->Wait(0, true)); |
| 555 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 556 | EXPECT_TRUE(ExpectNone()); |
| 557 | } |
| 558 | |
| 559 | // Test that we can handle getting tons of repeated signals and that we see all |
| 560 | // the different ones. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 561 | TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 562 | ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
| 563 | ss_->SetPosixSignalHandler(SIGINT, &RecordSignal); |
| 564 | for (int i = 0; i < 10000; ++i) { |
| 565 | raise(SIGTERM); |
| 566 | } |
| 567 | raise(SIGINT); |
| 568 | EXPECT_TRUE(ss_->Wait(0, true)); |
| 569 | // Order will be lowest signal numbers first. |
| 570 | EXPECT_TRUE(ExpectSignal(SIGINT)); |
| 571 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 572 | EXPECT_TRUE(ExpectNone()); |
| 573 | } |
| 574 | |
| 575 | // Test that a signal during a Wait() call is detected. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 576 | TEST_F(PosixSignalDeliveryTest, SignalDuringWait) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 577 | ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal); |
| 578 | alarm(1); |
| 579 | EXPECT_TRUE(ss_->Wait(1500, true)); |
| 580 | EXPECT_TRUE(ExpectSignal(SIGALRM)); |
| 581 | EXPECT_TRUE(ExpectNone()); |
| 582 | } |
| 583 | |
| 584 | class RaiseSigTermRunnable : public Runnable { |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 585 | void Run(Thread* thread) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 586 | thread->socketserver()->Wait(1000, false); |
| 587 | |
| 588 | // Allow SIGTERM. This will be the only thread with it not masked so it will |
| 589 | // be delivered to us. |
| 590 | sigset_t mask; |
| 591 | sigemptyset(&mask); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 592 | pthread_sigmask(SIG_SETMASK, &mask, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 593 | |
| 594 | // Raise it. |
| 595 | raise(SIGTERM); |
| 596 | } |
| 597 | }; |
| 598 | |
| 599 | // Test that it works no matter what thread the kernel chooses to give the |
| 600 | // 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] | 601 | TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 602 | ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
| 603 | // Mask out SIGTERM so that it can't be delivered to this thread. |
| 604 | sigset_t mask; |
| 605 | sigemptyset(&mask); |
| 606 | sigaddset(&mask, SIGTERM); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 607 | EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 608 | // Start a new thread that raises it. It will have to be delivered to that |
| 609 | // thread. Our implementation should safely handle it and dispatch |
| 610 | // RecordSignal() on this thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 611 | std::unique_ptr<Thread> thread(Thread::CreateWithSocketServer()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 612 | std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 613 | thread->Start(runnable.get()); |
| 614 | EXPECT_TRUE(ss_->Wait(1500, true)); |
| 615 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 616 | EXPECT_EQ(Thread::Current(), signaled_thread_); |
| 617 | EXPECT_TRUE(ExpectNone()); |
| 618 | } |
| 619 | |
| 620 | #endif |
| 621 | |
| 622 | } // namespace rtc |