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.h" | 7 #include "base/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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 238 |
239 DnsResourceRecord record; | 239 DnsResourceRecord record; |
240 DnsRecordParser parser = resp.Parser(); | 240 DnsRecordParser parser = resp.Parser(); |
241 EXPECT_TRUE(parser.ReadRecord(&record)); | 241 EXPECT_TRUE(parser.ReadRecord(&record)); |
242 EXPECT_FALSE(parser.AtEnd()); | 242 EXPECT_FALSE(parser.AtEnd()); |
243 EXPECT_TRUE(parser.ReadRecord(&record)); | 243 EXPECT_TRUE(parser.ReadRecord(&record)); |
244 EXPECT_TRUE(parser.AtEnd()); | 244 EXPECT_TRUE(parser.AtEnd()); |
245 EXPECT_FALSE(parser.ReadRecord(&record)); | 245 EXPECT_FALSE(parser.ReadRecord(&record)); |
246 } | 246 } |
247 | 247 |
| 248 TEST(DnsResponseTest, InitParseWithoutQuery) { |
| 249 DnsResponse resp; |
| 250 memcpy(resp.io_buffer()->data(), kT0ResponseDatagram, |
| 251 sizeof(kT0ResponseDatagram)); |
| 252 |
| 253 // Accept matching question. |
| 254 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(kT0ResponseDatagram))); |
| 255 EXPECT_TRUE(resp.IsValid()); |
| 256 |
| 257 // Check header access. |
| 258 EXPECT_EQ(0x8180, resp.flags()); |
| 259 EXPECT_EQ(0x0, resp.rcode()); |
| 260 EXPECT_EQ(kT0RecordCount, resp.answer_count()); |
| 261 |
| 262 // Check question access. |
| 263 EXPECT_EQ(kT0Qtype, resp.qtype()); |
| 264 EXPECT_EQ(kT0HostName, resp.GetDottedName()); |
| 265 |
| 266 DnsResourceRecord record; |
| 267 DnsRecordParser parser = resp.Parser(); |
| 268 for (unsigned i = 0; i < kT0RecordCount; i ++) { |
| 269 EXPECT_FALSE(parser.AtEnd()); |
| 270 EXPECT_TRUE(parser.ReadRecord(&record)); |
| 271 } |
| 272 EXPECT_TRUE(parser.AtEnd()); |
| 273 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 274 } |
| 275 |
| 276 TEST(DnsResponseTest, InitParseWithoutQueryNoQuestions) { |
| 277 const uint8 response_data[] = { |
| 278 // Header |
| 279 0xca, 0xfe, // ID |
| 280 0x81, 0x80, // Standard query response, RA, no error |
| 281 0x00, 0x00, // No question |
| 282 0x00, 0x01, // 2 RRs (answers) |
| 283 0x00, 0x00, // 0 authority RRs |
| 284 0x00, 0x00, // 0 additional RRs |
| 285 |
| 286 // Answer 1 |
| 287 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', |
| 288 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', |
| 289 0x03, 'o', 'r', 'g', |
| 290 0x00, |
| 291 0x00, 0x01, // TYPE is A. |
| 292 0x00, 0x01, // CLASS is IN. |
| 293 0x00, 0x00, // TTL (4 bytes) is 53 seconds. |
| 294 0x00, 0x35, |
| 295 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 296 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 |
| 297 0x5f, 0x79, |
| 298 }; |
| 299 |
| 300 DnsResponse resp; |
| 301 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
| 302 |
| 303 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data))); |
| 304 |
| 305 // Check header access. |
| 306 EXPECT_EQ(0x8180, resp.flags()); |
| 307 EXPECT_EQ(0x0, resp.rcode()); |
| 308 EXPECT_EQ(0x1u, resp.answer_count()); |
| 309 |
| 310 DnsResourceRecord record; |
| 311 DnsRecordParser parser = resp.Parser(); |
| 312 |
| 313 EXPECT_FALSE(parser.AtEnd()); |
| 314 EXPECT_TRUE(parser.ReadRecord(&record)); |
| 315 EXPECT_EQ("codereview.chromium.org", record.name); |
| 316 EXPECT_EQ(0x00000035u, record.ttl); |
| 317 EXPECT_EQ(dns_protocol::kTypeA, record.type); |
| 318 |
| 319 EXPECT_TRUE(parser.AtEnd()); |
| 320 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 321 } |
| 322 |
| 323 TEST(DnsResponseTest, InitParseWithoutQueryTwoQuestions) { |
| 324 const uint8 response_data[] = { |
| 325 // Header |
| 326 0xca, 0xfe, // ID |
| 327 0x81, 0x80, // Standard query response, RA, no error |
| 328 0x00, 0x02, // 2 questions |
| 329 0x00, 0x01, // 2 RRs (answers) |
| 330 0x00, 0x00, // 0 authority RRs |
| 331 0x00, 0x00, // 0 additional RRs |
| 332 |
| 333 // Question 1 |
| 334 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', |
| 335 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm', |
| 336 0x03, 'o', 'r', 'g', |
| 337 0x00, |
| 338 0x00, 0x01, // TYPE is A. |
| 339 0x00, 0x01, // CLASS is IN. |
| 340 |
| 341 // Question 2 |
| 342 0x0b, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', '2', |
| 343 0xc0, 0x18, // pointer to "chromium.org" |
| 344 0x00, 0x01, // TYPE is A. |
| 345 0x00, 0x01, // CLASS is IN. |
| 346 |
| 347 // Answer 1 |
| 348 0xc0, 0x0c, // NAME is a pointer to name in Question section. |
| 349 0x00, 0x01, // TYPE is A. |
| 350 0x00, 0x01, // CLASS is IN. |
| 351 0x00, 0x00, // TTL (4 bytes) is 53 seconds. |
| 352 0x00, 0x35, |
| 353 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 354 0x4a, 0x7d, // RDATA is the IP: 74.125.95.121 |
| 355 0x5f, 0x79, |
| 356 }; |
| 357 |
| 358 DnsResponse resp; |
| 359 memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data)); |
| 360 |
| 361 EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data))); |
| 362 |
| 363 // Check header access. |
| 364 EXPECT_EQ(0x8180, resp.flags()); |
| 365 EXPECT_EQ(0x0, resp.rcode()); |
| 366 EXPECT_EQ(0x01u, resp.answer_count()); |
| 367 |
| 368 DnsResourceRecord record; |
| 369 DnsRecordParser parser = resp.Parser(); |
| 370 |
| 371 EXPECT_FALSE(parser.AtEnd()); |
| 372 EXPECT_TRUE(parser.ReadRecord(&record)); |
| 373 EXPECT_EQ("codereview.chromium.org", record.name); |
| 374 EXPECT_EQ(0x35u, record.ttl); |
| 375 EXPECT_EQ(dns_protocol::kTypeA, record.type); |
| 376 |
| 377 EXPECT_TRUE(parser.AtEnd()); |
| 378 EXPECT_FALSE(parser.ReadRecord(&record)); |
| 379 } |
| 380 |
248 void VerifyAddressList(const std::vector<const char*>& ip_addresses, | 381 void VerifyAddressList(const std::vector<const char*>& ip_addresses, |
249 const AddressList& addrlist) { | 382 const AddressList& addrlist) { |
250 ASSERT_EQ(ip_addresses.size(), addrlist.size()); | 383 ASSERT_EQ(ip_addresses.size(), addrlist.size()); |
251 | 384 |
252 for (size_t i = 0; i < addrlist.size(); ++i) { | 385 for (size_t i = 0; i < addrlist.size(); ++i) { |
253 EXPECT_EQ(ip_addresses[i], addrlist[i].ToStringWithoutPort()); | 386 EXPECT_EQ(ip_addresses[i], addrlist[i].ToStringWithoutPort()); |
254 } | 387 } |
255 } | 388 } |
256 | 389 |
257 TEST(DnsResponseTest, ParseToAddressList) { | 390 TEST(DnsResponseTest, ParseToAddressList) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 AddressList addr_list; | 558 AddressList addr_list; |
426 base::TimeDelta ttl; | 559 base::TimeDelta ttl; |
427 EXPECT_EQ(t.expected_result, | 560 EXPECT_EQ(t.expected_result, |
428 response.ParseToAddressList(&addr_list, &ttl)); | 561 response.ParseToAddressList(&addr_list, &ttl)); |
429 } | 562 } |
430 } | 563 } |
431 | 564 |
432 } // namespace | 565 } // namespace |
433 | 566 |
434 } // namespace net | 567 } // namespace net |
OLD | NEW |