[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 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 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 9 | #include "base/big_endian.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/memory/weak_ptr.h" |
[email protected] | 5ee2098 | 2013-07-17 21:51:18 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 13 | #include "base/sys_byteorder.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 14 | #include "net/base/dns_util.h" |
| 15 | #include "net/base/io_buffer.h" |
| 16 | #include "net/base/net_errors.h" |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 17 | #include "net/dns/address_sorter.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 18 | #include "net/dns/dns_query.h" |
| 19 | #include "net/dns/dns_response.h" |
| 20 | #include "net/dns/dns_transaction.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | namespace net { |
| 24 | namespace { |
| 25 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 26 | class MockAddressSorter : public AddressSorter { |
| 27 | public: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 28 | ~MockAddressSorter() override {} |
| 29 | void Sort(const AddressList& list, |
| 30 | const CallbackType& callback) const override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 31 | // Do nothing. |
| 32 | callback.Run(true, list); |
| 33 | } |
| 34 | }; |
| 35 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 36 | // A DnsTransaction which uses MockDnsClientRuleList to determine the response. |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 37 | class MockTransaction : public DnsTransaction, |
| 38 | public base::SupportsWeakPtr<MockTransaction> { |
| 39 | public: |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 40 | MockTransaction(const MockDnsClientRuleList& rules, |
| 41 | const std::string& hostname, |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 42 | uint16 qtype, |
| 43 | const DnsTransactionFactory::CallbackType& callback) |
[email protected] | a210eef9 | 2013-07-19 19:06:12 | [diff] [blame] | 44 | : result_(MockDnsClientRule::FAIL), |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 45 | hostname_(hostname), |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 46 | qtype_(qtype), |
| 47 | callback_(callback), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 48 | started_(false), |
| 49 | delayed_(false) { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 50 | // Find the relevant rule which matches |qtype| and prefix of |hostname|. |
| 51 | for (size_t i = 0; i < rules.size(); ++i) { |
| 52 | const std::string& prefix = rules[i].prefix; |
| 53 | if ((rules[i].qtype == qtype) && |
| 54 | (hostname.size() >= prefix.size()) && |
| 55 | (hostname.compare(0, prefix.size(), prefix) == 0)) { |
| 56 | result_ = rules[i].result; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 57 | delayed_ = rules[i].delay; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 58 | break; |
| 59 | } |
| 60 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 61 | } |
| 62 | |
nick | d3f30d02 | 2015-04-23 10:18:37 | [diff] [blame^] | 63 | const std::string& GetHostname() const override { return hostname_; } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 64 | |
nick | d3f30d02 | 2015-04-23 10:18:37 | [diff] [blame^] | 65 | uint16 GetType() const override { return qtype_; } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 66 | |
nick | d3f30d02 | 2015-04-23 10:18:37 | [diff] [blame^] | 67 | void Start() override { |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 68 | EXPECT_FALSE(started_); |
| 69 | started_ = true; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 70 | if (delayed_) |
| 71 | return; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 72 | // Using WeakPtr to cleanly cancel when transaction is destroyed. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 73 | base::MessageLoop::current()->PostTask( |
| 74 | FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 77 | void FinishDelayedTransaction() { |
| 78 | EXPECT_TRUE(delayed_); |
| 79 | delayed_ = false; |
| 80 | Finish(); |
| 81 | } |
| 82 | |
| 83 | bool delayed() const { return delayed_; } |
| 84 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 85 | private: |
| 86 | void Finish() { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 87 | switch (result_) { |
| 88 | case MockDnsClientRule::EMPTY: |
| 89 | case MockDnsClientRule::OK: { |
| 90 | std::string qname; |
| 91 | DNSDomainFromDot(hostname_, &qname); |
| 92 | DnsQuery query(0, qname, qtype_); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 93 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 94 | DnsResponse response; |
| 95 | char* buffer = response.io_buffer()->data(); |
| 96 | int nbytes = query.io_buffer()->size(); |
| 97 | memcpy(buffer, query.io_buffer()->data(), nbytes); |
| 98 | dns_protocol::Header* header = |
| 99 | reinterpret_cast<dns_protocol::Header*>(buffer); |
| 100 | header->flags |= dns_protocol::kFlagResponse; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 101 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 102 | if (MockDnsClientRule::OK == result_) { |
| 103 | const uint16 kPointerToQueryName = |
| 104 | static_cast<uint16>(0xc000 | sizeof(*header)); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 105 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 106 | const uint32 kTTL = 86400; // One day. |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 107 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 108 | // Size of RDATA which is a IPv4 or IPv6 address. |
| 109 | size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ? |
| 110 | net::kIPv4AddressSize : net::kIPv6AddressSize; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 111 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 112 | // 12 is the sum of sizes of the compressed name reference, TYPE, |
| 113 | // CLASS, TTL and RDLENGTH. |
| 114 | size_t answer_size = 12 + rdata_size; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 115 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 116 | // Write answer with loopback IP address. |
| 117 | header->ancount = base::HostToNet16(1); |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 118 | base::BigEndianWriter writer(buffer + nbytes, answer_size); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 119 | writer.WriteU16(kPointerToQueryName); |
| 120 | writer.WriteU16(qtype_); |
| 121 | writer.WriteU16(net::dns_protocol::kClassIN); |
| 122 | writer.WriteU32(kTTL); |
| 123 | writer.WriteU16(rdata_size); |
| 124 | if (qtype_ == net::dns_protocol::kTypeA) { |
| 125 | char kIPv4Loopback[] = { 0x7f, 0, 0, 1 }; |
| 126 | writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback)); |
| 127 | } else { |
| 128 | char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, |
| 129 | 0, 0, 0, 0, 0, 0, 0, 1 }; |
| 130 | writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback)); |
| 131 | } |
| 132 | nbytes += answer_size; |
| 133 | } |
| 134 | EXPECT_TRUE(response.InitParse(nbytes, query)); |
| 135 | callback_.Run(this, OK, &response); |
| 136 | } break; |
[email protected] | a210eef9 | 2013-07-19 19:06:12 | [diff] [blame] | 137 | case MockDnsClientRule::FAIL: |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 138 | callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL); |
| 139 | break; |
[email protected] | 4817d12 | 2012-12-13 02:21:59 | [diff] [blame] | 140 | case MockDnsClientRule::TIMEOUT: |
| 141 | callback_.Run(this, ERR_DNS_TIMED_OUT, NULL); |
| 142 | break; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 143 | default: |
| 144 | NOTREACHED(); |
| 145 | break; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 149 | MockDnsClientRule::Result result_; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 150 | const std::string hostname_; |
| 151 | const uint16 qtype_; |
| 152 | DnsTransactionFactory::CallbackType callback_; |
| 153 | bool started_; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 154 | bool delayed_; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 155 | }; |
| 156 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 157 | } // namespace |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 158 | |
| 159 | // A DnsTransactionFactory which creates MockTransaction. |
| 160 | class MockTransactionFactory : public DnsTransactionFactory { |
| 161 | public: |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 162 | explicit MockTransactionFactory(const MockDnsClientRuleList& rules) |
| 163 | : rules_(rules) {} |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 164 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 165 | ~MockTransactionFactory() override {} |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 166 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 167 | scoped_ptr<DnsTransaction> CreateTransaction( |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 168 | const std::string& hostname, |
| 169 | uint16 qtype, |
| 170 | const DnsTransactionFactory::CallbackType& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 171 | const BoundNetLog&) override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 172 | MockTransaction* transaction = |
| 173 | new MockTransaction(rules_, hostname, qtype, callback); |
| 174 | if (transaction->delayed()) |
| 175 | delayed_transactions_.push_back(transaction->AsWeakPtr()); |
| 176 | return scoped_ptr<DnsTransaction>(transaction); |
| 177 | } |
| 178 | |
| 179 | void CompleteDelayedTransactions() { |
| 180 | DelayedTransactionList old_delayed_transactions; |
| 181 | old_delayed_transactions.swap(delayed_transactions_); |
| 182 | for (DelayedTransactionList::iterator it = old_delayed_transactions.begin(); |
| 183 | it != old_delayed_transactions.end(); ++it) { |
| 184 | if (it->get()) |
| 185 | (*it)->FinishDelayedTransaction(); |
| 186 | } |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 190 | typedef std::vector<base::WeakPtr<MockTransaction> > DelayedTransactionList; |
| 191 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 192 | MockDnsClientRuleList rules_; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 193 | DelayedTransactionList delayed_transactions_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 194 | }; |
| 195 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 196 | MockDnsClient::MockDnsClient(const DnsConfig& config, |
| 197 | const MockDnsClientRuleList& rules) |
| 198 | : config_(config), |
| 199 | factory_(new MockTransactionFactory(rules)), |
| 200 | address_sorter_(new MockAddressSorter()) { |
| 201 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 202 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 203 | MockDnsClient::~MockDnsClient() {} |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 204 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 205 | void MockDnsClient::SetConfig(const DnsConfig& config) { |
| 206 | config_ = config; |
| 207 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 208 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 209 | const DnsConfig* MockDnsClient::GetConfig() const { |
| 210 | return config_.IsValid() ? &config_ : NULL; |
| 211 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 212 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 213 | DnsTransactionFactory* MockDnsClient::GetTransactionFactory() { |
| 214 | return config_.IsValid() ? factory_.get() : NULL; |
| 215 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 216 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 217 | AddressSorter* MockDnsClient::GetAddressSorter() { |
| 218 | return address_sorter_.get(); |
| 219 | } |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 220 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 221 | void MockDnsClient::CompleteDelayedTransactions() { |
| 222 | factory_->CompleteDelayedTransactions(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 225 | } // namespace net |