| 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/core/hpack/hpack_huffman_decoder.h" | 5 #include "net/spdy/core/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 15 matching lines...) Expand all Loading... |
| 26 uint32_t RandUint32() { | 26 uint32_t RandUint32() { |
| 27 return static_cast<uint32_t>(base::RandUint64() & 0xffffffff); | 27 return static_cast<uint32_t>(base::RandUint64() & 0xffffffff); |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // anonymous namespace | 30 } // anonymous namespace |
| 31 | 31 |
| 32 // Bits(HuffmanWord) constructs a bitset<32>, which produces nicely formatted | 32 // Bits(HuffmanWord) constructs a bitset<32>, which produces nicely formatted |
| 33 // binary numbers when LOG'd. | 33 // binary numbers when LOG'd. |
| 34 typedef std::bitset<32> Bits; | 34 typedef std::bitset<32> Bits; |
| 35 | 35 |
| 36 typedef HpackHuffmanDecoder::HuffmanWord HuffmanWord; | 36 typedef SpdyHpackHuffmanDecoder::HuffmanWord HuffmanWord; |
| 37 typedef HpackHuffmanDecoder::HuffmanCodeLength HuffmanCodeLength; | 37 typedef SpdyHpackHuffmanDecoder::HuffmanCodeLength HuffmanCodeLength; |
| 38 | 38 |
| 39 class HpackHuffmanDecoderPeer { | 39 class HpackHuffmanDecoderPeer { |
| 40 public: | 40 public: |
| 41 static HuffmanCodeLength CodeLengthOfPrefix(HuffmanWord value) { | 41 static HuffmanCodeLength CodeLengthOfPrefix(HuffmanWord value) { |
| 42 return HpackHuffmanDecoder::CodeLengthOfPrefix(value); | 42 return SpdyHpackHuffmanDecoder::CodeLengthOfPrefix(value); |
| 43 } | 43 } |
| 44 | 44 |
| 45 static HuffmanWord DecodeToCanonical(HuffmanCodeLength code_length, | 45 static HuffmanWord DecodeToCanonical(HuffmanCodeLength code_length, |
| 46 HuffmanWord bits) { | 46 HuffmanWord bits) { |
| 47 return HpackHuffmanDecoder::DecodeToCanonical(code_length, bits); | 47 return SpdyHpackHuffmanDecoder::DecodeToCanonical(code_length, bits); |
| 48 } | 48 } |
| 49 | 49 |
| 50 static uint8_t CanonicalToSource(HuffmanWord canonical) { | 50 static uint8_t CanonicalToSource(HuffmanWord canonical) { |
| 51 return HpackHuffmanDecoder::CanonicalToSource(canonical); | 51 return SpdyHpackHuffmanDecoder::CanonicalToSource(canonical); |
| 52 } | 52 } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // Tests of the ability to decode the HPACK Huffman Code, defined in: | 55 // Tests of the ability to decode the HPACK Huffman Code, defined in: |
| 56 // https://httpwg.github.io/specs/rfc7541.html#huffman.code | 56 // https://httpwg.github.io/specs/rfc7541.html#huffman.code |
| 57 class HpackHuffmanDecoderTest : public ::testing::Test { | 57 class HpackHuffmanDecoderTest : public ::testing::Test { |
| 58 protected: | 58 protected: |
| 59 HpackHuffmanDecoderTest() : table_(ObtainHpackHuffmanTable()) {} | 59 HpackHuffmanDecoderTest() : table_(ObtainHpackHuffmanTable()) {} |
| 60 | 60 |
| 61 // Since kHpackHuffmanCode doesn't include the canonical symbol value, | 61 // Since kHpackHuffmanCode doesn't include the canonical symbol value, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 a2b_hex("25a849e95ba97d7f"), | 170 a2b_hex("25a849e95ba97d7f"), |
| 171 "custom-key", | 171 "custom-key", |
| 172 a2b_hex("25a849e95bb8e8b4bf"), | 172 a2b_hex("25a849e95bb8e8b4bf"), |
| 173 "custom-value", | 173 "custom-value", |
| 174 }; | 174 }; |
| 175 // Round-trip each test example. | 175 // Round-trip each test example. |
| 176 for (size_t i = 0; i != arraysize(test_table); i += 2) { | 176 for (size_t i = 0; i != arraysize(test_table); i += 2) { |
| 177 const SpdyString& encodedFixture(test_table[i]); | 177 const SpdyString& encodedFixture(test_table[i]); |
| 178 const SpdyString& decodedFixture(test_table[i + 1]); | 178 const SpdyString& decodedFixture(test_table[i + 1]); |
| 179 HpackInputStream input_stream(encodedFixture); | 179 HpackInputStream input_stream(encodedFixture); |
| 180 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buffer)); | 180 EXPECT_TRUE(SpdyHpackHuffmanDecoder::DecodeString(&input_stream, &buffer)); |
| 181 EXPECT_EQ(decodedFixture, buffer); | 181 EXPECT_EQ(decodedFixture, buffer); |
| 182 buffer = EncodeString(decodedFixture); | 182 buffer = EncodeString(decodedFixture); |
| 183 EXPECT_EQ(encodedFixture, buffer); | 183 EXPECT_EQ(encodedFixture, buffer); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 TEST_F(HpackHuffmanDecoderTest, SpecResponseExamples) { | 187 TEST_F(HpackHuffmanDecoderTest, SpecResponseExamples) { |
| 188 SpdyString buffer; | 188 SpdyString buffer; |
| 189 // clang-format off | 189 // clang-format off |
| 190 SpdyString test_table[] = { | 190 SpdyString test_table[] = { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 202 "d5af27087f3672c1ab270fb5291f9587" | 202 "d5af27087f3672c1ab270fb5291f9587" |
| 203 "316065c003ed4ee5b1063d5007"), | 203 "316065c003ed4ee5b1063d5007"), |
| 204 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", | 204 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
| 205 }; | 205 }; |
| 206 // clang-format on | 206 // clang-format on |
| 207 // Round-trip each test example. | 207 // Round-trip each test example. |
| 208 for (size_t i = 0; i != arraysize(test_table); i += 2) { | 208 for (size_t i = 0; i != arraysize(test_table); i += 2) { |
| 209 const SpdyString& encodedFixture(test_table[i]); | 209 const SpdyString& encodedFixture(test_table[i]); |
| 210 const SpdyString& decodedFixture(test_table[i + 1]); | 210 const SpdyString& decodedFixture(test_table[i + 1]); |
| 211 HpackInputStream input_stream(encodedFixture); | 211 HpackInputStream input_stream(encodedFixture); |
| 212 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buffer)); | 212 EXPECT_TRUE(SpdyHpackHuffmanDecoder::DecodeString(&input_stream, &buffer)); |
| 213 EXPECT_EQ(decodedFixture, buffer); | 213 EXPECT_EQ(decodedFixture, buffer); |
| 214 buffer = EncodeString(decodedFixture); | 214 buffer = EncodeString(decodedFixture); |
| 215 EXPECT_EQ(encodedFixture, buffer); | 215 EXPECT_EQ(encodedFixture, buffer); |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 | 218 |
| 219 TEST_F(HpackHuffmanDecoderTest, RoundTripIndividualSymbols) { | 219 TEST_F(HpackHuffmanDecoderTest, RoundTripIndividualSymbols) { |
| 220 for (size_t i = 0; i != 256; i++) { | 220 for (size_t i = 0; i != 256; i++) { |
| 221 char c = static_cast<char>(i); | 221 char c = static_cast<char>(i); |
| 222 char storage[3] = {c, c, c}; | 222 char storage[3] = {c, c, c}; |
| 223 SpdyStringPiece input(storage, arraysize(storage)); | 223 SpdyStringPiece input(storage, arraysize(storage)); |
| 224 SpdyString buffer_in = EncodeString(input); | 224 SpdyString buffer_in = EncodeString(input); |
| 225 SpdyString buffer_out; | 225 SpdyString buffer_out; |
| 226 HpackInputStream input_stream(buffer_in); | 226 HpackInputStream input_stream(buffer_in); |
| 227 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &buffer_out)); | 227 EXPECT_TRUE(SpdyHpackHuffmanDecoder::DecodeString(&input_stream, &buffer_out
)); |
| 228 EXPECT_EQ(input, buffer_out); | 228 EXPECT_EQ(input, buffer_out); |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 | 231 |
| 232 // Creates 256 input strings, each with a unique byte value i used to sandwich | 232 // Creates 256 input strings, each with a unique byte value i used to sandwich |
| 233 // all the other higher byte values. | 233 // all the other higher byte values. |
| 234 TEST_F(HpackHuffmanDecoderTest, RoundTripSymbolSequences) { | 234 TEST_F(HpackHuffmanDecoderTest, RoundTripSymbolSequences) { |
| 235 SpdyString input; | 235 SpdyString input; |
| 236 SpdyString encoded; | 236 SpdyString encoded; |
| 237 SpdyString decoded; | 237 SpdyString decoded; |
| 238 for (size_t i = 0; i != 256; i++) { | 238 for (size_t i = 0; i != 256; i++) { |
| 239 input.clear(); | 239 input.clear(); |
| 240 auto ic = static_cast<char>(i); | 240 auto ic = static_cast<char>(i); |
| 241 input.push_back(ic); | 241 input.push_back(ic); |
| 242 for (size_t j = i; j != 256; j++) { | 242 for (size_t j = i; j != 256; j++) { |
| 243 input.push_back(static_cast<char>(j)); | 243 input.push_back(static_cast<char>(j)); |
| 244 input.push_back(ic); | 244 input.push_back(ic); |
| 245 } | 245 } |
| 246 EncodeString(input, &encoded); | 246 EncodeString(input, &encoded); |
| 247 HpackInputStream input_stream(encoded); | 247 HpackInputStream input_stream(encoded); |
| 248 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &decoded)); | 248 EXPECT_TRUE(SpdyHpackHuffmanDecoder::DecodeString(&input_stream, &decoded)); |
| 249 EXPECT_EQ(input, decoded); | 249 EXPECT_EQ(input, decoded); |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 } // namespace test | 253 } // namespace test |
| 254 } // namespace net | 254 } // namespace net |
| OLD | NEW |