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