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. 4 5 #include "net/quic/quic_sent_entropy_manager.h" 6 7 #include <algorithm> 8 #include <vector> 9 10 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 using std::make_pair; 14 using std::pair; 15 using std::vector; 16 17 namespace net { 18 namespace test { 19 namespace { 20 21 class QuicSentEntropyManagerTest : public ::testing::Test { 22 protected: 23 QuicSentEntropyManager entropy_manager_; 24 }; 25 26 TEST_F(QuicSentEntropyManagerTest, SentEntropyHash) { 27 EXPECT_EQ(0, entropy_manager_.GetCumulativeEntropy(0)); 28 29 QuicPacketEntropyHash entropies[4] = {12, 1, 33, 3}; 30 for (size_t i = 0; i < arraysize(entropies); ++i) { 31 entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]); 32 } 33 34 QuicPacketEntropyHash hash = 0; 35 for (size_t i = 0; i < arraysize(entropies); ++i) { 36 hash ^= entropies[i]; 37 EXPECT_EQ(hash, entropy_manager_.GetCumulativeEntropy(i + 1)); 38 } 39 } 40 41 TEST_F(QuicSentEntropyManagerTest, IsValidEntropy) { 42 QuicPacketEntropyHash entropies[10] = 43 {12, 1, 33, 3, 32, 100, 28, 42, 22, 255}; 44 for (size_t i = 0; i < arraysize(entropies); ++i) { 45 entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]); 46 } 47 48 SequenceNumberSet missing_packets; 49 missing_packets.insert(1); 50 missing_packets.insert(4); 51 missing_packets.insert(7); 52 missing_packets.insert(8); 53 54 QuicPacketEntropyHash entropy_hash = 0; 55 for (size_t i = 0; i < arraysize(entropies); ++i) { 56 if (missing_packets.find(i + 1) == missing_packets.end()) { 57 entropy_hash ^= entropies[i]; 58 } 59 } 60 61 EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets, 62 entropy_hash)); 63 } 64 65 TEST_F(QuicSentEntropyManagerTest, ClearEntropiesBefore) { 66 QuicPacketEntropyHash entropies[10] = 67 {12, 1, 33, 3, 32, 100, 28, 42, 22, 255}; 68 69 for (size_t i = 0; i < arraysize(entropies); ++i) { 70 entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]); 71 } 72 73 // Discard the first 5 entropies and ensure IsValidEntropy and EntropyHash 74 // still return correct results. 75 entropy_manager_.ClearEntropyBefore(5); 76 77 SequenceNumberSet missing_packets; 78 missing_packets.insert(7); 79 missing_packets.insert(8); 80 81 QuicPacketEntropyHash entropy_hash = 0; 82 for (size_t i = 0; i < arraysize(entropies); ++i) { 83 if (missing_packets.find(i + 1) == missing_packets.end()) { 84 entropy_hash ^= entropies[i]; 85 } 86 } 87 EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets, 88 entropy_hash)); 89 90 entropy_hash = 0; 91 for (size_t i = 0; i < arraysize(entropies); ++i) { 92 entropy_hash ^= entropies[i]; 93 } 94 EXPECT_EQ(entropy_hash, entropy_manager_.GetCumulativeEntropy(10)); 95 } 96 97 } // namespace 98 } // namespace test 99 } // namespace net 100