Chromium Code Reviews| 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, RandomIdTest) { | |
| 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 DnsQuery q2(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
| 56 EXPECT_TRUE(q2.IsValid()); | |
| 57 EXPECT_EQ(kPort, q2.port()); | |
| 58 EXPECT_EQ(kDNS_A, q2.qtype()); | |
| 59 EXPECT_EQ(kHostname, q2.hostname()); | |
| 60 | |
| 61 // This has a 1/2^16 probability of failure. | |
| 62 EXPECT_FALSE(q1.id() == q2.id()); | |
| 63 } | |
| 64 | |
| 65 TEST(DnsQueryTest, ConstructorTest) { | |
| 66 const std::string kHostname = "www.google.com"; | |
| 67 const int kPort = 80; | |
| 68 | |
| 69 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
| 70 EXPECT_TRUE(q1.IsValid()); | |
| 71 EXPECT_EQ(kPort, q1.port()); | |
| 72 EXPECT_EQ(kDNS_A, q1.qtype()); | |
| 73 EXPECT_EQ(kHostname, q1.hostname()); | |
| 74 | |
| 75 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | |
| 76 | |
| 77 // See the top of the file for the description of a DNS query. | |
| 78 const uint8 query_data[] = { | |
| 79 // Header | |
| 80 id_hi, id_lo, | |
| 81 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | |
| 82 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | |
| 83 // rest are 0 for a query. | |
| 84 0x00, 0x00, | |
| 85 0x00, 0x00, | |
| 86 0x00, 0x00, | |
| 87 | |
| 88 // Question | |
| 89 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | |
| 90 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
| 91 0x03, 0x63, 0x6f, 0x6d, 0x00, | |
| 92 | |
| 93 0x00, 0x01, // QTYPE: A query | |
| 94 0x00, 0x01, // QCLASS: IN class | |
| 95 }; | |
| 96 | |
| 97 int expected_size = arraysize(query_data); | |
| 98 EXPECT_EQ(expected_size, q1.io_buffer()->size()); | |
| 99 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, | |
| 100 q1.io_buffer()->size())); | |
| 101 | |
| 102 // Query with a long hostname. | |
| 103 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"; | |
| 104 | |
| 105 DnsQuery q2(hostname_too_long, ADDRESS_FAMILY_IPV4, 80); | |
| 106 EXPECT_FALSE(q2.IsValid()); | |
| 107 } | |
| 108 | |
| 109 TEST(DnsQueryTest, IOBufferAccessRandomizesIdTest) { | |
|
cbentzel
2011/06/02 21:27:13
Remove this test.
agayev
2011/06/02 22:33:26
Done.
| |
| 110 const std::string kHostname = "www.google.com"; | |
| 111 const int kPort = 80; | |
| 112 | |
| 113 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
| 114 EXPECT_TRUE(q1.IsValid()); | |
| 115 EXPECT_EQ(kPort, q1.port()); | |
| 116 EXPECT_EQ(kDNS_A, q1.qtype()); | |
| 117 EXPECT_EQ(kHostname, q1.hostname()); | |
| 118 | |
| 119 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | |
| 120 | |
| 121 // See the top of the file for the description of a DNS query. | |
| 122 uint8 query_data[] = { | |
| 123 // Header | |
| 124 id_hi, id_lo, | |
| 125 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | |
| 126 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | |
| 127 // rest are 0 for a query. | |
| 128 0x00, 0x00, | |
| 129 0x00, 0x00, | |
| 130 0x00, 0x00, | |
| 131 | |
| 132 // Question | |
| 133 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | |
| 134 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | |
| 135 0x03, 0x63, 0x6f, 0x6d, 0x00, | |
| 136 | |
| 137 0x00, 0x01, // QTYPE: A query | |
| 138 0x00, 0x01, // QCLASS: IN class | |
| 139 }; | |
| 140 | |
| 141 int expected_size = arraysize(query_data); | |
| 142 EXPECT_EQ(expected_size, q1.io_buffer()->size()); | |
| 143 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, | |
| 144 q1.io_buffer()->size())); | |
| 145 } | |
| 146 | |
| 147 } // namespace net | |
| OLD | NEW |