| 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/base/address_list.h" | |
| 6 #include "net/base/dns_response.h" | 5 #include "net/base/dns_response.h" |
| 6 #include "net/base/dns_util.h" |
| 7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 8 #include "net/base/sys_addrinfo.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 9 |
| 11 namespace net { | 10 namespace net { |
| 12 | 11 |
| 13 // DNS response consists of a header followed by a question followed by | 12 // DNS response consists of a header followed by a question followed by |
| 14 // answer. Header format, question format and response format are | 13 // answer. Header format, question format and response format are |
| 15 // described below. For the meaning of specific fields, please see RFC | 14 // described below. For the meaning of specific fields, please see RFC |
| 16 // 1035. | 15 // 1035. |
| 17 | 16 |
| 18 // Header format. | 17 // Header format. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 59 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 61 // | TTL | | 60 // | TTL | |
| 62 // | | | 61 // | | |
| 63 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 62 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 64 // | RDLENGTH | | 63 // | RDLENGTH | |
| 65 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| | 64 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| |
| 66 // / RDATA / | 65 // / RDATA / |
| 67 // / / | 66 // / / |
| 68 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 67 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 69 | 68 |
| 69 // TODO(agayev): add more thorough tests. |
| 70 TEST(DnsResponseTest, ResponseWithCnameA) { | 70 TEST(DnsResponseTest, ResponseWithCnameA) { |
| 71 const std::string kHostname = "codereview.chromium.org"; | 71 const std::string kHostnameDns("\012codereview\010chromium\003org", 25); |
| 72 const uint16 kPort = 80; | |
| 73 | 72 |
| 74 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | 73 DnsQuery q1(kHostnameDns, kDNS_A); |
| 75 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | 74 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| 76 | 75 |
| 77 uint8 ip[] = { // codereview.chromium.org resolves to | 76 uint8 ip[] = { // codereview.chromium.org resolves to |
| 78 0x4a, 0x7d, 0x5f, 0x79 // 74.125.95.121 | 77 0x4a, 0x7d, 0x5f, 0x79 // 74.125.95.121 |
| 79 }; | 78 }; |
| 80 | 79 |
| 80 std::vector<IPAddressNumber> expected_ips; |
| 81 expected_ips.push_back(IPAddressNumber(ip, ip + arraysize(ip))); |
| 82 |
| 81 uint8 response_data[] = { | 83 uint8 response_data[] = { |
| 82 // Header | 84 // Header |
| 83 id_hi, id_lo, // ID | 85 id_hi, id_lo, // ID |
| 84 0x81, 0x80, // Standard query response, no error | 86 0x81, 0x80, // Standard query response, no error |
| 85 0x00, 0x01, // 1 question | 87 0x00, 0x01, // 1 question |
| 86 0x00, 0x02, // 2 RRs (answers) | 88 0x00, 0x02, // 2 RRs (answers) |
| 87 0x00, 0x00, // 0 authority RRs | 89 0x00, 0x00, // 0 authority RRs |
| 88 0x00, 0x00, // 0 additional RRs | 90 0x00, 0x00, // 0 additional RRs |
| 89 | 91 |
| 90 // Question | 92 // Question |
| (...skipping 28 matching lines...) Expand all Loading... |
| 119 0x00, 0x35, | 121 0x00, 0x35, |
| 120 0x00, 0x04, // RDLENGTH is 4 bytes. | 122 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 121 ip[0], ip[1], ip[2], ip[3], // RDATA is the IP. | 123 ip[0], ip[1], ip[2], ip[3], // RDATA is the IP. |
| 122 }; | 124 }; |
| 123 | 125 |
| 124 // Create a response object and simulate reading into it. | 126 // Create a response object and simulate reading into it. |
| 125 DnsResponse r1(&q1); | 127 DnsResponse r1(&q1); |
| 126 memcpy(r1.io_buffer()->data(), &response_data[0], | 128 memcpy(r1.io_buffer()->data(), &response_data[0], |
| 127 r1.io_buffer()->size()); | 129 r1.io_buffer()->size()); |
| 128 | 130 |
| 131 // Verify resolved IPs. |
| 129 int response_size = arraysize(response_data); | 132 int response_size = arraysize(response_data); |
| 130 AddressList address_list; | 133 std::vector<IPAddressNumber> actual_ips; |
| 131 EXPECT_EQ(OK, r1.Parse(response_size, &address_list)); | 134 EXPECT_EQ(OK, r1.Parse(response_size, &actual_ips)); |
| 132 | 135 EXPECT_EQ(expected_ips, actual_ips); |
| 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 // 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 // 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 } | 136 } |
| 151 | 137 |
| 152 } // namespace net | 138 } // namespace net |
| OLD | NEW |