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