| 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 <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.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/test_net_log.h" | 17 #include "net/log/test_net_log.h" |
| 18 #include "net/log/test_net_log_entry.h" | 18 #include "net/log/test_net_log_entry.h" |
| 19 #include "net/log/test_net_log_util.h" | 19 #include "net/log/test_net_log_util.h" |
| 20 #include "net/socket/client_socket_factory.h" | 20 #include "net/socket/client_socket_factory.h" |
| 21 #include "net/socket/socket_test_util.h" | 21 #include "net/socket/socket_test_util.h" |
| 22 #include "net/socket/tcp_client_socket.h" | 22 #include "net/socket/tcp_client_socket.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 25 | 25 |
| 26 //----------------------------------------------------------------------------- | 26 //----------------------------------------------------------------------------- |
| 27 | 27 |
| 28 namespace net { | 28 namespace net { |
| 29 | 29 |
| 30 const char kSOCKSOkRequest[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; | 30 const char kSOCKSOkRequest[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 31 const char kSOCKSOkReply[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; | 31 const char kSOCKSOkReply[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 32 | 32 |
| 33 class SOCKSClientSocketTest : public PlatformTest { | 33 class SOCKSClientSocketTest : public PlatformTest { |
| 34 public: | 34 public: |
| 35 SOCKSClientSocketTest(); | 35 SOCKSClientSocketTest(); |
| 36 // Create a SOCKSClientSocket on top of a MockSocket. | 36 // Create a SOCKSClientSocket on top of a MockSocket. |
| 37 scoped_ptr<SOCKSClientSocket> BuildMockSocket( | 37 std::unique_ptr<SOCKSClientSocket> BuildMockSocket( |
| 38 MockRead reads[], size_t reads_count, | 38 MockRead reads[], |
| 39 MockWrite writes[], size_t writes_count, | 39 size_t reads_count, |
| 40 MockWrite writes[], |
| 41 size_t writes_count, |
| 40 HostResolver* host_resolver, | 42 HostResolver* host_resolver, |
| 41 const std::string& hostname, int port, | 43 const std::string& hostname, |
| 44 int port, |
| 42 NetLog* net_log); | 45 NetLog* net_log); |
| 43 void SetUp() override; | 46 void SetUp() override; |
| 44 | 47 |
| 45 protected: | 48 protected: |
| 46 scoped_ptr<SOCKSClientSocket> user_sock_; | 49 std::unique_ptr<SOCKSClientSocket> user_sock_; |
| 47 AddressList address_list_; | 50 AddressList address_list_; |
| 48 // Filled in by BuildMockSocket() and owned by its return value | 51 // Filled in by BuildMockSocket() and owned by its return value |
| 49 // (which |user_sock| is set to). | 52 // (which |user_sock| is set to). |
| 50 StreamSocket* tcp_sock_; | 53 StreamSocket* tcp_sock_; |
| 51 TestCompletionCallback callback_; | 54 TestCompletionCallback callback_; |
| 52 scoped_ptr<MockHostResolver> host_resolver_; | 55 std::unique_ptr<MockHostResolver> host_resolver_; |
| 53 scoped_ptr<SocketDataProvider> data_; | 56 std::unique_ptr<SocketDataProvider> data_; |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 SOCKSClientSocketTest::SOCKSClientSocketTest() | 59 SOCKSClientSocketTest::SOCKSClientSocketTest() |
| 57 : host_resolver_(new MockHostResolver) { | 60 : host_resolver_(new MockHostResolver) { |
| 58 } | 61 } |
| 59 | 62 |
| 60 // Set up platform before every test case | 63 // Set up platform before every test case |
| 61 void SOCKSClientSocketTest::SetUp() { | 64 void SOCKSClientSocketTest::SetUp() { |
| 62 PlatformTest::SetUp(); | 65 PlatformTest::SetUp(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 scoped_ptr<SOCKSClientSocket> SOCKSClientSocketTest::BuildMockSocket( | 68 std::unique_ptr<SOCKSClientSocket> SOCKSClientSocketTest::BuildMockSocket( |
| 66 MockRead reads[], | 69 MockRead reads[], |
| 67 size_t reads_count, | 70 size_t reads_count, |
| 68 MockWrite writes[], | 71 MockWrite writes[], |
| 69 size_t writes_count, | 72 size_t writes_count, |
| 70 HostResolver* host_resolver, | 73 HostResolver* host_resolver, |
| 71 const std::string& hostname, | 74 const std::string& hostname, |
| 72 int port, | 75 int port, |
| 73 NetLog* net_log) { | 76 NetLog* net_log) { |
| 74 | |
| 75 TestCompletionCallback callback; | 77 TestCompletionCallback callback; |
| 76 data_.reset(new StaticSocketDataProvider(reads, reads_count, | 78 data_.reset(new StaticSocketDataProvider(reads, reads_count, |
| 77 writes, writes_count)); | 79 writes, writes_count)); |
| 78 tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); | 80 tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); |
| 79 | 81 |
| 80 int rv = tcp_sock_->Connect(callback.callback()); | 82 int rv = tcp_sock_->Connect(callback.callback()); |
| 81 EXPECT_EQ(ERR_IO_PENDING, rv); | 83 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 82 rv = callback.WaitForResult(); | 84 rv = callback.WaitForResult(); |
| 83 EXPECT_EQ(OK, rv); | 85 EXPECT_EQ(OK, rv); |
| 84 EXPECT_TRUE(tcp_sock_->IsConnected()); | 86 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 85 | 87 |
| 86 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | 88 std::unique_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 87 // |connection| takes ownership of |tcp_sock_|, but keep a | 89 // |connection| takes ownership of |tcp_sock_|, but keep a |
| 88 // non-owning pointer to it. | 90 // non-owning pointer to it. |
| 89 connection->SetSocket(scoped_ptr<StreamSocket>(tcp_sock_)); | 91 connection->SetSocket(std::unique_ptr<StreamSocket>(tcp_sock_)); |
| 90 return scoped_ptr<SOCKSClientSocket>(new SOCKSClientSocket( | 92 return std::unique_ptr<SOCKSClientSocket>(new SOCKSClientSocket( |
| 91 std::move(connection), | 93 std::move(connection), |
| 92 HostResolver::RequestInfo(HostPortPair(hostname, port)), DEFAULT_PRIORITY, | 94 HostResolver::RequestInfo(HostPortPair(hostname, port)), DEFAULT_PRIORITY, |
| 93 host_resolver)); | 95 host_resolver)); |
| 94 } | 96 } |
| 95 | 97 |
| 96 // Implementation of HostResolver that never completes its resolve request. | 98 // Implementation of HostResolver that never completes its resolve request. |
| 97 // We use this in the test "DisconnectWhileHostResolveInProgress" to make | 99 // We use this in the test "DisconnectWhileHostResolveInProgress" to make |
| 98 // sure that the outstanding resolve request gets cancelled. | 100 // sure that the outstanding resolve request gets cancelled. |
| 99 class HangingHostResolverWithCancel : public HostResolver { | 101 class HangingHostResolverWithCancel : public HostResolver { |
| 100 public: | 102 public: |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 382 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 381 EXPECT_FALSE(user_sock_->IsConnected()); | 383 EXPECT_FALSE(user_sock_->IsConnected()); |
| 382 log.GetEntries(&entries); | 384 log.GetEntries(&entries); |
| 383 EXPECT_TRUE(LogContainsEndEvent( | 385 EXPECT_TRUE(LogContainsEndEvent( |
| 384 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); | 386 entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
| 385 } | 387 } |
| 386 | 388 |
| 387 // Calls Disconnect() while a host resolve is in progress. The outstanding host | 389 // Calls Disconnect() while a host resolve is in progress. The outstanding host |
| 388 // resolve should be cancelled. | 390 // resolve should be cancelled. |
| 389 TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { | 391 TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { |
| 390 scoped_ptr<HangingHostResolverWithCancel> hanging_resolver( | 392 std::unique_ptr<HangingHostResolverWithCancel> hanging_resolver( |
| 391 new HangingHostResolverWithCancel()); | 393 new HangingHostResolverWithCancel()); |
| 392 | 394 |
| 393 // Doesn't matter what the socket data is, we will never use it -- garbage. | 395 // Doesn't matter what the socket data is, we will never use it -- garbage. |
| 394 MockWrite data_writes[] = { MockWrite(SYNCHRONOUS, "", 0) }; | 396 MockWrite data_writes[] = { MockWrite(SYNCHRONOUS, "", 0) }; |
| 395 MockRead data_reads[] = { MockRead(SYNCHRONOUS, "", 0) }; | 397 MockRead data_reads[] = { MockRead(SYNCHRONOUS, "", 0) }; |
| 396 | 398 |
| 397 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), | 399 user_sock_ = BuildMockSocket(data_reads, arraysize(data_reads), |
| 398 data_writes, arraysize(data_writes), | 400 data_writes, arraysize(data_writes), |
| 399 hanging_resolver.get(), | 401 hanging_resolver.get(), |
| 400 "foo", 80, | 402 "foo", 80, |
| 401 NULL); | 403 NULL); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 431 NULL); | 433 NULL); |
| 432 | 434 |
| 433 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, | 435 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, |
| 434 callback_.GetResult(user_sock_->Connect(callback_.callback()))); | 436 callback_.GetResult(user_sock_->Connect(callback_.callback()))); |
| 435 } | 437 } |
| 436 | 438 |
| 437 // Same as above, but with a real resolver, to protect against regressions. | 439 // Same as above, but with a real resolver, to protect against regressions. |
| 438 TEST_F(SOCKSClientSocketTest, NoIPv6RealResolver) { | 440 TEST_F(SOCKSClientSocketTest, NoIPv6RealResolver) { |
| 439 const char kHostName[] = "::1"; | 441 const char kHostName[] = "::1"; |
| 440 | 442 |
| 441 scoped_ptr<HostResolver> host_resolver( | 443 std::unique_ptr<HostResolver> host_resolver( |
| 442 HostResolver::CreateSystemResolver(HostResolver::Options(), NULL)); | 444 HostResolver::CreateSystemResolver(HostResolver::Options(), NULL)); |
| 443 | 445 |
| 444 user_sock_ = BuildMockSocket(NULL, 0, | 446 user_sock_ = BuildMockSocket(NULL, 0, |
| 445 NULL, 0, | 447 NULL, 0, |
| 446 host_resolver.get(), | 448 host_resolver.get(), |
| 447 kHostName, 80, | 449 kHostName, 80, |
| 448 NULL); | 450 NULL); |
| 449 | 451 |
| 450 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, | 452 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, |
| 451 callback_.GetResult(user_sock_->Connect(callback_.callback()))); | 453 callback_.GetResult(user_sock_->Connect(callback_.callback()))); |
| 452 } | 454 } |
| 453 | 455 |
| 454 } // namespace net | 456 } // namespace net |
| OLD | NEW |