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