summaryrefslogtreecommitdiff
path: root/chromium/net/dns/mdns_cache_unittest.cc
blob: c12ad6b6ec3c830fbb88489aea4d8c41df3ea5f4 (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (c) 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 <algorithm>

#include "base/bind.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_test_util.h"
#include "net/dns/mdns_cache.h"
#include "net/dns/record_parsed.h"
#include "net/dns/record_rdata.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Return;
using ::testing::StrictMock;

namespace net {

static const uint8 kTestResponsesDifferentAnswers[] = {
  // Answer 1
  // ghs.l.google.com in DNS format.
  3, 'g', 'h', 's',
  1, 'l',
  6, 'g', 'o', 'o', 'g', 'l', 'e',
  3, 'c', 'o', 'm',
  0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 53,        // TTL (4 bytes) is 53 seconds.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 121,   // RDATA is the IP: 74.125.95.121

  // Answer 2
  // Pointer to answer 1
  0xc0, 0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 53,        // TTL (4 bytes) is 53 seconds.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 122,   // RDATA is the IP: 74.125.95.122
};

static const uint8 kTestResponsesSameAnswers[] = {
  // Answer 1
  // ghs.l.google.com in DNS format.
  3, 'g', 'h', 's',
  1, 'l',
  6, 'g', 'o', 'o', 'g', 'l', 'e',
  3, 'c', 'o', 'm',
  0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 53,        // TTL (4 bytes) is 53 seconds.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 121,   // RDATA is the IP: 74.125.95.121

  // Answer 2
  // Pointer to answer 1
  0xc0, 0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 112,       // TTL (4 bytes) is 112 seconds.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 121,   // RDATA is the IP: 74.125.95.121
};

static const uint8 kTestResponseTwoRecords[] = {
  // Answer 1
  // ghs.l.google.com in DNS format. (A)
  3, 'g', 'h', 's',
  1, 'l',
  6, 'g', 'o', 'o', 'g', 'l', 'e',
  3, 'c', 'o', 'm',
  0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 53,        // TTL (4 bytes) is 53 seconds.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 121,   // RDATA is the IP: 74.125.95.121

  // Answer 2
  // ghs.l.google.com in DNS format. (AAAA)
  3, 'g', 'h', 's',
  1, 'l',
  6, 'g', 'o', 'o', 'g', 'l', 'e',
  3, 'c', 'o', 'm',
  0x00,
  0x00, 0x1c,         // TYPE is AAA.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 53,        // TTL (4 bytes) is 53 seconds.
  0, 16,              // RDLENGTH is 16 bytes.
  0x4a, 0x7d, 0x4a, 0x7d,
  0x5f, 0x79, 0x5f, 0x79,
  0x5f, 0x79, 0x5f, 0x79,
  0x5f, 0x79, 0x5f, 0x79,
};

static const uint8 kTestResponsesGoodbyePacket[] = {
  // Answer 1
  // ghs.l.google.com in DNS format. (Goodbye packet)
  3, 'g', 'h', 's',
  1, 'l',
  6, 'g', 'o', 'o', 'g', 'l', 'e',
  3, 'c', 'o', 'm',
  0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 0,         // TTL (4 bytes) is zero.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 121,   // RDATA is the IP: 74.125.95.121

  // Answer 2
  // ghs.l.google.com in DNS format.
  3, 'g', 'h', 's',
  1, 'l',
  6, 'g', 'o', 'o', 'g', 'l', 'e',
  3, 'c', 'o', 'm',
  0x00,
  0x00, 0x01,         // TYPE is A.
  0x00, 0x01,         // CLASS is IN.
  0, 0, 0, 53,        // TTL (4 bytes) is 53 seconds.
  0, 4,               // RDLENGTH is 4 bytes.
  74, 125, 95, 121,   // RDATA is the IP: 74.125.95.121
};

class RecordRemovalMock {
 public:
  MOCK_METHOD1(OnRecordRemoved, void(const RecordParsed*));
};

class MDnsCacheTest : public ::testing::Test {
 public:
  MDnsCacheTest()
      : default_time_(base::Time::FromDoubleT(1234.0)) {}
  virtual ~MDnsCacheTest() {}

 protected:
  base::Time default_time_;
  StrictMock<RecordRemovalMock> record_removal_;
  MDnsCache cache_;
};

// Test a single insert, corresponding lookup, and unsuccessful lookup.
TEST_F(MDnsCacheTest, InsertLookupSingle) {
  DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
                         sizeof(dns_protocol::Header));
  parser.SkipQuestion();

  scoped_ptr<const RecordParsed> record1;
  scoped_ptr<const RecordParsed> record2;
  std::vector<const RecordParsed*> results;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  record2 = RecordParsed::CreateFrom(&parser, default_time_);

  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));

  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));

  cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
                        default_time_);

  EXPECT_EQ(1u, results.size());
  EXPECT_EQ(default_time_, results.front()->time_created());

  EXPECT_EQ("ghs.l.google.com", results.front()->name());

  results.clear();
  cache_.FindDnsRecords(PtrRecordRdata::kType, "ghs.l.google.com", &results,
                        default_time_);

  EXPECT_EQ(0u, results.size());
}

