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 #ifndef NET_DNS_DNS_TEST_UTIL_H_ | 5 #ifndef NET_DNS_DNS_TEST_UTIL_H_ |
6 #define NET_DNS_DNS_TEST_UTIL_H_ | 6 #define NET_DNS_DNS_TEST_UTIL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
| 15 #include "base/big_endian.h" |
| 16 #include "base/callback_forward.h" |
15 #include "net/dns/dns_client.h" | 17 #include "net/dns/dns_client.h" |
16 #include "net/dns/dns_config_service.h" | 18 #include "net/dns/dns_config_service.h" |
17 #include "net/dns/dns_protocol.h" | 19 #include "net/dns/dns_protocol.h" |
18 | 20 |
19 namespace net { | 21 namespace net { |
20 | 22 |
21 //----------------------------------------------------------------------------- | 23 //----------------------------------------------------------------------------- |
22 // Query/response set for www.google.com, ID is fixed to 0. | 24 // Query/response set for www.google.com, ID is fixed to 0. |
23 static const char kT0HostName[] = "www.google.com"; | 25 static const char kT0HostName[] = "www.google.com"; |
24 static const uint16_t kT0Qtype = dns_protocol::kTypeA; | 26 static const uint16_t kT0Qtype = dns_protocol::kTypeA; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 class MockTransactionFactory; | 160 class MockTransactionFactory; |
159 | 161 |
160 struct MockDnsClientRule { | 162 struct MockDnsClientRule { |
161 enum Result { | 163 enum Result { |
162 FAIL, // Fail asynchronously with ERR_NAME_NOT_RESOLVED. | 164 FAIL, // Fail asynchronously with ERR_NAME_NOT_RESOLVED. |
163 TIMEOUT, // Fail asynchronously with ERR_DNS_TIMEOUT. | 165 TIMEOUT, // Fail asynchronously with ERR_DNS_TIMEOUT. |
164 EMPTY, // Return an empty response. | 166 EMPTY, // Return an empty response. |
165 OK, // Return a response with loopback address. | 167 OK, // Return a response with loopback address. |
166 }; | 168 }; |
167 | 169 |
| 170 // The type of callback invoked in order to prepare a response to a mock DNS |
| 171 // request. An empty response will be created, which can then be manipulated |
| 172 // via the |response_header| and |answer_writer|. It must return net::OK |
| 173 // when done, unless it is intended that the request fail, in which case it |
| 174 // must return a different net::Error. |
| 175 using ResponseCallback = |
| 176 base::Callback<int(dns_protocol::Header* response_header, |
| 177 base::BigEndianWriter* answer_writer)>; |
| 178 |
| 179 // Expect a mock DNS request with the given |prefix| and |qtype|. |
| 180 // The response is dictated by |result|. |
168 // If |delay| is true, matching transactions will be delayed until triggered | 181 // If |delay| is true, matching transactions will be delayed until triggered |
169 // by the consumer. | 182 // by the consumer. |
170 MockDnsClientRule(const std::string& prefix_arg, | 183 MockDnsClientRule(const std::string& prefix, |
171 uint16_t qtype_arg, | 184 uint16_t qtype, |
172 Result result_arg, | 185 Result result, |
173 bool delay) | 186 bool delay); |
174 : result(result_arg), | |
175 prefix(prefix_arg), | |
176 qtype(qtype_arg), | |
177 delay(delay) {} | |
178 | 187 |
179 Result result; | 188 // Expect a mock DNS request with the given |prefix| and |qtype|. |
| 189 // In response, invoke |response_callback|, which can modify the response |
| 190 // header, write answers into the response, or return a net::Error. |
| 191 // If |delay| is true, matching transactions will be delayed until triggered |
| 192 // by the consumer. |
| 193 MockDnsClientRule(const std::string& prefix, |
| 194 uint16_t qtype, |
| 195 ResponseCallback response_callback, |
| 196 bool delay); |
| 197 |
| 198 MockDnsClientRule(const MockDnsClientRule& copy); |
| 199 MockDnsClientRule(MockDnsClientRule&& move); |
| 200 ~MockDnsClientRule(); |
| 201 MockDnsClientRule& operator=(MockDnsClientRule o); |
| 202 |
| 203 friend void swap(MockDnsClientRule& x, MockDnsClientRule& y); |
| 204 |
| 205 ResponseCallback response_callback; |
180 std::string prefix; | 206 std::string prefix; |
181 uint16_t qtype; | 207 uint16_t qtype; |
182 bool delay; | 208 bool delay; |
183 }; | 209 }; |
184 | 210 |
185 typedef std::vector<MockDnsClientRule> MockDnsClientRuleList; | 211 typedef std::vector<MockDnsClientRule> MockDnsClientRuleList; |
186 | 212 |
187 // MockDnsClient provides MockTransactionFactory. | 213 // MockDnsClient provides MockTransactionFactory. |
188 class MockDnsClient : public DnsClient { | 214 class MockDnsClient : public DnsClient { |
189 public: | 215 public: |
(...skipping 11 matching lines...) Expand all Loading... |
201 | 227 |
202 private: | 228 private: |
203 DnsConfig config_; | 229 DnsConfig config_; |
204 std::unique_ptr<MockTransactionFactory> factory_; | 230 std::unique_ptr<MockTransactionFactory> factory_; |
205 std::unique_ptr<AddressSorter> address_sorter_; | 231 std::unique_ptr<AddressSorter> address_sorter_; |
206 }; | 232 }; |
207 | 233 |
208 } // namespace net | 234 } // namespace net |
209 | 235 |
210 #endif // NET_DNS_DNS_TEST_UTIL_H_ | 236 #endif // NET_DNS_DNS_TEST_UTIL_H_ |
OLD | NEW |