QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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/quic/core/quic_data_writer.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | |
| 9 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_data_reader.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 15 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 16 | |
| 17 | namespace quic { |
| 18 | namespace test { |
| 19 | namespace { |
| 20 | |
| 21 | char* AsChars(unsigned char* data) { |
| 22 | return reinterpret_cast<char*>(data); |
| 23 | } |
| 24 | |
| 25 | struct TestParams { |
| 26 | explicit TestParams(Endianness endianness) : endianness(endianness) {} |
| 27 | |
| 28 | Endianness endianness; |
| 29 | }; |
| 30 | |
| 31 | std::vector<TestParams> GetTestParams() { |
| 32 | std::vector<TestParams> params; |
| 33 | for (Endianness endianness : {NETWORK_BYTE_ORDER, HOST_BYTE_ORDER}) { |
| 34 | params.push_back(TestParams(endianness)); |
| 35 | } |
| 36 | return params; |
| 37 | } |
| 38 | |
| 39 | class QuicDataWriterTest : public QuicTestWithParam<TestParams> {}; |
| 40 | |
| 41 | INSTANTIATE_TEST_SUITE_P(QuicDataWriterTests, QuicDataWriterTest, |
| 42 | ::testing::ValuesIn(GetTestParams())); |
| 43 | |
| 44 | TEST_P(QuicDataWriterTest, SanityCheckUFloat16Consts) { |
| 45 | // Check the arithmetic on the constants - otherwise the values below make |
| 46 | // no sense. |
| 47 | EXPECT_EQ(30, kUFloat16MaxExponent); |
| 48 | EXPECT_EQ(11, kUFloat16MantissaBits); |
| 49 | EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); |
| 50 | EXPECT_EQ(UINT64_C(0x3FFC0000000), kUFloat16MaxValue); |
| 51 | } |
| 52 | |
| 53 | TEST_P(QuicDataWriterTest, WriteUFloat16) { |
| 54 | struct TestCase { |
| 55 | uint64_t decoded; |
| 56 | uint16_t encoded; |
| 57 | }; |
| 58 | TestCase test_cases[] = { |
| 59 | // Small numbers represent themselves. |
| 60 | {0, 0}, |
| 61 | {1, 1}, |
| 62 | {2, 2}, |
| 63 | {3, 3}, |
| 64 | {4, 4}, |
| 65 | {5, 5}, |
| 66 | {6, 6}, |
| 67 | {7, 7}, |
| 68 | {15, 15}, |
| 69 | {31, 31}, |
| 70 | {42, 42}, |
| 71 | {123, 123}, |
| 72 | {1234, 1234}, |
| 73 | // Check transition through 2^11. |
| 74 | {2046, 2046}, |
| 75 | {2047, 2047}, |
| 76 | {2048, 2048}, |
| 77 | {2049, 2049}, |
| 78 | // Running out of mantissa at 2^12. |
| 79 | {4094, 4094}, |
| 80 | {4095, 4095}, |
| 81 | {4096, 4096}, |
| 82 | {4097, 4096}, |
| 83 | {4098, 4097}, |
| 84 | {4099, 4097}, |
| 85 | {4100, 4098}, |
| 86 | {4101, 4098}, |
| 87 | // Check transition through 2^13. |
| 88 | {8190, 6143}, |
| 89 | {8191, 6143}, |
| 90 | {8192, 6144}, |
| 91 | {8193, 6144}, |
| 92 | {8194, 6144}, |
| 93 | {8195, 6144}, |
| 94 | {8196, 6145}, |
| 95 | {8197, 6145}, |
| 96 | // Half-way through the exponents. |
| 97 | {0x7FF8000, 0x87FF}, |
| 98 | {0x7FFFFFF, 0x87FF}, |
| 99 | {0x8000000, 0x8800}, |
| 100 | {0xFFF0000, 0x8FFF}, |
| 101 | {0xFFFFFFF, 0x8FFF}, |
| 102 | {0x10000000, 0x9000}, |
| 103 | // Transition into the largest exponent. |
| 104 | {0x1FFFFFFFFFE, 0xF7FF}, |
| 105 | {0x1FFFFFFFFFF, 0xF7FF}, |
| 106 | {0x20000000000, 0xF800}, |
| 107 | {0x20000000001, 0xF800}, |
| 108 | {0x2003FFFFFFE, 0xF800}, |
| 109 | {0x2003FFFFFFF, 0xF800}, |
| 110 | {0x20040000000, 0xF801}, |
| 111 | {0x20040000001, 0xF801}, |
| 112 | // Transition into the max value and clamping. |
| 113 | {0x3FF80000000, 0xFFFE}, |
| 114 | {0x3FFBFFFFFFF, 0xFFFE}, |
| 115 | {0x3FFC0000000, 0xFFFF}, |
| 116 | {0x3FFC0000001, 0xFFFF}, |
| 117 | {0x3FFFFFFFFFF, 0xFFFF}, |
| 118 | {0x40000000000, 0xFFFF}, |
| 119 | {0xFFFFFFFFFFFFFFFF, 0xFFFF}, |
| 120 | }; |
| 121 | int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 122 | |
| 123 | for (int i = 0; i < num_test_cases; ++i) { |
| 124 | char buffer[2]; |
| 125 | QuicDataWriter writer(2, buffer, GetParam().endianness); |
| 126 | EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); |
| 127 | uint16_t result = *reinterpret_cast<uint16_t*>(writer.data()); |
| 128 | if (GetParam().endianness == NETWORK_BYTE_ORDER) { |
| 129 | result = QuicEndian::HostToNet16(result); |
| 130 | } |
| 131 | EXPECT_EQ(test_cases[i].encoded, result); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | TEST_P(QuicDataWriterTest, ReadUFloat16) { |
| 136 | struct TestCase { |
| 137 | uint64_t decoded; |
| 138 | uint16_t encoded; |
| 139 | }; |
| 140 | TestCase test_cases[] = { |
| 141 | // There are fewer decoding test cases because encoding truncates, and |
| 142 | // decoding returns the smallest expansion. |
| 143 | // Small numbers represent themselves. |
| 144 | {0, 0}, |
| 145 | {1, 1}, |
| 146 | {2, 2}, |
| 147 | {3, 3}, |
| 148 | {4, 4}, |
| 149 | {5, 5}, |
| 150 | {6, 6}, |
| 151 | {7, 7}, |
| 152 | {15, 15}, |
| 153 | {31, 31}, |
| 154 | {42, 42}, |
| 155 | {123, 123}, |
| 156 | {1234, 1234}, |
| 157 | // Check transition through 2^11. |
| 158 | {2046, 2046}, |
| 159 | {2047, 2047}, |
| 160 | {2048, 2048}, |
| 161 | {2049, 2049}, |
| 162 | // Running out of mantissa at 2^12. |
| 163 | {4094, 4094}, |
| 164 | {4095, 4095}, |
| 165 | {4096, 4096}, |
| 166 | {4098, 4097}, |
| 167 | {4100, 4098}, |
| 168 | // Check transition through 2^13. |
| 169 | {8190, 6143}, |
| 170 | {8192, 6144}, |
| 171 | {8196, 6145}, |
| 172 | // Half-way through the exponents. |
| 173 | {0x7FF8000, 0x87FF}, |
| 174 | {0x8000000, 0x8800}, |
| 175 | {0xFFF0000, 0x8FFF}, |
| 176 | {0x10000000, 0x9000}, |
| 177 | // Transition into the largest exponent. |
| 178 | {0x1FFE0000000, 0xF7FF}, |
| 179 | {0x20000000000, 0xF800}, |
| 180 | {0x20040000000, 0xF801}, |
| 181 | // Transition into the max value. |
| 182 | {0x3FF80000000, 0xFFFE}, |
| 183 | {0x3FFC0000000, 0xFFFF}, |
| 184 | }; |
| 185 | int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 186 | |
| 187 | for (int i = 0; i < num_test_cases; ++i) { |
| 188 | uint16_t encoded_ufloat = test_cases[i].encoded; |
| 189 | if (GetParam().endianness == NETWORK_BYTE_ORDER) { |
| 190 | encoded_ufloat = QuicEndian::HostToNet16(encoded_ufloat); |
| 191 | } |
| 192 | QuicDataReader reader(reinterpret_cast<char*>(&encoded_ufloat), 2, |
| 193 | GetParam().endianness); |
| 194 | uint64_t value; |
| 195 | EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 196 | EXPECT_EQ(test_cases[i].decoded, value); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | TEST_P(QuicDataWriterTest, RoundTripUFloat16) { |
| 201 | // Just test all 16-bit encoded values. 0 and max already tested above. |
| 202 | uint64_t previous_value = 0; |
| 203 | for (uint16_t i = 1; i < 0xFFFF; ++i) { |
| 204 | // Read the two bytes. |
| 205 | uint16_t read_number = i; |
| 206 | if (GetParam().endianness == NETWORK_BYTE_ORDER) { |
| 207 | read_number = QuicEndian::HostToNet16(read_number); |
| 208 | } |
| 209 | QuicDataReader reader(reinterpret_cast<char*>(&read_number), 2, |
| 210 | GetParam().endianness); |
| 211 | uint64_t value; |
| 212 | // All values must be decodable. |
| 213 | EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 214 | // Check that small numbers represent themselves |
| 215 | if (i < 4097) { |
| 216 | EXPECT_EQ(i, value); |
| 217 | } |
| 218 | // Check there's monotonic growth. |
| 219 | EXPECT_LT(previous_value, value); |
| 220 | // Check that precision is within 0.5% away from the denormals. |
| 221 | if (i > 2000) { |
| 222 | EXPECT_GT(previous_value * 1005, value * 1000); |
| 223 | } |
| 224 | // Check we're always within the promised range. |
| 225 | EXPECT_LT(value, UINT64_C(0x3FFC0000000)); |
| 226 | previous_value = value; |
| 227 | char buffer[6]; |
| 228 | QuicDataWriter writer(6, buffer, GetParam().endianness); |
| 229 | EXPECT_TRUE(writer.WriteUFloat16(value - 1)); |
| 230 | EXPECT_TRUE(writer.WriteUFloat16(value)); |
| 231 | EXPECT_TRUE(writer.WriteUFloat16(value + 1)); |
| 232 | // Check minimal decoding (previous decoding has previous encoding). |
| 233 | uint16_t encoded1 = *reinterpret_cast<uint16_t*>(writer.data()); |
| 234 | uint16_t encoded2 = *reinterpret_cast<uint16_t*>(writer.data() + 2); |
| 235 | uint16_t encoded3 = *reinterpret_cast<uint16_t*>(writer.data() + 4); |
| 236 | if (GetParam().endianness == NETWORK_BYTE_ORDER) { |
| 237 | encoded1 = QuicEndian::NetToHost16(encoded1); |
| 238 | encoded2 = QuicEndian::NetToHost16(encoded2); |
| 239 | encoded3 = QuicEndian::NetToHost16(encoded3); |
| 240 | } |
| 241 | EXPECT_EQ(i - 1, encoded1); |
| 242 | // Check roundtrip. |
| 243 | EXPECT_EQ(i, encoded2); |
| 244 | // Check next decoding. |
| 245 | EXPECT_EQ(i < 4096 ? i + 1 : i, encoded3); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | TEST_P(QuicDataWriterTest, WriteConnectionId) { |
| 250 | QuicConnectionId connection_id = |
| 251 | TestConnectionId(UINT64_C(0x0011223344556677)); |
| 252 | char big_endian[] = { |
| 253 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 254 | }; |
| 255 | EXPECT_EQ(connection_id.length(), QUIC_ARRAYSIZE(big_endian)); |
| 256 | ASSERT_LE(connection_id.length(), kQuicMaxConnectionIdLength); |
| 257 | char buffer[kQuicMaxConnectionIdLength]; |
| 258 | QuicDataWriter writer(connection_id.length(), buffer, GetParam().endianness); |
| 259 | EXPECT_TRUE(writer.WriteConnectionId(connection_id)); |
| 260 | test::CompareCharArraysWithHexError("connection_id", buffer, |
| 261 | connection_id.length(), big_endian, |
| 262 | connection_id.length()); |
| 263 | |
| 264 | QuicConnectionId read_connection_id; |
| 265 | QuicDataReader reader(buffer, connection_id.length(), GetParam().endianness); |
| 266 | EXPECT_TRUE( |
| 267 | reader.ReadConnectionId(&read_connection_id, QUIC_ARRAYSIZE(big_endian))); |
| 268 | EXPECT_EQ(connection_id, read_connection_id); |
| 269 | } |
| 270 | |
| 271 | TEST_P(QuicDataWriterTest, EmptyConnectionIds) { |
| 272 | QuicConnectionId empty_connection_id = EmptyQuicConnectionId(); |
| 273 | char buffer[2]; |
| 274 | QuicDataWriter writer(QUIC_ARRAYSIZE(buffer), buffer, GetParam().endianness); |
| 275 | EXPECT_TRUE(writer.WriteConnectionId(empty_connection_id)); |
| 276 | EXPECT_TRUE(writer.WriteUInt8(1)); |
| 277 | EXPECT_TRUE(writer.WriteConnectionId(empty_connection_id)); |
| 278 | EXPECT_TRUE(writer.WriteUInt8(2)); |
| 279 | EXPECT_TRUE(writer.WriteConnectionId(empty_connection_id)); |
| 280 | EXPECT_FALSE(writer.WriteUInt8(3)); |
| 281 | |
| 282 | EXPECT_EQ(buffer[0], 1); |
| 283 | EXPECT_EQ(buffer[1], 2); |
| 284 | |
| 285 | QuicConnectionId read_connection_id = TestConnectionId(); |
| 286 | uint8_t read_byte; |
| 287 | QuicDataReader reader(buffer, QUIC_ARRAYSIZE(buffer), GetParam().endianness); |
| 288 | EXPECT_TRUE(reader.ReadConnectionId(&read_connection_id, 0)); |
| 289 | EXPECT_EQ(read_connection_id, empty_connection_id); |
| 290 | EXPECT_TRUE(reader.ReadUInt8(&read_byte)); |
| 291 | EXPECT_EQ(read_byte, 1); |
| 292 | // Reset read_connection_id to something else to verify that |
| 293 | // ReadConnectionId properly sets it back to empty. |
| 294 | read_connection_id = TestConnectionId(); |
| 295 | EXPECT_TRUE(reader.ReadConnectionId(&read_connection_id, 0)); |
| 296 | EXPECT_EQ(read_connection_id, empty_connection_id); |
| 297 | EXPECT_TRUE(reader.ReadUInt8(&read_byte)); |
| 298 | EXPECT_EQ(read_byte, 2); |
| 299 | read_connection_id = TestConnectionId(); |
| 300 | EXPECT_TRUE(reader.ReadConnectionId(&read_connection_id, 0)); |
| 301 | EXPECT_EQ(read_connection_id, empty_connection_id); |
| 302 | EXPECT_FALSE(reader.ReadUInt8(&read_byte)); |
| 303 | } |
| 304 | |
| 305 | TEST_P(QuicDataWriterTest, WriteTag) { |
| 306 | char CHLO[] = { |
| 307 | 'C', |
| 308 | 'H', |
| 309 | 'L', |
| 310 | 'O', |
| 311 | }; |
| 312 | const int kBufferLength = sizeof(QuicTag); |
| 313 | char buffer[kBufferLength]; |
| 314 | QuicDataWriter writer(kBufferLength, buffer, GetParam().endianness); |
| 315 | writer.WriteTag(kCHLO); |
| 316 | test::CompareCharArraysWithHexError("CHLO", buffer, kBufferLength, CHLO, |
| 317 | kBufferLength); |
| 318 | |
| 319 | QuicTag read_chlo; |
| 320 | QuicDataReader reader(buffer, kBufferLength, GetParam().endianness); |
| 321 | reader.ReadTag(&read_chlo); |
| 322 | EXPECT_EQ(kCHLO, read_chlo); |
| 323 | } |
| 324 | |
| 325 | TEST_P(QuicDataWriterTest, Write16BitUnsignedIntegers) { |
| 326 | char little_endian16[] = {0x22, 0x11}; |
| 327 | char big_endian16[] = {0x11, 0x22}; |
| 328 | char buffer16[2]; |
| 329 | { |
| 330 | uint16_t in_memory16 = 0x1122; |
| 331 | QuicDataWriter writer(2, buffer16, GetParam().endianness); |
| 332 | writer.WriteUInt16(in_memory16); |
| 333 | test::CompareCharArraysWithHexError( |
| 334 | "uint16_t", buffer16, 2, |
| 335 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian16 |
| 336 | : little_endian16, |
| 337 | 2); |
| 338 | |
| 339 | uint16_t read_number16; |
| 340 | QuicDataReader reader(buffer16, 2, GetParam().endianness); |
| 341 | reader.ReadUInt16(&read_number16); |
| 342 | EXPECT_EQ(in_memory16, read_number16); |
| 343 | } |
| 344 | |
| 345 | { |
| 346 | uint64_t in_memory16 = 0x0000000000001122; |
| 347 | QuicDataWriter writer(2, buffer16, GetParam().endianness); |
| 348 | writer.WriteBytesToUInt64(2, in_memory16); |
| 349 | test::CompareCharArraysWithHexError( |
| 350 | "uint16_t", buffer16, 2, |
| 351 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian16 |
| 352 | : little_endian16, |
| 353 | 2); |
| 354 | |
| 355 | uint64_t read_number16; |
| 356 | QuicDataReader reader(buffer16, 2, GetParam().endianness); |
| 357 | reader.ReadBytesToUInt64(2, &read_number16); |
| 358 | EXPECT_EQ(in_memory16, read_number16); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | TEST_P(QuicDataWriterTest, Write24BitUnsignedIntegers) { |
| 363 | char little_endian24[] = {0x33, 0x22, 0x11}; |
| 364 | char big_endian24[] = {0x11, 0x22, 0x33}; |
| 365 | char buffer24[3]; |
| 366 | uint64_t in_memory24 = 0x0000000000112233; |
| 367 | QuicDataWriter writer(3, buffer24, GetParam().endianness); |
| 368 | writer.WriteBytesToUInt64(3, in_memory24); |
| 369 | test::CompareCharArraysWithHexError( |
| 370 | "uint24", buffer24, 3, |
| 371 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian24 |
| 372 | : little_endian24, |
| 373 | 3); |
| 374 | |
| 375 | uint64_t read_number24; |
| 376 | QuicDataReader reader(buffer24, 3, GetParam().endianness); |
| 377 | reader.ReadBytesToUInt64(3, &read_number24); |
| 378 | EXPECT_EQ(in_memory24, read_number24); |
| 379 | } |
| 380 | |
| 381 | TEST_P(QuicDataWriterTest, Write32BitUnsignedIntegers) { |
| 382 | char little_endian32[] = {0x44, 0x33, 0x22, 0x11}; |
| 383 | char big_endian32[] = {0x11, 0x22, 0x33, 0x44}; |
| 384 | char buffer32[4]; |
| 385 | { |
| 386 | uint32_t in_memory32 = 0x11223344; |
| 387 | QuicDataWriter writer(4, buffer32, GetParam().endianness); |
| 388 | writer.WriteUInt32(in_memory32); |
| 389 | test::CompareCharArraysWithHexError( |
| 390 | "uint32_t", buffer32, 4, |
| 391 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian32 |
| 392 | : little_endian32, |
| 393 | 4); |
| 394 | |
| 395 | uint32_t read_number32; |
| 396 | QuicDataReader reader(buffer32, 4, GetParam().endianness); |
| 397 | reader.ReadUInt32(&read_number32); |
| 398 | EXPECT_EQ(in_memory32, read_number32); |
| 399 | } |
| 400 | |
| 401 | { |
| 402 | uint64_t in_memory32 = 0x11223344; |
| 403 | QuicDataWriter writer(4, buffer32, GetParam().endianness); |
| 404 | writer.WriteBytesToUInt64(4, in_memory32); |
| 405 | test::CompareCharArraysWithHexError( |
| 406 | "uint32_t", buffer32, 4, |
| 407 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian32 |
| 408 | : little_endian32, |
| 409 | 4); |
| 410 | |
| 411 | uint64_t read_number32; |
| 412 | QuicDataReader reader(buffer32, 4, GetParam().endianness); |
| 413 | reader.ReadBytesToUInt64(4, &read_number32); |
| 414 | EXPECT_EQ(in_memory32, read_number32); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | TEST_P(QuicDataWriterTest, Write40BitUnsignedIntegers) { |
| 419 | uint64_t in_memory40 = 0x0000001122334455; |
| 420 | char little_endian40[] = {0x55, 0x44, 0x33, 0x22, 0x11}; |
| 421 | char big_endian40[] = {0x11, 0x22, 0x33, 0x44, 0x55}; |
| 422 | char buffer40[5]; |
| 423 | QuicDataWriter writer(5, buffer40, GetParam().endianness); |
| 424 | writer.WriteBytesToUInt64(5, in_memory40); |
| 425 | test::CompareCharArraysWithHexError( |
| 426 | "uint40", buffer40, 5, |
| 427 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian40 |
| 428 | : little_endian40, |
| 429 | 5); |
| 430 | |
| 431 | uint64_t read_number40; |
| 432 | QuicDataReader reader(buffer40, 5, GetParam().endianness); |
| 433 | reader.ReadBytesToUInt64(5, &read_number40); |
| 434 | EXPECT_EQ(in_memory40, read_number40); |
| 435 | } |
| 436 | |
| 437 | TEST_P(QuicDataWriterTest, Write48BitUnsignedIntegers) { |
| 438 | uint64_t in_memory48 = 0x0000112233445566; |
| 439 | char little_endian48[] = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11}; |
| 440 | char big_endian48[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
| 441 | char buffer48[6]; |
| 442 | QuicDataWriter writer(6, buffer48, GetParam().endianness); |
| 443 | writer.WriteBytesToUInt64(6, in_memory48); |
| 444 | test::CompareCharArraysWithHexError( |
| 445 | "uint48", buffer48, 6, |
| 446 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian48 |
| 447 | : little_endian48, |
| 448 | 6); |
| 449 | |
| 450 | uint64_t read_number48; |
| 451 | QuicDataReader reader(buffer48, 6, GetParam().endianness); |
| 452 | reader.ReadBytesToUInt64(6., &read_number48); |
| 453 | EXPECT_EQ(in_memory48, read_number48); |
| 454 | } |
| 455 | |
| 456 | TEST_P(QuicDataWriterTest, Write56BitUnsignedIntegers) { |
| 457 | uint64_t in_memory56 = 0x0011223344556677; |
| 458 | char little_endian56[] = {0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11}; |
| 459 | char big_endian56[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; |
| 460 | char buffer56[7]; |
| 461 | QuicDataWriter writer(7, buffer56, GetParam().endianness); |
| 462 | writer.WriteBytesToUInt64(7, in_memory56); |
| 463 | test::CompareCharArraysWithHexError( |
| 464 | "uint56", buffer56, 7, |
| 465 | GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian56 |
| 466 | : little_endian56, |
| 467 | 7); |
| 468 | |
| 469 | uint64_t read_number56; |
| 470 | QuicDataReader reader(buffer56, 7, GetParam().endianness); |
| 471 | reader.ReadBytesToUInt64(7, &read_number56); |
| 472 | EXPECT_EQ(in_memory56, read_number56); |
| 473 | } |
| 474 | |
| 475 | TEST_P(QuicDataWriterTest, Write64BitUnsignedIntegers) { |
| 476 | uint64_t in_memory64 = 0x1122334455667788; |
| 477 | unsigned char little_endian64[] = {0x88, 0x77, 0x66, 0x55, |
| 478 | 0x44, 0x33, 0x22, 0x11}; |
| 479 | unsigned char big_endian64[] = {0x11, 0x22, 0x33, 0x44, |
| 480 | 0x55, 0x66, 0x77, 0x88}; |
| 481 | char buffer64[8]; |
| 482 | QuicDataWriter writer(8, buffer64, GetParam().endianness); |
| 483 | writer.WriteBytesToUInt64(8, in_memory64); |
| 484 | test::CompareCharArraysWithHexError( |
| 485 | "uint64_t", buffer64, 8, |
| 486 | GetParam().endianness == NETWORK_BYTE_ORDER ? AsChars(big_endian64) |
| 487 | : AsChars(little_endian64), |
| 488 | 8); |
| 489 | |
| 490 | uint64_t read_number64; |
| 491 | QuicDataReader reader(buffer64, 8, GetParam().endianness); |
| 492 | reader.ReadBytesToUInt64(8, &read_number64); |
| 493 | EXPECT_EQ(in_memory64, read_number64); |
| 494 | |
| 495 | QuicDataWriter writer2(8, buffer64, GetParam().endianness); |
| 496 | writer2.WriteUInt64(in_memory64); |
| 497 | test::CompareCharArraysWithHexError( |
| 498 | "uint64_t", buffer64, 8, |
| 499 | GetParam().endianness == NETWORK_BYTE_ORDER ? AsChars(big_endian64) |
| 500 | : AsChars(little_endian64), |
| 501 | 8); |
| 502 | read_number64 = 0u; |
| 503 | QuicDataReader reader2(buffer64, 8, GetParam().endianness); |
| 504 | reader2.ReadUInt64(&read_number64); |
| 505 | EXPECT_EQ(in_memory64, read_number64); |
| 506 | } |
| 507 | |
| 508 | TEST_P(QuicDataWriterTest, WriteIntegers) { |
| 509 | char buf[43]; |
| 510 | uint8_t i8 = 0x01; |
| 511 | uint16_t i16 = 0x0123; |
| 512 | uint32_t i32 = 0x01234567; |
| 513 | uint64_t i64 = 0x0123456789ABCDEF; |
| 514 | QuicDataWriter writer(46, buf, GetParam().endianness); |
| 515 | for (size_t i = 0; i < 10; ++i) { |
| 516 | switch (i) { |
| 517 | case 0u: |
| 518 | EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); |
| 519 | break; |
| 520 | case 1u: |
| 521 | EXPECT_TRUE(writer.WriteUInt8(i8)); |
| 522 | EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); |
| 523 | break; |
| 524 | case 2u: |
| 525 | EXPECT_TRUE(writer.WriteUInt16(i16)); |
| 526 | EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); |
| 527 | break; |
| 528 | case 3u: |
| 529 | EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); |
| 530 | break; |
| 531 | case 4u: |
| 532 | EXPECT_TRUE(writer.WriteUInt32(i32)); |
| 533 | EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); |
| 534 | break; |
| 535 | case 5u: |
| 536 | case 6u: |
| 537 | case 7u: |
| 538 | case 8u: |
| 539 | EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); |
| 540 | break; |
| 541 | default: |
| 542 | EXPECT_FALSE(writer.WriteBytesToUInt64(i, i64)); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | QuicDataReader reader(buf, 46, GetParam().endianness); |
| 547 | for (size_t i = 0; i < 10; ++i) { |
| 548 | uint8_t read8; |
| 549 | uint16_t read16; |
| 550 | uint32_t read32; |
| 551 | uint64_t read64; |
| 552 | switch (i) { |
| 553 | case 0u: |
| 554 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 555 | EXPECT_EQ(0u, read64); |
| 556 | break; |
| 557 | case 1u: |
| 558 | EXPECT_TRUE(reader.ReadUInt8(&read8)); |
| 559 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 560 | EXPECT_EQ(i8, read8); |
| 561 | EXPECT_EQ(0xEFu, read64); |
| 562 | break; |
| 563 | case 2u: |
| 564 | EXPECT_TRUE(reader.ReadUInt16(&read16)); |
| 565 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 566 | EXPECT_EQ(i16, read16); |
| 567 | EXPECT_EQ(0xCDEFu, read64); |
| 568 | break; |
| 569 | case 3u: |
| 570 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 571 | EXPECT_EQ(0xABCDEFu, read64); |
| 572 | break; |
| 573 | case 4u: |
| 574 | EXPECT_TRUE(reader.ReadUInt32(&read32)); |
| 575 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 576 | EXPECT_EQ(i32, read32); |
| 577 | EXPECT_EQ(0x89ABCDEFu, read64); |
| 578 | break; |
| 579 | case 5u: |
| 580 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 581 | EXPECT_EQ(0x6789ABCDEFu, read64); |
| 582 | break; |
| 583 | case 6u: |
| 584 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 585 | EXPECT_EQ(0x456789ABCDEFu, read64); |
| 586 | break; |
| 587 | case 7u: |
| 588 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 589 | EXPECT_EQ(0x23456789ABCDEFu, read64); |
| 590 | break; |
| 591 | case 8u: |
| 592 | EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); |
| 593 | EXPECT_EQ(0x0123456789ABCDEFu, read64); |
| 594 | break; |
| 595 | default: |
| 596 | EXPECT_FALSE(reader.ReadBytesToUInt64(i, &read64)); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | TEST_P(QuicDataWriterTest, WriteBytes) { |
| 602 | char bytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; |
| 603 | char buf[QUIC_ARRAYSIZE(bytes)]; |
| 604 | QuicDataWriter writer(QUIC_ARRAYSIZE(buf), buf, GetParam().endianness); |
| 605 | EXPECT_TRUE(writer.WriteBytes(bytes, QUIC_ARRAYSIZE(bytes))); |
| 606 | for (unsigned int i = 0; i < QUIC_ARRAYSIZE(bytes); ++i) { |
| 607 | EXPECT_EQ(bytes[i], buf[i]); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | const int kVarIntBufferLength = 1024; |
| 612 | |
| 613 | // Encodes and then decodes a specified value, checks that the |
| 614 | // value that was encoded is the same as the decoded value, the length |
| 615 | // is correct, and that after decoding, all data in the buffer has |
| 616 | // been consumed.. |
| 617 | // Returns true if everything works, false if not. |
| 618 | bool EncodeDecodeValue(uint64_t value_in, char* buffer, size_t size_of_buffer) { |
| 619 | // Init the buffer to all 0, just for cleanliness. Makes for better |
| 620 | // output if, in debugging, we need to dump out the buffer. |
| 621 | memset(buffer, 0, size_of_buffer); |
| 622 | // make a writer. Note that for IETF encoding |
| 623 | // we do not care about endianness... It's always big-endian, |
| 624 | // but the c'tor expects to be told what endianness is in force... |
| 625 | QuicDataWriter writer(size_of_buffer, buffer, Endianness::NETWORK_BYTE_ORDER); |
| 626 | |
| 627 | // Try to write the value. |
| 628 | if (writer.WriteVarInt62(value_in) != true) { |
| 629 | return false; |
| 630 | } |
| 631 | // Look at the value we encoded. Determine how much should have been |
| 632 | // used based on the value, and then check the state of the writer |
| 633 | // to see that it matches. |
| 634 | size_t expected_length = 0; |
| 635 | if (value_in <= 0x3f) { |
| 636 | expected_length = 1; |
| 637 | } else if (value_in <= 0x3fff) { |
| 638 | expected_length = 2; |
| 639 | } else if (value_in <= 0x3fffffff) { |
| 640 | expected_length = 4; |
| 641 | } else { |
| 642 | expected_length = 8; |
| 643 | } |
| 644 | if (writer.length() != expected_length) { |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | // set up a reader, just the length we've used, no more, no less. |
| 649 | QuicDataReader reader(buffer, expected_length, |
| 650 | Endianness::NETWORK_BYTE_ORDER); |
| 651 | uint64_t value_out; |
| 652 | |
| 653 | if (reader.ReadVarInt62(&value_out) == false) { |
| 654 | return false; |
| 655 | } |
| 656 | if (value_in != value_out) { |
| 657 | return false; |
| 658 | } |
| 659 | // We only write one value so there had better be nothing left to read |
| 660 | return reader.IsDoneReading(); |
| 661 | } |
| 662 | |
| 663 | // Test that 8-byte-encoded Variable Length Integers are properly laid |
| 664 | // out in the buffer. |
| 665 | TEST_P(QuicDataWriterTest, VarInt8Layout) { |
| 666 | char buffer[1024]; |
| 667 | |
| 668 | // Check that the layout of bytes in the buffer is correct. Bytes |
| 669 | // are always encoded big endian... |
| 670 | memset(buffer, 0, sizeof(buffer)); |
| 671 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 672 | Endianness::NETWORK_BYTE_ORDER); |
| 673 | EXPECT_TRUE(writer.WriteVarInt62(UINT64_C(0x3142f3e4d5c6b7a8))); |
| 674 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 0)), |
| 675 | (0x31 + 0xc0)); // 0xc0 for encoding |
| 676 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 1)), 0x42); |
| 677 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 2)), 0xf3); |
| 678 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 3)), 0xe4); |
| 679 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 4)), 0xd5); |
| 680 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 5)), 0xc6); |
| 681 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 6)), 0xb7); |
| 682 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 7)), 0xa8); |
| 683 | } |
| 684 | |
| 685 | // Test that 4-byte-encoded Variable Length Integers are properly laid |
| 686 | // out in the buffer. |
| 687 | TEST_P(QuicDataWriterTest, VarInt4Layout) { |
| 688 | char buffer[1024]; |
| 689 | |
| 690 | // Check that the layout of bytes in the buffer is correct. Bytes |
| 691 | // are always encoded big endian... |
| 692 | memset(buffer, 0, sizeof(buffer)); |
| 693 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 694 | Endianness::NETWORK_BYTE_ORDER); |
| 695 | EXPECT_TRUE(writer.WriteVarInt62(0x3243f4e5)); |
| 696 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 0)), |
| 697 | (0x32 + 0x80)); // 0x80 for encoding |
| 698 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 1)), 0x43); |
| 699 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 2)), 0xf4); |
| 700 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 3)), 0xe5); |
| 701 | } |
| 702 | |
| 703 | // Test that 2-byte-encoded Variable Length Integers are properly laid |
| 704 | // out in the buffer. |
| 705 | TEST_P(QuicDataWriterTest, VarInt2Layout) { |
| 706 | char buffer[1024]; |
| 707 | |
| 708 | // Check that the layout of bytes in the buffer is correct. Bytes |
| 709 | // are always encoded big endian... |
| 710 | memset(buffer, 0, sizeof(buffer)); |
| 711 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 712 | Endianness::NETWORK_BYTE_ORDER); |
| 713 | EXPECT_TRUE(writer.WriteVarInt62(0x3647)); |
| 714 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 0)), |
| 715 | (0x36 + 0x40)); // 0x40 for encoding |
| 716 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 1)), 0x47); |
| 717 | } |
| 718 | |
| 719 | // Test that 1-byte-encoded Variable Length Integers are properly laid |
| 720 | // out in the buffer. |
| 721 | TEST_P(QuicDataWriterTest, VarInt1Layout) { |
| 722 | char buffer[1024]; |
| 723 | |
| 724 | // Check that the layout of bytes in the buffer |
| 725 | // is correct. Bytes are always encoded big endian... |
| 726 | memset(buffer, 0, sizeof(buffer)); |
| 727 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 728 | Endianness::NETWORK_BYTE_ORDER); |
| 729 | EXPECT_TRUE(writer.WriteVarInt62(0x3f)); |
| 730 | EXPECT_EQ(static_cast<unsigned char>(*(writer.data() + 0)), 0x3f); |
| 731 | } |
| 732 | |
| 733 | // Test certain, targeted, values that are expected to succeed: |
| 734 | // 0, 1, |
| 735 | // 0x3e, 0x3f, 0x40, 0x41 (around the 1-2 byte transitions) |
| 736 | // 0x3ffe, 0x3fff, 0x4000, 0x4001 (the 2-4 byte transition) |
| 737 | // 0x3ffffffe, 0x3fffffff, 0x40000000, 0x40000001 (the 4-8 byte |
| 738 | // transition) |
| 739 | // 0x3ffffffffffffffe, 0x3fffffffffffffff, (the highest valid values) |
| 740 | // 0xfe, 0xff, 0x100, 0x101, |
| 741 | // 0xfffe, 0xffff, 0x10000, 0x10001, |
| 742 | // 0xfffffe, 0xffffff, 0x1000000, 0x1000001, |
| 743 | // 0xfffffffe, 0xffffffff, 0x100000000, 0x100000001, |
| 744 | // 0xfffffffffe, 0xffffffffff, 0x10000000000, 0x10000000001, |
| 745 | // 0xfffffffffffe, 0xffffffffffff, 0x1000000000000, 0x1000000000001, |
| 746 | // 0xfffffffffffffe, 0xffffffffffffff, 0x100000000000000, 0x100000000000001, |
| 747 | TEST_P(QuicDataWriterTest, VarIntGoodTargetedValues) { |
| 748 | char buffer[kVarIntBufferLength]; |
| 749 | uint64_t passing_values[] = { |
| 750 | 0, |
| 751 | 1, |
| 752 | 0x3e, |
| 753 | 0x3f, |
| 754 | 0x40, |
| 755 | 0x41, |
| 756 | 0x3ffe, |
| 757 | 0x3fff, |
| 758 | 0x4000, |
| 759 | 0x4001, |
| 760 | 0x3ffffffe, |
| 761 | 0x3fffffff, |
| 762 | 0x40000000, |
| 763 | 0x40000001, |
| 764 | 0x3ffffffffffffffe, |
| 765 | 0x3fffffffffffffff, |
| 766 | 0xfe, |
| 767 | 0xff, |
| 768 | 0x100, |
| 769 | 0x101, |
| 770 | 0xfffe, |
| 771 | 0xffff, |
| 772 | 0x10000, |
| 773 | 0x10001, |
| 774 | 0xfffffe, |
| 775 | 0xffffff, |
| 776 | 0x1000000, |
| 777 | 0x1000001, |
| 778 | 0xfffffffe, |
| 779 | 0xffffffff, |
| 780 | 0x100000000, |
| 781 | 0x100000001, |
| 782 | 0xfffffffffe, |
| 783 | 0xffffffffff, |
| 784 | 0x10000000000, |
| 785 | 0x10000000001, |
| 786 | 0xfffffffffffe, |
| 787 | 0xffffffffffff, |
| 788 | 0x1000000000000, |
| 789 | 0x1000000000001, |
| 790 | 0xfffffffffffffe, |
| 791 | 0xffffffffffffff, |
| 792 | 0x100000000000000, |
| 793 | 0x100000000000001, |
| 794 | }; |
| 795 | for (uint64_t test_val : passing_values) { |
| 796 | EXPECT_TRUE( |
| 797 | EncodeDecodeValue(test_val, static_cast<char*>(buffer), sizeof(buffer))) |
| 798 | << " encode/decode of " << test_val << " failed"; |
| 799 | } |
| 800 | } |
| 801 | // |
| 802 | // Test certain, targeted, values where failure is expected (the |
| 803 | // values are invalid w.r.t. IETF VarInt encoding): |
| 804 | // 0x4000000000000000, 0x4000000000000001, ( Just above max allowed value) |
| 805 | // 0xfffffffffffffffe, 0xffffffffffffffff, (should fail) |
| 806 | TEST_P(QuicDataWriterTest, VarIntBadTargetedValues) { |
| 807 | char buffer[kVarIntBufferLength]; |
| 808 | uint64_t failing_values[] = { |
| 809 | 0x4000000000000000, |
| 810 | 0x4000000000000001, |
| 811 | 0xfffffffffffffffe, |
| 812 | 0xffffffffffffffff, |
| 813 | }; |
| 814 | for (uint64_t test_val : failing_values) { |
| 815 | EXPECT_FALSE( |
| 816 | EncodeDecodeValue(test_val, static_cast<char*>(buffer), sizeof(buffer))) |
| 817 | << " encode/decode of " << test_val << " succeeded, but was an " |
| 818 | << "invalid value"; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // Following tests all try to fill the buffer with multiple values, |
| 823 | // go one value more than the buffer can accommodate, then read |
| 824 | // the successfully encoded values, and try to read the unsuccessfully |
| 825 | // encoded value. The following is the number of values to encode. |
| 826 | const int kMultiVarCount = 1000; |
| 827 | |
| 828 | // Test writing & reading multiple 8-byte-encoded varints |
| 829 | TEST_P(QuicDataWriterTest, MultiVarInt8) { |
| 830 | uint64_t test_val; |
| 831 | char buffer[8 * kMultiVarCount]; |
| 832 | memset(buffer, 0, sizeof(buffer)); |
| 833 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 834 | Endianness::NETWORK_BYTE_ORDER); |
| 835 | // Put N values into the buffer. Adding i to the value ensures that |
| 836 | // each value is different so we can detect if we overwrite values, |
| 837 | // or read the same value over and over. |
| 838 | for (int i = 0; i < kMultiVarCount; i++) { |
| 839 | EXPECT_TRUE(writer.WriteVarInt62(UINT64_C(0x3142f3e4d5c6b7a8) + i)); |
| 840 | } |
| 841 | EXPECT_EQ(writer.length(), 8u * kMultiVarCount); |
| 842 | |
| 843 | // N+1st should fail, the buffer is full. |
| 844 | EXPECT_FALSE(writer.WriteVarInt62(UINT64_C(0x3142f3e4d5c6b7a8))); |
| 845 | |
| 846 | // Now we should be able to read out the N values that were |
| 847 | // successfully encoded. |
| 848 | QuicDataReader reader(buffer, sizeof(buffer), Endianness::NETWORK_BYTE_ORDER); |
| 849 | for (int i = 0; i < kMultiVarCount; i++) { |
| 850 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 851 | EXPECT_EQ(test_val, (UINT64_C(0x3142f3e4d5c6b7a8) + i)); |
| 852 | } |
| 853 | // And the N+1st should fail. |
| 854 | EXPECT_FALSE(reader.ReadVarInt62(&test_val)); |
| 855 | } |
| 856 | |
| 857 | // Test writing & reading multiple 4-byte-encoded varints |
| 858 | TEST_P(QuicDataWriterTest, MultiVarInt4) { |
| 859 | uint64_t test_val; |
| 860 | char buffer[4 * kMultiVarCount]; |
| 861 | memset(buffer, 0, sizeof(buffer)); |
| 862 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 863 | Endianness::NETWORK_BYTE_ORDER); |
| 864 | // Put N values into the buffer. Adding i to the value ensures that |
| 865 | // each value is different so we can detect if we overwrite values, |
| 866 | // or read the same value over and over. |
| 867 | for (int i = 0; i < kMultiVarCount; i++) { |
| 868 | EXPECT_TRUE(writer.WriteVarInt62(UINT64_C(0x3142f3e4) + i)); |
| 869 | } |
| 870 | EXPECT_EQ(writer.length(), 4u * kMultiVarCount); |
| 871 | |
| 872 | // N+1st should fail, the buffer is full. |
| 873 | EXPECT_FALSE(writer.WriteVarInt62(UINT64_C(0x3142f3e4))); |
| 874 | |
| 875 | // Now we should be able to read out the N values that were |
| 876 | // successfully encoded. |
| 877 | QuicDataReader reader(buffer, sizeof(buffer), Endianness::NETWORK_BYTE_ORDER); |
| 878 | for (int i = 0; i < kMultiVarCount; i++) { |
| 879 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 880 | EXPECT_EQ(test_val, (UINT64_C(0x3142f3e4) + i)); |
| 881 | } |
| 882 | // And the N+1st should fail. |
| 883 | EXPECT_FALSE(reader.ReadVarInt62(&test_val)); |
| 884 | } |
| 885 | |
| 886 | // Test writing & reading multiple 2-byte-encoded varints |
| 887 | TEST_P(QuicDataWriterTest, MultiVarInt2) { |
| 888 | uint64_t test_val; |
| 889 | char buffer[2 * kMultiVarCount]; |
| 890 | memset(buffer, 0, sizeof(buffer)); |
| 891 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 892 | Endianness::NETWORK_BYTE_ORDER); |
| 893 | // Put N values into the buffer. Adding i to the value ensures that |
| 894 | // each value is different so we can detect if we overwrite values, |
| 895 | // or read the same value over and over. |
| 896 | for (int i = 0; i < kMultiVarCount; i++) { |
| 897 | EXPECT_TRUE(writer.WriteVarInt62(UINT64_C(0x3142) + i)); |
| 898 | } |
| 899 | EXPECT_EQ(writer.length(), 2u * kMultiVarCount); |
| 900 | |
| 901 | // N+1st should fail, the buffer is full. |
| 902 | EXPECT_FALSE(writer.WriteVarInt62(UINT64_C(0x3142))); |
| 903 | |
| 904 | // Now we should be able to read out the N values that were |
| 905 | // successfully encoded. |
| 906 | QuicDataReader reader(buffer, sizeof(buffer), Endianness::NETWORK_BYTE_ORDER); |
| 907 | for (int i = 0; i < kMultiVarCount; i++) { |
| 908 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 909 | EXPECT_EQ(test_val, (UINT64_C(0x3142) + i)); |
| 910 | } |
| 911 | // And the N+1st should fail. |
| 912 | EXPECT_FALSE(reader.ReadVarInt62(&test_val)); |
| 913 | } |
| 914 | |
| 915 | // Test writing & reading multiple 1-byte-encoded varints |
| 916 | TEST_P(QuicDataWriterTest, MultiVarInt1) { |
| 917 | uint64_t test_val; |
| 918 | char buffer[1 * kMultiVarCount]; |
| 919 | memset(buffer, 0, sizeof(buffer)); |
| 920 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 921 | Endianness::NETWORK_BYTE_ORDER); |
| 922 | // Put N values into the buffer. Adding i to the value ensures that |
| 923 | // each value is different so we can detect if we overwrite values, |
| 924 | // or read the same value over and over. &0xf ensures we do not |
| 925 | // overflow the max value for single-byte encoding. |
| 926 | for (int i = 0; i < kMultiVarCount; i++) { |
| 927 | EXPECT_TRUE(writer.WriteVarInt62(UINT64_C(0x30) + (i & 0xf))); |
| 928 | } |
| 929 | EXPECT_EQ(writer.length(), 1u * kMultiVarCount); |
| 930 | |
| 931 | // N+1st should fail, the buffer is full. |
| 932 | EXPECT_FALSE(writer.WriteVarInt62(UINT64_C(0x31))); |
| 933 | |
| 934 | // Now we should be able to read out the N values that were |
| 935 | // successfully encoded. |
| 936 | QuicDataReader reader(buffer, sizeof(buffer), Endianness::NETWORK_BYTE_ORDER); |
| 937 | for (int i = 0; i < kMultiVarCount; i++) { |
| 938 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 939 | EXPECT_EQ(test_val, (UINT64_C(0x30) + (i & 0xf))); |
| 940 | } |
| 941 | // And the N+1st should fail. |
| 942 | EXPECT_FALSE(reader.ReadVarInt62(&test_val)); |
| 943 | } |
| 944 | |
| 945 | // Test writing varints with a forced length. |
| 946 | TEST_P(QuicDataWriterTest, VarIntFixedLength) { |
| 947 | char buffer[90]; |
| 948 | memset(buffer, 0, sizeof(buffer)); |
| 949 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 950 | Endianness::NETWORK_BYTE_ORDER); |
| 951 | |
| 952 | writer.WriteVarInt62(1, VARIABLE_LENGTH_INTEGER_LENGTH_1); |
| 953 | writer.WriteVarInt62(1, VARIABLE_LENGTH_INTEGER_LENGTH_2); |
| 954 | writer.WriteVarInt62(1, VARIABLE_LENGTH_INTEGER_LENGTH_4); |
| 955 | writer.WriteVarInt62(1, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 956 | |
| 957 | writer.WriteVarInt62(63, VARIABLE_LENGTH_INTEGER_LENGTH_1); |
| 958 | writer.WriteVarInt62(63, VARIABLE_LENGTH_INTEGER_LENGTH_2); |
| 959 | writer.WriteVarInt62(63, VARIABLE_LENGTH_INTEGER_LENGTH_4); |
| 960 | writer.WriteVarInt62(63, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 961 | |
| 962 | writer.WriteVarInt62(64, VARIABLE_LENGTH_INTEGER_LENGTH_2); |
| 963 | writer.WriteVarInt62(64, VARIABLE_LENGTH_INTEGER_LENGTH_4); |
| 964 | writer.WriteVarInt62(64, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 965 | |
| 966 | writer.WriteVarInt62(16383, VARIABLE_LENGTH_INTEGER_LENGTH_2); |
| 967 | writer.WriteVarInt62(16383, VARIABLE_LENGTH_INTEGER_LENGTH_4); |
| 968 | writer.WriteVarInt62(16383, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 969 | |
| 970 | writer.WriteVarInt62(16384, VARIABLE_LENGTH_INTEGER_LENGTH_4); |
| 971 | writer.WriteVarInt62(16384, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 972 | |
| 973 | writer.WriteVarInt62(1073741823, VARIABLE_LENGTH_INTEGER_LENGTH_4); |
| 974 | writer.WriteVarInt62(1073741823, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 975 | |
| 976 | writer.WriteVarInt62(1073741824, VARIABLE_LENGTH_INTEGER_LENGTH_8); |
| 977 | |
| 978 | QuicDataReader reader(buffer, sizeof(buffer), Endianness::NETWORK_BYTE_ORDER); |
| 979 | |
| 980 | uint64_t test_val = 0; |
| 981 | for (int i = 0; i < 4; ++i) { |
| 982 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 983 | EXPECT_EQ(test_val, 1u); |
| 984 | } |
| 985 | for (int i = 0; i < 4; ++i) { |
| 986 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 987 | EXPECT_EQ(test_val, 63u); |
| 988 | } |
| 989 | |
| 990 | for (int i = 0; i < 3; ++i) { |
| 991 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 992 | EXPECT_EQ(test_val, 64u); |
| 993 | } |
| 994 | for (int i = 0; i < 3; ++i) { |
| 995 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 996 | EXPECT_EQ(test_val, 16383u); |
| 997 | } |
| 998 | |
| 999 | for (int i = 0; i < 2; ++i) { |
| 1000 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 1001 | EXPECT_EQ(test_val, 16384u); |
| 1002 | } |
| 1003 | for (int i = 0; i < 2; ++i) { |
| 1004 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 1005 | EXPECT_EQ(test_val, 1073741823u); |
| 1006 | } |
| 1007 | |
| 1008 | EXPECT_TRUE(reader.ReadVarInt62(&test_val)); |
| 1009 | EXPECT_EQ(test_val, 1073741824u); |
| 1010 | |
| 1011 | // We are at the end of the buffer so this should fail. |
| 1012 | EXPECT_FALSE(reader.ReadVarInt62(&test_val)); |
| 1013 | } |
| 1014 | |
| 1015 | // Test encoding/decoding stream-id values. |
| 1016 | void EncodeDecodeStreamId(uint64_t value_in, bool expected_decode_result) { |
| 1017 | char buffer[1 * kMultiVarCount]; |
| 1018 | memset(buffer, 0, sizeof(buffer)); |
| 1019 | |
| 1020 | // Encode the given Stream ID. |
| 1021 | QuicDataWriter writer(sizeof(buffer), static_cast<char*>(buffer), |
| 1022 | Endianness::NETWORK_BYTE_ORDER); |
| 1023 | EXPECT_TRUE(writer.WriteVarInt62(value_in)); |
| 1024 | |
| 1025 | QuicDataReader reader(buffer, sizeof(buffer), Endianness::NETWORK_BYTE_ORDER); |
| 1026 | QuicStreamId received_stream_id; |
| 1027 | bool read_result = reader.ReadVarIntStreamId(&received_stream_id); |
| 1028 | EXPECT_EQ(expected_decode_result, read_result); |
| 1029 | if (read_result) { |
| 1030 | EXPECT_EQ(value_in, received_stream_id); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // Test writing & reading stream-ids of various value. |
| 1035 | TEST_P(QuicDataWriterTest, StreamId1) { |
| 1036 | // Check a 1-byte QuicStreamId, should work |
| 1037 | EncodeDecodeStreamId(UINT64_C(0x15), true); |
| 1038 | |
| 1039 | // Check a 2-byte QuicStream ID. It should work. |
| 1040 | EncodeDecodeStreamId(UINT64_C(0x1567), true); |
| 1041 | |
| 1042 | // Check a QuicStreamId that requires 4 bytes of encoding |
| 1043 | // This should work. |
| 1044 | EncodeDecodeStreamId(UINT64_C(0x34567890), true); |
| 1045 | |
| 1046 | // Check a QuicStreamId that requires 8 bytes of encoding |
| 1047 | // but whose value is in the acceptable range. |
| 1048 | // This should work. |
| 1049 | EncodeDecodeStreamId(UINT64_C(0xf4567890), true); |
| 1050 | |
| 1051 | // Check QuicStreamIds that require 8 bytes of encoding |
| 1052 | // and whose value is not acceptable. |
| 1053 | // This should fail. |
| 1054 | EncodeDecodeStreamId(UINT64_C(0x100000000), false); |
| 1055 | EncodeDecodeStreamId(UINT64_C(0x3fffffffffffffff), false); |
| 1056 | } |
| 1057 | |
| 1058 | TEST_P(QuicDataWriterTest, WriteRandomBytes) { |
| 1059 | char buffer[20]; |
| 1060 | char expected[20]; |
| 1061 | for (size_t i = 0; i < 20; ++i) { |
| 1062 | expected[i] = 'r'; |
| 1063 | } |
| 1064 | MockRandom random; |
| 1065 | QuicDataWriter writer(20, buffer, GetParam().endianness); |
| 1066 | EXPECT_FALSE(writer.WriteRandomBytes(&random, 30)); |
| 1067 | |
| 1068 | EXPECT_TRUE(writer.WriteRandomBytes(&random, 20)); |
| 1069 | test::CompareCharArraysWithHexError("random", buffer, 20, expected, 20); |
| 1070 | } |
| 1071 | |
| 1072 | TEST_P(QuicDataWriterTest, PeekVarInt62Length) { |
| 1073 | // In range [0, 63], variable length should be 1 byte. |
| 1074 | char buffer[20]; |
| 1075 | QuicDataWriter writer(20, buffer, NETWORK_BYTE_ORDER); |
| 1076 | EXPECT_TRUE(writer.WriteVarInt62(50)); |
| 1077 | QuicDataReader reader(buffer, 20, NETWORK_BYTE_ORDER); |
| 1078 | EXPECT_EQ(1, reader.PeekVarInt62Length()); |
| 1079 | // In range (63-16383], variable length should be 2 byte2. |
| 1080 | char buffer2[20]; |
| 1081 | QuicDataWriter writer2(20, buffer2, NETWORK_BYTE_ORDER); |
| 1082 | EXPECT_TRUE(writer2.WriteVarInt62(100)); |
| 1083 | QuicDataReader reader2(buffer2, 20, NETWORK_BYTE_ORDER); |
| 1084 | EXPECT_EQ(2, reader2.PeekVarInt62Length()); |
| 1085 | // In range (16383, 1073741823], variable length should be 4 bytes. |
| 1086 | char buffer3[20]; |
| 1087 | QuicDataWriter writer3(20, buffer3, NETWORK_BYTE_ORDER); |
| 1088 | EXPECT_TRUE(writer3.WriteVarInt62(20000)); |
| 1089 | QuicDataReader reader3(buffer3, 20, NETWORK_BYTE_ORDER); |
| 1090 | EXPECT_EQ(4, reader3.PeekVarInt62Length()); |
| 1091 | // In range (1073741823, 4611686018427387903], variable length should be 8 |
| 1092 | // bytes. |
| 1093 | char buffer4[20]; |
| 1094 | QuicDataWriter writer4(20, buffer4, NETWORK_BYTE_ORDER); |
| 1095 | EXPECT_TRUE(writer4.WriteVarInt62(2000000000)); |
| 1096 | QuicDataReader reader4(buffer4, 20, NETWORK_BYTE_ORDER); |
| 1097 | EXPECT_EQ(8, reader4.PeekVarInt62Length()); |
| 1098 | } |
| 1099 | |
| 1100 | } // namespace |
| 1101 | } // namespace test |
| 1102 | } // namespace quic |