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