| 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/quic_framer.h" | 5 #include "net/quic/quic_framer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 associated_data_ = associated_data.as_string(); | 182 associated_data_ = associated_data.as_string(); |
| 183 ciphertext_ = ciphertext.as_string(); | 183 ciphertext_ = ciphertext.as_string(); |
| 184 memcpy(output, ciphertext.data(), ciphertext.length()); | 184 memcpy(output, ciphertext.data(), ciphertext.length()); |
| 185 *output_length = ciphertext.length(); | 185 *output_length = ciphertext.length(); |
| 186 return true; | 186 return true; |
| 187 } | 187 } |
| 188 StringPiece GetKey() const override { return StringPiece(); } | 188 StringPiece GetKey() const override { return StringPiece(); } |
| 189 StringPiece GetNoncePrefix() const override { return StringPiece(); } | 189 StringPiece GetNoncePrefix() const override { return StringPiece(); } |
| 190 const char* cipher_name() const override { return "Test"; } | 190 const char* cipher_name() const override { return "Test"; } |
| 191 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. | 191 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. |
| 192 uint32 cipher_id() const override { return 0xFFFFFFF2; } | 192 uint32_t cipher_id() const override { return 0xFFFFFFF2; } |
| 193 QuicPacketNumber packet_number_; | 193 QuicPacketNumber packet_number_; |
| 194 string associated_data_; | 194 string associated_data_; |
| 195 string ciphertext_; | 195 string ciphertext_; |
| 196 }; | 196 }; |
| 197 | 197 |
| 198 class TestQuicVisitor : public QuicFramerVisitorInterface { | 198 class TestQuicVisitor : public QuicFramerVisitorInterface { |
| 199 public: | 199 public: |
| 200 TestQuicVisitor() | 200 TestQuicVisitor() |
| 201 : error_count_(0), | 201 : error_count_(0), |
| 202 version_mismatch_(0), | 202 version_mismatch_(0), |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 QuicFramerTest, | 507 QuicFramerTest, |
| 508 ::testing::ValuesIn(kSupportedQuicVersions)); | 508 ::testing::ValuesIn(kSupportedQuicVersions)); |
| 509 | 509 |
| 510 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochStart) { | 510 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochStart) { |
| 511 // A few quick manual sanity checks. | 511 // A few quick manual sanity checks. |
| 512 CheckCalculatePacketNumber(UINT64_C(1), UINT64_C(0)); | 512 CheckCalculatePacketNumber(UINT64_C(1), UINT64_C(0)); |
| 513 CheckCalculatePacketNumber(kEpoch + 1, kMask); | 513 CheckCalculatePacketNumber(kEpoch + 1, kMask); |
| 514 CheckCalculatePacketNumber(kEpoch, kMask); | 514 CheckCalculatePacketNumber(kEpoch, kMask); |
| 515 | 515 |
| 516 // Cases where the last number was close to the start of the range. | 516 // Cases where the last number was close to the start of the range. |
| 517 for (uint64 last = 0; last < 10; last++) { | 517 for (uint64_t last = 0; last < 10; last++) { |
| 518 // Small numbers should not wrap (even if they're out of order). | 518 // Small numbers should not wrap (even if they're out of order). |
| 519 for (uint64 j = 0; j < 10; j++) { | 519 for (uint64_t j = 0; j < 10; j++) { |
| 520 CheckCalculatePacketNumber(j, last); | 520 CheckCalculatePacketNumber(j, last); |
| 521 } | 521 } |
| 522 | 522 |
| 523 // Large numbers should not wrap either (because we're near 0 already). | 523 // Large numbers should not wrap either (because we're near 0 already). |
| 524 for (uint64 j = 0; j < 10; j++) { | 524 for (uint64_t j = 0; j < 10; j++) { |
| 525 CheckCalculatePacketNumber(kEpoch - 1 - j, last); | 525 CheckCalculatePacketNumber(kEpoch - 1 - j, last); |
| 526 } | 526 } |
| 527 } | 527 } |
| 528 } | 528 } |
| 529 | 529 |
| 530 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochEnd) { | 530 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochEnd) { |
| 531 // Cases where the last number was close to the end of the range | 531 // Cases where the last number was close to the end of the range |
| 532 for (uint64 i = 0; i < 10; i++) { | 532 for (uint64_t i = 0; i < 10; i++) { |
| 533 QuicPacketNumber last = kEpoch - i; | 533 QuicPacketNumber last = kEpoch - i; |
| 534 | 534 |
| 535 // Small numbers should wrap. | 535 // Small numbers should wrap. |
| 536 for (uint64 j = 0; j < 10; j++) { | 536 for (uint64_t j = 0; j < 10; j++) { |
| 537 CheckCalculatePacketNumber(kEpoch + j, last); | 537 CheckCalculatePacketNumber(kEpoch + j, last); |
| 538 } | 538 } |
| 539 | 539 |
| 540 // Large numbers should not (even if they're out of order). | 540 // Large numbers should not (even if they're out of order). |
| 541 for (uint64 j = 0; j < 10; j++) { | 541 for (uint64_t j = 0; j < 10; j++) { |
| 542 CheckCalculatePacketNumber(kEpoch - 1 - j, last); | 542 CheckCalculatePacketNumber(kEpoch - 1 - j, last); |
| 543 } | 543 } |
| 544 } | 544 } |
| 545 } | 545 } |
| 546 | 546 |
| 547 // Next check where we're in a non-zero epoch to verify we handle | 547 // Next check where we're in a non-zero epoch to verify we handle |
| 548 // reverse wrapping, too. | 548 // reverse wrapping, too. |
| 549 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearPrevEpoch) { | 549 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearPrevEpoch) { |
| 550 const uint64 prev_epoch = 1 * kEpoch; | 550 const uint64_t prev_epoch = 1 * kEpoch; |
| 551 const uint64 cur_epoch = 2 * kEpoch; | 551 const uint64_t cur_epoch = 2 * kEpoch; |
| 552 // Cases where the last number was close to the start of the range | 552 // Cases where the last number was close to the start of the range |
| 553 for (uint64 i = 0; i < 10; i++) { | 553 for (uint64_t i = 0; i < 10; i++) { |
| 554 uint64 last = cur_epoch + i; | 554 uint64_t last = cur_epoch + i; |
| 555 // Small number should not wrap (even if they're out of order). | 555 // Small number should not wrap (even if they're out of order). |
| 556 for (uint64 j = 0; j < 10; j++) { | 556 for (uint64_t j = 0; j < 10; j++) { |
| 557 CheckCalculatePacketNumber(cur_epoch + j, last); | 557 CheckCalculatePacketNumber(cur_epoch + j, last); |
| 558 } | 558 } |
| 559 | 559 |
| 560 // But large numbers should reverse wrap. | 560 // But large numbers should reverse wrap. |
| 561 for (uint64 j = 0; j < 10; j++) { | 561 for (uint64_t j = 0; j < 10; j++) { |
| 562 uint64 num = kEpoch - 1 - j; | 562 uint64_t num = kEpoch - 1 - j; |
| 563 CheckCalculatePacketNumber(prev_epoch + num, last); | 563 CheckCalculatePacketNumber(prev_epoch + num, last); |
| 564 } | 564 } |
| 565 } | 565 } |
| 566 } | 566 } |
| 567 | 567 |
| 568 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextEpoch) { | 568 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextEpoch) { |
| 569 const uint64 cur_epoch = 2 * kEpoch; | 569 const uint64_t cur_epoch = 2 * kEpoch; |
| 570 const uint64 next_epoch = 3 * kEpoch; | 570 const uint64_t next_epoch = 3 * kEpoch; |
| 571 // Cases where the last number was close to the end of the range | 571 // Cases where the last number was close to the end of the range |
| 572 for (uint64 i = 0; i < 10; i++) { | 572 for (uint64_t i = 0; i < 10; i++) { |
| 573 QuicPacketNumber last = next_epoch - 1 - i; | 573 QuicPacketNumber last = next_epoch - 1 - i; |
| 574 | 574 |
| 575 // Small numbers should wrap. | 575 // Small numbers should wrap. |
| 576 for (uint64 j = 0; j < 10; j++) { | 576 for (uint64_t j = 0; j < 10; j++) { |
| 577 CheckCalculatePacketNumber(next_epoch + j, last); | 577 CheckCalculatePacketNumber(next_epoch + j, last); |
| 578 } | 578 } |
| 579 | 579 |
| 580 // but large numbers should not (even if they're out of order). | 580 // but large numbers should not (even if they're out of order). |
| 581 for (uint64 j = 0; j < 10; j++) { | 581 for (uint64_t j = 0; j < 10; j++) { |
| 582 uint64 num = kEpoch - 1 - j; | 582 uint64_t num = kEpoch - 1 - j; |
| 583 CheckCalculatePacketNumber(cur_epoch + num, last); | 583 CheckCalculatePacketNumber(cur_epoch + num, last); |
| 584 } | 584 } |
| 585 } | 585 } |
| 586 } | 586 } |
| 587 | 587 |
| 588 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextMax) { | 588 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextMax) { |
| 589 const uint64 max_number = numeric_limits<uint64>::max(); | 589 const uint64_t max_number = numeric_limits<uint64_t>::max(); |
| 590 const uint64 max_epoch = max_number & ~kMask; | 590 const uint64_t max_epoch = max_number & ~kMask; |
| 591 | 591 |
| 592 // Cases where the last number was close to the end of the range | 592 // Cases where the last number was close to the end of the range |
| 593 for (uint64 i = 0; i < 10; i++) { | 593 for (uint64_t i = 0; i < 10; i++) { |
| 594 // Subtract 1, because the expected next packet number is 1 more than the | 594 // Subtract 1, because the expected next packet number is 1 more than the |
| 595 // last packet number. | 595 // last packet number. |
| 596 QuicPacketNumber last = max_number - i - 1; | 596 QuicPacketNumber last = max_number - i - 1; |
| 597 | 597 |
| 598 // Small numbers should not wrap, because they have nowhere to go. | 598 // Small numbers should not wrap, because they have nowhere to go. |
| 599 for (uint64 j = 0; j < 10; j++) { | 599 for (uint64_t j = 0; j < 10; j++) { |
| 600 CheckCalculatePacketNumber(max_epoch + j, last); | 600 CheckCalculatePacketNumber(max_epoch + j, last); |
| 601 } | 601 } |
| 602 | 602 |
| 603 // Large numbers should not wrap either. | 603 // Large numbers should not wrap either. |
| 604 for (uint64 j = 0; j < 10; j++) { | 604 for (uint64_t j = 0; j < 10; j++) { |
| 605 uint64 num = kEpoch - 1 - j; | 605 uint64_t num = kEpoch - 1 - j; |
| 606 CheckCalculatePacketNumber(max_epoch + num, last); | 606 CheckCalculatePacketNumber(max_epoch + num, last); |
| 607 } | 607 } |
| 608 } | 608 } |
| 609 } | 609 } |
| 610 | 610 |
| 611 TEST_P(QuicFramerTest, EmptyPacket) { | 611 TEST_P(QuicFramerTest, EmptyPacket) { |
| 612 char packet[] = {0x00}; | 612 char packet[] = {0x00}; |
| 613 QuicEncryptedPacket encrypted(packet, 0, false); | 613 QuicEncryptedPacket encrypted(packet, 0, false); |
| 614 EXPECT_FALSE(framer_.ProcessPacket(encrypted)); | 614 EXPECT_FALSE(framer_.ProcessPacket(encrypted)); |
| 615 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error()); | 615 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error()); |
| (...skipping 2378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2994 0x34, 0x12, | 2994 0x34, 0x12, |
| 2995 // private flags | 2995 // private flags |
| 2996 0x00, | 2996 0x00, |
| 2997 | 2997 |
| 2998 // frame type (padding frame) | 2998 // frame type (padding frame) |
| 2999 0x00, | 2999 0x00, |
| 3000 0x00, 0x00, 0x00, 0x00 | 3000 0x00, 0x00, 0x00, 0x00 |
| 3001 }; | 3001 }; |
| 3002 // clang-format on | 3002 // clang-format on |
| 3003 | 3003 |
| 3004 uint64 header_size = GetPacketHeaderSize( | 3004 uint64_t header_size = GetPacketHeaderSize( |
| 3005 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, | 3005 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, |
| 3006 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); | 3006 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); |
| 3007 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); | 3007 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); |
| 3008 | 3008 |
| 3009 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); | 3009 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); |
| 3010 ASSERT_TRUE(data != nullptr); | 3010 ASSERT_TRUE(data != nullptr); |
| 3011 | 3011 |
| 3012 test::CompareCharArraysWithHexError("constructed packet", data->data(), | 3012 test::CompareCharArraysWithHexError("constructed packet", data->data(), |
| 3013 data->length(), AsChars(packet), | 3013 data->length(), AsChars(packet), |
| 3014 arraysize(packet)); | 3014 arraysize(packet)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 3041 0xBC, 0x9A, 0x78, 0x56, | 3041 0xBC, 0x9A, 0x78, 0x56, |
| 3042 // private flags | 3042 // private flags |
| 3043 0x00, | 3043 0x00, |
| 3044 | 3044 |
| 3045 // frame type (padding frame) | 3045 // frame type (padding frame) |
| 3046 0x00, | 3046 0x00, |
| 3047 0x00, 0x00, 0x00, 0x00 | 3047 0x00, 0x00, 0x00, 0x00 |
| 3048 }; | 3048 }; |
| 3049 // clang-format on | 3049 // clang-format on |
| 3050 | 3050 |
| 3051 uint64 header_size = GetPacketHeaderSize( | 3051 uint64_t header_size = GetPacketHeaderSize( |
| 3052 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, | 3052 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, |
| 3053 PACKET_4BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); | 3053 PACKET_4BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); |
| 3054 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); | 3054 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); |
| 3055 | 3055 |
| 3056 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); | 3056 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); |
| 3057 ASSERT_TRUE(data != nullptr); | 3057 ASSERT_TRUE(data != nullptr); |
| 3058 | 3058 |
| 3059 test::CompareCharArraysWithHexError("constructed packet", data->data(), | 3059 test::CompareCharArraysWithHexError("constructed packet", data->data(), |
| 3060 data->length(), AsChars(packet), | 3060 data->length(), AsChars(packet), |
| 3061 arraysize(packet)); | 3061 arraysize(packet)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 3088 0xBC, 0x9A, | 3088 0xBC, 0x9A, |
| 3089 // private flags | 3089 // private flags |
| 3090 0x00, | 3090 0x00, |
| 3091 | 3091 |
| 3092 // frame type (padding frame) | 3092 // frame type (padding frame) |
| 3093 0x00, | 3093 0x00, |
| 3094 0x00, 0x00, 0x00, 0x00 | 3094 0x00, 0x00, 0x00, 0x00 |
| 3095 }; | 3095 }; |
| 3096 // clang-format on | 3096 // clang-format on |
| 3097 | 3097 |
| 3098 uint64 header_size = GetPacketHeaderSize( | 3098 uint64_t header_size = GetPacketHeaderSize( |
| 3099 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, | 3099 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, |
| 3100 PACKET_2BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); | 3100 PACKET_2BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); |
| 3101 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); | 3101 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); |
| 3102 | 3102 |
| 3103 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); | 3103 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); |
| 3104 ASSERT_TRUE(data != nullptr); | 3104 ASSERT_TRUE(data != nullptr); |
| 3105 | 3105 |
| 3106 test::CompareCharArraysWithHexError("constructed packet", data->data(), | 3106 test::CompareCharArraysWithHexError("constructed packet", data->data(), |
| 3107 data->length(), AsChars(packet), | 3107 data->length(), AsChars(packet), |
| 3108 arraysize(packet)); | 3108 arraysize(packet)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 3135 0xBC, | 3135 0xBC, |
| 3136 // private flags | 3136 // private flags |
| 3137 0x00, | 3137 0x00, |
| 3138 | 3138 |
| 3139 // frame type (padding frame) | 3139 // frame type (padding frame) |
| 3140 0x00, | 3140 0x00, |
| 3141 0x00, 0x00, 0x00, 0x00 | 3141 0x00, 0x00, 0x00, 0x00 |
| 3142 }; | 3142 }; |
| 3143 // clang-format on | 3143 // clang-format on |
| 3144 | 3144 |
| 3145 uint64 header_size = GetPacketHeaderSize( | 3145 uint64_t header_size = GetPacketHeaderSize( |
| 3146 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, | 3146 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, !kIncludePathId, |
| 3147 PACKET_1BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); | 3147 PACKET_1BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP); |
| 3148 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); | 3148 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1); |
| 3149 | 3149 |
| 3150 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); | 3150 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames)); |
| 3151 ASSERT_TRUE(data != nullptr); | 3151 ASSERT_TRUE(data != nullptr); |
| 3152 | 3152 |
| 3153 test::CompareCharArraysWithHexError("constructed packet", data->data(), | 3153 test::CompareCharArraysWithHexError("constructed packet", data->data(), |
| 3154 data->length(), AsChars(packet), | 3154 data->length(), AsChars(packet), |
| 3155 arraysize(packet)); | 3155 arraysize(packet)); |
| (...skipping 1512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4668 'o', ' ', 'w', 'o', | 4668 'o', ' ', 'w', 'o', |
| 4669 'r', 'l', 'd', '!', | 4669 'r', 'l', 'd', '!', |
| 4670 }; | 4670 }; |
| 4671 // clang-format on | 4671 // clang-format on |
| 4672 | 4672 |
| 4673 QuicFramerFuzzFunc(packet, arraysize(packet)); | 4673 QuicFramerFuzzFunc(packet, arraysize(packet)); |
| 4674 } | 4674 } |
| 4675 | 4675 |
| 4676 } // namespace test | 4676 } // namespace test |
| 4677 } // namespace net | 4677 } // namespace net |
| OLD | NEW |