Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / spdy / hpack_encoding_context_test.cc
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.
4
5 #include "net/spdy/hpack_encoding_context.h"
6
7 #include <vector>
8
9 #include "base/basictypes.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace net {
13
14 namespace {
15
16 // Try to process an indexed header with an invalid index. That should
17 // fail.
18 TEST(HpackEncodingContextTest, IndexedHeaderInvalid) {
19   HpackEncodingContext encoding_context;
20
21   uint32 new_index = 0;
22   std::vector<uint32> removed_referenced_indices;
23   EXPECT_FALSE(
24       encoding_context.ProcessIndexedHeader(kuint32max,
25                                             &new_index,
26                                             &removed_referenced_indices));
27 }
28
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
31 // table.
32 TEST(HpackEncodingContextTest, IndexedHeaderStatic) {
33   HpackEncodingContext encoding_context;
34
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));
40
41   {
42     uint32 new_index = 0;
43     std::vector<uint32> removed_referenced_indices;
44     EXPECT_TRUE(
45         encoding_context.ProcessIndexedHeader(2,
46                                               &new_index,
47                                               &removed_referenced_indices));
48     EXPECT_EQ(1u, new_index);
49     EXPECT_TRUE(removed_referenced_indices.empty());
50   }
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));
55
56   {
57     uint32 new_index = 0;
58     std::vector<uint32> removed_referenced_indices;
59     EXPECT_TRUE(
60         encoding_context.ProcessIndexedHeader(1,
61                                               &new_index,
62                                               &removed_referenced_indices));
63     EXPECT_EQ(1u, new_index);
64     EXPECT_TRUE(removed_referenced_indices.empty());
65   }
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));
71 }
72
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);
79
80   uint32 new_index = 0;
81   std::vector<uint32> removed_referenced_indices;
82   EXPECT_TRUE(
83       encoding_context.ProcessIndexedHeader(1,
84                                             &new_index,
85                                             &removed_referenced_indices));
86   EXPECT_EQ(0u, new_index);
87   EXPECT_TRUE(removed_referenced_indices.empty());
88   EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());
89 }
90
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;
95
96   uint32 kEntryCount = 50;
97   std::vector<uint32> expected_removed_referenced_indices;
98
99   for (uint32 i = 1; i <= kEntryCount; ++i) {
100     uint32 new_index = 0;
101     std::vector<uint32> removed_referenced_indices;
102     EXPECT_TRUE(
103         encoding_context.ProcessIndexedHeader(i,
104                                               &new_index,
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);
110   }
111
112   uint32 new_index = 0;
113   std::vector<uint32> removed_referenced_indices;
114   EXPECT_TRUE(
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);
119 }
120
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.
124
125 // Try to process a reasonably-sized literal header with incremental
126 // indexing. It should succeed.
127 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexing) {
128   HpackEncodingContext encoding_context;
129
130   uint32 index = 0;
131   std::vector<uint32> removed_referenced_indices;
132   EXPECT_TRUE(
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());
141 }
142
143 // Try to process a literal header with incremental indexing that is
144 // too large for the header table. It should succeed without indexing
145 // into the table.
146 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexingDoesNotFit) {
147   HpackEncodingContext encoding_context;
148   encoding_context.SetMaxSize(0);
149
150   uint32 index = 0;
151   std::vector<uint32> removed_referenced_indices;
152   EXPECT_TRUE(
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());
158 }
159
160 }  // namespace
161
162 }  // namespace net