| 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/socks_client_socket.h" | 5 #include "net/socket/socks_client_socket.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
| 9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
| 10 #include "net/base/winsock_init.h" | 10 #include "net/base/winsock_init.h" |
| 11 #include "net/dns/host_resolver.h" | 11 #include "net/dns/host_resolver.h" |
| 12 #include "net/dns/mock_host_resolver.h" | 12 #include "net/dns/mock_host_resolver.h" |
| 13 #include "net/log/captured_net_log_entry.h" |
| 13 #include "net/log/net_log.h" | 14 #include "net/log/net_log.h" |
| 14 #include "net/log/net_log_unittest.h" | 15 #include "net/log/net_log_unittest.h" |
| 16 #include "net/log/test_net_log.h" |
| 15 #include "net/socket/client_socket_factory.h" | 17 #include "net/socket/client_socket_factory.h" |
| 16 #include "net/socket/socket_test_util.h" | 18 #include "net/socket/socket_test_util.h" |
| 17 #include "net/socket/tcp_client_socket.h" | 19 #include "net/socket/tcp_client_socket.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 20 | 22 |
| 21 //----------------------------------------------------------------------------- | 23 //----------------------------------------------------------------------------- |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 | 26 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 "localhost", 80, | 154 "localhost", 80, |
| 153 &log); | 155 &log); |
| 154 | 156 |
| 155 // At this state the TCP connection is completed but not the SOCKS handshake. | 157 // At this state the TCP connection is completed but not the SOCKS handshake. |
| 156 EXPECT_TRUE(tcp_sock_->IsConnected()); | 158 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 157 EXPECT_FALSE(user_sock_->IsConnected()); | 159 EXPECT_FALSE(user_sock_->IsConnected()); |
| 158 | 160 |
| 159 int rv = user_sock_->Connect(callback_.callback()); | 161 int rv = user_sock_->Connect(callback_.callback()); |
| 160 EXPECT_EQ(ERR_IO_PENDING, rv); | 162 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 161 | 163 |
| 162 TestNetLog::CapturedEntryList entries; | 164 CapturedNetLogEntry::List entries; |
| 163 log.GetEntries(&entries); | 165 log.GetEntries(&entries); |
| 164 EXPECT_TRUE( | 166 EXPECT_TRUE( |
| 165 LogContainsBeginEvent(entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 167 LogContainsBeginEvent(entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 166 EXPECT_FALSE(user_sock_->IsConnected()); | 168 EXPECT_FALSE(user_sock_->IsConnected()); |
| 167 | 169 |
| 168 rv = callback_.WaitForResult(); | 170 rv = callback_.WaitForResult(); |
| 169 EXPECT_EQ(OK, rv); | 171 EXPECT_EQ(OK, rv); |
| 170 EXPECT_TRUE(user_sock_->IsConnected()); | 172 EXPECT_TRUE(user_sock_->IsConnected()); |
| 171 log.GetEntries(&entries); | 173 log.GetEntries(&entries); |
| 172 EXPECT_TRUE(LogContainsEndEvent( | 174 EXPECT_TRUE(LogContainsEndEvent( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 226 |
| 225 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 227 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 226 data_writes, arraysize(data_writes), | 228 data_writes, arraysize(data_writes), |
| 227 host_resolver_.get(), | 229 host_resolver_.get(), |
| 228 "localhost", 80, | 230 "localhost", 80, |
| 229 &log); | 231 &log); |
| 230 | 232 |
| 231 int rv = user_sock_->Connect(callback_.callback()); | 233 int rv = user_sock_->Connect(callback_.callback()); |
| 232 EXPECT_EQ(ERR_IO_PENDING, rv); | 234 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 233 | 235 |
| 234 TestNetLog::CapturedEntryList entries; | 236 CapturedNetLogEntry::List entries; |
| 235 log.GetEntries(&entries); | 237 log.GetEntries(&entries); |
| 236 EXPECT_TRUE(LogContainsBeginEvent( | 238 EXPECT_TRUE(LogContainsBeginEvent( |
| 237 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 239 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 238 | 240 |
| 239 rv = callback_.WaitForResult(); | 241 rv = callback_.WaitForResult(); |
| 240 EXPECT_EQ(tests[i].fail_code, rv); | 242 EXPECT_EQ(tests[i].fail_code, rv); |
| 241 EXPECT_FALSE(user_sock_->IsConnected()); | 243 EXPECT_FALSE(user_sock_->IsConnected()); |
| 242 EXPECT_TRUE(tcp_sock_->IsConnected()); | 244 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 243 log.GetEntries(&entries); | 245 log.GetEntries(&entries); |
| 244 EXPECT_TRUE(LogContainsEndEvent( | 246 EXPECT_TRUE(LogContainsEndEvent( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 260 TestNetLog log; | 262 TestNetLog log; |
| 261 | 263 |
| 262 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 264 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 263 data_writes, arraysize(data_writes), | 265 data_writes, arraysize(data_writes), |
| 264 host_resolver_.get(), | 266 host_resolver_.get(), |
| 265 "localhost", 80, | 267 "localhost", 80, |
| 266 &log); | 268 &log); |
| 267 | 269 |
| 268 int rv = user_sock_->Connect(callback_.callback()); | 270 int rv = user_sock_->Connect(callback_.callback()); |
| 269 EXPECT_EQ(ERR_IO_PENDING, rv); | 271 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 270 TestNetLog::CapturedEntryList entries; | 272 CapturedNetLogEntry::List entries; |
| 271 log.GetEntries(&entries); | 273 log.GetEntries(&entries); |
| 272 EXPECT_TRUE(LogContainsBeginEvent( | 274 EXPECT_TRUE(LogContainsBeginEvent( |
| 273 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 275 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 274 | 276 |
| 275 rv = callback_.WaitForResult(); | 277 rv = callback_.WaitForResult(); |
| 276 EXPECT_EQ(OK, rv); | 278 EXPECT_EQ(OK, rv); |
| 277 EXPECT_TRUE(user_sock_->IsConnected()); | 279 EXPECT_TRUE(user_sock_->IsConnected()); |
| 278 log.GetEntries(&entries); | 280 log.GetEntries(&entries); |
| 279 EXPECT_TRUE(LogContainsEndEvent( | 281 EXPECT_TRUE(LogContainsEndEvent( |
| 280 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 282 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 298 TestNetLog log; | 300 TestNetLog log; |
| 299 | 301 |
| 300 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 302 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 301 data_writes, arraysize(data_writes), | 303 data_writes, arraysize(data_writes), |
| 302 host_resolver_.get(), | 304 host_resolver_.get(), |
| 303 "localhost", 80, | 305 "localhost", 80, |
| 304 &log); | 306 &log); |
| 305 | 307 |
| 306 int rv = user_sock_->Connect(callback_.callback()); | 308 int rv = user_sock_->Connect(callback_.callback()); |
| 307 EXPECT_EQ(ERR_IO_PENDING, rv); | 309 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 308 TestNetLog::CapturedEntryList entries; | 310 CapturedNetLogEntry::List entries; |
| 309 log.GetEntries(&entries); | 311 log.GetEntries(&entries); |
| 310 EXPECT_TRUE(LogContainsBeginEvent( | 312 EXPECT_TRUE(LogContainsBeginEvent( |
| 311 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 313 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 312 | 314 |
| 313 rv = callback_.WaitForResult(); | 315 rv = callback_.WaitForResult(); |
| 314 EXPECT_EQ(OK, rv); | 316 EXPECT_EQ(OK, rv); |
| 315 EXPECT_TRUE(user_sock_->IsConnected()); | 317 EXPECT_TRUE(user_sock_->IsConnected()); |
| 316 log.GetEntries(&entries); | 318 log.GetEntries(&entries); |
| 317 EXPECT_TRUE(LogContainsEndEvent( | 319 EXPECT_TRUE(LogContainsEndEvent( |
| 318 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 320 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 330 TestNetLog log; | 332 TestNetLog log; |
| 331 | 333 |
| 332 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 334 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 333 data_writes, arraysize(data_writes), | 335 data_writes, arraysize(data_writes), |
| 334 host_resolver_.get(), | 336 host_resolver_.get(), |
| 335 "localhost", 80, | 337 "localhost", 80, |
| 336 &log); | 338 &log); |
| 337 | 339 |
| 338 int rv = user_sock_->Connect(callback_.callback()); | 340 int rv = user_sock_->Connect(callback_.callback()); |
| 339 EXPECT_EQ(ERR_IO_PENDING, rv); | 341 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 340 TestNetLog::CapturedEntryList entries; | 342 CapturedNetLogEntry::List entries; |
| 341 log.GetEntries(&entries); | 343 log.GetEntries(&entries); |
| 342 EXPECT_TRUE(LogContainsBeginEvent( | 344 EXPECT_TRUE(LogContainsBeginEvent( |
| 343 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 345 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 344 | 346 |
| 345 rv = callback_.WaitForResult(); | 347 rv = callback_.WaitForResult(); |
| 346 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); | 348 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
| 347 EXPECT_FALSE(user_sock_->IsConnected()); | 349 EXPECT_FALSE(user_sock_->IsConnected()); |
| 348 log.GetEntries(&entries); | 350 log.GetEntries(&entries); |
| 349 EXPECT_TRUE(LogContainsEndEvent( | 351 EXPECT_TRUE(LogContainsEndEvent( |
| 350 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 352 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
| 351 } | 353 } |
| 352 | 354 |
| 353 // Tries to connect to an unknown hostname. Should fail rather than | 355 // Tries to connect to an unknown hostname. Should fail rather than |
| 354 // falling back to SOCKS4a. | 356 // falling back to SOCKS4a. |
| 355 TEST_F(SOCKSClientSocketTest, FailedDNS) { | 357 TEST_F(SOCKSClientSocketTest, FailedDNS) { |
| 356 const char hostname[] = "unresolved.ipv4.address"; | 358 const char hostname[] = "unresolved.ipv4.address"; |
| 357 | 359 |
| 358 host_resolver_->rules()->AddSimulatedFailure(hostname); | 360 host_resolver_->rules()->AddSimulatedFailure(hostname); |
| 359 | 361 |
| 360 TestNetLog log; | 362 TestNetLog log; |
| 361 | 363 |
| 362 user_sock_ = BuildMockSocket(NULL, 0, | 364 user_sock_ = BuildMockSocket(NULL, 0, |
| 363 NULL, 0, | 365 NULL, 0, |
| 364 host_resolver_.get(), | 366 host_resolver_.get(), |
| 365 hostname, 80, | 367 hostname, 80, |
| 366 &log); | 368 &log); |
| 367 | 369 |
| 368 int rv = user_sock_->Connect(callback_.callback()); | 370 int rv = user_sock_->Connect(callback_.callback()); |
| 369 EXPECT_EQ(ERR_IO_PENDING, rv); | 371 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 370 TestNetLog::CapturedEntryList entries; | 372 CapturedNetLogEntry::List entries; |
| 371 log.GetEntries(&entries); | 373 log.GetEntries(&entries); |
| 372 EXPECT_TRUE(LogContainsBeginEvent( | 374 EXPECT_TRUE(LogContainsBeginEvent( |
| 373 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 375 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 374 | 376 |
| 375 rv = callback_.WaitForResult(); | 377 rv = callback_.WaitForResult(); |
| 376 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 378 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 377 EXPECT_FALSE(user_sock_->IsConnected()); | 379 EXPECT_FALSE(user_sock_->IsConnected()); |
| 378 log.GetEntries(&entries); | 380 log.GetEntries(&entries); |
| 379 EXPECT_TRUE(LogContainsEndEvent( | 381 EXPECT_TRUE(LogContainsEndEvent( |
| 380 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 382 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 NULL, 0, | 443 NULL, 0, |
| 442 host_resolver.get(), | 444 host_resolver.get(), |
| 443 kHostName, 80, | 445 kHostName, 80, |
| 444 NULL); | 446 NULL); |
| 445 | 447 |
| 446 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, | 448 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, |
| 447 callback_.GetResult(user_sock_->Connect(callback_.callback()))); | 449 callback_.GetResult(user_sock_->Connect(callback_.callback()))); |
| 448 } | 450 } |
| 449 | 451 |
| 450 } // namespace net | 452 } // namespace net |
| OLD | NEW |