summaryrefslogtreecommitdiffstats
path: root/net/dns/record_parsed_unittest.cc
blob: cfaac31c03b10d53fa62064049c5af5ebb609909 (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
// 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 "net/dns/record_parsed.h"

#include "net/dns/dns_protocol.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_test_util.h"
#include "net/dns/record_rdata.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

static const uint8_t kT1ResponseWithCacheFlushBit[] = {
    0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h', 'r',
    'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00,
    0x05,        // TYPE is CNAME.
    0x80, 0x01,  // CLASS is IN with cache flush bit set.
    0x00, 0x01,  // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
    0x24, 0x74, 0x00, 0x12,  // RDLENGTH is 18 bytes.
    // ghs.l.google.com in DNS format.
    0x03, 'g', 'h', 's', 0x01, 'l', 0x06, 'g', 'o', 'o', 'g', 'l', 'e', 0x03,
    'c', 'o', 'm', 0x00};

TEST(RecordParsedTest, ParseSingleRecord) {
  DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
                         sizeof(dns_protocol::Header));
  scoped_ptr<const RecordParsed> record;
  const CnameRecordRdata* rdata;

  parser.SkipQuestion();
  record = RecordParsed::CreateFrom(&parser, base::Time());
  EXPECT_TRUE(record != NULL);

  ASSERT_EQ("codereview.chromium.org", record->name());
  ASSERT_EQ(dns_protocol::kTypeCNAME, record->type());
  ASSERT_EQ(dns_protocol::kClassIN, record->klass());

  rdata = record->rdata<CnameRecordRdata>();
  ASSERT_TRUE(rdata != NULL);
  ASSERT_EQ(kT1CanonName, rdata->cname());

  ASSERT_FALSE(record->rdata<SrvRecordRdata>());
  ASSERT_TRUE(record->IsEqual(record.get(), true));
}

TEST(RecordParsedTest, CacheFlushBitCompare) {
  DnsRecordParser parser1(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
                         sizeof(dns_protocol::Header));
  parser1.SkipQuestion();
  scoped_ptr<const RecordParsed> record1 =
      RecordParsed::CreateFrom(&parser1, base::Time());

  DnsRecordParser parser2(kT1ResponseWithCacheFlushBit,
                          sizeof(kT1ResponseWithCacheFlushBit),
                          0);

  scoped_ptr<const RecordParsed> record2 =
      RecordParsed::CreateFrom(&parser2, base::Time());

  EXPECT_FALSE(record1->IsEqual(record2.get(), false));
  EXPECT_TRUE(record1->IsEqual(record2.get(), true));
  EXPECT_FALSE(record2->IsEqual(record1.get(), false));
  EXPECT_TRUE(record2->IsEqual(record1.get(), true));
}

}  //namespace net