Chromium Code Reviews| 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 <signal.h> | 11 #include <signal.h> |
| 12 #include <stdarg.h> | 12 #include <stdarg.h> |
| 13 | 13 |
| 14 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
| 15 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
| 16 #include "webrtc/base/physicalsocketserver.h" | 16 #include "webrtc/base/physicalsocketserver.h" |
| 17 #include "webrtc/base/scoped_ptr.h" | 17 #include "webrtc/base/scoped_ptr.h" |
| 18 #include "webrtc/base/socket_unittest.h" | 18 #include "webrtc/base/socket_unittest.h" |
| 19 #include "webrtc/base/testutils.h" | 19 #include "webrtc/base/testutils.h" |
| 20 #include "webrtc/base/thread.h" | 20 #include "webrtc/base/thread.h" |
| 21 #include "webrtc/test/testsupport/gtest_disable.h" | 21 #include "webrtc/test/testsupport/gtest_disable.h" |
| 22 | 22 |
| 23 namespace rtc { | 23 namespace rtc { |
| 24 | 24 |
| 25 class PhysicalSocketTest; | |
| 26 | |
| 27 class MockSocketDispatcher : public SocketDispatcher { | |
|
pthatcher1
2015/12/03 22:56:32
I think we would call these Fake, not Mock
| |
| 28 public: | |
| 29 explicit MockSocketDispatcher(PhysicalSocketServer* ss) | |
| 30 : SocketDispatcher(ss) { | |
| 31 } | |
| 32 | |
| 33 protected: | |
| 34 SOCKET DoAccept(SOCKET socket, sockaddr* addr, socklen_t* addrlen) override; | |
| 35 }; | |
| 36 | |
| 37 class MockPhysicalSocketServer : public PhysicalSocketServer { | |
| 38 public: | |
| 39 explicit MockPhysicalSocketServer(PhysicalSocketTest* test) | |
| 40 : test_(test) { | |
| 41 } | |
| 42 | |
| 43 AsyncSocket* CreateAsyncSocket(int family, int type) override { | |
| 44 SocketDispatcher* dispatcher = new MockSocketDispatcher(this); | |
| 45 if (dispatcher->Create(family, type)) { | |
| 46 return dispatcher; | |
| 47 } else { | |
| 48 delete dispatcher; | |
| 49 return nullptr; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 PhysicalSocketTest* GetTest() const { return test_; } | |
| 54 | |
| 55 private: | |
| 56 PhysicalSocketTest* test_; | |
| 57 }; | |
| 58 | |
| 25 class PhysicalSocketTest : public SocketTest { | 59 class PhysicalSocketTest : public SocketTest { |
| 60 public: | |
| 61 // Set flag to simluate failures when calling "::accept" on a AsyncSocket. | |
| 62 void SetFailAccept(bool fail) { fail_accept_ = fail; } | |
| 63 bool FailAccept() const { return fail_accept_; } | |
| 64 | |
| 65 protected: | |
| 66 PhysicalSocketTest() | |
| 67 : server_(new MockPhysicalSocketServer(this)), | |
| 68 scope_(server_.get()), | |
| 69 fail_accept_(false) { | |
| 70 } | |
| 71 | |
| 72 void ConnectInternalAcceptError(const IPAddress& loopback); | |
| 73 | |
| 74 rtc::scoped_ptr<MockPhysicalSocketServer> server_; | |
| 75 SocketServerScope scope_; | |
| 76 bool fail_accept_; | |
| 26 }; | 77 }; |
| 27 | 78 |
| 79 SOCKET MockSocketDispatcher::DoAccept(SOCKET socket, | |
| 80 sockaddr* addr, | |
| 81 socklen_t* addrlen) { | |
| 82 MockPhysicalSocketServer* ss = | |
| 83 static_cast<MockPhysicalSocketServer*>(socketserver()); | |
| 84 if (ss->GetTest()->FailAccept()) { | |
| 85 return INVALID_SOCKET; | |
| 86 } | |
| 87 | |
| 88 return SocketDispatcher::DoAccept(socket, addr, addrlen); | |
| 89 } | |
| 90 | |
| 28 TEST_F(PhysicalSocketTest, TestConnectIPv4) { | 91 TEST_F(PhysicalSocketTest, TestConnectIPv4) { |
| 29 SocketTest::TestConnectIPv4(); | 92 SocketTest::TestConnectIPv4(); |
| 30 } | 93 } |
| 31 | 94 |
| 32 // Crashes on Linux. See webrtc:4923. | 95 // Crashes on Linux. See webrtc:4923. |
| 33 #if defined(WEBRTC_LINUX) | 96 #if defined(WEBRTC_LINUX) |
| 34 #define MAYBE_TestConnectIPv6 DISABLED_TestConnectIPv6 | 97 #define MAYBE_TestConnectIPv6 DISABLED_TestConnectIPv6 |
| 35 #else | 98 #else |
| 36 #define MAYBE_TestConnectIPv6 TestConnectIPv6 | 99 #define MAYBE_TestConnectIPv6 TestConnectIPv6 |
| 37 #endif | 100 #endif |
| 38 TEST_F(PhysicalSocketTest, MAYBE_TestConnectIPv6) { | 101 TEST_F(PhysicalSocketTest, MAYBE_TestConnectIPv6) { |
| 39 SocketTest::TestConnectIPv6(); | 102 SocketTest::TestConnectIPv6(); |
| 40 } | 103 } |
| 41 | 104 |
| 42 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) { | 105 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) { |
| 43 SocketTest::TestConnectWithDnsLookupIPv4(); | 106 SocketTest::TestConnectWithDnsLookupIPv4(); |
| 44 } | 107 } |
| 45 | 108 |
| 46 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) { | 109 TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) { |
| 47 SocketTest::TestConnectWithDnsLookupIPv6(); | 110 SocketTest::TestConnectWithDnsLookupIPv6(); |
| 48 } | 111 } |
| 49 | 112 |
| 50 TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { | 113 TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { |
| 51 SocketTest::TestConnectFailIPv4(); | 114 SocketTest::TestConnectFailIPv4(); |
| 52 } | 115 } |
| 53 | 116 |
| 117 void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) { | |
| 118 testing::StreamSink sink; | |
| 119 SocketAddress accept_addr; | |
| 120 | |
| 121 // Create two clients. | |
| 122 scoped_ptr<AsyncSocket> client1(server_->CreateAsyncSocket(loopback.family(), | |
| 123 SOCK_STREAM)); | |
| 124 sink.Monitor(client1.get()); | |
| 125 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState()); | |
| 126 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr()); | |
| 127 | |
| 128 scoped_ptr<AsyncSocket> client2(server_->CreateAsyncSocket(loopback.family(), | |
| 129 SOCK_STREAM)); | |
| 130 sink.Monitor(client2.get()); | |
| 131 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState()); | |
| 132 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr()); | |
| 133 | |
| 134 // Create server and listen. | |
| 135 scoped_ptr<AsyncSocket> server( | |
| 136 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); | |
| 137 sink.Monitor(server.get()); | |
| 138 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); | |
| 139 EXPECT_EQ(0, server->Listen(5)); | |
| 140 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState()); | |
| 141 | |
| 142 // Ensure no pending server connections, since we haven't done anything yet. | |
| 143 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); | |
| 144 EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); | |
| 145 EXPECT_TRUE(accept_addr.IsNil()); | |
| 146 | |
| 147 // Attempt first connect to listening socket. | |
| 148 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress())); | |
| 149 EXPECT_FALSE(client1->GetLocalAddress().IsNil()); | |
| 150 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress()); | |
| 151 | |
| 152 // Client is connecting, outcome not yet determined. | |
| 153 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState()); | |
| 154 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_OPEN)); | |
| 155 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_CLOSE)); | |
| 156 | |
| 157 // Server has pending connection, try to accept it (will fail). | |
| 158 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); | |
| 159 // Simulate "::accept" returning an error. | |
| 160 SetFailAccept(true); | |
| 161 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); | |
| 162 EXPECT_FALSE(accepted); | |
| 163 ASSERT_TRUE(accept_addr.IsNil()); | |
| 164 | |
| 165 // Ensure no more pending server connections. | |
| 166 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); | |
| 167 EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); | |
| 168 EXPECT_TRUE(accept_addr.IsNil()); | |
| 169 | |
| 170 // Attempt second connect to listening socket. | |
| 171 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress())); | |
| 172 EXPECT_FALSE(client2->GetLocalAddress().IsNil()); | |
| 173 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress()); | |
| 174 | |
| 175 // Client is connecting, outcome not yet determined. | |
| 176 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState()); | |
| 177 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_OPEN)); | |
| 178 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_CLOSE)); | |
| 179 | |
| 180 // Server has pending connection, try to accept it (will succeed). | |
| 181 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); | |
| 182 SetFailAccept(false); | |
| 183 scoped_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr)); | |
| 184 ASSERT_TRUE(accepted2); | |
| 185 EXPECT_FALSE(accept_addr.IsNil()); | |
| 186 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr); | |
| 187 } | |
| 188 | |
| 189 TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) { | |
| 190 ConnectInternalAcceptError(kIPv4Loopback); | |
| 191 } | |
| 192 | |
| 193 // Crashes on Linux. See webrtc:4923. | |
| 194 #if defined(WEBRTC_LINUX) | |
| 195 #define MAYBE_TestConnectAcceptErrorIPv6 DISABLED_TestConnectAcceptErrorIPv6 | |
| 196 #else | |
| 197 #define MAYBE_TestConnectAcceptErrorIPv6 TestConnectAcceptErrorIPv6 | |
| 198 #endif | |
| 199 TEST_F(PhysicalSocketTest, MAYBE_TestConnectAcceptErrorIPv6) { | |
| 200 ConnectInternalAcceptError(kIPv6Loopback); | |
| 201 } | |
| 202 | |
| 54 // Crashes on Linux. See webrtc:4923. | 203 // Crashes on Linux. See webrtc:4923. |
| 55 #if defined(WEBRTC_LINUX) | 204 #if defined(WEBRTC_LINUX) |
| 56 #define MAYBE_TestConnectFailIPv6 DISABLED_TestConnectFailIPv6 | 205 #define MAYBE_TestConnectFailIPv6 DISABLED_TestConnectFailIPv6 |
| 57 #else | 206 #else |
| 58 #define MAYBE_TestConnectFailIPv6 TestConnectFailIPv6 | 207 #define MAYBE_TestConnectFailIPv6 TestConnectFailIPv6 |
| 59 #endif | 208 #endif |
| 60 TEST_F(PhysicalSocketTest, MAYBE_TestConnectFailIPv6) { | 209 TEST_F(PhysicalSocketTest, MAYBE_TestConnectFailIPv6) { |
| 61 SocketTest::TestConnectFailIPv6(); | 210 SocketTest::TestConnectFailIPv6(); |
| 62 } | 211 } |
| 63 | 212 |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 thread->Start(runnable.get()); | 506 thread->Start(runnable.get()); |
| 358 EXPECT_TRUE(ss_->Wait(1500, true)); | 507 EXPECT_TRUE(ss_->Wait(1500, true)); |
| 359 EXPECT_TRUE(ExpectSignal(SIGTERM)); | 508 EXPECT_TRUE(ExpectSignal(SIGTERM)); |
| 360 EXPECT_EQ(Thread::Current(), signaled_thread_); | 509 EXPECT_EQ(Thread::Current(), signaled_thread_); |
| 361 EXPECT_TRUE(ExpectNone()); | 510 EXPECT_TRUE(ExpectNone()); |
| 362 } | 511 } |
| 363 | 512 |
| 364 #endif | 513 #endif |
| 365 | 514 |
| 366 } // namespace rtc | 515 } // namespace rtc |
| OLD | NEW |