blob: 247ce9c2a6ad767a8470830ddc69ce9d99af8262 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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 "net/third_party/quiche/src/http2/hpack/decoder/hpack_string_collector.h"
6
7#include <stddef.h>
8
9#include <iosfwd>
10#include <ostream>
11
12#include "testing/gtest/include/gtest/gtest.h"
13#include "net/third_party/quiche/src/http2/platform/api/http2_string_utils.h"
14#include "net/third_party/quiche/src/http2/platform/api/http2_test_helpers.h"
15
16namespace http2 {
17namespace test {
18namespace {
19
20std::ostream& operator<<(std::ostream& out,
21 HpackStringCollector::CollectorState v) {
22 switch (v) {
23 case HpackStringCollector::CollectorState::kGenesis:
24 return out << "kGenesis";
25 case HpackStringCollector::CollectorState::kStarted:
26 return out << "kStarted";
27 case HpackStringCollector::CollectorState::kEnded:
28 return out << "kEnded";
29 }
30 return out << "UnknownCollectorState";
31}
32
33} // namespace
34
35HpackStringCollector::HpackStringCollector() {
36 Clear();
37}
38
39HpackStringCollector::HpackStringCollector(const Http2String& str, bool huffman)
40 : s(str), len(str.size()), huffman_encoded(huffman), state(kEnded) {}
41
42void HpackStringCollector::Clear() {
43 s = "";
44 len = 0;
45 huffman_encoded = false;
46 state = kGenesis;
47}
48
49bool HpackStringCollector::IsClear() const {
50 return s.empty() && len == 0 && huffman_encoded == false && state == kGenesis;
51}
52
53bool HpackStringCollector::IsInProgress() const {
54 return state == kStarted;
55}
56
57bool HpackStringCollector::HasEnded() const {
58 return state == kEnded;
59}
60
61void HpackStringCollector::OnStringStart(bool huffman, size_t length) {
62 EXPECT_TRUE(IsClear()) << ToString();
63 state = kStarted;
64 huffman_encoded = huffman;
65 len = length;
66}
67
68void HpackStringCollector::OnStringData(const char* data, size_t length) {
69 Http2StringPiece sp(data, length);
70 EXPECT_TRUE(IsInProgress()) << ToString();
71 EXPECT_LE(sp.size(), len) << ToString();
72 Http2StrAppend(&s, sp);
73 EXPECT_LE(s.size(), len) << ToString();
74}
75
76void HpackStringCollector::OnStringEnd() {
77 EXPECT_TRUE(IsInProgress()) << ToString();
78 EXPECT_EQ(s.size(), len) << ToString();
79 state = kEnded;
80}
81
82::testing::AssertionResult HpackStringCollector::Collected(
83 Http2StringPiece str,
84 bool is_huffman_encoded) const {
85 VERIFY_TRUE(HasEnded());
86 VERIFY_EQ(str.size(), len);
87 VERIFY_EQ(is_huffman_encoded, huffman_encoded);
88 VERIFY_EQ(str, s);
89 return ::testing::AssertionSuccess();
90}
91
92Http2String HpackStringCollector::ToString() const {
93 std::stringstream ss;
94 ss << *this;
95 return ss.str();
96}
97
98bool operator==(const HpackStringCollector& a, const HpackStringCollector& b) {
99 return a.s == b.s && a.len == b.len &&
100 a.huffman_encoded == b.huffman_encoded && a.state == b.state;
101}
102
103bool operator!=(const HpackStringCollector& a, const HpackStringCollector& b) {
104 return !(a == b);
105}
106
107std::ostream& operator<<(std::ostream& out, const HpackStringCollector& v) {
108 out << "HpackStringCollector(state=" << v.state;
109 if (v.state == HpackStringCollector::kGenesis) {
110 return out << ")";
111 }
112 if (v.huffman_encoded) {
113 out << ", Huffman Encoded";
114 }
115 out << ", Length=" << v.len;
116 if (!v.s.empty() && v.len != v.s.size()) {
117 out << " (" << v.s.size() << ")";
118 }
119 return out << ", String=\"" << Http2HexEscape(v.s) << "\")";
120}
121
122} // namespace test
123} // namespace http2