Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(848)

Side by Side Diff: net/base/dns_query_unittest.cc

Issue 7008021: Added DnsQuery class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge with trunk Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/dns_query.cc ('k') | net/base/dns_response.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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, kPort);
106 EXPECT_FALSE(q2.IsValid());
107 }
108
109 } // namespace net
OLDNEW
« no previous file with comments | « net/base/dns_query.cc ('k') | net/base/dns_response.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698