Skip to content

Commit

Permalink
Implement working tests for the simple QUIC client classes.
Browse files Browse the repository at this point in the history
Review URL: https://codereview.chromium.org/1017973002

Cr-Commit-Position: refs/heads/master@{#321704}
  • Loading branch information
rch-chromium-org authored and Commit bot committed Mar 22, 2015
1 parent e28e561 commit a6d42cd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 87 deletions.
1 change: 1 addition & 0 deletions net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ if (!is_android && !is_win && !is_mac) {
":http_server",
":net",
":quic_tools",
":simple_quic_tools",
":test_support",
"//base",
"//base:i18n",
Expand Down
1 change: 1 addition & 0 deletions net/net.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
'net_extras',
'net_test_support',
'quic_tools',
'simple_quic_tools',
],
'sources': [
'<@(net_test_sources)',
Expand Down
3 changes: 3 additions & 0 deletions net/net.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,9 @@
'tools/dump_cache/url_utilities.cc',
'tools/dump_cache/url_utilities.h',
'tools/dump_cache/url_utilities_unittest.cc',
'tools/quic/quic_simple_client_session_test.cc',
'tools/quic/quic_simple_client_stream_test.cc',
'tools/quic/quic_simple_client_test.cc',
'tools/tld_cleanup/tld_cleanup_util_unittest.cc',
'udp/udp_socket_unittest.cc',
'url_request/sdch_dictionary_fetcher_unittest.cc',
Expand Down
2 changes: 1 addition & 1 deletion net/quic/quic_data_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SSLInfo;
class NET_EXPORT_PRIVATE QuicDataStream : public ReliableQuicStream {
public:
// Visitor receives callbacks from the stream.
class Visitor {
class NET_EXPORT_PRIVATE Visitor {
public:
Visitor() {}

Expand Down
29 changes: 15 additions & 14 deletions net/tools/quic/quic_simple_client_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/tools/quic/quic_client_session.h"
#include "net/tools/quic/quic_simple_client_session.h"

#include <vector>

Expand All @@ -12,7 +12,7 @@
#include "net/quic/test_tools/crypto_test_utils.h"
#include "net/quic/test_tools/quic_session_peer.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/tools/quic/quic_spdy_client_stream.h"
#include "net/tools/quic/quic_simple_client_stream.h"
#include "testing/gtest/include/gtest/gtest.h"

using net::test::CryptoTestUtils;
Expand All @@ -35,13 +35,14 @@ namespace {
const char kServerHostname[] = "www.example.org";
const uint16 kPort = 80;

class ToolsQuicClientSessionTest
class QuicSimpleClientSessionTest
: public ::testing::TestWithParam<QuicVersion> {
protected:
ToolsQuicClientSessionTest()
QuicSimpleClientSessionTest()
: connection_(new PacketSavingConnection(Perspective::IS_CLIENT,
SupportedVersions(GetParam()))) {
session_.reset(new QuicClientSession(DefaultQuicConfig(), connection_));
session_.reset(new QuicSimpleClientSession(DefaultQuicConfig(),
connection_));
session_->InitializeSession(
QuicServerId(kServerHostname, kPort, false, PRIVACY_MODE_DISABLED),
&crypto_config_);
Expand All @@ -56,24 +57,24 @@ class ToolsQuicClientSessionTest
}

PacketSavingConnection* connection_;
scoped_ptr<QuicClientSession> session_;
scoped_ptr<QuicSimpleClientSession> session_;
QuicCryptoClientConfig crypto_config_;
};

INSTANTIATE_TEST_CASE_P(Tests, ToolsQuicClientSessionTest,
INSTANTIATE_TEST_CASE_P(Tests, QuicSimpleClientSessionTest,
::testing::ValuesIn(QuicSupportedVersions()));

TEST_P(ToolsQuicClientSessionTest, CryptoConnect) {
TEST_P(QuicSimpleClientSessionTest, CryptoConnect) {
CompleteCryptoHandshake();
}

TEST_P(ToolsQuicClientSessionTest, MaxNumStreams) {
TEST_P(QuicSimpleClientSessionTest, MaxNumStreams) {
session_->config()->SetMaxStreamsPerConnection(1, 1);

// Initialize crypto before the client session will create a stream.
CompleteCryptoHandshake();

QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream();
QuicSimpleClientStream* stream = session_->CreateOutgoingDataStream();
ASSERT_TRUE(stream);
EXPECT_FALSE(session_->CreateOutgoingDataStream());

Expand All @@ -83,7 +84,7 @@ TEST_P(ToolsQuicClientSessionTest, MaxNumStreams) {
EXPECT_TRUE(stream);
}

TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) {
TEST_P(QuicSimpleClientSessionTest, GoAwayReceived) {
CompleteCryptoHandshake();

// After receiving a GoAway, I should no longer be able to create outgoing
Expand All @@ -92,7 +93,7 @@ TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) {
EXPECT_EQ(nullptr, session_->CreateOutgoingDataStream());
}

TEST_P(ToolsQuicClientSessionTest, SetFecProtectionFromConfig) {
TEST_P(QuicSimpleClientSessionTest, SetFecProtectionFromConfig) {
ValueRestore<bool> old_flag(&FLAGS_enable_quic_fec, true);

// Set FEC config in client's connection options.
Expand All @@ -107,13 +108,13 @@ TEST_P(ToolsQuicClientSessionTest, SetFecProtectionFromConfig) {
// optionally protected.
EXPECT_EQ(FEC_PROTECT_ALWAYS,
QuicSessionPeer::GetHeadersStream(session_.get())->fec_policy());
QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream();
QuicSimpleClientStream* stream = session_->CreateOutgoingDataStream();
ASSERT_TRUE(stream);
EXPECT_EQ(FEC_PROTECT_OPTIONAL, stream->fec_policy());
}

// Regression test for b/17206611.
TEST_P(ToolsQuicClientSessionTest, InvalidPacketReceived) {
TEST_P(QuicSimpleClientSessionTest, InvalidPacketReceived) {
// Create Packet with 0 length.
QuicEncryptedPacket invalid_packet(nullptr, 0, false);
IPEndPoint server_address(TestPeerIPAddress(), kTestPort);
Expand Down
43 changes: 23 additions & 20 deletions net/tools/quic/quic_simple_client_stream_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/tools/quic/quic_spdy_client_stream.h"
#include "net/tools/quic/quic_simple_client_stream.h"

#include "base/strings/string_number_conversions.h"
#include "net/quic/quic_utils.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/tools/quic/quic_client_session.h"
#include "net/tools/quic/quic_spdy_client_stream.h"
#include "net/spdy/spdy_http_utils.h"
#include "net/tools/quic/quic_simple_client_session.h"
#include "net/tools/quic/spdy_utils.h"
#include "net/tools/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
Expand All @@ -26,70 +26,73 @@ namespace tools {
namespace test {
namespace {

class QuicSpdyClientStreamTest : public TestWithParam<QuicVersion> {
class QuicSimpleClientStreamTest : public TestWithParam<QuicVersion> {
public:
QuicSpdyClientStreamTest()
QuicSimpleClientStreamTest()
: connection_(
new StrictMock<MockConnection>(Perspective::IS_CLIENT,
SupportedVersions(GetParam()))),
session_(DefaultQuicConfig(), connection_),
headers_(new HttpResponseHeaders("")),
body_("hello world") {
session_.InitializeSession(
QuicServerId("example.com", 80, false, PRIVACY_MODE_DISABLED),
&crypto_config_);

headers_.SetResponseFirstlineFromStringPieces("HTTP/1.1", "200", "Ok");
headers_.ReplaceOrAppendHeader("content-length", "11");
headers_->ReplaceStatusLine("HTTP/1.1 200 Ok");
headers_->AddHeader("content-length: 11");

headers_string_ = SpdyUtils::SerializeResponseHeaders(headers_);
SpdyHeaderBlock header_block;
CreateSpdyHeadersFromHttpResponse(*headers_, SPDY3, &header_block);
headers_string_ = SpdyUtils::SerializeUncompressedHeaders(header_block);

// New streams rely on having the peer's flow control receive window
// negotiated in the config.
session_.config()->SetInitialStreamFlowControlWindowToSend(
kInitialStreamFlowControlWindowForTest);
session_.config()->SetInitialSessionFlowControlWindowToSend(
kInitialSessionFlowControlWindowForTest);
stream_.reset(new QuicSpdyClientStream(3, &session_));
stream_.reset(new QuicSimpleClientStream(3, &session_));
}

StrictMock<MockConnection>* connection_;
QuicClientSession session_;
scoped_ptr<QuicSpdyClientStream> stream_;
BalsaHeaders headers_;
QuicSimpleClientSession session_;
scoped_ptr<QuicSimpleClientStream> stream_;
scoped_refptr<HttpResponseHeaders> headers_;
string headers_string_;
string body_;
QuicCryptoClientConfig crypto_config_;
};

INSTANTIATE_TEST_CASE_P(Tests, QuicSpdyClientStreamTest,
INSTANTIATE_TEST_CASE_P(Tests, QuicSimpleClientStreamTest,
::testing::ValuesIn(QuicSupportedVersions()));

TEST_P(QuicSpdyClientStreamTest, TestFraming) {
TEST_P(QuicSimpleClientStreamTest, TestFraming) {
EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
headers_string_.c_str(), headers_string_.size()));
EXPECT_EQ(body_.size(),
stream_->ProcessData(body_.c_str(), body_.size()));
EXPECT_EQ(200u, stream_->headers().parsed_response_code());
EXPECT_EQ(200, stream_->headers()->response_code());
EXPECT_EQ(body_, stream_->data());
}

TEST_P(QuicSpdyClientStreamTest, TestFramingOnePacket) {
TEST_P(QuicSimpleClientStreamTest, TestFramingOnePacket) {
string message = headers_string_ + body_;

EXPECT_EQ(message.size(), stream_->ProcessData(
message.c_str(), message.size()));
EXPECT_EQ(200u, stream_->headers().parsed_response_code());
EXPECT_EQ(200, stream_->headers()->response_code());
EXPECT_EQ(body_, stream_->data());
}

TEST_P(QuicSpdyClientStreamTest, DISABLED_TestFramingExtraData) {
TEST_P(QuicSimpleClientStreamTest, DISABLED_TestFramingExtraData) {
string large_body = "hello world!!!!!!";

EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
headers_string_.c_str(), headers_string_.size()));
// The headers should parse successfully.
EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
EXPECT_EQ(200u, stream_->headers().parsed_response_code());
EXPECT_EQ(200, stream_->headers()->response_code());

EXPECT_CALL(*connection_,
SendRstStream(stream_->id(), QUIC_BAD_APPLICATION_PAYLOAD, 0));
Expand All @@ -98,7 +101,7 @@ TEST_P(QuicSpdyClientStreamTest, DISABLED_TestFramingExtraData) {
EXPECT_NE(QUIC_STREAM_NO_ERROR, stream_->stream_error());
}

TEST_P(QuicSpdyClientStreamTest, TestNoBidirectionalStreaming) {
TEST_P(QuicSimpleClientStreamTest, TestNoBidirectionalStreaming) {
QuicStreamFrame frame(3, false, 3, MakeIOVector("asd"));

EXPECT_FALSE(stream_->write_side_closed());
Expand Down
57 changes: 5 additions & 52 deletions net/tools/quic/quic_simple_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <dirent.h>
#include <stdio.h>
#include "net/tools/quic/quic_simple_client.h"

#include "base/basictypes.h"
#include "base/strings/string_util.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/tools/epoll_server/epoll_server.h"
#include "net/tools/quic/quic_client.h"
#include "testing/gtest/include/gtest/gtest.h"

using net::EpollServer;

namespace net {
namespace tools {
namespace test {
namespace {

int NumOpenFDs() {
int number_of_open_fds = 0;
char buf[256];
struct dirent* dp;

base::snprintf(buf, arraysize(buf), "/proc/%i/fd/", getpid());
DIR* dir = opendir(buf);
while ((dp = readdir(dir)) != NULL)
number_of_open_fds++;
closedir(dir);

return number_of_open_fds;
}

// Creates a new QuicClient and Initializes it. Caller is responsible for
// deletion.
QuicClient* CreateAndInitializeQuicClient(EpollServer* eps, uint16 port) {
IPEndPoint server_address(IPEndPoint(net::test::Loopback4(), port));
TEST(QuicSimpleClientTest, Initialize) {
IPEndPoint server_address(IPEndPoint(net::test::Loopback4(), 80));
QuicServerId server_id("hostname", server_address.port(), false,
PRIVACY_MODE_DISABLED);
QuicVersionVector versions = QuicSupportedVersions();
QuicClient* client = new QuicClient(server_address, server_id, versions, eps);
EXPECT_TRUE(client->Initialize());
return client;
}

TEST(QuicClientTest, DoNotLeakFDs) {
// Make sure that the QuicClient doesn't leak FDs. Doing so could cause port
// exhaustion in long running processes which repeatedly create clients.

// Record initial number of FDs, after creation of EpollServer.
EpollServer eps;
int number_of_open_fds = NumOpenFDs();

// Create a number of clients, initialize them, and verify this has resulted
// in additional FDs being opened.
const int kNumClients = 5;
for (int i = 0; i < kNumClients; ++i) {
std::unique_ptr<QuicClient> client(
CreateAndInitializeQuicClient(&eps, net::test::kTestPort + i));

// Initializing the client will create a new FD.
EXPECT_LT(number_of_open_fds, NumOpenFDs());
}

// The FDs created by the QuicClients should now be closed.
EXPECT_EQ(number_of_open_fds, NumOpenFDs());
QuicSimpleClient client(server_address, server_id, versions);
EXPECT_TRUE(client.Initialize());
}

} // namespace
} // namespace test
} // namespace tools
} // namespace net

0 comments on commit a6d42cd

Please sign in to comment.