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/core/quic_packet_creator.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <memory> |
| 9 | #include <ostream> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_data_writer.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_pending_retransmission.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 22 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 24 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 25 | #include "net/third_party/quiche/src/quic/test_tools/quic_framer_peer.h" |
| 26 | #include "net/third_party/quiche/src/quic/test_tools/quic_packet_creator_peer.h" |
| 27 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 28 | #include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h" |
| 29 | |
| 30 | using testing::_; |
| 31 | using testing::DoAll; |
| 32 | using testing::InSequence; |
| 33 | using testing::Invoke; |
| 34 | using testing::Return; |
| 35 | using testing::SaveArg; |
| 36 | using testing::StrictMock; |
| 37 | |
| 38 | namespace quic { |
| 39 | namespace test { |
| 40 | namespace { |
| 41 | |
| 42 | // Run tests with combinations of {ParsedQuicVersion, |
| 43 | // ToggleVersionSerialization}. |
| 44 | struct TestParams { |
| 45 | TestParams(ParsedQuicVersion version, bool version_serialization) |
| 46 | : version(version), version_serialization(version_serialization) {} |
| 47 | |
| 48 | friend std::ostream& operator<<(std::ostream& os, const TestParams& p) { |
| 49 | os << "{ version: " << ParsedQuicVersionToString(p.version) |
| 50 | << " include version: " << p.version_serialization << " }"; |
| 51 | return os; |
| 52 | } |
| 53 | |
| 54 | ParsedQuicVersion version; |
| 55 | bool version_serialization; |
| 56 | }; |
| 57 | |
| 58 | // Constructs various test permutations. |
| 59 | std::vector<TestParams> GetTestParams() { |
| 60 | std::vector<TestParams> params; |
| 61 | ParsedQuicVersionVector all_supported_versions = AllSupportedVersions(); |
| 62 | for (size_t i = 0; i < all_supported_versions.size(); ++i) { |
| 63 | params.push_back(TestParams(all_supported_versions[i], true)); |
| 64 | params.push_back(TestParams(all_supported_versions[i], false)); |
| 65 | } |
| 66 | return params; |
| 67 | } |
| 68 | |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 69 | class MockDebugDelegate : public QuicPacketCreator::DebugDelegate { |
| 70 | public: |
| 71 | ~MockDebugDelegate() override = default; |
| 72 | |
| 73 | MOCK_METHOD1(OnFrameAddedToPacket, void(const QuicFrame& frame)); |
| 74 | }; |
| 75 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | class TestPacketCreator : public QuicPacketCreator { |
| 77 | public: |
| 78 | TestPacketCreator(QuicConnectionId connection_id, |
| 79 | QuicFramer* framer, |
| 80 | DelegateInterface* delegate, |
| 81 | SimpleDataProducer* producer) |
| 82 | : QuicPacketCreator(connection_id, framer, delegate), |
| 83 | producer_(producer), |
| 84 | version_(framer->transport_version()) {} |
| 85 | |
| 86 | bool ConsumeData(QuicStreamId id, |
| 87 | const struct iovec* iov, |
| 88 | int iov_count, |
| 89 | size_t total_length, |
| 90 | size_t iov_offset, |
| 91 | QuicStreamOffset offset, |
| 92 | bool fin, |
| 93 | bool needs_full_padding, |
| 94 | TransmissionType transmission_type, |
| 95 | QuicFrame* frame) { |
| 96 | // Save data before data is consumed. |
| 97 | QuicByteCount data_length = total_length - iov_offset; |
| 98 | if (data_length > 0) { |
| 99 | producer_->SaveStreamData(id, iov, iov_count, iov_offset, data_length); |
| 100 | } |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 101 | return QuicPacketCreator::ConsumeData(id, data_length - iov_offset, offset, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 102 | fin, needs_full_padding, |
| 103 | transmission_type, frame); |
| 104 | } |
| 105 | |
| 106 | void StopSendingVersion() { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 107 | if (VersionHasIetfInvariantHeader(version_)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 108 | set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 109 | return; |
| 110 | } |
| 111 | QuicPacketCreator::StopSendingVersion(); |
| 112 | } |
| 113 | |
| 114 | SimpleDataProducer* producer_; |
| 115 | QuicTransportVersion version_; |
| 116 | }; |
| 117 | |
| 118 | class QuicPacketCreatorTest : public QuicTestWithParam<TestParams> { |
| 119 | public: |
| 120 | void ClearSerializedPacketForTests(SerializedPacket* serialized_packet) { |
| 121 | if (serialized_packet == nullptr) { |
| 122 | return; |
| 123 | } |
| 124 | ClearSerializedPacket(serialized_packet); |
| 125 | } |
| 126 | |
| 127 | void SaveSerializedPacket(SerializedPacket* serialized_packet) { |
| 128 | if (serialized_packet == nullptr) { |
| 129 | return; |
| 130 | } |
| 131 | delete[] serialized_packet_.encrypted_buffer; |
| 132 | serialized_packet_ = *serialized_packet; |
| 133 | serialized_packet_.encrypted_buffer = CopyBuffer(*serialized_packet); |
| 134 | serialized_packet->retransmittable_frames.clear(); |
| 135 | } |
| 136 | |
| 137 | void DeleteSerializedPacket() { |
| 138 | delete[] serialized_packet_.encrypted_buffer; |
| 139 | serialized_packet_.encrypted_buffer = nullptr; |
| 140 | ClearSerializedPacket(&serialized_packet_); |
| 141 | } |
| 142 | |
| 143 | protected: |
| 144 | QuicPacketCreatorTest() |
| 145 | : connection_id_(TestConnectionId(2)), |
| 146 | server_framer_(SupportedVersions(GetParam().version), |
| 147 | QuicTime::Zero(), |
| 148 | Perspective::IS_SERVER, |
| 149 | connection_id_.length()), |
| 150 | client_framer_(SupportedVersions(GetParam().version), |
| 151 | QuicTime::Zero(), |
| 152 | Perspective::IS_CLIENT, |
| 153 | connection_id_.length()), |
| 154 | data_("foo"), |
| 155 | creator_(connection_id_, &client_framer_, &delegate_, &producer_), |
| 156 | serialized_packet_(creator_.NoPacket()) { |
| 157 | EXPECT_CALL(delegate_, GetPacketBuffer()).WillRepeatedly(Return(nullptr)); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 158 | creator_.SetEncrypter(ENCRYPTION_HANDSHAKE, std::make_unique<NullEncrypter>( |
QUICHE team | 88ea008 | 2019-03-15 10:05:26 -0700 | [diff] [blame] | 159 | Perspective::IS_CLIENT)); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 160 | creator_.SetEncrypter(ENCRYPTION_ZERO_RTT, std::make_unique<NullEncrypter>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | Perspective::IS_CLIENT)); |
| 162 | creator_.SetEncrypter( |
| 163 | ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 164 | std::make_unique<NullEncrypter>(Perspective::IS_CLIENT)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | client_framer_.set_visitor(&framer_visitor_); |
| 166 | server_framer_.set_visitor(&framer_visitor_); |
| 167 | client_framer_.set_data_producer(&producer_); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 168 | if (server_framer_.version().KnowsWhichDecrypterToUse()) { |
| 169 | server_framer_.InstallDecrypter( |
| 170 | ENCRYPTION_ZERO_RTT, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 171 | std::make_unique<NullDecrypter>(Perspective::IS_SERVER)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 172 | server_framer_.InstallDecrypter( |
| 173 | ENCRYPTION_HANDSHAKE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 174 | std::make_unique<NullDecrypter>(Perspective::IS_SERVER)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 175 | server_framer_.InstallDecrypter( |
| 176 | ENCRYPTION_FORWARD_SECURE, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 177 | std::make_unique<NullDecrypter>(Perspective::IS_SERVER)); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 178 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | ~QuicPacketCreatorTest() override { |
| 182 | delete[] serialized_packet_.encrypted_buffer; |
| 183 | ClearSerializedPacket(&serialized_packet_); |
| 184 | } |
| 185 | |
| 186 | SerializedPacket SerializeAllFrames(const QuicFrames& frames) { |
| 187 | SerializedPacket packet = QuicPacketCreatorPeer::SerializeAllFrames( |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 188 | &creator_, frames, buffer_, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 189 | EXPECT_EQ(QuicPacketCreatorPeer::GetEncryptionLevel(&creator_), |
| 190 | packet.encryption_level); |
| 191 | return packet; |
| 192 | } |
| 193 | |
| 194 | void ProcessPacket(const SerializedPacket& packet) { |
| 195 | QuicEncryptedPacket encrypted_packet(packet.encrypted_buffer, |
| 196 | packet.encrypted_length); |
| 197 | server_framer_.ProcessPacket(encrypted_packet); |
| 198 | } |
| 199 | |
| 200 | void CheckStreamFrame(const QuicFrame& frame, |
| 201 | QuicStreamId stream_id, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 202 | const std::string& data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 203 | QuicStreamOffset offset, |
| 204 | bool fin) { |
| 205 | EXPECT_EQ(STREAM_FRAME, frame.type); |
| 206 | EXPECT_EQ(stream_id, frame.stream_frame.stream_id); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 207 | char buf[kMaxOutgoingPacketSize]; |
| 208 | QuicDataWriter writer(kMaxOutgoingPacketSize, buf, HOST_BYTE_ORDER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 209 | if (frame.stream_frame.data_length > 0) { |
| 210 | producer_.WriteStreamData(stream_id, frame.stream_frame.offset, |
| 211 | frame.stream_frame.data_length, &writer); |
| 212 | } |
| 213 | EXPECT_EQ(data, QuicStringPiece(buf, frame.stream_frame.data_length)); |
| 214 | EXPECT_EQ(offset, frame.stream_frame.offset); |
| 215 | EXPECT_EQ(fin, frame.stream_frame.fin); |
| 216 | } |
| 217 | |
| 218 | // Returns the number of bytes consumed by the header of packet, including |
| 219 | // the version. |
| 220 | size_t GetPacketHeaderOverhead(QuicTransportVersion version) { |
| 221 | return GetPacketHeaderSize( |
| 222 | version, creator_.GetDestinationConnectionIdLength(), |
| 223 | creator_.GetSourceConnectionIdLength(), |
| 224 | QuicPacketCreatorPeer::SendVersionInPacket(&creator_), |
| 225 | !kIncludeDiversificationNonce, |
| 226 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_), |
| 227 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(&creator_), 0, |
| 228 | QuicPacketCreatorPeer::GetLengthLength(&creator_)); |
| 229 | } |
| 230 | |
| 231 | // Returns the number of bytes of overhead that will be added to a packet |
| 232 | // of maximum length. |
| 233 | size_t GetEncryptionOverhead() { |
| 234 | return creator_.max_packet_length() - |
| 235 | client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()); |
| 236 | } |
| 237 | |
| 238 | // Returns the number of bytes consumed by the non-data fields of a stream |
| 239 | // frame, assuming it is the last frame in the packet |
| 240 | size_t GetStreamFrameOverhead(QuicTransportVersion version) { |
| 241 | return QuicFramer::GetMinStreamFrameSize( |
| 242 | version, GetNthClientInitiatedStreamId(1), kOffset, true, |
| 243 | /* data_length= */ 0); |
| 244 | } |
| 245 | |
| 246 | QuicPendingRetransmission CreateRetransmission( |
| 247 | const QuicFrames& retransmittable_frames, |
| 248 | bool has_crypto_handshake, |
| 249 | int num_padding_bytes, |
| 250 | EncryptionLevel encryption_level, |
| 251 | QuicPacketNumberLength packet_number_length) { |
| 252 | return QuicPendingRetransmission(QuicPacketNumber(1u), NOT_RETRANSMISSION, |
| 253 | retransmittable_frames, |
| 254 | has_crypto_handshake, num_padding_bytes, |
| 255 | encryption_level, packet_number_length); |
| 256 | } |
| 257 | |
| 258 | bool IsDefaultTestConfiguration() { |
| 259 | TestParams p = GetParam(); |
| 260 | return p.version == AllSupportedVersions()[0] && p.version_serialization; |
| 261 | } |
| 262 | |
| 263 | QuicStreamId GetNthClientInitiatedStreamId(int n) const { |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 264 | return QuicUtils::GetFirstBidirectionalStreamId( |
| 265 | creator_.transport_version(), Perspective::IS_CLIENT) + |
| 266 | n * 2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static const QuicStreamOffset kOffset = 0u; |
| 270 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 271 | char buffer_[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 272 | QuicConnectionId connection_id_; |
| 273 | QuicFrames frames_; |
| 274 | QuicFramer server_framer_; |
| 275 | QuicFramer client_framer_; |
| 276 | StrictMock<MockFramerVisitor> framer_visitor_; |
| 277 | StrictMock<MockPacketCreatorDelegate> delegate_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 278 | std::string data_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 279 | struct iovec iov_; |
| 280 | TestPacketCreator creator_; |
| 281 | SerializedPacket serialized_packet_; |
| 282 | SimpleDataProducer producer_; |
| 283 | SimpleBufferAllocator allocator_; |
| 284 | }; |
| 285 | |
| 286 | // Run all packet creator tests with all supported versions of QUIC, and with |
| 287 | // and without version in the packet header, as well as doing a run for each |
| 288 | // length of truncated connection id. |
| 289 | INSTANTIATE_TEST_SUITE_P(QuicPacketCreatorTests, |
| 290 | QuicPacketCreatorTest, |
| 291 | ::testing::ValuesIn(GetTestParams())); |
| 292 | |
| 293 | TEST_P(QuicPacketCreatorTest, SerializeFrames) { |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 294 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 295 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 296 | creator_.set_encryption_level(level); |
| 297 | frames_.push_back(QuicFrame(new QuicAckFrame(InitAckFrame(1)))); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 298 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 299 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
fayang | 4952323 | 2019-05-03 06:28:22 -0700 | [diff] [blame] | 300 | if (level != ENCRYPTION_INITIAL && level != ENCRYPTION_HANDSHAKE) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 301 | frames_.push_back( |
| 302 | QuicFrame(QuicStreamFrame(stream_id, false, 0u, QuicStringPiece()))); |
| 303 | frames_.push_back( |
| 304 | QuicFrame(QuicStreamFrame(stream_id, true, 0u, QuicStringPiece()))); |
| 305 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 306 | SerializedPacket serialized = SerializeAllFrames(frames_); |
| 307 | EXPECT_EQ(level, serialized.encryption_level); |
| 308 | delete frames_[0].ack_frame; |
| 309 | frames_.clear(); |
| 310 | |
| 311 | { |
| 312 | InSequence s; |
| 313 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 314 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 315 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 316 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 317 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 318 | EXPECT_CALL(framer_visitor_, OnAckFrameStart(_, _)) |
| 319 | .WillOnce(Return(true)); |
| 320 | EXPECT_CALL(framer_visitor_, |
| 321 | OnAckRange(QuicPacketNumber(1), QuicPacketNumber(2))) |
| 322 | .WillOnce(Return(true)); |
| 323 | EXPECT_CALL(framer_visitor_, OnAckFrameEnd(QuicPacketNumber(1))) |
| 324 | .WillOnce(Return(true)); |
fayang | 4952323 | 2019-05-03 06:28:22 -0700 | [diff] [blame] | 325 | if (level != ENCRYPTION_INITIAL && level != ENCRYPTION_HANDSHAKE) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 326 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 327 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 328 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 329 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 330 | } |
| 331 | ProcessPacket(serialized); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 336 | if (VersionHasIetfInvariantHeader(client_framer_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 337 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 338 | } |
| 339 | // If the original packet number length, the current packet number |
| 340 | // length, and the configured send packet number length are different, the |
| 341 | // retransmit must sent with the original length and the others do not change. |
| 342 | QuicPacketCreatorPeer::SetPacketNumberLength(&creator_, |
| 343 | PACKET_2BYTE_PACKET_NUMBER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 344 | QuicFrames frames; |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 345 | std::string data("a"); |
| 346 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 347 | QuicStreamFrame stream_frame( |
| 348 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 349 | /*fin=*/false, 0u, QuicStringPiece()); |
| 350 | frames.push_back(QuicFrame(stream_frame)); |
| 351 | } else { |
| 352 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 353 | frames.push_back( |
| 354 | QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length()))); |
| 355 | } |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 356 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 357 | QuicPendingRetransmission retransmission(CreateRetransmission( |
| 358 | frames, true /* has_crypto_handshake */, -1 /* needs full padding */, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 359 | ENCRYPTION_INITIAL, PACKET_4BYTE_PACKET_NUMBER)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 360 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 361 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 362 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 363 | // The packet number length is updated after every packet is sent, |
| 364 | // so there is no need to restore the old length after sending. |
| 365 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 366 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 367 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 368 | serialized_packet_.packet_number_length); |
| 369 | |
| 370 | { |
| 371 | InSequence s; |
| 372 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 373 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 374 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 375 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 376 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 377 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 378 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 379 | } else { |
| 380 | EXPECT_CALL(framer_visitor_, OnCryptoFrame(_)); |
| 381 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 382 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 383 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 384 | } |
| 385 | ProcessPacket(serialized_packet_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 386 | DeleteFrames(&frames); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | TEST_P(QuicPacketCreatorTest, ReserializeCryptoFrameWithForwardSecurity) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 390 | QuicFrames frames; |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 391 | std::string data("a"); |
| 392 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 393 | QuicStreamFrame stream_frame( |
| 394 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 395 | /*fin=*/false, 0u, QuicStringPiece()); |
| 396 | frames.push_back(QuicFrame(stream_frame)); |
| 397 | } else { |
| 398 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 399 | frames.push_back( |
| 400 | QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length()))); |
| 401 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 402 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 403 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 404 | QuicPendingRetransmission retransmission(CreateRetransmission( |
| 405 | frames, true /* has_crypto_handshake */, -1 /* needs full padding */, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 406 | ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 407 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 408 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 409 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 410 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 411 | EXPECT_EQ(ENCRYPTION_INITIAL, serialized_packet_.encryption_level); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 412 | DeleteFrames(&frames); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TEST_P(QuicPacketCreatorTest, ReserializeFrameWithForwardSecurity) { |
| 416 | QuicStreamFrame stream_frame(0u, /*fin=*/false, 0u, QuicStringPiece()); |
| 417 | QuicFrames frames; |
| 418 | frames.push_back(QuicFrame(stream_frame)); |
| 419 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 420 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 421 | QuicPendingRetransmission retransmission(CreateRetransmission( |
| 422 | frames, false /* has_crypto_handshake */, 0 /* no padding */, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 423 | ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 424 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 425 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 426 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 427 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 428 | EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, serialized_packet_.encryption_level); |
| 429 | } |
| 430 | |
| 431 | TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPadding) { |
| 432 | QuicFrame frame; |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 433 | std::string data = "fake handshake message data"; |
| 434 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 435 | MakeIOVector(data, &iov_); |
| 436 | producer_.SaveStreamData( |
| 437 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), &iov_, |
| 438 | 1u, 0u, iov_.iov_len); |
| 439 | QuicPacketCreatorPeer::CreateStreamFrame( |
| 440 | &creator_, |
| 441 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 442 | iov_.iov_len, 0u, false, &frame); |
| 443 | } else { |
| 444 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 445 | EXPECT_TRUE(QuicPacketCreatorPeer::CreateCryptoFrame( |
| 446 | &creator_, ENCRYPTION_INITIAL, data.length(), 0, &frame)); |
| 447 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 448 | QuicFrames frames; |
| 449 | frames.push_back(frame); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 450 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 451 | QuicPendingRetransmission retransmission(CreateRetransmission( |
| 452 | frames, true /* has_crypto_handshake */, -1 /* needs full padding */, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 453 | ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 454 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 455 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 456 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 457 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 458 | EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 459 | DeleteFrames(&frames); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | TEST_P(QuicPacketCreatorTest, DoNotRetransmitPendingPadding) { |
| 463 | QuicFrame frame; |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 464 | std::string data = "fake message data"; |
| 465 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 466 | MakeIOVector(data, &iov_); |
| 467 | producer_.SaveStreamData( |
| 468 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), &iov_, |
| 469 | 1u, 0u, iov_.iov_len); |
| 470 | QuicPacketCreatorPeer::CreateStreamFrame( |
| 471 | &creator_, |
| 472 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 473 | iov_.iov_len, 0u, false, &frame); |
| 474 | } else { |
| 475 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 476 | EXPECT_TRUE(QuicPacketCreatorPeer::CreateCryptoFrame( |
| 477 | &creator_, ENCRYPTION_INITIAL, data.length(), 0, &frame)); |
| 478 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 479 | |
| 480 | const int kNumPaddingBytes1 = 4; |
| 481 | int packet_size = 0; |
| 482 | { |
| 483 | QuicFrames frames; |
| 484 | frames.push_back(frame); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 485 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 486 | QuicPendingRetransmission retransmission(CreateRetransmission( |
| 487 | frames, false /* has_crypto_handshake */, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 488 | kNumPaddingBytes1 /* padding bytes */, ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 489 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 490 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 491 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 492 | creator_.ReserializeAllFrames(retransmission, buffer, |
| 493 | kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 494 | packet_size = serialized_packet_.encrypted_length; |
| 495 | } |
| 496 | |
| 497 | { |
| 498 | InSequence s; |
| 499 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 500 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 501 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 502 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 503 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 504 | if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 505 | EXPECT_CALL(framer_visitor_, OnCryptoFrame(_)); |
| 506 | } else { |
| 507 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 508 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 509 | // Pending paddings are not retransmitted. |
| 510 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)).Times(0); |
| 511 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 512 | } |
| 513 | ProcessPacket(serialized_packet_); |
| 514 | |
| 515 | const int kNumPaddingBytes2 = 44; |
| 516 | QuicFrames frames; |
| 517 | frames.push_back(frame); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 518 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 519 | QuicPendingRetransmission retransmission(CreateRetransmission( |
| 520 | frames, false /* has_crypto_handshake */, |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 521 | kNumPaddingBytes2 /* padding bytes */, ENCRYPTION_INITIAL, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 522 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 523 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 524 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 525 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 526 | |
| 527 | EXPECT_EQ(packet_size, serialized_packet_.encrypted_length); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 528 | DeleteFrames(&frames); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 532 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 533 | const size_t overhead = |
| 534 | GetPacketHeaderOverhead(client_framer_.transport_version()) + |
| 535 | GetEncryptionOverhead() + |
| 536 | GetStreamFrameOverhead(client_framer_.transport_version()); |
| 537 | size_t capacity = kDefaultMaxPacketSize - overhead; |
| 538 | for (int delta = -5; delta <= 0; ++delta) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 539 | std::string data(capacity + delta, 'A'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 540 | size_t bytes_free = 0 - delta; |
| 541 | |
| 542 | QuicFrame frame; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 543 | SimpleDataProducer producer; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 544 | QuicPacketCreatorPeer::framer(&creator_)->set_data_producer(&producer); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 545 | MakeIOVector(data, &iov_); |
| 546 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 547 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 548 | producer.SaveStreamData(stream_id, &iov_, 1u, 0u, iov_.iov_len); |
| 549 | QuicPacketCreatorPeer::CreateStreamFrame(&creator_, stream_id, iov_.iov_len, |
| 550 | kOffset, false, &frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 551 | QuicFrames frames; |
| 552 | frames.push_back(frame); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 553 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 554 | QuicPendingRetransmission retransmission(CreateRetransmission( |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 555 | frames, false /* has_crypto_handshake */, -1 /* needs full padding */, |
| 556 | ENCRYPTION_FORWARD_SECURE, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 557 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 558 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 559 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 560 | creator_.ReserializeAllFrames(retransmission, buffer, |
| 561 | kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 562 | |
| 563 | // If there is not enough space in the packet to fit a padding frame |
| 564 | // (1 byte) and to expand the stream frame (another 2 bytes) the packet |
| 565 | // will not be padded. |
| 566 | if (bytes_free < 3) { |
| 567 | EXPECT_EQ(kDefaultMaxPacketSize - bytes_free, |
| 568 | serialized_packet_.encrypted_length); |
| 569 | } else { |
| 570 | EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
| 571 | } |
| 572 | |
| 573 | frames_.clear(); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) { |
fkastenholz | 591814c | 2019-09-06 12:11:46 -0700 | [diff] [blame] | 578 | QuicConnectionCloseFrame frame(GetParam().version.transport_version, |
| 579 | QUIC_NO_ERROR, "error", |
| 580 | /*transport_close_frame_type=*/0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 581 | |
| 582 | QuicFrames frames; |
| 583 | frames.push_back(QuicFrame(&frame)); |
| 584 | SerializedPacket serialized = SerializeAllFrames(frames); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 585 | EXPECT_EQ(ENCRYPTION_INITIAL, serialized.encryption_level); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 586 | ASSERT_EQ(QuicPacketNumber(1u), serialized.packet_number); |
| 587 | ASSERT_EQ(QuicPacketNumber(1u), creator_.packet_number()); |
| 588 | |
| 589 | InSequence s; |
| 590 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 591 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 592 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 593 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 594 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 595 | EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_)); |
| 596 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 597 | |
| 598 | ProcessPacket(serialized); |
| 599 | } |
| 600 | |
| 601 | TEST_P(QuicPacketCreatorTest, ConsumeCryptoData) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 602 | std::string data = "crypto data"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 603 | QuicFrame frame; |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 604 | ASSERT_TRUE(creator_.ConsumeCryptoData(ENCRYPTION_INITIAL, data.length(), 0, |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 605 | /*needs_full_padding=*/true, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 606 | NOT_RETRANSMISSION, &frame)); |
| 607 | EXPECT_EQ(frame.crypto_frame->data_length, data.length()); |
| 608 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 609 | } |
| 610 | |
| 611 | TEST_P(QuicPacketCreatorTest, ConsumeData) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 612 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 613 | QuicFrame frame; |
| 614 | MakeIOVector("test", &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 615 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 616 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 617 | ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, |
| 618 | false, false, NOT_RETRANSMISSION, &frame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 619 | size_t consumed = frame.stream_frame.data_length; |
| 620 | EXPECT_EQ(4u, consumed); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 621 | CheckStreamFrame(frame, stream_id, "test", 0u, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 622 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 623 | } |
| 624 | |
| 625 | TEST_P(QuicPacketCreatorTest, ConsumeDataFin) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 626 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 627 | QuicFrame frame; |
| 628 | MakeIOVector("test", &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 629 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 630 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 631 | ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, |
| 632 | true, false, NOT_RETRANSMISSION, &frame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 633 | size_t consumed = frame.stream_frame.data_length; |
| 634 | EXPECT_EQ(4u, consumed); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 635 | CheckStreamFrame(frame, stream_id, "test", 0u, true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 636 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 637 | } |
| 638 | |
| 639 | TEST_P(QuicPacketCreatorTest, ConsumeDataFinOnly) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 640 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 641 | QuicFrame frame; |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 642 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 643 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 644 | ASSERT_TRUE(creator_.ConsumeData(stream_id, nullptr, 0u, 0u, 0u, 0u, true, |
| 645 | false, NOT_RETRANSMISSION, &frame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 646 | size_t consumed = frame.stream_frame.data_length; |
| 647 | EXPECT_EQ(0u, consumed); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 648 | CheckStreamFrame(frame, stream_id, std::string(), 0u, true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 649 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 650 | } |
| 651 | |
| 652 | TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) { |
| 653 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 654 | const size_t overhead = |
| 655 | GetPacketHeaderOverhead(client_framer_.transport_version()) + |
| 656 | GetEncryptionOverhead(); |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 657 | for (size_t i = overhead + QuicPacketCreator::MinPlaintextPacketSize( |
| 658 | client_framer_.version()); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 659 | i < overhead + 100; ++i) { |
| 660 | SCOPED_TRACE(i); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 661 | creator_.SetMaxPacketLength(i); |
| 662 | const bool should_have_room = |
| 663 | i > |
| 664 | overhead + GetStreamFrameOverhead(client_framer_.transport_version()); |
| 665 | ASSERT_EQ(should_have_room, |
| 666 | creator_.HasRoomForStreamFrame(GetNthClientInitiatedStreamId(1), |
| 667 | kOffset, /* data_size=*/0xffff)); |
| 668 | if (should_have_room) { |
| 669 | QuicFrame frame; |
| 670 | MakeIOVector("testdata", &iov_); |
| 671 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 672 | .WillRepeatedly(Invoke( |
| 673 | this, &QuicPacketCreatorTest::ClearSerializedPacketForTests)); |
| 674 | ASSERT_TRUE(creator_.ConsumeData(GetNthClientInitiatedStreamId(1), &iov_, |
| 675 | 1u, iov_.iov_len, 0u, kOffset, false, |
| 676 | false, NOT_RETRANSMISSION, &frame)); |
| 677 | size_t bytes_consumed = frame.stream_frame.data_length; |
| 678 | EXPECT_LT(0u, bytes_consumed); |
| 679 | creator_.Flush(); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) { |
| 685 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 686 | // Compute the total overhead for a single frame in packet. |
| 687 | const size_t overhead = |
| 688 | GetPacketHeaderOverhead(client_framer_.transport_version()) + |
| 689 | GetEncryptionOverhead() + |
| 690 | GetStreamFrameOverhead(client_framer_.transport_version()); |
| 691 | size_t capacity = kDefaultMaxPacketSize - overhead; |
| 692 | // Now, test various sizes around this size. |
| 693 | for (int delta = -5; delta <= 5; ++delta) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 694 | std::string data(capacity + delta, 'A'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 695 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 696 | QuicFrame frame; |
| 697 | MakeIOVector(data, &iov_); |
| 698 | ASSERT_TRUE(creator_.ConsumeData(GetNthClientInitiatedStreamId(1), &iov_, |
| 699 | 1u, iov_.iov_len, 0u, kOffset, false, |
| 700 | false, NOT_RETRANSMISSION, &frame)); |
| 701 | |
| 702 | // BytesFree() returns bytes available for the next frame, which will |
| 703 | // be two bytes smaller since the stream frame would need to be grown. |
| 704 | EXPECT_EQ(2u, creator_.ExpansionOnNewFrame()); |
| 705 | size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2; |
| 706 | EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta; |
| 707 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 708 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 709 | creator_.Flush(); |
| 710 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 711 | DeleteSerializedPacket(); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | TEST_P(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) { |
ianswett | e28f022 | 2019-04-04 13:31:22 -0700 | [diff] [blame] | 716 | // This test serializes crypto payloads slightly larger than a packet, which |
| 717 | // Causes the multi-packet ClientHello check to fail. |
danzh | 88e3e05 | 2019-06-13 11:47:18 -0700 | [diff] [blame] | 718 | SetQuicFlag(FLAGS_quic_enforce_single_packet_chlo, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 719 | // Compute the total overhead for a single frame in packet. |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 720 | size_t overhead = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 721 | GetPacketHeaderOverhead(client_framer_.transport_version()) + |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 722 | GetEncryptionOverhead(); |
| 723 | if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 724 | overhead += |
| 725 | QuicFramer::GetMinCryptoFrameSize(kOffset, kMaxOutgoingPacketSize); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 726 | } else { |
| 727 | overhead += GetStreamFrameOverhead(client_framer_.transport_version()); |
| 728 | } |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 729 | ASSERT_GT(kMaxOutgoingPacketSize, overhead); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 730 | size_t capacity = kDefaultMaxPacketSize - overhead; |
| 731 | // Now, test various sizes around this size. |
| 732 | for (int delta = -5; delta <= 5; ++delta) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 733 | SCOPED_TRACE(delta); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 734 | std::string data(capacity + delta, 'A'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 735 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 736 | |
| 737 | QuicFrame frame; |
| 738 | MakeIOVector(data, &iov_); |
| 739 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 740 | .WillRepeatedly( |
| 741 | Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 742 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 743 | ASSERT_TRUE(creator_.ConsumeData( |
| 744 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 745 | &iov_, 1u, iov_.iov_len, 0u, kOffset, false, true, NOT_RETRANSMISSION, |
| 746 | &frame)); |
| 747 | size_t bytes_consumed = frame.stream_frame.data_length; |
| 748 | EXPECT_LT(0u, bytes_consumed); |
| 749 | } else { |
| 750 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, kOffset, data); |
nharper | 51961cf | 2019-05-13 13:23:24 -0700 | [diff] [blame] | 751 | ASSERT_TRUE(creator_.ConsumeCryptoData( |
| 752 | ENCRYPTION_INITIAL, data.length(), kOffset, |
| 753 | /*needs_full_padding=*/true, NOT_RETRANSMISSION, &frame)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 754 | size_t bytes_consumed = frame.crypto_frame->data_length; |
| 755 | EXPECT_LT(0u, bytes_consumed); |
| 756 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 757 | creator_.Flush(); |
| 758 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 759 | // If there is not enough space in the packet to fit a padding frame |
| 760 | // (1 byte) and to expand the stream frame (another 2 bytes) the packet |
| 761 | // will not be padded. |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 762 | if (bytes_free < 3 && |
| 763 | !QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 764 | EXPECT_EQ(kDefaultMaxPacketSize - bytes_free, |
| 765 | serialized_packet_.encrypted_length); |
| 766 | } else { |
| 767 | EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
| 768 | } |
| 769 | DeleteSerializedPacket(); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | TEST_P(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) { |
| 774 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 775 | // Compute the total overhead for a single frame in packet. |
| 776 | const size_t overhead = |
| 777 | GetPacketHeaderOverhead(client_framer_.transport_version()) + |
| 778 | GetEncryptionOverhead() + |
| 779 | GetStreamFrameOverhead(client_framer_.transport_version()); |
| 780 | ASSERT_GT(kDefaultMaxPacketSize, overhead); |
| 781 | size_t capacity = kDefaultMaxPacketSize - overhead; |
| 782 | // Now, test various sizes around this size. |
| 783 | for (int delta = -5; delta <= 5; ++delta) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 784 | std::string data(capacity + delta, 'A'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 785 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 786 | |
| 787 | QuicFrame frame; |
| 788 | MakeIOVector(data, &iov_); |
| 789 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 790 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 791 | ASSERT_TRUE(creator_.ConsumeData(GetNthClientInitiatedStreamId(1), &iov_, |
| 792 | 1u, iov_.iov_len, 0u, kOffset, false, |
| 793 | false, NOT_RETRANSMISSION, &frame)); |
| 794 | size_t bytes_consumed = frame.stream_frame.data_length; |
| 795 | EXPECT_LT(0u, bytes_consumed); |
| 796 | creator_.Flush(); |
| 797 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 798 | if (bytes_free > 0) { |
| 799 | EXPECT_EQ(kDefaultMaxPacketSize - bytes_free, |
| 800 | serialized_packet_.encrypted_length); |
| 801 | } else { |
| 802 | EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
| 803 | } |
| 804 | DeleteSerializedPacket(); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | TEST_P(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) { |
| 809 | QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER); |
| 810 | ParsedQuicVersionVector versions; |
| 811 | versions.push_back(test::QuicVersionMax()); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 812 | const bool ietf_quic = |
| 813 | VersionHasIetfInvariantHeader(GetParam().version.transport_version); |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 814 | const bool has_length_prefix = |
| 815 | GetParam().version.HasLengthPrefixedConnectionIds(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 816 | std::unique_ptr<QuicEncryptedPacket> encrypted( |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 817 | creator_.SerializeVersionNegotiationPacket(ietf_quic, has_length_prefix, |
| 818 | versions)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 819 | |
| 820 | { |
| 821 | InSequence s; |
| 822 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 823 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 824 | EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_)); |
| 825 | } |
| 826 | QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_CLIENT); |
| 827 | client_framer_.ProcessPacket(*encrypted); |
| 828 | } |
| 829 | |
| 830 | TEST_P(QuicPacketCreatorTest, SerializeConnectivityProbingPacket) { |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 831 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 832 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 833 | |
| 834 | creator_.set_encryption_level(level); |
| 835 | |
| 836 | OwningSerializedPacketPointer encrypted; |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 837 | if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 838 | QuicPathFrameBuffer payload = { |
| 839 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe}}; |
| 840 | encrypted = |
| 841 | creator_.SerializePathChallengeConnectivityProbingPacket(&payload); |
| 842 | } else { |
| 843 | encrypted = creator_.SerializeConnectivityProbingPacket(); |
| 844 | } |
| 845 | { |
| 846 | InSequence s; |
| 847 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 848 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 849 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 850 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 851 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 852 | if (VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 853 | EXPECT_CALL(framer_visitor_, OnPathChallengeFrame(_)); |
| 854 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 855 | } else { |
| 856 | EXPECT_CALL(framer_visitor_, OnPingFrame(_)); |
| 857 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 858 | } |
| 859 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 860 | } |
| 861 | // QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER); |
| 862 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 863 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | TEST_P(QuicPacketCreatorTest, SerializePathChallengeProbePacket) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 868 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 869 | return; |
| 870 | } |
| 871 | QuicPathFrameBuffer payload = { |
| 872 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 873 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 874 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 875 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 876 | |
| 877 | creator_.set_encryption_level(level); |
| 878 | |
| 879 | OwningSerializedPacketPointer encrypted( |
| 880 | creator_.SerializePathChallengeConnectivityProbingPacket(&payload)); |
| 881 | { |
| 882 | InSequence s; |
| 883 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 884 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 885 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 886 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 887 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 888 | EXPECT_CALL(framer_visitor_, OnPathChallengeFrame(_)); |
| 889 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 890 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 891 | } |
| 892 | // QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER); |
| 893 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 894 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | TEST_P(QuicPacketCreatorTest, SerializePathResponseProbePacket1PayloadPadded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 899 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | QuicPathFrameBuffer payload0 = { |
| 903 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 904 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 905 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 906 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 907 | creator_.set_encryption_level(level); |
| 908 | |
| 909 | QuicDeque<QuicPathFrameBuffer> payloads; |
| 910 | payloads.push_back(payload0); |
| 911 | |
| 912 | OwningSerializedPacketPointer encrypted( |
| 913 | creator_.SerializePathResponseConnectivityProbingPacket(payloads, |
| 914 | true)); |
| 915 | { |
| 916 | InSequence s; |
| 917 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 918 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 919 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 920 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 921 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 922 | EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)); |
| 923 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 924 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 925 | } |
| 926 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 927 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | TEST_P(QuicPacketCreatorTest, |
| 932 | SerializePathResponseProbePacket1PayloadUnPadded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 933 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 934 | return; |
| 935 | } |
| 936 | QuicPathFrameBuffer payload0 = { |
| 937 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 938 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 939 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 940 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 941 | creator_.set_encryption_level(level); |
| 942 | |
| 943 | QuicDeque<QuicPathFrameBuffer> payloads; |
| 944 | payloads.push_back(payload0); |
| 945 | |
| 946 | OwningSerializedPacketPointer encrypted( |
| 947 | creator_.SerializePathResponseConnectivityProbingPacket(payloads, |
| 948 | false)); |
| 949 | { |
| 950 | InSequence s; |
| 951 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 952 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 953 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 954 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 955 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 956 | EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)); |
| 957 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 958 | } |
| 959 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 960 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | TEST_P(QuicPacketCreatorTest, SerializePathResponseProbePacket2PayloadsPadded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 965 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 966 | return; |
| 967 | } |
| 968 | QuicPathFrameBuffer payload0 = { |
| 969 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 970 | QuicPathFrameBuffer payload1 = { |
| 971 | {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}}; |
| 972 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 973 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 974 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 975 | creator_.set_encryption_level(level); |
| 976 | |
| 977 | QuicDeque<QuicPathFrameBuffer> payloads; |
| 978 | payloads.push_back(payload0); |
| 979 | payloads.push_back(payload1); |
| 980 | |
| 981 | OwningSerializedPacketPointer encrypted( |
| 982 | creator_.SerializePathResponseConnectivityProbingPacket(payloads, |
| 983 | true)); |
| 984 | { |
| 985 | InSequence s; |
| 986 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 987 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 988 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 989 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 990 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 991 | EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(2); |
| 992 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 993 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 994 | } |
| 995 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 996 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | TEST_P(QuicPacketCreatorTest, |
| 1001 | SerializePathResponseProbePacket2PayloadsUnPadded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 1002 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1003 | return; |
| 1004 | } |
| 1005 | QuicPathFrameBuffer payload0 = { |
| 1006 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 1007 | QuicPathFrameBuffer payload1 = { |
| 1008 | {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}}; |
| 1009 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1010 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1011 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 1012 | creator_.set_encryption_level(level); |
| 1013 | |
| 1014 | QuicDeque<QuicPathFrameBuffer> payloads; |
| 1015 | payloads.push_back(payload0); |
| 1016 | payloads.push_back(payload1); |
| 1017 | |
| 1018 | OwningSerializedPacketPointer encrypted( |
| 1019 | creator_.SerializePathResponseConnectivityProbingPacket(payloads, |
| 1020 | false)); |
| 1021 | { |
| 1022 | InSequence s; |
| 1023 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1024 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1025 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1026 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1027 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1028 | EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(2); |
| 1029 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1030 | } |
| 1031 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 1032 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | TEST_P(QuicPacketCreatorTest, SerializePathResponseProbePacket3PayloadsPadded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 1037 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1038 | return; |
| 1039 | } |
| 1040 | QuicPathFrameBuffer payload0 = { |
| 1041 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 1042 | QuicPathFrameBuffer payload1 = { |
| 1043 | {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}}; |
| 1044 | QuicPathFrameBuffer payload2 = { |
| 1045 | {0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde, 0xad}}; |
| 1046 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1047 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1048 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 1049 | creator_.set_encryption_level(level); |
| 1050 | |
| 1051 | QuicDeque<QuicPathFrameBuffer> payloads; |
| 1052 | payloads.push_back(payload0); |
| 1053 | payloads.push_back(payload1); |
| 1054 | payloads.push_back(payload2); |
| 1055 | |
| 1056 | OwningSerializedPacketPointer encrypted( |
| 1057 | creator_.SerializePathResponseConnectivityProbingPacket(payloads, |
| 1058 | true)); |
| 1059 | { |
| 1060 | InSequence s; |
| 1061 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1062 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1063 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1064 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1065 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1066 | EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(3); |
| 1067 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1068 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1069 | } |
| 1070 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 1071 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | TEST_P(QuicPacketCreatorTest, |
| 1076 | SerializePathResponseProbePacket3PayloadsUnpadded) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 1077 | if (!VersionHasIetfQuicFrames(GetParam().version.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1078 | return; |
| 1079 | } |
| 1080 | QuicPathFrameBuffer payload0 = { |
| 1081 | {0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee}}; |
| 1082 | QuicPathFrameBuffer payload1 = { |
| 1083 | {0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde}}; |
| 1084 | QuicPathFrameBuffer payload2 = { |
| 1085 | {0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xee, 0xde, 0xad}}; |
| 1086 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1087 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; ++i) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1088 | EncryptionLevel level = static_cast<EncryptionLevel>(i); |
| 1089 | creator_.set_encryption_level(level); |
| 1090 | |
| 1091 | QuicDeque<QuicPathFrameBuffer> payloads; |
| 1092 | payloads.push_back(payload0); |
| 1093 | payloads.push_back(payload1); |
| 1094 | payloads.push_back(payload2); |
| 1095 | |
| 1096 | OwningSerializedPacketPointer encrypted( |
| 1097 | creator_.SerializePathResponseConnectivityProbingPacket(payloads, |
| 1098 | false)); |
| 1099 | InSequence s; |
| 1100 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1101 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1102 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1103 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1104 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1105 | EXPECT_CALL(framer_visitor_, OnPathResponseFrame(_)).Times(3); |
| 1106 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1107 | |
| 1108 | server_framer_.ProcessPacket(QuicEncryptedPacket( |
| 1109 | encrypted->encrypted_buffer, encrypted->encrypted_length)); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1114 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version) && |
fayang | 3ac15c1 | 2019-06-14 14:04:51 -0700 | [diff] [blame] | 1115 | !GetParam().version.SendsVariableLengthPacketNumberInLongHeader()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1116 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 1117 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1118 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1119 | } else { |
| 1120 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 1121 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1122 | } |
| 1123 | |
| 1124 | QuicPacketCreatorPeer::SetPacketNumber(&creator_, 64); |
| 1125 | creator_.UpdatePacketNumberLength(QuicPacketNumber(2), |
| 1126 | 10000 / kDefaultMaxPacketSize); |
| 1127 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 1128 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1129 | |
| 1130 | QuicPacketCreatorPeer::SetPacketNumber(&creator_, 64 * 256); |
| 1131 | creator_.UpdatePacketNumberLength(QuicPacketNumber(2), |
| 1132 | 10000 / kDefaultMaxPacketSize); |
| 1133 | EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, |
| 1134 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1135 | |
| 1136 | QuicPacketCreatorPeer::SetPacketNumber(&creator_, 64 * 256 * 256); |
| 1137 | creator_.UpdatePacketNumberLength(QuicPacketNumber(2), |
| 1138 | 10000 / kDefaultMaxPacketSize); |
| 1139 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 1140 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1141 | |
| 1142 | QuicPacketCreatorPeer::SetPacketNumber(&creator_, |
| 1143 | UINT64_C(64) * 256 * 256 * 256 * 256); |
| 1144 | creator_.UpdatePacketNumberLength(QuicPacketNumber(2), |
| 1145 | 10000 / kDefaultMaxPacketSize); |
| 1146 | EXPECT_EQ(PACKET_6BYTE_PACKET_NUMBER, |
| 1147 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1148 | } |
| 1149 | |
| 1150 | TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthCwnd) { |
| 1151 | QuicPacketCreatorPeer::SetPacketNumber(&creator_, 1); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1152 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version) && |
fayang | 3ac15c1 | 2019-06-14 14:04:51 -0700 | [diff] [blame] | 1153 | !GetParam().version.SendsVariableLengthPacketNumberInLongHeader()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1154 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 1155 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1156 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1157 | } else { |
| 1158 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 1159 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1160 | } |
| 1161 | |
| 1162 | creator_.UpdatePacketNumberLength(QuicPacketNumber(1), |
| 1163 | 10000 / kDefaultMaxPacketSize); |
| 1164 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 1165 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1166 | |
| 1167 | creator_.UpdatePacketNumberLength(QuicPacketNumber(1), |
| 1168 | 10000 * 256 / kDefaultMaxPacketSize); |
| 1169 | EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, |
| 1170 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1171 | |
| 1172 | creator_.UpdatePacketNumberLength(QuicPacketNumber(1), |
| 1173 | 10000 * 256 * 256 / kDefaultMaxPacketSize); |
| 1174 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 1175 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1176 | |
| 1177 | creator_.UpdatePacketNumberLength( |
| 1178 | QuicPacketNumber(1), |
| 1179 | UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize); |
| 1180 | EXPECT_EQ(PACKET_6BYTE_PACKET_NUMBER, |
| 1181 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1182 | } |
| 1183 | |
fayang | 4c1c236 | 2019-09-13 07:20:01 -0700 | [diff] [blame] | 1184 | TEST_P(QuicPacketCreatorTest, SkipNPacketNumbers) { |
| 1185 | QuicPacketCreatorPeer::SetPacketNumber(&creator_, 1); |
| 1186 | if (VersionHasIetfInvariantHeader(GetParam().version.transport_version) && |
| 1187 | !GetParam().version.SendsVariableLengthPacketNumberInLongHeader()) { |
| 1188 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 1189 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1190 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1191 | } else { |
| 1192 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 1193 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1194 | } |
| 1195 | creator_.SkipNPacketNumbers(63, QuicPacketNumber(2), |
| 1196 | 10000 / kDefaultMaxPacketSize); |
| 1197 | EXPECT_EQ(QuicPacketNumber(64), creator_.packet_number()); |
| 1198 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 1199 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1200 | |
| 1201 | creator_.SkipNPacketNumbers(64 * 255, QuicPacketNumber(2), |
| 1202 | 10000 / kDefaultMaxPacketSize); |
| 1203 | EXPECT_EQ(QuicPacketNumber(64 * 256), creator_.packet_number()); |
| 1204 | EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, |
| 1205 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1206 | |
| 1207 | creator_.SkipNPacketNumbers(64 * 256 * 255, QuicPacketNumber(2), |
| 1208 | 10000 / kDefaultMaxPacketSize); |
| 1209 | EXPECT_EQ(QuicPacketNumber(64 * 256 * 256), creator_.packet_number()); |
| 1210 | EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
| 1211 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
| 1212 | } |
| 1213 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1214 | TEST_P(QuicPacketCreatorTest, SerializeFrame) { |
| 1215 | if (!GetParam().version_serialization) { |
| 1216 | creator_.StopSendingVersion(); |
| 1217 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1218 | std::string data("test data"); |
| 1219 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1220 | QuicStreamFrame stream_frame( |
| 1221 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 1222 | /*fin=*/false, 0u, QuicStringPiece()); |
| 1223 | frames_.push_back(QuicFrame(stream_frame)); |
| 1224 | } else { |
| 1225 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 1226 | frames_.push_back( |
| 1227 | QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length()))); |
| 1228 | } |
| 1229 | SerializedPacket serialized = SerializeAllFrames(frames_); |
| 1230 | |
| 1231 | QuicPacketHeader header; |
| 1232 | { |
| 1233 | InSequence s; |
| 1234 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1235 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1236 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1237 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1238 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)) |
| 1239 | .WillOnce(DoAll(SaveArg<0>(&header), Return(true))); |
| 1240 | if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1241 | EXPECT_CALL(framer_visitor_, OnCryptoFrame(_)); |
| 1242 | } else { |
| 1243 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1244 | } |
| 1245 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1246 | } |
| 1247 | ProcessPacket(serialized); |
| 1248 | EXPECT_EQ(GetParam().version_serialization, header.version_flag); |
| 1249 | DeleteFrames(&frames_); |
| 1250 | } |
| 1251 | |
| 1252 | TEST_P(QuicPacketCreatorTest, SerializeFrameShortData) { |
| 1253 | if (!GetParam().version_serialization) { |
| 1254 | creator_.StopSendingVersion(); |
| 1255 | } |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1256 | std::string data("a"); |
| 1257 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1258 | QuicStreamFrame stream_frame( |
| 1259 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 1260 | /*fin=*/false, 0u, QuicStringPiece()); |
| 1261 | frames_.push_back(QuicFrame(stream_frame)); |
| 1262 | } else { |
| 1263 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 1264 | frames_.push_back( |
| 1265 | QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length()))); |
| 1266 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1267 | SerializedPacket serialized = SerializeAllFrames(frames_); |
| 1268 | |
| 1269 | QuicPacketHeader header; |
| 1270 | { |
| 1271 | InSequence s; |
| 1272 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1273 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1274 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1275 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1276 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)) |
| 1277 | .WillOnce(DoAll(SaveArg<0>(&header), Return(true))); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1278 | if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1279 | EXPECT_CALL(framer_visitor_, OnCryptoFrame(_)); |
| 1280 | } else { |
| 1281 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1282 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1283 | if (client_framer_.version().HasHeaderProtection()) { |
| 1284 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1285 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1286 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1287 | } |
| 1288 | ProcessPacket(serialized); |
| 1289 | EXPECT_EQ(GetParam().version_serialization, header.version_flag); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1290 | DeleteFrames(&frames_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | TEST_P(QuicPacketCreatorTest, ConsumeDataLargerThanOneStreamFrame) { |
| 1294 | if (!GetParam().version_serialization) { |
| 1295 | creator_.StopSendingVersion(); |
| 1296 | } |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1297 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1298 | // A string larger than fits into a frame. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1299 | QuicFrame frame; |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 1300 | size_t payload_length = creator_.max_packet_length(); |
| 1301 | const std::string too_long_payload(payload_length, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1302 | MakeIOVector(too_long_payload, &iov_); |
| 1303 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1304 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1305 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1306 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 1307 | ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, |
| 1308 | true, false, NOT_RETRANSMISSION, &frame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1309 | size_t consumed = frame.stream_frame.data_length; |
ianswett | 3085da8 | 2019-04-04 07:24:24 -0700 | [diff] [blame] | 1310 | // The entire payload could not be consumed. |
| 1311 | EXPECT_GT(payload_length, consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1312 | creator_.Flush(); |
| 1313 | DeleteSerializedPacket(); |
| 1314 | } |
| 1315 | |
| 1316 | TEST_P(QuicPacketCreatorTest, AddFrameAndFlush) { |
| 1317 | if (!GetParam().version_serialization) { |
| 1318 | creator_.StopSendingVersion(); |
| 1319 | } |
| 1320 | const size_t max_plaintext_size = |
| 1321 | client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()); |
| 1322 | EXPECT_FALSE(creator_.HasPendingFrames()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1323 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1324 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1325 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 1326 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1327 | stream_id = |
| 1328 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()); |
| 1329 | } |
| 1330 | EXPECT_FALSE(creator_.HasPendingStreamFramesOfStream(stream_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1331 | EXPECT_EQ(max_plaintext_size - |
| 1332 | GetPacketHeaderSize( |
| 1333 | client_framer_.transport_version(), |
| 1334 | creator_.GetDestinationConnectionIdLength(), |
| 1335 | creator_.GetSourceConnectionIdLength(), |
| 1336 | QuicPacketCreatorPeer::SendVersionInPacket(&creator_), |
| 1337 | !kIncludeDiversificationNonce, |
| 1338 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_), |
| 1339 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(&creator_), |
| 1340 | 0, QuicPacketCreatorPeer::GetLengthLength(&creator_)), |
| 1341 | creator_.BytesFree()); |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 1342 | StrictMock<MockDebugDelegate> debug; |
| 1343 | creator_.set_debug_delegate(&debug); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1344 | |
| 1345 | // Add a variety of frame types and then a padding frame. |
| 1346 | QuicAckFrame ack_frame(InitAckFrame(10u)); |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 1347 | EXPECT_CALL(debug, OnFrameAddedToPacket(_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1348 | EXPECT_TRUE( |
| 1349 | creator_.AddSavedFrame(QuicFrame(&ack_frame), NOT_RETRANSMISSION)); |
| 1350 | EXPECT_TRUE(creator_.HasPendingFrames()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1351 | EXPECT_FALSE(creator_.HasPendingStreamFramesOfStream(stream_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1352 | |
| 1353 | QuicFrame frame; |
| 1354 | MakeIOVector("test", &iov_); |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 1355 | EXPECT_CALL(debug, OnFrameAddedToPacket(_)); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1356 | ASSERT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, |
| 1357 | false, false, NOT_RETRANSMISSION, &frame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1358 | size_t consumed = frame.stream_frame.data_length; |
| 1359 | EXPECT_EQ(4u, consumed); |
| 1360 | EXPECT_TRUE(creator_.HasPendingFrames()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1361 | EXPECT_TRUE(creator_.HasPendingStreamFramesOfStream(stream_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1362 | |
| 1363 | QuicPaddingFrame padding_frame; |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 1364 | EXPECT_CALL(debug, OnFrameAddedToPacket(_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1365 | EXPECT_TRUE( |
| 1366 | creator_.AddSavedFrame(QuicFrame(padding_frame), NOT_RETRANSMISSION)); |
| 1367 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 1368 | EXPECT_EQ(0u, creator_.BytesFree()); |
| 1369 | |
| 1370 | // Packet is full. Creator will flush. |
| 1371 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1372 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1373 | EXPECT_FALSE( |
| 1374 | creator_.AddSavedFrame(QuicFrame(&ack_frame), NOT_RETRANSMISSION)); |
| 1375 | |
| 1376 | // Ensure the packet is successfully created. |
| 1377 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 1378 | ASSERT_FALSE(serialized_packet_.retransmittable_frames.empty()); |
| 1379 | const QuicFrames& retransmittable = serialized_packet_.retransmittable_frames; |
| 1380 | ASSERT_EQ(1u, retransmittable.size()); |
| 1381 | EXPECT_EQ(STREAM_FRAME, retransmittable[0].type); |
| 1382 | EXPECT_TRUE(serialized_packet_.has_ack); |
| 1383 | EXPECT_EQ(QuicPacketNumber(10u), serialized_packet_.largest_acked); |
| 1384 | DeleteSerializedPacket(); |
| 1385 | |
| 1386 | EXPECT_FALSE(creator_.HasPendingFrames()); |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 1387 | EXPECT_FALSE(creator_.HasPendingStreamFramesOfStream(stream_id)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1388 | EXPECT_EQ(max_plaintext_size - |
| 1389 | GetPacketHeaderSize( |
| 1390 | client_framer_.transport_version(), |
| 1391 | creator_.GetDestinationConnectionIdLength(), |
| 1392 | creator_.GetSourceConnectionIdLength(), |
| 1393 | QuicPacketCreatorPeer::SendVersionInPacket(&creator_), |
| 1394 | !kIncludeDiversificationNonce, |
| 1395 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_), |
| 1396 | QuicPacketCreatorPeer::GetRetryTokenLengthLength(&creator_), |
| 1397 | 0, QuicPacketCreatorPeer::GetLengthLength(&creator_)), |
| 1398 | creator_.BytesFree()); |
| 1399 | } |
| 1400 | |
| 1401 | TEST_P(QuicPacketCreatorTest, SerializeAndSendStreamFrame) { |
| 1402 | if (!GetParam().version_serialization) { |
| 1403 | creator_.StopSendingVersion(); |
| 1404 | } |
| 1405 | EXPECT_FALSE(creator_.HasPendingFrames()); |
| 1406 | |
| 1407 | MakeIOVector("test", &iov_); |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1408 | producer_.SaveStreamData(GetNthClientInitiatedStreamId(0), &iov_, 1u, 0u, |
| 1409 | iov_.iov_len); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1410 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1411 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1412 | size_t num_bytes_consumed; |
rch | c76cd74 | 2019-03-26 16:00:03 -0700 | [diff] [blame] | 1413 | StrictMock<MockDebugDelegate> debug; |
| 1414 | creator_.set_debug_delegate(&debug); |
| 1415 | EXPECT_CALL(debug, OnFrameAddedToPacket(_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1416 | creator_.CreateAndSerializeStreamFrame( |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1417 | GetNthClientInitiatedStreamId(0), iov_.iov_len, 0, 0, true, |
| 1418 | NOT_RETRANSMISSION, &num_bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1419 | EXPECT_EQ(4u, num_bytes_consumed); |
| 1420 | |
| 1421 | // Ensure the packet is successfully created. |
| 1422 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 1423 | ASSERT_FALSE(serialized_packet_.retransmittable_frames.empty()); |
| 1424 | const QuicFrames& retransmittable = serialized_packet_.retransmittable_frames; |
| 1425 | ASSERT_EQ(1u, retransmittable.size()); |
| 1426 | EXPECT_EQ(STREAM_FRAME, retransmittable[0].type); |
| 1427 | DeleteSerializedPacket(); |
| 1428 | |
| 1429 | EXPECT_FALSE(creator_.HasPendingFrames()); |
| 1430 | } |
| 1431 | |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 1432 | TEST_P(QuicPacketCreatorTest, SerializeStreamFrameWithPadding) { |
| 1433 | // Regression test to check that CreateAndSerializeStreamFrame uses a |
| 1434 | // correctly formatted stream frame header when appending padding. |
| 1435 | |
| 1436 | if (!GetParam().version_serialization) { |
| 1437 | creator_.StopSendingVersion(); |
| 1438 | } |
| 1439 | EXPECT_FALSE(creator_.HasPendingFrames()); |
| 1440 | |
| 1441 | // Send one byte of stream data. |
| 1442 | MakeIOVector("a", &iov_); |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1443 | producer_.SaveStreamData(GetNthClientInitiatedStreamId(0), &iov_, 1u, 0u, |
| 1444 | iov_.iov_len); |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 1445 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1446 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1447 | size_t num_bytes_consumed; |
| 1448 | creator_.CreateAndSerializeStreamFrame( |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1449 | GetNthClientInitiatedStreamId(0), iov_.iov_len, 0, 0, true, |
| 1450 | NOT_RETRANSMISSION, &num_bytes_consumed); |
nharper | ebabffd | 2019-06-03 17:34:45 -0700 | [diff] [blame] | 1451 | EXPECT_EQ(1u, num_bytes_consumed); |
| 1452 | |
| 1453 | // Check that a packet is created. |
| 1454 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 1455 | ASSERT_FALSE(serialized_packet_.retransmittable_frames.empty()); |
| 1456 | { |
| 1457 | InSequence s; |
| 1458 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1459 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1460 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1461 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1462 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1463 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1464 | if (client_framer_.version().HasHeaderProtection()) { |
| 1465 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1466 | } |
| 1467 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1468 | } |
| 1469 | ProcessPacket(serialized_packet_); |
| 1470 | } |
| 1471 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1472 | TEST_P(QuicPacketCreatorTest, AddUnencryptedStreamDataClosesConnection) { |
| 1473 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 1474 | if (!IsDefaultTestConfiguration()) { |
| 1475 | return; |
| 1476 | } |
| 1477 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1478 | creator_.set_encryption_level(ENCRYPTION_INITIAL); |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 1479 | EXPECT_CALL(delegate_, OnUnrecoverableError(_, _)); |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1480 | QuicStreamFrame stream_frame(GetNthClientInitiatedStreamId(0), |
| 1481 | /*fin=*/false, 0u, QuicStringPiece()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1482 | EXPECT_QUIC_BUG( |
| 1483 | creator_.AddSavedFrame(QuicFrame(stream_frame), NOT_RETRANSMISSION), |
fayang | 4952323 | 2019-05-03 06:28:22 -0700 | [diff] [blame] | 1484 | "Cannot send stream data with level: ENCRYPTION_INITIAL"); |
| 1485 | } |
| 1486 | |
| 1487 | TEST_P(QuicPacketCreatorTest, SendStreamDataWithEncryptionHandshake) { |
| 1488 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 1489 | if (!IsDefaultTestConfiguration()) { |
| 1490 | return; |
| 1491 | } |
| 1492 | |
| 1493 | creator_.set_encryption_level(ENCRYPTION_HANDSHAKE); |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 1494 | EXPECT_CALL(delegate_, OnUnrecoverableError(_, _)); |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 1495 | QuicStreamFrame stream_frame(GetNthClientInitiatedStreamId(0), |
| 1496 | /*fin=*/false, 0u, QuicStringPiece()); |
fayang | 4952323 | 2019-05-03 06:28:22 -0700 | [diff] [blame] | 1497 | EXPECT_QUIC_BUG( |
| 1498 | creator_.AddSavedFrame(QuicFrame(stream_frame), NOT_RETRANSMISSION), |
| 1499 | "Cannot send stream data with level: ENCRYPTION_HANDSHAKE"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | TEST_P(QuicPacketCreatorTest, ChloTooLarge) { |
| 1503 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 1504 | if (!IsDefaultTestConfiguration()) { |
| 1505 | return; |
| 1506 | } |
| 1507 | |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1508 | // This test only matters when the crypto handshake is sent in stream frames. |
| 1509 | // TODO(b/128596274): Re-enable when this check is supported for CRYPTO |
| 1510 | // frames. |
| 1511 | if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1512 | return; |
| 1513 | } |
| 1514 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1515 | CryptoHandshakeMessage message; |
| 1516 | message.set_tag(kCHLO); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1517 | message.set_minimum_size(kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1518 | CryptoFramer framer; |
| 1519 | std::unique_ptr<QuicData> message_data; |
QUICHE team | 3fe6a8b | 2019-03-14 09:10:38 -0700 | [diff] [blame] | 1520 | message_data = framer.ConstructHandshakeMessage(message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1521 | |
| 1522 | struct iovec iov; |
| 1523 | MakeIOVector(QuicStringPiece(message_data->data(), message_data->length()), |
| 1524 | &iov); |
| 1525 | QuicFrame frame; |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 1526 | EXPECT_CALL(delegate_, OnUnrecoverableError(QUIC_CRYPTO_CHLO_TOO_LARGE, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1527 | EXPECT_QUIC_BUG(creator_.ConsumeData(QuicUtils::GetCryptoStreamId( |
| 1528 | client_framer_.transport_version()), |
| 1529 | &iov, 1u, iov.iov_len, 0u, 0u, false, |
| 1530 | false, NOT_RETRANSMISSION, &frame), |
| 1531 | "Client hello won't fit in a single packet."); |
| 1532 | } |
| 1533 | |
| 1534 | TEST_P(QuicPacketCreatorTest, PendingPadding) { |
| 1535 | EXPECT_EQ(0u, creator_.pending_padding_bytes()); |
| 1536 | creator_.AddPendingPadding(kMaxNumRandomPaddingBytes * 10); |
| 1537 | EXPECT_EQ(kMaxNumRandomPaddingBytes * 10, creator_.pending_padding_bytes()); |
| 1538 | |
| 1539 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1540 | .WillRepeatedly( |
| 1541 | Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1542 | // Flush all paddings. |
| 1543 | while (creator_.pending_padding_bytes() > 0) { |
| 1544 | creator_.Flush(); |
| 1545 | { |
| 1546 | InSequence s; |
| 1547 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1548 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1549 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1550 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1551 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1552 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1553 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1554 | } |
| 1555 | // Packet only contains padding. |
| 1556 | ProcessPacket(serialized_packet_); |
| 1557 | } |
| 1558 | EXPECT_EQ(0u, creator_.pending_padding_bytes()); |
| 1559 | } |
| 1560 | |
| 1561 | TEST_P(QuicPacketCreatorTest, FullPaddingDoesNotConsumePendingPadding) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1562 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1563 | creator_.AddPendingPadding(kMaxNumRandomPaddingBytes); |
| 1564 | QuicFrame frame; |
| 1565 | MakeIOVector("test", &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1566 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1567 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1568 | ASSERT_TRUE(creator_.ConsumeData( |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1569 | stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1570 | /*needs_full_padding=*/true, NOT_RETRANSMISSION, &frame)); |
| 1571 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1572 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1573 | creator_.Flush(); |
| 1574 | EXPECT_EQ(kMaxNumRandomPaddingBytes, creator_.pending_padding_bytes()); |
| 1575 | } |
| 1576 | |
| 1577 | TEST_P(QuicPacketCreatorTest, SendPendingPaddingInRetransmission) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1578 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
| 1579 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1580 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 1581 | QuicStreamFrame stream_frame(stream_id, |
| 1582 | /*fin=*/false, 0u, QuicStringPiece()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1583 | QuicFrames frames; |
| 1584 | frames.push_back(QuicFrame(stream_frame)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1585 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1586 | QuicPendingRetransmission retransmission(CreateRetransmission( |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1587 | frames, true, /*num_padding_bytes=*/0, ENCRYPTION_FORWARD_SECURE, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1588 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 1589 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1590 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1591 | creator_.AddPendingPadding(kMaxNumRandomPaddingBytes); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1592 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1593 | EXPECT_EQ(0u, creator_.pending_padding_bytes()); |
| 1594 | { |
| 1595 | InSequence s; |
| 1596 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1597 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1598 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1599 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1600 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1601 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1602 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1603 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1604 | } |
| 1605 | ProcessPacket(serialized_packet_); |
| 1606 | } |
| 1607 | |
| 1608 | TEST_P(QuicPacketCreatorTest, SendPacketAfterFullPaddingRetransmission) { |
| 1609 | // Making sure needs_full_padding gets reset after a full padding |
| 1610 | // retransmission. |
| 1611 | EXPECT_EQ(0u, creator_.pending_padding_bytes()); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1612 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1613 | QuicFrame frame; |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1614 | std::string data = "fake handshake message data"; |
| 1615 | MakeIOVector(data, &iov_); |
| 1616 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1617 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 1618 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1619 | stream_id = |
| 1620 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()); |
| 1621 | } |
| 1622 | producer_.SaveStreamData(stream_id, &iov_, 1u, 0u, iov_.iov_len); |
| 1623 | QuicPacketCreatorPeer::CreateStreamFrame(&creator_, stream_id, iov_.iov_len, |
| 1624 | 0u, false, &frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1625 | QuicFrames frames; |
| 1626 | frames.push_back(frame); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1627 | char buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1628 | QuicPendingRetransmission retransmission(CreateRetransmission( |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1629 | frames, true, /*num_padding_bytes=*/-1, ENCRYPTION_FORWARD_SECURE, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1630 | QuicPacketCreatorPeer::GetPacketNumberLength(&creator_))); |
| 1631 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1632 | .WillRepeatedly( |
| 1633 | Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1634 | creator_.ReserializeAllFrames(retransmission, buffer, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1635 | EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
| 1636 | { |
| 1637 | InSequence s; |
| 1638 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1639 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1640 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1641 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1642 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1643 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1644 | // Full padding. |
| 1645 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1646 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1647 | } |
| 1648 | ProcessPacket(serialized_packet_); |
| 1649 | |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1650 | creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false, false, |
| 1651 | NOT_RETRANSMISSION, &frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1652 | creator_.Flush(); |
| 1653 | { |
| 1654 | InSequence s; |
| 1655 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1656 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1657 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1658 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1659 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 1660 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1661 | // needs_full_padding gets reset. |
| 1662 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)).Times(0); |
| 1663 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1664 | } |
| 1665 | ProcessPacket(serialized_packet_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1666 | DeleteFrames(&frames); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | TEST_P(QuicPacketCreatorTest, ConsumeDataAndRandomPadding) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1670 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1671 | const QuicByteCount kStreamFramePayloadSize = 100u; |
| 1672 | // Set the packet size be enough for one stream frame with 0 stream offset + |
| 1673 | // 1. |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1674 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1675 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1676 | size_t length = |
| 1677 | GetPacketHeaderOverhead(client_framer_.transport_version()) + |
| 1678 | GetEncryptionOverhead() + |
| 1679 | QuicFramer::GetMinStreamFrameSize( |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1680 | client_framer_.transport_version(), stream_id, 0, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1681 | /*last_frame_in_packet=*/false, kStreamFramePayloadSize + 1) + |
| 1682 | kStreamFramePayloadSize + 1; |
| 1683 | creator_.SetMaxPacketLength(length); |
| 1684 | creator_.AddPendingPadding(kMaxNumRandomPaddingBytes); |
| 1685 | QuicByteCount pending_padding_bytes = creator_.pending_padding_bytes(); |
| 1686 | QuicFrame frame; |
| 1687 | char buf[kStreamFramePayloadSize + 1] = {}; |
| 1688 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1689 | .WillRepeatedly( |
| 1690 | Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1691 | // Send stream frame of size kStreamFramePayloadSize. |
| 1692 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize), &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1693 | creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false, false, |
| 1694 | NOT_RETRANSMISSION, &frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1695 | creator_.Flush(); |
| 1696 | // 1 byte padding is sent. |
| 1697 | EXPECT_EQ(pending_padding_bytes - 1, creator_.pending_padding_bytes()); |
| 1698 | // Send stream frame of size kStreamFramePayloadSize + 1. |
| 1699 | MakeIOVector(QuicStringPiece(buf, kStreamFramePayloadSize + 1), &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1700 | creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, |
| 1701 | kStreamFramePayloadSize, false, false, |
| 1702 | NOT_RETRANSMISSION, &frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1703 | // No padding is sent. |
| 1704 | creator_.Flush(); |
| 1705 | EXPECT_EQ(pending_padding_bytes - 1, creator_.pending_padding_bytes()); |
| 1706 | // Flush all paddings. |
| 1707 | while (creator_.pending_padding_bytes() > 0) { |
| 1708 | creator_.Flush(); |
| 1709 | } |
| 1710 | EXPECT_EQ(0u, creator_.pending_padding_bytes()); |
| 1711 | } |
| 1712 | |
| 1713 | TEST_P(QuicPacketCreatorTest, FlushWithExternalBuffer) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1714 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1715 | char external_buffer[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1716 | char* expected_buffer = external_buffer; |
| 1717 | EXPECT_CALL(delegate_, GetPacketBuffer()).WillOnce(Return(expected_buffer)); |
| 1718 | |
| 1719 | QuicFrame frame; |
| 1720 | MakeIOVector("test", &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1721 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1722 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1723 | ASSERT_TRUE(creator_.ConsumeData( |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1724 | stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, false, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1725 | /*needs_full_padding=*/true, NOT_RETRANSMISSION, &frame)); |
| 1726 | |
| 1727 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1728 | .WillOnce(Invoke([expected_buffer](SerializedPacket* serialized_packet) { |
| 1729 | EXPECT_EQ(expected_buffer, serialized_packet->encrypted_buffer); |
| 1730 | ClearSerializedPacket(serialized_packet); |
| 1731 | })); |
| 1732 | creator_.Flush(); |
| 1733 | } |
| 1734 | |
| 1735 | // Test for error found in |
| 1736 | // https://bugs.chromium.org/p/chromium/issues/detail?id=859949 where a gap |
| 1737 | // length that crosses an IETF VarInt length boundary would cause a |
| 1738 | // failure. While this test is not applicable to versions other than version 99, |
| 1739 | // it should still work. Hence, it is not made version-specific. |
| 1740 | TEST_P(QuicPacketCreatorTest, IetfAckGapErrorRegression) { |
| 1741 | QuicAckFrame ack_frame = |
| 1742 | InitAckFrame({{QuicPacketNumber(60), QuicPacketNumber(61)}, |
| 1743 | {QuicPacketNumber(125), QuicPacketNumber(126)}}); |
| 1744 | frames_.push_back(QuicFrame(&ack_frame)); |
| 1745 | SerializeAllFrames(frames_); |
| 1746 | } |
| 1747 | |
| 1748 | TEST_P(QuicPacketCreatorTest, AddMessageFrame) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1749 | if (!VersionSupportsMessageFrames(client_framer_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1750 | return; |
| 1751 | } |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1752 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1753 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1754 | .Times(3) |
| 1755 | .WillRepeatedly( |
| 1756 | Invoke(this, &QuicPacketCreatorTest::ClearSerializedPacketForTests)); |
| 1757 | QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
| 1758 | // Verify that there is enough room for the largest message payload. |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1759 | EXPECT_TRUE(creator_.HasRoomForMessageFrame( |
| 1760 | creator_.GetCurrentLargestMessagePayload())); |
| 1761 | std::string message(creator_.GetCurrentLargestMessagePayload(), 'a'); |
wub | 553a966 | 2019-03-28 20:13:23 -0700 | [diff] [blame] | 1762 | QuicMessageFrame* message_frame = |
| 1763 | new QuicMessageFrame(1, MakeSpan(&allocator_, message, &storage)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1764 | EXPECT_TRUE( |
| 1765 | creator_.AddSavedFrame(QuicFrame(message_frame), NOT_RETRANSMISSION)); |
| 1766 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 1767 | creator_.Flush(); |
| 1768 | |
wub | 553a966 | 2019-03-28 20:13:23 -0700 | [diff] [blame] | 1769 | QuicMessageFrame* frame2 = |
| 1770 | new QuicMessageFrame(2, MakeSpan(&allocator_, "message", &storage)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1771 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame2), NOT_RETRANSMISSION)); |
| 1772 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 1773 | // Verify if a new frame is added, 1 byte message length will be added. |
| 1774 | EXPECT_EQ(1u, creator_.ExpansionOnNewFrame()); |
wub | 553a966 | 2019-03-28 20:13:23 -0700 | [diff] [blame] | 1775 | QuicMessageFrame* frame3 = |
| 1776 | new QuicMessageFrame(3, MakeSpan(&allocator_, "message2", &storage)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1777 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame3), NOT_RETRANSMISSION)); |
| 1778 | EXPECT_EQ(1u, creator_.ExpansionOnNewFrame()); |
| 1779 | creator_.Flush(); |
| 1780 | |
| 1781 | QuicFrame frame; |
| 1782 | MakeIOVector("test", &iov_); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1783 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1784 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 1785 | EXPECT_TRUE(creator_.ConsumeData(stream_id, &iov_, 1u, iov_.iov_len, 0u, 0u, |
| 1786 | false, false, NOT_RETRANSMISSION, &frame)); |
wub | 553a966 | 2019-03-28 20:13:23 -0700 | [diff] [blame] | 1787 | QuicMessageFrame* frame4 = |
| 1788 | new QuicMessageFrame(4, MakeSpan(&allocator_, "message", &storage)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1789 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame4), NOT_RETRANSMISSION)); |
| 1790 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 1791 | // Verify there is not enough room for largest payload. |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1792 | EXPECT_FALSE(creator_.HasRoomForMessageFrame( |
| 1793 | creator_.GetCurrentLargestMessagePayload())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1794 | // Add largest message will causes the flush of the stream frame. |
wub | 553a966 | 2019-03-28 20:13:23 -0700 | [diff] [blame] | 1795 | QuicMessageFrame frame5(5, MakeSpan(&allocator_, message, &storage)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1796 | EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&frame5), NOT_RETRANSMISSION)); |
| 1797 | EXPECT_FALSE(creator_.HasPendingFrames()); |
| 1798 | } |
| 1799 | |
| 1800 | TEST_P(QuicPacketCreatorTest, MessageFrameConsumption) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1801 | if (!VersionSupportsMessageFrames(client_framer_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1802 | return; |
| 1803 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1804 | std::string message_data(kDefaultMaxPacketSize, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1805 | QuicStringPiece message_buffer(message_data); |
| 1806 | QuicMemSliceStorage storage(nullptr, 0, nullptr, 0); |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1807 | // Test all possible encryption levels of message frames. |
| 1808 | for (EncryptionLevel level : |
| 1809 | {ENCRYPTION_ZERO_RTT, ENCRYPTION_FORWARD_SECURE}) { |
| 1810 | creator_.set_encryption_level(level); |
| 1811 | // Test all possible sizes of message frames. |
| 1812 | for (size_t message_size = 0; |
| 1813 | message_size <= creator_.GetCurrentLargestMessagePayload(); |
| 1814 | ++message_size) { |
| 1815 | QuicMessageFrame* frame = new QuicMessageFrame( |
| 1816 | 0, MakeSpan(&allocator_, |
| 1817 | QuicStringPiece(message_buffer.data(), message_size), |
| 1818 | &storage)); |
| 1819 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(frame), NOT_RETRANSMISSION)); |
| 1820 | EXPECT_TRUE(creator_.HasPendingFrames()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1821 | |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 1822 | size_t expansion_bytes = message_size >= 64 ? 2 : 1; |
| 1823 | EXPECT_EQ(expansion_bytes, creator_.ExpansionOnNewFrame()); |
| 1824 | // Verify BytesFree returns bytes available for the next frame, which |
| 1825 | // should subtract the message length. |
| 1826 | size_t expected_bytes_free = |
| 1827 | creator_.GetCurrentLargestMessagePayload() - message_size < |
| 1828 | expansion_bytes |
| 1829 | ? 0 |
| 1830 | : creator_.GetCurrentLargestMessagePayload() - expansion_bytes - |
| 1831 | message_size; |
| 1832 | EXPECT_EQ(expected_bytes_free, creator_.BytesFree()); |
| 1833 | EXPECT_LE(creator_.GetGuaranteedLargestMessagePayload(), |
| 1834 | creator_.GetCurrentLargestMessagePayload()); |
| 1835 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1836 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1837 | creator_.Flush(); |
| 1838 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 1839 | DeleteSerializedPacket(); |
| 1840 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 1844 | // Regression test for bugfix of GetPacketHeaderSize. |
| 1845 | TEST_P(QuicPacketCreatorTest, GetGuaranteedLargestMessagePayload) { |
| 1846 | QuicTransportVersion version = GetParam().version.transport_version; |
| 1847 | if (!VersionSupportsMessageFrames(version)) { |
| 1848 | return; |
| 1849 | } |
| 1850 | QuicPacketLength expected_largest_payload = 1319; |
| 1851 | if (QuicVersionHasLongHeaderLengths(version)) { |
| 1852 | expected_largest_payload -= 2; |
| 1853 | } |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 1854 | if (GetParam().version.HasLengthPrefixedConnectionIds()) { |
| 1855 | expected_largest_payload -= 1; |
| 1856 | } |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 1857 | EXPECT_EQ(expected_largest_payload, |
| 1858 | creator_.GetGuaranteedLargestMessagePayload()); |
| 1859 | } |
| 1860 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1861 | TEST_P(QuicPacketCreatorTest, PacketTransmissionType) { |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1862 | creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1863 | creator_.set_can_set_transmission_type(true); |
| 1864 | creator_.SetTransmissionType(NOT_RETRANSMISSION); |
| 1865 | |
| 1866 | QuicAckFrame temp_ack_frame = InitAckFrame(1); |
| 1867 | QuicFrame ack_frame(&temp_ack_frame); |
| 1868 | ASSERT_FALSE(QuicUtils::IsRetransmittableFrame(ack_frame.type)); |
| 1869 | |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1870 | QuicStreamId stream_id = QuicUtils::GetFirstBidirectionalStreamId( |
| 1871 | client_framer_.transport_version(), Perspective::IS_CLIENT); |
| 1872 | QuicFrame stream_frame(QuicStreamFrame(stream_id, |
| 1873 | /*fin=*/false, 0u, QuicStringPiece())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1874 | ASSERT_TRUE(QuicUtils::IsRetransmittableFrame(stream_frame.type)); |
| 1875 | |
| 1876 | QuicFrame padding_frame{QuicPaddingFrame()}; |
| 1877 | ASSERT_FALSE(QuicUtils::IsRetransmittableFrame(padding_frame.type)); |
| 1878 | |
| 1879 | EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
| 1880 | .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
| 1881 | |
| 1882 | EXPECT_TRUE(creator_.AddSavedFrame(ack_frame, LOSS_RETRANSMISSION)); |
| 1883 | ASSERT_FALSE(serialized_packet_.encrypted_buffer); |
| 1884 | |
| 1885 | EXPECT_TRUE(creator_.AddSavedFrame(stream_frame, RTO_RETRANSMISSION)); |
| 1886 | ASSERT_FALSE(serialized_packet_.encrypted_buffer); |
| 1887 | |
| 1888 | EXPECT_TRUE(creator_.AddSavedFrame(padding_frame, TLP_RETRANSMISSION)); |
| 1889 | creator_.Flush(); |
| 1890 | ASSERT_TRUE(serialized_packet_.encrypted_buffer); |
| 1891 | |
wub | 98669f5 | 2019-04-18 10:49:18 -0700 | [diff] [blame] | 1892 | if (creator_.can_set_transmission_type()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1893 | // The last retransmittable frame on packet is a stream frame, the packet's |
| 1894 | // transmission type should be the same as the stream frame's. |
| 1895 | EXPECT_EQ(serialized_packet_.transmission_type, RTO_RETRANSMISSION); |
| 1896 | } else { |
| 1897 | EXPECT_EQ(serialized_packet_.transmission_type, NOT_RETRANSMISSION); |
| 1898 | } |
| 1899 | DeleteSerializedPacket(); |
| 1900 | } |
| 1901 | |
| 1902 | TEST_P(QuicPacketCreatorTest, RetryToken) { |
| 1903 | if (!GetParam().version_serialization || |
| 1904 | !QuicVersionHasLongHeaderLengths(client_framer_.transport_version())) { |
| 1905 | return; |
| 1906 | } |
| 1907 | |
| 1908 | char retry_token_bytes[] = {1, 2, 3, 4, 5, 6, 7, 8, |
| 1909 | 9, 10, 11, 12, 13, 14, 15, 16}; |
| 1910 | |
| 1911 | creator_.SetRetryToken( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1912 | std::string(retry_token_bytes, sizeof(retry_token_bytes))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1913 | |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1914 | std::string data("a"); |
| 1915 | if (!QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1916 | QuicStreamFrame stream_frame( |
| 1917 | QuicUtils::GetCryptoStreamId(client_framer_.transport_version()), |
| 1918 | /*fin=*/false, 0u, QuicStringPiece()); |
| 1919 | frames_.push_back(QuicFrame(stream_frame)); |
| 1920 | } else { |
| 1921 | producer_.SaveCryptoData(ENCRYPTION_INITIAL, 0, data); |
| 1922 | frames_.push_back( |
| 1923 | QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, 0, data.length()))); |
| 1924 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1925 | SerializedPacket serialized = SerializeAllFrames(frames_); |
| 1926 | |
| 1927 | QuicPacketHeader header; |
| 1928 | { |
| 1929 | InSequence s; |
| 1930 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 1931 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 1932 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 1933 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 1934 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)) |
| 1935 | .WillOnce(DoAll(SaveArg<0>(&header), Return(true))); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1936 | if (QuicVersionUsesCryptoFrames(client_framer_.transport_version())) { |
| 1937 | EXPECT_CALL(framer_visitor_, OnCryptoFrame(_)); |
| 1938 | } else { |
| 1939 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 1940 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1941 | if (client_framer_.version().HasHeaderProtection()) { |
| 1942 | EXPECT_CALL(framer_visitor_, OnPaddingFrame(_)); |
| 1943 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1944 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 1945 | } |
| 1946 | ProcessPacket(serialized); |
| 1947 | ASSERT_TRUE(header.version_flag); |
| 1948 | ASSERT_EQ(header.long_packet_type, INITIAL); |
| 1949 | ASSERT_EQ(header.retry_token.length(), sizeof(retry_token_bytes)); |
| 1950 | test::CompareCharArraysWithHexError( |
| 1951 | "retry token", header.retry_token.data(), header.retry_token.length(), |
| 1952 | retry_token_bytes, sizeof(retry_token_bytes)); |
QUICHE team | dc41bf1 | 2019-03-20 12:58:42 -0700 | [diff] [blame] | 1953 | DeleteFrames(&frames_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1954 | } |
| 1955 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1956 | TEST_P(QuicPacketCreatorTest, GetConnectionId) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1957 | EXPECT_EQ(TestConnectionId(2), creator_.GetDestinationConnectionId()); |
| 1958 | EXPECT_EQ(EmptyQuicConnectionId(), creator_.GetSourceConnectionId()); |
| 1959 | } |
| 1960 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 1961 | TEST_P(QuicPacketCreatorTest, ClientConnectionId) { |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 1962 | if (!client_framer_.version().SupportsClientConnectionIds()) { |
| 1963 | return; |
| 1964 | } |
| 1965 | EXPECT_EQ(TestConnectionId(2), creator_.GetDestinationConnectionId()); |
| 1966 | EXPECT_EQ(EmptyQuicConnectionId(), creator_.GetSourceConnectionId()); |
| 1967 | creator_.SetClientConnectionId(TestConnectionId(0x33)); |
| 1968 | EXPECT_EQ(TestConnectionId(2), creator_.GetDestinationConnectionId()); |
| 1969 | EXPECT_EQ(TestConnectionId(0x33), creator_.GetSourceConnectionId()); |
| 1970 | } |
| 1971 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1972 | } // namespace |
| 1973 | } // namespace test |
| 1974 | } // namespace quic |