summaryrefslogtreecommitdiffstats
path: root/net/socket/socks5_client_socket_unittest.cc
diff options
context:
space:
mode:
authorarindam@chromium.org <arindam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-14 17:17:24 +0000
committerarindam@chromium.org <arindam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-14 17:17:24 +0000
commite47dfdaef4d1caea9ec18e6446d699c9115b9e93 (patch)
treebfc5d734c585c99d6fc5f1679cca8acdf1aa8eda /net/socket/socks5_client_socket_unittest.cc
parent140af009d3d76623b106ee3e5e554d770af0e3c5 (diff)
downloadchromium_src-e47dfdaef4d1caea9ec18e6446d699c9115b9e93.zip
chromium_src-e47dfdaef4d1caea9ec18e6446d699c9115b9e93.tar.gz
chromium_src-e47dfdaef4d1caea9ec18e6446d699c9115b9e93.tar.bz2
Reverting 20626.
TBR=eroman Review URL: http://codereview.chromium.org/155505 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20628 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket/socks5_client_socket_unittest.cc')
-rw-r--r--net/socket/socks5_client_socket_unittest.cc307
1 files changed, 0 insertions, 307 deletions
diff --git a/net/socket/socks5_client_socket_unittest.cc b/net/socket/socks5_client_socket_unittest.cc
deleted file mode 100644
index e62f191..0000000
--- a/net/socket/socks5_client_socket_unittest.cc
+++ /dev/null
@@ -1,307 +0,0 @@
-// 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/socks5_client_socket.h"
-
-#include <map>
-#include "build/build_config.h"
-#if defined(OS_WIN)
-#include <ws2tcpip.h>
-#elif defined(OS_POSIX)
-#include <netdb.h>
-#endif
-#include "net/base/address_list.h"
-#include "net/base/host_resolver_unittest.h"
-#include "net/base/test_completion_callback.h"
-#include "net/base/winsock_init.h"
-#include "net/socket/client_socket_factory.h"
-#include "net/socket/socket_test_util.h"
-#include "net/socket/tcp_client_socket.h"
-#include "testing/gtest/include/gtest/gtest.h"
-#include "testing/platform_test.h"
-
-//-----------------------------------------------------------------------------
-
-namespace net {
-
-// Base class to test SOCKS5ClientSocket
-class SOCKS5ClientSocketTest : public PlatformTest {
- public:
- SOCKS5ClientSocketTest();
- // Create a SOCKSClientSocket on top of a MockSocket.
- SOCKS5ClientSocket* BuildMockSocket(MockRead reads[],
- MockWrite writes[],
- const std::string& hostname,
- int port);
- virtual void SetUp();
-
- protected:
- scoped_ptr<SOCKS5ClientSocket> user_sock_;
- AddressList address_list_;
- ClientSocket* tcp_sock_;
- ScopedHostMapper host_mapper_;
- TestCompletionCallback callback_;
- scoped_refptr<RuleBasedHostMapper> mapper_;
- scoped_refptr<HostResolver> host_resolver_;
- scoped_ptr<MockSocket> mock_socket_;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocketTest);
-};
-
-SOCKS5ClientSocketTest::SOCKS5ClientSocketTest()
- : host_resolver_(new HostResolver(0, 0)) {
-}
-
-// Set up platform before every test case
-void SOCKS5ClientSocketTest::SetUp() {
- PlatformTest::SetUp();
-
- // Resolve the "localhost" AddressList used by the TCP connection to connect.
- scoped_refptr<HostResolver> resolver = new HostResolver();
- HostResolver::RequestInfo info("www.socks-proxy.com", 1080);
- int rv = resolver->Resolve(info, &address_list_, NULL, NULL);
- ASSERT_EQ(OK, rv);
-
- // Create a new host mapping for the duration of this test case only.
- mapper_ = new RuleBasedHostMapper();
- host_mapper_.Init(mapper_);
-}
-
-SOCKS5ClientSocket* SOCKS5ClientSocketTest::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 SOCKS5ClientSocket(tcp_sock_,
- HostResolver::RequestInfo(hostname, port),
- host_resolver_);
-}
-
-const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 };
-const char kSOCKS5GreetResponse[] = { 0x05, 0x00 };
-
-const char kSOCKS5OkRequest[] =
- { 0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
-const char kSOCKS5OkResponse[] =
- { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
-
-const uint16 kNwPort = htons(80);
-
-// Tests a complete SOCKS5 handshake and the disconnection.
-TEST_F(SOCKS5ClientSocketTest, CompleteHandshake) {
- const std::string payload_write = "random data";
- const std::string payload_read = "moar random data";
-
- MockWrite data_writes[] = {
- MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)),
- MockWrite(true, payload_write.data(), payload_write.size()) };
- MockRead data_reads[] = {
- MockRead(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)),
- 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(SOCKS5ClientSocket::kEndPointResolvedIPv4,
- user_sock_->address_type_);
-
- 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());
-}
-
-// Tries to connect to a DNS which fails domain lookup.
-TEST_F(SOCKS5ClientSocketTest, FailedDNS) {
- const std::string hostname = "unresolved.ipv4.address";
- const char kSOCKS5DomainRequest[] = { 0x05, 0x01, 0x00, 0x03 };
-
- mapper_->AddSimulatedFailure(hostname.c_str());
-
- std::string request(kSOCKS5DomainRequest,
- arraysize(kSOCKS5DomainRequest));
- request.push_back(hostname.size());
- request.append(hostname);
- request.append(reinterpret_cast<const char*>(&kNwPort), sizeof(kNwPort));
-
- MockWrite data_writes[] = {
- MockWrite(false, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(false, request.data(), request.size()) };
- MockRead data_reads[] = {
- MockRead(false, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(false, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) };
-
- 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(SOCKS5ClientSocket::kEndPointFailedDomain,
- user_sock_->address_type_);
-}
-
-// Tries to connect to a domain that resolves to IPv6.
-TEST_F(SOCKS5ClientSocketTest, IPv6Domain) {
- const std::string hostname = "an.ipv6.address";
- const char kSOCKS5IPv6Request[] = { 0x05, 0x01, 0x00, 0x04 };
- const uint8 ipv6_addr[] = { 0x20, 0x01, 0x0d, 0xb8, 0x87, 0x14, 0x3a, 0x90,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x000, 0x00, 0x12 };
-
- mapper_->AddRule(hostname.c_str(), "2001:db8:8714:3a90::12");
-
- AddressList address_list;
- scoped_refptr<HostResolver> resolver = new HostResolver();
- HostResolver::RequestInfo info(hostname, 80);
- int rv = resolver->Resolve(info, &address_list, NULL, NULL);
- if (rv != OK || !address_list.head()) {
- // This machine does not support IPv6. We skip this test altogether.
- // TODO(arindam): create a MockIPv6HostResolver to manually
- // populate the |address_list| in case of a machine with no IPv6 suppport.
- return;
- }
-
- std::string request(kSOCKS5IPv6Request,
- arraysize(kSOCKS5IPv6Request));
- request.append(reinterpret_cast<const char*>(&ipv6_addr), sizeof(ipv6_addr));
- request.append(reinterpret_cast<const char*>(&kNwPort), sizeof(kNwPort));
-
- MockWrite data_writes[] = {
- MockWrite(false, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(false, request.data(), request.size()) };
- MockRead data_reads[] = {
- MockRead(false, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(false, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) };
-
- user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80));
-
- 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(SOCKS5ClientSocket::kEndPointResolvedIPv6,
- user_sock_->address_type_);
-}
-
-TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) {
- const std::string hostname = "www.google.com";
-
- // Test for partial greet request write
- {
- const char partial1[] = { 0x05, 0x01 };
- const char partial2[] = { 0x00 };
- MockWrite data_writes[] = {
- MockWrite(true, arraysize(partial1)),
- MockWrite(true, partial2, arraysize(partial2)),
- MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)) };
- MockRead data_reads[] = {
- MockRead(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) };
- 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());
- }
-
- // Test for partial greet response read
- {
- const char partial1[] = { 0x05 };
- const char partial2[] = { 0x00 };
- MockWrite data_writes[] = {
- MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)) };
- MockRead data_reads[] = {
- MockRead(true, partial1, arraysize(partial1)),
- MockRead(true, partial2, arraysize(partial2)),
- MockRead(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) };
- 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());
- }
-
- // Test for partial handshake request write
- {
- const char partial1[] = { 0x05, 0x01, 0x00 };
- const char partial2[] = { 0x01, 127, 0, 0, 1, 0x00, 0x50 };
- MockWrite data_writes[] = {
- MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(true, arraysize(partial1)),
- MockWrite(true, partial2, arraysize(partial2)) };
- MockRead data_reads[] = {
- MockRead(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) };
- 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());
- }
-
- // Test for partial handshake response read
- {
- const char partial1[] = { 0x05, 0x00, 0x00, 0x01, 127, 0 };
- const char partial2[] = { 0, 1, 0x00, 0x50 };
- MockWrite data_writes[] = {
- MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)) };
- MockRead data_reads[] = {
- MockRead(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(true, partial1, arraysize(partial1)),
- MockRead(true, partial2, arraysize(partial2)) };
- 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());
- }
-}
-
-} // namespace net
-