summaryrefslogtreecommitdiffstats
path: root/net/socket/socks_client_socket_unittest.cc
blob: 32eb7258534025ba5bab290a9cabf397e95d7031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket/socks_client_socket.h"

#include "net/base/address_list.h"
#include "net/base/listen_socket.h"
#include "net/base/mock_host_resolver.h"
#include "net/base/test_completion_callback.h"
#include "net/base/winsock_init.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/tcp_client_socket.h"
#include "net/socket/socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

//-----------------------------------------------------------------------------

namespace net {

const char kSOCKSOkRequest[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 };
const char kSOCKS4aInitialRequest[] =
    { 0x04, 0x01, 0x00, 0x50, 0, 0, 0, 127, 0 };
const char kSOCKSOkReply[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 };

class SOCKSClientSocketTest : public PlatformTest {
 public:
  SOCKSClientSocketTest();
  // Create a SOCKSClientSocket on top of a MockSocket.
  SOCKSClientSocket* BuildMockSocket(MockRead reads[], MockWrite writes[],
                                     const std::string& hostname, int port);
  virtual void SetUp();

 protected:
  scoped_ptr<SOCKSClientSocket> user_sock_;
  AddressList address_list_;
  ClientSocket* tcp_sock_;
  TestCompletionCallback callback_;
  scoped_refptr<MockHostResolver> host_resolver_;
  scoped_ptr<MockSocket> mock_socket_;

 private:
  DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocketTest);
};

SOCKSClientSocketTest::SOCKSClientSocketTest()
  : host_resolver_(new MockHostResolver) {
}

// Set up platform before every test case
void SOCKSClientSocketTest::SetUp() {
  PlatformTest::SetUp();
}

SOCKSClientSocket* SOCKSClientSocketTest::BuildMockSocket(
    MockRead reads[],
    MockWrite writes[],
    const std::string& hostname,
    int port) {

  TestCompletionCallback callback;
  mock_socket_.reset(new StaticMockSocket(reads, writes));
  tcp_sock_ = new MockTCPClientSocket(address_list_, mock_socket_.get());

  int rv = tcp_sock_->Connect(&callback);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback.WaitForResult();
  EXPECT_EQ(OK, rv);
  EXPECT_TRUE(tcp_sock_->IsConnected());

  return new SOCKSClientSocket(tcp_sock_,
      HostResolver::RequestInfo(hostname, port),
      host_resolver_);
}

