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