1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/physical_socket_server.h"
12
13 #include <signal.h>
14
15 #include <algorithm>
16 #include <memory>
17
18 #include "rtc_base/gunit.h"
19 #include "rtc_base/ip_address.h"
20 #include "rtc_base/logging.h"
21 #include "rtc_base/net_helpers.h"
22 #include "rtc_base/network_monitor.h"
23 #include "rtc_base/socket_unittest.h"
24 #include "rtc_base/test_utils.h"
25 #include "rtc_base/thread.h"
26 #include "test/field_trial.h"
27 #include "test/gtest.h"
28
29 namespace rtc {
30
31 #define MAYBE_SKIP_IPV4 \
32 if (!HasIPv4Enabled()) { \
33 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
34 return; \
35 }
36
37 #define MAYBE_SKIP_IPV6 \
38 if (!HasIPv6Enabled()) { \
39 RTC_LOG(LS_INFO) << "No IPv6... skipping"; \
40 return; \
41 }
42
43 class PhysicalSocketTest;
44
45 class FakeSocketDispatcher : public SocketDispatcher {
46 public:
FakeSocketDispatcher(PhysicalSocketServer * ss)47 explicit FakeSocketDispatcher(PhysicalSocketServer* ss)
48 : SocketDispatcher(ss) {}
49
FakeSocketDispatcher(SOCKET s,PhysicalSocketServer * ss)50 FakeSocketDispatcher(SOCKET s, PhysicalSocketServer* ss)
51 : SocketDispatcher(s, ss) {}
52
53 protected:
54 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override;
55 int DoSend(SOCKET socket, const char* buf, int len, int flags) override;
56 int DoSendTo(SOCKET socket,
57 const char* buf,
58 int len,
59 int flags,
60 const struct sockaddr* dest_addr,
61 socklen_t addrlen) override;
62 };
63
64 class FakePhysicalSocketServer : public PhysicalSocketServer {
65 public:
FakePhysicalSocketServer(PhysicalSocketTest * test)66 explicit FakePhysicalSocketServer(PhysicalSocketTest* test) : test_(test) {}
67
CreateSocket(int family,int type)68 Socket* CreateSocket(int family, int type) override {
69 SocketDispatcher* dispatcher = new FakeSocketDispatcher(this);
70 if (!dispatcher->Create(family, type)) {
71 delete dispatcher;
72 return nullptr;
73 }
74 return dispatcher;
75 }
76
WrapSocket(SOCKET s)77 Socket* WrapSocket(SOCKET s) override {
78 SocketDispatcher* dispatcher = new FakeSocketDispatcher(s, this);
79 if (!dispatcher->Initialize()) {
80 delete dispatcher;
81 return nullptr;
82 }
83 return dispatcher;
84 }
85
GetTest() const86 PhysicalSocketTest* GetTest() const { return test_; }
87
88 private:
89 PhysicalSocketTest* test_;
90 };
91
92 class FakeNetworkBinder : public NetworkBinderInterface {
93 public:
BindSocketToNetwork(int,const IPAddress &)94 NetworkBindingResult BindSocketToNetwork(int, const IPAddress&) override {
95 ++num_binds_;
96 return result_;
97 }
98
set_result(NetworkBindingResult result)99 void set_result(NetworkBindingResult result) { result_ = result; }
100
num_binds()101 int num_binds() { return num_binds_; }
102
103 private:
104 NetworkBindingResult result_ = NetworkBindingResult::SUCCESS;
105 int num_binds_ = 0;
106 };
107
108 class PhysicalSocketTest : public SocketTest {
109 public:
110 // Set flag to simluate failures when calling "::accept" on a Socket.
SetFailAccept(bool fail)111 void SetFailAccept(bool fail) { fail_accept_ = fail; }
FailAccept() const112 bool FailAccept() const { return fail_accept_; }
113
114 // Maximum size to ::send to a socket. Set to < 0 to disable limiting.
SetMaxSendSize(int max_size)115 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; }
MaxSendSize() const116 int MaxSendSize() const { return max_send_size_; }
117
118 protected:
PhysicalSocketTest()119 PhysicalSocketTest()
120 : SocketTest(&server_),
121 server_(this),
122 thread_(&server_),
123 fail_accept_(false),
124 max_send_size_(-1) {}
125
126 void ConnectInternalAcceptError(const IPAddress& loopback);
127 void WritableAfterPartialWrite(const IPAddress& loopback);
128
129 FakePhysicalSocketServer server_;
130 rtc::AutoSocketServerThread thread_;
131 bool fail_accept_;
132 int max_send_size_;
133 };
134
DoAccept(SOCKET socket,sockaddr * addr,socklen_t * addrlen)135 SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket,
136 sockaddr* addr,
137 socklen_t* addrlen) {
138 FakePhysicalSocketServer* ss =
139 static_cast<FakePhysicalSocketServer*>(socketserver());
140 if (ss->GetTest()->FailAccept()) {
141 return INVALID_SOCKET;
142 }
143
144 return SocketDispatcher::DoAccept(socket, addr, addrlen);
145 }
146
DoSend(SOCKET socket,const char * buf,int len,int flags)147 int FakeSocketDispatcher::DoSend(SOCKET socket,
148 const char* buf,
149 int len,
150 int flags) {
151 FakePhysicalSocketServer* ss =
152 static_cast<FakePhysicalSocketServer*>(socketserver());
153 if (ss->GetTest()->MaxSendSize() >= 0) {
154 len = std::min(len, ss->GetTest()->MaxSendSize());
155 }
156
157 return SocketDispatcher::DoSend(socket, buf, len, flags);
158 }
159
DoSendTo(SOCKET socket,const char * buf,int len,int flags,const struct sockaddr * dest_addr,socklen_t addrlen)160 int FakeSocketDispatcher::DoSendTo(SOCKET socket,
161 const char* buf,
162 int len,
163 int flags,
164 const struct sockaddr* dest_addr,
165 socklen_t addrlen) {
166 FakePhysicalSocketServer* ss =
167 static_cast<FakePhysicalSocketServer*>(socketserver());
168 if (ss->GetTest()->MaxSendSize() >= 0) {
169 len = std::min(len, ss->GetTest()->MaxSendSize());
170 }
171
172 return SocketDispatcher::DoSendTo(socket, buf, len, flags, dest_addr,
173 addrlen);
174 }
175
TEST_F(PhysicalSocketTest,TestConnectIPv4)176 TEST_F(PhysicalSocketTest, TestConnectIPv4) {
177 MAYBE_SKIP_IPV4;
178 SocketTest::TestConnectIPv4();
179 }
180
TEST_F(PhysicalSocketTest,TestConnectIPv6)181 TEST_F(PhysicalSocketTest, TestConnectIPv6) {
182 SocketTest::TestConnectIPv6();
183 }
184
TEST_F(PhysicalSocketTest,TestConnectWithDnsLookupIPv4)185 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
186 MAYBE_SKIP_IPV4;
187 SocketTest::TestConnectWithDnsLookupIPv4();
188 }
189
TEST_F(PhysicalSocketTest,TestConnectWithDnsLookupIPv6)190 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
191 SocketTest::TestConnectWithDnsLookupIPv6();
192 }
193
TEST_F(PhysicalSocketTest,TestConnectFailIPv4)194 TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
195 MAYBE_SKIP_IPV4;
196 SocketTest::TestConnectFailIPv4();
197 }
198
ConnectInternalAcceptError(const IPAddress & loopback)199 void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) {
200 webrtc::testing::StreamSink sink;
201 SocketAddress accept_addr;
202
203 // Create two clients.
204 std::unique_ptr<Socket> client1(
205 server_.CreateSocket(loopback.family(), SOCK_STREAM));
206 sink.Monitor(client1.get());
207 EXPECT_EQ(Socket::CS_CLOSED, client1->GetState());
208 EXPECT_TRUE(IsUnspecOrEmptyIP(client1->GetLocalAddress().ipaddr()));
209
210 std::unique_ptr<Socket> client2(
211 server_.CreateSocket(loopback.family(), SOCK_STREAM));
212 sink.Monitor(client2.get());
213 EXPECT_EQ(Socket::CS_CLOSED, client2->GetState());
214 EXPECT_TRUE(IsUnspecOrEmptyIP(client2->GetLocalAddress().ipaddr()));
215
216 // Create server and listen.
217 std::unique_ptr<Socket> server(
218 server_.CreateSocket(loopback.family(), SOCK_STREAM));
219 sink.Monitor(server.get());
220 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
221 EXPECT_EQ(0, server->Listen(5));
222 EXPECT_EQ(Socket::CS_CONNECTING, server->GetState());
223
224 // Ensure no pending server connections, since we haven't done anything yet.
225 EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ));
226 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
227 EXPECT_TRUE(accept_addr.IsNil());
228
229 // Attempt first connect to listening socket.
230 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress()));
231 EXPECT_FALSE(client1->GetLocalAddress().IsNil());
232 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress());
233
234 // Client is connecting, outcome not yet determined.
235 EXPECT_EQ(Socket::CS_CONNECTING, client1->GetState());
236 EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_OPEN));
237 EXPECT_FALSE(sink.Check(client1.get(), webrtc::testing::SSE_CLOSE));
238
239 // Server has pending connection, try to accept it (will fail).
240 EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)),
241 kTimeout);
242 // Simulate "::accept" returning an error.
243 SetFailAccept(true);
244 std::unique_ptr<Socket> accepted(server->Accept(&accept_addr));
245 EXPECT_FALSE(accepted);
246 ASSERT_TRUE(accept_addr.IsNil());
247
248 // Ensure no more pending server connections.
249 EXPECT_FALSE(sink.Check(server.get(), webrtc::testing::SSE_READ));
250 EXPECT_TRUE(nullptr == server->Accept(&accept_addr));
251 EXPECT_TRUE(accept_addr.IsNil());
252
253 // Attempt second connect to listening socket.
254 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress()));
255 EXPECT_FALSE(client2->GetLocalAddress().IsNil());
256 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress());
257
258 // Client is connecting, outcome not yet determined.
259 EXPECT_EQ(Socket::CS_CONNECTING, client2->GetState());
260 EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_OPEN));
261 EXPECT_FALSE(sink.Check(client2.get(), webrtc::testing::SSE_CLOSE));
262
263 // Server has pending connection, try to accept it (will succeed).
264 EXPECT_TRUE_WAIT((sink.Check(server.get(), webrtc::testing::SSE_READ)),
265 kTimeout);
266 SetFailAccept(false);
267 std::unique_ptr<Socket> accepted2(server->Accept(&accept_addr));
268 ASSERT_TRUE(accepted2);
269 EXPECT_FALSE(accept_addr.IsNil());
270 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr);
271 }
272
TEST_F(PhysicalSocketTest,TestConnectAcceptErrorIPv4)273 TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) {
274 MAYBE_SKIP_IPV4;
275 ConnectInternalAcceptError(kIPv4Loopback);
276 }
277
TEST_F(PhysicalSocketTest,TestConnectAcceptErrorIPv6)278 TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv6) {
279 MAYBE_SKIP_IPV6;
280 ConnectInternalAcceptError(kIPv6Loopback);
281 }
282
WritableAfterPartialWrite(const IPAddress & loopback)283 void PhysicalSocketTest::WritableAfterPartialWrite(const IPAddress& loopback) {
284 // Simulate a really small maximum send size.
285 const int kMaxSendSize = 128;
286 SetMaxSendSize(kMaxSendSize);
287
288 // Run the default send/receive socket tests with a smaller amount of data
289 // to avoid long running times due to the small maximum send size.
290 const size_t kDataSize = 128 * 1024;
291 TcpInternal(loopback, kDataSize, kMaxSendSize);
292 }
293
294 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
295 #if defined(WEBRTC_WIN)
296 #define MAYBE_TestWritableAfterPartialWriteIPv4 \
297 DISABLED_TestWritableAfterPartialWriteIPv4
298 #else
299 #define MAYBE_TestWritableAfterPartialWriteIPv4 \
300 TestWritableAfterPartialWriteIPv4
301 #endif
TEST_F(PhysicalSocketTest,MAYBE_TestWritableAfterPartialWriteIPv4)302 TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv4) {
303 MAYBE_SKIP_IPV4;
304 WritableAfterPartialWrite(kIPv4Loopback);
305 }
306
307 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
308 #if defined(WEBRTC_WIN)
309 #define MAYBE_TestWritableAfterPartialWriteIPv6 \
310 DISABLED_TestWritableAfterPartialWriteIPv6
311 #else
312 #define MAYBE_TestWritableAfterPartialWriteIPv6 \
313 TestWritableAfterPartialWriteIPv6
314 #endif
TEST_F(PhysicalSocketTest,MAYBE_TestWritableAfterPartialWriteIPv6)315 TEST_F(PhysicalSocketTest, MAYBE_TestWritableAfterPartialWriteIPv6) {
316 MAYBE_SKIP_IPV6;
317 WritableAfterPartialWrite(kIPv6Loopback);
318 }
319
TEST_F(PhysicalSocketTest,TestConnectFailIPv6)320 TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
321 SocketTest::TestConnectFailIPv6();
322 }
323
TEST_F(PhysicalSocketTest,TestConnectWithDnsLookupFailIPv4)324 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
325 MAYBE_SKIP_IPV4;
326 SocketTest::TestConnectWithDnsLookupFailIPv4();
327 }
328
TEST_F(PhysicalSocketTest,TestConnectWithDnsLookupFailIPv6)329 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
330 SocketTest::TestConnectWithDnsLookupFailIPv6();
331 }
332
TEST_F(PhysicalSocketTest,TestConnectWithClosedSocketIPv4)333 TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
334 MAYBE_SKIP_IPV4;
335 SocketTest::TestConnectWithClosedSocketIPv4();
336 }
337
TEST_F(PhysicalSocketTest,TestConnectWithClosedSocketIPv6)338 TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
339 SocketTest::TestConnectWithClosedSocketIPv6();
340 }
341
TEST_F(PhysicalSocketTest,TestConnectWhileNotClosedIPv4)342 TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
343 MAYBE_SKIP_IPV4;
344 SocketTest::TestConnectWhileNotClosedIPv4();
345 }
346
TEST_F(PhysicalSocketTest,TestConnectWhileNotClosedIPv6)347 TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
348 SocketTest::TestConnectWhileNotClosedIPv6();
349 }
350
TEST_F(PhysicalSocketTest,TestServerCloseDuringConnectIPv4)351 TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
352 MAYBE_SKIP_IPV4;
353 SocketTest::TestServerCloseDuringConnectIPv4();
354 }
355
TEST_F(PhysicalSocketTest,TestServerCloseDuringConnectIPv6)356 TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
357 SocketTest::TestServerCloseDuringConnectIPv6();
358 }
359
TEST_F(PhysicalSocketTest,TestClientCloseDuringConnectIPv4)360 TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
361 MAYBE_SKIP_IPV4;
362 SocketTest::TestClientCloseDuringConnectIPv4();
363 }
364
TEST_F(PhysicalSocketTest,TestClientCloseDuringConnectIPv6)365 TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
366 SocketTest::TestClientCloseDuringConnectIPv6();
367 }
368
TEST_F(PhysicalSocketTest,TestServerCloseIPv4)369 TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
370 MAYBE_SKIP_IPV4;
371 SocketTest::TestServerCloseIPv4();
372 }
373
TEST_F(PhysicalSocketTest,TestServerCloseIPv6)374 TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
375 SocketTest::TestServerCloseIPv6();
376 }
377
TEST_F(PhysicalSocketTest,TestCloseInClosedCallbackIPv4)378 TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
379 MAYBE_SKIP_IPV4;
380 SocketTest::TestCloseInClosedCallbackIPv4();
381 }
382
TEST_F(PhysicalSocketTest,TestCloseInClosedCallbackIPv6)383 TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
384 SocketTest::TestCloseInClosedCallbackIPv6();
385 }
386
TEST_F(PhysicalSocketTest,TestDeleteInReadCallbackIPv4)387 TEST_F(PhysicalSocketTest, TestDeleteInReadCallbackIPv4) {
388 MAYBE_SKIP_IPV4;
389 SocketTest::TestDeleteInReadCallbackIPv4();
390 }
391
TEST_F(PhysicalSocketTest,TestDeleteInReadCallbackIPv6)392 TEST_F(PhysicalSocketTest, TestDeleteInReadCallbackIPv6) {
393 SocketTest::TestDeleteInReadCallbackIPv6();
394 }
395
TEST_F(PhysicalSocketTest,TestSocketServerWaitIPv4)396 TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
397 MAYBE_SKIP_IPV4;
398 SocketTest::TestSocketServerWaitIPv4();
399 }
400
TEST_F(PhysicalSocketTest,TestSocketServerWaitIPv6)401 TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
402 SocketTest::TestSocketServerWaitIPv6();
403 }
404
TEST_F(PhysicalSocketTest,TestTcpIPv4)405 TEST_F(PhysicalSocketTest, TestTcpIPv4) {
406 MAYBE_SKIP_IPV4;
407 SocketTest::TestTcpIPv4();
408 }
409
TEST_F(PhysicalSocketTest,TestTcpIPv6)410 TEST_F(PhysicalSocketTest, TestTcpIPv6) {
411 SocketTest::TestTcpIPv6();
412 }
413
TEST_F(PhysicalSocketTest,TestUdpIPv4)414 TEST_F(PhysicalSocketTest, TestUdpIPv4) {
415 MAYBE_SKIP_IPV4;
416 SocketTest::TestUdpIPv4();
417 }
418
TEST_F(PhysicalSocketTest,TestUdpIPv6)419 TEST_F(PhysicalSocketTest, TestUdpIPv6) {
420 SocketTest::TestUdpIPv6();
421 }
422
423 // Disable for TSan v2, see
424 // https://code.google.com/p/webrtc/issues/detail?id=3498 for details.
425 // Also disable for MSan, see:
426 // https://code.google.com/p/webrtc/issues/detail?id=4958
427 // TODO(deadbeef): Enable again once test is reimplemented to be unflaky.
428 // Also disable for ASan.
429 // Disabled on Android: https://code.google.com/p/webrtc/issues/detail?id=4364
430 // Disabled on Linux: https://bugs.chromium.org/p/webrtc/issues/detail?id=5233
431 #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
432 defined(ADDRESS_SANITIZER) || defined(WEBRTC_ANDROID) || \
433 defined(WEBRTC_LINUX)
434 #define MAYBE_TestUdpReadyToSendIPv4 DISABLED_TestUdpReadyToSendIPv4
435 #else
436 #define MAYBE_TestUdpReadyToSendIPv4 TestUdpReadyToSendIPv4
437 #endif
TEST_F(PhysicalSocketTest,MAYBE_TestUdpReadyToSendIPv4)438 TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv4) {
439 MAYBE_SKIP_IPV4;
440 SocketTest::TestUdpReadyToSendIPv4();
441 }
442
443 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6167
444 #if defined(WEBRTC_WIN)
445 #define MAYBE_TestUdpReadyToSendIPv6 DISABLED_TestUdpReadyToSendIPv6
446 #else
447 #define MAYBE_TestUdpReadyToSendIPv6 TestUdpReadyToSendIPv6
448 #endif
TEST_F(PhysicalSocketTest,MAYBE_TestUdpReadyToSendIPv6)449 TEST_F(PhysicalSocketTest, MAYBE_TestUdpReadyToSendIPv6) {
450 SocketTest::TestUdpReadyToSendIPv6();
451 }
452
TEST_F(PhysicalSocketTest,TestGetSetOptionsIPv4)453 TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
454 MAYBE_SKIP_IPV4;
455 SocketTest::TestGetSetOptionsIPv4();
456 }
457
TEST_F(PhysicalSocketTest,TestGetSetOptionsIPv6)458 TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
459 SocketTest::TestGetSetOptionsIPv6();
460 }
461
462 #if defined(WEBRTC_POSIX)
463
464 #if !defined(WEBRTC_MAC)
465 // We don't get recv timestamps on Mac without the experiment
466 // WebRTC-SCM-Timestamp
TEST_F(PhysicalSocketTest,TestSocketRecvTimestampIPv4)467 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) {
468 MAYBE_SKIP_IPV4;
469 SocketTest::TestSocketRecvTimestampIPv4();
470 }
471
TEST_F(PhysicalSocketTest,TestSocketRecvTimestampIPv6)472 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) {
473 SocketTest::TestSocketRecvTimestampIPv6();
474 }
475 #endif
476
TEST_F(PhysicalSocketTest,TestSocketRecvTimestampIPv4ScmExperiment)477 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4ScmExperiment) {
478 MAYBE_SKIP_IPV4;
479 webrtc::test::ScopedFieldTrials trial("WebRTC-SCM-Timestamp/Enabled/");
480 SocketTest::TestSocketRecvTimestampIPv4();
481 }
482
TEST_F(PhysicalSocketTest,TestSocketRecvTimestampIPv6ScmExperiment)483 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6ScmExperiment) {
484 webrtc::test::ScopedFieldTrials trial("WebRTC-SCM-Timestamp/Enabled/");
485 SocketTest::TestSocketRecvTimestampIPv6();
486 }
487
488 // Verify that if the socket was unable to be bound to a real network interface
489 // (not loopback), Bind will return an error.
TEST_F(PhysicalSocketTest,BindFailsIfNetworkBinderFailsForNonLoopbackInterface)490 TEST_F(PhysicalSocketTest,
491 BindFailsIfNetworkBinderFailsForNonLoopbackInterface) {
492 MAYBE_SKIP_IPV4;
493 FakeNetworkBinder fake_network_binder;
494 server_.set_network_binder(&fake_network_binder);
495 std::unique_ptr<Socket> socket(server_.CreateSocket(AF_INET, SOCK_DGRAM));
496 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
497 EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0)));
498 server_.set_network_binder(nullptr);
499 }
500
501 // Network binder shouldn't be used if the socket is bound to the "any" IP.
TEST_F(PhysicalSocketTest,NetworkBinderIsNotUsedForAnyIp)502 TEST_F(PhysicalSocketTest, NetworkBinderIsNotUsedForAnyIp) {
503 MAYBE_SKIP_IPV4;
504 FakeNetworkBinder fake_network_binder;
505 server_.set_network_binder(&fake_network_binder);
506 std::unique_ptr<Socket> socket(server_.CreateSocket(AF_INET, SOCK_DGRAM));
507 EXPECT_EQ(0, socket->Bind(SocketAddress("0.0.0.0", 0)));
508 EXPECT_EQ(0, fake_network_binder.num_binds());
509 server_.set_network_binder(nullptr);
510 }
511
512 // For a loopback interface, failures to bind to the interface should be
513 // tolerated.
TEST_F(PhysicalSocketTest,BindSucceedsIfNetworkBinderFailsForLoopbackInterface)514 TEST_F(PhysicalSocketTest,
515 BindSucceedsIfNetworkBinderFailsForLoopbackInterface) {
516 MAYBE_SKIP_IPV4;
517 FakeNetworkBinder fake_network_binder;
518 server_.set_network_binder(&fake_network_binder);
519 std::unique_ptr<Socket> socket(server_.CreateSocket(AF_INET, SOCK_DGRAM));
520 fake_network_binder.set_result(NetworkBindingResult::FAILURE);
521 EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0)));
522 server_.set_network_binder(nullptr);
523 }
524
525 #endif
526
TEST_F(PhysicalSocketTest,UdpSocketRecvTimestampUseRtcEpochIPv4ScmExperiment)527 TEST_F(PhysicalSocketTest, UdpSocketRecvTimestampUseRtcEpochIPv4ScmExperiment) {
528 MAYBE_SKIP_IPV4;
529 webrtc::test::ScopedFieldTrials trial("WebRTC-SCM-Timestamp/Enabled/");
530 SocketTest::TestUdpSocketRecvTimestampUseRtcEpochIPv4();
531 }
532
TEST_F(PhysicalSocketTest,UdpSocketRecvTimestampUseRtcEpochIPv6ScmExperiment)533 TEST_F(PhysicalSocketTest, UdpSocketRecvTimestampUseRtcEpochIPv6ScmExperiment) {
534 webrtc::test::ScopedFieldTrials trial("WebRTC-SCM-Timestamp/Enabled/");
535 SocketTest::TestUdpSocketRecvTimestampUseRtcEpochIPv6();
536 }
537
538 } // namespace rtc
539