Chromium Code Reviews| 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 "base/bind.h" | |
| 5 #include "base/rand_util.h" | 6 #include "base/rand_util.h" |
| 6 #include "net/base/dns_query.h" | 7 #include "net/base/dns_query.h" |
|
willchan no longer on Chromium
2011/06/28 09:58:23
This header should go first.
agayev
2011/06/28 14:37:24
Done.
| |
| 7 #include "net/base/dns_util.h" | 8 #include "net/base/dns_util.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 10 |
| 10 namespace net { | 11 namespace net { |
| 11 | 12 |
| 12 // DNS query consists of a header followed by a question. Header format | 13 // DNS query consists of a header followed by a question. Header format |
| 13 // and question format are described below. For the meaning of specific | 14 // and question format are described below. For the meaning of specific |
| 14 // fields, please see RFC 1035. | 15 // fields, please see RFC 1035. |
| 15 | 16 |
| 16 // Header format. | 17 // Header format. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 38 // / QNAME / | 39 // / QNAME / |
| 39 // / / | 40 // / / |
| 40 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 41 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 41 // | QTYPE | | 42 // | QTYPE | |
| 42 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 43 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 43 // | QCLASS | | 44 // | QCLASS | |
| 44 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 45 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 45 | 46 |
| 46 TEST(DnsQueryTest, ConstructorTest) { | 47 TEST(DnsQueryTest, ConstructorTest) { |
| 47 std::string kHostnameDns("\003www\006google\003com", 16); | 48 std::string kHostnameDns("\003www\006google\003com", 16); |
| 49 base::RandIntCallback rand_int_cb = base::Bind(&base::RandInt); | |
| 48 | 50 |
| 49 DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64); | 51 DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb); |
| 50 EXPECT_EQ(kDNS_A, q1.qtype()); | 52 EXPECT_EQ(kDNS_A, q1.qtype()); |
| 51 | 53 |
| 52 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; | 54 uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff; |
| 53 | 55 |
| 54 // See the top of the file for the description of a DNS query. | 56 // See the top of the file for the description of a DNS query. |
| 55 const uint8 query_data[] = { | 57 const uint8 query_data[] = { |
| 56 // Header | 58 // Header |
| 57 id_hi, id_lo, | 59 id_hi, id_lo, |
| 58 0x01, 0x00, // Flags -- set RD (recursion desired) bit. | 60 0x01, 0x00, // Flags -- set RD (recursion desired) bit. |
| 59 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the | 61 0x00, 0x01, // Set QDCOUNT (question count) to 1, all the |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 71 0x00, 0x01, // QCLASS: IN class. | 73 0x00, 0x01, // QCLASS: IN class. |
| 72 }; | 74 }; |
| 73 | 75 |
| 74 int expected_size = arraysize(query_data); | 76 int expected_size = arraysize(query_data); |
| 75 EXPECT_EQ(expected_size, q1.io_buffer()->size()); | 77 EXPECT_EQ(expected_size, q1.io_buffer()->size()); |
| 76 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size)); | 78 EXPECT_EQ(0, memcmp(q1.io_buffer()->data(), query_data, expected_size)); |
| 77 } | 79 } |
| 78 | 80 |
| 79 TEST(DnsQueryTest, CloneTest) { | 81 TEST(DnsQueryTest, CloneTest) { |
| 80 std::string kHostnameDns("\003www\006google\003com", 16); | 82 std::string kHostnameDns("\003www\006google\003com", 16); |
| 83 base::RandIntCallback rand_int_cb = base::Bind(&base::RandInt); | |
| 81 | 84 |
| 82 DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64); | 85 DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb); |
| 83 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId()); | 86 scoped_ptr<DnsQuery> q2(q1.CloneWithNewId()); |
| 84 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); | 87 EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size()); |
| 85 EXPECT_EQ(q1.qtype(), q2->qtype()); | 88 EXPECT_EQ(q1.qtype(), q2->qtype()); |
| 86 EXPECT_EQ(q1.question_size(), q2->question_size()); | 89 EXPECT_EQ(q1.question_size(), q2->question_size()); |
| 87 EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(), | 90 EXPECT_EQ(0, memcmp(q1.question_data(), q2->question_data(), |
| 88 q1.question_size())); | 91 q1.question_size())); |
| 89 } | 92 } |
| 90 | 93 |
| 91 TEST(DnsQueryTest, RandomIdTest) { | 94 TEST(DnsQueryTest, RandomIdTest) { |
| 92 std::string kHostnameDns("\003www\006google\003com", 16); | 95 std::string kHostnameDns("\003www\006google\003com", 16); |
| 96 base::RandIntCallback rand_int_cb = base::Bind(&base::RandInt); | |
| 93 | 97 |
| 94 // Since id fields are 16-bit values, we iterate to reduce the | 98 // Since id fields are 16-bit values, we iterate to reduce the |
| 95 // probability of collision, to avoid a flaky test. | 99 // probability of collision, to avoid a flaky test. |
| 96 bool ids_are_random = false; | 100 bool ids_are_random = false; |
| 97 for (int i = 0; i < 1000; ++i) { | 101 for (int i = 0; i < 1000; ++i) { |
| 98 DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64); | 102 DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb); |
| 99 DnsQuery q2(kHostnameDns, kDNS_A, base::RandUint64); | 103 DnsQuery q2(kHostnameDns, kDNS_A, rand_int_cb); |
| 100 scoped_ptr<DnsQuery> q3(q1.CloneWithNewId()); | 104 scoped_ptr<DnsQuery> q3(q1.CloneWithNewId()); |
| 101 ids_are_random = q1.id () != q2.id() && q1.id() != q3->id(); | 105 ids_are_random = q1.id () != q2.id() && q1.id() != q3->id(); |
| 102 if (ids_are_random) | 106 if (ids_are_random) |
| 103 break; | 107 break; |
| 104 } | 108 } |
| 105 EXPECT_TRUE(ids_are_random); | 109 EXPECT_TRUE(ids_are_random); |
| 106 } | 110 } |
| 107 | 111 |
| 108 } // namespace net | 112 } // namespace net |
| OLD | NEW |