Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(395)

Side by Side Diff: net/dns/mdns_cache_unittest.cc

Issue 14697022: Cache for mDNS records (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@record_parsed_klassbit
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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 "base/bind.h"
6 #include "net/dns/dns_response.h"
7 #include "net/dns/dns_test_util.h"
8 #include "net/dns/mdns_cache.h"
9 #include "net/dns/record_parsed.h"
10 #include "net/dns/record_rdata.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::Return;
15 using ::testing::StrictMock;
16
17 namespace net {
18
19 static const uint8 kTestResponsesDifferentAnswers[] = {
20 // Answer 1
21 // ghs.l.google.com in DNS format.
22 0x03, 'g', 'h', 's',
23 0x01, 'l',
24 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
25 0x03, 'c', 'o', 'm',
26 0x00,
27 0x00, 0x01, // TYPE is A.
28 0x00, 0x01, // CLASS is IN.
29 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
30 0x00, 0x35,
31 0x00, 0x04, // RDLENGTH is 4 bytes.
32 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
33 0x5f, 0x79,
34
35 // Answer 2
36 // Pointer to answer 1
37 0xc0, 0x00,
38 0x00, 0x01, // TYPE is A.
39 0x00, 0x01, // CLASS is IN.
40 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
41 0x00, 0x35,
42 0x00, 0x04, // RDLENGTH is 4 bytes.
43 0x4a, 0x7d, // RDATA is the IP: 74.125.95.122
44 0x5f, 0x80,
45 };
46
47 static const uint8 kTestResponsesSameAnswers[] = {
48 // Answer 1
49 // ghs.l.google.com in DNS format.
50 0x03, 'g', 'h', 's',
51 0x01, 'l',
52 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
53 0x03, 'c', 'o', 'm',
54 0x00,
55 0x00, 0x01, // TYPE is A.
56 0x00, 0x01, // CLASS is IN.
57 0x00, 0x00, // TTL (4 bytes) is 53 seconds.
58 0x00, 0x35,
59 0x00, 0x04, // RDLENGTH is 4 bytes.
60 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
61 0x5f, 0x79,
62
63 // Answer 2
64 // Pointer to answer 1
65 0xc0, 0x00,
66 0x00, 0x01, // TYPE is A.
67 0x00, 0x01, // CLASS is IN.
68 0x00, 0x00, // TTL (4 bytes) is 112 seconds.
69 0x00, 0x70,
70 0x00, 0x04, // RDLENGTH is 4 bytes.
71 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
72 0x5f, 0x79,
73 };
74
75 class RecordRemovalMock {
76 public:
77 MOCK_METHOD1(OnRecordRemoved, void(const RecordParsed*));
78 };
79
80 class MDnsCacheTest : public ::testing::Test {
81 public:
82 MDnsCacheTest()
83 : default_time_(base::Time::FromDoubleT(1234.0)),
84 cache_(base::Bind(&RecordRemovalMock::OnRecordRemoved,
85 base::Unretained(&record_removal_))) {}
86 virtual ~MDnsCacheTest() {}
87
88 protected:
89 base::Time default_time_;
90 StrictMock<RecordRemovalMock> record_removal_;
91 MDnsCache cache_;
92 };
93
94 // Test a single insert, corresponding lookup, and unsuccessful lookup.
95 TEST_F(MDnsCacheTest, InsertLookupSingle) {
96 DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
97 sizeof(dns_protocol::Header));
98 parser.SkipQuestion();
99
100 scoped_ptr<const RecordParsed> record1;
101 scoped_ptr<const RecordParsed> record2;
102 std::vector<const RecordParsed*> results;
103
104 record1 = RecordParsed::CreateFrom(&parser, default_time_);
105 record2 = RecordParsed::CreateFrom(&parser, default_time_);
106
107 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
108
109 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
110
111 cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
112 default_time_);
113
114 EXPECT_EQ(1u, results.size());
115 EXPECT_EQ(default_time_, results.front()->time_created());
116
117 EXPECT_EQ("ghs.l.google.com", results.front()->name());
118
119 results.clear();
120 cache_.FindDnsRecords(PtrRecordRdata::kType, "ghs.l.google.com", &results,
121 default_time_);
122
123 EXPECT_EQ(0u, results.size());
124 }
125
126 // Test that records expire when their ttl has passed.
127 TEST_F(MDnsCacheTest, Expiration) {
128 DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
129 sizeof(dns_protocol::Header));
130 parser.SkipQuestion();
131 scoped_ptr<const RecordParsed> record1;
132 scoped_ptr<const RecordParsed> record2;
133
134 std::vector<const RecordParsed*> results;
135 const RecordParsed* record_to_be_deleted;
136
137 record1 = RecordParsed::CreateFrom(&parser, default_time_);
138 base::TimeDelta ttl1 = base::TimeDelta::FromSeconds(record1->ttl());
139
140 record2 = RecordParsed::CreateFrom(&parser, default_time_);
141 base::TimeDelta ttl2 = base::TimeDelta::FromSeconds(record2->ttl());
142 record_to_be_deleted = record2.get();
143
144 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
145 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
146
147 cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
148 default_time_);
149
150 EXPECT_EQ(1u, results.size());
151
152 EXPECT_EQ(default_time_ + ttl2, cache_.next_expiration());
153
154
155 cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
156 default_time_ + ttl2);
157
158 EXPECT_EQ(0u, results.size());
159
160 EXPECT_CALL(record_removal_, OnRecordRemoved(record_to_be_deleted));
161
162 cache_.CleanupRecords(default_time_ + ttl2);
163
164 // To make sure that we've indeed removed them from the map, check no funny
165 // business happens once they're deleted for good.
166
167 EXPECT_EQ(default_time_ + ttl1, cache_.next_expiration());
168 cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
169 default_time_ + ttl2);
170
171 EXPECT_EQ(0u, results.size());
172 }
173
174 // Test that a new record replacing one with the same identity (name/rrtype for
175 // unique records) causes the cache to output a "record changed" event.
176 TEST_F(MDnsCacheTest, RecordChange) {
177 DnsRecordParser parser(kTestResponsesDifferentAnswers,
178 sizeof(kTestResponsesDifferentAnswers),
179 0);
180
181 scoped_ptr<const RecordParsed> record1;
182 scoped_ptr<const RecordParsed> record2;
183 std::vector<const RecordParsed*> results;
184
185 record1 = RecordParsed::CreateFrom(&parser, default_time_);
186 record2 = RecordParsed::CreateFrom(&parser, default_time_);
187
188 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
189 EXPECT_EQ(MDnsCache::RecordChanged,
190 cache_.UpdateDnsRecord(record2.Pass()));
191 }
192
193 // Test that a new record replacing an otherwise identical one already in the
194 // cache causes the cache to output a "no change" event.
195 TEST_F(MDnsCacheTest, RecordNoChange) {
196 DnsRecordParser parser(kTestResponsesSameAnswers,
197 sizeof(kTestResponsesSameAnswers),
198 0);
199
200 scoped_ptr<const RecordParsed> record1;
201 scoped_ptr<const RecordParsed> record2;
202 std::vector<const RecordParsed*> results;
203
204 record1 = RecordParsed::CreateFrom(&parser, default_time_);
205 record2 = RecordParsed::CreateFrom(&parser, default_time_ +
206 base::TimeDelta::FromSeconds(1));
207
208 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
209 EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record2.Pass()));
210 }
211
212 // Test that the next expiration time of the cache is updated properly on record
213 // insertion.
214 TEST_F(MDnsCacheTest, RecordPreemptExpirationTime) {
215 DnsRecordParser parser(kTestResponsesSameAnswers,
216 sizeof(kTestResponsesSameAnswers),
217 0);
218
219 scoped_ptr<const RecordParsed> record1;
220 scoped_ptr<const RecordParsed> record2;
221 std::vector<const RecordParsed*> results;
222
223 record1 = RecordParsed::CreateFrom(&parser, default_time_);
224 record2 = RecordParsed::CreateFrom(&parser, default_time_);
225 base::TimeDelta ttl1 = base::TimeDelta::FromSeconds(record1->ttl());
226 base::TimeDelta ttl2 = base::TimeDelta::FromSeconds(record2->ttl());
227
228 EXPECT_EQ(base::Time(), cache_.next_expiration());
229 EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
230 EXPECT_EQ(default_time_ + ttl2, cache_.next_expiration());
231 EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record1.Pass()));
232 EXPECT_EQ(default_time_ + ttl1, cache_.next_expiration());
233 }
234 }
OLDNEW
« net/dns/mdns_cache.cc ('K') | « net/dns/mdns_cache.cc ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698