blob: 4189290eebf5aa1e882a7aaec6d9c4713dd9fa49 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2018 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
bnce42f7ad2019-10-25 17:46:31 -07005#include "net/third_party/quiche/src/quic/test_tools/qpack/qpack_decoder_test_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05006
7#include <algorithm>
8#include <cstddef>
9#include <utility>
10
dschinazi580d30b2019-04-26 15:05:20 -070011#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
QUICHE team6dcf6ab2019-12-11 10:10:51 -080012#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013
14namespace quic {
15namespace test {
16
17void NoopEncoderStreamErrorDelegate::OnEncoderStreamError(
QUICHE team6dcf6ab2019-12-11 10:10:51 -080018 quiche::QuicheStringPiece /*error_message*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050019
QUICHE teama6ef0a62019-03-07 20:34:33 -050020TestHeadersHandler::TestHeadersHandler()
21 : decoding_completed_(false), decoding_error_detected_(false) {}
22
QUICHE team6dcf6ab2019-12-11 10:10:51 -080023void TestHeadersHandler::OnHeaderDecoded(quiche::QuicheStringPiece name,
24 quiche::QuicheStringPiece value) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050025 ASSERT_FALSE(decoding_completed_);
26 ASSERT_FALSE(decoding_error_detected_);
27
28 header_list_.AppendValueOrAddHeader(name, value);
29}
30
31void TestHeadersHandler::OnDecodingCompleted() {
32 ASSERT_FALSE(decoding_completed_);
33 ASSERT_FALSE(decoding_error_detected_);
34
35 decoding_completed_ = true;
36}
37
38void TestHeadersHandler::OnDecodingErrorDetected(
QUICHE team6dcf6ab2019-12-11 10:10:51 -080039 quiche::QuicheStringPiece error_message) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 ASSERT_FALSE(decoding_completed_);
41 ASSERT_FALSE(decoding_error_detected_);
42
43 decoding_error_detected_ = true;
bnce551d3c2019-07-10 10:59:21 -070044 error_message_.assign(error_message.data(), error_message.size());
QUICHE teama6ef0a62019-03-07 20:34:33 -050045}
46
47spdy::SpdyHeaderBlock TestHeadersHandler::ReleaseHeaderList() {
48 DCHECK(decoding_completed_);
49 DCHECK(!decoding_error_detected_);
50
51 return std::move(header_list_);
52}
53
54bool TestHeadersHandler::decoding_completed() const {
55 return decoding_completed_;
56}
57
58bool TestHeadersHandler::decoding_error_detected() const {
59 return decoding_error_detected_;
60}
61
bnce551d3c2019-07-10 10:59:21 -070062const std::string& TestHeadersHandler::error_message() const {
63 DCHECK(decoding_error_detected_);
64 return error_message_;
65}
66
QUICHE teama6ef0a62019-03-07 20:34:33 -050067void QpackDecode(
bnc4c664c52019-08-04 18:14:12 -070068 uint64_t maximum_dynamic_table_capacity,
69 uint64_t maximum_blocked_streams,
QUICHE teama6ef0a62019-03-07 20:34:33 -050070 QpackDecoder::EncoderStreamErrorDelegate* encoder_stream_error_delegate,
renjietangc2aa5cb2019-06-20 12:22:53 -070071 QpackStreamSenderDelegate* decoder_stream_sender_delegate,
QUICHE teama6ef0a62019-03-07 20:34:33 -050072 QpackProgressiveDecoder::HeadersHandlerInterface* handler,
73 const FragmentSizeGenerator& fragment_size_generator,
QUICHE team6dcf6ab2019-12-11 10:10:51 -080074 quiche::QuicheStringPiece data) {
bnc4c664c52019-08-04 18:14:12 -070075 QpackDecoder decoder(maximum_dynamic_table_capacity, maximum_blocked_streams,
renjietang8a2df8f2019-08-07 10:43:52 -070076 encoder_stream_error_delegate);
77 decoder.set_qpack_stream_sender_delegate(decoder_stream_sender_delegate);
QUICHE teama6ef0a62019-03-07 20:34:33 -050078 auto progressive_decoder =
bnca904b052019-06-11 11:34:38 -070079 decoder.CreateProgressiveDecoder(/* stream_id = */ 1, handler);
QUICHE teama6ef0a62019-03-07 20:34:33 -050080 while (!data.empty()) {
81 size_t fragment_size = std::min(fragment_size_generator(), data.size());
82 progressive_decoder->Decode(data.substr(0, fragment_size));
83 data = data.substr(fragment_size);
84 }
85 progressive_decoder->EndHeaderBlock();
86}
87
88} // namespace test
89} // namespace quic