| 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 #include <utility> | 10 #include <utility> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 namespace net { | 30 namespace net { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // Base class to test SOCKS5ClientSocket | 34 // Base class to test SOCKS5ClientSocket |
| 35 class SOCKS5ClientSocketTest : public PlatformTest { | 35 class SOCKS5ClientSocketTest : public PlatformTest { |
| 36 public: | 36 public: |
| 37 SOCKS5ClientSocketTest(); | 37 SOCKS5ClientSocketTest(); |
| 38 // Create a SOCKSClientSocket on top of a MockSocket. | 38 // Create a SOCKSClientSocket on top of a MockSocket. |
| 39 scoped_ptr<SOCKS5ClientSocket> BuildMockSocket(MockRead reads[], | 39 std::unique_ptr<SOCKS5ClientSocket> BuildMockSocket( |
| 40 size_t reads_count, | 40 MockRead reads[], |
| 41 MockWrite writes[], | 41 size_t reads_count, |
| 42 size_t writes_count, | 42 MockWrite writes[], |
| 43 const std::string& hostname, | 43 size_t writes_count, |
| 44 int port, | 44 const std::string& hostname, |
| 45 NetLog* net_log); | 45 int port, |
| 46 NetLog* net_log); |
| 46 | 47 |
| 47 void SetUp() override; | 48 void SetUp() override; |
| 48 | 49 |
| 49 protected: | 50 protected: |
| 50 const uint16_t kNwPort; | 51 const uint16_t kNwPort; |
| 51 TestNetLog net_log_; | 52 TestNetLog net_log_; |
| 52 scoped_ptr<SOCKS5ClientSocket> user_sock_; | 53 std::unique_ptr<SOCKS5ClientSocket> user_sock_; |
| 53 AddressList address_list_; | 54 AddressList address_list_; |
| 54 // Filled in by BuildMockSocket() and owned by its return value | 55 // Filled in by BuildMockSocket() and owned by its return value |
| 55 // (which |user_sock| is set to). | 56 // (which |user_sock| is set to). |
| 56 StreamSocket* tcp_sock_; | 57 StreamSocket* tcp_sock_; |
| 57 TestCompletionCallback callback_; | 58 TestCompletionCallback callback_; |
| 58 scoped_ptr<MockHostResolver> host_resolver_; | 59 std::unique_ptr<MockHostResolver> host_resolver_; |
| 59 scoped_ptr<SocketDataProvider> data_; | 60 std::unique_ptr<SocketDataProvider> data_; |
| 60 | 61 |
| 61 private: | 62 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocketTest); | 63 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocketTest); |
| 63 }; | 64 }; |
| 64 | 65 |
| 65 SOCKS5ClientSocketTest::SOCKS5ClientSocketTest() | 66 SOCKS5ClientSocketTest::SOCKS5ClientSocketTest() |
| 66 : kNwPort(base::HostToNet16(80)), | 67 : kNwPort(base::HostToNet16(80)), |
| 67 host_resolver_(new MockHostResolver) { | 68 host_resolver_(new MockHostResolver) { |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Set up platform before every test case | 71 // Set up platform before every test case |
| 71 void SOCKS5ClientSocketTest::SetUp() { | 72 void SOCKS5ClientSocketTest::SetUp() { |
| 72 PlatformTest::SetUp(); | 73 PlatformTest::SetUp(); |
| 73 | 74 |
| 74 // Resolve the "localhost" AddressList used by the TCP connection to connect. | 75 // Resolve the "localhost" AddressList used by the TCP connection to connect. |
| 75 HostResolver::RequestInfo info(HostPortPair("www.socks-proxy.com", 1080)); | 76 HostResolver::RequestInfo info(HostPortPair("www.socks-proxy.com", 1080)); |
| 76 TestCompletionCallback callback; | 77 TestCompletionCallback callback; |
| 77 int rv = host_resolver_->Resolve(info, | 78 int rv = host_resolver_->Resolve(info, |
| 78 DEFAULT_PRIORITY, | 79 DEFAULT_PRIORITY, |
| 79 &address_list_, | 80 &address_list_, |
| 80 callback.callback(), | 81 callback.callback(), |
| 81 NULL, | 82 NULL, |
| 82 BoundNetLog()); | 83 BoundNetLog()); |
| 83 ASSERT_EQ(ERR_IO_PENDING, rv); | 84 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 84 rv = callback.WaitForResult(); | 85 rv = callback.WaitForResult(); |
| 85 ASSERT_EQ(OK, rv); | 86 ASSERT_EQ(OK, rv); |
| 86 } | 87 } |
| 87 | 88 |
| 88 scoped_ptr<SOCKS5ClientSocket> SOCKS5ClientSocketTest::BuildMockSocket( | 89 std::unique_ptr<SOCKS5ClientSocket> SOCKS5ClientSocketTest::BuildMockSocket( |
| 89 MockRead reads[], | 90 MockRead reads[], |
| 90 size_t reads_count, | 91 size_t reads_count, |
| 91 MockWrite writes[], | 92 MockWrite writes[], |
| 92 size_t writes_count, | 93 size_t writes_count, |
| 93 const std::string& hostname, | 94 const std::string& hostname, |
| 94 int port, | 95 int port, |
| 95 NetLog* net_log) { | 96 NetLog* net_log) { |
| 96 TestCompletionCallback callback; | 97 TestCompletionCallback callback; |
| 97 data_.reset(new StaticSocketDataProvider(reads, reads_count, | 98 data_.reset(new StaticSocketDataProvider(reads, reads_count, |
| 98 writes, writes_count)); | 99 writes, writes_count)); |
| 99 tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); | 100 tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); |
| 100 | 101 |
| 101 int rv = tcp_sock_->Connect(callback.callback()); | 102 int rv = tcp_sock_->Connect(callback.callback()); |
| 102 EXPECT_EQ(ERR_IO_PENDING, rv); | 103 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 103 rv = callback.WaitForResult(); | 104 rv = callback.WaitForResult(); |
| 104 EXPECT_EQ(OK, rv); | 105 EXPECT_EQ(OK, rv); |
| 105 EXPECT_TRUE(tcp_sock_->IsConnected()); | 106 EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 106 | 107 |
| 107 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | 108 std::unique_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 108 // |connection| takes ownership of |tcp_sock_|, but keep a | 109 // |connection| takes ownership of |tcp_sock_|, but keep a |
| 109 // non-owning pointer to it. | 110 // non-owning pointer to it. |
| 110 connection->SetSocket(scoped_ptr<StreamSocket>(tcp_sock_)); | 111 connection->SetSocket(std::unique_ptr<StreamSocket>(tcp_sock_)); |
| 111 return scoped_ptr<SOCKS5ClientSocket>(new SOCKS5ClientSocket( | 112 return std::unique_ptr<SOCKS5ClientSocket>(new SOCKS5ClientSocket( |
| 112 std::move(connection), | 113 std::move(connection), |
| 113 HostResolver::RequestInfo(HostPortPair(hostname, port)))); | 114 HostResolver::RequestInfo(HostPortPair(hostname, port)))); |
| 114 } | 115 } |
| 115 | 116 |
| 116 // Tests a complete SOCKS5 handshake and the disconnection. | 117 // Tests a complete SOCKS5 handshake and the disconnection. |
| 117 TEST_F(SOCKS5ClientSocketTest, CompleteHandshake) { | 118 TEST_F(SOCKS5ClientSocketTest, CompleteHandshake) { |
| 118 const std::string payload_write = "random data"; | 119 const std::string payload_write = "random data"; |
| 119 const std::string payload_read = "moar random data"; | 120 const std::string payload_read = "moar random data"; |
| 120 | 121 |
| 121 const char kOkRequest[] = { | 122 const char kOkRequest[] = { |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 EXPECT_TRUE(user_sock_->IsConnected()); | 375 EXPECT_TRUE(user_sock_->IsConnected()); |
| 375 net_log_.GetEntries(&net_log_entries); | 376 net_log_.GetEntries(&net_log_entries); |
| 376 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, | 377 EXPECT_TRUE(LogContainsEndEvent(net_log_entries, -1, |
| 377 NetLog::TYPE_SOCKS5_CONNECT)); | 378 NetLog::TYPE_SOCKS5_CONNECT)); |
| 378 } | 379 } |
| 379 } | 380 } |
| 380 | 381 |
| 381 } // namespace | 382 } // namespace |
| 382 | 383 |
| 383 } // namespace net | 384 } // namespace net |
| OLD | NEW |