// Tests a complete handshake and the disconnection.
TEST_F(SOCKSClientSocketTest, CompleteHandshake) {
  const std::string payload_write = "random data";
  const std::string payload_read = "moar random data";

  MockWrite data_writes[] = {
      MockWrite(true, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)),
      MockWrite(true, payload_write.data(), payload_write.size()) };
  MockRead data_reads[] = {
      MockRead(true, kSOCKSOkReply, arraysize(kSOCKSOkReply)),
      MockRead(true, payload_read.data(), payload_read.size()) };

  user_sock_.reset(BuildMockSocket(data_reads, data_writes, "localhost", 80));

  // At this state the TCP connection is completed but not the SOCKS handshake.
  EXPECT_TRUE(tcp_sock_->IsConnected());
  EXPECT_FALSE(user_sock_->IsConnected());

  int rv = user_sock_->Connect(&callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  EXPECT_FALSE(user_sock_->IsConnected());
  rv = callback_.WaitForResult();

  EXPECT_EQ(OK, rv);
  EXPECT_TRUE(user_sock_->IsConnected());
  EXPECT_EQ(SOCKSClientSocket::kSOCKS4, user_sock_->socks_version_);

  scoped_refptr<IOBuffer> buffer = new IOBuffer(payload_write.size());
  memcpy(buffer->data(), payload_write.data(), payload_write.size());
  rv = user_sock_->Write(buffer, payload_write.size(), &callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(static_cast<int>(payload_write.size()), rv);

  buffer = new IOBuffer(payload_read.size());
  rv = user_sock_->Read(buffer, payload_read.size(), &callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(static_cast<int>(payload_read.size()), rv);
  EXPECT_EQ(payload_read, std::string(buffer->data(), payload_read.size()));

  user_sock_->Disconnect();
  EXPECT_FALSE(tcp_sock_->IsConnected());
  EXPECT_FALSE(user_sock_->IsConnected());
}

// List of responses from the socks server and the errors they should
// throw up are tested here.
TEST_F(SOCKSClientSocketTest, HandshakeFailures) {
  const struct {
    const char fail_reply[8];
    Error fail_code;
  } tests[] = {
    // Failure of the server response code
    {
      { 0x01, 0x5A, 0x00, 0x00, 0, 0, 0, 0 },
      ERR_INVALID_RESPONSE,
    },
    // Failure of the null byte
    {
      { 0x00, 0x5B, 0x00, 0x00, 0, 0, 0, 0 },
      ERR_FAILED,
    },
  };

  //---------------------------------------

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
    MockWrite data_writes[] = {
        MockWrite(false, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) };
    MockRead data_reads[] = {
        MockRead(false, tests[i].fail_reply, arraysize(tests[i].fail_reply)) };

    user_sock_.reset(BuildMockSocket(data_reads, data_writes, "localhost", 80));

    int rv = user_sock_->Connect(&callback_);
    EXPECT_EQ(ERR_IO_PENDING, rv);
    rv = callback_.WaitForResult();
    EXPECT_EQ(tests[i].fail_code, rv);
    EXPECT_FALSE(user_sock_->IsConnected());
    EXPECT_TRUE(tcp_sock_->IsConnected());
  }
}

// Tests scenario when the server sends the handshake response in
// more than one packet.
TEST_F(SOCKSClientSocketTest, PartialServerReads) {
  const char kSOCKSPartialReply1[] = { 0x00 };
  const char kSOCKSPartialReply2[] = { 0x5A, 0x00, 0x00, 0, 0, 0, 0 };

  MockWrite data_writes[] = {
      MockWrite(true, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) };
  MockRead data_reads[] = {
      MockRead(true, kSOCKSPartialReply1, arraysize(kSOCKSPartialReply1)),
      MockRead(true, kSOCKSPartialReply2, arraysize(kSOCKSPartialReply2)) };

  user_sock_.reset(BuildMockSocket(data_reads, data_writes, "localhost", 80));

  int rv = user_sock_->Connect(&callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(OK, rv);
  EXPECT_TRUE(user_sock_->IsConnected());
}

// Tests scenario when the client sends the handshake request in
// more than one packet.
TEST_F(SOCKSClientSocketTest, PartialClientWrites) {
  const char kSOCKSPartialRequest1[] = { 0x04, 0x01 };
  const char kSOCKSPartialRequest2[] = { 0x00, 0x50, 127, 0, 0, 1, 0 };

  MockWrite data_writes[] = {
      MockWrite(true, arraysize(kSOCKSPartialRequest1)),
      // simulate some empty writes
      MockWrite(true, 0),
      MockWrite(true, 0),
      MockWrite(true, kSOCKSPartialRequest2,
                arraysize(kSOCKSPartialRequest2)) };
  MockRead data_reads[] = {
      MockRead(true, kSOCKSOkReply, arraysize(kSOCKSOkReply)) };

  user_sock_.reset(BuildMockSocket(data_reads, data_writes, "localhost", 80));

  int rv = user_sock_->Connect(&callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(OK, rv);
  EXPECT_TRUE(user_sock_->IsConnected());
}

// Tests the case when the server sends a smaller sized handshake data
// and closes the connection.
TEST_F(SOCKSClientSocketTest, FailedSocketRead) {
  MockWrite data_writes[] = {
      MockWrite(true, kSOCKSOkRequest, arraysize(kSOCKSOkRequest)) };
  MockRead data_reads[] = {
      MockRead(true, kSOCKSOkReply, arraysize(kSOCKSOkReply) - 2),
      // close connection unexpectedly
      MockRead(false, 0) };

  user_sock_.reset(BuildMockSocket(data_reads, data_writes, "localhost", 80));

  int rv = user_sock_->Connect(&callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(ERR_CONNECTION_CLOSED, rv);
  EXPECT_FALSE(user_sock_->IsConnected());
}

// Tries to connect to an unknown DNS and on failure should revert to SOCKS4A.
TEST_F(SOCKSClientSocketTest, SOCKS4AFailedDNS) {
  const char hostname[] = "unresolved.ipv4.address";

  host_resolver_->rules()->AddSimulatedFailure(hostname);

  std::string request(kSOCKS4aInitialRequest,
                      arraysize(kSOCKS4aInitialRequest));
  request.append(hostname, arraysize(hostname));

  MockWrite data_writes[] = {
      MockWrite(false, request.data(), request.size()) };
  MockRead data_reads[] = {
      MockRead(false, kSOCKSOkReply, arraysize(kSOCKSOkReply)) };

  user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80));

  int rv = user_sock_->Connect(&callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(OK, rv);
  EXPECT_TRUE(user_sock_->IsConnected());
  EXPECT_EQ(SOCKSClientSocket::kSOCKS4a, user_sock_->socks_version_);
}

// Tries to connect to a domain that resolves to IPv6.
// Should revert to SOCKS4a.
TEST_F(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6) {
  const char hostname[] = "an.ipv6.address";

  host_resolver_->rules()->AddIPv6Rule(hostname, "2001:db8:8714:3a90::12");

  std::string request(kSOCKS4aInitialRequest,
                      arraysize(kSOCKS4aInitialRequest));
  request.append(hostname, arraysize(hostname));

  MockWrite data_writes[] = {
      MockWrite(false, request.data(), request.size()) };
  MockRead data_reads[] = {
      MockRead(false, kSOCKSOkReply, arraysize(kSOCKSOkReply)) };

  user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80));

  int rv = user_sock_->Connect(&callback_);
  EXPECT_EQ(ERR_IO_PENDING, rv);
  rv = callback_.WaitForResult();
  EXPECT_EQ(OK, rv);
  EXPECT_TRUE(user_sock_->IsConnected());
  EXPECT_EQ(SOCKSClientSocket::kSOCKS4a, user_sock_->socks_version_);
}

}  // namespace net