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 | #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 | |
| 16 | namespace http2 { |
| 17 | namespace test { |
| 18 | namespace { |
| 19 | |
| 20 | std::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 | |
| 35 | HpackStringCollector::HpackStringCollector() { |
| 36 | Clear(); |
| 37 | } |
| 38 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 39 | HpackStringCollector::HpackStringCollector(const std::string& str, bool huffman) |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 40 | : s(str), len(str.size()), huffman_encoded(huffman), state(kEnded) {} |
| 41 | |
| 42 | void HpackStringCollector::Clear() { |
| 43 | s = ""; |
| 44 | len = 0; |
| 45 | huffman_encoded = false; |
| 46 | state = kGenesis; |
| 47 | } |
| 48 | |
| 49 | bool HpackStringCollector::IsClear() const { |
| 50 | return s.empty() && len == 0 && huffman_encoded == false && state == kGenesis; |
| 51 | } |
| 52 | |
| 53 | bool HpackStringCollector::IsInProgress() const { |
| 54 | return state == kStarted; |
| 55 | } |
| 56 | |
| 57 | bool HpackStringCollector::HasEnded() const { |
| 58 | return state == kEnded; |
| 59 | } |
| 60 | |
| 61 | void HpackStringCollector::OnStringStart(bool huffman, size_t length) { |
| 62 | EXPECT_TRUE(IsClear()) << ToString(); |
| 63 | state = kStarted; |
| 64 | huffman_encoded = huffman; |
| 65 | len = length; |
| 66 | } |
| 67 | |
| 68 | void HpackStringCollector::OnStringData(const char* data, size_t length) { |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 69 | quiche::QuicheStringPiece sp(data, length); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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 | |
| 76 | void HpackStringCollector::OnStringEnd() { |
| 77 | EXPECT_TRUE(IsInProgress()) << ToString(); |
| 78 | EXPECT_EQ(s.size(), len) << ToString(); |
| 79 | state = kEnded; |
| 80 | } |
| 81 | |
| 82 | ::testing::AssertionResult HpackStringCollector::Collected( |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 83 | quiche::QuicheStringPiece str, |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 92 | std::string HpackStringCollector::ToString() const { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 93 | std::stringstream ss; |
| 94 | ss << *this; |
| 95 | return ss.str(); |
| 96 | } |
| 97 | |
| 98 | bool 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 | |
| 103 | bool operator!=(const HpackStringCollector& a, const HpackStringCollector& b) { |
| 104 | return !(a == b); |
| 105 | } |
| 106 | |
| 107 | std::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 |