// Test that records expire when their ttl has passed.
TEST_F(MDnsCacheTest, Expiration) {
  DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
                         sizeof(dns_protocol::Header));
  parser.SkipQuestion();
  scoped_ptr<const RecordParsed> record1;
  scoped_ptr<const RecordParsed> record2;

  std::vector<const RecordParsed*> results;
  const RecordParsed* record_to_be_deleted;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  base::TimeDelta ttl1 = base::TimeDelta::FromSeconds(record1->ttl());

  record2 = RecordParsed::CreateFrom(&parser, default_time_);
  base::TimeDelta ttl2 = base::TimeDelta::FromSeconds(record2->ttl());
  record_to_be_deleted = record2.get();

  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));

  cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
                        default_time_);

  EXPECT_EQ(1u, results.size());

  EXPECT_EQ(default_time_ + ttl2, cache_.next_expiration());


  cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
                        default_time_ + ttl2);

  EXPECT_EQ(0u, results.size());

  EXPECT_CALL(record_removal_, OnRecordRemoved(record_to_be_deleted));

  cache_.CleanupRecords(default_time_ + ttl2, base::Bind(
      &RecordRemovalMock::OnRecordRemoved, base::Unretained(&record_removal_)));

  // To make sure that we've indeed removed them from the map, check no funny
  // business happens once they're deleted for good.

  EXPECT_EQ(default_time_ + ttl1, cache_.next_expiration());
  cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
                        default_time_ + ttl2);

  EXPECT_EQ(0u, results.size());
}

// Test that a new record replacing one with the same identity (name/rrtype for
// unique records) causes the cache to output a "record changed" event.
TEST_F(MDnsCacheTest, RecordChange) {
  DnsRecordParser parser(kTestResponsesDifferentAnswers,
                         sizeof(kTestResponsesDifferentAnswers),
                         0);

  scoped_ptr<const RecordParsed> record1;
  scoped_ptr<const RecordParsed> record2;
  std::vector<const RecordParsed*> results;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  record2 = RecordParsed::CreateFrom(&parser, default_time_);

  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
  EXPECT_EQ(MDnsCache::RecordChanged,
            cache_.UpdateDnsRecord(record2.Pass()));
}

// Test that a new record replacing an otherwise identical one already in the
// cache causes the cache to output a "no change" event.
TEST_F(MDnsCacheTest, RecordNoChange) {
  DnsRecordParser parser(kTestResponsesSameAnswers,
                         sizeof(kTestResponsesSameAnswers),
                         0);

  scoped_ptr<const RecordParsed> record1;
  scoped_ptr<const RecordParsed> record2;
  std::vector<const RecordParsed*> results;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  record2 = RecordParsed::CreateFrom(&parser, default_time_ +
                                     base::TimeDelta::FromSeconds(1));

  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
  EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record2.Pass()));
}

