Android WebView: add sfntly to deps whitelist.
[chromium-blink-merge.git] / net / quic / quic_received_packet_manager_test.cc
blobdd7d5290127cef63b60d8db9dcc81a2df665cb9b
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"
7 #include <algorithm>
8 #include <vector>
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"
14 using std::make_pair;
15 using std::pair;
16 using std::vector;
18 namespace net {
19 namespace test {
20 namespace {
22 class QuicReceivedPacketManagerTest : public ::testing::Test {
23 protected:
24 void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number,
25 QuicPacketEntropyHash entropy_hash) {
26 QuicPacketHeader header;
27 header.packet_sequence_number = sequence_number;
28 header.entropy_hash = entropy_hash;
29 received_manager_.RecordPacketReceived(header, QuicTime::Zero());;
32 QuicReceivedPacketManager received_manager_;
35 TEST_F(QuicReceivedPacketManagerTest, ReceivedPacketEntropyHash) {
36 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies;
37 entropies.push_back(make_pair(1, 12));
38 entropies.push_back(make_pair(7, 1));
39 entropies.push_back(make_pair(2, 33));
40 entropies.push_back(make_pair(5, 3));
41 entropies.push_back(make_pair(8, 34));
43 for (size_t i = 0; i < entropies.size(); ++i) {
44 RecordPacketEntropyHash(entropies[i].first,
45 entropies[i].second);
48 sort(entropies.begin(), entropies.end());
50 QuicPacketEntropyHash hash = 0;
51 size_t index = 0;
52 for (size_t i = 1; i <= (*entropies.rbegin()).first; ++i) {
53 if (entropies[index].first == i) {
54 hash ^= entropies[index].second;
55 ++index;
57 EXPECT_EQ(hash, received_manager_.EntropyHash(i));
61 TEST_F(QuicReceivedPacketManagerTest, EntropyHashBelowLeastObserved) {
62 EXPECT_EQ(0, received_manager_.EntropyHash(0));
63 RecordPacketEntropyHash(4, 5);
64 EXPECT_EQ(0, received_manager_.EntropyHash(3));
67 TEST_F(QuicReceivedPacketManagerTest, EntropyHashAboveLargestObserved) {
68 EXPECT_EQ(0, received_manager_.EntropyHash(0));
69 RecordPacketEntropyHash(4, 5);
70 EXPECT_EQ(0, received_manager_.EntropyHash(3));
73 TEST_F(QuicReceivedPacketManagerTest, RecalculateEntropyHash) {
74 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies;
75 entropies.push_back(make_pair(1, 12));
76 entropies.push_back(make_pair(2, 1));
77 entropies.push_back(make_pair(3, 33));
78 entropies.push_back(make_pair(4, 3));
79 entropies.push_back(make_pair(5, 34));
80 entropies.push_back(make_pair(6, 29));
82 QuicPacketEntropyHash entropy_hash = 0;
83 for (size_t i = 0; i < entropies.size(); ++i) {
84 RecordPacketEntropyHash(entropies[i].first, entropies[i].second);
85 entropy_hash ^= entropies[i].second;
87 EXPECT_EQ(entropy_hash, received_manager_.EntropyHash(6));
89 // Now set the entropy hash up to 4 to be 100.
90 entropy_hash ^= 100;
91 for (size_t i = 0; i < 3; ++i) {
92 entropy_hash ^= entropies[i].second;
94 QuicReceivedPacketManagerPeer::RecalculateEntropyHash(
95 &received_manager_, 4, 100);
96 EXPECT_EQ(entropy_hash, received_manager_.EntropyHash(6));
98 QuicReceivedPacketManagerPeer::RecalculateEntropyHash(
99 &received_manager_, 1, 50);
100 EXPECT_EQ(entropy_hash, received_manager_.EntropyHash(6));
103 TEST_F(QuicReceivedPacketManagerTest, DontWaitForPacketsBefore) {
104 QuicPacketHeader header;
105 header.packet_sequence_number = 2u;
106 received_manager_.RecordPacketReceived(header, QuicTime::Zero());
107 header.packet_sequence_number = 7u;
108 received_manager_.RecordPacketReceived(header, QuicTime::Zero());
109 EXPECT_TRUE(received_manager_.IsAwaitingPacket(3u));
110 EXPECT_TRUE(received_manager_.IsAwaitingPacket(6u));
111 EXPECT_TRUE(QuicReceivedPacketManagerPeer::DontWaitForPacketsBefore(
112 &received_manager_, 4));
113 EXPECT_FALSE(received_manager_.IsAwaitingPacket(3u));
114 EXPECT_TRUE(received_manager_.IsAwaitingPacket(6u));
117 TEST_F(QuicReceivedPacketManagerTest, UpdateReceivedPacketInfo) {
118 QuicPacketHeader header;
119 header.packet_sequence_number = 2u;
120 QuicTime two_ms = QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(2));
121 received_manager_.RecordPacketReceived(header, two_ms);
123 ReceivedPacketInfo info;
124 received_manager_.UpdateReceivedPacketInfo(&info, QuicTime::Zero());
125 // When UpdateReceivedPacketInfo with a time earlier than the time of the
126 // largest observed packet, make sure that the delta is 0, not negative.
127 EXPECT_EQ(QuicTime::Delta::Zero(), info.delta_time_largest_observed);
129 QuicTime four_ms = QuicTime::Zero().Add(QuicTime::Delta::FromMilliseconds(4));
130 received_manager_.UpdateReceivedPacketInfo(&info, four_ms);
131 // When UpdateReceivedPacketInfo after not having received a new packet,
132 // the delta should still be accurate.
133 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(2),
134 info.delta_time_largest_observed);
137 } // namespace
138 } // namespace test
139 } // namespace net