blob: 6327ed259539645c5a84b2a816717535cadf9b2f [file] [log] [blame]
[email protected]78eac2a2012-03-14 19:09:271// 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]d9806a972014-02-26 18:14:579#include "base/big_endian.h"
[email protected]78eac2a2012-03-14 19:09:2710#include "base/bind.h"
11#include "base/memory/weak_ptr.h"
[email protected]5ee20982013-07-17 21:51:1812#include "base/message_loop/message_loop.h"
[email protected]78eac2a2012-03-14 19:09:2713#include "base/sys_byteorder.h"
[email protected]78eac2a2012-03-14 19:09:2714#include "net/base/dns_util.h"
15#include "net/base/io_buffer.h"
16#include "net/base/net_errors.h"
[email protected]0adcb2b2012-08-15 21:30:4617#include "net/dns/address_sorter.h"
[email protected]78eac2a2012-03-14 19:09:2718#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
23namespace net {
24namespace {
25
[email protected]daae1322013-09-05 18:26:5026class MockAddressSorter : public AddressSorter {
27 public:
28 virtual ~MockAddressSorter() {}
29 virtual void Sort(const AddressList& list,
mostynbba063d6032014-10-09 11:01:1330 const CallbackType& callback) const override {
[email protected]daae1322013-09-05 18:26:5031 // Do nothing.
32 callback.Run(true, list);
33 }
34};
35
[email protected]0adcb2b2012-08-15 21:30:4636// A DnsTransaction which uses MockDnsClientRuleList to determine the response.
[email protected]78eac2a2012-03-14 19:09:2737class MockTransaction : public DnsTransaction,
38 public base::SupportsWeakPtr<MockTransaction> {
39 public:
[email protected]0adcb2b2012-08-15 21:30:4640 MockTransaction(const MockDnsClientRuleList& rules,
41 const std::string& hostname,
[email protected]78eac2a2012-03-14 19:09:2742 uint16 qtype,
43 const DnsTransactionFactory::CallbackType& callback)
[email protected]a210eef92013-07-19 19:06:1244 : result_(MockDnsClientRule::FAIL),
[email protected]0adcb2b2012-08-15 21:30:4645 hostname_(hostname),
[email protected]78eac2a2012-03-14 19:09:2746 qtype_(qtype),
47 callback_(callback),
[email protected]daae1322013-09-05 18:26:5048 started_(false),
49 delayed_(false) {
[email protected]0adcb2b2012-08-15 21:30:4650 // 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]daae1322013-09-05 18:26:5057 delayed_ = rules[i].delay;
[email protected]0adcb2b2012-08-15 21:30:4658 break;
59 }
60 }
[email protected]78eac2a2012-03-14 19:09:2761 }
62
mostynbba063d6032014-10-09 11:01:1363 virtual const std::string& GetHostname() const override {
[email protected]78eac2a2012-03-14 19:09:2764 return hostname_;
65 }
66
mostynbba063d6032014-10-09 11:01:1367 virtual uint16 GetType() const override {
[email protected]78eac2a2012-03-14 19:09:2768 return qtype_;
69 }
70
mostynbba063d6032014-10-09 11:01:1371 virtual void Start() override {
[email protected]78eac2a2012-03-14 19:09:2772 EXPECT_FALSE(started_);
73 started_ = true;
[email protected]daae1322013-09-05 18:26:5074 if (delayed_)
75 return;
[email protected]78eac2a2012-03-14 19:09:2776 // Using WeakPtr to cleanly cancel when transaction is destroyed.
[email protected]2da659e2013-05-23 20:51:3477 base::MessageLoop::current()->PostTask(
78 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr()));
[email protected]78eac2a2012-03-14 19:09:2779 }
80
[email protected]daae1322013-09-05 18:26:5081 void FinishDelayedTransaction() {
82 EXPECT_TRUE(delayed_);
83 delayed_ = false;
84 Finish();
85 }
86
87 bool delayed() const { return delayed_; }
88
[email protected]78eac2a2012-03-14 19:09:2789 private:
90 void Finish() {
[email protected]0adcb2b2012-08-15 21:30:4691 switch (result_) {
92 case MockDnsClientRule::EMPTY:
93 case MockDnsClientRule::OK: {
94 std::string qname;
95 DNSDomainFromDot(hostname_, &qname);
96 DnsQuery query(0, qname, qtype_);
[email protected]78eac2a2012-03-14 19:09:2797
[email protected]0adcb2b2012-08-15 21:30:4698 DnsResponse response;
99 char* buffer = response.io_buffer()->data();
100 int nbytes = query.io_buffer()->size();
101 memcpy(buffer, query.io_buffer()->data(), nbytes);
102 dns_protocol::Header* header =
103 reinterpret_cast<dns_protocol::Header*>(buffer);
104 header->flags |= dns_protocol::kFlagResponse;
[email protected]78eac2a2012-03-14 19:09:27105
[email protected]0adcb2b2012-08-15 21:30:46106 if (MockDnsClientRule::OK == result_) {
107 const uint16 kPointerToQueryName =
108 static_cast<uint16>(0xc000 | sizeof(*header));
[email protected]78eac2a2012-03-14 19:09:27109
[email protected]0adcb2b2012-08-15 21:30:46110 const uint32 kTTL = 86400; // One day.
[email protected]78eac2a2012-03-14 19:09:27111
[email protected]0adcb2b2012-08-15 21:30:46112 // Size of RDATA which is a IPv4 or IPv6 address.
113 size_t rdata_size = qtype_ == net::dns_protocol::kTypeA ?
114 net::kIPv4AddressSize : net::kIPv6AddressSize;
[email protected]78eac2a2012-03-14 19:09:27115
[email protected]0adcb2b2012-08-15 21:30:46116 // 12 is the sum of sizes of the compressed name reference, TYPE,
117 // CLASS, TTL and RDLENGTH.
118 size_t answer_size = 12 + rdata_size;
[email protected]78eac2a2012-03-14 19:09:27119
[email protected]0adcb2b2012-08-15 21:30:46120 // Write answer with loopback IP address.
121 header->ancount = base::HostToNet16(1);
[email protected]d9806a972014-02-26 18:14:57122 base::BigEndianWriter writer(buffer + nbytes, answer_size);
[email protected]0adcb2b2012-08-15 21:30:46123 writer.WriteU16(kPointerToQueryName);
124 writer.WriteU16(qtype_);
125 writer.WriteU16(net::dns_protocol::kClassIN);
126 writer.WriteU32(kTTL);
127 writer.WriteU16(rdata_size);
128 if (qtype_ == net::dns_protocol::kTypeA) {
129 char kIPv4Loopback[] = { 0x7f, 0, 0, 1 };
130 writer.WriteBytes(kIPv4Loopback, sizeof(kIPv4Loopback));
131 } else {
132 char kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0,
133 0, 0, 0, 0, 0, 0, 0, 1 };
134 writer.WriteBytes(kIPv6Loopback, sizeof(kIPv6Loopback));
135 }
136 nbytes += answer_size;
137 }
138 EXPECT_TRUE(response.InitParse(nbytes, query));
139 callback_.Run(this, OK, &response);
140 } break;
[email protected]a210eef92013-07-19 19:06:12141 case MockDnsClientRule::FAIL:
[email protected]0adcb2b2012-08-15 21:30:46142 callback_.Run(this, ERR_NAME_NOT_RESOLVED, NULL);
143 break;
[email protected]4817d122012-12-13 02:21:59144 case MockDnsClientRule::TIMEOUT:
145 callback_.Run(this, ERR_DNS_TIMED_OUT, NULL);
146 break;
[email protected]0adcb2b2012-08-15 21:30:46147 default:
148 NOTREACHED();
149 break;
[email protected]78eac2a2012-03-14 19:09:27150 }
151 }
152
[email protected]0adcb2b2012-08-15 21:30:46153 MockDnsClientRule::Result result_;
[email protected]78eac2a2012-03-14 19:09:27154 const std::string hostname_;
155 const uint16 qtype_;
156 DnsTransactionFactory::CallbackType callback_;
157 bool started_;
[email protected]daae1322013-09-05 18:26:50158 bool delayed_;
[email protected]78eac2a2012-03-14 19:09:27159};
160
[email protected]daae1322013-09-05 18:26:50161} // namespace
[email protected]78eac2a2012-03-14 19:09:27162
163// A DnsTransactionFactory which creates MockTransaction.
164class MockTransactionFactory : public DnsTransactionFactory {
165 public:
[email protected]0adcb2b2012-08-15 21:30:46166 explicit MockTransactionFactory(const MockDnsClientRuleList& rules)
167 : rules_(rules) {}
[email protected]daae1322013-09-05 18:26:50168
[email protected]78eac2a2012-03-14 19:09:27169 virtual ~MockTransactionFactory() {}
170
171 virtual scoped_ptr<DnsTransaction> CreateTransaction(
172 const std::string& hostname,
173 uint16 qtype,
174 const DnsTransactionFactory::CallbackType& callback,
mostynbba063d6032014-10-09 11:01:13175 const BoundNetLog&) override {
[email protected]daae1322013-09-05 18:26:50176 MockTransaction* transaction =
177 new MockTransaction(rules_, hostname, qtype, callback);
178 if (transaction->delayed())
179 delayed_transactions_.push_back(transaction->AsWeakPtr());
180 return scoped_ptr<DnsTransaction>(transaction);
181 }
182
183 void CompleteDelayedTransactions() {
184 DelayedTransactionList old_delayed_transactions;
185 old_delayed_transactions.swap(delayed_transactions_);
186 for (DelayedTransactionList::iterator it = old_delayed_transactions.begin();
187 it != old_delayed_transactions.end(); ++it) {
188 if (it->get())
189 (*it)->FinishDelayedTransaction();
190 }
[email protected]0adcb2b2012-08-15 21:30:46191 }
192
193 private:
[email protected]daae1322013-09-05 18:26:50194 typedef std::vector<base::WeakPtr<MockTransaction> > DelayedTransactionList;
195
[email protected]0adcb2b2012-08-15 21:30:46196 MockDnsClientRuleList rules_;
[email protected]daae1322013-09-05 18:26:50197 DelayedTransactionList delayed_transactions_;
[email protected]0adcb2b2012-08-15 21:30:46198};
199
[email protected]daae1322013-09-05 18:26:50200MockDnsClient::MockDnsClient(const DnsConfig& config,
201 const MockDnsClientRuleList& rules)
202 : config_(config),
203 factory_(new MockTransactionFactory(rules)),
204 address_sorter_(new MockAddressSorter()) {
205}
[email protected]78eac2a2012-03-14 19:09:27206
[email protected]daae1322013-09-05 18:26:50207MockDnsClient::~MockDnsClient() {}
[email protected]78eac2a2012-03-14 19:09:27208
[email protected]daae1322013-09-05 18:26:50209void MockDnsClient::SetConfig(const DnsConfig& config) {
210 config_ = config;
211}
[email protected]78eac2a2012-03-14 19:09:27212
[email protected]daae1322013-09-05 18:26:50213const DnsConfig* MockDnsClient::GetConfig() const {
214 return config_.IsValid() ? &config_ : NULL;
215}
[email protected]78eac2a2012-03-14 19:09:27216
[email protected]daae1322013-09-05 18:26:50217DnsTransactionFactory* MockDnsClient::GetTransactionFactory() {
218 return config_.IsValid() ? factory_.get() : NULL;
219}
[email protected]78eac2a2012-03-14 19:09:27220
[email protected]daae1322013-09-05 18:26:50221AddressSorter* MockDnsClient::GetAddressSorter() {
222 return address_sorter_.get();
223}
[email protected]0adcb2b2012-08-15 21:30:46224
[email protected]daae1322013-09-05 18:26:50225void MockDnsClient::CompleteDelayedTransactions() {
226 factory_->CompleteDelayedTransactions();
[email protected]78eac2a2012-03-14 19:09:27227}
228
[email protected]78eac2a2012-03-14 19:09:27229} // namespace net