summaryrefslogtreecommitdiffstats
path: root/net/quic/quic_sent_entropy_manager_test.cc
blob: 78bdcce8a2bdf5f4e014b8668374dbb34ff2debc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/quic_sent_entropy_manager.h"

#include <algorithm>
#include <vector>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using std::make_pair;
using std::pair;
using std::vector;

namespace net {
namespace test {
namespace {

class QuicSentEntropyManagerTest : public ::testing::Test {
 protected:
  QuicSentEntropyManager entropy_manager_;
};

TEST_F(QuicSentEntropyManagerTest, SentEntropyHash) {
  EXPECT_EQ(0, entropy_manager_.GetCumulativeEntropy(0));

  QuicPacketEntropyHash entropies[4] = {12, 1, 33, 3};
  for (size_t i = 0; i < arraysize(entropies); ++i) {
    entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]);
  }

  QuicPacketEntropyHash hash = 0;
  for (size_t i = 0; i < arraysize(entropies); ++i) {
    hash ^= entropies[i];
    EXPECT_EQ(hash, entropy_manager_.GetCumulativeEntropy(i + 1));
  }
}

TEST_F(QuicSentEntropyManagerTest, IsValidEntropy) {
  QuicPacketEntropyHash entropies[10] =
      {12, 1, 33, 3, 32, 100, 28, 42, 22, 255};
  for (size_t i = 0; i < arraysize(entropies); ++i) {
    entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]);
  }

  SequenceNumberSet missing_packets;
  missing_packets.insert(1);
  missing_packets.insert(4);
  missing_packets.insert(7);
  missing_packets.insert(8);

  QuicPacketEntropyHash entropy_hash = 0;
  for (size_t i = 0; i < arraysize(entropies); ++i) {
    if (missing_packets.find(i + 1) == missing_packets.end()) {
      entropy_hash ^= entropies[i];
    }
  }

  EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets,
                                              entropy_hash));
}

TEST_F(QuicSentEntropyManagerTest, ClearEntropiesBefore) {
  QuicPacketEntropyHash entropies[10] =
      {12, 1, 33, 3, 32, 100, 28, 42, 22, 255};

  for (size_t i = 0; i < arraysize(entropies); ++i) {
    entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]);
  }

  // Discard the first 5 entropies and ensure IsValidEntropy and EntropyHash
  // still return correct results.
  entropy_manager_.ClearEntropyBefore(5);

  SequenceNumberSet missing_packets;
  missing_packets.insert(7);
  missing_packets.insert(8);

  QuicPacketEntropyHash entropy_hash = 0;
  for (size_t i = 0; i < arraysize(entropies); ++i) {
    if (missing_packets.find(i + 1) == missing_packets.end()) {
      entropy_hash ^= entropies[i];
    }
  }
  EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets,
                                              entropy_hash));

  entropy_hash = 0;
  for (size_t i = 0; i < arraysize(entropies); ++i) {
    entropy_hash ^= entropies[i];
  }
  EXPECT_EQ(entropy_hash, entropy_manager_.GetCumulativeEntropy(10));
}

}  // namespace
}  // namespace test
}  // namespace net