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> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 13 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | : SocketDispatcher(ss) {} |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 43 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 44 | FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 45 | : SocketDispatcher(s, ss) {} |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 46 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 47 | protected: |
| 48 | SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 49 | int DoSend(SOCKET socket, const char* buf, int len, int flags) override; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 50 | int DoSendTo(SOCKET socket, |
| 51 | const char* buf, |
| 52 | int len, |
| 53 | int flags, |
| 54 | const struct sockaddr* dest_addr, |
| 55 | socklen_t addrlen) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 58 | class FakePhysicalSocketServer : public PhysicalSocketServer { |
| 59 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | explicit FakePhysicalSocketServer(PhysicalSocketTest* test) : test_(test) {} |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 61 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 62 | AsyncSocket* CreateAsyncSocket(int family, int type) override { |
| 63 | SocketDispatcher* dispatcher = new FakeSocketDispatcher(this); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 64 | if (!dispatcher->Create(family, 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; |
| 69 | } |
| 70 | |
| 71 | AsyncSocket* WrapSocket(SOCKET s) override { |
| 72 | SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this); |
| 73 | if (!dispatcher->Initialize()) { |
| 74 | delete dispatcher; |
| 75 | return nullptr; |
| 76 | } |
| 77 | return dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | PhysicalSocketTest* GetTest() const { return test_; } |
| 81 | |
| 82 | private: |
| 83 | PhysicalSocketTest* test_; |
| 84 | }; |
| 85 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 86 | class FakeNetworkBinder : public NetworkBinderInterface { |
| 87 | public: |
| 88 | NetworkBindingResult BindSocketToNetwork(int, const IPAddress&) override { |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 89 | ++num_binds_; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 90 | return result_; |
| 91 | } |
| 92 | |
| 93 | void set_result(NetworkBindingResult result) { result_ = result; } |
| 94 | |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 95 | int num_binds() { return num_binds_; } |
| 96 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 97 | private: |
| 98 | NetworkBindingResult result_ = NetworkBindingResult::SUCCESS; |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 99 | int num_binds_ = 0; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 102 | class PhysicalSocketTest : public SocketTest { |
| 103 | public: |
| 104 | // Set flag to simluate failures when calling "::accept" on a AsyncSocket. |
| 105 | void SetFailAccept(bool fail) { fail_accept_ = fail; } |
| 106 | bool FailAccept() const { return fail_accept_; } |
| 107 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 108 | // Maximum size to ::send to a socket. Set to < 0 to disable limiting. |
| 109 | void SetMaxSendSize(int max_size) { max_send_size_ = max_size; } |
| 110 | int MaxSendSize() const { return max_send_size_; } |
| 111 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 112 | protected: |
| 113 | PhysicalSocketTest() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | : server_(new FakePhysicalSocketServer(this)), |
| 115 | thread_(server_.get()), |
| 116 | fail_accept_(false), |
| 117 | max_send_size_(-1) {} |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 118 | |
| 119 | void ConnectInternalAcceptError(const IPAddress& loopback); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 120 | void WritableAfterPartialWrite(const IPAddress& loopback); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 121 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 122 | std::unique_ptr<FakePhysicalSocketServer> server_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 123 | rtc::AutoSocketServerThread thread_; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 124 | bool fail_accept_; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 125 | int max_send_size_; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket, |
| 129 | sockaddr* addr, |
| 130 | socklen_t* addrlen) { |
| 131 | FakePhysicalSocketServer* ss = |
| 132 | static_cast<FakePhysicalSocketServer*>(socketserver()); |
| 133 | if (ss->GetTest()->FailAccept()) { |
| 134 | return INVALID_SOCKET; |
| 135 | } |
| 136 | |
| 137 | return SocketDispatcher::DoAccept(socket, addr, addrlen); |
| 138 | } |
| 139 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 140 | int FakeSocketDispatcher::DoSend(SOCKET socket, |
| 141 | const char* buf, |
| 142 | int len, |
| 143 | int flags) { |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 144 | FakePhysicalSocketServer* ss = |
| 145 | static_cast<FakePhysicalSocketServer*>(socketserver()); |
| 146 | if (ss->GetTest()->MaxSendSize() >= 0) { |
| 147 | len = std::min(len, ss->GetTest()->MaxSendSize()); |
| 148 | } |
| 149 | |
| 150 | return SocketDispatcher::DoSend(socket, buf, len, flags); |
| 151 | } |
| 152 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 153 | int FakeSocketDispatcher::DoSendTo(SOCKET socket, |
| 154 | const char* buf, |
| 155 | int len, |
| 156 | int flags, |
| 157 | const struct sockaddr* dest_addr, |
| 158 | socklen_t addrlen) { |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 159 | FakePhysicalSocketServer* ss = |
| 160 | static_cast<FakePhysicalSocketServer*>(socketserver()); |
| 161 | if (ss->GetTest()->MaxSendSize() >= 0) { |
| 162 | len = std::min(len, ss->GetTest()->MaxSendSize()); |
| 163 | } |
| 164 | |
| 165 | return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 166 | addrlen); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 167 | } |
| 168 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 169 | TEST_F(PhysicalSocketTest, TestConnectIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 170 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | SocketTest::TestConnectIPv4(); |
| 172 | } |
| 173 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 174 | TEST_F(PhysicalSocketTest, TestConnectIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | SocketTest::TestConnectIPv6(); |
| 176 | } |
| 177 | |
| 178 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 179 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | SocketTest::TestConnectWithDnsLookupIPv4(); |
| 181 | } |
| 182 | |
| 183 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) { |
| 184 | SocketTest::TestConnectWithDnsLookupIPv6(); |
| 185 | } |
| 186 | |
| 187 | TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 188 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 189 | SocketTest::TestConnectFailIPv4(); |
| 190 | } |
| 191 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 192 | void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) { |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 193 | webrtc::testing::StreamSink sink; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 194 | SocketAddress accept_addr; |
| 195 | |
| 196 | // Create two clients. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 197 | std::unique_ptr<AsyncSocket> client1( |
| 198 | server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 199 | sink.Monitor(client1.get()); |
| 200 | EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState()); |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 201 | EXPECT_TRUE(IsUnspecOrEmptyIP(client1->GetLocalAddress().ipaddr())); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 202 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 203 | std::unique_ptr<AsyncSocket> client2( |
| 204 | server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 205 | sink.Monitor(client2.get()); |
| 206 | EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState()); |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 207 | EXPECT_TRUE(IsUnspecOrEmptyIP(client2->GetLocalAddress().ipaddr())); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 208 | |
| 209 | // Create server and listen. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 210 | std::unique_ptr<AsyncSocket> server( |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 211 | server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
| 212 | sink.Monitor(server.get()); |
| 213 | EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); |
| 214 | EXPECT_EQ(0, server->Listen(5)); |
| 215 | EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState()); |
| 216 | |
| 217 | // Ensure no pending server connections, since we haven't done anything yet. |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 218 | EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 219 | EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
| 220 | EXPECT_TRUE(accept_addr.IsNil()); |
| 221 | |
| 222 | // Attempt first connect to listening socket. |
| 223 | EXPECT_EQ(0, client1->Connect(server->GetLocalAddress())); |
| 224 | EXPECT_FALSE(client1->GetLocalAddress().IsNil()); |
| 225 | EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress()); |
| 226 | |
| 227 | // Client is connecting, outcome not yet determined. |
| 228 | EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState()); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 229 | EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_OPEN)); |
| 230 | EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_CLOSE)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 231 | |
| 232 | // Server has pending connection, try to accept it (will fail). |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 233 | EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)), |
| 234 | kTimeout); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 235 | // Simulate "::accept" returning an error. |
| 236 | SetFailAccept(true); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 237 | std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 238 | EXPECT_FALSE(accepted); |
| 239 | ASSERT_TRUE(accept_addr.IsNil()); |
| 240 | |
| 241 | // Ensure no more pending server connections. |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 242 | EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 243 | EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
| 244 | EXPECT_TRUE(accept_addr.IsNil()); |
| 245 | |
| 246 | // Attempt second connect to listening socket. |
| 247 | EXPECT_EQ(0, client2->Connect(server->GetLocalAddress())); |
| 248 | EXPECT_FALSE(client2->GetLocalAddress().IsNil()); |
| 249 | EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress()); |
| 250 | |
| 251 | // Client is connecting, outcome not yet determined. |
| 252 | EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState()); |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 253 | EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_OPEN)); |
| 254 | EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_CLOSE)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 255 | |
| 256 | // Server has pending connection, try to accept it (will succeed). |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 257 | EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)), |
| 258 | kTimeout); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 259 | SetFailAccept(false); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 260 | std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 261 | ASSERT_TRUE(accepted2); |
| 262 | EXPECT_FALSE(accept_addr.IsNil()); |
| 263 | EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr); |
| 264 | } |
| 265 | |
| 266 | TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 267 | MAYBE_SKIP_IPV4; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 268 | ConnectInternalAcceptError(kIPv4Loopback); |
| 269 | } |
| 270 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 271 | TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) { |
| 272 | MAYBE_SKIP_IPV6; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 273 | ConnectInternalAcceptError(kIPv6Loopback); |
| 274 | } |
| 275 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 276 | void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) { |
| 277 | // Simulate a really small maximum send size. |
| 278 | const int kMaxSendSize = 128; |
| 279 | SetMaxSendSize(kMaxSendSize); |
| 280 | |
| 281 | // Run the default send/receive socket tests with a smaller amount of data |
| 282 | // to avoid long running times due to the small maximum send size. |
| 283 | const size_t kDataSize = 128 * 1024; |
| 284 | TcpInternal(loopback, kDataSize, kMaxSendSize); |
| 285 | } |
| 286 | |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 287 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167 |
| 288 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 289 | #define MAYBE_TestWritableAfterPartialWriteIPv4 \ |
| 290 | DISABLED_TestWritableAfterPartialWriteIPv4 |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 291 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 292 | #define MAYBE_TestWritableAfterPartialWriteIPv4 \ |
| 293 | TestWritableAfterPartialWriteIPv4 |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 294 | #endif |
| 295 | TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 296 | MAYBE_SKIP_IPV4; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 297 | WritableAfterPartialWrite(kIPv4Loopback); |
| 298 | } |
| 299 | |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 300 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167 |
| 301 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 302 | #define MAYBE_TestWritableAfterPartialWriteIPv6 \ |
| 303 | DISABLED_TestWritableAfterPartialWriteIPv6 |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 304 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 305 | #define MAYBE_TestWritableAfterPartialWriteIPv6 \ |
| 306 | TestWritableAfterPartialWriteIPv6 |
danilchap | b7b9dca | 2016-08-05 05:55:43 -0700 | [diff] [blame] | 307 | #endif |
| 308 | TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) { |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 309 | MAYBE_SKIP_IPV6; |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 310 | WritableAfterPartialWrite(kIPv6Loopback); |
| 311 | } |
| 312 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 313 | TEST_F(PhysicalSocketTest, TestConnectFailIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 314 | SocketTest::TestConnectFailIPv6(); |
| 315 | } |
| 316 | |
| 317 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 318 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | SocketTest::TestConnectWithDnsLookupFailIPv4(); |
| 320 | } |
| 321 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 322 | TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 323 | SocketTest::TestConnectWithDnsLookupFailIPv6(); |
| 324 | } |
| 325 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 475 | TEST_F(PhysicalSocketTest, NetworkBinderIsNotUsedForAnyIp) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 476 | MAYBE_SKIP_IPV4; |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 477 | FakeNetworkBinder fake_network_binder; |
| 478 | server_->set_network_binder(&fake_network_binder); |
| 479 | std::unique_ptr<AsyncSocket> socket( |
| 480 | server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 481 | EXPECT_EQ(0, socket->Bind(SocketAddress("0.0.0.0", 0))); |
| 482 | EXPECT_EQ(0, fake_network_binder.num_binds()); |
| 483 | server_->set_network_binder(nullptr); |
| 484 | } |
| 485 | |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 486 | // For a loopback interface, failures to bind to the interface should be |
| 487 | // tolerated. |
| 488 | TEST_F(PhysicalSocketTest, |
| 489 | BindSucceedsIfNetworkBinderFailsForLoopbackInterface) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 490 | MAYBE_SKIP_IPV4; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 491 | FakeNetworkBinder fake_network_binder; |
| 492 | server_->set_network_binder(&fake_network_binder); |
| 493 | std::unique_ptr<AsyncSocket> socket( |
| 494 | server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 495 | fake_network_binder.set_result(NetworkBindingResult::FAILURE); |
| 496 | EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0))); |
| 497 | server_->set_network_binder(nullptr); |
| 498 | } |
| 499 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 500 | class PosixSignalDeliveryTest : public testing::Test { |
| 501 | public: |
| 502 | static void RecordSignal(int signum) { |
| 503 | signals_received_.push_back(signum); |
| 504 | signaled_thread_ = Thread::Current(); |
| 505 | } |
| 506 | |
| 507 | protected: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 508 | void SetUp() override { ss_.reset(new PhysicalSocketServer()); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 509 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 510 | void TearDown() override { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 511 | ss_.reset(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 512 | signals_received_.clear(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 513 | signaled_thread_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | bool ExpectSignal(int signum) { |
| 517 | if (signals_received_.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 518 | RTC_LOG(LS_ERROR) << "ExpectSignal(): No signal received"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | if (signals_received_[0] != signum) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 522 | RTC_LOG(LS_ERROR) << "ExpectSignal(): Received signal " |
| 523 | << signals_received_[0] << ", expected " << signum; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
| 526 | signals_received_.erase(signals_received_.begin()); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | bool ExpectNone() { |
| 531 | bool ret = signals_received_.empty(); |
| 532 | if (!ret) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 533 | RTC_LOG(LS_ERROR) << "ExpectNone(): Received signal " |
| 534 | << signals_received_[0] << ", expected none"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 535 | } |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static std::vector<int> signals_received_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 540 | static Thread* signaled_thread_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 541 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 542 | std::unique_ptr<PhysicalSocketServer> ss_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | std::vector<int> PosixSignalDeliveryTest::signals_received_; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 546 | Thread* PosixSignalDeliveryTest::signaled_thread_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 547 | |
| 548 | // Test receiving a synchronous signal while not in Wait() and then entering |
| 549 | // Wait() afterwards. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 550 | TEST_F(PosixSignalDeliveryTest, RaiseThenWait) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 551 | ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal)); |
| 552 | raise(SIGTERM); |
| 553 | EXPECT_TRUE(ss_->Wait(0, true)); |
| 554 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 555 | EXPECT_TRUE(ExpectNone()); |
| 556 | } |
| 557 | |
| 558 | // Test that we can handle getting tons of repeated signals and that we see all |
| 559 | // the different ones. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 560 | TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 561 | ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
| 562 | ss_->SetPosixSignalHandler(SIGINT, &RecordSignal); |
| 563 | for (int i = 0; i < 10000; ++i) { |
| 564 | raise(SIGTERM); |
| 565 | } |
| 566 | raise(SIGINT); |
| 567 | EXPECT_TRUE(ss_->Wait(0, true)); |
| 568 | // Order will be lowest signal numbers first. |
| 569 | EXPECT_TRUE(ExpectSignal(SIGINT)); |
| 570 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 571 | EXPECT_TRUE(ExpectNone()); |
| 572 | } |
| 573 | |
| 574 | // Test that a signal during a Wait() call is detected. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 575 | TEST_F(PosixSignalDeliveryTest, SignalDuringWait) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 576 | ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal); |
| 577 | alarm(1); |
| 578 | EXPECT_TRUE(ss_->Wait(1500, true)); |
| 579 | EXPECT_TRUE(ExpectSignal(SIGALRM)); |
| 580 | EXPECT_TRUE(ExpectNone()); |
| 581 | } |
| 582 | |
| 583 | class RaiseSigTermRunnable : public Runnable { |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 584 | void Run(Thread* thread) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 585 | thread->socketserver()->Wait(1000, false); |
| 586 | |
| 587 | // Allow SIGTERM. This will be the only thread with it not masked so it will |
| 588 | // be delivered to us. |
| 589 | sigset_t mask; |
| 590 | sigemptyset(&mask); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 591 | pthread_sigmask(SIG_SETMASK, &mask, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 592 | |
| 593 | // Raise it. |
| 594 | raise(SIGTERM); |
| 595 | } |
| 596 | }; |
| 597 | |
| 598 | // Test that it works no matter what thread the kernel chooses to give the |
| 599 | // 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] | 600 | TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 601 | ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
| 602 | // Mask out SIGTERM so that it can't be delivered to this thread. |
| 603 | sigset_t mask; |
| 604 | sigemptyset(&mask); |
| 605 | sigaddset(&mask, SIGTERM); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 606 | EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 607 | // Start a new thread that raises it. It will have to be delivered to that |
| 608 | // thread. Our implementation should safely handle it and dispatch |
| 609 | // RecordSignal() on this thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 610 | std::unique_ptr<Thread> thread(Thread::CreateWithSocketServer()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 611 | std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 612 | thread->Start(runnable.get()); |
| 613 | EXPECT_TRUE(ss_->Wait(1500, true)); |
| 614 | EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 615 | EXPECT_EQ(Thread::Current(), signaled_thread_); |
| 616 | EXPECT_TRUE(ExpectNone()); |
| 617 | } |
| 618 | |
| 619 | #endif |
| 620 | |
| 621 | } // namespace rtc |