| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_packet_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 QuicPacketEntropyManagerTest : public ::testing::Test { | |
| 22 protected: | |
| 23 QuicPacketEntropyManager entropy_manager_; | |
| 24 }; | |
| 25 | |
| 26 TEST_F(QuicPacketEntropyManagerTest, ReceivedPacketEntropyHash) { | |
| 27 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies; | |
| 28 entropies.push_back(make_pair(1, 12)); | |
| 29 entropies.push_back(make_pair(7, 1)); | |
| 30 entropies.push_back(make_pair(2, 33)); | |
| 31 entropies.push_back(make_pair(5, 3)); | |
| 32 entropies.push_back(make_pair(8, 34)); | |
| 33 | |
| 34 for (size_t i = 0; i < entropies.size(); ++i) { | |
| 35 entropy_manager_.RecordReceivedPacketEntropyHash(entropies[i].first, | |
| 36 entropies[i].second); | |
| 37 } | |
| 38 | |
| 39 sort(entropies.begin(), entropies.end()); | |
| 40 | |
| 41 QuicPacketEntropyHash hash = 0; | |
| 42 size_t index = 0; | |
| 43 for (size_t i = 1; i <= (*entropies.rbegin()).first; ++i) { | |
| 44 if (entropies[index].first == i) { | |
| 45 hash ^= entropies[index].second; | |
| 46 ++index; | |
| 47 } | |
| 48 EXPECT_EQ(hash, entropy_manager_.ReceivedEntropyHash(i)); | |
| 49 } | |
| 50 }; | |
| 51 | |
| 52 TEST_F(QuicPacketEntropyManagerTest, EntropyHashBelowLeastObserved) { | |
| 53 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(0)); | |
| 54 entropy_manager_.RecordReceivedPacketEntropyHash(4, 5); | |
| 55 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(3)); | |
| 56 }; | |
| 57 | |
| 58 TEST_F(QuicPacketEntropyManagerTest, EntropyHashAboveLargestObserved) { | |
| 59 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(0)); | |
| 60 entropy_manager_.RecordReceivedPacketEntropyHash(4, 5); | |
| 61 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(3)); | |
| 62 }; | |
| 63 | |
| 64 TEST_F(QuicPacketEntropyManagerTest, RecalculateReceivedEntropyHash) { | |
| 65 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies; | |
| 66 entropies.push_back(make_pair(1, 12)); | |
| 67 entropies.push_back(make_pair(2, 1)); | |
| 68 entropies.push_back(make_pair(3, 33)); | |
| 69 entropies.push_back(make_pair(4, 3)); | |
| 70 entropies.push_back(make_pair(5, 34)); | |
| 71 entropies.push_back(make_pair(6, 29)); | |
| 72 | |
| 73 QuicPacketEntropyHash entropy_hash = 0; | |
| 74 for (size_t i = 0; i < entropies.size(); ++i) { | |
| 75 entropy_manager_.RecordReceivedPacketEntropyHash(entropies[i].first, | |
| 76 entropies[i].second); | |
| 77 entropy_hash ^= entropies[i].second; | |
| 78 } | |
| 79 EXPECT_EQ(entropy_hash, entropy_manager_.ReceivedEntropyHash(6)); | |
| 80 | |
| 81 // Now set the entropy hash up to 4 to be 100. | |
| 82 entropy_hash ^= 100; | |
| 83 for (size_t i = 0; i < 3; ++i) { | |
| 84 entropy_hash ^= entropies[i].second; | |
| 85 } | |
| 86 entropy_manager_.RecalculateReceivedEntropyHash(4, 100); | |
| 87 EXPECT_EQ(entropy_hash, entropy_manager_.ReceivedEntropyHash(6)); | |
| 88 | |
| 89 // Ensure it doesn't change with an old received sequence number or entropy. | |
| 90 entropy_manager_.RecordReceivedPacketEntropyHash(1, 50); | |
| 91 EXPECT_EQ(entropy_hash, entropy_manager_.ReceivedEntropyHash(6)); | |
| 92 | |
| 93 entropy_manager_.RecalculateReceivedEntropyHash(1, 50); | |
| 94 EXPECT_EQ(entropy_hash, entropy_manager_.ReceivedEntropyHash(6)); | |
| 95 } | |
| 96 | |
| 97 TEST_F(QuicPacketEntropyManagerTest, SentEntropyHash) { | |
| 98 EXPECT_EQ(0, entropy_manager_.SentEntropyHash(0)); | |
| 99 | |
| 100 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies; | |
| 101 entropies.push_back(make_pair(1, 12)); | |
| 102 entropies.push_back(make_pair(2, 1)); | |
| 103 entropies.push_back(make_pair(3, 33)); | |
| 104 entropies.push_back(make_pair(4, 3)); | |
| 105 | |
| 106 for (size_t i = 0; i < entropies.size(); ++i) { | |
| 107 entropy_manager_.RecordSentPacketEntropyHash(entropies[i].first, | |
| 108 entropies[i].second); | |
| 109 } | |
| 110 | |
| 111 QuicPacketEntropyHash hash = 0; | |
| 112 for (size_t i = 0; i < entropies.size(); ++i) { | |
| 113 hash ^= entropies[i].second; | |
| 114 EXPECT_EQ(hash, entropy_manager_.SentEntropyHash(i + 1)); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 TEST_F(QuicPacketEntropyManagerTest, IsValidEntropy) { | |
| 119 QuicPacketEntropyHash entropies[10] = | |
| 120 {12, 1, 33, 3, 32, 100, 28, 42, 22, 255}; | |
| 121 for (size_t i = 0; i < 10; ++i) { | |
| 122 entropy_manager_.RecordSentPacketEntropyHash(i + 1, entropies[i]); | |
| 123 } | |
| 124 | |
| 125 SequenceNumberSet missing_packets; | |
| 126 missing_packets.insert(1); | |
| 127 missing_packets.insert(4); | |
| 128 missing_packets.insert(7); | |
| 129 missing_packets.insert(8); | |
| 130 | |
| 131 QuicPacketEntropyHash entropy_hash = 0; | |
| 132 for (size_t i = 0; i < 10; ++i) { | |
| 133 if (missing_packets.find(i + 1) == missing_packets.end()) { | |
| 134 entropy_hash ^= entropies[i]; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets, | |
| 139 entropy_hash)); | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| 143 } // namespace test | |
| 144 } // namespace net | |
| OLD | NEW |