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/crypto/crypto_framer.h" | 5 #include "net/quic/core/crypto/crypto_framer.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "net/quic/core/crypto/crypto_handshake.h" | 11 #include "net/quic/core/crypto/crypto_handshake.h" |
12 #include "net/quic/core/crypto/crypto_protocol.h" | 12 #include "net/quic/core/crypto/crypto_protocol.h" |
13 #include "net/quic/core/quic_packets.h" | 13 #include "net/quic/core/quic_packets.h" |
14 #include "net/quic/platform/api/quic_logging.h" | 14 #include "net/quic/platform/api/quic_logging.h" |
15 #include "net/quic/test_tools/crypto_test_utils.h" | 15 #include "net/quic/test_tools/crypto_test_utils.h" |
16 #include "net/quic/test_tools/quic_test_utils.h" | 16 #include "net/quic/test_tools/quic_test_utils.h" |
17 | 17 |
18 using base::StringPiece; | |
19 using std::string; | 18 using std::string; |
20 | 19 |
21 namespace net { | 20 namespace net { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 char* AsChars(unsigned char* data) { | 24 char* AsChars(unsigned char* data) { |
26 return reinterpret_cast<char*>(data); | 25 return reinterpret_cast<char*>(data); |
27 } | 26 } |
28 | 27 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 0x79, 0x56, 0x34, 0x12, | 259 0x79, 0x56, 0x34, 0x12, |
261 // end offset 2 | 260 // end offset 2 |
262 0x0b, 0x00, 0x00, 0x00, | 261 0x0b, 0x00, 0x00, 0x00, |
263 // value 1 | 262 // value 1 |
264 'a', 'b', 'c', 'd', 'e', 'f', | 263 'a', 'b', 'c', 'd', 'e', 'f', |
265 // value 2 | 264 // value 2 |
266 'g', 'h', 'i', 'j', 'k', | 265 'g', 'h', 'i', 'j', 'k', |
267 }; | 266 }; |
268 | 267 |
269 EXPECT_TRUE( | 268 EXPECT_TRUE( |
270 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input)))); | 269 framer.ProcessInput(QuicStringPiece(AsChars(input), arraysize(input)))); |
271 EXPECT_EQ(0u, framer.InputBytesRemaining()); | 270 EXPECT_EQ(0u, framer.InputBytesRemaining()); |
272 EXPECT_EQ(0, visitor.error_count_); | 271 EXPECT_EQ(0, visitor.error_count_); |
273 ASSERT_EQ(1u, visitor.messages_.size()); | 272 ASSERT_EQ(1u, visitor.messages_.size()); |
274 const CryptoHandshakeMessage& message = visitor.messages_[0]; | 273 const CryptoHandshakeMessage& message = visitor.messages_[0]; |
275 EXPECT_EQ(0xFFAA7733, message.tag()); | 274 EXPECT_EQ(0xFFAA7733, message.tag()); |
276 EXPECT_EQ(2u, message.tag_value_map().size()); | 275 EXPECT_EQ(2u, message.tag_value_map().size()); |
277 EXPECT_EQ("abcdef", crypto_test_utils::GetValueForTag(message, 0x12345678)); | 276 EXPECT_EQ("abcdef", crypto_test_utils::GetValueForTag(message, 0x12345678)); |
278 EXPECT_EQ("ghijk", crypto_test_utils::GetValueForTag(message, 0x12345679)); | 277 EXPECT_EQ("ghijk", crypto_test_utils::GetValueForTag(message, 0x12345679)); |
279 } | 278 } |
280 | 279 |
(...skipping 23 matching lines...) Expand all Loading... |
304 0x12, 0x00, 0x00, 0x00, | 303 0x12, 0x00, 0x00, 0x00, |
305 // value 1 | 304 // value 1 |
306 'a', 'b', 'c', 'd', 'e', 'f', | 305 'a', 'b', 'c', 'd', 'e', 'f', |
307 // value 2 | 306 // value 2 |
308 'g', 'h', 'i', 'j', 'k', | 307 'g', 'h', 'i', 'j', 'k', |
309 // value 3 | 308 // value 3 |
310 'l', 'm', 'n', 'o', 'p', 'q', 'r', | 309 'l', 'm', 'n', 'o', 'p', 'q', 'r', |
311 }; | 310 }; |
312 | 311 |
313 EXPECT_TRUE( | 312 EXPECT_TRUE( |
314 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input)))); | 313 framer.ProcessInput(QuicStringPiece(AsChars(input), arraysize(input)))); |
315 EXPECT_EQ(0u, framer.InputBytesRemaining()); | 314 EXPECT_EQ(0u, framer.InputBytesRemaining()); |
316 EXPECT_EQ(0, visitor.error_count_); | 315 EXPECT_EQ(0, visitor.error_count_); |
317 ASSERT_EQ(1u, visitor.messages_.size()); | 316 ASSERT_EQ(1u, visitor.messages_.size()); |
318 const CryptoHandshakeMessage& message = visitor.messages_[0]; | 317 const CryptoHandshakeMessage& message = visitor.messages_[0]; |
319 EXPECT_EQ(0xFFAA7733, message.tag()); | 318 EXPECT_EQ(0xFFAA7733, message.tag()); |
320 EXPECT_EQ(3u, message.tag_value_map().size()); | 319 EXPECT_EQ(3u, message.tag_value_map().size()); |
321 EXPECT_EQ("abcdef", crypto_test_utils::GetValueForTag(message, 0x12345678)); | 320 EXPECT_EQ("abcdef", crypto_test_utils::GetValueForTag(message, 0x12345678)); |
322 EXPECT_EQ("ghijk", crypto_test_utils::GetValueForTag(message, 0x12345679)); | 321 EXPECT_EQ("ghijk", crypto_test_utils::GetValueForTag(message, 0x12345679)); |
323 EXPECT_EQ("lmnopqr", crypto_test_utils::GetValueForTag(message, 0x1234567A)); | 322 EXPECT_EQ("lmnopqr", crypto_test_utils::GetValueForTag(message, 0x1234567A)); |
324 } | 323 } |
(...skipping 18 matching lines...) Expand all Loading... |
343 0x79, 0x56, 0x34, 0x12, | 342 0x79, 0x56, 0x34, 0x12, |
344 // end offset 2 | 343 // end offset 2 |
345 0x0b, 0x00, 0x00, 0x00, | 344 0x0b, 0x00, 0x00, 0x00, |
346 // value 1 | 345 // value 1 |
347 'a', 'b', 'c', 'd', 'e', 'f', | 346 'a', 'b', 'c', 'd', 'e', 'f', |
348 // value 2 | 347 // value 2 |
349 'g', 'h', 'i', 'j', 'k', | 348 'g', 'h', 'i', 'j', 'k', |
350 }; | 349 }; |
351 | 350 |
352 for (size_t i = 0; i < arraysize(input); i++) { | 351 for (size_t i = 0; i < arraysize(input); i++) { |
353 EXPECT_TRUE(framer.ProcessInput(StringPiece(AsChars(input) + i, 1))); | 352 EXPECT_TRUE(framer.ProcessInput(QuicStringPiece(AsChars(input) + i, 1))); |
354 } | 353 } |
355 EXPECT_EQ(0u, framer.InputBytesRemaining()); | 354 EXPECT_EQ(0u, framer.InputBytesRemaining()); |
356 ASSERT_EQ(1u, visitor.messages_.size()); | 355 ASSERT_EQ(1u, visitor.messages_.size()); |
357 const CryptoHandshakeMessage& message = visitor.messages_[0]; | 356 const CryptoHandshakeMessage& message = visitor.messages_[0]; |
358 EXPECT_EQ(0xFFAA7733, message.tag()); | 357 EXPECT_EQ(0xFFAA7733, message.tag()); |
359 EXPECT_EQ(2u, message.tag_value_map().size()); | 358 EXPECT_EQ(2u, message.tag_value_map().size()); |
360 EXPECT_EQ("abcdef", crypto_test_utils::GetValueForTag(message, 0x12345678)); | 359 EXPECT_EQ("abcdef", crypto_test_utils::GetValueForTag(message, 0x12345678)); |
361 EXPECT_EQ("ghijk", crypto_test_utils::GetValueForTag(message, 0x12345679)); | 360 EXPECT_EQ("ghijk", crypto_test_utils::GetValueForTag(message, 0x12345679)); |
362 } | 361 } |
363 | 362 |
(...skipping 13 matching lines...) Expand all Loading... |
377 0x78, 0x56, 0x34, 0x13, | 376 0x78, 0x56, 0x34, 0x13, |
378 // end offset 1 | 377 // end offset 1 |
379 0x01, 0x00, 0x00, 0x00, | 378 0x01, 0x00, 0x00, 0x00, |
380 // tag 2 | 379 // tag 2 |
381 0x79, 0x56, 0x34, 0x12, | 380 0x79, 0x56, 0x34, 0x12, |
382 // end offset 2 | 381 // end offset 2 |
383 0x02, 0x00, 0x00, 0x00, | 382 0x02, 0x00, 0x00, 0x00, |
384 }; | 383 }; |
385 | 384 |
386 EXPECT_FALSE( | 385 EXPECT_FALSE( |
387 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input)))); | 386 framer.ProcessInput(QuicStringPiece(AsChars(input), arraysize(input)))); |
388 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error()); | 387 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error()); |
389 EXPECT_EQ(1, visitor.error_count_); | 388 EXPECT_EQ(1, visitor.error_count_); |
390 } | 389 } |
391 | 390 |
392 TEST(CryptoFramerTest, ProcessEndOffsetsOutOfOrder) { | 391 TEST(CryptoFramerTest, ProcessEndOffsetsOutOfOrder) { |
393 test::TestCryptoVisitor visitor; | 392 test::TestCryptoVisitor visitor; |
394 CryptoFramer framer; | 393 CryptoFramer framer; |
395 framer.set_visitor(&visitor); | 394 framer.set_visitor(&visitor); |
396 | 395 |
397 unsigned char input[] = { | 396 unsigned char input[] = { |
398 // tag | 397 // tag |
399 0x33, 0x77, 0xAA, 0xFF, | 398 0x33, 0x77, 0xAA, 0xFF, |
400 // num entries | 399 // num entries |
401 0x02, 0x00, | 400 0x02, 0x00, |
402 // padding | 401 // padding |
403 0x00, 0x00, | 402 0x00, 0x00, |
404 // tag 1 | 403 // tag 1 |
405 0x79, 0x56, 0x34, 0x12, | 404 0x79, 0x56, 0x34, 0x12, |
406 // end offset 1 | 405 // end offset 1 |
407 0x01, 0x00, 0x00, 0x00, | 406 0x01, 0x00, 0x00, 0x00, |
408 // tag 2 | 407 // tag 2 |
409 0x78, 0x56, 0x34, 0x13, | 408 0x78, 0x56, 0x34, 0x13, |
410 // end offset 2 | 409 // end offset 2 |
411 0x00, 0x00, 0x00, 0x00, | 410 0x00, 0x00, 0x00, 0x00, |
412 }; | 411 }; |
413 | 412 |
414 EXPECT_FALSE( | 413 EXPECT_FALSE( |
415 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input)))); | 414 framer.ProcessInput(QuicStringPiece(AsChars(input), arraysize(input)))); |
416 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error()); | 415 EXPECT_EQ(QUIC_CRYPTO_TAGS_OUT_OF_ORDER, framer.error()); |
417 EXPECT_EQ(1, visitor.error_count_); | 416 EXPECT_EQ(1, visitor.error_count_); |
418 } | 417 } |
419 | 418 |
420 TEST(CryptoFramerTest, ProcessInputTooManyEntries) { | 419 TEST(CryptoFramerTest, ProcessInputTooManyEntries) { |
421 test::TestCryptoVisitor visitor; | 420 test::TestCryptoVisitor visitor; |
422 CryptoFramer framer; | 421 CryptoFramer framer; |
423 framer.set_visitor(&visitor); | 422 framer.set_visitor(&visitor); |
424 | 423 |
425 unsigned char input[] = { | 424 unsigned char input[] = { |
426 // tag | 425 // tag |
427 0x33, 0x77, 0xAA, 0xFF, | 426 0x33, 0x77, 0xAA, 0xFF, |
428 // num entries | 427 // num entries |
429 0xA0, 0x00, | 428 0xA0, 0x00, |
430 // padding | 429 // padding |
431 0x00, 0x00, | 430 0x00, 0x00, |
432 }; | 431 }; |
433 | 432 |
434 EXPECT_FALSE( | 433 EXPECT_FALSE( |
435 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input)))); | 434 framer.ProcessInput(QuicStringPiece(AsChars(input), arraysize(input)))); |
436 EXPECT_EQ(QUIC_CRYPTO_TOO_MANY_ENTRIES, framer.error()); | 435 EXPECT_EQ(QUIC_CRYPTO_TOO_MANY_ENTRIES, framer.error()); |
437 EXPECT_EQ(1, visitor.error_count_); | 436 EXPECT_EQ(1, visitor.error_count_); |
438 } | 437 } |
439 | 438 |
440 TEST(CryptoFramerTest, ProcessInputZeroLength) { | 439 TEST(CryptoFramerTest, ProcessInputZeroLength) { |
441 test::TestCryptoVisitor visitor; | 440 test::TestCryptoVisitor visitor; |
442 CryptoFramer framer; | 441 CryptoFramer framer; |
443 framer.set_visitor(&visitor); | 442 framer.set_visitor(&visitor); |
444 | 443 |
445 unsigned char input[] = { | 444 unsigned char input[] = { |
446 // tag | 445 // tag |
447 0x33, 0x77, 0xAA, 0xFF, | 446 0x33, 0x77, 0xAA, 0xFF, |
448 // num entries | 447 // num entries |
449 0x02, 0x00, | 448 0x02, 0x00, |
450 // padding | 449 // padding |
451 0x00, 0x00, | 450 0x00, 0x00, |
452 // tag 1 | 451 // tag 1 |
453 0x78, 0x56, 0x34, 0x12, | 452 0x78, 0x56, 0x34, 0x12, |
454 // end offset 1 | 453 // end offset 1 |
455 0x00, 0x00, 0x00, 0x00, | 454 0x00, 0x00, 0x00, 0x00, |
456 // tag 2 | 455 // tag 2 |
457 0x79, 0x56, 0x34, 0x12, | 456 0x79, 0x56, 0x34, 0x12, |
458 // end offset 2 | 457 // end offset 2 |
459 0x05, 0x00, 0x00, 0x00, | 458 0x05, 0x00, 0x00, 0x00, |
460 }; | 459 }; |
461 | 460 |
462 EXPECT_TRUE( | 461 EXPECT_TRUE( |
463 framer.ProcessInput(StringPiece(AsChars(input), arraysize(input)))); | 462 framer.ProcessInput(QuicStringPiece(AsChars(input), arraysize(input)))); |
464 EXPECT_EQ(0, visitor.error_count_); | 463 EXPECT_EQ(0, visitor.error_count_); |
465 } | 464 } |
466 | 465 |
467 } // namespace test | 466 } // namespace test |
468 | 467 |
469 } // namespace net | 468 } // namespace net |
OLD | NEW |