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