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

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

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

Powered by Google App Engine
This is Rietveld 408576698