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 5 #ifndef QUICHE_QUIC_TEST_TOOLS_FIRST_FLIGHT_H_ 6 #define QUICHE_QUIC_TEST_TOOLS_FIRST_FLIGHT_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "quiche/quic/core/crypto/quic_crypto_client_config.h" 12 #include "quiche/quic/core/quic_config.h" 13 #include "quiche/quic/core/quic_connection_id.h" 14 #include "quiche/quic/core/quic_packet_writer.h" 15 #include "quiche/quic/core/quic_packets.h" 16 #include "quiche/quic/core/quic_versions.h" 17 #include "quiche/quic/platform/api/quic_ip_address.h" 18 #include "quiche/quic/platform/api/quic_socket_address.h" 19 20 namespace quic { 21 namespace test { 22 23 // Implementation of QuicPacketWriter that sends all packets to a delegate. 24 class QUIC_NO_EXPORT DelegatedPacketWriter : public QuicPacketWriter { 25 public: 26 class QUIC_NO_EXPORT Delegate { 27 public: ~Delegate()28 virtual ~Delegate() {} 29 // Note that |buffer| may be released after this call completes so overrides 30 // that want to use the data after the call is complete MUST copy it. 31 virtual void OnDelegatedPacket(const char* buffer, size_t buf_len, 32 const QuicIpAddress& self_client_address, 33 const QuicSocketAddress& peer_client_address, 34 PerPacketOptions* options, 35 const QuicPacketWriterParams& params) = 0; 36 }; 37 38 // |delegate| MUST be valid for the duration of the DelegatedPacketWriter's 39 // lifetime. DelegatedPacketWriter(Delegate * delegate)40 explicit DelegatedPacketWriter(Delegate* delegate) : delegate_(delegate) { 41 QUICHE_CHECK_NE(delegate_, nullptr); 42 } 43 44 // Overrides for QuicPacketWriter. IsWriteBlocked()45 bool IsWriteBlocked() const override { return false; } SetWritable()46 void SetWritable() override {} MessageTooBigErrorCode()47 std::optional<int> MessageTooBigErrorCode() const override { 48 return std::nullopt; 49 } GetMaxPacketSize(const QuicSocketAddress &)50 QuicByteCount GetMaxPacketSize( 51 const QuicSocketAddress& /*peer_address*/) const override { 52 return kMaxOutgoingPacketSize; 53 } SupportsReleaseTime()54 bool SupportsReleaseTime() const override { return false; } IsBatchMode()55 bool IsBatchMode() const override { return false; } SupportsEcn()56 bool SupportsEcn() const override { return true; } GetNextWriteLocation(const QuicIpAddress &,const QuicSocketAddress &)57 QuicPacketBuffer GetNextWriteLocation( 58 const QuicIpAddress& /*self_address*/, 59 const QuicSocketAddress& /*peer_address*/) override { 60 return {nullptr, nullptr}; 61 } Flush()62 WriteResult Flush() override { return WriteResult(WRITE_STATUS_OK, 0); } 63 WritePacket(const char * buffer,size_t buf_len,const QuicIpAddress & self_client_address,const QuicSocketAddress & peer_client_address,PerPacketOptions * options,const QuicPacketWriterParams & params)64 WriteResult WritePacket(const char* buffer, size_t buf_len, 65 const QuicIpAddress& self_client_address, 66 const QuicSocketAddress& peer_client_address, 67 PerPacketOptions* options, 68 const QuicPacketWriterParams& params) override { 69 delegate_->OnDelegatedPacket(buffer, buf_len, self_client_address, 70 peer_client_address, options, params); 71 return WriteResult(WRITE_STATUS_OK, buf_len); 72 } 73 74 private: 75 Delegate* delegate_; // Unowned. 76 }; 77 78 // Returns an array of packets that represent the first flight of a real 79 // HTTP/3 connection. In most cases, this array will only contain one packet 80 // that carries the CHLO. 81 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 82 const ParsedQuicVersion& version, const QuicConfig& config, 83 const QuicConnectionId& server_connection_id, 84 const QuicConnectionId& client_connection_id, 85 std::unique_ptr<QuicCryptoClientConfig> crypto_config, 86 QuicEcnCodepoint ecn); 87 88 // Below are various convenience overloads that use default values for the 89 // omitted parameters: 90 // |config| = DefaultQuicConfig(), 91 // |server_connection_id| = TestConnectionId(), 92 // |client_connection_id| = EmptyQuicConnectionId(). 93 // |crypto_config| = 94 // QuicCryptoClientConfig(crypto_test_utils::ProofVerifierForTesting()) 95 // |ecn| = ECN_NOT_ECT 96 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 97 const ParsedQuicVersion& version, const QuicConfig& config, 98 const QuicConnectionId& server_connection_id, 99 const QuicConnectionId& client_connection_id, 100 std::unique_ptr<QuicCryptoClientConfig> crypto_config); 101 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 102 const ParsedQuicVersion& version, const QuicConfig& config, 103 const QuicConnectionId& server_connection_id, 104 const QuicConnectionId& client_connection_id); 105 106 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 107 const ParsedQuicVersion& version, const QuicConfig& config, 108 const QuicConnectionId& server_connection_id); 109 110 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 111 const ParsedQuicVersion& version, 112 const QuicConnectionId& server_connection_id, 113 const QuicConnectionId& client_connection_id); 114 115 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 116 const ParsedQuicVersion& version, 117 const QuicConnectionId& server_connection_id); 118 119 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 120 const ParsedQuicVersion& version, const QuicConfig& config); 121 122 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( 123 const ParsedQuicVersion& version); 124 125 // Functions that also provide additional information about the session. 126 struct AnnotatedPackets { 127 std::vector<std::unique_ptr<QuicReceivedPacket>> packets; 128 uint64_t crypto_stream_size; 129 }; 130 131 AnnotatedPackets GetAnnotatedFirstFlightOfPackets( 132 const ParsedQuicVersion& version, const QuicConfig& config, 133 const QuicConnectionId& server_connection_id, 134 const QuicConnectionId& client_connection_id, 135 std::unique_ptr<QuicCryptoClientConfig> crypto_config); 136 137 AnnotatedPackets GetAnnotatedFirstFlightOfPackets( 138 const ParsedQuicVersion& version, const QuicConfig& config); 139 140 } // namespace test 141 } // namespace quic 142 143 #endif // QUICHE_QUIC_TEST_TOOLS_FIRST_FLIGHT_H_ 144