| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 13 #include "net/base/winsock_init.h" | 13 #include "net/base/winsock_init.h" |
| 14 #include "net/dns/host_resolver.h" | 14 #include "net/dns/host_resolver.h" |
| 15 #include "net/dns/mock_host_resolver.h" | 15 #include "net/dns/mock_host_resolver.h" |
| 16 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
| 17 #include "net/log/net_log_event_type.h" |
| 17 #include "net/log/test_net_log.h" | 18 #include "net/log/test_net_log.h" |
| 18 #include "net/log/test_net_log_entry.h" | 19 #include "net/log/test_net_log_entry.h" |
| 19 #include "net/log/test_net_log_util.h" | 20 #include "net/log/test_net_log_util.h" |
| 20 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
| 21 #include "net/socket/socket_test_util.h" | 22 #include "net/socket/socket_test_util.h" |
| 22 #include "net/socket/tcp_client_socket.h" | 23 #include "net/socket/tcp_client_socket.h" |
| 23 #include "net/test/gtest_util.h" | 24 #include "net/test/gtest_util.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "testing/platform_test.h" | 27 #include "testing/platform_test.h" |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // At this state the TCP connection is completed but not the SOCKS handshake. | 180 // At this state the TCP connection is completed but not the SOCKS handshake. |
| 180 EXPECT_TRUE(tcp_sock_->IsConnected()); | 181 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 181 EXPECT_FALSE(user_sock_->IsConnected()); | 182 EXPECT_FALSE(user_sock_->IsConnected()); |
| 182 | 183 |
| 183 int rv = user_sock_->Connect(callback_.callback()); | 184 int rv = user_sock_->Connect(callback_.callback()); |
| 184 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 185 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 185 | 186 |
| 186 TestNetLogEntry::List entries; | 187 TestNetLogEntry::List entries; |
| 187 log.GetEntries(&entries); | 188 log.GetEntries(&entries); |
| 188 EXPECT_TRUE( | 189 EXPECT_TRUE( |
| 189 LogContainsBeginEvent(entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 190 LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 190 EXPECT_FALSE(user_sock_->IsConnected()); | 191 EXPECT_FALSE(user_sock_->IsConnected()); |
| 191 | 192 |
| 192 rv = callback_.WaitForResult(); | 193 rv = callback_.WaitForResult(); |
| 193 EXPECT_THAT(rv, IsOk()); | 194 EXPECT_THAT(rv, IsOk()); |
| 194 EXPECT_TRUE(user_sock_->IsConnected()); | 195 EXPECT_TRUE(user_sock_->IsConnected()); |
| 195 log.GetEntries(&entries); | 196 log.GetEntries(&entries); |
| 196 EXPECT_TRUE(LogContainsEndEvent( | 197 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 197 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | |
| 198 | 198 |
| 199 scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); | 199 scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); |
| 200 memcpy(buffer->data(), payload_write.data(), payload_write.size()); | 200 memcpy(buffer->data(), payload_write.data(), payload_write.size()); |
| 201 rv = user_sock_->Write( | 201 rv = user_sock_->Write( |
| 202 buffer.get(), payload_write.size(), callback_.callback()); | 202 buffer.get(), payload_write.size(), callback_.callback()); |
| 203 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 203 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 204 rv = callback_.WaitForResult(); | 204 rv = callback_.WaitForResult(); |
| 205 EXPECT_EQ(static_cast<int>(payload_write.size()), rv); | 205 EXPECT_EQ(static_cast<int>(payload_write.size()), rv); |
| 206 | 206 |
| 207 buffer = new IOBuffer(payload_read.size()); | 207 buffer = new IOBuffer(payload_read.size()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 data_writes, arraysize(data_writes), | 250 data_writes, arraysize(data_writes), |
| 251 host_resolver_.get(), | 251 host_resolver_.get(), |
| 252 "localhost", 80, | 252 "localhost", 80, |
| 253 &log); | 253 &log); |
| 254 | 254 |
| 255 int rv = user_sock_->Connect(callback_.callback()); | 255 int rv = user_sock_->Connect(callback_.callback()); |
| 256 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 256 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 257 | 257 |
| 258 TestNetLogEntry::List entries; | 258 TestNetLogEntry::List entries; |
| 259 log.GetEntries(&entries); | 259 log.GetEntries(&entries); |
| 260 EXPECT_TRUE(LogContainsBeginEvent( | 260 EXPECT_TRUE( |
| 261 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 261 LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 262 | 262 |
| 263 rv = callback_.WaitForResult(); | 263 rv = callback_.WaitForResult(); |
| 264 EXPECT_EQ(tests[i].fail_code, rv); | 264 EXPECT_EQ(tests[i].fail_code, rv); |
| 265 EXPECT_FALSE(user_sock_->IsConnected()); | 265 EXPECT_FALSE(user_sock_->IsConnected()); |
| 266 EXPECT_TRUE(tcp_sock_->IsConnected()); | 266 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 267 log.GetEntries(&entries); | 267 log.GetEntries(&entries); |
| 268 EXPECT_TRUE(LogContainsEndEvent( | 268 EXPECT_TRUE( |
| 269 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 269 LogContainsEndEvent(entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 | 272 |
| 273 // Tests scenario when the server sends the handshake response in | 273 // Tests scenario when the server sends the handshake response in |
| 274 // more than one packet. | 274 // more than one packet. |
| 275 TEST_F(SOCKSClientSocketTest, PartialServerReads) { | 275 TEST_F(SOCKSClientSocketTest, PartialServerReads) { |
| 276 const char kSOCKSPartialReply1[] = { 0x00 }; | 276 const char kSOCKSPartialReply1[] = { 0x00 }; |
| 277 const char kSOCKSPartialReply2[] = { 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; | 277 const char kSOCKSPartialReply2[] = { 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 278 | 278 |
| 279 MockWrite data_writes[] = { | 279 MockWrite data_writes[] = { |
| 280 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; | 280 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 281 MockRead data_reads[] = { | 281 MockRead data_reads[] = { |
| 282 MockRead(ASYNC, kSOCKSPartialReply1, arraysize(kSOCKSPartialReply1)), | 282 MockRead(ASYNC, kSOCKSPartialReply1, arraysize(kSOCKSPartialReply1)), |
| 283 MockRead(ASYNC, kSOCKSPartialReply2, arraysize(kSOCKSPartialReply2)) }; | 283 MockRead(ASYNC, kSOCKSPartialReply2, arraysize(kSOCKSPartialReply2)) }; |
| 284 TestNetLog log; | 284 TestNetLog log; |
| 285 | 285 |
| 286 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 286 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 287 data_writes, arraysize(data_writes), | 287 data_writes, arraysize(data_writes), |
| 288 host_resolver_.get(), | 288 host_resolver_.get(), |
| 289 "localhost", 80, | 289 "localhost", 80, |
| 290 &log); | 290 &log); |
| 291 | 291 |
| 292 int rv = user_sock_->Connect(callback_.callback()); | 292 int rv = user_sock_->Connect(callback_.callback()); |
| 293 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 293 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 294 TestNetLogEntry::List entries; | 294 TestNetLogEntry::List entries; |
| 295 log.GetEntries(&entries); | 295 log.GetEntries(&entries); |
| 296 EXPECT_TRUE(LogContainsBeginEvent( | 296 EXPECT_TRUE( |
| 297 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 297 LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 298 | 298 |
| 299 rv = callback_.WaitForResult(); | 299 rv = callback_.WaitForResult(); |
| 300 EXPECT_THAT(rv, IsOk()); | 300 EXPECT_THAT(rv, IsOk()); |
| 301 EXPECT_TRUE(user_sock_->IsConnected()); | 301 EXPECT_TRUE(user_sock_->IsConnected()); |
| 302 log.GetEntries(&entries); | 302 log.GetEntries(&entries); |
| 303 EXPECT_TRUE(LogContainsEndEvent( | 303 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 304 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | |
| 305 } | 304 } |
| 306 | 305 |
| 307 // Tests scenario when the client sends the handshake request in | 306 // Tests scenario when the client sends the handshake request in |
| 308 // more than one packet. | 307 // more than one packet. |
| 309 TEST_F(SOCKSClientSocketTest, PartialClientWrites) { | 308 TEST_F(SOCKSClientSocketTest, PartialClientWrites) { |
| 310 const char kSOCKSPartialRequest1[] = { 0x04, 0x01 }; | 309 const char kSOCKSPartialRequest1[] = { 0x04, 0x01 }; |
| 311 const char kSOCKSPartialRequest2[] = { 0x00, 0x50, 127, 0, 0, 1, 0 }; | 310 const char kSOCKSPartialRequest2[] = { 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 312 | 311 |
| 313 MockWrite data_writes[] = { | 312 MockWrite data_writes[] = { |
| 314 MockWrite(ASYNC, kSOCKSPartialRequest1, arraysize(kSOCKSPartialRequest1)), | 313 MockWrite(ASYNC, kSOCKSPartialRequest1, arraysize(kSOCKSPartialRequest1)), |
| 315 // simulate some empty writes | 314 // simulate some empty writes |
| 316 MockWrite(ASYNC, 0), | 315 MockWrite(ASYNC, 0), |
| 317 MockWrite(ASYNC, 0), | 316 MockWrite(ASYNC, 0), |
| 318 MockWrite(ASYNC, kSOCKSPartialRequest2, arraysize(kSOCKSPartialRequest2)), | 317 MockWrite(ASYNC, kSOCKSPartialRequest2, arraysize(kSOCKSPartialRequest2)), |
| 319 }; | 318 }; |
| 320 MockRead data_reads[] = { | 319 MockRead data_reads[] = { |
| 321 MockRead(ASYNC, kSOCKSOkReply, arraysize(kSOCKSOkReply)) }; | 320 MockRead(ASYNC, kSOCKSOkReply, arraysize(kSOCKSOkReply)) }; |
| 322 TestNetLog log; | 321 TestNetLog log; |
| 323 | 322 |
| 324 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 323 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 325 data_writes, arraysize(data_writes), | 324 data_writes, arraysize(data_writes), |
| 326 host_resolver_.get(), | 325 host_resolver_.get(), |
| 327 "localhost", 80, | 326 "localhost", 80, |
| 328 &log); | 327 &log); |
| 329 | 328 |
| 330 int rv = user_sock_->Connect(callback_.callback()); | 329 int rv = user_sock_->Connect(callback_.callback()); |
| 331 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 330 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 332 TestNetLogEntry::List entries; | 331 TestNetLogEntry::List entries; |
| 333 log.GetEntries(&entries); | 332 log.GetEntries(&entries); |
| 334 EXPECT_TRUE(LogContainsBeginEvent( | 333 EXPECT_TRUE( |
| 335 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 334 LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 336 | 335 |
| 337 rv = callback_.WaitForResult(); | 336 rv = callback_.WaitForResult(); |
| 338 EXPECT_THAT(rv, IsOk()); | 337 EXPECT_THAT(rv, IsOk()); |
| 339 EXPECT_TRUE(user_sock_->IsConnected()); | 338 EXPECT_TRUE(user_sock_->IsConnected()); |
| 340 log.GetEntries(&entries); | 339 log.GetEntries(&entries); |
| 341 EXPECT_TRUE(LogContainsEndEvent( | 340 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 342 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | |
| 343 } | 341 } |
| 344 | 342 |
| 345 // Tests the case when the server sends a smaller sized handshake data | 343 // Tests the case when the server sends a smaller sized handshake data |
| 346 // and closes the connection. | 344 // and closes the connection. |
| 347 TEST_F(SOCKSClientSocketTest, FailedSocketRead) { | 345 TEST_F(SOCKSClientSocketTest, FailedSocketRead) { |
| 348 MockWrite data_writes[] = { | 346 MockWrite data_writes[] = { |
| 349 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; | 347 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 350 MockRead data_reads[] = { | 348 MockRead data_reads[] = { |
| 351 MockRead(ASYNC, kSOCKSOkReply, arraysize(kSOCKSOkReply) - 2), | 349 MockRead(ASYNC, kSOCKSOkReply, arraysize(kSOCKSOkReply) - 2), |
| 352 // close connection unexpectedly | 350 // close connection unexpectedly |
| 353 MockRead(SYNCHRONOUS, 0) }; | 351 MockRead(SYNCHRONOUS, 0) }; |
| 354 TestNetLog log; | 352 TestNetLog log; |
| 355 | 353 |
| 356 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 354 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 357 data_writes, arraysize(data_writes), | 355 data_writes, arraysize(data_writes), |
| 358 host_resolver_.get(), | 356 host_resolver_.get(), |
| 359 "localhost", 80, | 357 "localhost", 80, |
| 360 &log); | 358 &log); |
| 361 | 359 |
| 362 int rv = user_sock_->Connect(callback_.callback()); | 360 int rv = user_sock_->Connect(callback_.callback()); |
| 363 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 361 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 364 TestNetLogEntry::List entries; | 362 TestNetLogEntry::List entries; |
| 365 log.GetEntries(&entries); | 363 log.GetEntries(&entries); |
| 366 EXPECT_TRUE(LogContainsBeginEvent( | 364 EXPECT_TRUE( |
| 367 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 365 LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 368 | 366 |
| 369 rv = callback_.WaitForResult(); | 367 rv = callback_.WaitForResult(); |
| 370 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); | 368 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); |
| 371 EXPECT_FALSE(user_sock_->IsConnected()); | 369 EXPECT_FALSE(user_sock_->IsConnected()); |
| 372 log.GetEntries(&entries); | 370 log.GetEntries(&entries); |
| 373 EXPECT_TRUE(LogContainsEndEvent( | 371 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 374 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | |
| 375 } | 372 } |
| 376 | 373 |
| 377 // Tries to connect to an unknown hostname. Should fail rather than | 374 // Tries to connect to an unknown hostname. Should fail rather than |
| 378 // falling back to SOCKS4a. | 375 // falling back to SOCKS4a. |
| 379 TEST_F(SOCKSClientSocketTest, FailedDNS) { | 376 TEST_F(SOCKSClientSocketTest, FailedDNS) { |
| 380 const char hostname[] = "unresolved.ipv4.address"; | 377 const char hostname[] = "unresolved.ipv4.address"; |
| 381 | 378 |
| 382 host_resolver_->rules()->AddSimulatedFailure(hostname); | 379 host_resolver_->rules()->AddSimulatedFailure(hostname); |
| 383 | 380 |
| 384 TestNetLog log; | 381 TestNetLog log; |
| 385 | 382 |
| 386 user_sock_ = BuildMockSocket(NULL, 0, | 383 user_sock_ = BuildMockSocket(NULL, 0, |
| 387 NULL, 0, | 384 NULL, 0, |
| 388 host_resolver_.get(), | 385 host_resolver_.get(), |
| 389 hostname, 80, | 386 hostname, 80, |
| 390 &log); | 387 &log); |
| 391 | 388 |
| 392 int rv = user_sock_->Connect(callback_.callback()); | 389 int rv = user_sock_->Connect(callback_.callback()); |
| 393 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 390 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 394 TestNetLogEntry::List entries; | 391 TestNetLogEntry::List entries; |
| 395 log.GetEntries(&entries); | 392 log.GetEntries(&entries); |
| 396 EXPECT_TRUE(LogContainsBeginEvent( | 393 EXPECT_TRUE( |
| 397 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 394 LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 398 | 395 |
| 399 rv = callback_.WaitForResult(); | 396 rv = callback_.WaitForResult(); |
| 400 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); | 397 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
| 401 EXPECT_FALSE(user_sock_->IsConnected()); | 398 EXPECT_FALSE(user_sock_->IsConnected()); |
| 402 log.GetEntries(&entries); | 399 log.GetEntries(&entries); |
| 403 EXPECT_TRUE(LogContainsEndEvent( | 400 EXPECT_TRUE(LogContainsEndEvent(entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 404 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | |
| 405 } | 401 } |
| 406 | 402 |
| 407 // Calls Disconnect() while a host resolve is in progress. The outstanding host | 403 // Calls Disconnect() while a host resolve is in progress. The outstanding host |
| 408 // resolve should be cancelled. | 404 // resolve should be cancelled. |
| 409 TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { | 405 TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { |
| 410 std::unique_ptr<HangingHostResolverWithCancel> hanging_resolver( | 406 std::unique_ptr<HangingHostResolverWithCancel> hanging_resolver( |
| 411 new HangingHostResolverWithCancel()); | 407 new HangingHostResolverWithCancel()); |
| 412 | 408 |
| 413 // Doesn't matter what the socket data is, we will never use it -- garbage. | 409 // Doesn't matter what the socket data is, we will never use it -- garbage. |
| 414 MockWrite data_writes[] = { MockWrite(SYNCHRONOUS, "", 0) }; | 410 MockWrite data_writes[] = { MockWrite(SYNCHRONOUS, "", 0) }; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 NULL, 0, | 461 NULL, 0, |
| 466 host_resolver.get(), | 462 host_resolver.get(), |
| 467 kHostName, 80, | 463 kHostName, 80, |
| 468 NULL); | 464 NULL); |
| 469 | 465 |
| 470 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, | 466 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, |
| 471 callback_.GetResult(user_sock_->Connect(callback_.callback()))); | 467 callback_.GetResult(user_sock_->Connect(callback_.callback()))); |
| 472 } | 468 } |
| 473 | 469 |
| 474 } // namespace net | 470 } // namespace net |
| OLD | NEW |