Upstream version 5.34.92.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   int32 new_index = -1;
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(1).as_string();
36   std::string value = encoding_context.GetValueAt(1).as_string();
37   EXPECT_NE(name, encoding_context.GetNameAt(0));
38   EXPECT_NE(value, encoding_context.GetValueAt(0));
39
40   {
41     int32 new_index = -1;
42     std::vector<uint32> removed_referenced_indices;
43     EXPECT_TRUE(
44         encoding_context.ProcessIndexedHeader(1,
45                                               &new_index,
46                                               &removed_referenced_indices));
47     EXPECT_EQ(0, new_index);
48     EXPECT_TRUE(removed_referenced_indices.empty());
49   }
50   EXPECT_EQ(name, encoding_context.GetNameAt(0));
51   EXPECT_EQ(value, encoding_context.GetValueAt(0));
52   EXPECT_TRUE(encoding_context.IsReferencedAt(0));
53
54   {
55     int32 new_index = -1;
56     std::vector<uint32> removed_referenced_indices;
57     EXPECT_TRUE(
58         encoding_context.ProcessIndexedHeader(0,
59                                               &new_index,
60                                               &removed_referenced_indices));
61     EXPECT_EQ(0, new_index);
62     EXPECT_TRUE(removed_referenced_indices.empty());
63   }
64   EXPECT_EQ(name, encoding_context.GetNameAt(0));
65   EXPECT_EQ(value, encoding_context.GetValueAt(0));
66   EXPECT_FALSE(encoding_context.IsReferencedAt(0));
67 }
68
69 // Try to process an indexed header with an index for a static header
70 // and an encoding context where a copy of that header wouldn't
71 // fit. That should succeed without making a copy.
72 TEST(HpackEncodingContextTest, IndexedHeaderStaticCopyDoesNotFit) {
73   HpackEncodingContext encoding_context;
74   encoding_context.SetMaxSize(0);
75
76   int32 new_index = -1;
77   std::vector<uint32> removed_referenced_indices;
78   EXPECT_TRUE(
79       encoding_context.ProcessIndexedHeader(1,
80                                             &new_index,
81                                             &removed_referenced_indices));
82   EXPECT_EQ(-1, new_index);
83   EXPECT_TRUE(removed_referenced_indices.empty());
84 }
85
86 // NOTE: It's too onerous to try to test invalid input to
87 // ProcessLiteralHeaderWithIncrementalIndexing(); that would require
88 // making a really large (>4GB of memory) string.
89
90 // Try to process a reasonably-sized literal header with incremental
91 // indexing. It should succeed.
92 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexing) {
93   HpackEncodingContext encoding_context;
94
95   int32 index = -1;
96   std::vector<uint32> removed_referenced_indices;
97   EXPECT_TRUE(
98       encoding_context.ProcessLiteralHeaderWithIncrementalIndexing(
99           "name", "value", &index, &removed_referenced_indices));
100   EXPECT_EQ(0, index);
101   EXPECT_TRUE(removed_referenced_indices.empty());
102   EXPECT_EQ("name", encoding_context.GetNameAt(0).as_string());
103   EXPECT_EQ("value", encoding_context.GetValueAt(0).as_string());
104   EXPECT_TRUE(encoding_context.IsReferencedAt(0));
105 }
106
107 // Try to process a literal header with incremental indexing that is
108 // too large for the header table. It should succeed without indexing
109 // into the table.
110 TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexingDoesNotFit) {
111   HpackEncodingContext encoding_context;
112   encoding_context.SetMaxSize(0);
113
114   int32 index = -1;
115   std::vector<uint32> removed_referenced_indices;
116   EXPECT_TRUE(
117       encoding_context.ProcessLiteralHeaderWithIncrementalIndexing(
118           "name", "value", &index, &removed_referenced_indices));
119   EXPECT_EQ(-1, index);
120   EXPECT_TRUE(removed_referenced_indices.empty());
121 }
122
123 }  // namespace
124
125 }  // namespace net