Chromium Code Reviews| Index: net/dns/dns_test_util.cc |
| diff --git a/net/dns/dns_test_util.cc b/net/dns/dns_test_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c9d9ce0c5daef6dff3ac4dc0478041fcfed8ad5c |
| --- /dev/null |
| +++ b/net/dns/dns_test_util.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright (c) 2012 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. |
| + |
| +#include <netinet/in.h> |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "net/base/big_endian.h" |
| +#include "net/base/dns_util.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/dns/dns_client.h" |
| +#include "net/dns/dns_config_service.h" |
| +#include "net/dns/dns_protocol.h" |
| +#include "net/dns/dns_query.h" |
| +#include "net/dns/dns_response.h" |
| +#include "net/dns/dns_transaction.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +// A DnsTransactionFactory which responds with 127.0.0.1 to all queries |
| +// starting with "ok", fails synchronously on all queries starting with "er", |
| +// and NXDOMAIN to all others. |
| +class MockTransactionFactory : public DnsTransactionFactory { |
|
mmenke
2012/03/12 16:23:31
nit: This should all be in an anonymous namespace
|
| + public: |
| + // Using WeakPtr to support cancellation. |
| + class MockTransaction : public DnsTransaction, |
| + public base::SupportsWeakPtr<MockTransaction> { |
|
mmenke
2012/03/12 16:23:31
nit: To make the code a little easier to follow,
|
| + public: |
| + MockTransaction(const std::string& hostname, |
| + uint16 qtype, |
| + const DnsTransactionFactory::CallbackType& callback) |
| + : hostname_(hostname), |
| + qtype_(qtype), |
| + callback_(callback), |
| + started_(false) { |
| + } |
| + |
| + virtual const std::string& GetHostname() const OVERRIDE { |
| + return hostname_; |
| + } |
| + |
| + virtual uint16 GetType() const OVERRIDE { |
| + return qtype_; |
| + } |
| + |
| + virtual int Start() OVERRIDE { |
| + EXPECT_FALSE(started_); |
| + started_ = true; |
| + if (hostname_.substr(0, 2) == "er") |
| + return ERR_NAME_NOT_RESOLVED; |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
| + return ERR_IO_PENDING; |
| + } |
| + |
| + private: |
| + void Finish() { |
| + if (hostname_.substr(0, 2) == "ok") { |
| + std::string qname; |
| + DNSDomainFromDot(hostname_, &qname); |
| + DnsQuery query(0, qname, qtype_); |
| + |
| + DnsResponse response; |
| + char* buffer = response.io_buffer()->data(); |
| + int nbytes = query.io_buffer()->size(); |
| + memcpy(buffer, query.io_buffer()->data(), nbytes); |
| + reinterpret_cast<dns_protocol::Header*>(buffer)->ancount = htons(1); |
| + |
| + const uint16 kPointerToQueryName = |
| + static_cast<uint16>(0xc000 | sizeof(net::dns_protocol::Header)); |
| + |
| + const uint32 kTTL = 86400; // One day. |
| + |
| + // Size of RDATA which is a IPv4 or IPv6 address. |
| + size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ? |
| + net::kIPv4AddressSize : net::kIPv6AddressSize; |
| + |
| + // 12 is the sum of sizes of the compressed name reference, TYPE, |
| + // CLASS, TTL and RDLENGTH. |
| + size_t answer_size = 12 + rdata_size; |
| + |
| + // Write 127.0.0.1 answer |
|
mmenke
2012/03/12 16:23:31
nit: Suggest you say "loopback address", since IP
|
| + BigEndianWriter writer(buffer + nbytes, answer_size); |
| + writer.WriteU16(kPointerToQueryName); |
| + writer.WriteU16(qtype_); |
| + writer.WriteU16(net::dns_protocol::kClassIN); |
| + writer.WriteU32(kTTL); |
| + writer.WriteU16(rdata_size); |
| + if (qtype_ == net::dns_protocol::kTypeA) |
| + writer.WriteU32(INADDR_LOOPBACK); |
| + else |
| + writer.WriteBytes(&in6addr_loopback, sizeof(in6_addr)); |
| + |
| + EXPECT_TRUE(response.InitParse(nbytes + answer_size, query)); |
| + callback_.Run(this, OK, &response); |
| + } else { |
| + callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); |
| + } |
| + } |
| + |
| + const std::string hostname_; |
| + const uint16 qtype_; |
| + DnsTransactionFactory::CallbackType callback_; |
| + bool started_; |
| + }; |
| + |
| + typedef std::pair<std::string, uint16> Key; |
| + |
| + MockTransactionFactory() : num_requests_(0) {} |
| + virtual ~MockTransactionFactory() {} |
| + |
| + virtual scoped_ptr<DnsTransaction> CreateTransaction( |
| + const std::string& hostname, |
| + uint16 qtype, |
| + const DnsTransactionFactory::CallbackType& callback, |
| + const BoundNetLog&) OVERRIDE { |
| + ++num_requests_; |
| + return scoped_ptr<DnsTransaction>( |
| + new MockTransaction(hostname, qtype, callback)); |
| + } |
| + |
| + int num_requests() const { return num_requests_; } |
| + |
| + private: |
| + int num_requests_; |
| +}; |
| + |
| +// MockDnsClient provides MockTransactionFactory. |
| +class MockDnsClient : public DnsClient { |
| + public: |
| + virtual ~MockDnsClient() {} |
| + |
| + virtual void SetConfig(const DnsConfig& config) OVERRIDE { |
| + config_ = config; |
| + } |
| + |
| + virtual const DnsConfig* GetConfig() const OVERRIDE { |
| + return config_.IsValid() ? &config_ : NULL; |
| + } |
| + |
| + virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE { |
| + return &factory_; |
| + } |
| + |
| + private: |
| + DnsConfig config_; |
| + MockTransactionFactory factory_; |
| +}; |
| + |
| + |
| +// static |
| +scoped_ptr<DnsClient> CreateMockDnsClient() { |
| + return scoped_ptr<DnsClient>(new MockDnsClient()); |
| +} |
| + |
| +} // namespace net |