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.
5 #include "net/dns/record_rdata.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "net/base/ip_address_number.h"
9 #include "net/dns/dns_response.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 base::StringPiece
MakeStringPiece(const uint8
* data
, unsigned size
) {
15 const char* data_cc
= reinterpret_cast<const char*>(data
);
16 return base::StringPiece(data_cc
, size
);
19 TEST(RecordRdataTest
, ParseSrvRecord
) {
20 scoped_ptr
<SrvRecordRdata
> record1_obj
;
21 scoped_ptr
<SrvRecordRdata
> record2_obj
;
23 // These are just the rdata portions of the DNS records, rather than complete
24 // records, but it works well enough for this test.
26 const uint8 record
[] = {
31 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
37 0x04, 'w', 'w', 'w', '2',
38 0xc0, 0x0a, // Pointer to "google.com"
41 DnsRecordParser
parser(record
, sizeof(record
), 0);
42 const unsigned first_record_len
= 22;
43 base::StringPiece record1_strpiece
= MakeStringPiece(
44 record
, first_record_len
);
45 base::StringPiece record2_strpiece
= MakeStringPiece(
46 record
+ first_record_len
, sizeof(record
) - first_record_len
);
48 record1_obj
= SrvRecordRdata::Create(record1_strpiece
, parser
);
49 ASSERT_TRUE(record1_obj
!= NULL
);
50 ASSERT_EQ(1, record1_obj
->priority());
51 ASSERT_EQ(2, record1_obj
->weight());
52 ASSERT_EQ(80, record1_obj
->port());
54 ASSERT_EQ("www.google.com", record1_obj
->target());
56 record2_obj
= SrvRecordRdata::Create(record2_strpiece
, parser
);
57 ASSERT_TRUE(record2_obj
!= NULL
);
58 ASSERT_EQ(257, record2_obj
->priority());
59 ASSERT_EQ(258, record2_obj
->weight());
60 ASSERT_EQ(259, record2_obj
->port());
62 ASSERT_EQ("www2.google.com", record2_obj
->target());
64 ASSERT_TRUE(record1_obj
->IsEqual(record1_obj
.get()));
65 ASSERT_FALSE(record1_obj
->IsEqual(record2_obj
.get()));
68 TEST(RecordRdataTest
, ParseARecord
) {
69 scoped_ptr
<ARecordRdata
> record_obj
;
71 // These are just the rdata portions of the DNS records, rather than complete
72 // records, but it works well enough for this test.
74 const uint8 record
[] = {
75 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1
78 DnsRecordParser
parser(record
, sizeof(record
), 0);
79 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
81 record_obj
= ARecordRdata::Create(record_strpiece
, parser
);
82 ASSERT_TRUE(record_obj
!= NULL
);
84 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj
->address()));
86 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
89 TEST(RecordRdataTest
, ParseAAAARecord
) {
90 scoped_ptr
<AAAARecordRdata
> record_obj
;
92 // These are just the rdata portions of the DNS records, rather than complete
93 // records, but it works well enough for this test.
95 const uint8 record
[] = {
96 0x12, 0x34, 0x56, 0x78,
97 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A
102 DnsRecordParser
parser(record
, sizeof(record
), 0);
103 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
105 record_obj
= AAAARecordRdata::Create(record_strpiece
, parser
);
106 ASSERT_TRUE(record_obj
!= NULL
);
108 ASSERT_EQ("1234:5678::9",
109 IPAddressToString(record_obj
->address()));
111 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
114 TEST(RecordRdataTest
, ParseCnameRecord
) {
115 scoped_ptr
<CnameRecordRdata
> record_obj
;
117 // These are just the rdata portions of the DNS records, rather than complete
118 // records, but it works well enough for this test.
120 const uint8 record
[] = {
122 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
127 DnsRecordParser
parser(record
, sizeof(record
), 0);
128 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
130 record_obj
= CnameRecordRdata::Create(record_strpiece
, parser
);
131 ASSERT_TRUE(record_obj
!= NULL
);
133 ASSERT_EQ("www.google.com", record_obj
->cname());
135 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
138 TEST(RecordRdataTest
, ParsePtrRecord
) {
139 scoped_ptr
<PtrRecordRdata
> record_obj
;
141 // These are just the rdata portions of the DNS records, rather than complete
142 // records, but it works well enough for this test.
144 const uint8 record
[] = {
146 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
151 DnsRecordParser
parser(record
, sizeof(record
), 0);
152 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
154 record_obj
= PtrRecordRdata::Create(record_strpiece
, parser
);
155 ASSERT_TRUE(record_obj
!= NULL
);
157 ASSERT_EQ("www.google.com", record_obj
->ptrdomain());
159 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
162 TEST(RecordRdataTest
, ParseTxtRecord
) {
163 scoped_ptr
<TxtRecordRdata
> record_obj
;
165 // These are just the rdata portions of the DNS records, rather than complete
166 // records, but it works well enough for this test.
168 const uint8 record
[] = {
170 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
174 DnsRecordParser
parser(record
, sizeof(record
), 0);
175 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
177 record_obj
= TxtRecordRdata::Create(record_strpiece
, parser
);
178 ASSERT_TRUE(record_obj
!= NULL
);
180 std::vector
<std::string
> expected
;
181 expected
.push_back("www");
182 expected
.push_back("google");
183 expected
.push_back("com");
185 ASSERT_EQ(expected
, record_obj
->texts());
187 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
190 TEST(RecordRdataTest
, ParseNsecRecord
) {
191 scoped_ptr
<NsecRecordRdata
> record_obj
;
193 // These are just the rdata portions of the DNS records, rather than complete
194 // records, but it works well enough for this test.
196 const uint8 record
[] = {
198 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
201 0x00, 0x02, 0x40, 0x01
204 DnsRecordParser
parser(record
, sizeof(record
), 0);
205 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
207 record_obj
= NsecRecordRdata::Create(record_strpiece
, parser
);
208 ASSERT_TRUE(record_obj
!= NULL
);
210 ASSERT_EQ(16u, record_obj
->bitmap_length());
212 EXPECT_FALSE(record_obj
->GetBit(0));
213 EXPECT_TRUE(record_obj
->GetBit(1));
214 for (int i
= 2; i
< 15; i
++) {
215 EXPECT_FALSE(record_obj
->GetBit(i
));
217 EXPECT_TRUE(record_obj
->GetBit(15));
219 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));