blob: 325a60c00679a7c0e40771dc89732e29ac37d0c8 [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_block_collector.h"
6
7#include <algorithm>
8#include <memory>
9
QUICHE team61940b42019-03-07 23:32:27 -050010#include "net/third_party/quiche/src/http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050011#include "net/third_party/quiche/src/http2/platform/api/http2_test_helpers.h"
12
13using ::testing::AssertionResult;
14using ::testing::AssertionSuccess;
15
16namespace http2 {
17namespace test {
18
19HpackBlockCollector::HpackBlockCollector() = default;
20HpackBlockCollector::HpackBlockCollector(const HpackBlockCollector& other)
21 : pending_entry_(other.pending_entry_), entries_(other.entries_) {}
22HpackBlockCollector::~HpackBlockCollector() = default;
23
24void HpackBlockCollector::OnIndexedHeader(size_t index) {
25 pending_entry_.OnIndexedHeader(index);
26 PushPendingEntry();
27}
28void HpackBlockCollector::OnDynamicTableSizeUpdate(size_t size) {
29 pending_entry_.OnDynamicTableSizeUpdate(size);
30 PushPendingEntry();
31}
32void HpackBlockCollector::OnStartLiteralHeader(HpackEntryType header_type,
33 size_t maybe_name_index) {
34 pending_entry_.OnStartLiteralHeader(header_type, maybe_name_index);
35}
36void HpackBlockCollector::OnNameStart(bool huffman_encoded, size_t len) {
37 pending_entry_.OnNameStart(huffman_encoded, len);
38}
39void HpackBlockCollector::OnNameData(const char* data, size_t len) {
40 pending_entry_.OnNameData(data, len);
41}
42void HpackBlockCollector::OnNameEnd() {
43 pending_entry_.OnNameEnd();
44}
45void HpackBlockCollector::OnValueStart(bool huffman_encoded, size_t len) {
46 pending_entry_.OnValueStart(huffman_encoded, len);
47}
48void HpackBlockCollector::OnValueData(const char* data, size_t len) {
49 pending_entry_.OnValueData(data, len);
50}
51void HpackBlockCollector::OnValueEnd() {
52 pending_entry_.OnValueEnd();
53 PushPendingEntry();
54}
55
56void HpackBlockCollector::PushPendingEntry() {
57 EXPECT_TRUE(pending_entry_.IsComplete());
QUICHE team61940b42019-03-07 23:32:27 -050058 HTTP2_DVLOG(2) << "PushPendingEntry: " << pending_entry_;
QUICHE teamfd50a402018-12-07 22:54:05 -050059 entries_.push_back(pending_entry_);
60 EXPECT_TRUE(entries_.back().IsComplete());
61 pending_entry_.Clear();
62}
63void HpackBlockCollector::Clear() {
64 pending_entry_.Clear();
65 entries_.clear();
66}
67
68void HpackBlockCollector::ExpectIndexedHeader(size_t index) {
69 entries_.push_back(
70 HpackEntryCollector(HpackEntryType::kIndexedHeader, index));
71}
72void HpackBlockCollector::ExpectDynamicTableSizeUpdate(size_t size) {
73 entries_.push_back(
74 HpackEntryCollector(HpackEntryType::kDynamicTableSizeUpdate, size));
75}
76void HpackBlockCollector::ExpectNameIndexAndLiteralValue(
77 HpackEntryType type,
78 size_t index,
79 bool value_huffman,
bnc47904002019-08-16 11:49:48 -070080 const std::string& value) {
QUICHE teamfd50a402018-12-07 22:54:05 -050081 entries_.push_back(HpackEntryCollector(type, index, value_huffman, value));
82}
83void HpackBlockCollector::ExpectLiteralNameAndValue(HpackEntryType type,
84 bool name_huffman,
bnc47904002019-08-16 11:49:48 -070085 const std::string& name,
QUICHE teamfd50a402018-12-07 22:54:05 -050086 bool value_huffman,
bnc47904002019-08-16 11:49:48 -070087 const std::string& value) {
QUICHE teamfd50a402018-12-07 22:54:05 -050088 entries_.push_back(
89 HpackEntryCollector(type, name_huffman, name, value_huffman, value));
90}
91
92void HpackBlockCollector::ShuffleEntries(Http2Random* rng) {
93 std::shuffle(entries_.begin(), entries_.end(), *rng);
94}
95
96void HpackBlockCollector::AppendToHpackBlockBuilder(
97 HpackBlockBuilder* hbb) const {
98 CHECK(IsNotPending());
99 for (const auto& entry : entries_) {
100 entry.AppendToHpackBlockBuilder(hbb);
101 }
102}
103
104AssertionResult 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}
111AssertionResult HpackBlockCollector::ValidateSoleLiteralValueHeader(
112 HpackEntryType expected_type,
113 size_t expected_index,
114 bool expected_value_huffman,
bnc74646d12019-12-13 09:21:19 -0800115 quiche::QuicheStringPiece expected_value) const {
QUICHE teamfd50a402018-12-07 22:54:05 -0500116 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}
122AssertionResult HpackBlockCollector::ValidateSoleLiteralNameValueHeader(
123 HpackEntryType expected_type,
124 bool expected_name_huffman,
bnc74646d12019-12-13 09:21:19 -0800125 quiche::QuicheStringPiece expected_name,
QUICHE teamfd50a402018-12-07 22:54:05 -0500126 bool expected_value_huffman,
bnc74646d12019-12-13 09:21:19 -0800127 quiche::QuicheStringPiece expected_value) const {
QUICHE teamfd50a402018-12-07 22:54:05 -0500128 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}
135AssertionResult 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
143AssertionResult 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