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 <algorithm> |
7 #include <map> | 8 #include <map> |
8 | 9 |
9 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
10 #include "net/base/load_log.h" | 11 #include "net/base/load_log.h" |
11 #include "net/base/load_log_unittest.h" | 12 #include "net/base/load_log_unittest.h" |
12 #include "net/base/mock_host_resolver.h" | 13 #include "net/base/mock_host_resolver.h" |
13 #include "net/base/sys_addrinfo.h" | 14 #include "net/base/sys_addrinfo.h" |
14 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
15 #include "net/base/winsock_init.h" | 16 #include "net/base/winsock_init.h" |
16 #include "net/socket/client_socket_factory.h" | 17 #include "net/socket/client_socket_factory.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 EXPECT_EQ(ERR_IO_PENDING, rv); | 147 EXPECT_EQ(ERR_IO_PENDING, rv); |
147 rv = callback_.WaitForResult(); | 148 rv = callback_.WaitForResult(); |
148 EXPECT_EQ(static_cast<int>(payload_read.size()), rv); | 149 EXPECT_EQ(static_cast<int>(payload_read.size()), rv); |
149 EXPECT_EQ(payload_read, std::string(buffer->data(), payload_read.size())); | 150 EXPECT_EQ(payload_read, std::string(buffer->data(), payload_read.size())); |
150 | 151 |
151 user_sock_->Disconnect(); | 152 user_sock_->Disconnect(); |
152 EXPECT_FALSE(tcp_sock_->IsConnected()); | 153 EXPECT_FALSE(tcp_sock_->IsConnected()); |
153 EXPECT_FALSE(user_sock_->IsConnected()); | 154 EXPECT_FALSE(user_sock_->IsConnected()); |
154 } | 155 } |
155 | 156 |
156 // Connect to a domain, making sure to defer the host resolving to the proxy | 157 // Test that you can call Connect() again after having called Disconnect(). |
157 // server. | 158 TEST_F(SOCKS5ClientSocketTest, ConnectAndDisconnectTwice) { |
158 TEST_F(SOCKS5ClientSocketTest, ResolveHostsProxySide) { | |
159 const std::string hostname = "my-host-name"; | 159 const std::string hostname = "my-host-name"; |
160 const char kSOCKS5DomainRequest[] = { | 160 const char kSOCKS5DomainRequest[] = { |
161 0x05, // VER | 161 0x05, // VER |
162 0x01, // CMD | 162 0x01, // CMD |
163 0x00, // RSV | 163 0x00, // RSV |
164 0x03, // ATYPE | 164 0x03, // ATYPE |
165 }; | 165 }; |
166 | 166 |
167 std::string request(kSOCKS5DomainRequest, | 167 std::string request(kSOCKS5DomainRequest, arraysize(kSOCKS5DomainRequest)); |
168 arraysize(kSOCKS5DomainRequest)); | |
169 request.push_back(hostname.size()); | 168 request.push_back(hostname.size()); |
170 request.append(hostname); | 169 request.append(hostname); |
171 request.append(reinterpret_cast<const char*>(&kNwPort), sizeof(kNwPort)); | 170 request.append(reinterpret_cast<const char*>(&kNwPort), sizeof(kNwPort)); |
172 | 171 |
173 MockWrite data_writes[] = { | 172 for (int i = 0; i < 2; ++i) { |
174 MockWrite(false, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), | 173 MockWrite data_writes[] = { |
175 MockWrite(false, request.data(), request.size()) | 174 MockWrite(false, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
176 }; | 175 MockWrite(false, request.data(), request.size()) |
177 MockRead data_reads[] = { | 176 }; |
178 MockRead(false, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), | 177 MockRead data_reads[] = { |
179 MockRead(false, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) | 178 MockRead(false, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
180 }; | 179 MockRead(false, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) |
| 180 }; |
181 | 181 |
182 user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80)); | 182 user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80)); |
183 | 183 |
184 int rv = user_sock_->Connect(&callback_, NULL); | 184 int rv = user_sock_->Connect(&callback_, NULL); |
185 EXPECT_EQ(OK, rv); | 185 EXPECT_EQ(OK, rv); |
186 EXPECT_TRUE(user_sock_->IsConnected()); | 186 EXPECT_TRUE(user_sock_->IsConnected()); |
| 187 |
| 188 user_sock_->Disconnect(); |
| 189 EXPECT_FALSE(user_sock_->IsConnected()); |
| 190 } |
| 191 } |
| 192 |
| 193 // Test that we fail trying to connect to a hosname longer than 255 bytes. |
| 194 TEST_F(SOCKS5ClientSocketTest, LargeHostNameFails) { |
| 195 // Create a string of length 256, where each character is 'x'. |
| 196 std::string large_host_name; |
| 197 std::fill_n(std::back_inserter(large_host_name), 256, 'x'); |
| 198 |
| 199 // Create a SOCKS socket, with mock transport socket. |
| 200 MockWrite data_writes[] = {MockWrite()}; |
| 201 MockRead data_reads[] = {MockRead()}; |
| 202 user_sock_.reset(BuildMockSocket(data_reads, data_writes, |
| 203 large_host_name, 80)); |
| 204 |
| 205 // Try to connect -- should fail (without having read/written anything to |
| 206 // the transport socket first) because the hostname is too long. |
| 207 TestCompletionCallback callback; |
| 208 int rv = user_sock_->Connect(&callback, NULL); |
| 209 EXPECT_EQ(ERR_INVALID_URL, rv); |
187 } | 210 } |
188 | 211 |
189 TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) { | 212 TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) { |
190 const std::string hostname = "www.google.com"; | 213 const std::string hostname = "www.google.com"; |
191 | 214 |
192 const char kSOCKS5OkRequest[] = { | 215 const char kSOCKS5OkRequest[] = { |
193 0x05, // Version | 216 0x05, // Version |
194 0x01, // Command (CONNECT) | 217 0x01, // Command (CONNECT) |
195 0x00, // Reserved. | 218 0x00, // Reserved. |
196 0x03, // Address type (DOMAINNAME). | 219 0x03, // Address type (DOMAINNAME). |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 EXPECT_EQ(OK, rv); | 319 EXPECT_EQ(OK, rv); |
297 EXPECT_TRUE(user_sock_->IsConnected()); | 320 EXPECT_TRUE(user_sock_->IsConnected()); |
298 EXPECT_TRUE(LogContains( | 321 EXPECT_TRUE(LogContains( |
299 *log, -1, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_END)); | 322 *log, -1, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_END)); |
300 } | 323 } |
301 } | 324 } |
302 | 325 |
303 } // namespace | 326 } // namespace |
304 | 327 |
305 } // namespace net | 328 } // namespace net |
OLD | NEW |