QUIC - wait for disk cache to load server config if the server is among
[chromium-blink-merge.git] / net / spdy / hpack / hpack_round_trip_test.cc
blobbc703674c1d2aa96cea70e38c65d1fcccb6087ef
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 <cmath>
6 #include <ctime>
7 #include <map>
8 #include <string>
9 #include <vector>
11 #include "base/rand_util.h"
12 #include "net/spdy/hpack/hpack_constants.h"
13 #include "net/spdy/hpack/hpack_decoder.h"
14 #include "net/spdy/hpack/hpack_encoder.h"
15 #include "net/spdy/spdy_test_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace net {
19 namespace test {
21 using std::map;
22 using std::string;
23 using std::vector;
25 namespace {
27 class HpackRoundTripTest : public ::testing::Test {
28 protected:
29 HpackRoundTripTest()
30 : encoder_(ObtainHpackHuffmanTable()),
31 decoder_(ObtainHpackHuffmanTable()) {}
33 void SetUp() override {
34 // Use a small table size to tickle eviction handling.
35 encoder_.ApplyHeaderTableSizeSetting(256);
36 decoder_.ApplyHeaderTableSizeSetting(256);
39 bool RoundTrip(const SpdyHeaderBlock& header_set) {
40 string encoded;
41 encoder_.EncodeHeaderSet(header_set, &encoded);
43 bool success = decoder_.HandleControlFrameHeadersData(1, encoded.data(),
44 encoded.size());
45 success &= decoder_.HandleControlFrameHeadersComplete(1, nullptr);
47 EXPECT_TRUE(CompareSpdyHeaderBlocks(header_set, decoder_.decoded_block()));
48 return success;
51 size_t SampleExponential(size_t mean, size_t sanity_bound) {
52 return std::min<size_t>(-std::log(base::RandDouble()) * mean, sanity_bound);
55 HpackEncoder encoder_;
56 HpackDecoder decoder_;
59 TEST_F(HpackRoundTripTest, ResponseFixtures) {
61 SpdyHeaderBlock headers;
62 headers[":status"] = "302";
63 headers["cache-control"] = "private";
64 headers["date"] = "Mon, 21 Oct 2013 20:13:21 GMT";
65 headers["location"] = "https://www.example.com";
66 EXPECT_TRUE(RoundTrip(headers));
69 SpdyHeaderBlock headers;
70 headers[":status"] = "200";
71 headers["cache-control"] = "private";
72 headers["date"] = "Mon, 21 Oct 2013 20:13:21 GMT";
73 headers["location"] = "https://www.example.com";
74 EXPECT_TRUE(RoundTrip(headers));
77 SpdyHeaderBlock headers;
78 headers[":status"] = "200";
79 headers["cache-control"] = "private";
80 headers["content-encoding"] = "gzip";
81 headers["date"] = "Mon, 21 Oct 2013 20:13:22 GMT";
82 headers["location"] = "https://www.example.com";
83 headers["set-cookie"] =
84 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU;"
85 " max-age=3600; version=1";
86 headers["multivalue"] = string("foo\0bar", 7);
87 EXPECT_TRUE(RoundTrip(headers));
91 TEST_F(HpackRoundTripTest, RequestFixtures) {
93 SpdyHeaderBlock headers;
94 headers[":authority"] = "www.example.com";
95 headers[":method"] = "GET";
96 headers[":path"] = "/";
97 headers[":scheme"] = "http";
98 headers["cookie"] = "baz=bing; foo=bar";
99 EXPECT_TRUE(RoundTrip(headers));
102 SpdyHeaderBlock headers;
103 headers[":authority"] = "www.example.com";
104 headers[":method"] = "GET";
105 headers[":path"] = "/";
106 headers[":scheme"] = "http";
107 headers["cache-control"] = "no-cache";
108 headers["cookie"] = "foo=bar; spam=eggs";
109 EXPECT_TRUE(RoundTrip(headers));
112 SpdyHeaderBlock headers;
113 headers[":authority"] = "www.example.com";
114 headers[":method"] = "GET";
115 headers[":path"] = "/index.html";
116 headers[":scheme"] = "https";
117 headers["custom-key"] = "custom-value";
118 headers["cookie"] = "baz=bing; fizzle=fazzle; garbage";
119 headers["multivalue"] = string("foo\0bar", 7);
120 EXPECT_TRUE(RoundTrip(headers));
124 TEST_F(HpackRoundTripTest, RandomizedExamples) {
125 // Grow vectors of names & values, which are seeded with fixtures and then
126 // expanded with dynamically generated data. Samples are taken using the
127 // exponential distribution.
128 vector<string> pseudo_header_names, random_header_names;
129 pseudo_header_names.push_back(":authority");
130 pseudo_header_names.push_back(":path");
131 pseudo_header_names.push_back(":status");
133 // TODO(jgraettinger): Enable "cookie" as a name fixture. Crumbs may be
134 // reconstructed in any order, which breaks the simple validation used here.
136 vector<string> values;
137 values.push_back("/");
138 values.push_back("/index.html");
139 values.push_back("200");
140 values.push_back("404");
141 values.push_back("");
142 values.push_back("baz=bing; foo=bar; garbage");
143 values.push_back("baz=bing; fizzle=fazzle; garbage");
145 int seed = std::time(NULL);
146 LOG(INFO) << "Seeding with srand(" << seed << ")";
147 srand(seed);
149 for (size_t i = 0; i != 2000; ++i) {
150 SpdyHeaderBlock headers;
152 // Choose a random number of headers to add, and of these a random subset
153 // will be HTTP/2 pseudo headers.
154 size_t header_count = 1 + SampleExponential(7, 50);
155 size_t pseudo_header_count =
156 std::min(header_count, 1 + SampleExponential(7, 50));
157 EXPECT_LE(pseudo_header_count, header_count);
158 for (size_t j = 0; j != header_count; ++j) {
159 string name, value;
160 // Pseudo headers must be added before regular headers.
161 if (j < pseudo_header_count) {
162 // Choose one of the defined pseudo headers at random.
163 size_t name_index = base::RandGenerator(pseudo_header_names.size());
164 name = pseudo_header_names[name_index];
165 } else {
166 // Randomly reuse an existing header name, or generate a new one.
167 size_t name_index = SampleExponential(20, 200);
168 if (name_index >= random_header_names.size()) {
169 name = base::RandBytesAsString(1 + SampleExponential(5, 30));
170 // A regular header cannot begin with the pseudo header prefix ":".
171 if (name[0] == ':') {
172 name[0] = 'x';
174 random_header_names.push_back(name);
175 } else {
176 name = random_header_names[name_index];
180 // Randomly reuse an existing value, or generate a new one.
181 size_t value_index = SampleExponential(20, 200);
182 if (value_index >= values.size()) {
183 string newvalue =
184 base::RandBytesAsString(1 + SampleExponential(15, 75));
185 // Currently order is not preserved in the encoder. In particular,
186 // when a value is decomposed at \0 delimiters, its parts might get
187 // encoded out of order if some but not all of them already exist in
188 // the header table. For now, avoid \0 bytes in values.
189 std::replace(newvalue.begin(), newvalue.end(), '\x00', '\x01');
190 values.push_back(newvalue);
191 value = values.back();
192 } else {
193 value = values[value_index];
195 headers[name] = value;
197 EXPECT_TRUE(RoundTrip(headers));
201 } // namespace
203 } // namespace test
204 } // namespace net