summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_query_unittest.cc
diff options
context:
space:
mode:
authorszym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-08 19:29:15 +0000
committerszym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-08 19:29:15 +0000
commit7556ea2db8b28d15a37d5680febadc5cf1a5cc5b (patch)
treef9bb844166850fca5a4c64adf61361e6b20a2032 /net/dns/dns_query_unittest.cc
parent334e0cd1ff440365b1f3b28630362ede5548f400 (diff)
downloadchromium_src-7556ea2db8b28d15a37d5680febadc5cf1a5cc5b.zip
chromium_src-7556ea2db8b28d15a37d5680febadc5cf1a5cc5b.tar.gz
chromium_src-7556ea2db8b28d15a37d5680febadc5cf1a5cc5b.tar.bz2
Isolates generic DnsClient from AsyncHostResolver.
DnsClient provides a generic DNS client that allows fetching resource records. DnsClient is very lightweight and does not support aggregation, queuing or prioritization of requests. This is the first CL in a series to merge AsyncHostResolver into HostResolverImpl. Also introduces general-purpose BigEndianReader/Writer. Removes DnsTransactionTest-related suppressions. BUG=90881,80225,106688 TEST=./net_unittests Review URL: http://codereview.chromium.org/8852009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113640 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/dns/dns_query_unittest.cc')
-rw-r--r--net/dns/dns_query_unittest.cc103
1 files changed, 31 insertions, 72 deletions
diff --git a/net/dns/dns_query_unittest.cc b/net/dns/dns_query_unittest.cc
index d43cf8be..ffe02e7 100644
--- a/net/dns/dns_query_unittest.cc
+++ b/net/dns/dns_query_unittest.cc
@@ -5,58 +5,21 @@
#include "net/dns/dns_query.h"
#include "base/bind.h"
-#include "base/rand_util.h"
#include "net/base/dns_util.h"
#include "net/base/io_buffer.h"
+#include "net/dns/dns_protocol.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.
+namespace {
-// 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) {
- std::string kQname("\003www\006google\003com", 16);
- DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt));
- EXPECT_EQ(kDNS_A, q1.qtype());
-
- uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff;
-
- // See the top of the file for the description of a DNS query.
+TEST(DnsQueryTest, Constructor) {
+ // This includes \0 at the end.
+ const char qname_data[] = "\x03""www""\x07""example""\x03""com";
const uint8 query_data[] = {
// Header
- id_hi, id_lo,
+ 0xbe, 0xef,
0x01, 0x00, // Flags -- set RD (recursion desired) bit.
0x00, 0x01, // Set QDCOUNT (question count) to 1, all the
// rest are 0 for a query.
@@ -65,46 +28,42 @@ TEST(DnsQueryTest, ConstructorTest) {
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,
+ 0x03, 'w', 'w', 'w', // QNAME: www.example.com in DNS format.
+ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
+ 0x03, 'c', 'o', 'm',
+ 0x00,
0x00, 0x01, // QTYPE: A query.
0x00, 0x01, // QCLASS: IN class.
};
- int expected_size = arraysize(query_data);
- EXPECT_EQ(expected_size, q1.io_buffer()->size());
- EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size));
+ base::StringPiece qname(qname_data, sizeof(qname_data));
+ DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA);
+ EXPECT_EQ(dns_protocol::kTypeA, q1.qtype());
+
+ ASSERT_EQ(static_cast<int>(sizeof(query_data)), q1.io_buffer()->size());
+ EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, sizeof(query_data)));
+ EXPECT_EQ(qname, q1.qname());
+
+ base::StringPiece question(reinterpret_cast<const char*>(query_data) + 12,
+ 21);
+ EXPECT_EQ(question, q1.question());
}
-TEST(DnsQueryTest, CloneTest) {
- std::string kQname("\003www\006google\003com", 16);
- DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt));
+TEST(DnsQueryTest, Clone) {
+ // This includes \0 at the end.
+ const char qname_data[] = "\x03""www""\x07""example""\x03""com";
+ base::StringPiece qname(qname_data, sizeof(qname_data));
- scoped_ptr<DnsQuery> q2(q1.CloneWithNewId());
+ DnsQuery q1(0, qname, dns_protocol::kTypeA);
+ EXPECT_EQ(0, q1.id());
+ scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42));
+ EXPECT_EQ(42, q2->id());
EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
EXPECT_EQ(q1.qtype(), q2->qtype());
- EXPECT_EQ(q1.question_size(), q2->question_size());
- EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(),
- q1.question_size()));
+ EXPECT_EQ(q1.question(), q2->question());
}
-TEST(DnsQueryTest, RandomIdTest) {
- std::string kQname("\003www\006google\003com", 16);
-
- // Since id fields are 16-bit values, we iterate to reduce the
- // probability of collision, to avoid a flaky test.
- bool ids_are_random = false;
- for (int i = 0; i < 1000; ++i) {
- DnsQuery q1(kQname, kDNS_A, base::Bind(&base::RandInt));
- DnsQuery q2(kQname, kDNS_A, base::Bind(&base::RandInt));
- scoped_ptr<DnsQuery> q3(q1.CloneWithNewId());
- ids_are_random = q1.id () != q2.id() && q1.id() != q3->id();
- if (ids_are_random)
- break;
- }
- EXPECT_TRUE(ids_are_random);
-}
+} // namespace
} // namespace net