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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "net/base/net_util.h" | 6 #include "net/base/net_util.h" |
7 #include "net/dns/dns_response.h" | 7 #include "net/dns/dns_response.h" |
8 #include "net/dns/record_rdata.h" | 8 #include "net/dns/record_rdata.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 | 12 |
13 base::StringPiece MakeStringPiece(const uint8* data, unsigned size) { | 13 base::StringPiece MakeStringPiece(const uint8* data, unsigned size) { |
14 const char* data_cc = reinterpret_cast<const char*>(data); | 14 const char* data_cc = reinterpret_cast<const char*>(data); |
15 return base::StringPiece(data_cc, size); | 15 return base::StringPiece(data_cc, size); |
16 } | 16 } |
17 | 17 |
18 TEST(RecordRdataTest, ParseSrvRecord) { | 18 TEST(RecordRdataTest, ParseSrvRecord) { |
19 scoped_ptr<SrvRecordRdata> record1_obj; | 19 scoped_ptr<SrvRecordRdata> record1_obj; |
20 scoped_ptr<SrvRecordRdata> record2_obj; | 20 scoped_ptr<SrvRecordRdata> record2_obj; |
21 | 21 |
22 // These are just the rdata portions of the DNS records, rather than complete | 22 // These are just the rdata portions of the DNS records, rather than complete |
23 // records, but it works well enough for this test. | 23 // records, but it works well enough for this test. |
24 | 24 |
25 const uint8 record[] = { | 25 const uint8 |
26 0x00, 0x01, | 26 record |
27 0x00, 0x02, | 27 [] = { |
28 0x00, 0x50, | 28 0x00, 0x01, 0x00, 0x02, 0x00, 0x50, 0x03, 'w', 'w', |
29 0x03, 'w', 'w', 'w', | 29 'w', 0x06, 'g', 'o', 'o', 'g', 'l', 'e', 0x03, |
30 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | 30 'c', 'o', 'm', 0x00, 0x01, 0x01, 0x01, 0x02, 0x01, |
31 0x03, 'c', 'o', 'm', | 31 0x03, 0x04, 'w', 'w', 'w', '2', 0xc0, 0x0a, // Pointer to |
32 0x00, | 32 // "google.com" |
33 0x01, 0x01, | 33 }; |
34 0x01, 0x02, | |
35 0x01, 0x03, | |
36 0x04, 'w', 'w', 'w', '2', | |
37 0xc0, 0x0a, // Pointer to "google.com" | |
38 }; | |
39 | 34 |
40 DnsRecordParser parser(record, sizeof(record), 0); | 35 DnsRecordParser parser(record, sizeof(record), 0); |
41 const unsigned first_record_len = 22; | 36 const unsigned first_record_len = 22; |
42 base::StringPiece record1_strpiece = MakeStringPiece( | 37 base::StringPiece record1_strpiece = |
43 record, first_record_len); | 38 MakeStringPiece(record, first_record_len); |
44 base::StringPiece record2_strpiece = MakeStringPiece( | 39 base::StringPiece record2_strpiece = MakeStringPiece( |
45 record + first_record_len, sizeof(record) - first_record_len); | 40 record + first_record_len, sizeof(record) - first_record_len); |
46 | 41 |
47 record1_obj = SrvRecordRdata::Create(record1_strpiece, parser); | 42 record1_obj = SrvRecordRdata::Create(record1_strpiece, parser); |
48 ASSERT_TRUE(record1_obj != NULL); | 43 ASSERT_TRUE(record1_obj != NULL); |
49 ASSERT_EQ(1, record1_obj->priority()); | 44 ASSERT_EQ(1, record1_obj->priority()); |
50 ASSERT_EQ(2, record1_obj->weight()); | 45 ASSERT_EQ(2, record1_obj->weight()); |
51 ASSERT_EQ(80, record1_obj->port()); | 46 ASSERT_EQ(80, record1_obj->port()); |
52 | 47 |
53 ASSERT_EQ("www.google.com", record1_obj->target()); | 48 ASSERT_EQ("www.google.com", record1_obj->target()); |
(...skipping 10 matching lines...) Expand all Loading... |
64 ASSERT_FALSE(record1_obj->IsEqual(record2_obj.get())); | 59 ASSERT_FALSE(record1_obj->IsEqual(record2_obj.get())); |
65 } | 60 } |
66 | 61 |
67 TEST(RecordRdataTest, ParseARecord) { | 62 TEST(RecordRdataTest, ParseARecord) { |
68 scoped_ptr<ARecordRdata> record_obj; | 63 scoped_ptr<ARecordRdata> record_obj; |
69 | 64 |
70 // These are just the rdata portions of the DNS records, rather than complete | 65 // These are just the rdata portions of the DNS records, rather than complete |
71 // records, but it works well enough for this test. | 66 // records, but it works well enough for this test. |
72 | 67 |
73 const uint8 record[] = { | 68 const uint8 record[] = { |
74 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1 | 69 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1 |
75 }; | 70 }; |
76 | 71 |
77 DnsRecordParser parser(record, sizeof(record), 0); | 72 DnsRecordParser parser(record, sizeof(record), 0); |
78 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | 73 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); |
79 | 74 |
80 record_obj = ARecordRdata::Create(record_strpiece, parser); | 75 record_obj = ARecordRdata::Create(record_strpiece, parser); |
81 ASSERT_TRUE(record_obj != NULL); | 76 ASSERT_TRUE(record_obj != NULL); |
82 | 77 |
83 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj->address())); | 78 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj->address())); |
84 | 79 |
85 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | 80 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); |
86 } | 81 } |
87 | 82 |
88 TEST(RecordRdataTest, ParseAAAARecord) { | 83 TEST(RecordRdataTest, ParseAAAARecord) { |
89 scoped_ptr<AAAARecordRdata> record_obj; | 84 scoped_ptr<AAAARecordRdata> record_obj; |
90 | 85 |
91 // These are just the rdata portions of the DNS records, rather than complete | 86 // These are just the rdata portions of the DNS records, rather than complete |
92 // records, but it works well enough for this test. | 87 // records, but it works well enough for this test. |
93 | 88 |
94 const uint8 record[] = { | 89 const uint8 record[] = { |
95 0x12, 0x34, 0x56, 0x78, | 90 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, |
96 0x00, 0x00, 0x00, 0x00, | 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A |
97 0x00, 0x00, 0x00, 0x00, | |
98 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A | |
99 }; | 92 }; |
100 | 93 |
101 DnsRecordParser parser(record, sizeof(record), 0); | 94 DnsRecordParser parser(record, sizeof(record), 0); |
102 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | 95 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); |
103 | 96 |
104 record_obj = AAAARecordRdata::Create(record_strpiece, parser); | 97 record_obj = AAAARecordRdata::Create(record_strpiece, parser); |
105 ASSERT_TRUE(record_obj != NULL); | 98 ASSERT_TRUE(record_obj != NULL); |
106 | 99 |
107 ASSERT_EQ("1234:5678::9", | 100 ASSERT_EQ("1234:5678::9", IPAddressToString(record_obj->address())); |
108 IPAddressToString(record_obj->address())); | |
109 | 101 |
110 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | 102 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); |
111 } | 103 } |
112 | 104 |
113 TEST(RecordRdataTest, ParseCnameRecord) { | 105 TEST(RecordRdataTest, ParseCnameRecord) { |
114 scoped_ptr<CnameRecordRdata> record_obj; | 106 scoped_ptr<CnameRecordRdata> record_obj; |
115 | 107 |
116 // These are just the rdata portions of the DNS records, rather than complete | 108 // These are just the rdata portions of the DNS records, rather than complete |
117 // records, but it works well enough for this test. | 109 // records, but it works well enough for this test. |
118 | 110 |
119 const uint8 record[] = { | 111 const uint8 record[] = {0x03, 'w', 'w', 'w', 0x06, 'g', 'o', 'o', |
120 0x03, 'w', 'w', 'w', | 112 'g', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00}; |
121 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
122 0x03, 'c', 'o', 'm', | |
123 0x00 | |
124 }; | |
125 | 113 |
126 DnsRecordParser parser(record, sizeof(record), 0); | 114 DnsRecordParser parser(record, sizeof(record), 0); |
127 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | 115 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); |
128 | 116 |
129 record_obj = CnameRecordRdata::Create(record_strpiece, parser); | 117 record_obj = CnameRecordRdata::Create(record_strpiece, parser); |
130 ASSERT_TRUE(record_obj != NULL); | 118 ASSERT_TRUE(record_obj != NULL); |
131 | 119 |
132 ASSERT_EQ("www.google.com", record_obj->cname()); | 120 ASSERT_EQ("www.google.com", record_obj->cname()); |
133 | 121 |
134 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | 122 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); |
135 } | 123 } |
136 | 124 |
137 TEST(RecordRdataTest, ParsePtrRecord) { | 125 TEST(RecordRdataTest, ParsePtrRecord) { |
138 scoped_ptr<PtrRecordRdata> record_obj; | 126 scoped_ptr<PtrRecordRdata> record_obj; |
139 | 127 |
140 // These are just the rdata portions of the DNS records, rather than complete | 128 // These are just the rdata portions of the DNS records, rather than complete |
141 // records, but it works well enough for this test. | 129 // records, but it works well enough for this test. |
142 | 130 |
143 const uint8 record[] = { | 131 const uint8 record[] = {0x03, 'w', 'w', 'w', 0x06, 'g', 'o', 'o', |
144 0x03, 'w', 'w', 'w', | 132 'g', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00}; |
145 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
146 0x03, 'c', 'o', 'm', | |
147 0x00 | |
148 }; | |
149 | 133 |
150 DnsRecordParser parser(record, sizeof(record), 0); | 134 DnsRecordParser parser(record, sizeof(record), 0); |
151 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | 135 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); |
152 | 136 |
153 record_obj = PtrRecordRdata::Create(record_strpiece, parser); | 137 record_obj = PtrRecordRdata::Create(record_strpiece, parser); |
154 ASSERT_TRUE(record_obj != NULL); | 138 ASSERT_TRUE(record_obj != NULL); |
155 | 139 |
156 ASSERT_EQ("www.google.com", record_obj->ptrdomain()); | 140 ASSERT_EQ("www.google.com", record_obj->ptrdomain()); |
157 | 141 |
158 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | 142 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); |
159 } | 143 } |
160 | 144 |
161 TEST(RecordRdataTest, ParseTxtRecord) { | 145 TEST(RecordRdataTest, ParseTxtRecord) { |
162 scoped_ptr<TxtRecordRdata> record_obj; | 146 scoped_ptr<TxtRecordRdata> record_obj; |
163 | 147 |
164 // These are just the rdata portions of the DNS records, rather than complete | 148 // These are just the rdata portions of the DNS records, rather than complete |
165 // records, but it works well enough for this test. | 149 // records, but it works well enough for this test. |
166 | 150 |
167 const uint8 record[] = { | 151 const uint8 record[] = {0x03, 'w', 'w', 'w', 0x06, 'g', 'o', 'o', |
168 0x03, 'w', 'w', 'w', | 152 'g', 'l', 'e', 0x03, 'c', 'o', 'm'}; |
169 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
170 0x03, 'c', 'o', 'm' | |
171 }; | |
172 | 153 |
173 DnsRecordParser parser(record, sizeof(record), 0); | 154 DnsRecordParser parser(record, sizeof(record), 0); |
174 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | 155 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); |
175 | 156 |
176 record_obj = TxtRecordRdata::Create(record_strpiece, parser); | 157 record_obj = TxtRecordRdata::Create(record_strpiece, parser); |
177 ASSERT_TRUE(record_obj != NULL); | 158 ASSERT_TRUE(record_obj != NULL); |
178 | 159 |
179 std::vector<std::string> expected; | 160 std::vector<std::string> expected; |
180 expected.push_back("www"); | 161 expected.push_back("www"); |
181 expected.push_back("google"); | 162 expected.push_back("google"); |
182 expected.push_back("com"); | 163 expected.push_back("com"); |
183 | 164 |
184 ASSERT_EQ(expected, record_obj->texts()); | 165 ASSERT_EQ(expected, record_obj->texts()); |
185 | 166 |
186 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | 167 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); |
187 } | 168 } |
188 | 169 |
189 TEST(RecordRdataTest, ParseNsecRecord) { | 170 TEST(RecordRdataTest, ParseNsecRecord) { |
190 scoped_ptr<NsecRecordRdata> record_obj; | 171 scoped_ptr<NsecRecordRdata> record_obj; |
191 | 172 |
192 // These are just the rdata portions of the DNS records, rather than complete | 173 // These are just the rdata portions of the DNS records, rather than complete |
193 // records, but it works well enough for this test. | 174 // records, but it works well enough for this test. |
194 | 175 |
195 const uint8 record[] = { | 176 const uint8 record[] = {0x03, 'w', 'w', 'w', 0x06, 'g', 'o', |
196 0x03, 'w', 'w', 'w', | 177 'o', 'g', 'l', 'e', 0x03, 'c', 'o', |
197 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | 178 'm', 0x00, 0x00, 0x02, 0x40, 0x01}; |
198 0x03, 'c', 'o', 'm', | |
199 0x00, | |
200 0x00, 0x02, 0x40, 0x01 | |
201 }; | |
202 | 179 |
203 DnsRecordParser parser(record, sizeof(record), 0); | 180 DnsRecordParser parser(record, sizeof(record), 0); |
204 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | 181 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); |
205 | 182 |
206 record_obj = NsecRecordRdata::Create(record_strpiece, parser); | 183 record_obj = NsecRecordRdata::Create(record_strpiece, parser); |
207 ASSERT_TRUE(record_obj != NULL); | 184 ASSERT_TRUE(record_obj != NULL); |
208 | 185 |
209 ASSERT_EQ(16u, record_obj->bitmap_length()); | 186 ASSERT_EQ(16u, record_obj->bitmap_length()); |
210 | 187 |
211 EXPECT_FALSE(record_obj->GetBit(0)); | 188 EXPECT_FALSE(record_obj->GetBit(0)); |
212 EXPECT_TRUE(record_obj->GetBit(1)); | 189 EXPECT_TRUE(record_obj->GetBit(1)); |
213 for (int i = 2; i < 15; i++) { | 190 for (int i = 2; i < 15; i++) { |
214 EXPECT_FALSE(record_obj->GetBit(i)); | 191 EXPECT_FALSE(record_obj->GetBit(i)); |
215 } | 192 } |
216 EXPECT_TRUE(record_obj->GetBit(15)); | 193 EXPECT_TRUE(record_obj->GetBit(15)); |
217 | 194 |
218 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | 195 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); |
219 } | 196 } |
220 | 197 |
221 | |
222 } // namespace net | 198 } // namespace net |
OLD | NEW |