blob: 197ba7dfa9c7e586fe390011bdb88bd3f6e39df5 [file] [log] [blame]
// Copyright (c) 2012 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/http/http_network_transaction.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_vector.h"
#include "net/base/auth.h"
#include "net/base/net_log_unittest.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/base/upload_file_element_reader.h"
#include "net/http/http_network_session_peer.h"
#include "net/http/http_transaction_unittest.h"
#include "net/socket/client_socket_pool_base.h"
#include "net/spdy/buffered_spdy_framer.h"
#include "net/spdy/spdy_http_stream.h"
#include "net/spdy/spdy_http_utils.h"
#include "net/spdy/spdy_session.h"
#include "net/spdy/spdy_session_pool.h"
#include "net/spdy/spdy_test_util_spdy3.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/platform_test.h"
using namespace net::test_spdy3;
//-----------------------------------------------------------------------------
namespace net {
enum SpdyNetworkTransactionSpdy3TestTypes {
SPDYNPN,
SPDYNOSSL,
SPDYSSL,
};
class SpdyNetworkTransactionSpdy3Test
: public ::testing::TestWithParam<SpdyNetworkTransactionSpdy3TestTypes> {
protected:
virtual void SetUp() {
google_get_request_initialized_ = false;
google_post_request_initialized_ = false;
google_chunked_post_request_initialized_ = false;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
}
virtual void TearDown() {
UploadDataStream::ResetMergeChunks();
// Empty the current queue.
MessageLoop::current()->RunUntilIdle();
}
void set_merge_chunks(bool merge) {
UploadDataStream::set_merge_chunks(merge);
}
struct TransactionHelperResult {
int rv;
std::string status_line;
std::string response_data;
HttpResponseInfo response_info;
};
// A helper class that handles all the initial npn/ssl setup.
class NormalSpdyTransactionHelper {
public:
NormalSpdyTransactionHelper(const HttpRequestInfo& request,
const BoundNetLog& log,
SpdyNetworkTransactionSpdy3TestTypes test_type,
SpdySessionDependencies* session_deps)
: request_(request),
session_deps_(session_deps == NULL ?
new SpdySessionDependencies() : session_deps),
session_(SpdySessionDependencies::SpdyCreateSession(
session_deps_.get())),
log_(log),
test_type_(test_type),
deterministic_(false),
spdy_enabled_(true) {
switch (test_type_) {
case SPDYNOSSL:
case SPDYSSL:
port_ = 80;
break;
case SPDYNPN:
port_ = 443;
break;
default:
NOTREACHED();
}
}
~NormalSpdyTransactionHelper() {
// Any test which doesn't close the socket by sending it an EOF will
// have a valid session left open, which leaks the entire session pool.
// This is just fine - in fact, some of our tests intentionally do this
// so that we can check consistency of the SpdySessionPool as the test
// finishes. If we had put an EOF on the socket, the SpdySession would
// have closed and we wouldn't be able to check the consistency.
// Forcefully close existing sessions here.
session()->spdy_session_pool()->CloseAllSessions();
}
void SetDeterministic() {
session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic(
session_deps_.get());
deterministic_ = true;
}
void SetSpdyDisabled() {
spdy_enabled_ = false;
port_ = 80;
}
void RunPreTestSetup() {
if (!session_deps_.get())
session_deps_.reset(new SpdySessionDependencies());
if (!session_.get())
session_ = SpdySessionDependencies::SpdyCreateSession(
session_deps_.get());
HttpStreamFactory::set_use_alternate_protocols(false);
HttpStreamFactory::set_force_spdy_over_ssl(false);
HttpStreamFactory::set_force_spdy_always(false);
std::vector<std::string> next_protos;
next_protos.push_back("http/1.1");
next_protos.push_back("spdy/2");
next_protos.push_back("spdy/3");
switch (test_type_) {
case SPDYNPN:
session_->http_server_properties()->SetAlternateProtocol(
HostPortPair("www.google.com", 80), 443,
NPN_SPDY_3);
HttpStreamFactory::set_use_alternate_protocols(true);
HttpStreamFactory::SetNextProtos(next_protos);
break;
case SPDYNOSSL:
HttpStreamFactory::set_force_spdy_over_ssl(false);
HttpStreamFactory::set_force_spdy_always(true);
break;
case SPDYSSL:
HttpStreamFactory::set_force_spdy_over_ssl(true);
HttpStreamFactory::set_force_spdy_always(true);
break;
default:
NOTREACHED();
}
// We're now ready to use SSL-npn SPDY.
trans_.reset(new HttpNetworkTransaction(session_));
}
// Start the transaction, read some data, finish.
void RunDefaultTest() {
output_.rv = trans_->Start(&request_, callback.callback(), log_);
// We expect an IO Pending or some sort of error.
EXPECT_LT(output_.rv, 0);
if (output_.rv != ERR_IO_PENDING)
return;
output_.rv = callback.WaitForResult();
if (output_.rv != OK) {
session_->spdy_session_pool()->CloseCurrentSessions(net::ERR_ABORTED);
return;
}
// Verify responses.
const HttpResponseInfo* response = trans_->GetResponseInfo();
ASSERT_TRUE(response != NULL);
ASSERT_TRUE(response->headers != NULL);
EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
EXPECT_EQ(spdy_enabled_, response->was_fetched_via_spdy);
if (test_type_ == SPDYNPN && spdy_enabled_) {
EXPECT_TRUE(response->was_npn_negotiated);
} else {
EXPECT_TRUE(!response->was_npn_negotiated);
}
// If SPDY is not enabled, a HTTP request should not be diverted
// over a SSL session.
if (!spdy_enabled_) {
EXPECT_EQ(request_.url.SchemeIs("https"),
response->was_npn_negotiated);
}
EXPECT_EQ("127.0.0.1", response->socket_address.host());
EXPECT_EQ(port_, response->socket_address.port());
output_.status_line = response->headers->GetStatusLine();
output_.response_info = *response; // Make a copy so we can verify.
output_.rv = ReadTransaction(trans_.get(), &output_.response_data);
}
// Most tests will want to call this function. In particular, the MockReads
// should end with an empty read, and that read needs to be processed to
// ensure proper deletion of the spdy_session_pool.
void VerifyDataConsumed() {
for (DataVector::iterator it = data_vector_.begin();
it != data_vector_.end(); ++it) {
EXPECT_TRUE((*it)->at_read_eof()) << "Read count: "
<< (*it)->read_count()
<< " Read index: "
<< (*it)->read_index();
EXPECT_TRUE((*it)->at_write_eof()) << "Write count: "
<< (*it)->write_count()
<< " Write index: "
<< (*it)->write_index();
}
}
// Occasionally a test will expect to error out before certain reads are
// processed. In that case we want to explicitly ensure that the reads were
// not processed.
void VerifyDataNotConsumed() {
for (DataVector::iterator it = data_vector_.begin();
it != data_vector_.end(); ++it) {
EXPECT_TRUE(!(*it)->at_read_eof()) << "Read count: "
<< (*it)->read_count()
<< " Read index: "
<< (*it)->read_index();
EXPECT_TRUE(!(*it)->at_write_eof()) << "Write count: "
<< (*it)->write_count()
<< " Write index: "
<< (*it)->write_index();
}
}
void RunToCompletion(StaticSocketDataProvider* data) {
RunPreTestSetup();
AddData(data);
RunDefaultTest();
VerifyDataConsumed();
}
void AddData(StaticSocketDataProvider* data) {
DCHECK(!deterministic_);
data_vector_.push_back(data);
SSLSocketDataProvider* ssl_provider =
new SSLSocketDataProvider(ASYNC, OK);
if (test_type_ == SPDYNPN)
ssl_provider->SetNextProto(kProtoSPDY3);
ssl_vector_.push_back(ssl_provider);
if (test_type_ == SPDYNPN || test_type_ == SPDYSSL)
session_deps_->socket_factory->AddSSLSocketDataProvider(ssl_provider);
session_deps_->socket_factory->AddSocketDataProvider(data);
if (test_type_ == SPDYNPN) {
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
StaticSocketDataProvider* hanging_non_alternate_protocol_socket =
new StaticSocketDataProvider(NULL, 0, NULL, 0);
hanging_non_alternate_protocol_socket->set_connect_data(
never_finishing_connect);
session_deps_->socket_factory->AddSocketDataProvider(
hanging_non_alternate_protocol_socket);
alternate_vector_.push_back(hanging_non_alternate_protocol_socket);
}
}
void AddDeterministicData(DeterministicSocketData* data) {
DCHECK(deterministic_);
data_vector_.push_back(data);
SSLSocketDataProvider* ssl_provider =
new SSLSocketDataProvider(ASYNC, OK);
if (test_type_ == SPDYNPN)
ssl_provider->SetNextProto(kProtoSPDY3);
ssl_vector_.push_back(ssl_provider);
if (test_type_ == SPDYNPN || test_type_ == SPDYSSL) {
session_deps_->deterministic_socket_factory->
AddSSLSocketDataProvider(ssl_provider);
}
session_deps_->deterministic_socket_factory->AddSocketDataProvider(data);
if (test_type_ == SPDYNPN) {
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
DeterministicSocketData* hanging_non_alternate_protocol_socket =
new DeterministicSocketData(NULL, 0, NULL, 0);
hanging_non_alternate_protocol_socket->set_connect_data(
never_finishing_connect);
session_deps_->deterministic_socket_factory->AddSocketDataProvider(
hanging_non_alternate_protocol_socket);
alternate_deterministic_vector_.push_back(
hanging_non_alternate_protocol_socket);
}
}
void SetSession(const scoped_refptr<HttpNetworkSession>& session) {
session_ = session;
}
HttpNetworkTransaction* trans() { return trans_.get(); }
void ResetTrans() { trans_.reset(); }
TransactionHelperResult& output() { return output_; }
const HttpRequestInfo& request() const { return request_; }
const scoped_refptr<HttpNetworkSession>& session() const {
return session_;
}
scoped_ptr<SpdySessionDependencies>& session_deps() {
return session_deps_;
}
int port() const { return port_; }
SpdyNetworkTransactionSpdy3TestTypes test_type() const {
return test_type_;
}
private:
typedef std::vector<StaticSocketDataProvider*> DataVector;
typedef ScopedVector<SSLSocketDataProvider> SSLVector;
typedef ScopedVector<StaticSocketDataProvider> AlternateVector;
typedef ScopedVector<DeterministicSocketData> AlternateDeterministicVector;
HttpRequestInfo request_;
scoped_ptr<SpdySessionDependencies> session_deps_;
scoped_refptr<HttpNetworkSession> session_;
TransactionHelperResult output_;
scoped_ptr<StaticSocketDataProvider> first_transaction_;
SSLVector ssl_vector_;
TestCompletionCallback callback;
scoped_ptr<HttpNetworkTransaction> trans_;
scoped_ptr<HttpNetworkTransaction> trans_http_;
DataVector data_vector_;
AlternateVector alternate_vector_;
AlternateDeterministicVector alternate_deterministic_vector_;
const BoundNetLog& log_;
SpdyNetworkTransactionSpdy3TestTypes test_type_;
int port_;
bool deterministic_;
bool spdy_enabled_;
};
void ConnectStatusHelperWithExpectedStatus(const MockRead& status,
int expected_status);
void ConnectStatusHelper(const MockRead& status);
const HttpRequestInfo& CreateGetPushRequest() {
google_get_push_request_.method = "GET";
google_get_push_request_.url = GURL("http://www.google.com/foo.dat");
google_get_push_request_.load_flags = 0;
return google_get_push_request_;
}
const HttpRequestInfo& CreateGetRequest() {
if (!google_get_request_initialized_) {
google_get_request_.method = "GET";
google_get_request_.url = GURL(kDefaultURL);
google_get_request_.load_flags = 0;
google_get_request_initialized_ = true;
}
return google_get_request_;
}
const HttpRequestInfo& CreateGetRequestWithUserAgent() {
if (!google_get_request_initialized_) {
google_get_request_.method = "GET";
google_get_request_.url = GURL(kDefaultURL);
google_get_request_.load_flags = 0;
google_get_request_.extra_headers.SetHeader("User-Agent", "Chrome");
google_get_request_initialized_ = true;
}
return google_get_request_;
}
const HttpRequestInfo& CreatePostRequest() {
if (!google_post_request_initialized_) {
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadBytesElementReader(kUploadData, kUploadDataSize));
upload_data_stream_.reset(new UploadDataStream(&element_readers, 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
}
return google_post_request_;
}
const HttpRequestInfo& CreateFilePostRequest() {
if (!google_post_request_initialized_) {
FilePath file_path;
CHECK(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
file_util::WriteFile(file_path, kUploadData, kUploadDataSize));
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(new UploadFileElementReader(
file_path, 0, kUploadDataSize, base::Time()));
upload_data_stream_.reset(new UploadDataStream(&element_readers, 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
}
return google_post_request_;
}
const HttpRequestInfo& CreateComplexPostRequest() {
if (!google_post_request_initialized_) {
const int kFileRangeOffset = 1;
const int kFileRangeLength = 3;
CHECK_LT(kFileRangeOffset + kFileRangeLength, kUploadDataSize);
FilePath file_path;
CHECK(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &file_path));
CHECK_EQ(static_cast<int>(kUploadDataSize),
file_util::WriteFile(file_path, kUploadData, kUploadDataSize));
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadBytesElementReader(kUploadData, kFileRangeOffset));
element_readers.push_back(new UploadFileElementReader(
file_path, kFileRangeOffset, kFileRangeLength, base::Time()));
element_readers.push_back(new UploadBytesElementReader(
kUploadData + kFileRangeOffset + kFileRangeLength,
kUploadDataSize - (kFileRangeOffset + kFileRangeLength)));
upload_data_stream_.reset(new UploadDataStream(&element_readers, 0));
google_post_request_.method = "POST";
google_post_request_.url = GURL(kDefaultURL);
google_post_request_.upload_data_stream = upload_data_stream_.get();
google_post_request_initialized_ = true;
}
return google_post_request_;
}
const HttpRequestInfo& CreateChunkedPostRequest() {
if (!google_chunked_post_request_initialized_) {
upload_data_stream_.reset(
new UploadDataStream(UploadDataStream::CHUNKED, 0));
upload_data_stream_->AppendChunk(kUploadData, kUploadDataSize, false);
upload_data_stream_->AppendChunk(kUploadData, kUploadDataSize, true);
google_chunked_post_request_.method = "POST";
google_chunked_post_request_.url = GURL(kDefaultURL);
google_chunked_post_request_.upload_data_stream =
upload_data_stream_.get();
google_chunked_post_request_initialized_ = true;
}
return google_chunked_post_request_;
}
// Read the result of a particular transaction, knowing that we've got
// multiple transactions in the read pipeline; so as we read, we may have
// to skip over data destined for other transactions while we consume
// the data for |trans|.
int ReadResult(HttpNetworkTransaction* trans,
StaticSocketDataProvider* data,
std::string* result) {
const int kSize = 3000;
int bytes_read = 0;
scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(kSize));
TestCompletionCallback callback;
while (true) {
int rv = trans->Read(buf, kSize, callback.callback());
if (rv == ERR_IO_PENDING) {
// Multiple transactions may be in the data set. Keep pulling off
// reads until we complete our callback.
while (!callback.have_result()) {
data->CompleteRead();
MessageLoop::current()->RunUntilIdle();
}
rv = callback.WaitForResult();
} else if (rv <= 0) {
break;
}
result->append(buf->data(), rv);
bytes_read += rv;
}
return bytes_read;
}
void VerifyStreamsClosed(const NormalSpdyTransactionHelper& helper) {
// This lengthy block is reaching into the pool to dig out the active
// session. Once we have the session, we verify that the streams are
// all closed and not leaked at this point.
const GURL& url = helper.request().url;
int port = helper.test_type() == SPDYNPN ? 443 : 80;
HostPortPair host_port_pair(url.host(), port);
HostPortProxyPair pair(host_port_pair, ProxyServer::Direct());
BoundNetLog log;
const scoped_refptr<HttpNetworkSession>& session = helper.session();
SpdySessionPool* pool(session->spdy_session_pool());
EXPECT_TRUE(pool->HasSession(pair));
scoped_refptr<SpdySession> spdy_session(pool->Get(pair, log));
ASSERT_TRUE(spdy_session.get() != NULL);
EXPECT_EQ(0u, spdy_session->num_active_streams());
EXPECT_EQ(0u, spdy_session->num_unclaimed_pushed_streams());
}
void RunServerPushTest(OrderedSocketData* data,
HttpResponseInfo* response,
HttpResponseInfo* push_response,
std::string& expected) {
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(data);
HttpNetworkTransaction* trans = helper.trans();
// Start the transaction with basic parameters.
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
// Request the pushed path.
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
rv = trans2->Start(
&CreateGetPushRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
MessageLoop::current()->RunUntilIdle();
// The data for the pushed path may be coming in more than 1 packet. Compile
// the results into a single string.
// Read the server push body.
std::string result2;
ReadResult(trans2.get(), data, &result2);
// Read the response body.
std::string result;
ReadResult(trans, data, &result);
// Verify that we consumed all test data.
EXPECT_TRUE(data->at_read_eof());
EXPECT_TRUE(data->at_write_eof());
// Verify that the received push data is same as the expected push data.
EXPECT_EQ(result2.compare(expected), 0) << "Received data: "
<< result2
<< "||||| Expected data: "
<< expected;
// Verify the SYN_REPLY.
// Copy the response info, because trans goes away.
*response = *trans->GetResponseInfo();
*push_response = *trans2->GetResponseInfo();
VerifyStreamsClosed(helper);
}
static void DeleteSessionCallback(NormalSpdyTransactionHelper* helper,
int result) {
helper->ResetTrans();
}
static void StartTransactionCallback(
const scoped_refptr<HttpNetworkSession>& session,
int result) {
scoped_ptr<HttpNetworkTransaction> trans(
new HttpNetworkTransaction(session));
TestCompletionCallback callback;
HttpRequestInfo request;
request.method = "GET";
request.url = GURL("http://www.google.com/");
request.load_flags = 0;
int rv = trans->Start(&request, callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
callback.WaitForResult();
}
private:
scoped_ptr<UploadDataStream> upload_data_stream_;
bool google_get_request_initialized_;
bool google_post_request_initialized_;
bool google_chunked_post_request_initialized_;
HttpRequestInfo google_get_request_;
HttpRequestInfo google_post_request_;
HttpRequestInfo google_chunked_post_request_;
HttpRequestInfo google_get_push_request_;
base::ScopedTempDir temp_dir_;
};
//-----------------------------------------------------------------------------
// All tests are run with three different connection types: SPDY after NPN
// negotiation, SPDY without SSL, and SPDY with SSL.
INSTANTIATE_TEST_CASE_P(Spdy,
SpdyNetworkTransactionSpdy3Test,
::testing::Values(SPDYNOSSL, SPDYSSL, SPDYNPN));
// Verify HttpNetworkTransaction constructor.
TEST_P(SpdyNetworkTransactionSpdy3Test, Constructor) {
SpdySessionDependencies session_deps;
scoped_refptr<HttpNetworkSession> session(
SpdySessionDependencies::SpdyCreateSession(&session_deps));
scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session));
}
TEST_P(SpdyNetworkTransactionSpdy3Test, Get) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
MockWrite writes[] = { CreateMockWrite(*req) };
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
TEST_P(SpdyNetworkTransactionSpdy3Test, GetAtEachPriority) {
for (RequestPriority p = MINIMUM_PRIORITY; p < NUM_PRIORITIES;
p = RequestPriority(p + 1)) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, p));
MockWrite writes[] = { CreateMockWrite(*req) };
const int spdy_prio = reinterpret_cast<SpdySynStreamControlFrame*>(
req.get())->priority();
// this repeats the RequestPriority-->SpdyPriority mapping from
// SpdyFramer::ConvertRequestPriorityToSpdyPriority to make
// sure it's being done right.
switch(p) {
case HIGHEST:
EXPECT_EQ(0, spdy_prio);
break;
case MEDIUM:
EXPECT_EQ(1, spdy_prio);
break;
case LOW:
EXPECT_EQ(2, spdy_prio);
break;
case LOWEST:
EXPECT_EQ(3, spdy_prio);
break;
case IDLE:
EXPECT_EQ(4, spdy_prio);
break;
default:
FAIL();
}
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
HttpRequestInfo http_req = CreateGetRequest();
http_req.priority = p;
NormalSpdyTransactionHelper helper(http_req, BoundNetLog(),
GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
}
// Start three gets simultaniously; making sure that multiplexed
// streams work properly.
// This can't use the TransactionHelper method, since it only
// handles a single transaction, and finishes them as soon
// as it launches them.
// TODO(gavinp): create a working generalized TransactionHelper that
// can allow multiple streams in flight.
TEST_P(SpdyNetworkTransactionSpdy3Test, ThreeGets) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(ConstructSpdyBodyFrame(3, true));
scoped_ptr<SpdyFrame> req3(ConstructSpdyGet(NULL, 0, false, 5, LOWEST));
scoped_ptr<SpdyFrame> resp3(ConstructSpdyGetSynReply(NULL, 0, 5));
scoped_ptr<SpdyFrame> body3(ConstructSpdyBodyFrame(5, false));
scoped_ptr<SpdyFrame> fbody3(ConstructSpdyBodyFrame(5, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
CreateMockWrite(*req3),
};
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body),
CreateMockRead(*resp2, 4),
CreateMockRead(*body2),
CreateMockRead(*resp3, 7),
CreateMockRead(*body3),
CreateMockRead(*fbody),
CreateMockRead(*fbody2),
CreateMockRead(*fbody3),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = callback3.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
trans2->GetResponseInfo();
out.rv = ReadTransaction(trans1.get(), &out.response_data);
helper.VerifyDataConsumed();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
}
TEST_P(SpdyNetworkTransactionSpdy3Test, TwoGetsLateBinding) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(ConstructSpdyBodyFrame(3, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body),
CreateMockRead(*resp2, 4),
CreateMockRead(*body2),
CreateMockRead(*fbody),
CreateMockRead(*fbody2),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING);
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
data_placeholder.set_connect_data(never_finishing_connect);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because two get requests are sent out, so
// there needs to be two sets of SSL connection data.
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
EXPECT_TRUE(response2->headers != NULL);
EXPECT_TRUE(response2->was_fetched_via_spdy);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionSpdy3Test, TwoGetsLateBindingFromPreconnect) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(ConstructSpdyBodyFrame(3, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*resp, 1),
CreateMockRead(*body),
CreateMockRead(*resp2, 4),
CreateMockRead(*body2),
CreateMockRead(*fbody),
CreateMockRead(*fbody2),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData preconnect_data(reads, arraysize(reads),
writes, arraysize(writes));
MockConnect never_finishing_connect(ASYNC, ERR_IO_PENDING);
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
data_placeholder.set_connect_data(never_finishing_connect);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&preconnect_data);
// We require placeholder data because 3 connections are attempted (first is
// the preconnect, 2nd and 3rd are the never finished connections.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
HttpRequestInfo httpreq = CreateGetRequest();
// Preconnect the first.
SSLConfig preconnect_ssl_config;
helper.session()->ssl_config_service()->GetSSLConfig(&preconnect_ssl_config);
HttpStreamFactory* http_stream_factory =
helper.session()->http_stream_factory();
if (http_stream_factory->has_next_protos()) {
preconnect_ssl_config.next_protos = http_stream_factory->next_protos();
}
http_stream_factory->PreconnectStreams(
1, httpreq, preconnect_ssl_config, preconnect_ssl_config);
out.rv = trans1->Start(&httpreq, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans2->Start(&httpreq, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
EXPECT_TRUE(response2->headers != NULL);
EXPECT_TRUE(response2->was_fetched_via_spdy);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
// Similar to ThreeGets above, however this test adds a SETTINGS
// frame. The SETTINGS frame is read during the IO loop waiting on
// the first transaction completion, and sets a maximum concurrent
// stream limit of 1. This means that our IO loop exists after the
// second transaction completes, so we can assert on read_index().
TEST_P(SpdyNetworkTransactionSpdy3Test, ThreeGetsWithMaxConcurrent) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(ConstructSpdyBodyFrame(3, true));
scoped_ptr<SpdyFrame> req3(ConstructSpdyGet(NULL, 0, false, 5, LOWEST));
scoped_ptr<SpdyFrame> resp3(ConstructSpdyGetSynReply(NULL, 0, 5));
scoped_ptr<SpdyFrame> body3(ConstructSpdyBodyFrame(5, false));
scoped_ptr<SpdyFrame> fbody3(ConstructSpdyBodyFrame(5, true));
SettingsMap settings;
const size_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(ConstructSpdySettings(settings));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*req2),
CreateMockWrite(*req3),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fbody),
CreateMockRead(*resp2, 7),
CreateMockRead(*body2),
CreateMockRead(*fbody2),
CreateMockRead(*resp3, 12),
CreateMockRead(*body3),
CreateMockRead(*fbody3),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
{
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
// run transaction 1 through quickly to force a read of our SETTINGS
// frame
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
EXPECT_EQ(7U, data.read_index()); // i.e. the third trans was queued
out.rv = callback3.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
ASSERT_TRUE(response1 != NULL);
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response3 = trans3->GetResponseInfo();
out.status_line = response3->headers->GetStatusLine();
out.response_info = *response3;
out.rv = ReadTransaction(trans3.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
}
EXPECT_EQ(OK, out.rv);
}
// Similar to ThreeGetsWithMaxConcurrent above, however this test adds
// a fourth transaction. The third and fourth transactions have
// different data ("hello!" vs "hello!hello!") and because of the
// user specified priority, we expect to see them inverted in
// the response from the server.
TEST_P(SpdyNetworkTransactionSpdy3Test, FourGetsWithMaxConcurrentPriority) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(ConstructSpdyBodyFrame(3, true));
scoped_ptr<SpdyFrame> req4(
ConstructSpdyGet(NULL, 0, false, 5, HIGHEST));
scoped_ptr<SpdyFrame> resp4(ConstructSpdyGetSynReply(NULL, 0, 5));
scoped_ptr<SpdyFrame> fbody4(ConstructSpdyBodyFrame(5, true));
scoped_ptr<SpdyFrame> req3(ConstructSpdyGet(NULL, 0, false, 7, LOWEST));
scoped_ptr<SpdyFrame> resp3(ConstructSpdyGetSynReply(NULL, 0, 7));
scoped_ptr<SpdyFrame> body3(ConstructSpdyBodyFrame(7, false));
scoped_ptr<SpdyFrame> fbody3(ConstructSpdyBodyFrame(7, true));
SettingsMap settings;
const size_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(ConstructSpdySettings(settings));
MockWrite writes[] = { CreateMockWrite(*req),
CreateMockWrite(*req2),
CreateMockWrite(*req4),
CreateMockWrite(*req3),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fbody),
CreateMockRead(*resp2, 7),
CreateMockRead(*body2),
CreateMockRead(*fbody2),
CreateMockRead(*resp4, 13),
CreateMockRead(*fbody4),
CreateMockRead(*resp3, 16),
CreateMockRead(*body3),
CreateMockRead(*fbody3),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because four get requests are sent out, so
// there needs to be four sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans4(
new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
TestCompletionCallback callback4;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
HttpRequestInfo httpreq4 = CreateGetRequest();
httpreq4.priority = HIGHEST;
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = trans4->Start(&httpreq4, callback4.callback(), log);
ASSERT_EQ(ERR_IO_PENDING, out.rv);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
EXPECT_EQ(data.read_index(), 7U); // i.e. the third & fourth trans queued
out.rv = callback3.WaitForResult();
ASSERT_EQ(OK, out.rv);
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
// notice: response3 gets two hellos, response4 gets one
// hello, so we know dequeuing priority was respected.
const HttpResponseInfo* response3 = trans3->GetResponseInfo();
out.status_line = response3->headers->GetStatusLine();
out.response_info = *response3;
out.rv = ReadTransaction(trans3.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
out.rv = callback4.WaitForResult();
EXPECT_EQ(OK, out.rv);
const HttpResponseInfo* response4 = trans4->GetResponseInfo();
out.status_line = response4->headers->GetStatusLine();
out.response_info = *response4;
out.rv = ReadTransaction(trans4.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
helper.VerifyDataConsumed();
EXPECT_EQ(OK, out.rv);
}
// Similar to ThreeGetsMaxConcurrrent above, however, this test
// deletes a session in the middle of the transaction to insure
// that we properly remove pendingcreatestream objects from
// the spdy_session
TEST_P(SpdyNetworkTransactionSpdy3Test, ThreeGetsWithMaxConcurrentDelete) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fbody(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, false));
scoped_ptr<SpdyFrame> fbody2(ConstructSpdyBodyFrame(3, true));
SettingsMap settings;
const size_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(ConstructSpdySettings(settings));
MockWrite writes[] = { CreateMockWrite(*req),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fbody),
CreateMockRead(*resp2, 7),
CreateMockRead(*body2),
CreateMockRead(*fbody2),
MockRead(ASYNC, 0, 0), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
scoped_ptr<HttpNetworkTransaction> trans1(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans2(
new HttpNetworkTransaction(helper.session()));
scoped_ptr<HttpNetworkTransaction> trans3(
new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
TestCompletionCallback callback3;
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1->Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2->Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
delete trans3.release();
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback2.WaitForResult();
ASSERT_EQ(OK, out.rv);
EXPECT_EQ(8U, data.read_index());
const HttpResponseInfo* response1 = trans1->GetResponseInfo();
ASSERT_TRUE(response1 != NULL);
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(trans1.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
const HttpResponseInfo* response2 = trans2->GetResponseInfo();
ASSERT_TRUE(response2 != NULL);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(trans2.get(), &out.response_data);
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
helper.VerifyDataConsumed();
EXPECT_EQ(OK, out.rv);
}
namespace {
// The KillerCallback will delete the transaction on error as part of the
// callback.
class KillerCallback : public TestCompletionCallbackBase {
public:
explicit KillerCallback(HttpNetworkTransaction* transaction)
: transaction_(transaction),
ALLOW_THIS_IN_INITIALIZER_LIST(callback_(
base::Bind(&KillerCallback::OnComplete, base::Unretained(this)))) {
}
virtual ~KillerCallback() {}
const CompletionCallback& callback() const { return callback_; }
private:
void OnComplete(int result) {
if (result < 0)
delete transaction_;
SetResult(result);
}
HttpNetworkTransaction* transaction_;
CompletionCallback callback_;
};
} // namespace
// Similar to ThreeGetsMaxConcurrrentDelete above, however, this test
// closes the socket while we have a pending transaction waiting for
// a pending stream creation. http://crbug.com/52901
TEST_P(SpdyNetworkTransactionSpdy3Test, ThreeGetsWithMaxConcurrentSocketClose) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> fin_body(ConstructSpdyBodyFrame(1, true));
scoped_ptr<SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST));
scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3));
SettingsMap settings;
const size_t max_concurrent_streams = 1;
settings[SETTINGS_MAX_CONCURRENT_STREAMS] =
SettingsFlagsAndValue(SETTINGS_FLAG_NONE, max_concurrent_streams);
scoped_ptr<SpdyFrame> settings_frame(ConstructSpdySettings(settings));
MockWrite writes[] = { CreateMockWrite(*req),
CreateMockWrite(*req2),
};
MockRead reads[] = {
CreateMockRead(*settings_frame, 1),
CreateMockRead(*resp),
CreateMockRead(*body),
CreateMockRead(*fin_body),
CreateMockRead(*resp2, 7),
MockRead(ASYNC, ERR_CONNECTION_RESET, 0), // Abort!
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
OrderedSocketData data_placeholder(NULL, 0, NULL, 0);
BoundNetLog log;
TransactionHelperResult out;
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
// We require placeholder data because three get requests are sent out, so
// there needs to be three sets of SSL connection data.
helper.AddData(&data_placeholder);
helper.AddData(&data_placeholder);
HttpNetworkTransaction trans1(helper.session());
HttpNetworkTransaction trans2(helper.session());
HttpNetworkTransaction* trans3(new HttpNetworkTransaction(helper.session()));
TestCompletionCallback callback1;
TestCompletionCallback callback2;
KillerCallback callback3(trans3);
HttpRequestInfo httpreq1 = CreateGetRequest();
HttpRequestInfo httpreq2 = CreateGetRequest();
HttpRequestInfo httpreq3 = CreateGetRequest();
out.rv = trans1.Start(&httpreq1, callback1.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
// Run transaction 1 through quickly to force a read of our SETTINGS frame.
out.rv = callback1.WaitForResult();
ASSERT_EQ(OK, out.rv);
out.rv = trans2.Start(&httpreq2, callback2.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = trans3->Start(&httpreq3, callback3.callback(), log);
ASSERT_EQ(out.rv, ERR_IO_PENDING);
out.rv = callback3.WaitForResult();
ASSERT_EQ(ERR_ABORTED, out.rv);
EXPECT_EQ(6U, data.read_index());
const HttpResponseInfo* response1 = trans1.GetResponseInfo();
ASSERT_TRUE(response1 != NULL);
EXPECT_TRUE(response1->headers != NULL);
EXPECT_TRUE(response1->was_fetched_via_spdy);
out.status_line = response1->headers->GetStatusLine();
out.response_info = *response1;
out.rv = ReadTransaction(&trans1, &out.response_data);
EXPECT_EQ(OK, out.rv);
const HttpResponseInfo* response2 = trans2.GetResponseInfo();
ASSERT_TRUE(response2 != NULL);
out.status_line = response2->headers->GetStatusLine();
out.response_info = *response2;
out.rv = ReadTransaction(&trans2, &out.response_data);
EXPECT_EQ(ERR_CONNECTION_RESET, out.rv);
helper.VerifyDataConsumed();
}
// Test that a simple PUT request works.
TEST_P(SpdyNetworkTransactionSpdy3Test, Put) {
// Setup the request
HttpRequestInfo request;
request.method = "PUT";
request.url = GURL("http://www.google.com/");
const SpdyHeaderInfo kSynStartHeader = {
SYN_STREAM, // Kind = Syn
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(LOWEST, 3), // Priority
0, // Credential Slot
CONTROL_FLAG_FIN, // Control Flags
false, // Compressed
INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
const char* const kPutHeaders[] = {
":method", "PUT",
":path", "/",
":host", "www.google.com",
":scheme", "http",
":version", "HTTP/1.1",
"content-length", "0"
};
scoped_ptr<SpdyFrame> req(ConstructSpdyPacket(kSynStartHeader, NULL, 0,
kPutHeaders, arraysize(kPutHeaders) / 2));
MockWrite writes[] = {
CreateMockWrite(*req)
};
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
const SpdyHeaderInfo kSynReplyHeader = {
SYN_REPLY, // Kind = SynReply
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(LOWEST, 3), // Priority
0, // Credential Slot
CONTROL_FLAG_NONE, // Control Flags
false, // Compressed
INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
static const char* const kStandardRespHeaders[] = {
":status", "200",
":version", "HTTP/1.1"
"content-length", "1234"
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPacket(kSynReplyHeader,
NULL, 0, kStandardRespHeaders, arraysize(kStandardRespHeaders) / 2));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
}
// Test that a simple HEAD request works.
TEST_P(SpdyNetworkTransactionSpdy3Test, Head) {
// Setup the request
HttpRequestInfo request;
request.method = "HEAD";
request.url = GURL("http://www.google.com/");
const SpdyHeaderInfo kSynStartHeader = {
SYN_STREAM, // Kind = Syn
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(LOWEST, 3), // Priority
0, // Credential Slot
CONTROL_FLAG_FIN, // Control Flags
false, // Compressed
INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
const char* const kHeadHeaders[] = {
":method", "HEAD",
":path", "/",
":host", "www.google.com",
":scheme", "http",
":version", "HTTP/1.1",
"content-length", "0"
};
scoped_ptr<SpdyFrame> req(ConstructSpdyPacket(kSynStartHeader, NULL, 0,
kHeadHeaders, arraysize(kHeadHeaders) / 2));
MockWrite writes[] = {
CreateMockWrite(*req)
};
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
const SpdyHeaderInfo kSynReplyHeader = {
SYN_REPLY, // Kind = SynReply
1, // Stream ID
0, // Associated stream ID
ConvertRequestPriorityToSpdyPriority(LOWEST, 3), // Priority
0, // Credential Slot
CONTROL_FLAG_NONE, // Control Flags
false, // Compressed
INVALID, // Status
NULL, // Data
0, // Length
DATA_FLAG_NONE // Data Flags
};
static const char* const kStandardRespHeaders[] = {
":status", "200",
":version", "HTTP/1.1"
"content-length", "1234"
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPacket(kSynReplyHeader,
NULL, 0, kStandardRespHeaders, arraysize(kStandardRespHeaders) / 2));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
}
// Test that a simple POST works.
TEST_P(SpdyNetworkTransactionSpdy3Test, Post) {
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(kUploadDataSize, NULL, 0));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body), // POST upload frame
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreatePostRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a POST with a file works.
TEST_P(SpdyNetworkTransactionSpdy3Test, FilePost) {
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(kUploadDataSize, NULL, 0));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body), // POST upload frame
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateFilePostRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a complex POST works.
TEST_P(SpdyNetworkTransactionSpdy3Test, ComplexPost) {
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(kUploadDataSize, NULL, 0));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body), // POST upload frame
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateComplexPostRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a chunked POST works.
TEST_P(SpdyNetworkTransactionSpdy3Test, ChunkedPost) {
set_merge_chunks(false);
scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0));
scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*chunk1),
CreateMockWrite(*chunk2),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*chunk1),
CreateMockRead(*chunk2),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(2, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateChunkedPostRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!hello!", out.response_data);
}
// Test that a POST without any post data works.
TEST_P(SpdyNetworkTransactionSpdy3Test, NullPost) {
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
// Create an empty UploadData.
request.upload_data_stream = NULL;
// When request.upload_data_stream is NULL for post, content-length is
// expected to be 0.
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(0, NULL, 0));
// Set the FIN bit since there will be no body.
req->set_flags(CONTROL_FLAG_FIN);
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request,
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// Test that a simple POST works.
TEST_P(SpdyNetworkTransactionSpdy3Test, EmptyPost) {
// Create an empty UploadDataStream.
ScopedVector<UploadElementReader> element_readers;
UploadDataStream stream(&element_readers, 0);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &stream;
const uint64 kContentLength = 0;
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(kContentLength, NULL, 0));
// Set the FIN bit since there will be no body.
req->set_flags(CONTROL_FLAG_FIN);
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads), writes, arraysize(writes));
NormalSpdyTransactionHelper helper(request, BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
}
// While we're doing a post, the server sends back a SYN_REPLY.
TEST_P(SpdyNetworkTransactionSpdy3Test, PostWithEarlySynReply) {
static const char upload[] = { "hello!" };
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(
new UploadBytesElementReader(upload, sizeof(upload)));
UploadDataStream stream(&element_readers, 0);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &stream;
scoped_ptr<SpdyFrame> stream_reply(ConstructSpdyPostSynReply(NULL, 0));
scoped_ptr<SpdyFrame> stream_body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*stream_reply, 1),
MockRead(ASYNC, 0, 3) // EOF
};
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(kUploadDataSize, NULL, 0));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockWrite writes[] = {
CreateMockWrite(*req, 0),
CreateMockWrite(*body, 2),
};
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreatePostRequest(),
BoundNetLog(), GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreatePostRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
data.RunFor(2);
rv = callback.WaitForResult();
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, rv);
data.RunFor(1);
}
// The client upon cancellation tries to send a RST_STREAM frame. The mock
// socket causes the TCP write to return zero. This test checks that the client
// tries to queue up the RST_STREAM frame again.
TEST_P(SpdyNetworkTransactionSpdy3Test, SocketWriteReturnsZero) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> rst(
ConstructSpdyRstStream(1, CANCEL));
MockWrite writes[] = {
CreateMockWrite(*req.get(), 0, SYNCHRONOUS),
MockWrite(SYNCHRONOUS, 0, 0, 2),
CreateMockWrite(*rst.get(), 3, SYNCHRONOUS),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp.get(), 1, ASYNC),
MockRead(ASYNC, 0, 0, 4) // EOF
};
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
data.SetStop(2);
data.Run();
helper.ResetTrans();
data.SetStop(20);
data.Run();
helper.VerifyDataConsumed();
}
// Test that the transaction doesn't crash when we don't have a reply.
TEST_P(SpdyNetworkTransactionSpdy3Test, ResponseWithoutSynReply) {
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads), NULL, 0);
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(ERR_SYN_REPLY_NOT_RECEIVED, out.rv);
}
// Test that the transaction doesn't crash when we get two replies on the same
// stream ID. See http://crbug.com/45639.
TEST_P(SpdyNetworkTransactionSpdy3Test, ResponseWithTwoSynReplies) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
MockWrite writes[] = { CreateMockWrite(*req) };
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
const HttpResponseInfo* response = trans->GetResponseInfo();
ASSERT_TRUE(response != NULL);
EXPECT_TRUE(response->headers != NULL);
EXPECT_TRUE(response->was_fetched_via_spdy);
std::string response_data;
rv = ReadTransaction(trans, &response_data);
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, rv);
helper.VerifyDataConsumed();
}
// Test that sent data frames and received WINDOW_UPDATE frames change
// the send_window_size_ correctly.
// WINDOW_UPDATE is different than most other frames in that it can arrive
// while the client is still sending the request body. In order to enforce
// this scenario, we feed a couple of dummy frames and give a delay of 0 to
// socket data provider, so that initial read that is done as soon as the
// stream is created, succeeds and schedules another read. This way reads
// and writes are interleaved; after doing a full frame write, SpdyStream
// will break out of DoLoop and will read and process a WINDOW_UPDATE.
// Once our WINDOW_UPDATE is read, we cannot send SYN_REPLY right away
// since request has not been completely written, therefore we feed
// enough number of WINDOW_UPDATEs to finish the first read and cause a
// write, leading to a complete write of request body; after that we send
// a reply with a body, to cause a graceful shutdown.
// TODO(agayev): develop a socket data provider where both, reads and
// writes are ordered so that writing tests like these are easy and rewrite
// all these tests using it. Right now we are working around the
// limitations as described above and it's not deterministic, tests may
// fail under specific circumstances.
TEST_P(SpdyNetworkTransactionSpdy3Test, WindowUpdateReceived) {
static int kFrameCount = 2;
scoped_ptr<std::string> content(
new std::string(kMaxSpdyFrameChunkSize, 'a'));
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(
kMaxSpdyFrameChunkSize * kFrameCount, NULL, 0));
scoped_ptr<SpdyFrame> body(
ConstructSpdyBodyFrame(1, content->c_str(), content->size(), false));
scoped_ptr<SpdyFrame> body_end(
ConstructSpdyBodyFrame(1, content->c_str(), content->size(), true));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*body),
CreateMockWrite(*body_end),
};
static const int32 kDeltaWindowSize = 0xff;
static const int kDeltaCount = 4;
scoped_ptr<SpdyFrame> window_update(
ConstructSpdyWindowUpdate(1, kDeltaWindowSize));
scoped_ptr<SpdyFrame> window_update_dummy(
ConstructSpdyWindowUpdate(2, kDeltaWindowSize));
scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*window_update_dummy),
CreateMockRead(*window_update_dummy),
CreateMockRead(*window_update_dummy),
CreateMockRead(*window_update), // Four updates, therefore window
CreateMockRead(*window_update), // size should increase by
CreateMockRead(*window_update), // kDeltaWindowSize * 4
CreateMockRead(*window_update),
CreateMockRead(*resp),
CreateMockRead(*body_end),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(0, reads, arraysize(reads),
writes, arraysize(writes));
ScopedVector<UploadElementReader> element_readers;
for (int i = 0; i < kFrameCount; ++i) {
element_readers.push_back(
new UploadBytesElementReader(content->c_str(), content->size()));
}
UploadDataStream upload_data_stream(&element_readers, 0);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL(kDefaultURL);
request.upload_data_stream = &upload_data_stream;
NormalSpdyTransactionHelper helper(request, BoundNetLog(), GetParam(), NULL);
helper.AddData(&data);
helper.RunPreTestSetup();
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
SpdyHttpStream* stream = static_cast<SpdyHttpStream*>(trans->stream_.get());
ASSERT_TRUE(stream != NULL);
ASSERT_TRUE(stream->stream() != NULL);
EXPECT_EQ(static_cast<int>(kSpdyStreamInitialWindowSize) +
kDeltaWindowSize * kDeltaCount -
kMaxSpdyFrameChunkSize * kFrameCount,
stream->stream()->send_window_size());
helper.VerifyDataConsumed();
}
// Test that received data frames and sent WINDOW_UPDATE frames change
// the recv_window_size_ correctly.
TEST_P(SpdyNetworkTransactionSpdy3Test, WindowUpdateSent) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> window_update(
ConstructSpdyWindowUpdate(1, kUploadDataSize));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*window_update),
};
scoped_ptr<SpdyFrame> resp(
ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body_no_fin(
ConstructSpdyBodyFrame(1, false));
scoped_ptr<SpdyFrame> body_fin(
ConstructSpdyBodyFrame(1, NULL, 0, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body_no_fin),
MockRead(ASYNC, ERR_IO_PENDING, 0), // Force a pause
CreateMockRead(*body_fin),
MockRead(ASYNC, ERR_IO_PENDING, 0), // Force a pause
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.AddData(&data);
helper.RunPreTestSetup();
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
SpdyHttpStream* stream =
static_cast<SpdyHttpStream*>(trans->stream_.get());
ASSERT_TRUE(stream != NULL);
ASSERT_TRUE(stream->stream() != NULL);
EXPECT_EQ(
static_cast<int>(kSpdyStreamInitialWindowSize) - kUploadDataSize,
stream->stream()->recv_window_size());
const HttpResponseInfo* response = trans->GetResponseInfo();
ASSERT_TRUE(response != NULL);
ASSERT_TRUE(response->headers != NULL);
EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
EXPECT_TRUE(response->was_fetched_via_spdy);
// Force sending of WINDOW_UPDATE by setting initial_recv_window_size to a
// small value.
stream->stream()->set_initial_recv_window_size(kUploadDataSize / 2);
// Issue a read which will cause a WINDOW_UPDATE to be sent and window
// size increased to default.
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kUploadDataSize));
rv = trans->Read(buf, kUploadDataSize, CompletionCallback());
EXPECT_EQ(kUploadDataSize, rv);
std::string content(buf->data(), buf->data()+kUploadDataSize);
EXPECT_STREQ(kUploadData, content.c_str());
// Schedule the reading of empty data frame with FIN
data.CompleteRead();
// Force write of WINDOW_UPDATE which was scheduled during the above
// read.
MessageLoop::current()->RunUntilIdle();
// Read EOF.
data.CompleteRead();
helper.VerifyDataConsumed();
}
// Test that WINDOW_UPDATE frame causing overflow is handled correctly.
TEST_P(SpdyNetworkTransactionSpdy3Test, WindowUpdateOverflow) {
// Number of full frames we hope to write (but will not, used to
// set content-length header correctly)
static int kFrameCount = 3;
scoped_ptr<std::string> content(
new std::string(kMaxSpdyFrameChunkSize, 'a'));
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(
kMaxSpdyFrameChunkSize * kFrameCount, NULL, 0));
scoped_ptr<SpdyFrame> body(
ConstructSpdyBodyFrame(1, content->c_str(), content->size(), false));
scoped_ptr<SpdyFrame> rst(
ConstructSpdyRstStream(1, FLOW_CONTROL_ERROR));
// We're not going to write a data frame with FIN, we'll receive a bad
// WINDOW_UPDATE while sending a request and will send a RST_STREAM frame.
MockWrite writes[] = {
CreateMockWrite(*req, 0),
CreateMockWrite(*body, 2),
CreateMockWrite(*rst, 3),
};
static const int32 kDeltaWindowSize = 0x7fffffff; // cause an overflow
scoped_ptr<SpdyFrame> window_update(
ConstructSpdyWindowUpdate(1, kDeltaWindowSize));
MockRead reads[] = {
CreateMockRead(*window_update, 1),
MockRead(ASYNC, 0, 4) // EOF
};
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
ScopedVector<UploadElementReader> element_readers;
for (int i = 0; i < kFrameCount; ++i) {
element_readers.push_back(
new UploadBytesElementReader(content->c_str(), content->size()));
}
UploadDataStream upload_data_stream(&element_readers, 0);
// Setup the request
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_data_stream;
NormalSpdyTransactionHelper helper(request,
BoundNetLog(), GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
ASSERT_EQ(ERR_IO_PENDING, rv);
data.RunFor(5);
ASSERT_TRUE(callback.have_result());
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, callback.WaitForResult());
helper.VerifyDataConsumed();
}
// Test that after hitting a send window size of 0, the write process
// stalls and upon receiving WINDOW_UPDATE frame write resumes.
// This test constructs a POST request followed by enough data frames
// containing 'a' that would make the window size 0, followed by another
// data frame containing default content (which is "hello!") and this frame
// also contains a FIN flag. DelayedSocketData is used to enforce all
// writes go through before a read could happen. However, the last frame
// ("hello!") is not supposed to go through since by the time its turn
// arrives, window size is 0. At this point MessageLoop::Run() called via
// callback would block. Therefore we call MessageLoop::RunUntilIdle()
// which returns after performing all possible writes. We use DCHECKS to
// ensure that last data frame is still there and stream has stalled.
// After that, next read is artifically enforced, which causes a
// WINDOW_UPDATE to be read and I/O process resumes.
TEST_P(SpdyNetworkTransactionSpdy3Test, FlowControlStallResume) {
// Number of frames we need to send to zero out the window size: data
// frames plus SYN_STREAM plus the last data frame; also we need another
// data frame that we will send once the WINDOW_UPDATE is received,
// therefore +3.
size_t num_writes = kSpdyStreamInitialWindowSize / kMaxSpdyFrameChunkSize + 3;
// Calculate last frame's size; 0 size data frame is legal.
size_t last_frame_size =
kSpdyStreamInitialWindowSize % kMaxSpdyFrameChunkSize;
// Construct content for a data frame of maximum size.
std::string content(kMaxSpdyFrameChunkSize, 'a');
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(
kSpdyStreamInitialWindowSize + kUploadDataSize, NULL, 0));
// Full frames.
scoped_ptr<SpdyFrame> body1(
ConstructSpdyBodyFrame(1, content.c_str(), content.size(), false));
// Last frame to zero out the window size.
scoped_ptr<SpdyFrame> body2(
ConstructSpdyBodyFrame(1, content.c_str(), last_frame_size, false));
// Data frame to be sent once WINDOW_UPDATE frame is received.
scoped_ptr<SpdyFrame> body3(ConstructSpdyBodyFrame(1, true));
// Fill in mock writes.
scoped_array<MockWrite> writes(new MockWrite[num_writes]);
size_t i = 0;
writes[i] = CreateMockWrite(*req);
for (i = 1; i < num_writes - 2; i++)
writes[i] = CreateMockWrite(*body1);
writes[i++] = CreateMockWrite(*body2);
writes[i] = CreateMockWrite(*body3);
// Construct read frame, give enough space to upload the rest of the
// data.
scoped_ptr<SpdyFrame> window_update(
ConstructSpdyWindowUpdate(1, kUploadDataSize));
scoped_ptr<SpdyFrame> reply(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*window_update),
CreateMockRead(*window_update),
CreateMockRead(*reply),
CreateMockRead(*body2),
CreateMockRead(*body3),
MockRead(ASYNC, 0, 0) // EOF
};
// Force all writes to happen before any read, last write will not
// actually queue a frame, due to window size being 0.
DelayedSocketData data(num_writes, reads, arraysize(reads),
writes.get(), num_writes);
ScopedVector<UploadElementReader> element_readers;
std::string upload_data_string(kSpdyStreamInitialWindowSize, 'a');
upload_data_string.append(kUploadData, kUploadDataSize);
element_readers.push_back(new UploadBytesElementReader(
upload_data_string.c_str(), upload_data_string.size()));
UploadDataStream upload_data_stream(&element_readers, 0);
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_data_stream;
NormalSpdyTransactionHelper helper(request, BoundNetLog(), GetParam(), NULL);
helper.AddData(&data);
helper.RunPreTestSetup();
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
MessageLoop::current()->RunUntilIdle(); // Write as much as we can.
SpdyHttpStream* stream = static_cast<SpdyHttpStream*>(trans->stream_.get());
ASSERT_TRUE(stream != NULL);
ASSERT_TRUE(stream->stream() != NULL);
EXPECT_EQ(0, stream->stream()->send_window_size());
// All the body data should have been read.
// TODO(satorux): This is because of the weirdness in reading the request
// body in OnSendBodyComplete(). See crbug.com/113107.
EXPECT_TRUE(upload_data_stream.IsEOF());
// But the body is not yet fully sent (kUploadData is not yet sent).
EXPECT_FALSE(stream->stream()->body_sent());
data.ForceNextRead(); // Read in WINDOW_UPDATE frame.
rv = callback.WaitForResult();
helper.VerifyDataConsumed();
}
// Test we correctly handle the case where the SETTINGS frame results in
// unstalling the send window.
TEST_P(SpdyNetworkTransactionSpdy3Test, FlowControlStallResumeAfterSettings) {
// Number of frames we need to send to zero out the window size: data
// frames plus SYN_STREAM plus the last data frame; also we need another
// data frame that we will send once the SETTING is received, therefore +3.
size_t num_writes = kSpdyStreamInitialWindowSize / kMaxSpdyFrameChunkSize + 3;
// Calculate last frame's size; 0 size data frame is legal.
size_t last_frame_size =
kSpdyStreamInitialWindowSize % kMaxSpdyFrameChunkSize;
// Construct content for a data frame of maximum size.
std::string content(kMaxSpdyFrameChunkSize, 'a');
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(
kSpdyStreamInitialWindowSize + kUploadDataSize, NULL, 0));
// Full frames.
scoped_ptr<SpdyFrame> body1(
ConstructSpdyBodyFrame(1, content.c_str(), content.size(), false));
// Last frame to zero out the window size.
scoped_ptr<SpdyFrame> body2(
ConstructSpdyBodyFrame(1, content.c_str(), last_frame_size, false));
// Data frame to be sent once SETTINGS frame is received.
scoped_ptr<SpdyFrame> body3(ConstructSpdyBodyFrame(1, true));
// Fill in mock writes.
scoped_array<MockWrite> writes(new MockWrite[num_writes]);
size_t i = 0;
writes[i] = CreateMockWrite(*req);
for (i = 1; i < num_writes - 2; i++)
writes[i] = CreateMockWrite(*body1);
writes[i++] = CreateMockWrite(*body2);
writes[i] = CreateMockWrite(*body3);
// Construct read frame for SETTINGS that gives enough space to upload the
// rest of the data.
SettingsMap settings;
settings[SETTINGS_INITIAL_WINDOW_SIZE] =
SettingsFlagsAndValue(
SETTINGS_FLAG_NONE, kSpdyStreamInitialWindowSize * 2);
scoped_ptr<SpdyFrame> settings_frame_large(ConstructSpdySettings(settings));
scoped_ptr<SpdyFrame> reply(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*settings_frame_large),
CreateMockRead(*reply),
CreateMockRead(*body2),
CreateMockRead(*body3),
MockRead(ASYNC, 0, 0) // EOF
};
// Force all writes to happen before any read, last write will not
// actually queue a frame, due to window size being 0.
DelayedSocketData data(num_writes, reads, arraysize(reads),
writes.get(), num_writes);
ScopedVector<UploadElementReader> element_readers;
std::string upload_data_string(kSpdyStreamInitialWindowSize, 'a');
upload_data_string.append(kUploadData, kUploadDataSize);
element_readers.push_back(new UploadBytesElementReader(
upload_data_string.c_str(), upload_data_string.size()));
UploadDataStream upload_data_stream(&element_readers, 0);
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_data_stream;
NormalSpdyTransactionHelper helper(request, BoundNetLog(), GetParam(), NULL);
helper.AddData(&data);
helper.RunPreTestSetup();
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
MessageLoop::current()->RunUntilIdle(); // Write as much as we can.
SpdyHttpStream* stream = static_cast<SpdyHttpStream*>(trans->stream_.get());
ASSERT_TRUE(stream != NULL);
ASSERT_TRUE(stream->stream() != NULL);
EXPECT_EQ(0, stream->stream()->send_window_size());
// All the body data should have been read.
// TODO(satorux): This is because of the weirdness in reading the request
// body in OnSendBodyComplete(). See crbug.com/113107.
EXPECT_TRUE(upload_data_stream.IsEOF());
// But the body is not yet fully sent (kUploadData is not yet sent).
EXPECT_FALSE(stream->stream()->body_sent());
EXPECT_TRUE(stream->stream()->stalled_by_flow_control());
data.ForceNextRead(); // Read in SETTINGS frame to unstall.
rv = callback.WaitForResult();
helper.VerifyDataConsumed();
EXPECT_FALSE(stream->stream()->stalled_by_flow_control());
}
// Test we correctly handle the case where the SETTINGS frame results in a
// negative send window size.
TEST_P(SpdyNetworkTransactionSpdy3Test, FlowControlNegativeSendWindowSize) {
// Number of frames we need to send to zero out the window size: data
// frames plus SYN_STREAM plus the last data frame; also we need another
// data frame that we will send once the SETTING is received, therefore +3.
size_t num_writes = kSpdyStreamInitialWindowSize / kMaxSpdyFrameChunkSize + 3;
// Calculate last frame's size; 0 size data frame is legal.
size_t last_frame_size =
kSpdyStreamInitialWindowSize % kMaxSpdyFrameChunkSize;
// Construct content for a data frame of maximum size.
std::string content(kMaxSpdyFrameChunkSize, 'a');
scoped_ptr<SpdyFrame> req(ConstructSpdyPost(
kSpdyStreamInitialWindowSize + kUploadDataSize, NULL, 0));
// Full frames.
scoped_ptr<SpdyFrame> body1(
ConstructSpdyBodyFrame(1, content.c_str(), content.size(), false));
// Last frame to zero out the window size.
scoped_ptr<SpdyFrame> body2(
ConstructSpdyBodyFrame(1, content.c_str(), last_frame_size, false));
// Data frame to be sent once SETTINGS frame is received.
scoped_ptr<SpdyFrame> body3(ConstructSpdyBodyFrame(1, true));
// Fill in mock writes.
scoped_array<MockWrite> writes(new MockWrite[num_writes]);
size_t i = 0;
writes[i] = CreateMockWrite(*req);
for (i = 1; i < num_writes - 2; i++)
writes[i] = CreateMockWrite(*body1);
writes[i++] = CreateMockWrite(*body2);
writes[i] = CreateMockWrite(*body3);
// Construct read frame for SETTINGS that makes the send_window_size
// negative.
SettingsMap new_settings;
new_settings[SETTINGS_INITIAL_WINDOW_SIZE] =
SettingsFlagsAndValue(
SETTINGS_FLAG_NONE, kSpdyStreamInitialWindowSize / 2);
scoped_ptr<SpdyFrame> settings_frame_small(
ConstructSpdySettings(new_settings));
// Construct read frame for WINDOW_UPDATE that makes the send_window_size
// postive.
scoped_ptr<SpdyFrame> window_update_init_size(
ConstructSpdyWindowUpdate(1, kSpdyStreamInitialWindowSize));
scoped_ptr<SpdyFrame> reply(ConstructSpdyPostSynReply(NULL, 0));
MockRead reads[] = {
CreateMockRead(*settings_frame_small),
CreateMockRead(*window_update_init_size),
CreateMockRead(*reply),
CreateMockRead(*body2),
CreateMockRead(*body3),
MockRead(ASYNC, 0, 0) // EOF
};
// Force all writes to happen before any read, last write will not actually
// queue a frame, due to window size being 0.
DelayedSocketData data(num_writes, reads, arraysize(reads),
writes.get(), num_writes);
ScopedVector<UploadElementReader> element_readers;
std::string upload_data_string(kSpdyStreamInitialWindowSize, 'a');
upload_data_string.append(kUploadData, kUploadDataSize);
element_readers.push_back(new UploadBytesElementReader(
upload_data_string.c_str(), upload_data_string.size()));
UploadDataStream upload_data_stream(&element_readers, 0);
HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_data_stream;
NormalSpdyTransactionHelper helper(request, BoundNetLog(), GetParam(), NULL);
helper.AddData(&data);
helper.RunPreTestSetup();
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
MessageLoop::current()->RunUntilIdle(); // Write as much as we can.
SpdyHttpStream* stream = static_cast<SpdyHttpStream*>(trans->stream_.get());
ASSERT_TRUE(stream != NULL);
ASSERT_TRUE(stream->stream() != NULL);
EXPECT_EQ(0, stream->stream()->send_window_size());
// All the body data should have been read.
// TODO(satorux): This is because of the weirdness in reading the request
// body in OnSendBodyComplete(). See crbug.com/113107.
EXPECT_TRUE(upload_data_stream.IsEOF());
// But the body is not yet fully sent (kUploadData is not yet sent).
EXPECT_FALSE(stream->stream()->body_sent());
data.ForceNextRead(); // Read in WINDOW_UPDATE or SETTINGS frame.
rv = callback.WaitForResult();
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionSpdy3Test, ResetReplyWithTransferEncoding) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> rst(ConstructSpdyRstStream(1, PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*rst),
};
const char* const headers[] = {
"transfer-encoding", "chuncked"
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(headers, 1, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, out.rv);
helper.session()->spdy_session_pool()->CloseAllSessions();
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionSpdy3Test, ResetPushWithTransferEncoding) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> rst(ConstructSpdyRstStream(2, PROTOCOL_ERROR));
MockWrite writes[] = {
CreateMockWrite(*req),
CreateMockWrite(*rst),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
const char* const headers[] = {":scheme", "http",
":host", "www.google.com",
":path", "/1",
"transfer-encoding", "chunked"};
scoped_ptr<SpdyFrame> push(ConstructSpdyPush(headers, arraysize(headers) / 2,
2, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp),
CreateMockRead(*push),
CreateMockRead(*body),
MockRead(ASYNC, 0, 0) // EOF
};
DelayedSocketData data(1, reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunToCompletion(&data);
TransactionHelperResult out = helper.output();
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("hello!", out.response_data);
helper.session()->spdy_session_pool()->CloseAllSessions();
helper.VerifyDataConsumed();
}
TEST_P(SpdyNetworkTransactionSpdy3Test, CancelledTransaction) {
// Construct the request.
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
MockWrite writes[] = {
CreateMockWrite(*req),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp),
// This following read isn't used by the test, except during the
// RunUntilIdle() call at the end since the SpdySession survives the
// HttpNetworkTransaction and still tries to continue Read()'ing. Any
// MockRead will do here.
MockRead(ASYNC, 0, 0) // EOF
};
StaticSocketDataProvider data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
helper.ResetTrans(); // Cancel the transaction.
// Flush the MessageLoop while the SpdySessionDependencies (in particular, the
// MockClientSocketFactory) are still alive.
MessageLoop::current()->RunUntilIdle();
helper.VerifyDataNotConsumed();
}
// Verify that the client sends a Rst Frame upon cancelling the stream.
TEST_P(SpdyNetworkTransactionSpdy3Test, CancelledTransactionSendRst) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
scoped_ptr<SpdyFrame> rst(
ConstructSpdyRstStream(1, CANCEL));
MockWrite writes[] = {
CreateMockWrite(*req, 0, SYNCHRONOUS),
CreateMockWrite(*rst, 2, SYNCHRONOUS),
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp, 1, ASYNC),
MockRead(ASYNC, 0, 0, 3) // EOF
};
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(),
GetParam(), NULL);
helper.SetDeterministic();
helper.RunPreTestSetup();
helper.AddDeterministicData(&data);
HttpNetworkTransaction* trans = helper.trans();
TestCompletionCallback callback;
int rv = trans->Start(
&CreateGetRequest(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
data.SetStop(2);
data.Run();
helper.ResetTrans();
data.SetStop(20);
data.Run();
helper.VerifyDataConsumed();
}
// Verify that the client can correctly deal with the user callback attempting
// to start another transaction on a session that is closing down. See
// http://crbug.com/47455
TEST_P(SpdyNetworkTransactionSpdy3Test, StartTransactionOnReadCallback) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
MockWrite writes[] = { CreateMockWrite(*req) };
MockWrite writes2[] = { CreateMockWrite(*req) };
// The indicated length of this packet is longer than its actual length. When
// the session receives an empty packet after this one, it shuts down the
// session, and calls the read callback with the incomplete data.
const uint8 kGetBodyFrame2[] = {
0x00, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x07,
'h', 'e', 'l', 'l', 'o', '!',
};
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
MockRead reads[] = {
CreateMockRead(*resp, 2),
MockRead(ASYNC, ERR_IO_PENDING, 3), // Force a pause
MockRead(ASYNC, reinterpret_cast<const char*>(kGetBodyFrame2),
arraysize(kGetBodyFrame2), 4),
MockRead(ASYNC, ERR_IO_PENDING, 5), // Force a pause
MockRead(ASYNC, 0, 0, 6), // EOF
};
MockRead reads2[] = {
CreateMockRead(*resp, 2),
MockRead(ASYNC, 0, 0, 3), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
DelayedSocketData data2(1, reads2, arraysize(reads2),
writes2, arraysize(writes2));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
helper.AddData(&data2);
HttpNetworkTransaction* trans = helper.trans();
// Start the transaction with basic parameters.
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
const int kSize = 3000;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
rv = trans->Read(
buf, kSize,
base::Bind(&SpdyNetworkTransactionSpdy3Test::StartTransactionCallback,
helper.session()));
// This forces an err_IO_pending, which sets the callback.
data.CompleteRead();
// This finishes the read.
data.CompleteRead();
helper.VerifyDataConsumed();
}
// Verify that the client can correctly deal with the user callback deleting the
// transaction. Failures will usually be valgrind errors. See
// http://crbug.com/46925
TEST_P(SpdyNetworkTransactionSpdy3Test, DeleteSessionOnReadCallback) {
scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST));
MockWrite writes[] = { CreateMockWrite(*req) };
scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1));
scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true));
MockRead reads[] = {
CreateMockRead(*resp.get(), 2),
MockRead(ASYNC, ERR_IO_PENDING, 3), // Force a pause
CreateMockRead(*body.get(), 4),
MockRead(ASYNC, 0, 0, 5), // EOF
};
OrderedSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
NormalSpdyTransactionHelper helper(CreateGetRequest(),
BoundNetLog(), GetParam(), NULL);
helper.RunPreTestSetup();
helper.AddData(&data);
HttpNetworkTransaction* trans = helper.trans();
// Start the transaction with basic parameters.
TestCompletionCallback callback;
int rv = trans->Start(&helper.request(), callback.callback(), BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
// Setup a user callback which will delete the session, and clear out the
// memory holding the stream object. Note that the callback deletes trans.
const int kSize = 3000;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
rv = trans->Read(
buf, kSize,
base::Bind(&SpdyNetworkTransactionSpdy3Test::DeleteSessionCallback,
base::Unretained(&helper)));
ASSERT_EQ(ERR_IO_PENDING, rv);
data.CompleteRead();
// Finish running rest of tasks.
MessageLoop::current()->RunUntilIdle();
helper.VerifyDataConsumed();
}
// Send a spdy request to www.google.com that gets redirected to www.foo.com.
TEST_P(SpdyNetworkTransactionSpdy3Test, RedirectGetRequest) {
// These are headers which the net::URLRequest tacks on.
const char* const kExtraHeaders[] = {
"accept-encoding",