summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authoragayev@chromium.org <agayev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-16 19:21:53 +0000
committeragayev@chromium.org <agayev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-16 19:21:53 +0000
commit118989f2cda6af3e3705c5a84d0773bc52331a14 (patch)
treeffea8f37fd4122f3ee9d7ec36958ce9cbb088cad /net
parentbd8caffad1c2eeae69a7836be5ed54ec30190723 (diff)
downloadchromium_src-118989f2cda6af3e3705c5a84d0773bc52331a14.zip
chromium_src-118989f2cda6af3e3705c5a84d0773bc52331a14.tar.gz
chromium_src-118989f2cda6af3e3705c5a84d0773bc52331a14.tar.bz2
Parametrize DnsQuery over PRNG to aid testing.
BUG=60149 TEST=net_unittests Review URL: http://codereview.chromium.org/7187012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@89380 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/dns_query.cc11
-rw-r--r--net/base/dns_query.h5
-rw-r--r--net/base/dns_query_unittest.cc9
-rw-r--r--net/base/dns_response_unittest.cc3
4 files changed, 18 insertions, 10 deletions
diff --git a/net/base/dns_query.cc b/net/base/dns_query.cc
index cbc1e82..7ccf32a 100644
--- a/net/base/dns_query.cc
+++ b/net/base/dns_query.cc
@@ -35,8 +35,9 @@ static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static const size_t kHeaderSize = arraysize(kHeader);
-DnsQuery::DnsQuery(const std::string& dns_name, uint16 qtype)
- : dns_name_size_(dns_name.size()) {
+DnsQuery::DnsQuery(const std::string& dns_name, uint16 qtype, uint64 (*prng)())
+ : dns_name_size_(dns_name.size()),
+ prng_(prng) {
DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(dns_name.c_str()),
dns_name.size()).DNSName(NULL));
DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA);
@@ -55,7 +56,9 @@ DnsQuery::DnsQuery(const std::string& dns_name, uint16 qtype)
RandomizeId();
}
-DnsQuery::DnsQuery(const DnsQuery& rhs) : dns_name_size_(rhs.dns_name_size_) {
+DnsQuery::DnsQuery(const DnsQuery& rhs)
+ : dns_name_size_(rhs.dns_name_size_),
+ prng_(rhs.prng_) {
io_buffer_ = new IOBufferWithSize(rhs.io_buffer()->size());
memcpy(io_buffer_->data(), rhs.io_buffer()->data(), rhs.io_buffer()->size());
RandomizeId();
@@ -87,7 +90,7 @@ const char* DnsQuery::question_data() const {
}
void DnsQuery::RandomizeId() {
- PackUint16BE(&io_buffer_->data()[0], base::RandUint64() & 0xffff);
+ PackUint16BE(&io_buffer_->data()[0], (*prng_)() & 0xffff);
}
} // namespace net
diff --git a/net/base/dns_query.h b/net/base/dns_query.h
index 71b260c..dcc7c0c 100644
--- a/net/base/dns_query.h
+++ b/net/base/dns_query.h
@@ -22,7 +22,7 @@ class DnsQuery {
// Every generated object has a random ID, hence two objects generated
// with the same set of constructor arguments are generally not equal;
// there is a 1/2^16 chance of them being equal due to size of |id_|.
- DnsQuery(const std::string& dns_name, uint16 qtype);
+ DnsQuery(const std::string& dns_name, uint16 qtype, uint64 (*prng)());
~DnsQuery();
// Clones |this| verbatim with ID field of the header regenerated.
@@ -61,6 +61,9 @@ class DnsQuery {
// Contains query bytes to be consumed by higher level Write() call.
scoped_refptr<IOBufferWithSize> io_buffer_;
+
+ // PRNG function for generating IDs.
+ uint64 (*prng_)();
};
} // namespace net
diff --git a/net/base/dns_query_unittest.cc b/net/base/dns_query_unittest.cc
index 7e5e75e..124be07 100644
--- a/net/base/dns_query_unittest.cc
+++ b/net/base/dns_query_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/rand_util.h"
#include "net/base/dns_query.h"
#include "net/base/dns_util.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -45,7 +46,7 @@ namespace net {
TEST(DnsQueryTest, ConstructorTest) {
std::string kHostnameDns("\003www\006google\003com", 16);
- DnsQuery q1(kHostnameDns, kDNS_A);
+ DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
EXPECT_EQ(kDNS_A, q1.qtype());
uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff;
@@ -78,7 +79,7 @@ TEST(DnsQueryTest, ConstructorTest) {
TEST(DnsQueryTest, CloneTest) {
std::string kHostnameDns("\003www\006google\003com", 16);
- DnsQuery q1(kHostnameDns, kDNS_A);
+ DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
scoped_ptr<DnsQuery> q2(q1.CloneWithNewId());
EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
EXPECT_EQ(q1.qtype(), q2->qtype());
@@ -94,8 +95,8 @@ TEST(DnsQueryTest, RandomIdTest) {
// probability of collision, to avoid a flaky test.
bool ids_are_random = false;
for (int i = 0; i < 1000; ++i) {
- DnsQuery q1(kHostnameDns, kDNS_A);
- DnsQuery q2(kHostnameDns, kDNS_A);
+ DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
+ DnsQuery q2(kHostnameDns, kDNS_A, base::RandUint64);
scoped_ptr<DnsQuery> q3(q1.CloneWithNewId());
ids_are_random = q1.id () != q2.id() && q1.id() != q3->id();
if (ids_are_random)
diff --git a/net/base/dns_response_unittest.cc b/net/base/dns_response_unittest.cc
index 6dd790e..470fe63 100644
--- a/net/base/dns_response_unittest.cc
+++ b/net/base/dns_response_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/rand_util.h"
#include "net/base/dns_response.h"
#include "net/base/dns_util.h"
#include "net/base/net_errors.h"
@@ -70,7 +71,7 @@ namespace net {
TEST(DnsResponseTest, ResponseWithCnameA) {
const std::string kHostnameDns("\012codereview\010chromium\003org", 25);
- DnsQuery q1(kHostnameDns, kDNS_A);
+ DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff;
uint8 ip[] = { // codereview.chromium.org resolves to