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