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.
11 #include "base/rand_util.h"
12 #include "net/spdy/hpack_constants.h"
13 #include "net/spdy/hpack_decoder.h"
14 #include "net/spdy/hpack_encoder.h"
15 #include "testing/gtest/include/gtest/gtest.h"
25 class HpackRoundTripTest
: public ::testing::Test
{
28 : encoder_(ObtainHpackHuffmanTable()),
29 decoder_(ObtainHpackHuffmanTable()) {}
31 void SetUp() override
{
32 // Use a small table size to tickle eviction handling.
33 encoder_
.ApplyHeaderTableSizeSetting(256);
34 decoder_
.ApplyHeaderTableSizeSetting(256);
37 bool RoundTrip(const SpdyHeaderBlock
& header_set
) {
39 encoder_
.EncodeHeaderSet(header_set
, &encoded
);
41 bool success
= decoder_
.HandleControlFrameHeadersData(
42 1, encoded
.data(), encoded
.size());
43 success
&= decoder_
.HandleControlFrameHeadersComplete(1, nullptr);
45 EXPECT_EQ(header_set
, decoder_
.decoded_block());
49 size_t SampleExponential(size_t mean
, size_t sanity_bound
) {
50 return std::min
<size_t>(-std::log(base::RandDouble()) * mean
,
54 HpackEncoder encoder_
;
55 HpackDecoder decoder_
;
58 TEST_F(HpackRoundTripTest
, ResponseFixtures
) {
60 SpdyHeaderBlock headers
;
61 headers
[":status"] = "302";
62 headers
["cache-control"] = "private";
63 headers
["date"] = "Mon, 21 Oct 2013 20:13:21 GMT";
64 headers
["location"] = "https://www.example.com";
65 EXPECT_TRUE(RoundTrip(headers
));
68 SpdyHeaderBlock headers
;
69 headers
[":status"] = "200";
70 headers
["cache-control"] = "private";
71 headers
["date"] = "Mon, 21 Oct 2013 20:13:21 GMT";
72 headers
["location"] = "https://www.example.com";
73 EXPECT_TRUE(RoundTrip(headers
));
76 SpdyHeaderBlock headers
;
77 headers
[":status"] = "200";
78 headers
["cache-control"] = "private";
79 headers
["content-encoding"] = "gzip";
80 headers
["date"] = "Mon, 21 Oct 2013 20:13:22 GMT";
81 headers
["location"] = "https://www.example.com";
82 headers
["set-cookie"] = "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;"
83 " max-age=3600; version=1";
84 headers
["multivalue"] = string("foo\0bar", 7);
85 EXPECT_TRUE(RoundTrip(headers
));
89 TEST_F(HpackRoundTripTest
, RequestFixtures
) {
91 SpdyHeaderBlock headers
;
92 headers
[":authority"] = "www.example.com";
93 headers
[":method"] = "GET";
94 headers
[":path"] = "/";
95 headers
[":scheme"] = "http";
96 headers
["cookie"] = "baz=bing; foo=bar";
97 EXPECT_TRUE(RoundTrip(headers
));
100 SpdyHeaderBlock headers
;
101 headers
[":authority"] = "www.example.com";
102 headers
[":method"] = "GET";
103 headers
[":path"] = "/";
104 headers
[":scheme"] = "http";
105 headers
["cache-control"] = "no-cache";
106 headers
["cookie"] = "foo=bar; spam=eggs";
107 EXPECT_TRUE(RoundTrip(headers
));
110 SpdyHeaderBlock headers
;
111 headers
[":authority"] = "www.example.com";
112 headers
[":method"] = "GET";
113 headers
[":path"] = "/index.html";
114 headers
[":scheme"] = "https";
115 headers
["custom-key"] = "custom-value";
116 headers
["cookie"] = "baz=bing; fizzle=fazzle; garbage";
117 headers
["multivalue"] = string("foo\0bar", 7);
118 EXPECT_TRUE(RoundTrip(headers
));
122 TEST_F(HpackRoundTripTest
, RandomizedExamples
) {
123 // Grow vectors of names & values, which are seeded with fixtures and then
124 // expanded with dynamically generated data. Samples are taken using the
125 // exponential distribution.
126 vector
<string
> names
;
127 names
.push_back(":authority");
128 names
.push_back(":path");
129 names
.push_back(":status");
130 // TODO(jgraettinger): Enable "cookie" as a name fixture. Crumbs may be
131 // reconstructed in any order, which breaks the simple validation used here.
133 vector
<string
> values
;
134 values
.push_back("/");
135 values
.push_back("/index.html");
136 values
.push_back("200");
137 values
.push_back("404");
138 values
.push_back("");
139 values
.push_back("baz=bing; foo=bar; garbage");
140 values
.push_back("baz=bing; fizzle=fazzle; garbage");
142 int seed
= std::time(NULL
);
143 LOG(INFO
) << "Seeding with srand(" << seed
<< ")";
146 for (size_t i
= 0; i
!= 2000; ++i
) {
147 SpdyHeaderBlock headers
;
149 size_t header_count
= 1 + SampleExponential(7, 50);
150 for (size_t j
= 0; j
!= header_count
; ++j
) {
151 size_t name_index
= SampleExponential(20, 200);
152 size_t value_index
= SampleExponential(20, 200);
155 if (name_index
>= names
.size()) {
156 names
.push_back(base::RandBytesAsString(1 + SampleExponential(5, 30)));
159 name
= names
[name_index
];
161 if (value_index
>= values
.size()) {
163 base::RandBytesAsString(1 + SampleExponential(15, 75));
164 // Currently order is not preserved in the encoder. In particular,
165 // when a value is decomposed at \0 delimiters, its parts might get
166 // encoded out of order if some but not all of them already exist in
167 // the header table. For now, avoid \0 bytes in values.
168 std::replace(newvalue
.begin(), newvalue
.end(), '\x00', '\x01');
169 values
.push_back(newvalue
);
170 value
= values
.back();
172 value
= values
[value_index
];
174 headers
[name
] = value
;
176 EXPECT_TRUE(RoundTrip(headers
));