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_block_collector.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
| 9 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/http2/platform/api/http2_logging.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/http2/platform/api/http2_test_helpers.h" |
| 12 | |
| 13 | using ::testing::AssertionResult; |
| 14 | using ::testing::AssertionSuccess; |
| 15 | |
| 16 | namespace http2 { |
| 17 | namespace test { |
| 18 | |
| 19 | HpackBlockCollector::HpackBlockCollector() = default; |
| 20 | HpackBlockCollector::HpackBlockCollector(const HpackBlockCollector& other) |
| 21 | : pending_entry_(other.pending_entry_), entries_(other.entries_) {} |
| 22 | HpackBlockCollector::~HpackBlockCollector() = default; |
| 23 | |
| 24 | void HpackBlockCollector::OnIndexedHeader(size_t index) { |
| 25 | pending_entry_.OnIndexedHeader(index); |
| 26 | PushPendingEntry(); |
| 27 | } |
| 28 | void HpackBlockCollector::OnDynamicTableSizeUpdate(size_t size) { |
| 29 | pending_entry_.OnDynamicTableSizeUpdate(size); |
| 30 | PushPendingEntry(); |
| 31 | } |
| 32 | void HpackBlockCollector::OnStartLiteralHeader(HpackEntryType header_type, |
| 33 | size_t maybe_name_index) { |
| 34 | pending_entry_.OnStartLiteralHeader(header_type, maybe_name_index); |
| 35 | } |
| 36 | void HpackBlockCollector::OnNameStart(bool huffman_encoded, size_t len) { |
| 37 | pending_entry_.OnNameStart(huffman_encoded, len); |
| 38 | } |
| 39 | void HpackBlockCollector::OnNameData(const char* data, size_t len) { |
| 40 | pending_entry_.OnNameData(data, len); |
| 41 | } |
| 42 | void HpackBlockCollector::OnNameEnd() { |
| 43 | pending_entry_.OnNameEnd(); |
| 44 | } |
| 45 | void HpackBlockCollector::OnValueStart(bool huffman_encoded, size_t len) { |
| 46 | pending_entry_.OnValueStart(huffman_encoded, len); |
| 47 | } |
| 48 | void HpackBlockCollector::OnValueData(const char* data, size_t len) { |
| 49 | pending_entry_.OnValueData(data, len); |
| 50 | } |
| 51 | void HpackBlockCollector::OnValueEnd() { |
| 52 | pending_entry_.OnValueEnd(); |
| 53 | PushPendingEntry(); |
| 54 | } |
| 55 | |
| 56 | void HpackBlockCollector::PushPendingEntry() { |
| 57 | EXPECT_TRUE(pending_entry_.IsComplete()); |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame] | 58 | HTTP2_DVLOG(2) << "PushPendingEntry: " << pending_entry_; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 59 | entries_.push_back(pending_entry_); |
| 60 | EXPECT_TRUE(entries_.back().IsComplete()); |
| 61 | pending_entry_.Clear(); |
| 62 | } |
| 63 | void HpackBlockCollector::Clear() { |
| 64 | pending_entry_.Clear(); |
| 65 | entries_.clear(); |
| 66 | } |
| 67 | |
| 68 | void HpackBlockCollector::ExpectIndexedHeader(size_t index) { |
| 69 | entries_.push_back( |
| 70 | HpackEntryCollector(HpackEntryType::kIndexedHeader, index)); |
| 71 | } |
| 72 | void HpackBlockCollector::ExpectDynamicTableSizeUpdate(size_t size) { |
| 73 | entries_.push_back( |
| 74 | HpackEntryCollector(HpackEntryType::kDynamicTableSizeUpdate, size)); |
| 75 | } |
| 76 | void HpackBlockCollector::ExpectNameIndexAndLiteralValue( |
| 77 | HpackEntryType type, |
| 78 | size_t index, |
| 79 | bool value_huffman, |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 80 | const std::string& value) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 81 | entries_.push_back(HpackEntryCollector(type, index, value_huffman, value)); |
| 82 | } |
| 83 | void HpackBlockCollector::ExpectLiteralNameAndValue(HpackEntryType type, |
| 84 | bool name_huffman, |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 85 | const std::string& name, |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 86 | bool value_huffman, |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 87 | const std::string& value) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 88 | entries_.push_back( |
| 89 | HpackEntryCollector(type, name_huffman, name, value_huffman, value)); |
| 90 | } |
| 91 | |
| 92 | void HpackBlockCollector::ShuffleEntries(Http2Random* rng) { |
| 93 | std::shuffle(entries_.begin(), entries_.end(), *rng); |
| 94 | } |
| 95 | |
| 96 | void HpackBlockCollector::AppendToHpackBlockBuilder( |
| 97 | HpackBlockBuilder* hbb) const { |
| 98 | CHECK(IsNotPending()); |
| 99 | for (const auto& entry : entries_) { |
| 100 | entry.AppendToHpackBlockBuilder(hbb); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | AssertionResult HpackBlockCollector::ValidateSoleIndexedHeader( |
| 105 | size_t ndx) const { |
| 106 | VERIFY_TRUE(pending_entry_.IsClear()); |
| 107 | VERIFY_EQ(1u, entries_.size()); |
| 108 | VERIFY_TRUE(entries_.front().ValidateIndexedHeader(ndx)); |
| 109 | return AssertionSuccess(); |
| 110 | } |
| 111 | AssertionResult HpackBlockCollector::ValidateSoleLiteralValueHeader( |
| 112 | HpackEntryType expected_type, |
| 113 | size_t expected_index, |
| 114 | bool expected_value_huffman, |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 115 | quiche::QuicheStringPiece expected_value) const { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 116 | VERIFY_TRUE(pending_entry_.IsClear()); |
| 117 | VERIFY_EQ(1u, entries_.size()); |
| 118 | VERIFY_TRUE(entries_.front().ValidateLiteralValueHeader( |
| 119 | expected_type, expected_index, expected_value_huffman, expected_value)); |
| 120 | return AssertionSuccess(); |
| 121 | } |
| 122 | AssertionResult HpackBlockCollector::ValidateSoleLiteralNameValueHeader( |
| 123 | HpackEntryType expected_type, |
| 124 | bool expected_name_huffman, |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 125 | quiche::QuicheStringPiece expected_name, |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 126 | bool expected_value_huffman, |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 127 | quiche::QuicheStringPiece expected_value) const { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 128 | VERIFY_TRUE(pending_entry_.IsClear()); |
| 129 | VERIFY_EQ(1u, entries_.size()); |
| 130 | VERIFY_TRUE(entries_.front().ValidateLiteralNameValueHeader( |
| 131 | expected_type, expected_name_huffman, expected_name, |
| 132 | expected_value_huffman, expected_value)); |
| 133 | return AssertionSuccess(); |
| 134 | } |
| 135 | AssertionResult HpackBlockCollector::ValidateSoleDynamicTableSizeUpdate( |
| 136 | size_t size) const { |
| 137 | VERIFY_TRUE(pending_entry_.IsClear()); |
| 138 | VERIFY_EQ(1u, entries_.size()); |
| 139 | VERIFY_TRUE(entries_.front().ValidateDynamicTableSizeUpdate(size)); |
| 140 | return AssertionSuccess(); |
| 141 | } |
| 142 | |
| 143 | AssertionResult HpackBlockCollector::VerifyEq( |
| 144 | const HpackBlockCollector& that) const { |
| 145 | VERIFY_EQ(pending_entry_, that.pending_entry_); |
| 146 | VERIFY_EQ(entries_, that.entries_); |
| 147 | return AssertionSuccess(); |
| 148 | } |
| 149 | |
| 150 | } // namespace test |
| 151 | } // namespace http2 |