| 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( |
| 197 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 198 entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 198 | 199 |
| 199 scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); | 200 scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); |
| 200 memcpy(buffer->data(), payload_write.data(), payload_write.size()); | 201 memcpy(buffer->data(), payload_write.data(), payload_write.size()); |
| 201 rv = user_sock_->Write( | 202 rv = user_sock_->Write( |
| 202 buffer.get(), payload_write.size(), callback_.callback()); | 203 buffer.get(), payload_write.size(), callback_.callback()); |
| 203 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 204 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 204 rv = callback_.WaitForResult(); | 205 rv = callback_.WaitForResult(); |
| 205 EXPECT_EQ(static_cast<int>(payload_write.size()), rv); | 206 EXPECT_EQ(static_cast<int>(payload_write.size()), rv); |
| 206 | 207 |
| 207 buffer = new IOBuffer(payload_read.size()); | 208 buffer = new IOBuffer(payload_read.size()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 host_resolver_.get(), | 252 host_resolver_.get(), |
| 252 "localhost", 80, | 253 "localhost", 80, |
| 253 &log); | 254 &log); |
| 254 | 255 |
| 255 int rv = user_sock_->Connect(callback_.callback()); | 256 int rv = user_sock_->Connect(callback_.callback()); |
| 256 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 257 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 257 | 258 |
| 258 TestNetLogEntry::List entries; | 259 TestNetLogEntry::List entries; |
| 259 log.GetEntries(&entries); | 260 log.GetEntries(&entries); |
| 260 EXPECT_TRUE(LogContainsBeginEvent( | 261 EXPECT_TRUE(LogContainsBeginEvent( |
| 261 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 262 entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 262 | 263 |
| 263 rv = callback_.WaitForResult(); | 264 rv = callback_.WaitForResult(); |
| 264 EXPECT_EQ(tests[i].fail_code, rv); | 265 EXPECT_EQ(tests[i].fail_code, rv); |
| 265 EXPECT_FALSE(user_sock_->IsConnected()); | 266 EXPECT_FALSE(user_sock_->IsConnected()); |
| 266 EXPECT_TRUE(tcp_sock_->IsConnected()); | 267 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 267 log.GetEntries(&entries); | 268 log.GetEntries(&entries); |
| 268 EXPECT_TRUE(LogContainsEndEvent( | 269 EXPECT_TRUE(LogContainsEndEvent( |
| 269 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 270 entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 270 } | 271 } |
| 271 } | 272 } |
| 272 | 273 |
| 273 // Tests scenario when the server sends the handshake response in | 274 // Tests scenario when the server sends the handshake response in |
| 274 // more than one packet. | 275 // more than one packet. |
| 275 TEST_F(SOCKSClientSocketTest, PartialServerReads) { | 276 TEST_F(SOCKSClientSocketTest, PartialServerReads) { |
| 276 const char kSOCKSPartialReply1[] = { 0x00 }; | 277 const char kSOCKSPartialReply1[] = { 0x00 }; |
| 277 const char kSOCKSPartialReply2[] = { 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; | 278 const char kSOCKSPartialReply2[] = { 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 278 | 279 |
| 279 MockWrite data_writes[] = { | 280 MockWrite data_writes[] = { |
| 280 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; | 281 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 281 MockRead data_reads[] = { | 282 MockRead data_reads[] = { |
| 282 MockRead(ASYNC, kSOCKSPartialReply1, arraysize(kSOCKSPartialReply1)), | 283 MockRead(ASYNC, kSOCKSPartialReply1, arraysize(kSOCKSPartialReply1)), |
| 283 MockRead(ASYNC, kSOCKSPartialReply2, arraysize(kSOCKSPartialReply2)) }; | 284 MockRead(ASYNC, kSOCKSPartialReply2, arraysize(kSOCKSPartialReply2)) }; |
| 284 TestNetLog log; | 285 TestNetLog log; |
| 285 | 286 |
| 286 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 287 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 287 data_writes, arraysize(data_writes), | 288 data_writes, arraysize(data_writes), |
| 288 host_resolver_.get(), | 289 host_resolver_.get(), |
| 289 "localhost", 80, | 290 "localhost", 80, |
| 290 &log); | 291 &log); |
| 291 | 292 |
| 292 int rv = user_sock_->Connect(callback_.callback()); | 293 int rv = user_sock_->Connect(callback_.callback()); |
| 293 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 294 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 294 TestNetLogEntry::List entries; | 295 TestNetLogEntry::List entries; |
| 295 log.GetEntries(&entries); | 296 log.GetEntries(&entries); |
| 296 EXPECT_TRUE(LogContainsBeginEvent( | 297 EXPECT_TRUE(LogContainsBeginEvent( |
| 297 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 298 entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 298 | 299 |
| 299 rv = callback_.WaitForResult(); | 300 rv = callback_.WaitForResult(); |
| 300 EXPECT_THAT(rv, IsOk()); | 301 EXPECT_THAT(rv, IsOk()); |
| 301 EXPECT_TRUE(user_sock_->IsConnected()); | 302 EXPECT_TRUE(user_sock_->IsConnected()); |
| 302 log.GetEntries(&entries); | 303 log.GetEntries(&entries); |
| 303 EXPECT_TRUE(LogContainsEndEvent( | 304 EXPECT_TRUE(LogContainsEndEvent( |
| 304 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 305 entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 305 } | 306 } |
| 306 | 307 |
| 307 // Tests scenario when the client sends the handshake request in | 308 // Tests scenario when the client sends the handshake request in |
| 308 // more than one packet. | 309 // more than one packet. |
| 309 TEST_F(SOCKSClientSocketTest, PartialClientWrites) { | 310 TEST_F(SOCKSClientSocketTest, PartialClientWrites) { |
| 310 const char kSOCKSPartialRequest1[] = { 0x04, 0x01 }; | 311 const char kSOCKSPartialRequest1[] = { 0x04, 0x01 }; |
| 311 const char kSOCKSPartialRequest2[] = { 0x00, 0x50, 127, 0, 0, 1, 0 }; | 312 const char kSOCKSPartialRequest2[] = { 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 312 | 313 |
| 313 MockWrite data_writes[] = { | 314 MockWrite data_writes[] = { |
| 314 MockWrite(ASYNC, kSOCKSPartialRequest1, arraysize(kSOCKSPartialRequest1)), | 315 MockWrite(ASYNC, kSOCKSPartialRequest1, arraysize(kSOCKSPartialRequest1)), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 325 data_writes, arraysize(data_writes), | 326 data_writes, arraysize(data_writes), |
| 326 host_resolver_.get(), | 327 host_resolver_.get(), |
| 327 "localhost", 80, | 328 "localhost", 80, |
| 328 &log); | 329 &log); |
| 329 | 330 |
| 330 int rv = user_sock_->Connect(callback_.callback()); | 331 int rv = user_sock_->Connect(callback_.callback()); |
| 331 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 332 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 332 TestNetLogEntry::List entries; | 333 TestNetLogEntry::List entries; |
| 333 log.GetEntries(&entries); | 334 log.GetEntries(&entries); |
| 334 EXPECT_TRUE(LogContainsBeginEvent( | 335 EXPECT_TRUE(LogContainsBeginEvent( |
| 335 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 336 entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 336 | 337 |
| 337 rv = callback_.WaitForResult(); | 338 rv = callback_.WaitForResult(); |
| 338 EXPECT_THAT(rv, IsOk()); | 339 EXPECT_THAT(rv, IsOk()); |
| 339 EXPECT_TRUE(user_sock_->IsConnected()); | 340 EXPECT_TRUE(user_sock_->IsConnected()); |
| 340 log.GetEntries(&entries); | 341 log.GetEntries(&entries); |
| 341 EXPECT_TRUE(LogContainsEndEvent( | 342 EXPECT_TRUE(LogContainsEndEvent( |
| 342 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 343 entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 343 } | 344 } |
| 344 | 345 |
| 345 // Tests the case when the server sends a smaller sized handshake data | 346 // Tests the case when the server sends a smaller sized handshake data |
| 346 // and closes the connection. | 347 // and closes the connection. |
| 347 TEST_F(SOCKSClientSocketTest, FailedSocketRead) { | 348 TEST_F(SOCKSClientSocketTest, FailedSocketRead) { |
| 348 MockWrite data_writes[] = { | 349 MockWrite data_writes[] = { |
| 349 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; | 350 MockWrite(ASYNC, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 350 MockRead data_reads[] = { | 351 MockRead data_reads[] = { |
| 351 MockRead(ASYNC, kSOCKSOkReply, arraysize(kSOCKSOkReply) - 2), | 352 MockRead(ASYNC, kSOCKSOkReply, arraysize(kSOCKSOkReply) - 2), |
| 352 // close connection unexpectedly | 353 // close connection unexpectedly |
| 353 MockRead(SYNCHRONOUS, 0) }; | 354 MockRead(SYNCHRONOUS, 0) }; |
| 354 TestNetLog log; | 355 TestNetLog log; |
| 355 | 356 |
| 356 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 357 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 357 data_writes, arraysize(data_writes), | 358 data_writes, arraysize(data_writes), |
| 358 host_resolver_.get(), | 359 host_resolver_.get(), |
| 359 "localhost", 80, | 360 "localhost", 80, |
| 360 &log); | 361 &log); |
| 361 | 362 |
| 362 int rv = user_sock_->Connect(callback_.callback()); | 363 int rv = user_sock_->Connect(callback_.callback()); |
| 363 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 364 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 364 TestNetLogEntry::List entries; | 365 TestNetLogEntry::List entries; |
| 365 log.GetEntries(&entries); | 366 log.GetEntries(&entries); |
| 366 EXPECT_TRUE(LogContainsBeginEvent( | 367 EXPECT_TRUE(LogContainsBeginEvent( |
| 367 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 368 entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 368 | 369 |
| 369 rv = callback_.WaitForResult(); | 370 rv = callback_.WaitForResult(); |
| 370 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); | 371 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); |
| 371 EXPECT_FALSE(user_sock_->IsConnected()); | 372 EXPECT_FALSE(user_sock_->IsConnected()); |
| 372 log.GetEntries(&entries); | 373 log.GetEntries(&entries); |
| 373 EXPECT_TRUE(LogContainsEndEvent( | 374 EXPECT_TRUE(LogContainsEndEvent( |
| 374 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 375 entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 375 } | 376 } |
| 376 | 377 |
| 377 // Tries to connect to an unknown hostname. Should fail rather than | 378 // Tries to connect to an unknown hostname. Should fail rather than |
| 378 // falling back to SOCKS4a. | 379 // falling back to SOCKS4a. |
| 379 TEST_F(SOCKSClientSocketTest, FailedDNS) { | 380 TEST_F(SOCKSClientSocketTest, FailedDNS) { |
| 380 const char hostname[] = "unresolved.ipv4.address"; | 381 const char hostname[] = "unresolved.ipv4.address"; |
| 381 | 382 |
| 382 host_resolver_->rules()->AddSimulatedFailure(hostname); | 383 host_resolver_->rules()->AddSimulatedFailure(hostname); |
| 383 | 384 |
| 384 TestNetLog log; | 385 TestNetLog log; |
| 385 | 386 |
| 386 user_sock_ = BuildMockSocket(NULL, 0, | 387 user_sock_ = BuildMockSocket(NULL, 0, |
| 387 NULL, 0, | 388 NULL, 0, |
| 388 host_resolver_.get(), | 389 host_resolver_.get(), |
| 389 hostname, 80, | 390 hostname, 80, |
| 390 &log); | 391 &log); |
| 391 | 392 |
| 392 int rv = user_sock_->Connect(callback_.callback()); | 393 int rv = user_sock_->Connect(callback_.callback()); |
| 393 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 394 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 394 TestNetLogEntry::List entries; | 395 TestNetLogEntry::List entries; |
| 395 log.GetEntries(&entries); | 396 log.GetEntries(&entries); |
| 396 EXPECT_TRUE(LogContainsBeginEvent( | 397 EXPECT_TRUE(LogContainsBeginEvent( |
| 397 entries, 0, NetLog::TYPE_SOCKS_CONNECT)); | 398 entries, 0, NetLogEventType::SOCKS_CONNECT)); |
| 398 | 399 |
| 399 rv = callback_.WaitForResult(); | 400 rv = callback_.WaitForResult(); |
| 400 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); | 401 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
| 401 EXPECT_FALSE(user_sock_->IsConnected()); | 402 EXPECT_FALSE(user_sock_->IsConnected()); |
| 402 log.GetEntries(&entries); | 403 log.GetEntries(&entries); |
| 403 EXPECT_TRUE(LogContainsEndEvent( | 404 EXPECT_TRUE(LogContainsEndEvent( |
| 404 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 405 entries, -1, NetLogEventType::SOCKS_CONNECT)); |
| 405 } | 406 } |
| 406 | 407 |
| 407 // Calls Disconnect() while a host resolve is in progress. The outstanding host | 408 // Calls Disconnect() while a host resolve is in progress. The outstanding host |
| 408 // resolve should be cancelled. | 409 // resolve should be cancelled. |
| 409 TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { | 410 TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { |
| 410 std::unique_ptr<HangingHostResolverWithCancel> hanging_resolver( | 411 std::unique_ptr<HangingHostResolverWithCancel> hanging_resolver( |
| 411 new HangingHostResolverWithCancel()); | 412 new HangingHostResolverWithCancel()); |
| 412 | 413 |
| 413 // Doesn't matter what the socket data is, we will never use it -- garbage. | 414 // Doesn't matter what the socket data is, we will never use it -- garbage. |
| 414 MockWrite data_writes[] = { MockWrite(SYNCHRONOUS, "", 0) }; | 415 MockWrite data_writes[] = { MockWrite(SYNCHRONOUS, "", 0) }; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 NULL, 0, | 466 NULL, 0, |
| 466 host_resolver.get(), | 467 host_resolver.get(), |
| 467 kHostName, 80, | 468 kHostName, 80, |
| 468 NULL); | 469 NULL); |
| 469 | 470 |
| 470 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, | 471 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, |
| 471 callback_.GetResult(user_sock_->Connect(callback_.callback()))); | 472 callback_.GetResult(user_sock_->Connect(callback_.callback()))); |
| 472 } | 473 } |
| 473 | 474 |
| 474 } // namespace net | 475 } // namespace net |
| OLD | NEW |