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/base/dns_query.h" | 5 #include "net/base/dns_query.h" |
6 #include "net/base/dns_util.h" | 6 #include "net/base/dns_util.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace net { | 9 namespace net { |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... | |
35 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 35 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
36 // | | | 36 // | | |
37 // / QNAME / | 37 // / QNAME / |
38 // / / | 38 // / / |
39 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 39 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
40 // | QTYPE | | 40 // | QTYPE | |
41 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 41 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
42 // | QCLASS | | 42 // | QCLASS | |
43 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 43 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
44 | 44 |
45 TEST(DnsQueryTest, RandomIdTest) { | 45 TEST(DnsQueryTest, ConstructorTest) { |
46 const std::string kHostname = "www.google.com"; | 46 std::string kHostnameDns("\003www\006google\003com", 16); |
47 const uint16 kPort = 80; | |
48 | 47 |
49 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | 48 DnsQuery q1(kHostnameDns, kDNS_A); |
50 EXPECT_TRUE(q1.IsValid()); | |
51 EXPECT_EQ(kPort, q1.port()); | |
52 EXPECT_EQ(kDNS_A, q1.qtype()); | 49 EXPECT_EQ(kDNS_A, q1.qtype()); |
53 EXPECT_EQ(kHostname, q1.hostname()); | |
54 | |
55 DnsQuery q2(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
56 EXPECT_TRUE(q2.IsValid()); | |
57 EXPECT_EQ(kPort, q2.port()); | |
58 EXPECT_EQ(kDNS_A, q2.qtype()); | |
59 EXPECT_EQ(kHostname, q2.hostname()); | |
60 | |
61 // This has a 1/2^16 probability of failure. | |
62 EXPECT_FALSE(q1.id() == q2.id()); | |
63 } | |
64 | |
65 TEST(DnsQueryTest, ConstructorTest) { | |
66 const std::string kHostname = "www.google.com"; | |
67 const uint16 kPort = 80; | |
68 | |
69 DnsQuery q1(kHostname, ADDRESS_FAMILY_IPV4, kPort); | |
70 EXPECT_TRUE(q1.IsValid()); | |
71 EXPECT_EQ(kPort, q1.port()); | |
72 EXPECT_EQ(kDNS_A, q1.qtype()); | |
73 EXPECT_EQ(kHostname, q1.hostname()); | |
74 | 50 |
75 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | 51 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
76 | 52 |
77 // See the top of the file for the description of a DNS query. | 53 // See the top of the file for the description of a DNS query. |
78 const uint8 query_data[] = { | 54 const uint8 query_data[] = { |
79 // Header | 55 // Header |
80 id_hi, id_lo, | 56 id_hi, id_lo, |
81 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | 57 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
82 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | 58 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
83 // rest are 0 for a query. | 59 // rest are 0 for a query. |
84 0x00, 0x00, | 60 0x00, 0x00, |
85 0x00, 0x00, | 61 0x00, 0x00, |
86 0x00, 0x00, | 62 0x00, 0x00, |
87 | 63 |
88 // Question | 64 // Question |
89 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. | 65 0x03, 0x77, 0x77, 0x77, // QNAME: www.google.com in DNS format. |
90 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, | 66 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, |
91 0x03, 0x63, 0x6f, 0x6d, 0x00, | 67 0x03, 0x63, 0x6f, 0x6d, 0x00, |
92 | 68 |
93 0x00, 0x01, // QTYPE: A query. | 69 0x00, 0x01, // QTYPE: A query. |
94 0x00, 0x01, // QCLASS: IN class. | 70 0x00, 0x01, // QCLASS: IN class. |
95 }; | 71 }; |
96 | 72 |
97 int expected_size = arraysize(query_data); | 73 int expected_size = arraysize(query_data); |
98 EXPECT_EQ(expected_size, q1.io_buffer()->size()); | 74 EXPECT_EQ(expected_size, q1.io_buffer()->size()); |
99 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, | 75 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size)); |
100 q1.io_buffer()->size())); | 76 } |
101 | 77 |
102 // Query with a long hostname. | 78 TEST(DnsQueryTest, CloneTest) { |
103 const char hostname_too_long[] = "123456789.123456789.123456789.123456789.1234 56789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234 56789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234 56789.123456789.123456789.123456789.123456789.1234"; | 79 std::string kHostnameDns("\003www\006google\003com", 16); |
104 | 80 |
105 DnsQuery q2(hostname_too_long, ADDRESS_FAMILY_IPV4, kPort); | 81 DnsQuery q1(kHostnameDns, kDNS_A); |
106 EXPECT_FALSE(q2.IsValid()); | 82 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId()); |
83 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); | |
84 EXPECT_EQ(q1.qtype(), q2->qtype()); | |
85 EXPECT_EQ(q1.question_size(), q2->question_size()); | |
86 EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(), | |
87 q1.question_size())); | |
88 } | |
89 | |
90 TEST(DnsQueryTest, RandomIdTest) { | |
91 std::string kHostnameDns("\003www\006google\003com", 16); | |
92 | |
93 DnsQuery q1(kHostnameDns, kDNS_A); | |
94 DnsQuery q2(kHostnameDns, kDNS_A); | |
95 | |
96 // Each of the following equality tests have a 1/2^16 probability of | |
97 // failure. | |
98 EXPECT_FALSE(q1.id() == q2.id()); | |
cbentzel
2011/06/09 01:16:06
I still don't like this test because it _will_ fai
agayev
2011/06/09 14:23:02
We don't have an API for setting a seed to get a d
| |
99 | |
100 scoped_ptr<DnsQuery> q3(q1.CloneWithNewId()); | |
101 EXPECT_FALSE(q1.id() == q3->id()); | |
107 } | 102 } |
108 | 103 |
109 } // namespace net | 104 } // namespace net |
OLD | NEW |