| 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_framer.h" | 5 #include "net/quic/core/quic_framer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstdint> | 8 #include <cstdint> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 const size_t kConnectionIdOffset = kPublicFlagsSize; | 50 const size_t kConnectionIdOffset = kPublicFlagsSize; |
| 51 // Index into the version string in the header. (if present). | 51 // Index into the version string in the header. (if present). |
| 52 const size_t kVersionOffset = kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID; | 52 const size_t kVersionOffset = kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID; |
| 53 | 53 |
| 54 // Size in bytes of the stream frame fields for an arbitrary StreamID and | 54 // Size in bytes of the stream frame fields for an arbitrary StreamID and |
| 55 // offset and the last frame in a packet. | 55 // offset and the last frame in a packet. |
| 56 size_t GetMinStreamFrameSize() { | 56 size_t GetMinStreamFrameSize() { |
| 57 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize; | 57 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize; |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Index into the path id offset in the header (if present). | |
| 61 size_t GetPathIdOffset(QuicConnectionIdLength connection_id_length, | |
| 62 bool include_version) { | |
| 63 return kConnectionIdOffset + connection_id_length + | |
| 64 (include_version ? kQuicVersionSize : 0); | |
| 65 } | |
| 66 | |
| 67 // Index into the packet number offset in the header. | 60 // Index into the packet number offset in the header. |
| 68 size_t GetPacketNumberOffset(QuicConnectionIdLength connection_id_length, | 61 size_t GetPacketNumberOffset(QuicConnectionIdLength connection_id_length, |
| 69 bool include_version) { | 62 bool include_version) { |
| 70 return kConnectionIdOffset + connection_id_length + | 63 return kConnectionIdOffset + connection_id_length + |
| 71 (include_version ? kQuicVersionSize : 0); | 64 (include_version ? kQuicVersionSize : 0); |
| 72 } | 65 } |
| 73 | 66 |
| 74 size_t GetPacketNumberOffset(bool include_version) { | 67 size_t GetPacketNumberOffset(bool include_version) { |
| 75 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version); | 68 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version); |
| 76 } | 69 } |
| 77 | 70 |
| 78 // Index into the private flags offset in the data packet header. | |
| 79 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length, | |
| 80 bool include_version) { | |
| 81 return GetPacketNumberOffset(connection_id_length, include_version) + | |
| 82 PACKET_6BYTE_PACKET_NUMBER; | |
| 83 } | |
| 84 | |
| 85 size_t GetPrivateFlagsOffset(bool include_version) { | |
| 86 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version); | |
| 87 } | |
| 88 | |
| 89 size_t GetPrivateFlagsOffset(bool include_version, | |
| 90 QuicPacketNumberLength packet_number_length) { | |
| 91 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version) + | |
| 92 packet_number_length; | |
| 93 } | |
| 94 | |
| 95 // Index into the message tag of the public reset packet. | 71 // Index into the message tag of the public reset packet. |
| 96 // Public resets always have full connection_ids. | 72 // Public resets always have full connection_ids. |
| 97 const size_t kPublicResetPacketMessageTagOffset = | 73 const size_t kPublicResetPacketMessageTagOffset = |
| 98 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID; | 74 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID; |
| 99 | 75 |
| 100 class TestEncrypter : public QuicEncrypter { | 76 class TestEncrypter : public QuicEncrypter { |
| 101 public: | 77 public: |
| 102 ~TestEncrypter() override {} | 78 ~TestEncrypter() override {} |
| 103 bool SetKey(QuicStringPiece key) override { return true; } | 79 bool SetKey(QuicStringPiece key) override { return true; } |
| 104 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override { return true; } | 80 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override { return true; } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 122 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { | 98 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { |
| 123 return ciphertext_size; | 99 return ciphertext_size; |
| 124 } | 100 } |
| 125 size_t GetCiphertextSize(size_t plaintext_size) const override { | 101 size_t GetCiphertextSize(size_t plaintext_size) const override { |
| 126 return plaintext_size; | 102 return plaintext_size; |
| 127 } | 103 } |
| 128 QuicStringPiece GetKey() const override { return QuicStringPiece(); } | 104 QuicStringPiece GetKey() const override { return QuicStringPiece(); } |
| 129 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); } | 105 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); } |
| 130 | 106 |
| 131 QuicVersion version_; | 107 QuicVersion version_; |
| 132 Perspective perspective_; | |
| 133 QuicPacketNumber packet_number_; | 108 QuicPacketNumber packet_number_; |
| 134 string associated_data_; | 109 string associated_data_; |
| 135 string plaintext_; | 110 string plaintext_; |
| 136 }; | 111 }; |
| 137 | 112 |
| 138 class TestDecrypter : public QuicDecrypter { | 113 class TestDecrypter : public QuicDecrypter { |
| 139 public: | 114 public: |
| 140 ~TestDecrypter() override {} | 115 ~TestDecrypter() override {} |
| 141 bool SetKey(QuicStringPiece key) override { return true; } | 116 bool SetKey(QuicStringPiece key) override { return true; } |
| 142 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override { return true; } | 117 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override { return true; } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 161 memcpy(output, ciphertext.data(), ciphertext.length()); | 136 memcpy(output, ciphertext.data(), ciphertext.length()); |
| 162 *output_length = ciphertext.length(); | 137 *output_length = ciphertext.length(); |
| 163 return true; | 138 return true; |
| 164 } | 139 } |
| 165 QuicStringPiece GetKey() const override { return QuicStringPiece(); } | 140 QuicStringPiece GetKey() const override { return QuicStringPiece(); } |
| 166 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); } | 141 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); } |
| 167 const char* cipher_name() const override { return "Test"; } | 142 const char* cipher_name() const override { return "Test"; } |
| 168 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. | 143 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. |
| 169 uint32_t cipher_id() const override { return 0xFFFFFFF2; } | 144 uint32_t cipher_id() const override { return 0xFFFFFFF2; } |
| 170 QuicVersion version_; | 145 QuicVersion version_; |
| 171 Perspective perspective_; | |
| 172 QuicPacketNumber packet_number_; | 146 QuicPacketNumber packet_number_; |
| 173 string associated_data_; | 147 string associated_data_; |
| 174 string ciphertext_; | 148 string ciphertext_; |
| 175 }; | 149 }; |
| 176 | 150 |
| 177 class TestQuicVisitor : public QuicFramerVisitorInterface { | 151 class TestQuicVisitor : public QuicFramerVisitorInterface { |
| 178 public: | 152 public: |
| 179 TestQuicVisitor() | 153 TestQuicVisitor() |
| 180 : error_count_(0), | 154 : error_count_(0), |
| 181 version_mismatch_(0), | 155 version_mismatch_(0), |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override { | 255 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override { |
| 282 window_update_frame_ = frame; | 256 window_update_frame_ = frame; |
| 283 return true; | 257 return true; |
| 284 } | 258 } |
| 285 | 259 |
| 286 bool OnBlockedFrame(const QuicBlockedFrame& frame) override { | 260 bool OnBlockedFrame(const QuicBlockedFrame& frame) override { |
| 287 blocked_frame_ = frame; | 261 blocked_frame_ = frame; |
| 288 return true; | 262 return true; |
| 289 } | 263 } |
| 290 | 264 |
| 291 bool OnPathCloseFrame(const QuicPathCloseFrame& frame) override { | |
| 292 path_close_frame_ = frame; | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 // Counters from the visitor_ callbacks. | 265 // Counters from the visitor_ callbacks. |
| 297 int error_count_; | 266 int error_count_; |
| 298 int version_mismatch_; | 267 int version_mismatch_; |
| 299 int packet_count_; | 268 int packet_count_; |
| 300 int frame_count_; | 269 int frame_count_; |
| 301 int complete_packets_; | 270 int complete_packets_; |
| 302 bool accept_packet_; | 271 bool accept_packet_; |
| 303 bool accept_public_header_; | 272 bool accept_public_header_; |
| 304 | 273 |
| 305 std::unique_ptr<QuicPacketHeader> header_; | 274 std::unique_ptr<QuicPacketHeader> header_; |
| 306 std::unique_ptr<QuicPacketPublicHeader> public_header_; | 275 std::unique_ptr<QuicPacketPublicHeader> public_header_; |
| 307 std::unique_ptr<QuicPublicResetPacket> public_reset_packet_; | 276 std::unique_ptr<QuicPublicResetPacket> public_reset_packet_; |
| 308 std::unique_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_; | 277 std::unique_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_; |
| 309 std::vector<std::unique_ptr<QuicStreamFrame>> stream_frames_; | 278 std::vector<std::unique_ptr<QuicStreamFrame>> stream_frames_; |
| 310 std::vector<std::unique_ptr<QuicAckFrame>> ack_frames_; | 279 std::vector<std::unique_ptr<QuicAckFrame>> ack_frames_; |
| 311 std::vector<std::unique_ptr<QuicStopWaitingFrame>> stop_waiting_frames_; | 280 std::vector<std::unique_ptr<QuicStopWaitingFrame>> stop_waiting_frames_; |
| 312 std::vector<std::unique_ptr<QuicPaddingFrame>> padding_frames_; | 281 std::vector<std::unique_ptr<QuicPaddingFrame>> padding_frames_; |
| 313 std::vector<std::unique_ptr<QuicPingFrame>> ping_frames_; | 282 std::vector<std::unique_ptr<QuicPingFrame>> ping_frames_; |
| 314 QuicRstStreamFrame rst_stream_frame_; | 283 QuicRstStreamFrame rst_stream_frame_; |
| 315 QuicConnectionCloseFrame connection_close_frame_; | 284 QuicConnectionCloseFrame connection_close_frame_; |
| 316 QuicGoAwayFrame goaway_frame_; | 285 QuicGoAwayFrame goaway_frame_; |
| 317 QuicWindowUpdateFrame window_update_frame_; | 286 QuicWindowUpdateFrame window_update_frame_; |
| 318 QuicBlockedFrame blocked_frame_; | 287 QuicBlockedFrame blocked_frame_; |
| 319 QuicPathCloseFrame path_close_frame_; | |
| 320 std::vector<std::unique_ptr<string>> stream_data_; | 288 std::vector<std::unique_ptr<string>> stream_data_; |
| 321 }; | 289 }; |
| 322 | 290 |
| 323 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> { | 291 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> { |
| 324 public: | 292 public: |
| 325 QuicFramerTest() | 293 QuicFramerTest() |
| 326 : encrypter_(new test::TestEncrypter()), | 294 : encrypter_(new test::TestEncrypter()), |
| 327 decrypter_(new test::TestDecrypter()), | 295 decrypter_(new test::TestDecrypter()), |
| 328 start_(QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(0x10)), | 296 start_(QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(0x10)), |
| 329 framer_(AllSupportedVersions(), start_, Perspective::IS_SERVER) { | 297 framer_(AllSupportedVersions(), start_, Perspective::IS_SERVER) { |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 EXPECT_EQ(kConnectionId, visitor_.header_->public_header.connection_id); | 902 EXPECT_EQ(kConnectionId, visitor_.header_->public_header.connection_id); |
| 935 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, | 903 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 936 visitor_.header_->public_header.packet_number_length); | 904 visitor_.header_->public_header.packet_number_length); |
| 937 EXPECT_EQ(kPacketNumber - 1, visitor_.header_->packet_number); | 905 EXPECT_EQ(kPacketNumber - 1, visitor_.header_->packet_number); |
| 938 } | 906 } |
| 939 | 907 |
| 940 TEST_P(QuicFramerTest, PacketWithDiversificationNonce) { | 908 TEST_P(QuicFramerTest, PacketWithDiversificationNonce) { |
| 941 // clang-format off | 909 // clang-format off |
| 942 unsigned char packet[] = { | 910 unsigned char packet[] = { |
| 943 // public flags: includes nonce flag | 911 // public flags: includes nonce flag |
| 944 0x7C, | 912 static_cast<unsigned char>( |
| 913 FLAGS_quic_reloadable_flag_quic_remove_multipath_bit ? 0x3C : 0x7C), |
| 945 // connection_id | 914 // connection_id |
| 946 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, | 915 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, |
| 947 // nonce | 916 // nonce |
| 948 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 917 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 949 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | 918 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 950 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | 919 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 951 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, | 920 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 952 // packet number | 921 // packet number |
| 953 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, | 922 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, |
| 954 | 923 |
| 955 // frame type (padding) | 924 // frame type (padding) |
| 956 0x00, | 925 0x00, |
| 957 0x00, 0x00, 0x00, 0x00 | 926 0x00, 0x00, 0x00, 0x00 |
| 958 }; | 927 }; |
| 959 // clang-format on | 928 // clang-format on |
| 960 | 929 |
| 961 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false); | 930 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false); |
| 962 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT); | 931 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT); |
| 963 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); | 932 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); |
| 964 ASSERT_TRUE(visitor_.public_header_->nonce != nullptr); | 933 ASSERT_TRUE(visitor_.public_header_->nonce != nullptr); |
| 965 for (char i = 0; i < 32; ++i) { | 934 for (char i = 0; i < 32; ++i) { |
| 966 EXPECT_EQ(i, (*visitor_.public_header_->nonce)[static_cast<size_t>(i)]); | 935 EXPECT_EQ(i, (*visitor_.public_header_->nonce)[static_cast<size_t>(i)]); |
| 967 } | 936 } |
| 968 }; | 937 }; |
| 969 | 938 |
| 970 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) { | 939 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) { |
| 971 // clang-format off | 940 // clang-format off |
| 972 unsigned char packet[] = { | 941 unsigned char packet[] = { |
| 973 // public flags (8 byte connection_id, version flag and an unknown flag) | 942 // public flags (8 byte connection_id, version flag and an unknown flag) |
| 974 0x79, | 943 static_cast<unsigned char>( |
| 944 FLAGS_quic_reloadable_flag_quic_remove_multipath_bit ? 0x39 : 0x79), |
| 975 // connection_id | 945 // connection_id |
| 976 0x10, 0x32, 0x54, 0x76, | 946 0x10, 0x32, 0x54, 0x76, |
| 977 0x98, 0xBA, 0xDC, 0xFE, | 947 0x98, 0xBA, 0xDC, 0xFE, |
| 978 // version tag | 948 // version tag |
| 979 'Q', '0', '0', '0', | 949 'Q', '0', '0', '0', |
| 980 // packet number | 950 // packet number |
| 981 0xBC, 0x9A, 0x78, 0x56, | 951 0xBC, 0x9A, 0x78, 0x56, |
| 982 0x34, 0x12, | 952 0x34, 0x12, |
| 983 | 953 |
| 984 // frame type (padding frame) | 954 // frame type (padding frame) |
| (...skipping 2576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3561 TEST_P(QuicFramerTest, ConstructEncryptedPacket) { | 3531 TEST_P(QuicFramerTest, ConstructEncryptedPacket) { |
| 3562 // Since we are using ConstructEncryptedPacket, we have to set the framer's | 3532 // Since we are using ConstructEncryptedPacket, we have to set the framer's |
| 3563 // crypto to be Null. | 3533 // crypto to be Null. |
| 3564 framer_.SetDecrypter(ENCRYPTION_NONE, | 3534 framer_.SetDecrypter(ENCRYPTION_NONE, |
| 3565 new NullDecrypter(framer_.perspective())); | 3535 new NullDecrypter(framer_.perspective())); |
| 3566 framer_.SetEncrypter(ENCRYPTION_NONE, | 3536 framer_.SetEncrypter(ENCRYPTION_NONE, |
| 3567 new NullEncrypter(framer_.perspective())); | 3537 new NullEncrypter(framer_.perspective())); |
| 3568 QuicVersionVector versions; | 3538 QuicVersionVector versions; |
| 3569 versions.push_back(framer_.version()); | 3539 versions.push_back(framer_.version()); |
| 3570 std::unique_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket( | 3540 std::unique_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket( |
| 3571 42, false, false, false, kTestQuicStreamId, kTestString, | 3541 42, false, false, kTestQuicStreamId, kTestString, |
| 3572 PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER, &versions)); | 3542 PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER, &versions)); |
| 3573 | 3543 |
| 3574 MockFramerVisitor visitor; | 3544 MockFramerVisitor visitor; |
| 3575 framer_.set_visitor(&visitor); | 3545 framer_.set_visitor(&visitor); |
| 3576 EXPECT_CALL(visitor, OnPacket()).Times(1); | 3546 EXPECT_CALL(visitor, OnPacket()).Times(1); |
| 3577 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)) | 3547 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)) |
| 3578 .Times(1) | 3548 .Times(1) |
| 3579 .WillOnce(Return(true)); | 3549 .WillOnce(Return(true)); |
| 3580 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_)) | 3550 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_)) |
| 3581 .Times(1) | 3551 .Times(1) |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3684 'o', ' ', 'w', 'o', | 3654 'o', ' ', 'w', 'o', |
| 3685 'r', 'l', 'd', '!', | 3655 'r', 'l', 'd', '!', |
| 3686 }; | 3656 }; |
| 3687 // clang-format on | 3657 // clang-format on |
| 3688 | 3658 |
| 3689 QuicFramerFuzzFunc(packet, arraysize(packet)); | 3659 QuicFramerFuzzFunc(packet, arraysize(packet)); |
| 3690 } | 3660 } |
| 3691 | 3661 |
| 3692 } // namespace test | 3662 } // namespace test |
| 3693 } // namespace net | 3663 } // namespace net |
| OLD | NEW |