| // 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 <algorithm> |
| #include <ostream> |
| #include <string> |
| #include <utility> |
| #include <vector> |
| |
| #include "base/bind.h" |
| #include "base/compiler_specific.h" |
| #include "base/macros.h" |
| #include "base/run_loop.h" |
| #include "base/stl_util.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_piece.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/test/metrics/histogram_tester.h" |
| #include "base/test/scoped_feature_list.h" |
| #include "net/base/chunked_upload_data_stream.h" |
| #include "net/base/completion_once_callback.h" |
| #include "net/base/features.h" |
| #include "net/base/ip_endpoint.h" |
| #include "net/base/mock_network_change_notifier.h" |
| #include "net/base/network_isolation_key.h" |
| #include "net/base/test_completion_callback.h" |
| #include "net/base/test_proxy_delegate.h" |
| #include "net/cert/ct_policy_enforcer.h" |
| #include "net/cert/mock_cert_verifier.h" |
| #include "net/cert/multi_log_ct_verifier.h" |
| #include "net/dns/mock_host_resolver.h" |
| #include "net/http/http_auth_handler_factory.h" |
| #include "net/http/http_network_session.h" |
| #include "net/http/http_network_transaction.h" |
| #include "net/http/http_proxy_connect_job.h" |
| #include "net/http/http_server_properties.h" |
| #include "net/http/http_stream.h" |
| #include "net/http/http_stream_factory.h" |
| #include "net/http/http_transaction_test_util.h" |
| #include "net/http/test_upload_data_stream_not_allow_http1.h" |
| #include "net/http/transport_security_state.h" |
| #include "net/log/net_log_event_type.h" |
| #include "net/log/test_net_log.h" |
| #include "net/log/test_net_log_util.h" |
| #include "net/proxy_resolution/configured_proxy_resolution_service.h" |
| #include "net/proxy_resolution/proxy_config_service_fixed.h" |
| #include "net/proxy_resolution/proxy_resolver.h" |
| #include "net/quic/crypto/proof_verifier_chromium.h" |
| #include "net/quic/mock_crypto_client_stream_factory.h" |
| #include "net/quic/mock_quic_context.h" |
| #include "net/quic/mock_quic_data.h" |
| #include "net/quic/quic_chromium_alarm_factory.h" |
| #include "net/quic/quic_http_stream.h" |
| #include "net/quic/quic_http_utils.h" |
| #include "net/quic/quic_stream_factory_peer.h" |
| #include "net/quic/quic_test_packet_maker.h" |
| #include "net/quic/test_task_runner.h" |
| #include "net/socket/client_socket_factory.h" |
| #include "net/socket/mock_client_socket_pool_manager.h" |
| #include "net/socket/next_proto.h" |
| #include "net/socket/socket_performance_watcher.h" |
| #include "net/socket/socket_performance_watcher_factory.h" |
| #include "net/socket/socket_test_util.h" |
| #include "net/spdy/spdy_test_util_common.h" |
| #include "net/ssl/ssl_config_service_defaults.h" |
| #include "net/test/cert_test_util.h" |
| #include "net/test/gtest_util.h" |
| #include "net/test/test_data_directory.h" |
| #include "net/test/test_with_task_environment.h" |
| #include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h" |
| #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
| #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| #include "net/third_party/quiche/src/quic/core/quic_framer.h" |
| #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| #include "net/third_party/quiche/src/quic/test_tools/mock_random.h" |
| #include "net/third_party/quiche/src/quic/test_tools/quic_spdy_session_peer.h" |
| #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| #include "net/third_party/quiche/src/spdy/core/spdy_frame_builder.h" |
| #include "net/third_party/quiche/src/spdy/core/spdy_framer.h" |
| #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| #include "net/url_request/static_http_user_agent_settings.h" |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_job_factory_impl.h" |
| #include "net/url_request/url_request_test_util.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "testing/platform_test.h" |
| #include "url/gurl.h" |
| #include "url/origin.h" |
| |
| using ::testing::ElementsAre; |
| using ::testing::Key; |
| |
| namespace net { |
| 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. |
| }; |
| |
| static const char kQuicAlternativeServiceHeader[] = |
| "Alt-Svc: quic=\":443\"\r\n\r\n"; |
| static const char kQuicAlternativeServiceWithProbabilityHeader[] = |
| "Alt-Svc: quic=\":443\";p=\".5\"\r\n\r\n"; |
| static const char kQuicAlternativeServiceDifferentPortHeader[] = |
| "Alt-Svc: quic=\":137\"\r\n\r\n"; |
| |
| const char kDefaultServerHostName[] = "mail.example.org"; |
| const char kDifferentHostname[] = "different.example.com"; |
| |
| struct TestParams { |
| quic::ParsedQuicVersion version; |
| bool client_headers_include_h2_stream_dependency; |
| }; |
| |
| // Used by ::testing::PrintToStringParamName(). |
| std::string PrintToString(const TestParams& p) { |
| return quiche::QuicheStrCat( |
| ParsedQuicVersionToString(p.version), "_", |
| (p.client_headers_include_h2_stream_dependency ? "" : "No"), |
| "Dependency"); |
| } |
| |
| // Run QuicNetworkTransactionWithDestinationTest instances with all value |
| // combinations of version and destination_type. |
| struct PoolingTestParams { |
| quic::ParsedQuicVersion version; |
| DestinationType destination_type; |
| bool client_headers_include_h2_stream_dependency; |
| }; |
| |
| // Used by ::testing::PrintToStringParamName(). |
| std::string PrintToString(const PoolingTestParams& p) { |
| const char* destination_string = ""; |
| switch (p.destination_type) { |
| case SAME_AS_FIRST: |
| destination_string = "SAME_AS_FIRST"; |
| break; |
| case SAME_AS_SECOND: |
| destination_string = "SAME_AS_SECOND"; |
| break; |
| case DIFFERENT: |
| destination_string = "DIFFERENT"; |
| break; |
| } |
| return quiche::QuicheStrCat( |
| ParsedQuicVersionToString(p.version), "_", destination_string, "_", |
| (p.client_headers_include_h2_stream_dependency ? "" : "No"), |
| "Dependency"); |
| } |
| |
| std::string GenerateQuicAltSvcHeader( |
| const quic::ParsedQuicVersionVector& versions) { |
| std::string altsvc_header = "Alt-Svc: "; |
| std::string version_string; |
| bool first_version = true; |
| for (const auto& version : versions) { |
| if (first_version) { |
| first_version = false; |
| } else { |
| altsvc_header.append(", "); |
| } |
| altsvc_header.append(quic::AlpnForVersion(version)); |
| altsvc_header.append("=\":443\""); |
| if (version.SupportsGoogleAltSvcFormat()) { |
| if (!version_string.empty()) { |
| version_string.append(","); |
| } |
| version_string.append(base::NumberToString(version.transport_version)); |
| } |
| } |
| if (!version_string.empty()) { |
| altsvc_header.append(", quic=\":443\"; v=\"" + version_string + "\""); |
| } |
| altsvc_header.append("\r\n"); |
| |
| return altsvc_header; |
| } |
| |
| std::vector<TestParams> GetTestParams() { |
| std::vector<TestParams> params; |
| quic::ParsedQuicVersionVector all_supported_versions = |
| quic::AllSupportedVersions(); |
| for (const quic::ParsedQuicVersion& version : all_supported_versions) { |
| params.push_back(TestParams{version, false}); |
| params.push_back(TestParams{version, true}); |
| } |
| return params; |
| } |
| |
| std::vector<PoolingTestParams> GetPoolingTestParams() { |
| std::vector<PoolingTestParams> params; |
| quic::ParsedQuicVersionVector all_supported_versions = |
| quic::AllSupportedVersions(); |
| for (const quic::ParsedQuicVersion& version : all_supported_versions) { |
| params.push_back(PoolingTestParams{version, SAME_AS_FIRST, false}); |
| params.push_back(PoolingTestParams{version, SAME_AS_FIRST, true}); |
| params.push_back(PoolingTestParams{version, SAME_AS_SECOND, false}); |
| params.push_back(PoolingTestParams{version, SAME_AS_SECOND, true}); |
| params.push_back(PoolingTestParams{version, DIFFERENT, false}); |
| params.push_back(PoolingTestParams{version, DIFFERENT, true}); |
| } |
| return params; |
| } |
| |
| } // namespace |
| |
| class TestSocketPerformanceWatcher : public SocketPerformanceWatcher { |
| public: |
| TestSocketPerformanceWatcher(bool* should_notify_updated_rtt, |
| bool* rtt_notification_received) |
| : should_notify_updated_rtt_(should_notify_updated_rtt), |
| rtt_notification_received_(rtt_notification_received) {} |
| ~TestSocketPerformanceWatcher() override {} |
| |
| bool ShouldNotifyUpdatedRTT() const override { |
| return *should_notify_updated_rtt_; |
| } |
| |
| void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override { |
| *rtt_notification_received_ = true; |
| } |
| |
| void OnConnectionChanged() override {} |
| |
| private: |
| bool* should_notify_updated_rtt_; |
| bool* rtt_notification_received_; |
| |
| DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcher); |
| }; |
| |
| class TestSocketPerformanceWatcherFactory |
| : public SocketPerformanceWatcherFactory { |
| public: |
| TestSocketPerformanceWatcherFactory() |
| : watcher_count_(0u), |
| should_notify_updated_rtt_(true), |
| rtt_notification_received_(false) {} |
| ~TestSocketPerformanceWatcherFactory() override {} |
| |
| // SocketPerformanceWatcherFactory implementation: |
| std::unique_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( |
| const Protocol protocol, |
| const AddressList& /* address_list */) override { |
| if (protocol != PROTOCOL_QUIC) { |
| return nullptr; |
| } |
| ++watcher_count_; |
| return std::unique_ptr<SocketPerformanceWatcher>( |
| new TestSocketPerformanceWatcher(&should_notify_updated_rtt_, |
| &rtt_notification_received_)); |
| } |
| |
| size_t watcher_count() const { return watcher_count_; } |
| |
| bool rtt_notification_received() const { return rtt_notification_received_; } |
| |
| void set_should_notify_updated_rtt(bool should_notify_updated_rtt) { |
| should_notify_updated_rtt_ = should_notify_updated_rtt; |
| } |
| |
| private: |
| size_t watcher_count_; |
| bool should_notify_updated_rtt_; |
| bool rtt_notification_received_; |
| |
| DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcherFactory); |
| }; |
| |
| class QuicNetworkTransactionTest |
| : public PlatformTest, |
| public ::testing::WithParamInterface<TestParams>, |
| public WithTaskEnvironment { |
| protected: |
| QuicNetworkTransactionTest() |
| : version_(GetParam().version), |
| client_headers_include_h2_stream_dependency_( |
| GetParam().client_headers_include_h2_stream_dependency), |
| supported_versions_(quic::test::SupportedVersions(version_)), |
| client_maker_(new QuicTestPacketMaker( |
| version_, |
| quic::QuicUtils::CreateRandomConnectionId( |
| context_.random_generator()), |
| context_.clock(), |
| kDefaultServerHostName, |
| quic::Perspective::IS_CLIENT, |
| client_headers_include_h2_stream_dependency_)), |
| server_maker_(version_, |
| quic::QuicUtils::CreateRandomConnectionId( |
| context_.random_generator()), |
| context_.clock(), |
| kDefaultServerHostName, |
| quic::Perspective::IS_SERVER, |
| false), |
| quic_task_runner_(new TestTaskRunner(context_.mock_clock())), |
| cert_transparency_verifier_(new MultiLogCTVerifier()), |
| ssl_config_service_(new SSLConfigServiceDefaults), |
| proxy_resolution_service_( |
| ConfiguredProxyResolutionService::CreateDirect()), |
| auth_handler_factory_(HttpAuthHandlerFactory::CreateDefault()), |
| http_server_properties_(std::make_unique<HttpServerProperties>()), |
| ssl_data_(ASYNC, OK) { |
| FLAGS_quic_enable_http3_grease_randomness = false; |
| request_.method = "GET"; |
| std::string url("https://"); |
| url.append(kDefaultServerHostName); |
| request_.url = GURL(url); |
| request_.load_flags = 0; |
| request_.traffic_annotation = |
| net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| context_.AdvanceTime(quic::QuicTime::Delta::FromMilliseconds(20)); |
| |
| scoped_refptr<X509Certificate> cert( |
| ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem")); |
| verify_details_.cert_verify_result.verified_cert = cert; |
| verify_details_.cert_verify_result.is_issued_by_known_root = true; |
| crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details_); |
| } |
| |
| void SetUp() override { |
| NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| base::RunLoop().RunUntilIdle(); |
| } |
| |
| void TearDown() override { |
| NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| // Empty the current queue. |
| base::RunLoop().RunUntilIdle(); |
| PlatformTest::TearDown(); |
| NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| base::RunLoop().RunUntilIdle(); |
| session_.reset(); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientConnectionClosePacket(uint64_t num) { |
| return client_maker_->MakeConnectionClosePacket( |
| num, false, quic::QUIC_CRYPTO_VERSION_NOT_SUPPORTED, "Time to panic!"); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructServerConnectionClosePacket(uint64_t num) { |
| return server_maker_.MakeConnectionClosePacket( |
| num, false, quic::QUIC_CRYPTO_VERSION_NOT_SUPPORTED, "Time to panic!"); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructServerGoAwayPacket( |
| uint64_t num, |
| quic::QuicErrorCode error_code, |
| std::string reason_phrase) { |
| return server_maker_.MakeGoAwayPacket(num, error_code, reason_phrase); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructClientAckPacket( |
| uint64_t packet_number, |
| uint64_t largest_received, |
| uint64_t smallest_received) { |
| return client_maker_->MakeAckPacket(packet_number, largest_received, |
| smallest_received); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructClientAckAndRstPacket( |
| uint64_t num, |
| quic::QuicStreamId stream_id, |
| quic::QuicRstStreamErrorCode error_code, |
| uint64_t largest_received, |
| uint64_t smallest_received) { |
| return client_maker_->MakeAckAndRstPacket( |
| num, false, stream_id, error_code, largest_received, smallest_received); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructClientRstPacket( |
| uint64_t num, |
| quic::QuicStreamId stream_id, |
| quic::QuicRstStreamErrorCode error_code) { |
| return client_maker_->MakeRstPacket(num, false, stream_id, error_code, |
| /*include_stop_sending_if_v99=*/true); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientAckAndConnectionClosePacket( |
| uint64_t num, |
| uint64_t largest_received, |
| uint64_t smallest_received, |
| quic::QuicErrorCode quic_error, |
| const std::string& quic_error_details, |
| uint64_t frame_type) { |
| return client_maker_->MakeAckAndConnectionClosePacket( |
| num, false, largest_received, smallest_received, quic_error, |
| quic_error_details, frame_type); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructServerRstPacket( |
| uint64_t num, |
| bool include_version, |
| quic::QuicStreamId stream_id, |
| quic::QuicRstStreamErrorCode error_code) { |
| return server_maker_.MakeRstPacket(num, include_version, stream_id, |
| error_code); |
| } |
| |
| std::unique_ptr<quic::QuicReceivedPacket> ConstructInitialSettingsPacket( |
| uint64_t packet_number) { |
| return client_maker_->MakeInitialSettingsPacket(packet_number); |
| } |
| |
| std::unique_ptr<quic::QuicReceivedPacket> ConstructServerAckPacket( |
| uint64_t packet_number, |
| uint64_t largest_received, |
| uint64_t smallest_received) { |
| return server_maker_.MakeAckPacket(packet_number, largest_received, |
| smallest_received); |
| } |
| |
| std::unique_ptr<quic::QuicReceivedPacket> ConstructClientPriorityPacket( |
| uint64_t packet_number, |
| bool should_include_version, |
| quic::QuicStreamId id, |
| quic::QuicStreamId parent_stream_id, |
| RequestPriority request_priority) { |
| return client_maker_->MakePriorityPacket( |
| packet_number, should_include_version, id, parent_stream_id, |
| ConvertRequestPriorityToQuicPriority(request_priority)); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientPriorityFramesPacket( |
| uint64_t packet_number, |
| bool should_include_version, |
| const std::vector<QuicTestPacketMaker::Http2StreamDependency>& |
| priority_frames) { |
| return client_maker_->MakeMultiplePriorityFramesPacket( |
| packet_number, should_include_version, priority_frames); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientAckAndPriorityFramesPacket( |
| uint64_t packet_number, |
| bool should_include_version, |
| uint64_t largest_received, |
| uint64_t smallest_received, |
| const std::vector<QuicTestPacketMaker::Http2StreamDependency>& |
| priority_frames) { |
| return client_maker_->MakeAckAndMultiplePriorityFramesPacket( |
| packet_number, should_include_version, largest_received, |
| smallest_received, priority_frames); |
| } |
| |
| std::unique_ptr<quic::QuicReceivedPacket> ConstructClientAckAndPriorityPacket( |
| uint64_t packet_number, |
| bool should_include_version, |
| uint64_t largest_received, |
| uint64_t smallest_received, |
| quic::QuicStreamId id, |
| quic::QuicStreamId parent_stream_id, |
| RequestPriority request_priority) { |
| return client_maker_->MakeAckAndPriorityPacket( |
| packet_number, should_include_version, largest_received, |
| smallest_received, id, parent_stream_id, |
| ConvertRequestPriorityToQuicPriority(request_priority)); |
| } |
| |
| // Uses default QuicTestPacketMaker. |
| spdy::SpdyHeaderBlock GetRequestHeaders(const std::string& method, |
| const std::string& scheme, |
| const std::string& path) { |
| return GetRequestHeaders(method, scheme, path, client_maker_.get()); |
| } |
| |
| // Uses customized QuicTestPacketMaker. |
| spdy::SpdyHeaderBlock GetRequestHeaders(const std::string& method, |
| const std::string& scheme, |
| const std::string& path, |
| QuicTestPacketMaker* maker) { |
| return maker->GetRequestHeaders(method, scheme, path); |
| } |
| |
| spdy::SpdyHeaderBlock ConnectRequestHeaders(const std::string& host_port) { |
| return client_maker_->ConnectRequestHeaders(host_port); |
| } |
| |
| spdy::SpdyHeaderBlock GetResponseHeaders(const std::string& status) { |
| return server_maker_.GetResponseHeaders(status); |
| } |
| |
| // Appends alt_svc headers in the response headers. |
| spdy::SpdyHeaderBlock GetResponseHeaders(const std::string& status, |
| const std::string& alt_svc) { |
| return server_maker_.GetResponseHeaders(status, alt_svc); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructServerDataPacket( |
| uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| quiche::QuicheStringPiece data) { |
| return server_maker_.MakeDataPacket(packet_number, stream_id, |
| should_include_version, fin, data); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructClientDataPacket( |
| uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| quiche::QuicheStringPiece data) { |
| return client_maker_->MakeDataPacket(packet_number, stream_id, |
| should_include_version, fin, data); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructClientAckAndDataPacket( |
| uint64_t packet_number, |
| bool include_version, |
| quic::QuicStreamId stream_id, |
| uint64_t largest_received, |
| uint64_t smallest_received, |
| bool fin, |
| quiche::QuicheStringPiece data) { |
| return client_maker_->MakeAckAndDataPacket(packet_number, include_version, |
| stream_id, largest_received, |
| smallest_received, fin, data); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientRequestHeadersPacket(uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| spdy::SpdyHeaderBlock headers) { |
| return ConstructClientRequestHeadersPacket(packet_number, stream_id, |
| should_include_version, fin, |
| std::move(headers), 0); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientRequestHeadersPacket(uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| spdy::SpdyHeaderBlock headers, |
| quic::QuicStreamId parent_stream_id) { |
| return ConstructClientRequestHeadersPacket( |
| packet_number, stream_id, should_include_version, fin, DEFAULT_PRIORITY, |
| std::move(headers), parent_stream_id); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructClientRequestHeadersPacket(uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| RequestPriority request_priority, |
| spdy::SpdyHeaderBlock headers, |
| quic::QuicStreamId parent_stream_id) { |
| spdy::SpdyPriority priority = |
| ConvertRequestPriorityToQuicPriority(request_priority); |
| return client_maker_->MakeRequestHeadersPacket( |
| packet_number, stream_id, should_include_version, fin, priority, |
| std::move(headers), parent_stream_id, nullptr); |
| } |
| |
| std::unique_ptr<quic::QuicReceivedPacket> |
| ConstructClientRequestHeadersAndDataFramesPacket( |
| uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| RequestPriority request_priority, |
| spdy::SpdyHeaderBlock headers, |
| quic::QuicStreamId parent_stream_id, |
| size_t* spdy_headers_frame_length, |
| const std::vector<std::string>& data_writes) { |
| spdy::SpdyPriority priority = |
| ConvertRequestPriorityToQuicPriority(request_priority); |
| return client_maker_->MakeRequestHeadersAndMultipleDataFramesPacket( |
| packet_number, stream_id, should_include_version, fin, priority, |
| std::move(headers), parent_stream_id, spdy_headers_frame_length, |
| data_writes); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> ConstructServerPushPromisePacket( |
| uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| quic::QuicStreamId promised_stream_id, |
| bool should_include_version, |
| spdy::SpdyHeaderBlock headers) { |
| return server_maker_.MakePushPromisePacket( |
| packet_number, stream_id, promised_stream_id, should_include_version, |
| false, std::move(headers), nullptr); |
| } |
| |
| std::unique_ptr<quic::QuicEncryptedPacket> |
| ConstructServerResponseHeadersPacket(uint64_t packet_number, |
| quic::QuicStreamId stream_id, |
| bool should_include_version, |
| bool fin, |
| spdy::SpdyHeaderBlock headers) { |
| return server_maker_.MakeResponseHeadersPacket(packet_number, stream_id, |
| should_include_version, fin, |
| std::move(headers), nullptr); |
| } |
| |
| std::string ConstructDataHeader(size_t body_len) { |
| if (!version_.HasIetfQuicFrames()) { |
| return ""; |
| } |
| std::unique_ptr<char[]> buffer; |
| auto header_length = |
| quic::HttpEncoder::SerializeDataFrameHeader(body_len, &buffer); |
| return std::string(buffer.get(), header_length); |
| } |
| |
| void CreateSession(const quic::ParsedQuicVersionVector& supported_versions) { |
| session_params_.enable_quic = true; |
| context_.params()->supported_versions = supported_versions; |
| context_.params()->headers_include_h2_stream_dependency = |
| client_headers_include_h2_stream_dependency_; |
| |
| session_context_.quic_context = &context_; |
| session_context_.client_socket_factory = &socket_factory_; |
| session_context_.quic_crypto_client_stream_factory = |
| &crypto_client_stream_factory_; |
| session_context_.host_resolver = &host_resolver_; |
| session_context_.cert_verifier = &cert_verifier_; |
| session_context_.transport_security_state = &transport_security_state_; |
| session_context_.cert_transparency_verifier = |
| cert_transparency_verifier_.get(); |
| session_context_.ct_policy_enforcer = &ct_policy_enforcer_; |
| session_context_.socket_performance_watcher_factory = |
| &test_socket_performance_watcher_factory_; |
| session_context_.proxy_resolution_service = proxy_resolution_service_.get(); |
| session_context_.ssl_config_service = ssl_config_service_.get(); |
| session_context_.http_auth_handler_factory = auth_handler_factory_.get(); |
| session_context_.http_server_properties = http_server_properties_.get(); |
| session_context_.net_log = net_log_.bound().net_log(); |
| |
| session_.reset(new HttpNetworkSession(session_params_, session_context_)); |
| session_->quic_stream_factory() |
| ->set_is_quic_known_to_work_on_current_network(true); |
| SpdySessionPoolPeer spdy_pool_peer(session_->spdy_session_pool()); |
| spdy_pool_peer.SetEnableSendingInitialData(false); |
| } |
| |
| void CreateSession() { return CreateSession(supported_versions_); } |
| |
| void CheckWasQuicResponse(HttpNetworkTransaction* trans, |
| const std::string& status_line, |
| const quic::ParsedQuicVersion& version) { |
| const HttpResponseInfo* response = trans->GetResponseInfo(); |
| ASSERT_TRUE(response != nullptr); |
| ASSERT_TRUE(response->headers.get() != nullptr); |
| EXPECT_EQ(status_line, response->headers->GetStatusLine()); |
| EXPECT_TRUE(response->was_fetched_via_spdy); |
| EXPECT_TRUE(response->was_alpn_negotiated); |
| EXPECT_EQ(QuicHttpStream::ConnectionInfoFromQuicVersion(version), |
| response->connection_info); |
| } |
| |
| void CheckWasQuicResponse(HttpNetworkTransaction* trans, |
| const std::string& status_line) { |
| CheckWasQuicResponse(trans, status_line, version_); |
| } |
| |
| void CheckWasQuicResponse(HttpNetworkTransaction* trans) { |
| CheckWasQuicResponse(trans, "HTTP/1.1 200 OK", version_); |
| } |
| |
| void CheckResponsePort(HttpNetworkTransaction* trans, uint16_t port) { |
| const HttpResponseInfo* response = trans->GetResponseInfo(); |
| ASSERT_TRUE(response != nullptr); |
| EXPECT_EQ(port, response->remote_endpoint.port()); |
| } |
| |
| void CheckWasHttpResponse(HttpNetworkTransaction* trans) { |
| const HttpResponseInfo* response = trans->GetResponseInfo(); |
| ASSERT_TRUE(response != nullptr); |
| ASSERT_TRUE(response->headers.get() != nullptr); |
| EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| EXPECT_FALSE(response->was_fetched_via_spdy); |
| EXPECT_FALSE(response->was_alpn_negotiated); |
| EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1_1, |
| response->connection_info); |
| } |
| |
| void CheckWasSpdyResponse(HttpNetworkTransaction* trans) { |
| const HttpResponseInfo* response = trans->GetResponseInfo(); |
| ASSERT_TRUE(response != nullptr); |
| ASSERT_TRUE(response->headers.get() != nullptr); |
| EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| EXPECT_TRUE(response->was_fetched_via_spdy); |
| EXPECT_TRUE(response->was_alpn_negotiated); |
| EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP2, |
| response->connection_info); |
| } |
| |
| void CheckResponseData(HttpNetworkTransaction* trans, |
| const std::string& expected) { |
| std::string response_data; |
| ASSERT_THAT(ReadTransaction(trans, &response_data), IsOk()); |
| EXPECT_EQ(expected, response_data); |
| } |
| |
| void RunTransaction(HttpNetworkTransaction* trans) { |
| TestCompletionCallback callback; |
| int rv = trans->Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| } |
| |
| void SendRequestAndExpectHttpResponse(const std::string& expected) { |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| RunTransaction(&trans); |
| CheckWasHttpResponse(&trans); |
| CheckResponseData(&trans, expected); |
| } |
| |
| void SendRequestAndExpectHttpResponseFromProxy(const std::string& expected, |
| bool used_proxy, |
| uint16_t port) { |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| RunTransaction(&trans); |
| CheckWasHttpResponse(&trans); |
| CheckResponsePort(&trans, port); |
| CheckResponseData(&trans, expected); |
| if (used_proxy) { |
| EXPECT_TRUE(trans.GetResponseInfo()->proxy_server.is_https()); |
| } else { |
| EXPECT_TRUE(trans.GetResponseInfo()->proxy_server.is_direct()); |
| } |
| } |
| void SendRequestAndExpectQuicResponse(const std::string& expected, |
| const std::string& status_line) { |
| SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false, 443, |
| status_line); |
| } |
| |
| void SendRequestAndExpectQuicResponse(const std::string& expected) { |
| SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false, 443); |
| } |
| |
| void SendRequestAndExpectQuicResponseFromProxyOnPort( |
| const std::string& expected, |
| uint16_t port) { |
| SendRequestAndExpectQuicResponseMaybeFromProxy(expected, true, port); |
| } |
| |
| void AddQuicAlternateProtocolMapping( |
| MockCryptoClientStream::HandshakeMode handshake_mode, |
| const NetworkIsolationKey& network_isolation_key = |
| NetworkIsolationKey()) { |
| crypto_client_stream_factory_.set_handshake_mode(handshake_mode); |
| url::SchemeHostPort server(request_.url); |
| AlternativeService alternative_service(kProtoQUIC, server.host(), 443); |
| base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| http_server_properties_->SetQuicAlternativeService( |
| server, network_isolation_key, alternative_service, expiration, |
| supported_versions_); |
| } |
| |
| void AddQuicRemoteAlternativeServiceMapping( |
| MockCryptoClientStream::HandshakeMode handshake_mode, |
| const HostPortPair& alternative) { |
| crypto_client_stream_factory_.set_handshake_mode(handshake_mode); |
| url::SchemeHostPort server(request_.url); |
| AlternativeService alternative_service(kProtoQUIC, alternative.host(), |
| alternative.port()); |
| base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| http_server_properties_->SetQuicAlternativeService( |
| server, NetworkIsolationKey(), alternative_service, expiration, |
| supported_versions_); |
| } |
| |
| void ExpectBrokenAlternateProtocolMapping( |
| const NetworkIsolationKey& network_isolation_key = |
| NetworkIsolationKey()) { |
| const url::SchemeHostPort server(request_.url); |
| const AlternativeServiceInfoVector alternative_service_info_vector = |
| http_server_properties_->GetAlternativeServiceInfos( |
| server, network_isolation_key); |
| EXPECT_EQ(1u, alternative_service_info_vector.size()); |
| EXPECT_TRUE(http_server_properties_->IsAlternativeServiceBroken( |
| alternative_service_info_vector[0].alternative_service(), |
| network_isolation_key)); |
| } |
| |
| void ExpectQuicAlternateProtocolMapping( |
| const NetworkIsolationKey& network_isolation_key = |
| NetworkIsolationKey()) { |
| const url::SchemeHostPort server(request_.url); |
| const AlternativeServiceInfoVector alternative_service_info_vector = |
| http_server_properties_->GetAlternativeServiceInfos( |
| server, network_isolation_key); |
| EXPECT_EQ(1u, alternative_service_info_vector.size()); |
| EXPECT_EQ( |
| kProtoQUIC, |
| alternative_service_info_vector[0].alternative_service().protocol); |
| EXPECT_FALSE(http_server_properties_->IsAlternativeServiceBroken( |
| alternative_service_info_vector[0].alternative_service(), |
| network_isolation_key)); |
| } |
| |
| void AddHangingNonAlternateProtocolSocketData() { |
| std::unique_ptr<StaticSocketDataProvider> hanging_data; |
| hanging_data.reset(new StaticSocketDataProvider()); |
| MockConnect hanging_connect(SYNCHRONOUS, ERR_IO_PENDING); |
| hanging_data->set_connect_data(hanging_connect); |
| hanging_data_.push_back(std::move(hanging_data)); |
| socket_factory_.AddSocketDataProvider(hanging_data_.back().get()); |
| } |
| |
| void SetUpTestForRetryConnectionOnAlternateNetwork() { |
| context_.params()->migrate_sessions_on_network_change_v2 = true; |
| context_.params()->migrate_sessions_early_v2 = true; |
| context_.params()->retry_on_alternate_network_before_handshake = true; |
| scoped_mock_change_notifier_.reset(new ScopedMockNetworkChangeNotifier()); |
| MockNetworkChangeNotifier* mock_ncn = |
| scoped_mock_change_notifier_->mock_network_change_notifier(); |
| mock_ncn->ForceNetworkHandlesSupported(); |
| mock_ncn->SetConnectedNetworksList( |
| {kDefaultNetworkForTests, kNewNetworkForTests}); |
| } |
| |
| // Adds a new socket data provider for an HTTP request, and runs a request, |
| // expecting it to be used. |
| void AddHttpDataAndRunRequest() { |
| MockWrite http_writes[] = { |
| MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"), |
| MockWrite(SYNCHRONOUS, 1, "Host: mail.example.org\r\n"), |
| MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n")}; |
| |
| MockRead http_reads[] = { |
| MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"), |
| MockRead(SYNCHRONOUS, 4, kQuicAlternativeServiceHeader), |
| MockRead(SYNCHRONOUS, 5, "http used"), |
| // Connection closed. |
| MockRead(SYNCHRONOUS, OK, 6)}; |
| SequencedSocketData http_data(http_reads, http_writes); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| SSLSocketDataProvider ssl_data(ASYNC, OK); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data); |
| SendRequestAndExpectHttpResponse("http used"); |
| EXPECT_TRUE(http_data.AllWriteDataConsumed()); |
| EXPECT_TRUE(http_data.AllReadDataConsumed()); |
| } |
| |
| // Adds a new socket data provider for a QUIC request, and runs a request, |
| // expecting it to be used. The new QUIC session is not closed. |
| void AddQuicDataAndRunRequest() { |
| QuicTestPacketMaker client_maker( |
| version_, |
| quic::QuicUtils::CreateRandomConnectionId(context_.random_generator()), |
| context_.clock(), kDefaultServerHostName, quic::Perspective::IS_CLIENT, |
| client_headers_include_h2_stream_dependency_); |
| QuicTestPacketMaker server_maker( |
| version_, |
| quic::QuicUtils::CreateRandomConnectionId(context_.random_generator()), |
| context_.clock(), kDefaultServerHostName, quic::Perspective::IS_SERVER, |
| false); |
| MockQuicData quic_data(version_); |
| int packet_number = 1; |
| client_maker.SetEncryptionLevel(quic::ENCRYPTION_ZERO_RTT); |
| if (VersionUsesHttp3(version_.transport_version)) { |
| quic_data.AddWrite( |
| SYNCHRONOUS, client_maker.MakeInitialSettingsPacket(packet_number++)); |
| } |
| quic_data.AddWrite( |
| SYNCHRONOUS, |
| client_maker.MakeRequestHeadersPacket( |
| packet_number++, GetNthClientInitiatedBidirectionalStreamId(0), |
| true, true, ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY), |
| GetRequestHeaders("GET", "https", "/", &client_maker), 0, nullptr)); |
| client_maker.SetEncryptionLevel(quic::ENCRYPTION_FORWARD_SECURE); |
| quic_data.AddRead( |
| ASYNC, server_maker.MakeResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, |
| false, server_maker.GetResponseHeaders("200 OK"), nullptr)); |
| std::string header = ConstructDataHeader(9); |
| quic_data.AddRead( |
| ASYNC, server_maker.MakeDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, |
| true, header + "quic used")); |
| // Don't care about the final ack. |
| quic_data.AddWrite(SYNCHRONOUS, ERR_IO_PENDING); |
| // No more data to read. |
| quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); |
| quic_data.AddSocketDataToFactory(&socket_factory_); |
| SendRequestAndExpectQuicResponse("quic used"); |
| |
| EXPECT_TRUE(quic_data.AllReadDataConsumed()); |
| } |
| |
| quic::QuicStreamId GetNthClientInitiatedBidirectionalStreamId(int n) const { |
| return quic::test::GetNthClientInitiatedBidirectionalStreamId( |
| version_.transport_version, n); |
| } |
| |
| quic::QuicStreamId GetNthServerInitiatedUnidirectionalStreamId(int n) const { |
| return quic::test::GetNthServerInitiatedUnidirectionalStreamId( |
| version_.transport_version, n); |
| } |
| |
| quic::QuicStreamId GetQpackDecoderStreamId() const { |
| return quic::test::GetNthClientInitiatedUnidirectionalStreamId( |
| version_.transport_version, 1); |
| } |
| |
| std::string StreamCancellationQpackDecoderInstruction(int n) const { |
| return StreamCancellationQpackDecoderInstruction(n, true); |
| } |
| |
| std::string StreamCancellationQpackDecoderInstruction( |
| int n, |
| bool create_stream) const { |
| const quic::QuicStreamId cancelled_stream_id = |
| GetNthClientInitiatedBidirectionalStreamId(n); |
| EXPECT_LT(cancelled_stream_id, 63u); |
| |
| const unsigned char opcode = 0x40; |
| if (create_stream) { |
| return {0x03, opcode | static_cast<unsigned char>(cancelled_stream_id)}; |
| } else { |
| return {opcode | static_cast<unsigned char>(cancelled_stream_id)}; |
| } |
| } |
| |
| static void AddCertificate(SSLSocketDataProvider* ssl_data) { |
| ssl_data->ssl_info.cert = |
| ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| ASSERT_TRUE(ssl_data->ssl_info.cert); |
| } |
| |
| void SendRequestAndExpectQuicResponseMaybeFromProxy( |
| const std::string& expected, |
| bool used_proxy, |
| uint16_t port, |
| const std::string& status_line, |
| const quic::ParsedQuicVersion& version) { |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| RunTransaction(&trans); |
| CheckWasQuicResponse(&trans, status_line, version); |
| CheckResponsePort(&trans, port); |
| CheckResponseData(&trans, expected); |
| if (used_proxy) { |
| EXPECT_TRUE(trans.GetResponseInfo()->proxy_server.is_quic()); |
| } else { |
| EXPECT_TRUE(trans.GetResponseInfo()->proxy_server.is_direct()); |
| } |
| } |
| |
| const quic::ParsedQuicVersion version_; |
| const bool client_headers_include_h2_stream_dependency_; |
| quic::ParsedQuicVersionVector supported_versions_; |
| QuicFlagSaver flags_; // Save/restore all QUIC flag values. |
| MockQuicContext context_; |
| std::unique_ptr<QuicTestPacketMaker> client_maker_; |
| QuicTestPacketMaker server_maker_; |
| scoped_refptr<TestTaskRunner> quic_task_runner_; |
| std::unique_ptr<HttpNetworkSession> session_; |
| MockClientSocketFactory socket_factory_; |
| ProofVerifyDetailsChromium verify_details_; |
| MockCryptoClientStreamFactory crypto_client_stream_factory_; |
| MockHostResolver host_resolver_; |
| MockCertVerifier cert_verifier_; |
| TransportSecurityState transport_security_state_; |
| std::unique_ptr<CTVerifier> cert_transparency_verifier_; |
| DefaultCTPolicyEnforcer ct_policy_enforcer_; |
| TestSocketPerformanceWatcherFactory test_socket_performance_watcher_factory_; |
| std::unique_ptr<SSLConfigServiceDefaults> ssl_config_service_; |
| std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; |
| std::unique_ptr<HttpAuthHandlerFactory> auth_handler_factory_; |
| std::unique_ptr<HttpServerProperties> http_server_properties_; |
| HttpNetworkSession::Params session_params_; |
| HttpNetworkSession::Context session_context_; |
| HttpRequestInfo request_; |
| RecordingBoundTestNetLog net_log_; |
| std::vector<std::unique_ptr<StaticSocketDataProvider>> hanging_data_; |
| SSLSocketDataProvider ssl_data_; |
| std::unique_ptr<ScopedMockNetworkChangeNotifier> scoped_mock_change_notifier_; |
| |
| private: |
| void SendRequestAndExpectQuicResponseMaybeFromProxy( |
| const std::string& expected, |
| bool used_proxy, |
| uint16_t port, |
| const std::string& status_line) { |
| SendRequestAndExpectQuicResponseMaybeFromProxy(expected, used_proxy, port, |
| status_line, version_); |
| } |
| |
| void SendRequestAndExpectQuicResponseMaybeFromProxy( |
| const std::string& expected, |
| bool used_proxy, |
| uint16_t port) { |
| SendRequestAndExpectQuicResponseMaybeFromProxy(expected, used_proxy, port, |
| "HTTP/1.1 200 OK", version_); |
| } |
| }; |
| |
| INSTANTIATE_TEST_SUITE_P(VersionIncludeStreamDependencySequence, |
| QuicNetworkTransactionTest, |
| ::testing::ValuesIn(GetTestParams()), |
| ::testing::PrintToStringParamName()); |
| |
| // TODO(950069): Add testing for frame_origin in NetworkIsolationKey using |
| // kAppendInitiatingFrameOriginToNetworkIsolationKey. |
| |
| TEST_P(QuicNetworkTransactionTest, WriteErrorHandshakeConfirmed) { |
| context_.params()->retry_without_alt_svc_on_quic_errors = false; |
| base::HistogramTester histograms; |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| crypto_client_stream_factory_.set_handshake_mode( |
| MockCryptoClientStream::CONFIRM_HANDSHAKE); |
| |
| MockQuicData mock_quic_data(version_); |
| if (VersionUsesHttp3(version_.transport_version)) |
| mock_quic_data.AddWrite(SYNCHRONOUS, ConstructInitialSettingsPacket(1)); |
| mock_quic_data.AddWrite(SYNCHRONOUS, ERR_INTERNET_DISCONNECTED); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // Pause |
| mock_quic_data.AddRead(ASYNC, OK); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| EXPECT_THAT(callback.WaitForResult(), IsError(ERR_QUIC_PROTOCOL_ERROR)); |
| |
| histograms.ExpectBucketCount("Net.QuicSession.WriteError", |
| -ERR_INTERNET_DISCONNECTED, 1); |
| histograms.ExpectBucketCount("Net.QuicSession.WriteError.HandshakeConfirmed", |
| -ERR_INTERNET_DISCONNECTED, 1); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, WriteErrorHandshakeConfirmedAsync) { |
| context_.params()->retry_without_alt_svc_on_quic_errors = false; |
| base::HistogramTester histograms; |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| crypto_client_stream_factory_.set_handshake_mode( |
| MockCryptoClientStream::CONFIRM_HANDSHAKE); |
| |
| MockQuicData mock_quic_data(version_); |
| if (VersionUsesHttp3(version_.transport_version)) |
| mock_quic_data.AddWrite(SYNCHRONOUS, ConstructInitialSettingsPacket(1)); |
| mock_quic_data.AddWrite(ASYNC, ERR_INTERNET_DISCONNECTED); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // Pause |
| mock_quic_data.AddRead(ASYNC, OK); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| EXPECT_THAT(callback.WaitForResult(), IsError(ERR_QUIC_PROTOCOL_ERROR)); |
| |
| histograms.ExpectBucketCount("Net.QuicSession.WriteError", |
| -ERR_INTERNET_DISCONNECTED, 1); |
| histograms.ExpectBucketCount("Net.QuicSession.WriteError.HandshakeConfirmed", |
| -ERR_INTERNET_DISCONNECTED, 1); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, SocketWatcherEnabled) { |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| test_socket_performance_watcher_factory_.set_should_notify_updated_rtt(true); |
| |
| EXPECT_FALSE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| SendRequestAndExpectQuicResponse("hello!"); |
| EXPECT_TRUE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, SocketWatcherDisabled) { |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| test_socket_performance_watcher_factory_.set_should_notify_updated_rtt(false); |
| |
| EXPECT_FALSE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| SendRequestAndExpectQuicResponse("hello!"); |
| EXPECT_FALSE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, ForceQuic) { |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| SendRequestAndExpectQuicResponse("hello!"); |
| |
| // Check that the NetLog was filled reasonably. |
| auto entries = net_log_.GetEntries(); |
| EXPECT_LT(0u, entries.size()); |
| |
| // Check that we logged a QUIC_SESSION_PACKET_RECEIVED. |
| int pos = ExpectLogContainsSomewhere( |
| entries, 0, NetLogEventType::QUIC_SESSION_PACKET_RECEIVED, |
| NetLogEventPhase::NONE); |
| EXPECT_LT(0, pos); |
| |
| // ... and also a TYPE_QUIC_SESSION_UNAUTHENTICATED_PACKET_HEADER_RECEIVED. |
| pos = ExpectLogContainsSomewhere( |
| entries, 0, |
| NetLogEventType::QUIC_SESSION_UNAUTHENTICATED_PACKET_HEADER_RECEIVED, |
| NetLogEventPhase::NONE); |
| EXPECT_LT(0, pos); |
| |
| EXPECT_EQ(1, GetIntegerValueFromParams(entries[pos], "packet_number")); |
| |
| // ... and also a TYPE_QUIC_SESSION_PACKET_AUTHENTICATED. |
| pos = ExpectLogContainsSomewhere( |
| entries, 0, NetLogEventType::QUIC_SESSION_PACKET_AUTHENTICATED, |
| NetLogEventPhase::NONE); |
| EXPECT_LT(0, pos); |
| |
| // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED. |
| pos = ExpectLogContainsSomewhere( |
| entries, 0, NetLogEventType::QUIC_SESSION_STREAM_FRAME_RECEIVED, |
| NetLogEventPhase::NONE); |
| EXPECT_LT(0, pos); |
| |
| int log_stream_id = GetIntegerValueFromParams(entries[pos], "stream_id"); |
| if (quic::VersionUsesHttp3(version_.transport_version)) { |
| EXPECT_EQ(GetNthClientInitiatedBidirectionalStreamId(0), |
| static_cast<quic::QuicStreamId>(log_stream_id)); |
| } else { |
| EXPECT_EQ(quic::QuicUtils::GetHeadersStreamId(version_.transport_version), |
| static_cast<quic::QuicStreamId>(log_stream_id)); |
| } |
| } |
| |
| // Regression test for https://crbug.com/1043531. |
| TEST_P(QuicNetworkTransactionTest, ResetOnEmptyResponseHeaders) { |
| if (!quic::VersionUsesHttp3(version_.transport_version)) { |
| return; |
| } |
| |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data(version_); |
| int write_packet_num = 1; |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(write_packet_num++)); |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| write_packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), |
| true, true, GetRequestHeaders("GET", "https", "/"))); |
| |
| const quic::QuicStreamId request_stream_id = |
| GetNthClientInitiatedBidirectionalStreamId(0); |
| spdy::SpdyHeaderBlock empty_response_headers; |
| const std::string response_data = server_maker_.QpackEncodeHeaders( |
| request_stream_id, std::move(empty_response_headers), nullptr); |
| uint64_t read_packet_num = 1; |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket(read_packet_num++, request_stream_id, |
| false, false, response_data)); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| |
| mock_quic_data.AddWrite( |
| ASYNC, |
| ConstructClientAckAndDataPacket( |
| write_packet_num++, true, GetQpackDecoderStreamId(), 1, 1, false, |
| StreamCancellationQpackDecoderInstruction(request_stream_id))); |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| EXPECT_THAT(callback.WaitForResult(), IsError(ERR_INVALID_RESPONSE)); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, LargeResponseHeaders) { |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| spdy::SpdyHeaderBlock response_headers = GetResponseHeaders("200 OK"); |
| response_headers["key1"] = std::string(30000, 'A'); |
| response_headers["key2"] = std::string(30000, 'A'); |
| response_headers["key3"] = std::string(30000, 'A'); |
| response_headers["key4"] = std::string(30000, 'A'); |
| response_headers["key5"] = std::string(30000, 'A'); |
| response_headers["key6"] = std::string(30000, 'A'); |
| response_headers["key7"] = std::string(30000, 'A'); |
| response_headers["key8"] = std::string(30000, 'A'); |
| quic::QuicStreamId stream_id; |
| std::string response_data; |
| if (quic::VersionUsesHttp3(version_.transport_version)) { |
| stream_id = GetNthClientInitiatedBidirectionalStreamId(0); |
| response_data = server_maker_.QpackEncodeHeaders( |
| stream_id, std::move(response_headers), nullptr); |
| } else { |
| stream_id = quic::QuicUtils::GetHeadersStreamId(version_.transport_version); |
| spdy::SpdyHeadersIR headers_frame( |
| GetNthClientInitiatedBidirectionalStreamId(0), |
| std::move(response_headers)); |
| spdy::SpdyFramer response_framer(spdy::SpdyFramer::ENABLE_COMPRESSION); |
| spdy::SpdySerializedFrame spdy_frame = |
| response_framer.SerializeFrame(headers_frame); |
| response_data = std::string(spdy_frame.data(), spdy_frame.size()); |
| } |
| |
| uint64_t packet_number = 1; |
| size_t chunk_size = 1200; |
| for (size_t offset = 0; offset < response_data.length(); |
| offset += chunk_size) { |
| size_t len = std::min(chunk_size, response_data.length() - offset); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| packet_number++, stream_id, false, false, |
| base::StringPiece(response_data.data() + offset, len))); |
| } |
| |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| packet_number, GetNthClientInitiatedBidirectionalStreamId(0), |
| false, true, header + "hello!")); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddWrite(ASYNC, ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddWrite( |
| ASYNC, ConstructClientAckPacket(packet_num++, packet_number, 3)); |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| SendRequestAndExpectQuicResponse("hello!"); |
| EXPECT_TRUE(mock_quic_data.AllReadDataConsumed()); |
| EXPECT_TRUE(mock_quic_data.AllWriteDataConsumed()); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, TooLargeResponseHeaders) { |
| context_.params()->retry_without_alt_svc_on_quic_errors = false; |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| |
| spdy::SpdyHeaderBlock response_headers = GetResponseHeaders("200 OK"); |
| response_headers["key1"] = std::string(30000, 'A'); |
| response_headers["key2"] = std::string(30000, 'A'); |
| response_headers["key3"] = std::string(30000, 'A'); |
| response_headers["key4"] = std::string(30000, 'A'); |
| response_headers["key5"] = std::string(30000, 'A'); |
| response_headers["key6"] = std::string(30000, 'A'); |
| response_headers["key7"] = std::string(30000, 'A'); |
| response_headers["key8"] = std::string(30000, 'A'); |
| response_headers["key9"] = std::string(30000, 'A'); |
| |
| quic::QuicStreamId stream_id; |
| std::string response_data; |
| if (quic::VersionUsesHttp3(version_.transport_version)) { |
| stream_id = GetNthClientInitiatedBidirectionalStreamId(0); |
| response_data = server_maker_.QpackEncodeHeaders( |
| stream_id, std::move(response_headers), nullptr); |
| } else { |
| stream_id = quic::QuicUtils::GetHeadersStreamId(version_.transport_version); |
| spdy::SpdyHeadersIR headers_frame( |
| GetNthClientInitiatedBidirectionalStreamId(0), |
| std::move(response_headers)); |
| spdy::SpdyFramer response_framer(spdy::SpdyFramer::ENABLE_COMPRESSION); |
| spdy::SpdySerializedFrame spdy_frame = |
| response_framer.SerializeFrame(headers_frame); |
| response_data = std::string(spdy_frame.data(), spdy_frame.size()); |
| } |
| |
| uint64_t packet_number = 1; |
| size_t chunk_size = 1200; |
| for (size_t offset = 0; offset < response_data.length(); |
| offset += chunk_size) { |
| size_t len = std::min(chunk_size, response_data.length() - offset); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| packet_number++, stream_id, false, false, |
| base::StringPiece(response_data.data() + offset, len))); |
| } |
| |
| std::string header = ConstructDataHeader(6); |
| |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| packet_number, GetNthClientInitiatedBidirectionalStreamId(0), |
| false, true, header + "hello!")); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddWrite(ASYNC, ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddWrite( |
| ASYNC, ConstructClientAckAndRstPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), |
| quic::QUIC_HEADERS_TOO_LARGE, packet_number, 3)); |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| EXPECT_THAT(callback.WaitForResult(), IsError(ERR_QUIC_PROTOCOL_ERROR)); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, ForceQuicForAll) { |
| context_.params()->origins_to_force_quic_on.insert(HostPortPair()); |
| |
| AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| SendRequestAndExpectQuicResponse("hello!"); |
| EXPECT_TRUE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| } |
| |
| // Regression test for https://crbug.com/695225 |
| TEST_P(QuicNetworkTransactionTest, 408Response) { |
| context_.params()->origins_to_force_quic_on.insert(HostPortPair()); |
| |
| AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("408 Request Timeout"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more data to read |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| SendRequestAndExpectQuicResponse("hello!", "HTTP/1.1 408 Request Timeout"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, QuicProxy) { |
| session_params_.enable_quic = true; |
| proxy_resolution_service_ = |
| ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
| "QUIC mail.example.org:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "http", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| EXPECT_FALSE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| // There is no need to set up an alternate protocol job, because |
| // no attempt will be made to speak to the proxy over TCP. |
| |
| request_.url = GURL("http://mail.example.org/"); |
| CreateSession(); |
| |
| SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70); |
| EXPECT_TRUE( |
| test_socket_performance_watcher_factory_.rtt_notification_received()); |
| } |
| |
| // Regression test for https://crbug.com/492458. Test that for an HTTP |
| // connection through a QUIC proxy, the certificate exhibited by the proxy is |
| // checked against the proxy hostname, not the origin hostname. |
| TEST_P(QuicNetworkTransactionTest, QuicProxyWithCert) { |
| const std::string origin_host = "mail.example.com"; |
| const std::string proxy_host = "www.example.org"; |
| |
| session_params_.enable_quic = true; |
| proxy_resolution_service_ = |
| ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
| "QUIC " + proxy_host + ":70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| |
| client_maker_->set_hostname(origin_host); |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "http", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| scoped_refptr<X509Certificate> cert( |
| ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem")); |
| ASSERT_TRUE(cert.get()); |
| // This certificate is valid for the proxy, but not for the origin. |
| EXPECT_TRUE(cert->VerifyNameMatch(proxy_host)); |
| EXPECT_FALSE(cert->VerifyNameMatch(origin_host)); |
| ProofVerifyDetailsChromium verify_details; |
| verify_details.cert_verify_result.verified_cert = cert; |
| crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details); |
| ProofVerifyDetailsChromium verify_details2; |
| verify_details2.cert_verify_result.verified_cert = cert; |
| crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details2); |
| |
| request_.url = GURL("http://" + origin_host); |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE); |
| SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, AlternativeServicesDifferentHost) { |
| context_.params()->allow_remote_alt_svc = true; |
| HostPortPair origin("www.example.org", 443); |
| HostPortPair alternative("mail.example.org", 443); |
| |
| base::FilePath certs_dir = GetTestCertsDirectory(); |
| scoped_refptr<X509Certificate> cert( |
| ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem")); |
| ASSERT_TRUE(cert.get()); |
| // TODO(rch): the connection should be "to" the origin, so if the cert is |
| // valid for the origin but not the alternative, that should work too. |
| EXPECT_TRUE(cert->VerifyNameMatch(origin.host())); |
| EXPECT_TRUE(cert->VerifyNameMatch(alternative.host())); |
| ProofVerifyDetailsChromium verify_details; |
| verify_details.cert_verify_result.verified_cert = cert; |
| crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details); |
| |
| client_maker_->set_hostname(origin.host()); |
| MockQuicData mock_quic_data(version_); |
| |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| request_.url = GURL("https://" + origin.host()); |
| AddQuicRemoteAlternativeServiceMapping( |
| MockCryptoClientStream::CONFIRM_HANDSHAKE, alternative); |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, DoNotUseQuicForUnsupportedVersion) { |
| quic::ParsedQuicVersion unsupported_version = |
| quic::ParsedQuicVersion::Unsupported(); |
| // Add support for another QUIC version besides |version_|. Also find an |
| // unsupported version. |
| for (const quic::ParsedQuicVersion& version : quic::AllSupportedVersions()) { |
| if (version == version_) |
| continue; |
| if (supported_versions_.size() != 2) { |
| supported_versions_.push_back(version); |
| continue; |
| } |
| unsupported_version = version; |
| break; |
| } |
| ASSERT_EQ(2u, supported_versions_.size()); |
| ASSERT_NE(quic::ParsedQuicVersion::Unsupported(), unsupported_version); |
| |
| // Set up alternative service to use QUIC with a version that is not |
| // supported. |
| url::SchemeHostPort server(request_.url); |
| AlternativeService alternative_service(kProtoQUIC, kDefaultServerHostName, |
| 443); |
| base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| http_server_properties_->SetQuicAlternativeService( |
| server, NetworkIsolationKey(), alternative_service, expiration, |
| {unsupported_version}); |
| |
| AlternativeServiceInfoVector alt_svc_info_vector = |
| http_server_properties_->GetAlternativeServiceInfos( |
| server, NetworkIsolationKey()); |
| EXPECT_EQ(1u, alt_svc_info_vector.size()); |
| EXPECT_EQ(kProtoQUIC, alt_svc_info_vector[0].alternative_service().protocol); |
| EXPECT_EQ(1u, alt_svc_info_vector[0].advertised_versions().size()); |
| EXPECT_EQ(unsupported_version, |
| alt_svc_info_vector[0].advertised_versions()[0]); |
| |
| // First request should still be sent via TCP as the QUIC version advertised |
| // in the stored AlternativeService is not supported by the client. However, |
| // the response from the server will advertise new Alt-Svc with supported |
| // versions. |
| std::string altsvc_header = GenerateQuicAltSvcHeader(supported_versions_); |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead(altsvc_header.c_str()), |
| MockRead("\r\n"), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| // Second request should be sent via QUIC as a new list of verions supported |
| // by the client has been advertised by the server. |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| |
| CreateSession(supported_versions_); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| |
| // Check alternative service list is updated with new versions. |
| alt_svc_info_vector = |
| session_->http_server_properties()->GetAlternativeServiceInfos( |
| server, NetworkIsolationKey()); |
| // Versions that support the legacy Google-specific Alt-Svc format are sent in |
| // a single Alt-Svc entry, therefore they are accumulated in a single |
| // AlternativeServiceInfo, whereas more recent versions all have their own |
| // Alt-Svc entry and AlternativeServiceInfo entry. Flatten to compare. |
| quic::ParsedQuicVersionVector alt_svc_negotiated_versions; |
| for (const auto& alt_svc_info : alt_svc_info_vector) { |
| EXPECT_EQ(kProtoQUIC, alt_svc_info.alternative_service().protocol); |
| for (const auto& version : alt_svc_info.advertised_versions()) { |
| if (std::find(alt_svc_negotiated_versions.begin(), |
| alt_svc_negotiated_versions.end(), |
| version) == alt_svc_negotiated_versions.end()) { |
| alt_svc_negotiated_versions.push_back(version); |
| } |
| } |
| } |
| |
| ASSERT_EQ(supported_versions_.size(), alt_svc_negotiated_versions.size()); |
| auto version_compare = [](const quic::ParsedQuicVersion& a, |
| const quic::ParsedQuicVersion& b) { |
| return std::tie(a.transport_version, a.handshake_protocol) < |
| std::tie(b.transport_version, b.handshake_protocol); |
| }; |
| std::sort(supported_versions_.begin(), supported_versions_.end(), |
| version_compare); |
| std::sort(alt_svc_negotiated_versions.begin(), |
| alt_svc_negotiated_versions.end(), version_compare); |
| EXPECT_TRUE(std::equal(supported_versions_.begin(), supported_versions_.end(), |
| alt_svc_negotiated_versions.begin())); |
| } |
| |
| // Regression test for https://crbug.com/546991. |
| // The server might not be able to serve a request on an alternative connection, |
| // and might send a 421 Misdirected Request response status to indicate this. |
| // HttpNetworkTransaction should reset the request and retry without using |
| // alternative services. |
| TEST_P(QuicNetworkTransactionTest, RetryMisdirectedRequest) { |
| // Set up alternative service to use QUIC. |
| // Note that |origins_to_force_quic_on| cannot be used in this test, because |
| // that overrides |enable_alternative_services|. |
| url::SchemeHostPort server(request_.url); |
| AlternativeService alternative_service(kProtoQUIC, kDefaultServerHostName, |
| 443); |
| base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| http_server_properties_->SetQuicAlternativeService( |
| server, NetworkIsolationKey(), alternative_service, expiration, |
| supported_versions_); |
| |
| // First try: The alternative job uses QUIC and reports an HTTP 421 |
| // Misdirected Request error. The main job uses TCP, but |http_data| below is |
| // paused at Connect(), so it will never exit the socket pool. This ensures |
| // that the alternate job always wins the race and keeps whether the |
| // |http_data| exits the socket pool before the main job is aborted |
| // deterministic. The first main job gets aborted without the socket pool ever |
| // dispensing the socket, making it available for the second try. |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| GetResponseHeaders("421"))); |
| mock_quic_data.AddRead(ASYNC, OK); |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| // Second try: The main job uses TCP, and there is no alternate job. Once the |
| // Connect() is unblocked, |http_data| will leave the socket pool, binding to |
| // the main job of the second request. It then succeeds over HTTP/1.1. |
| // Note that if there was an alternative QUIC Job created for the second try, |
| // that would read these data, and would fail with ERR_QUIC_PROTOCOL_ERROR. |
| // Therefore this test ensures that no alternative Job is created on retry. |
| MockWrite writes[] = {MockWrite(ASYNC, 0, "GET / HTTP/1.1\r\n"), |
| MockWrite(ASYNC, 1, "Host: mail.example.org\r\n"), |
| MockWrite(ASYNC, 2, "Connection: keep-alive\r\n\r\n")}; |
| MockRead reads[] = {MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n\r\n"), |
| MockRead(ASYNC, 4, "hello!"), MockRead(ASYNC, OK, 5)}; |
| SequencedSocketData http_data(MockConnect(ASYNC, ERR_IO_PENDING) /* pause */, |
| reads, writes); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| CreateSession(); |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| |
| // Run until |mock_quic_data| has failed and |http_data| has paused. |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| base::RunLoop().RunUntilIdle(); |
| |
| // |mock_quic_data| must have run to completion. |
| EXPECT_TRUE(mock_quic_data.AllReadDataConsumed()); |
| EXPECT_TRUE(mock_quic_data.AllWriteDataConsumed()); |
| |
| // Now that the QUIC data has been consumed, unblock |http_data|. |
| http_data.socket()->OnConnectComplete(MockConnect()); |
| |
| // The retry logic must hide the 421 status. The transaction succeeds on |
| // |http_data|. |
| EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| CheckWasHttpResponse(&trans); |
| CheckResponsePort(&trans, 443); |
| CheckResponseData(&trans, "hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, ForceQuicWithErrorConnecting) { |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("mail.example.org:443")); |
| |
| MockQuicData mock_quic_data1(version_); |
| if (VersionUsesHttp3(version_.transport_version)) |
| mock_quic_data1.AddWrite(SYNCHRONOUS, ConstructInitialSettingsPacket(1)); |
| mock_quic_data1.AddRead(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
| client_maker_->Reset(); |
| MockQuicData mock_quic_data2(version_); |
| if (VersionUsesHttp3(version_.transport_version)) |
| mock_quic_data2.AddWrite(SYNCHRONOUS, ConstructInitialSettingsPacket(1)); |
| crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details_); |
| mock_quic_data2.AddRead(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
| crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details_); |
| |
| mock_quic_data1.AddSocketDataToFactory(&socket_factory_); |
| mock_quic_data2.AddSocketDataToFactory(&socket_factory_); |
| |
| CreateSession(); |
| |
| EXPECT_EQ(0U, test_socket_performance_watcher_factory_.watcher_count()); |
| for (size_t i = 0; i < 2; ++i) { |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_CLOSED)); |
| EXPECT_EQ(1 + i, test_socket_performance_watcher_factory_.watcher_count()); |
| |
| NetErrorDetails details; |
| trans.PopulateNetErrorDetails(&details); |
| EXPECT_EQ(quic::QUIC_PACKET_READ_ERROR, details.quic_connection_error); |
| } |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, DoNotForceQuicForHttps) { |
| // Attempt to "force" quic on 443, which will not be honored. |
| context_.params()->origins_to_force_quic_on.insert( |
| HostPortPair::FromString("www.google.com:443")); |
| |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&data); |
| SSLSocketDataProvider ssl(ASYNC, OK); |
| socket_factory_.AddSSLSocketDataProvider(&ssl); |
| |
| CreateSession(); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| EXPECT_EQ(0U, test_socket_performance_watcher_factory_.watcher_count()); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, UseAlternativeServiceForQuic) { |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), MockRead(kQuicAlternativeServiceHeader), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, UseIetfAlternativeServiceForQuic) { |
| std::string alt_svc_header = |
| "Alt-Svc: " + quic::AlpnForVersion(version_) + "=\":443\"\r\n\r\n"; |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), MockRead(alt_svc_header.data()), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| // Much like above, but makes sure NetworkIsolationKey is respected. |
| TEST_P(QuicNetworkTransactionTest, |
| UseAlternativeServiceForQuicWithNetworkIsolationKey) { |
| base::test::ScopedFeatureList feature_list; |
| feature_list.InitWithFeatures( |
| // enabled_features |
| {features::kPartitionHttpServerPropertiesByNetworkIsolationKey, |
| features::kPartitionConnectionsByNetworkIsolationKey}, |
| // disabled_features |
| {}); |
| // Since HttpServerProperties caches the feature value, have to create a new |
| // one. |
| http_server_properties_ = std::make_unique<HttpServerProperties>(); |
| |
| const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), MockRead(kQuicAlternativeServiceHeader), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| AddCertificate(&ssl_data_); |
| |
| // Request with empty NetworkIsolationKey. |
| StaticSocketDataProvider http_data1(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data1); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| // First request with kNetworkIsolationKey1. |
| StaticSocketDataProvider http_data2(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data2); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| // Request with kNetworkIsolationKey2. |
| StaticSocketDataProvider http_data3(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data3); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| // Second request with kNetworkIsolationKey1, can finally use QUIC, since |
| // alternative service infrmation has been received in this context before. |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| |
| // This is first so that the test fails if alternative service info is |
| // written with the right NetworkIsolationKey, but always queried with an |
| // empty one. |
| request_.network_isolation_key = NetworkIsolationKey(); |
| SendRequestAndExpectHttpResponse("hello world"); |
| request_.network_isolation_key = kNetworkIsolationKey1; |
| SendRequestAndExpectHttpResponse("hello world"); |
| request_.network_isolation_key = kNetworkIsolationKey2; |
| SendRequestAndExpectHttpResponse("hello world"); |
| |
| // Only use QUIC when using a NetworkIsolationKey which has been used when |
| // alternative service information was received. |
| request_.network_isolation_key = kNetworkIsolationKey1; |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, UseAlternativeServiceWithVersionForQuic1) { |
| // Both server advertises and client supports two QUIC versions. |
| // Only |version_| is advertised and supported. |
| // The QuicStreamFactoy will pick up |version_|, which is verified as the |
| // PacketMakers are using |version_|. |
| |
| // Add support for another QUIC version besides |version_| on the client side. |
| // Also find a different version advertised by the server. |
| quic::ParsedQuicVersion advertised_version_2 = |
| quic::ParsedQuicVersion::Unsupported(); |
| for (const quic::ParsedQuicVersion& version : quic::AllSupportedVersions()) { |
| if (version == version_) |
| continue; |
| if (supported_versions_.size() != 2) { |
| supported_versions_.push_back(version); |
| continue; |
| } |
| advertised_version_2 = version; |
| break; |
| } |
| ASSERT_EQ(2u, supported_versions_.size()); |
| ASSERT_NE(quic::ParsedQuicVersion::Unsupported(), advertised_version_2); |
| |
| std::string QuicAltSvcWithVersionHeader = |
| base::StringPrintf("Alt-Svc: %s=\":443\", %s=\":443\"\r\n\r\n", |
| quic::AlpnForVersion(advertised_version_2).c_str(), |
| quic::AlpnForVersion(version_).c_str()); |
| |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead(QuicAltSvcWithVersionHeader.c_str()), MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(supported_versions_); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, |
| PickQuicVersionWhenMultipleVersionsAreSupported) { |
| // Client and server both support more than one QUIC_VERSION. |
| // Client prefers |version_|, and then common_version_2. |
| // Server prefers common_version_2, and then |version_|. |
| // We should honor the server's preference. |
| // The picked version is verified via checking the version used by the |
| // TestPacketMakers and the response. |
| |
| // Find an alternative commonly supported version other than |version_|. |
| quic::ParsedQuicVersion common_version_2 = |
| quic::ParsedQuicVersion::Unsupported(); |
| for (const quic::ParsedQuicVersion& version : quic::AllSupportedVersions()) { |
| if (version != version_) { |
| common_version_2 = version; |
| break; |
| } |
| } |
| ASSERT_NE(common_version_2, quic::ParsedQuicVersion::Unsupported()); |
| |
| // Setting up client's preference list: {|version_|, |common_version_2|}. |
| supported_versions_.clear(); |
| supported_versions_.push_back(version_); |
| supported_versions_.push_back(common_version_2); |
| |
| // Setting up server's Alt-Svc header in the following preference order: |
| // |common_version_2|, |version_|. |
| std::string QuicAltSvcWithVersionHeader; |
| quic::ParsedQuicVersion picked_version = |
| quic::ParsedQuicVersion::Unsupported(); |
| QuicAltSvcWithVersionHeader = |
| "Alt-Svc: " + quic::AlpnForVersion(common_version_2) + |
| "=\":443\"; ma=3600, " + quic::AlpnForVersion(version_) + |
| "=\":443\"; ma=3600\r\n\r\n"; |
| picked_version = common_version_2; // Use server's preference. |
| |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead(QuicAltSvcWithVersionHeader.c_str()), MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(picked_version); |
| |
| // Reset QuicTestPacket makers as the version picked may not be |version_|. |
| client_maker_.reset(new QuicTestPacketMaker( |
| picked_version, |
| quic::QuicUtils::CreateRandomConnectionId(context_.random_generator()), |
| context_.clock(), kDefaultServerHostName, quic::Perspective::IS_CLIENT, |
| client_headers_include_h2_stream_dependency_)); |
| QuicTestPacketMaker server_maker( |
| picked_version, |
| quic::QuicUtils::CreateRandomConnectionId(context_.random_generator()), |
| context_.clock(), kDefaultServerHostName, quic::Perspective::IS_SERVER, |
| false); |
| |
| int packet_num = 1; |
| if (VersionUsesHttp3(picked_version.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| |
| quic::QuicStreamId client_stream_0 = |
| quic::test::GetNthClientInitiatedBidirectionalStreamId( |
| picked_version.transport_version, 0); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, client_stream_0, true, true, |
| GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, server_maker.MakeResponseHeadersPacket( |
| 1, client_stream_0, false, false, |
| server_maker.GetResponseHeaders("200 OK"), nullptr)); |
| std::string header(""); |
| if (VersionUsesHttp3(picked_version.transport_version)) { |
| std::unique_ptr<char[]> buffer; |
| auto header_length = |
| quic::HttpEncoder::SerializeDataFrameHeader(6, &buffer); |
| header = std::string(buffer.get(), header_length); |
| } |
| |
| mock_quic_data.AddRead( |
| ASYNC, server_maker.MakeDataPacket(2, client_stream_0, false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(supported_versions_); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponseMaybeFromProxy( |
| "hello!", false, 443, "HTTP/1.1 200 OK", picked_version); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, |
| UseAlternativeServiceWithProbabilityForQuic) { |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead(kQuicAlternativeServiceWithProbabilityHeader), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, SetAlternativeServiceWithScheme) { |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead("Alt-Svc: quic=\"foo.example.org:443\", quic=\":444\"\r\n\r\n"), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| CreateSession(); |
| // Send https request, ignore alternative service advertising if response |
| // header advertises alternative service for mail.example.org. |
| request_.url = GURL("https://mail.example.org:443"); |
| SendRequestAndExpectHttpResponse("hello world"); |
| HttpServerProperties* http_server_properties = |
| session_->http_server_properties(); |
| url::SchemeHostPort http_server("http", "mail.example.org", 443); |
| url::SchemeHostPort https_server("https", "mail.example.org", 443); |
| // Check alternative service is set for the correct origin. |
| EXPECT_EQ( |
| 2u, http_server_properties |
| ->GetAlternativeServiceInfos(https_server, NetworkIsolationKey()) |
| .size()); |
| EXPECT_TRUE( |
| http_server_properties |
| ->GetAlternativeServiceInfos(http_server, NetworkIsolationKey()) |
| .empty()); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, DoNotGetAltSvcForDifferentOrigin) { |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead("Alt-Svc: quic=\"foo.example.org:443\", quic=\":444\"\r\n\r\n"), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| AddCertificate(&ssl_data_); |
| |
| socket_factory_.AddSocketDataProvider(&http_data); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| CreateSession(); |
| |
| // Send https request and set alternative services if response header |
| // advertises alternative service for mail.example.org. |
| SendRequestAndExpectHttpResponse("hello world"); |
| HttpServerProperties* http_server_properties = |
| session_->http_server_properties(); |
| |
| const url::SchemeHostPort https_server(request_.url); |
| // Check alternative service is set. |
| EXPECT_EQ( |
| 2u, http_server_properties |
| ->GetAlternativeServiceInfos(https_server, NetworkIsolationKey()) |
| .size()); |
| |
| // Send http request to the same origin but with diffrent scheme, should not |
| // use QUIC. |
| request_.url = GURL("http://mail.example.org:443"); |
| SendRequestAndExpectHttpResponse("hello world"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, |
| StoreMutuallySupportedVersionsWhenProcessAltSvc) { |
| // Add support for another QUIC version besides |version_|. |
| for (const quic::ParsedQuicVersion& version : quic::AllSupportedVersions()) { |
| if (version != version_) { |
| supported_versions_.push_back(version); |
| break; |
| } |
| } |
| |
| std::string altsvc_header = GenerateQuicAltSvcHeader(supported_versions_); |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), |
| MockRead(altsvc_header.c_str()), |
| MockRead("\r\n"), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| |
| CreateSession(supported_versions_); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| |
| // Alt-Svc header contains all possible versions, so alternative services |
| // should contain all of |supported_versions_|. |
| const url::SchemeHostPort https_server(request_.url); |
| const AlternativeServiceInfoVector alt_svc_info_vector = |
| session_->http_server_properties()->GetAlternativeServiceInfos( |
| https_server, NetworkIsolationKey()); |
| // Versions that support the legacy Google-specific Alt-Svc format are sent in |
| // a single Alt-Svc entry, therefore they are accumulated in a single |
| // AlternativeServiceInfo, whereas more recent versions all have their own |
| // Alt-Svc entry and AlternativeServiceInfo entry. Flatten to compare. |
| quic::ParsedQuicVersionVector alt_svc_negotiated_versions; |
| for (const auto& alt_svc_info : alt_svc_info_vector) { |
| EXPECT_EQ(kProtoQUIC, alt_svc_info.alternative_service().protocol); |
| for (const auto& version : alt_svc_info.advertised_versions()) { |
| if (std::find(alt_svc_negotiated_versions.begin(), |
| alt_svc_negotiated_versions.end(), |
| version) == alt_svc_negotiated_versions.end()) { |
| alt_svc_negotiated_versions.push_back(version); |
| } |
| } |
| } |
| |
| ASSERT_EQ(supported_versions_.size(), alt_svc_negotiated_versions.size()); |
| auto version_compare = [](const quic::ParsedQuicVersion& a, |
| const quic::ParsedQuicVersion& b) { |
| return std::tie(a.transport_version, a.handshake_protocol) < |
| std::tie(b.transport_version, b.handshake_protocol); |
| }; |
| std::sort(supported_versions_.begin(), supported_versions_.end(), |
| version_compare); |
| std::sort(alt_svc_negotiated_versions.begin(), |
| alt_svc_negotiated_versions.end(), version_compare); |
| EXPECT_TRUE(std::equal(supported_versions_.begin(), supported_versions_.end(), |
| alt_svc_negotiated_versions.begin())); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, UseAlternativeServiceAllSupportedVersion) { |
| std::string altsvc_header = base::StringPrintf( |
| "Alt-Svc: %s=\":443\"\r\n\r\n", quic::AlpnForVersion(version_).c_str()); |
| MockRead http_reads[] = { |
| MockRead("HTTP/1.1 200 OK\r\n"), MockRead(altsvc_header.c_str()), |
| MockRead("hello world"), |
| MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| MockRead(ASYNC, OK)}; |
| |
| StaticSocketDataProvider http_data(http_reads, base::span<MockWrite>()); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| AddCertificate(&ssl_data_); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerDataPacket( |
| 2, GetNthClientInitiatedBidirectionalStreamId(0), false, true, |
| header + "hello!")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| AddHangingNonAlternateProtocolSocketData(); |
| CreateSession(); |
| |
| SendRequestAndExpectHttpResponse("hello world"); |
| SendRequestAndExpectQuicResponse("hello!"); |
| } |
| |
| TEST_P(QuicNetworkTransactionTest, GoAwayWithConnectionMigrationOnPortsOnly) { |
| if (version_.HasIetfQuicFrames()) { |
| // GoAway is not available under version 99 |
| return; |
| } |
| MockQuicData mock_quic_data(version_); |
| int packet_num = 1; |
| if (VersionUsesHttp3(version_.transport_version)) { |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructInitialSettingsPacket(packet_num++)); |
| } |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientRequestHeadersPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true, |
| true, GetRequestHeaders("GET", "https", "/"))); |
| mock_quic_data.AddRead( |
| ASYNC, ConstructServerResponseHeadersPacket( |
| 1, GetNthClientInitiatedBidirectionalStreamId(0), false, false, |
| GetResponseHeaders("200 OK"))); |
| // Read a GoAway packet with |
| // quic::QuicErrorCode: quic::QUIC_ERROR_MIGRATING_PORT from the peer. |
| mock_quic_data.AddRead(SYNCHRONOUS, |
| ConstructServerGoAwayPacket( |
| 2, quic::QUIC_ERROR_MIGRATING_PORT, |
| "connection migration with port change only")); |
| mock_quic_data.AddWrite(SYNCHRONOUS, |
| ConstructClientAckPacket(packet_num++, 2, 1)); |
| std::string header = ConstructDataHeader(6); |
| mock_quic_data.AddRead( |
| SYNCHRONOUS, ConstructServerDataPacket( |
| 3, GetNthClientInitiatedBidirectionalStreamId(0), false, |
| true, header + "hello!")); |
| mock_quic_data.AddWrite( |
| SYNCHRONOUS, |
| ConstructClientAckAndRstPacket( |
| packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), |
| quic::QUIC_STREAM_CANCELLED, 3, 3)); |
| mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read |
| mock_quic_data.AddRead(ASYNC, 0); // EOF |
| |
| mock_quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| // The non-alternate protocol job needs to hang in order to guarantee that |
| // the alternate-protocol job will "win". |
| AddHangingNonAlternateProtocolSocketData(); |
| |
| // In order for a new QUIC session to be established via alternate-protocol |
| // without racing an HTTP connection, we need the host resolution to happen |
| // synchronously. Of course, even though QUIC *could* perform a 0-RTT |
| // connection to the the server, in this test we require confirmation |
| // before encrypting so the HTTP job will still start. |
| host_resolver_.set_synchronous_mode(true); |
| host_resolver_.rules()->AddIPLiteralRule("mail.example.org", "192.168.0.1", |
| ""); |
| |
| CreateSession(); |
| session_->quic_stream_factory()->set_is_quic_known_to_work_on_current_network( |
| false); |
| AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| |
| crypto_client_stream_factory_.last_stream() |
| ->NotifySessionOneRttKeyAvailable(); |
| EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| |
| // Check whether this transaction is correctly marked as received a go-away |
| // because of migrating port. |
| NetErrorDetails details; |
| EXPECT_FALSE(details.quic_port_migration_detected); |
| trans.PopulateNetErrorDetails(&details); |
| EXPECT_TRUE(details.quic_port_migration_detected); |
| } |
| |
| // This test verifies that a new QUIC connection will be attempted on the |
| // alternate network if the original QUIC connection fails with idle timeout |
| // before handshake is confirmed. If TCP succeeds and QUIC fails on the |
| // alternate network as well, QUIC is marked as broken and the brokenness will |
| // not expire when default network changes. |
| // TODO(fayang): Add time driven idle network detection test. |
| TEST_P(QuicNetworkTransactionTest, |
| DISABLED_QuicFailsOnBothNetworksWhileTCPSucceeds) { |
| if (version_.UsesTls()) { |
| // QUIC with TLS1.3 handshake doesn't support 0-rtt. |
| return; |
| } |
| SetUpTestForRetryConnectionOnAlternateNetwork(); |
| |
| client_maker_->SetEncryptionLevel(quic::ENCRYPTION_ZERO_RTT); |
| |
| // The request will initially go out over QUIC. |
| MockQuicData quic_data(version_); |
| quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // Hanging read |
| int packet_num = 1; |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); // CHLO |
| // Retranmit the handshake messages. |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| // After timeout, connection will be closed with QUIC_NETWORK_IDLE_TIMEOUT. |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeConnectionClosePacket( |
| packet_num++, true, quic::QUIC_NETWORK_IDLE_TIMEOUT, |
| "No recent network activity after 4s. Timeout:4s")); |
| quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| // Add successful TCP data so that TCP job will succeed. |
| MockWrite http_writes[] = { |
| MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"), |
| MockWrite(SYNCHRONOUS, 1, "Host: mail.example.org\r\n"), |
| MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n")}; |
| |
| MockRead http_reads[] = { |
| MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"), |
| MockRead(SYNCHRONOUS, 4, kQuicAlternativeServiceHeader), |
| MockRead(SYNCHRONOUS, 5, "TCP succeeds"), MockRead(SYNCHRONOUS, OK, 6)}; |
| SequencedSocketData http_data(http_reads, http_writes); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| // Add data for the second QUIC connection to fail. |
| MockQuicData quic_data2(version_); |
| quic_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING); |
| quic_data2.AddWrite(SYNCHRONOUS, ERR_ADDRESS_UNREACHABLE); // Write error. |
| quic_data2.AddSocketDataToFactory(&socket_factory_); |
| |
| // Resolve the host resolution synchronously. |
| host_resolver_.set_synchronous_mode(true); |
| host_resolver_.rules()->AddIPLiteralRule("mail.example.org", "192.168.0.1", |
| ""); |
| |
| CreateSession(); |
| session_->quic_stream_factory()->set_is_quic_known_to_work_on_current_network( |
| false); |
| // Use a TestTaskRunner to avoid waiting in real time for timeouts. |
| QuicStreamFactoryPeer::SetAlarmFactory( |
| session_->quic_stream_factory(), |
| std::make_unique<QuicChromiumAlarmFactory>(quic_task_runner_.get(), |
| context_.clock())); |
| // Add alternate protocol mapping to race QUIC and TCP. |
| // QUIC connection requires handshake to be confirmed and sends CHLO to the |
| // peer. |
| AddQuicAlternateProtocolMapping( |
| MockCryptoClientStream::COLD_START_WITH_CHLO_SENT); |
| |
| HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get()); |
| TestCompletionCallback callback; |
| int rv = trans.Start(&request_, callback.callback(), net_log_.bound()); |
| EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| |
| // Pump the message loop to get the request started. |
| // Request will be served with TCP job. |
| base::RunLoop().RunUntilIdle(); |
| EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| CheckResponseData(&trans, "TCP succeeds"); |
| |
| // Fast forward to idle timeout the original connection. A new connection will |
| // be kicked off on the alternate network. |
| quic_task_runner_->FastForwardBy(quic::QuicTime::Delta::FromSeconds(4)); |
| ASSERT_TRUE(quic_data.AllReadDataConsumed()); |
| ASSERT_TRUE(quic_data.AllWriteDataConsumed()); |
| |
| // Run the message loop to execute posted tasks, which will report job status. |
| base::RunLoop().RunUntilIdle(); |
| |
| // Verify that QUIC is marked as broken. |
| ExpectBrokenAlternateProtocolMapping(); |
| |
| // Deliver a message to notify the new network becomes default, the brokenness |
| // will not expire as QUIC is broken on both networks. |
| scoped_mock_change_notifier_->mock_network_change_notifier() |
| ->NotifyNetworkMadeDefault(kNewNetworkForTests); |
| ExpectBrokenAlternateProtocolMapping(); |
| |
| ASSERT_TRUE(quic_data2.AllReadDataConsumed()); |
| ASSERT_TRUE(quic_data2.AllWriteDataConsumed()); |
| } |
| |
| // This test verifies that a new QUIC connection will be attempted on the |
| // alternate network if the original QUIC connection fails with idle timeout |
| // before handshake is confirmed. If TCP succeeds and QUIC succeeds on the |
| // alternate network, QUIC is marked as broken. The brokenness will expire when |
| // the default network changes. |
| // TODO(fayang): Add time driven idle network detection test. |
| TEST_P(QuicNetworkTransactionTest, |
| DISABLED_RetryOnAlternateNetworkWhileTCPSucceeds) { |
| if (version_.UsesTls()) { |
| // QUIC with TLS1.3 handshake doesn't support 0-rtt. |
| return; |
| } |
| |
| SetUpTestForRetryConnectionOnAlternateNetwork(); |
| |
| client_maker_->SetEncryptionLevel(quic::ENCRYPTION_ZERO_RTT); |
| |
| // The request will initially go out over QUIC. |
| MockQuicData quic_data(version_); |
| quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // Hanging read |
| int packet_num = 1; |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); // CHLO |
| // Retranmit the handshake messages. |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(packet_num++)); |
| // After timeout, connection will be closed with QUIC_NETWORK_IDLE_TIMEOUT. |
| quic_data.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeConnectionClosePacket( |
| packet_num++, true, quic::QUIC_NETWORK_IDLE_TIMEOUT, |
| "No recent network activity after 4s. Timeout:4s")); |
| quic_data.AddSocketDataToFactory(&socket_factory_); |
| |
| // Add successful TCP data so that TCP job will succeed. |
| MockWrite http_writes[] = { |
| MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"), |
| MockWrite(SYNCHRONOUS, 1, "Host: mail.example.org\r\n"), |
| MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n")}; |
| |
| MockRead http_reads[] = { |
| MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"), |
| MockRead(SYNCHRONOUS, 4, kQuicAlternativeServiceHeader), |
| MockRead(SYNCHRONOUS, 5, "TCP succeeds"), MockRead(SYNCHRONOUS, OK, 6)}; |
| SequencedSocketData http_data(http_reads, http_writes); |
| socket_factory_.AddSocketDataProvider(&http_data); |
| socket_factory_.AddSSLSocketDataProvider(&ssl_data_); |
| |
| // Quic connection will be retried on the alternate network after the initial |
| // one fails on the default network. |
| MockQuicData quic_data2(version_); |
| quic_data2.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // Handing read. |
| quic_data2.AddWrite(SYNCHRONOUS, |
| client_maker_->MakeDummyCHLOPacket(1)); // CHLO |
| |
| client_maker_->SetEncryptionLevel(quic::ENCRYPTION_FORWARD_SECURE); |
| if (VersionUsesHttp3(version_.transport_version)) |
| quic_data2.AddWrite(SYNCHRONOUS, ConstructInitialSettingsPacket(2)); |
| quic_data2.AddSocketDataToFactory(&socket_factory_); |
| |
| // Resolve the host resolution synchronously. |
| host_resolver_.set_synchronous_mode(true); |
| host_resolver_.rules()->AddIPLiteralRule("mail.example.org", "192.168.0.1", |
| ""); |
| |
| CreateSession(); |
| session_->quic_stream_factory()->set_is_quic_known_to_work_on_current_network( |
| false); |
| // Use a TestTaskRunner to avoid waiting in real time for timeouts. |
| |