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