| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 EXPECT_FALSE(DnsRecordParser().IsValid()); | 23 EXPECT_FALSE(DnsRecordParser().IsValid()); |
| 24 EXPECT_TRUE(DnsRecordParser(data, 1, 0).IsValid()); | 24 EXPECT_TRUE(DnsRecordParser(data, 1, 0).IsValid()); |
| 25 EXPECT_TRUE(DnsRecordParser(data, 1, 1).IsValid()); | 25 EXPECT_TRUE(DnsRecordParser(data, 1, 1).IsValid()); |
| 26 | 26 |
| 27 EXPECT_FALSE(DnsRecordParser(data, 1, 0).AtEnd()); | 27 EXPECT_FALSE(DnsRecordParser(data, 1, 0).AtEnd()); |
| 28 EXPECT_TRUE(DnsRecordParser(data, 1, 1).AtEnd()); | 28 EXPECT_TRUE(DnsRecordParser(data, 1, 1).AtEnd()); |
| 29 } | 29 } |
| 30 | 30 |
| 31 TEST(DnsRecordParserTest, ReadName) { | 31 TEST(DnsRecordParserTest, ReadName) { |
| 32 const uint8 data[] = { | 32 const uint8_t data[] = { |
| 33 // all labels "foo.example.com" | 33 // all labels "foo.example.com" |
| 34 0x03, 'f', 'o', 'o', | 34 0x03, 'f', 'o', 'o', 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', |
| 35 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', | 35 'o', 'm', |
| 36 0x03, 'c', 'o', 'm', | |
| 37 // byte 0x10 | 36 // byte 0x10 |
| 38 0x00, | 37 0x00, |
| 39 // byte 0x11 | 38 // byte 0x11 |
| 40 // part label, part pointer, "bar.example.com" | 39 // part label, part pointer, "bar.example.com" |
| 41 0x03, 'b', 'a', 'r', | 40 0x03, 'b', 'a', 'r', 0xc0, 0x04, |
| 42 0xc0, 0x04, | |
| 43 // byte 0x17 | 41 // byte 0x17 |
| 44 // all pointer to "bar.example.com", 2 jumps | 42 // all pointer to "bar.example.com", 2 jumps |
| 45 0xc0, 0x11, | 43 0xc0, 0x11, |
| 46 // byte 0x1a | 44 // byte 0x1a |
| 47 }; | 45 }; |
| 48 | 46 |
| 49 std::string out; | 47 std::string out; |
| 50 DnsRecordParser parser(data, sizeof(data), 0); | 48 DnsRecordParser parser(data, sizeof(data), 0); |
| 51 ASSERT_TRUE(parser.IsValid()); | 49 ASSERT_TRUE(parser.IsValid()); |
| 52 | 50 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 68 EXPECT_EQ(0x1u, parser.ReadName(data + 0x10, NULL)); | 66 EXPECT_EQ(0x1u, parser.ReadName(data + 0x10, NULL)); |
| 69 EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, NULL)); | 67 EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, NULL)); |
| 70 EXPECT_EQ(0x2u, parser.ReadName(data + 0x17, NULL)); | 68 EXPECT_EQ(0x2u, parser.ReadName(data + 0x17, NULL)); |
| 71 | 69 |
| 72 // Check that it works even if initial position is different. | 70 // Check that it works even if initial position is different. |
| 73 parser = DnsRecordParser(data, sizeof(data), 0x12); | 71 parser = DnsRecordParser(data, sizeof(data), 0x12); |
| 74 EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, NULL)); | 72 EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, NULL)); |
| 75 } | 73 } |
| 76 | 74 |
| 77 TEST(DnsRecordParserTest, ReadNameFail) { | 75 TEST(DnsRecordParserTest, ReadNameFail) { |
| 78 const uint8 data[] = { | 76 const uint8_t data[] = { |
| 79 // label length beyond packet | 77 // label length beyond packet |
| 80 0x30, 'x', 'x', | 78 0x30, 'x', 'x', 0x00, |
| 81 0x00, | |
| 82 // pointer offset beyond packet | 79 // pointer offset beyond packet |
| 83 0xc0, 0x20, | 80 0xc0, 0x20, |
| 84 // pointer loop | 81 // pointer loop |
| 85 0xc0, 0x08, | 82 0xc0, 0x08, 0xc0, 0x06, |
| 86 0xc0, 0x06, | |
| 87 // incorrect label type (currently supports only direct and pointer) | 83 // incorrect label type (currently supports only direct and pointer) |
| 88 0x80, 0x00, | 84 0x80, 0x00, |
| 89 // truncated name (missing root label) | 85 // truncated name (missing root label) |
| 90 0x02, 'x', 'x', | 86 0x02, 'x', 'x', |
| 91 }; | 87 }; |
| 92 | 88 |
| 93 DnsRecordParser parser(data, sizeof(data), 0); | 89 DnsRecordParser parser(data, sizeof(data), 0); |
| 94 ASSERT_TRUE(parser.IsValid()); | 90 ASSERT_TRUE(parser.IsValid()); |
| 95 | 91 |
| 96 std::string out; | 92 std::string out; |
| 97 EXPECT_EQ(0u, parser.ReadName(data + 0x00, &out)); | 93 EXPECT_EQ(0u, parser.ReadName(data + 0x00, &out)); |
| 98 EXPECT_EQ(0u, parser.ReadName(data + 0x04, &out)); | 94 EXPECT_EQ(0u, parser.ReadName(data + 0x04, &out)); |
| 99 EXPECT_EQ(0u, parser.ReadName(data + 0x08, &out)); | 95 EXPECT_EQ(0u, parser.ReadName(data + 0x08, &out)); |
| 100 EXPECT_EQ(0u, parser.ReadName(data + 0x0a, &out)); | 96 EXPECT_EQ(0u, parser.ReadName(data + 0x0a, &out)); |
| 101 EXPECT_EQ(0u, parser.ReadName(data + 0x0c, &out)); | 97 EXPECT_EQ(0u, parser.ReadName(data + 0x0c, &out)); |
| 102 EXPECT_EQ(0u, parser.ReadName(data + 0x0e, &out)); | 98 EXPECT_EQ(0u, parser.ReadName(data + 0x0e, &out)); |
| 103 } | 99 } |
| 104 | 100 |
| 105 TEST(DnsRecordParserTest, ReadRecord) { | 101 TEST(DnsRecordParserTest, ReadRecord) { |
| 106 const uint8 data[] = { | 102 const uint8_t data[] = { |
| 107 // Type CNAME record. | 103 // Type CNAME record. |
| 108 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', | 104 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00, 0x00, |
| 109 0x03, 'c', 'o', 'm', | 105 0x05, // TYPE is CNAME. |
| 110 0x00, | |
| 111 0x00, 0x05, // TYPE is CNAME. | |
| 112 0x00, 0x01, // CLASS is IN. | 106 0x00, 0x01, // CLASS is IN. |
| 113 0x00, 0x01, 0x24, 0x74, // TTL is 0x00012474. | 107 0x00, 0x01, 0x24, 0x74, // TTL is 0x00012474. |
| 114 0x00, 0x06, // RDLENGTH is 6 bytes. | 108 0x00, 0x06, // RDLENGTH is 6 bytes. |
| 115 0x03, 'f', 'o', 'o', // compressed name in record | 109 0x03, 'f', 'o', 'o', // compressed name in record |
| 116 0xc0, 0x00, | 110 0xc0, 0x00, |
| 117 // Type A record. | 111 // Type A record. |
| 118 0x03, 'b', 'a', 'r', // compressed owner name | 112 0x03, 'b', 'a', 'r', // compressed owner name |
| 119 0xc0, 0x00, | 113 0xc0, 0x00, 0x00, 0x01, // TYPE is A. |
| 120 0x00, 0x01, // TYPE is A. | |
| 121 0x00, 0x01, // CLASS is IN. | 114 0x00, 0x01, // CLASS is IN. |
| 122 0x00, 0x20, 0x13, 0x55, // TTL is 0x00201355. | 115 0x00, 0x20, 0x13, 0x55, // TTL is 0x00201355. |
| 123 0x00, 0x04, // RDLENGTH is 4 bytes. | 116 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 124 0x7f, 0x02, 0x04, 0x01, // IP is 127.2.4.1 | 117 0x7f, 0x02, 0x04, 0x01, // IP is 127.2.4.1 |
| 125 }; | 118 }; |
| 126 | 119 |
| 127 std::string out; | 120 std::string out; |
| 128 DnsRecordParser parser(data, sizeof(data), 0); | 121 DnsRecordParser parser(data, sizeof(data), 0); |
| 129 | 122 |
| 130 DnsResourceRecord record; | 123 DnsResourceRecord record; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 154 EXPECT_FALSE(parser.ReadRecord(&record)); | 147 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 155 } | 148 } |
| 156 | 149 |
| 157 TEST(DnsResponseTest, InitParse) { | 150 TEST(DnsResponseTest, InitParse) { |
| 158 // This includes \0 at the end. | 151 // This includes \0 at the end. |
| 159 const char qname_data[] = "\x0A""codereview""\x08""chromium""\x03""org"; | 152 const char qname_data[] = "\x0A""codereview""\x08""chromium""\x03""org"; |
| 160 const base::StringPiece qname(qname_data, sizeof(qname_data)); | 153 const base::StringPiece qname(qname_data, sizeof(qname_data)); |
| 161 // Compilers want to copy when binding temporary to const &, so must use heap. | 154 // Compilers want to copy when binding temporary to const &, so must use heap. |
| 162 scoped_ptr<DnsQuery> query(new DnsQuery(0xcafe, qname, dns_protocol::kTypeA)); | 155 scoped_ptr<DnsQuery> query(new DnsQuery(0xcafe, qname, dns_protocol::kTypeA)); |
| 163 | 156 |
| 164 const uint8 response_data[] = { | 157 const uint8_t response_data[] = { |
| 165 // Header | 158 // Header |
| 166 0xca, 0xfe, // ID | 159 0xca, 0xfe, // ID |
| 167 0x81, 0x80, // Standard query response, RA, no error | 160 0x81, 0x80, // Standard query response, RA, no error |
| 168 0x00, 0x01, // 1 question | 161 0x00, 0x01, // 1 question |
| 169 0x00, 0x02, // 2 RRs (answers) | 162 0x00, 0x02, // 2 RRs (answers) |
| 170 0x00, 0x00, // 0 authority RRs | 163 0x00, 0x00, // 0 authority RRs |
| 171 0x00, 0x00, // 0 additional RRs | 164 0x00, 0x00, // 0 additional RRs |
| 172 | 165 |
| 173 // Question | 166 // Question |
| 174 // This part is echoed back from the respective query. | 167 // This part is echoed back from the respective query. |
| 175 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', | 168 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h', |
| 176 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', | 169 'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00, |
| 177 0x03, 'o', 'r', 'g', | 170 0x01, // TYPE is A. |
| 178 0x00, | 171 0x00, 0x01, // CLASS is IN. |
| 179 0x00, 0x01, // TYPE is A. | |
| 180 0x00, 0x01, // CLASS is IN. | |
| 181 | 172 |
| 182 // Answer 1 | 173 // Answer 1 |
| 183 0xc0, 0x0c, // NAME is a pointer to name in Question section. | 174 0xc0, 0x0c, // NAME is a pointer to name in Question section. |
| 184 0x00, 0x05, // TYPE is CNAME. | 175 0x00, 0x05, // TYPE is CNAME. |
| 185 0x00, 0x01, // CLASS is IN. | 176 0x00, 0x01, // CLASS is IN. |
| 186 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 177 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 187 0x24, 0x74, | 178 0x24, 0x74, 0x00, 0x12, // RDLENGTH is 18 bytes. |
| 188 0x00, 0x12, // RDLENGTH is 18 bytes. | 179 // ghs.l.google.com in DNS format. |
| 189 // ghs.l.google.com in DNS format. | 180 0x03, 'g', 'h', 's', 0x01, 'l', 0x06, 'g', 'o', 'o', 'g', 'l', 'e', 0x03, |
| 190 0x03, 'g', 'h', 's', | 181 'c', 'o', 'm', 0x00, |
| 191 0x01, 'l', | |
| 192 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
| 193 0x03, 'c', 'o', 'm', | |
| 194 0x00, | |
| 195 | 182 |
| 196 // Answer 2 | 183 // Answer 2 |
| 197 0xc0, 0x35, // NAME is a pointer to name in Answer 1. | 184 0xc0, 0x35, // NAME is a pointer to name in Answer 1. |
| 198 0x00, 0x01, // TYPE is A. | 185 0x00, 0x01, // TYPE is A. |
| 199 0x00, 0x01, // CLASS is IN. | 186 0x00, 0x01, // CLASS is IN. |
| 200 0x00, 0x00, // TTL (4 bytes) is 53 seconds. | 187 0x00, 0x00, // TTL (4 bytes) is 53 seconds. |
| 201 0x00, 0x35, | 188 0x00, 0x35, 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 202 0x00, 0x04, // RDLENGTH is 4 bytes. | 189 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 |
| 203 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 | 190 0x5f, 0x79, |
| 204 0x5f, 0x79, | |
| 205 }; | 191 }; |
| 206 | 192 |
| 207 DnsResponse resp; | 193 DnsResponse resp; |
| 208 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); | 194 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
| 209 | 195 |
| 210 // Reject too short. | 196 // Reject too short. |
| 211 EXPECT_FALSE(resp.InitParse(query->io_buffer()->size() - 1, *query)); | 197 EXPECT_FALSE(resp.InitParse(query->io_buffer()->size() - 1, *query)); |
| 212 EXPECT_FALSE(resp.IsValid()); | 198 EXPECT_FALSE(resp.IsValid()); |
| 213 | 199 |
| 214 // Reject wrong id. | 200 // Reject wrong id. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 DnsRecordParser parser = resp.Parser(); | 253 DnsRecordParser parser = resp.Parser(); |
| 268 for (unsigned i = 0; i < kT0RecordCount; i ++) { | 254 for (unsigned i = 0; i < kT0RecordCount; i ++) { |
| 269 EXPECT_FALSE(parser.AtEnd()); | 255 EXPECT_FALSE(parser.AtEnd()); |
| 270 EXPECT_TRUE(parser.ReadRecord(&record)); | 256 EXPECT_TRUE(parser.ReadRecord(&record)); |
| 271 } | 257 } |
| 272 EXPECT_TRUE(parser.AtEnd()); | 258 EXPECT_TRUE(parser.AtEnd()); |
| 273 EXPECT_FALSE(parser.ReadRecord(&record)); | 259 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 274 } | 260 } |
| 275 | 261 |
| 276 TEST(DnsResponseTest, InitParseWithoutQueryNoQuestions) { | 262 TEST(DnsResponseTest, InitParseWithoutQueryNoQuestions) { |
| 277 const uint8 response_data[] = { | 263 const uint8_t response_data[] = { |
| 278 // Header | 264 // Header |
| 279 0xca, 0xfe, // ID | 265 0xca, 0xfe, // ID |
| 280 0x81, 0x80, // Standard query response, RA, no error | 266 0x81, 0x80, // Standard query response, RA, no error |
| 281 0x00, 0x00, // No question | 267 0x00, 0x00, // No question |
| 282 0x00, 0x01, // 2 RRs (answers) | 268 0x00, 0x01, // 2 RRs (answers) |
| 283 0x00, 0x00, // 0 authority RRs | 269 0x00, 0x00, // 0 authority RRs |
| 284 0x00, 0x00, // 0 additional RRs | 270 0x00, 0x00, // 0 additional RRs |
| 285 | 271 |
| 286 // Answer 1 | 272 // Answer 1 |
| 287 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', | 273 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h', |
| 288 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', | 274 'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00, |
| 289 0x03, 'o', 'r', 'g', | 275 0x01, // TYPE is A. |
| 290 0x00, | 276 0x00, 0x01, // CLASS is IN. |
| 291 0x00, 0x01, // TYPE is A. | 277 0x00, 0x00, // TTL (4 bytes) is 53 seconds. |
| 292 0x00, 0x01, // CLASS is IN. | 278 0x00, 0x35, 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 293 0x00, 0x00, // TTL (4 bytes) is 53 seconds. | 279 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 |
| 294 0x00, 0x35, | 280 0x5f, 0x79, |
| 295 0x00, 0x04, // RDLENGTH is 4 bytes. | |
| 296 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 | |
| 297 0x5f, 0x79, | |
| 298 }; | 281 }; |
| 299 | 282 |
| 300 DnsResponse resp; | 283 DnsResponse resp; |
| 301 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); | 284 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
| 302 | 285 |
| 303 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data))); | 286 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data))); |
| 304 | 287 |
| 305 // Check header access. | 288 // Check header access. |
| 306 EXPECT_EQ(0x8180, resp.flags()); | 289 EXPECT_EQ(0x8180, resp.flags()); |
| 307 EXPECT_EQ(0x0, resp.rcode()); | 290 EXPECT_EQ(0x0, resp.rcode()); |
| 308 EXPECT_EQ(0x1u, resp.answer_count()); | 291 EXPECT_EQ(0x1u, resp.answer_count()); |
| 309 | 292 |
| 310 DnsResourceRecord record; | 293 DnsResourceRecord record; |
| 311 DnsRecordParser parser = resp.Parser(); | 294 DnsRecordParser parser = resp.Parser(); |
| 312 | 295 |
| 313 EXPECT_FALSE(parser.AtEnd()); | 296 EXPECT_FALSE(parser.AtEnd()); |
| 314 EXPECT_TRUE(parser.ReadRecord(&record)); | 297 EXPECT_TRUE(parser.ReadRecord(&record)); |
| 315 EXPECT_EQ("codereview.chromium.org", record.name); | 298 EXPECT_EQ("codereview.chromium.org", record.name); |
| 316 EXPECT_EQ(0x00000035u, record.ttl); | 299 EXPECT_EQ(0x00000035u, record.ttl); |
| 317 EXPECT_EQ(dns_protocol::kTypeA, record.type); | 300 EXPECT_EQ(dns_protocol::kTypeA, record.type); |
| 318 | 301 |
| 319 EXPECT_TRUE(parser.AtEnd()); | 302 EXPECT_TRUE(parser.AtEnd()); |
| 320 EXPECT_FALSE(parser.ReadRecord(&record)); | 303 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 321 } | 304 } |
| 322 | 305 |
| 323 TEST(DnsResponseTest, InitParseWithoutQueryTwoQuestions) { | 306 TEST(DnsResponseTest, InitParseWithoutQueryTwoQuestions) { |
| 324 const uint8 response_data[] = { | 307 const uint8_t response_data[] = { |
| 325 // Header | 308 // Header |
| 326 0xca, 0xfe, // ID | 309 0xca, 0xfe, // ID |
| 327 0x81, 0x80, // Standard query response, RA, no error | 310 0x81, 0x80, // Standard query response, RA, no error |
| 328 0x00, 0x02, // 2 questions | 311 0x00, 0x02, // 2 questions |
| 329 0x00, 0x01, // 2 RRs (answers) | 312 0x00, 0x01, // 2 RRs (answers) |
| 330 0x00, 0x00, // 0 authority RRs | 313 0x00, 0x00, // 0 authority RRs |
| 331 0x00, 0x00, // 0 additional RRs | 314 0x00, 0x00, // 0 additional RRs |
| 332 | 315 |
| 333 // Question 1 | 316 // Question 1 |
| 334 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', | 317 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h', |
| 335 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', | 318 'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00, |
| 336 0x03, 'o', 'r', 'g', | 319 0x01, // TYPE is A. |
| 337 0x00, | 320 0x00, 0x01, // CLASS is IN. |
| 338 0x00, 0x01, // TYPE is A. | |
| 339 0x00, 0x01, // CLASS is IN. | |
| 340 | 321 |
| 341 // Question 2 | 322 // Question 2 |
| 342 0x0b, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', '2', | 323 0x0b, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', '2', 0xc0, |
| 343 0xc0, 0x18, // pointer to "chromium.org" | 324 0x18, // pointer to "chromium.org" |
| 344 0x00, 0x01, // TYPE is A. | 325 0x00, 0x01, // TYPE is A. |
| 345 0x00, 0x01, // CLASS is IN. | 326 0x00, 0x01, // CLASS is IN. |
| 346 | 327 |
| 347 // Answer 1 | 328 // Answer 1 |
| 348 0xc0, 0x0c, // NAME is a pointer to name in Question section. | 329 0xc0, 0x0c, // NAME is a pointer to name in Question section. |
| 349 0x00, 0x01, // TYPE is A. | 330 0x00, 0x01, // TYPE is A. |
| 350 0x00, 0x01, // CLASS is IN. | 331 0x00, 0x01, // CLASS is IN. |
| 351 0x00, 0x00, // TTL (4 bytes) is 53 seconds. | 332 0x00, 0x00, // TTL (4 bytes) is 53 seconds. |
| 352 0x00, 0x35, | 333 0x00, 0x35, 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 353 0x00, 0x04, // RDLENGTH is 4 bytes. | 334 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 |
| 354 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 | 335 0x5f, 0x79, |
| 355 0x5f, 0x79, | |
| 356 }; | 336 }; |
| 357 | 337 |
| 358 DnsResponse resp; | 338 DnsResponse resp; |
| 359 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); | 339 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
| 360 | 340 |
| 361 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data))); | 341 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data))); |
| 362 | 342 |
| 363 // Check header access. | 343 // Check header access. |
| 364 EXPECT_EQ(0x8180, resp.flags()); | 344 EXPECT_EQ(0x8180, resp.flags()); |
| 365 EXPECT_EQ(0x0, resp.rcode()); | 345 EXPECT_EQ(0x0, resp.rcode()); |
| 366 EXPECT_EQ(0x01u, resp.answer_count()); | 346 EXPECT_EQ(0x01u, resp.answer_count()); |
| 367 | 347 |
| 368 DnsResourceRecord record; | 348 DnsResourceRecord record; |
| 369 DnsRecordParser parser = resp.Parser(); | 349 DnsRecordParser parser = resp.Parser(); |
| 370 | 350 |
| 371 EXPECT_FALSE(parser.AtEnd()); | 351 EXPECT_FALSE(parser.AtEnd()); |
| 372 EXPECT_TRUE(parser.ReadRecord(&record)); | 352 EXPECT_TRUE(parser.ReadRecord(&record)); |
| 373 EXPECT_EQ("codereview.chromium.org", record.name); | 353 EXPECT_EQ("codereview.chromium.org", record.name); |
| 374 EXPECT_EQ(0x35u, record.ttl); | 354 EXPECT_EQ(0x35u, record.ttl); |
| 375 EXPECT_EQ(dns_protocol::kTypeA, record.type); | 355 EXPECT_EQ(dns_protocol::kTypeA, record.type); |
| 376 | 356 |
| 377 EXPECT_TRUE(parser.AtEnd()); | 357 EXPECT_TRUE(parser.AtEnd()); |
| 378 EXPECT_FALSE(parser.ReadRecord(&record)); | 358 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 379 } | 359 } |
| 380 | 360 |
| 381 TEST(DnsResponseTest, InitParseWithoutQueryPacketTooShort) { | 361 TEST(DnsResponseTest, InitParseWithoutQueryPacketTooShort) { |
| 382 const uint8 response_data[] = { | 362 const uint8_t response_data[] = { |
| 383 // Header | 363 // Header |
| 384 0xca, 0xfe, // ID | 364 0xca, 0xfe, // ID |
| 385 0x81, 0x80, // Standard query response, RA, no error | 365 0x81, 0x80, // Standard query response, RA, no error |
| 386 0x00, 0x00, // No question | 366 0x00, 0x00, // No question |
| 387 }; | 367 }; |
| 388 | 368 |
| 389 DnsResponse resp; | 369 DnsResponse resp; |
| 390 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); | 370 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
| 391 | 371 |
| 392 EXPECT_FALSE(resp.InitParseWithoutQuery(sizeof(response_data))); | 372 EXPECT_FALSE(resp.InitParseWithoutQuery(sizeof(response_data))); |
| 393 } | 373 } |
| 394 | 374 |
| 395 void VerifyAddressList(const std::vector<const char*>& ip_addresses, | 375 void VerifyAddressList(const std::vector<const char*>& ip_addresses, |
| 396 const AddressList& addrlist) { | 376 const AddressList& addrlist) { |
| 397 ASSERT_EQ(ip_addresses.size(), addrlist.size()); | 377 ASSERT_EQ(ip_addresses.size(), addrlist.size()); |
| 398 | 378 |
| 399 for (size_t i = 0; i < addrlist.size(); ++i) { | 379 for (size_t i = 0; i < addrlist.size(); ++i) { |
| 400 EXPECT_EQ(ip_addresses[i], addrlist[i].ToStringWithoutPort()); | 380 EXPECT_EQ(ip_addresses[i], addrlist[i].ToStringWithoutPort()); |
| 401 } | 381 } |
| 402 } | 382 } |
| 403 | 383 |
| 404 TEST(DnsResponseTest, ParseToAddressList) { | 384 TEST(DnsResponseTest, ParseToAddressList) { |
| 405 const struct TestCase { | 385 const struct TestCase { |
| 406 size_t query_size; | 386 size_t query_size; |
| 407 const uint8* response_data; | 387 const uint8_t* response_data; |
| 408 size_t response_size; | 388 size_t response_size; |
| 409 const char* const* expected_addresses; | 389 const char* const* expected_addresses; |
| 410 size_t num_expected_addresses; | 390 size_t num_expected_addresses; |
| 411 const char* expected_cname; | 391 const char* expected_cname; |
| 412 int expected_ttl_sec; | 392 int expected_ttl_sec; |
| 413 } cases[] = { | 393 } cases[] = { |
| 414 { | 394 { |
| 415 kT0QuerySize, | 395 kT0QuerySize, |
| 416 kT0ResponseDatagram, arraysize(kT0ResponseDatagram), | 396 kT0ResponseDatagram, arraysize(kT0ResponseDatagram), |
| 417 kT0IpAddresses, arraysize(kT0IpAddresses), | 397 kT0IpAddresses, arraysize(kT0IpAddresses), |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 response.ParseToAddressList(&addr_list, &ttl)); | 430 response.ParseToAddressList(&addr_list, &ttl)); |
| 451 std::vector<const char*> expected_addresses( | 431 std::vector<const char*> expected_addresses( |
| 452 t.expected_addresses, | 432 t.expected_addresses, |
| 453 t.expected_addresses + t.num_expected_addresses); | 433 t.expected_addresses + t.num_expected_addresses); |
| 454 VerifyAddressList(expected_addresses, addr_list); | 434 VerifyAddressList(expected_addresses, addr_list); |
| 455 EXPECT_EQ(t.expected_cname, addr_list.canonical_name()); | 435 EXPECT_EQ(t.expected_cname, addr_list.canonical_name()); |
| 456 EXPECT_EQ(base::TimeDelta::FromSeconds(t.expected_ttl_sec), ttl); | 436 EXPECT_EQ(base::TimeDelta::FromSeconds(t.expected_ttl_sec), ttl); |
| 457 } | 437 } |
| 458 } | 438 } |
| 459 | 439 |
| 460 const uint8 kResponseTruncatedRecord[] = { | 440 const uint8_t kResponseTruncatedRecord[] = { |
| 461 // Header: 1 question, 1 answer RR | 441 // Header: 1 question, 1 answer RR |
| 462 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | 442 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 463 // Question: name = 'a', type = A (0x1) | 443 // Question: name = 'a', type = A (0x1) |
| 464 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, | 444 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, |
| 465 // Answer: name = 'a', type = A, TTL = 0xFF, RDATA = 10.10.10.10 | 445 // Answer: name = 'a', type = A, TTL = 0xFF, RDATA = 10.10.10.10 |
| 466 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 446 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 467 0x00, 0x04, 0x0A, 0x0A, 0x0A, // Truncated RDATA. | 447 0x0A, 0x0A, 0x0A, // Truncated RDATA. |
| 468 }; | 448 }; |
| 469 | 449 |
| 470 const uint8 kResponseTruncatedCNAME[] = { | 450 const uint8_t kResponseTruncatedCNAME[] = { |
| 471 // Header: 1 question, 1 answer RR | 451 // Header: 1 question, 1 answer RR |
| 472 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | 452 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 473 // Question: name = 'a', type = A (0x1) | 453 // Question: name = 'a', type = A (0x1) |
| 474 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, | 454 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, |
| 475 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'foo' (truncated) | 455 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'foo' (truncated) |
| 476 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 456 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x03, |
| 477 0x00, 0x03, 0x03, 'f', 'o', // Truncated name. | 457 0x03, 'f', 'o', // Truncated name. |
| 478 }; | 458 }; |
| 479 | 459 |
| 480 const uint8 kResponseNameMismatch[] = { | 460 const uint8_t kResponseNameMismatch[] = { |
| 481 // Header: 1 question, 1 answer RR | 461 // Header: 1 question, 1 answer RR |
| 482 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | 462 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 483 // Question: name = 'a', type = A (0x1) | 463 // Question: name = 'a', type = A (0x1) |
| 484 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, | 464 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, |
| 485 // Answer: name = 'b', type = A, TTL = 0xFF, RDATA = 10.10.10.10 | 465 // Answer: name = 'b', type = A, TTL = 0xFF, RDATA = 10.10.10.10 |
| 486 0x01, 'b', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 466 0x01, 'b', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 487 0x00, 0x04, 0x0A, 0x0A, 0x0A, 0x0A, | 467 0x0A, 0x0A, 0x0A, 0x0A, |
| 488 }; | 468 }; |
| 489 | 469 |
| 490 const uint8 kResponseNameMismatchInChain[] = { | 470 const uint8_t kResponseNameMismatchInChain[] = { |
| 491 // Header: 1 question, 3 answer RR | 471 // Header: 1 question, 3 answer RR |
| 492 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, | 472 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, |
| 493 // Question: name = 'a', type = A (0x1) | 473 // Question: name = 'a', type = A (0x1) |
| 494 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, | 474 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, |
| 495 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'b' | 475 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'b' |
| 496 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 476 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x03, |
| 497 0x00, 0x03, 0x01, 'b', 0x00, | 477 0x01, 'b', 0x00, |
| 498 // Answer: name = 'b', type = A, TTL = 0xFF, RDATA = 10.10.10.10 | 478 // Answer: name = 'b', type = A, TTL = 0xFF, RDATA = 10.10.10.10 |
| 499 0x01, 'b', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 479 0x01, 'b', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 500 0x00, 0x04, 0x0A, 0x0A, 0x0A, 0x0A, | 480 0x0A, 0x0A, 0x0A, 0x0A, |
| 501 // Answer: name = 'c', type = A, TTL = 0xFF, RDATA = 10.10.10.11 | 481 // Answer: name = 'c', type = A, TTL = 0xFF, RDATA = 10.10.10.11 |
| 502 0x01, 'c', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 482 0x01, 'c', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 503 0x00, 0x04, 0x0A, 0x0A, 0x0A, 0x0B, | 483 0x0A, 0x0A, 0x0A, 0x0B, |
| 504 }; | 484 }; |
| 505 | 485 |
| 506 const uint8 kResponseSizeMismatch[] = { | 486 const uint8_t kResponseSizeMismatch[] = { |
| 507 // Header: 1 answer RR | 487 // Header: 1 answer RR |
| 508 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | 488 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 509 // Question: name = 'a', type = AAAA (0x1c) | 489 // Question: name = 'a', type = AAAA (0x1c) |
| 510 0x01, 'a', 0x00, 0x00, 0x1c, 0x00, 0x01, | 490 0x01, 'a', 0x00, 0x00, 0x1c, 0x00, 0x01, |
| 511 // Answer: name = 'a', type = AAAA, TTL = 0xFF, RDATA = 10.10.10.10 | 491 // Answer: name = 'a', type = AAAA, TTL = 0xFF, RDATA = 10.10.10.10 |
| 512 0x01, 'a', 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 492 0x01, 'a', 0x00, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 513 0x00, 0x04, 0x0A, 0x0A, 0x0A, 0x0A, | 493 0x0A, 0x0A, 0x0A, 0x0A, |
| 514 }; | 494 }; |
| 515 | 495 |
| 516 const uint8 kResponseCNAMEAfterAddress[] = { | 496 const uint8_t kResponseCNAMEAfterAddress[] = { |
| 517 // Header: 2 answer RR | 497 // Header: 2 answer RR |
| 518 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, | 498 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, |
| 519 // Question: name = 'a', type = A (0x1) | 499 // Question: name = 'a', type = A (0x1) |
| 520 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, | 500 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, |
| 521 // Answer: name = 'a', type = A, TTL = 0xFF, RDATA = 10.10.10.10. | 501 // Answer: name = 'a', type = A, TTL = 0xFF, RDATA = 10.10.10.10. |
| 522 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 502 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 523 0x00, 0x04, 0x0A, 0x0A, 0x0A, 0x0A, | 503 0x0A, 0x0A, 0x0A, 0x0A, |
| 524 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'b' | 504 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'b' |
| 525 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 505 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x03, |
| 526 0x00, 0x03, 0x01, 'b', 0x00, | 506 0x01, 'b', 0x00, |
| 527 }; | 507 }; |
| 528 | 508 |
| 529 const uint8 kResponseNoAddresses[] = { | 509 const uint8_t kResponseNoAddresses[] = { |
| 530 // Header: 1 question, 1 answer RR, 1 authority RR | 510 // Header: 1 question, 1 answer RR, 1 authority RR |
| 531 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | 511 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 532 // Question: name = 'a', type = A (0x1) | 512 // Question: name = 'a', type = A (0x1) |
| 533 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, | 513 0x01, 'a', 0x00, 0x00, 0x01, 0x00, 0x01, |
| 534 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'b' | 514 // Answer: name = 'a', type = CNAME, TTL = 0xFF, RDATA = 'b' |
| 535 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 515 0x01, 'a', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x03, |
| 536 0x00, 0x03, 0x01, 'b', 0x00, | 516 0x01, 'b', 0x00, |
| 537 // Authority section | 517 // Authority section |
| 538 // Answer: name = 'b', type = A, TTL = 0xFF, RDATA = 10.10.10.10 | 518 // Answer: name = 'b', type = A, TTL = 0xFF, RDATA = 10.10.10.10 |
| 539 0x01, 'b', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, | 519 0x01, 'b', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x04, |
| 540 0x00, 0x04, 0x0A, 0x0A, 0x0A, 0x0A, | 520 0x0A, 0x0A, 0x0A, 0x0A, |
| 541 }; | 521 }; |
| 542 | 522 |
| 543 TEST(DnsResponseTest, ParseToAddressListFail) { | 523 TEST(DnsResponseTest, ParseToAddressListFail) { |
| 544 const struct TestCase { | 524 const struct TestCase { |
| 545 const uint8* data; | 525 const uint8_t* data; |
| 546 size_t size; | 526 size_t size; |
| 547 DnsResponse::Result expected_result; | 527 DnsResponse::Result expected_result; |
| 548 } cases[] = { | 528 } cases[] = { |
| 549 { kResponseTruncatedRecord, arraysize(kResponseTruncatedRecord), | 529 { kResponseTruncatedRecord, arraysize(kResponseTruncatedRecord), |
| 550 DnsResponse::DNS_MALFORMED_RESPONSE }, | 530 DnsResponse::DNS_MALFORMED_RESPONSE }, |
| 551 { kResponseTruncatedCNAME, arraysize(kResponseTruncatedCNAME), | 531 { kResponseTruncatedCNAME, arraysize(kResponseTruncatedCNAME), |
| 552 DnsResponse::DNS_MALFORMED_CNAME }, | 532 DnsResponse::DNS_MALFORMED_CNAME }, |
| 553 { kResponseNameMismatch, arraysize(kResponseNameMismatch), | 533 { kResponseNameMismatch, arraysize(kResponseNameMismatch), |
| 554 DnsResponse::DNS_NAME_MISMATCH }, | 534 DnsResponse::DNS_NAME_MISMATCH }, |
| 555 { kResponseNameMismatchInChain, arraysize(kResponseNameMismatchInChain), | 535 { kResponseNameMismatchInChain, arraysize(kResponseNameMismatchInChain), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 572 AddressList addr_list; | 552 AddressList addr_list; |
| 573 base::TimeDelta ttl; | 553 base::TimeDelta ttl; |
| 574 EXPECT_EQ(t.expected_result, | 554 EXPECT_EQ(t.expected_result, |
| 575 response.ParseToAddressList(&addr_list, &ttl)); | 555 response.ParseToAddressList(&addr_list, &ttl)); |
| 576 } | 556 } |
| 577 } | 557 } |
| 578 | 558 |
| 579 } // namespace | 559 } // namespace |
| 580 | 560 |
| 581 } // namespace net | 561 } // namespace net |
| OLD | NEW |