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