15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/dns_query.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/dns_util.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/io_buffer.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/dns_protocol.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(DnsQueryTest, Constructor) {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This includes \0 at the end.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char qname_data[] = "\x03""www""\x07""example""\x03""com";
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const uint8 query_data[] = {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Header
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0xbe, 0xef,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x01, 0x00,               // Flags -- set RD (recursion desired) bit.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00, 0x01,               // Set QDCOUNT (question count) to 1, all the
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              // rest are 0 for a query.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00, 0x00,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00, 0x00,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00, 0x00,
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Question
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x03, 'w', 'w', 'w',      // QNAME: www.example.com in DNS format.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x03, 'c', 'o', 'm',
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00, 0x01,               // QTYPE: A query.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    0x00, 0x01,               // QCLASS: IN class.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringPiece qname(qname_data, sizeof(qname_data));
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(dns_protocol::kTypeA, q1.qtype());
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(static_cast<int>(sizeof(query_data)), q1.io_buffer()->size());
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, sizeof(query_data)));
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(qname, q1.qname());
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringPiece question(reinterpret_cast<const char*>(query_data) + 12,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             21);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(question, q1.question());
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(DnsQueryTest, Clone) {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This includes \0 at the end.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char qname_data[] = "\x03""www""\x07""example""\x03""com";
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringPiece qname(qname_data, sizeof(qname_data));
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DnsQuery q1(0, qname, dns_protocol::kTypeA);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, q1.id());
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42));
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(42, q2->id());
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(q1.qtype(), q2->qtype());
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(q1.question(), q2->question());
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
70