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/dns/dns_query.h" | 5 #include "net/dns/dns_query.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/rand_util.h" |
8 #include "net/base/dns_util.h" | 9 #include "net/base/dns_util.h" |
9 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
10 #include "net/dns/dns_protocol.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 namespace { | 15 // DNS query consists of a header followed by a question. Header format |
| 16 // and question format are described below. For the meaning of specific |
| 17 // fields, please see RFC 1035. |
16 | 18 |
17 TEST(DnsQueryTest, Constructor) { | 19 // Header format. |
18 // This includes \0 at the end. | 20 // 1 1 1 1 1 1 |
19 const char qname_data[] = "\x03""www""\x07""example""\x03""com"; | 21 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 22 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 23 // | ID | |
| 24 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 25 // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | |
| 26 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 27 // | QDCOUNT | |
| 28 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 29 // | ANCOUNT | |
| 30 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 31 // | NSCOUNT | |
| 32 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 33 // | ARCOUNT | |
| 34 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 35 |
| 36 // Question format. |
| 37 // 1 1 1 1 1 1 |
| 38 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 39 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 40 // | | |
| 41 // / QNAME / |
| 42 // / / |
| 43 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 44 // | QTYPE | |
| 45 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 46 // | QCLASS | |
| 47 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 48 |
| 49 TEST(DnsQueryTest, ConstructorTest) { |
| 50 std::string kQname("\003www\006google\003com", 16); |
| 51 DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt)); |
| 52 EXPECT_EQ(kDNS_A, q1.qtype()); |
| 53 |
| 54 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| 55 |
| 56 // See the top of the file for the description of a DNS query. |
20 const uint8 query_data[] = { | 57 const uint8 query_data[] = { |
21 // Header | 58 // Header |
22 0xbe, 0xef, | 59 id_hi, id_lo, |
23 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | 60 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
24 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | 61 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
25 // rest are 0 for a query. | 62 // rest are 0 for a query. |
26 0x00, 0x00, | 63 0x00, 0x00, |
27 0x00, 0x00, | 64 0x00, 0x00, |
28 0x00, 0x00, | 65 0x00, 0x00, |
29 | 66 |
30 // Question | 67 // Question |
31 0x03, 'w', 'w', 'w', // QNAME: www.example.com in DNS format. | 68 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. |
32 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', | 69 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, |
33 0x03, 'c', 'o', 'm', | 70 0x03, 0x63, 0x6f, 0x6d, 0x00, |
34 0x00, | |
35 | 71 |
36 0x00, 0x01, // QTYPE: A query. | 72 0x00, 0x01, // QTYPE: A query. |
37 0x00, 0x01, // QCLASS: IN class. | 73 0x00, 0x01, // QCLASS: IN class. |
38 }; | 74 }; |
39 | 75 |
40 base::StringPiece qname(qname_data, sizeof(qname_data)); | 76 int expected_size = arraysize(query_data); |
41 DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA); | 77 EXPECT_EQ(expected_size, q1.io_buffer()->size()); |
42 EXPECT_EQ(dns_protocol::kTypeA, q1.qtype()); | 78 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size)); |
43 | |
44 ASSERT_EQ(static_cast<int>(sizeof(query_data)), q1.io_buffer()->size()); | |
45 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, sizeof(query_data))); | |
46 EXPECT_EQ(qname, q1.qname()); | |
47 | |
48 base::StringPiece question(reinterpret_cast<const char*>(query_data) + 12, | |
49 21); | |
50 EXPECT_EQ(question, q1.question()); | |
51 } | 79 } |
52 | 80 |
53 TEST(DnsQueryTest, Clone) { | 81 TEST(DnsQueryTest, CloneTest) { |
54 // This includes \0 at the end. | 82 std::string kQname("\003www\006google\003com", 16); |
55 const char qname_data[] = "\x03""www""\x07""example""\x03""com"; | 83 DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt)); |
56 base::StringPiece qname(qname_data, sizeof(qname_data)); | |
57 | 84 |
58 DnsQuery q1(0, qname, dns_protocol::kTypeA); | 85 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId()); |
59 EXPECT_EQ(0, q1.id()); | |
60 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42)); | |
61 EXPECT_EQ(42, q2->id()); | |
62 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); | 86 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); |
63 EXPECT_EQ(q1.qtype(), q2->qtype()); | 87 EXPECT_EQ(q1.qtype(), q2->qtype()); |
64 EXPECT_EQ(q1.question(), q2->question()); | 88 EXPECT_EQ(q1.question_size(), q2->question_size()); |
| 89 EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(), |
| 90 q1.question_size())); |
65 } | 91 } |
66 | 92 |
67 } // namespace | 93 TEST(DnsQueryTest, RandomIdTest) { |
| 94 std::string kQname("\003www\006google\003com", 16); |
| 95 |
| 96 // Since id fields are 16-bit values, we iterate to reduce the |
| 97 // probability of collision, to avoid a flaky test. |
| 98 bool ids_are_random = false; |
| 99 for (int i = 0; i < 1000; ++i) { |
| 100 DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt)); |
| 101 DnsQuery q2(kQname, kDNS_A, base::Bind(&base::RandInt)); |
| 102 scoped_ptr<DnsQuery> q3(q1.CloneWithNewId()); |
| 103 ids_are_random = q1.id () != q2.id() && q1.id() != q3->id(); |
| 104 if (ids_are_random) |
| 105 break; |
| 106 } |
| 107 EXPECT_TRUE(ids_are_random); |
| 108 } |
68 | 109 |
69 } // namespace net | 110 } // namespace net |
OLD | NEW |