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_sent_entropy_manager.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
20 class QuicSentEntropyManagerTest
: public ::testing::Test
{
22 QuicSentEntropyManager entropy_manager_
;
25 TEST_F(QuicSentEntropyManagerTest
, SentEntropyHash
) {
26 EXPECT_EQ(0, entropy_manager_
.GetCumulativeEntropy(0));
28 QuicPacketEntropyHash entropies
[4] = {12, 1, 33, 3};
29 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
30 entropy_manager_
.RecordPacketEntropyHash(i
+ 1, entropies
[i
]);
33 QuicPacketEntropyHash hash
= 0;
34 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
36 EXPECT_EQ(hash
, entropy_manager_
.GetCumulativeEntropy(i
+ 1));
40 TEST_F(QuicSentEntropyManagerTest
, IsValidEntropy
) {
41 QuicPacketEntropyHash entropies
[10] =
42 {12, 1, 33, 3, 32, 100, 28, 42, 22, 255};
43 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
44 entropy_manager_
.RecordPacketEntropyHash(i
+ 1, entropies
[i
]);
47 PacketNumberQueue missing_packets
;
48 missing_packets
.Add(1);
49 missing_packets
.Add(4);
50 missing_packets
.Add(7, 9);
52 QuicPacketEntropyHash entropy_hash
= 0;
53 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
54 if (!missing_packets
.Contains(i
+ 1)) {
55 entropy_hash
^= entropies
[i
];
59 EXPECT_TRUE(entropy_manager_
.IsValidEntropy(10, missing_packets
,
63 TEST_F(QuicSentEntropyManagerTest
, ClearEntropiesBefore
) {
64 QuicPacketEntropyHash entropies
[10] =
65 {12, 1, 33, 3, 32, 100, 28, 42, 22, 255};
67 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
68 entropy_manager_
.RecordPacketEntropyHash(i
+ 1, entropies
[i
]);
71 // Discard the first 5 entropies and ensure IsValidEntropy and EntropyHash
72 // still return correct results.
73 entropy_manager_
.ClearEntropyBefore(5);
75 PacketNumberQueue missing_packets
;
76 missing_packets
.Add(7, 9);
78 QuicPacketEntropyHash entropy_hash
= 0;
79 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
80 if (!missing_packets
.Contains(i
+ 1)) {
81 entropy_hash
^= entropies
[i
];
84 EXPECT_TRUE(entropy_manager_
.IsValidEntropy(10, missing_packets
,
88 for (size_t i
= 0; i
< arraysize(entropies
); ++i
) {
89 entropy_hash
^= entropies
[i
];
91 EXPECT_EQ(entropy_hash
, entropy_manager_
.GetCumulativeEntropy(10));