QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2016 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 | #ifndef QUICHE_HTTP2_HPACK_DECODER_HPACK_ENTRY_COLLECTOR_H_ |
| 6 | #define QUICHE_HTTP2_HPACK_DECODER_HPACK_ENTRY_COLLECTOR_H_ |
| 7 | |
| 8 | // HpackEntryCollector records calls to HpackEntryDecoderListener in support |
| 9 | // of tests of HpackEntryDecoder, or which use it. Can only record the callbacks |
| 10 | // for the decoding of a single entry; call Clear() between decoding successive |
| 11 | // entries or use a distinct HpackEntryCollector for each entry. |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | #include <iosfwd> |
| 16 | |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | #include "net/third_party/quiche/src/http2/hpack/decoder/hpack_entry_decoder_listener.h" |
| 19 | #include "net/third_party/quiche/src/http2/hpack/decoder/hpack_string_collector.h" |
| 20 | #include "net/third_party/quiche/src/http2/hpack/http2_hpack_constants.h" |
| 21 | #include "net/third_party/quiche/src/http2/hpack/tools/hpack_block_builder.h" |
| 22 | #include "net/third_party/quiche/src/http2/platform/api/http2_string.h" |
| 23 | #include "net/third_party/quiche/src/http2/platform/api/http2_string_piece.h" |
| 24 | |
| 25 | namespace http2 { |
| 26 | namespace test { |
| 27 | |
| 28 | class HpackEntryCollector : public HpackEntryDecoderListener { |
| 29 | public: |
| 30 | HpackEntryCollector(); |
| 31 | HpackEntryCollector(const HpackEntryCollector& other); |
| 32 | |
| 33 | // These next three constructors are intended for use in tests that create |
| 34 | // an HpackEntryCollector "manually", and then compare it against another |
| 35 | // that is populated via calls to the HpackEntryDecoderListener methods. |
| 36 | HpackEntryCollector(HpackEntryType type, size_t index_or_size); |
| 37 | HpackEntryCollector(HpackEntryType type, |
| 38 | size_t index, |
| 39 | bool value_huffman, |
| 40 | const Http2String& value); |
| 41 | HpackEntryCollector(HpackEntryType type, |
| 42 | bool name_huffman, |
| 43 | const Http2String& name, |
| 44 | bool value_huffman, |
| 45 | const Http2String& value); |
| 46 | |
| 47 | ~HpackEntryCollector() override; |
| 48 | |
| 49 | // Methods defined by HpackEntryDecoderListener. |
| 50 | void OnIndexedHeader(size_t index) override; |
| 51 | void OnStartLiteralHeader(HpackEntryType header_type, |
| 52 | size_t maybe_name_index) override; |
| 53 | void OnNameStart(bool huffman_encoded, size_t len) override; |
| 54 | void OnNameData(const char* data, size_t len) override; |
| 55 | void OnNameEnd() override; |
| 56 | void OnValueStart(bool huffman_encoded, size_t len) override; |
| 57 | void OnValueData(const char* data, size_t len) override; |
| 58 | void OnValueEnd() override; |
| 59 | void OnDynamicTableSizeUpdate(size_t size) override; |
| 60 | |
| 61 | // Clears the fields of the collector so that it is ready to start collecting |
| 62 | // another HPACK block entry. |
| 63 | void Clear(); |
| 64 | |
| 65 | // Is the collector ready to start collecting another HPACK block entry. |
| 66 | bool IsClear() const; |
| 67 | |
| 68 | // Has a complete entry been collected? |
| 69 | bool IsComplete() const; |
| 70 | |
| 71 | // Based on the HpackEntryType, is a literal name expected? |
| 72 | bool LiteralNameExpected() const; |
| 73 | |
| 74 | // Based on the HpackEntryType, is a literal value expected? |
| 75 | bool LiteralValueExpected() const; |
| 76 | |
| 77 | // Returns success if collected an Indexed Header (i.e. OnIndexedHeader was |
| 78 | // called). |
| 79 | ::testing::AssertionResult ValidateIndexedHeader(size_t expected_index) const; |
| 80 | |
| 81 | // Returns success if collected a Header with an indexed name and literal |
| 82 | // value (i.e. OnStartLiteralHeader was called with a non-zero index for |
| 83 | // the name, which must match expected_index). |
| 84 | ::testing::AssertionResult ValidateLiteralValueHeader( |
| 85 | HpackEntryType expected_type, |
| 86 | size_t expected_index, |
| 87 | bool expected_value_huffman, |
| 88 | Http2StringPiece expected_value) const; |
| 89 | |
| 90 | // Returns success if collected a Header with an literal name and literal |
| 91 | // value. |
| 92 | ::testing::AssertionResult ValidateLiteralNameValueHeader( |
| 93 | HpackEntryType expected_type, |
| 94 | bool expected_name_huffman, |
| 95 | Http2StringPiece expected_name, |
| 96 | bool expected_value_huffman, |
| 97 | Http2StringPiece expected_value) const; |
| 98 | |
| 99 | // Returns success if collected a Dynamic Table Size Update, |
| 100 | // with the specified size. |
| 101 | ::testing::AssertionResult ValidateDynamicTableSizeUpdate( |
| 102 | size_t expected_size) const; |
| 103 | |
| 104 | void set_header_type(HpackEntryType v) { header_type_ = v; } |
| 105 | HpackEntryType header_type() const { return header_type_; } |
| 106 | |
| 107 | void set_index(size_t v) { index_ = v; } |
| 108 | size_t index() const { return index_; } |
| 109 | |
| 110 | void set_name(const HpackStringCollector& v) { name_ = v; } |
| 111 | const HpackStringCollector& name() const { return name_; } |
| 112 | |
| 113 | void set_value(const HpackStringCollector& v) { value_ = v; } |
| 114 | const HpackStringCollector& value() const { return value_; } |
| 115 | |
| 116 | void set_started(bool v) { started_ = v; } |
| 117 | bool started() const { return started_; } |
| 118 | |
| 119 | void set_ended(bool v) { ended_ = v; } |
| 120 | bool ended() const { return ended_; } |
| 121 | |
| 122 | void AppendToHpackBlockBuilder(HpackBlockBuilder* hbb) const; |
| 123 | |
| 124 | // Returns a debug string. |
| 125 | Http2String ToString() const; |
| 126 | |
| 127 | private: |
| 128 | void Init(HpackEntryType type, size_t maybe_index); |
| 129 | |
| 130 | HpackEntryType header_type_; |
| 131 | size_t index_; |
| 132 | |
| 133 | HpackStringCollector name_; |
| 134 | HpackStringCollector value_; |
| 135 | |
| 136 | // True if has received a call to an HpackEntryDecoderListener method |
| 137 | // indicating the start of decoding an HPACK entry; for example, |
| 138 | // OnIndexedHeader set it true, but OnNameStart does not change it. |
| 139 | bool started_ = false; |
| 140 | |
| 141 | // True if has received a call to an HpackEntryDecoderListener method |
| 142 | // indicating the end of decoding an HPACK entry; for example, |
| 143 | // OnIndexedHeader and OnValueEnd both set it true, but OnNameEnd does |
| 144 | // not change it. |
| 145 | bool ended_ = false; |
| 146 | }; |
| 147 | |
| 148 | bool operator==(const HpackEntryCollector& a, const HpackEntryCollector& b); |
| 149 | bool operator!=(const HpackEntryCollector& a, const HpackEntryCollector& b); |
| 150 | std::ostream& operator<<(std::ostream& out, const HpackEntryCollector& v); |
| 151 | |
| 152 | } // namespace test |
| 153 | } // namespace http2 |
| 154 | |
| 155 | #endif // QUICHE_HTTP2_HPACK_DECODER_HPACK_ENTRY_COLLECTOR_H_ |