| 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/hpack_output_stream.h" | 5 #include "net/spdy/hpack/hpack_output_stream.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 using std::string; | |
| 16 | |
| 17 // Make sure that AppendBits() appends bits starting from the most | 15 // Make sure that AppendBits() appends bits starting from the most |
| 18 // significant bit, and that it can handle crossing a byte boundary. | 16 // significant bit, and that it can handle crossing a byte boundary. |
| 19 TEST(HpackOutputStreamTest, AppendBits) { | 17 TEST(HpackOutputStreamTest, AppendBits) { |
| 20 HpackOutputStream output_stream; | 18 HpackOutputStream output_stream; |
| 21 string expected_str; | 19 SpdyString expected_str; |
| 22 | 20 |
| 23 output_stream.AppendBits(0x1, 1); | 21 output_stream.AppendBits(0x1, 1); |
| 24 expected_str.append(1, 0x00); | 22 expected_str.append(1, 0x00); |
| 25 *expected_str.rbegin() |= (0x1 << 7); | 23 *expected_str.rbegin() |= (0x1 << 7); |
| 26 | 24 |
| 27 output_stream.AppendBits(0x0, 1); | 25 output_stream.AppendBits(0x0, 1); |
| 28 | 26 |
| 29 output_stream.AppendBits(0x3, 2); | 27 output_stream.AppendBits(0x3, 2); |
| 30 *expected_str.rbegin() |= (0x3 << 4); | 28 *expected_str.rbegin() |= (0x3 << 4); |
| 31 | 29 |
| 32 output_stream.AppendBits(0x0, 2); | 30 output_stream.AppendBits(0x0, 2); |
| 33 | 31 |
| 34 // Byte-crossing append. | 32 // Byte-crossing append. |
| 35 output_stream.AppendBits(0x7, 3); | 33 output_stream.AppendBits(0x7, 3); |
| 36 *expected_str.rbegin() |= (0x7 >> 1); | 34 *expected_str.rbegin() |= (0x7 >> 1); |
| 37 expected_str.append(1, 0x00); | 35 expected_str.append(1, 0x00); |
| 38 *expected_str.rbegin() |= (0x7 << 7); | 36 *expected_str.rbegin() |= (0x7 << 7); |
| 39 | 37 |
| 40 output_stream.AppendBits(0x0, 7); | 38 output_stream.AppendBits(0x0, 7); |
| 41 | 39 |
| 42 string str; | 40 SpdyString str; |
| 43 output_stream.TakeString(&str); | 41 output_stream.TakeString(&str); |
| 44 EXPECT_EQ(expected_str, str); | 42 EXPECT_EQ(expected_str, str); |
| 45 } | 43 } |
| 46 | 44 |
| 47 // Utility function to return I as a string encoded with an N-bit | 45 // Utility function to return I as a string encoded with an N-bit |
| 48 // prefix. | 46 // prefix. |
| 49 string EncodeUint32(uint8_t N, uint32_t I) { | 47 SpdyString EncodeUint32(uint8_t N, uint32_t I) { |
| 50 HpackOutputStream output_stream; | 48 HpackOutputStream output_stream; |
| 51 if (N < 8) { | 49 if (N < 8) { |
| 52 output_stream.AppendBits(0x00, 8 - N); | 50 output_stream.AppendBits(0x00, 8 - N); |
| 53 } | 51 } |
| 54 output_stream.AppendUint32(I); | 52 output_stream.AppendUint32(I); |
| 55 string str; | 53 SpdyString str; |
| 56 output_stream.TakeString(&str); | 54 output_stream.TakeString(&str); |
| 57 return str; | 55 return str; |
| 58 } | 56 } |
| 59 | 57 |
| 60 // The {Number}ByteIntegersEightBitPrefix tests below test that | 58 // The {Number}ByteIntegersEightBitPrefix tests below test that |
| 61 // certain integers are encoded correctly with an 8-bit prefix in | 59 // certain integers are encoded correctly with an 8-bit prefix in |
| 62 // exactly {Number} bytes. | 60 // exactly {Number} bytes. |
| 63 | 61 |
| 64 TEST(HpackOutputStreamTest, OneByteIntegersEightBitPrefix) { | 62 TEST(HpackOutputStreamTest, OneByteIntegersEightBitPrefix) { |
| 65 // Minimum. | 63 // Minimum. |
| 66 EXPECT_EQ(string("\x00", 1), EncodeUint32(8, 0x00)); | 64 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(8, 0x00)); |
| 67 EXPECT_EQ("\x7f", EncodeUint32(8, 0x7f)); | 65 EXPECT_EQ("\x7f", EncodeUint32(8, 0x7f)); |
| 68 // Maximum. | 66 // Maximum. |
| 69 EXPECT_EQ("\xfe", EncodeUint32(8, 0xfe)); | 67 EXPECT_EQ("\xfe", EncodeUint32(8, 0xfe)); |
| 70 } | 68 } |
| 71 | 69 |
| 72 TEST(HpackOutputStreamTest, TwoByteIntegersEightBitPrefix) { | 70 TEST(HpackOutputStreamTest, TwoByteIntegersEightBitPrefix) { |
| 73 // Minimum. | 71 // Minimum. |
| 74 EXPECT_EQ(string("\xff\x00", 2), EncodeUint32(8, 0xff)); | 72 EXPECT_EQ(SpdyString("\xff\x00", 2), EncodeUint32(8, 0xff)); |
| 75 EXPECT_EQ("\xff\x01", EncodeUint32(8, 0x0100)); | 73 EXPECT_EQ("\xff\x01", EncodeUint32(8, 0x0100)); |
| 76 // Maximum. | 74 // Maximum. |
| 77 EXPECT_EQ("\xff\x7f", EncodeUint32(8, 0x017e)); | 75 EXPECT_EQ("\xff\x7f", EncodeUint32(8, 0x017e)); |
| 78 } | 76 } |
| 79 | 77 |
| 80 TEST(HpackOutputStreamTest, ThreeByteIntegersEightBitPrefix) { | 78 TEST(HpackOutputStreamTest, ThreeByteIntegersEightBitPrefix) { |
| 81 // Minimum. | 79 // Minimum. |
| 82 EXPECT_EQ("\xff\x80\x01", EncodeUint32(8, 0x017f)); | 80 EXPECT_EQ("\xff\x80\x01", EncodeUint32(8, 0x017f)); |
| 83 EXPECT_EQ("\xff\x80\x1e", EncodeUint32(8, 0x0fff)); | 81 EXPECT_EQ("\xff\x80\x1e", EncodeUint32(8, 0x0fff)); |
| 84 // Maximum. | 82 // Maximum. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 107 // Maximum. | 105 // Maximum. |
| 108 EXPECT_EQ("\xff\x80\xfe\xff\xff\x0f", EncodeUint32(8, 0xffffffff)); | 106 EXPECT_EQ("\xff\x80\xfe\xff\xff\x0f", EncodeUint32(8, 0xffffffff)); |
| 109 } | 107 } |
| 110 | 108 |
| 111 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that | 109 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that |
| 112 // certain integers are encoded correctly with an N-bit prefix in | 110 // certain integers are encoded correctly with an N-bit prefix in |
| 113 // exactly {Number} bytes for N in {1, 2, ..., 7}. | 111 // exactly {Number} bytes for N in {1, 2, ..., 7}. |
| 114 | 112 |
| 115 TEST(HpackOutputStreamTest, OneByteIntegersOneToSevenBitPrefixes) { | 113 TEST(HpackOutputStreamTest, OneByteIntegersOneToSevenBitPrefixes) { |
| 116 // Minimums. | 114 // Minimums. |
| 117 EXPECT_EQ(string("\x00", 1), EncodeUint32(7, 0x00)); | 115 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(7, 0x00)); |
| 118 EXPECT_EQ(string("\x00", 1), EncodeUint32(6, 0x00)); | 116 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(6, 0x00)); |
| 119 EXPECT_EQ(string("\x00", 1), EncodeUint32(5, 0x00)); | 117 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(5, 0x00)); |
| 120 EXPECT_EQ(string("\x00", 1), EncodeUint32(4, 0x00)); | 118 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(4, 0x00)); |
| 121 EXPECT_EQ(string("\x00", 1), EncodeUint32(3, 0x00)); | 119 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(3, 0x00)); |
| 122 EXPECT_EQ(string("\x00", 1), EncodeUint32(2, 0x00)); | 120 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(2, 0x00)); |
| 123 EXPECT_EQ(string("\x00", 1), EncodeUint32(1, 0x00)); | 121 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(1, 0x00)); |
| 124 | 122 |
| 125 // Maximums. | 123 // Maximums. |
| 126 EXPECT_EQ("\x7e", EncodeUint32(7, 0x7e)); | 124 EXPECT_EQ("\x7e", EncodeUint32(7, 0x7e)); |
| 127 EXPECT_EQ("\x3e", EncodeUint32(6, 0x3e)); | 125 EXPECT_EQ("\x3e", EncodeUint32(6, 0x3e)); |
| 128 EXPECT_EQ("\x1e", EncodeUint32(5, 0x1e)); | 126 EXPECT_EQ("\x1e", EncodeUint32(5, 0x1e)); |
| 129 EXPECT_EQ("\x0e", EncodeUint32(4, 0x0e)); | 127 EXPECT_EQ("\x0e", EncodeUint32(4, 0x0e)); |
| 130 EXPECT_EQ("\x06", EncodeUint32(3, 0x06)); | 128 EXPECT_EQ("\x06", EncodeUint32(3, 0x06)); |
| 131 EXPECT_EQ("\x02", EncodeUint32(2, 0x02)); | 129 EXPECT_EQ("\x02", EncodeUint32(2, 0x02)); |
| 132 EXPECT_EQ(string("\x00", 1), EncodeUint32(1, 0x00)); | 130 EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(1, 0x00)); |
| 133 } | 131 } |
| 134 | 132 |
| 135 TEST(HpackOutputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) { | 133 TEST(HpackOutputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) { |
| 136 // Minimums. | 134 // Minimums. |
| 137 EXPECT_EQ(string("\x7f\x00", 2), EncodeUint32(7, 0x7f)); | 135 EXPECT_EQ(SpdyString("\x7f\x00", 2), EncodeUint32(7, 0x7f)); |
| 138 EXPECT_EQ(string("\x3f\x00", 2), EncodeUint32(6, 0x3f)); | 136 EXPECT_EQ(SpdyString("\x3f\x00", 2), EncodeUint32(6, 0x3f)); |
| 139 EXPECT_EQ(string("\x1f\x00", 2), EncodeUint32(5, 0x1f)); | 137 EXPECT_EQ(SpdyString("\x1f\x00", 2), EncodeUint32(5, 0x1f)); |
| 140 EXPECT_EQ(string("\x0f\x00", 2), EncodeUint32(4, 0x0f)); | 138 EXPECT_EQ(SpdyString("\x0f\x00", 2), EncodeUint32(4, 0x0f)); |
| 141 EXPECT_EQ(string("\x07\x00", 2), EncodeUint32(3, 0x07)); | 139 EXPECT_EQ(SpdyString("\x07\x00", 2), EncodeUint32(3, 0x07)); |
| 142 EXPECT_EQ(string("\x03\x00", 2), EncodeUint32(2, 0x03)); | 140 EXPECT_EQ(SpdyString("\x03\x00", 2), EncodeUint32(2, 0x03)); |
| 143 EXPECT_EQ(string("\x01\x00", 2), EncodeUint32(1, 0x01)); | 141 EXPECT_EQ(SpdyString("\x01\x00", 2), EncodeUint32(1, 0x01)); |
| 144 | 142 |
| 145 // Maximums. | 143 // Maximums. |
| 146 EXPECT_EQ("\x7f\x7f", EncodeUint32(7, 0xfe)); | 144 EXPECT_EQ("\x7f\x7f", EncodeUint32(7, 0xfe)); |
| 147 EXPECT_EQ("\x3f\x7f", EncodeUint32(6, 0xbe)); | 145 EXPECT_EQ("\x3f\x7f", EncodeUint32(6, 0xbe)); |
| 148 EXPECT_EQ("\x1f\x7f", EncodeUint32(5, 0x9e)); | 146 EXPECT_EQ("\x1f\x7f", EncodeUint32(5, 0x9e)); |
| 149 EXPECT_EQ("\x0f\x7f", EncodeUint32(4, 0x8e)); | 147 EXPECT_EQ("\x0f\x7f", EncodeUint32(4, 0x8e)); |
| 150 EXPECT_EQ("\x07\x7f", EncodeUint32(3, 0x86)); | 148 EXPECT_EQ("\x07\x7f", EncodeUint32(3, 0x86)); |
| 151 EXPECT_EQ("\x03\x7f", EncodeUint32(2, 0x82)); | 149 EXPECT_EQ("\x03\x7f", EncodeUint32(2, 0x82)); |
| 152 EXPECT_EQ("\x01\x7f", EncodeUint32(1, 0x80)); | 150 EXPECT_EQ("\x01\x7f", EncodeUint32(1, 0x80)); |
| 153 } | 151 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 EXPECT_EQ("\x03\xfc\xff\xff\xff\x0f", EncodeUint32(2, 0xffffffff)); | 229 EXPECT_EQ("\x03\xfc\xff\xff\xff\x0f", EncodeUint32(2, 0xffffffff)); |
| 232 EXPECT_EQ("\x01\xfe\xff\xff\xff\x0f", EncodeUint32(1, 0xffffffff)); | 230 EXPECT_EQ("\x01\xfe\xff\xff\xff\x0f", EncodeUint32(1, 0xffffffff)); |
| 233 } | 231 } |
| 234 | 232 |
| 235 // Test that encoding an integer with an N-bit prefix preserves the | 233 // Test that encoding an integer with an N-bit prefix preserves the |
| 236 // upper (8-N) bits of the first byte. | 234 // upper (8-N) bits of the first byte. |
| 237 TEST(HpackOutputStreamTest, AppendUint32PreservesUpperBits) { | 235 TEST(HpackOutputStreamTest, AppendUint32PreservesUpperBits) { |
| 238 HpackOutputStream output_stream; | 236 HpackOutputStream output_stream; |
| 239 output_stream.AppendBits(0x7f, 7); | 237 output_stream.AppendBits(0x7f, 7); |
| 240 output_stream.AppendUint32(0x01); | 238 output_stream.AppendUint32(0x01); |
| 241 string str; | 239 SpdyString str; |
| 242 output_stream.TakeString(&str); | 240 output_stream.TakeString(&str); |
| 243 EXPECT_EQ(string("\xff\x00", 2), str); | 241 EXPECT_EQ(SpdyString("\xff\x00", 2), str); |
| 244 } | 242 } |
| 245 | 243 |
| 246 TEST(HpackOutputStreamTest, AppendBytes) { | 244 TEST(HpackOutputStreamTest, AppendBytes) { |
| 247 HpackOutputStream output_stream; | 245 HpackOutputStream output_stream; |
| 248 | 246 |
| 249 output_stream.AppendBytes("buffer1"); | 247 output_stream.AppendBytes("buffer1"); |
| 250 output_stream.AppendBytes("buffer2"); | 248 output_stream.AppendBytes("buffer2"); |
| 251 | 249 |
| 252 string str; | 250 SpdyString str; |
| 253 output_stream.TakeString(&str); | 251 output_stream.TakeString(&str); |
| 254 EXPECT_EQ("buffer1buffer2", str); | 252 EXPECT_EQ("buffer1buffer2", str); |
| 255 } | 253 } |
| 256 | 254 |
| 257 TEST(HpackOutputStreamTest, BoundedTakeString) { | 255 TEST(HpackOutputStreamTest, BoundedTakeString) { |
| 258 HpackOutputStream output_stream; | 256 HpackOutputStream output_stream; |
| 259 | 257 |
| 260 output_stream.AppendBytes("buffer12"); | 258 output_stream.AppendBytes("buffer12"); |
| 261 output_stream.AppendBytes("buffer456"); | 259 output_stream.AppendBytes("buffer456"); |
| 262 | 260 |
| 263 string str; | 261 SpdyString str; |
| 264 output_stream.BoundedTakeString(9, &str); | 262 output_stream.BoundedTakeString(9, &str); |
| 265 EXPECT_EQ("buffer12b", str); | 263 EXPECT_EQ("buffer12b", str); |
| 266 | 264 |
| 267 output_stream.AppendBits(0x7f, 7); | 265 output_stream.AppendBits(0x7f, 7); |
| 268 output_stream.AppendUint32(0x11); | 266 output_stream.AppendUint32(0x11); |
| 269 output_stream.BoundedTakeString(9, &str); | 267 output_stream.BoundedTakeString(9, &str); |
| 270 EXPECT_EQ("uffer456\xff", str); | 268 EXPECT_EQ("uffer456\xff", str); |
| 271 | 269 |
| 272 output_stream.BoundedTakeString(9, &str); | 270 output_stream.BoundedTakeString(9, &str); |
| 273 EXPECT_EQ("\x10", str); | 271 EXPECT_EQ("\x10", str); |
| 274 } | 272 } |
| 275 | 273 |
| 276 } // namespace | 274 } // namespace |
| 277 | 275 |
| 278 } // namespace net | 276 } // namespace net |
| OLD | NEW |