| 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_received_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 QuicReceivedEntropyManagerTest : public ::testing::Test { |
| 22 protected: |
| 23 QuicReceivedEntropyManager entropy_manager_; |
| 24 }; |
| 25 |
| 26 TEST_F(QuicReceivedEntropyManagerTest, 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_.RecordPacketEntropyHash(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_.EntropyHash(i)); |
| 49 } |
| 50 } |
| 51 |
| 52 TEST_F(QuicReceivedEntropyManagerTest, EntropyHashBelowLeastObserved) { |
| 53 EXPECT_EQ(0, entropy_manager_.EntropyHash(0)); |
| 54 entropy_manager_.RecordPacketEntropyHash(4, 5); |
| 55 EXPECT_EQ(0, entropy_manager_.EntropyHash(3)); |
| 56 } |
| 57 |
| 58 TEST_F(QuicReceivedEntropyManagerTest, EntropyHashAboveLargestObserved) { |
| 59 EXPECT_EQ(0, entropy_manager_.EntropyHash(0)); |
| 60 entropy_manager_.RecordPacketEntropyHash(4, 5); |
| 61 EXPECT_EQ(0, entropy_manager_.EntropyHash(3)); |
| 62 } |
| 63 |
| 64 TEST_F(QuicReceivedEntropyManagerTest, RecalculateEntropyHash) { |
| 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_.RecordPacketEntropyHash(entropies[i].first, |
| 76 entropies[i].second); |
| 77 entropy_hash ^= entropies[i].second; |
| 78 } |
| 79 EXPECT_EQ(entropy_hash, entropy_manager_.EntropyHash(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_.RecalculateEntropyHash(4, 100); |
| 87 EXPECT_EQ(entropy_hash, entropy_manager_.EntropyHash(6)); |
| 88 |
| 89 // Ensure it doesn't change with an old received sequence number or entropy. |
| 90 entropy_manager_.RecordPacketEntropyHash(1, 50); |
| 91 EXPECT_EQ(entropy_hash, entropy_manager_.EntropyHash(6)); |
| 92 |
| 93 entropy_manager_.RecalculateEntropyHash(1, 50); |
| 94 EXPECT_EQ(entropy_hash, entropy_manager_.EntropyHash(6)); |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 } // namespace test |
| 99 } // namespace net |
| OLD | NEW |