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