summaryrefslogtreecommitdiffstats
path: root/net/spdy/hpack_encoding_context_test.cc
blob: de7f87d07220728ace95120435b8298a9e631a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/spdy/hpack_encoding_context.h"

#include <vector>

#include "base/basictypes.h"
#include "net/spdy/hpack_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace test {

class HpackEncodingContextPeer {
 public:
  explicit HpackEncodingContextPeer(const HpackEncodingContext& context)
      : context_(context) {}

  const HpackHeaderTable& header_table() {
    return context_.header_table_;
  }
  uint32 settings_header_table_size() {
    return context_.settings_header_table_size_;
  }

 private:
  const HpackEncodingContext& context_;
};

}  // namespace test

namespace {

TEST(HpackEncodingContextTest, ApplyHeaderTableSizeSetting) {
  HpackEncodingContext context;
  test::HpackEncodingContextPeer peer(context);

  // Default setting and table size are kDefaultHeaderTableSizeSetting.
  EXPECT_EQ(kDefaultHeaderTableSizeSetting,
            peer.settings_header_table_size());
  EXPECT_EQ(kDefaultHeaderTableSizeSetting,
            peer.header_table().max_size());

  // Applying a larger table size setting doesn't affect the headers table.
  context.ApplyHeaderTableSizeSetting(kDefaultHeaderTableSizeSetting * 2);

  EXPECT_EQ(kDefaultHeaderTableSizeSetting * 2,
            peer.settings_header_table_size());
  EXPECT_EQ(kDefaultHeaderTableSizeSetting,
            peer.header_table().max_size());

  // Applying a smaller size setting does update the headers table.
  context.ApplyHeaderTableSizeSetting(kDefaultHeaderTableSizeSetting / 2);

  EXPECT_EQ(kDefaultHeaderTableSizeSetting / 2,
            peer.settings_header_table_size());
  EXPECT_EQ(kDefaultHeaderTableSizeSetting / 2,
            peer.header_table().max_size());
}

TEST(HpackEncodingContextTest, ProcessContextUpdateNewMaximumSize) {
  HpackEncodingContext context;
  test::HpackEncodingContextPeer peer(context);

  EXPECT_EQ(kDefaultHeaderTableSizeSetting,
            peer.settings_header_table_size());

  // Shrink maximum size by half. Succeeds.
  EXPECT_TRUE(context.ProcessContextUpdateNewMaximumSize(
      kDefaultHeaderTableSizeSetting / 2));
  EXPECT_EQ(kDefaultHeaderTableSizeSetting / 2,
            peer.header_table().max_size());

  // Double maximum size. Succeeds.
  EXPECT_TRUE(context.ProcessContextUpdateNewMaximumSize(
      kDefaultHeaderTableSizeSetting));
  EXPECT_EQ(kDefaultHeaderTableSizeSetting, peer.header_table().max_size());

  // One beyond table size setting. Fails.
  EXPECT_FALSE(context.ProcessContextUpdateNewMaximumSize(
      kDefaultHeaderTableSizeSetting + 1));
  EXPECT_EQ(kDefaultHeaderTableSizeSetting, peer.header_table().max_size());
}

// Try to process an indexed header with an index for a static
// header. That should succeed and add a mutable copy into the header
// table.
TEST(HpackEncodingContextTest, IndexedHeaderStatic) {
  HpackEncodingContext encoding_context;

  std::string name = encoding_context.GetNameAt(2).as_string();
  std::string value = encoding_context.GetValueAt(2).as_string();
  EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());
  EXPECT_NE(name, encoding_context.GetNameAt(1));
  EXPECT_NE(value, encoding_context.GetValueAt(1));

  {
    uint32 new_index = 0;
    std::vector<uint32> removed_referenced_indices;
    EXPECT_TRUE(
        encoding_context.ProcessIndexedHeader(2,
                                              &new_index,
                                              &removed_referenced_indices));
    EXPECT_EQ(1u, new_index);
    EXPECT_TRUE(removed_referenced_indices.empty());
  }
  EXPECT_EQ(1u, encoding_context.GetMutableEntryCount());
  EXPECT_EQ(name, encoding_context.GetNameAt(1));
  EXPECT_EQ(value, encoding_context.GetValueAt(1));
  EXPECT_TRUE(encoding_context.IsReferencedAt(1));

