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/dns_query.h" | |
6 #include "net/base/dns_util.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 | |
9 namespace net { | |
10 | |
11 // DNS query consists of a header followed by a question. Header format | |
12 // and question format are described below. For the meaning of specific | |
13 // fields, please see RFC 1035. | |
14 | |
15 // Header format. | |
16 // 1 1 1 1 1 1 | |
17 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 | |
18 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
19 // | ID | | |
20 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
21 // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | | |
22 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
23 // | QDCOUNT | | |
24 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
25 // | ANCOUNT | | |
26 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
27 // | NSCOUNT | | |
28 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
29 // | ARCOUNT | | |
30 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
31 | |
32 // Question format. | |
33 // 1 1 1 1 1 1 | |
34 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 | |
35 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
36 // | | | |
37 // / QNAME / | |
38 // / / | |
39 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
40 // | QTYPE | | |
41 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
42 // | QCLASS | | |
43 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
44 | |
45 TEST(DnsQueryTest, ConstructorTest) { | |
46 const std::string kHostname = "www.google.com"; | |
47 const int kPort = 80; | |
48 | |
49 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
50 EXPECT_TRUE(q1.IsValid()); | |
51 EXPECT_EQ(kPort, q1.port()); | |
52 EXPECT_EQ(kDNS_A, q1.qtype()); | |
53 EXPECT_EQ(kHostname, q1.hostname()); | |
54 | |
55 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | |
cbentzel
2011/06/01 17:17:03
I'm pretty sure this will do a logical shift inste
agayev
2011/06/01 19:15:01
id_hi is a single byte, masking it with 0xff is a
cbentzel
2011/06/01 19:41:21
Oops, I was thinking of it as uint16. No, the mask
| |
56 | |
57 // See the top of the file for the description of a DNS query. | |
58 const uint8 query_data[] = { | |
59 // Header | |
60 id_hi, id_lo, | |
61 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | |
62 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | |
63 // rest are 0 for a query. | |
64 0x00, 0x00, | |
65 0x00, 0x00, | |
66 0x00, 0x00, | |
67 | |
68 // Question | |
69 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | |
70 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
71 0x03, 0x63, 0x6f, 0x6d, 0x00, | |
72 | |
73 0x00, 0x01, // QTYPE: A query | |
74 0x00, 0x01, // QCLASS: IN class | |
75 }; | |
76 | |
77 int expected_size = arraysize(query_data); | |
78 EXPECT_EQ(expected_size, q1.size()); | |
79 EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); | |
80 | |
81 // Query with a long hostname. | |
82 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"; | |
83 | |
84 DnsQuery q2(hostname_too_long, ADDRESS_FAMILY_IPV4, 80); | |
85 EXPECT_FALSE(q2.IsValid()); | |
86 } | |
87 | |
88 TEST(DnsQueryTest, IOBufferAccessRandomizesIdTest) { | |
89 const std::string kHostname = "www.google.com"; | |
90 const int kPort = 80; | |
91 | |
92 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
93 EXPECT_TRUE(q1.IsValid()); | |
94 EXPECT_EQ(kPort, q1.port()); | |
95 EXPECT_EQ(kDNS_A, q1.qtype()); | |
96 EXPECT_EQ(kHostname, q1.hostname()); | |
97 | |
98 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | |
99 | |
100 // See the top of the file for the description of a DNS query. | |
101 uint8 query_data[] = { | |
102 // Header | |
103 id_hi, id_lo, | |
104 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | |
105 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | |
106 // rest are 0 for a query. | |
107 0x00, 0x00, | |
108 0x00, 0x00, | |
109 0x00, 0x00, | |
110 | |
111 // Question | |
112 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | |
113 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
114 0x03, 0x63, 0x6f, 0x6d, 0x00, | |
115 | |
116 0x00, 0x01, // QTYPE: A query | |
117 0x00, 0x01, // QCLASS: IN class | |
118 }; | |
119 | |
120 int expected_size = arraysize(query_data); | |
121 EXPECT_EQ(expected_size, q1.size()); | |
122 EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); | |
123 | |
124 q1.io_buffer(); // Simulate a write, ID has changed. | |
125 EXPECT_FALSE(id_hi == q1.data()[0]); | |
cbentzel
2011/06/01 17:17:03
The bytes will occasionally match, so this test wi
agayev
2011/06/01 19:15:01
Decreased the probability of a failure to 1/2^16,
| |
126 EXPECT_FALSE(id_lo == q1.data()[1]); | |
127 } | |
128 | |
129 } // namespace net | |
OLD | NEW |