Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: trunk/src/net/dns/dns_test_util.cc

Issue 23102009: Revert 218616 "[net/dns] Perform A/AAAA queries for AF_UNSPEC re..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « trunk/src/net/dns/dns_test_util.h ('k') | trunk/src/net/dns/host_resolver_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Deleted: svn:mergeinfo
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/dns_test_util.h" 5 #include "net/dns/dns_test_util.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/sys_byteorder.h" 12 #include "base/sys_byteorder.h"
13 #include "net/base/big_endian.h" 13 #include "net/base/big_endian.h"
14 #include "net/base/dns_util.h" 14 #include "net/base/dns_util.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
17 #include "net/dns/address_sorter.h" 17 #include "net/dns/address_sorter.h"
18 #include "net/dns/dns_client.h"
19 #include "net/dns/dns_config_service.h"
20 #include "net/dns/dns_protocol.h"
18 #include "net/dns/dns_query.h" 21 #include "net/dns/dns_query.h"
19 #include "net/dns/dns_response.h" 22 #include "net/dns/dns_response.h"
20 #include "net/dns/dns_transaction.h" 23 #include "net/dns/dns_transaction.h"
21 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
22 25
23 namespace net { 26 namespace net {
24 namespace { 27 namespace {
25 28
26 class MockAddressSorter : public AddressSorter {
27 public:
28 virtual ~MockAddressSorter() {}
29 virtual void Sort(const AddressList& list,
30 const CallbackType& callback) const OVERRIDE {
31 // Do nothing.
32 callback.Run(true, list);
33 }
34 };
35
36 // A DnsTransaction which uses MockDnsClientRuleList to determine the response. 29 // A DnsTransaction which uses MockDnsClientRuleList to determine the response.
37 class MockTransaction : public DnsTransaction, 30 class MockTransaction : public DnsTransaction,
38 public base::SupportsWeakPtr<MockTransaction> { 31 public base::SupportsWeakPtr<MockTransaction> {
39 public: 32 public:
40 MockTransaction(const MockDnsClientRuleList& rules, 33 MockTransaction(const MockDnsClientRuleList& rules,
41 const std::string& hostname, 34 const std::string& hostname,
42 uint16 qtype, 35 uint16 qtype,
43 const DnsTransactionFactory::CallbackType& callback) 36 const DnsTransactionFactory::CallbackType& callback)
44 : result_(MockDnsClientRule::FAIL), 37 : result_(MockDnsClientRule::FAIL),
45 hostname_(hostname), 38 hostname_(hostname),
46 qtype_(qtype), 39 qtype_(qtype),
47 callback_(callback), 40 callback_(callback),
48 started_(false), 41 started_(false) {
49 delayed_(false) {
50 // Find the relevant rule which matches |qtype| and prefix of |hostname|. 42 // Find the relevant rule which matches |qtype| and prefix of |hostname|.
51 for (size_t i = 0; i < rules.size(); ++i) { 43 for (size_t i = 0; i < rules.size(); ++i) {
52 const std::string& prefix = rules[i].prefix; 44 const std::string& prefix = rules[i].prefix;
53 if ((rules[i].qtype == qtype) && 45 if ((rules[i].qtype == qtype) &&
54 (hostname.size() >= prefix.size()) && 46 (hostname.size() >= prefix.size()) &&
55 (hostname.compare(0, prefix.size(), prefix) == 0)) { 47 (hostname.compare(0, prefix.size(), prefix) == 0)) {
56 result_ = rules[i].result; 48 result_ = rules[i].result;
57 delayed_ = rules[i].delay;
58 break; 49 break;
59 } 50 }
60 } 51 }
61 } 52 }
62 53
63 virtual const std::string& GetHostname() const OVERRIDE { 54 virtual const std::string& GetHostname() const OVERRIDE {
64 return hostname_; 55 return hostname_;
65 } 56 }
66 57
67 virtual uint16 GetType() const OVERRIDE { 58 virtual uint16 GetType() const OVERRIDE {
68 return qtype_; 59 return qtype_;
69 } 60 }
70 61
71 virtual void Start() OVERRIDE { 62 virtual void Start() OVERRIDE {
72 EXPECT_FALSE(started_); 63 EXPECT_FALSE(started_);
73 started_ = true; 64 started_ = true;
74 if (delayed_)
75 return;
76 // Using WeakPtr to cleanly cancel when transaction is destroyed. 65 // Using WeakPtr to cleanly cancel when transaction is destroyed.
77 base::MessageLoop::current()->PostTask( 66 base::MessageLoop::current()->PostTask(
78 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); 67 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr()));
79 } 68 }
80 69
81 void FinishDelayedTransaction() {
82 EXPECT_TRUE(delayed_);
83 delayed_ = false;
84 Finish();
85 }
86
87 bool delayed() const { return delayed_; }
88
89 private: 70 private:
90 void Finish() { 71 void Finish() {
91 switch (result_) { 72 switch (result_) {
92 case MockDnsClientRule::EMPTY: 73 case MockDnsClientRule::EMPTY:
93 case MockDnsClientRule::OK: { 74 case MockDnsClientRule::OK: {
94 std::string qname; 75 std::string qname;
95 DNSDomainFromDot(hostname_, &qname); 76 DNSDomainFromDot(hostname_, &qname);
96 DnsQuery query(0, qname, qtype_); 77 DnsQuery query(0, qname, qtype_);
97 78
98 DnsResponse response; 79 DnsResponse response;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 NOTREACHED(); 129 NOTREACHED();
149 break; 130 break;
150 } 131 }
151 } 132 }
152 133
153 MockDnsClientRule::Result result_; 134 MockDnsClientRule::Result result_;
154 const std::string hostname_; 135 const std::string hostname_;
155 const uint16 qtype_; 136 const uint16 qtype_;
156 DnsTransactionFactory::CallbackType callback_; 137 DnsTransactionFactory::CallbackType callback_;
157 bool started_; 138 bool started_;
158 bool delayed_;
159 }; 139 };
160 140
161 } // namespace
162 141
163 // A DnsTransactionFactory which creates MockTransaction. 142 // A DnsTransactionFactory which creates MockTransaction.
164 class MockTransactionFactory : public DnsTransactionFactory { 143 class MockTransactionFactory : public DnsTransactionFactory {
165 public: 144 public:
166 explicit MockTransactionFactory(const MockDnsClientRuleList& rules) 145 explicit MockTransactionFactory(const MockDnsClientRuleList& rules)
167 : rules_(rules) {} 146 : rules_(rules) {}
168
169 virtual ~MockTransactionFactory() {} 147 virtual ~MockTransactionFactory() {}
170 148
171 virtual scoped_ptr<DnsTransaction> CreateTransaction( 149 virtual scoped_ptr<DnsTransaction> CreateTransaction(
172 const std::string& hostname, 150 const std::string& hostname,
173 uint16 qtype, 151 uint16 qtype,
174 const DnsTransactionFactory::CallbackType& callback, 152 const DnsTransactionFactory::CallbackType& callback,
175 const BoundNetLog&) OVERRIDE { 153 const BoundNetLog&) OVERRIDE {
176 MockTransaction* transaction = 154 return scoped_ptr<DnsTransaction>(
177 new MockTransaction(rules_, hostname, qtype, callback); 155 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 }
191 } 156 }
192 157
193 private: 158 private:
194 typedef std::vector<base::WeakPtr<MockTransaction> > DelayedTransactionList;
195
196 MockDnsClientRuleList rules_; 159 MockDnsClientRuleList rules_;
197 DelayedTransactionList delayed_transactions_;
198 }; 160 };
199 161
200 MockDnsClient::MockDnsClient(const DnsConfig& config, 162 class MockAddressSorter : public AddressSorter {
201 const MockDnsClientRuleList& rules) 163 public:
202 : config_(config), 164 virtual ~MockAddressSorter() {}
203 factory_(new MockTransactionFactory(rules)), 165 virtual void Sort(const AddressList& list,
204 address_sorter_(new MockAddressSorter()) { 166 const CallbackType& callback) const OVERRIDE {
205 } 167 // Do nothing.
168 callback.Run(true, list);
169 }
170 };
206 171
207 MockDnsClient::~MockDnsClient() {} 172 // MockDnsClient provides MockTransactionFactory.
173 class MockDnsClient : public DnsClient {
174 public:
175 MockDnsClient(const DnsConfig& config,
176 const MockDnsClientRuleList& rules)
177 : config_(config), factory_(rules) {}
178 virtual ~MockDnsClient() {}
208 179
209 void MockDnsClient::SetConfig(const DnsConfig& config) { 180 virtual void SetConfig(const DnsConfig& config) OVERRIDE {
210 config_ = config; 181 config_ = config;
211 } 182 }
212 183
213 const DnsConfig* MockDnsClient::GetConfig() const { 184 virtual const DnsConfig* GetConfig() const OVERRIDE {
214 return config_.IsValid() ? &config_ : NULL; 185 return config_.IsValid() ? &config_ : NULL;
215 } 186 }
216 187
217 DnsTransactionFactory* MockDnsClient::GetTransactionFactory() { 188 virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE {
218 return config_.IsValid() ? factory_.get() : NULL; 189 return config_.IsValid() ? &factory_ : NULL;
219 } 190 }
220 191
221 AddressSorter* MockDnsClient::GetAddressSorter() { 192 virtual AddressSorter* GetAddressSorter() OVERRIDE {
222 return address_sorter_.get(); 193 return &address_sorter_;
223 } 194 }
224 195
225 void MockDnsClient::CompleteDelayedTransactions() { 196 private:
226 factory_->CompleteDelayedTransactions(); 197 DnsConfig config_;
198 MockTransactionFactory factory_;
199 MockAddressSorter address_sorter_;
200 };
201
202 } // namespace
203
204 // static
205 scoped_ptr<DnsClient> CreateMockDnsClient(const DnsConfig& config,
206 const MockDnsClientRuleList& rules) {
207 return scoped_ptr<DnsClient>(new MockDnsClient(config, rules));
227 } 208 }
228 209
229 } // namespace net 210 } // namespace net
OLDNEW
« no previous file with comments | « trunk/src/net/dns/dns_test_util.h ('k') | trunk/src/net/dns/host_resolver_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698