Chromium Code Reviews| Index: net/base/dns_response_unittest.cc |
| diff --git a/net/base/dns_response_unittest.cc b/net/base/dns_response_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3d2e4fd9052dee7367242c7b52eb0d67e2f67cd2 |
| --- /dev/null |
| +++ b/net/base/dns_response_unittest.cc |
| @@ -0,0 +1,152 @@ |
| +// Copyright (c) 2011 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 "net/base/address_list.h" |
| +#include "net/base/dns_response.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/sys_addrinfo.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +// DNS response consists of a header followed by a question followed by |
| +// answer. Header format, question format and response format are |
| +// described below. For the meaning of specific fields, please see RFC |
| +// 1035. |
| + |
| +// Header format. |
| +// 1 1 1 1 1 1 |
| +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | ID | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// |QR| Opcode |AA|TC|RD|RA| Z | RCODE | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | QDCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | ANCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | NSCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | ARCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| + |
| +// Question format. |
| +// 1 1 1 1 1 1 |
| +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | | |
| +// / QNAME / |
| +// / / |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | QTYPE | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | QCLASS | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| + |
| +// Answser format. |
| +// 1 1 1 1 1 1 |
| +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | | |
| +// / / |
| +// / NAME / |
| +// | | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | TYPE | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | CLASS | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | TTL | |
| +// | | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | RDLENGTH | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| |
| +// / RDATA / |
| +// / / |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| + |
| +TEST(DnsResponseTest, ResponseWithCnameA) { |
|
cbentzel
2011/06/02 21:27:13
Thanks for including CNAME and making sure that th
cbentzel
2011/06/02 21:27:13
You should add a lot more tests for the invalid ca
agayev
2011/06/02 22:33:26
I will add more tests, but I need to stabilize the
|
| + const std::string kHostname = "codereview.chromium.org"; |
| + const int kPort = 80; |
| + |
| + DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); |
| + uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| + |
| + uint8 ip[] = { // codereview.chromium.org resolves to |
| + 0x4a, 0x7d, 0x5f, 0x79 // 74.125.95.121 |
| + }; |
| + |
| + uint8 response_data[] = { |
| + // Header |
| + id_hi, id_lo, // ID |
| + 0x81, 0x80, // Standard query response, no error |
| + 0x00, 0x01, // 1 question |
| + 0x00, 0x02, // 2 RRs (answers) |
| + 0x00, 0x00, // 0 authority RRs |
| + 0x00, 0x00, // 0 additional RRs |
| + |
| + // Question |
| + 0x0a, 0x63, 0x6f, 0x64, // This part should be echoed back. |
| + 0x65, 0x72, 0x65, 0x76, |
| + 0x69, 0x65, 0x77, 0x08, |
| + 0x63, 0x68, 0x72, 0x6f, |
| + 0x6d, 0x69, 0x75, 0x6d, |
| + 0x03, 0x6f, 0x72, 0x67, |
| + 0x00, |
| + 0x00, 0x01, |
| + 0x00, 0x01, |
| + |
| + // Answer 1 |
| + 0xc0, 0x0c, // NAME is a pointer to name in Question section |
| + 0x00, 0x05, // TYPE is CNAME |
| + 0x00, 0x01, // CLASS is IN |
| + 0x00, 0x01, // TTL is 20 hours, 47 minutes, 48 seconds (4 bytes) |
| + 0x24, 0x74, |
| + 0x00, 0x12, // RDLENGTH is 18 bytes |
| + 0x03, 0x67, 0x68, 0x73, // ghs.l.google.com in DNS format. |
| + 0x01, 0x6c, 0x06, 0x67, |
| + 0x6f, 0x6f, 0x67, 0x6c, |
| + 0x65, 0x03, 0x63, 0x6f, |
| + 0x6d, 0x00, |
| + |
| + // Answer 2 |
| + 0xc0, 0x35, // NAME is a pointer to name in Question section |
| + 0x00, 0x01, // TYPE is A |
| + 0x00, 0x01, // CLASS is IN |
| + 0x00, 0x00, // TTL is 53 seconds (4 bytes) |
| + 0x00, 0x35, |
| + 0x00, 0x04, // RDLENGTH is 4 bytes |
| + ip[0], ip[1], ip[2], ip[3], // RDATA is the IP. |
| + }; |
| + |
| + // Create a response object and simulate reading into it. |
| + DnsResponse r1(&q1); |
| + memcpy(r1.io_buffer()->data(), &response_data[0], |
| + r1.io_buffer()->size()); |
| + |
| + int response_size = arraysize(response_data); |
| + AddressList address_list; |
| + EXPECT_EQ(0, r1.Parse(response_size, &address_list)); |
| + |
| + // Verify AddressList content. |
| + size_t sockaddr_size = sizeof(struct sockaddr_in); |
| + const struct addrinfo* ai = address_list.head(); |
| + EXPECT_EQ(kPort, address_list.GetPort()); |
| + |
| + // Verify addrinfo part. |
| + EXPECT_TRUE(ai != NULL); |
| + EXPECT_EQ(AF_INET, ai->ai_family); |
| + EXPECT_EQ(SOCK_STREAM, ai->ai_socktype); |
| + EXPECT_EQ(sockaddr_size, ai->ai_addrlen); |
| + |
| + // Verify sockaddr_in part. |
| + struct sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(ai->ai_addr); |
| + ASSERT_TRUE(sa != NULL); |
| + EXPECT_EQ(AF_INET, sa->sin_family); |
| + EXPECT_EQ(kPort, ntohs(sa->sin_port)); |
| + EXPECT_EQ(0, memcmp(&sa->sin_addr, &ip[0], kIPv4AddressSize)); |
| +} |
| + |
| +} // namespace net |