OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/socks5_client_socket.h" | 5 #include "net/socket/socks5_client_socket.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <ws2tcpip.h> | 10 #include <ws2tcpip.h> |
11 #elif defined(OS_POSIX) | 11 #elif defined(OS_POSIX) |
12 #include <netdb.h> | 12 #include <netdb.h> |
13 #endif | 13 #endif |
14 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" |
15 #include "net/base/host_resolver_unittest.h" | 15 #include "net/base/mock_host_resolver.h" |
16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
17 #include "net/base/winsock_init.h" | 17 #include "net/base/winsock_init.h" |
18 #include "net/socket/client_socket_factory.h" | 18 #include "net/socket/client_socket_factory.h" |
19 #include "net/socket/socket_test_util.h" | 19 #include "net/socket/socket_test_util.h" |
20 #include "net/socket/tcp_client_socket.h" | 20 #include "net/socket/tcp_client_socket.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
23 | 23 |
24 //----------------------------------------------------------------------------- | 24 //----------------------------------------------------------------------------- |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 // Base class to test SOCKS5ClientSocket | 28 // Base class to test SOCKS5ClientSocket |
29 class SOCKS5ClientSocketTest : public PlatformTest { | 29 class SOCKS5ClientSocketTest : public PlatformTest { |
30 public: | 30 public: |
31 SOCKS5ClientSocketTest(); | 31 SOCKS5ClientSocketTest(); |
32 // Create a SOCKSClientSocket on top of a MockSocket. | 32 // Create a SOCKSClientSocket on top of a MockSocket. |
33 SOCKS5ClientSocket* BuildMockSocket(MockRead reads[], | 33 SOCKS5ClientSocket* BuildMockSocket(MockRead reads[], |
34 MockWrite writes[], | 34 MockWrite writes[], |
35 const std::string& hostname, | 35 const std::string& hostname, |
36 int port); | 36 int port); |
37 virtual void SetUp(); | 37 virtual void SetUp(); |
38 | 38 |
39 protected: | 39 protected: |
40 const uint16 kNwPort; | 40 const uint16 kNwPort; |
41 scoped_ptr<SOCKS5ClientSocket> user_sock_; | 41 scoped_ptr<SOCKS5ClientSocket> user_sock_; |
42 AddressList address_list_; | 42 AddressList address_list_; |
43 ClientSocket* tcp_sock_; | 43 ClientSocket* tcp_sock_; |
44 ScopedHostMapper host_mapper_; | |
45 TestCompletionCallback callback_; | 44 TestCompletionCallback callback_; |
46 scoped_refptr<RuleBasedHostMapper> mapper_; | 45 scoped_refptr<MockHostResolver> host_resolver_; |
47 scoped_refptr<HostResolver> host_resolver_; | |
48 scoped_ptr<MockSocket> mock_socket_; | 46 scoped_ptr<MockSocket> mock_socket_; |
49 | 47 |
50 private: | 48 private: |
51 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocketTest); | 49 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocketTest); |
52 }; | 50 }; |
53 | 51 |
54 SOCKS5ClientSocketTest::SOCKS5ClientSocketTest() | 52 SOCKS5ClientSocketTest::SOCKS5ClientSocketTest() |
55 : kNwPort(htons(80)), host_resolver_(new HostResolver(0, 0)) { | 53 : kNwPort(htons(80)), host_resolver_(new MockHostResolver) { |
56 } | 54 } |
57 | 55 |
58 // Set up platform before every test case | 56 // Set up platform before every test case |
59 void SOCKS5ClientSocketTest::SetUp() { | 57 void SOCKS5ClientSocketTest::SetUp() { |
60 PlatformTest::SetUp(); | 58 PlatformTest::SetUp(); |
61 | 59 |
62 // Resolve the "localhost" AddressList used by the TCP connection to connect. | 60 // Resolve the "localhost" AddressList used by the TCP connection to connect. |
63 scoped_refptr<HostResolver> resolver = new HostResolver(); | |
64 HostResolver::RequestInfo info("www.socks-proxy.com", 1080); | 61 HostResolver::RequestInfo info("www.socks-proxy.com", 1080); |
65 int rv = resolver->Resolve(info, &address_list_, NULL, NULL); | 62 int rv = host_resolver_->Resolve(info, &address_list_, NULL, NULL); |
66 ASSERT_EQ(OK, rv); | 63 ASSERT_EQ(OK, rv); |
67 | |
68 // Create a new host mapping for the duration of this test case only. | |
69 mapper_ = new RuleBasedHostMapper(); | |
70 host_mapper_.Init(mapper_); | |
71 } | 64 } |
72 | 65 |
73 SOCKS5ClientSocket* SOCKS5ClientSocketTest::BuildMockSocket( | 66 SOCKS5ClientSocket* SOCKS5ClientSocketTest::BuildMockSocket( |
74 MockRead reads[], | 67 MockRead reads[], |
75 MockWrite writes[], | 68 MockWrite writes[], |
76 const std::string& hostname, | 69 const std::string& hostname, |
77 int port) { | 70 int port) { |
78 | 71 |
79 TestCompletionCallback callback; | 72 TestCompletionCallback callback; |
80 mock_socket_.reset(new StaticMockSocket(reads, writes)); | 73 mock_socket_.reset(new StaticMockSocket(reads, writes)); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 user_sock_->Disconnect(); | 139 user_sock_->Disconnect(); |
147 EXPECT_FALSE(tcp_sock_->IsConnected()); | 140 EXPECT_FALSE(tcp_sock_->IsConnected()); |
148 EXPECT_FALSE(user_sock_->IsConnected()); | 141 EXPECT_FALSE(user_sock_->IsConnected()); |
149 } | 142 } |
150 | 143 |
151 // Tries to connect to a DNS which fails domain lookup. | 144 // Tries to connect to a DNS which fails domain lookup. |
152 TEST_F(SOCKS5ClientSocketTest, FailedDNS) { | 145 TEST_F(SOCKS5ClientSocketTest, FailedDNS) { |
153 const std::string hostname = "unresolved.ipv4.address"; | 146 const std::string hostname = "unresolved.ipv4.address"; |
154 const char kSOCKS5DomainRequest[] = { 0x05, 0x01, 0x00, 0x03 }; | 147 const char kSOCKS5DomainRequest[] = { 0x05, 0x01, 0x00, 0x03 }; |
155 | 148 |
156 mapper_->AddSimulatedFailure(hostname.c_str()); | 149 host_resolver_->rules()->AddSimulatedFailure(hostname.c_str()); |
157 | 150 |
158 std::string request(kSOCKS5DomainRequest, | 151 std::string request(kSOCKS5DomainRequest, |
159 arraysize(kSOCKS5DomainRequest)); | 152 arraysize(kSOCKS5DomainRequest)); |
160 request.push_back(hostname.size()); | 153 request.push_back(hostname.size()); |
161 request.append(hostname); | 154 request.append(hostname); |
162 request.append(reinterpret_cast<const char*>(&kNwPort), sizeof(kNwPort)); | 155 request.append(reinterpret_cast<const char*>(&kNwPort), sizeof(kNwPort)); |
163 | 156 |
164 MockWrite data_writes[] = { | 157 MockWrite data_writes[] = { |
165 MockWrite(false, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), | 158 MockWrite(false, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
166 MockWrite(false, request.data(), request.size()) }; | 159 MockWrite(false, request.data(), request.size()) }; |
(...skipping 12 matching lines...) Expand all Loading... |
179 user_sock_->address_type_); | 172 user_sock_->address_type_); |
180 } | 173 } |
181 | 174 |
182 // Tries to connect to a domain that resolves to IPv6. | 175 // Tries to connect to a domain that resolves to IPv6. |
183 TEST_F(SOCKS5ClientSocketTest, IPv6Domain) { | 176 TEST_F(SOCKS5ClientSocketTest, IPv6Domain) { |
184 const std::string hostname = "an.ipv6.address"; | 177 const std::string hostname = "an.ipv6.address"; |
185 const char kSOCKS5IPv6Request[] = { 0x05, 0x01, 0x00, 0x04 }; | 178 const char kSOCKS5IPv6Request[] = { 0x05, 0x01, 0x00, 0x04 }; |
186 const uint8 ipv6_addr[] = { 0x20, 0x01, 0x0d, 0xb8, 0x87, 0x14, 0x3a, 0x90, | 179 const uint8 ipv6_addr[] = { 0x20, 0x01, 0x0d, 0xb8, 0x87, 0x14, 0x3a, 0x90, |
187 0x00, 0x00, 0x00, 0x00, 0x00, 0x000, 0x00, 0x12 }; | 180 0x00, 0x00, 0x00, 0x00, 0x00, 0x000, 0x00, 0x12 }; |
188 | 181 |
189 mapper_->AddRule(hostname.c_str(), "2001:db8:8714:3a90::12"); | 182 host_resolver_->rules()->AddRule(hostname, "2001:db8:8714:3a90::12"); |
190 | 183 |
191 AddressList address_list; | 184 AddressList address_list; |
192 scoped_refptr<HostResolver> resolver = new HostResolver(); | |
193 HostResolver::RequestInfo info(hostname, 80); | 185 HostResolver::RequestInfo info(hostname, 80); |
194 int rv = resolver->Resolve(info, &address_list, NULL, NULL); | 186 int rv = host_resolver_->Resolve(info, &address_list, NULL, NULL); |
195 if (rv != OK || !address_list.head()) { | 187 if (rv != OK || !address_list.head()) { |
196 // This machine does not support IPv6. We skip this test altogether. | 188 // This machine does not support IPv6. We skip this test altogether. |
197 // TODO(arindam): create a MockIPv6HostResolver to manually | 189 // TODO(arindam): create a MockIPv6HostResolver to manually |
198 // populate the |address_list| in case of a machine with no IPv6 suppport. | 190 // populate the |address_list| in case of a machine with no IPv6 suppport. |
199 return; | 191 return; |
200 } | 192 } |
201 | 193 |
202 std::string request(kSOCKS5IPv6Request, | 194 std::string request(kSOCKS5IPv6Request, |
203 arraysize(kSOCKS5IPv6Request)); | 195 arraysize(kSOCKS5IPv6Request)); |
204 request.append(reinterpret_cast<const char*>(&ipv6_addr), sizeof(ipv6_addr)); | 196 request.append(reinterpret_cast<const char*>(&ipv6_addr), sizeof(ipv6_addr)); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 int rv = user_sock_->Connect(&callback_); | 289 int rv = user_sock_->Connect(&callback_); |
298 EXPECT_EQ(ERR_IO_PENDING, rv); | 290 EXPECT_EQ(ERR_IO_PENDING, rv); |
299 rv = callback_.WaitForResult(); | 291 rv = callback_.WaitForResult(); |
300 EXPECT_EQ(OK, rv); | 292 EXPECT_EQ(OK, rv); |
301 EXPECT_TRUE(user_sock_->IsConnected()); | 293 EXPECT_TRUE(user_sock_->IsConnected()); |
302 } | 294 } |
303 } | 295 } |
304 | 296 |
305 } // namespace net | 297 } // namespace net |
306 | 298 |
OLD | NEW |