OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
cbentzel
2011/05/26 17:32:40
Wrong year
agayev
2011/05/26 22:55:55
Done.
| |
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/dns_query.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | |
7 | |
8 namespace net { | |
9 | |
10 // DNS query consists of a header followed by a question. Header format | |
11 // and question format are described below. For the meaning of specific | |
12 // fields, please see RFC 1035. | |
13 | |
14 // Header format. | |
15 // 1 1 1 1 1 1 | |
16 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 | |
17 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
18 // | ID | | |
19 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
20 // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | | |
21 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
22 // | QDCOUNT | | |
23 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
24 // | ANCOUNT | | |
25 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
26 // | NSCOUNT | | |
27 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
28 // | ARCOUNT | | |
29 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
30 | |
31 // Question format. | |
32 // 1 1 1 1 1 1 | |
33 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 | |
34 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
35 // | | | |
36 // / QNAME / | |
37 // / / | |
38 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
39 // | QTYPE | | |
40 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
41 // | QCLASS | | |
42 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
43 | |
44 | |
45 TEST(DnsQueryTest, ConstructorTest) { | |
46 const char hostname[] = "www.google.com"; | |
47 | |
48 DnsQuery q1(hostname, ADDRESS_FAMILY_IPV4, 80); | |
49 EXPECT_TRUE(q1.IsValid()); | |
50 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | |
51 | |
52 // See the top of the file for the description of a DNS query. | |
53 const uint8 query_data[] = { | |
54 // Header | |
55 id_hi, id_lo, | |
56 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | |
57 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | |
58 // rest are 0 for a query. | |
59 0x00, 0x00, | |
60 0x00, 0x00, | |
61 0x00, 0x00, | |
62 | |
63 // Question | |
64 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | |
65 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
66 0x03, 0x63, 0x6f, 0x6d, 0x00, | |
67 | |
68 0x00, 0x01, // QTYPE: A query | |
69 0x00, 0x01, // QCLASS: IN class | |
70 }; | |
71 | |
72 EXPECT_EQ(arraysize(query_data), q1.size()); | |
73 EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); | |
74 | |
75 // Query with a long hostname. | |
76 const char hostname_too_long[] = "123456789.123456789.123456789.123456789.1234 56789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234 56789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234 56789.123456789.123456789.123456789.123456789.1234"; | |
77 | |
78 DnsQuery q2(hostname_too_long, ADDRESS_FAMILY_IPV4, 80); | |
79 EXPECT_FALSE(q2.IsValid()); | |
80 } | |
81 | |
82 TEST(DnsQueryTest, IOBufferAccessRandomizesIdTest) { | |
83 const char hostname[] = "www.google.com"; | |
84 | |
85 DnsQuery q1(hostname, ADDRESS_FAMILY_IPV4, 80); | |
86 EXPECT_TRUE(q1.IsValid()); | |
87 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | |
88 | |
89 // See the top of the file for the description of a DNS query. | |
90 uint8 query_data[] = { | |
91 // Header | |
92 id_hi, id_lo, | |
93 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | |
94 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | |
95 // rest are 0 for a query. | |
96 0x00, 0x00, | |
97 0x00, 0x00, | |
98 0x00, 0x00, | |
99 | |
100 // Question | |
101 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | |
102 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
103 0x03, 0x63, 0x6f, 0x6d, 0x00, | |
104 | |
105 0x00, 0x01, // QTYPE: A query | |
106 0x00, 0x01, // QCLASS: IN class | |
107 }; | |
108 | |
109 EXPECT_EQ(arraysize(query_data), q1.size()); | |
110 EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); | |
111 | |
112 q1.io_buffer(); // We did a write, ID has changed. | |
113 EXPECT_FALSE(id_hi == q1.data()[0]); | |
114 EXPECT_FALSE(id_lo == q1.data()[1]); | |
115 } | |
116 | |
117 } // namespace net | |
OLD | NEW |