blob: 14380c8f16a537dd573cfd21e20c127b00f254fb [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/quic/chromium/quic_stream_factory.h"
#include <memory>
#include <ostream>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/base/test_proxy_delegate.h"
#include "net/cert/cert_verifier.h"
#include "net/cert/ct_policy_enforcer.h"
#include "net/cert/multi_log_ct_verifier.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_response_info.h"
#include "net/http/http_server_properties_impl.h"
#include "net/http/http_util.h"
#include "net/http/transport_security_state.h"
#include "net/quic/chromium/crypto/proof_verifier_chromium.h"
#include "net/quic/chromium/mock_crypto_client_stream_factory.h"
#include "net/quic/chromium/mock_network_change_notifier.h"
#include "net/quic/chromium/mock_quic_data.h"
#include "net/quic/chromium/quic_http_utils.h"
#include "net/quic/chromium/quic_test_packet_maker.h"
#include "net/quic/core/crypto/crypto_handshake.h"
#include "net/quic/core/crypto/properties_based_quic_server_info.h"
#include "net/quic/core/crypto/quic_crypto_client_config.h"
#include "net/quic/core/crypto/quic_decrypter.h"
#include "net/quic/core/crypto/quic_encrypter.h"
#include "net/quic/core/crypto/quic_server_info.h"
#include "net/quic/core/quic_client_promised_info.h"
#include "net/quic/test_tools/mock_clock.h"
#include "net/quic/test_tools/mock_random.h"
#include "net/quic/test_tools/quic_config_peer.h"
#include "net/quic/test_tools/quic_stream_factory_peer.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/quic/test_tools/test_task_runner.h"
#include "net/socket/next_proto.h"
#include "net/socket/socket_test_util.h"
#include "net/spdy/spdy_session_test_util.h"
#include "net/spdy/spdy_test_utils.h"
#include "net/ssl/channel_id_service.h"
#include "net/ssl/default_channel_id_store.h"
#include "net/test/cert_test_util.h"
#include "net/test/gtest_util.h"
#include "net/test/test_data_directory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using net::test::IsError;
using net::test::IsOk;
using std::string;
namespace net {
namespace {
class MockSSLConfigService : public SSLConfigService {
public:
MockSSLConfigService() {}
void GetSSLConfig(SSLConfig* config) override { *config = config_; }
private:
~MockSSLConfigService() override {}
SSLConfig config_;
};
} // namespace
namespace test {
namespace {
enum DestinationType {
// In pooling tests with two requests for different origins to the same
// destination, the destination should be
SAME_AS_FIRST, // the same as the first origin,
SAME_AS_SECOND, // the same as the second origin, or
DIFFERENT, // different from both.
};
const char kDefaultServerHostName[] = "www.example.org";
const char kServer2HostName[] = "mail.example.org";
const char kDifferentHostname[] = "different.example.com";
const int kDefaultServerPort = 443;
const char kDefaultUrl[] = "https://www.example.org/";
const char kServer2Url[] = "https://mail.example.org/";
const char kServer3Url[] = "https://docs.example.org/";
const char kServer4Url[] = "https://images.example.org/";
// Run QuicStreamFactoryTest instances with all value combinations of version
// and enable_connection_racting.
struct TestParams {
friend std::ostream& operator<<(std::ostream& os, const TestParams& p) {
os << "{ version: " << QuicVersionToString(p.version)
<< ", enable_connection_racing: "
<< (p.enable_connection_racing ? "true" : "false") << " }";
return os;
}
QuicVersion version;
bool enable_connection_racing;
};
std::vector<TestParams> GetTestParams() {
std::vector<TestParams> params;
QuicVersionVector all_supported_versions = AllSupportedVersions();
for (const QuicVersion version : all_supported_versions) {
params.push_back(TestParams{version, false});
params.push_back(TestParams{version, true});
}
return params;
}
// Run QuicStreamFactoryWithDestinationTest instances with all value
// combinations of version, enable_connection_racting, and destination_type.
struct PoolingTestParams {
friend std::ostream& operator<<(std::ostream& os,
const PoolingTestParams& p) {
os << "{ version: " << QuicVersionToString(p.version)
<< ", enable_connection_racing: "
<< (p.enable_connection_racing ? "true" : "false")
<< ", destination_type: ";
switch (p.destination_type) {
case SAME_AS_FIRST:
os << "SAME_AS_FIRST";
break;
case SAME_AS_SECOND:
os << "SAME_AS_SECOND";
break;
case DIFFERENT:
os << "DIFFERENT";
break;
}
os << " }";
return os;
}
QuicVersion version;
bool enable_connection_racing;
DestinationType destination_type;
};
std::vector<PoolingTestParams> GetPoolingTestParams() {
std::vector<PoolingTestParams> params;
QuicVersionVector all_supported_versions = AllSupportedVersions();
for (const QuicVersion version : all_supported_versions) {
params.push_back(PoolingTestParams{version, false, SAME_AS_FIRST});
params.push_back(PoolingTestParams{version, false, SAME_AS_SECOND});
params.push_back(PoolingTestParams{version, false, DIFFERENT});
params.push_back(PoolingTestParams{version, true, SAME_AS_FIRST});
params.push_back(PoolingTestParams{version, true, SAME_AS_SECOND});
params.push_back(PoolingTestParams{version, true, DIFFERENT});
}
return params;
}
} // namespace
class QuicHttpStreamPeer {
public:
static QuicChromiumClientSession* GetSession(QuicHttpStream* stream) {
return stream->session_.get();
}
};
class MockQuicServerInfo : public QuicServerInfo {
public:
explicit MockQuicServerInfo(const QuicServerId& server_id)
: QuicServerInfo(server_id) {}
~MockQuicServerInfo() override {}
void Start() override {}
int WaitForDataReady(const CompletionCallback& callback) override {
return ERR_IO_PENDING;
}
void ResetWaitForDataReadyCallback() override {}
void CancelWaitForDataReadyCallback() override {}
bool IsDataReady() override { return false; }
bool IsReadyToPersist() override { return false; }
void Persist() override {}
void OnExternalCacheHit() override {}
};
class MockQuicServerInfoFactory : public QuicServerInfoFactory {
public:
MockQuicServerInfoFactory() {}
~MockQuicServerInfoFactory() override {}
QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
return new MockQuicServerInfo(server_id);
}
};
class QuicStreamFactoryTestBase {
protected:
QuicStreamFactoryTestBase(QuicVersion version, bool enable_connection_racing)
: ssl_config_service_(new MockSSLConfigService),
random_generator_(0),
clock_(new MockClock()),
runner_(new TestTaskRunner(clock_)),
version_(version),
client_maker_(version_,
0,
clock_,
kDefaultServerHostName,
Perspective::IS_CLIENT),
server_maker_(version_,
0,
clock_,
kDefaultServerHostName,
Perspective::IS_SERVER),
cert_verifier_(CertVerifier::CreateDefault()),
channel_id_service_(
new ChannelIDService(new DefaultChannelIDStore(nullptr),
base::ThreadTaskRunnerHandle::Get())),
cert_transparency_verifier_(new MultiLogCTVerifier()),
scoped_mock_network_change_notifier_(nullptr),
factory_(nullptr),
host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
url_(kDefaultUrl),
url2_(kServer2Url),
url3_(kServer3Url),
url4_(kServer4Url),
privacy_mode_(PRIVACY_MODE_DISABLED),
always_require_handshake_confirmation_(false),
disable_connection_pooling_(false),
load_server_info_timeout_srtt_multiplier_(0.0f),
enable_connection_racing_(enable_connection_racing),
enable_non_blocking_io_(true),
disable_disk_cache_(false),
prefer_aes_(false),
receive_buffer_size_(0),
delay_tcp_race_(true),
close_sessions_on_ip_change_(false),
disable_quic_on_timeout_with_open_streams_(false),
idle_connection_timeout_seconds_(kIdleConnectionTimeoutSeconds),
reduced_ping_timeout_seconds_(kPingTimeoutSecs),
packet_reader_yield_after_duration_milliseconds_(
kQuicYieldAfterDurationMilliseconds),
migrate_sessions_on_network_change_(false),
migrate_sessions_early_(false),
allow_server_migration_(false),
force_hol_blocking_(false),
race_cert_verification_(false) {
clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
}
~QuicStreamFactoryTestBase() {
// If |factory_| was initialized, then it took over ownership of |clock_|.
// If |factory_| was not initialized, then |clock_| needs to be destroyed.
if (!factory_)
delete clock_;
}
void Initialize() {
DCHECK(!factory_);
factory_.reset(new QuicStreamFactory(
net_log_.net_log(), &host_resolver_, ssl_config_service_.get(),
&socket_factory_, &http_server_properties_, &test_proxy_delegate_,
cert_verifier_.get(), &ct_policy_enforcer_, channel_id_service_.get(),
&transport_security_state_, cert_transparency_verifier_.get(),
/*SocketPerformanceWatcherFactory*/ nullptr,
&crypto_client_stream_factory_, &random_generator_, clock_,
kDefaultMaxPacketSize, string(), SupportedVersions(version_),
always_require_handshake_confirmation_, disable_connection_pooling_,
load_server_info_timeout_srtt_multiplier_, enable_connection_racing_,
enable_non_blocking_io_, disable_disk_cache_, prefer_aes_,
receive_buffer_size_, delay_tcp_race_,
/*max_server_configs_stored_in_properties*/ 0,
close_sessions_on_ip_change_,
disable_quic_on_timeout_with_open_streams_,
idle_connection_timeout_seconds_, reduced_ping_timeout_seconds_,
packet_reader_yield_after_duration_milliseconds_,
migrate_sessions_on_network_change_, migrate_sessions_early_,
allow_server_migration_, force_hol_blocking_, race_cert_verification_,
/*do_not_fragment*/ true, QuicTagVector(),
/*enable_token_binding*/ false));
factory_->set_require_confirmation(false);
EXPECT_FALSE(factory_->has_quic_server_info_factory());
factory_->set_quic_server_info_factory(new MockQuicServerInfoFactory());
EXPECT_TRUE(factory_->has_quic_server_info_factory());
}
void InitializeConnectionMigrationTest(
NetworkChangeNotifier::NetworkList connected_networks) {
scoped_mock_network_change_notifier_.reset(
new ScopedMockNetworkChangeNotifier());
MockNetworkChangeNotifier* mock_ncn =
scoped_mock_network_change_notifier_->mock_network_change_notifier();
mock_ncn->ForceNetworkHandlesSupported();
mock_ncn->SetConnectedNetworksList(connected_networks);
migrate_sessions_on_network_change_ = true;
migrate_sessions_early_ = true;
Initialize();
}
bool HasActiveSession(const HostPortPair& host_port_pair) {
QuicServerId server_id(host_port_pair, PRIVACY_MODE_DISABLED);
return QuicStreamFactoryPeer::HasActiveSession(factory_.get(), server_id);
}
bool HasActiveCertVerifierJob(const QuicServerId& server_id) {
return QuicStreamFactoryPeer::HasActiveCertVerifierJob(factory_.get(),
server_id);
}
QuicChromiumClientSession* GetActiveSession(
const HostPortPair& host_port_pair) {
QuicServerId server_id(host_port_pair, PRIVACY_MODE_DISABLED);
return QuicStreamFactoryPeer::GetActiveSession(factory_.get(), server_id);
}
std::unique_ptr<QuicHttpStream> CreateFromSession(
const HostPortPair& host_port_pair) {
QuicChromiumClientSession* session = GetActiveSession(host_port_pair);
return QuicStreamFactoryPeer::CreateFromSession(factory_.get(), session);
}
int GetSourcePortForNewSession(const HostPortPair& destination) {
return GetSourcePortForNewSessionInner(destination, false);
}
int GetSourcePortForNewSessionAndGoAway(const HostPortPair& destination) {
return GetSourcePortForNewSessionInner(destination, true);
}
int GetSourcePortForNewSessionInner(const HostPortPair& destination,
bool goaway_received) {
// Should only be called if there is no active session for this destination.
EXPECT_FALSE(HasActiveSession(destination));
size_t socket_count = socket_factory_.udp_client_socket_ports().size();
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
GURL url("https://" + destination.host() + "/");
EXPECT_EQ(ERR_IO_PENDING,
request.Request(destination, privacy_mode_,
/*cert_verify_flags=*/0, url, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
stream.reset();
QuicChromiumClientSession* session = GetActiveSession(destination);
if (socket_count + 1 != socket_factory_.udp_client_socket_ports().size()) {
ADD_FAILURE();
return 0;
}
if (goaway_received) {
QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
session->connection()->OnGoAwayFrame(goaway);
}
factory_->OnSessionClosed(session);
EXPECT_FALSE(HasActiveSession(destination));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
return socket_factory_.udp_client_socket_ports()[socket_count];
}
std::unique_ptr<QuicEncryptedPacket> ConstructClientConnectionClosePacket(
QuicPacketNumber num) {
return client_maker_.MakeConnectionClosePacket(num);
}
std::unique_ptr<QuicEncryptedPacket> ConstructClientRstPacket() {
QuicStreamId stream_id = kClientDataStreamId1;
return client_maker_.MakeRstPacket(1, true, stream_id,
QUIC_RST_ACKNOWLEDGEMENT);
}
std::unique_ptr<QuicEncryptedPacket> ConstructClientRstPacket(
QuicPacketNumber packet_number) {
QuicStreamId stream_id = kClientDataStreamId1;
return client_maker_.MakeRstPacket(packet_number, true, stream_id,
QUIC_RST_ACKNOWLEDGEMENT);
}
static ProofVerifyDetailsChromium DefaultProofVerifyDetails() {
// Load a certificate that is valid for *.example.org
scoped_refptr<X509Certificate> test_cert(
ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"));
EXPECT_TRUE(test_cert.get());
ProofVerifyDetailsChromium verify_details;
verify_details.cert_verify_result.verified_cert = test_cert;
verify_details.cert_verify_result.is_issued_by_known_root = true;
return verify_details;
}
void NotifyIPAddressChanged() {
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
// Spin the message loop so the notification is delivered.
base::RunLoop().RunUntilIdle();
}
std::unique_ptr<QuicEncryptedPacket> ConstructGetRequestPacket(
QuicPacketNumber packet_number,
QuicStreamId stream_id,
bool should_include_version,
bool fin) {
SpdyHeaderBlock headers =
client_maker_.GetRequestHeaders("GET", "https", "/");
SpdyPriority priority =
ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY);
size_t spdy_headers_frame_len;
return client_maker_.MakeRequestHeadersPacket(
packet_number, stream_id, should_include_version, fin, priority,
std::move(headers), &spdy_headers_frame_len);
}
std::unique_ptr<QuicEncryptedPacket> ConstructGetRequestPacket(
QuicPacketNumber packet_number,
QuicStreamId stream_id,
bool should_include_version,
bool fin,
QuicStreamOffset* offset) {
SpdyHeaderBlock headers =
client_maker_.GetRequestHeaders("GET", "https", "/");
SpdyPriority priority =
ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY);
size_t spdy_headers_frame_len;
return client_maker_.MakeRequestHeadersPacket(
packet_number, stream_id, should_include_version, fin, priority,
std::move(headers), &spdy_headers_frame_len, offset);
}
std::unique_ptr<QuicEncryptedPacket> ConstructOkResponsePacket(
QuicPacketNumber packet_number,
QuicStreamId stream_id,
bool should_include_version,
bool fin) {
SpdyHeaderBlock headers = server_maker_.GetResponseHeaders("200 OK");
size_t spdy_headers_frame_len;
return server_maker_.MakeResponseHeadersPacket(
packet_number, stream_id, should_include_version, fin,
std::move(headers), &spdy_headers_frame_len);
}
std::unique_ptr<QuicReceivedPacket> ConstructSettingsPacket(
QuicPacketNumber packet_number,
SpdySettingsIds id,
size_t value,
QuicStreamOffset* offset) {
return client_maker_.MakeSettingsPacket(packet_number, id, value,
kIncludeVersion, offset);
}
// Helper method for server migration tests.
void VerifyServerMigration(QuicConfig& config, IPEndPoint expected_address) {
allow_server_migration_ = true;
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.SetConfig(config);
// Set up first socket data provider.
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddSocketDataToFactory(&socket_factory_);
// Set up second socket data provider that is used after
// migration.
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddWrite(
client_maker_.MakePingPacket(2, /*include_version=*/true));
socket_data2.AddWrite(client_maker_.MakeRstPacket(
3, true, kClientDataStreamId1, QUIC_STREAM_CANCELLED));
socket_data2.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_EQ(OK, callback_.WaitForResult());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
request_info.method = "GET";
request_info.url = GURL("https://www.example.org/");
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
IPEndPoint actual_address;
session->GetDefaultSocket()->GetPeerAddress(&actual_address);
EXPECT_EQ(actual_address, expected_address);
DVLOG(1) << "Socket connected to: " << actual_address.address().ToString()
<< " " << actual_address.port();
DVLOG(1) << "Expected address: " << expected_address.address().ToString()
<< " " << expected_address.port();
stream.reset();
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
// Verifies that the QUIC stream factory is initialized correctly.
// If |proxy_delegate_provides_quic_supported_proxy| is true, then
// ProxyDelegate provides a proxy that supports QUIC at startup. Otherwise,
// a non proxy server that support alternative services is added to the
// HttpServerProperties map.
void VerifyInitialization(bool proxy_delegate_provides_quic_supported_proxy) {
idle_connection_timeout_seconds_ = 500;
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
const QuicConfig* config = QuicStreamFactoryPeer::GetConfig(factory_.get());
EXPECT_EQ(500, config->IdleNetworkTimeout().ToSeconds());
QuicStreamFactoryPeer::SetTaskRunner(factory_.get(), runner_.get());
const AlternativeService alternative_service1(
kProtoQUIC, host_port_pair_.host(), host_port_pair_.port());
AlternativeServiceInfoVector alternative_service_info_vector;
base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1);
alternative_service_info_vector.push_back(
AlternativeServiceInfo(alternative_service1, expiration));
http_server_properties_.SetAlternativeServices(
url::SchemeHostPort(url_), alternative_service_info_vector);
HostPortPair host_port_pair2(kServer2HostName, kDefaultServerPort);
url::SchemeHostPort server2("https", kServer2HostName, kDefaultServerPort);
const AlternativeService alternative_service2(
kProtoQUIC, host_port_pair2.host(), host_port_pair2.port());
AlternativeServiceInfoVector alternative_service_info_vector2;
alternative_service_info_vector2.push_back(
AlternativeServiceInfo(alternative_service2, expiration));
if (!proxy_delegate_provides_quic_supported_proxy) {
http_server_properties_.SetAlternativeServices(
server2, alternative_service_info_vector2);
// Verify that the properties of both QUIC servers are stored in the
// HTTP properties map.
EXPECT_EQ(2U, http_server_properties_.alternative_service_map().size());
} else {
test_proxy_delegate_.set_alternative_proxy_server(net::ProxyServer(
net::ProxyServer::SCHEME_QUIC,
net::HostPortPair(kServer2HostName, kDefaultServerPort)));
// Verify that the properties of only the first QUIC server are stored in
// the HTTP properties map.
EXPECT_EQ(1U, http_server_properties_.alternative_service_map().size());
}
http_server_properties_.SetMaxServerConfigsStoredInProperties(
kMaxQuicServersToPersist);
QuicServerId quic_server_id(kDefaultServerHostName, 80,
PRIVACY_MODE_DISABLED);
QuicServerInfoFactory* quic_server_info_factory =
new PropertiesBasedQuicServerInfoFactory(&http_server_properties_);
factory_->set_quic_server_info_factory(quic_server_info_factory);
std::unique_ptr<QuicServerInfo> quic_server_info(
quic_server_info_factory->GetForServer(quic_server_id));
// Update quic_server_info's server_config and persist it.
QuicServerInfo::State* state = quic_server_info->mutable_state();
// Minimum SCFG that passes config validation checks.
const char scfg[] = {// SCFG
0x53, 0x43, 0x46, 0x47,
// num entries
0x01, 0x00,
// padding
0x00, 0x00,
// EXPY
0x45, 0x58, 0x50, 0x59,
// EXPY end offset
0x08, 0x00, 0x00, 0x00,
// Value
'1', '2', '3', '4', '5', '6', '7', '8'};
// Create temporary strings becasue Persist() clears string data in |state|.
string server_config(reinterpret_cast<const char*>(&scfg), sizeof(scfg));
string source_address_token("test_source_address_token");
string cert_sct("test_cert_sct");
string chlo_hash("test_chlo_hash");
string signature("test_signature");
string test_cert("test_cert");
std::vector<string> certs;
certs.push_back(test_cert);
state->server_config = server_config;
state->source_address_token = source_address_token;
state->cert_sct = cert_sct;
state->chlo_hash = chlo_hash;
state->server_config_sig = signature;
state->certs = certs;
quic_server_info->Persist();
QuicServerId quic_server_id2(kServer2HostName, 80, PRIVACY_MODE_DISABLED);
std::unique_ptr<QuicServerInfo> quic_server_info2(
quic_server_info_factory->GetForServer(quic_server_id2));
// Update quic_server_info2's server_config and persist it.
QuicServerInfo::State* state2 = quic_server_info2->mutable_state();
// Minimum SCFG that passes config validation checks.
const char scfg2[] = {// SCFG
0x53, 0x43, 0x46, 0x47,
// num entries
0x01, 0x00,
// padding
0x00, 0x00,
// EXPY
0x45, 0x58, 0x50, 0x59,
// EXPY end offset
0x08, 0x00, 0x00, 0x00,
// Value
'8', '7', '3', '4', '5', '6', '2', '1'};
// Create temporary strings becasue Persist() clears string data in
// |state2|.
string server_config2(reinterpret_cast<const char*>(&scfg2), sizeof(scfg2));
string source_address_token2("test_source_address_token2");
string cert_sct2("test_cert_sct2");
string chlo_hash2("test_chlo_hash2");
string signature2("test_signature2");
string test_cert2("test_cert2");
std::vector<string> certs2;
certs2.push_back(test_cert2);
state2->server_config = server_config2;
state2->source_address_token = source_address_token2;
state2->cert_sct = cert_sct2;
state2->chlo_hash = chlo_hash2;
state2->server_config_sig = signature2;
state2->certs = certs2;
quic_server_info2->Persist();
QuicStreamFactoryPeer::MaybeInitialize(factory_.get());
EXPECT_TRUE(QuicStreamFactoryPeer::HasInitializedData(factory_.get()));
// Verify the MRU order is maintained.
const QuicServerInfoMap& quic_server_info_map =
http_server_properties_.quic_server_info_map();
EXPECT_EQ(2u, quic_server_info_map.size());
QuicServerInfoMap::const_iterator quic_server_info_map_it =
quic_server_info_map.begin();
EXPECT_EQ(quic_server_info_map_it->first, quic_server_id2);
++quic_server_info_map_it;
EXPECT_EQ(quic_server_info_map_it->first, quic_server_id);
EXPECT_TRUE(QuicStreamFactoryPeer::SupportsQuicAtStartUp(factory_.get(),
host_port_pair_));
EXPECT_FALSE(QuicStreamFactoryPeer::CryptoConfigCacheIsEmpty(
factory_.get(), quic_server_id));
QuicCryptoClientConfig* crypto_config =
QuicStreamFactoryPeer::GetCryptoConfig(factory_.get());
QuicCryptoClientConfig::CachedState* cached =
crypto_config->LookupOrCreate(quic_server_id);
EXPECT_FALSE(cached->server_config().empty());
EXPECT_TRUE(cached->GetServerConfig());
EXPECT_EQ(server_config, cached->server_config());
EXPECT_EQ(source_address_token, cached->source_address_token());
EXPECT_EQ(cert_sct, cached->cert_sct());
EXPECT_EQ(chlo_hash, cached->chlo_hash());
EXPECT_EQ(signature, cached->signature());
ASSERT_EQ(1U, cached->certs().size());
EXPECT_EQ(test_cert, cached->certs()[0]);
EXPECT_TRUE(QuicStreamFactoryPeer::SupportsQuicAtStartUp(factory_.get(),
host_port_pair2));
EXPECT_FALSE(QuicStreamFactoryPeer::CryptoConfigCacheIsEmpty(
factory_.get(), quic_server_id2));
QuicCryptoClientConfig::CachedState* cached2 =
crypto_config->LookupOrCreate(quic_server_id2);
EXPECT_FALSE(cached2->server_config().empty());
EXPECT_TRUE(cached2->GetServerConfig());
EXPECT_EQ(server_config2, cached2->server_config());
EXPECT_EQ(source_address_token2, cached2->source_address_token());
EXPECT_EQ(cert_sct2, cached2->cert_sct());
EXPECT_EQ(chlo_hash2, cached2->chlo_hash());
EXPECT_EQ(signature2, cached2->signature());
ASSERT_EQ(1U, cached->certs().size());
EXPECT_EQ(test_cert2, cached2->certs()[0]);
}
void RunTestLoopUntilIdle() {
while (!runner_->GetPostedTasks().empty())
runner_->RunNextTask();
}
// Helper methods for tests of connection migration on write error.
void TestMigrationOnWriteErrorNonMigratableStream(IoMode write_error_mode);
void TestMigrationOnWriteErrorMigrationDisabled(IoMode write_error_mode);
void TestMigrationOnWriteError(IoMode write_error_mode);
void TestMigrationOnWriteErrorNoNewNetwork(IoMode write_error_mode);
void TestMigrationOnMultipleWriteErrors(IoMode first_write_error_mode,
IoMode second_write_error_mode);
void TestMigrationOnWriteErrorWithNotificationQueued(bool disconnected);
void TestMigrationOnNotificationWithWriteErrorQueued(bool disconnected);
void OnNetworkDisconnected(bool async_write_before);
void OnNetworkMadeDefault(bool async_write_before);
void TestMigrationOnWriteErrorPauseBeforeConnected(IoMode write_error_mode);
void OnNetworkDisconnectedWithNetworkList(
NetworkChangeNotifier::NetworkList network_list);
void TestMigrationOnWriteErrorWithNetworkAddedBeforeNotification(
IoMode write_error_mode,
bool disconnected);
MockHostResolver host_resolver_;
scoped_refptr<SSLConfigService> ssl_config_service_;
MockClientSocketFactory socket_factory_;
MockCryptoClientStreamFactory crypto_client_stream_factory_;
MockRandom random_generator_;
MockClock* clock_; // Owned by |factory_| once created.
scoped_refptr<TestTaskRunner> runner_;
QuicVersion version_;
QuicTestPacketMaker client_maker_;
QuicTestPacketMaker server_maker_;
HttpServerPropertiesImpl http_server_properties_;
TestProxyDelegate test_proxy_delegate_;
std::unique_ptr<CertVerifier> cert_verifier_;
std::unique_ptr<ChannelIDService> channel_id_service_;
TransportSecurityState transport_security_state_;
std::unique_ptr<CTVerifier> cert_transparency_verifier_;
CTPolicyEnforcer ct_policy_enforcer_;
std::unique_ptr<ScopedMockNetworkChangeNotifier>
scoped_mock_network_change_notifier_;
std::unique_ptr<QuicStreamFactory> factory_;
HostPortPair host_port_pair_;
GURL url_;
GURL url2_;
GURL url3_;
GURL url4_;
PrivacyMode privacy_mode_;
NetLogWithSource net_log_;
TestCompletionCallback callback_;
// Variables to configure QuicStreamFactory.
bool always_require_handshake_confirmation_;
bool disable_connection_pooling_;
double load_server_info_timeout_srtt_multiplier_;
bool enable_connection_racing_;
bool enable_non_blocking_io_;
bool disable_disk_cache_;
bool prefer_aes_;
int receive_buffer_size_;
bool delay_tcp_race_;
bool close_sessions_on_ip_change_;
bool disable_quic_on_timeout_with_open_streams_;
int idle_connection_timeout_seconds_;
int reduced_ping_timeout_seconds_;
int packet_reader_yield_after_duration_milliseconds_;
bool migrate_sessions_on_network_change_;
bool migrate_sessions_early_;
bool allow_server_migration_;
bool force_hol_blocking_;
bool race_cert_verification_;
};
class QuicStreamFactoryTest : public QuicStreamFactoryTestBase,
public ::testing::TestWithParam<TestParams> {
protected:
QuicStreamFactoryTest()
: QuicStreamFactoryTestBase(GetParam().version,
GetParam().enable_connection_racing) {}
};
INSTANTIATE_TEST_CASE_P(Version,
QuicStreamFactoryTest,
::testing::ValuesIn(GetTestParams()));
TEST_P(QuicStreamFactoryTest, Create) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Will reset stream 3.
stream = CreateFromSession(host_port_pair_);
EXPECT_TRUE(stream.get());
// TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
// in streams on different sessions.
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
stream = request2.CreateStream(); // Will reset stream 5.
stream.reset(); // Will reset stream 7.
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddSocketDataToFactory(&socket_factory_);
crypto_client_stream_factory_.set_handshake_mode(
MockCryptoClientStream::ZERO_RTT);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
"192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddSocketDataToFactory(&socket_factory_);
crypto_client_stream_factory_.set_handshake_mode(
MockCryptoClientStream::ZERO_RTT);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
"192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "POST", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, GoAway) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
session->OnGoAway(QuicGoAwayFrame());
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, GoAwayForConnectionMigrationWithPortOnly) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
session->OnGoAway(
QuicGoAwayFrame(QUIC_ERROR_MIGRATING_PORT, 0,
"peer connection migration due to port change only"));
NetErrorDetails details;
EXPECT_FALSE(details.quic_port_migration_detected);
session->PopulateNetErrorDetails(&details);
EXPECT_TRUE(details.quic_port_migration_detected);
details.quic_port_migration_detected = false;
stream->PopulateNetErrorDetails(&details);
EXPECT_TRUE(details.quic_port_migration_detected);
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, Pooling) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
HostPortPair server2(kServer2HostName, kDefaultServerPort);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
"192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_EQ(GetActiveSession(host_port_pair_), GetActiveSession(server2));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, PoolingWithServerMigration) {
// Set up session to migrate.
host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
"192.168.0.1", "");
IPEndPoint alt_address = IPEndPoint(IPAddress(1, 2, 3, 4), 443);
QuicConfig config;
config.SetAlternateServerAddressToSend(
QuicSocketAddress(QuicSocketAddressImpl(alt_address)));
VerifyServerMigration(config, alt_address);
// Close server-migrated session.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
session->CloseSessionOnError(0u, QUIC_NO_ERROR);
// Set up server IP, socket, proof, and config for new session.
HostPortPair server2(kServer2HostName, kDefaultServerPort);
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0)};
std::unique_ptr<QuicEncryptedPacket> settings_packet(
client_maker_.MakeSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, true,
nullptr));
MockWrite writes[] = {
MockWrite(ASYNC, settings_packet->data(), settings_packet->length(), 1)};
SequencedSocketData socket_data(reads, arraysize(reads), writes,
arraysize(writes));
socket_factory_.AddSocketDataProvider(&socket_data);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
QuicConfig config2;
crypto_client_stream_factory_.SetConfig(config2);
// Create new request to cause new session creation.
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET", net_log_,
callback.callback()));
EXPECT_EQ(OK, callback.WaitForResult());
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
// EXPECT_EQ(GetActiveSession(host_port_pair_), GetActiveSession(server2));
}
TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
disable_connection_pooling_ = true;
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data1.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
HostPortPair server2(kServer2HostName, kDefaultServerPort);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
"192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_NE(GetActiveSession(host_port_pair_), GetActiveSession(server2));
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data1.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
HostPortPair server2(kServer2HostName, kDefaultServerPort);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
"192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
factory_->OnSessionGoingAway(GetActiveSession(host_port_pair_));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_FALSE(HasActiveSession(server2));
TestCompletionCallback callback3;
QuicStreamRequest request3(factory_.get());
EXPECT_EQ(OK, request3.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback3.callback()));
std::unique_ptr<QuicHttpStream> stream3 = request3.CreateStream();
EXPECT_TRUE(stream3.get());
EXPECT_TRUE(HasActiveSession(server2));
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, HttpsPooling) {
Initialize();
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
HostPortPair server1(kDefaultServerHostName, 443);
HostPortPair server2(kServer2HostName, 443);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(server1, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback_.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_EQ(GetActiveSession(server1), GetActiveSession(server2));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
disable_connection_pooling_ = true;
Initialize();
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data1.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
HostPortPair server1(kDefaultServerHostName, 443);
HostPortPair server2(kServer2HostName, 443);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(server1, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback_.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_NE(GetActiveSession(server1), GetActiveSession(server2));
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
Initialize();
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
HostPortPair server1(kDefaultServerHostName, 443);
HostPortPair server2(kServer2HostName, 443);
uint8_t primary_pin = 1;
uint8_t backup_pin = 2;
test::AddPin(&transport_security_state_, kServer2HostName, primary_pin,
backup_pin);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
verify_details.cert_verify_result.public_key_hashes.push_back(
test::GetTestHashValue(primary_pin));
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(server1, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback_.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_EQ(GetActiveSession(server1), GetActiveSession(server2));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
disable_connection_pooling_ = true;
Initialize();
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data1.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
HostPortPair server1(kDefaultServerHostName, 443);
HostPortPair server2(kServer2HostName, 443);
uint8_t primary_pin = 1;
uint8_t backup_pin = 2;
test::AddPin(&transport_security_state_, kServer2HostName, primary_pin,
backup_pin);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
verify_details.cert_verify_result.public_key_hashes.push_back(
test::GetTestHashValue(primary_pin));
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(server1, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback_.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_NE(GetActiveSession(server1), GetActiveSession(server2));
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
Initialize();
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data1.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
HostPortPair server1(kDefaultServerHostName, 443);
HostPortPair server2(kServer2HostName, 443);
uint8_t primary_pin = 1;
uint8_t backup_pin = 2;
uint8_t bad_pin = 3;
test::AddPin(&transport_security_state_, kServer2HostName, primary_pin,
backup_pin);
ProofVerifyDetailsChromium verify_details1 = DefaultProofVerifyDetails();
verify_details1.cert_verify_result.public_key_hashes.push_back(
test::GetTestHashValue(bad_pin));
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details1);
ProofVerifyDetailsChromium verify_details2 = DefaultProofVerifyDetails();
verify_details2.cert_verify_result.public_key_hashes.push_back(
test::GetTestHashValue(primary_pin));
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details2);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(server1, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
TestCompletionCallback callback;
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback_.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_NE(GetActiveSession(server1), GetActiveSession(server2));
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, Goaway) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Mark the session as going away. Ensure that while it is still alive
// that it is no longer active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
factory_->OnSessionGoingAway(session);
EXPECT_EQ(true,
QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
// Create a new request for the same destination and verify that a
// new session is created.
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_TRUE(HasActiveSession(host_port_pair_));
EXPECT_NE(session, GetActiveSession(host_port_pair_));
EXPECT_EQ(true,
QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
stream2.reset();
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
QuicStreamId stream_id = kClientDataStreamId1;
MockQuicData socket_data;
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(
client_maker_.MakeRstPacket(2, true, stream_id, QUIC_STREAM_CANCELLED));
socket_data.AddRead(
server_maker_.MakeRstPacket(1, false, stream_id, QUIC_STREAM_CANCELLED));
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddSocketDataToFactory(&socket_factory_);
HttpRequestInfo request_info;
std::vector<std::unique_ptr<QuicHttpStream>> streams;
// The MockCryptoClientStream sets max_open_streams to be
// kDefaultMaxStreamsPerConnection / 2.
for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
QuicStreamRequest request(factory_.get());
int rv = request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback());
if (i == 0) {
EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
} else {
EXPECT_THAT(rv, IsOk());
}
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream);
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
streams.push_back(std::move(stream));
}
QuicStreamRequest request(factory_.get());
EXPECT_EQ(OK, request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
CompletionCallback()));
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream);
EXPECT_EQ(ERR_IO_PENDING,
stream->InitializeStream(&request_info, DEFAULT_PRIORITY, net_log_,
callback_.callback()));
// Close the first stream.
streams.front()->Close(false);
// Trigger exchange of RSTs that in turn allow progress for the last
// stream.
EXPECT_THAT(callback_.WaitForResult(), IsOk());
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
// Force close of the connection to suppress the generation of RST
// packets when streams are torn down, which wouldn't be relevant to
// this test anyway.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
session->connection()->CloseConnection(QUIC_PUBLIC_RESET, "test",
ConnectionCloseBehavior::SILENT_CLOSE);
}
TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
Initialize();
MockQuicData socket_data;
socket_data.AddSocketDataToFactory(&socket_factory_);
host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
Initialize();
MockQuicData socket_data;
socket_data.AddConnect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
socket_data.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsError(ERR_ADDRESS_IN_USE));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, CancelCreate) {
Initialize();
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
{
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
}
base::RunLoop().RunUntilIdle();
std::unique_ptr<QuicHttpStream> stream(CreateFromSession(host_port_pair_));
EXPECT_TRUE(stream.get());
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(ConstructClientRstPacket(2));
socket_data.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
HttpRequestInfo request_info;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Close the session and verify that stream saw the error.
factory_->CloseAllSessions(ERR_INTERNET_DISCONNECTED, QUIC_INTERNAL_ERROR);
EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
stream->ReadResponseHeaders(callback_.callback()));
// Now attempting to request a stream to the same origin should create
// a new session.
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
stream = request2.CreateStream();
stream.reset(); // Will reset stream 3.
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
close_sessions_on_ip_change_ = true;
Initialize();
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(ConstructClientRstPacket(2));
socket_data.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
HttpRequestInfo request_info;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Change the IP address and verify that stream saw the error.
NotifyIPAddressChanged();
EXPECT_EQ(ERR_NETWORK_CHANGED,
stream->ReadResponseHeaders(callback_.callback()));
EXPECT_TRUE(factory_->require_confirmation());
// Now attempting to request a stream to the same origin should create
// a new session.
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
stream = request2.CreateStream();
stream.reset(); // Will reset stream 3.
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkMadeDefaultWithSynchronousWriteBefore) {
OnNetworkMadeDefault(/*async_write_before=*/false);
}
TEST_P(QuicStreamFactoryTest, OnNetworkMadeDefaultWithAsyncWriteBefore) {
OnNetworkMadeDefault(/*async_write_before=*/true);
}
void QuicStreamFactoryTestBase::OnNetworkMadeDefault(bool async_write_before) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
int packet_number = 1;
MockQuicData socket_data;
QuicStreamOffset header_stream_offset = 0;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(ConstructSettingsPacket(
packet_number++, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, &header_stream_offset));
socket_data.AddWrite(ConstructGetRequestPacket(packet_number++,
kClientDataStreamId1, true,
true, &header_stream_offset));
if (async_write_before) {
socket_data.AddWrite(ASYNC, OK);
packet_number++;
}
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
request_info.method = "GET";
request_info.url = url_;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Send GET request on stream.
HttpResponseInfo response;
HttpRequestHeaders request_headers;
EXPECT_EQ(OK, stream->SendRequest(request_headers, &response,
callback_.callback()));
// Do an async write to leave writer blocked.
if (async_write_before)
session->connection()->SendPing();
// Set up second socket data provider that is used after migration.
// The response to the earlier request is read on this new socket.
MockQuicData socket_data1;
socket_data1.AddWrite(
client_maker_.MakePingPacket(packet_number++, /*include_version=*/true));
socket_data1.AddRead(
ConstructOkResponsePacket(1, kClientDataStreamId1, false, false));
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(client_maker_.MakeAckAndRstPacket(
packet_number++, false, kClientDataStreamId1, QUIC_STREAM_CANCELLED, 1, 1,
1, true));
socket_data1.AddSocketDataToFactory(&socket_factory_);
// Trigger connection migration. This should cause a PING frame
// to be emitted.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkMadeDefault(kNewNetworkForTests);
// The session should now be marked as going away. Ensure that
// while it is still alive, it is no longer active.
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
// Verify that response headers on the migrated socket were delivered to the
// stream.
EXPECT_THAT(stream->ReadResponseHeaders(callback_.callback()), IsOk());
EXPECT_EQ(200, response.headers->response_code());
// Create a new request for the same destination and verify that a
// new session is created.
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_TRUE(HasActiveSession(host_port_pair_));
QuicChromiumClientSession* new_session = GetActiveSession(host_port_pair_);
EXPECT_NE(session, new_session);
// On a DISCONNECTED notification, nothing happens to the migrated
// session, but the new session is closed since it has no open
// streams.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_EQ(1u, session->GetNumActiveStreams());
EXPECT_FALSE(
QuicStreamFactoryPeer::IsLiveSession(factory_.get(), new_session));
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkDisconnectedWithSynchronousWriteBefore) {
OnNetworkDisconnected(/*async_write_before=*/false);
}
TEST_P(QuicStreamFactoryTest, OnNetworkDisconnectedWithAsyncWriteBefore) {
OnNetworkDisconnected(/*async_write_before=*/true);
}
void QuicStreamFactoryTestBase::OnNetworkDisconnected(bool async_write_before) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
int packet_number = 1;
MockQuicData socket_data;
QuicStreamOffset header_stream_offset = 0;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(ConstructSettingsPacket(
packet_number++, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, &header_stream_offset));
socket_data.AddWrite(ConstructGetRequestPacket(packet_number++,
kClientDataStreamId1, true,
true, &header_stream_offset));
if (async_write_before) {
socket_data.AddWrite(ASYNC, OK);
packet_number++;
}
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
request_info.method = "GET";
request_info.url = url_;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Send GET request on stream.
HttpResponseInfo response_info;
HttpRequestHeaders request_headers;
EXPECT_EQ(OK, stream->SendRequest(request_headers, &response_info,
callback_.callback()));
// Do an async write to leave writer blocked.
if (async_write_before)
session->connection()->SendPing();
// Set up second socket data provider that is used after migration.
// The response to the earlier request is read on this new socket.
MockQuicData socket_data1;
socket_data1.AddWrite(
client_maker_.MakePingPacket(packet_number++, /*include_version=*/true));
socket_data1.AddRead(
ConstructOkResponsePacket(1, kClientDataStreamId1, false, false));
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(client_maker_.MakeAckAndRstPacket(
packet_number++, false, kClientDataStreamId1, QUIC_STREAM_CANCELLED, 1, 1,
1, true));
socket_data1.AddSocketDataToFactory(&socket_factory_);
// Trigger connection migration. This should cause a PING frame
// to be emitted.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
// The session should now be marked as going away. Ensure that
// while it is still alive, it is no longer active.
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
// Create a new request for the same destination and verify that a
// new session is created.
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_TRUE(HasActiveSession(host_port_pair_));
EXPECT_NE(session, GetActiveSession(host_port_pair_));
EXPECT_EQ(true,
QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkDisconnectedNoNetworks) {
NetworkChangeNotifier::NetworkList no_networks(0);
OnNetworkDisconnectedWithNetworkList(no_networks);
}
TEST_P(QuicStreamFactoryTest, OnNetworkDisconnectedNoNewNetwork) {
OnNetworkDisconnectedWithNetworkList({kDefaultNetworkForTests});
}
void QuicStreamFactoryTestBase::OnNetworkDisconnectedWithNetworkList(
NetworkChangeNotifier::NetworkList network_list) {
InitializeConnectionMigrationTest(network_list);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
// Use the test task runner, to force the migration alarm timeout later.
QuicStreamFactoryPeer::SetTaskRunner(factory_.get(), runner_.get());
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Trigger connection migration. Since there are no networks
// to migrate to, this should cause the session to wait for a new network.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
// The migration will not fail until the migration alarm timeout.
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
EXPECT_EQ(ERR_IO_PENDING, stream->ReadResponseHeaders(callback_.callback()));
EXPECT_EQ(true, session->connection()->writer()->IsWriteBlocked());
// Force the migration alarm timeout to run.
RunTestLoopUntilIdle();
// The connection should now be closed. A request for response
// headers should fail.
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(ERR_NETWORK_CHANGED, callback_.WaitForResult());
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkMadeDefaultNonMigratableStream) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(client_maker_.MakeRstPacket(
2, true, kClientDataStreamId1, QUIC_STREAM_CANCELLED));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created, but marked as non-migratable.
HttpRequestInfo request_info;
request_info.load_flags |= LOAD_DISABLE_CONNECTION_MIGRATION;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Trigger connection migration. Since there is a non-migratable stream,
// this should cause session to continue but be marked as going away.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkMadeDefault(kNewNetworkForTests);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkMadeDefaultConnectionMigrationDisabled) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(client_maker_.MakeRstPacket(
2, true, kClientDataStreamId1, QUIC_STREAM_CANCELLED));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Set session config to have connection migration disabled.
QuicConfigPeer::SetReceivedDisableConnectionMigration(session->config());
EXPECT_TRUE(session->config()->DisableConnectionMigration());
// Trigger connection migration. Since there is a non-migratable stream,
// this should cause session to continue but be marked as going away.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkMadeDefault(kNewNetworkForTests);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkDisconnectedNonMigratableStream) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(client_maker_.MakeRstPacket(
2, true, kClientDataStreamId1, QUIC_RST_ACKNOWLEDGEMENT));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created, but marked as non-migratable.
HttpRequestInfo request_info;
request_info.load_flags |= LOAD_DISABLE_CONNECTION_MIGRATION;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Trigger connection migration. Since there is a non-migratable stream,
// this should cause a RST_STREAM frame to be emitted with
// QUIC_RST_ACKNOWLEDGEMENT error code, and the session will be closed.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest,
OnNetworkDisconnectedConnectionMigrationDisabled) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddWrite(client_maker_.MakeRstPacket(
2, true, kClientDataStreamId1, QUIC_RST_ACKNOWLEDGEMENT));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Set session config to have connection migration disabled.
QuicConfigPeer::SetReceivedDisableConnectionMigration(session->config());
EXPECT_TRUE(session->config()->DisableConnectionMigration());
// Trigger connection migration. Since there is a non-migratable stream,
// this should cause a RST_STREAM frame to be emitted with
// QUIC_RST_ACKNOWLEDGEMENT error code, and the session will be closed.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkMadeDefaultNoOpenStreams) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Trigger connection migration. Since there are no active streams,
// the session will be closed.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkMadeDefault(kNewNetworkForTests);
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkDisconnectedNoOpenStreams) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Trigger connection migration. Since there are no active streams,
// the session will be closed.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, OnNetworkChangeDisconnectedPauseBeforeConnected) {
InitializeConnectionMigrationTest({kDefaultNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
QuicStreamOffset header_stream_offset = 0;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(ConstructSettingsPacket(
1, SETTINGS_MAX_HEADER_LIST_SIZE, kDefaultMaxUncompressedHeaderSize,
&header_stream_offset));
socket_data.AddWrite(ConstructGetRequestPacket(2, kClientDataStreamId1, true,
true, &header_stream_offset));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream = request.CreateStream();
EXPECT_TRUE(stream.get());
// Cause QUIC stream to be created.
HttpRequestInfo request_info;
request_info.method = "GET";
request_info.url = url_;
EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
// Ensure that session is alive and active.
QuicChromiumClientSession* session = GetActiveSession(host_port_pair_);
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_TRUE(HasActiveSession(host_port_pair_));
// Send GET request on stream.
HttpResponseInfo response;
HttpRequestHeaders request_headers;
EXPECT_EQ(OK, stream->SendRequest(request_headers, &response,
callback_.callback()));
// Trigger connection migration. Since there are no networks
// to migrate to, this should cause the session to wait for a new network.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
// The connection should still be alive, but marked as going away.
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
EXPECT_EQ(ERR_IO_PENDING, stream->ReadResponseHeaders(callback_.callback()));
// Set up second socket data provider that is used after migration.
// The response to the earlier request is read on this new socket.
MockQuicData socket_data1;
socket_data1.AddWrite(
client_maker_.MakePingPacket(3, /*include_version=*/true));
socket_data1.AddRead(
ConstructOkResponsePacket(1, kClientDataStreamId1, false, false));
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(client_maker_.MakeAckAndRstPacket(
4, false, kClientDataStreamId1, QUIC_STREAM_CANCELLED, 1, 1, 1, true));
socket_data1.AddSocketDataToFactory(&socket_factory_);
// Add a new network and notify the stream factory of a new connected network.
// This causes a PING packet to be sent over the new network.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->SetConnectedNetworksList({kNewNetworkForTests});
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkConnected(kNewNetworkForTests);
// Ensure that the session is still alive.
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
EXPECT_FALSE(HasActiveSession(host_port_pair_));
EXPECT_EQ(1u, session->GetNumActiveStreams());
// Run the message loop so that data queued in the new socket is read by the
// packet reader.
base::RunLoop().RunUntilIdle();
// Response headers are received over the new network.
EXPECT_THAT(callback_.WaitForResult(), IsOk());
EXPECT_EQ(200, response.headers->response_code());
// Create a new request and verify that a new session is created.
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddSocketDataToFactory(&socket_factory_);
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request2.Request(host_port_pair_, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
EXPECT_THAT(callback_.WaitForResult(), IsOk());
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
EXPECT_TRUE(HasActiveSession(host_port_pair_));
EXPECT_NE(session, GetActiveSession(host_port_pair_));
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session));
stream.reset();
EXPECT_TRUE(socket_data.AllReadDataConsumed());
EXPECT_TRUE(socket_data.AllWriteDataConsumed());
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest,
OnNetworkChangeDisconnectedPauseBeforeConnectedMultipleSessions) {
InitializeConnectionMigrationTest({kDefaultNetworkForTests});
MockQuicData socket_data1;
socket_data1.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data1.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data1.AddWrite(ASYNC, OK);
socket_data1.AddSocketDataToFactory(&socket_factory_);
MockQuicData socket_data2;
socket_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data2.AddWrite(
ConstructSettingsPacket(1, SETTINGS_MAX_HEADER_LIST_SIZE,
kDefaultMaxUncompressedHeaderSize, nullptr));
socket_data2.AddWrite(ASYNC, OK);
socket_data2.AddSocketDataToFactory(&socket_factory_);
HostPortPair server1(kDefaultServerHostName, 443);
HostPortPair server2(kServer2HostName, 443);
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
host_resolver_.set_synchronous_mode(true);
host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.2", "");
// Create request and QuicHttpStream to create session1.
QuicStreamRequest request1(factory_.get());
EXPECT_EQ(OK, request1.Request(server1, privacy_mode_,
/*cert_verify_flags=*/0, url_, "GET", net_log_,
callback_.callback()));
std::unique_ptr<QuicHttpStream> stream1 = request1.CreateStream();
EXPECT_TRUE(stream1.get());
// Create request and QuicHttpStream to create session2.
QuicStreamRequest request2(factory_.get());
EXPECT_EQ(OK, request2.Request(server2, privacy_mode_,
/*cert_verify_flags=*/0, url2_, "GET",
net_log_, callback_.callback()));
std::unique_ptr<QuicHttpStream> stream2 = request2.CreateStream();
EXPECT_TRUE(stream2.get());
QuicChromiumClientSession* session1 = GetActiveSession(server1);
QuicChromiumClientSession* session2 = GetActiveSession(server2);
EXPECT_NE(session1, session2);
// Cause QUIC stream to be created and send GET so session1 has an open
// stream.
HttpRequestInfo request_info1;
request_info1.method = "GET";
request_info1.url = url_;
EXPECT_EQ(OK, stream1->InitializeStream(&request_info1, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
HttpResponseInfo response1;
HttpRequestHeaders request_headers1;
EXPECT_EQ(OK, stream1->SendRequest(request_headers1, &response1,
callback_.callback()));
// Cause QUIC stream to be created and send GET so session2 has an open
// stream.
HttpRequestInfo request_info2;
request_info2.method = "GET";
request_info2.url = url_;
EXPECT_EQ(OK, stream2->InitializeStream(&request_info2, DEFAULT_PRIORITY,
net_log_, CompletionCallback()));
HttpResponseInfo response2;
HttpRequestHeaders request_headers2;
EXPECT_EQ(OK, stream2->SendRequest(request_headers2, &response2,
callback_.callback()));
// Cause both sessions to be paused due to DISCONNECTED.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkDisconnected(kDefaultNetworkForTests);
// Ensure that both sessions are paused but alive.
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session1));
EXPECT_TRUE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session2));
// Add new sockets to use post migration.
MockConnect connect_result =
MockConnect(SYNCHRONOUS, ERR_INTERNET_DISCONNECTED);
SequencedSocketData socket_data3(connect_result, nullptr, 0, nullptr, 0);
socket_factory_.AddSocketDataProvider(&socket_data3);
SequencedSocketData socket_data4(connect_result, nullptr, 0, nullptr, 0);
socket_factory_.AddSocketDataProvider(&socket_data4);
// Add a new network and cause migration to bad sockets, causing sessions to
// close.
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->SetConnectedNetworksList({kNewNetworkForTests});
scoped_mock_network_change_notifier_->mock_network_change_notifier()
->NotifyNetworkConnected(kNewNetworkForTests);
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session1));
EXPECT_FALSE(QuicStreamFactoryPeer::IsLiveSession(factory_.get(), session2));
EXPECT_TRUE(socket_data1.AllReadDataConsumed());
EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
EXPECT_TRUE(socket_data2.AllReadDataConsumed());
EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
}
TEST_P(QuicStreamFactoryTest, MigrateSessionEarly) {
InitializeConnectionMigrationTest(
{kDefaultNetworkForTests, kNewNetworkForTests});
ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
MockQuicData socket_data;
QuicStreamOffset header_stream_offset = 0;
socket_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING);
socket_data.AddWrite(ConstructSettingsPacket(
1, SETTINGS_MAX_HEADER_LIST_SIZE, kDefaultMaxUncompressedHeaderSize,
&header_stream_offset));
socket_data.AddWrite(ConstructGetRequestPacket(2, kClientDataStreamId1, true,
true, &header_stream_offset));
socket_data.AddSocketDataToFactory(&socket_factory_);
// Create request and QuicHttpStream.
QuicStreamRequest request(factory_.get());
EXPECT_EQ(ERR_IO_PENDING,
request.Request