// Test that the next expiration time of the cache is updated properly on record
// insertion.
TEST_F(MDnsCacheTest, RecordPreemptExpirationTime) {
  DnsRecordParser parser(kTestResponsesSameAnswers,
                         sizeof(kTestResponsesSameAnswers),
                         0);

  scoped_ptr<const RecordParsed> record1;
  scoped_ptr<const RecordParsed> record2;
  std::vector<const RecordParsed*> results;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  record2 = RecordParsed::CreateFrom(&parser, default_time_);
  base::TimeDelta ttl1 = base::TimeDelta::FromSeconds(record1->ttl());
  base::TimeDelta ttl2 = base::TimeDelta::FromSeconds(record2->ttl());

  EXPECT_EQ(base::Time(), cache_.next_expiration());
  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
  EXPECT_EQ(default_time_ + ttl2, cache_.next_expiration());
  EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record1.Pass()));
  EXPECT_EQ(default_time_ + ttl1, cache_.next_expiration());
}

// Test that the cache handles mDNS "goodbye" packets correctly, not adding the
// records to the cache if they are not already there, and eventually removing
// records from the cache if they are.
TEST_F(MDnsCacheTest, GoodbyePacket) {
  DnsRecordParser parser(kTestResponsesGoodbyePacket,
                         sizeof(kTestResponsesGoodbyePacket),
                         0);

  scoped_ptr<const RecordParsed> record_goodbye;
  scoped_ptr<const RecordParsed> record_hello;
  scoped_ptr<const RecordParsed> record_goodbye2;
  std::vector<const RecordParsed*> results;

  record_goodbye = RecordParsed::CreateFrom(&parser, default_time_);
  record_hello = RecordParsed::CreateFrom(&parser, default_time_);
  parser = DnsRecordParser(kTestResponsesGoodbyePacket,
                           sizeof(kTestResponsesGoodbyePacket),
                           0);
  record_goodbye2 = RecordParsed::CreateFrom(&parser, default_time_);

  base::TimeDelta ttl = base::TimeDelta::FromSeconds(record_hello->ttl());

  EXPECT_EQ(base::Time(), cache_.next_expiration());
  EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record_goodbye.Pass()));
  EXPECT_EQ(base::Time(), cache_.next_expiration());
  EXPECT_EQ(MDnsCache::RecordAdded,
            cache_.UpdateDnsRecord(record_hello.Pass()));
  EXPECT_EQ(default_time_ + ttl, cache_.next_expiration());
  EXPECT_EQ(MDnsCache::NoChange,
            cache_.UpdateDnsRecord(record_goodbye2.Pass()));
  EXPECT_EQ(default_time_ + base::TimeDelta::FromSeconds(1),
            cache_.next_expiration());
}

TEST_F(MDnsCacheTest, AnyRRType) {
  DnsRecordParser parser(kTestResponseTwoRecords,
                         sizeof(kTestResponseTwoRecords),
                         0);

  scoped_ptr<const RecordParsed> record1;
  scoped_ptr<const RecordParsed> record2;
  std::vector<const RecordParsed*> results;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  record2 = RecordParsed::CreateFrom(&parser, default_time_);
  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));

  cache_.FindDnsRecords(0, "ghs.l.google.com", &results, default_time_);

  EXPECT_EQ(2u, results.size());
  EXPECT_EQ(default_time_, results.front()->time_created());

  EXPECT_EQ("ghs.l.google.com", results[0]->name());
  EXPECT_EQ("ghs.l.google.com", results[1]->name());
  EXPECT_EQ(dns_protocol::kTypeA,
            std::min(results[0]->type(), results[1]->type()));
  EXPECT_EQ(dns_protocol::kTypeAAAA,
            std::max(results[0]->type(), results[1]->type()));
}

TEST_F(MDnsCacheTest, RemoveRecord) {
  DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
                         sizeof(dns_protocol::Header));
  parser.SkipQuestion();

  scoped_ptr<const RecordParsed> record1;
  std::vector<const RecordParsed*> results;

  record1 = RecordParsed::CreateFrom(&parser, default_time_);
  EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));

  cache_.FindDnsRecords(dns_protocol::kTypeCNAME, "codereview.chromium.org",
                        &results, default_time_);

  EXPECT_EQ(1u, results.size());

  scoped_ptr<const RecordParsed> record_out =
      cache_.RemoveRecord(results.front());

  EXPECT_EQ(record_out.get(), results.front());

  cache_.FindDnsRecords(dns_protocol::kTypeCNAME, "codereview.chromium.org",
                        &results, default_time_);

  EXPECT_EQ(0u, results.size());
}

}  // namespace net