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 1057 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1068 FLAGS_quic_restart_flag_quic_big_endian_connection_id | 1068 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
1069 ? arraysize(packet_cid_be) | 1069 ? arraysize(packet_cid_be) |
1070 : arraysize(packet), | 1070 : arraysize(packet), |
1071 false); | 1071 false); |
1072 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT); | 1072 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT); |
1073 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); | 1073 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); |
1074 ASSERT_TRUE(visitor_.public_header_->nonce != nullptr); | 1074 ASSERT_TRUE(visitor_.public_header_->nonce != nullptr); |
1075 for (char i = 0; i < 32; ++i) { | 1075 for (char i = 0; i < 32; ++i) { |
1076 EXPECT_EQ(i, (*visitor_.public_header_->nonce)[static_cast<size_t>(i)]); | 1076 EXPECT_EQ(i, (*visitor_.public_header_->nonce)[static_cast<size_t>(i)]); |
1077 } | 1077 } |
| 1078 EXPECT_EQ(1u, visitor_.padding_frames_.size()); |
| 1079 EXPECT_EQ(5, visitor_.padding_frames_[0]->num_padding_bytes); |
1078 }; | 1080 }; |
1079 | 1081 |
1080 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) { | 1082 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) { |
1081 // clang-format off | 1083 // clang-format off |
1082 unsigned char packet[] = { | 1084 unsigned char packet[] = { |
1083 // public flags (8 byte connection_id, version flag and an unknown flag) | 1085 // public flags (8 byte connection_id, version flag and an unknown flag) |
1084 static_cast<unsigned char>( | 1086 static_cast<unsigned char>( |
1085 FLAGS_quic_reloadable_flag_quic_remove_multipath_bit ? 0x39 : 0x79), | 1087 FLAGS_quic_reloadable_flag_quic_remove_multipath_bit ? 0x39 : 0x79), |
1086 // connection_id | 1088 // connection_id |
1087 0x10, 0x32, 0x54, 0x76, | 1089 0x10, 0x32, 0x54, 0x76, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 : packet), | 1122 : packet), |
1121 FLAGS_quic_restart_flag_quic_big_endian_connection_id | 1123 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
1122 ? arraysize(packet_cid_be) | 1124 ? arraysize(packet_cid_be) |
1123 : arraysize(packet), | 1125 : arraysize(packet), |
1124 false); | 1126 false); |
1125 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); | 1127 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); |
1126 EXPECT_EQ(QUIC_NO_ERROR, framer_.error()); | 1128 EXPECT_EQ(QUIC_NO_ERROR, framer_.error()); |
1127 ASSERT_TRUE(visitor_.header_.get()); | 1129 ASSERT_TRUE(visitor_.header_.get()); |
1128 EXPECT_EQ(0, visitor_.frame_count_); | 1130 EXPECT_EQ(0, visitor_.frame_count_); |
1129 EXPECT_EQ(1, visitor_.version_mismatch_); | 1131 EXPECT_EQ(1, visitor_.version_mismatch_); |
| 1132 EXPECT_EQ(1u, visitor_.padding_frames_.size()); |
| 1133 EXPECT_EQ(5, visitor_.padding_frames_[0]->num_padding_bytes); |
1130 }; | 1134 }; |
1131 | 1135 |
1132 TEST_P(QuicFramerTest, PaddingFrame) { | 1136 TEST_P(QuicFramerTest, PaddingFrame) { |
1133 // clang-format off | 1137 // clang-format off |
1134 unsigned char packet[] = { | 1138 unsigned char packet[] = { |
1135 // public flags (8 byte connection_id) | 1139 // public flags (8 byte connection_id) |
1136 0x38, | 1140 0x38, |
1137 // connection_id | 1141 // connection_id |
1138 0x10, 0x32, 0x54, 0x76, | 1142 0x10, 0x32, 0x54, 0x76, |
1139 0x98, 0xBA, 0xDC, 0xFE, | 1143 0x98, 0xBA, 0xDC, 0xFE, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 0xDC, 0xFE, 0x98, 0xBA, | 1184 0xDC, 0xFE, 0x98, 0xBA, |
1181 // data length | 1185 // data length |
1182 0x0c, 0x00, | 1186 0x0c, 0x00, |
1183 // data | 1187 // data |
1184 'h', 'e', 'l', 'l', | 1188 'h', 'e', 'l', 'l', |
1185 'o', ' ', 'w', 'o', | 1189 'o', ' ', 'w', 'o', |
1186 'r', 'l', 'd', '!', | 1190 'r', 'l', 'd', '!', |
1187 }; | 1191 }; |
1188 // clang-format on | 1192 // clang-format on |
1189 | 1193 |
| 1194 if (framer_.version() > QUIC_VERSION_37) { |
| 1195 return; |
| 1196 } |
| 1197 |
1190 QuicEncryptedPacket encrypted( | 1198 QuicEncryptedPacket encrypted( |
1191 AsChars(FLAGS_quic_restart_flag_quic_big_endian_connection_id | 1199 AsChars(FLAGS_quic_restart_flag_quic_big_endian_connection_id |
1192 ? packet_cid_be | 1200 ? packet_cid_be |
1193 : packet), | 1201 : packet), |
1194 FLAGS_quic_restart_flag_quic_big_endian_connection_id | 1202 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
1195 ? arraysize(packet_cid_be) | 1203 ? arraysize(packet_cid_be) |
1196 : arraysize(packet), | 1204 : arraysize(packet), |
1197 false); | 1205 false); |
1198 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); | 1206 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); |
1199 EXPECT_EQ(QUIC_NO_ERROR, framer_.error()); | 1207 EXPECT_EQ(QUIC_NO_ERROR, framer_.error()); |
1200 ASSERT_TRUE(visitor_.header_.get()); | 1208 ASSERT_TRUE(visitor_.header_.get()); |
1201 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion, | 1209 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion, |
1202 !kIncludeDiversificationNonce)); | 1210 !kIncludeDiversificationNonce)); |
1203 | 1211 |
1204 ASSERT_EQ(0u, visitor_.stream_frames_.size()); | 1212 ASSERT_EQ(0u, visitor_.stream_frames_.size()); |
1205 EXPECT_EQ(0u, visitor_.ack_frames_.size()); | 1213 EXPECT_EQ(0u, visitor_.ack_frames_.size()); |
| 1214 EXPECT_EQ(1u, visitor_.padding_frames_.size()); |
| 1215 EXPECT_EQ(28, visitor_.padding_frames_[0]->num_padding_bytes); |
1206 // A packet with no frames is not acceptable. | 1216 // A packet with no frames is not acceptable. |
1207 CheckProcessingFails( | 1217 CheckProcessingFails( |
1208 FLAGS_quic_restart_flag_quic_big_endian_connection_id ? packet_cid_be | 1218 FLAGS_quic_restart_flag_quic_big_endian_connection_id ? packet_cid_be |
1209 : packet, | 1219 : packet, |
1210 GetPacketHeaderSize(framer_.version(), PACKET_8BYTE_CONNECTION_ID, | 1220 GetPacketHeaderSize(framer_.version(), PACKET_8BYTE_CONNECTION_ID, |
1211 !kIncludeVersion, !kIncludeDiversificationNonce, | 1221 !kIncludeVersion, !kIncludeDiversificationNonce, |
1212 PACKET_6BYTE_PACKET_NUMBER), | 1222 PACKET_6BYTE_PACKET_NUMBER), |
1213 "Packet has no frames.", QUIC_MISSING_PAYLOAD); | 1223 "Packet has no frames.", QUIC_MISSING_PAYLOAD); |
1214 } | 1224 } |
1215 | 1225 |
| 1226 TEST_P(QuicFramerTest, NewPaddingFrame) { |
| 1227 // clang-format off |
| 1228 unsigned char packet[] = { |
| 1229 // public flags (8 byte connection_id) |
| 1230 0x38, |
| 1231 // connection_id |
| 1232 0x10, 0x32, 0x54, 0x76, |
| 1233 0x98, 0xBA, 0xDC, 0xFE, |
| 1234 // packet number |
| 1235 0xBC, 0x9A, 0x78, 0x56, |
| 1236 0x34, 0x12, |
| 1237 |
| 1238 // paddings |
| 1239 0x00, 0x00, |
| 1240 // frame type (stream frame with fin) |
| 1241 0xFF, |
| 1242 // stream id |
| 1243 0x04, 0x03, 0x02, 0x01, |
| 1244 // offset |
| 1245 0x54, 0x76, 0x10, 0x32, |
| 1246 0xDC, 0xFE, 0x98, 0xBA, |
| 1247 // data length |
| 1248 0x0c, 0x00, |
| 1249 // data |
| 1250 'h', 'e', 'l', 'l', |
| 1251 'o', ' ', 'w', 'o', |
| 1252 'r', 'l', 'd', '!', |
| 1253 // paddings |
| 1254 0x00, 0x00, |
| 1255 }; |
| 1256 |
| 1257 unsigned char packet_cid_be[] = { |
| 1258 // public flags (8 byte connection_id) |
| 1259 0x38, |
| 1260 // connection_id |
| 1261 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, |
| 1262 // packet number |
| 1263 0xBC, 0x9A, 0x78, 0x56, |
| 1264 0x34, 0x12, |
| 1265 |
| 1266 // paddings |
| 1267 0x00, 0x00, |
| 1268 // frame type (stream frame with fin) |
| 1269 0xFF, |
| 1270 // stream id |
| 1271 0x04, 0x03, 0x02, 0x01, |
| 1272 // offset |
| 1273 0x54, 0x76, 0x10, 0x32, |
| 1274 0xDC, 0xFE, 0x98, 0xBA, |
| 1275 // data length |
| 1276 0x0c, 0x00, |
| 1277 // data |
| 1278 'h', 'e', 'l', 'l', |
| 1279 'o', ' ', 'w', 'o', |
| 1280 'r', 'l', 'd', '!', |
| 1281 // paddings |
| 1282 0x00, 0x00, |
| 1283 }; |
| 1284 // clang-format on |
| 1285 |
| 1286 if (framer_.version() <= QUIC_VERSION_37) { |
| 1287 return; |
| 1288 } |
| 1289 |
| 1290 QuicEncryptedPacket encrypted( |
| 1291 AsChars(FLAGS_quic_restart_flag_quic_big_endian_connection_id |
| 1292 ? packet_cid_be |
| 1293 : packet), |
| 1294 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
| 1295 ? arraysize(packet_cid_be) |
| 1296 : arraysize(packet), |
| 1297 false); |
| 1298 EXPECT_TRUE(framer_.ProcessPacket(encrypted)); |
| 1299 EXPECT_EQ(QUIC_NO_ERROR, framer_.error()); |
| 1300 ASSERT_TRUE(visitor_.header_.get()); |
| 1301 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion, |
| 1302 !kIncludeDiversificationNonce)); |
| 1303 |
| 1304 ASSERT_EQ(1u, visitor_.stream_frames_.size()); |
| 1305 EXPECT_EQ(0u, visitor_.ack_frames_.size()); |
| 1306 EXPECT_EQ(2u, visitor_.padding_frames_.size()); |
| 1307 EXPECT_EQ(2, visitor_.padding_frames_[0]->num_padding_bytes); |
| 1308 EXPECT_EQ(2, visitor_.padding_frames_[1]->num_padding_bytes); |
| 1309 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id); |
| 1310 EXPECT_TRUE(visitor_.stream_frames_[0]->fin); |
| 1311 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset); |
| 1312 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get()); |
| 1313 } |
| 1314 |
1216 TEST_P(QuicFramerTest, StreamFrame) { | 1315 TEST_P(QuicFramerTest, StreamFrame) { |
1217 // clang-format off | 1316 // clang-format off |
1218 unsigned char packet[] = { | 1317 unsigned char packet[] = { |
1219 // public flags (8 byte connection_id) | 1318 // public flags (8 byte connection_id) |
1220 0x38, | 1319 0x38, |
1221 // connection_id | 1320 // connection_id |
1222 0x10, 0x32, 0x54, 0x76, | 1321 0x10, 0x32, 0x54, 0x76, |
1223 0x98, 0xBA, 0xDC, 0xFE, | 1322 0x98, 0xBA, 0xDC, 0xFE, |
1224 // packet number | 1323 // packet number |
1225 0xBC, 0x9A, 0x78, 0x56, | 1324 0xBC, 0x9A, 0x78, 0x56, |
(...skipping 2017 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3243 test::CompareCharArraysWithHexError( | 3342 test::CompareCharArraysWithHexError( |
3244 "constructed packet", data->data(), data->length(), | 3343 "constructed packet", data->data(), data->length(), |
3245 AsChars(FLAGS_quic_restart_flag_quic_big_endian_connection_id | 3344 AsChars(FLAGS_quic_restart_flag_quic_big_endian_connection_id |
3246 ? packet_cid_be | 3345 ? packet_cid_be |
3247 : packet), | 3346 : packet), |
3248 FLAGS_quic_restart_flag_quic_big_endian_connection_id | 3347 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
3249 ? arraysize(packet_cid_be) | 3348 ? arraysize(packet_cid_be) |
3250 : arraysize(packet)); | 3349 : arraysize(packet)); |
3251 } | 3350 } |
3252 | 3351 |
| 3352 TEST_P(QuicFramerTest, BuildStreamFramePacketWithNewPaddingFrame) { |
| 3353 if (framer_.version() <= QUIC_VERSION_37) { |
| 3354 return; |
| 3355 } |
| 3356 QuicPacketHeader header; |
| 3357 header.public_header.connection_id = kConnectionId; |
| 3358 header.public_header.reset_flag = false; |
| 3359 header.public_header.version_flag = false; |
| 3360 header.packet_number = kPacketNumber; |
| 3361 |
| 3362 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset, |
| 3363 QuicStringPiece("hello world!")); |
| 3364 QuicPaddingFrame padding_frame(2); |
| 3365 QuicFrames frames = {QuicFrame(padding_frame), QuicFrame(&stream_frame), |
| 3366 QuicFrame(padding_frame)}; |
| 3367 |
| 3368 // clang-format off |
| 3369 unsigned char packet[] = { |
| 3370 // public flags (8 byte connection_id) |
| 3371 0x38, |
| 3372 // connection_id |
| 3373 0x10, 0x32, 0x54, 0x76, |
| 3374 0x98, 0xBA, 0xDC, 0xFE, |
| 3375 // packet number |
| 3376 0xBC, 0x9A, 0x78, 0x56, |
| 3377 0x34, 0x12, |
| 3378 |
| 3379 // paddings |
| 3380 0x00, 0x00, |
| 3381 // frame type (stream frame with fin) |
| 3382 0xFF, |
| 3383 // stream id |
| 3384 0x04, 0x03, 0x02, 0x01, |
| 3385 // offset |
| 3386 0x54, 0x76, 0x10, 0x32, |
| 3387 0xDC, 0xFE, 0x98, 0xBA, |
| 3388 // data length |
| 3389 0x0c, 0x00, |
| 3390 // data |
| 3391 'h', 'e', 'l', 'l', |
| 3392 'o', ' ', 'w', 'o', |
| 3393 'r', 'l', 'd', '!', |
| 3394 // paddings |
| 3395 0x00, 0x00, |
| 3396 }; |
| 3397 |
| 3398 unsigned char packet_cid_be[] = { |
| 3399 // public flags (8 byte connection_id) |
| 3400 0x38, |
| 3401 // connection_id |
| 3402 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, |
| 3403 // packet number |
| 3404 0xBC, 0x9A, 0x78, 0x56, |
| 3405 0x34, 0x12, |
| 3406 |
| 3407 // paddings |
| 3408 0x00, 0x00, |
| 3409 // frame type (stream frame with fin) |
| 3410 0xFF, |
| 3411 // stream id |
| 3412 0x04, 0x03, 0x02, 0x01, |
| 3413 // offset |
| 3414 0x54, 0x76, 0x10, 0x32, |
| 3415 0xDC, 0xFE, 0x98, 0xBA, |
| 3416 // data length |
| 3417 0x0c, 0x00, |
| 3418 // data |
| 3419 'h', 'e', 'l', 'l', |
| 3420 'o', ' ', 'w', 'o', |
| 3421 'r', 'l', 'd', '!', |
| 3422 // paddings |
| 3423 0x00, 0x00, |
| 3424 }; |
| 3425 // clang-format on |
| 3426 |
| 3427 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames)); |
| 3428 ASSERT_TRUE(data != nullptr); |
| 3429 |
| 3430 test::CompareCharArraysWithHexError( |
| 3431 "constructed packet", data->data(), data->length(), |
| 3432 AsChars(FLAGS_quic_restart_flag_quic_big_endian_connection_id |
| 3433 ? packet_cid_be |
| 3434 : packet), |
| 3435 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
| 3436 ? arraysize(packet_cid_be) |
| 3437 : arraysize(packet)); |
| 3438 } |
| 3439 |
3253 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) { | 3440 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) { |
3254 QuicPacketHeader header; | 3441 QuicPacketHeader header; |
3255 header.public_header.connection_id = kConnectionId; | 3442 header.public_header.connection_id = kConnectionId; |
3256 header.public_header.reset_flag = false; | 3443 header.public_header.reset_flag = false; |
3257 header.public_header.version_flag = false; | 3444 header.public_header.version_flag = false; |
3258 header.public_header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER; | 3445 header.public_header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER; |
3259 header.packet_number = kPacketNumber; | 3446 header.packet_number = kPacketNumber; |
3260 | 3447 |
3261 QuicFrames frames = {QuicFrame(QuicPaddingFrame())}; | 3448 QuicFrames frames = {QuicFrame(QuicPaddingFrame())}; |
3262 | 3449 |
(...skipping 2144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5407 QuicFramerFuzzFunc(FLAGS_quic_restart_flag_quic_big_endian_connection_id | 5594 QuicFramerFuzzFunc(FLAGS_quic_restart_flag_quic_big_endian_connection_id |
5408 ? packet_cid_be | 5595 ? packet_cid_be |
5409 : packet, | 5596 : packet, |
5410 FLAGS_quic_restart_flag_quic_big_endian_connection_id | 5597 FLAGS_quic_restart_flag_quic_big_endian_connection_id |
5411 ? arraysize(packet_cid_be) | 5598 ? arraysize(packet_cid_be) |
5412 : arraysize(packet)); | 5599 : arraysize(packet)); |
5413 } | 5600 } |
5414 | 5601 |
5415 } // namespace test | 5602 } // namespace test |
5416 } // namespace net | 5603 } // namespace net |
OLD | NEW |