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 | |
| 5 | #include "net/third_party/quiche/src/quic/test_tools/first_flight.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_client_config.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/http/quic_client_push_promise_index.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/http/quic_spdy_client_session.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_config.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_packet_writer.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_ip_address.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
| 21 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 22 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 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_, |
| 49 | QuicSocketAddress(TestPeerIPAddress(), kTestPort), |
| 50 | &connection_helper_, &alarm_factory_, &writer_, |
| 51 | /*owns_writer=*/false, Perspective::IS_CLIENT, |
| 52 | ParsedQuicVersionVector{version_}); |
| 53 | connection_->set_client_connection_id(client_connection_id_); |
| 54 | session_ = std::make_unique<QuicSpdyClientSession>( |
| 55 | config_, ParsedQuicVersionVector{version_}, |
| 56 | connection_, // session_ takes ownership of connection_ here. |
| 57 | TestServerId(), &crypto_config_, &push_promise_index_); |
| 58 | session_->Initialize(); |
| 59 | session_->CryptoConnect(); |
| 60 | } |
| 61 | |
| 62 | void OnDelegatedPacket(const char* buffer, |
| 63 | size_t buf_len, |
| 64 | const QuicIpAddress& /*self_client_address*/, |
| 65 | const QuicSocketAddress& /*peer_client_address*/, |
| 66 | PerPacketOptions* /*options*/) override { |
| 67 | packets_.emplace_back( |
| 68 | QuicReceivedPacket(buffer, buf_len, |
| 69 | connection_helper_.GetClock()->ApproximateNow(), |
| 70 | /*owns_buffer=*/false) |
| 71 | .Clone()); |
| 72 | } |
| 73 | |
| 74 | std::vector<std::unique_ptr<QuicReceivedPacket>>&& ConsumePackets() { |
| 75 | return std::move(packets_); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | ParsedQuicVersion version_; |
| 80 | QuicConnectionId server_connection_id_; |
| 81 | QuicConnectionId client_connection_id_; |
| 82 | MockQuicConnectionHelper connection_helper_; |
| 83 | MockAlarmFactory alarm_factory_; |
| 84 | DelegatedPacketWriter writer_; |
| 85 | QuicConfig config_; |
| 86 | QuicCryptoClientConfig crypto_config_; |
| 87 | QuicClientPushPromiseIndex push_promise_index_; |
| 88 | QuicConnection* connection_; // Owned by session_. |
| 89 | std::unique_ptr<QuicSpdyClientSession> session_; |
| 90 | std::vector<std::unique_ptr<QuicReceivedPacket>> packets_; |
| 91 | }; |
| 92 | |
| 93 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 94 | const ParsedQuicVersion& version, |
| 95 | const QuicConfig& config, |
| 96 | const QuicConnectionId& server_connection_id, |
| 97 | const QuicConnectionId& client_connection_id) { |
| 98 | FirstFlightExtractor first_flight_extractor( |
| 99 | version, config, server_connection_id, client_connection_id); |
| 100 | first_flight_extractor.GenerateFirstFlight(); |
| 101 | return first_flight_extractor.ConsumePackets(); |
| 102 | } |
| 103 | |
| 104 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 105 | const ParsedQuicVersion& version, |
| 106 | const QuicConfig& config, |
| 107 | const QuicConnectionId& server_connection_id) { |
| 108 | return GetFirstFlightOfPackets(version, config, server_connection_id, |
| 109 | EmptyQuicConnectionId()); |
| 110 | } |
| 111 | |
| 112 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 113 | const ParsedQuicVersion& version, |
| 114 | const QuicConfig& config) { |
| 115 | return GetFirstFlightOfPackets(version, config, TestConnectionId()); |
| 116 | } |
| 117 | |
| 118 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 119 | const ParsedQuicVersion& version, |
| 120 | const QuicConnectionId& server_connection_id, |
| 121 | const QuicConnectionId& client_connection_id) { |
| 122 | return GetFirstFlightOfPackets(version, DefaultQuicConfig(), |
| 123 | server_connection_id, client_connection_id); |
| 124 | } |
| 125 | |
| 126 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 127 | const ParsedQuicVersion& version, |
| 128 | const QuicConnectionId& server_connection_id) { |
| 129 | return GetFirstFlightOfPackets(version, DefaultQuicConfig(), |
| 130 | server_connection_id, EmptyQuicConnectionId()); |
| 131 | } |
| 132 | |
| 133 | std::vector<std::unique_ptr<QuicReceivedPacket>> GetFirstFlightOfPackets( |
| 134 | const ParsedQuicVersion& version) { |
| 135 | return GetFirstFlightOfPackets(version, DefaultQuicConfig(), |
| 136 | TestConnectionId()); |
| 137 | } |
| 138 | |
| 139 | } // namespace test |
| 140 | } // namespace quic |