summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authoragayev@chromium.org <agayev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-28 22:15:17 +0000
committeragayev@chromium.org <agayev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-28 22:15:17 +0000
commit784008311bfbc2df0640fa81062665a016cfbe01 (patch)
treee525dd3392bca3e5ebac0f72c47cddaf0247e9fd /net/base
parentb60234a97dae0b8247b88a3cc20acb320d2fc696 (diff)
downloadchromium_src-784008311bfbc2df0640fa81062665a016cfbe01.zip
chromium_src-784008311bfbc2df0640fa81062665a016cfbe01.tar.gz
chromium_src-784008311bfbc2df0640fa81062665a016cfbe01.tar.bz2
DnsQuery: changed raw function pointer to callback.
BUG=60149 TEST=net_unittests Review URL: http://codereview.chromium.org/7276014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90844 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/dns_query.cc13
-rw-r--r--net/base/dns_query.h9
-rw-r--r--net/base/dns_query_unittest.cc15
-rw-r--r--net/base/dns_response_unittest.cc7
-rw-r--r--net/base/rand_callback.h17
5 files changed, 47 insertions, 14 deletions
diff --git a/net/base/dns_query.cc b/net/base/dns_query.cc
index 7ccf32a..f32282e 100644
--- a/net/base/dns_query.cc
+++ b/net/base/dns_query.cc
@@ -4,6 +4,7 @@
#include "net/base/dns_query.h"
+#include <limits>
#include <string>
#include "base/rand_util.h"
@@ -35,9 +36,11 @@ 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, uint64 (*prng)())
+DnsQuery::DnsQuery(const std::string& dns_name,
+ uint16 qtype,
+ const RandIntCallback& rand_int)
: dns_name_size_(dns_name.size()),
- prng_(prng) {
+ rand_int_(rand_int) {
DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(dns_name.c_str()),
dns_name.size()).DNSName(NULL));
DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA);
@@ -58,7 +61,7 @@ DnsQuery::DnsQuery(const std::string& dns_name, uint16 qtype, uint64 (*prng)())
DnsQuery::DnsQuery(const DnsQuery& rhs)
: dns_name_size_(rhs.dns_name_size_),
- prng_(rhs.prng_) {
+ rand_int_(rhs.rand_int_) {
io_buffer_ = new IOBufferWithSize(rhs.io_buffer()->size());
memcpy(io_buffer_->data(), rhs.io_buffer()->data(), rhs.io_buffer()->size());
RandomizeId();
@@ -90,7 +93,9 @@ const char* DnsQuery::question_data() const {
}
void DnsQuery::RandomizeId() {
- PackUint16BE(&io_buffer_->data()[0], (*prng_)() & 0xffff);
+ PackUint16BE(&io_buffer_->data()[0], rand_int_.Run(
+ std::numeric_limits<uint16>::min(),
+ std::numeric_limits<uint16>::max()));
}
} // namespace net
diff --git a/net/base/dns_query.h b/net/base/dns_query.h
index c0884bf..be555de 100644
--- a/net/base/dns_query.h
+++ b/net/base/dns_query.h
@@ -11,8 +11,9 @@
#include "net/base/io_buffer.h"
#include "net/base/net_api.h"
#include "net/base/net_util.h"
+#include "net/base/rand_callback.h"
-namespace net{
+namespace net {
// Represents on-the-wire DNS query message as an object.
class NET_TEST DnsQuery {
@@ -23,7 +24,9 @@ class NET_TEST 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, uint64 (*prng)());
+ DnsQuery(const std::string& dns_name,
+ uint16 qtype,
+ const RandIntCallback& rand_int);
~DnsQuery();
// Clones |this| verbatim with ID field of the header regenerated.
@@ -64,7 +67,7 @@ class NET_TEST DnsQuery {
scoped_refptr<IOBufferWithSize> io_buffer_;
// PRNG function for generating IDs.
- uint64 (*prng_)();
+ RandIntCallback rand_int_;
};
} // namespace net
diff --git a/net/base/dns_query_unittest.cc b/net/base/dns_query_unittest.cc
index 124be07..cd8ce00 100644
--- a/net/base/dns_query_unittest.cc
+++ b/net/base/dns_query_unittest.cc
@@ -2,8 +2,10 @@
// 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 "base/bind.h"
+#include "base/rand_util.h"
#include "net/base/dns_util.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -45,8 +47,9 @@ namespace net {
TEST(DnsQueryTest, ConstructorTest) {
std::string kHostnameDns("\003www\006google\003com", 16);
+ RandIntCallback rand_int_cb = base::Bind(&base::RandInt);
- DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
+ DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb);
EXPECT_EQ(kDNS_A, q1.qtype());
uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff;
@@ -78,8 +81,9 @@ TEST(DnsQueryTest, ConstructorTest) {
TEST(DnsQueryTest, CloneTest) {
std::string kHostnameDns("\003www\006google\003com", 16);
+ RandIntCallback rand_int_cb = base::Bind(&base::RandInt);
- DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
+ DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb);
scoped_ptr<DnsQuery> q2(q1.CloneWithNewId());
EXPECT_EQ(q1.io_buffer()->size(), q2->io_buffer()->size());
EXPECT_EQ(q1.qtype(), q2->qtype());
@@ -90,13 +94,14 @@ TEST(DnsQueryTest, CloneTest) {
TEST(DnsQueryTest, RandomIdTest) {
std::string kHostnameDns("\003www\006google\003com", 16);
+ RandIntCallback rand_int_cb = base::Bind(&base::RandInt);
// Since id fields are 16-bit values, we iterate to reduce the
// 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, base::RandUint64);
- DnsQuery q2(kHostnameDns, kDNS_A, base::RandUint64);
+ DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb);
+ DnsQuery q2(kHostnameDns, kDNS_A, rand_int_cb);
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 470fe63..086dad3 100644
--- a/net/base/dns_response_unittest.cc
+++ b/net/base/dns_response_unittest.cc
@@ -2,8 +2,10 @@
// 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 "base/bind.h"
+#include "base/rand_util.h"
#include "net/base/dns_util.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -70,8 +72,9 @@ namespace net {
// TODO(agayev): add more thorough tests.
TEST(DnsResponseTest, ResponseWithCnameA) {
const std::string kHostnameDns("\012codereview\010chromium\003org", 25);
+ RandIntCallback rand_int_cb = base::Bind(&base::RandInt);
- DnsQuery q1(kHostnameDns, kDNS_A, base::RandUint64);
+ DnsQuery q1(kHostnameDns, kDNS_A, rand_int_cb);
uint8 id_hi = q1.id() >> 8, id_lo = q1.id() & 0xff;
uint8 ip[] = { // codereview.chromium.org resolves to
diff --git a/net/base/rand_callback.h b/net/base/rand_callback.h
new file mode 100644
index 0000000..894b020
--- /dev/null
+++ b/net/base/rand_callback.h
@@ -0,0 +1,17 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_RAND_CALLBACK_H_
+#define NET_BASE_RAND_CALLBACK_H_
+#pragma once
+
+#include "base/callback.h"
+
+namespace net {
+
+typedef base::Callback<int(int, int)> RandIntCallback;
+
+} // namespace net
+
+#endif // NET_BASE_RAND_CALLBACK_H_