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

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

Issue 8852009: Isolates generic DnsClient from AsyncHostResolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: and again Created 9 years 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/dns/dns_query.cc ('k') | net/dns/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
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"
9 #include "net/base/dns_util.h" 8 #include "net/base/dns_util.h"
10 #include "net/base/io_buffer.h" 9 #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 // DNS query consists of a header followed by a question. Header format 15 namespace {
16 // and question format are described below. For the meaning of specific
17 // fields, please see RFC 1035.
18 16
19 // Header format. 17 TEST(DnsQueryTest, Constructor) {
20 // 1 1 1 1 1 1 18 // This includes \0 at the end.
21 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 19 const char qname_data[] = "\x03""www""\x07""example""\x03""com";
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.
57 const uint8 query_data[] = { 20 const uint8 query_data[] = {
58 // Header 21 // Header
59 id_hi, id_lo, 22 0xbe, 0xef,
60 0x01, 0x00, // Flags -- set RD (recursion desired) bit. 23 0x01, 0x00, // Flags -- set RD (recursion desired) bit.
61 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the 24 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the
62 // rest are 0 for a query. 25 // rest are 0 for a query.
63 0x00, 0x00, 26 0x00, 0x00,
64 0x00, 0x00, 27 0x00, 0x00,
65 0x00, 0x00, 28 0x00, 0x00,
66 29
67 // Question 30 // Question
68 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. 31 0x03, 'w', 'w', 'w', // QNAME: www.example.com in DNS format.
69 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 32 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
70 0x03, 0x63, 0x6f, 0x6d, 0x00, 33 0x03, 'c', 'o', 'm',
34 0x00,
71 35
72 0x00, 0x01, // QTYPE: A query. 36 0x00, 0x01, // QTYPE: A query.
73 0x00, 0x01, // QCLASS: IN class. 37 0x00, 0x01, // QCLASS: IN class.
74 }; 38 };
75 39
76 int expected_size = arraysize(query_data); 40 base::StringPiece qname(qname_data, sizeof(qname_data));
77 EXPECT_EQ(expected_size, q1.io_buffer()->size()); 41 DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA);
78 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size)); 42 EXPECT_EQ(dns_protocol::kTypeA, q1.qtype());
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());
79 } 51 }
80 52
81 TEST(DnsQueryTest, CloneTest) { 53 TEST(DnsQueryTest, Clone) {
82 std::string kQname("\003www\006google\003com", 16); 54 // This includes \0 at the end.
83 DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt)); 55 const char qname_data[] = "\x03""www""\x07""example""\x03""com";
56 base::StringPiece qname(qname_data, sizeof(qname_data));
84 57
85 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId()); 58 DnsQuery q1(0, qname, dns_protocol::kTypeA);
59 EXPECT_EQ(0, q1.id());
60 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42));
61 EXPECT_EQ(42, q2->id());
86 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); 62 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
87 EXPECT_EQ(q1.qtype(), q2->qtype()); 63 EXPECT_EQ(q1.qtype(), q2->qtype());
88 EXPECT_EQ(q1.question_size(), q2->question_size()); 64 EXPECT_EQ(q1.question(), q2->question());
89 EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(),
90 q1.question_size()));
91 } 65 }
92 66
93 TEST(DnsQueryTest, RandomIdTest) { 67 } // namespace
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 }
109 68
110 } // namespace net 69 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_query.cc ('k') | net/dns/dns_response.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698