1 // Copyright 2014 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.
5 #include "net/spdy/hpack_encoding_context.h"
9 #include "base/basictypes.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 // Try to process an indexed header with an invalid index. That should
18 TEST(HpackEncodingContextTest, IndexedHeaderInvalid) {
19 HpackEncodingContext encoding_context;
22 std::vector<uint32> removed_referenced_indices;
24 encoding_context.ProcessIndexedHeader(kuint32max,
26 &removed_referenced_indices));
29 // Try to process an indexed header with an index for a static
30 // header. That should succeed and add a mutable copy into the header
32 TEST(HpackEncodingContextTest, IndexedHeaderStatic) {
33 HpackEncodingContext encoding_context;
35 std::string name = encoding_context.GetNameAt(2).as_string();
36 std::string value = encoding_context.GetValueAt(2).as_string();
37 EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());
38 EXPECT_NE(name, encoding_context.GetNameAt(1));
39 EXPECT_NE(value, encoding_context.GetValueAt(1));
43 std::vector<uint32> removed_referenced_indices;
45 encoding_context.ProcessIndexedHeader(2,
47 &removed_referenced_indices));
48 EXPECT_EQ(1u, new_index);
49 EXPECT_TRUE(removed_referenced_indices.empty());
51 EXPECT_EQ(1u, encoding_context.GetMutableEntryCount());
52 EXPECT_EQ(name, encoding_context.GetNameAt(1));
53 EXPECT_EQ(value, encoding_context.GetValueAt(1));
54 EXPECT_TRUE(encoding_context.IsReferencedAt(1));
58 std::vector<uint32> removed_referenced_indices;
60 encoding_context.ProcessIndexedHeader(1,
62 &removed_referenced_indices));
63 EXPECT_EQ(1u, new_index);
64 EXPECT_TRUE(removed_referenced_indices.empty());
66 EXPECT_EQ(1u, encoding_context.GetMutableEntryCount());
67 EXPECT_EQ(name, encoding_context.GetNameAt(1));
68 EXPECT_EQ(value, encoding_context.GetValueAt(1));
69 EXPECT_LE(1u, encoding_context.GetMutableEntryCount());
70 EXPECT_FALSE(encoding_context.IsReferencedAt(1));
73 // Try to process an indexed header with an index for a static header
74 // and an encoding context where a copy of that header wouldn't
75 // fit. That should succeed without making a copy.
76 TEST(HpackEncodingContextTest, IndexedHeaderStaticCopyDoesNotFit) {
77 HpackEncodingContext encoding_context;
78 encoding_context.SetMaxSize(0);
81 std::vector<uint32> removed_referenced_indices;
83 encoding_context.ProcessIndexedHeader(1,
85 &removed_referenced_indices));
86 EXPECT_EQ(0u, new_index);
87 EXPECT_TRUE(removed_referenced_indices.empty());
88 EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());
91 // Add a bunch of new headers and then try to process an indexed
92 // header with index 0. That should clear the reference set.
93 TEST(HpackEncodingContextTest, IndexedHeaderZero) {
94 HpackEncodingContext encoding_context;
96 uint32 kEntryCount = 50;
97 std::vector<uint32> expected_removed_referenced_indices;
99 for (uint32 i = 1; i <= kEntryCount; ++i) {
100 uint32 new_index = 0;
101 std::vector<uint32> removed_referenced_indices;
103 encoding_context.ProcessIndexedHeader(i,
105 &removed_referenced_indices));
106 EXPECT_EQ(1u, new_index);
107 EXPECT_TRUE(removed_referenced_indices.empty());
108 EXPECT_EQ(i, encoding_context.GetMutableEntryCount());
109 expected_removed_referenced_indices.push_back(i);
112 uint32 new_index = 0;
113 std::vector<uint32> removed_referenced_indices;
115 encoding_context.ProcessIndexedHeader(0, &new_index,
116 &removed_referenced_indices));
117 EXPECT_EQ(0u, new_index);
118 EXPECT_EQ(expected_removed_referenced_indices, removed_referenced_indices);
121 // NOTE: It's too onerous to try to test invalid input to
122 // ProcessLiteralHeaderWithIncrementalIndexing(); that would require
123 // making a really large (>4GB of memory) string.
125 // Try to process a reasonably-sized literal header with incremental
126 // indexing. It should succeed.
127 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexing) {
128 HpackEncodingContext encoding_context;
131 std::vector<uint32> removed_referenced_indices;
133 encoding_context.ProcessLiteralHeaderWithIncrementalIndexing(
134 "name", "value", &index, &removed_referenced_indices));
135 EXPECT_EQ(1u, index);
136 EXPECT_TRUE(removed_referenced_indices.empty());
137 EXPECT_EQ("name", encoding_context.GetNameAt(1).as_string());
138 EXPECT_EQ("value", encoding_context.GetValueAt(1).as_string());
139 EXPECT_TRUE(encoding_context.IsReferencedAt(1));
140 EXPECT_EQ(1u, encoding_context.GetMutableEntryCount());
143 // Try to process a literal header with incremental indexing that is
144 // too large for the header table. It should succeed without indexing
146 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexingDoesNotFit) {
147 HpackEncodingContext encoding_context;
148 encoding_context.SetMaxSize(0);
151 std::vector<uint32> removed_referenced_indices;
153 encoding_context.ProcessLiteralHeaderWithIncrementalIndexing(
154 "name", "value", &index, &removed_referenced_indices));
155 EXPECT_EQ(0u, index);
156 EXPECT_TRUE(removed_referenced_indices.empty());
157 EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());