QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 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/tools/quic_simple_server_stream.h" |
| 6 | |
| 7 | #include <list> |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/http/http_encoder.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/http/spdy_utils.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 18 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
renjietang | bb2e22a | 2019-09-12 15:46:39 -0700 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/test_tools/quic_session_peer.h" |
| 21 | #include "net/third_party/quiche/src/quic/test_tools/quic_spdy_session_peer.h" |
| 22 | #include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h" |
| 23 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 24 | #include "net/third_party/quiche/src/quic/tools/quic_backend_response.h" |
| 25 | #include "net/third_party/quiche/src/quic/tools/quic_memory_cache_backend.h" |
| 26 | #include "net/third_party/quiche/src/quic/tools/quic_simple_server_session.h" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 27 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 28 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | |
| 30 | using testing::_; |
| 31 | using testing::AnyNumber; |
| 32 | using testing::InSequence; |
| 33 | using testing::Invoke; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 34 | using testing::StrictMock; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | |
| 36 | namespace quic { |
| 37 | namespace test { |
| 38 | |
| 39 | const size_t kFakeFrameLen = 60; |
| 40 | const size_t kErrorLength = strlen(QuicSimpleServerStream::kErrorResponseBody); |
| 41 | const size_t kDataFrameHeaderLength = 2; |
| 42 | |
| 43 | class TestStream : public QuicSimpleServerStream { |
| 44 | public: |
| 45 | TestStream(QuicStreamId stream_id, |
| 46 | QuicSpdySession* session, |
| 47 | StreamType type, |
| 48 | QuicSimpleServerBackend* quic_simple_server_backend) |
| 49 | : QuicSimpleServerStream(stream_id, |
| 50 | session, |
| 51 | type, |
| 52 | quic_simple_server_backend) {} |
| 53 | |
| 54 | ~TestStream() override = default; |
| 55 | |
| 56 | MOCK_METHOD1(WriteHeadersMock, void(bool fin)); |
| 57 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 58 | size_t WriteHeaders(spdy::SpdyHeaderBlock /*header_block*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 59 | bool fin, |
| 60 | QuicReferenceCountedPointer<QuicAckListenerInterface> |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 61 | /*ack_listener*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | WriteHeadersMock(fin); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | // Expose protected QuicSimpleServerStream methods. |
| 67 | void DoSendResponse() { SendResponse(); } |
| 68 | void DoSendErrorResponse() { SendErrorResponse(); } |
| 69 | |
| 70 | spdy::SpdyHeaderBlock* mutable_headers() { return &request_headers_; } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 71 | void set_body(std::string body) { body_ = std::move(body); } |
| 72 | const std::string& body() const { return body_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | int content_length() const { return content_length_; } |
| 74 | |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 75 | quiche::QuicheStringPiece GetHeader(quiche::QuicheStringPiece key) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | auto it = request_headers_.find(key); |
| 77 | DCHECK(it != request_headers_.end()); |
| 78 | return it->second; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | namespace { |
| 83 | |
| 84 | class MockQuicSimpleServerSession : public QuicSimpleServerSession { |
| 85 | public: |
| 86 | const size_t kMaxStreamsForTest = 100; |
| 87 | |
| 88 | MockQuicSimpleServerSession( |
| 89 | QuicConnection* connection, |
| 90 | MockQuicSessionVisitor* owner, |
| 91 | MockQuicCryptoServerStreamHelper* helper, |
| 92 | QuicCryptoServerConfig* crypto_config, |
| 93 | QuicCompressedCertsCache* compressed_certs_cache, |
| 94 | QuicSimpleServerBackend* quic_simple_server_backend) |
| 95 | : QuicSimpleServerSession(DefaultQuicConfig(), |
| 96 | CurrentSupportedVersions(), |
| 97 | connection, |
| 98 | owner, |
| 99 | helper, |
| 100 | crypto_config, |
| 101 | compressed_certs_cache, |
| 102 | quic_simple_server_backend) { |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 103 | if (VersionHasIetfQuicFrames(connection->transport_version())) { |
fkastenholz | d3a1de9 | 2019-05-15 07:00:07 -0700 | [diff] [blame] | 104 | QuicSessionPeer::SetMaxOpenIncomingUnidirectionalStreams( |
| 105 | this, kMaxStreamsForTest); |
| 106 | QuicSessionPeer::SetMaxOpenIncomingBidirectionalStreams( |
| 107 | this, kMaxStreamsForTest); |
| 108 | } else { |
| 109 | QuicSessionPeer::SetMaxOpenIncomingStreams(this, kMaxStreamsForTest); |
| 110 | QuicSessionPeer::SetMaxOpenOutgoingStreams(this, kMaxStreamsForTest); |
| 111 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | ON_CALL(*this, WritevData(_, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 113 | .WillByDefault(Invoke(this, &MockQuicSimpleServerSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | MockQuicSimpleServerSession(const MockQuicSimpleServerSession&) = delete; |
| 117 | MockQuicSimpleServerSession& operator=(const MockQuicSimpleServerSession&) = |
| 118 | delete; |
| 119 | ~MockQuicSimpleServerSession() override = default; |
| 120 | |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 121 | MOCK_METHOD2(OnConnectionClosed, |
| 122 | void(const QuicConnectionCloseFrame& frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 123 | ConnectionCloseSource source)); |
| 124 | MOCK_METHOD1(CreateIncomingStream, QuicSpdyStream*(QuicStreamId id)); |
| 125 | MOCK_METHOD5(WritevData, |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 126 | QuicConsumedData(QuicStreamId id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 127 | size_t write_length, |
| 128 | QuicStreamOffset offset, |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 129 | StreamSendingState state, |
| 130 | bool is_retransmission)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 131 | MOCK_METHOD4(OnStreamHeaderList, |
| 132 | void(QuicStreamId stream_id, |
| 133 | bool fin, |
| 134 | size_t frame_len, |
| 135 | const QuicHeaderList& header_list)); |
| 136 | MOCK_METHOD2(OnStreamHeadersPriority, |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 137 | void(QuicStreamId stream_id, |
| 138 | const spdy::SpdyStreamPrecedence& precedence)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | MOCK_METHOD3(SendRstStream, |
| 140 | void(QuicStreamId stream_id, |
| 141 | QuicRstStreamErrorCode error, |
| 142 | QuicStreamOffset bytes_written)); |
| 143 | MOCK_METHOD1(OnHeadersHeadOfLineBlocking, void(QuicTime::Delta delta)); |
| 144 | // Matchers cannot be used on non-copyable types like SpdyHeaderBlock. |
| 145 | void PromisePushResources( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 146 | const std::string& request_url, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | const std::list<QuicBackendResponse::ServerPushInfo>& resources, |
| 148 | QuicStreamId original_stream_id, |
fayang | 9a42376 | 2019-07-31 08:12:58 -0700 | [diff] [blame] | 149 | const spdy::SpdyStreamPrecedence& original_precedence, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 150 | const spdy::SpdyHeaderBlock& original_request_headers) override { |
| 151 | original_request_headers_ = original_request_headers.Clone(); |
| 152 | PromisePushResourcesMock(request_url, resources, original_stream_id, |
fayang | 9a42376 | 2019-07-31 08:12:58 -0700 | [diff] [blame] | 153 | original_precedence, original_request_headers); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | } |
fayang | 9a42376 | 2019-07-31 08:12:58 -0700 | [diff] [blame] | 155 | MOCK_METHOD5(PromisePushResourcesMock, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 156 | void(const std::string&, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 157 | const std::list<QuicBackendResponse::ServerPushInfo>&, |
| 158 | QuicStreamId, |
fayang | 9a42376 | 2019-07-31 08:12:58 -0700 | [diff] [blame] | 159 | const spdy::SpdyStreamPrecedence&, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 160 | const spdy::SpdyHeaderBlock&)); |
| 161 | |
| 162 | using QuicSession::ActivateStream; |
| 163 | |
| 164 | MOCK_METHOD1(OnStopSendingReceived, void(const QuicStopSendingFrame& frame)); |
| 165 | |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 166 | QuicConsumedData ConsumeData(QuicStreamId id, |
| 167 | size_t write_length, |
| 168 | QuicStreamOffset offset, |
| 169 | StreamSendingState state, |
| 170 | bool /*is_retransmission*/) { |
| 171 | if (write_length > 0) { |
| 172 | auto buf = std::make_unique<char[]>(write_length); |
| 173 | QuicStream* stream = GetOrCreateStream(id); |
| 174 | DCHECK(stream); |
| 175 | QuicDataWriter writer(write_length, buf.get(), quiche::HOST_BYTE_ORDER); |
| 176 | stream->WriteStreamData(offset, write_length, &writer); |
| 177 | } else { |
| 178 | DCHECK(state != NO_FIN); |
| 179 | } |
| 180 | return QuicConsumedData(write_length, state != NO_FIN); |
| 181 | } |
| 182 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | spdy::SpdyHeaderBlock original_request_headers_; |
| 184 | }; |
| 185 | |
| 186 | class QuicSimpleServerStreamTest : public QuicTestWithParam<ParsedQuicVersion> { |
| 187 | public: |
| 188 | QuicSimpleServerStreamTest() |
| 189 | : connection_( |
| 190 | new StrictMock<MockQuicConnection>(&helper_, |
| 191 | &alarm_factory_, |
| 192 | Perspective::IS_SERVER, |
| 193 | SupportedVersions(GetParam()))), |
| 194 | crypto_config_(new QuicCryptoServerConfig( |
| 195 | QuicCryptoServerConfig::TESTING, |
| 196 | QuicRandom::GetInstance(), |
| 197 | crypto_test_utils::ProofSourceForTesting(), |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 198 | KeyExchangeSource::Default())), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 199 | compressed_certs_cache_( |
| 200 | QuicCompressedCertsCache::kQuicCompressedCertsCacheSize), |
| 201 | session_(connection_, |
| 202 | &session_owner_, |
| 203 | &session_helper_, |
| 204 | crypto_config_.get(), |
| 205 | &compressed_certs_cache_, |
| 206 | &memory_cache_backend_), |
| 207 | quic_response_(new QuicBackendResponse), |
| 208 | body_("hello world") { |
| 209 | connection_->set_visitor(&session_); |
| 210 | header_list_.OnHeaderBlockStart(); |
| 211 | header_list_.OnHeader(":authority", "www.google.com"); |
| 212 | header_list_.OnHeader(":path", "/"); |
| 213 | header_list_.OnHeader(":method", "POST"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 214 | header_list_.OnHeader("content-length", "11"); |
| 215 | |
| 216 | header_list_.OnHeaderBlockEnd(128, 128); |
| 217 | |
| 218 | // New streams rely on having the peer's flow control receive window |
| 219 | // negotiated in the config. |
| 220 | session_.config()->SetInitialStreamFlowControlWindowToSend( |
| 221 | kInitialStreamFlowControlWindowForTest); |
| 222 | session_.config()->SetInitialSessionFlowControlWindowToSend( |
| 223 | kInitialSessionFlowControlWindowForTest); |
renjietang | 8513ffe | 2019-08-12 10:08:57 -0700 | [diff] [blame] | 224 | session_.Initialize(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 225 | stream_ = new StrictMock<TestStream>( |
| 226 | GetNthClientInitiatedBidirectionalStreamId( |
| 227 | connection_->transport_version(), 0), |
| 228 | &session_, BIDIRECTIONAL, &memory_cache_backend_); |
| 229 | // Register stream_ in dynamic_stream_map_ and pass ownership to session_. |
| 230 | session_.ActivateStream(QuicWrapUnique(stream_)); |
renjietang | bb2e22a | 2019-09-12 15:46:39 -0700 | [diff] [blame] | 231 | QuicConfigPeer::SetReceivedInitialSessionFlowControlWindow( |
| 232 | session_.config(), kMinimumFlowControlSendWindow); |
dschinazi | 18cdf13 | 2019-10-09 16:08:18 -0700 | [diff] [blame] | 233 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesUnidirectional( |
| 234 | session_.config(), kMinimumFlowControlSendWindow); |
| 235 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesIncomingBidirectional( |
| 236 | session_.config(), kMinimumFlowControlSendWindow); |
| 237 | QuicConfigPeer::SetReceivedInitialMaxStreamDataBytesOutgoingBidirectional( |
| 238 | session_.config(), kMinimumFlowControlSendWindow); |
renjietang | e6d9467 | 2020-01-07 10:30:10 -0800 | [diff] [blame] | 239 | QuicConfigPeer::SetReceivedMaxUnidirectionalStreams(session_.config(), 10); |
renjietang | bb2e22a | 2019-09-12 15:46:39 -0700 | [diff] [blame] | 240 | session_.OnConfigNegotiated(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 241 | connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 242 | } |
| 243 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 244 | const std::string& StreamBody() { return stream_->body(); } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 245 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 246 | std::string StreamHeadersValue(const std::string& key) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 247 | return (*stream_->mutable_headers())[key].as_string(); |
| 248 | } |
| 249 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 250 | bool UsesHttp3() const { |
| 251 | return VersionUsesHttp3(connection_->transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | spdy::SpdyHeaderBlock response_headers_; |
| 255 | MockQuicConnectionHelper helper_; |
| 256 | MockAlarmFactory alarm_factory_; |
| 257 | StrictMock<MockQuicConnection>* connection_; |
| 258 | StrictMock<MockQuicSessionVisitor> session_owner_; |
| 259 | StrictMock<MockQuicCryptoServerStreamHelper> session_helper_; |
| 260 | std::unique_ptr<QuicCryptoServerConfig> crypto_config_; |
| 261 | QuicCompressedCertsCache compressed_certs_cache_; |
| 262 | QuicMemoryCacheBackend memory_cache_backend_; |
| 263 | StrictMock<MockQuicSimpleServerSession> session_; |
| 264 | StrictMock<TestStream>* stream_; // Owned by session_. |
| 265 | std::unique_ptr<QuicBackendResponse> quic_response_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 266 | std::string body_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 267 | QuicHeaderList header_list_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | INSTANTIATE_TEST_SUITE_P(Tests, |
| 271 | QuicSimpleServerStreamTest, |
dschinazi | 88bd5b0 | 2019-10-10 00:52:20 -0700 | [diff] [blame] | 272 | ::testing::ValuesIn(AllSupportedVersions()), |
| 273 | ::testing::PrintToStringParamName()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 274 | |
| 275 | TEST_P(QuicSimpleServerStreamTest, TestFraming) { |
| 276 | EXPECT_CALL(session_, WritevData(_, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 277 | .WillRepeatedly( |
| 278 | Invoke(&session_, &MockQuicSimpleServerSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 279 | stream_->OnStreamHeaderList(false, kFakeFrameLen, header_list_); |
| 280 | std::unique_ptr<char[]> buffer; |
| 281 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 282 | HttpEncoder::SerializeDataFrameHeader(body_.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 283 | std::string header = std::string(buffer.get(), header_length); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 284 | std::string data = UsesHttp3() ? header + body_ : body_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 285 | stream_->OnStreamFrame( |
| 286 | QuicStreamFrame(stream_->id(), /*fin=*/false, /*offset=*/0, data)); |
| 287 | EXPECT_EQ("11", StreamHeadersValue("content-length")); |
| 288 | EXPECT_EQ("/", StreamHeadersValue(":path")); |
| 289 | EXPECT_EQ("POST", StreamHeadersValue(":method")); |
| 290 | EXPECT_EQ(body_, StreamBody()); |
| 291 | } |
| 292 | |
| 293 | TEST_P(QuicSimpleServerStreamTest, TestFramingOnePacket) { |
| 294 | EXPECT_CALL(session_, WritevData(_, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 295 | .WillRepeatedly( |
| 296 | Invoke(&session_, &MockQuicSimpleServerSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 297 | |
| 298 | stream_->OnStreamHeaderList(false, kFakeFrameLen, header_list_); |
| 299 | std::unique_ptr<char[]> buffer; |
| 300 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 301 | HttpEncoder::SerializeDataFrameHeader(body_.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 302 | std::string header = std::string(buffer.get(), header_length); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 303 | std::string data = UsesHttp3() ? header + body_ : body_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 304 | stream_->OnStreamFrame( |
| 305 | QuicStreamFrame(stream_->id(), /*fin=*/false, /*offset=*/0, data)); |
| 306 | EXPECT_EQ("11", StreamHeadersValue("content-length")); |
| 307 | EXPECT_EQ("/", StreamHeadersValue(":path")); |
| 308 | EXPECT_EQ("POST", StreamHeadersValue(":method")); |
| 309 | EXPECT_EQ(body_, StreamBody()); |
| 310 | } |
| 311 | |
| 312 | TEST_P(QuicSimpleServerStreamTest, SendQuicRstStreamNoErrorInStopReading) { |
| 313 | EXPECT_CALL(session_, WritevData(_, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 314 | .WillRepeatedly( |
| 315 | Invoke(&session_, &MockQuicSimpleServerSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 316 | |
| 317 | EXPECT_FALSE(stream_->fin_received()); |
| 318 | EXPECT_FALSE(stream_->rst_received()); |
| 319 | |
| 320 | stream_->set_fin_sent(true); |
| 321 | stream_->CloseWriteSide(); |
| 322 | |
| 323 | EXPECT_CALL(session_, SendRstStream(_, QUIC_STREAM_NO_ERROR, _)).Times(1); |
| 324 | stream_->StopReading(); |
| 325 | } |
| 326 | |
| 327 | TEST_P(QuicSimpleServerStreamTest, TestFramingExtraData) { |
| 328 | InSequence seq; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 329 | std::string large_body = "hello world!!!!!!"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 330 | |
| 331 | // We'll automatically write out an error (headers + body) |
| 332 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 333 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 334 | EXPECT_CALL(session_, WritevData(_, kDataFrameHeaderLength, _, NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 335 | } |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 336 | EXPECT_CALL(session_, WritevData(_, kErrorLength, _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 337 | |
| 338 | EXPECT_CALL(session_, SendRstStream(_, QUIC_STREAM_NO_ERROR, _)).Times(0); |
| 339 | |
| 340 | stream_->OnStreamHeaderList(false, kFakeFrameLen, header_list_); |
| 341 | std::unique_ptr<char[]> buffer; |
| 342 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 343 | HttpEncoder::SerializeDataFrameHeader(body_.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 344 | std::string header = std::string(buffer.get(), header_length); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 345 | std::string data = UsesHttp3() ? header + body_ : body_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 346 | |
| 347 | stream_->OnStreamFrame( |
| 348 | QuicStreamFrame(stream_->id(), /*fin=*/false, /*offset=*/0, data)); |
| 349 | // Content length is still 11. This will register as an error and we won't |
| 350 | // accept the bytes. |
| 351 | header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 352 | HttpEncoder::SerializeDataFrameHeader(large_body.length(), &buffer); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 353 | header = std::string(buffer.get(), header_length); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 354 | std::string data2 = UsesHttp3() ? header + large_body : large_body; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 355 | stream_->OnStreamFrame( |
| 356 | QuicStreamFrame(stream_->id(), /*fin=*/true, data.size(), data2)); |
| 357 | EXPECT_EQ("11", StreamHeadersValue("content-length")); |
| 358 | EXPECT_EQ("/", StreamHeadersValue(":path")); |
| 359 | EXPECT_EQ("POST", StreamHeadersValue(":method")); |
| 360 | } |
| 361 | |
| 362 | TEST_P(QuicSimpleServerStreamTest, SendResponseWithIllegalResponseStatus) { |
| 363 | // Send an illegal response with response status not supported by HTTP/2. |
| 364 | spdy::SpdyHeaderBlock* request_headers = stream_->mutable_headers(); |
| 365 | (*request_headers)[":path"] = "/bar"; |
| 366 | (*request_headers)[":authority"] = "www.google.com"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 367 | (*request_headers)[":method"] = "GET"; |
| 368 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 369 | // HTTP/2 only supports integer responsecode, so "200 OK" is illegal. |
| 370 | response_headers_[":status"] = "200 OK"; |
| 371 | response_headers_["content-length"] = "5"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 372 | std::string body = "Yummm"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 373 | std::unique_ptr<char[]> buffer; |
| 374 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 375 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 376 | |
| 377 | memory_cache_backend_.AddResponse("www.google.com", "/bar", |
| 378 | std::move(response_headers_), body); |
| 379 | |
| 380 | stream_->set_fin_received(true); |
| 381 | |
| 382 | InSequence s; |
| 383 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 384 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 385 | EXPECT_CALL(session_, WritevData(_, header_length, _, NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 386 | } |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 387 | EXPECT_CALL(session_, WritevData(_, kErrorLength, _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 388 | |
| 389 | stream_->DoSendResponse(); |
| 390 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 391 | EXPECT_TRUE(stream_->write_side_closed()); |
| 392 | } |
| 393 | |
| 394 | TEST_P(QuicSimpleServerStreamTest, SendResponseWithIllegalResponseStatus2) { |
| 395 | // Send an illegal response with response status not supported by HTTP/2. |
| 396 | spdy::SpdyHeaderBlock* request_headers = stream_->mutable_headers(); |
| 397 | (*request_headers)[":path"] = "/bar"; |
| 398 | (*request_headers)[":authority"] = "www.google.com"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 399 | (*request_headers)[":method"] = "GET"; |
| 400 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 401 | // HTTP/2 only supports 3-digit-integer, so "+200" is illegal. |
| 402 | response_headers_[":status"] = "+200"; |
| 403 | response_headers_["content-length"] = "5"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 404 | std::string body = "Yummm"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 405 | |
| 406 | std::unique_ptr<char[]> buffer; |
| 407 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 408 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 409 | |
| 410 | memory_cache_backend_.AddResponse("www.google.com", "/bar", |
| 411 | std::move(response_headers_), body); |
| 412 | |
| 413 | stream_->set_fin_received(true); |
| 414 | |
| 415 | InSequence s; |
| 416 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 417 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 418 | EXPECT_CALL(session_, WritevData(_, header_length, _, NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 419 | } |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 420 | EXPECT_CALL(session_, WritevData(_, kErrorLength, _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 421 | |
| 422 | stream_->DoSendResponse(); |
| 423 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 424 | EXPECT_TRUE(stream_->write_side_closed()); |
| 425 | } |
| 426 | |
| 427 | TEST_P(QuicSimpleServerStreamTest, SendPushResponseWith404Response) { |
| 428 | // Create a new promised stream with even id(). |
| 429 | auto promised_stream = new StrictMock<TestStream>( |
| 430 | GetNthServerInitiatedUnidirectionalStreamId( |
renjietang | 87cd7de | 2019-08-16 08:35:10 -0700 | [diff] [blame] | 431 | connection_->transport_version(), 3), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 432 | &session_, WRITE_UNIDIRECTIONAL, &memory_cache_backend_); |
| 433 | session_.ActivateStream(QuicWrapUnique(promised_stream)); |
| 434 | |
| 435 | // Send a push response with response status 404, which will be regarded as |
| 436 | // invalid server push response. |
| 437 | spdy::SpdyHeaderBlock* request_headers = promised_stream->mutable_headers(); |
| 438 | (*request_headers)[":path"] = "/bar"; |
| 439 | (*request_headers)[":authority"] = "www.google.com"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 440 | (*request_headers)[":method"] = "GET"; |
| 441 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 442 | response_headers_[":status"] = "404"; |
| 443 | response_headers_["content-length"] = "8"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 444 | std::string body = "NotFound"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 445 | |
| 446 | memory_cache_backend_.AddResponse("www.google.com", "/bar", |
| 447 | std::move(response_headers_), body); |
| 448 | |
| 449 | InSequence s; |
| 450 | EXPECT_CALL(session_, |
| 451 | SendRstStream(promised_stream->id(), QUIC_STREAM_CANCELLED, 0)); |
| 452 | |
| 453 | promised_stream->DoSendResponse(); |
| 454 | } |
| 455 | |
| 456 | TEST_P(QuicSimpleServerStreamTest, SendResponseWithValidHeaders) { |
| 457 | // Add a request and response with valid headers. |
| 458 | spdy::SpdyHeaderBlock* request_headers = stream_->mutable_headers(); |
| 459 | (*request_headers)[":path"] = "/bar"; |
| 460 | (*request_headers)[":authority"] = "www.google.com"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 461 | (*request_headers)[":method"] = "GET"; |
| 462 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 463 | response_headers_[":status"] = "200"; |
| 464 | response_headers_["content-length"] = "5"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 465 | std::string body = "Yummm"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | |
| 467 | std::unique_ptr<char[]> buffer; |
| 468 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 469 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 470 | |
| 471 | memory_cache_backend_.AddResponse("www.google.com", "/bar", |
| 472 | std::move(response_headers_), body); |
| 473 | stream_->set_fin_received(true); |
| 474 | |
| 475 | InSequence s; |
| 476 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 477 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 478 | EXPECT_CALL(session_, WritevData(_, header_length, _, NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 479 | } |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 480 | EXPECT_CALL(session_, WritevData(_, body.length(), _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 481 | |
| 482 | stream_->DoSendResponse(); |
| 483 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 484 | EXPECT_TRUE(stream_->write_side_closed()); |
| 485 | } |
| 486 | |
| 487 | TEST_P(QuicSimpleServerStreamTest, SendResponseWithPushResources) { |
| 488 | // Tests that if a response has push resources to be send, SendResponse() will |
| 489 | // call PromisePushResources() to handle these resources. |
| 490 | |
| 491 | // Add a request and response with valid headers into cache. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 492 | std::string host = "www.google.com"; |
| 493 | std::string request_path = "/foo"; |
| 494 | std::string body = "Yummm"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 495 | std::unique_ptr<char[]> buffer; |
| 496 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 497 | HttpEncoder::SerializeDataFrameHeader(body.length(), &buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 498 | QuicBackendResponse::ServerPushInfo push_info( |
| 499 | QuicUrl(host, "/bar"), spdy::SpdyHeaderBlock(), |
| 500 | QuicStream::kDefaultPriority, "Push body"); |
| 501 | std::list<QuicBackendResponse::ServerPushInfo> push_resources; |
| 502 | push_resources.push_back(push_info); |
| 503 | memory_cache_backend_.AddSimpleResponseWithServerPushResources( |
| 504 | host, request_path, 200, body, push_resources); |
| 505 | |
| 506 | spdy::SpdyHeaderBlock* request_headers = stream_->mutable_headers(); |
| 507 | (*request_headers)[":path"] = request_path; |
| 508 | (*request_headers)[":authority"] = host; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 509 | (*request_headers)[":method"] = "GET"; |
| 510 | |
| 511 | stream_->set_fin_received(true); |
| 512 | InSequence s; |
| 513 | EXPECT_CALL(session_, PromisePushResourcesMock( |
| 514 | host + request_path, _, |
| 515 | GetNthClientInitiatedBidirectionalStreamId( |
| 516 | connection_->transport_version(), 0), |
fayang | 9a42376 | 2019-07-31 08:12:58 -0700 | [diff] [blame] | 517 | _, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 518 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 519 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 520 | EXPECT_CALL(session_, WritevData(_, header_length, _, NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 521 | } |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 522 | EXPECT_CALL(session_, WritevData(_, body.length(), _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 523 | stream_->DoSendResponse(); |
| 524 | EXPECT_EQ(*request_headers, session_.original_request_headers_); |
| 525 | } |
| 526 | |
| 527 | TEST_P(QuicSimpleServerStreamTest, PushResponseOnClientInitiatedStream) { |
| 528 | // EXPECT_QUIC_BUG tests are expensive so only run one instance of them. |
| 529 | if (GetParam() != AllSupportedVersions()[0]) { |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | // Calling PushResponse() on a client initialted stream is never supposed to |
| 534 | // happen. |
| 535 | EXPECT_QUIC_BUG(stream_->PushResponse(spdy::SpdyHeaderBlock()), |
| 536 | "Client initiated stream" |
| 537 | " shouldn't be used as promised stream."); |
| 538 | } |
| 539 | |
| 540 | TEST_P(QuicSimpleServerStreamTest, PushResponseOnServerInitiatedStream) { |
| 541 | // Tests that PushResponse() should take the given headers as request headers |
| 542 | // and fetch response from cache, and send it out. |
| 543 | |
| 544 | // Create a stream with even stream id and test against this stream. |
| 545 | const QuicStreamId kServerInitiatedStreamId = |
| 546 | GetNthServerInitiatedUnidirectionalStreamId( |
renjietang | 87cd7de | 2019-08-16 08:35:10 -0700 | [diff] [blame] | 547 | connection_->transport_version(), 3); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 548 | // Create a server initiated stream and pass it to session_. |
| 549 | auto server_initiated_stream = |
| 550 | new StrictMock<TestStream>(kServerInitiatedStreamId, &session_, |
| 551 | WRITE_UNIDIRECTIONAL, &memory_cache_backend_); |
| 552 | session_.ActivateStream(QuicWrapUnique(server_initiated_stream)); |
| 553 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 554 | const std::string kHost = "www.foo.com"; |
| 555 | const std::string kPath = "/bar"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 556 | spdy::SpdyHeaderBlock headers; |
| 557 | headers[":path"] = kPath; |
| 558 | headers[":authority"] = kHost; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 559 | headers[":method"] = "GET"; |
| 560 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 561 | response_headers_[":status"] = "200"; |
| 562 | response_headers_["content-length"] = "5"; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 563 | const std::string kBody = "Hello"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 564 | std::unique_ptr<char[]> buffer; |
| 565 | QuicByteCount header_length = |
bnc | 4694272 | 2019-10-29 11:56:21 -0700 | [diff] [blame] | 566 | HttpEncoder::SerializeDataFrameHeader(kBody.length(), &buffer); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 567 | memory_cache_backend_.AddResponse(kHost, kPath, std::move(response_headers_), |
| 568 | kBody); |
| 569 | |
| 570 | // Call PushResponse() should trigger stream to fetch response from cache |
| 571 | // and send it back. |
| 572 | InSequence s; |
| 573 | EXPECT_CALL(*server_initiated_stream, WriteHeadersMock(false)); |
| 574 | |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 575 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 576 | EXPECT_CALL(session_, WritevData(kServerInitiatedStreamId, header_length, _, |
| 577 | NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 578 | } |
| 579 | EXPECT_CALL(session_, |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 580 | WritevData(kServerInitiatedStreamId, kBody.size(), _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 581 | server_initiated_stream->PushResponse(std::move(headers)); |
| 582 | EXPECT_EQ(kPath, server_initiated_stream->GetHeader(":path")); |
| 583 | EXPECT_EQ("GET", server_initiated_stream->GetHeader(":method")); |
| 584 | } |
| 585 | |
| 586 | TEST_P(QuicSimpleServerStreamTest, TestSendErrorResponse) { |
| 587 | EXPECT_CALL(session_, SendRstStream(_, QUIC_STREAM_NO_ERROR, _)).Times(0); |
| 588 | |
| 589 | stream_->set_fin_received(true); |
| 590 | |
| 591 | InSequence s; |
| 592 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
renjietang | a29a96a | 2019-10-10 12:47:50 -0700 | [diff] [blame] | 593 | if (UsesHttp3()) { |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 594 | EXPECT_CALL(session_, WritevData(_, kDataFrameHeaderLength, _, NO_FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 595 | } |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 596 | EXPECT_CALL(session_, WritevData(_, kErrorLength, _, FIN, _)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 597 | |
| 598 | stream_->DoSendErrorResponse(); |
| 599 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 600 | EXPECT_TRUE(stream_->write_side_closed()); |
| 601 | } |
| 602 | |
| 603 | TEST_P(QuicSimpleServerStreamTest, InvalidMultipleContentLength) { |
| 604 | EXPECT_CALL(session_, SendRstStream(_, QUIC_STREAM_NO_ERROR, _)).Times(0); |
| 605 | |
| 606 | spdy::SpdyHeaderBlock request_headers; |
| 607 | // \000 is a way to write the null byte when followed by a literal digit. |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 608 | header_list_.OnHeader("content-length", |
| 609 | quiche::QuicheStringPiece("11\00012", 5)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 610 | |
| 611 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 612 | EXPECT_CALL(session_, WritevData(_, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 613 | .WillRepeatedly( |
| 614 | Invoke(&session_, &MockQuicSimpleServerSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 615 | stream_->OnStreamHeaderList(true, kFakeFrameLen, header_list_); |
| 616 | |
| 617 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
| 618 | EXPECT_TRUE(stream_->reading_stopped()); |
| 619 | EXPECT_TRUE(stream_->write_side_closed()); |
| 620 | } |
| 621 | |
| 622 | TEST_P(QuicSimpleServerStreamTest, InvalidLeadingNullContentLength) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 623 | EXPECT_CALL(session_, SendRstStream(_, QUIC_STREAM_NO_ERROR, _)).Times(0); |
| 624 | |
| 625 | spdy::SpdyHeaderBlock request_headers; |
| 626 | // \000 is a way to write the null byte when followed by a literal digit. |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 627 | header_list_.OnHeader("content-length", |
| 628 | quiche::QuicheStringPiece("\00012", 3)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 629 | |
| 630 | EXPECT_CALL(*stream_, WriteHeadersMock(false)); |
| 631 | EXPECT_CALL(session_, WritevData(_, _, _, _, _)) |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 632 | .WillRepeatedly( |
| 633 | Invoke(&session_, &MockQuicSimpleServerSession::ConsumeData)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 634 | stream_->OnStreamHeaderList(true, kFakeFrameLen, header_list_); |
| 635 | |
| 636 | EXPECT_TRUE(QuicStreamPeer::read_side_closed(stream_)); |
| 637 | EXPECT_TRUE(stream_->reading_stopped()); |
| 638 | EXPECT_TRUE(stream_->write_side_closed()); |
| 639 | } |
| 640 | |
| 641 | TEST_P(QuicSimpleServerStreamTest, ValidMultipleContentLength) { |
| 642 | spdy::SpdyHeaderBlock request_headers; |
| 643 | // \000 is a way to write the null byte when followed by a literal digit. |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 644 | header_list_.OnHeader("content-length", |
| 645 | quiche::QuicheStringPiece("11\00011", 5)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 646 | |
| 647 | stream_->OnStreamHeaderList(false, kFakeFrameLen, header_list_); |
| 648 | |
| 649 | EXPECT_EQ(11, stream_->content_length()); |
| 650 | EXPECT_FALSE(QuicStreamPeer::read_side_closed(stream_)); |
| 651 | EXPECT_FALSE(stream_->reading_stopped()); |
| 652 | EXPECT_FALSE(stream_->write_side_closed()); |
| 653 | } |
| 654 | |
| 655 | TEST_P(QuicSimpleServerStreamTest, |
| 656 | DoNotSendQuicRstStreamNoErrorWithRstReceived) { |
| 657 | InSequence s; |
| 658 | EXPECT_FALSE(stream_->reading_stopped()); |
| 659 | |
| 660 | EXPECT_CALL(session_, SendRstStream(_, QUIC_STREAM_NO_ERROR, _)).Times(0); |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 661 | if (VersionUsesHttp3(connection_->transport_version())) { |
| 662 | // Unidirectional stream type and then a Stream Cancellation instruction is |
| 663 | // sent on the QPACK decoder stream. Ignore these writes without any |
| 664 | // assumption on their number or size. |
| 665 | auto* qpack_decoder_stream = |
| 666 | QuicSpdySessionPeer::GetQpackDecoderSendStream(&session_); |
renjietang | 7c23917 | 2020-02-21 13:50:39 -0800 | [diff] [blame] | 667 | EXPECT_CALL(session_, WritevData(qpack_decoder_stream->id(), _, _, _, _)) |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 668 | .Times(AnyNumber()); |
| 669 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 670 | EXPECT_CALL(session_, SendRstStream(_, QUIC_RST_ACKNOWLEDGEMENT, _)).Times(1); |
| 671 | QuicRstStreamFrame rst_frame(kInvalidControlFrameId, stream_->id(), |
| 672 | QUIC_STREAM_CANCELLED, 1234); |
| 673 | stream_->OnStreamReset(rst_frame); |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 674 | if (VersionHasIetfQuicFrames(connection_->transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 675 | // For V99 receiving a RST_STREAM causes a 1-way close; the test requires |
| 676 | // a full close. A CloseWriteSide closes the other half of the stream. |
| 677 | // Everything should then work properly. |
| 678 | stream_->CloseWriteSide(); |
| 679 | } |
| 680 | EXPECT_TRUE(stream_->reading_stopped()); |
| 681 | EXPECT_TRUE(stream_->write_side_closed()); |
| 682 | } |
| 683 | |
| 684 | TEST_P(QuicSimpleServerStreamTest, InvalidHeadersWithFin) { |
| 685 | char arr[] = { |
| 686 | 0x3a, 0x68, 0x6f, 0x73, // :hos |
| 687 | 0x74, 0x00, 0x00, 0x00, // t... |
| 688 | 0x00, 0x00, 0x00, 0x00, // .... |
| 689 | 0x07, 0x3a, 0x6d, 0x65, // .:me |
| 690 | 0x74, 0x68, 0x6f, 0x64, // thod |
| 691 | 0x00, 0x00, 0x00, 0x03, // .... |
| 692 | 0x47, 0x45, 0x54, 0x00, // GET. |
| 693 | 0x00, 0x00, 0x05, 0x3a, // ...: |
| 694 | 0x70, 0x61, 0x74, 0x68, // path |
| 695 | 0x00, 0x00, 0x00, 0x04, // .... |
| 696 | 0x2f, 0x66, 0x6f, 0x6f, // /foo |
| 697 | 0x00, 0x00, 0x00, 0x07, // .... |
| 698 | 0x3a, 0x73, 0x63, 0x68, // :sch |
| 699 | 0x65, 0x6d, 0x65, 0x00, // eme. |
| 700 | 0x00, 0x00, 0x00, 0x00, // .... |
| 701 | 0x00, 0x00, 0x08, 0x3a, // ...: |
| 702 | 0x76, 0x65, 0x72, 0x73, // vers |
| 703 | 0x96, 0x6f, 0x6e, 0x00, // <i(69)>on. |
| 704 | 0x00, 0x00, 0x08, 0x48, // ...H |
| 705 | 0x54, 0x54, 0x50, 0x2f, // TTP/ |
| 706 | 0x31, 0x2e, 0x31, // 1.1 |
| 707 | }; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 708 | quiche::QuicheStringPiece data(arr, QUICHE_ARRAYSIZE(arr)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 709 | QuicStreamFrame frame(stream_->id(), true, 0, data); |
| 710 | // Verify that we don't crash when we get a invalid headers in stream frame. |
| 711 | stream_->OnStreamFrame(frame); |
| 712 | } |
| 713 | |
| 714 | } // namespace |
| 715 | } // namespace test |
| 716 | } // namespace quic |