blob: b2bccbeeb72735677eb2413121c2816fce2f74ea [file] [log] [blame]
QUICHE team2252b702019-05-14 23:55:14 -04001// 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
fayangfb5fb612019-10-21 13:19:14 -07007#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
QUICHE team2252b702019-05-14 23:55:14 -04008#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"
dmcardle8f7df532020-01-07 13:28:57 -080010#include "net/third_party/quiche/src/common/test_tools/quiche_test_utils.h"
QUICHE team2252b702019-05-14 23:55:14 -040011
12namespace quic {
13namespace test {
14namespace {
15
16QuicPacketHeader 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
25class QuicPacketsTest : public QuicTest {};
26
27TEST_F(QuicPacketsTest, GetServerConnectionIdAsRecipient) {
28 QuicPacketHeader header = CreateFakePacketHeader();
QUICHE team2252b702019-05-14 23:55:14 -040029 EXPECT_EQ(TestConnectionId(1),
30 GetServerConnectionIdAsRecipient(header, Perspective::IS_SERVER));
31 EXPECT_EQ(TestConnectionId(2),
32 GetServerConnectionIdAsRecipient(header, Perspective::IS_CLIENT));
33}
34
35TEST_F(QuicPacketsTest, GetServerConnectionIdAsSender) {
36 QuicPacketHeader header = CreateFakePacketHeader();
QUICHE team2252b702019-05-14 23:55:14 -040037 EXPECT_EQ(TestConnectionId(2),
38 GetServerConnectionIdAsSender(header, Perspective::IS_SERVER));
39 EXPECT_EQ(TestConnectionId(1),
40 GetServerConnectionIdAsSender(header, Perspective::IS_CLIENT));
41}
42
43TEST_F(QuicPacketsTest, GetServerConnectionIdIncludedAsSender) {
44 QuicPacketHeader header = CreateFakePacketHeader();
QUICHE team2252b702019-05-14 23:55:14 -040045 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
51TEST_F(QuicPacketsTest, GetClientConnectionIdIncludedAsSender) {
52 QuicPacketHeader header = CreateFakePacketHeader();
QUICHE team2252b702019-05-14 23:55:14 -040053 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
dschinaziac6805d2019-05-30 09:44:27 -070059TEST_F(QuicPacketsTest, GetClientConnectionIdAsRecipient) {
dschinaziac6805d2019-05-30 09:44:27 -070060 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
67TEST_F(QuicPacketsTest, GetClientConnectionIdAsSender) {
68 QuicPacketHeader header = CreateFakePacketHeader();
dschinaziac6805d2019-05-30 09:44:27 -070069 EXPECT_EQ(TestConnectionId(1),
70 GetClientConnectionIdAsSender(header, Perspective::IS_SERVER));
71 EXPECT_EQ(TestConnectionId(2),
72 GetClientConnectionIdAsSender(header, Perspective::IS_CLIENT));
73}
74
fayangfb5fb612019-10-21 13:19:14 -070075TEST_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);
dmcardle8f7df532020-01-07 13:28:57 -0800101 quiche::test::CompareCharArraysWithHexError(
fayangfb5fb612019-10-21 13:19:14 -0700102 "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);
fayangfb5fb612019-10-21 13:19:14 -0700109}
110
QUICHE team2252b702019-05-14 23:55:14 -0400111} // namespace
112} // namespace test
113} // namespace quic