Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/core/quic_data_writer.h" | 5 #include "net/quic/core/quic_data_writer.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 | 8 |
| 9 #include "net/quic/core/quic_data_reader.h" | 9 #include "net/quic/core/quic_data_reader.h" |
| 10 #include "net/quic/core/quic_utils.h" | 10 #include "net/quic/core/quic_utils.h" |
| 11 #include "net/quic/platform/api/quic_flags.h" | 11 #include "net/quic/platform/api/quic_flags.h" |
| 12 #include "net/quic/test_tools/quic_test_utils.h" | 12 #include "net/quic/test_tools/quic_test_utils.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 namespace test { | 16 namespace test { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 class QuicDataWriterTest : public ::testing::TestWithParam<Perspective> {}; | 19 struct TestParams { |
| 20 TestParams(Perspective perspective, Endianness endianness) | |
| 21 : perspective(perspective), endianness(endianness) {} | |
| 22 | |
| 23 Perspective perspective; | |
| 24 Endianness endianness; | |
| 25 }; | |
| 26 | |
| 27 std::vector<TestParams> GetTestParams() { | |
| 28 std::vector<TestParams> params; | |
| 29 for (Perspective perspective : | |
| 30 {Perspective::IS_CLIENT, Perspective::IS_SERVER}) { | |
| 31 for (Endianness endianness : {NETWORK_BYTE_ORDER, HOST_BYTE_ORDER}) { | |
| 32 params.push_back(TestParams(perspective, endianness)); | |
| 33 } | |
| 34 } | |
| 35 return params; | |
| 36 } | |
| 37 | |
| 38 class QuicDataWriterTest : public ::testing::TestWithParam<TestParams> {}; | |
| 39 | |
| 40 INSTANTIATE_TEST_CASE_P(QuicDataWriterTests, | |
| 41 QuicDataWriterTest, | |
| 42 ::testing::ValuesIn(GetTestParams())); | |
| 20 | 43 |
| 21 TEST_P(QuicDataWriterTest, SanityCheckUFloat16Consts) { | 44 TEST_P(QuicDataWriterTest, SanityCheckUFloat16Consts) { |
| 22 // Check the arithmetic on the constants - otherwise the values below make | 45 // Check the arithmetic on the constants - otherwise the values below make |
| 23 // no sense. | 46 // no sense. |
| 24 EXPECT_EQ(30, kUFloat16MaxExponent); | 47 EXPECT_EQ(30, kUFloat16MaxExponent); |
| 25 EXPECT_EQ(11, kUFloat16MantissaBits); | 48 EXPECT_EQ(11, kUFloat16MantissaBits); |
| 26 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); | 49 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); |
| 27 EXPECT_EQ(UINT64_C(0x3FFC0000000), kUFloat16MaxValue); | 50 EXPECT_EQ(UINT64_C(0x3FFC0000000), kUFloat16MaxValue); |
| 28 } | 51 } |
| 29 | 52 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 {0x3FFC0000000, 0xFFFF}, | 115 {0x3FFC0000000, 0xFFFF}, |
| 93 {0x3FFC0000001, 0xFFFF}, | 116 {0x3FFC0000001, 0xFFFF}, |
| 94 {0x3FFFFFFFFFF, 0xFFFF}, | 117 {0x3FFFFFFFFFF, 0xFFFF}, |
| 95 {0x40000000000, 0xFFFF}, | 118 {0x40000000000, 0xFFFF}, |
| 96 {0xFFFFFFFFFFFFFFFF, 0xFFFF}, | 119 {0xFFFFFFFFFFFFFFFF, 0xFFFF}, |
| 97 }; | 120 }; |
| 98 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 121 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 99 | 122 |
| 100 for (int i = 0; i < num_test_cases; ++i) { | 123 for (int i = 0; i < num_test_cases; ++i) { |
| 101 char buffer[2]; | 124 char buffer[2]; |
| 102 QuicDataWriter writer(2, buffer, GetParam()); | 125 QuicDataWriter writer(2, buffer, GetParam().perspective, |
| 126 GetParam().endianness); | |
| 103 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); | 127 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); |
| 104 EXPECT_EQ(test_cases[i].encoded, | 128 uint16_t result = *reinterpret_cast<uint16_t*>(writer.data()); |
| 105 *reinterpret_cast<uint16_t*>(writer.data())); | 129 if (GetParam().endianness == NETWORK_BYTE_ORDER) { |
| 130 result = QuicEndian::HostToNet16(result); | |
| 131 } | |
| 132 EXPECT_EQ(test_cases[i].encoded, result); | |
| 106 } | 133 } |
| 107 } | 134 } |
| 108 | 135 |
| 109 TEST_P(QuicDataWriterTest, ReadUFloat16) { | 136 TEST_P(QuicDataWriterTest, ReadUFloat16) { |
| 110 struct TestCase { | 137 struct TestCase { |
| 111 uint64_t decoded; | 138 uint64_t decoded; |
| 112 uint16_t encoded; | 139 uint16_t encoded; |
| 113 }; | 140 }; |
| 114 TestCase test_cases[] = { | 141 TestCase test_cases[] = { |
| 115 // There are fewer decoding test cases because encoding truncates, and | 142 // There are fewer decoding test cases because encoding truncates, and |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 {0x1FFE0000000, 0xF7FF}, | 179 {0x1FFE0000000, 0xF7FF}, |
| 153 {0x20000000000, 0xF800}, | 180 {0x20000000000, 0xF800}, |
| 154 {0x20040000000, 0xF801}, | 181 {0x20040000000, 0xF801}, |
| 155 // Transition into the max value. | 182 // Transition into the max value. |
| 156 {0x3FF80000000, 0xFFFE}, | 183 {0x3FF80000000, 0xFFFE}, |
| 157 {0x3FFC0000000, 0xFFFF}, | 184 {0x3FFC0000000, 0xFFFF}, |
| 158 }; | 185 }; |
| 159 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 186 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 160 | 187 |
| 161 for (int i = 0; i < num_test_cases; ++i) { | 188 for (int i = 0; i < num_test_cases; ++i) { |
| 162 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2, | 189 uint16_t encoded_ufloat = reinterpret_cast<uint16_t>(test_cases[i].encoded); |
|
Zhongyi Shi
2017/04/27 18:14:03
TestCast::encoded fied by default is uint16_t, it
| |
| 163 GetParam()); | 190 if (GetParam().endianness == NETWORK_BYTE_ORDER) { |
| 191 encoded_ufloat = QuicEndian::HostToNet16(encoded_ufloat); | |
| 192 } | |
| 193 QuicDataReader reader(reinterpret_cast<char*>(&encoded_ufloat), 2, | |
| 194 GetParam().perspective, GetParam().endianness); | |
| 164 uint64_t value; | 195 uint64_t value; |
| 165 EXPECT_TRUE(reader.ReadUFloat16(&value)); | 196 EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 166 EXPECT_EQ(test_cases[i].decoded, value); | 197 EXPECT_EQ(test_cases[i].decoded, value); |
| 167 } | 198 } |
| 168 } | 199 } |
| 169 | 200 |
| 170 TEST_P(QuicDataWriterTest, RoundTripUFloat16) { | 201 TEST_P(QuicDataWriterTest, RoundTripUFloat16) { |
| 171 // Just test all 16-bit encoded values. 0 and max already tested above. | 202 // Just test all 16-bit encoded values. 0 and max already tested above. |
| 172 uint64_t previous_value = 0; | 203 uint64_t previous_value = 0; |
| 173 for (uint16_t i = 1; i < 0xFFFF; ++i) { | 204 for (uint16_t i = 1; i < 0xFFFF; ++i) { |
| 174 // Read the two bytes. | 205 // Read the two bytes. |
| 175 QuicDataReader reader(reinterpret_cast<char*>(&i), 2, GetParam()); | 206 uint16_t read_number = i; |
| 207 if (GetParam().endianness == NETWORK_BYTE_ORDER) { | |
| 208 read_number = QuicEndian::HostToNet16(read_number); | |
| 209 } | |
| 210 QuicDataReader reader(reinterpret_cast<char*>(&read_number), 2, | |
| 211 GetParam().perspective, GetParam().endianness); | |
| 176 uint64_t value; | 212 uint64_t value; |
| 177 // All values must be decodable. | 213 // All values must be decodable. |
| 178 EXPECT_TRUE(reader.ReadUFloat16(&value)); | 214 EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 179 // Check that small numbers represent themselves | 215 // Check that small numbers represent themselves |
| 180 if (i < 4097) | 216 if (i < 4097) { |
| 181 EXPECT_EQ(i, value); | 217 EXPECT_EQ(i, value); |
| 218 } | |
| 182 // Check there's monotonic growth. | 219 // Check there's monotonic growth. |
| 183 EXPECT_LT(previous_value, value); | 220 EXPECT_LT(previous_value, value); |
| 184 // Check that precision is within 0.5% away from the denormals. | 221 // Check that precision is within 0.5% away from the denormals. |
| 185 if (i > 2000) | 222 if (i > 2000) { |
| 186 EXPECT_GT(previous_value * 1005, value * 1000); | 223 EXPECT_GT(previous_value * 1005, value * 1000); |
| 224 } | |
| 187 // Check we're always within the promised range. | 225 // Check we're always within the promised range. |
| 188 EXPECT_LT(value, UINT64_C(0x3FFC0000000)); | 226 EXPECT_LT(value, UINT64_C(0x3FFC0000000)); |
| 189 previous_value = value; | 227 previous_value = value; |
| 190 char buffer[6]; | 228 char buffer[6]; |
| 191 QuicDataWriter writer(6, buffer, GetParam()); | 229 QuicDataWriter writer(6, buffer, GetParam().perspective, |
| 230 GetParam().endianness); | |
| 192 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); | 231 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); |
| 193 EXPECT_TRUE(writer.WriteUFloat16(value)); | 232 EXPECT_TRUE(writer.WriteUFloat16(value)); |
| 194 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); | 233 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); |
| 195 // Check minimal decoding (previous decoding has previous encoding). | 234 // Check minimal decoding (previous decoding has previous encoding). |
| 196 EXPECT_EQ(i - 1, *reinterpret_cast<uint16_t*>(writer.data())); | 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); | |
| 197 // Check roundtrip. | 244 // Check roundtrip. |
| 198 EXPECT_EQ(i, *reinterpret_cast<uint16_t*>(writer.data() + 2)); | 245 EXPECT_EQ(i, encoded2); |
| 199 // Check next decoding. | 246 // Check next decoding. |
| 200 EXPECT_EQ(i < 4096 ? i + 1 : i, | 247 EXPECT_EQ(i < 4096 ? i + 1 : i, encoded3); |
| 201 *reinterpret_cast<uint16_t*>(writer.data() + 4)); | |
| 202 } | 248 } |
| 203 } | 249 } |
| 204 | 250 |
| 205 TEST_P(QuicDataWriterTest, WriteConnectionId) { | 251 TEST_P(QuicDataWriterTest, WriteConnectionId) { |
| 206 uint64_t connection_id = 0x0011223344556677; | 252 uint64_t connection_id = 0x0011223344556677; |
| 207 char little_endian[] = { | 253 char little_endian[] = { |
| 208 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, | 254 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, |
| 209 }; | 255 }; |
| 210 char big_endian[] = { | 256 char big_endian[] = { |
| 211 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, | 257 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 212 }; | 258 }; |
| 213 const int kBufferLength = sizeof(connection_id); | 259 const int kBufferLength = sizeof(connection_id); |
| 214 char buffer[kBufferLength]; | 260 char buffer[kBufferLength]; |
| 215 QuicDataWriter writer(kBufferLength, buffer, GetParam()); | 261 QuicDataWriter writer(kBufferLength, buffer, GetParam().perspective, |
| 262 GetParam().endianness); | |
| 216 writer.WriteConnectionId(connection_id); | 263 writer.WriteConnectionId(connection_id); |
| 217 test::CompareCharArraysWithHexError( | 264 test::CompareCharArraysWithHexError( |
| 218 "connection_id", buffer, kBufferLength, | 265 "connection_id", buffer, kBufferLength, |
| 219 QuicUtils::IsConnectionIdWireFormatBigEndian(GetParam()) ? big_endian | 266 QuicUtils::IsConnectionIdWireFormatBigEndian(GetParam().perspective) |
| 220 : little_endian, | 267 ? big_endian |
| 268 : little_endian, | |
| 221 kBufferLength); | 269 kBufferLength); |
| 222 | 270 |
| 223 uint64_t read_connection_id; | 271 uint64_t read_connection_id; |
| 224 QuicDataReader reader(buffer, kBufferLength, GetParam()); | 272 QuicDataReader reader(buffer, kBufferLength, GetParam().perspective, |
| 273 GetParam().endianness); | |
| 225 reader.ReadConnectionId(&read_connection_id); | 274 reader.ReadConnectionId(&read_connection_id); |
| 226 EXPECT_EQ(connection_id, read_connection_id); | 275 EXPECT_EQ(connection_id, read_connection_id); |
| 227 } | 276 } |
| 228 | 277 |
| 229 TEST_P(QuicDataWriterTest, WriteTag) { | 278 TEST_P(QuicDataWriterTest, WriteTag) { |
| 230 char CHLO[] = { | 279 char CHLO[] = { |
| 231 'C', 'H', 'L', 'O', | 280 'C', 'H', 'L', 'O', |
| 232 }; | 281 }; |
| 233 const int kBufferLength = sizeof(QuicTag); | 282 const int kBufferLength = sizeof(QuicTag); |
| 234 char buffer[kBufferLength]; | 283 char buffer[kBufferLength]; |
| 235 QuicDataWriter writer(kBufferLength, buffer, GetParam()); | 284 QuicDataWriter writer(kBufferLength, buffer, GetParam().perspective, |
| 285 GetParam().endianness); | |
| 236 writer.WriteTag(kCHLO); | 286 writer.WriteTag(kCHLO); |
| 237 test::CompareCharArraysWithHexError("CHLO", buffer, kBufferLength, CHLO, | 287 test::CompareCharArraysWithHexError("CHLO", buffer, kBufferLength, CHLO, |
| 238 kBufferLength); | 288 kBufferLength); |
| 239 | 289 |
| 240 QuicTag read_chlo; | 290 QuicTag read_chlo; |
| 241 QuicDataReader reader(buffer, kBufferLength, GetParam()); | 291 QuicDataReader reader(buffer, kBufferLength, GetParam().perspective, |
| 292 GetParam().endianness); | |
| 242 reader.ReadTag(&read_chlo); | 293 reader.ReadTag(&read_chlo); |
| 243 EXPECT_EQ(kCHLO, read_chlo); | 294 EXPECT_EQ(kCHLO, read_chlo); |
| 244 } | 295 } |
| 245 | 296 |
| 297 TEST_P(QuicDataWriterTest, Write16BitUnsignedIntegers) { | |
| 298 char little_endian16[] = {0x22, 0x11}; | |
| 299 char big_endian16[] = {0x11, 0x22}; | |
| 300 char buffer16[2]; | |
| 301 { | |
| 302 uint16_t in_memory16 = 0x1122; | |
| 303 QuicDataWriter writer(2, buffer16, GetParam().perspective, | |
| 304 GetParam().endianness); | |
| 305 writer.WriteUInt16(in_memory16); | |
| 306 test::CompareCharArraysWithHexError( | |
| 307 "uint16_t", buffer16, 2, | |
| 308 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian16 | |
| 309 : little_endian16, | |
| 310 2); | |
| 311 | |
| 312 uint16_t read_number16; | |
| 313 QuicDataReader reader(buffer16, 2, GetParam().perspective, | |
| 314 GetParam().endianness); | |
| 315 reader.ReadUInt16(&read_number16); | |
| 316 EXPECT_EQ(in_memory16, read_number16); | |
| 317 } | |
| 318 | |
| 319 { | |
| 320 uint64_t in_memory16 = 0x0000000000001122; | |
| 321 QuicDataWriter writer(2, buffer16, GetParam().perspective, | |
| 322 GetParam().endianness); | |
| 323 writer.WriteBytesToUInt64(2, in_memory16); | |
| 324 test::CompareCharArraysWithHexError( | |
| 325 "uint16_t", buffer16, 2, | |
| 326 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian16 | |
| 327 : little_endian16, | |
| 328 2); | |
| 329 | |
| 330 uint64_t read_number16 = 0u; | |
| 331 QuicDataReader reader(buffer16, 2, GetParam().perspective, | |
| 332 GetParam().endianness); | |
| 333 reader.ReadBytesToUInt64(2, &read_number16); | |
| 334 EXPECT_EQ(in_memory16, read_number16); | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 TEST_P(QuicDataWriterTest, Write24BitUnsignedIntegers) { | |
| 339 char little_endian24[] = {0x33, 0x22, 0x11}; | |
| 340 char big_endian24[] = {0x11, 0x22, 0x33}; | |
| 341 char buffer24[3]; | |
| 342 uint64_t in_memory24 = 0x0000000000112233; | |
| 343 QuicDataWriter writer(3, buffer24, GetParam().perspective, | |
| 344 GetParam().endianness); | |
| 345 writer.WriteBytesToUInt64(3, in_memory24); | |
| 346 test::CompareCharArraysWithHexError( | |
| 347 "uint24", buffer24, 3, | |
| 348 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian24 | |
| 349 : little_endian24, | |
| 350 3); | |
| 351 | |
| 352 uint64_t read_number24 = 0u; | |
| 353 QuicDataReader reader(buffer24, 3, GetParam().perspective, | |
| 354 GetParam().endianness); | |
| 355 reader.ReadBytesToUInt64(3, &read_number24); | |
| 356 EXPECT_EQ(in_memory24, read_number24); | |
| 357 } | |
| 358 | |
| 359 TEST_P(QuicDataWriterTest, Write32BitUnsignedIntegers) { | |
| 360 char little_endian32[] = {0x44, 0x33, 0x22, 0x11}; | |
| 361 char big_endian32[] = {0x11, 0x22, 0x33, 0x44}; | |
| 362 char buffer32[4]; | |
| 363 { | |
| 364 uint32_t in_memory32 = 0x11223344; | |
| 365 QuicDataWriter writer(4, buffer32, GetParam().perspective, | |
| 366 GetParam().endianness); | |
| 367 writer.WriteUInt32(in_memory32); | |
| 368 test::CompareCharArraysWithHexError( | |
| 369 "uint32_t", buffer32, 4, | |
| 370 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian32 | |
| 371 : little_endian32, | |
| 372 4); | |
| 373 | |
| 374 uint32_t read_number32; | |
| 375 QuicDataReader reader(buffer32, 4, GetParam().perspective, | |
| 376 GetParam().endianness); | |
| 377 reader.ReadUInt32(&read_number32); | |
| 378 EXPECT_EQ(in_memory32, read_number32); | |
| 379 } | |
| 380 | |
| 381 { | |
| 382 uint64_t in_memory32 = 0x11223344; | |
| 383 QuicDataWriter writer(4, buffer32, GetParam().perspective, | |
| 384 GetParam().endianness); | |
| 385 writer.WriteBytesToUInt64(4, in_memory32); | |
| 386 test::CompareCharArraysWithHexError( | |
| 387 "uint32_t", buffer32, 4, | |
| 388 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian32 | |
| 389 : little_endian32, | |
| 390 4); | |
| 391 | |
| 392 uint64_t read_number32 = 0u; | |
| 393 QuicDataReader reader(buffer32, 4, GetParam().perspective, | |
| 394 GetParam().endianness); | |
| 395 reader.ReadBytesToUInt64(4, &read_number32); | |
| 396 EXPECT_EQ(in_memory32, read_number32); | |
| 397 } | |
| 398 } | |
| 399 | |
| 400 TEST_P(QuicDataWriterTest, Write40BitUnsignedIntegers) { | |
| 401 uint64_t in_memory40 = 0x0000001122334455; | |
| 402 char little_endian40[] = {0x55, 0x44, 0x33, 0x22, 0x11}; | |
| 403 char big_endian40[] = {0x11, 0x22, 0x33, 0x44, 0x55}; | |
| 404 char buffer40[5]; | |
| 405 QuicDataWriter writer(5, buffer40, GetParam().perspective, | |
| 406 GetParam().endianness); | |
| 407 writer.WriteBytesToUInt64(5, in_memory40); | |
| 408 test::CompareCharArraysWithHexError( | |
| 409 "uint40", buffer40, 5, | |
| 410 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian40 | |
| 411 : little_endian40, | |
| 412 5); | |
| 413 | |
| 414 uint64_t read_number40 = 0u; | |
| 415 QuicDataReader reader(buffer40, 5, GetParam().perspective, | |
| 416 GetParam().endianness); | |
| 417 reader.ReadBytesToUInt64(5, &read_number40); | |
| 418 EXPECT_EQ(in_memory40, read_number40); | |
| 419 } | |
| 420 | |
| 421 TEST_P(QuicDataWriterTest, Write48BitUnsignedIntegers) { | |
| 422 uint64_t in_memory48 = 0x0000112233445566; | |
| 423 char little_endian48[] = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11}; | |
| 424 char big_endian48[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; | |
| 425 char buffer48[6]; | |
| 426 QuicDataWriter writer(6, buffer48, GetParam().perspective, | |
| 427 GetParam().endianness); | |
| 428 writer.WriteBytesToUInt64(6, in_memory48); | |
| 429 test::CompareCharArraysWithHexError( | |
| 430 "uint48", buffer48, 6, | |
| 431 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian48 | |
| 432 : little_endian48, | |
| 433 6); | |
| 434 | |
| 435 uint64_t read_number48 = 0u; | |
| 436 QuicDataReader reader(buffer48, 6, GetParam().perspective, | |
| 437 GetParam().endianness); | |
| 438 reader.ReadBytesToUInt64(6., &read_number48); | |
| 439 EXPECT_EQ(in_memory48, read_number48); | |
| 440 } | |
| 441 | |
| 442 TEST_P(QuicDataWriterTest, Write56BitUnsignedIntegers) { | |
| 443 uint64_t in_memory56 = 0x0011223344556677; | |
| 444 char little_endian56[] = {0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11}; | |
| 445 char big_endian56[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; | |
| 446 char buffer56[7]; | |
| 447 QuicDataWriter writer(7, buffer56, GetParam().perspective, | |
| 448 GetParam().endianness); | |
| 449 writer.WriteBytesToUInt64(7, in_memory56); | |
| 450 test::CompareCharArraysWithHexError( | |
| 451 "uint56", buffer56, 7, | |
| 452 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian56 | |
| 453 : little_endian56, | |
| 454 7); | |
| 455 | |
| 456 uint64_t read_number56 = 0u; | |
| 457 QuicDataReader reader(buffer56, 7, GetParam().perspective, | |
| 458 GetParam().endianness); | |
| 459 reader.ReadBytesToUInt64(7, &read_number56); | |
| 460 EXPECT_EQ(in_memory56, read_number56); | |
| 461 } | |
| 462 | |
| 463 TEST_P(QuicDataWriterTest, Write64BitUnsignedIntegers) { | |
| 464 uint64_t in_memory64 = 0x1122334455667788; | |
| 465 char little_endian64[] = {0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11}; | |
| 466 char big_endian64[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; | |
| 467 char buffer64[8]; | |
| 468 QuicDataWriter writer(8, buffer64, GetParam().perspective, | |
| 469 GetParam().endianness); | |
| 470 writer.WriteBytesToUInt64(8, in_memory64); | |
| 471 test::CompareCharArraysWithHexError( | |
| 472 "uint64_t", buffer64, 8, | |
| 473 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian64 | |
| 474 : little_endian64, | |
| 475 8); | |
| 476 | |
| 477 uint64_t read_number64 = 0u; | |
| 478 QuicDataReader reader(buffer64, 8, GetParam().perspective, | |
| 479 GetParam().endianness); | |
| 480 reader.ReadBytesToUInt64(8, &read_number64); | |
| 481 EXPECT_EQ(in_memory64, read_number64); | |
| 482 | |
| 483 QuicDataWriter writer2(8, buffer64, GetParam().perspective, | |
| 484 GetParam().endianness); | |
| 485 writer2.WriteUInt64(in_memory64); | |
| 486 test::CompareCharArraysWithHexError( | |
| 487 "uint64_t", buffer64, 8, | |
| 488 GetParam().endianness == NETWORK_BYTE_ORDER ? big_endian64 | |
| 489 : little_endian64, | |
| 490 8); | |
| 491 read_number64 = 0u; | |
| 492 QuicDataReader reader2(buffer64, 8, GetParam().perspective, | |
| 493 GetParam().endianness); | |
| 494 reader2.ReadUInt64(&read_number64); | |
| 495 EXPECT_EQ(in_memory64, read_number64); | |
| 496 } | |
| 497 | |
| 498 TEST_P(QuicDataWriterTest, WriteIntegers) { | |
| 499 char buf[43]; | |
| 500 uint8_t i8 = 0x01; | |
| 501 uint16_t i16 = 0x0123; | |
| 502 uint32_t i32 = 0x01234567; | |
| 503 uint64_t i64 = 0x0123456789ABCDEF; | |
| 504 QuicDataWriter writer(46, buf, GetParam().perspective, GetParam().endianness); | |
| 505 for (size_t i = 0; i < 10; ++i) { | |
| 506 switch (i) { | |
| 507 case 0u: | |
| 508 EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); | |
| 509 break; | |
| 510 case 1u: | |
| 511 EXPECT_TRUE(writer.WriteUInt8(i8)); | |
| 512 EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); | |
| 513 break; | |
| 514 case 2u: | |
| 515 EXPECT_TRUE(writer.WriteUInt16(i16)); | |
| 516 EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); | |
| 517 break; | |
| 518 case 3u: | |
| 519 EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); | |
| 520 break; | |
| 521 case 4u: | |
| 522 EXPECT_TRUE(writer.WriteUInt32(i32)); | |
| 523 EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); | |
| 524 break; | |
| 525 case 5u: | |
| 526 case 6u: | |
| 527 case 7u: | |
| 528 case 8u: | |
| 529 EXPECT_TRUE(writer.WriteBytesToUInt64(i, i64)); | |
| 530 break; | |
| 531 default: | |
| 532 EXPECT_FALSE(writer.WriteBytesToUInt64(i, i64)); | |
| 533 } | |
| 534 } | |
| 535 | |
| 536 QuicDataReader reader(buf, 46, GetParam().perspective, GetParam().endianness); | |
| 537 for (size_t i = 0; i < 10; ++i) { | |
| 538 uint8_t read8; | |
| 539 uint16_t read16; | |
| 540 uint32_t read32; | |
| 541 uint64_t read64 = 0u; | |
| 542 switch (i) { | |
| 543 case 0u: | |
| 544 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 545 EXPECT_EQ(0u, read64); | |
| 546 break; | |
| 547 case 1u: | |
| 548 EXPECT_TRUE(reader.ReadUInt8(&read8)); | |
| 549 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 550 EXPECT_EQ(i8, read8); | |
| 551 EXPECT_EQ(0xEFu, read64); | |
| 552 break; | |
| 553 case 2u: | |
| 554 EXPECT_TRUE(reader.ReadUInt16(&read16)); | |
| 555 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 556 EXPECT_EQ(i16, read16); | |
| 557 EXPECT_EQ(0xCDEFu, read64); | |
| 558 break; | |
| 559 case 3u: | |
| 560 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 561 EXPECT_EQ(0xABCDEFu, read64); | |
| 562 break; | |
| 563 case 4u: | |
| 564 EXPECT_TRUE(reader.ReadUInt32(&read32)); | |
| 565 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 566 EXPECT_EQ(i32, read32); | |
| 567 EXPECT_EQ(0x89ABCDEFu, read64); | |
| 568 break; | |
| 569 case 5u: | |
| 570 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 571 EXPECT_EQ(0x6789ABCDEFu, read64); | |
| 572 break; | |
| 573 case 6u: | |
| 574 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 575 EXPECT_EQ(0x456789ABCDEFu, read64); | |
| 576 break; | |
| 577 case 7u: | |
| 578 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 579 EXPECT_EQ(0x23456789ABCDEFu, read64); | |
| 580 break; | |
| 581 case 8u: | |
| 582 EXPECT_TRUE(reader.ReadBytesToUInt64(i, &read64)); | |
| 583 EXPECT_EQ(0x0123456789ABCDEFu, read64); | |
| 584 break; | |
| 585 default: | |
| 586 EXPECT_FALSE(reader.ReadBytesToUInt64(i, &read64)); | |
| 587 } | |
| 588 } | |
| 589 } | |
| 590 | |
| 246 } // namespace | 591 } // namespace |
| 247 } // namespace test | 592 } // namespace test |
| 248 } // namespace net | 593 } // namespace net |
| OLD | NEW |