1 // Copyright 2013 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_received_packet_manager.h"
10 #include "net/quic/test_tools/quic_received_packet_manager_peer.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
22 class QuicReceivedPacketManagerTest
: public ::testing::Test
{
24 QuicReceivedPacketManagerTest() : received_manager_(kTCP
) { }
26 void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number
,
27 QuicPacketEntropyHash entropy_hash
) {
28 QuicPacketHeader header
;
29 header
.packet_sequence_number
= sequence_number
;
30 header
.entropy_hash
= entropy_hash
;
31 received_manager_
.RecordPacketReceived(0u, header
, QuicTime::Zero(), false);
34 QuicReceivedPacketManager received_manager_
;
37 TEST_F(QuicReceivedPacketManagerTest
, ReceivedPacketEntropyHash
) {
38 vector
<pair
<QuicPacketSequenceNumber
, QuicPacketEntropyHash
> > entropies
;
39 entropies
.push_back(make_pair(1, 12));
40 entropies
.push_back(make_pair(7, 1));
41 entropies
.push_back(make_pair(2, 33));
42 entropies
.push_back(make_pair(5, 3));
43 entropies
.push_back(make_pair(8, 34));
45 for (size_t i
= 0; i
< entropies
.size(); ++i
) {
46 RecordPacketEntropyHash(entropies
[i
].first
,
50 sort(entropies
.begin(), entropies
.end());
52 QuicPacketEntropyHash hash
= 0;
54 for (size_t i
= 1; i
<= (*entropies
.rbegin()).first
; ++i
) {
55 if (entropies
[index
].first
== i
) {
56 hash
^= entropies
[index
].second
;
59 EXPECT_EQ(hash
, received_manager_
.EntropyHash(i
));
63 TEST_F(QuicReceivedPacketManagerTest
, EntropyHashBelowLeastObserved
) {
64 EXPECT_EQ(0, received_manager_
.EntropyHash(0));
65 RecordPacketEntropyHash(4, 5);
66 EXPECT_EQ(0, received_manager_
.EntropyHash(3));
69 TEST_F(QuicReceivedPacketManagerTest
, EntropyHashAboveLargestObserved
) {
70 EXPECT_EQ(0, received_manager_
.EntropyHash(0));
71 RecordPacketEntropyHash(4, 5);
72 EXPECT_EQ(0, received_manager_
.EntropyHash(3));
75 TEST_F(QuicReceivedPacketManagerTest
, RecalculateEntropyHash
) {
76 vector
<pair
<QuicPacketSequenceNumber
, QuicPacketEntropyHash
> > entropies
;
77 entropies
.push_back(make_pair(1, 12));
78 entropies
.push_back(make_pair(2, 1));
79 entropies
.push_back(make_pair(3, 33));
80 entropies
.push_back(make_pair(4, 3));
81 entropies
.push_back(make_pair(5, 34));
82 entropies
.push_back(make_pair(6, 29));
84 QuicPacketEntropyHash entropy_hash
= 0;
85 for (size_t i
= 0; i
< entropies
.size(); ++i
) {
86 RecordPacketEntropyHash(entropies
[i
].first
, entropies
[i
].second
);
87 entropy_hash
^= entropies
[i
].second
;
89 EXPECT_EQ(entropy_hash
, received_manager_
.EntropyHash(6));
91 // Now set the entropy hash up to 4 to be 100.
93 for (size_t i
= 0; i
< 3; ++i
) {
94 entropy_hash
^= entropies
[i
].second
;
96 QuicReceivedPacketManagerPeer::RecalculateEntropyHash(
97 &received_manager_
, 4, 100);
98 EXPECT_EQ(entropy_hash
, received_manager_
.EntropyHash(6));
100 QuicReceivedPacketManagerPeer::RecalculateEntropyHash(
101 &received_manager_
, 1, 50);
102 EXPECT_EQ(entropy_hash
, received_manager_
.EntropyHash(6));
105 TEST_F(QuicReceivedPacketManagerTest
, DontWaitForPacketsBefore
) {
106 QuicPacketHeader header
;
107 header
.packet_sequence_number
= 2u;
108 received_manager_
.RecordPacketReceived(0u, header
, QuicTime::Zero(), false);
109 header
.packet_sequence_number
= 7u;
110 received_manager_
.RecordPacketReceived(0u, header
, QuicTime::Zero(), false);
111 EXPECT_TRUE(received_manager_
.IsAwaitingPacket(3u));
112 EXPECT_TRUE(received_manager_
.IsAwaitingPacket(6u));
113 EXPECT_TRUE(QuicReceivedPacketManagerPeer::DontWaitForPacketsBefore(
114 &received_manager_
, 4));
115 EXPECT_FALSE(received_manager_
.IsAwaitingPacket(3u));
116 EXPECT_TRUE(received_manager_
.IsAwaitingPacket(6u));
119 TEST_F(QuicReceivedPacketManagerTest
, UpdateReceivedPacketInfo
) {
120 QuicPacketHeader header
;
121 header
.packet_sequence_number
= 2u;
122 QuicTime two_ms
= QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(2));
123 received_manager_
.RecordPacketReceived(0u, header
, two_ms
, false);
125 ReceivedPacketInfo info
;
126 received_manager_
.UpdateReceivedPacketInfo(&info
, QuicTime::Zero());
127 // When UpdateReceivedPacketInfo with a time earlier than the time of the
128 // largest observed packet, make sure that the delta is 0, not negative.
129 EXPECT_EQ(QuicTime::Delta::Zero(), info
.delta_time_largest_observed
);
131 QuicTime four_ms
= QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(4));
132 received_manager_
.UpdateReceivedPacketInfo(&info
, four_ms
);
133 // When UpdateReceivedPacketInfo after not having received a new packet,
134 // the delta should still be accurate.
135 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(2),
136 info
.delta_time_largest_observed
);