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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/dns_udp_tracker.h"
#include "base/test/simple_test_tick_clock.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class DnsUdpTrackerTest : public testing::Test {
public:
DnsUdpTrackerTest() {
tracker_.set_tick_clock_for_testing(&test_tick_clock_);
}
protected:
DnsUdpTracker tracker_;
base::SimpleTestTickClock test_tick_clock_;
};
TEST_F(DnsUdpTrackerTest, MatchingId) {
uint16_t port = 416;
uint16_t id = 56;
for (size_t i = 0; i < DnsUdpTracker::kRecognizedIdMismatchThreshold; ++i) {
tracker_.RecordQuery(++port, ++id);
tracker_.RecordResponseId(id /* query_id */, id /* response_id */);
EXPECT_FALSE(tracker_.low_entropy());
}
}
TEST_F(DnsUdpTrackerTest, ReusedMismatches) {
static const uint16_t kOldId = 786;
tracker_.RecordQuery(123 /* port */, kOldId);
uint16_t port = 3889;
uint16_t id = 3456;
for (size_t i = 0; i < DnsUdpTracker::kRecognizedIdMismatchThreshold; ++i) {
EXPECT_FALSE(tracker_.low_entropy());
tracker_.RecordQuery(++port, ++id);
tracker_.RecordResponseId(id /* query_id */, kOldId /* response_id */);
}
EXPECT_TRUE(tracker_.low_entropy());
}
TEST_F(DnsUdpTrackerTest, ReusedMismatches_Expired) {
static const uint16_t kOldId = 786;
tracker_.RecordQuery(123 /* port */, kOldId);
test_tick_clock_.Advance(DnsUdpTracker::kMaxAge + base::Milliseconds(1));
uint16_t port = 3889;
uint16_t id = 3456;
// Because the query record has expired, the ID should be treated as
// unrecognized.
for (size_t i = 0; i < DnsUdpTracker::kUnrecognizedIdMismatchThreshold; ++i) {
EXPECT_FALSE(tracker_.low_entropy());
tracker_.RecordQuery(++port, ++id);
tracker_.RecordResponseId(id /* query_id */, kOldId /* response_id */);
}
EXPECT_TRUE(tracker_.low_entropy());
}
// Test for ID mismatches using an ID still kept in recorded queries, but not
// recent enough to be considered reognized.
TEST_F(DnsUdpTrackerTest, ReusedMismatches_Old) {
static const uint16_t kOldId = 786;
tracker_.RecordQuery(123 /* port */, kOldId);
test_tick_clock_.Advance(DnsUdpTracker::kMaxRecognizedIdAge +
base::Milliseconds(1));
uint16_t port = 3889;
uint16_t id = 3456;
// Expect the ID to be treated as unrecognized.
for (size_t i = 0; i < DnsUdpTracker::kUnrecognizedIdMismatchThreshold; ++i) {
EXPECT_FALSE(tracker_.low_entropy());
tracker_.RecordQuery(++port, ++id);
tracker_.RecordResponseId(id /* query_id */, kOldId /* response_id */);
}
EXPECT_TRUE(tracker_.low_entropy());
}
TEST_F(DnsUdpTrackerTest, ReusedMismatches_Full) {
static const uint16_t kOldId = 786;
tracker_.RecordQuery(123 /* port */, kOldId);
uint16_t port = 124;
uint16_t id = 3457;
for (size_t i = 0; i < DnsUdpTracker::kMaxRecordedQueries; ++i) {
tracker_.RecordQuery(++port, ++id);
}
// Expect the ID to be treated as unrecognized.
for (size_t i = 0; i < DnsUdpTracker::kUnrecognizedIdMismatchThreshold; ++i) {
EXPECT_FALSE(tracker_.low_entropy());
tracker_.RecordResponseId(id /* query_id */, kOldId /* response_id */);
}
EXPECT_TRUE(tracker_.low_entropy());
}
TEST_F(DnsUdpTrackerTest, UnknownMismatches) {
uint16_t port = 10014;
uint16_t id = 4332;
for (size_t i = 0; i < DnsUdpTracker::kUnrecognizedIdMismatchThreshold; ++i) {
EXPECT_FALSE(tracker_.low_entropy());
tracker_.RecordQuery(++port, ++id);
tracker_.RecordResponseId(id /* query_id */, 743 /* response_id */);
}
EXPECT_TRUE(tracker_.low_entropy());
}
TEST_F(DnsUdpTrackerTest, ReusedPort) {
static const uint16_t kPort = 2135;
tracker_.RecordQuery(kPort, 579 /* query_id */);
uint16_t id = 580;
for (int i = 0; i < DnsUdpTracker::kPortReuseThreshold; ++i) {
EXPECT_FALSE(tracker_.low_entropy());
tracker_.RecordQuery(kPort, ++id);
tracker_.RecordResponseId(id /* query_id */, id /* response_id */);
}
EXPECT_TRUE(tracker_.low_entropy());
}
TEST_F(DnsUdpTrackerTest, ReusedPort_Expired) {
static const uint16_t kPort = 2135;
tracker_.RecordQuery(kPort, 579 /* query_id */);
test_tick_clock_.Advance(DnsUdpTracker::kMaxAge + base::Milliseconds(1));
EXPECT_FALSE(tracker_.low_entropy());
uint16_t id = 580;
for (int i = 0; i < DnsUdpTracker::kPortReuseThreshold; ++i) {
tracker_.RecordQuery(kPort, ++id);
tracker_.RecordResponseId(id /* query_id */, id /* response_id */);
EXPECT_FALSE(tracker_.low_entropy());
}
}
TEST_F(DnsUdpTrackerTest, ReusedPort_Full) {
static const uint16_t kPort = 2135;
tracker_.RecordQuery(kPort, 579 /* query_id */);
uint16_t port = 124;
uint16_t id = 3457;
for (size_t i = 0; i < DnsUdpTracker::kMaxRecordedQueries; ++i) {
tracker_.RecordQuery(++port, ++id);
}
EXPECT_FALSE(tracker_.low_entropy());
for (int i = 0; i < DnsUdpTracker::kPortReuseThreshold; ++i) {
tracker_.RecordQuery(kPort, ++id);
tracker_.RecordResponseId(id /* query_id */, id /* response_id */);
EXPECT_FALSE(tracker_.low_entropy());
}
}
TEST_F(DnsUdpTrackerTest, ConnectionError) {
tracker_.RecordConnectionError(ERR_FAILED);
EXPECT_FALSE(tracker_.low_entropy());
}
TEST_F(DnsUdpTrackerTest, ConnectionError_InsufficientResources) {
tracker_.RecordConnectionError(ERR_INSUFFICIENT_RESOURCES);
EXPECT_TRUE(tracker_.low_entropy());
}
} // namespace
} // namespace net
|