QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/spdy/core/hpack/hpack_output_stream.h" |
| 6 | |
| 7 | #include <cstddef> |
| 8 | |
danzh | 8f3a576 | 2019-06-25 13:43:51 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_test.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 10 | |
| 11 | namespace spdy { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // Make sure that AppendBits() appends bits starting from the most |
| 16 | // significant bit, and that it can handle crossing a byte boundary. |
| 17 | TEST(HpackOutputStreamTest, AppendBits) { |
| 18 | HpackOutputStream output_stream; |
| 19 | SpdyString expected_str; |
| 20 | |
| 21 | output_stream.AppendBits(0x1, 1); |
| 22 | expected_str.append(1, 0x00); |
| 23 | expected_str.back() |= (0x1 << 7); |
| 24 | |
| 25 | output_stream.AppendBits(0x0, 1); |
| 26 | |
| 27 | output_stream.AppendBits(0x3, 2); |
| 28 | *expected_str.rbegin() |= (0x3 << 4); |
| 29 | |
| 30 | output_stream.AppendBits(0x0, 2); |
| 31 | |
| 32 | // Byte-crossing append. |
| 33 | output_stream.AppendBits(0x7, 3); |
| 34 | *expected_str.rbegin() |= (0x7 >> 1); |
| 35 | expected_str.append(1, 0x00); |
| 36 | expected_str.back() |= (0x7 << 7); |
| 37 | |
| 38 | output_stream.AppendBits(0x0, 7); |
| 39 | |
| 40 | SpdyString str; |
| 41 | output_stream.TakeString(&str); |
| 42 | EXPECT_EQ(expected_str, str); |
| 43 | } |
| 44 | |
| 45 | // Utility function to return I as a string encoded with an N-bit |
| 46 | // prefix. |
| 47 | SpdyString EncodeUint32(uint8_t N, uint32_t I) { |
| 48 | HpackOutputStream output_stream; |
| 49 | if (N < 8) { |
| 50 | output_stream.AppendBits(0x00, 8 - N); |
| 51 | } |
| 52 | output_stream.AppendUint32(I); |
| 53 | SpdyString str; |
| 54 | output_stream.TakeString(&str); |
| 55 | return str; |
| 56 | } |
| 57 | |
| 58 | // The {Number}ByteIntegersEightBitPrefix tests below test that |
| 59 | // certain integers are encoded correctly with an 8-bit prefix in |
| 60 | // exactly {Number} bytes. |
| 61 | |
| 62 | TEST(HpackOutputStreamTest, OneByteIntegersEightBitPrefix) { |
| 63 | // Minimum. |
| 64 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(8, 0x00)); |
| 65 | EXPECT_EQ("\x7f", EncodeUint32(8, 0x7f)); |
| 66 | // Maximum. |
| 67 | EXPECT_EQ("\xfe", EncodeUint32(8, 0xfe)); |
| 68 | } |
| 69 | |
| 70 | TEST(HpackOutputStreamTest, TwoByteIntegersEightBitPrefix) { |
| 71 | // Minimum. |
| 72 | EXPECT_EQ(SpdyString("\xff\x00", 2), EncodeUint32(8, 0xff)); |
| 73 | EXPECT_EQ("\xff\x01", EncodeUint32(8, 0x0100)); |
| 74 | // Maximum. |
| 75 | EXPECT_EQ("\xff\x7f", EncodeUint32(8, 0x017e)); |
| 76 | } |
| 77 | |
| 78 | TEST(HpackOutputStreamTest, ThreeByteIntegersEightBitPrefix) { |
| 79 | // Minimum. |
| 80 | EXPECT_EQ("\xff\x80\x01", EncodeUint32(8, 0x017f)); |
| 81 | EXPECT_EQ("\xff\x80\x1e", EncodeUint32(8, 0x0fff)); |
| 82 | // Maximum. |
| 83 | EXPECT_EQ("\xff\xff\x7f", EncodeUint32(8, 0x40fe)); |
| 84 | } |
| 85 | |
| 86 | TEST(HpackOutputStreamTest, FourByteIntegersEightBitPrefix) { |
| 87 | // Minimum. |
| 88 | EXPECT_EQ("\xff\x80\x80\x01", EncodeUint32(8, 0x40ff)); |
| 89 | EXPECT_EQ("\xff\x80\xfe\x03", EncodeUint32(8, 0xffff)); |
| 90 | // Maximum. |
| 91 | EXPECT_EQ("\xff\xff\xff\x7f", EncodeUint32(8, 0x002000fe)); |
| 92 | } |
| 93 | |
| 94 | TEST(HpackOutputStreamTest, FiveByteIntegersEightBitPrefix) { |
| 95 | // Minimum. |
| 96 | EXPECT_EQ("\xff\x80\x80\x80\x01", EncodeUint32(8, 0x002000ff)); |
| 97 | EXPECT_EQ("\xff\x80\xfe\xff\x07", EncodeUint32(8, 0x00ffffff)); |
| 98 | // Maximum. |
| 99 | EXPECT_EQ("\xff\xff\xff\xff\x7f", EncodeUint32(8, 0x100000fe)); |
| 100 | } |
| 101 | |
| 102 | TEST(HpackOutputStreamTest, SixByteIntegersEightBitPrefix) { |
| 103 | // Minimum. |
| 104 | EXPECT_EQ("\xff\x80\x80\x80\x80\x01", EncodeUint32(8, 0x100000ff)); |
| 105 | // Maximum. |
| 106 | EXPECT_EQ("\xff\x80\xfe\xff\xff\x0f", EncodeUint32(8, 0xffffffff)); |
| 107 | } |
| 108 | |
| 109 | // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that |
| 110 | // certain integers are encoded correctly with an N-bit prefix in |
| 111 | // exactly {Number} bytes for N in {1, 2, ..., 7}. |
| 112 | |
| 113 | TEST(HpackOutputStreamTest, OneByteIntegersOneToSevenBitPrefixes) { |
| 114 | // Minimums. |
| 115 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(7, 0x00)); |
| 116 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(6, 0x00)); |
| 117 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(5, 0x00)); |
| 118 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(4, 0x00)); |
| 119 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(3, 0x00)); |
| 120 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(2, 0x00)); |
| 121 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(1, 0x00)); |
| 122 | |
| 123 | // Maximums. |
| 124 | EXPECT_EQ("\x7e", EncodeUint32(7, 0x7e)); |
| 125 | EXPECT_EQ("\x3e", EncodeUint32(6, 0x3e)); |
| 126 | EXPECT_EQ("\x1e", EncodeUint32(5, 0x1e)); |
| 127 | EXPECT_EQ("\x0e", EncodeUint32(4, 0x0e)); |
| 128 | EXPECT_EQ("\x06", EncodeUint32(3, 0x06)); |
| 129 | EXPECT_EQ("\x02", EncodeUint32(2, 0x02)); |
| 130 | EXPECT_EQ(SpdyString("\x00", 1), EncodeUint32(1, 0x00)); |
| 131 | } |
| 132 | |
| 133 | TEST(HpackOutputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) { |
| 134 | // Minimums. |
| 135 | EXPECT_EQ(SpdyString("\x7f\x00", 2), EncodeUint32(7, 0x7f)); |
| 136 | EXPECT_EQ(SpdyString("\x3f\x00", 2), EncodeUint32(6, 0x3f)); |
| 137 | EXPECT_EQ(SpdyString("\x1f\x00", 2), EncodeUint32(5, 0x1f)); |
| 138 | EXPECT_EQ(SpdyString("\x0f\x00", 2), EncodeUint32(4, 0x0f)); |
| 139 | EXPECT_EQ(SpdyString("\x07\x00", 2), EncodeUint32(3, 0x07)); |
| 140 | EXPECT_EQ(SpdyString("\x03\x00", 2), EncodeUint32(2, 0x03)); |
| 141 | EXPECT_EQ(SpdyString("\x01\x00", 2), EncodeUint32(1, 0x01)); |
| 142 | |
| 143 | // Maximums. |
| 144 | EXPECT_EQ("\x7f\x7f", EncodeUint32(7, 0xfe)); |
| 145 | EXPECT_EQ("\x3f\x7f", EncodeUint32(6, 0xbe)); |
| 146 | EXPECT_EQ("\x1f\x7f", EncodeUint32(5, 0x9e)); |
| 147 | EXPECT_EQ("\x0f\x7f", EncodeUint32(4, 0x8e)); |
| 148 | EXPECT_EQ("\x07\x7f", EncodeUint32(3, 0x86)); |
| 149 | EXPECT_EQ("\x03\x7f", EncodeUint32(2, 0x82)); |
| 150 | EXPECT_EQ("\x01\x7f", EncodeUint32(1, 0x80)); |
| 151 | } |
| 152 | |
| 153 | TEST(HpackOutputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) { |
| 154 | // Minimums. |
| 155 | EXPECT_EQ("\x7f\x80\x01", EncodeUint32(7, 0xff)); |
| 156 | EXPECT_EQ("\x3f\x80\x01", EncodeUint32(6, 0xbf)); |
| 157 | EXPECT_EQ("\x1f\x80\x01", EncodeUint32(5, 0x9f)); |
| 158 | EXPECT_EQ("\x0f\x80\x01", EncodeUint32(4, 0x8f)); |
| 159 | EXPECT_EQ("\x07\x80\x01", EncodeUint32(3, 0x87)); |
| 160 | EXPECT_EQ("\x03\x80\x01", EncodeUint32(2, 0x83)); |
| 161 | EXPECT_EQ("\x01\x80\x01", EncodeUint32(1, 0x81)); |
| 162 | |
| 163 | // Maximums. |
| 164 | EXPECT_EQ("\x7f\xff\x7f", EncodeUint32(7, 0x407e)); |
| 165 | EXPECT_EQ("\x3f\xff\x7f", EncodeUint32(6, 0x403e)); |
| 166 | EXPECT_EQ("\x1f\xff\x7f", EncodeUint32(5, 0x401e)); |
| 167 | EXPECT_EQ("\x0f\xff\x7f", EncodeUint32(4, 0x400e)); |
| 168 | EXPECT_EQ("\x07\xff\x7f", EncodeUint32(3, 0x4006)); |
| 169 | EXPECT_EQ("\x03\xff\x7f", EncodeUint32(2, 0x4002)); |
| 170 | EXPECT_EQ("\x01\xff\x7f", EncodeUint32(1, 0x4000)); |
| 171 | } |
| 172 | |
| 173 | TEST(HpackOutputStreamTest, FourByteIntegersOneToSevenBitPrefixes) { |
| 174 | // Minimums. |
| 175 | EXPECT_EQ("\x7f\x80\x80\x01", EncodeUint32(7, 0x407f)); |
| 176 | EXPECT_EQ("\x3f\x80\x80\x01", EncodeUint32(6, 0x403f)); |
| 177 | EXPECT_EQ("\x1f\x80\x80\x01", EncodeUint32(5, 0x401f)); |
| 178 | EXPECT_EQ("\x0f\x80\x80\x01", EncodeUint32(4, 0x400f)); |
| 179 | EXPECT_EQ("\x07\x80\x80\x01", EncodeUint32(3, 0x4007)); |
| 180 | EXPECT_EQ("\x03\x80\x80\x01", EncodeUint32(2, 0x4003)); |
| 181 | EXPECT_EQ("\x01\x80\x80\x01", EncodeUint32(1, 0x4001)); |
| 182 | |
| 183 | // Maximums. |
| 184 | EXPECT_EQ("\x7f\xff\xff\x7f", EncodeUint32(7, 0x20007e)); |
| 185 | EXPECT_EQ("\x3f\xff\xff\x7f", EncodeUint32(6, 0x20003e)); |
| 186 | EXPECT_EQ("\x1f\xff\xff\x7f", EncodeUint32(5, 0x20001e)); |
| 187 | EXPECT_EQ("\x0f\xff\xff\x7f", EncodeUint32(4, 0x20000e)); |
| 188 | EXPECT_EQ("\x07\xff\xff\x7f", EncodeUint32(3, 0x200006)); |
| 189 | EXPECT_EQ("\x03\xff\xff\x7f", EncodeUint32(2, 0x200002)); |
| 190 | EXPECT_EQ("\x01\xff\xff\x7f", EncodeUint32(1, 0x200000)); |
| 191 | } |
| 192 | |
| 193 | TEST(HpackOutputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) { |
| 194 | // Minimums. |
| 195 | EXPECT_EQ("\x7f\x80\x80\x80\x01", EncodeUint32(7, 0x20007f)); |
| 196 | EXPECT_EQ("\x3f\x80\x80\x80\x01", EncodeUint32(6, 0x20003f)); |
| 197 | EXPECT_EQ("\x1f\x80\x80\x80\x01", EncodeUint32(5, 0x20001f)); |
| 198 | EXPECT_EQ("\x0f\x80\x80\x80\x01", EncodeUint32(4, 0x20000f)); |
| 199 | EXPECT_EQ("\x07\x80\x80\x80\x01", EncodeUint32(3, 0x200007)); |
| 200 | EXPECT_EQ("\x03\x80\x80\x80\x01", EncodeUint32(2, 0x200003)); |
| 201 | EXPECT_EQ("\x01\x80\x80\x80\x01", EncodeUint32(1, 0x200001)); |
| 202 | |
| 203 | // Maximums. |
| 204 | EXPECT_EQ("\x7f\xff\xff\xff\x7f", EncodeUint32(7, 0x1000007e)); |
| 205 | EXPECT_EQ("\x3f\xff\xff\xff\x7f", EncodeUint32(6, 0x1000003e)); |
| 206 | EXPECT_EQ("\x1f\xff\xff\xff\x7f", EncodeUint32(5, 0x1000001e)); |
| 207 | EXPECT_EQ("\x0f\xff\xff\xff\x7f", EncodeUint32(4, 0x1000000e)); |
| 208 | EXPECT_EQ("\x07\xff\xff\xff\x7f", EncodeUint32(3, 0x10000006)); |
| 209 | EXPECT_EQ("\x03\xff\xff\xff\x7f", EncodeUint32(2, 0x10000002)); |
| 210 | EXPECT_EQ("\x01\xff\xff\xff\x7f", EncodeUint32(1, 0x10000000)); |
| 211 | } |
| 212 | |
| 213 | TEST(HpackOutputStreamTest, SixByteIntegersOneToSevenBitPrefixes) { |
| 214 | // Minimums. |
| 215 | EXPECT_EQ("\x7f\x80\x80\x80\x80\x01", EncodeUint32(7, 0x1000007f)); |
| 216 | EXPECT_EQ("\x3f\x80\x80\x80\x80\x01", EncodeUint32(6, 0x1000003f)); |
| 217 | EXPECT_EQ("\x1f\x80\x80\x80\x80\x01", EncodeUint32(5, 0x1000001f)); |
| 218 | EXPECT_EQ("\x0f\x80\x80\x80\x80\x01", EncodeUint32(4, 0x1000000f)); |
| 219 | EXPECT_EQ("\x07\x80\x80\x80\x80\x01", EncodeUint32(3, 0x10000007)); |
| 220 | EXPECT_EQ("\x03\x80\x80\x80\x80\x01", EncodeUint32(2, 0x10000003)); |
| 221 | EXPECT_EQ("\x01\x80\x80\x80\x80\x01", EncodeUint32(1, 0x10000001)); |
| 222 | |
| 223 | // Maximums. |
| 224 | EXPECT_EQ("\x7f\x80\xff\xff\xff\x0f", EncodeUint32(7, 0xffffffff)); |
| 225 | EXPECT_EQ("\x3f\xc0\xff\xff\xff\x0f", EncodeUint32(6, 0xffffffff)); |
| 226 | EXPECT_EQ("\x1f\xe0\xff\xff\xff\x0f", EncodeUint32(5, 0xffffffff)); |
| 227 | EXPECT_EQ("\x0f\xf0\xff\xff\xff\x0f", EncodeUint32(4, 0xffffffff)); |
| 228 | EXPECT_EQ("\x07\xf8\xff\xff\xff\x0f", EncodeUint32(3, 0xffffffff)); |
| 229 | EXPECT_EQ("\x03\xfc\xff\xff\xff\x0f", EncodeUint32(2, 0xffffffff)); |
| 230 | EXPECT_EQ("\x01\xfe\xff\xff\xff\x0f", EncodeUint32(1, 0xffffffff)); |
| 231 | } |
| 232 | |
| 233 | // Test that encoding an integer with an N-bit prefix preserves the |
| 234 | // upper (8-N) bits of the first byte. |
| 235 | TEST(HpackOutputStreamTest, AppendUint32PreservesUpperBits) { |
| 236 | HpackOutputStream output_stream; |
| 237 | output_stream.AppendBits(0x7f, 7); |
| 238 | output_stream.AppendUint32(0x01); |
| 239 | SpdyString str; |
| 240 | output_stream.TakeString(&str); |
| 241 | EXPECT_EQ(SpdyString("\xff\x00", 2), str); |
| 242 | } |
| 243 | |
| 244 | TEST(HpackOutputStreamTest, AppendBytes) { |
| 245 | HpackOutputStream output_stream; |
| 246 | |
| 247 | output_stream.AppendBytes("buffer1"); |
| 248 | output_stream.AppendBytes("buffer2"); |
| 249 | |
| 250 | SpdyString str; |
| 251 | output_stream.TakeString(&str); |
| 252 | EXPECT_EQ("buffer1buffer2", str); |
| 253 | } |
| 254 | |
| 255 | TEST(HpackOutputStreamTest, BoundedTakeString) { |
| 256 | HpackOutputStream output_stream; |
| 257 | |
| 258 | output_stream.AppendBytes("buffer12"); |
| 259 | output_stream.AppendBytes("buffer456"); |
| 260 | |
| 261 | SpdyString str; |
| 262 | output_stream.BoundedTakeString(9, &str); |
| 263 | EXPECT_EQ("buffer12b", str); |
| 264 | |
| 265 | output_stream.AppendBits(0x7f, 7); |
| 266 | output_stream.AppendUint32(0x11); |
| 267 | output_stream.BoundedTakeString(9, &str); |
| 268 | EXPECT_EQ("uffer456\xff", str); |
| 269 | |
| 270 | output_stream.BoundedTakeString(9, &str); |
| 271 | EXPECT_EQ("\x10", str); |
| 272 | } |
| 273 | |
| 274 | } // namespace |
| 275 | |
| 276 | } // namespace spdy |