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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// 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 "base/memory/scoped_ptr.h"
#include "net/dns/record_rdata.h"
#include "net/dns/dns_response.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
TEST(RecordRdataTest, ParseSrvRecord) {
scoped_ptr<SrvRecordRdata> record1_obj;
scoped_ptr<SrvRecordRdata> record2_obj;
// These are just the rdata portions of the DNS records, rather than complete
// records, but it works well enough for this test.
const char record[] = {
0x00, 0x01,
0x00, 0x02,
0x00, 0x50,
0x03, 'w', 'w', 'w',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
0x01, 0x01,
0x01, 0x02,
0x01, 0x03,
0x04, 'w', 'w', 'w', '2',
0xc0, 0x0a, // Pointer to "google.com"
};
DnsRecordParser parser(record, sizeof(record), 0);
const unsigned first_record_len = 22;
base::StringPiece record1_strpiece(record, first_record_len);
base::StringPiece record2_strpiece(
record + first_record_len, sizeof(record) - first_record_len);
record1_obj = SrvRecordRdata::Create(record1_strpiece, parser);
ASSERT_TRUE(record1_obj != NULL);
ASSERT_EQ(1, record1_obj->priority());
ASSERT_EQ(2, record1_obj->weight());
ASSERT_EQ(80, record1_obj->port());
ASSERT_EQ("www.google.com", record1_obj->target());
record2_obj = SrvRecordRdata::Create(record2_strpiece, parser);
ASSERT_TRUE(record2_obj != NULL);
ASSERT_EQ(257, record2_obj->priority());
ASSERT_EQ(258, record2_obj->weight());
ASSERT_EQ(259, record2_obj->port());
ASSERT_EQ("www2.google.com", record2_obj->target());
}
TEST(RecordRdataTest, ParseARecord) {
scoped_ptr<ARecordRdata> record_obj;
// These are just the rdata portions of the DNS records, rather than complete
// records, but it works well enough for this test.
const char record[] = {
0x7F, 0x00, 0x00, 0x01 // 127.0.0.1
};
DnsRecordParser parser(record, sizeof(record), 0);
base::StringPiece record_strpiece(record, sizeof(record));
record_obj = ARecordRdata::Create(record_strpiece, parser);
ASSERT_TRUE(record_obj != NULL);
ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj->address()));
}
TEST(RecordRdataTest, ParseCnameRecord) {
scoped_ptr<CnameRecordRdata> record_obj;
// These are just the rdata portions of the DNS records, rather than complete
// records, but it works well enough for this test.
const char record[] = {
0x03, 'w', 'w', 'w',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00
};
DnsRecordParser parser(record, sizeof(record), 0);
base::StringPiece record_strpiece(record, sizeof(record));
record_obj = CnameRecordRdata::Create(record_strpiece, parser);
ASSERT_TRUE(record_obj != NULL);
ASSERT_EQ("www.google.com", record_obj->cname());
}
TEST(RecordRdataTest, ParsePtrRecord) {
scoped_ptr<PtrRecordRdata> record_obj;
// These are just the rdata portions of the DNS records, rather than complete
// records, but it works well enough for this test.
const char record[] = {
0x03, 'w', 'w', 'w',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00
};
DnsRecordParser parser(record, sizeof(record), 0);
base::StringPiece record_strpiece(record, sizeof(record));
record_obj = PtrRecordRdata::Create(record_strpiece, parser);
ASSERT_TRUE(record_obj != NULL);
ASSERT_EQ("www.google.com", record_obj->ptrdomain());
}
TEST(RecordRdataTest, ParseTxtRecord) {
scoped_ptr<TxtRecordRdata> record_obj;
// These are just the rdata portions of the DNS records, rather than complete
// records, but it works well enough for this test.
const char record[] = {
0x03, 'w', 'w', 'w',
0x06, 'g', 'o', 'o', 'g', 'l', 'e',
0x03, 'c', 'o', 'm'
};
DnsRecordParser parser(record, sizeof(record), 0);
base::StringPiece record_strpiece(record, sizeof(record));
record_obj = TxtRecordRdata::Create(record_strpiece, parser);
ASSERT_TRUE(record_obj != NULL);
std::vector<std::string> expected;
expected.push_back("www");
expected.push_back("google");
expected.push_back("com");
ASSERT_EQ(expected, record_obj->texts());
}
} // namespace net
|