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 TEST(RecordRdataTest
, ParseSrvRecord
) {
14 scoped_ptr
<SrvRecordRdata
> record1_obj
;
15 scoped_ptr
<SrvRecordRdata
> record2_obj
;
17 // These are just the rdata portions of the DNS records, rather than complete
18 // records, but it works well enough for this test.
20 const char record
[] = {
25 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
31 0x04, 'w', 'w', 'w', '2',
32 0xc0, 0x0a, // Pointer to "google.com"
35 DnsRecordParser
parser(record
, sizeof(record
), 0);
36 const unsigned first_record_len
= 22;
37 base::StringPiece
record1_strpiece(record
, first_record_len
);
38 base::StringPiece
record2_strpiece(
39 record
+ first_record_len
, sizeof(record
) - first_record_len
);
41 record1_obj
= SrvRecordRdata::Create(record1_strpiece
, parser
);
42 ASSERT_TRUE(record1_obj
!= NULL
);
43 ASSERT_EQ(1, record1_obj
->priority());
44 ASSERT_EQ(2, record1_obj
->weight());
45 ASSERT_EQ(80, record1_obj
->port());
47 ASSERT_EQ("www.google.com", record1_obj
->target());
49 record2_obj
= SrvRecordRdata::Create(record2_strpiece
, parser
);
50 ASSERT_TRUE(record2_obj
!= NULL
);
51 ASSERT_EQ(257, record2_obj
->priority());
52 ASSERT_EQ(258, record2_obj
->weight());
53 ASSERT_EQ(259, record2_obj
->port());
55 ASSERT_EQ("www2.google.com", record2_obj
->target());
57 ASSERT_TRUE(record1_obj
->IsEqual(record1_obj
.get()));
58 ASSERT_FALSE(record1_obj
->IsEqual(record2_obj
.get()));
61 TEST(RecordRdataTest
, ParseARecord
) {
62 scoped_ptr
<ARecordRdata
> record_obj
;
64 // These are just the rdata portions of the DNS records, rather than complete
65 // records, but it works well enough for this test.
67 const char record
[] = {
68 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1
71 DnsRecordParser
parser(record
, sizeof(record
), 0);
72 base::StringPiece
record_strpiece(record
, sizeof(record
));
74 record_obj
= ARecordRdata::Create(record_strpiece
, parser
);
75 ASSERT_TRUE(record_obj
!= NULL
);
77 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj
->address()));
79 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
82 TEST(RecordRdataTest
, ParseAAAARecord
) {
83 scoped_ptr
<AAAARecordRdata
> record_obj
;
85 // These are just the rdata portions of the DNS records, rather than complete
86 // records, but it works well enough for this test.
88 const char record
[] = {
89 0x12, 0x34, 0x56, 0x78,
90 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A
95 DnsRecordParser
parser(record
, sizeof(record
), 0);
96 base::StringPiece
record_strpiece(record
, sizeof(record
));
98 record_obj
= AAAARecordRdata::Create(record_strpiece
, parser
);
99 ASSERT_TRUE(record_obj
!= NULL
);
101 ASSERT_EQ("1234:5678::9",
102 IPAddressToString(record_obj
->address()));
104 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
107 TEST(RecordRdataTest
, ParseCnameRecord
) {
108 scoped_ptr
<CnameRecordRdata
> record_obj
;
110 // These are just the rdata portions of the DNS records, rather than complete
111 // records, but it works well enough for this test.
113 const char record
[] = {
115 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
120 DnsRecordParser
parser(record
, sizeof(record
), 0);
121 base::StringPiece
record_strpiece(record
, sizeof(record
));
123 record_obj
= CnameRecordRdata::Create(record_strpiece
, parser
);
124 ASSERT_TRUE(record_obj
!= NULL
);
126 ASSERT_EQ("www.google.com", record_obj
->cname());
128 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
131 TEST(RecordRdataTest
, ParsePtrRecord
) {
132 scoped_ptr
<PtrRecordRdata
> record_obj
;
134 // These are just the rdata portions of the DNS records, rather than complete
135 // records, but it works well enough for this test.
137 const char record
[] = {
139 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
144 DnsRecordParser
parser(record
, sizeof(record
), 0);
145 base::StringPiece
record_strpiece(record
, sizeof(record
));
147 record_obj
= PtrRecordRdata::Create(record_strpiece
, parser
);
148 ASSERT_TRUE(record_obj
!= NULL
);
150 ASSERT_EQ("www.google.com", record_obj
->ptrdomain());
152 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));
155 TEST(RecordRdataTest
, ParseTxtRecord
) {
156 scoped_ptr
<TxtRecordRdata
> record_obj
;
158 // These are just the rdata portions of the DNS records, rather than complete
159 // records, but it works well enough for this test.
161 const char record
[] = {
163 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
167 DnsRecordParser
parser(record
, sizeof(record
), 0);
168 base::StringPiece
record_strpiece(record
, sizeof(record
));
170 record_obj
= TxtRecordRdata::Create(record_strpiece
, parser
);
171 ASSERT_TRUE(record_obj
!= NULL
);
173 std::vector
<std::string
> expected
;
174 expected
.push_back("www");
175 expected
.push_back("google");
176 expected
.push_back("com");
178 ASSERT_EQ(expected
, record_obj
->texts());
180 ASSERT_TRUE(record_obj
->IsEqual(record_obj
.get()));