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