[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/socks_client_socket.h" |
| 6 | |
| 7 | #include "net/base/address_list.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 8 | #include "net/base/net_log.h" |
| 9 | #include "net/base/net_log_unittest.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 10 | #include "net/base/mock_host_resolver.h" |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 11 | #include "net/base/test_completion_callback.h" |
| 12 | #include "net/base/winsock_init.h" |
| 13 | #include "net/socket/client_socket_factory.h" |
| 14 | #include "net/socket/tcp_client_socket.h" |
| 15 | #include "net/socket/socket_test_util.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | #include "testing/platform_test.h" |
| 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | |
| 21 | namespace net { |
| 22 | |
| 23 | const char kSOCKSOkRequest[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 24 | const char kSOCKSOkReply[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 25 | |
| 26 | class SOCKSClientSocketTest : public PlatformTest { |
| 27 | public: |
| 28 | SOCKSClientSocketTest(); |
| 29 | // Create a SOCKSClientSocket on top of a MockSocket. |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 30 | SOCKSClientSocket* BuildMockSocket(MockRead reads[], size_t reads_count, |
| 31 | MockWrite writes[], size_t writes_count, |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 32 | HostResolver* host_resolver, |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 33 | const std::string& hostname, int port, |
| 34 | NetLog* net_log); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 35 | virtual void SetUp(); |
| 36 | |
| 37 | protected: |
| 38 | scoped_ptr<SOCKSClientSocket> user_sock_; |
| 39 | AddressList address_list_; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 40 | StreamSocket* tcp_sock_; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 41 | TestCompletionCallback callback_; |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 42 | scoped_ptr<MockHostResolver> host_resolver_; |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 43 | scoped_ptr<SocketDataProvider> data_; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | SOCKSClientSocketTest::SOCKSClientSocketTest() |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 47 | : host_resolver_(new MockHostResolver) { |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Set up platform before every test case |
| 51 | void SOCKSClientSocketTest::SetUp() { |
| 52 | PlatformTest::SetUp(); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | SOCKSClientSocket* SOCKSClientSocketTest::BuildMockSocket( |
| 56 | MockRead reads[], |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 57 | size_t reads_count, |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 58 | MockWrite writes[], |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 59 | size_t writes_count, |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 60 | HostResolver* host_resolver, |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 61 | const std::string& hostname, |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 62 | int port, |
| 63 | NetLog* net_log) { |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 64 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 65 | TestCompletionCallback callback; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 66 | data_.reset(new StaticSocketDataProvider(reads, reads_count, |
| 67 | writes, writes_count)); |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 68 | tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 69 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 70 | int rv = tcp_sock_->Connect(callback.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 71 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 72 | rv = callback.WaitForResult(); |
| 73 | EXPECT_EQ(OK, rv); |
| 74 | EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 75 | |
| 76 | return new SOCKSClientSocket(tcp_sock_, |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 77 | HostResolver::RequestInfo(HostPortPair(hostname, port)), |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 78 | host_resolver); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 81 | // Implementation of HostResolver that never completes its resolve request. |
| 82 | // We use this in the test "DisconnectWhileHostResolveInProgress" to make |
| 83 | // sure that the outstanding resolve request gets cancelled. |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 84 | class HangingHostResolverWithCancel : public HostResolver { |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 85 | public: |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 86 | HangingHostResolverWithCancel() : outstanding_request_(NULL) {} |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 87 | |
| 88 | virtual int Resolve(const RequestInfo& info, |
| 89 | AddressList* addresses, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 90 | const CompletionCallback& callback, |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 91 | RequestHandle* out_req, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 92 | const BoundNetLog& net_log) OVERRIDE { |
| 93 | DCHECK(addresses); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 94 | DCHECK_EQ(false, callback.is_null()); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 95 | EXPECT_FALSE(HasOutstandingRequest()); |
| 96 | outstanding_request_ = reinterpret_cast<RequestHandle>(1); |
| 97 | *out_req = outstanding_request_; |
| 98 | return ERR_IO_PENDING; |
| 99 | } |
| 100 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 101 | virtual int ResolveFromCache(const RequestInfo& info, |
| 102 | AddressList* addresses, |
| 103 | const BoundNetLog& net_log) OVERRIDE { |
| 104 | NOTIMPLEMENTED(); |
| 105 | return ERR_UNEXPECTED; |
| 106 | } |
| 107 | |
| 108 | virtual void CancelRequest(RequestHandle req) OVERRIDE { |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 109 | EXPECT_TRUE(HasOutstandingRequest()); |
| 110 | EXPECT_EQ(outstanding_request_, req); |
| 111 | outstanding_request_ = NULL; |
| 112 | } |
| 113 | |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 114 | bool HasOutstandingRequest() { |
| 115 | return outstanding_request_ != NULL; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | RequestHandle outstanding_request_; |
| 120 | |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(HangingHostResolverWithCancel); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 122 | }; |
| 123 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 124 | // Tests a complete handshake and the disconnection. |
| 125 | TEST_F(SOCKSClientSocketTest, CompleteHandshake) { |
| 126 | const std::string payload_write = "random data"; |
| 127 | const std::string payload_read = "moar random data"; |
| 128 | |
| 129 | MockWrite data_writes[] = { |
| 130 | MockWrite(true, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)), |
| 131 | MockWrite(true, payload_write.data(), payload_write.size()) }; |
| 132 | MockRead data_reads[] = { |
| 133 | MockRead(true, kSOCKSOkReply, arraysize(kSOCKSOkReply)), |
| 134 | MockRead(true, payload_read.data(), payload_read.size()) }; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 135 | CapturingNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 136 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 137 | user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), |
| 138 | data_writes, arraysize(data_writes), |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 139 | host_resolver_.get(), |
| 140 | "localhost", 80, |
| 141 | &log)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 142 | |
| 143 | // At this state the TCP connection is completed but not the SOCKS handshake. |
| 144 | EXPECT_TRUE(tcp_sock_->IsConnected()); |
| 145 | EXPECT_FALSE(user_sock_->IsConnected()); |
| 146 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 147 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 148 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 149 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 150 | net::CapturingNetLog::EntryList entries; |
| 151 | log.GetEntries(&entries); |
| 152 | EXPECT_TRUE( |
| 153 | LogContainsBeginEvent(entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 154 | EXPECT_FALSE(user_sock_->IsConnected()); |
| 155 | |
| 156 | rv = callback_.WaitForResult(); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 157 | EXPECT_EQ(OK, rv); |
| 158 | EXPECT_TRUE(user_sock_->IsConnected()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 159 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 160 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 161 | entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 162 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 163 | scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 164 | memcpy(buffer->data(), payload_write.data(), payload_write.size()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 165 | rv = user_sock_->Write(buffer, payload_write.size(), callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 166 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 167 | rv = callback_.WaitForResult(); |
| 168 | EXPECT_EQ(static_cast<int>(payload_write.size()), rv); |
| 169 | |
| 170 | buffer = new IOBuffer(payload_read.size()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 171 | rv = user_sock_->Read(buffer, payload_read.size(), callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 172 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 173 | rv = callback_.WaitForResult(); |
| 174 | EXPECT_EQ(static_cast<int>(payload_read.size()), rv); |
| 175 | EXPECT_EQ(payload_read, std::string(buffer->data(), payload_read.size())); |
| 176 | |
| 177 | user_sock_->Disconnect(); |
| 178 | EXPECT_FALSE(tcp_sock_->IsConnected()); |
| 179 | EXPECT_FALSE(user_sock_->IsConnected()); |
| 180 | } |
| 181 | |
| 182 | // List of responses from the socks server and the errors they should |
| 183 | // throw up are tested here. |
| 184 | TEST_F(SOCKSClientSocketTest, HandshakeFailures) { |
| 185 | const struct { |
| 186 | const char fail_reply[8]; |
| 187 | Error fail_code; |
| 188 | } tests[] = { |
| 189 | // Failure of the server response code |
| 190 | { |
| 191 | { 0x01, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }, |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 192 | ERR_SOCKS_CONNECTION_FAILED, |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 193 | }, |
| 194 | // Failure of the null byte |
| 195 | { |
| 196 | { 0x00, 0x5B, 0x00, 0x00, 0, 0, 0, 0 }, |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 197 | ERR_SOCKS_CONNECTION_FAILED, |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 198 | }, |
| 199 | }; |
| 200 | |
| 201 | //--------------------------------------- |
| 202 | |
| 203 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 204 | MockWrite data_writes[] = { |
| 205 | MockWrite(false, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 206 | MockRead data_reads[] = { |
| 207 | MockRead(false, tests[i].fail_reply, arraysize(tests[i].fail_reply)) }; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 208 | CapturingNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 209 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 210 | user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), |
| 211 | data_writes, arraysize(data_writes), |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 212 | host_resolver_.get(), |
| 213 | "localhost", 80, |
| 214 | &log)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 215 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 216 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 217 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 218 | |
| 219 | net::CapturingNetLog::EntryList entries; |
| 220 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 221 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 222 | entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 223 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 224 | rv = callback_.WaitForResult(); |
| 225 | EXPECT_EQ(tests[i].fail_code, rv); |
| 226 | EXPECT_FALSE(user_sock_->IsConnected()); |
| 227 | EXPECT_TRUE(tcp_sock_->IsConnected()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 228 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 229 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 230 | entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | // Tests scenario when the server sends the handshake response in |
| 235 | // more than one packet. |
| 236 | TEST_F(SOCKSClientSocketTest, PartialServerReads) { |
| 237 | const char kSOCKSPartialReply1[] = { 0x00 }; |
| 238 | const char kSOCKSPartialReply2[] = { 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 239 | |
| 240 | MockWrite data_writes[] = { |
| 241 | MockWrite(true, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 242 | MockRead data_reads[] = { |
| 243 | MockRead(true, kSOCKSPartialReply1, arraysize(kSOCKSPartialReply1)), |
| 244 | MockRead(true, kSOCKSPartialReply2, arraysize(kSOCKSPartialReply2)) }; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 245 | CapturingNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 246 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 247 | user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), |
| 248 | data_writes, arraysize(data_writes), |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 249 | host_resolver_.get(), |
| 250 | "localhost", 80, |
| 251 | &log)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 252 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 253 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 254 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 255 | net::CapturingNetLog::EntryList entries; |
| 256 | log.GetEntries(&entries); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 257 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 258 | entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 259 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 260 | rv = callback_.WaitForResult(); |
| 261 | EXPECT_EQ(OK, rv); |
| 262 | EXPECT_TRUE(user_sock_->IsConnected()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 263 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 264 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 265 | entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Tests scenario when the client sends the handshake request in |
| 269 | // more than one packet. |
| 270 | TEST_F(SOCKSClientSocketTest, PartialClientWrites) { |
| 271 | const char kSOCKSPartialRequest1[] = { 0x04, 0x01 }; |
| 272 | const char kSOCKSPartialRequest2[] = { 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 273 | |
| 274 | MockWrite data_writes[] = { |
| 275 | MockWrite(true, arraysize(kSOCKSPartialRequest1)), |
| 276 | // simulate some empty writes |
| 277 | MockWrite(true, 0), |
| 278 | MockWrite(true, 0), |
| 279 | MockWrite(true, kSOCKSPartialRequest2, |
| 280 | arraysize(kSOCKSPartialRequest2)) }; |
| 281 | MockRead data_reads[] = { |
| 282 | MockRead(true, kSOCKSOkReply, arraysize(kSOCKSOkReply)) }; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 283 | CapturingNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 284 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 285 | user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), |
| 286 | data_writes, arraysize(data_writes), |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 287 | host_resolver_.get(), |
| 288 | "localhost", 80, |
| 289 | &log)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 290 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 291 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 292 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 293 | net::CapturingNetLog::EntryList entries; |
| 294 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 295 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 296 | entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 297 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 298 | rv = callback_.WaitForResult(); |
| 299 | EXPECT_EQ(OK, rv); |
| 300 | EXPECT_TRUE(user_sock_->IsConnected()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 301 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 302 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 303 | entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Tests the case when the server sends a smaller sized handshake data |
| 307 | // and closes the connection. |
| 308 | TEST_F(SOCKSClientSocketTest, FailedSocketRead) { |
| 309 | MockWrite data_writes[] = { |
| 310 | MockWrite(true, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) }; |
| 311 | MockRead data_reads[] = { |
| 312 | MockRead(true, kSOCKSOkReply, arraysize(kSOCKSOkReply) - 2), |
| 313 | // close connection unexpectedly |
| 314 | MockRead(false, 0) }; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 315 | CapturingNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 316 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 317 | user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), |
| 318 | data_writes, arraysize(data_writes), |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 319 | host_resolver_.get(), |
| 320 | "localhost", 80, |
| 321 | &log)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 322 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 323 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 324 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 325 | net::CapturingNetLog::EntryList entries; |
| 326 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 327 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 328 | entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 329 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 330 | rv = callback_.WaitForResult(); |
| 331 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
| 332 | EXPECT_FALSE(user_sock_->IsConnected()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 333 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 334 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 335 | entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 338 | // Tries to connect to an unknown hostname. Should fail rather than |
| 339 | // falling back to SOCKS4a. |
| 340 | TEST_F(SOCKSClientSocketTest, FailedDNS) { |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 341 | const char hostname[] = "unresolved.ipv4.address"; |
| 342 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 343 | host_resolver_->rules()->AddSimulatedFailure(hostname); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 344 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 345 | CapturingNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 346 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 347 | user_sock_.reset(BuildMockSocket(NULL, 0, |
| 348 | NULL, 0, |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 349 | host_resolver_.get(), |
| 350 | hostname, 80, |
| 351 | &log)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 352 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 353 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 354 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 355 | net::CapturingNetLog::EntryList entries; |
| 356 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 357 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 358 | entries, 0, NetLog::TYPE_SOCKS_CONNECT)); |
| 359 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 360 | rv = callback_.WaitForResult(); |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 361 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 362 | EXPECT_FALSE(user_sock_->IsConnected()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 363 | log.GetEntries(&entries); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 364 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 365 | entries, -1, NetLog::TYPE_SOCKS_CONNECT)); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 368 | // Calls Disconnect() while a host resolve is in progress. The outstanding host |
| 369 | // resolve should be cancelled. |
| 370 | TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 371 | scoped_ptr<HangingHostResolverWithCancel> hanging_resolver( |
| 372 | new HangingHostResolverWithCancel()); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 373 | |
| 374 | // Doesn't matter what the socket data is, we will never use it -- garbage. |
| 375 | MockWrite data_writes[] = { MockWrite(false, "", 0) }; |
| 376 | MockRead data_reads[] = { MockRead(false, "", 0) }; |
| 377 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 378 | user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), |
| 379 | data_writes, arraysize(data_writes), |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 380 | hanging_resolver.get(), |
| 381 | "foo", 80, |
| 382 | NULL)); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 383 | |
| 384 | // Start connecting (will get stuck waiting for the host to resolve). |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame^] | 385 | int rv = user_sock_->Connect(callback_.callback()); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 386 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 387 | |
| 388 | EXPECT_FALSE(user_sock_->IsConnected()); |
| 389 | EXPECT_FALSE(user_sock_->IsConnectedAndIdle()); |
| 390 | |
| 391 | // The host resolver should have received the resolve request. |
| 392 | EXPECT_TRUE(hanging_resolver->HasOutstandingRequest()); |
| 393 | |
| 394 | // Disconnect the SOCKS socket -- this should cancel the outstanding resolve. |
| 395 | user_sock_->Disconnect(); |
| 396 | |
| 397 | EXPECT_FALSE(hanging_resolver->HasOutstandingRequest()); |
| 398 | |
| 399 | EXPECT_FALSE(user_sock_->IsConnected()); |
| 400 | EXPECT_FALSE(user_sock_->IsConnectedAndIdle()); |
| 401 | } |
| 402 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 403 | } // namespace net |