dns_query_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/dns/dns_query.h"
6
7#include "base/bind.h"
8#include "net/base/dns_util.h"
9#include "net/base/io_buffer.h"
10#include "net/dns/dns_protocol.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace net {
14
15namespace {
16
17TEST(DnsQueryTest, Constructor) {
18  // This includes \0 at the end.
19  const char qname_data[] = "\x03""www""\x07""example""\x03""com";
20  const uint8 query_data[] = {
21    // Header
22    0xbe, 0xef,
23    0x01, 0x00,               // Flags -- set RD (recursion desired) bit.
24    0x00, 0x01,               // Set QDCOUNT (question count) to 1, all the
25                              // rest are 0 for a query.
26    0x00, 0x00,
27    0x00, 0x00,
28    0x00, 0x00,
29
30    // Question
31    0x03, 'w', 'w', 'w',      // QNAME: www.example.com in DNS format.
32    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
33    0x03, 'c', 'o', 'm',
34    0x00,
35
36    0x00, 0x01,               // QTYPE: A query.
37    0x00, 0x01,               // QCLASS: IN class.
38  };
39
40  base::StringPiece qname(qname_data, sizeof(qname_data));
41  DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA);
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());
51}
52
53TEST(DnsQueryTest, Clone) {
54  // This includes \0 at the end.
55  const char qname_data[] = "\x03""www""\x07""example""\x03""com";
56  base::StringPiece qname(qname_data, sizeof(qname_data));
57
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());
62  EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
63  EXPECT_EQ(q1.qtype(), q2->qtype());
64  EXPECT_EQ(q1.question(), q2->question());
65}
66
67}  // namespace
68
69}  // namespace net
70