OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/dns/record_parsed.h" | 5 #include "net/dns/record_parsed.h" |
6 | 6 |
7 #include "net/dns/dns_protocol.h" | 7 #include "net/dns/dns_protocol.h" |
8 #include "net/dns/dns_response.h" | 8 #include "net/dns/dns_response.h" |
9 #include "net/dns/dns_test_util.h" | 9 #include "net/dns/dns_test_util.h" |
10 #include "net/dns/record_rdata.h" | 10 #include "net/dns/record_rdata.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
| 15 static const char kT1ResponseWithCacheFlushBit[] = { |
| 16 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', |
| 17 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', |
| 18 0x03, 'o', 'r', 'g', |
| 19 0x00, |
| 20 0x00, 0x05, // TYPE is CNAME. |
| 21 0x80, 0x01, // CLASS is IN with cache flush bit set. |
| 22 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 23 0x24, 0x74, |
| 24 0x00, 0x12, // RDLENGTH is 18 bytes. |
| 25 // ghs.l.google.com in DNS format. |
| 26 0x03, 'g', 'h', 's', |
| 27 0x01, 'l', |
| 28 0x06, 'g', 'o', 'o', 'g', 'l', 'e', |
| 29 0x03, 'c', 'o', 'm', |
| 30 0x00 |
| 31 }; |
| 32 |
15 TEST(RecordParsedTest, ParseSingleRecord) { | 33 TEST(RecordParsedTest, ParseSingleRecord) { |
16 DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram), | 34 DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram), |
17 sizeof(dns_protocol::Header)); | 35 sizeof(dns_protocol::Header)); |
18 scoped_ptr<const RecordParsed> record; | 36 scoped_ptr<const RecordParsed> record; |
19 const CnameRecordRdata* rdata; | 37 const CnameRecordRdata* rdata; |
20 | 38 |
21 parser.SkipQuestion(); | 39 parser.SkipQuestion(); |
22 record = RecordParsed::CreateFrom(&parser); | 40 record = RecordParsed::CreateFrom(&parser, base::Time()); |
23 EXPECT_TRUE(record != NULL); | 41 EXPECT_TRUE(record != NULL); |
24 | 42 |
25 ASSERT_EQ("codereview.chromium.org", record->name()); | 43 ASSERT_EQ("codereview.chromium.org", record->name()); |
26 ASSERT_EQ(dns_protocol::kTypeCNAME, record->type()); | 44 ASSERT_EQ(dns_protocol::kTypeCNAME, record->type()); |
27 ASSERT_EQ(dns_protocol::kClassIN, record->klass()); | 45 ASSERT_EQ(dns_protocol::kClassIN, record->klass()); |
28 | 46 |
29 rdata = record->rdata<CnameRecordRdata>(); | 47 rdata = record->rdata<CnameRecordRdata>(); |
30 ASSERT_TRUE(rdata != NULL); | 48 ASSERT_TRUE(rdata != NULL); |
31 ASSERT_EQ(kT1CanonName, rdata->cname()); | 49 ASSERT_EQ(kT1CanonName, rdata->cname()); |
32 | 50 |
33 ASSERT_FALSE(record->rdata<SrvRecordRdata>()); | 51 ASSERT_FALSE(record->rdata<SrvRecordRdata>()); |
| 52 ASSERT_TRUE(record->IsEqual(record.get(), true)); |
| 53 } |
| 54 |
| 55 TEST(RecordParsedTest, CopyConstructor) { |
| 56 DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram), |
| 57 sizeof(dns_protocol::Header)); |
| 58 scoped_ptr<const RecordParsed> record; |
| 59 const CnameRecordRdata* rdata; |
| 60 |
| 61 parser.SkipQuestion(); |
| 62 record = RecordParsed::CreateFrom(&parser, base::Time()); |
| 63 EXPECT_TRUE(record.get() != NULL); |
| 64 |
| 65 scoped_ptr<RecordParsed> record2; |
| 66 record2.reset(new RecordParsed(*record)); |
| 67 |
| 68 record.reset(); |
| 69 |
| 70 ASSERT_EQ("codereview.chromium.org", record2->name()); |
| 71 ASSERT_EQ(dns_protocol::kTypeCNAME, record2->type()); |
| 72 ASSERT_EQ(dns_protocol::kClassIN, record2->klass()); |
| 73 |
| 74 rdata = record2->rdata<CnameRecordRdata>(); |
| 75 EXPECT_TRUE(rdata != NULL); |
| 76 ASSERT_EQ(kT1CanonName, rdata->cname()); |
| 77 |
| 78 ASSERT_FALSE(record2->rdata<SrvRecordRdata>()); |
| 79 } |
| 80 |
| 81 TEST(RecordParsedTest, CacheFlushBitCompare) { |
| 82 DnsRecordParser parser1(kT1ResponseDatagram, sizeof(kT1ResponseDatagram), |
| 83 sizeof(dns_protocol::Header)); |
| 84 parser1.SkipQuestion(); |
| 85 scoped_ptr<const RecordParsed> record1 = |
| 86 RecordParsed::CreateFrom(&parser1, base::Time()); |
| 87 |
| 88 DnsRecordParser parser2(kT1ResponseWithCacheFlushBit, |
| 89 sizeof(kT1ResponseWithCacheFlushBit), |
| 90 0); |
| 91 |
| 92 scoped_ptr<const RecordParsed> record2 = |
| 93 RecordParsed::CreateFrom(&parser2, base::Time()); |
| 94 |
| 95 EXPECT_FALSE(record1->IsEqual(record2.get(), false)); |
| 96 EXPECT_TRUE(record1->IsEqual(record2.get(), true)); |
| 97 EXPECT_FALSE(record2->IsEqual(record1.get(), false)); |
| 98 EXPECT_TRUE(record2->IsEqual(record1.get(), true)); |
34 } | 99 } |
35 } | 100 } |
OLD | NEW |