xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/http2/test_tools/hpack_string_collector.cc (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 #include "quiche/http2/test_tools/hpack_string_collector.h"
6 
7 #include <stddef.h>
8 
9 #include <iosfwd>
10 #include <ostream>
11 
12 #include "absl/strings/escaping.h"
13 #include "absl/strings/str_cat.h"
14 #include "quiche/http2/test_tools/verify_macros.h"
15 #include "quiche/common/platform/api/quiche_test.h"
16 
17 namespace http2 {
18 namespace test {
19 namespace {
20 
operator <<(std::ostream & out,HpackStringCollector::CollectorState v)21 std::ostream& operator<<(std::ostream& out,
22                          HpackStringCollector::CollectorState v) {
23   switch (v) {
24     case HpackStringCollector::CollectorState::kGenesis:
25       return out << "kGenesis";
26     case HpackStringCollector::CollectorState::kStarted:
27       return out << "kStarted";
28     case HpackStringCollector::CollectorState::kEnded:
29       return out << "kEnded";
30   }
31   return out << "UnknownCollectorState";
32 }
33 
34 }  // namespace
35 
HpackStringCollector()36 HpackStringCollector::HpackStringCollector() { Clear(); }
37 
HpackStringCollector(const std::string & str,bool huffman)38 HpackStringCollector::HpackStringCollector(const std::string& str, bool huffman)
39     : s(str), len(str.size()), huffman_encoded(huffman), state(kEnded) {}
40 
Clear()41 void HpackStringCollector::Clear() {
42   s = "";
43   len = 0;
44   huffman_encoded = false;
45   state = kGenesis;
46 }
47 
IsClear() const48 bool HpackStringCollector::IsClear() const {
49   return s.empty() && len == 0 && huffman_encoded == false && state == kGenesis;
50 }
51 
IsInProgress() const52 bool HpackStringCollector::IsInProgress() const { return state == kStarted; }
53 
HasEnded() const54 bool HpackStringCollector::HasEnded() const { return state == kEnded; }
55 
OnStringStart(bool huffman,size_t length)56 void HpackStringCollector::OnStringStart(bool huffman, size_t length) {
57   EXPECT_TRUE(IsClear()) << ToString();
58   state = kStarted;
59   huffman_encoded = huffman;
60   len = length;
61 }
62 
OnStringData(const char * data,size_t length)63 void HpackStringCollector::OnStringData(const char* data, size_t length) {
64   absl::string_view sp(data, length);
65   EXPECT_TRUE(IsInProgress()) << ToString();
66   EXPECT_LE(sp.size(), len) << ToString();
67   absl::StrAppend(&s, sp);
68   EXPECT_LE(s.size(), len) << ToString();
69 }
70 
OnStringEnd()71 void HpackStringCollector::OnStringEnd() {
72   EXPECT_TRUE(IsInProgress()) << ToString();
73   EXPECT_EQ(s.size(), len) << ToString();
74   state = kEnded;
75 }
76 
Collected(absl::string_view str,bool is_huffman_encoded) const77 ::testing::AssertionResult HpackStringCollector::Collected(
78     absl::string_view str, bool is_huffman_encoded) const {
79   HTTP2_VERIFY_TRUE(HasEnded());
80   HTTP2_VERIFY_EQ(str.size(), len);
81   HTTP2_VERIFY_EQ(is_huffman_encoded, huffman_encoded);
82   HTTP2_VERIFY_EQ(str, s);
83   return ::testing::AssertionSuccess();
84 }
85 
ToString() const86 std::string HpackStringCollector::ToString() const {
87   std::stringstream ss;
88   ss << *this;
89   return ss.str();
90 }
91 
operator ==(const HpackStringCollector & a,const HpackStringCollector & b)92 bool operator==(const HpackStringCollector& a, const HpackStringCollector& b) {
93   return a.s == b.s && a.len == b.len &&
94          a.huffman_encoded == b.huffman_encoded && a.state == b.state;
95 }
96 
operator !=(const HpackStringCollector & a,const HpackStringCollector & b)97 bool operator!=(const HpackStringCollector& a, const HpackStringCollector& b) {
98   return !(a == b);
99 }
100 
operator <<(std::ostream & out,const HpackStringCollector & v)101 std::ostream& operator<<(std::ostream& out, const HpackStringCollector& v) {
102   out << "HpackStringCollector(state=" << v.state;
103   if (v.state == HpackStringCollector::kGenesis) {
104     return out << ")";
105   }
106   if (v.huffman_encoded) {
107     out << ", Huffman Encoded";
108   }
109   out << ", Length=" << v.len;
110   if (!v.s.empty() && v.len != v.s.size()) {
111     out << " (" << v.s.size() << ")";
112   }
113   return out << ", String=\"" << absl::CHexEscape(v.s) << "\")";
114 }
115 
116 }  // namespace test
117 }  // namespace http2
118