  {
    uint32 new_index = 0;
    std::vector<uint32> removed_referenced_indices;
    EXPECT_TRUE(
        encoding_context.ProcessIndexedHeader(1,
                                              &new_index,
                                              &removed_referenced_indices));
    EXPECT_EQ(1u, new_index);
    EXPECT_TRUE(removed_referenced_indices.empty());
  }
  EXPECT_EQ(1u, encoding_context.GetMutableEntryCount());
  EXPECT_EQ(name, encoding_context.GetNameAt(1));
  EXPECT_EQ(value, encoding_context.GetValueAt(1));
  EXPECT_LE(1u, encoding_context.GetMutableEntryCount());
  EXPECT_FALSE(encoding_context.IsReferencedAt(1));
}

// Try to process an indexed header with an index for a static header
// and an encoding context where a copy of that header wouldn't
// fit. That should succeed without making a copy.
TEST(HpackEncodingContextTest, IndexedHeaderStaticCopyDoesNotFit) {
  HpackEncodingContext encoding_context;
  encoding_context.ProcessContextUpdateNewMaximumSize(0);

  uint32 new_index = 0;
  std::vector<uint32> removed_referenced_indices;
  EXPECT_TRUE(
      encoding_context.ProcessIndexedHeader(1,
                                            &new_index,
                                            &removed_referenced_indices));
  EXPECT_EQ(0u, new_index);
  EXPECT_TRUE(removed_referenced_indices.empty());
  EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());
}

// Add a bunch of new headers and then process a context update to empty the
// reference set. Expect it to be empty.
TEST(HpackEncodingContextTest, ProcessContextUpdateEmptyReferenceSet) {
  HpackEncodingContext encoding_context;
  test::HpackEncodingContextPeer peer(encoding_context);

  uint32 kEntryCount = 50;

  for (uint32 i = 1; i <= kEntryCount; ++i) {
    uint32 new_index = 0;
    std::vector<uint32> removed_referenced_indices;
    EXPECT_TRUE(
        encoding_context.ProcessIndexedHeader(i,
                                              &new_index,
                                              &removed_referenced_indices));
    EXPECT_EQ(1u, new_index);
    EXPECT_TRUE(removed_referenced_indices.empty());
    EXPECT_EQ(i, encoding_context.GetMutableEntryCount());
  }

  for (uint32 i = 1; i <= kEntryCount; ++i) {
    EXPECT_TRUE(peer.header_table().GetEntry(i).IsReferenced());
  }
  encoding_context.ProcessContextUpdateEmptyReferenceSet();
  for (uint32 i = 1; i <= kEntryCount; ++i) {
    EXPECT_FALSE(peer.header_table().GetEntry(i).IsReferenced());
  }
}

// NOTE: It's too onerous to try to test invalid input to
// ProcessLiteralHeaderWithIncrementalIndexing(); that would require
// making a really large (>4GB of memory) string.

// Try to process a reasonably-sized literal header with incremental
// indexing. It should succeed.
TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexing) {
  HpackEncodingContext encoding_context;

  uint32 index = 0;
  std::vector<uint32> removed_referenced_indices;
  EXPECT_TRUE(
      encoding_context.ProcessLiteralHeaderWithIncrementalIndexing(
          "name", "value", &index, &removed_referenced_indices));
  EXPECT_EQ(1u, index);
  EXPECT_TRUE(removed_referenced_indices.empty());
  EXPECT_EQ("name", encoding_context.GetNameAt(1).as_string());
  EXPECT_EQ("value", encoding_context.GetValueAt(1).as_string());
  EXPECT_TRUE(encoding_context.IsReferencedAt(1));
  EXPECT_EQ(1u, encoding_context.GetMutableEntryCount());
}

// Try to process a literal header with incremental indexing that is
// too large for the header table. It should succeed without indexing
// into the table.
TEST(HpackEncodingContextTest, LiteralHeaderIncrementalIndexingDoesNotFit) {
  HpackEncodingContext encoding_context;
  encoding_context.ProcessContextUpdateNewMaximumSize(0);

  uint32 index = 0;
  std::vector<uint32> removed_referenced_indices;
  EXPECT_TRUE(
      encoding_context.ProcessLiteralHeaderWithIncrementalIndexing(
          "name", "value", &index, &removed_referenced_indices));
  EXPECT_EQ(0u, index);
  EXPECT_TRUE(removed_referenced_indices.empty());
  EXPECT_EQ(0u, encoding_context.GetMutableEntryCount());
}

}  // namespace

}  // namespace net