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(kClassIN, q1.qclass()); |
| 54 EXPECT_EQ(kHostname, q1.hostname()); |
| 55 |
| 56 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| 57 |
| 58 // See the top of the file for the description of a DNS query. |
| 59 const uint8 query_data[] = { |
| 60 // Header |
| 61 id_hi, id_lo, |
| 62 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
| 63 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
| 64 // rest are 0 for a query. |
| 65 0x00, 0x00, |
| 66 0x00, 0x00, |
| 67 0x00, 0x00, |
| 68 |
| 69 // Question |
| 70 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. |
| 71 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, |
| 72 0x03, 0x63, 0x6f, 0x6d, 0x00, |
| 73 |
| 74 0x00, 0x01, // QTYPE: A query |
| 75 0x00, 0x01, // QCLASS: IN class |
| 76 }; |
| 77 |
| 78 int expected_size = arraysize(query_data); |
| 79 EXPECT_EQ(expected_size, q1.size()); |
| 80 EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); |
| 81 |
| 82 // Query with a long hostname. |
| 83 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"; |
| 84 |
| 85 DnsQuery q2(hostname_too_long, ADDRESS_FAMILY_IPV4, 80); |
| 86 EXPECT_FALSE(q2.IsValid()); |
| 87 } |
| 88 |
| 89 TEST(DnsQueryTest, IOBufferAccessRandomizesIdTest) { |
| 90 const std::string kHostname = "www.google.com"; |
| 91 const int kPort = 80; |
| 92 |
| 93 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); |
| 94 EXPECT_TRUE(q1.IsValid()); |
| 95 EXPECT_EQ(kPort, q1.port()); |
| 96 EXPECT_EQ(kDNS_A, q1.qtype()); |
| 97 EXPECT_EQ(kClassIN, q1.qclass()); |
| 98 EXPECT_EQ(kHostname, q1.hostname()); |
| 99 |
| 100 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| 101 |
| 102 // See the top of the file for the description of a DNS query. |
| 103 uint8 query_data[] = { |
| 104 // Header |
| 105 id_hi, id_lo, |
| 106 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
| 107 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
| 108 // rest are 0 for a query. |
| 109 0x00, 0x00, |
| 110 0x00, 0x00, |
| 111 0x00, 0x00, |
| 112 |
| 113 // Question |
| 114 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. |
| 115 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, |
| 116 0x03, 0x63, 0x6f, 0x6d, 0x00, |
| 117 |
| 118 0x00, 0x01, // QTYPE: A query |
| 119 0x00, 0x01, // QCLASS: IN class |
| 120 }; |
| 121 |
| 122 int expected_size = arraysize(query_data); |
| 123 EXPECT_EQ(expected_size, q1.size()); |
| 124 EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); |
| 125 |
| 126 q1.io_buffer(); // Simulate a write, ID has changed. |
| 127 EXPECT_FALSE(id_hi == q1.data()[0]); |
| 128 EXPECT_FALSE(id_lo == q1.data()[1]); |
| 129 } |
| 130 |
| 131 } // namespace net |
OLD | NEW |