QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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/quic_test_utils.h" |
| 6 | |
| 7 | #include <algorithm> |
vasilvv | c201848 | 2019-04-26 15:47:55 -0700 | [diff] [blame] | 8 | #include <cstdint> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <memory> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 10 | #include <utility> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
vasilvv | c201848 | 2019-04-26 15:47:55 -0700 | [diff] [blame] | 12 | #include "third_party/boringssl/src/include/openssl/chacha.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "third_party/boringssl/src/include/openssl/sha.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/core/http/quic_spdy_client_session.h" |
vasilvv | 2b0ab24 | 2020-01-07 07:32:09 -0800 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/quic/core/quic_buffer_allocator.h" |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quic/core/quic_config.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/core/quic_data_writer.h" |
| 24 | #include "net/third_party/quiche/src/quic/core/quic_framer.h" |
| 25 | #include "net/third_party/quiche/src/quic/core/quic_packet_creator.h" |
vasilvv | 2b0ab24 | 2020-01-07 07:32:09 -0800 | [diff] [blame] | 26 | #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 27 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 29 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 31 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 32 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
| 33 | #include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h" |
| 34 | #include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 35 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 36 | #include "net/third_party/quiche/src/common/platform/api/quiche_endian.h" |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 37 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | #include "net/third_party/quiche/src/spdy/core/spdy_frame_builder.h" |
| 39 | |
| 40 | using testing::_; |
| 41 | using testing::Invoke; |
| 42 | |
| 43 | namespace quic { |
| 44 | namespace test { |
| 45 | |
| 46 | QuicConnectionId TestConnectionId() { |
| 47 | // Chosen by fair dice roll. |
| 48 | // Guaranteed to be random. |
| 49 | return TestConnectionId(42); |
| 50 | } |
| 51 | |
| 52 | QuicConnectionId TestConnectionId(uint64_t connection_number) { |
| 53 | const uint64_t connection_id64_net = |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 54 | quiche::QuicheEndian::HostToNet64(connection_number); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | return QuicConnectionId(reinterpret_cast<const char*>(&connection_id64_net), |
| 56 | sizeof(connection_id64_net)); |
| 57 | } |
| 58 | |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 59 | QuicConnectionId TestConnectionIdNineBytesLong(uint64_t connection_number) { |
| 60 | const uint64_t connection_number_net = |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 61 | quiche::QuicheEndian::HostToNet64(connection_number); |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 62 | char connection_id_bytes[9] = {}; |
| 63 | static_assert( |
| 64 | sizeof(connection_id_bytes) == 1 + sizeof(connection_number_net), |
| 65 | "bad lengths"); |
| 66 | memcpy(connection_id_bytes + 1, &connection_number_net, |
| 67 | sizeof(connection_number_net)); |
| 68 | return QuicConnectionId(connection_id_bytes, sizeof(connection_id_bytes)); |
| 69 | } |
| 70 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | uint64_t TestConnectionIdToUInt64(QuicConnectionId connection_id) { |
| 72 | DCHECK_EQ(connection_id.length(), kQuicDefaultConnectionIdLength); |
| 73 | uint64_t connection_id64_net = 0; |
| 74 | memcpy(&connection_id64_net, connection_id.data(), |
| 75 | std::min<size_t>(static_cast<size_t>(connection_id.length()), |
| 76 | sizeof(connection_id64_net))); |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 77 | return quiche::QuicheEndian::NetToHost64(connection_id64_net); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 78 | } |
| 79 | |
dschinazi | 18f1067 | 2020-04-21 16:29:34 -0700 | [diff] [blame] | 80 | std::string TestHostname() { |
| 81 | return "test.example.org"; |
| 82 | } |
| 83 | |
| 84 | QuicServerId TestServerId() { |
| 85 | return QuicServerId(TestHostname(), kTestPort); |
| 86 | } |
| 87 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | QuicAckFrame InitAckFrame(const std::vector<QuicAckBlock>& ack_blocks) { |
| 89 | DCHECK_GT(ack_blocks.size(), 0u); |
| 90 | |
| 91 | QuicAckFrame ack; |
| 92 | QuicPacketNumber end_of_previous_block(1); |
| 93 | for (const QuicAckBlock& block : ack_blocks) { |
| 94 | DCHECK_GE(block.start, end_of_previous_block); |
| 95 | DCHECK_GT(block.limit, block.start); |
| 96 | ack.packets.AddRange(block.start, block.limit); |
| 97 | end_of_previous_block = block.limit; |
| 98 | } |
| 99 | |
| 100 | ack.largest_acked = ack.packets.Max(); |
| 101 | |
| 102 | return ack; |
| 103 | } |
| 104 | |
| 105 | QuicAckFrame InitAckFrame(uint64_t largest_acked) { |
| 106 | return InitAckFrame(QuicPacketNumber(largest_acked)); |
| 107 | } |
| 108 | |
| 109 | QuicAckFrame InitAckFrame(QuicPacketNumber largest_acked) { |
| 110 | return InitAckFrame({{QuicPacketNumber(1), largest_acked + 1}}); |
| 111 | } |
| 112 | |
| 113 | QuicAckFrame MakeAckFrameWithAckBlocks(size_t num_ack_blocks, |
| 114 | uint64_t least_unacked) { |
| 115 | QuicAckFrame ack; |
| 116 | ack.largest_acked = QuicPacketNumber(2 * num_ack_blocks + least_unacked); |
| 117 | // Add enough received packets to get num_ack_blocks ack blocks. |
| 118 | for (QuicPacketNumber i = QuicPacketNumber(2); |
| 119 | i < QuicPacketNumber(2 * num_ack_blocks + 1); i += 2) { |
| 120 | ack.packets.Add(i + least_unacked); |
| 121 | } |
| 122 | return ack; |
| 123 | } |
| 124 | |
fayang | 91c23d7 | 2020-03-09 12:35:05 -0700 | [diff] [blame] | 125 | QuicAckFrame MakeAckFrameWithGaps(uint64_t gap_size, |
| 126 | size_t max_num_gaps, |
| 127 | uint64_t largest_acked) { |
| 128 | QuicAckFrame ack; |
| 129 | ack.largest_acked = QuicPacketNumber(largest_acked); |
| 130 | ack.packets.Add(QuicPacketNumber(largest_acked)); |
| 131 | for (size_t i = 0; i < max_num_gaps; ++i) { |
| 132 | if (largest_acked <= gap_size) { |
| 133 | break; |
| 134 | } |
| 135 | largest_acked -= gap_size; |
| 136 | ack.packets.Add(QuicPacketNumber(largest_acked)); |
| 137 | } |
| 138 | return ack; |
| 139 | } |
| 140 | |
nharper | 965e592 | 2019-09-23 22:33:54 -0700 | [diff] [blame] | 141 | EncryptionLevel HeaderToEncryptionLevel(const QuicPacketHeader& header) { |
| 142 | if (header.form == IETF_QUIC_SHORT_HEADER_PACKET) { |
| 143 | return ENCRYPTION_FORWARD_SECURE; |
| 144 | } else if (header.form == IETF_QUIC_LONG_HEADER_PACKET) { |
| 145 | if (header.long_packet_type == HANDSHAKE) { |
| 146 | return ENCRYPTION_HANDSHAKE; |
| 147 | } else if (header.long_packet_type == ZERO_RTT_PROTECTED) { |
| 148 | return ENCRYPTION_ZERO_RTT; |
| 149 | } |
| 150 | } |
| 151 | return ENCRYPTION_INITIAL; |
| 152 | } |
| 153 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | std::unique_ptr<QuicPacket> BuildUnsizedDataPacket( |
| 155 | QuicFramer* framer, |
| 156 | const QuicPacketHeader& header, |
| 157 | const QuicFrames& frames) { |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 158 | const size_t max_plaintext_size = |
| 159 | framer->GetMaxPlaintextSize(kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 160 | size_t packet_size = GetPacketHeaderSize(framer->transport_version(), header); |
| 161 | for (size_t i = 0; i < frames.size(); ++i) { |
| 162 | DCHECK_LE(packet_size, max_plaintext_size); |
| 163 | bool first_frame = i == 0; |
| 164 | bool last_frame = i == frames.size() - 1; |
| 165 | const size_t frame_size = framer->GetSerializedFrameLength( |
| 166 | frames[i], max_plaintext_size - packet_size, first_frame, last_frame, |
| 167 | header.packet_number_length); |
| 168 | DCHECK(frame_size); |
| 169 | packet_size += frame_size; |
| 170 | } |
| 171 | return BuildUnsizedDataPacket(framer, header, frames, packet_size); |
| 172 | } |
| 173 | |
| 174 | std::unique_ptr<QuicPacket> BuildUnsizedDataPacket( |
| 175 | QuicFramer* framer, |
| 176 | const QuicPacketHeader& header, |
| 177 | const QuicFrames& frames, |
| 178 | size_t packet_size) { |
| 179 | char* buffer = new char[packet_size]; |
nharper | 965e592 | 2019-09-23 22:33:54 -0700 | [diff] [blame] | 180 | EncryptionLevel level = HeaderToEncryptionLevel(header); |
nharper | c6b9951 | 2019-09-19 11:13:48 -0700 | [diff] [blame] | 181 | size_t length = |
| 182 | framer->BuildDataPacket(header, frames, buffer, packet_size, level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | DCHECK_NE(0u, length); |
| 184 | // Re-construct the data packet with data ownership. |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 185 | return std::make_unique<QuicPacket>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 186 | buffer, length, /* owns_buffer */ true, |
| 187 | GetIncludedDestinationConnectionIdLength(header), |
| 188 | GetIncludedSourceConnectionIdLength(header), header.version_flag, |
| 189 | header.nonce != nullptr, header.packet_number_length, |
| 190 | header.retry_token_length_length, header.retry_token.length(), |
| 191 | header.length_length); |
| 192 | } |
| 193 | |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 194 | std::string Sha1Hash(quiche::QuicheStringPiece data) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 195 | char buffer[SHA_DIGEST_LENGTH]; |
| 196 | SHA1(reinterpret_cast<const uint8_t*>(data.data()), data.size(), |
| 197 | reinterpret_cast<uint8_t*>(buffer)); |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 198 | return std::string(buffer, QUICHE_ARRAYSIZE(buffer)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 199 | } |
| 200 | |
bnc | 5b3c3be | 2019-06-25 10:37:09 -0700 | [diff] [blame] | 201 | bool ClearControlFrame(const QuicFrame& frame) { |
| 202 | DeleteFrame(&const_cast<QuicFrame&>(frame)); |
| 203 | return true; |
| 204 | } |
| 205 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 206 | uint64_t SimpleRandom::RandUint64() { |
vasilvv | c201848 | 2019-04-26 15:47:55 -0700 | [diff] [blame] | 207 | uint64_t result; |
| 208 | RandBytes(&result, sizeof(result)); |
| 209 | return result; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void SimpleRandom::RandBytes(void* data, size_t len) { |
vasilvv | c201848 | 2019-04-26 15:47:55 -0700 | [diff] [blame] | 213 | uint8_t* data_bytes = reinterpret_cast<uint8_t*>(data); |
| 214 | while (len > 0) { |
| 215 | const size_t buffer_left = sizeof(buffer_) - buffer_offset_; |
| 216 | const size_t to_copy = std::min(buffer_left, len); |
| 217 | memcpy(data_bytes, buffer_ + buffer_offset_, to_copy); |
| 218 | data_bytes += to_copy; |
| 219 | buffer_offset_ += to_copy; |
| 220 | len -= to_copy; |
| 221 | |
| 222 | if (buffer_offset_ == sizeof(buffer_)) { |
| 223 | FillBuffer(); |
| 224 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
vasilvv | c201848 | 2019-04-26 15:47:55 -0700 | [diff] [blame] | 228 | void SimpleRandom::FillBuffer() { |
| 229 | uint8_t nonce[12]; |
| 230 | memcpy(nonce, buffer_, sizeof(nonce)); |
| 231 | CRYPTO_chacha_20(buffer_, buffer_, sizeof(buffer_), key_, nonce, 0); |
| 232 | buffer_offset_ = 0; |
| 233 | } |
| 234 | |
| 235 | void SimpleRandom::set_seed(uint64_t seed) { |
| 236 | static_assert(sizeof(key_) == SHA256_DIGEST_LENGTH, "Key has to be 256 bits"); |
| 237 | SHA256(reinterpret_cast<const uint8_t*>(&seed), sizeof(seed), key_); |
| 238 | |
| 239 | memset(buffer_, 0, sizeof(buffer_)); |
| 240 | FillBuffer(); |
| 241 | } |
| 242 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 243 | MockFramerVisitor::MockFramerVisitor() { |
| 244 | // By default, we want to accept packets. |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 245 | ON_CALL(*this, OnProtocolVersionMismatch(_)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 246 | .WillByDefault(testing::Return(false)); |
| 247 | |
| 248 | // By default, we want to accept packets. |
| 249 | ON_CALL(*this, OnUnauthenticatedHeader(_)) |
| 250 | .WillByDefault(testing::Return(true)); |
| 251 | |
| 252 | ON_CALL(*this, OnUnauthenticatedPublicHeader(_)) |
| 253 | .WillByDefault(testing::Return(true)); |
| 254 | |
| 255 | ON_CALL(*this, OnPacketHeader(_)).WillByDefault(testing::Return(true)); |
| 256 | |
| 257 | ON_CALL(*this, OnStreamFrame(_)).WillByDefault(testing::Return(true)); |
| 258 | |
| 259 | ON_CALL(*this, OnCryptoFrame(_)).WillByDefault(testing::Return(true)); |
| 260 | |
| 261 | ON_CALL(*this, OnStopWaitingFrame(_)).WillByDefault(testing::Return(true)); |
| 262 | |
| 263 | ON_CALL(*this, OnPaddingFrame(_)).WillByDefault(testing::Return(true)); |
| 264 | |
| 265 | ON_CALL(*this, OnPingFrame(_)).WillByDefault(testing::Return(true)); |
| 266 | |
| 267 | ON_CALL(*this, OnRstStreamFrame(_)).WillByDefault(testing::Return(true)); |
| 268 | |
| 269 | ON_CALL(*this, OnConnectionCloseFrame(_)) |
| 270 | .WillByDefault(testing::Return(true)); |
| 271 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 272 | ON_CALL(*this, OnStopSendingFrame(_)).WillByDefault(testing::Return(true)); |
| 273 | |
| 274 | ON_CALL(*this, OnPathChallengeFrame(_)).WillByDefault(testing::Return(true)); |
| 275 | |
| 276 | ON_CALL(*this, OnPathResponseFrame(_)).WillByDefault(testing::Return(true)); |
| 277 | |
| 278 | ON_CALL(*this, OnGoAwayFrame(_)).WillByDefault(testing::Return(true)); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 279 | ON_CALL(*this, OnMaxStreamsFrame(_)).WillByDefault(testing::Return(true)); |
| 280 | ON_CALL(*this, OnStreamsBlockedFrame(_)).WillByDefault(testing::Return(true)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | MockFramerVisitor::~MockFramerVisitor() {} |
| 284 | |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 285 | bool NoOpFramerVisitor::OnProtocolVersionMismatch( |
| 286 | ParsedQuicVersion /*version*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 291 | const QuicPacketHeader& /*header*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool NoOpFramerVisitor::OnUnauthenticatedHeader( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 296 | const QuicPacketHeader& /*header*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 300 | bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& /*header*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 304 | void NoOpFramerVisitor::OnCoalescedPacket( |
| 305 | const QuicEncryptedPacket& /*packet*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 306 | |
dschinazi | 4b5a68a | 2019-08-15 15:45:36 -0700 | [diff] [blame] | 307 | void NoOpFramerVisitor::OnUndecryptablePacket( |
| 308 | const QuicEncryptedPacket& /*packet*/, |
| 309 | EncryptionLevel /*decryption_level*/, |
| 310 | bool /*has_decryption_key*/) {} |
| 311 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 312 | bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 313 | return true; |
| 314 | } |
| 315 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 316 | bool NoOpFramerVisitor::OnCryptoFrame(const QuicCryptoFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 320 | bool NoOpFramerVisitor::OnAckFrameStart(QuicPacketNumber /*largest_acked*/, |
| 321 | QuicTime::Delta /*ack_delay_time*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 325 | bool NoOpFramerVisitor::OnAckRange(QuicPacketNumber /*start*/, |
| 326 | QuicPacketNumber /*end*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 330 | bool NoOpFramerVisitor::OnAckTimestamp(QuicPacketNumber /*packet_number*/, |
| 331 | QuicTime /*timestamp*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 332 | return true; |
| 333 | } |
| 334 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 335 | bool NoOpFramerVisitor::OnAckFrameEnd(QuicPacketNumber /*start*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 339 | bool NoOpFramerVisitor::OnStopWaitingFrame( |
| 340 | const QuicStopWaitingFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 344 | bool NoOpFramerVisitor::OnPaddingFrame(const QuicPaddingFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 348 | bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 352 | bool NoOpFramerVisitor::OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
| 356 | bool NoOpFramerVisitor::OnConnectionCloseFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 357 | const QuicConnectionCloseFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 361 | bool NoOpFramerVisitor::OnNewConnectionIdFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 362 | const QuicNewConnectionIdFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
| 366 | bool NoOpFramerVisitor::OnRetireConnectionIdFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 367 | const QuicRetireConnectionIdFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 368 | return true; |
| 369 | } |
| 370 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 371 | bool NoOpFramerVisitor::OnNewTokenFrame(const QuicNewTokenFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 375 | bool NoOpFramerVisitor::OnStopSendingFrame( |
| 376 | const QuicStopSendingFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
| 380 | bool NoOpFramerVisitor::OnPathChallengeFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 381 | const QuicPathChallengeFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | |
| 385 | bool NoOpFramerVisitor::OnPathResponseFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 386 | const QuicPathResponseFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 390 | bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 394 | bool NoOpFramerVisitor::OnMaxStreamsFrame( |
| 395 | const QuicMaxStreamsFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 396 | return true; |
| 397 | } |
| 398 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 399 | bool NoOpFramerVisitor::OnStreamsBlockedFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 400 | const QuicStreamsBlockedFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | |
| 404 | bool NoOpFramerVisitor::OnWindowUpdateFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 405 | const QuicWindowUpdateFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 409 | bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 413 | bool NoOpFramerVisitor::OnMessageFrame(const QuicMessageFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 414 | return true; |
| 415 | } |
| 416 | |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 417 | bool NoOpFramerVisitor::OnHandshakeDoneFrame( |
| 418 | const QuicHandshakeDoneFrame& /*frame*/) { |
| 419 | return true; |
| 420 | } |
| 421 | |
haoyuewang | 6a6a0ff | 2020-06-23 16:32:26 -0700 | [diff] [blame] | 422 | bool NoOpFramerVisitor::OnAckFrequencyFrame( |
| 423 | const QuicAckFrequencyFrame& /*frame*/) { |
| 424 | return true; |
| 425 | } |
| 426 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 427 | bool NoOpFramerVisitor::IsValidStatelessResetToken( |
| 428 | QuicUint128 /*token*/) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | |
| 432 | MockQuicConnectionVisitor::MockQuicConnectionVisitor() {} |
| 433 | |
| 434 | MockQuicConnectionVisitor::~MockQuicConnectionVisitor() {} |
| 435 | |
| 436 | MockQuicConnectionHelper::MockQuicConnectionHelper() {} |
| 437 | |
| 438 | MockQuicConnectionHelper::~MockQuicConnectionHelper() {} |
| 439 | |
| 440 | const QuicClock* MockQuicConnectionHelper::GetClock() const { |
| 441 | return &clock_; |
| 442 | } |
| 443 | |
| 444 | QuicRandom* MockQuicConnectionHelper::GetRandomGenerator() { |
| 445 | return &random_generator_; |
| 446 | } |
| 447 | |
| 448 | QuicAlarm* MockAlarmFactory::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 449 | return new MockAlarmFactory::TestAlarm( |
| 450 | QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); |
| 451 | } |
| 452 | |
| 453 | QuicArenaScopedPtr<QuicAlarm> MockAlarmFactory::CreateAlarm( |
| 454 | QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
| 455 | QuicConnectionArena* arena) { |
| 456 | if (arena != nullptr) { |
| 457 | return arena->New<TestAlarm>(std::move(delegate)); |
| 458 | } else { |
| 459 | return QuicArenaScopedPtr<TestAlarm>(new TestAlarm(std::move(delegate))); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | QuicBufferAllocator* MockQuicConnectionHelper::GetStreamSendBufferAllocator() { |
| 464 | return &buffer_allocator_; |
| 465 | } |
| 466 | |
| 467 | void MockQuicConnectionHelper::AdvanceTime(QuicTime::Delta delta) { |
| 468 | clock_.AdvanceTime(delta); |
| 469 | } |
| 470 | |
| 471 | MockQuicConnection::MockQuicConnection(MockQuicConnectionHelper* helper, |
| 472 | MockAlarmFactory* alarm_factory, |
| 473 | Perspective perspective) |
| 474 | : MockQuicConnection(TestConnectionId(), |
| 475 | QuicSocketAddress(TestPeerIPAddress(), kTestPort), |
| 476 | helper, |
| 477 | alarm_factory, |
| 478 | perspective, |
| 479 | ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {} |
| 480 | |
| 481 | MockQuicConnection::MockQuicConnection(QuicSocketAddress address, |
| 482 | MockQuicConnectionHelper* helper, |
| 483 | MockAlarmFactory* alarm_factory, |
| 484 | Perspective perspective) |
| 485 | : MockQuicConnection(TestConnectionId(), |
| 486 | address, |
| 487 | helper, |
| 488 | alarm_factory, |
| 489 | perspective, |
| 490 | ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {} |
| 491 | |
| 492 | MockQuicConnection::MockQuicConnection(QuicConnectionId connection_id, |
| 493 | MockQuicConnectionHelper* helper, |
| 494 | MockAlarmFactory* alarm_factory, |
| 495 | Perspective perspective) |
| 496 | : MockQuicConnection(connection_id, |
| 497 | QuicSocketAddress(TestPeerIPAddress(), kTestPort), |
| 498 | helper, |
| 499 | alarm_factory, |
| 500 | perspective, |
| 501 | ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {} |
| 502 | |
| 503 | MockQuicConnection::MockQuicConnection( |
| 504 | MockQuicConnectionHelper* helper, |
| 505 | MockAlarmFactory* alarm_factory, |
| 506 | Perspective perspective, |
| 507 | const ParsedQuicVersionVector& supported_versions) |
| 508 | : MockQuicConnection(TestConnectionId(), |
| 509 | QuicSocketAddress(TestPeerIPAddress(), kTestPort), |
| 510 | helper, |
| 511 | alarm_factory, |
| 512 | perspective, |
| 513 | supported_versions) {} |
| 514 | |
| 515 | MockQuicConnection::MockQuicConnection( |
| 516 | QuicConnectionId connection_id, |
| 517 | QuicSocketAddress address, |
| 518 | MockQuicConnectionHelper* helper, |
| 519 | MockAlarmFactory* alarm_factory, |
| 520 | Perspective perspective, |
| 521 | const ParsedQuicVersionVector& supported_versions) |
| 522 | : QuicConnection(connection_id, |
| 523 | address, |
| 524 | helper, |
| 525 | alarm_factory, |
| 526 | new testing::NiceMock<MockPacketWriter>(), |
| 527 | /* owns_writer= */ true, |
| 528 | perspective, |
| 529 | supported_versions) { |
| 530 | ON_CALL(*this, OnError(_)) |
| 531 | .WillByDefault( |
| 532 | Invoke(this, &PacketSavingConnection::QuicConnection_OnError)); |
| 533 | ON_CALL(*this, SendCryptoData(_, _, _)) |
| 534 | .WillByDefault( |
| 535 | Invoke(this, &MockQuicConnection::QuicConnection_SendCryptoData)); |
| 536 | |
| 537 | SetSelfAddress(QuicSocketAddress(QuicIpAddress::Any4(), 5)); |
| 538 | } |
| 539 | |
| 540 | MockQuicConnection::~MockQuicConnection() {} |
| 541 | |
| 542 | void MockQuicConnection::AdvanceTime(QuicTime::Delta delta) { |
| 543 | static_cast<MockQuicConnectionHelper*>(helper())->AdvanceTime(delta); |
| 544 | } |
| 545 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 546 | bool MockQuicConnection::OnProtocolVersionMismatch( |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 547 | ParsedQuicVersion /*version*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | |
| 551 | PacketSavingConnection::PacketSavingConnection(MockQuicConnectionHelper* helper, |
| 552 | MockAlarmFactory* alarm_factory, |
| 553 | Perspective perspective) |
| 554 | : MockQuicConnection(helper, alarm_factory, perspective) {} |
| 555 | |
| 556 | PacketSavingConnection::PacketSavingConnection( |
| 557 | MockQuicConnectionHelper* helper, |
| 558 | MockAlarmFactory* alarm_factory, |
| 559 | Perspective perspective, |
| 560 | const ParsedQuicVersionVector& supported_versions) |
| 561 | : MockQuicConnection(helper, |
| 562 | alarm_factory, |
| 563 | perspective, |
| 564 | supported_versions) {} |
| 565 | |
| 566 | PacketSavingConnection::~PacketSavingConnection() {} |
| 567 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 568 | void PacketSavingConnection::SendOrQueuePacket(SerializedPacket packet) { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 569 | encrypted_packets_.push_back(std::make_unique<QuicEncryptedPacket>( |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 570 | CopyBuffer(packet), packet.encrypted_length, true)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 571 | clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 572 | // Transfer ownership of the packet to the SentPacketManager and the |
| 573 | // ack notifier to the AckNotifierManager. |
renjietang | e7b2cd8 | 2020-06-03 16:59:18 -0700 | [diff] [blame] | 574 | OnPacketSent(packet.encryption_level, packet.transmission_type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 575 | QuicConnectionPeer::GetSentPacketManager(this)->OnPacketSent( |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 576 | &packet, clock_.ApproximateNow(), NOT_RETRANSMISSION, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 577 | HAS_RETRANSMITTABLE_DATA); |
| 578 | } |
| 579 | |
| 580 | MockQuicSession::MockQuicSession(QuicConnection* connection) |
| 581 | : MockQuicSession(connection, true) {} |
| 582 | |
| 583 | MockQuicSession::MockQuicSession(QuicConnection* connection, |
| 584 | bool create_mock_crypto_stream) |
| 585 | : QuicSession(connection, |
| 586 | nullptr, |
| 587 | DefaultQuicConfig(), |
renjietang | 216dc01 | 2019-08-27 11:28:27 -0700 | [diff] [blame] | 588 | connection->supported_versions(), |
| 589 | /*num_expected_unidirectional_static_streams = */ 0) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 590 | if (create_mock_crypto_stream) { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 591 | crypto_stream_ = std::make_unique<MockQuicCryptoStream>(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 592 | } |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 593 | ON_CALL(*this, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 594 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
| 595 | } |
| 596 | |
| 597 | MockQuicSession::~MockQuicSession() { |
ianswett | 6aefa0b | 2019-12-10 07:26:15 -0800 | [diff] [blame] | 598 | DeleteConnection(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | QuicCryptoStream* MockQuicSession::GetMutableCryptoStream() { |
| 602 | return crypto_stream_.get(); |
| 603 | } |
| 604 | |
| 605 | const QuicCryptoStream* MockQuicSession::GetCryptoStream() const { |
| 606 | return crypto_stream_.get(); |
| 607 | } |
| 608 | |
| 609 | void MockQuicSession::SetCryptoStream(QuicCryptoStream* crypto_stream) { |
| 610 | crypto_stream_.reset(crypto_stream); |
| 611 | } |
| 612 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 613 | QuicConsumedData MockQuicSession::ConsumeData( |
| 614 | QuicStreamId id, |
| 615 | size_t write_length, |
| 616 | QuicStreamOffset offset, |
| 617 | StreamSendingState state, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 618 | TransmissionType /*type*/, |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 619 | quiche::QuicheOptional<EncryptionLevel> /*level*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 620 | if (write_length > 0) { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 621 | auto buf = std::make_unique<char[]>(write_length); |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 622 | QuicStream* stream = GetOrCreateStream(id); |
| 623 | DCHECK(stream); |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 624 | QuicDataWriter writer(write_length, buf.get(), quiche::HOST_BYTE_ORDER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 625 | stream->WriteStreamData(offset, write_length, &writer); |
| 626 | } else { |
| 627 | DCHECK(state != NO_FIN); |
| 628 | } |
| 629 | return QuicConsumedData(write_length, state != NO_FIN); |
| 630 | } |
| 631 | |
| 632 | MockQuicCryptoStream::MockQuicCryptoStream(QuicSession* session) |
| 633 | : QuicCryptoStream(session), params_(new QuicCryptoNegotiatedParameters) {} |
| 634 | |
| 635 | MockQuicCryptoStream::~MockQuicCryptoStream() {} |
| 636 | |
| 637 | bool MockQuicCryptoStream::encryption_established() const { |
| 638 | return false; |
| 639 | } |
| 640 | |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 641 | bool MockQuicCryptoStream::one_rtt_keys_available() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | |
| 645 | const QuicCryptoNegotiatedParameters& |
| 646 | MockQuicCryptoStream::crypto_negotiated_params() const { |
| 647 | return *params_; |
| 648 | } |
| 649 | |
| 650 | CryptoMessageParser* MockQuicCryptoStream::crypto_message_parser() { |
| 651 | return &crypto_framer_; |
| 652 | } |
| 653 | |
| 654 | MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection) |
| 655 | : MockQuicSpdySession(connection, true) {} |
| 656 | |
| 657 | MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection, |
| 658 | bool create_mock_crypto_stream) |
| 659 | : QuicSpdySession(connection, |
| 660 | nullptr, |
| 661 | DefaultQuicConfig(), |
| 662 | connection->supported_versions()) { |
| 663 | if (create_mock_crypto_stream) { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 664 | crypto_stream_ = std::make_unique<MockQuicCryptoStream>(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 665 | } |
| 666 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 667 | ON_CALL(*this, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 668 | .WillByDefault(testing::Return(QuicConsumedData(0, false))); |
wub | 713afae | 2020-04-27 07:48:31 -0700 | [diff] [blame] | 669 | |
| 670 | ON_CALL(*this, SendWindowUpdate(_, _)) |
| 671 | .WillByDefault([this](QuicStreamId id, QuicStreamOffset byte_offset) { |
| 672 | return QuicSpdySession::SendWindowUpdate(id, byte_offset); |
| 673 | }); |
| 674 | |
| 675 | ON_CALL(*this, SendBlocked(_)).WillByDefault([this](QuicStreamId id) { |
| 676 | return QuicSpdySession::SendBlocked(id); |
| 677 | }); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | MockQuicSpdySession::~MockQuicSpdySession() { |
ianswett | 6aefa0b | 2019-12-10 07:26:15 -0800 | [diff] [blame] | 681 | DeleteConnection(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | QuicCryptoStream* MockQuicSpdySession::GetMutableCryptoStream() { |
| 685 | return crypto_stream_.get(); |
| 686 | } |
| 687 | |
| 688 | const QuicCryptoStream* MockQuicSpdySession::GetCryptoStream() const { |
| 689 | return crypto_stream_.get(); |
| 690 | } |
| 691 | |
| 692 | void MockQuicSpdySession::SetCryptoStream(QuicCryptoStream* crypto_stream) { |
| 693 | crypto_stream_.reset(crypto_stream); |
| 694 | } |
| 695 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 696 | QuicConsumedData MockQuicSpdySession::ConsumeData( |
| 697 | QuicStreamId id, |
| 698 | size_t write_length, |
| 699 | QuicStreamOffset offset, |
| 700 | StreamSendingState state, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 701 | TransmissionType /*type*/, |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 702 | quiche::QuicheOptional<EncryptionLevel> /*level*/) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 703 | if (write_length > 0) { |
| 704 | auto buf = std::make_unique<char[]>(write_length); |
| 705 | QuicStream* stream = GetOrCreateStream(id); |
| 706 | DCHECK(stream); |
| 707 | QuicDataWriter writer(write_length, buf.get(), quiche::HOST_BYTE_ORDER); |
| 708 | stream->WriteStreamData(offset, write_length, &writer); |
| 709 | } else { |
| 710 | DCHECK(state != NO_FIN); |
| 711 | } |
| 712 | return QuicConsumedData(write_length, state != NO_FIN); |
| 713 | } |
| 714 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 715 | TestQuicSpdyServerSession::TestQuicSpdyServerSession( |
| 716 | QuicConnection* connection, |
| 717 | const QuicConfig& config, |
| 718 | const ParsedQuicVersionVector& supported_versions, |
| 719 | const QuicCryptoServerConfig* crypto_config, |
| 720 | QuicCompressedCertsCache* compressed_certs_cache) |
| 721 | : QuicServerSessionBase(config, |
| 722 | supported_versions, |
| 723 | connection, |
| 724 | &visitor_, |
| 725 | &helper_, |
| 726 | crypto_config, |
| 727 | compressed_certs_cache) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 728 | ON_CALL(helper_, CanAcceptClientHello(_, _, _, _, _)) |
| 729 | .WillByDefault(testing::Return(true)); |
| 730 | } |
| 731 | |
| 732 | TestQuicSpdyServerSession::~TestQuicSpdyServerSession() { |
ianswett | 6aefa0b | 2019-12-10 07:26:15 -0800 | [diff] [blame] | 733 | DeleteConnection(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 734 | } |
| 735 | |
nharper | e5e28f9 | 2020-01-03 14:10:07 -0800 | [diff] [blame] | 736 | std::unique_ptr<QuicCryptoServerStreamBase> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 737 | TestQuicSpdyServerSession::CreateQuicCryptoServerStream( |
| 738 | const QuicCryptoServerConfig* crypto_config, |
| 739 | QuicCompressedCertsCache* compressed_certs_cache) { |
nharper | e5e28f9 | 2020-01-03 14:10:07 -0800 | [diff] [blame] | 740 | return CreateCryptoServerStream(crypto_config, compressed_certs_cache, this, |
| 741 | &helper_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 742 | } |
| 743 | |
nharper | f579b5e | 2020-01-21 14:11:18 -0800 | [diff] [blame] | 744 | QuicCryptoServerStreamBase* |
| 745 | TestQuicSpdyServerSession::GetMutableCryptoStream() { |
| 746 | return QuicServerSessionBase::GetMutableCryptoStream(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 747 | } |
| 748 | |
nharper | f579b5e | 2020-01-21 14:11:18 -0800 | [diff] [blame] | 749 | const QuicCryptoServerStreamBase* TestQuicSpdyServerSession::GetCryptoStream() |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 750 | const { |
nharper | f579b5e | 2020-01-21 14:11:18 -0800 | [diff] [blame] | 751 | return QuicServerSessionBase::GetCryptoStream(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | TestQuicSpdyClientSession::TestQuicSpdyClientSession( |
| 755 | QuicConnection* connection, |
| 756 | const QuicConfig& config, |
| 757 | const ParsedQuicVersionVector& supported_versions, |
| 758 | const QuicServerId& server_id, |
| 759 | QuicCryptoClientConfig* crypto_config) |
| 760 | : QuicSpdyClientSessionBase(connection, |
| 761 | &push_promise_index_, |
| 762 | config, |
| 763 | supported_versions) { |
nharper | ac52a86 | 2020-06-08 12:41:06 -0700 | [diff] [blame] | 764 | // TODO(b/153726130): Consider adding SetServerApplicationStateForResumption |
| 765 | // calls in tests and set |has_application_state| to true. |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 766 | crypto_stream_ = std::make_unique<QuicCryptoClientStream>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 767 | server_id, this, crypto_test_utils::ProofVerifyContextForTesting(), |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 768 | crypto_config, this, /*has_application_state = */ false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 769 | Initialize(); |
| 770 | } |
| 771 | |
| 772 | TestQuicSpdyClientSession::~TestQuicSpdyClientSession() {} |
| 773 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 774 | bool TestQuicSpdyClientSession::IsAuthorized(const std::string& /*authority*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 775 | return true; |
| 776 | } |
| 777 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 778 | QuicCryptoClientStream* TestQuicSpdyClientSession::GetMutableCryptoStream() { |
| 779 | return crypto_stream_.get(); |
| 780 | } |
| 781 | |
| 782 | const QuicCryptoClientStream* TestQuicSpdyClientSession::GetCryptoStream() |
| 783 | const { |
| 784 | return crypto_stream_.get(); |
| 785 | } |
| 786 | |
| 787 | TestPushPromiseDelegate::TestPushPromiseDelegate(bool match) |
| 788 | : match_(match), rendezvous_fired_(false), rendezvous_stream_(nullptr) {} |
| 789 | |
| 790 | bool TestPushPromiseDelegate::CheckVary( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 791 | const spdy::SpdyHeaderBlock& /*client_request*/, |
| 792 | const spdy::SpdyHeaderBlock& /*promise_request*/, |
| 793 | const spdy::SpdyHeaderBlock& /*promise_response*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 794 | QUIC_DVLOG(1) << "match " << match_; |
| 795 | return match_; |
| 796 | } |
| 797 | |
| 798 | void TestPushPromiseDelegate::OnRendezvousResult(QuicSpdyStream* stream) { |
| 799 | rendezvous_fired_ = true; |
| 800 | rendezvous_stream_ = stream; |
| 801 | } |
| 802 | |
| 803 | MockPacketWriter::MockPacketWriter() { |
| 804 | ON_CALL(*this, GetMaxPacketSize(_)) |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 805 | .WillByDefault(testing::Return(kMaxOutgoingPacketSize)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 806 | ON_CALL(*this, IsBatchMode()).WillByDefault(testing::Return(false)); |
| 807 | ON_CALL(*this, GetNextWriteLocation(_, _)) |
wub | 50d4c71 | 2020-05-19 15:48:28 -0700 | [diff] [blame] | 808 | .WillByDefault(testing::Return(QuicPacketBuffer())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 809 | ON_CALL(*this, Flush()) |
| 810 | .WillByDefault(testing::Return(WriteResult(WRITE_STATUS_OK, 0))); |
dschinazi | 42fc2da | 2020-04-24 16:19:17 -0700 | [diff] [blame] | 811 | ON_CALL(*this, SupportsReleaseTime()).WillByDefault(testing::Return(false)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | MockPacketWriter::~MockPacketWriter() {} |
| 815 | |
dschinazi | 3cf1d47 | 2020-04-01 14:37:43 -0700 | [diff] [blame] | 816 | MockSendAlgorithm::MockSendAlgorithm() { |
| 817 | ON_CALL(*this, PacingRate(_)) |
| 818 | .WillByDefault(testing::Return(QuicBandwidth::Zero())); |
| 819 | ON_CALL(*this, BandwidthEstimate()) |
| 820 | .WillByDefault(testing::Return(QuicBandwidth::Zero())); |
| 821 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 822 | |
| 823 | MockSendAlgorithm::~MockSendAlgorithm() {} |
| 824 | |
| 825 | MockLossAlgorithm::MockLossAlgorithm() {} |
| 826 | |
| 827 | MockLossAlgorithm::~MockLossAlgorithm() {} |
| 828 | |
| 829 | MockAckListener::MockAckListener() {} |
| 830 | |
| 831 | MockAckListener::~MockAckListener() {} |
| 832 | |
| 833 | MockNetworkChangeVisitor::MockNetworkChangeVisitor() {} |
| 834 | |
| 835 | MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {} |
| 836 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 837 | QuicIpAddress TestPeerIPAddress() { |
| 838 | return QuicIpAddress::Loopback4(); |
| 839 | } |
| 840 | |
| 841 | ParsedQuicVersion QuicVersionMax() { |
| 842 | return AllSupportedVersions().front(); |
| 843 | } |
| 844 | |
| 845 | ParsedQuicVersion QuicVersionMin() { |
| 846 | return AllSupportedVersions().back(); |
| 847 | } |
| 848 | |
dschinazi | c1521a0 | 2020-04-07 09:55:08 -0700 | [diff] [blame] | 849 | void DisableQuicVersionsWithTls() { |
dschinazi | 5a50d93 | 2020-06-17 12:43:36 -0700 | [diff] [blame] | 850 | for (const ParsedQuicVersion& version : AllSupportedVersionsWithTls()) { |
| 851 | QuicDisableVersion(version); |
| 852 | } |
dschinazi | c1521a0 | 2020-04-07 09:55:08 -0700 | [diff] [blame] | 853 | } |
| 854 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 855 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 856 | QuicConnectionId destination_connection_id, |
| 857 | QuicConnectionId source_connection_id, |
| 858 | bool version_flag, |
| 859 | bool reset_flag, |
| 860 | uint64_t packet_number, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 861 | const std::string& data) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 862 | return ConstructEncryptedPacket( |
| 863 | destination_connection_id, source_connection_id, version_flag, reset_flag, |
| 864 | packet_number, data, CONNECTION_ID_PRESENT, CONNECTION_ID_ABSENT, |
| 865 | PACKET_4BYTE_PACKET_NUMBER); |
| 866 | } |
| 867 | |
| 868 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 869 | QuicConnectionId destination_connection_id, |
| 870 | QuicConnectionId source_connection_id, |
| 871 | bool version_flag, |
| 872 | bool reset_flag, |
| 873 | uint64_t packet_number, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 874 | const std::string& data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 875 | QuicConnectionIdIncluded destination_connection_id_included, |
| 876 | QuicConnectionIdIncluded source_connection_id_included, |
| 877 | QuicPacketNumberLength packet_number_length) { |
| 878 | return ConstructEncryptedPacket( |
| 879 | destination_connection_id, source_connection_id, version_flag, reset_flag, |
| 880 | packet_number, data, destination_connection_id_included, |
| 881 | source_connection_id_included, packet_number_length, nullptr); |
| 882 | } |
| 883 | |
| 884 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 885 | QuicConnectionId destination_connection_id, |
| 886 | QuicConnectionId source_connection_id, |
| 887 | bool version_flag, |
| 888 | bool reset_flag, |
| 889 | uint64_t packet_number, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 890 | const std::string& data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 891 | QuicConnectionIdIncluded destination_connection_id_included, |
| 892 | QuicConnectionIdIncluded source_connection_id_included, |
| 893 | QuicPacketNumberLength packet_number_length, |
| 894 | ParsedQuicVersionVector* versions) { |
| 895 | return ConstructEncryptedPacket( |
| 896 | destination_connection_id, source_connection_id, version_flag, reset_flag, |
fayang | e3f2f7b | 2019-09-19 17:01:57 -0700 | [diff] [blame] | 897 | packet_number, data, false, destination_connection_id_included, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 898 | source_connection_id_included, packet_number_length, versions, |
| 899 | Perspective::IS_CLIENT); |
| 900 | } |
fayang | e3f2f7b | 2019-09-19 17:01:57 -0700 | [diff] [blame] | 901 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 902 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 903 | QuicConnectionId destination_connection_id, |
| 904 | QuicConnectionId source_connection_id, |
| 905 | bool version_flag, |
| 906 | bool reset_flag, |
| 907 | uint64_t packet_number, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 908 | const std::string& data, |
fayang | e3f2f7b | 2019-09-19 17:01:57 -0700 | [diff] [blame] | 909 | bool full_padding, |
| 910 | QuicConnectionIdIncluded destination_connection_id_included, |
| 911 | QuicConnectionIdIncluded source_connection_id_included, |
| 912 | QuicPacketNumberLength packet_number_length, |
| 913 | ParsedQuicVersionVector* versions) { |
| 914 | return ConstructEncryptedPacket( |
| 915 | destination_connection_id, source_connection_id, version_flag, reset_flag, |
| 916 | packet_number, data, full_padding, destination_connection_id_included, |
| 917 | source_connection_id_included, packet_number_length, versions, |
| 918 | Perspective::IS_CLIENT); |
| 919 | } |
| 920 | |
| 921 | QuicEncryptedPacket* ConstructEncryptedPacket( |
| 922 | QuicConnectionId destination_connection_id, |
| 923 | QuicConnectionId source_connection_id, |
| 924 | bool version_flag, |
| 925 | bool reset_flag, |
| 926 | uint64_t packet_number, |
| 927 | const std::string& data, |
| 928 | bool full_padding, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 929 | QuicConnectionIdIncluded destination_connection_id_included, |
| 930 | QuicConnectionIdIncluded source_connection_id_included, |
| 931 | QuicPacketNumberLength packet_number_length, |
| 932 | ParsedQuicVersionVector* versions, |
| 933 | Perspective perspective) { |
| 934 | QuicPacketHeader header; |
| 935 | header.destination_connection_id = destination_connection_id; |
| 936 | header.destination_connection_id_included = |
| 937 | destination_connection_id_included; |
| 938 | header.source_connection_id = source_connection_id; |
| 939 | header.source_connection_id_included = source_connection_id_included; |
| 940 | header.version_flag = version_flag; |
| 941 | header.reset_flag = reset_flag; |
| 942 | header.packet_number_length = packet_number_length; |
| 943 | header.packet_number = QuicPacketNumber(packet_number); |
| 944 | ParsedQuicVersionVector supported_versions = CurrentSupportedVersions(); |
| 945 | if (!versions) { |
| 946 | versions = &supported_versions; |
| 947 | } |
dschinazi | 4161684 | 2020-01-21 15:46:11 -0800 | [diff] [blame] | 948 | EXPECT_FALSE(versions->empty()); |
dschinazi | ecad964 | 2019-10-01 10:44:17 -0700 | [diff] [blame] | 949 | ParsedQuicVersion version = (*versions)[0]; |
| 950 | if (QuicVersionHasLongHeaderLengths(version.transport_version) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 951 | version_flag) { |
| 952 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 953 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 954 | } |
| 955 | |
| 956 | QuicFrames frames; |
| 957 | QuicFramer framer(*versions, QuicTime::Zero(), perspective, |
| 958 | kQuicDefaultConnectionIdLength); |
nharper | 7c07528 | 2019-09-18 10:45:32 -0700 | [diff] [blame] | 959 | framer.SetInitialObfuscators(destination_connection_id); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 960 | EncryptionLevel level = |
| 961 | header.version_flag ? ENCRYPTION_INITIAL : ENCRYPTION_FORWARD_SECURE; |
nharper | 7c07528 | 2019-09-18 10:45:32 -0700 | [diff] [blame] | 962 | if (level != ENCRYPTION_INITIAL) { |
| 963 | framer.SetEncrypter(level, std::make_unique<NullEncrypter>(perspective)); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 964 | } |
| 965 | if (!QuicVersionUsesCryptoFrames(version.transport_version)) { |
| 966 | QuicFrame frame( |
| 967 | QuicStreamFrame(QuicUtils::GetCryptoStreamId(version.transport_version), |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 968 | false, 0, quiche::QuicheStringPiece(data))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 969 | frames.push_back(frame); |
| 970 | } else { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 971 | QuicFrame frame(new QuicCryptoFrame(level, 0, data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 972 | frames.push_back(frame); |
| 973 | } |
fayang | e3f2f7b | 2019-09-19 17:01:57 -0700 | [diff] [blame] | 974 | if (full_padding) { |
| 975 | frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 976 | } else { |
| 977 | // We need a minimum number of bytes of encrypted payload. This will |
| 978 | // guarantee that we have at least that much. (It ignores the overhead of |
| 979 | // the stream/crypto framing, so it overpads slightly.) |
| 980 | size_t min_plaintext_size = |
| 981 | QuicPacketCreator::MinPlaintextPacketSize(version); |
| 982 | if (data.length() < min_plaintext_size) { |
| 983 | size_t padding_length = min_plaintext_size - data.length(); |
| 984 | frames.push_back(QuicFrame(QuicPaddingFrame(padding_length))); |
| 985 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 986 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 987 | |
| 988 | std::unique_ptr<QuicPacket> packet( |
| 989 | BuildUnsizedDataPacket(&framer, header, frames)); |
| 990 | EXPECT_TRUE(packet != nullptr); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 991 | char* buffer = new char[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 992 | size_t encrypted_length = |
nharper | 7c07528 | 2019-09-18 10:45:32 -0700 | [diff] [blame] | 993 | framer.EncryptPayload(level, QuicPacketNumber(packet_number), *packet, |
| 994 | buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 995 | EXPECT_NE(0u, encrypted_length); |
| 996 | DeleteFrames(&frames); |
| 997 | return new QuicEncryptedPacket(buffer, encrypted_length, true); |
| 998 | } |
| 999 | |
dschinazi | 42fc2da | 2020-04-24 16:19:17 -0700 | [diff] [blame] | 1000 | std::unique_ptr<QuicEncryptedPacket> GetUndecryptableEarlyPacket( |
| 1001 | const ParsedQuicVersion& version, |
| 1002 | const QuicConnectionId& server_connection_id) { |
| 1003 | QuicPacketHeader header; |
| 1004 | header.destination_connection_id = server_connection_id; |
| 1005 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 1006 | header.source_connection_id = EmptyQuicConnectionId(); |
| 1007 | header.source_connection_id_included = CONNECTION_ID_PRESENT; |
| 1008 | if (!version.SupportsClientConnectionIds()) { |
| 1009 | header.source_connection_id_included = CONNECTION_ID_ABSENT; |
| 1010 | } |
| 1011 | header.version_flag = true; |
| 1012 | header.reset_flag = false; |
| 1013 | header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER; |
| 1014 | header.packet_number = QuicPacketNumber(33); |
| 1015 | header.long_packet_type = ZERO_RTT_PROTECTED; |
| 1016 | if (version.HasLongHeaderLengths()) { |
| 1017 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 1018 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 1019 | } |
| 1020 | |
| 1021 | QuicFrames frames; |
| 1022 | frames.push_back(QuicFrame(QuicPingFrame())); |
| 1023 | frames.push_back(QuicFrame(QuicPaddingFrame(100))); |
| 1024 | QuicFramer framer({version}, QuicTime::Zero(), Perspective::IS_CLIENT, |
| 1025 | kQuicDefaultConnectionIdLength); |
| 1026 | framer.SetInitialObfuscators(server_connection_id); |
| 1027 | |
| 1028 | framer.SetEncrypter(ENCRYPTION_ZERO_RTT, |
| 1029 | std::make_unique<NullEncrypter>(Perspective::IS_CLIENT)); |
| 1030 | std::unique_ptr<QuicPacket> packet( |
| 1031 | BuildUnsizedDataPacket(&framer, header, frames)); |
| 1032 | EXPECT_TRUE(packet != nullptr); |
| 1033 | char* buffer = new char[kMaxOutgoingPacketSize]; |
| 1034 | size_t encrypted_length = |
| 1035 | framer.EncryptPayload(ENCRYPTION_ZERO_RTT, header.packet_number, *packet, |
| 1036 | buffer, kMaxOutgoingPacketSize); |
| 1037 | EXPECT_NE(0u, encrypted_length); |
| 1038 | DeleteFrames(&frames); |
| 1039 | return std::make_unique<QuicEncryptedPacket>(buffer, encrypted_length, |
| 1040 | /*owns_buffer=*/true); |
| 1041 | } |
| 1042 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1043 | QuicReceivedPacket* ConstructReceivedPacket( |
| 1044 | const QuicEncryptedPacket& encrypted_packet, |
| 1045 | QuicTime receipt_time) { |
| 1046 | char* buffer = new char[encrypted_packet.length()]; |
| 1047 | memcpy(buffer, encrypted_packet.data(), encrypted_packet.length()); |
| 1048 | return new QuicReceivedPacket(buffer, encrypted_packet.length(), receipt_time, |
| 1049 | true); |
| 1050 | } |
| 1051 | |
| 1052 | QuicEncryptedPacket* ConstructMisFramedEncryptedPacket( |
| 1053 | QuicConnectionId destination_connection_id, |
| 1054 | QuicConnectionId source_connection_id, |
| 1055 | bool version_flag, |
| 1056 | bool reset_flag, |
| 1057 | uint64_t packet_number, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1058 | const std::string& data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1059 | QuicConnectionIdIncluded destination_connection_id_included, |
| 1060 | QuicConnectionIdIncluded source_connection_id_included, |
| 1061 | QuicPacketNumberLength packet_number_length, |
bnc | 9711a9e | 2019-11-01 06:31:51 -0700 | [diff] [blame] | 1062 | ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1063 | Perspective perspective) { |
| 1064 | QuicPacketHeader header; |
| 1065 | header.destination_connection_id = destination_connection_id; |
| 1066 | header.destination_connection_id_included = |
| 1067 | destination_connection_id_included; |
| 1068 | header.source_connection_id = source_connection_id; |
| 1069 | header.source_connection_id_included = source_connection_id_included; |
| 1070 | header.version_flag = version_flag; |
| 1071 | header.reset_flag = reset_flag; |
| 1072 | header.packet_number_length = packet_number_length; |
| 1073 | header.packet_number = QuicPacketNumber(packet_number); |
bnc | 9711a9e | 2019-11-01 06:31:51 -0700 | [diff] [blame] | 1074 | if (QuicVersionHasLongHeaderLengths(version.transport_version) && |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1075 | version_flag) { |
| 1076 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 1077 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 1078 | } |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 1079 | QuicFrame frame( |
| 1080 | QuicStreamFrame(1, false, 0, quiche::QuicheStringPiece(data))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1081 | QuicFrames frames; |
| 1082 | frames.push_back(frame); |
bnc | 9711a9e | 2019-11-01 06:31:51 -0700 | [diff] [blame] | 1083 | QuicFramer framer({version}, QuicTime::Zero(), perspective, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1084 | kQuicDefaultConnectionIdLength); |
nharper | 7c07528 | 2019-09-18 10:45:32 -0700 | [diff] [blame] | 1085 | framer.SetInitialObfuscators(destination_connection_id); |
| 1086 | EncryptionLevel level = |
| 1087 | version_flag ? ENCRYPTION_INITIAL : ENCRYPTION_FORWARD_SECURE; |
| 1088 | if (level != ENCRYPTION_INITIAL) { |
| 1089 | framer.SetEncrypter(level, std::make_unique<NullEncrypter>(perspective)); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1090 | } |
| 1091 | // We need a minimum of 7 bytes of encrypted payload. This will guarantee that |
| 1092 | // we have at least that much. (It ignores the overhead of the stream/crypto |
| 1093 | // framing, so it overpads slightly.) |
| 1094 | if (data.length() < 7) { |
| 1095 | size_t padding_length = 7 - data.length(); |
| 1096 | frames.push_back(QuicFrame(QuicPaddingFrame(padding_length))); |
| 1097 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1098 | |
| 1099 | std::unique_ptr<QuicPacket> packet( |
| 1100 | BuildUnsizedDataPacket(&framer, header, frames)); |
| 1101 | EXPECT_TRUE(packet != nullptr); |
| 1102 | |
| 1103 | // Now set the frame type to 0x1F, which is an invalid frame type. |
| 1104 | reinterpret_cast<unsigned char*>( |
| 1105 | packet->mutable_data())[GetStartOfEncryptedData( |
| 1106 | framer.transport_version(), |
| 1107 | GetIncludedDestinationConnectionIdLength(header), |
| 1108 | GetIncludedSourceConnectionIdLength(header), version_flag, |
| 1109 | false /* no diversification nonce */, packet_number_length, |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 1110 | header.retry_token_length_length, 0, header.length_length)] = 0x1F; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1111 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1112 | char* buffer = new char[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1113 | size_t encrypted_length = |
nharper | 7c07528 | 2019-09-18 10:45:32 -0700 | [diff] [blame] | 1114 | framer.EncryptPayload(level, QuicPacketNumber(packet_number), *packet, |
| 1115 | buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1116 | EXPECT_NE(0u, encrypted_length); |
| 1117 | return new QuicEncryptedPacket(buffer, encrypted_length, true); |
| 1118 | } |
| 1119 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1120 | QuicConfig DefaultQuicConfig() { |
| 1121 | QuicConfig config; |
rch | b045185 | 2019-09-11 21:17:01 -0700 | [diff] [blame] | 1122 | config.SetInitialMaxStreamDataBytesIncomingBidirectionalToSend( |
| 1123 | kInitialStreamFlowControlWindowForTest); |
| 1124 | config.SetInitialMaxStreamDataBytesOutgoingBidirectionalToSend( |
| 1125 | kInitialStreamFlowControlWindowForTest); |
| 1126 | config.SetInitialMaxStreamDataBytesUnidirectionalToSend( |
| 1127 | kInitialStreamFlowControlWindowForTest); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1128 | config.SetInitialStreamFlowControlWindowToSend( |
| 1129 | kInitialStreamFlowControlWindowForTest); |
| 1130 | config.SetInitialSessionFlowControlWindowToSend( |
| 1131 | kInitialSessionFlowControlWindowForTest); |
renjietang | e6d9467 | 2020-01-07 10:30:10 -0800 | [diff] [blame] | 1132 | QuicConfigPeer::SetReceivedMaxBidirectionalStreams( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1133 | &config, kDefaultMaxStreamsPerConnection); |
| 1134 | // Default enable NSTP. |
| 1135 | // This is unnecessary for versions > 44 |
| 1136 | if (!config.HasClientSentConnectionOption(quic::kNSTP, |
| 1137 | quic::Perspective::IS_CLIENT)) { |
| 1138 | quic::QuicTagVector connection_options; |
| 1139 | connection_options.push_back(quic::kNSTP); |
| 1140 | config.SetConnectionOptionsToSend(connection_options); |
| 1141 | } |
| 1142 | return config; |
| 1143 | } |
| 1144 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1145 | ParsedQuicVersionVector SupportedVersions(ParsedQuicVersion version) { |
| 1146 | ParsedQuicVersionVector versions; |
| 1147 | versions.push_back(version); |
| 1148 | return versions; |
| 1149 | } |
| 1150 | |
| 1151 | MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {} |
| 1152 | |
| 1153 | MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {} |
| 1154 | |
| 1155 | MockReceivedPacketManager::MockReceivedPacketManager(QuicConnectionStats* stats) |
| 1156 | : QuicReceivedPacketManager(stats) {} |
| 1157 | |
| 1158 | MockReceivedPacketManager::~MockReceivedPacketManager() {} |
| 1159 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1160 | MockPacketCreatorDelegate::MockPacketCreatorDelegate() {} |
| 1161 | MockPacketCreatorDelegate::~MockPacketCreatorDelegate() {} |
| 1162 | |
| 1163 | MockSessionNotifier::MockSessionNotifier() {} |
| 1164 | MockSessionNotifier::~MockSessionNotifier() {} |
| 1165 | |
nharper | 1e32f4f | 2020-04-20 12:23:01 -0700 | [diff] [blame] | 1166 | // static |
| 1167 | QuicCryptoClientStream::HandshakerInterface* |
| 1168 | QuicCryptoClientStreamPeer::GetHandshaker(QuicCryptoClientStream* stream) { |
| 1169 | return stream->handshaker_.get(); |
| 1170 | } |
| 1171 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1172 | void CreateClientSessionForTest( |
| 1173 | QuicServerId server_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1174 | QuicTime::Delta connection_start_time, |
| 1175 | const ParsedQuicVersionVector& supported_versions, |
| 1176 | MockQuicConnectionHelper* helper, |
| 1177 | MockAlarmFactory* alarm_factory, |
| 1178 | QuicCryptoClientConfig* crypto_client_config, |
| 1179 | PacketSavingConnection** client_connection, |
| 1180 | TestQuicSpdyClientSession** client_session) { |
| 1181 | CHECK(crypto_client_config); |
| 1182 | CHECK(client_connection); |
| 1183 | CHECK(client_session); |
| 1184 | CHECK(!connection_start_time.IsZero()) |
| 1185 | << "Connections must start at non-zero times, otherwise the " |
| 1186 | << "strike-register will be unhappy."; |
| 1187 | |
wub | e9dc7da | 2019-05-22 06:08:54 -0700 | [diff] [blame] | 1188 | QuicConfig config = DefaultQuicConfig(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1189 | *client_connection = new PacketSavingConnection( |
| 1190 | helper, alarm_factory, Perspective::IS_CLIENT, supported_versions); |
| 1191 | *client_session = new TestQuicSpdyClientSession(*client_connection, config, |
| 1192 | supported_versions, server_id, |
| 1193 | crypto_client_config); |
| 1194 | (*client_connection)->AdvanceTime(connection_start_time); |
| 1195 | } |
| 1196 | |
| 1197 | void CreateServerSessionForTest( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1198 | QuicServerId /*server_id*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1199 | QuicTime::Delta connection_start_time, |
| 1200 | ParsedQuicVersionVector supported_versions, |
| 1201 | MockQuicConnectionHelper* helper, |
| 1202 | MockAlarmFactory* alarm_factory, |
| 1203 | QuicCryptoServerConfig* server_crypto_config, |
| 1204 | QuicCompressedCertsCache* compressed_certs_cache, |
| 1205 | PacketSavingConnection** server_connection, |
| 1206 | TestQuicSpdyServerSession** server_session) { |
| 1207 | CHECK(server_crypto_config); |
| 1208 | CHECK(server_connection); |
| 1209 | CHECK(server_session); |
| 1210 | CHECK(!connection_start_time.IsZero()) |
| 1211 | << "Connections must start at non-zero times, otherwise the " |
| 1212 | << "strike-register will be unhappy."; |
| 1213 | |
| 1214 | *server_connection = |
| 1215 | new PacketSavingConnection(helper, alarm_factory, Perspective::IS_SERVER, |
| 1216 | ParsedVersionOfIndex(supported_versions, 0)); |
| 1217 | *server_session = new TestQuicSpdyServerSession( |
| 1218 | *server_connection, DefaultQuicConfig(), supported_versions, |
| 1219 | server_crypto_config, compressed_certs_cache); |
wub | a89eee3 | 2019-11-22 13:02:52 -0800 | [diff] [blame] | 1220 | (*server_session)->Initialize(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1221 | |
| 1222 | // We advance the clock initially because the default time is zero and the |
| 1223 | // strike register worries that we've just overflowed a uint32_t time. |
| 1224 | (*server_connection)->AdvanceTime(connection_start_time); |
| 1225 | } |
| 1226 | |
| 1227 | QuicStreamId GetNthClientInitiatedBidirectionalStreamId( |
| 1228 | QuicTransportVersion version, |
| 1229 | int n) { |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1230 | int num = n; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1231 | if (!VersionUsesHttp3(version)) { |
renjietang | 118c8ac | 2019-07-30 11:43:59 -0700 | [diff] [blame] | 1232 | num++; |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1233 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1234 | return QuicUtils::GetFirstBidirectionalStreamId(version, |
| 1235 | Perspective::IS_CLIENT) + |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1236 | QuicUtils::StreamIdDelta(version) * num; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | QuicStreamId GetNthServerInitiatedBidirectionalStreamId( |
| 1240 | QuicTransportVersion version, |
| 1241 | int n) { |
| 1242 | return QuicUtils::GetFirstBidirectionalStreamId(version, |
| 1243 | Perspective::IS_SERVER) + |
| 1244 | QuicUtils::StreamIdDelta(version) * n; |
| 1245 | } |
| 1246 | |
| 1247 | QuicStreamId GetNthServerInitiatedUnidirectionalStreamId( |
| 1248 | QuicTransportVersion version, |
| 1249 | int n) { |
| 1250 | return QuicUtils::GetFirstUnidirectionalStreamId(version, |
| 1251 | Perspective::IS_SERVER) + |
| 1252 | QuicUtils::StreamIdDelta(version) * n; |
| 1253 | } |
| 1254 | |
renjietang | 3a1bb80 | 2019-06-11 10:42:41 -0700 | [diff] [blame] | 1255 | QuicStreamId GetNthClientInitiatedUnidirectionalStreamId( |
| 1256 | QuicTransportVersion version, |
| 1257 | int n) { |
| 1258 | return QuicUtils::GetFirstUnidirectionalStreamId(version, |
| 1259 | Perspective::IS_CLIENT) + |
| 1260 | QuicUtils::StreamIdDelta(version) * n; |
| 1261 | } |
| 1262 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1263 | StreamType DetermineStreamType(QuicStreamId id, |
renjietang | d262e25 | 2020-06-19 15:11:24 -0700 | [diff] [blame] | 1264 | ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1265 | Perspective perspective, |
| 1266 | bool is_incoming, |
| 1267 | StreamType default_type) { |
renjietang | d262e25 | 2020-06-19 15:11:24 -0700 | [diff] [blame] | 1268 | return version.HasIetfQuicFrames() |
| 1269 | ? QuicUtils::GetStreamType(id, perspective, is_incoming, version) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1270 | : default_type; |
| 1271 | } |
| 1272 | |
| 1273 | QuicMemSliceSpan MakeSpan(QuicBufferAllocator* allocator, |
QUICHE team | 6dcf6ab | 2019-12-11 10:10:51 -0800 | [diff] [blame] | 1274 | quiche::QuicheStringPiece message_data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1275 | QuicMemSliceStorage* storage) { |
| 1276 | if (message_data.length() == 0) { |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1277 | *storage = |
| 1278 | QuicMemSliceStorage(nullptr, 0, allocator, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1279 | return storage->ToSpan(); |
| 1280 | } |
| 1281 | struct iovec iov = {const_cast<char*>(message_data.data()), |
| 1282 | message_data.length()}; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1283 | *storage = QuicMemSliceStorage(&iov, 1, allocator, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1284 | return storage->ToSpan(); |
| 1285 | } |
| 1286 | |
vasilvv | 2b0ab24 | 2020-01-07 07:32:09 -0800 | [diff] [blame] | 1287 | QuicMemSlice MemSliceFromString(quiche::QuicheStringPiece data) { |
| 1288 | static SimpleBufferAllocator* allocator = new SimpleBufferAllocator(); |
| 1289 | QuicUniqueBufferPtr buffer = MakeUniqueBuffer(allocator, data.size()); |
| 1290 | memcpy(buffer.get(), data.data(), data.size()); |
| 1291 | return QuicMemSlice(std::move(buffer), data.size()); |
| 1292 | } |
| 1293 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1294 | } // namespace test |
| 1295 | } // namespace quic |