QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2013 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/http/quic_spdy_stream.h" |
| 6 | |
| 7 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
fayang | a45ee8a | 2020-03-20 08:56:11 -0700 | [diff] [blame^] | 11 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/http/http_encoder.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/http/spdy_utils.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_connection.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_stream_sequencer_buffer.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_write_blocked_list.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
bnc | 7e9155d | 2019-11-21 17:50:40 -0800 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/test_tools/qpack/qpack_test_utils.h" |
renjietang | bb2e22a | 2019-09-12 15:46:39 -0700 | [diff] [blame] | 24 | #include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h" |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 25 | #include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | #include "net/third_party/quiche/src/quic/test_tools/quic_flow_controller_peer.h" |
| 27 | #include "net/third_party/quiche/src/quic/test_tools/quic_session_peer.h" |
| 28 | #include "net/third_party/quiche/src/quic/test_tools/quic_spdy_session_peer.h" |
| 29 | #include "net/third_party/quiche/src/quic/test_tools/quic_spdy_stream_peer.h" |
| 30 | #include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h" |
| 31 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 32 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 33 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
| 34 | #include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | |
| 36 | using spdy::kV3HighestPriority; |
| 37 | using spdy::kV3LowestPriority; |
| 38 | using spdy::SpdyHeaderBlock; |
| 39 | using spdy::SpdyPriority; |
| 40 | using testing::_; |
| 41 | using testing::AtLeast; |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 42 | using testing::ElementsAre; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | using testing::Invoke; |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 44 | using testing::InvokeWithoutArgs; |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 45 | using testing::MatchesRegex; |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 46 | using testing::Pair; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 47 | using testing::Return; |
| 48 | using testing::StrictMock; |
| 49 | |
| 50 | namespace quic { |
| 51 | namespace test { |
| 52 | namespace { |
| 53 | |
| 54 | const bool kShouldProcessData = true; |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 55 | const char kDataFramePayload[] = "some data"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 57 | class TestCryptoStream : public QuicCryptoStream, public QuicCryptoHandshaker { |
| 58 | public: |
| 59 | explicit TestCryptoStream(QuicSession* session) |
| 60 | : QuicCryptoStream(session), |
| 61 | QuicCryptoHandshaker(this, session), |
| 62 | encryption_established_(false), |
| 63 | one_rtt_keys_available_(false), |
| 64 | params_(new QuicCryptoNegotiatedParameters) { |
| 65 | // Simulate a negotiated cipher_suite with a fake value. |
| 66 | params_->cipher_suite = 1; |
| 67 | } |
| 68 | |
| 69 | void OnHandshakeMessage(const CryptoHandshakeMessage& /*message*/) override { |
| 70 | encryption_established_ = true; |
| 71 | one_rtt_keys_available_ = true; |
| 72 | QuicErrorCode error; |
| 73 | std::string error_details; |
| 74 | session()->config()->SetInitialStreamFlowControlWindowToSend( |
| 75 | kInitialStreamFlowControlWindowForTest); |
| 76 | session()->config()->SetInitialSessionFlowControlWindowToSend( |
| 77 | kInitialSessionFlowControlWindowForTest); |
| 78 | if (session()->connection()->version().handshake_protocol == |
| 79 | PROTOCOL_TLS1_3) { |
| 80 | TransportParameters transport_parameters; |
| 81 | EXPECT_TRUE( |
| 82 | session()->config()->FillTransportParameters(&transport_parameters)); |
| 83 | error = session()->config()->ProcessTransportParameters( |
| 84 | transport_parameters, CLIENT, &error_details); |
| 85 | } else { |
| 86 | CryptoHandshakeMessage msg; |
| 87 | session()->config()->ToHandshakeMessage(&msg, transport_version()); |
| 88 | error = |
| 89 | session()->config()->ProcessPeerHello(msg, CLIENT, &error_details); |
| 90 | } |
| 91 | EXPECT_THAT(error, IsQuicNoError()); |
fayang | a45ee8a | 2020-03-20 08:56:11 -0700 | [diff] [blame^] | 92 | session()->OnNewEncryptionKeyAvailable( |
| 93 | ENCRYPTION_FORWARD_SECURE, |
| 94 | std::make_unique<NullEncrypter>(session()->perspective())); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 95 | session()->OnConfigNegotiated(); |
fayang | d18bfb9 | 2020-03-19 17:24:21 -0700 | [diff] [blame] | 96 | if (session()->connection()->version().handshake_protocol == |
| 97 | PROTOCOL_TLS1_3) { |
| 98 | session()->OnOneRttKeysAvailable(); |
| 99 | } else { |
| 100 | session()->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 101 | } |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 102 | session()->DiscardOldEncryptionKey(ENCRYPTION_INITIAL); |
| 103 | } |
| 104 | |
| 105 | // QuicCryptoStream implementation |
| 106 | bool encryption_established() const override { |
| 107 | return encryption_established_; |
| 108 | } |
| 109 | bool one_rtt_keys_available() const override { |
| 110 | return one_rtt_keys_available_; |
| 111 | } |
| 112 | HandshakeState GetHandshakeState() const override { |
| 113 | return one_rtt_keys_available() ? HANDSHAKE_COMPLETE : HANDSHAKE_START; |
| 114 | } |
| 115 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 116 | const override { |
| 117 | return *params_; |
| 118 | } |
| 119 | CryptoMessageParser* crypto_message_parser() override { |
| 120 | return QuicCryptoHandshaker::crypto_message_parser(); |
| 121 | } |
| 122 | void OnPacketDecrypted(EncryptionLevel /*level*/) override {} |
| 123 | void OnOneRttPacketAcknowledged() override {} |
| 124 | void OnHandshakeDoneReceived() override {} |
| 125 | |
| 126 | MOCK_METHOD0(OnCanWrite, void()); |
| 127 | |
| 128 | bool HasPendingCryptoRetransmission() const override { return false; } |
| 129 | |
| 130 | MOCK_CONST_METHOD0(HasPendingRetransmission, bool()); |
| 131 | |
| 132 | private: |
| 133 | using QuicCryptoStream::session; |
| 134 | |
| 135 | bool encryption_established_; |
| 136 | bool one_rtt_keys_available_; |
| 137 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_; |
| 138 | }; |
| 139 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 140 | class TestStream : public QuicSpdyStream { |
| 141 | public: |
| 142 | TestStream(QuicStreamId id, |
| 143 | QuicSpdySession* session, |
| 144 | bool should_process_data) |
| 145 | : QuicSpdyStream(id, session, BIDIRECTIONAL), |
| 146 | should_process_data_(should_process_data) {} |
| 147 | ~TestStream() override = default; |
| 148 | |
| 149 | using QuicSpdyStream::set_ack_listener; |
| 150 | using QuicStream::CloseWriteSide; |
| 151 | using QuicStream::WriteOrBufferData; |
| 152 | |
| 153 | void OnBodyAvailable() override { |
| 154 | if (!should_process_data_) { |
| 155 | return; |
| 156 | } |
| 157 | char buffer[2048]; |
| 158 | struct iovec vec; |
| 159 | vec.iov_base = buffer; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 160 | vec.iov_len = QUICHE_ARRAYSIZE(buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | size_t bytes_read = Readv(&vec, 1); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 162 | data_ += std::string(buffer, bytes_read); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | MOCK_METHOD1(WriteHeadersMock, void(bool fin)); |
| 166 | |
| 167 | size_t WriteHeadersImpl(spdy::SpdyHeaderBlock header_block, |
| 168 | bool fin, |
| 169 | QuicReferenceCountedPointer<QuicAckListenerInterface> |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 170 | /*ack_listener*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 171 | saved_headers_ = std::move(header_block); |
| 172 | WriteHeadersMock(fin); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 173 | if (VersionUsesHttp3(transport_version())) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 174 | // In this case, call QuicSpdyStream::WriteHeadersImpl() that does the |
| 175 | // actual work of closing the stream. |
| 176 | QuicSpdyStream::WriteHeadersImpl(saved_headers_.Clone(), fin, nullptr); |
| 177 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 181 | const std::string& data() const { return data_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 182 | const spdy::SpdyHeaderBlock& saved_headers() const { return saved_headers_; } |
| 183 | |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 184 | // Expose protected accessor. |
| 185 | const QuicStreamSequencer* sequencer() const { |
| 186 | return QuicStream::sequencer(); |
| 187 | } |
| 188 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 189 | private: |
| 190 | bool should_process_data_; |
| 191 | spdy::SpdyHeaderBlock saved_headers_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 192 | std::string data_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 193 | }; |
| 194 | |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 195 | class TestSession : public MockQuicSpdySession { |
| 196 | public: |
| 197 | explicit TestSession(QuicConnection* connection) |
| 198 | : MockQuicSpdySession(connection, /*create_mock_crypto_stream=*/false), |
| 199 | crypto_stream_(this) {} |
| 200 | |
| 201 | TestCryptoStream* GetMutableCryptoStream() override { |
| 202 | return &crypto_stream_; |
| 203 | } |
| 204 | |
| 205 | const TestCryptoStream* GetCryptoStream() const override { |
| 206 | return &crypto_stream_; |
| 207 | } |
| 208 | |
| 209 | private: |
| 210 | StrictMock<TestCryptoStream> crypto_stream_; |
| 211 | }; |
| 212 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | class TestMockUpdateStreamSession : public MockQuicSpdySession { |
| 214 | public: |
| 215 | explicit TestMockUpdateStreamSession(QuicConnection* connection) |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 216 | : MockQuicSpdySession(connection), |
| 217 | expected_precedence_( |
| 218 | spdy::SpdyStreamPrecedence(QuicStream::kDefaultPriority)) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 219 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 220 | void UpdateStreamPriority( |
| 221 | QuicStreamId id, |
| 222 | const spdy::SpdyStreamPrecedence& precedence) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 223 | EXPECT_EQ(id, expected_stream_->id()); |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 224 | EXPECT_EQ(expected_precedence_, precedence); |
| 225 | EXPECT_EQ(expected_precedence_, expected_stream_->precedence()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void SetExpectedStream(QuicSpdyStream* stream) { expected_stream_ = stream; } |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 229 | void SetExpectedPriority(const spdy::SpdyStreamPrecedence& precedence) { |
| 230 | expected_precedence_ = precedence; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private: |
| 234 | QuicSpdyStream* expected_stream_; |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 235 | spdy::SpdyStreamPrecedence expected_precedence_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | class QuicSpdyStreamTest : public QuicTestWithParam<ParsedQuicVersion> { |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 239 | protected: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 240 | QuicSpdyStreamTest() { |
| 241 | headers_[":host"] = "www.google.com"; |
| 242 | headers_[":path"] = "/index.hml"; |
| 243 | headers_[":scheme"] = "https"; |
| 244 | headers_["cookie"] = |
| 245 | "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; " |
| 246 | "__utmc=160408618; " |
| 247 | "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX" |
| 248 | "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX" |
| 249 | "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT" |
| 250 | "pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0" |
| 251 | "O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh" |
| 252 | "1zFMi5vzcns38-8_Sns; " |
| 253 | "GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-" |
| 254 | "yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339" |
| 255 | "47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c" |
| 256 | "v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%" |
| 257 | "2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4" |
| 258 | "SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1" |
| 259 | "3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP" |
| 260 | "ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6" |
| 261 | "edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b" |
| 262 | "Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6" |
| 263 | "QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG" |
| 264 | "tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk" |
| 265 | "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn" |
| 266 | "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr" |
| 267 | "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo "; |
| 268 | } |
| 269 | |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 270 | ~QuicSpdyStreamTest() override = default; |
| 271 | |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 272 | // Return QPACK-encoded header block without using the dynamic table. |
| 273 | std::string EncodeQpackHeaders( |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 274 | std::vector<std::pair<quiche::QuicheStringPiece, |
| 275 | quiche::QuicheStringPiece>> headers) { |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 276 | SpdyHeaderBlock header_block; |
| 277 | for (const auto& header_field : headers) { |
| 278 | header_block.AppendValueOrAddHeader(header_field.first, |
| 279 | header_field.second); |
| 280 | } |
| 281 | |
| 282 | return EncodeQpackHeaders(header_block); |
| 283 | } |
| 284 | |
| 285 | // Return QPACK-encoded header block without using the dynamic table. |
| 286 | std::string EncodeQpackHeaders(const SpdyHeaderBlock& header) { |
renjietang | c2aa5cb | 2019-06-20 12:22:53 -0700 | [diff] [blame] | 287 | NoopQpackStreamSenderDelegate encoder_stream_sender_delegate; |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 288 | auto qpack_encoder = std::make_unique<QpackEncoder>(session_.get()); |
renjietang | 8a2df8f | 2019-08-07 10:43:52 -0700 | [diff] [blame] | 289 | qpack_encoder->set_qpack_stream_sender_delegate( |
| 290 | &encoder_stream_sender_delegate); |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 291 | // QpackEncoder does not use the dynamic table by default, |
| 292 | // therefore the value of |stream_id| does not matter. |
bnc | 609c24e | 2019-09-10 05:24:32 -0700 | [diff] [blame] | 293 | return qpack_encoder->EncodeHeaderList(/* stream_id = */ 0, header, |
| 294 | nullptr); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 295 | } |
| 296 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 297 | void Initialize(bool stream_should_process_data) { |
renjietang | 7498c8c | 2019-07-02 19:28:42 -0700 | [diff] [blame] | 298 | InitializeWithPerspective(stream_should_process_data, |
| 299 | Perspective::IS_SERVER); |
| 300 | } |
| 301 | |
| 302 | void InitializeWithPerspective(bool stream_should_process_data, |
| 303 | Perspective perspective) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 304 | connection_ = new StrictMock<MockQuicConnection>( |
renjietang | 7498c8c | 2019-07-02 19:28:42 -0700 | [diff] [blame] | 305 | &helper_, &alarm_factory_, perspective, SupportedVersions(GetParam())); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 306 | session_ = std::make_unique<StrictMock<TestSession>>(connection_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 307 | session_->Initialize(); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 308 | ON_CALL(*session_, WritevData(_, _, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 309 | .WillByDefault( |
| 310 | Invoke(session_.get(), &MockQuicSpdySession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 311 | |
| 312 | stream_ = |
| 313 | new StrictMock<TestStream>(GetNthClientInitiatedBidirectionalId(0), |
| 314 | session_.get(), stream_should_process_data); |
| 315 | session_->ActivateStream(QuicWrapUnique(stream_)); |
| 316 | stream2_ = |
| 317 | new StrictMock<TestStream>(GetNthClientInitiatedBidirectionalId(1), |
| 318 | session_.get(), stream_should_process_data); |
| 319 | session_->ActivateStream(QuicWrapUnique(stream2_)); |
renjietang | bb2e22a | 2019-09-12 15:46:39 -0700 | [diff] [blame] | 320 | QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow( |
| 321 | session_->config(), kMinimumFlowControlSendWindow); |
dschinazi | 18cdf13 | 2019-10-09 16:08:18 -0700 | [diff] [blame] | 322 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesUnidirectional( |
| 323 | session_->config(), kMinimumFlowControlSendWindow); |
| 324 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional( |
| 325 | session_->config(), kMinimumFlowControlSendWindow); |
| 326 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesOutgoingBidirectional( |
| 327 | session_->config(), kMinimumFlowControlSendWindow); |
renjietang | e6d9467 | 2020-01-07 10:30:10 -0800 | [diff] [blame] | 328 | QuicConfigPeer::SetReceivedMaxUnidirectionalStreams(session_->config(), 10); |
renjietang | bb2e22a | 2019-09-12 15:46:39 -0700 | [diff] [blame] | 329 | session_->OnConfigNegotiated(); |
rch | ee32232 | 2019-10-11 14:56:03 -0700 | [diff] [blame] | 330 | if (UsesHttp3()) { |
renjietang | d6e461b | 2020-02-04 16:16:35 -0800 | [diff] [blame] | 331 | // The control stream will write the stream type, a greased frame, and |
| 332 | // SETTINGS frame. |
| 333 | int num_control_stream_writes = 3; |
rch | 4cd745d | 2019-12-03 13:19:41 -0800 | [diff] [blame] | 334 | if (session_->perspective() == Perspective::IS_CLIENT) { |
| 335 | // The control stream also writes the max push id frame. |
| 336 | num_control_stream_writes++; |
| 337 | } |
bnc | cd3c4b5 | 2020-01-10 11:13:18 -0800 | [diff] [blame] | 338 | auto send_control_stream = |
| 339 | QuicSpdySessionPeer::GetSendControlStream(session_.get()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 340 | EXPECT_CALL(*session_, |
| 341 | WritevData(send_control_stream->id(), _, _, _, _, _)) |
rch | 4cd745d | 2019-12-03 13:19:41 -0800 | [diff] [blame] | 342 | .Times(num_control_stream_writes); |
rch | ee32232 | 2019-10-11 14:56:03 -0700 | [diff] [blame] | 343 | auto qpack_decoder_stream = |
| 344 | QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get()); |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 345 | EXPECT_CALL(*session_, |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 346 | WritevData(qpack_decoder_stream->id(), 1, 0, _, _, _)); |
renjietang | 296cfa3 | 2019-11-27 12:49:46 -0800 | [diff] [blame] | 347 | auto qpack_encoder_stream = |
| 348 | QuicSpdySessionPeer::GetQpackEncoderSendStream(session_.get()); |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 349 | EXPECT_CALL(*session_, |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 350 | WritevData(qpack_encoder_stream->id(), 1, 0, _, _, _)); |
rch | ee32232 | 2019-10-11 14:56:03 -0700 | [diff] [blame] | 351 | } |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 352 | TestCryptoStream* crypto_stream = session_->GetMutableCryptoStream(); |
| 353 | EXPECT_CALL(*crypto_stream, HasPendingRetransmission()) |
| 354 | .Times(testing::AnyNumber()); |
| 355 | |
| 356 | if (connection_->version().HasHandshakeDone() && |
| 357 | session_->perspective() == Perspective::IS_SERVER) { |
| 358 | EXPECT_CALL(*connection_, SendControlFrame(_)) |
| 359 | .WillOnce(Invoke(&ClearControlFrame)); |
| 360 | } |
| 361 | CryptoHandshakeMessage message; |
| 362 | session_->GetMutableCryptoStream()->OnHandshakeMessage(message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | QuicHeaderList ProcessHeaders(bool fin, const SpdyHeaderBlock& headers) { |
| 366 | QuicHeaderList h = AsHeaderList(headers); |
| 367 | stream_->OnStreamHeaderList(fin, h.uncompressed_header_bytes(), h); |
| 368 | return h; |
| 369 | } |
| 370 | |
| 371 | QuicStreamId GetNthClientInitiatedBidirectionalId(int n) { |
| 372 | return GetNthClientInitiatedBidirectionalStreamId( |
| 373 | connection_->transport_version(), n); |
| 374 | } |
| 375 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 376 | bool UsesHttp3() const { |
| 377 | return VersionUsesHttp3(GetParam().transport_version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 378 | } |
| 379 | |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 380 | // Construct HEADERS frame with QPACK-encoded |headers| without using the |
| 381 | // dynamic table. |
| 382 | std::string HeadersFrame( |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 383 | std::vector<std::pair<quiche::QuicheStringPiece, |
| 384 | quiche::QuicheStringPiece>> headers) { |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 385 | return HeadersFrame(EncodeQpackHeaders(headers)); |
| 386 | } |
| 387 | |
| 388 | // Construct HEADERS frame with QPACK-encoded |headers| without using the |
| 389 | // dynamic table. |
| 390 | std::string HeadersFrame(const SpdyHeaderBlock& headers) { |
| 391 | return HeadersFrame(EncodeQpackHeaders(headers)); |
| 392 | } |
| 393 | |
| 394 | // Construct HEADERS frame with given payload. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 395 | std::string HeadersFrame(quiche::QuicheStringPiece payload) { |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 396 | std::unique_ptr<char[]> headers_buffer; |
| 397 | QuicByteCount headers_frame_header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 398 | HttpEncoder::SerializeHeadersFrameHeader(payload.length(), |
| 399 | &headers_buffer); |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 400 | quiche::QuicheStringPiece headers_frame_header(headers_buffer.get(), |
| 401 | headers_frame_header_length); |
| 402 | return quiche::QuicheStrCat(headers_frame_header, payload); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 403 | } |
| 404 | |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 405 | std::string DataFrame(quiche::QuicheStringPiece payload) { |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 406 | std::unique_ptr<char[]> data_buffer; |
| 407 | QuicByteCount data_frame_header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 408 | HttpEncoder::SerializeDataFrameHeader(payload.length(), &data_buffer); |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 409 | quiche::QuicheStringPiece data_frame_header(data_buffer.get(), |
| 410 | data_frame_header_length); |
| 411 | return quiche::QuicheStrCat(data_frame_header, payload); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 412 | } |
| 413 | |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 414 | std::string UnknownFrame(uint64_t frame_type, |
| 415 | quiche::QuicheStringPiece payload) { |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 416 | std::string frame; |
| 417 | const size_t length = QuicDataWriter::GetVarInt62Len(frame_type) + |
| 418 | QuicDataWriter::GetVarInt62Len(payload.size()) + |
| 419 | payload.size(); |
| 420 | frame.resize(length); |
| 421 | |
fayang | f0041ed | 2019-07-31 02:39:45 -0700 | [diff] [blame] | 422 | QuicDataWriter writer(length, const_cast<char*>(frame.data())); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 423 | writer.WriteVarInt62(frame_type); |
| 424 | writer.WriteStringPieceVarInt62(payload); |
| 425 | // Even though integers can be encoded with different lengths, |
| 426 | // QuicDataWriter is expected to produce an encoding in Write*() of length |
| 427 | // promised in GetVarInt62Len(). |
| 428 | DCHECK_EQ(length, writer.length()); |
| 429 | |
| 430 | return frame; |
| 431 | } |
| 432 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 433 | MockQuicConnectionHelper helper_; |
| 434 | MockAlarmFactory alarm_factory_; |
| 435 | MockQuicConnection* connection_; |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 436 | std::unique_ptr<TestSession> session_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 437 | |
| 438 | // Owned by the |session_|. |
| 439 | TestStream* stream_; |
| 440 | TestStream* stream2_; |
| 441 | |
| 442 | SpdyHeaderBlock headers_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 443 | }; |
| 444 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 445 | INSTANTIATE_TEST_SUITE_P(Tests, |
| 446 | QuicSpdyStreamTest, |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 447 | ::testing::ValuesIn(AllSupportedVersions()), |
| 448 | ::testing::PrintToStringParamName()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 449 | |
| 450 | TEST_P(QuicSpdyStreamTest, ProcessHeaderList) { |
| 451 | Initialize(kShouldProcessData); |
| 452 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 453 | stream_->OnStreamHeadersPriority( |
| 454 | spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 455 | ProcessHeaders(false, headers_); |
| 456 | EXPECT_EQ("", stream_->data()); |
| 457 | EXPECT_FALSE(stream_->header_list().empty()); |
| 458 | EXPECT_FALSE(stream_->IsDoneReading()); |
| 459 | } |
| 460 | |
| 461 | TEST_P(QuicSpdyStreamTest, ProcessTooLargeHeaderList) { |
| 462 | Initialize(kShouldProcessData); |
| 463 | |
bnc | 446887e | 2019-11-27 13:08:28 -0800 | [diff] [blame] | 464 | if (!UsesHttp3()) { |
| 465 | QuicHeaderList headers; |
| 466 | stream_->OnStreamHeadersPriority( |
| 467 | spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 468 | |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 469 | EXPECT_CALL(*session_, |
| 470 | SendRstStream(stream_->id(), QUIC_HEADERS_TOO_LARGE, 0)); |
bnc | 446887e | 2019-11-27 13:08:28 -0800 | [diff] [blame] | 471 | stream_->OnStreamHeaderList(false, 1 << 20, headers); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 472 | |
bnc | c576950 | 2019-11-27 10:01:44 -0800 | [diff] [blame] | 473 | EXPECT_THAT(stream_->stream_error(), IsStreamError(QUIC_HEADERS_TOO_LARGE)); |
bnc | 446887e | 2019-11-27 13:08:28 -0800 | [diff] [blame] | 474 | |
| 475 | return; |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 476 | } |
bnc | 446887e | 2019-11-27 13:08:28 -0800 | [diff] [blame] | 477 | |
| 478 | // Header list size includes 32 bytes for overhead per header field. |
| 479 | session_->set_max_inbound_header_list_size(40); |
| 480 | std::string headers = |
| 481 | HeadersFrame({std::make_pair("foo", "too long headers")}); |
| 482 | |
| 483 | QuicStreamFrame frame(stream_->id(), false, 0, headers); |
| 484 | |
| 485 | EXPECT_CALL( |
| 486 | *connection_, |
| 487 | CloseConnection(QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE, |
| 488 | MatchesRegex("Too large headers received on stream \\d+"), |
| 489 | _)); |
| 490 | |
| 491 | stream_->OnStreamFrame(frame); |
| 492 | |
| 493 | EXPECT_TRUE(stream_->header_list().empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | TEST_P(QuicSpdyStreamTest, ProcessHeaderListWithFin) { |
| 497 | Initialize(kShouldProcessData); |
| 498 | |
| 499 | size_t total_bytes = 0; |
| 500 | QuicHeaderList headers; |
| 501 | for (auto p : headers_) { |
| 502 | headers.OnHeader(p.first, p.second); |
| 503 | total_bytes += p.first.size() + p.second.size(); |
| 504 | } |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 505 | stream_->OnStreamHeadersPriority( |
| 506 | spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 507 | stream_->OnStreamHeaderList(true, total_bytes, headers); |
| 508 | EXPECT_EQ("", stream_->data()); |
| 509 | EXPECT_FALSE(stream_->header_list().empty()); |
| 510 | EXPECT_FALSE(stream_->IsDoneReading()); |
renjietang | 6c06656 | 2019-11-04 17:05:59 -0800 | [diff] [blame] | 511 | EXPECT_TRUE(stream_->HasReceivedFinalOffset()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 512 | } |
| 513 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 514 | // A valid status code should be 3-digit integer. The first digit should be in |
| 515 | // the range of [1, 5]. All the others are invalid. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 516 | TEST_P(QuicSpdyStreamTest, ParseHeaderStatusCode) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 517 | Initialize(kShouldProcessData); |
| 518 | int status_code = 0; |
| 519 | |
| 520 | // Valid status codes. |
| 521 | headers_[":status"] = "404"; |
| 522 | EXPECT_TRUE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 523 | EXPECT_EQ(404, status_code); |
| 524 | |
| 525 | headers_[":status"] = "100"; |
| 526 | EXPECT_TRUE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 527 | EXPECT_EQ(100, status_code); |
| 528 | |
| 529 | headers_[":status"] = "599"; |
| 530 | EXPECT_TRUE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 531 | EXPECT_EQ(599, status_code); |
| 532 | |
| 533 | // Invalid status codes. |
| 534 | headers_[":status"] = "010"; |
| 535 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 536 | |
| 537 | headers_[":status"] = "600"; |
| 538 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 539 | |
| 540 | headers_[":status"] = "200 ok"; |
| 541 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 542 | |
| 543 | headers_[":status"] = "2000"; |
| 544 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 545 | |
| 546 | headers_[":status"] = "+200"; |
| 547 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 548 | |
| 549 | headers_[":status"] = "+20"; |
| 550 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 551 | |
| 552 | headers_[":status"] = "-10"; |
| 553 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 554 | |
| 555 | headers_[":status"] = "-100"; |
| 556 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 557 | |
| 558 | // Leading or trailing spaces are also invalid. |
| 559 | headers_[":status"] = " 200"; |
| 560 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 561 | |
| 562 | headers_[":status"] = "200 "; |
| 563 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 564 | |
| 565 | headers_[":status"] = " 200 "; |
| 566 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 567 | |
| 568 | headers_[":status"] = " "; |
| 569 | EXPECT_FALSE(stream_->ParseHeaderStatusCode(headers_, &status_code)); |
| 570 | } |
| 571 | |
| 572 | TEST_P(QuicSpdyStreamTest, MarkHeadersConsumed) { |
| 573 | Initialize(kShouldProcessData); |
| 574 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 575 | std::string body = "this is the body"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 576 | QuicHeaderList headers = ProcessHeaders(false, headers_); |
| 577 | EXPECT_EQ(headers, stream_->header_list()); |
| 578 | |
| 579 | stream_->ConsumeHeaderList(); |
| 580 | EXPECT_EQ(QuicHeaderList(), stream_->header_list()); |
| 581 | } |
| 582 | |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 583 | TEST_P(QuicSpdyStreamTest, ProcessWrongFramesOnSpdyStream) { |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 584 | if (!UsesHttp3()) { |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 585 | return; |
| 586 | } |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 587 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 588 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 589 | testing::InSequence s; |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 590 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 591 | GoAwayFrame goaway; |
| 592 | goaway.stream_id = 0x1; |
| 593 | std::unique_ptr<char[]> buffer; |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 594 | QuicByteCount header_length = |
| 595 | HttpEncoder::SerializeGoAwayFrame(goaway, &buffer); |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 596 | std::string data = std::string(buffer.get(), header_length); |
| 597 | |
| 598 | EXPECT_EQ("", stream_->data()); |
| 599 | QuicHeaderList headers = ProcessHeaders(false, headers_); |
| 600 | EXPECT_EQ(headers, stream_->header_list()); |
| 601 | stream_->ConsumeHeaderList(); |
| 602 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 603 | quiche::QuicheStringPiece(data)); |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 604 | |
bnc | dfabdfb | 2020-01-17 17:46:40 -0800 | [diff] [blame] | 605 | EXPECT_CALL(*connection_, |
| 606 | CloseConnection(QUIC_HTTP_FRAME_UNEXPECTED_ON_SPDY_STREAM, _, _)) |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 607 | .WillOnce( |
| 608 | (Invoke([this](QuicErrorCode error, const std::string& error_details, |
| 609 | ConnectionCloseBehavior connection_close_behavior) { |
| 610 | connection_->ReallyCloseConnection(error, error_details, |
| 611 | connection_close_behavior); |
| 612 | }))); |
ianswett | dc1e7ab | 2019-05-03 16:10:44 -0700 | [diff] [blame] | 613 | EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 614 | EXPECT_CALL(*session_, OnConnectionClosed(_, _)) |
| 615 | .WillOnce(Invoke([this](const QuicConnectionCloseFrame& frame, |
| 616 | ConnectionCloseSource source) { |
| 617 | session_->ReallyOnConnectionClosed(frame, source); |
| 618 | })); |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 619 | EXPECT_CALL(*session_, SendRstStream(_, _, _)); |
| 620 | EXPECT_CALL(*session_, SendRstStream(_, _, _)); |
| 621 | |
| 622 | stream_->OnStreamFrame(frame); |
| 623 | } |
| 624 | |
bnc | dfabdfb | 2020-01-17 17:46:40 -0800 | [diff] [blame] | 625 | TEST_P(QuicSpdyStreamTest, Http3FrameError) { |
| 626 | if (!UsesHttp3()) { |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | Initialize(kShouldProcessData); |
| 631 | |
| 632 | // PUSH_PROMISE frame with empty payload is considered invalid. |
| 633 | std::string invalid_http3_frame = quiche::QuicheTextUtils::HexDecode("0500"); |
| 634 | QuicStreamFrame stream_frame(stream_->id(), /* fin = */ false, |
| 635 | /* offset = */ 0, invalid_http3_frame); |
| 636 | |
| 637 | EXPECT_CALL(*connection_, CloseConnection(QUIC_HTTP_FRAME_ERROR, _, _)); |
| 638 | stream_->OnStreamFrame(stream_frame); |
| 639 | } |
| 640 | |
| 641 | TEST_P(QuicSpdyStreamTest, UnexpectedHttp3Frame) { |
| 642 | if (!UsesHttp3()) { |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | Initialize(kShouldProcessData); |
| 647 | |
| 648 | // SETTINGS frame with empty payload. |
| 649 | std::string settings = quiche::QuicheTextUtils::HexDecode("0400"); |
| 650 | QuicStreamFrame stream_frame(stream_->id(), /* fin = */ false, |
| 651 | /* offset = */ 0, settings); |
| 652 | |
| 653 | EXPECT_CALL(*connection_, |
| 654 | CloseConnection(QUIC_HTTP_FRAME_UNEXPECTED_ON_SPDY_STREAM, _, _)); |
| 655 | stream_->OnStreamFrame(stream_frame); |
| 656 | } |
| 657 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 658 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBody) { |
| 659 | Initialize(kShouldProcessData); |
| 660 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 661 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 662 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 663 | |
| 664 | EXPECT_EQ("", stream_->data()); |
| 665 | QuicHeaderList headers = ProcessHeaders(false, headers_); |
| 666 | EXPECT_EQ(headers, stream_->header_list()); |
| 667 | stream_->ConsumeHeaderList(); |
| 668 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 669 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 670 | stream_->OnStreamFrame(frame); |
| 671 | EXPECT_EQ(QuicHeaderList(), stream_->header_list()); |
| 672 | EXPECT_EQ(body, stream_->data()); |
| 673 | } |
| 674 | |
| 675 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyFragments) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 676 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 677 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 678 | |
| 679 | for (size_t fragment_size = 1; fragment_size < data.size(); ++fragment_size) { |
| 680 | Initialize(kShouldProcessData); |
| 681 | QuicHeaderList headers = ProcessHeaders(false, headers_); |
| 682 | ASSERT_EQ(headers, stream_->header_list()); |
| 683 | stream_->ConsumeHeaderList(); |
| 684 | for (size_t offset = 0; offset < data.size(); offset += fragment_size) { |
| 685 | size_t remaining_data = data.size() - offset; |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 686 | quiche::QuicheStringPiece fragment( |
| 687 | data.data() + offset, std::min(fragment_size, remaining_data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 688 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 689 | offset, quiche::QuicheStringPiece(fragment)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 690 | stream_->OnStreamFrame(frame); |
| 691 | } |
| 692 | ASSERT_EQ(body, stream_->data()) << "fragment_size: " << fragment_size; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyFragmentsSplit) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 697 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 698 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 699 | |
| 700 | for (size_t split_point = 1; split_point < data.size() - 1; ++split_point) { |
| 701 | Initialize(kShouldProcessData); |
| 702 | QuicHeaderList headers = ProcessHeaders(false, headers_); |
| 703 | ASSERT_EQ(headers, stream_->header_list()); |
| 704 | stream_->ConsumeHeaderList(); |
| 705 | |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 706 | quiche::QuicheStringPiece fragment1(data.data(), split_point); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 707 | QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 708 | quiche::QuicheStringPiece(fragment1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 709 | stream_->OnStreamFrame(frame1); |
| 710 | |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 711 | quiche::QuicheStringPiece fragment2(data.data() + split_point, |
| 712 | data.size() - split_point); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 713 | QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 714 | split_point, quiche::QuicheStringPiece(fragment2)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 715 | stream_->OnStreamFrame(frame2); |
| 716 | |
| 717 | ASSERT_EQ(body, stream_->data()) << "split_point: " << split_point; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyReadv) { |
| 722 | Initialize(!kShouldProcessData); |
| 723 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 724 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 725 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 726 | |
| 727 | ProcessHeaders(false, headers_); |
| 728 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 729 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 730 | stream_->OnStreamFrame(frame); |
| 731 | stream_->ConsumeHeaderList(); |
| 732 | |
| 733 | char buffer[2048]; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 734 | ASSERT_LT(data.length(), QUICHE_ARRAYSIZE(buffer)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 735 | struct iovec vec; |
| 736 | vec.iov_base = buffer; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 737 | vec.iov_len = QUICHE_ARRAYSIZE(buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 738 | |
| 739 | size_t bytes_read = stream_->Readv(&vec, 1); |
QUICHE team | 396d109 | 2019-03-20 10:21:07 -0700 | [diff] [blame] | 740 | QuicStreamPeer::CloseReadSide(stream_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 741 | EXPECT_EQ(body.length(), bytes_read); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 742 | EXPECT_EQ(body, std::string(buffer, bytes_read)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndLargeBodySmallReadv) { |
| 746 | Initialize(kShouldProcessData); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 747 | std::string body(12 * 1024, 'a'); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 748 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 749 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 750 | ProcessHeaders(false, headers_); |
| 751 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 752 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 753 | stream_->OnStreamFrame(frame); |
| 754 | stream_->ConsumeHeaderList(); |
| 755 | char buffer[2048]; |
| 756 | char buffer2[2048]; |
| 757 | struct iovec vec[2]; |
| 758 | vec[0].iov_base = buffer; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 759 | vec[0].iov_len = QUICHE_ARRAYSIZE(buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 760 | vec[1].iov_base = buffer2; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 761 | vec[1].iov_len = QUICHE_ARRAYSIZE(buffer2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 762 | size_t bytes_read = stream_->Readv(vec, 2); |
| 763 | EXPECT_EQ(2048u * 2, bytes_read); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 764 | EXPECT_EQ(body.substr(0, 2048), std::string(buffer, 2048)); |
| 765 | EXPECT_EQ(body.substr(2048, 2048), std::string(buffer2, 2048)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyMarkConsumed) { |
| 769 | Initialize(!kShouldProcessData); |
| 770 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 771 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 772 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 773 | |
| 774 | ProcessHeaders(false, headers_); |
| 775 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 776 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 777 | stream_->OnStreamFrame(frame); |
| 778 | stream_->ConsumeHeaderList(); |
| 779 | |
| 780 | struct iovec vec; |
| 781 | |
| 782 | EXPECT_EQ(1, stream_->GetReadableRegions(&vec, 1)); |
| 783 | EXPECT_EQ(body.length(), vec.iov_len); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 784 | EXPECT_EQ(body, std::string(static_cast<char*>(vec.iov_base), vec.iov_len)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 785 | |
| 786 | stream_->MarkConsumed(body.length()); |
| 787 | EXPECT_EQ(data.length(), stream_->flow_controller()->bytes_consumed()); |
| 788 | } |
| 789 | |
| 790 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndConsumeMultipleBody) { |
| 791 | Initialize(!kShouldProcessData); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 792 | std::string body1 = "this is body 1"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 793 | std::string data1 = UsesHttp3() ? DataFrame(body1) : body1; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 794 | std::string body2 = "body 2"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 795 | std::string data2 = UsesHttp3() ? DataFrame(body2) : body2; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 796 | |
| 797 | ProcessHeaders(false, headers_); |
| 798 | QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 799 | quiche::QuicheStringPiece(data1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 800 | QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 801 | data1.length(), quiche::QuicheStringPiece(data2)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 802 | stream_->OnStreamFrame(frame1); |
| 803 | stream_->OnStreamFrame(frame2); |
| 804 | stream_->ConsumeHeaderList(); |
| 805 | |
| 806 | stream_->MarkConsumed(body1.length() + body2.length()); |
| 807 | EXPECT_EQ(data1.length() + data2.length(), |
| 808 | stream_->flow_controller()->bytes_consumed()); |
| 809 | } |
| 810 | |
| 811 | TEST_P(QuicSpdyStreamTest, ProcessHeadersAndBodyIncrementalReadv) { |
| 812 | Initialize(!kShouldProcessData); |
| 813 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 814 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 815 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 816 | |
| 817 | ProcessHeaders(false, headers_); |
| 818 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 819 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 820 | stream_->OnStreamFrame(frame); |
| 821 | stream_->ConsumeHeaderList(); |
| 822 | |
| 823 | char buffer[1]; |
| 824 | struct iovec vec; |
| 825 | vec.iov_base = buffer; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 826 | vec.iov_len = QUICHE_ARRAYSIZE(buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 827 | |
| 828 | for (size_t i = 0; i < body.length(); ++i) { |
| 829 | size_t bytes_read = stream_->Readv(&vec, 1); |
| 830 | ASSERT_EQ(1u, bytes_read); |
| 831 | EXPECT_EQ(body.data()[i], buffer[0]); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | TEST_P(QuicSpdyStreamTest, ProcessHeadersUsingReadvWithMultipleIovecs) { |
| 836 | Initialize(!kShouldProcessData); |
| 837 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 838 | std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 839 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 840 | |
| 841 | ProcessHeaders(false, headers_); |
| 842 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 843 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 844 | stream_->OnStreamFrame(frame); |
| 845 | stream_->ConsumeHeaderList(); |
| 846 | |
| 847 | char buffer1[1]; |
| 848 | char buffer2[1]; |
| 849 | struct iovec vec[2]; |
| 850 | vec[0].iov_base = buffer1; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 851 | vec[0].iov_len = QUICHE_ARRAYSIZE(buffer1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 852 | vec[1].iov_base = buffer2; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 853 | vec[1].iov_len = QUICHE_ARRAYSIZE(buffer2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 854 | |
| 855 | for (size_t i = 0; i < body.length(); i += 2) { |
| 856 | size_t bytes_read = stream_->Readv(vec, 2); |
| 857 | ASSERT_EQ(2u, bytes_read) << i; |
| 858 | ASSERT_EQ(body.data()[i], buffer1[0]) << i; |
| 859 | ASSERT_EQ(body.data()[i + 1], buffer2[0]) << i; |
| 860 | } |
| 861 | } |
| 862 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 863 | // Tests that we send a BLOCKED frame to the peer when we attempt to write, but |
| 864 | // are flow control blocked. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 865 | TEST_P(QuicSpdyStreamTest, StreamFlowControlBlocked) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 866 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 867 | testing::InSequence seq; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 868 | |
| 869 | // Set a small flow control limit. |
| 870 | const uint64_t kWindow = 36; |
| 871 | QuicFlowControllerPeer::SetSendWindowOffset(stream_->flow_controller(), |
| 872 | kWindow); |
| 873 | EXPECT_EQ(kWindow, QuicFlowControllerPeer::SendWindowOffset( |
| 874 | stream_->flow_controller())); |
| 875 | |
| 876 | // Try to send more data than the flow control limit allows. |
| 877 | const uint64_t kOverflow = 15; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 878 | std::string body(kWindow + kOverflow, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 879 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 880 | const uint64_t kHeaderLength = UsesHttp3() ? 2 : 0; |
| 881 | if (UsesHttp3()) { |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 882 | EXPECT_CALL(*session_, WritevData(_, kHeaderLength, _, NO_FIN, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 883 | } |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 884 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 885 | .WillOnce(Return(QuicConsumedData(kWindow - kHeaderLength, true))); |
| 886 | EXPECT_CALL(*connection_, SendControlFrame(_)); |
| 887 | stream_->WriteOrBufferBody(body, false); |
| 888 | |
| 889 | // Should have sent as much as possible, resulting in no send window left. |
| 890 | EXPECT_EQ(0u, |
| 891 | QuicFlowControllerPeer::SendWindowSize(stream_->flow_controller())); |
| 892 | |
| 893 | // And we should have queued the overflowed data. |
bnc | c7d9e0c | 2019-04-16 10:22:15 -0700 | [diff] [blame] | 894 | EXPECT_EQ(kOverflow + kHeaderLength, stream_->BufferedDataBytes()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 895 | } |
| 896 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 897 | // The flow control receive window decreases whenever we add new bytes to the |
| 898 | // sequencer, whether they are consumed immediately or buffered. However we only |
| 899 | // send WINDOW_UPDATE frames based on increasing number of bytes consumed. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 900 | TEST_P(QuicSpdyStreamTest, StreamFlowControlNoWindowUpdateIfNotConsumed) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 901 | // Don't process data - it will be buffered instead. |
| 902 | Initialize(!kShouldProcessData); |
| 903 | |
| 904 | // Expect no WINDOW_UPDATE frames to be sent. |
| 905 | EXPECT_CALL(*connection_, SendWindowUpdate(_, _)).Times(0); |
| 906 | |
| 907 | // Set a small flow control receive window. |
| 908 | const uint64_t kWindow = 36; |
| 909 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), |
| 910 | kWindow); |
| 911 | QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(), |
| 912 | kWindow); |
| 913 | EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowOffset( |
| 914 | stream_->flow_controller())); |
| 915 | |
| 916 | // Stream receives enough data to fill a fraction of the receive window. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 917 | std::string body(kWindow / 3, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 918 | QuicByteCount header_length = 0; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 919 | std::string data; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 920 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 921 | if (UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 922 | std::unique_ptr<char[]> buffer; |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 923 | header_length = |
| 924 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 925 | std::string header = std::string(buffer.get(), header_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 926 | data = header + body; |
| 927 | } else { |
| 928 | data = body; |
| 929 | } |
| 930 | |
| 931 | ProcessHeaders(false, headers_); |
| 932 | |
| 933 | QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 934 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 935 | stream_->OnStreamFrame(frame1); |
| 936 | EXPECT_EQ( |
| 937 | kWindow - (kWindow / 3) - header_length, |
| 938 | QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller())); |
| 939 | |
| 940 | // Now receive another frame which results in the receive window being over |
| 941 | // half full. This should all be buffered, decreasing the receive window but |
| 942 | // not sending WINDOW_UPDATE. |
| 943 | QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 944 | kWindow / 3 + header_length, |
| 945 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 946 | stream_->OnStreamFrame(frame2); |
| 947 | EXPECT_EQ( |
| 948 | kWindow - (2 * kWindow / 3) - 2 * header_length, |
| 949 | QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller())); |
| 950 | } |
| 951 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 952 | // Tests that on receipt of data, the stream updates its receive window offset |
| 953 | // appropriately, and sends WINDOW_UPDATE frames when its receive window drops |
| 954 | // too low. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 955 | TEST_P(QuicSpdyStreamTest, StreamFlowControlWindowUpdate) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 956 | Initialize(kShouldProcessData); |
| 957 | |
| 958 | // Set a small flow control limit. |
| 959 | const uint64_t kWindow = 36; |
| 960 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), |
| 961 | kWindow); |
| 962 | QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(), |
| 963 | kWindow); |
| 964 | EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowOffset( |
| 965 | stream_->flow_controller())); |
| 966 | |
| 967 | // Stream receives enough data to fill a fraction of the receive window. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 968 | std::string body(kWindow / 3, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 969 | QuicByteCount header_length = 0; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 970 | std::string data; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 971 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 972 | if (UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 973 | std::unique_ptr<char[]> buffer; |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 974 | header_length = |
| 975 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 976 | std::string header = std::string(buffer.get(), header_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 977 | data = header + body; |
| 978 | } else { |
| 979 | data = body; |
| 980 | } |
| 981 | |
| 982 | ProcessHeaders(false, headers_); |
| 983 | stream_->ConsumeHeaderList(); |
| 984 | |
| 985 | QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 986 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 987 | stream_->OnStreamFrame(frame1); |
| 988 | EXPECT_EQ( |
| 989 | kWindow - (kWindow / 3) - header_length, |
| 990 | QuicFlowControllerPeer::ReceiveWindowSize(stream_->flow_controller())); |
| 991 | |
| 992 | // Now receive another frame which results in the receive window being over |
| 993 | // half full. This will trigger the stream to increase its receive window |
| 994 | // offset and send a WINDOW_UPDATE. The result will be again an available |
| 995 | // window of kWindow bytes. |
| 996 | QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(0), false, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 997 | kWindow / 3 + header_length, |
| 998 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 999 | EXPECT_CALL(*connection_, SendControlFrame(_)); |
| 1000 | stream_->OnStreamFrame(frame2); |
| 1001 | EXPECT_EQ(kWindow, QuicFlowControllerPeer::ReceiveWindowSize( |
| 1002 | stream_->flow_controller())); |
| 1003 | } |
| 1004 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1005 | // Tests that on receipt of data, the connection updates its receive window |
| 1006 | // offset appropriately, and sends WINDOW_UPDATE frames when its receive window |
| 1007 | // drops too low. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1008 | TEST_P(QuicSpdyStreamTest, ConnectionFlowControlWindowUpdate) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1009 | Initialize(kShouldProcessData); |
| 1010 | |
| 1011 | // Set a small flow control limit for streams and connection. |
| 1012 | const uint64_t kWindow = 36; |
| 1013 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), |
| 1014 | kWindow); |
| 1015 | QuicFlowControllerPeer::SetMaxReceiveWindow(stream_->flow_controller(), |
| 1016 | kWindow); |
| 1017 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream2_->flow_controller(), |
| 1018 | kWindow); |
| 1019 | QuicFlowControllerPeer::SetMaxReceiveWindow(stream2_->flow_controller(), |
| 1020 | kWindow); |
| 1021 | QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(), |
| 1022 | kWindow); |
| 1023 | QuicFlowControllerPeer::SetMaxReceiveWindow(session_->flow_controller(), |
| 1024 | kWindow); |
| 1025 | |
| 1026 | // Supply headers to both streams so that they are happy to receive data. |
| 1027 | auto headers = AsHeaderList(headers_); |
| 1028 | stream_->OnStreamHeaderList(false, headers.uncompressed_header_bytes(), |
| 1029 | headers); |
| 1030 | stream_->ConsumeHeaderList(); |
| 1031 | stream2_->OnStreamHeaderList(false, headers.uncompressed_header_bytes(), |
| 1032 | headers); |
| 1033 | stream2_->ConsumeHeaderList(); |
| 1034 | |
| 1035 | // Each stream gets a quarter window of data. This should not trigger a |
| 1036 | // WINDOW_UPDATE for either stream, nor for the connection. |
| 1037 | QuicByteCount header_length = 0; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1038 | std::string body; |
| 1039 | std::string data; |
| 1040 | std::string data2; |
| 1041 | std::string body2(1, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1042 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1043 | if (UsesHttp3()) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1044 | body = std::string(kWindow / 4 - 2, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1045 | std::unique_ptr<char[]> buffer; |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 1046 | header_length = |
| 1047 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1048 | std::string header = std::string(buffer.get(), header_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1049 | data = header + body; |
| 1050 | std::unique_ptr<char[]> buffer2; |
| 1051 | QuicByteCount header_length2 = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 1052 | HttpEncoder::SerializeDataFrameHeader(body2.length(), &buffer2); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1053 | std::string header2 = std::string(buffer2.get(), header_length2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1054 | data2 = header2 + body2; |
| 1055 | } else { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1056 | body = std::string(kWindow / 4, 'a'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1057 | data = body; |
| 1058 | data2 = body2; |
| 1059 | } |
| 1060 | |
| 1061 | QuicStreamFrame frame1(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1062 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1063 | stream_->OnStreamFrame(frame1); |
| 1064 | QuicStreamFrame frame2(GetNthClientInitiatedBidirectionalId(1), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1065 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1066 | stream2_->OnStreamFrame(frame2); |
| 1067 | |
| 1068 | // Now receive a further single byte on one stream - again this does not |
| 1069 | // trigger a stream WINDOW_UPDATE, but now the connection flow control window |
| 1070 | // is over half full and thus a connection WINDOW_UPDATE is sent. |
| 1071 | EXPECT_CALL(*connection_, SendControlFrame(_)); |
| 1072 | QuicStreamFrame frame3(GetNthClientInitiatedBidirectionalId(0), false, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1073 | body.length() + header_length, |
| 1074 | quiche::QuicheStringPiece(data2)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1075 | stream_->OnStreamFrame(frame3); |
| 1076 | } |
| 1077 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1078 | // Tests that on if the peer sends too much data (i.e. violates the flow control |
| 1079 | // protocol), then we terminate the connection. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1080 | TEST_P(QuicSpdyStreamTest, StreamFlowControlViolation) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1081 | // Stream should not process data, so that data gets buffered in the |
| 1082 | // sequencer, triggering flow control limits. |
| 1083 | Initialize(!kShouldProcessData); |
| 1084 | |
| 1085 | // Set a small flow control limit. |
| 1086 | const uint64_t kWindow = 50; |
| 1087 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), |
| 1088 | kWindow); |
| 1089 | |
| 1090 | ProcessHeaders(false, headers_); |
| 1091 | |
| 1092 | // Receive data to overflow the window, violating flow control. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1093 | std::string body(kWindow + 1, 'a'); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1094 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1095 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1096 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1097 | EXPECT_CALL(*connection_, |
| 1098 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)); |
| 1099 | stream_->OnStreamFrame(frame); |
| 1100 | } |
| 1101 | |
| 1102 | TEST_P(QuicSpdyStreamTest, TestHandlingQuicRstStreamNoError) { |
| 1103 | Initialize(kShouldProcessData); |
| 1104 | ProcessHeaders(false, headers_); |
| 1105 | |
| 1106 | stream_->OnStreamReset(QuicRstStreamFrame( |
| 1107 | kInvalidControlFrameId, stream_->id(), QUIC_STREAM_NO_ERROR, 0)); |
| 1108 | EXPECT_TRUE(stream_->write_side_closed()); |
| 1109 | EXPECT_FALSE(stream_->reading_stopped()); |
| 1110 | } |
| 1111 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1112 | // Tests that on if the peer sends too much data (i.e. violates the flow control |
| 1113 | // protocol), at the connection level (rather than the stream level) then we |
| 1114 | // terminate the connection. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1115 | TEST_P(QuicSpdyStreamTest, ConnectionFlowControlViolation) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1116 | // Stream should not process data, so that data gets buffered in the |
| 1117 | // sequencer, triggering flow control limits. |
| 1118 | Initialize(!kShouldProcessData); |
| 1119 | |
| 1120 | // Set a small flow control window on streams, and connection. |
| 1121 | const uint64_t kStreamWindow = 50; |
| 1122 | const uint64_t kConnectionWindow = 10; |
| 1123 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), |
| 1124 | kStreamWindow); |
| 1125 | QuicFlowControllerPeer::SetReceiveWindowOffset(session_->flow_controller(), |
| 1126 | kConnectionWindow); |
| 1127 | |
| 1128 | ProcessHeaders(false, headers_); |
| 1129 | |
| 1130 | // Send enough data to overflow the connection level flow control window. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1131 | std::string body(kConnectionWindow + 1, 'a'); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1132 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1133 | |
| 1134 | EXPECT_LT(data.size(), kStreamWindow); |
| 1135 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), false, 0, |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1136 | quiche::QuicheStringPiece(data)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1137 | |
| 1138 | EXPECT_CALL(*connection_, |
| 1139 | CloseConnection(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA, _, _)); |
| 1140 | stream_->OnStreamFrame(frame); |
| 1141 | } |
| 1142 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1143 | // An attempt to write a FIN with no data should not be flow control blocked, |
| 1144 | // even if the send window is 0. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1145 | TEST_P(QuicSpdyStreamTest, StreamFlowControlFinNotBlocked) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1146 | Initialize(kShouldProcessData); |
| 1147 | |
| 1148 | // Set a flow control limit of zero. |
| 1149 | QuicFlowControllerPeer::SetReceiveWindowOffset(stream_->flow_controller(), 0); |
| 1150 | EXPECT_EQ(0u, QuicFlowControllerPeer::ReceiveWindowOffset( |
| 1151 | stream_->flow_controller())); |
| 1152 | |
| 1153 | // Send a frame with a FIN but no data. This should not be blocked. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1154 | std::string body = ""; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1155 | bool fin = true; |
| 1156 | |
| 1157 | EXPECT_CALL(*connection_, |
| 1158 | SendBlocked(GetNthClientInitiatedBidirectionalId(0))) |
| 1159 | .Times(0); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1160 | EXPECT_CALL(*session_, WritevData(_, 0, _, FIN, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1161 | |
| 1162 | stream_->WriteOrBufferBody(body, fin); |
| 1163 | } |
| 1164 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1165 | // Test that receiving trailing headers from the peer via OnStreamHeaderList() |
| 1166 | // works, and can be read from the stream and consumed. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1167 | TEST_P(QuicSpdyStreamTest, ReceivingTrailersViaHeaderList) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1168 | Initialize(kShouldProcessData); |
| 1169 | |
| 1170 | // Receive initial headers. |
| 1171 | size_t total_bytes = 0; |
| 1172 | QuicHeaderList headers; |
| 1173 | for (const auto& p : headers_) { |
| 1174 | headers.OnHeader(p.first, p.second); |
| 1175 | total_bytes += p.first.size() + p.second.size(); |
| 1176 | } |
| 1177 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 1178 | stream_->OnStreamHeadersPriority( |
| 1179 | spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1180 | stream_->OnStreamHeaderList(/*fin=*/false, total_bytes, headers); |
| 1181 | stream_->ConsumeHeaderList(); |
| 1182 | |
| 1183 | // Receive trailing headers. |
| 1184 | SpdyHeaderBlock trailers_block; |
| 1185 | trailers_block["key1"] = "value1"; |
| 1186 | trailers_block["key2"] = "value2"; |
| 1187 | trailers_block["key3"] = "value3"; |
| 1188 | SpdyHeaderBlock trailers_block_with_final_offset = trailers_block.Clone(); |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1189 | if (!UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1190 | // :final-offset pseudo-header is only added if trailers are sent |
| 1191 | // on the headers stream. |
| 1192 | trailers_block_with_final_offset[kFinalOffsetHeaderKey] = "0"; |
| 1193 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1194 | total_bytes = 0; |
| 1195 | QuicHeaderList trailers; |
| 1196 | for (const auto& p : trailers_block_with_final_offset) { |
| 1197 | trailers.OnHeader(p.first, p.second); |
| 1198 | total_bytes += p.first.size() + p.second.size(); |
| 1199 | } |
| 1200 | stream_->OnStreamHeaderList(/*fin=*/true, total_bytes, trailers); |
| 1201 | |
| 1202 | // The trailers should be decompressed, and readable from the stream. |
| 1203 | EXPECT_TRUE(stream_->trailers_decompressed()); |
| 1204 | EXPECT_EQ(trailers_block, stream_->received_trailers()); |
| 1205 | |
| 1206 | // IsDoneReading() returns false until trailers marked consumed. |
| 1207 | EXPECT_FALSE(stream_->IsDoneReading()); |
| 1208 | stream_->MarkTrailersConsumed(); |
| 1209 | EXPECT_TRUE(stream_->IsDoneReading()); |
| 1210 | } |
| 1211 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1212 | // Test that when receiving trailing headers with an offset before response |
| 1213 | // body, stream is closed at the right offset. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1214 | TEST_P(QuicSpdyStreamTest, ReceivingTrailersWithOffset) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1215 | // kFinalOffsetHeaderKey is not used when HEADERS are sent on the |
| 1216 | // request/response stream. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1217 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1218 | return; |
| 1219 | } |
| 1220 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1221 | Initialize(kShouldProcessData); |
| 1222 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1223 | // Receive initial headers. |
| 1224 | QuicHeaderList headers = ProcessHeaders(false, headers_); |
| 1225 | stream_->ConsumeHeaderList(); |
| 1226 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1227 | const std::string body = "this is the body"; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1228 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1229 | |
| 1230 | // Receive trailing headers. |
| 1231 | SpdyHeaderBlock trailers_block; |
| 1232 | trailers_block["key1"] = "value1"; |
| 1233 | trailers_block["key2"] = "value2"; |
| 1234 | trailers_block["key3"] = "value3"; |
| 1235 | trailers_block[kFinalOffsetHeaderKey] = |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1236 | quiche::QuicheTextUtils::Uint64ToString(data.size()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1237 | |
| 1238 | QuicHeaderList trailers = ProcessHeaders(true, trailers_block); |
| 1239 | |
| 1240 | // The trailers should be decompressed, and readable from the stream. |
| 1241 | EXPECT_TRUE(stream_->trailers_decompressed()); |
| 1242 | |
| 1243 | // The final offset trailer will be consumed by QUIC. |
| 1244 | trailers_block.erase(kFinalOffsetHeaderKey); |
| 1245 | EXPECT_EQ(trailers_block, stream_->received_trailers()); |
| 1246 | |
| 1247 | // Consuming the trailers erases them from the stream. |
| 1248 | stream_->MarkTrailersConsumed(); |
| 1249 | EXPECT_TRUE(stream_->FinishedReadingTrailers()); |
| 1250 | |
| 1251 | EXPECT_FALSE(stream_->IsDoneReading()); |
| 1252 | // Receive and consume body. |
| 1253 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), /*fin=*/false, |
| 1254 | 0, data); |
| 1255 | stream_->OnStreamFrame(frame); |
| 1256 | EXPECT_EQ(body, stream_->data()); |
| 1257 | EXPECT_TRUE(stream_->IsDoneReading()); |
| 1258 | } |
| 1259 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1260 | // Test that receiving trailers without a final offset field is an error. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1261 | TEST_P(QuicSpdyStreamTest, ReceivingTrailersWithoutOffset) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1262 | // kFinalOffsetHeaderKey is not used when HEADERS are sent on the |
| 1263 | // request/response stream. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1264 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1265 | return; |
| 1266 | } |
| 1267 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1268 | Initialize(kShouldProcessData); |
| 1269 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1270 | // Receive initial headers. |
| 1271 | ProcessHeaders(false, headers_); |
| 1272 | stream_->ConsumeHeaderList(); |
| 1273 | |
| 1274 | // Receive trailing headers, without kFinalOffsetHeaderKey. |
| 1275 | SpdyHeaderBlock trailers_block; |
| 1276 | trailers_block["key1"] = "value1"; |
| 1277 | trailers_block["key2"] = "value2"; |
| 1278 | trailers_block["key3"] = "value3"; |
| 1279 | auto trailers = AsHeaderList(trailers_block); |
| 1280 | |
| 1281 | // Verify that the trailers block didn't contain a final offset. |
| 1282 | EXPECT_EQ("", trailers_block[kFinalOffsetHeaderKey].as_string()); |
| 1283 | |
| 1284 | // Receipt of the malformed trailers will close the connection. |
| 1285 | EXPECT_CALL(*connection_, |
| 1286 | CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _)) |
| 1287 | .Times(1); |
| 1288 | stream_->OnStreamHeaderList(/*fin=*/true, |
| 1289 | trailers.uncompressed_header_bytes(), trailers); |
| 1290 | } |
| 1291 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1292 | // Test that received Trailers must always have the FIN set. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1293 | TEST_P(QuicSpdyStreamTest, ReceivingTrailersWithoutFin) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1294 | // In IETF QUIC, there is no such thing as FIN flag on HTTP/3 frames like the |
| 1295 | // HEADERS frame. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1296 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1300 | Initialize(kShouldProcessData); |
| 1301 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1302 | // Receive initial headers. |
| 1303 | auto headers = AsHeaderList(headers_); |
| 1304 | stream_->OnStreamHeaderList(/*fin=*/false, |
| 1305 | headers.uncompressed_header_bytes(), headers); |
| 1306 | stream_->ConsumeHeaderList(); |
| 1307 | |
| 1308 | // Receive trailing headers with FIN deliberately set to false. |
| 1309 | SpdyHeaderBlock trailers_block; |
| 1310 | trailers_block["foo"] = "bar"; |
| 1311 | auto trailers = AsHeaderList(trailers_block); |
| 1312 | |
| 1313 | EXPECT_CALL(*connection_, |
| 1314 | CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _)) |
| 1315 | .Times(1); |
| 1316 | stream_->OnStreamHeaderList(/*fin=*/false, |
| 1317 | trailers.uncompressed_header_bytes(), trailers); |
| 1318 | } |
| 1319 | |
| 1320 | TEST_P(QuicSpdyStreamTest, ReceivingTrailersAfterHeadersWithFin) { |
| 1321 | // If headers are received with a FIN, no trailers should then arrive. |
| 1322 | Initialize(kShouldProcessData); |
| 1323 | |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 1324 | // If HEADERS frames are sent on the request/response stream, then the |
| 1325 | // sequencer will signal an error if any stream data arrives after a FIN, |
| 1326 | // so QuicSpdyStream does not need to. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1327 | if (UsesHttp3()) { |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 1328 | return; |
| 1329 | } |
| 1330 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1331 | // Receive initial headers with FIN set. |
| 1332 | ProcessHeaders(true, headers_); |
| 1333 | stream_->ConsumeHeaderList(); |
| 1334 | |
| 1335 | // Receive trailing headers after FIN already received. |
| 1336 | SpdyHeaderBlock trailers_block; |
| 1337 | trailers_block["foo"] = "bar"; |
| 1338 | EXPECT_CALL(*connection_, |
| 1339 | CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _)) |
| 1340 | .Times(1); |
| 1341 | ProcessHeaders(true, trailers_block); |
| 1342 | } |
| 1343 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1344 | // If body data are received with a FIN, no trailers should then arrive. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1345 | TEST_P(QuicSpdyStreamTest, ReceivingTrailersAfterBodyWithFin) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1346 | // If HEADERS frames are sent on the request/response stream, |
| 1347 | // then the sequencer will block them from reaching QuicSpdyStream |
| 1348 | // after the stream is closed. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1349 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1350 | return; |
| 1351 | } |
| 1352 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1353 | Initialize(kShouldProcessData); |
| 1354 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1355 | // Receive initial headers without FIN set. |
| 1356 | ProcessHeaders(false, headers_); |
| 1357 | stream_->ConsumeHeaderList(); |
| 1358 | |
| 1359 | // Receive body data, with FIN. |
| 1360 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), /*fin=*/true, |
| 1361 | 0, "body"); |
| 1362 | stream_->OnStreamFrame(frame); |
| 1363 | |
| 1364 | // Receive trailing headers after FIN already received. |
| 1365 | SpdyHeaderBlock trailers_block; |
| 1366 | trailers_block["foo"] = "bar"; |
| 1367 | EXPECT_CALL(*connection_, |
| 1368 | CloseConnection(QUIC_INVALID_HEADERS_STREAM_DATA, _, _)) |
| 1369 | .Times(1); |
| 1370 | ProcessHeaders(true, trailers_block); |
| 1371 | } |
| 1372 | |
| 1373 | TEST_P(QuicSpdyStreamTest, ClosingStreamWithNoTrailers) { |
| 1374 | // Verify that a stream receiving headers, body, and no trailers is correctly |
| 1375 | // marked as done reading on consumption of headers and body. |
| 1376 | Initialize(kShouldProcessData); |
| 1377 | |
| 1378 | // Receive and consume initial headers with FIN not set. |
| 1379 | auto h = AsHeaderList(headers_); |
| 1380 | stream_->OnStreamHeaderList(/*fin=*/false, h.uncompressed_header_bytes(), h); |
| 1381 | stream_->ConsumeHeaderList(); |
| 1382 | |
| 1383 | // Receive and consume body with FIN set, and no trailers. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1384 | std::string body(1024, 'x'); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1385 | std::string data = UsesHttp3() ? DataFrame(body) : body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1386 | |
| 1387 | QuicStreamFrame frame(GetNthClientInitiatedBidirectionalId(0), /*fin=*/true, |
| 1388 | 0, data); |
| 1389 | stream_->OnStreamFrame(frame); |
| 1390 | |
| 1391 | EXPECT_TRUE(stream_->IsDoneReading()); |
| 1392 | } |
| 1393 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1394 | // Test that writing trailers will send a FIN, as Trailers are the last thing to |
| 1395 | // be sent on a stream. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1396 | TEST_P(QuicSpdyStreamTest, WritingTrailersSendsAFin) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1397 | Initialize(kShouldProcessData); |
| 1398 | |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1399 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1400 | // In this case, TestStream::WriteHeadersImpl() does not prevent writes. |
bnc | cd3c4b5 | 2020-01-10 11:13:18 -0800 | [diff] [blame] | 1401 | // Four writes on the request stream: HEADERS frame header and payload both |
| 1402 | // for headers and trailers. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1403 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(4); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1406 | // Write the initial headers, without a FIN. |
| 1407 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 1408 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
| 1409 | |
| 1410 | // Writing trailers implicitly sends a FIN. |
| 1411 | SpdyHeaderBlock trailers; |
| 1412 | trailers["trailer key"] = "trailer value"; |
| 1413 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1414 | stream_->WriteTrailers(std::move(trailers), nullptr); |
| 1415 | EXPECT_TRUE(stream_->fin_sent()); |
| 1416 | } |
| 1417 | |
bnc | b8abf38 | 2020-01-30 11:26:12 -0800 | [diff] [blame] | 1418 | TEST_P(QuicSpdyStreamTest, DoNotSendPriorityUpdateWithDefaultUrgency) { |
| 1419 | if (!UsesHttp3()) { |
| 1420 | return; |
| 1421 | } |
| 1422 | |
| 1423 | InitializeWithPerspective(kShouldProcessData, Perspective::IS_CLIENT); |
| 1424 | |
| 1425 | // Four writes on the request stream: HEADERS frame header and payload both |
| 1426 | // for headers and trailers. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1427 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(4); |
bnc | b8abf38 | 2020-01-30 11:26:12 -0800 | [diff] [blame] | 1428 | |
bnc | 5f20251 | 2020-02-01 18:43:02 -0800 | [diff] [blame] | 1429 | // No PRIORITY_UPDATE frames on the control stream, |
| 1430 | // because the stream has default priority. |
bnc | b8abf38 | 2020-01-30 11:26:12 -0800 | [diff] [blame] | 1431 | auto send_control_stream = |
| 1432 | QuicSpdySessionPeer::GetSendControlStream(session_.get()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1433 | EXPECT_CALL(*session_, WritevData(send_control_stream->id(), _, _, _, _, _)) |
bnc | b8abf38 | 2020-01-30 11:26:12 -0800 | [diff] [blame] | 1434 | .Times(0); |
| 1435 | |
| 1436 | // Write the initial headers, without a FIN. |
| 1437 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 1438 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
| 1439 | |
| 1440 | // Writing trailers implicitly sends a FIN. |
| 1441 | SpdyHeaderBlock trailers; |
| 1442 | trailers["trailer key"] = "trailer value"; |
| 1443 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1444 | stream_->WriteTrailers(std::move(trailers), nullptr); |
| 1445 | EXPECT_TRUE(stream_->fin_sent()); |
| 1446 | } |
| 1447 | |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 1448 | TEST_P(QuicSpdyStreamTest, ChangePriority) { |
| 1449 | if (!UsesHttp3()) { |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | InitializeWithPerspective(kShouldProcessData, Perspective::IS_CLIENT); |
| 1454 | |
| 1455 | // Two writes on the request stream: HEADERS frame header and payload. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1456 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(2); |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 1457 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
bnc | 5f20251 | 2020-02-01 18:43:02 -0800 | [diff] [blame] | 1458 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
| 1459 | |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 1460 | // PRIORITY_UPDATE frame on the control stream. |
| 1461 | auto send_control_stream = |
| 1462 | QuicSpdySessionPeer::GetSendControlStream(session_.get()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1463 | EXPECT_CALL(*session_, WritevData(send_control_stream->id(), _, _, _, _, _)); |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 1464 | stream_->SetPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
| 1465 | } |
| 1466 | |
| 1467 | TEST_P(QuicSpdyStreamTest, ChangePriorityBeforeWritingHeaders) { |
| 1468 | if (!UsesHttp3()) { |
| 1469 | return; |
| 1470 | } |
| 1471 | |
| 1472 | InitializeWithPerspective(kShouldProcessData, Perspective::IS_CLIENT); |
| 1473 | |
| 1474 | // PRIORITY_UPDATE frame sent on the control stream as soon as SetPriority() |
| 1475 | // is called, before HEADERS frame is sent. |
| 1476 | auto send_control_stream = |
| 1477 | QuicSpdySessionPeer::GetSendControlStream(session_.get()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1478 | EXPECT_CALL(*session_, WritevData(send_control_stream->id(), _, _, _, _, _)); |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 1479 | |
| 1480 | stream_->SetPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
| 1481 | testing::Mock::VerifyAndClearExpectations(session_.get()); |
| 1482 | |
| 1483 | // Two writes on the request stream: HEADERS frame header and payload. |
| 1484 | // PRIORITY_UPDATE frame is not sent this time, because one is already sent. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1485 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(2); |
bnc | cf09f95 | 2020-01-30 17:35:59 -0800 | [diff] [blame] | 1486 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1487 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/true, nullptr); |
| 1488 | } |
| 1489 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1490 | // Test that when writing trailers, the trailers that are actually sent to the |
| 1491 | // peer contain the final offset field indicating last byte of data. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1492 | TEST_P(QuicSpdyStreamTest, WritingTrailersFinalOffset) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1493 | Initialize(kShouldProcessData); |
| 1494 | |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1495 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1496 | // In this case, TestStream::WriteHeadersImpl() does not prevent writes. |
bnc | cd3c4b5 | 2020-01-10 11:13:18 -0800 | [diff] [blame] | 1497 | // HEADERS frame header and payload on the request stream. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1498 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)).Times(2); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1501 | // Write the initial headers. |
| 1502 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 1503 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
| 1504 | |
| 1505 | // Write non-zero body data to force a non-zero final offset. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1506 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1507 | std::string body(1024, 'x'); // 1 kB |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1508 | QuicByteCount header_length = 0; |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1509 | if (UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1510 | std::unique_ptr<char[]> buf; |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 1511 | header_length = HttpEncoder::SerializeDataFrameHeader(body.length(), &buf); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | stream_->WriteOrBufferBody(body, false); |
| 1515 | |
| 1516 | // The final offset field in the trailing headers is populated with the |
| 1517 | // number of body bytes written (including queued bytes). |
| 1518 | SpdyHeaderBlock trailers; |
| 1519 | trailers["trailer key"] = "trailer value"; |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1520 | |
| 1521 | SpdyHeaderBlock expected_trailers(trailers.Clone()); |
| 1522 | // :final-offset pseudo-header is only added if trailers are sent |
| 1523 | // on the headers stream. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1524 | if (!UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1525 | expected_trailers[kFinalOffsetHeaderKey] = |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1526 | quiche::QuicheTextUtils::Uint64ToString(body.length() + header_length); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1529 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1530 | stream_->WriteTrailers(std::move(trailers), nullptr); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1531 | EXPECT_EQ(expected_trailers, stream_->saved_headers()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1532 | } |
| 1533 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1534 | // Test that if trailers are written after all other data has been written |
| 1535 | // (headers and body), that this closes the stream for writing. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1536 | TEST_P(QuicSpdyStreamTest, WritingTrailersClosesWriteSide) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1537 | Initialize(kShouldProcessData); |
| 1538 | |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1539 | // Expect data being written on the stream. In addition to that, headers are |
| 1540 | // also written on the stream in case of IETF QUIC. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1541 | EXPECT_CALL(*session_, WritevData(stream_->id(), _, _, _, _, _)) |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1542 | .Times(AtLeast(1)); |
| 1543 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1544 | // Write the initial headers. |
| 1545 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 1546 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
| 1547 | |
| 1548 | // Write non-zero body data. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1549 | const int kBodySize = 1 * 1024; // 1 kB |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1550 | stream_->WriteOrBufferBody(std::string(kBodySize, 'x'), false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1551 | EXPECT_EQ(0u, stream_->BufferedDataBytes()); |
| 1552 | |
| 1553 | // Headers and body have been fully written, there is no queued data. Writing |
| 1554 | // trailers marks the end of this stream, and thus the write side is closed. |
| 1555 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1556 | stream_->WriteTrailers(SpdyHeaderBlock(), nullptr); |
| 1557 | EXPECT_TRUE(stream_->write_side_closed()); |
| 1558 | } |
| 1559 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1560 | // Test that the stream is not closed for writing when trailers are sent while |
| 1561 | // there are still body bytes queued. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1562 | TEST_P(QuicSpdyStreamTest, WritingTrailersWithQueuedBytes) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1563 | // This test exercises sending trailers on the headers stream while data is |
| 1564 | // still queued on the response/request stream. In IETF QUIC, data and |
| 1565 | // trailers are sent on the same stream, so this test does not apply. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1566 | if (UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1567 | return; |
| 1568 | } |
| 1569 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1570 | testing::InSequence seq; |
| 1571 | Initialize(kShouldProcessData); |
| 1572 | |
| 1573 | // Write the initial headers. |
| 1574 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 1575 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
| 1576 | |
| 1577 | // Write non-zero body data, but only consume partially, ensuring queueing. |
| 1578 | const int kBodySize = 1 * 1024; // 1 kB |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1579 | if (UsesHttp3()) { |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1580 | EXPECT_CALL(*session_, WritevData(_, 3, _, NO_FIN, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1581 | } |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1582 | EXPECT_CALL(*session_, WritevData(_, kBodySize, _, NO_FIN, _, _)) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1583 | .WillOnce(Return(QuicConsumedData(kBodySize - 1, false))); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1584 | stream_->WriteOrBufferBody(std::string(kBodySize, 'x'), false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1585 | EXPECT_EQ(1u, stream_->BufferedDataBytes()); |
| 1586 | |
| 1587 | // Writing trailers will send a FIN, but not close the write side of the |
| 1588 | // stream as there are queued bytes. |
| 1589 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1590 | stream_->WriteTrailers(SpdyHeaderBlock(), nullptr); |
| 1591 | EXPECT_TRUE(stream_->fin_sent()); |
| 1592 | EXPECT_FALSE(stream_->write_side_closed()); |
| 1593 | |
| 1594 | // Writing the queued bytes will close the write side of the stream. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1595 | EXPECT_CALL(*session_, WritevData(_, 1, _, NO_FIN, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1596 | stream_->OnCanWrite(); |
| 1597 | EXPECT_TRUE(stream_->write_side_closed()); |
| 1598 | } |
| 1599 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1600 | // Test that it is not possible to write Trailers after a FIN has been sent. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1601 | TEST_P(QuicSpdyStreamTest, WritingTrailersAfterFIN) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1602 | // In IETF QUIC, there is no such thing as FIN flag on HTTP/3 frames like the |
bnc | 01de0b8 | 2020-02-25 11:36:51 -0800 | [diff] [blame] | 1603 | // HEADERS frame. |
| 1604 | if (UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1605 | return; |
| 1606 | } |
| 1607 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1608 | Initialize(kShouldProcessData); |
| 1609 | |
| 1610 | // Write the initial headers, with a FIN. |
| 1611 | EXPECT_CALL(*stream_, WriteHeadersMock(true)); |
| 1612 | stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/true, nullptr); |
| 1613 | EXPECT_TRUE(stream_->fin_sent()); |
| 1614 | |
| 1615 | // Writing Trailers should fail, as the FIN has already been sent. |
| 1616 | // populated with the number of body bytes written. |
| 1617 | EXPECT_QUIC_BUG(stream_->WriteTrailers(SpdyHeaderBlock(), nullptr), |
| 1618 | "Trailers cannot be sent after a FIN"); |
| 1619 | } |
| 1620 | |
| 1621 | TEST_P(QuicSpdyStreamTest, HeaderStreamNotiferCorrespondingSpdyStream) { |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1622 | // There is no headers stream if QPACK is used. |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1623 | if (UsesHttp3()) { |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1624 | return; |
| 1625 | } |
| 1626 | |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1627 | const char kHeader1[] = "Header1"; |
| 1628 | const char kHeader2[] = "Header2"; |
| 1629 | const char kBody1[] = "Test1"; |
| 1630 | const char kBody2[] = "Test2"; |
| 1631 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1632 | Initialize(kShouldProcessData); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1633 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1634 | testing::InSequence s; |
| 1635 | QuicReferenceCountedPointer<MockAckListener> ack_listener1( |
| 1636 | new MockAckListener()); |
| 1637 | QuicReferenceCountedPointer<MockAckListener> ack_listener2( |
| 1638 | new MockAckListener()); |
| 1639 | stream_->set_ack_listener(ack_listener1); |
| 1640 | stream2_->set_ack_listener(ack_listener2); |
| 1641 | |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1642 | session_->headers_stream()->WriteOrBufferData(kHeader1, false, ack_listener1); |
| 1643 | stream_->WriteOrBufferBody(kBody1, true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1644 | |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1645 | session_->headers_stream()->WriteOrBufferData(kHeader2, false, ack_listener2); |
| 1646 | stream2_->WriteOrBufferBody(kBody2, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1647 | |
| 1648 | QuicStreamFrame frame1( |
| 1649 | QuicUtils::GetHeadersStreamId(connection_->transport_version()), false, 0, |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1650 | kHeader1); |
| 1651 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1652 | std::string data1 = UsesHttp3() ? DataFrame(kBody1) : kBody1; |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1653 | QuicStreamFrame frame2(stream_->id(), true, 0, data1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1654 | QuicStreamFrame frame3( |
| 1655 | QuicUtils::GetHeadersStreamId(connection_->transport_version()), false, 7, |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1656 | kHeader2); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1657 | std::string data2 = UsesHttp3() ? DataFrame(kBody2) : kBody2; |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1658 | QuicStreamFrame frame4(stream2_->id(), false, 0, data2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1659 | |
| 1660 | EXPECT_CALL(*ack_listener1, OnPacketRetransmitted(7)); |
| 1661 | session_->OnStreamFrameRetransmitted(frame1); |
| 1662 | |
| 1663 | EXPECT_CALL(*ack_listener1, OnPacketAcked(7, _)); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1664 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame1), QuicTime::Delta::Zero(), |
| 1665 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1666 | EXPECT_CALL(*ack_listener1, OnPacketAcked(5, _)); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1667 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame2), QuicTime::Delta::Zero(), |
| 1668 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1669 | EXPECT_CALL(*ack_listener2, OnPacketAcked(7, _)); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1670 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame3), QuicTime::Delta::Zero(), |
| 1671 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1672 | EXPECT_CALL(*ack_listener2, OnPacketAcked(5, _)); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1673 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame4), QuicTime::Delta::Zero(), |
| 1674 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | TEST_P(QuicSpdyStreamTest, StreamBecomesZombieWithWriteThatCloses) { |
| 1678 | Initialize(kShouldProcessData); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1679 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1680 | QuicStreamPeer::CloseReadSide(stream_); |
| 1681 | // This write causes stream to be closed. |
| 1682 | stream_->WriteOrBufferBody("Test1", true); |
| 1683 | // stream_ has unacked data and should become zombie. |
| 1684 | EXPECT_TRUE(QuicContainsKey(QuicSessionPeer::zombie_streams(session_.get()), |
| 1685 | stream_->id())); |
| 1686 | EXPECT_TRUE(QuicSessionPeer::closed_streams(session_.get()).empty()); |
| 1687 | } |
| 1688 | |
| 1689 | TEST_P(QuicSpdyStreamTest, OnPriorityFrame) { |
| 1690 | Initialize(kShouldProcessData); |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 1691 | stream_->OnPriorityFrame(spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
| 1692 | EXPECT_EQ(spdy::SpdyStreamPrecedence(kV3HighestPriority), |
| 1693 | stream_->precedence()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | TEST_P(QuicSpdyStreamTest, OnPriorityFrameAfterSendingData) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1697 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 1698 | testing::InSequence seq; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1699 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1700 | if (UsesHttp3()) { |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1701 | EXPECT_CALL(*session_, WritevData(_, 2, _, NO_FIN, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1702 | } |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1703 | EXPECT_CALL(*session_, WritevData(_, 4, _, FIN, _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1704 | stream_->WriteOrBufferBody("data", true); |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 1705 | stream_->OnPriorityFrame(spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
| 1706 | EXPECT_EQ(spdy::SpdyStreamPrecedence(kV3HighestPriority), |
| 1707 | stream_->precedence()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | TEST_P(QuicSpdyStreamTest, SetPriorityBeforeUpdateStreamPriority) { |
| 1711 | MockQuicConnection* connection = new StrictMock<MockQuicConnection>( |
| 1712 | &helper_, &alarm_factory_, Perspective::IS_SERVER, |
| 1713 | SupportedVersions(GetParam())); |
| 1714 | std::unique_ptr<TestMockUpdateStreamSession> session( |
| 1715 | new StrictMock<TestMockUpdateStreamSession>(connection)); |
renjietang | d1d0085 | 2019-09-06 10:43:12 -0700 | [diff] [blame] | 1716 | auto stream = |
| 1717 | new StrictMock<TestStream>(GetNthClientInitiatedBidirectionalStreamId( |
| 1718 | session->transport_version(), 0), |
| 1719 | session.get(), |
| 1720 | /*should_process_data=*/true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1721 | session->ActivateStream(QuicWrapUnique(stream)); |
| 1722 | |
| 1723 | // QuicSpdyStream::SetPriority() should eventually call UpdateStreamPriority() |
| 1724 | // on the session. Make sure stream->priority() returns the updated priority |
| 1725 | // if called within UpdateStreamPriority(). This expectation is enforced in |
| 1726 | // TestMockUpdateStreamSession::UpdateStreamPriority(). |
| 1727 | session->SetExpectedStream(stream); |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 1728 | session->SetExpectedPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
| 1729 | stream->SetPriority(spdy::SpdyStreamPrecedence(kV3HighestPriority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1730 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 1731 | session->SetExpectedPriority(spdy::SpdyStreamPrecedence(kV3LowestPriority)); |
| 1732 | stream->SetPriority(spdy::SpdyStreamPrecedence(kV3LowestPriority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | TEST_P(QuicSpdyStreamTest, StreamWaitsForAcks) { |
| 1736 | Initialize(kShouldProcessData); |
| 1737 | QuicReferenceCountedPointer<MockAckListener> mock_ack_listener( |
| 1738 | new StrictMock<MockAckListener>); |
| 1739 | stream_->set_ack_listener(mock_ack_listener); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1740 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1741 | // Stream is not waiting for acks initially. |
| 1742 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1743 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1744 | |
| 1745 | // Send kData1. |
| 1746 | stream_->WriteOrBufferData("FooAndBar", false, nullptr); |
| 1747 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1748 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1749 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(9, _)); |
| 1750 | QuicByteCount newly_acked_length = 0; |
| 1751 | EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1752 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1753 | &newly_acked_length)); |
| 1754 | // Stream is not waiting for acks as all sent data is acked. |
| 1755 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1756 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1757 | |
| 1758 | // Send kData2. |
| 1759 | stream_->WriteOrBufferData("FooAndBar", false, nullptr); |
| 1760 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1761 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1762 | // Send FIN. |
| 1763 | stream_->WriteOrBufferData("", true, nullptr); |
| 1764 | // Fin only frame is not stored in send buffer. |
| 1765 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1766 | |
| 1767 | // kData2 is retransmitted. |
| 1768 | EXPECT_CALL(*mock_ack_listener, OnPacketRetransmitted(9)); |
| 1769 | stream_->OnStreamFrameRetransmitted(9, 9, false); |
| 1770 | |
| 1771 | // kData2 is acked. |
| 1772 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(9, _)); |
| 1773 | EXPECT_TRUE(stream_->OnStreamFrameAcked(9, 9, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1774 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1775 | &newly_acked_length)); |
| 1776 | // Stream is waiting for acks as FIN is not acked. |
| 1777 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1778 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1779 | |
| 1780 | // FIN is acked. |
| 1781 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(0, _)); |
| 1782 | EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 0, true, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1783 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1784 | &newly_acked_length)); |
| 1785 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1786 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1787 | } |
| 1788 | |
| 1789 | TEST_P(QuicSpdyStreamTest, StreamDataGetAckedMultipleTimes) { |
| 1790 | Initialize(kShouldProcessData); |
| 1791 | QuicReferenceCountedPointer<MockAckListener> mock_ack_listener( |
| 1792 | new StrictMock<MockAckListener>); |
| 1793 | stream_->set_ack_listener(mock_ack_listener); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1794 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1795 | // Send [0, 27) and fin. |
| 1796 | stream_->WriteOrBufferData("FooAndBar", false, nullptr); |
| 1797 | stream_->WriteOrBufferData("FooAndBar", false, nullptr); |
| 1798 | stream_->WriteOrBufferData("FooAndBar", true, nullptr); |
| 1799 | |
| 1800 | // Ack [0, 9), [5, 22) and [18, 26) |
| 1801 | // Verify [0, 9) 9 bytes are acked. |
| 1802 | QuicByteCount newly_acked_length = 0; |
| 1803 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(9, _)); |
| 1804 | EXPECT_TRUE(stream_->OnStreamFrameAcked(0, 9, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1805 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1806 | &newly_acked_length)); |
| 1807 | EXPECT_EQ(2u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1808 | // Verify [9, 22) 13 bytes are acked. |
| 1809 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(13, _)); |
| 1810 | EXPECT_TRUE(stream_->OnStreamFrameAcked(5, 17, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1811 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1812 | &newly_acked_length)); |
| 1813 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1814 | // Verify [22, 26) 4 bytes are acked. |
| 1815 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(4, _)); |
| 1816 | EXPECT_TRUE(stream_->OnStreamFrameAcked(18, 8, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1817 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1818 | &newly_acked_length)); |
| 1819 | EXPECT_EQ(1u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1820 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1821 | |
| 1822 | // Ack [0, 27). |
| 1823 | // Verify [26, 27) 1 byte is acked. |
| 1824 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(1, _)); |
| 1825 | EXPECT_TRUE(stream_->OnStreamFrameAcked(26, 1, false, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1826 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1827 | &newly_acked_length)); |
| 1828 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1829 | EXPECT_TRUE(stream_->IsWaitingForAcks()); |
| 1830 | |
| 1831 | // Ack Fin. Verify OnPacketAcked is called. |
| 1832 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(0, _)); |
| 1833 | EXPECT_TRUE(stream_->OnStreamFrameAcked(27, 0, true, QuicTime::Delta::Zero(), |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1834 | QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1835 | &newly_acked_length)); |
| 1836 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1837 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1838 | |
| 1839 | // Ack [10, 27) and fin. |
| 1840 | // No new data is acked, verify OnPacketAcked is not called. |
| 1841 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(_, _)).Times(0); |
QUICHE team | 2f5f30b | 2020-02-18 08:52:28 -0800 | [diff] [blame] | 1842 | EXPECT_FALSE( |
| 1843 | stream_->OnStreamFrameAcked(10, 17, true, QuicTime::Delta::Zero(), |
| 1844 | QuicTime::Zero(), &newly_acked_length)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1845 | EXPECT_EQ(0u, QuicStreamPeer::SendBuffer(stream_).size()); |
| 1846 | EXPECT_FALSE(stream_->IsWaitingForAcks()); |
| 1847 | } |
| 1848 | |
| 1849 | // HTTP/3 only. |
| 1850 | TEST_P(QuicSpdyStreamTest, HeadersAckNotReportedWriteOrBufferBody) { |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1851 | if (!UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1852 | return; |
| 1853 | } |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1854 | |
| 1855 | Initialize(kShouldProcessData); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1856 | QuicReferenceCountedPointer<MockAckListener> mock_ack_listener( |
| 1857 | new StrictMock<MockAckListener>); |
| 1858 | stream_->set_ack_listener(mock_ack_listener); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1859 | std::string body = "Test1"; |
| 1860 | std::string body2(100, 'x'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1861 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1862 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1863 | stream_->WriteOrBufferBody(body, false); |
| 1864 | stream_->WriteOrBufferBody(body2, true); |
| 1865 | |
| 1866 | std::unique_ptr<char[]> buffer; |
| 1867 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 1868 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1869 | std::string header = std::string(buffer.get(), header_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1870 | |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 1871 | header_length = |
| 1872 | HttpEncoder::SerializeDataFrameHeader(body2.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1873 | std::string header2 = std::string(buffer.get(), header_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1874 | |
| 1875 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(body.length(), _)); |
| 1876 | QuicStreamFrame frame(stream_->id(), false, 0, header + body); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1877 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame), QuicTime::Delta::Zero(), |
| 1878 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1879 | |
| 1880 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(0, _)); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1881 | QuicStreamFrame frame2(stream_->id(), false, header.length() + body.length(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1882 | header2); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1883 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame2), QuicTime::Delta::Zero(), |
| 1884 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1885 | |
| 1886 | EXPECT_CALL(*mock_ack_listener, OnPacketAcked(body2.length(), _)); |
| 1887 | QuicStreamFrame frame3(stream_->id(), true, |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1888 | header.length() + body.length() + header2.length(), |
| 1889 | body2); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1890 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame3), QuicTime::Delta::Zero(), |
| 1891 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1892 | |
| 1893 | EXPECT_TRUE( |
| 1894 | QuicSpdyStreamPeer::unacked_frame_headers_offsets(stream_).Empty()); |
| 1895 | } |
| 1896 | |
| 1897 | // HTTP/3 only. |
| 1898 | TEST_P(QuicSpdyStreamTest, HeadersAckNotReportedWriteBodySlices) { |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1899 | if (!UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1900 | return; |
| 1901 | } |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1902 | |
| 1903 | Initialize(kShouldProcessData); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1904 | QuicReferenceCountedPointer<MockAckListener> mock_ack_listener( |
| 1905 | new StrictMock<MockAckListener>); |
| 1906 | stream_->set_ack_listener(mock_ack_listener); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1907 | std::string body1 = "Test1"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1908 | std::string body2(100, 'x'); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1909 | struct iovec body1_iov = {const_cast<char*>(body1.data()), body1.length()}; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1910 | struct iovec body2_iov = {const_cast<char*>(body2.data()), body2.length()}; |
| 1911 | QuicMemSliceStorage storage(&body1_iov, 1, |
| 1912 | helper_.GetStreamSendBufferAllocator(), 1024); |
| 1913 | QuicMemSliceStorage storage2(&body2_iov, 1, |
| 1914 | helper_.GetStreamSendBufferAllocator(), 1024); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1915 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1916 | stream_->WriteBodySlices(storage.ToSpan(), false); |
| 1917 | stream_->WriteBodySlices(storage2.ToSpan(), true); |
| 1918 | |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1919 | std::string data1 = DataFrame(body1); |
| 1920 | std::string data2 = DataFrame(body2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1921 | |
| 1922 | EXPECT_CALL(*mock_ack_listener, |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1923 | OnPacketAcked(body1.length() + body2.length(), _)); |
| 1924 | QuicStreamFrame frame(stream_->id(), true, 0, data1 + data2); |
QUICHE team | 9467db0 | 2019-05-30 09:38:45 -0700 | [diff] [blame] | 1925 | EXPECT_TRUE(session_->OnFrameAcked(QuicFrame(frame), QuicTime::Delta::Zero(), |
| 1926 | QuicTime::Zero())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1927 | |
| 1928 | EXPECT_TRUE( |
| 1929 | QuicSpdyStreamPeer::unacked_frame_headers_offsets(stream_).Empty()); |
| 1930 | } |
| 1931 | |
| 1932 | // HTTP/3 only. |
| 1933 | TEST_P(QuicSpdyStreamTest, HeaderBytesNotReportedOnRetransmission) { |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 1934 | if (!UsesHttp3()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1935 | return; |
| 1936 | } |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1937 | |
| 1938 | Initialize(kShouldProcessData); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1939 | QuicReferenceCountedPointer<MockAckListener> mock_ack_listener( |
| 1940 | new StrictMock<MockAckListener>); |
| 1941 | stream_->set_ack_listener(mock_ack_listener); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1942 | std::string body1 = "Test1"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1943 | std::string body2(100, 'x'); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1944 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 1945 | EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)).Times(AtLeast(1)); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1946 | stream_->WriteOrBufferBody(body1, false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1947 | stream_->WriteOrBufferBody(body2, true); |
| 1948 | |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1949 | std::string data1 = DataFrame(body1); |
| 1950 | std::string data2 = DataFrame(body2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1951 | |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1952 | EXPECT_CALL(*mock_ack_listener, OnPacketRetransmitted(body1.length())); |
| 1953 | QuicStreamFrame frame(stream_->id(), false, 0, data1); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1954 | session_->OnStreamFrameRetransmitted(frame); |
| 1955 | |
| 1956 | EXPECT_CALL(*mock_ack_listener, OnPacketRetransmitted(body2.length())); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1957 | QuicStreamFrame frame2(stream_->id(), true, data1.length(), data2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1958 | session_->OnStreamFrameRetransmitted(frame2); |
| 1959 | |
| 1960 | EXPECT_FALSE( |
| 1961 | QuicSpdyStreamPeer::unacked_frame_headers_offsets(stream_).Empty()); |
| 1962 | } |
| 1963 | |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1964 | TEST_P(QuicSpdyStreamTest, HeadersFrameOnRequestStream) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1965 | if (!UsesHttp3()) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1966 | return; |
| 1967 | } |
| 1968 | |
| 1969 | Initialize(kShouldProcessData); |
| 1970 | |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 1971 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1972 | std::string data = DataFrame(kDataFramePayload); |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 1973 | std::string trailers = |
| 1974 | HeadersFrame({std::make_pair("custom-key", "custom-value")}); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1975 | |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 1976 | std::string stream_frame_payload = |
| 1977 | quiche::QuicheStrCat(headers, data, trailers); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1978 | QuicStreamFrame frame(stream_->id(), false, 0, stream_frame_payload); |
| 1979 | stream_->OnStreamFrame(frame); |
| 1980 | |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 1981 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1982 | |
| 1983 | // QuicSpdyStream only calls OnBodyAvailable() |
| 1984 | // after the header list has been consumed. |
| 1985 | EXPECT_EQ("", stream_->data()); |
| 1986 | stream_->ConsumeHeaderList(); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 1987 | EXPECT_EQ(kDataFramePayload, stream_->data()); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1988 | |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 1989 | EXPECT_THAT(stream_->received_trailers(), |
| 1990 | ElementsAre(Pair("custom-key", "custom-value"))); |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 1993 | TEST_P(QuicSpdyStreamTest, ProcessBodyAfterTrailers) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 1994 | if (!UsesHttp3()) { |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 1995 | return; |
| 1996 | } |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 1997 | |
| 1998 | Initialize(!kShouldProcessData); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 1999 | |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2000 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 2001 | std::string data = DataFrame(kDataFramePayload); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 2002 | |
| 2003 | // A header block that will take more than one block of sequencer buffer. |
| 2004 | // This ensures that when the trailers are consumed, some buffer buckets will |
| 2005 | // be freed. |
| 2006 | SpdyHeaderBlock trailers_block; |
| 2007 | trailers_block["key1"] = std::string(10000, 'x'); |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2008 | std::string trailers = HeadersFrame(trailers_block); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 2009 | |
bnc | d48a92e | 2019-06-18 19:24:58 -0700 | [diff] [blame] | 2010 | // Feed all three HTTP/3 frames in a single stream frame. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2011 | std::string stream_frame_payload = |
| 2012 | quiche::QuicheStrCat(headers, data, trailers); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 2013 | QuicStreamFrame frame(stream_->id(), false, 0, stream_frame_payload); |
| 2014 | stream_->OnStreamFrame(frame); |
| 2015 | |
| 2016 | stream_->ConsumeHeaderList(); |
| 2017 | stream_->MarkTrailersConsumed(); |
bnc | d48a92e | 2019-06-18 19:24:58 -0700 | [diff] [blame] | 2018 | |
| 2019 | EXPECT_TRUE(stream_->trailers_decompressed()); |
| 2020 | EXPECT_EQ(trailers_block, stream_->received_trailers()); |
| 2021 | |
bnc | ab33c71 | 2019-06-28 15:42:15 -0700 | [diff] [blame] | 2022 | EXPECT_TRUE(stream_->HasBytesToRead()); |
bnc | d48a92e | 2019-06-18 19:24:58 -0700 | [diff] [blame] | 2023 | |
| 2024 | // Consume data. |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 2025 | char buffer[2048]; |
| 2026 | struct iovec vec; |
| 2027 | vec.iov_base = buffer; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 2028 | vec.iov_len = QUICHE_ARRAYSIZE(buffer); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 2029 | size_t bytes_read = stream_->Readv(&vec, 1); |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2030 | EXPECT_EQ(kDataFramePayload, quiche::QuicheStringPiece(buffer, bytes_read)); |
bnc | d48a92e | 2019-06-18 19:24:58 -0700 | [diff] [blame] | 2031 | |
bnc | ab33c71 | 2019-06-28 15:42:15 -0700 | [diff] [blame] | 2032 | EXPECT_FALSE(stream_->HasBytesToRead()); |
renjietang | bd1a039 | 2019-05-31 11:36:24 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
bnc | 677451a | 2019-06-07 10:13:30 -0700 | [diff] [blame] | 2035 | // The test stream will receive a stream frame containing malformed headers and |
| 2036 | // normal body. Make sure the http decoder stops processing body after the |
| 2037 | // connection shuts down. |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2038 | TEST_P(QuicSpdyStreamTest, MalformedHeadersStopHttpDecoder) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2039 | if (!UsesHttp3()) { |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
| 2043 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 2044 | testing::InSequence s; |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2045 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 2046 | |
| 2047 | // Random bad headers. |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 2048 | std::string headers = |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2049 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("00002a94e7036261")); |
bnc | 9f977e4 | 2019-06-07 11:36:34 -0700 | [diff] [blame] | 2050 | std::string data = DataFrame(kDataFramePayload); |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2051 | |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2052 | std::string stream_frame_payload = quiche::QuicheStrCat(headers, data); |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2053 | QuicStreamFrame frame(stream_->id(), false, 0, stream_frame_payload); |
| 2054 | |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 2055 | EXPECT_CALL( |
| 2056 | *connection_, |
bnc | 232ff9b | 2019-11-13 18:38:22 -0800 | [diff] [blame] | 2057 | CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED, |
| 2058 | MatchesRegex("Error decoding headers on stream \\d+: " |
| 2059 | "Incomplete header block."), |
| 2060 | _)) |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2061 | .WillOnce( |
| 2062 | (Invoke([this](QuicErrorCode error, const std::string& error_details, |
| 2063 | ConnectionCloseBehavior connection_close_behavior) { |
| 2064 | connection_->ReallyCloseConnection(error, error_details, |
| 2065 | connection_close_behavior); |
| 2066 | }))); |
| 2067 | EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _)); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 2068 | EXPECT_CALL(*session_, OnConnectionClosed(_, _)) |
| 2069 | .WillOnce(Invoke([this](const QuicConnectionCloseFrame& frame, |
| 2070 | ConnectionCloseSource source) { |
| 2071 | session_->ReallyOnConnectionClosed(frame, source); |
| 2072 | })); |
renjietang | 546a628 | 2019-06-03 10:21:21 -0700 | [diff] [blame] | 2073 | EXPECT_CALL(*session_, SendRstStream(_, _, _)); |
| 2074 | EXPECT_CALL(*session_, SendRstStream(_, _, _)); |
| 2075 | stream_->OnStreamFrame(frame); |
| 2076 | } |
| 2077 | |
bnc | f9ff46b | 2019-11-26 10:32:09 -0800 | [diff] [blame] | 2078 | // Regression test for https://crbug.com/1027895: a HEADERS frame triggers an |
| 2079 | // error in QuicSpdyStream::OnHeadersFramePayload(). This closes the |
| 2080 | // connection, freeing the buffer of QuicStreamSequencer. Therefore |
| 2081 | // QuicStreamSequencer::MarkConsumed() must not be called from |
| 2082 | // QuicSpdyStream::OnHeadersFramePayload(). |
| 2083 | TEST_P(QuicSpdyStreamTest, DoNotMarkConsumedAfterQpackDecodingError) { |
| 2084 | if (!UsesHttp3()) { |
| 2085 | return; |
| 2086 | } |
| 2087 | |
| 2088 | Initialize(kShouldProcessData); |
| 2089 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 2090 | |
| 2091 | testing::InSequence s; |
| 2092 | EXPECT_CALL( |
| 2093 | *connection_, |
| 2094 | CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED, |
| 2095 | MatchesRegex("Error decoding headers on stream \\d+: " |
| 2096 | "Invalid relative index."), |
| 2097 | _)) |
| 2098 | .WillOnce( |
| 2099 | (Invoke([this](QuicErrorCode error, const std::string& error_details, |
| 2100 | ConnectionCloseBehavior connection_close_behavior) { |
| 2101 | connection_->ReallyCloseConnection(error, error_details, |
| 2102 | connection_close_behavior); |
| 2103 | }))); |
| 2104 | EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _)); |
| 2105 | EXPECT_CALL(*session_, OnConnectionClosed(_, _)) |
| 2106 | .WillOnce(Invoke([this](const QuicConnectionCloseFrame& frame, |
| 2107 | ConnectionCloseSource source) { |
| 2108 | session_->ReallyOnConnectionClosed(frame, source); |
| 2109 | })); |
| 2110 | EXPECT_CALL(*session_, SendRstStream(stream_->id(), _, _)); |
| 2111 | EXPECT_CALL(*session_, SendRstStream(stream2_->id(), _, _)); |
| 2112 | |
| 2113 | // Invalid headers: Required Insert Count is zero, but the header block |
| 2114 | // contains a dynamic table reference. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2115 | std::string headers = |
| 2116 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("000080")); |
bnc | f9ff46b | 2019-11-26 10:32:09 -0800 | [diff] [blame] | 2117 | QuicStreamFrame frame(stream_->id(), false, 0, headers); |
| 2118 | stream_->OnStreamFrame(frame); |
| 2119 | } |
| 2120 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2121 | TEST_P(QuicSpdyStreamTest, ImmediateHeaderDecodingWithDynamicTableEntries) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2122 | if (!UsesHttp3()) { |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 2127 | testing::InSequence s; |
bnc | 40e9a7b | 2019-08-30 05:27:08 -0700 | [diff] [blame] | 2128 | session_->qpack_decoder()->OnSetDynamicTableCapacity(1024); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2129 | |
renjietang | 87cd7de | 2019-08-16 08:35:10 -0700 | [diff] [blame] | 2130 | auto decoder_send_stream = |
| 2131 | QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get()); |
| 2132 | |
| 2133 | // The stream byte will be written in the first byte. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2134 | EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2135 | // Deliver dynamic table entry to decoder. |
| 2136 | session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar"); |
| 2137 | |
| 2138 | // HEADERS frame referencing first dynamic table entry. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2139 | std::string headers = |
| 2140 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("020080")); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2141 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers)); |
| 2142 | |
| 2143 | // Headers can be decoded immediately. |
| 2144 | EXPECT_TRUE(stream_->headers_decompressed()); |
| 2145 | |
| 2146 | // Verify headers. |
| 2147 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2148 | stream_->ConsumeHeaderList(); |
| 2149 | |
| 2150 | // DATA frame. |
| 2151 | std::string data = DataFrame(kDataFramePayload); |
| 2152 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, /* offset = */ |
| 2153 | headers.length(), data)); |
| 2154 | EXPECT_EQ(kDataFramePayload, stream_->data()); |
| 2155 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2156 | EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2157 | // Deliver second dynamic table entry to decoder. |
| 2158 | session_->qpack_decoder()->OnInsertWithoutNameReference("trailing", "foobar"); |
| 2159 | |
| 2160 | // Trailing HEADERS frame referencing second dynamic table entry. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2161 | std::string trailers = |
| 2162 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("030080")); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2163 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), true, /* offset = */ |
| 2164 | headers.length() + data.length(), |
| 2165 | trailers)); |
| 2166 | |
| 2167 | // Trailers can be decoded immediately. |
| 2168 | EXPECT_TRUE(stream_->trailers_decompressed()); |
| 2169 | |
| 2170 | // Verify trailers. |
| 2171 | EXPECT_THAT(stream_->received_trailers(), |
| 2172 | ElementsAre(Pair("trailing", "foobar"))); |
| 2173 | stream_->MarkTrailersConsumed(); |
| 2174 | } |
| 2175 | |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2176 | TEST_P(QuicSpdyStreamTest, BlockedHeaderDecoding) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2177 | if (!UsesHttp3()) { |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
| 2181 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 2182 | testing::InSequence s; |
bnc | 40e9a7b | 2019-08-30 05:27:08 -0700 | [diff] [blame] | 2183 | session_->qpack_decoder()->OnSetDynamicTableCapacity(1024); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2184 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2185 | // HEADERS frame referencing first dynamic table entry. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2186 | std::string headers = |
| 2187 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("020080")); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2188 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers)); |
| 2189 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2190 | // Decoding is blocked because dynamic table entry has not been received yet. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2191 | EXPECT_FALSE(stream_->headers_decompressed()); |
| 2192 | |
renjietang | 87cd7de | 2019-08-16 08:35:10 -0700 | [diff] [blame] | 2193 | auto decoder_send_stream = |
| 2194 | QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get()); |
| 2195 | |
| 2196 | // The stream byte will be written in the first byte. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2197 | EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2198 | // Deliver dynamic table entry to decoder. |
| 2199 | session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar"); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2200 | EXPECT_TRUE(stream_->headers_decompressed()); |
| 2201 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2202 | // Verify headers. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2203 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2204 | stream_->ConsumeHeaderList(); |
| 2205 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2206 | // DATA frame. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2207 | std::string data = DataFrame(kDataFramePayload); |
| 2208 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, /* offset = */ |
| 2209 | headers.length(), data)); |
| 2210 | EXPECT_EQ(kDataFramePayload, stream_->data()); |
| 2211 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2212 | // Trailing HEADERS frame referencing second dynamic table entry. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2213 | std::string trailers = |
| 2214 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("030080")); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2215 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), true, /* offset = */ |
| 2216 | headers.length() + data.length(), |
| 2217 | trailers)); |
| 2218 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2219 | // Decoding is blocked because dynamic table entry has not been received yet. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2220 | EXPECT_FALSE(stream_->trailers_decompressed()); |
| 2221 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2222 | EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2223 | // Deliver second dynamic table entry to decoder. |
| 2224 | session_->qpack_decoder()->OnInsertWithoutNameReference("trailing", "foobar"); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2225 | EXPECT_TRUE(stream_->trailers_decompressed()); |
| 2226 | |
| 2227 | // Verify trailers. |
| 2228 | EXPECT_THAT(stream_->received_trailers(), |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2229 | ElementsAre(Pair("trailing", "foobar"))); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2230 | stream_->MarkTrailersConsumed(); |
| 2231 | } |
| 2232 | |
| 2233 | TEST_P(QuicSpdyStreamTest, AsyncErrorDecodingHeaders) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2234 | if (!UsesHttp3()) { |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2235 | return; |
| 2236 | } |
| 2237 | |
| 2238 | Initialize(kShouldProcessData); |
bnc | 40e9a7b | 2019-08-30 05:27:08 -0700 | [diff] [blame] | 2239 | session_->qpack_decoder()->OnSetDynamicTableCapacity(1024); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2240 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2241 | // HEADERS frame only referencing entry with absolute index 0 but with |
| 2242 | // Required Insert Count = 2, which is incorrect. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2243 | std::string headers = |
| 2244 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("030081")); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2245 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers)); |
| 2246 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2247 | // Even though entire header block is received and every referenced entry is |
| 2248 | // available, decoding is blocked until insert count reaches the Required |
| 2249 | // Insert Count value advertised in the header block prefix. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2250 | EXPECT_FALSE(stream_->headers_decompressed()); |
| 2251 | |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2252 | EXPECT_CALL( |
| 2253 | *connection_, |
bnc | 232ff9b | 2019-11-13 18:38:22 -0800 | [diff] [blame] | 2254 | CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED, |
| 2255 | MatchesRegex("Error decoding headers on stream \\d+: " |
| 2256 | "Required Insert Count too large."), |
| 2257 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2258 | |
| 2259 | // Deliver two dynamic table entries to decoder |
| 2260 | // to trigger decoding of header block. |
| 2261 | session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar"); |
| 2262 | session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar"); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 2265 | // Regression test for https://crbug.com/1024263 and for |
| 2266 | // https://crbug.com/1025209#c11. |
| 2267 | TEST_P(QuicSpdyStreamTest, BlockedHeaderDecodingUnblockedWithBufferedError) { |
| 2268 | if (!UsesHttp3()) { |
| 2269 | return; |
| 2270 | } |
| 2271 | |
| 2272 | Initialize(kShouldProcessData); |
| 2273 | session_->qpack_decoder()->OnSetDynamicTableCapacity(1024); |
| 2274 | |
| 2275 | // Relative index 2 is invalid because it is larger than or equal to the Base. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2276 | std::string headers = |
| 2277 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("020082")); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 2278 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers)); |
| 2279 | |
| 2280 | // Decoding is blocked. |
| 2281 | EXPECT_FALSE(stream_->headers_decompressed()); |
| 2282 | |
| 2283 | EXPECT_CALL( |
| 2284 | *connection_, |
| 2285 | CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED, |
| 2286 | MatchesRegex("Error decoding headers on stream \\d+: " |
| 2287 | "Invalid relative index."), |
| 2288 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET)); |
| 2289 | |
| 2290 | // Deliver one dynamic table entry to decoder |
| 2291 | // to trigger decoding of header block. |
| 2292 | session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar"); |
| 2293 | } |
| 2294 | |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2295 | TEST_P(QuicSpdyStreamTest, AsyncErrorDecodingTrailers) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2296 | if (!UsesHttp3()) { |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2297 | return; |
| 2298 | } |
| 2299 | |
| 2300 | Initialize(kShouldProcessData); |
fayang | 8790d29 | 2020-03-18 13:17:23 -0700 | [diff] [blame] | 2301 | testing::InSequence s; |
bnc | 40e9a7b | 2019-08-30 05:27:08 -0700 | [diff] [blame] | 2302 | session_->qpack_decoder()->OnSetDynamicTableCapacity(1024); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2303 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2304 | // HEADERS frame referencing first dynamic table entry. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2305 | std::string headers = |
| 2306 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("020080")); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2307 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, headers)); |
| 2308 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2309 | // Decoding is blocked because dynamic table entry has not been received yet. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2310 | EXPECT_FALSE(stream_->headers_decompressed()); |
| 2311 | |
renjietang | 87cd7de | 2019-08-16 08:35:10 -0700 | [diff] [blame] | 2312 | auto decoder_send_stream = |
| 2313 | QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get()); |
| 2314 | |
| 2315 | // The stream byte will be written in the first byte. |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2316 | EXPECT_CALL(*session_, WritevData(decoder_send_stream->id(), _, _, _, _, _)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2317 | // Deliver dynamic table entry to decoder. |
| 2318 | session_->qpack_decoder()->OnInsertWithoutNameReference("foo", "bar"); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2319 | EXPECT_TRUE(stream_->headers_decompressed()); |
| 2320 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2321 | // Verify headers. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2322 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2323 | stream_->ConsumeHeaderList(); |
| 2324 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2325 | // DATA frame. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2326 | std::string data = DataFrame(kDataFramePayload); |
| 2327 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, /* offset = */ |
| 2328 | headers.length(), data)); |
| 2329 | EXPECT_EQ(kDataFramePayload, stream_->data()); |
| 2330 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2331 | // Trailing HEADERS frame only referencing entry with absolute index 0 but |
| 2332 | // with Required Insert Count = 2, which is incorrect. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2333 | std::string trailers = |
| 2334 | HeadersFrame(quiche::QuicheTextUtils::HexDecode("030081")); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2335 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), true, /* offset = */ |
| 2336 | headers.length() + data.length(), |
| 2337 | trailers)); |
| 2338 | |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2339 | // Even though entire header block is received and every referenced entry is |
| 2340 | // available, decoding is blocked until insert count reaches the Required |
| 2341 | // Insert Count value advertised in the header block prefix. |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2342 | EXPECT_FALSE(stream_->trailers_decompressed()); |
| 2343 | |
bnc | 232ff9b | 2019-11-13 18:38:22 -0800 | [diff] [blame] | 2344 | EXPECT_CALL( |
| 2345 | *connection_, |
| 2346 | CloseConnection(QUIC_QPACK_DECOMPRESSION_FAILED, |
| 2347 | MatchesRegex("Error decoding trailers on stream \\d+: " |
| 2348 | "Required Insert Count too large."), |
| 2349 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET)); |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 2350 | |
| 2351 | // Deliver second dynamic table entry to decoder |
| 2352 | // to trigger decoding of trailing header block. |
| 2353 | session_->qpack_decoder()->OnInsertWithoutNameReference("trailing", "foobar"); |
bnc | c057c01 | 2019-07-02 11:13:22 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2356 | class QuicSpdyStreamIncrementalConsumptionTest : public QuicSpdyStreamTest { |
| 2357 | protected: |
| 2358 | QuicSpdyStreamIncrementalConsumptionTest() : offset_(0), consumed_bytes_(0) {} |
| 2359 | ~QuicSpdyStreamIncrementalConsumptionTest() override = default; |
| 2360 | |
| 2361 | // Create QuicStreamFrame with |payload| |
| 2362 | // and pass it to stream_->OnStreamFrame(). |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2363 | void OnStreamFrame(quiche::QuicheStringPiece payload) { |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2364 | QuicStreamFrame frame(stream_->id(), /* fin = */ false, offset_, payload); |
| 2365 | stream_->OnStreamFrame(frame); |
| 2366 | offset_ += payload.size(); |
| 2367 | } |
| 2368 | |
| 2369 | // Return number of bytes marked consumed with sequencer |
| 2370 | // since last NewlyConsumedBytes() call. |
| 2371 | QuicStreamOffset NewlyConsumedBytes() { |
| 2372 | QuicStreamOffset previously_consumed_bytes = consumed_bytes_; |
| 2373 | consumed_bytes_ = stream_->sequencer()->NumBytesConsumed(); |
| 2374 | return consumed_bytes_ - previously_consumed_bytes; |
| 2375 | } |
| 2376 | |
| 2377 | // Read |size| bytes from the stream. |
| 2378 | std::string ReadFromStream(QuicByteCount size) { |
| 2379 | std::string buffer; |
| 2380 | buffer.resize(size); |
| 2381 | |
| 2382 | struct iovec vec; |
| 2383 | vec.iov_base = const_cast<char*>(buffer.data()); |
| 2384 | vec.iov_len = size; |
| 2385 | |
| 2386 | size_t bytes_read = stream_->Readv(&vec, 1); |
| 2387 | EXPECT_EQ(bytes_read, size); |
| 2388 | |
| 2389 | return buffer; |
| 2390 | } |
| 2391 | |
| 2392 | private: |
| 2393 | QuicStreamOffset offset_; |
| 2394 | QuicStreamOffset consumed_bytes_; |
| 2395 | }; |
| 2396 | |
| 2397 | INSTANTIATE_TEST_SUITE_P(Tests, |
| 2398 | QuicSpdyStreamIncrementalConsumptionTest, |
dschinazi | 38cc1ee | 2020-02-28 14:33:58 -0800 | [diff] [blame] | 2399 | ::testing::ValuesIn(AllSupportedVersions()), |
dschinazi | 142051a | 2019-09-18 18:17:29 -0700 | [diff] [blame] | 2400 | ::testing::PrintToStringParamName()); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2401 | |
| 2402 | // Test that stream bytes are consumed (by calling |
| 2403 | // sequencer()->MarkConsumed()) incrementally, as soon as possible. |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2404 | TEST_P(QuicSpdyStreamIncrementalConsumptionTest, OnlyKnownFrames) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2405 | if (!UsesHttp3()) { |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2406 | return; |
| 2407 | } |
| 2408 | |
| 2409 | Initialize(!kShouldProcessData); |
| 2410 | |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2411 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2412 | |
| 2413 | // All HEADERS frame bytes are consumed even if the frame is not received |
bnc | 7deaf37 | 2019-07-25 18:00:40 -0700 | [diff] [blame] | 2414 | // completely. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2415 | OnStreamFrame( |
| 2416 | quiche::QuicheStringPiece(headers).substr(0, headers.size() - 1)); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2417 | EXPECT_EQ(headers.size() - 1, NewlyConsumedBytes()); |
| 2418 | |
| 2419 | // The rest of the HEADERS frame is also consumed immediately. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2420 | OnStreamFrame(quiche::QuicheStringPiece(headers).substr(headers.size() - 1)); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2421 | EXPECT_EQ(1u, NewlyConsumedBytes()); |
| 2422 | |
| 2423 | // Verify headers. |
| 2424 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2425 | stream_->ConsumeHeaderList(); |
| 2426 | |
| 2427 | // DATA frame. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2428 | quiche::QuicheStringPiece data_payload(kDataFramePayload); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2429 | std::string data_frame = DataFrame(data_payload); |
bnc | e5f9c03 | 2019-07-25 11:30:40 -0700 | [diff] [blame] | 2430 | QuicByteCount data_frame_header_length = |
| 2431 | data_frame.size() - data_payload.size(); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2432 | |
bnc | e5f9c03 | 2019-07-25 11:30:40 -0700 | [diff] [blame] | 2433 | // DATA frame header is consumed. |
| 2434 | // DATA frame payload is not consumed because payload has to be buffered. |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2435 | OnStreamFrame(data_frame); |
bnc | e5f9c03 | 2019-07-25 11:30:40 -0700 | [diff] [blame] | 2436 | EXPECT_EQ(data_frame_header_length, NewlyConsumedBytes()); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2437 | |
| 2438 | // Consume all but last byte of data. |
| 2439 | EXPECT_EQ(data_payload.substr(0, data_payload.size() - 1), |
| 2440 | ReadFromStream(data_payload.size() - 1)); |
bnc | e5f9c03 | 2019-07-25 11:30:40 -0700 | [diff] [blame] | 2441 | EXPECT_EQ(data_payload.size() - 1, NewlyConsumedBytes()); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2442 | |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2443 | std::string trailers = |
| 2444 | HeadersFrame({std::make_pair("custom-key", "custom-value")}); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2445 | |
| 2446 | // No bytes are consumed, because last byte of DATA payload is still buffered. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2447 | OnStreamFrame( |
| 2448 | quiche::QuicheStringPiece(trailers).substr(0, trailers.size() - 1)); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2449 | EXPECT_EQ(0u, NewlyConsumedBytes()); |
| 2450 | |
| 2451 | // Reading last byte of DATA payload triggers consumption of all data received |
| 2452 | // so far, even though last HEADERS frame has not been received completely. |
| 2453 | EXPECT_EQ(data_payload.substr(data_payload.size() - 1), ReadFromStream(1)); |
| 2454 | EXPECT_EQ(1 + trailers.size() - 1, NewlyConsumedBytes()); |
| 2455 | |
| 2456 | // Last byte of trailers is immediately consumed. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2457 | OnStreamFrame( |
| 2458 | quiche::QuicheStringPiece(trailers).substr(trailers.size() - 1)); |
bnc | 8d04130 | 2019-06-10 10:19:04 -0700 | [diff] [blame] | 2459 | EXPECT_EQ(1u, NewlyConsumedBytes()); |
| 2460 | |
| 2461 | // Verify trailers. |
| 2462 | EXPECT_THAT(stream_->received_trailers(), |
| 2463 | ElementsAre(Pair("custom-key", "custom-value"))); |
| 2464 | } |
| 2465 | |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2466 | TEST_P(QuicSpdyStreamIncrementalConsumptionTest, UnknownFramesInterleaved) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2467 | if (!UsesHttp3()) { |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2468 | return; |
| 2469 | } |
| 2470 | |
| 2471 | Initialize(!kShouldProcessData); |
| 2472 | |
| 2473 | // Unknown frame of reserved type before HEADERS is consumed immediately. |
| 2474 | std::string unknown_frame1 = UnknownFrame(0x21, "foo"); |
| 2475 | OnStreamFrame(unknown_frame1); |
| 2476 | EXPECT_EQ(unknown_frame1.size(), NewlyConsumedBytes()); |
| 2477 | |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2478 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2479 | |
| 2480 | // All HEADERS frame bytes are consumed even if the frame is not received |
| 2481 | // completely. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2482 | OnStreamFrame( |
| 2483 | quiche::QuicheStringPiece(headers).substr(0, headers.size() - 1)); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2484 | EXPECT_EQ(headers.size() - 1, NewlyConsumedBytes()); |
| 2485 | |
| 2486 | // The rest of the HEADERS frame is also consumed immediately. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2487 | OnStreamFrame(quiche::QuicheStringPiece(headers).substr(headers.size() - 1)); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2488 | EXPECT_EQ(1u, NewlyConsumedBytes()); |
| 2489 | |
| 2490 | // Verify headers. |
| 2491 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2492 | stream_->ConsumeHeaderList(); |
| 2493 | |
| 2494 | // Frame of unknown, not reserved type between HEADERS and DATA is consumed |
| 2495 | // immediately. |
| 2496 | std::string unknown_frame2 = UnknownFrame(0x3a, ""); |
| 2497 | OnStreamFrame(unknown_frame2); |
| 2498 | EXPECT_EQ(unknown_frame2.size(), NewlyConsumedBytes()); |
| 2499 | |
| 2500 | // DATA frame. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2501 | quiche::QuicheStringPiece data_payload(kDataFramePayload); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2502 | std::string data_frame = DataFrame(data_payload); |
| 2503 | QuicByteCount data_frame_header_length = |
| 2504 | data_frame.size() - data_payload.size(); |
| 2505 | |
| 2506 | // DATA frame header is consumed. |
| 2507 | // DATA frame payload is not consumed because payload has to be buffered. |
| 2508 | OnStreamFrame(data_frame); |
| 2509 | EXPECT_EQ(data_frame_header_length, NewlyConsumedBytes()); |
| 2510 | |
| 2511 | // Frame of unknown, not reserved type is not consumed because DATA payload is |
| 2512 | // still buffered. |
| 2513 | std::string unknown_frame3 = UnknownFrame(0x39, "bar"); |
| 2514 | OnStreamFrame(unknown_frame3); |
| 2515 | EXPECT_EQ(0u, NewlyConsumedBytes()); |
| 2516 | |
| 2517 | // Consume all but last byte of data. |
| 2518 | EXPECT_EQ(data_payload.substr(0, data_payload.size() - 1), |
| 2519 | ReadFromStream(data_payload.size() - 1)); |
| 2520 | EXPECT_EQ(data_payload.size() - 1, NewlyConsumedBytes()); |
| 2521 | |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2522 | std::string trailers = |
| 2523 | HeadersFrame({std::make_pair("custom-key", "custom-value")}); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2524 | |
| 2525 | // No bytes are consumed, because last byte of DATA payload is still buffered. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2526 | OnStreamFrame( |
| 2527 | quiche::QuicheStringPiece(trailers).substr(0, trailers.size() - 1)); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2528 | EXPECT_EQ(0u, NewlyConsumedBytes()); |
| 2529 | |
| 2530 | // Reading last byte of DATA payload triggers consumption of all data received |
| 2531 | // so far, even though last HEADERS frame has not been received completely. |
| 2532 | EXPECT_EQ(data_payload.substr(data_payload.size() - 1), ReadFromStream(1)); |
| 2533 | EXPECT_EQ(1 + unknown_frame3.size() + trailers.size() - 1, |
| 2534 | NewlyConsumedBytes()); |
| 2535 | |
| 2536 | // Last byte of trailers is immediately consumed. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2537 | OnStreamFrame( |
| 2538 | quiche::QuicheStringPiece(trailers).substr(trailers.size() - 1)); |
bnc | 25827c4 | 2019-07-29 08:57:24 -0700 | [diff] [blame] | 2539 | EXPECT_EQ(1u, NewlyConsumedBytes()); |
| 2540 | |
| 2541 | // Verify trailers. |
| 2542 | EXPECT_THAT(stream_->received_trailers(), |
| 2543 | ElementsAre(Pair("custom-key", "custom-value"))); |
| 2544 | |
| 2545 | // Unknown frame of reserved type after trailers is consumed immediately. |
| 2546 | std::string unknown_frame4 = UnknownFrame(0x40, ""); |
| 2547 | OnStreamFrame(unknown_frame4); |
| 2548 | EXPECT_EQ(unknown_frame4.size(), NewlyConsumedBytes()); |
| 2549 | } |
| 2550 | |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 2551 | TEST_P(QuicSpdyStreamTest, PushPromiseOnDataStream) { |
QUICHE team | 6c57946 | 2019-06-21 14:06:09 -0700 | [diff] [blame] | 2552 | Initialize(kShouldProcessData); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 2553 | if (!UsesHttp3()) { |
QUICHE team | 6c57946 | 2019-06-21 14:06:09 -0700 | [diff] [blame] | 2554 | return; |
| 2555 | } |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 2556 | |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2557 | std::string headers = EncodeQpackHeaders({{"foo", "bar"}}); |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 2558 | |
QUICHE team | 6c57946 | 2019-06-21 14:06:09 -0700 | [diff] [blame] | 2559 | PushPromiseFrame push_promise; |
| 2560 | push_promise.push_id = 0x01; |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 2561 | push_promise.headers = headers; |
QUICHE team | 6c57946 | 2019-06-21 14:06:09 -0700 | [diff] [blame] | 2562 | std::unique_ptr<char[]> buffer; |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 2563 | uint64_t length = HttpEncoder::SerializePushPromiseFrameWithOnlyPushId( |
| 2564 | push_promise, &buffer); |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 2565 | std::string data = std::string(buffer.get(), length) + headers; |
| 2566 | QuicStreamFrame frame(stream_->id(), false, 0, data); |
| 2567 | |
| 2568 | EXPECT_CALL(*session_, |
| 2569 | OnPromiseHeaderList(stream_->id(), push_promise.push_id, |
| 2570 | headers.length(), _)); |
QUICHE team | 6c57946 | 2019-06-21 14:06:09 -0700 | [diff] [blame] | 2571 | stream_->OnStreamFrame(frame); |
| 2572 | } |
| 2573 | |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2574 | // Close connection if a DATA frame is received before a HEADERS frame. |
| 2575 | TEST_P(QuicSpdyStreamTest, DataBeforeHeaders) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2576 | if (!UsesHttp3()) { |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2577 | return; |
| 2578 | } |
| 2579 | |
| 2580 | Initialize(kShouldProcessData); |
| 2581 | |
| 2582 | // Closing the connection is mocked out in tests. Instead, simply stop |
| 2583 | // reading data at the stream level to prevent QuicSpdyStream from blowing up. |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2584 | EXPECT_CALL( |
| 2585 | *connection_, |
bnc | 3e8fe7c | 2020-03-10 17:44:58 -0700 | [diff] [blame] | 2586 | CloseConnection(QUIC_HTTP_INVALID_FRAME_SEQUENCE_ON_SPDY_STREAM, |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2587 | "Unexpected DATA frame received.", |
| 2588 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET)) |
| 2589 | .WillOnce(InvokeWithoutArgs([this]() { stream_->StopReading(); })); |
| 2590 | |
| 2591 | std::string data = DataFrame(kDataFramePayload); |
| 2592 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, 0, data)); |
| 2593 | } |
| 2594 | |
| 2595 | // Close connection if a HEADERS frame is received after the trailing HEADERS. |
| 2596 | TEST_P(QuicSpdyStreamTest, TrailersAfterTrailers) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2597 | if (!UsesHttp3()) { |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2598 | return; |
| 2599 | } |
| 2600 | |
| 2601 | Initialize(kShouldProcessData); |
| 2602 | |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2603 | // Receive and consume headers. |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2604 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2605 | QuicStreamOffset offset = 0; |
| 2606 | stream_->OnStreamFrame( |
| 2607 | QuicStreamFrame(stream_->id(), false, offset, headers)); |
| 2608 | offset += headers.size(); |
| 2609 | |
| 2610 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2611 | stream_->ConsumeHeaderList(); |
| 2612 | |
| 2613 | // Receive data. It is consumed by TestStream. |
| 2614 | std::string data = DataFrame(kDataFramePayload); |
| 2615 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, offset, data)); |
| 2616 | offset += data.size(); |
| 2617 | |
| 2618 | EXPECT_EQ(kDataFramePayload, stream_->data()); |
| 2619 | |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2620 | // Receive and consume trailers. |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2621 | std::string trailers1 = |
| 2622 | HeadersFrame({std::make_pair("custom-key", "custom-value")}); |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2623 | stream_->OnStreamFrame( |
| 2624 | QuicStreamFrame(stream_->id(), false, offset, trailers1)); |
| 2625 | offset += trailers1.size(); |
| 2626 | |
| 2627 | EXPECT_TRUE(stream_->trailers_decompressed()); |
| 2628 | EXPECT_THAT(stream_->received_trailers(), |
| 2629 | ElementsAre(Pair("custom-key", "custom-value"))); |
| 2630 | |
| 2631 | // Closing the connection is mocked out in tests. Instead, simply stop |
| 2632 | // reading data at the stream level to prevent QuicSpdyStream from blowing up. |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2633 | EXPECT_CALL( |
| 2634 | *connection_, |
bnc | 3e8fe7c | 2020-03-10 17:44:58 -0700 | [diff] [blame] | 2635 | CloseConnection(QUIC_HTTP_INVALID_FRAME_SEQUENCE_ON_SPDY_STREAM, |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2636 | "HEADERS frame received after trailing HEADERS.", |
| 2637 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET)) |
| 2638 | .WillOnce(InvokeWithoutArgs([this]() { stream_->StopReading(); })); |
| 2639 | |
| 2640 | // Receive another HEADERS frame, with no header fields. |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2641 | std::string trailers2 = HeadersFrame(SpdyHeaderBlock()); |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2642 | stream_->OnStreamFrame( |
| 2643 | QuicStreamFrame(stream_->id(), false, offset, trailers2)); |
| 2644 | } |
| 2645 | |
| 2646 | // Regression test for https://crbug.com/978733. |
| 2647 | // Close connection if a DATA frame is received after the trailing HEADERS. |
| 2648 | TEST_P(QuicSpdyStreamTest, DataAfterTrailers) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2649 | if (!UsesHttp3()) { |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2650 | return; |
| 2651 | } |
| 2652 | |
| 2653 | Initialize(kShouldProcessData); |
| 2654 | |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2655 | // Receive and consume headers. |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2656 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2657 | QuicStreamOffset offset = 0; |
| 2658 | stream_->OnStreamFrame( |
| 2659 | QuicStreamFrame(stream_->id(), false, offset, headers)); |
| 2660 | offset += headers.size(); |
| 2661 | |
| 2662 | EXPECT_THAT(stream_->header_list(), ElementsAre(Pair("foo", "bar"))); |
| 2663 | stream_->ConsumeHeaderList(); |
| 2664 | |
| 2665 | // Receive data. It is consumed by TestStream. |
| 2666 | std::string data1 = DataFrame(kDataFramePayload); |
| 2667 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, offset, data1)); |
| 2668 | offset += data1.size(); |
| 2669 | EXPECT_EQ(kDataFramePayload, stream_->data()); |
| 2670 | |
| 2671 | // Receive trailers, with single header field "custom-key: custom-value". |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2672 | std::string trailers = |
| 2673 | HeadersFrame({std::make_pair("custom-key", "custom-value")}); |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2674 | stream_->OnStreamFrame( |
| 2675 | QuicStreamFrame(stream_->id(), false, offset, trailers)); |
| 2676 | offset += trailers.size(); |
| 2677 | |
| 2678 | EXPECT_THAT(stream_->received_trailers(), |
| 2679 | ElementsAre(Pair("custom-key", "custom-value"))); |
| 2680 | |
| 2681 | // Closing the connection is mocked out in tests. Instead, simply stop |
| 2682 | // reading data at the stream level to prevent QuicSpdyStream from blowing up. |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2683 | EXPECT_CALL( |
| 2684 | *connection_, |
bnc | 3e8fe7c | 2020-03-10 17:44:58 -0700 | [diff] [blame] | 2685 | CloseConnection(QUIC_HTTP_INVALID_FRAME_SEQUENCE_ON_SPDY_STREAM, |
bnc | 519216c | 2019-07-09 05:03:48 -0700 | [diff] [blame] | 2686 | "Unexpected DATA frame received.", |
| 2687 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET)) |
| 2688 | .WillOnce(InvokeWithoutArgs([this]() { stream_->StopReading(); })); |
| 2689 | |
| 2690 | // Receive more data. |
| 2691 | std::string data2 = DataFrame("This payload should not be proccessed."); |
| 2692 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), false, offset, data2)); |
| 2693 | } |
| 2694 | |
bnc | ef51815 | 2019-07-18 05:36:44 -0700 | [diff] [blame] | 2695 | // SETTINGS frames are invalid on bidirectional streams. If one is received, |
| 2696 | // the connection is closed. No more data should be processed. |
| 2697 | TEST_P(QuicSpdyStreamTest, StopProcessingIfConnectionClosed) { |
renjietang | e47e15f | 2019-10-29 14:19:30 -0700 | [diff] [blame] | 2698 | if (!UsesHttp3()) { |
bnc | ef51815 | 2019-07-18 05:36:44 -0700 | [diff] [blame] | 2699 | return; |
| 2700 | } |
| 2701 | |
| 2702 | Initialize(kShouldProcessData); |
| 2703 | |
| 2704 | // SETTINGS frame with empty payload. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2705 | std::string settings = quiche::QuicheTextUtils::HexDecode("0400"); |
bnc | 2e963aa | 2019-07-31 18:03:17 -0700 | [diff] [blame] | 2706 | |
| 2707 | // HEADERS frame. |
bnc | ef51815 | 2019-07-18 05:36:44 -0700 | [diff] [blame] | 2708 | // Since it arrives after a SETTINGS frame, it should never be read. |
fayang | 7e3c1a1 | 2019-08-02 08:07:13 -0700 | [diff] [blame] | 2709 | std::string headers = HeadersFrame({std::make_pair("foo", "bar")}); |
bnc | ef51815 | 2019-07-18 05:36:44 -0700 | [diff] [blame] | 2710 | |
| 2711 | // Combine the two frames to make sure they are processed in a single |
| 2712 | // QuicSpdyStream::OnDataAvailable() call. |
dmcardle | ba2fb7e | 2019-12-13 07:44:34 -0800 | [diff] [blame] | 2713 | std::string frames = quiche::QuicheStrCat(settings, headers); |
bnc | ef51815 | 2019-07-18 05:36:44 -0700 | [diff] [blame] | 2714 | |
| 2715 | EXPECT_EQ(0u, stream_->sequencer()->NumBytesConsumed()); |
| 2716 | |
bnc | dfabdfb | 2020-01-17 17:46:40 -0800 | [diff] [blame] | 2717 | EXPECT_CALL(*connection_, |
| 2718 | CloseConnection(QUIC_HTTP_FRAME_UNEXPECTED_ON_SPDY_STREAM, _, _)) |
bnc | ef51815 | 2019-07-18 05:36:44 -0700 | [diff] [blame] | 2719 | .WillOnce( |
| 2720 | Invoke(connection_, &MockQuicConnection::ReallyCloseConnection)); |
| 2721 | EXPECT_CALL(*connection_, SendConnectionClosePacket(_, _)); |
| 2722 | EXPECT_CALL(*session_, OnConnectionClosed(_, _)); |
| 2723 | |
| 2724 | stream_->OnStreamFrame(QuicStreamFrame(stream_->id(), /* fin = */ false, |
| 2725 | /* offset = */ 0, frames)); |
| 2726 | |
| 2727 | EXPECT_EQ(0u, stream_->sequencer()->NumBytesConsumed()); |
| 2728 | } |
| 2729 | |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 2730 | // Stream Cancellation instruction is sent on QPACK decoder stream |
| 2731 | // when stream is reset. |
bnc | 84aa7e5 | 2019-12-13 05:25:21 -0800 | [diff] [blame] | 2732 | TEST_P(QuicSpdyStreamTest, StreamCancellationWhenStreamReset) { |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 2733 | if (!UsesHttp3()) { |
| 2734 | return; |
| 2735 | } |
| 2736 | |
| 2737 | Initialize(kShouldProcessData); |
| 2738 | |
| 2739 | auto qpack_decoder_stream = |
| 2740 | QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2741 | EXPECT_CALL(*session_, WritevData(qpack_decoder_stream->id(), 1, 1, _, _, _)); |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 2742 | EXPECT_CALL(*session_, |
| 2743 | SendRstStream(stream_->id(), QUIC_STREAM_CANCELLED, 0)); |
| 2744 | |
| 2745 | stream_->Reset(QUIC_STREAM_CANCELLED); |
| 2746 | } |
| 2747 | |
| 2748 | // Stream Cancellation instruction is sent on QPACK decoder stream |
| 2749 | // when RESET_STREAM frame is received. |
bnc | 84aa7e5 | 2019-12-13 05:25:21 -0800 | [diff] [blame] | 2750 | TEST_P(QuicSpdyStreamTest, StreamCancellationOnResetReceived) { |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 2751 | if (!UsesHttp3()) { |
| 2752 | return; |
| 2753 | } |
| 2754 | |
| 2755 | Initialize(kShouldProcessData); |
| 2756 | |
| 2757 | auto qpack_decoder_stream = |
| 2758 | QuicSpdySessionPeer::GetQpackDecoderSendStream(session_.get()); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 2759 | EXPECT_CALL(*session_, WritevData(qpack_decoder_stream->id(), 1, 1, _, _, _)); |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 2760 | |
| 2761 | stream_->OnStreamReset(QuicRstStreamFrame( |
| 2762 | kInvalidControlFrameId, stream_->id(), QUIC_STREAM_CANCELLED, 0)); |
| 2763 | } |
| 2764 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2765 | } // namespace |
| 2766 | } // namespace test |
| 2767 | } // namespace quic |