OLD | NEW |
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" | |
21 #include "net/dns/dns_query.h" | 18 #include "net/dns/dns_query.h" |
22 #include "net/dns/dns_response.h" | 19 #include "net/dns/dns_response.h" |
23 #include "net/dns/dns_transaction.h" | 20 #include "net/dns/dns_transaction.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
25 | 22 |
26 namespace net { | 23 namespace net { |
27 namespace { | 24 namespace { |
28 | 25 |
29 // A DnsTransaction which uses MockDnsClientRuleList to determine the response. | 26 // A DnsTransaction which uses MockDnsClientRuleList to determine the response. |
30 class MockTransaction : public DnsTransaction, | 27 class MockTransaction : public DnsTransaction, |
31 public base::SupportsWeakPtr<MockTransaction> { | 28 public base::SupportsWeakPtr<MockTransaction> { |
32 public: | 29 public: |
33 MockTransaction(const MockDnsClientRuleList& rules, | 30 MockTransaction(const MockDnsClientRuleList& rules, |
34 const std::string& hostname, | 31 const std::string& hostname, |
35 uint16 qtype, | 32 uint16 qtype, |
36 const DnsTransactionFactory::CallbackType& callback) | 33 const DnsTransactionFactory::CallbackType& callback) |
37 : result_(MockDnsClientRule::FAIL), | 34 : result_(MockDnsClientRule::FAIL), |
38 hostname_(hostname), | 35 hostname_(hostname), |
39 qtype_(qtype), | 36 qtype_(qtype), |
40 callback_(callback), | 37 callback_(callback), |
41 started_(false) { | 38 started_(false), |
| 39 delayed_(false) { |
42 // Find the relevant rule which matches |qtype| and prefix of |hostname|. | 40 // Find the relevant rule which matches |qtype| and prefix of |hostname|. |
43 for (size_t i = 0; i < rules.size(); ++i) { | 41 for (size_t i = 0; i < rules.size(); ++i) { |
44 const std::string& prefix = rules[i].prefix; | 42 const std::string& prefix = rules[i].prefix; |
45 if ((rules[i].qtype == qtype) && | 43 if ((rules[i].qtype == qtype) && |
46 (hostname.size() >= prefix.size()) && | 44 (hostname.size() >= prefix.size()) && |
47 (hostname.compare(0, prefix.size(), prefix) == 0)) { | 45 (hostname.compare(0, prefix.size(), prefix) == 0)) { |
48 result_ = rules[i].result; | 46 result_ = rules[i].result; |
| 47 delayed_ = rules[i].delay; |
49 break; | 48 break; |
50 } | 49 } |
51 } | 50 } |
52 } | 51 } |
53 | 52 |
54 virtual const std::string& GetHostname() const OVERRIDE { | 53 virtual const std::string& GetHostname() const OVERRIDE { |
55 return hostname_; | 54 return hostname_; |
56 } | 55 } |
57 | 56 |
58 virtual uint16 GetType() const OVERRIDE { | 57 virtual uint16 GetType() const OVERRIDE { |
59 return qtype_; | 58 return qtype_; |
60 } | 59 } |
61 | 60 |
62 virtual void Start() OVERRIDE { | 61 virtual void Start() OVERRIDE { |
63 EXPECT_FALSE(started_); | 62 EXPECT_FALSE(started_); |
64 started_ = true; | 63 started_ = true; |
65 // Using WeakPtr to cleanly cancel when transaction is destroyed. | 64 if (!delayed_) { |
66 base::MessageLoop::current()->PostTask( | 65 // Using WeakPtr to cleanly cancel when transaction is destroyed. |
67 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); | 66 base::MessageLoop::current()->PostTask( |
| 67 FROM_HERE, base::Bind(&MockTransaction::Finish, AsWeakPtr())); |
| 68 } |
68 } | 69 } |
69 | 70 |
| 71 void FinishDelayedTransaction() { |
| 72 EXPECT_TRUE(delayed_); |
| 73 delayed_ = false; |
| 74 Finish(); |
| 75 } |
| 76 |
| 77 bool delayed() const { return delayed_; } |
| 78 |
70 private: | 79 private: |
71 void Finish() { | 80 void Finish() { |
72 switch (result_) { | 81 switch (result_) { |
73 case MockDnsClientRule::EMPTY: | 82 case MockDnsClientRule::EMPTY: |
74 case MockDnsClientRule::OK: { | 83 case MockDnsClientRule::OK: { |
75 std::string qname; | 84 std::string qname; |
76 DNSDomainFromDot(hostname_, &qname); | 85 DNSDomainFromDot(hostname_, &qname); |
77 DnsQuery query(0, qname, qtype_); | 86 DnsQuery query(0, qname, qtype_); |
78 | 87 |
79 DnsResponse response; | 88 DnsResponse response; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 NOTREACHED(); | 138 NOTREACHED(); |
130 break; | 139 break; |
131 } | 140 } |
132 } | 141 } |
133 | 142 |
134 MockDnsClientRule::Result result_; | 143 MockDnsClientRule::Result result_; |
135 const std::string hostname_; | 144 const std::string hostname_; |
136 const uint16 qtype_; | 145 const uint16 qtype_; |
137 DnsTransactionFactory::CallbackType callback_; | 146 DnsTransactionFactory::CallbackType callback_; |
138 bool started_; | 147 bool started_; |
| 148 bool delayed_; |
139 }; | 149 }; |
140 | 150 |
| 151 class MockAddressSorter : public AddressSorter { |
| 152 public: |
| 153 virtual ~MockAddressSorter() {} |
| 154 virtual void Sort(const AddressList& list, |
| 155 const CallbackType& callback) const OVERRIDE { |
| 156 // Do nothing. |
| 157 callback.Run(true, list); |
| 158 } |
| 159 }; |
| 160 |
| 161 } // namespace |
141 | 162 |
142 // A DnsTransactionFactory which creates MockTransaction. | 163 // A DnsTransactionFactory which creates MockTransaction. |
143 class MockTransactionFactory : public DnsTransactionFactory { | 164 class MockTransactionFactory : public DnsTransactionFactory { |
144 public: | 165 public: |
145 explicit MockTransactionFactory(const MockDnsClientRuleList& rules) | 166 explicit MockTransactionFactory(const MockDnsClientRuleList& rules) |
146 : rules_(rules) {} | 167 : rules_(rules) {} |
| 168 |
147 virtual ~MockTransactionFactory() {} | 169 virtual ~MockTransactionFactory() {} |
148 | 170 |
149 virtual scoped_ptr<DnsTransaction> CreateTransaction( | 171 virtual scoped_ptr<DnsTransaction> CreateTransaction( |
150 const std::string& hostname, | 172 const std::string& hostname, |
151 uint16 qtype, | 173 uint16 qtype, |
152 const DnsTransactionFactory::CallbackType& callback, | 174 const DnsTransactionFactory::CallbackType& callback, |
153 const BoundNetLog&) OVERRIDE { | 175 const BoundNetLog&) OVERRIDE { |
154 return scoped_ptr<DnsTransaction>( | 176 MockTransaction* transaction = |
155 new MockTransaction(rules_, hostname, qtype, callback)); | 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 } |
156 } | 191 } |
157 | 192 |
158 private: | 193 private: |
| 194 typedef std::vector<base::WeakPtr<MockTransaction> > DelayedTransactionList; |
| 195 |
159 MockDnsClientRuleList rules_; | 196 MockDnsClientRuleList rules_; |
| 197 DelayedTransactionList delayed_transactions_; |
160 }; | 198 }; |
161 | 199 |
162 class MockAddressSorter : public AddressSorter { | 200 MockDnsClient::MockDnsClient(const DnsConfig& config, |
163 public: | 201 const MockDnsClientRuleList& rules) |
164 virtual ~MockAddressSorter() {} | 202 : config_(config), |
165 virtual void Sort(const AddressList& list, | 203 factory_(new MockTransactionFactory(rules)), |
166 const CallbackType& callback) const OVERRIDE { | 204 address_sorter_(new MockAddressSorter()) { |
167 // Do nothing. | 205 } |
168 callback.Run(true, list); | |
169 } | |
170 }; | |
171 | 206 |
172 // MockDnsClient provides MockTransactionFactory. | 207 MockDnsClient::~MockDnsClient() {} |
173 class MockDnsClient : public DnsClient { | |
174 public: | |
175 MockDnsClient(const DnsConfig& config, | |
176 const MockDnsClientRuleList& rules) | |
177 : config_(config), factory_(rules) {} | |
178 virtual ~MockDnsClient() {} | |
179 | 208 |
180 virtual void SetConfig(const DnsConfig& config) OVERRIDE { | 209 void MockDnsClient::SetConfig(const DnsConfig& config) { |
181 config_ = config; | 210 config_ = config; |
182 } | 211 } |
183 | 212 |
184 virtual const DnsConfig* GetConfig() const OVERRIDE { | 213 const DnsConfig* MockDnsClient::GetConfig() const { |
185 return config_.IsValid() ? &config_ : NULL; | 214 return config_.IsValid() ? &config_ : NULL; |
186 } | 215 } |
187 | 216 |
188 virtual DnsTransactionFactory* GetTransactionFactory() OVERRIDE { | 217 DnsTransactionFactory* MockDnsClient::GetTransactionFactory() { |
189 return config_.IsValid() ? &factory_ : NULL; | 218 return config_.IsValid() ? factory_.get() : NULL; |
190 } | 219 } |
191 | 220 |
192 virtual AddressSorter* GetAddressSorter() OVERRIDE { | 221 AddressSorter* MockDnsClient::GetAddressSorter() { |
193 return &address_sorter_; | 222 return address_sorter_.get(); |
194 } | 223 } |
195 | 224 |
196 private: | 225 void MockDnsClient::CompleteDelayedTransactions() { |
197 DnsConfig config_; | 226 factory_->CompleteDelayedTransactions(); |
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)); | |
208 } | 227 } |
209 | 228 |
210 } // namespace net | 229 } // namespace net |
OLD | NEW |