blob: 8213480bf915ae79583048d2679167a9182aa57a [file] [log] [blame]
fayang503ca4f2019-10-23 07:20:51 -07001// 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_coalesced_packet.h"
6
fayang58f71072019-11-05 08:47:02 -08007#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
fayang503ca4f2019-10-23 07:20:51 -07008#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"
10
11namespace quic {
12namespace test {
13namespace {
14
15TEST(QuicCoalescedPacketTest, MaybeCoalescePacket) {
16 QuicCoalescedPacket coalesced;
17 SimpleBufferAllocator allocator;
18 EXPECT_EQ(0u, coalesced.length());
19 char buffer[1000];
20 QuicSocketAddress self_address(QuicIpAddress::Loopback4(), 1);
21 QuicSocketAddress peer_address(QuicIpAddress::Loopback4(), 2);
22 SerializedPacket packet1(QuicPacketNumber(1), PACKET_4BYTE_PACKET_NUMBER,
23 buffer, 500, false, false);
24 QuicAckFrame ack_frame(InitAckFrame(1));
25 packet1.nonretransmittable_frames.push_back(QuicFrame(&ack_frame));
26 packet1.retransmittable_frames.push_back(
27 QuicFrame(QuicStreamFrame(1, true, 0, 100)));
28 ASSERT_TRUE(coalesced.MaybeCoalescePacket(packet1, self_address, peer_address,
29 &allocator, 1500));
30 EXPECT_EQ(1500u, coalesced.max_packet_length());
31 EXPECT_EQ(500u, coalesced.length());
32
33 // Cannot coalesce packet of the same encryption level.
34 SerializedPacket packet2(QuicPacketNumber(2), PACKET_4BYTE_PACKET_NUMBER,
35 buffer, 500, false, false);
36 EXPECT_FALSE(coalesced.MaybeCoalescePacket(packet2, self_address,
37 peer_address, &allocator, 1500));
38
39 SerializedPacket packet3(QuicPacketNumber(3), PACKET_4BYTE_PACKET_NUMBER,
40 buffer, 500, false, false);
41 packet3.nonretransmittable_frames.push_back(QuicFrame(QuicPaddingFrame(100)));
42 packet3.encryption_level = ENCRYPTION_ZERO_RTT;
43 ASSERT_TRUE(coalesced.MaybeCoalescePacket(packet3, self_address, peer_address,
44 &allocator, 1500));
45 EXPECT_EQ(1500u, coalesced.max_packet_length());
46 EXPECT_EQ(1000u, coalesced.length());
47
48 SerializedPacket packet4(QuicPacketNumber(4), PACKET_4BYTE_PACKET_NUMBER,
49 buffer, 500, false, false);
50 packet4.encryption_level = ENCRYPTION_FORWARD_SECURE;
51 // Cannot coalesce packet of changed self/peer address.
52 EXPECT_FALSE(coalesced.MaybeCoalescePacket(
53 packet4, QuicSocketAddress(QuicIpAddress::Loopback4(), 3), peer_address,
54 &allocator, 1500));
55
56 // Packet does not fit.
57 SerializedPacket packet5(QuicPacketNumber(5), PACKET_4BYTE_PACKET_NUMBER,
58 buffer, 501, false, false);
59 packet5.encryption_level = ENCRYPTION_FORWARD_SECURE;
60 EXPECT_FALSE(coalesced.MaybeCoalescePacket(packet5, self_address,
61 peer_address, &allocator, 1500));
62 EXPECT_EQ(1500u, coalesced.max_packet_length());
63 EXPECT_EQ(1000u, coalesced.length());
64
65 // Max packet number length changed.
66 SerializedPacket packet6(QuicPacketNumber(6), PACKET_4BYTE_PACKET_NUMBER,
67 buffer, 100, false, false);
68 packet6.encryption_level = ENCRYPTION_FORWARD_SECURE;
fayang58f71072019-11-05 08:47:02 -080069 EXPECT_QUIC_BUG(coalesced.MaybeCoalescePacket(packet6, self_address,
70 peer_address, &allocator, 1000),
71 "Max packet length changes in the middle of the write path");
fayang503ca4f2019-10-23 07:20:51 -070072 EXPECT_EQ(1500u, coalesced.max_packet_length());
73 EXPECT_EQ(1000u, coalesced.length());
74}
75
fayang08750832019-10-24 11:25:34 -070076TEST(QuicCoalescedPacketTest, CopyEncryptedBuffers) {
77 QuicCoalescedPacket coalesced;
78 SimpleBufferAllocator allocator;
79 QuicSocketAddress self_address(QuicIpAddress::Loopback4(), 1);
80 QuicSocketAddress peer_address(QuicIpAddress::Loopback4(), 2);
81 std::string buffer(500, 'a');
82 std::string buffer2(500, 'b');
83 SerializedPacket packet1(QuicPacketNumber(1), PACKET_4BYTE_PACKET_NUMBER,
84 buffer.data(), 500,
85 /*has_ack=*/false, /*has_stop_waiting=*/false);
86 packet1.encryption_level = ENCRYPTION_ZERO_RTT;
87 SerializedPacket packet2(QuicPacketNumber(2), PACKET_4BYTE_PACKET_NUMBER,
88 buffer2.data(), 500,
89 /*has_ack=*/false, /*has_stop_waiting=*/false);
90 packet2.encryption_level = ENCRYPTION_FORWARD_SECURE;
91
92 ASSERT_TRUE(coalesced.MaybeCoalescePacket(packet1, self_address, peer_address,
93 &allocator, 1500));
94 ASSERT_TRUE(coalesced.MaybeCoalescePacket(packet2, self_address, peer_address,
95 &allocator, 1500));
96 EXPECT_EQ(1000u, coalesced.length());
97
98 char copy_buffer[1000];
99 size_t length_copied = 0;
100 EXPECT_FALSE(
101 coalesced.CopyEncryptedBuffers(copy_buffer, 900, &length_copied));
102 ASSERT_TRUE(
103 coalesced.CopyEncryptedBuffers(copy_buffer, 1000, &length_copied));
104 EXPECT_EQ(1000u, length_copied);
105 char expected[1000];
106 memset(expected, 'a', 500);
107 memset(expected + 500, 'b', 500);
108 test::CompareCharArraysWithHexError("copied buffers", copy_buffer,
109 length_copied, expected, 1000);
110}
111
fayang503ca4f2019-10-23 07:20:51 -0700112} // namespace
113} // namespace test
114} // namespace quic