OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <signal.h> | 12 #include <signal.h> |
13 #include <stdarg.h> | 13 #include <stdarg.h> |
14 | 14 |
15 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/base/networkmonitor.h" |
17 #include "webrtc/base/physicalsocketserver.h" | 18 #include "webrtc/base/physicalsocketserver.h" |
18 #include "webrtc/base/socket_unittest.h" | 19 #include "webrtc/base/socket_unittest.h" |
19 #include "webrtc/base/testutils.h" | 20 #include "webrtc/base/testutils.h" |
20 #include "webrtc/base/thread.h" | 21 #include "webrtc/base/thread.h" |
21 | 22 |
22 namespace rtc { | 23 namespace rtc { |
23 | 24 |
24 #define MAYBE_SKIP_IPV6 \ | 25 #define MAYBE_SKIP_IPV6 \ |
25 if (!HasIPv6Enabled()) { \ | 26 if (!HasIPv6Enabled()) { \ |
26 LOG(LS_INFO) << "No IPv6... skipping"; \ | 27 LOG(LS_INFO) << "No IPv6... skipping"; \ |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 79 } |
79 return dispatcher; | 80 return dispatcher; |
80 } | 81 } |
81 | 82 |
82 PhysicalSocketTest* GetTest() const { return test_; } | 83 PhysicalSocketTest* GetTest() const { return test_; } |
83 | 84 |
84 private: | 85 private: |
85 PhysicalSocketTest* test_; | 86 PhysicalSocketTest* test_; |
86 }; | 87 }; |
87 | 88 |
| 89 class FakeNetworkBinder : public NetworkBinderInterface { |
| 90 public: |
| 91 NetworkBindingResult BindSocketToNetwork(int, const IPAddress&) override { |
| 92 return result_; |
| 93 } |
| 94 |
| 95 void set_result(NetworkBindingResult result) { result_ = result; } |
| 96 |
| 97 private: |
| 98 NetworkBindingResult result_ = NetworkBindingResult::SUCCESS; |
| 99 }; |
| 100 |
88 class PhysicalSocketTest : public SocketTest { | 101 class PhysicalSocketTest : public SocketTest { |
89 public: | 102 public: |
90 // Set flag to simluate failures when calling "::accept" on a AsyncSocket. | 103 // Set flag to simluate failures when calling "::accept" on a AsyncSocket. |
91 void SetFailAccept(bool fail) { fail_accept_ = fail; } | 104 void SetFailAccept(bool fail) { fail_accept_ = fail; } |
92 bool FailAccept() const { return fail_accept_; } | 105 bool FailAccept() const { return fail_accept_; } |
93 | 106 |
94 // Maximum size to ::send to a socket. Set to < 0 to disable limiting. | 107 // Maximum size to ::send to a socket. Set to < 0 to disable limiting. |
95 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; } | 108 void SetMaxSendSize(int max_size) { max_send_size_ = max_size; } |
96 int MaxSendSize() const { return max_send_size_; } | 109 int MaxSendSize() const { return max_send_size_; } |
97 | 110 |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 #if !defined(WEBRTC_MAC) | 421 #if !defined(WEBRTC_MAC) |
409 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) { | 422 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv4) { |
410 SocketTest::TestSocketRecvTimestampIPv4(); | 423 SocketTest::TestSocketRecvTimestampIPv4(); |
411 } | 424 } |
412 | 425 |
413 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) { | 426 TEST_F(PhysicalSocketTest, TestSocketRecvTimestampIPv6) { |
414 SocketTest::TestSocketRecvTimestampIPv6(); | 427 SocketTest::TestSocketRecvTimestampIPv6(); |
415 } | 428 } |
416 #endif | 429 #endif |
417 | 430 |
| 431 // Verify that if the socket was unable to be bound to a real network interface |
| 432 // (not loopback), Bind will return an error. |
| 433 TEST_F(PhysicalSocketTest, |
| 434 BindFailsIfNetworkBinderFailsForNonLoopbackInterface) { |
| 435 FakeNetworkBinder fake_network_binder; |
| 436 server_->set_network_binder(&fake_network_binder); |
| 437 std::unique_ptr<AsyncSocket> socket( |
| 438 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 439 fake_network_binder.set_result(NetworkBindingResult::FAILURE); |
| 440 EXPECT_EQ(-1, socket->Bind(SocketAddress("192.168.0.1", 0))); |
| 441 server_->set_network_binder(nullptr); |
| 442 } |
| 443 |
| 444 // For a loopback interface, failures to bind to the interface should be |
| 445 // tolerated. |
| 446 TEST_F(PhysicalSocketTest, |
| 447 BindSucceedsIfNetworkBinderFailsForLoopbackInterface) { |
| 448 FakeNetworkBinder fake_network_binder; |
| 449 server_->set_network_binder(&fake_network_binder); |
| 450 std::unique_ptr<AsyncSocket> socket( |
| 451 server_->CreateAsyncSocket(AF_INET, SOCK_DGRAM)); |
| 452 fake_network_binder.set_result(NetworkBindingResult::FAILURE); |
| 453 EXPECT_EQ(0, socket->Bind(SocketAddress(kIPv4Loopback, 0))); |
| 454 server_->set_network_binder(nullptr); |
| 455 } |
| 456 |
418 class PosixSignalDeliveryTest : public testing::Test { | 457 class PosixSignalDeliveryTest : public testing::Test { |
419 public: | 458 public: |
420 static void RecordSignal(int signum) { | 459 static void RecordSignal(int signum) { |
421 signals_received_.push_back(signum); | 460 signals_received_.push_back(signum); |
422 signaled_thread_ = Thread::Current(); | 461 signaled_thread_ = Thread::Current(); |
423 } | 462 } |
424 | 463 |
425 protected: | 464 protected: |
426 void SetUp() { | 465 void SetUp() { |
427 ss_.reset(new PhysicalSocketServer()); | 466 ss_.reset(new PhysicalSocketServer()); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 thread->Start(runnable.get()); | 571 thread->Start(runnable.get()); |
533 EXPECT_TRUE(ss_->Wait(1500, true)); | 572 EXPECT_TRUE(ss_->Wait(1500, true)); |
534 EXPECT_TRUE(ExpectSignal(SIGTERM)); | 573 EXPECT_TRUE(ExpectSignal(SIGTERM)); |
535 EXPECT_EQ(Thread::Current(), signaled_thread_); | 574 EXPECT_EQ(Thread::Current(), signaled_thread_); |
536 EXPECT_TRUE(ExpectNone()); | 575 EXPECT_TRUE(ExpectNone()); |
537 } | 576 } |
538 | 577 |
539 #endif | 578 #endif |
540 | 579 |
541 } // namespace rtc | 580 } // namespace rtc |
OLD | NEW |