OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/address_list.h" |
| 6 #include "net/base/dns_response.h" |
| 7 #include "net/base/sys_addrinfo.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace net { |
| 11 |
| 12 // DNS response consists of a header followed by a question followed by |
| 13 // answer. Header format, question format and response format are |
| 14 // described below. For the meaning of specific fields, please see RFC |
| 15 // 1035. |
| 16 |
| 17 // Header format. |
| 18 // 1 1 1 1 1 1 |
| 19 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 20 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 21 // | ID | |
| 22 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 23 // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | |
| 24 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 25 // | QDCOUNT | |
| 26 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 27 // | ANCOUNT | |
| 28 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 29 // | NSCOUNT | |
| 30 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 31 // | ARCOUNT | |
| 32 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 33 |
| 34 // Question format. |
| 35 // 1 1 1 1 1 1 |
| 36 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 37 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 38 // | | |
| 39 // / QNAME / |
| 40 // / / |
| 41 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 42 // | QTYPE | |
| 43 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 44 // | QCLASS | |
| 45 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 46 |
| 47 // Answser format. |
| 48 // 1 1 1 1 1 1 |
| 49 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 50 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 51 // | | |
| 52 // / / |
| 53 // / NAME / |
| 54 // | | |
| 55 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 56 // | TYPE | |
| 57 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 58 // | CLASS | |
| 59 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 60 // | TTL | |
| 61 // | | |
| 62 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 63 // | RDLENGTH | |
| 64 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| |
| 65 // / RDATA / |
| 66 // / / |
| 67 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 68 |
| 69 TEST(DnsResponseTest, ResponseWithCnameA) { |
| 70 const std::string kHostname = "codereview.chromium.org"; |
| 71 const int kPort = 80; |
| 72 |
| 73 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); |
| 74 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| 75 |
| 76 uint8 ip[] = { // codereview.chromium.org resolves to |
| 77 0x4a, 0x7d, 0x5f, 0x79 // 74.125.95.121 |
| 78 }; |
| 79 |
| 80 uint8 response_data[] = { |
| 81 // Header |
| 82 id_hi, id_lo, // ID |
| 83 0x81, 0x80, // Standard query response, no error |
| 84 0x00, 0x01, // 1 question |
| 85 0x00, 0x02, // 2 RRs (answers) |
| 86 0x00, 0x00, // 0 authority RRs |
| 87 0x00, 0x00, // 0 additional RRs |
| 88 |
| 89 // Question |
| 90 0x0a, 0x63, 0x6f, 0x64, // This part should be echoed back. |
| 91 0x65, 0x72, 0x65, 0x76, |
| 92 0x69, 0x65, 0x77, 0x08, |
| 93 0x63, 0x68, 0x72, 0x6f, |
| 94 0x6d, 0x69, 0x75, 0x6d, |
| 95 0x03, 0x6f, 0x72, 0x67, |
| 96 0x00, |
| 97 0x00, 0x01, |
| 98 0x00, 0x01, |
| 99 |
| 100 // Answer 1 |
| 101 0xc0, 0x0c, // NAME is a pointer to name in Question section |
| 102 0x00, 0x05, // TYPE is CNAME |
| 103 0x00, 0x01, // CLASS is IN |
| 104 0x00, 0x01, // TTL is 20 hours, 47 minutes, 48 seconds (4 bytes) |
| 105 0x24, 0x74, |
| 106 0x00, 0x12, // RDLENGTH is 18 bytes |
| 107 0x03, 0x67, 0x68, 0x73, // ghs.l.google.com in DNS format. |
| 108 0x01, 0x6c, 0x06, 0x67, |
| 109 0x6f, 0x6f, 0x67, 0x6c, |
| 110 0x65, 0x03, 0x63, 0x6f, |
| 111 0x6d, 0x00, |
| 112 |
| 113 // Answer 2 |
| 114 0xc0, 0x35, // NAME is a pointer to name in Question section |
| 115 0x00, 0x01, // TYPE is A |
| 116 0x00, 0x01, // CLASS is IN |
| 117 0x00, 0x00, // TTL is 53 seconds (4 bytes) |
| 118 0x00, 0x35, |
| 119 0x00, 0x04, // RDLENGTH is 4 bytes |
| 120 ip[0], ip[1], ip[2], ip[3], // RDATA is the IP. |
| 121 }; |
| 122 |
| 123 // Create a response object and simulate reading into it. |
| 124 DnsResponse r1(&q1); |
| 125 memcpy(r1.data(), &response_data[0], r1.size()); |
| 126 |
| 127 int response_size = arraysize(response_data); |
| 128 AddressList address_list; |
| 129 EXPECT_TRUE(r1.Parse(response_size, &address_list)); |
| 130 EXPECT_EQ(0, r1.error()); |
| 131 EXPECT_EQ(response_size, r1.size()); |
| 132 |
| 133 // Verify AddressList content. |
| 134 size_t sockaddr_size = sizeof(struct sockaddr_in); |
| 135 const struct addrinfo* ai = address_list.head(); |
| 136 EXPECT_EQ(kPort, address_list.GetPort()); |
| 137 |
| 138 // Verify addrinfo part. |
| 139 EXPECT_TRUE(ai != NULL); |
| 140 EXPECT_EQ(AF_INET, ai->ai_family); |
| 141 EXPECT_EQ(SOCK_STREAM, ai->ai_socktype); |
| 142 EXPECT_EQ(sockaddr_size, ai->ai_addrlen); |
| 143 |
| 144 // Verify sockaddr_in part. |
| 145 struct sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(ai->ai_addr); |
| 146 ASSERT_TRUE(sa != NULL); |
| 147 EXPECT_EQ(AF_INET, sa->sin_family); |
| 148 EXPECT_EQ(kPort, ntohs(sa->sin_port)); |
| 149 EXPECT_EQ(0, memcmp(&sa->sin_addr, &ip[0], kIPv4AddressSize)); |
| 150 } |
| 151 |
| 152 } // namespace net |
OLD | NEW |