| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_output_stream.h" | 5 #include "net/spdy/hpack_output_stream.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 using std::string; | 16 using std::string; |
| 17 | 17 |
| 18 // Make sure that AppendBits() appends bits starting from the most | 18 // Make sure that AppendBits() appends bits starting from the most |
| 19 // significant bit, and that it can handle crossing a byte boundary. | 19 // significant bit, and that it can handle crossing a byte boundary. |
| 20 TEST(HpackOutputStreamTest, AppendBits) { | 20 TEST(HpackOutputStreamTest, AppendBits) { |
| 21 HpackOutputStream output_stream(kuint32max); | 21 HpackOutputStream output_stream; |
| 22 string expected_str; | 22 string expected_str; |
| 23 | 23 |
| 24 output_stream.AppendBitsForTest(0x1, 1); | 24 output_stream.AppendBits(0x1, 1); |
| 25 expected_str.append(1, 0x00); | 25 expected_str.append(1, 0x00); |
| 26 *expected_str.rbegin() |= (0x1 << 7); | 26 *expected_str.rbegin() |= (0x1 << 7); |
| 27 | 27 |
| 28 output_stream.AppendBitsForTest(0x0, 1); | 28 output_stream.AppendBits(0x0, 1); |
| 29 | 29 |
| 30 output_stream.AppendBitsForTest(0x3, 2); | 30 output_stream.AppendBits(0x3, 2); |
| 31 *expected_str.rbegin() |= (0x3 << 4); | 31 *expected_str.rbegin() |= (0x3 << 4); |
| 32 | 32 |
| 33 output_stream.AppendBitsForTest(0x0, 2); | 33 output_stream.AppendBits(0x0, 2); |
| 34 | 34 |
| 35 // Byte-crossing append. | 35 // Byte-crossing append. |
| 36 output_stream.AppendBitsForTest(0x7, 3); | 36 output_stream.AppendBits(0x7, 3); |
| 37 *expected_str.rbegin() |= (0x7 >> 1); | 37 *expected_str.rbegin() |= (0x7 >> 1); |
| 38 expected_str.append(1, 0x00); | 38 expected_str.append(1, 0x00); |
| 39 *expected_str.rbegin() |= (0x7 << 7); | 39 *expected_str.rbegin() |= (0x7 << 7); |
| 40 | 40 |
| 41 output_stream.AppendBitsForTest(0x0, 7); | 41 output_stream.AppendBits(0x0, 7); |
| 42 | 42 |
| 43 string str; | 43 string str; |
| 44 output_stream.TakeString(&str); | 44 output_stream.TakeString(&str); |
| 45 EXPECT_EQ(expected_str, str); | 45 EXPECT_EQ(expected_str, str); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Utility function to return I as a string encoded with an N-bit | 48 // Utility function to return I as a string encoded with an N-bit |
| 49 // prefix. | 49 // prefix. |
| 50 string EncodeUint32(uint8 N, uint32 I) { | 50 string EncodeUint32(uint8 N, uint32 I) { |
| 51 HpackOutputStream output_stream(kuint32max); | 51 HpackOutputStream output_stream; |
| 52 if (N < 8) { | 52 if (N < 8) { |
| 53 output_stream.AppendBitsForTest(0x00, 8 - N); | 53 output_stream.AppendBits(0x00, 8 - N); |
| 54 } | 54 } |
| 55 output_stream.AppendUint32ForTest(I); | 55 output_stream.AppendUint32(I); |
| 56 string str; | 56 string str; |
| 57 output_stream.TakeString(&str); | 57 output_stream.TakeString(&str); |
| 58 return str; | 58 return str; |
| 59 } | 59 } |
| 60 | 60 |
| 61 // The {Number}ByteIntegersEightBitPrefix tests below test that | 61 // The {Number}ByteIntegersEightBitPrefix tests below test that |
| 62 // certain integers are encoded correctly with an 8-bit prefix in | 62 // certain integers are encoded correctly with an 8-bit prefix in |
| 63 // exactly {Number} bytes. | 63 // exactly {Number} bytes. |
| 64 | 64 |
| 65 TEST(HpackOutputStreamTest, OneByteIntegersEightBitPrefix) { | 65 TEST(HpackOutputStreamTest, OneByteIntegersEightBitPrefix) { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 EXPECT_EQ("\x1f\xe0\xff\xff\xff\x0f", EncodeUint32(5, 0xffffffff)); | 229 EXPECT_EQ("\x1f\xe0\xff\xff\xff\x0f", EncodeUint32(5, 0xffffffff)); |
| 230 EXPECT_EQ("\x0f\xf0\xff\xff\xff\x0f", EncodeUint32(4, 0xffffffff)); | 230 EXPECT_EQ("\x0f\xf0\xff\xff\xff\x0f", EncodeUint32(4, 0xffffffff)); |
| 231 EXPECT_EQ("\x07\xf8\xff\xff\xff\x0f", EncodeUint32(3, 0xffffffff)); | 231 EXPECT_EQ("\x07\xf8\xff\xff\xff\x0f", EncodeUint32(3, 0xffffffff)); |
| 232 EXPECT_EQ("\x03\xfc\xff\xff\xff\x0f", EncodeUint32(2, 0xffffffff)); | 232 EXPECT_EQ("\x03\xfc\xff\xff\xff\x0f", EncodeUint32(2, 0xffffffff)); |
| 233 EXPECT_EQ("\x01\xfe\xff\xff\xff\x0f", EncodeUint32(1, 0xffffffff)); | 233 EXPECT_EQ("\x01\xfe\xff\xff\xff\x0f", EncodeUint32(1, 0xffffffff)); |
| 234 } | 234 } |
| 235 | 235 |
| 236 // Test that encoding an integer with an N-bit prefix preserves the | 236 // Test that encoding an integer with an N-bit prefix preserves the |
| 237 // upper (8-N) bits of the first byte. | 237 // upper (8-N) bits of the first byte. |
| 238 TEST(HpackOutputStreamTest, AppendUint32PreservesUpperBits) { | 238 TEST(HpackOutputStreamTest, AppendUint32PreservesUpperBits) { |
| 239 HpackOutputStream output_stream(kuint32max); | 239 HpackOutputStream output_stream; |
| 240 output_stream.AppendBitsForTest(0x7f, 7); | 240 output_stream.AppendBits(0x7f, 7); |
| 241 output_stream.AppendUint32ForTest(0x01); | 241 output_stream.AppendUint32(0x01); |
| 242 string str; | 242 string str; |
| 243 output_stream.TakeString(&str); | 243 output_stream.TakeString(&str); |
| 244 EXPECT_EQ(string("\xff\x00", 2), str); | 244 EXPECT_EQ(string("\xff\x00", 2), str); |
| 245 } | 245 } |
| 246 | 246 |
| 247 // Test that encoding a string literal without huffman encoding | 247 TEST(HpackOutputStreamTest, AppendBytes) { |
| 248 // encodes the size first with a 7-bit prefix and then the bytes of | 248 HpackOutputStream output_stream; |
| 249 // the string. | |
| 250 TEST(HpackOutputStreamTest, AppendStringLiteralNoHuffmanEncoding) { | |
| 251 HpackOutputStream output_stream(kuint32max); | |
| 252 | 249 |
| 253 string literal(0x7f, 'x'); | 250 output_stream.AppendBytes("buffer1"); |
| 254 EXPECT_TRUE(output_stream.AppendStringLiteralForTest(literal)); | 251 output_stream.AppendBytes("buffer2"); |
| 255 | 252 |
| 256 string str; | 253 string str; |
| 257 output_stream.TakeString(&str); | 254 output_stream.TakeString(&str); |
| 258 EXPECT_EQ(string("\x7f\x00", 2) + literal, str); | 255 EXPECT_EQ("buffer1buffer2", str); |
| 259 } | |
| 260 | |
| 261 // Test that trying to encode a too-long string literal will fail. | |
| 262 TEST(HpackOutputStreamTest, AppendStringLiteralTooLong) { | |
| 263 HpackOutputStream output_stream(kuint32max - 1); | |
| 264 | |
| 265 EXPECT_FALSE(output_stream.AppendStringLiteralForTest( | |
| 266 base::StringPiece(NULL, kuint32max))); | |
| 267 } | |
| 268 | |
| 269 // Test that encoding an indexed header simply encodes the index. | |
| 270 TEST(HpackOutputStreamTest, AppendIndexedHeader) { | |
| 271 HpackOutputStream output_stream(kuint32max); | |
| 272 output_stream.AppendIndexedHeader(0xffffffff); | |
| 273 | |
| 274 string str; | |
| 275 output_stream.TakeString(&str); | |
| 276 EXPECT_EQ("\xff\x80\xff\xff\xff\x0f", str); | |
| 277 } | |
| 278 | |
| 279 // Test that encoding a literal header without indexing with a name | |
| 280 // encodes both the name and value as string literals. | |
| 281 TEST(HpackOutputStreamTest, AppendLiteralHeaderNoIndexingWithName) { | |
| 282 HpackOutputStream output_stream(kuint32max); | |
| 283 EXPECT_TRUE( | |
| 284 output_stream.AppendLiteralHeaderNoIndexingWithName("name", "value")); | |
| 285 | |
| 286 string str; | |
| 287 output_stream.TakeString(&str); | |
| 288 EXPECT_EQ("\x40\x04name\x05value", str); | |
| 289 } | |
| 290 | |
| 291 // Test that trying to encode a header with a too-long header name or | |
| 292 // value will fail. | |
| 293 TEST(HpackOutputStreamTest, AppendLiteralHeaderNoIndexingWithNameTooLong) { | |
| 294 { | |
| 295 HpackOutputStream output_stream(10); | |
| 296 EXPECT_FALSE(output_stream.AppendLiteralHeaderNoIndexingWithName( | |
| 297 "name", "too-long value")); | |
| 298 } | |
| 299 { | |
| 300 HpackOutputStream output_stream(10); | |
| 301 EXPECT_FALSE(output_stream.AppendLiteralHeaderNoIndexingWithName( | |
| 302 "too-long name", "value")); | |
| 303 } | |
| 304 } | 256 } |
| 305 | 257 |
| 306 } // namespace | 258 } // namespace |
| 307 | 259 |
| 308 } // namespace net | 260 } // namespace net |
| OLD | NEW |