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