Add some instrumentation to investigate a possible use after free.
[chromium-blink-merge.git] / net / quic / quic_ack_notifier_manager_test.cc
blobe791b91b9e18d95fdc99fae541aebd466b06765a
1 // Copyright (c) 2015 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.
5 #include "net/quic/quic_ack_notifier_manager.h"
7 #include "net/quic/quic_ack_notifier.h"
8 #include "net/quic/quic_protocol.h"
9 #include "net/quic/test_tools/quic_ack_notifier_manager_peer.h"
10 #include "net/quic/test_tools/quic_test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace net {
14 namespace test {
15 namespace {
17 // Test fixture for testing AckNotifierManager. Instantiates a manager and
18 // provides shared code for adding notifiers and verifying the contents of the
19 // manager.
20 class QuicAckNotifierManagerTest : public ::testing::Test {
21 protected:
22 AckNotifierManager manager_;
23 scoped_refptr<MockAckNotifierDelegate> delegate_;
24 QuicTime::Delta zero_;
26 QuicAckNotifierManagerTest() : zero_(QuicTime::Delta::Zero()) {
27 delegate_ = new MockAckNotifierDelegate;
30 ~QuicAckNotifierManagerTest() override {}
32 size_t CountPackets() const {
33 return AckNotifierManagerPeer::GetNumberOfRegisteredPackets(&manager_);
36 // Add a mock packet with specified parameters. The packet with given
37 // sequence number must not exist in the map before.
38 void AddPacket(QuicPacketSequenceNumber sequence_number,
39 bool retransmittable) {
40 // Create a mock packet.
41 RetransmittableFrames frames(ENCRYPTION_NONE);
42 SerializedPacket packet(sequence_number, PACKET_4BYTE_SEQUENCE_NUMBER,
43 /*packet=*/nullptr,
44 /*entropy_hash=*/0,
45 retransmittable ? &frames : nullptr);
47 // Create and register a notifier. Normally, this would be created by
48 // QuicPacketGenerator.
49 QuicAckNotifier* notifier = new QuicAckNotifier(delegate_.get());
50 packet.notifiers.push_back(notifier);
52 // Ensure that exactly one packet is added.
53 const size_t old_packet_count = CountPackets();
55 // Actually add the packet.
56 manager_.OnSerializedPacket(packet);
58 // Ensure the change in the number of packets.
59 EXPECT_EQ(old_packet_count + 1, CountPackets());
63 // This test verifies that QuicAckNotifierManager can handle the trivial case of
64 // received packet notification.
65 TEST_F(QuicAckNotifierManagerTest, SimpleAck) {
66 AddPacket(1, false);
67 AddPacket(2, true);
69 EXPECT_CALL(*delegate_, OnAckNotification(0, 0, zero_)).Times(2);
70 manager_.OnPacketAcked(1, zero_);
71 EXPECT_EQ(1u, CountPackets());
72 manager_.OnPacketAcked(2, zero_);
73 EXPECT_EQ(0u, CountPackets());
75 manager_.OnPacketRemoved(1);
76 manager_.OnPacketRemoved(2);
77 EXPECT_EQ(0u, CountPackets());
80 // This test verifies that QuicAckNotifierManager can correctly handle the case
81 // when some of the packets are lost, which causes retransmission and removal
82 // from the unacked packet map.
83 TEST_F(QuicAckNotifierManagerTest, AckWithLosses) {
84 const size_t retransmitted_packet_size = kDefaultMaxPacketSize;
86 // Here, we simulate the following scenario:
87 // 1. We send packets 1 to 5, where only odd-numbered packets are
88 // retransmittable.
89 // 2. The peer acks 1, 2 and 5, but not 3 and 4.
90 // 3. We retransmit 3 as 6.
91 // 4. We remove 1 and 2, since we no longer care about them.
92 // 4. The peer acks 6.
93 // 5. We remove packets 3 to 6.
95 // Step 1: send five packets.
96 AddPacket(1, true);
97 AddPacket(2, false);
98 AddPacket(3, true);
99 AddPacket(4, false);
100 AddPacket(5, true);
102 // Step 2: handle acks from peer.
103 EXPECT_CALL(*delegate_, OnAckNotification(0, 0, zero_)).Times(3);
104 manager_.OnPacketAcked(1, zero_);
105 EXPECT_EQ(4u, CountPackets());
106 manager_.OnPacketAcked(2, zero_);
107 EXPECT_EQ(3u, CountPackets());
108 manager_.OnPacketAcked(5, zero_);
109 EXPECT_EQ(2u, CountPackets());
111 // Step 3: retransmit 3 as 6.
112 manager_.OnPacketRetransmitted(3, 6, retransmitted_packet_size);
113 EXPECT_EQ(2u, CountPackets());
115 // Step 4: remove 1 and 2.
116 manager_.OnPacketRemoved(1);
117 manager_.OnPacketRemoved(2);
118 EXPECT_EQ(2u, CountPackets());
120 // Step 4: ack packet 6.
121 EXPECT_CALL(*delegate_,
122 OnAckNotification(1, retransmitted_packet_size, zero_)).Times(1);
123 manager_.OnPacketAcked(6, zero_);
124 EXPECT_EQ(1u, CountPackets());
126 // Step 5: remove all packets. This causes packet 4 to be dropped from the
127 // map.
128 manager_.OnPacketRemoved(3);
129 manager_.OnPacketRemoved(4);
130 manager_.OnPacketRemoved(5);
131 manager_.OnPacketRemoved(6);
132 EXPECT_EQ(0u, CountPackets());
135 // This test verifies that the QuicAckNotifierManager behaves correctly when
136 // there are many retransmissions.
137 TEST_F(QuicAckNotifierManagerTest, RepeatedRetransmission) {
138 AddPacket(1, true);
140 const size_t packet_size = kDefaultMaxPacketSize;
141 const size_t times_lost = 100;
142 const size_t total_size_lost = packet_size * times_lost;
143 const QuicPacketSequenceNumber last_packet = times_lost + 1;
145 // Retransmit the packet many times.
146 for (size_t sequence_number = 1; sequence_number < last_packet;
147 sequence_number++) {
148 manager_.OnPacketRetransmitted(sequence_number, sequence_number + 1,
149 packet_size);
150 EXPECT_EQ(1u, CountPackets());
153 // Remove all lost packets.
154 for (QuicPacketSequenceNumber packet = 1; packet < last_packet; packet++) {
155 manager_.OnPacketRemoved(packet);
157 EXPECT_EQ(1u, CountPackets());
159 // Finally get the packet acknowledged.
160 EXPECT_CALL(*delegate_, OnAckNotification(times_lost, total_size_lost, zero_))
161 .Times(1);
162 manager_.OnPacketAcked(last_packet, zero_);
163 EXPECT_EQ(0u, CountPackets());
165 // Remove the last packet.
166 manager_.OnPacketRemoved(last_packet);
167 EXPECT_EQ(0u, CountPackets());
170 } // namespace
171 } // namespace test
172 } // namespace net