| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
| 12 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
| 13 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 14 #include "net/base/winsock_init.h" | 14 #include "net/base/winsock_init.h" |
| 15 #include "net/dns/mock_host_resolver.h" | 15 #include "net/dns/mock_host_resolver.h" |
| 16 #include "net/log/captured_net_log_entry.h" |
| 16 #include "net/log/net_log.h" | 17 #include "net/log/net_log.h" |
| 17 #include "net/log/net_log_unittest.h" | 18 #include "net/log/net_log_unittest.h" |
| 19 #include "net/log/test_net_log.h" |
| 18 #include "net/socket/client_socket_factory.h" | 20 #include "net/socket/client_socket_factory.h" |
| 19 #include "net/socket/socket_test_util.h" | 21 #include "net/socket/socket_test_util.h" |
| 20 #include "net/socket/tcp_client_socket.h" | 22 #include "net/socket/tcp_client_socket.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 23 | 25 |
| 24 //----------------------------------------------------------------------------- | 26 //----------------------------------------------------------------------------- |
| 25 | 27 |
| 26 namespace net { | 28 namespace net { |
| 27 | 29 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 "localhost", 80, &net_log_); | 141 "localhost", 80, &net_log_); |
| 140 | 142 |
| 141 // At this state the TCP connection is completed but not the SOCKS handshake. | 143 // At this state the TCP connection is completed but not the SOCKS handshake. |
| 142 EXPECT_TRUE(tcp_sock_->IsConnected()); | 144 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 143 EXPECT_FALSE(user_sock_->IsConnected()); | 145 EXPECT_FALSE(user_sock_->IsConnected()); |
| 144 | 146 |
| 145 int rv = user_sock_->Connect(callback_.callback()); | 147 int rv = user_sock_->Connect(callback_.callback()); |
| 146 EXPECT_EQ(ERR_IO_PENDING, rv); | 148 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 147 EXPECT_FALSE(user_sock_->IsConnected()); | 149 EXPECT_FALSE(user_sock_->IsConnected()); |
| 148 | 150 |
| 149 TestNetLog::CapturedEntryList net_log_entries; | 151 CapturedNetLogEntry::List net_log_entries; |
| 150 net_log_.GetEntries(&net_log_entries); | 152 net_log_.GetEntries(&net_log_entries); |
| 151 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, | 153 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, |
| 152 NetLog::TYPE_SOCKS5_CONNECT)); | 154 NetLog::TYPE_SOCKS5_CONNECT)); |
| 153 | 155 |
| 154 rv = callback_.WaitForResult(); | 156 rv = callback_.WaitForResult(); |
| 155 | 157 |
| 156 EXPECT_EQ(OK, rv); | 158 EXPECT_EQ(OK, rv); |
| 157 EXPECT_TRUE(user_sock_->IsConnected()); | 159 EXPECT_TRUE(user_sock_->IsConnected()); |
| 158 | 160 |
| 159 net_log_.GetEntries(&net_log_entries); | 161 net_log_.GetEntries(&net_log_entries); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 MockWrite(ASYNC, kOkRequest, arraysize(kOkRequest)) }; | 265 MockWrite(ASYNC, kOkRequest, arraysize(kOkRequest)) }; |
| 264 MockRead data_reads[] = { | 266 MockRead data_reads[] = { |
| 265 MockRead(ASYNC, kSOCKS5GreetResponse, kSOCKS5GreetResponseLength), | 267 MockRead(ASYNC, kSOCKS5GreetResponse, kSOCKS5GreetResponseLength), |
| 266 MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength) }; | 268 MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength) }; |
| 267 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 269 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 268 data_writes, arraysize(data_writes), | 270 data_writes, arraysize(data_writes), |
| 269 hostname, 80, &net_log_); | 271 hostname, 80, &net_log_); |
| 270 int rv = user_sock_->Connect(callback_.callback()); | 272 int rv = user_sock_->Connect(callback_.callback()); |
| 271 EXPECT_EQ(ERR_IO_PENDING, rv); | 273 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 272 | 274 |
| 273 TestNetLog::CapturedEntryList net_log_entries; | 275 CapturedNetLogEntry::List net_log_entries; |
| 274 net_log_.GetEntries(&net_log_entries); | 276 net_log_.GetEntries(&net_log_entries); |
| 275 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, | 277 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, |
| 276 NetLog::TYPE_SOCKS5_CONNECT)); | 278 NetLog::TYPE_SOCKS5_CONNECT)); |
| 277 | 279 |
| 278 rv = callback_.WaitForResult(); | 280 rv = callback_.WaitForResult(); |
| 279 EXPECT_EQ(OK, rv); | 281 EXPECT_EQ(OK, rv); |
| 280 EXPECT_TRUE(user_sock_->IsConnected()); | 282 EXPECT_TRUE(user_sock_->IsConnected()); |
| 281 | 283 |
| 282 net_log_.GetEntries(&net_log_entries); | 284 net_log_.GetEntries(&net_log_entries); |
| 283 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, | 285 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 294 MockRead data_reads[] = { | 296 MockRead data_reads[] = { |
| 295 MockRead(ASYNC, partial1, arraysize(partial1)), | 297 MockRead(ASYNC, partial1, arraysize(partial1)), |
| 296 MockRead(ASYNC, partial2, arraysize(partial2)), | 298 MockRead(ASYNC, partial2, arraysize(partial2)), |
| 297 MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength) }; | 299 MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength) }; |
| 298 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 300 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 299 data_writes, arraysize(data_writes), | 301 data_writes, arraysize(data_writes), |
| 300 hostname, 80, &net_log_); | 302 hostname, 80, &net_log_); |
| 301 int rv = user_sock_->Connect(callback_.callback()); | 303 int rv = user_sock_->Connect(callback_.callback()); |
| 302 EXPECT_EQ(ERR_IO_PENDING, rv); | 304 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 303 | 305 |
| 304 TestNetLog::CapturedEntryList net_log_entries; | 306 CapturedNetLogEntry::List net_log_entries; |
| 305 net_log_.GetEntries(&net_log_entries); | 307 net_log_.GetEntries(&net_log_entries); |
| 306 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, | 308 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, |
| 307 NetLog::TYPE_SOCKS5_CONNECT)); | 309 NetLog::TYPE_SOCKS5_CONNECT)); |
| 308 rv = callback_.WaitForResult(); | 310 rv = callback_.WaitForResult(); |
| 309 EXPECT_EQ(OK, rv); | 311 EXPECT_EQ(OK, rv); |
| 310 EXPECT_TRUE(user_sock_->IsConnected()); | 312 EXPECT_TRUE(user_sock_->IsConnected()); |
| 311 net_log_.GetEntries(&net_log_entries); | 313 net_log_.GetEntries(&net_log_entries); |
| 312 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, | 314 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, |
| 313 NetLog::TYPE_SOCKS5_CONNECT)); | 315 NetLog::TYPE_SOCKS5_CONNECT)); |
| 314 } | 316 } |
| 315 | 317 |
| 316 // Test for partial handshake request write. | 318 // Test for partial handshake request write. |
| 317 { | 319 { |
| 318 const int kSplitPoint = 3; // Break handshake write into two parts. | 320 const int kSplitPoint = 3; // Break handshake write into two parts. |
| 319 MockWrite data_writes[] = { | 321 MockWrite data_writes[] = { |
| 320 MockWrite(ASYNC, kSOCKS5GreetRequest, kSOCKS5GreetRequestLength), | 322 MockWrite(ASYNC, kSOCKS5GreetRequest, kSOCKS5GreetRequestLength), |
| 321 MockWrite(ASYNC, kOkRequest, kSplitPoint), | 323 MockWrite(ASYNC, kOkRequest, kSplitPoint), |
| 322 MockWrite(ASYNC, kOkRequest + kSplitPoint, | 324 MockWrite(ASYNC, kOkRequest + kSplitPoint, |
| 323 arraysize(kOkRequest) - kSplitPoint) | 325 arraysize(kOkRequest) - kSplitPoint) |
| 324 }; | 326 }; |
| 325 MockRead data_reads[] = { | 327 MockRead data_reads[] = { |
| 326 MockRead(ASYNC, kSOCKS5GreetResponse, kSOCKS5GreetResponseLength), | 328 MockRead(ASYNC, kSOCKS5GreetResponse, kSOCKS5GreetResponseLength), |
| 327 MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength) }; | 329 MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength) }; |
| 328 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 330 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 329 data_writes, arraysize(data_writes), | 331 data_writes, arraysize(data_writes), |
| 330 hostname, 80, &net_log_); | 332 hostname, 80, &net_log_); |
| 331 int rv = user_sock_->Connect(callback_.callback()); | 333 int rv = user_sock_->Connect(callback_.callback()); |
| 332 EXPECT_EQ(ERR_IO_PENDING, rv); | 334 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 333 TestNetLog::CapturedEntryList net_log_entries; | 335 CapturedNetLogEntry::List net_log_entries; |
| 334 net_log_.GetEntries(&net_log_entries); | 336 net_log_.GetEntries(&net_log_entries); |
| 335 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, | 337 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, |
| 336 NetLog::TYPE_SOCKS5_CONNECT)); | 338 NetLog::TYPE_SOCKS5_CONNECT)); |
| 337 rv = callback_.WaitForResult(); | 339 rv = callback_.WaitForResult(); |
| 338 EXPECT_EQ(OK, rv); | 340 EXPECT_EQ(OK, rv); |
| 339 EXPECT_TRUE(user_sock_->IsConnected()); | 341 EXPECT_TRUE(user_sock_->IsConnected()); |
| 340 net_log_.GetEntries(&net_log_entries); | 342 net_log_.GetEntries(&net_log_entries); |
| 341 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, | 343 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, |
| 342 NetLog::TYPE_SOCKS5_CONNECT)); | 344 NetLog::TYPE_SOCKS5_CONNECT)); |
| 343 } | 345 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 354 MockRead(ASYNC, kSOCKS5OkResponse, kSplitPoint), | 356 MockRead(ASYNC, kSOCKS5OkResponse, kSplitPoint), |
| 355 MockRead(ASYNC, kSOCKS5OkResponse + kSplitPoint, | 357 MockRead(ASYNC, kSOCKS5OkResponse + kSplitPoint, |
| 356 kSOCKS5OkResponseLength - kSplitPoint) | 358 kSOCKS5OkResponseLength - kSplitPoint) |
| 357 }; | 359 }; |
| 358 | 360 |
| 359 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 361 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 360 data_writes, arraysize(data_writes), | 362 data_writes, arraysize(data_writes), |
| 361 hostname, 80, &net_log_); | 363 hostname, 80, &net_log_); |
| 362 int rv = user_sock_->Connect(callback_.callback()); | 364 int rv = user_sock_->Connect(callback_.callback()); |
| 363 EXPECT_EQ(ERR_IO_PENDING, rv); | 365 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 364 TestNetLog::CapturedEntryList net_log_entries; | 366 CapturedNetLogEntry::List net_log_entries; |
| 365 net_log_.GetEntries(&net_log_entries); | 367 net_log_.GetEntries(&net_log_entries); |
| 366 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, | 368 EXPECT_TRUE(LogContainsBeginEvent(net_log_entries, 0, |
| 367 NetLog::TYPE_SOCKS5_CONNECT)); | 369 NetLog::TYPE_SOCKS5_CONNECT)); |
| 368 rv = callback_.WaitForResult(); | 370 rv = callback_.WaitForResult(); |
| 369 EXPECT_EQ(OK, rv); | 371 EXPECT_EQ(OK, rv); |
| 370 EXPECT_TRUE(user_sock_->IsConnected()); | 372 EXPECT_TRUE(user_sock_->IsConnected()); |
| 371 net_log_.GetEntries(&net_log_entries); | 373 net_log_.GetEntries(&net_log_entries); |
| 372 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, | 374 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, |
| 373 NetLog::TYPE_SOCKS5_CONNECT)); | 375 NetLog::TYPE_SOCKS5_CONNECT)); |
| 374 } | 376 } |
| 375 } | 377 } |
| 376 | 378 |
| 377 } // namespace | 379 } // namespace |
| 378 | 380 |
| 379 } // namespace net | 381 } // namespace net |
| OLD | NEW |