Chromium Code Reviews| Index: net/base/dns_query_unittest.cc |
| diff --git a/net/base/dns_query_unittest.cc b/net/base/dns_query_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0b152491bff2266e87ce32942721d7d767d2e3f6 |
| --- /dev/null |
| +++ b/net/base/dns_query_unittest.cc |
| @@ -0,0 +1,117 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/dns_query.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +// DNS query consists of a header followed by a question. Header format |
| +// and question format are described below. For the meaning of specific |
| +// fields, please see RFC 1035. |
| + |
| +// Header format. |
| +// 1 1 1 1 1 1 |
| +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | ID | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// |QR| Opcode |AA|TC|RD|RA| Z | RCODE | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | QDCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | ANCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | NSCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | ARCOUNT | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| + |
| +// Question format. |
| +// 1 1 1 1 1 1 |
| +// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | | |
| +// / QNAME / |
| +// / / |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | QTYPE | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| +// | QCLASS | |
| +// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| + |
| + |
| +TEST(DnsQueryTest, ConstructorTest) { |
| + const char hostname[] = "www.google.com"; |
| + |
| + DnsQuery q1(hostname, ADDRESS_FAMILY_IPV4, 80); |
| + EXPECT_TRUE(q1.IsValid()); |
| + uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| + |
| + // See the top of the file for the description of a DNS query. |
| + const uint8 query_data[] = { |
| + // Header |
| + id_hi, id_lo, |
| + 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
| + 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
| + // rest are 0 for a query. |
| + 0x00, 0x00, |
| + 0x00, 0x00, |
| + 0x00, 0x00, |
| + |
| + // Question |
| + 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. |
| + 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, |
| + 0x03, 0x63, 0x6f, 0x6d, 0x00, |
| + |
| + 0x00, 0x01, // QTYPE: A query |
| + 0x00, 0x01, // QCLASS: IN class |
| + }; |
| + |
| + EXPECT_EQ(arraysize(query_data), q1.size()); |
| + EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); |
| + |
| + // Query with a long hostname. |
| + const char hostname_too_long[] = "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234"; |
| + |
| + DnsQuery q2(hostname_too_long, ADDRESS_FAMILY_IPV4, 80); |
| + EXPECT_FALSE(q2.IsValid()); |
| +} |
| + |
| +TEST(DnsQueryTest, IOBufferAccessRandomizesIdTest) { |
| + const char hostname[] = "www.google.com"; |
| + |
| + DnsQuery q1(hostname, ADDRESS_FAMILY_IPV4, 80); |
| + EXPECT_TRUE(q1.IsValid()); |
| + uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| + |
| + // See the top of the file for the description of a DNS query. |
| + uint8 query_data[] = { |
| + // Header |
| + id_hi, id_lo, |
| + 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
| + 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
| + // rest are 0 for a query. |
| + 0x00, 0x00, |
| + 0x00, 0x00, |
| + 0x00, 0x00, |
| + |
| + // Question |
| + 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. |
| + 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, |
| + 0x03, 0x63, 0x6f, 0x6d, 0x00, |
| + |
| + 0x00, 0x01, // QTYPE: A query |
| + 0x00, 0x01, // QCLASS: IN class |
| + }; |
| + |
| + EXPECT_EQ(arraysize(query_data), q1.size()); |
| + EXPECT_EQ(0, memcmp(q1.data(), query_data, q1.size())); |
| + |
| + q1.io_buffer(); // We did a write, ID has changed. |
| + EXPECT_FALSE(id_hi == q1.data()[0]); |
| + EXPECT_FALSE(id_lo == q1.data()[1]); |
| +} |
| + |
| +} // namespace net |