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