OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/hpack/hpack_huffman_decoder.h" | 5 #include "net/spdy/hpack/hpack_huffman_decoder.h" |
6 | 6 |
7 #include <bitset> | 7 #include <bitset> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 "no-cache", | 172 "no-cache", |
173 a2b_hex("25a849e95ba97d7f"), | 173 a2b_hex("25a849e95ba97d7f"), |
174 "custom-key", | 174 "custom-key", |
175 a2b_hex("25a849e95bb8e8b4bf"), | 175 a2b_hex("25a849e95bb8e8b4bf"), |
176 "custom-value", | 176 "custom-value", |
177 }; | 177 }; |
178 // Round-trip each test example. | 178 // Round-trip each test example. |
179 for (size_t i = 0; i != arraysize(test_table); i += 2) { | 179 for (size_t i = 0; i != arraysize(test_table); i += 2) { |
180 const std::string& encodedFixture(test_table[i]); | 180 const std::string& encodedFixture(test_table[i]); |
181 const std::string& decodedFixture(test_table[i + 1]); | 181 const std::string& decodedFixture(test_table[i + 1]); |
182 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 182 HpackInputStream input_stream(encodedFixture); |
183 encodedFixture); | 183 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buffer)); |
184 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString( | |
185 &input_stream, decodedFixture.size(), &buffer)); | |
186 EXPECT_EQ(decodedFixture, buffer); | 184 EXPECT_EQ(decodedFixture, buffer); |
187 buffer = EncodeString(decodedFixture); | 185 buffer = EncodeString(decodedFixture); |
188 EXPECT_EQ(encodedFixture, buffer); | 186 EXPECT_EQ(encodedFixture, buffer); |
189 } | 187 } |
190 } | 188 } |
191 | 189 |
192 TEST_F(HpackHuffmanDecoderTest, TooLong) { | |
193 std::string buffer; | |
194 std::string test_table[] = { | |
195 a2b_hex("f1e3c2e5f23a6ba0ab90f4ff"), | |
196 "www.example.com", | |
197 a2b_hex("a8eb10649cbf"), | |
198 "no-cache", | |
199 a2b_hex("25a849e95ba97d7f"), | |
200 "custom-key", | |
201 a2b_hex("25a849e95bb8e8b4bf"), | |
202 "custom-value", | |
203 }; | |
204 // Round-trip each test example, but with too small an output buffer. | |
205 for (size_t i = 0; i != arraysize(test_table); i += 2) { | |
206 const std::string& encodedFixture(test_table[i]); | |
207 const std::string& decodedFixture(test_table[i + 1]); | |
208 uint32_t limit = base::RandInt(0, decodedFixture.size() - 1); | |
209 HpackInputStream strm(std::numeric_limits<uint32_t>::max(), encodedFixture); | |
210 EXPECT_FALSE(HpackHuffmanDecoder::DecodeString(&strm, limit, &buffer)); | |
211 | |
212 // This is NOT a required test as it really tests an implementation detail, | |
213 // i.e. the fact that it writes the first |limit| values into |buffer|, | |
214 // then returns false leaving those chars in the buffer. | |
215 EXPECT_EQ(decodedFixture.substr(0, limit), buffer); | |
216 } | |
217 } | |
218 | |
219 TEST_F(HpackHuffmanDecoderTest, SpecResponseExamples) { | 190 TEST_F(HpackHuffmanDecoderTest, SpecResponseExamples) { |
220 std::string buffer; | 191 std::string buffer; |
221 // clang-format off | 192 // clang-format off |
222 std::string test_table[] = { | 193 std::string test_table[] = { |
223 a2b_hex("6402"), | 194 a2b_hex("6402"), |
224 "302", | 195 "302", |
225 a2b_hex("aec3771a4b"), | 196 a2b_hex("aec3771a4b"), |
226 "private", | 197 "private", |
227 a2b_hex("d07abe941054d444a8200595040b8166" | 198 a2b_hex("d07abe941054d444a8200595040b8166" |
228 "e082a62d1bff"), | 199 "e082a62d1bff"), |
229 "Mon, 21 Oct 2013 20:13:21 GMT", | 200 "Mon, 21 Oct 2013 20:13:21 GMT", |
230 a2b_hex("9d29ad171863c78f0b97c8e9ae82ae43" | 201 a2b_hex("9d29ad171863c78f0b97c8e9ae82ae43" |
231 "d3"), | 202 "d3"), |
232 "https://www.example.com", | 203 "https://www.example.com", |
233 a2b_hex("94e7821dd7f2e6c7b335dfdfcd5b3960" | 204 a2b_hex("94e7821dd7f2e6c7b335dfdfcd5b3960" |
234 "d5af27087f3672c1ab270fb5291f9587" | 205 "d5af27087f3672c1ab270fb5291f9587" |
235 "316065c003ed4ee5b1063d5007"), | 206 "316065c003ed4ee5b1063d5007"), |
236 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", | 207 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
237 }; | 208 }; |
238 // clang-format on | 209 // clang-format on |
239 // Round-trip each test example. | 210 // Round-trip each test example. |
240 for (size_t i = 0; i != arraysize(test_table); i += 2) { | 211 for (size_t i = 0; i != arraysize(test_table); i += 2) { |
241 const std::string& encodedFixture(test_table[i]); | 212 const std::string& encodedFixture(test_table[i]); |
242 const std::string& decodedFixture(test_table[i + 1]); | 213 const std::string& decodedFixture(test_table[i + 1]); |
243 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 214 HpackInputStream input_stream(encodedFixture); |
244 encodedFixture); | 215 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buffer)); |
245 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString( | |
246 &input_stream, decodedFixture.size(), &buffer)); | |
247 EXPECT_EQ(decodedFixture, buffer); | 216 EXPECT_EQ(decodedFixture, buffer); |
248 buffer = EncodeString(decodedFixture); | 217 buffer = EncodeString(decodedFixture); |
249 EXPECT_EQ(encodedFixture, buffer); | 218 EXPECT_EQ(encodedFixture, buffer); |
250 } | 219 } |
251 } | 220 } |
252 | 221 |
253 TEST_F(HpackHuffmanDecoderTest, RoundTripIndividualSymbols) { | 222 TEST_F(HpackHuffmanDecoderTest, RoundTripIndividualSymbols) { |
254 for (size_t i = 0; i != 256; i++) { | 223 for (size_t i = 0; i != 256; i++) { |
255 char c = static_cast<char>(i); | 224 char c = static_cast<char>(i); |
256 char storage[3] = {c, c, c}; | 225 char storage[3] = {c, c, c}; |
257 StringPiece input(storage, arraysize(storage)); | 226 StringPiece input(storage, arraysize(storage)); |
258 std::string buffer_in = EncodeString(input); | 227 std::string buffer_in = EncodeString(input); |
259 std::string buffer_out; | 228 std::string buffer_out; |
260 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 229 HpackInputStream input_stream(buffer_in); |
261 buffer_in); | 230 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buffer_out)); |
262 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, input.size(), | |
263 &buffer_out)); | |
264 EXPECT_EQ(input, buffer_out); | 231 EXPECT_EQ(input, buffer_out); |
265 } | 232 } |
266 } | 233 } |
267 | 234 |
268 // Creates 256 input strings, each with a unique byte value i used to sandwich | 235 // Creates 256 input strings, each with a unique byte value i used to sandwich |
269 // all the other higher byte values. | 236 // all the other higher byte values. |
270 TEST_F(HpackHuffmanDecoderTest, RoundTripSymbolSequences) { | 237 TEST_F(HpackHuffmanDecoderTest, RoundTripSymbolSequences) { |
271 std::string input; | 238 std::string input; |
272 std::string encoded; | 239 std::string encoded; |
273 std::string decoded; | 240 std::string decoded; |
274 for (size_t i = 0; i != 256; i++) { | 241 for (size_t i = 0; i != 256; i++) { |
275 input.clear(); | 242 input.clear(); |
276 auto ic = static_cast<char>(i); | 243 auto ic = static_cast<char>(i); |
277 input.push_back(ic); | 244 input.push_back(ic); |
278 for (size_t j = i; j != 256; j++) { | 245 for (size_t j = i; j != 256; j++) { |
279 input.push_back(static_cast<char>(j)); | 246 input.push_back(static_cast<char>(j)); |
280 input.push_back(ic); | 247 input.push_back(ic); |
281 } | 248 } |
282 EncodeString(input, &encoded); | 249 EncodeString(input, &encoded); |
283 HpackInputStream input_stream(std::numeric_limits<uint32_t>::max(), | 250 HpackInputStream input_stream(encoded); |
284 encoded); | 251 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &decoded)); |
285 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, input.size(), | |
286 &decoded)); | |
287 EXPECT_EQ(input, decoded); | 252 EXPECT_EQ(input, decoded); |
288 } | 253 } |
289 } | 254 } |
290 | 255 |
291 } // namespace test | 256 } // namespace test |
292 } // namespace net | 257 } // namespace net |
OLD | NEW |