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 "base/memory/scoped_ptr.h"
6 #include "net/base/net_util.h"
7 #include "net/dns/dns_response.h"
8 #include "net/dns/record_rdata.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 base::StringPiece
MakeStringPiece(const uint8
* data
, unsigned size
) {
14 const char* data_cc
= reinterpret_cast<const char*>(data
);
15 return base::StringPiece(data_cc
, size
);
18 TEST(RecordRdataTest
, ParseSrvRecord
) {
19 scoped_ptr
<SrvRecordRdata
> record1_obj
;
20 scoped_ptr
<SrvRecordRdata
> record2_obj
;
22 // These are just the rdata portions of the DNS records, rather than complete
23 // records, but it works well enough for this test.
25 const uint8 record
[] = {
30 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
36 0x04, 'w', 'w', 'w', '2',
37 0xc0, 0x0a, // Pointer to "google.com"
40 DnsRecordParser
parser(record
, sizeof(record
), 0);
41 const unsigned first_record_len
= 22;
42 base::StringPiece record1_strpiece
= MakeStringPiece(
43 record
, first_record_len
);
44 base::StringPiece record2_strpiece
= MakeStringPiece(
45 record
+ first_record_len
, sizeof(record
) - first_record_len
);
47 record1_obj
= SrvRecordRdata::Create(record1_strpiece
, parser
);
48 ASSERT_TRUE(record1_obj
!= NULL
);
49 ASSERT_EQ(1, record1_obj
->priority());
50 ASSERT_EQ(2, record1_obj
->weight());
51 ASSERT_EQ(80, record1_obj
->port());
53 ASSERT_EQ("www.google.com", record1_obj
->target());
55 record2_obj
= SrvRecordRdata::Create(record2_strpiece
, parser
);
56 ASSERT_TRUE(record2_obj
!= NULL
);
57 ASSERT_EQ(257, record2_obj
->priority());
58 ASSERT_EQ(258, record2_obj
->weight());
59 ASSERT_EQ(259, record2_obj
->port());
61 ASSERT_EQ("www2.google.com", record2_obj
->target());
63 ASSERT_TRUE(record1_obj
->IsEqual(record1_obj
.get()));
64 ASSERT_FALSE(record1_obj
->IsEqual(record2_obj
.get()));
67 TEST(RecordRdataTest
, ParseARecord
) {
68 scoped_ptr
<ARecordRdata
> record_obj
;
70 // These are just the rdata portions of the DNS records, rather than complete
71 // records, but it works well enough for this test.
73 const uint8 record
[] = {
74 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1
77 DnsRecordParser
parser(record
, sizeof(record
), 0);
78 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
80 record_obj
= ARecordRdata::Create(record_strpiece
, parser
);
81 ASSERT_TRUE(record_obj
!= NULL
);
83 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj
->address()));
85 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
88 TEST(RecordRdataTest
, ParseAAAARecord
) {
89 scoped_ptr
<AAAARecordRdata
> record_obj
;
91 // These are just the rdata portions of the DNS records, rather than complete
92 // records, but it works well enough for this test.
94 const uint8 record
[] = {
95 0x12, 0x34, 0x56, 0x78,
96 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A
101 DnsRecordParser
parser(record
, sizeof(record
), 0);
102 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
104 record_obj
= AAAARecordRdata::Create(record_strpiece
, parser
);
105 ASSERT_TRUE(record_obj
!= NULL
);
107 ASSERT_EQ("1234:5678::9",
108 IPAddressToString(record_obj
->address()));
110 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
113 TEST(RecordRdataTest
, ParseCnameRecord
) {
114 scoped_ptr
<CnameRecordRdata
> record_obj
;
116 // These are just the rdata portions of the DNS records, rather than complete
117 // records, but it works well enough for this test.
119 const uint8 record
[] = {
121 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
126 DnsRecordParser
parser(record
, sizeof(record
), 0);
127 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
129 record_obj
= CnameRecordRdata::Create(record_strpiece
, parser
);
130 ASSERT_TRUE(record_obj
!= NULL
);
132 ASSERT_EQ("www.google.com", record_obj
->cname());
134 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
137 TEST(RecordRdataTest
, ParsePtrRecord
) {
138 scoped_ptr
<PtrRecordRdata
> record_obj
;
140 // These are just the rdata portions of the DNS records, rather than complete
141 // records, but it works well enough for this test.
143 const uint8 record
[] = {
145 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
150 DnsRecordParser
parser(record
, sizeof(record
), 0);
151 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
153 record_obj
= PtrRecordRdata::Create(record_strpiece
, parser
);
154 ASSERT_TRUE(record_obj
!= NULL
);
156 ASSERT_EQ("www.google.com", record_obj
->ptrdomain());
158 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
161 TEST(RecordRdataTest
, ParseTxtRecord
) {
162 scoped_ptr
<TxtRecordRdata
> record_obj
;
164 // These are just the rdata portions of the DNS records, rather than complete
165 // records, but it works well enough for this test.
167 const uint8 record
[] = {
169 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
173 DnsRecordParser
parser(record
, sizeof(record
), 0);
174 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
176 record_obj
= TxtRecordRdata::Create(record_strpiece
, parser
);
177 ASSERT_TRUE(record_obj
!= NULL
);
179 std::vector
<std::string
> expected
;
180 expected
.push_back("www");
181 expected
.push_back("google");
182 expected
.push_back("com");
184 ASSERT_EQ(expected
, record_obj
->texts());
186 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
189 TEST(RecordRdataTest
, ParseNsecRecord
) {
190 scoped_ptr
<NsecRecordRdata
> record_obj
;
192 // These are just the rdata portions of the DNS records, rather than complete
193 // records, but it works well enough for this test.
195 const uint8 record
[] = {
197 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
200 0x00, 0x02, 0x40, 0x01
203 DnsRecordParser
parser(record
, sizeof(record
), 0);
204 base::StringPiece record_strpiece
= MakeStringPiece(record
, sizeof(record
));
206 record_obj
= NsecRecordRdata::Create(record_strpiece
, parser
);
207 ASSERT_TRUE(record_obj
!= NULL
);
209 ASSERT_EQ(16u, record_obj
->bitmap_length());
211 EXPECT_FALSE(record_obj
->GetBit(0));
212 EXPECT_TRUE(record_obj
->GetBit(1));
213 for (int i
= 2; i
< 15; i
++) {
214 EXPECT_FALSE(record_obj
->GetBit(i
));
216 EXPECT_TRUE(record_obj
->GetBit(15));
218 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));