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

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: uintX_t -> uintX 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"
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 TEST(DnsQueryTest, Constructor) {
16 // and question format are described below. For the meaning of specific 16 // This includes \0 at the end.
17 // fields, please see RFC 1035. 17 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[] = { 18 const uint8 query_data[] = {
58 // Header 19 // Header
59 id_hi, id_lo, 20 0xbe, 0xef,
60 0x01, 0x00, // Flags -- set RD (recursion desired) bit. 21 0x01, 0x00, // Flags -- set RD (recursion desired) bit.
61 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the 22 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the
62 // rest are 0 for a query. 23 // rest are 0 for a query.
63 0x00, 0x00, 24 0x00, 0x00,
64 0x00, 0x00, 25 0x00, 0x00,
65 0x00, 0x00, 26 0x00, 0x00,
66 27
67 // Question 28 // Question
68 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. 29 0x03, 'w', 'w', 'w', // QNAME: www.example.com in DNS format.
69 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 30 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
70 0x03, 0x63, 0x6f, 0x6d, 0x00, 31 0x03, 'c', 'o', 'm',
32 0x00,
71 33
72 0x00, 0x01, // QTYPE: A query. 34 0x00, 0x01, // QTYPE: A query.
73 0x00, 0x01, // QCLASS: IN class. 35 0x00, 0x01, // QCLASS: IN class.
74 }; 36 };
75 37
76 int expected_size = arraysize(query_data); 38 base::StringPiece qname(qname_data, sizeof(qname_data));
77 EXPECT_EQ(expected_size, q1.io_buffer()->size()); 39 DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA);
78 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size)); 40 EXPECT_EQ(dns_protocol::kTypeA, q1.qtype());
41
42 ASSERT_EQ(static_cast<int>(sizeof(query_data)), q1.io_buffer()->size());
43 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, sizeof(query_data)));
44 EXPECT_EQ(qname, q1.qname());
45
46 base::StringPiece question(reinterpret_cast<const char*>(query_data) + 12,
47 21);
48 EXPECT_EQ(question, q1.question());
79 } 49 }
80 50
81 TEST(DnsQueryTest, CloneTest) { 51 TEST(DnsQueryTest, Clone) {
82 std::string kQname("\003www\006google\003com", 16); 52 // This includes \0 at the end.
83 DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt)); 53 const char qname_data[] = "\x03""www""\x07""example""\x03""com";
54 base::StringPiece qname(qname_data, sizeof(qname_data));
84 55
85 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId()); 56 DnsQuery q1(0, qname, dns_protocol::kTypeA);
57 EXPECT_EQ(0, q1.id());
58 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42));
59 EXPECT_EQ(42, q2->id());
86 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); 60 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
87 EXPECT_EQ(q1.qtype(), q2->qtype()); 61 EXPECT_EQ(q1.qtype(), q2->qtype());
88 EXPECT_EQ(q1.question_size(), q2->question_size()); 62 EXPECT_EQ(q1.question(), q2->question());
89 EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(),
90 q1.question_size()));
91 } 63 }
92 64
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 65
110 } // namespace net 66 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698