quic_sent_entropy_manager_test.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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
13using std::make_pair;
14using std::pair;
15using std::vector;
16
17namespace net {
18namespace test {
19namespace {
20
21class QuicSentEntropyManagerTest : public ::testing::Test {
22 protected:
23  QuicSentEntropyManager entropy_manager_;
24};
25
26TEST_F(QuicSentEntropyManagerTest, SentEntropyHash) {
27  EXPECT_EQ(0, entropy_manager_.EntropyHash(0));
28
29  vector<pair<QuicPacketSequenceNumber, QuicPacketEntropyHash> > entropies;
30  entropies.push_back(make_pair(1, 12));
31  entropies.push_back(make_pair(2, 1));
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  }
39
40  QuicPacketEntropyHash hash = 0;
41  for (size_t i = 0; i < entropies.size(); ++i) {
42    hash ^= entropies[i].second;
43    EXPECT_EQ(hash, entropy_manager_.EntropyHash(i + 1));
44  }
45}
46
47TEST_F(QuicSentEntropyManagerTest, IsValidEntropy) {
48  QuicPacketEntropyHash entropies[10] =
49      {12, 1, 33, 3, 32, 100, 28, 42, 22, 255};
50  for (size_t i = 0; i < 10; ++i) {
51    entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]);
52  }
53
54  SequenceNumberSet missing_packets;
55  missing_packets.insert(1);
56  missing_packets.insert(4);
57  missing_packets.insert(7);
58  missing_packets.insert(8);
59
60  QuicPacketEntropyHash entropy_hash = 0;
61  for (size_t i = 0; i < 10; ++i) {
62    if (missing_packets.find(i + 1) == missing_packets.end()) {
63      entropy_hash ^= entropies[i];
64    }
65  }
66
67  EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets,
68                                              entropy_hash));
69}
70
71}  // namespace
72}  // namespace test
73}  // namespace net
74