| 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 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(9)); |
| 55 entropy_manager_.RecordReceivedPacketEntropyHash(4, 5); |
| 56 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(3)); |
| 57 }; |
| 58 |
| 59 TEST_F(QuicPacketEntropyManagerTest, EntropyHashAboveLargesObserved) { |
| 60 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(0)); |
| 61 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(9)); |
| 62 entropy_manager_.RecordReceivedPacketEntropyHash(4, 5); |
| 63 EXPECT_EQ(0, entropy_manager_.ReceivedEntropyHash(3)); |
| 64 }; |
| 65 |
| 66 TEST_F(QuicPacketEntropyManagerTest, RecalculateReceivedEntropyHash) { |
| 67 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies; |
| 68 entropies.push_back(make_pair(1, 12)); |
| 69 entropies.push_back(make_pair(2, 1)); |
| 70 entropies.push_back(make_pair(3, 33)); |
| 71 entropies.push_back(make_pair(4, 3)); |
| 72 entropies.push_back(make_pair(5, 34)); |
| 73 entropies.push_back(make_pair(6, 29)); |
| 74 |
| 75 QuicPacketEntropyHash entropy_hash = 0; |
| 76 for (size_t i = 0; i < entropies.size(); ++i) { |
| 77 entropy_manager_.RecordReceivedPacketEntropyHash(entropies[i].first, |
| 78 entropies[i].second); |
| 79 entropy_hash ^= entropies[i].second; |
| 80 } |
| 81 EXPECT_EQ(entropy_hash, entropy_manager_.ReceivedEntropyHash(6)); |
| 82 |
| 83 // Now set the entropy hash up to 4 to be 100. |
| 84 entropy_hash ^= 100; |
| 85 for (size_t i = 0; i < 3; ++i) { |
| 86 entropy_hash ^= entropies[i].second; |
| 87 } |
| 88 entropy_manager_.RecalculateReceivedEntropyHash(4, 100); |
| 89 EXPECT_EQ(entropy_hash, entropy_manager_.ReceivedEntropyHash(6)); |
| 90 } |
| 91 |
| 92 TEST_F(QuicPacketEntropyManagerTest, SentEntropyHash) { |
| 93 EXPECT_EQ(0, entropy_manager_.SentEntropyHash(0)); |
| 94 |
| 95 vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies; |
| 96 entropies.push_back(make_pair(1, 12)); |
| 97 entropies.push_back(make_pair(2, 1)); |
| 98 entropies.push_back(make_pair(3, 33)); |
| 99 entropies.push_back(make_pair(4, 3)); |
| 100 |
| 101 for (size_t i = 0; i < entropies.size(); ++i) { |
| 102 entropy_manager_.RecordSentPacketEntropyHash(entropies[i].first, |
| 103 entropies[i].second); |
| 104 } |
| 105 |
| 106 QuicPacketEntropyHash hash = 0; |
| 107 for (size_t i = 0; i < entropies.size(); ++i) { |
| 108 hash ^= entropies[i].second; |
| 109 EXPECT_EQ(hash, entropy_manager_.SentEntropyHash(i + 1)); |
| 110 } |
| 111 } |
| 112 |
| 113 TEST_F(QuicPacketEntropyManagerTest, IsValidEntropy) { |
| 114 QuicPacketEntropyHash entropies[10] = |
| 115 {12, 1, 33, 3, 32, 100, 28, 42, 22, 255}; |
| 116 for (size_t i = 0; i < 10; ++i) { |
| 117 entropy_manager_.RecordSentPacketEntropyHash(i + 1, entropies[i]); |
| 118 } |
| 119 |
| 120 SequenceNumberSet missing_packets; |
| 121 missing_packets.insert(1); |
| 122 missing_packets.insert(4); |
| 123 missing_packets.insert(7); |
| 124 missing_packets.insert(8); |
| 125 |
| 126 QuicPacketEntropyHash entropy_hash = 0; |
| 127 for (size_t i = 0; i < 10; ++i) { |
| 128 if (missing_packets.find(i + 1) == missing_packets.end()) { |
| 129 entropy_hash ^= entropies[i]; |
| 130 } |
| 131 } |
| 132 |
| 133 EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets, |
| 134 entropy_hash)); |
| 135 } |
| 136 |
| 137 } // namespace |
| 138 } // namespace test |
| 139 } // namespace net |
| OLD | NEW |