OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/dns/dns_test_util.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/sys_byteorder.h" |
| 13 #include "net/base/big_endian.h" |
| 14 #include "net/base/dns_util.h" |
| 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "net/dns/dns_client.h" |
| 18 #include "net/dns/dns_config_service.h" |
| 19 #include "net/dns/dns_protocol.h" |
| 20 #include "net/dns/dns_query.h" |
| 21 #include "net/dns/dns_response.h" |
| 22 #include "net/dns/dns_transaction.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace net { |
| 26 namespace { |
| 27 |
| 28 // A DnsTransaction which responds with loopback to all queries starting with |
| 29 // "ok", fails synchronously on all queries starting with "er", and NXDOMAIN to |
| 30 // all others. |
| 31 class MockTransaction : public DnsTransaction, |
| 32 public base::SupportsWeakPtr<MockTransaction> { |
| 33 public: |
| 34 MockTransaction(const std::string& hostname, |
| 35 uint16 qtype, |
| 36 const DnsTransactionFactory::CallbackType& callback) |
| 37 : hostname_(hostname), |
| 38 qtype_(qtype), |
| 39 callback_(callback), |
| 40 started_(false) { |
| 41 } |
| 42 |
| 43 virtual const std::string& GetHostname() const OVERRIDE { |
| 44 return hostname_; |
| 45 } |
| 46 |
| 47 virtual uint16 GetType() const OVERRIDE { |
| 48 return qtype_; |
| 49 } |
| 50 |
| 51 virtual int Start() OVERRIDE { |
| 52 EXPECT_FALSE(started_); |
| 53 started_ = true; |
| 54 if (hostname_.substr(0, 2) == "er") |
| 55 return ERR_NAME_NOT_RESOLVED; |
| 56 // Using WeakPtr to cleanly cancel when transaction is destroyed. |
| 57 MessageLoop::current()->PostTask( |
| 58 FROM_HERE, |
| 59 base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
| 60 return ERR_IO_PENDING; |
| 61 } |
| 62 |
| 63 private: |
| 64 void Finish() { |
| 65 if (hostname_.substr(0, 2) == "ok") { |
| 66 std::string qname; |
| 67 DNSDomainFromDot(hostname_, &qname); |
| 68 DnsQuery query(0, qname, qtype_); |
| 69 |
| 70 DnsResponse response; |
| 71 char* buffer = response.io_buffer()->data(); |
| 72 int nbytes = query.io_buffer()->size(); |
| 73 memcpy(buffer, query.io_buffer()->data(), nbytes); |
| 74 |
| 75 const uint16 kPointerToQueryName = |
| 76 static_cast<uint16>(0xc000 | sizeof(net::dns_protocol::Header)); |
| 77 |
| 78 const uint32 kTTL = 86400; // One day. |
| 79 |
| 80 // Size of RDATA which is a IPv4 or IPv6 address. |
| 81 size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ? |
| 82 net::kIPv4AddressSize : net::kIPv6AddressSize; |
| 83 |
| 84 // 12 is the sum of sizes of the compressed name reference, TYPE, |
| 85 // CLASS, TTL and RDLENGTH. |
| 86 size_t answer_size = 12 + rdata_size; |
| 87 |
| 88 // Write answer with loopback IP address. |
| 89 reinterpret_cast<dns_protocol::Header*>(buffer)->ancount = htons(1); |
| 90 BigEndianWriter writer(buffer + nbytes, answer_size); |
| 91 writer.WriteU16(kPointerToQueryName); |
| 92 writer.WriteU16(qtype_); |
| 93 writer.WriteU16(net::dns_protocol::kClassIN); |
| 94 writer.WriteU32(kTTL); |
| 95 writer.WriteU16(rdata_size); |
| 96 if (qtype_ == net::dns_protocol::kTypeA) { |
| 97 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; |
| 98 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); |
| 99 } else { |
| 100 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, |
| 101 0, 0, 0, 0, 0, 0, 0, 1 }; |
| 102 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); |
| 103 } |
| 104 |
| 105 EXPECT_TRUE(response.InitParse(nbytes + answer_size, query)); |
| 106 callback_.Run(this, OK, &response); |
| 107 } else { |
| 108 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); |
| 109 } |
| 110 } |
| 111 |
| 112 const std::string hostname_; |
| 113 const uint16 qtype_; |
| 114 DnsTransactionFactory::CallbackType callback_; |
| 115 bool started_; |
| 116 }; |
| 117 |
| 118 |
| 119 // A DnsTransactionFactory which creates MockTransaction. |
| 120 class MockTransactionFactory : public DnsTransactionFactory { |
| 121 public: |
| 122 MockTransactionFactory() {} |
| 123 virtual ~MockTransactionFactory() {} |
| 124 |
| 125 virtual scoped_ptr<DnsTransaction> CreateTransaction( |
| 126 const std::string& hostname, |
| 127 uint16 qtype, |
| 128 const DnsTransactionFactory::CallbackType& callback, |
| 129 const BoundNetLog&) OVERRIDE { |
| 130 return scoped_ptr<DnsTransaction>( |
| 131 new MockTransaction(hostname, qtype, callback)); |
| 132 } |
| 133 }; |
| 134 |
| 135 // MockDnsClient provides MockTransactionFactory. |
| 136 class MockDnsClient : public DnsClient { |
| 137 public: |
| 138 explicit MockDnsClient(const DnsConfig& config) : config_(config) {} |
| 139 virtual ~MockDnsClient() {} |
| 140 |
| 141 virtual void SetConfig(const DnsConfig& config) OVERRIDE { |
| 142 config_ = config; |
| 143 } |
| 144 |
| 145 virtual const DnsConfig* GetConfig() const OVERRIDE { |
| 146 return config_.IsValid() ? &config_ : NULL; |
| 147 } |
| 148 |
| 149 virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE { |
| 150 return config_.IsValid() ? &factory_ : NULL; |
| 151 } |
| 152 |
| 153 private: |
| 154 DnsConfig config_; |
| 155 MockTransactionFactory factory_; |
| 156 }; |
| 157 |
| 158 } // namespace |
| 159 |
| 160 // static |
| 161 scoped_ptr<DnsClient> CreateMockDnsClient(const DnsConfig& config) { |
| 162 return scoped_ptr<DnsClient>(new MockDnsClient(config)); |
| 163 } |
| 164 |
| 165 } // namespace net |
| 166 |
OLD | NEW |