Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Unified Diff: net/socket/socks5_client_socket_unittest.cc

Issue 552164: Merge 34903, 34928, 35008, 35549, 36054 to the 249s branch.... (Closed) Base URL: svn://chrome-svn/chrome/branches/249s/src/
Patch Set: Fix some other merge conflicts Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/socket/socks5_client_socket.cc ('k') | net/socket_stream/socket_stream.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/socket/socks5_client_socket_unittest.cc
===================================================================
--- net/socket/socks5_client_socket_unittest.cc (revision 37149)
+++ net/socket/socks5_client_socket_unittest.cc (working copy)
@@ -4,6 +4,7 @@
#include "net/socket/socks5_client_socket.h"
+#include <algorithm>
#include <map>
#include "build/build_config.h"
#if defined(OS_WIN)
@@ -27,6 +28,8 @@
namespace net {
+namespace {
+
// Base class to test SOCKS5ClientSocket
class SOCKS5ClientSocketTest : public PlatformTest {
public:
@@ -36,6 +39,7 @@
MockWrite writes[],
const std::string& hostname,
int port);
+
virtual void SetUp();
protected:
@@ -70,7 +74,6 @@
MockWrite writes[],
const std::string& hostname,
int port) {
-
TestCompletionCallback callback;
data_.reset(new StaticSocketDataProvider(reads, writes));
tcp_sock_ = new MockTCPClientSocket(address_list_, data_.get());
@@ -82,23 +85,31 @@
EXPECT_TRUE(tcp_sock_->IsConnected());
return new SOCKS5ClientSocket(tcp_sock_,
- HostResolver::RequestInfo(hostname, port),
- host_resolver_);
+ HostResolver::RequestInfo(hostname, port));
}
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 };
+
// 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";
+ const char kSOCKS5OkRequest[] = {
+ 0x05, // Version
+ 0x01, // Command (CONNECT)
+ 0x00, // Reserved.
+ 0x03, // Address type (DOMAINNAME).
+ 0x09, // Length of domain (9)
+ // Domain string:
+ 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
+ 0x00, 0x50, // 16-bit port (80)
+ };
+
MockWrite data_writes[] = {
MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)),
@@ -125,8 +136,6 @@
EXPECT_EQ(OK, rv);
EXPECT_TRUE(user_sock_->IsConnected());
- EXPECT_EQ(SOCKS5ClientSocket::kEndPointResolvedIPv4,
- user_sock_->address_type_);
EXPECT_TRUE(LogContains(
*log, -1, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_END));
@@ -149,82 +158,76 @@
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 };
+// Test that you can call Connect() again after having called Disconnect().
+TEST_F(SOCKS5ClientSocketTest, ConnectAndDisconnectTwice) {
+ const std::string hostname = "my-host-name";
+ const char kSOCKS5DomainRequest[] = {
+ 0x05, // VER
+ 0x01, // CMD
+ 0x00, // RSV
+ 0x03, // ATYPE
+ };
- host_resolver_->rules()->AddSimulatedFailure(hostname.c_str());
-
- std::string request(kSOCKS5DomainRequest,
- arraysize(kSOCKS5DomainRequest));
+ 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)) };
+ for (int i = 0; i < 2; ++i) {
+ 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));
+ user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80));
- scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
- int rv = user_sock_->Connect(&callback_, log);
- EXPECT_EQ(ERR_IO_PENDING, rv);
- EXPECT_TRUE(LogContains(
- *log, 0, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_BEGIN));
- rv = callback_.WaitForResult();
- EXPECT_EQ(OK, rv);
- EXPECT_TRUE(user_sock_->IsConnected());
- EXPECT_EQ(SOCKS5ClientSocket::kEndPointFailedDomain,
- user_sock_->address_type_);
- EXPECT_TRUE(LogContains(
- *log, -1, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_END));
+ int rv = user_sock_->Connect(&callback_, NULL);
+ EXPECT_EQ(OK, rv);
+ EXPECT_TRUE(user_sock_->IsConnected());
+
+ user_sock_->Disconnect();
+ EXPECT_FALSE(user_sock_->IsConnected());
+ }
}
-// 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 };
+// Test that we fail trying to connect to a hosname longer than 255 bytes.
+TEST_F(SOCKS5ClientSocketTest, LargeHostNameFails) {
+ // Create a string of length 256, where each character is 'x'.
+ std::string large_host_name;
+ std::fill_n(std::back_inserter(large_host_name), 256, 'x');
- host_resolver_->rules()->AddIPv6Rule(hostname, "2001:db8:8714:3a90::12");
+ // Create a SOCKS socket, with mock transport socket.
+ // (Data from these mocks will not be used).
+ MockWrite data_writes[] = {MockWrite(false, ERR_UNEXPECTED)};
+ MockRead data_reads[] = {MockRead(false, ERR_UNEXPECTED)};
+ user_sock_.reset(BuildMockSocket(data_reads, data_writes,
+ large_host_name, 80));
- 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));
-
- scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
- int rv = user_sock_->Connect(&callback_, log);
- EXPECT_EQ(ERR_IO_PENDING, rv);
- EXPECT_TRUE(LogContains(
- *log, 0, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_BEGIN));
- rv = callback_.WaitForResult();
- EXPECT_EQ(OK, rv);
- EXPECT_TRUE(user_sock_->IsConnected());
- EXPECT_EQ(SOCKS5ClientSocket::kEndPointResolvedIPv6,
- user_sock_->address_type_);
- EXPECT_TRUE(LogContains(
- *log, -1, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_END));
+ // Try to connect -- should fail (without having read/written anything to
+ // the transport socket first) because the hostname is too long.
+ TestCompletionCallback callback;
+ int rv = user_sock_->Connect(&callback, NULL);
+ EXPECT_EQ(ERR_INVALID_URL, rv);
}
TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) {
const std::string hostname = "www.google.com";
+ const char kSOCKS5OkRequest[] = {
+ 0x05, // Version
+ 0x01, // Command (CONNECT)
+ 0x00, // Reserved.
+ 0x03, // Address type (DOMAINNAME).
+ 0x0E, // Length of domain (14)
+ // Domain string:
+ 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm',
+ 0x00, 0x50, // 16-bit port (80)
+ };
+
// Test for partial greet request write
{
const char partial1[] = { 0x05, 0x01 };
@@ -273,14 +276,17 @@
*log, -1, LoadLog::TYPE_SOCKS5_CONNECT, LoadLog::PHASE_END));
}
- // Test for partial handshake request write
+ // Test for partial handshake request write.
{
- const char partial1[] = { 0x05, 0x01, 0x00 };
- const char partial2[] = { 0x01, 127, 0, 0, 1, 0x00, 0x50 };
+ // NOTE: in this branch MockWrite doesn't support partial successes, so
+ // the MockWrite must correspond exactly with the request.
+ const int kSplitPoint = arraysize(kSOCKS5OkRequest);
MockWrite data_writes[] = {
MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(true, arraysize(partial1)),
- MockWrite(true, partial2, arraysize(partial2)) };
+ MockWrite(true, kSOCKS5OkRequest, kSplitPoint),
+ MockWrite(true, kSOCKS5OkRequest + kSplitPoint,
+ arraysize(kSOCKS5OkRequest) - kSplitPoint)
+ };
MockRead data_reads[] = {
MockRead(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
MockRead(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)) };
@@ -299,15 +305,17 @@
// Test for partial handshake response read
{
- const char partial1[] = { 0x05, 0x00, 0x00, 0x01, 127, 0 };
- const char partial2[] = { 0, 1, 0x00, 0x50 };
+ const int kSplitPoint = 6; // Break the handshake read into two parts.
MockWrite data_writes[] = {
MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)),
- MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)) };
+ MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest))
+ };
MockRead data_reads[] = {
MockRead(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)),
- MockRead(true, partial1, arraysize(partial1)),
- MockRead(true, partial2, arraysize(partial2)) };
+ MockRead(true, kSOCKS5OkResponse, kSplitPoint),
+ MockRead(true, kSOCKS5OkResponse + kSplitPoint, arraysize(kSOCKS5OkResponse) - kSplitPoint)
+ };
+
user_sock_.reset(BuildMockSocket(data_reads, data_writes, hostname, 80));
scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
int rv = user_sock_->Connect(&callback_, log);
@@ -322,4 +330,6 @@
}
}
+} // namespace
+
} // namespace net
« no previous file with comments | « net/socket/socks5_client_socket.cc ('k') | net/socket_stream/socket_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698