| OLD | NEW |
| 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 "net/base/dns_util.h" | 8 #include "net/base/dns_util.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/dns/dns_protocol.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 namespace { | 15 namespace { |
| 16 | 16 |
| 17 TEST(DnsQueryTest, Constructor) { | 17 TEST(DnsQueryTest, Constructor) { |
| 18 // This includes \0 at the end. | 18 // This includes \0 at the end. |
| 19 const char qname_data[] = "\x03""www""\x07""example""\x03""com"; | 19 const char qname_data[] = |
| 20 "\x03" |
| 21 "www" |
| 22 "\x07" |
| 23 "example" |
| 24 "\x03" |
| 25 "com"; |
| 20 const uint8 query_data[] = { | 26 const uint8 query_data[] = { |
| 21 // Header | 27 // Header |
| 22 0xbe, 0xef, | 28 0xbe, 0xef, 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
| 23 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | 29 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
| 24 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | 30 // rest are 0 for a query. |
| 25 // rest are 0 for a query. | 31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 26 0x00, 0x00, | |
| 27 0x00, 0x00, | |
| 28 0x00, 0x00, | |
| 29 | 32 |
| 30 // Question | 33 // Question |
| 31 0x03, 'w', 'w', 'w', // QNAME: www.example.com in DNS format. | 34 0x03, 'w', 'w', 'w', // QNAME: www.example.com in DNS format. |
| 32 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', | 35 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 33 0x03, 'c', 'o', 'm', | 36 0x03, 'c', 'o', 'm', 0x00, 0x00, 0x01, // QTYPE: A query. |
| 34 0x00, | 37 0x00, 0x01, // QCLASS: IN class. |
| 35 | |
| 36 0x00, 0x01, // QTYPE: A query. | |
| 37 0x00, 0x01, // QCLASS: IN class. | |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 base::StringPiece qname(qname_data, sizeof(qname_data)); | 40 base::StringPiece qname(qname_data, sizeof(qname_data)); |
| 41 DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA); | 41 DnsQuery q1(0xbeef, qname, dns_protocol::kTypeA); |
| 42 EXPECT_EQ(dns_protocol::kTypeA, q1.qtype()); | 42 EXPECT_EQ(dns_protocol::kTypeA, q1.qtype()); |
| 43 | 43 |
| 44 ASSERT_EQ(static_cast<int>(sizeof(query_data)), q1.io_buffer()->size()); | 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))); | 45 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, sizeof(query_data))); |
| 46 EXPECT_EQ(qname, q1.qname()); | 46 EXPECT_EQ(qname, q1.qname()); |
| 47 | 47 |
| 48 base::StringPiece question(reinterpret_cast<const char*>(query_data) + 12, | 48 base::StringPiece question(reinterpret_cast<const char*>(query_data) + 12, |
| 49 21); | 49 21); |
| 50 EXPECT_EQ(question, q1.question()); | 50 EXPECT_EQ(question, q1.question()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 TEST(DnsQueryTest, Clone) { | 53 TEST(DnsQueryTest, Clone) { |
| 54 // This includes \0 at the end. | 54 // This includes \0 at the end. |
| 55 const char qname_data[] = "\x03""www""\x07""example""\x03""com"; | 55 const char qname_data[] = |
| 56 "\x03" |
| 57 "www" |
| 58 "\x07" |
| 59 "example" |
| 60 "\x03" |
| 61 "com"; |
| 56 base::StringPiece qname(qname_data, sizeof(qname_data)); | 62 base::StringPiece qname(qname_data, sizeof(qname_data)); |
| 57 | 63 |
| 58 DnsQuery q1(0, qname, dns_protocol::kTypeA); | 64 DnsQuery q1(0, qname, dns_protocol::kTypeA); |
| 59 EXPECT_EQ(0, q1.id()); | 65 EXPECT_EQ(0, q1.id()); |
| 60 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42)); | 66 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId(42)); |
| 61 EXPECT_EQ(42, q2->id()); | 67 EXPECT_EQ(42, q2->id()); |
| 62 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); | 68 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); |
| 63 EXPECT_EQ(q1.qtype(), q2->qtype()); | 69 EXPECT_EQ(q1.qtype(), q2->qtype()); |
| 64 EXPECT_EQ(q1.question(), q2->question()); | 70 EXPECT_EQ(q1.question(), q2->question()); |
| 65 } | 71 } |
| 66 | 72 |
| 67 } // namespace | 73 } // namespace |
| 68 | 74 |
| 69 } // namespace net | 75 } // namespace net |
| OLD | NEW |