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