OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/dns/dns_response.h" | 5 #include "net/dns/dns_response.h" |
6 | 6 |
7 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
8 #include "net/dns/dns_protocol.h" | 8 #include "net/dns/dns_protocol.h" |
9 #include "net/dns/dns_query.h" | 9 #include "net/dns/dns_query.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 TEST(DnsRecordParserTest, Constructor) { | 16 TEST(DnsRecordParserTest, Constructor) { |
17 const char data[] = { 0 }; | 17 const char data[] = { 0 }; |
18 | 18 |
19 EXPECT_FALSE(DnsRecordParser().IsValid()); | 19 EXPECT_FALSE(DnsRecordParser().is_valid()); |
20 EXPECT_TRUE(DnsRecordParser(data, 1, 0).IsValid()); | 20 EXPECT_TRUE(DnsRecordParser(data, 1, 0).is_valid()); |
21 EXPECT_TRUE(DnsRecordParser(data, 1, 1).IsValid()); | 21 EXPECT_TRUE(DnsRecordParser(data, 1, 1).is_valid()); |
22 | 22 |
23 EXPECT_FALSE(DnsRecordParser(data, 1, 0).AtEnd()); | 23 EXPECT_FALSE(DnsRecordParser(data, 1, 0).AtEnd()); |
24 EXPECT_TRUE(DnsRecordParser(data, 1, 1).AtEnd()); | 24 EXPECT_TRUE(DnsRecordParser(data, 1, 1).AtEnd()); |
25 } | 25 } |
26 | 26 |
27 TEST(DnsRecordParserTest, ParseName) { | 27 TEST(DnsRecordParserTest, ParseName) { |
28 const uint8 data[] = { | 28 const uint8 data[] = { |
29 // all labels "foo.example.com" | 29 // all labels "foo.example.com" |
30 0x03, 'f', 'o', 'o', | 30 0x03, 'f', 'o', 'o', |
31 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', | 31 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
32 0x03, 'c', 'o', 'm', | 32 0x03, 'c', 'o', 'm', |
33 // byte 0x10 | 33 // byte 0x10 |
34 0x00, | 34 0x00, |
35 // byte 0x11 | 35 // byte 0x11 |
36 // part label, part pointer, "bar.example.com" | 36 // part label, part pointer, "bar.example.com" |
37 0x03, 'b', 'a', 'r', | 37 0x03, 'b', 'a', 'r', |
38 0xc0, 0x04, | 38 0xc0, 0x04, |
39 // byte 0x17 | 39 // byte 0x17 |
40 // all pointer to "bar.example.com", 2 jumps | 40 // all pointer to "bar.example.com", 2 jumps |
41 0xc0, 0x11, | 41 0xc0, 0x11, |
42 // byte 0x1a | 42 // byte 0x1a |
43 }; | 43 }; |
44 | 44 |
45 std::string out; | 45 std::string out; |
46 DnsRecordParser parser(data, sizeof(data), 0); | 46 DnsRecordParser parser(data, sizeof(data), 0); |
47 ASSERT_TRUE(parser.IsValid()); | 47 ASSERT_TRUE(parser.is_valid()); |
48 | 48 |
49 EXPECT_EQ(0x11, parser.ParseName(data + 0x00, &out)); | 49 EXPECT_EQ(0x11, parser.ParseName(data + 0x00, &out)); |
50 EXPECT_EQ("foo.example.com", out); | 50 EXPECT_EQ("foo.example.com", out); |
51 // Check that the last "." is never stored. | 51 // Check that the last "." is never stored. |
52 out.clear(); | 52 out.clear(); |
53 EXPECT_EQ(0x1, parser.ParseName(data + 0x10, &out)); | 53 EXPECT_EQ(0x1, parser.ParseName(data + 0x10, &out)); |
54 EXPECT_EQ("", out); | 54 EXPECT_EQ("", out); |
55 out.clear(); | 55 out.clear(); |
56 EXPECT_EQ(0x6, parser.ParseName(data + 0x11, &out)); | 56 EXPECT_EQ(0x6, parser.ParseName(data + 0x11, &out)); |
57 EXPECT_EQ("bar.example.com", out); | 57 EXPECT_EQ("bar.example.com", out); |
(...skipping 22 matching lines...) Expand all Loading... |
80 // pointer loop | 80 // pointer loop |
81 0xc0, 0x08, | 81 0xc0, 0x08, |
82 0xc0, 0x06, | 82 0xc0, 0x06, |
83 // incorrect label type (currently supports only direct and pointer) | 83 // incorrect label type (currently supports only direct and pointer) |
84 0x80, 0x00, | 84 0x80, 0x00, |
85 // truncated name (missing root label) | 85 // truncated name (missing root label) |
86 0x02, 'x', 'x', | 86 0x02, 'x', 'x', |
87 }; | 87 }; |
88 | 88 |
89 DnsRecordParser parser(data, sizeof(data), 0); | 89 DnsRecordParser parser(data, sizeof(data), 0); |
90 ASSERT_TRUE(parser.IsValid()); | 90 ASSERT_TRUE(parser.is_valid()); |
91 | 91 |
92 std::string out; | 92 std::string out; |
93 EXPECT_EQ(0, parser.ParseName(data + 0x00, &out)); | 93 EXPECT_EQ(0, parser.ParseName(data + 0x00, &out)); |
94 EXPECT_EQ(0, parser.ParseName(data + 0x04, &out)); | 94 EXPECT_EQ(0, parser.ParseName(data + 0x04, &out)); |
95 EXPECT_EQ(0, parser.ParseName(data + 0x08, &out)); | 95 EXPECT_EQ(0, parser.ParseName(data + 0x08, &out)); |
96 EXPECT_EQ(0, parser.ParseName(data + 0x0a, &out)); | 96 EXPECT_EQ(0, parser.ParseName(data + 0x0a, &out)); |
97 EXPECT_EQ(0, parser.ParseName(data + 0x0c, &out)); | 97 EXPECT_EQ(0, parser.ParseName(data + 0x0c, &out)); |
98 EXPECT_EQ(0, parser.ParseName(data + 0x0e, &out)); | 98 EXPECT_EQ(0, parser.ParseName(data + 0x0e, &out)); |
99 } | 99 } |
100 | 100 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 0x00, 0x04, // RDLENGTH is 4 bytes. | 198 0x00, 0x04, // RDLENGTH is 4 bytes. |
199 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 | 199 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 |
200 0x5f, 0x79, | 200 0x5f, 0x79, |
201 }; | 201 }; |
202 | 202 |
203 DnsResponse resp; | 203 DnsResponse resp; |
204 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); | 204 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
205 | 205 |
206 // Reject too short. | 206 // Reject too short. |
207 EXPECT_FALSE(resp.InitParse(query->io_buffer()->size() - 1, *query)); | 207 EXPECT_FALSE(resp.InitParse(query->io_buffer()->size() - 1, *query)); |
| 208 EXPECT_FALSE(resp.is_valid()); |
208 | 209 |
209 // Reject wrong id. | 210 // Reject wrong id. |
210 scoped_ptr<DnsQuery> other_query(query->CloneWithNewId(0xbeef)); | 211 scoped_ptr<DnsQuery> other_query(query->CloneWithNewId(0xbeef)); |
211 EXPECT_FALSE(resp.InitParse(sizeof(response_data), *other_query)); | 212 EXPECT_FALSE(resp.InitParse(sizeof(response_data), *other_query)); |
| 213 EXPECT_FALSE(resp.is_valid()); |
212 | 214 |
213 // Reject wrong question. | 215 // Reject wrong question. |
214 scoped_ptr<DnsQuery> wrong_query( | 216 scoped_ptr<DnsQuery> wrong_query( |
215 new DnsQuery(0xcafe, qname, dns_protocol::kTypeCNAME)); | 217 new DnsQuery(0xcafe, qname, dns_protocol::kTypeCNAME)); |
216 EXPECT_FALSE(resp.InitParse(sizeof(response_data), *wrong_query)); | 218 EXPECT_FALSE(resp.InitParse(sizeof(response_data), *wrong_query)); |
| 219 EXPECT_FALSE(resp.is_valid()); |
217 | 220 |
218 // Accept matching question. | 221 // Accept matching question. |
219 EXPECT_TRUE(resp.InitParse(sizeof(response_data), *query)); | 222 EXPECT_TRUE(resp.InitParse(sizeof(response_data), *query)); |
| 223 EXPECT_TRUE(resp.is_valid()); |
220 | 224 |
221 // Check header access. | 225 // Check header access. |
222 EXPECT_EQ(0x81, resp.flags0()); | 226 EXPECT_EQ(0x8180, resp.flags()); |
223 EXPECT_EQ(0x80, resp.flags1()); | |
224 EXPECT_EQ(0x0, resp.rcode()); | 227 EXPECT_EQ(0x0, resp.rcode()); |
225 EXPECT_EQ(2, resp.answer_count()); | 228 EXPECT_EQ(2, resp.answer_count()); |
226 | 229 |
| 230 // Check question access. |
| 231 EXPECT_EQ(query->qname(), resp.qname()); |
| 232 EXPECT_EQ(query->qtype(), resp.qtype()); |
| 233 EXPECT_EQ("codereview.chromium.org", resp.GetDottedName()); |
| 234 |
227 DnsResourceRecord record; | 235 DnsResourceRecord record; |
228 DnsRecordParser parser = resp.Parser(); | 236 DnsRecordParser parser = resp.Parser(); |
229 EXPECT_TRUE(parser.ParseRecord(&record)); | 237 EXPECT_TRUE(parser.ParseRecord(&record)); |
230 EXPECT_TRUE(parser.ParseRecord(&record)); | 238 EXPECT_TRUE(parser.ParseRecord(&record)); |
231 EXPECT_FALSE(parser.ParseRecord(&record)); | 239 EXPECT_FALSE(parser.ParseRecord(&record)); |
232 } | 240 } |
233 | 241 |
234 } // namespace | 242 } // namespace |
235 | 243 |
236 } // namespace net | 244 } // namespace net |
OLD | NEW |