dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 1 | // Copyright (c) 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/test_tools/first_flight.h" |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | #include <vector> |
| 9 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 10 | #include "quic/core/crypto/quic_crypto_client_config.h" |
| 11 | #include "quic/core/http/quic_client_push_promise_index.h" |
| 12 | #include "quic/core/http/quic_spdy_client_session.h" |
| 13 | #include "quic/core/quic_config.h" |
| 14 | #include "quic/core/quic_connection.h" |
| 15 | #include "quic/core/quic_connection_id.h" |
| 16 | #include "quic/core/quic_packet_writer.h" |
| 17 | #include "quic/core/quic_packets.h" |
| 18 | #include "quic/core/quic_versions.h" |
| 19 | #include "quic/platform/api/quic_ip_address.h" |
| 20 | #include "quic/platform/api/quic_socket_address.h" |
| 21 | #include "quic/test_tools/crypto_test_utils.h" |
| 22 | #include "quic/test_tools/quic_test_utils.h" |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 23 | |
| 24 | namespace quic { |
| 25 | namespace test { |
| 26 | |
| 27 | // Utility class that creates a custom HTTP/3 session and QUIC connection in |
| 28 | // order to extract the first flight of packets it sends. This is meant to only |
| 29 | // be used by GetFirstFlightOfPackets() below. |
| 30 | class FirstFlightExtractor : public DelegatedPacketWriter::Delegate { |
| 31 | public: |
| 32 | FirstFlightExtractor(const ParsedQuicVersion& version, |
| 33 | const QuicConfig& config, |
| 34 | const QuicConnectionId& server_connection_id, |
| 35 | const QuicConnectionId& client_connection_id) |
| 36 | : version_(version), |
| 37 | server_connection_id_(server_connection_id), |
| 38 | client_connection_id_(client_connection_id), |
| 39 | writer_(this), |
| 40 | config_(config), |
| 41 | crypto_config_(crypto_test_utils::ProofVerifierForTesting()) { |
| 42 | EXPECT_NE(version_, UnsupportedQuicVersion()); |
| 43 | } |
| 44 | |
| 45 | void GenerateFirstFlight() { |
| 46 | crypto_config_.set_alpn(AlpnForVersion(version_)); |
| 47 | connection_ = |
| 48 | new QuicConnection(server_connection_id_, |
wub | 5df3663 | 2020-10-01 14:42:17 -0700 | [diff] [blame] | 49 | /*initial_self_address=*/QuicSocketAddress(), |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 50 | QuicSocketAddress(TestPeerIPAddress(), kTestPort), |
| 51 | &connection_helper_, &alarm_factory_, &writer_, |
| 52 | /*owns_writer=*/false, Perspective::IS_CLIENT, |
| 53 | ParsedQuicVersionVector{version_}); |
| 54 | connection_->set_client_connection_id(client_connection_id_); |
| 55 | session_ = std::make_unique<QuicSpdyClientSession>( |
| 56 | config_, ParsedQuicVersionVector{version_}, |
| 57 | connection_, // session_ takes ownership of connection_ here. |
| 58 | TestServerId(), &crypto_config_, &push_promise_index_); |
| 59 | session_->Initialize(); |
| 60 | session_->CryptoConnect(); |
| 61 | } |
| 62 | |
| 63 | void OnDelegatedPacket(const char* buffer, |
| 64 | size_t buf_len, |
| 65 | const QuicIpAddress& /*self_client_address*/, |
| 66 | const QuicSocketAddress& /*peer_client_address*/, |
| 67 | PerPacketOptions* /*options*/) override { |
| 68 | packets_.emplace_back( |
| 69 | QuicReceivedPacket(buffer, buf_len, |
| 70 | connection_helper_.GetClock()->ApproximateNow(), |
| 71 | /*owns_buffer=*/false) |
| 72 | .Clone()); |
| 73 | } |
| 74 | |
| 75 | std::vector<std::unique_ptr<QuicReceivedPacket>>&& ConsumePackets() { |
| 76 | return std::move(packets_); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | ParsedQuicVersion version_; |
| 81 | QuicConnectionId server_connection_id_; |
| 82 | QuicConnectionId client_connection_id_; |
| 83 | MockQuicConnectionHelper connection_helper_; |
| 84 | MockAlarmFactory alarm_factory_; |
| 85 | DelegatedPacketWriter writer_; |
| 86 | QuicConfig config_; |
| 87 | QuicCryptoClientConfig crypto_config_; |
| 88 | QuicClientPushPromiseIndex push_promise_index_; |
| 89 | QuicConnection* connection_; // Owned by session_. |
| 90 | std::unique_ptr<QuicSpdyClientSession> session_; |
| 91 | std::vector<std::unique_ptr<QuicReceivedPacket>> packets_; |
| 92 | }; |
| 93 | |
| 94 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 95 | const ParsedQuicVersion& version, |
| 96 | const QuicConfig& config, |
| 97 | const QuicConnectionId& server_connection_id, |
| 98 | const QuicConnectionId& client_connection_id) { |
| 99 | FirstFlightExtractor first_flight_extractor( |
| 100 | version, config, server_connection_id, client_connection_id); |
| 101 | first_flight_extractor.GenerateFirstFlight(); |
| 102 | return first_flight_extractor.ConsumePackets(); |
| 103 | } |
| 104 | |
| 105 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 106 | const ParsedQuicVersion& version, |
| 107 | const QuicConfig& config, |
| 108 | const QuicConnectionId& server_connection_id) { |
| 109 | return GetFirstFlightOfPackets(version, config, server_connection_id, |
| 110 | EmptyQuicConnectionId()); |
| 111 | } |
| 112 | |
| 113 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 114 | const ParsedQuicVersion& version, |
| 115 | const QuicConfig& config) { |
| 116 | return GetFirstFlightOfPackets(version, config, TestConnectionId()); |
| 117 | } |
| 118 | |
| 119 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 120 | const ParsedQuicVersion& version, |
| 121 | const QuicConnectionId& server_connection_id, |
| 122 | const QuicConnectionId& client_connection_id) { |
| 123 | return GetFirstFlightOfPackets(version, DefaultQuicConfig(), |
| 124 | server_connection_id, client_connection_id); |
| 125 | } |
| 126 | |
| 127 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 128 | const ParsedQuicVersion& version, |
| 129 | const QuicConnectionId& server_connection_id) { |
| 130 | return GetFirstFlightOfPackets(version, DefaultQuicConfig(), |
| 131 | server_connection_id, EmptyQuicConnectionId()); |
| 132 | } |
| 133 | |
| 134 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 135 | const ParsedQuicVersion& version) { |
| 136 | return GetFirstFlightOfPackets(version, DefaultQuicConfig(), |
| 137 | TestConnectionId()); |
| 138 | } |
| 139 | |
| 140 | } // namespace test |
| 141 | } // namespace quic |