• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) = 0;
35   };
36 
37   // |delegate| MUST be valid for the duration of the DelegatedPacketWriter's
38   // lifetime.
DelegatedPacketWriter(Delegate * delegate)39   explicit DelegatedPacketWriter(Delegate* delegate) : delegate_(delegate) {
40     QUICHE_CHECK_NE(delegate_, nullptr);
41   }
42 
43   // Overrides for QuicPacketWriter.
IsWriteBlocked()44   bool IsWriteBlocked() const override { return false; }
SetWritable()45   void SetWritable() override {}
MessageTooBigErrorCode()46   absl::optional<int> MessageTooBigErrorCode() const override {
47     return absl::nullopt;
48   }
GetMaxPacketSize(const QuicSocketAddress &)49   QuicByteCount GetMaxPacketSize(
50       const QuicSocketAddress& /*peer_address*/) const override {
51     return kMaxOutgoingPacketSize;
52   }
SupportsReleaseTime()53   bool SupportsReleaseTime() const override { return false; }
IsBatchMode()54   bool IsBatchMode() const override { return false; }
GetNextWriteLocation(const QuicIpAddress &,const QuicSocketAddress &)55   QuicPacketBuffer GetNextWriteLocation(
56       const QuicIpAddress& /*self_address*/,
57       const QuicSocketAddress& /*peer_address*/) override {
58     return {nullptr, nullptr};
59   }
Flush()60   WriteResult Flush() override { return WriteResult(WRITE_STATUS_OK, 0); }
61 
WritePacket(const char * buffer,size_t buf_len,const QuicIpAddress & self_client_address,const QuicSocketAddress & peer_client_address,PerPacketOptions * options)62   WriteResult WritePacket(const char* buffer, size_t buf_len,
63                           const QuicIpAddress& self_client_address,
64                           const QuicSocketAddress& peer_client_address,
65                           PerPacketOptions* options) override {
66     delegate_->OnDelegatedPacket(buffer, buf_len, self_client_address,
67                                  peer_client_address, options);
68     return WriteResult(WRITE_STATUS_OK, buf_len);
69   }
70 
71  private:
72   Delegate* delegate_;  // Unowned.
73 };
74 
75 // Returns an array of packets that represent the first flight of a real
76 // HTTP/3 connection. In most cases, this array will only contain one packet
77 // that carries the CHLO.
78 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
79     const ParsedQuicVersion& version, const QuicConfig& config,
80     const QuicConnectionId& server_connection_id,
81     const QuicConnectionId& client_connection_id,
82     std::unique_ptr<QuicCryptoClientConfig> crypto_config);
83 
84 // Below are various convenience overloads that use default values for the
85 // omitted parameters:
86 // |config| = DefaultQuicConfig(),
87 // |server_connection_id| = TestConnectionId(),
88 // |client_connection_id| = EmptyQuicConnectionId().
89 // |crypto_config| =
90 //     QuicCryptoClientConfig(crypto_test_utils::ProofVerifierForTesting())
91 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
92     const ParsedQuicVersion& version, const QuicConfig& config,
93     const QuicConnectionId& server_connection_id,
94     const QuicConnectionId& client_connection_id);
95 
96 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
97     const ParsedQuicVersion& version, const QuicConfig& config,
98     const QuicConnectionId& server_connection_id);
99 
100 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
101     const ParsedQuicVersion& version,
102     const QuicConnectionId& server_connection_id,
103     const QuicConnectionId& client_connection_id);
104 
105 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
106     const ParsedQuicVersion& version,
107     const QuicConnectionId& server_connection_id);
108 
109 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
110     const ParsedQuicVersion& version, const QuicConfig& config);
111 
112 std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets(
113     const ParsedQuicVersion& version);
114 
115 // Functions that also provide additional information about the session.
116 struct AnnotatedPackets {
117   std::vector<std::unique_ptr<QuicReceivedPacket>> packets;
118   uint64_t crypto_stream_size;
119 };
120 
121 AnnotatedPackets GetAnnotatedFirstFlightOfPackets(
122     const ParsedQuicVersion& version, const QuicConfig& config,
123     const QuicConnectionId& server_connection_id,
124     const QuicConnectionId& client_connection_id,
125     std::unique_ptr<QuicCryptoClientConfig> crypto_config);
126 
127 AnnotatedPackets GetAnnotatedFirstFlightOfPackets(
128     const ParsedQuicVersion& version, const QuicConfig& config);
129 
130 }  // namespace test
131 }  // namespace quic
132 
133 #endif  // QUICHE_QUIC_TEST_TOOLS_FIRST_FLIGHT_H_
134