QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1 | // Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 6 | |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 9 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
dmcardle | 8f7df53 | 2020-01-07 13:28:57 -0800 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/common/test_tools/quiche_test_utils.h" |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 11 | |
| 12 | namespace quic { |
| 13 | namespace test { |
| 14 | namespace { |
| 15 | |
| 16 | QuicPacketHeader CreateFakePacketHeader() { |
| 17 | QuicPacketHeader header; |
| 18 | header.destination_connection_id = TestConnectionId(1); |
| 19 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 20 | header.source_connection_id = TestConnectionId(2); |
| 21 | header.source_connection_id_included = CONNECTION_ID_ABSENT; |
| 22 | return header; |
| 23 | } |
| 24 | |
| 25 | class QuicPacketsTest : public QuicTest {}; |
| 26 | |
| 27 | TEST_F(QuicPacketsTest, GetServerConnectionIdAsRecipient) { |
| 28 | QuicPacketHeader header = CreateFakePacketHeader(); |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 29 | EXPECT_EQ(TestConnectionId(1), |
| 30 | GetServerConnectionIdAsRecipient(header, Perspective::IS_SERVER)); |
| 31 | EXPECT_EQ(TestConnectionId(2), |
| 32 | GetServerConnectionIdAsRecipient(header, Perspective::IS_CLIENT)); |
| 33 | } |
| 34 | |
| 35 | TEST_F(QuicPacketsTest, GetServerConnectionIdAsSender) { |
| 36 | QuicPacketHeader header = CreateFakePacketHeader(); |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 37 | EXPECT_EQ(TestConnectionId(2), |
| 38 | GetServerConnectionIdAsSender(header, Perspective::IS_SERVER)); |
| 39 | EXPECT_EQ(TestConnectionId(1), |
| 40 | GetServerConnectionIdAsSender(header, Perspective::IS_CLIENT)); |
| 41 | } |
| 42 | |
| 43 | TEST_F(QuicPacketsTest, GetServerConnectionIdIncludedAsSender) { |
| 44 | QuicPacketHeader header = CreateFakePacketHeader(); |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 45 | EXPECT_EQ(CONNECTION_ID_ABSENT, GetServerConnectionIdIncludedAsSender( |
| 46 | header, Perspective::IS_SERVER)); |
| 47 | EXPECT_EQ(CONNECTION_ID_PRESENT, GetServerConnectionIdIncludedAsSender( |
| 48 | header, Perspective::IS_CLIENT)); |
| 49 | } |
| 50 | |
| 51 | TEST_F(QuicPacketsTest, GetClientConnectionIdIncludedAsSender) { |
| 52 | QuicPacketHeader header = CreateFakePacketHeader(); |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 53 | EXPECT_EQ(CONNECTION_ID_PRESENT, GetClientConnectionIdIncludedAsSender( |
| 54 | header, Perspective::IS_SERVER)); |
| 55 | EXPECT_EQ(CONNECTION_ID_ABSENT, GetClientConnectionIdIncludedAsSender( |
| 56 | header, Perspective::IS_CLIENT)); |
| 57 | } |
| 58 | |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 59 | TEST_F(QuicPacketsTest, GetClientConnectionIdAsRecipient) { |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 60 | QuicPacketHeader header = CreateFakePacketHeader(); |
| 61 | EXPECT_EQ(TestConnectionId(2), |
| 62 | GetClientConnectionIdAsRecipient(header, Perspective::IS_SERVER)); |
| 63 | EXPECT_EQ(TestConnectionId(1), |
| 64 | GetClientConnectionIdAsRecipient(header, Perspective::IS_CLIENT)); |
| 65 | } |
| 66 | |
| 67 | TEST_F(QuicPacketsTest, GetClientConnectionIdAsSender) { |
| 68 | QuicPacketHeader header = CreateFakePacketHeader(); |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 69 | EXPECT_EQ(TestConnectionId(1), |
| 70 | GetClientConnectionIdAsSender(header, Perspective::IS_SERVER)); |
| 71 | EXPECT_EQ(TestConnectionId(2), |
| 72 | GetClientConnectionIdAsSender(header, Perspective::IS_CLIENT)); |
| 73 | } |
| 74 | |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 75 | TEST_F(QuicPacketsTest, CopySerializedPacket) { |
| 76 | std::string buffer(1000, 'a'); |
| 77 | SimpleBufferAllocator allocator; |
| 78 | SerializedPacket packet(QuicPacketNumber(1), PACKET_1BYTE_PACKET_NUMBER, |
| 79 | buffer.data(), buffer.length(), /*has_ack=*/false, |
| 80 | /*has_stop_waiting=*/false); |
| 81 | packet.retransmittable_frames.push_back( |
| 82 | QuicFrame(new QuicWindowUpdateFrame())); |
| 83 | packet.retransmittable_frames.push_back(QuicFrame(QuicStreamFrame())); |
| 84 | |
| 85 | QuicAckFrame ack_frame(InitAckFrame(1)); |
| 86 | packet.nonretransmittable_frames.push_back(QuicFrame(&ack_frame)); |
| 87 | packet.nonretransmittable_frames.push_back(QuicFrame(QuicPaddingFrame(-1))); |
| 88 | |
| 89 | std::unique_ptr<SerializedPacket> copy = QuicWrapUnique<SerializedPacket>( |
| 90 | CopySerializedPacket(packet, &allocator, /*copy_buffer=*/true)); |
| 91 | EXPECT_EQ(quic::QuicPacketNumber(1), copy->packet_number); |
| 92 | EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, copy->packet_number_length); |
| 93 | ASSERT_EQ(2u, copy->retransmittable_frames.size()); |
| 94 | EXPECT_EQ(WINDOW_UPDATE_FRAME, copy->retransmittable_frames[0].type); |
| 95 | EXPECT_EQ(STREAM_FRAME, copy->retransmittable_frames[1].type); |
| 96 | |
| 97 | ASSERT_EQ(2u, copy->nonretransmittable_frames.size()); |
| 98 | EXPECT_EQ(ACK_FRAME, copy->nonretransmittable_frames[0].type); |
| 99 | EXPECT_EQ(PADDING_FRAME, copy->nonretransmittable_frames[1].type); |
| 100 | EXPECT_EQ(1000u, copy->encrypted_length); |
dmcardle | 8f7df53 | 2020-01-07 13:28:57 -0800 | [diff] [blame] | 101 | quiche::test::CompareCharArraysWithHexError( |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 102 | "encrypted_buffer", copy->encrypted_buffer, copy->encrypted_length, |
| 103 | packet.encrypted_buffer, packet.encrypted_length); |
| 104 | |
| 105 | std::unique_ptr<SerializedPacket> copy2 = QuicWrapUnique<SerializedPacket>( |
| 106 | CopySerializedPacket(packet, &allocator, /*copy_buffer=*/false)); |
| 107 | EXPECT_EQ(packet.encrypted_buffer, copy2->encrypted_buffer); |
| 108 | EXPECT_EQ(1000u, copy2->encrypted_length); |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 109 | } |
| 110 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 111 | } // namespace |
| 112 | } // namespace test |
| 113 | } // namespace quic |