dns_test_util.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #ifndef NET_DNS_DNS_TEST_UTIL_H_
  5. #define NET_DNS_DNS_TEST_UTIL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/scoped_refptr.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/time/time.h"
  17. #include "net/base/connection_endpoint_metadata.h"
  18. #include "net/base/ip_endpoint.h"
  19. #include "net/dns/dns_client.h"
  20. #include "net/dns/dns_config.h"
  21. #include "net/dns/dns_response.h"
  22. #include "net/dns/dns_transaction.h"
  23. #include "net/dns/dns_util.h"
  24. #include "net/dns/public/dns_over_https_server_config.h"
  25. #include "net/dns/public/dns_protocol.h"
  26. #include "net/dns/public/secure_dns_mode.h"
  27. #include "net/socket/socket_test_util.h"
  28. #include "third_party/abseil-cpp/absl/types/optional.h"
  29. #include "url/scheme_host_port.h"
  30. namespace net {
  31. //-----------------------------------------------------------------------------
  32. // Query/response set for www.google.com, ID is fixed to 0.
  33. static const char kT0HostName[] = "www.google.com";
  34. static const uint16_t kT0Qtype = dns_protocol::kTypeA;
  35. static const char kT0DnsName[] = {
  36. 0x03, 'w', 'w', 'w',
  37. 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
  38. 0x03, 'c', 'o', 'm',
  39. 0x00
  40. };
  41. static const size_t kT0QuerySize = 32;
  42. static const uint8_t kT0ResponseDatagram[] = {
  43. // response contains one CNAME for www.l.google.com and the following
  44. // IP addresses: 74.125.226.{179,180,176,177,178}
  45. 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  46. 0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
  47. 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05,
  48. 0x00, 0x01, 0x00, 0x01, 0x4d, 0x13, 0x00, 0x08, 0x03, 0x77, 0x77, 0x77,
  49. 0x01, 0x6c, 0xc0, 0x10, 0xc0, 0x2c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  50. 0x00, 0xe4, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb3, 0xc0, 0x2c, 0x00, 0x01,
  51. 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb4,
  52. 0xc0, 0x2c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04,
  53. 0x4a, 0x7d, 0xe2, 0xb0, 0xc0, 0x2c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  54. 0x00, 0xe4, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb1, 0xc0, 0x2c, 0x00, 0x01,
  55. 0x00, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb2};
  56. static const char* const kT0IpAddresses[] = {
  57. "74.125.226.179", "74.125.226.180", "74.125.226.176",
  58. "74.125.226.177", "74.125.226.178"
  59. };
  60. static const char kT0CanonName[] = "www.l.google.com";
  61. static const base::TimeDelta kT0Ttl = base::Seconds(0x000000e4);
  62. // +1 for the CNAME record.
  63. static const unsigned kT0RecordCount = std::size(kT0IpAddresses) + 1;
  64. //-----------------------------------------------------------------------------
  65. // Query/response set for codereview.chromium.org, ID is fixed to 1.
  66. static const char kT1HostName[] = "codereview.chromium.org";
  67. static const uint16_t kT1Qtype = dns_protocol::kTypeA;
  68. static const char kT1DnsName[] = {
  69. 0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w',
  70. 0x08, 'c', 'h', 'r', 'o', 'm', 'i', 'u', 'm',
  71. 0x03, 'o', 'r', 'g',
  72. 0x00
  73. };
  74. static const size_t kT1QuerySize = 41;
  75. static const uint8_t kT1ResponseDatagram[] = {
  76. // response contains one CNAME for ghs.l.google.com and the following
  77. // IP address: 64.233.169.121
  78. 0x00, 0x01, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
  79. 0x00, 0x0a, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x65, 0x76, 0x69, 0x65,
  80. 0x77, 0x08, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x69, 0x75, 0x6d, 0x03,
  81. 0x6f, 0x72, 0x67, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00,
  82. 0x05, 0x00, 0x01, 0x00, 0x01, 0x41, 0x75, 0x00, 0x12, 0x03, 0x67,
  83. 0x68, 0x73, 0x01, 0x6c, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
  84. 0x03, 0x63, 0x6f, 0x6d, 0x00, 0xc0, 0x35, 0x00, 0x01, 0x00, 0x01,
  85. 0x00, 0x00, 0x01, 0x0b, 0x00, 0x04, 0x40, 0xe9, 0xa9, 0x79};
  86. static const char* const kT1IpAddresses[] = {
  87. "64.233.169.121"
  88. };
  89. static const char kT1CanonName[] = "ghs.l.google.com";
  90. static const base::TimeDelta kT1Ttl = base::Seconds(0x0000010b);
  91. // +1 for the CNAME record.
  92. static const unsigned kT1RecordCount = std::size(kT1IpAddresses) + 1;
  93. //-----------------------------------------------------------------------------
  94. // Query/response set for www.ccs.neu.edu, ID is fixed to 2.
  95. static const char kT2HostName[] = "www.ccs.neu.edu";
  96. static const uint16_t kT2Qtype = dns_protocol::kTypeA;
  97. static const char kT2DnsName[] = {
  98. 0x03, 'w', 'w', 'w',
  99. 0x03, 'c', 'c', 's',
  100. 0x03, 'n', 'e', 'u',
  101. 0x03, 'e', 'd', 'u',
  102. 0x00
  103. };
  104. static const size_t kT2QuerySize = 33;
  105. static const uint8_t kT2ResponseDatagram[] = {
  106. // response contains one CNAME for vulcan.ccs.neu.edu and the following
  107. // IP address: 129.10.116.81
  108. 0x00, 0x02, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
  109. 0x03, 0x77, 0x77, 0x77, 0x03, 0x63, 0x63, 0x73, 0x03, 0x6e, 0x65, 0x75,
  110. 0x03, 0x65, 0x64, 0x75, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00,
  111. 0x05, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x09, 0x06, 0x76, 0x75,
  112. 0x6c, 0x63, 0x61, 0x6e, 0xc0, 0x10, 0xc0, 0x2d, 0x00, 0x01, 0x00, 0x01,
  113. 0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x81, 0x0a, 0x74, 0x51};
  114. static const char* const kT2IpAddresses[] = {
  115. "129.10.116.81"
  116. };
  117. static const char kT2CanonName[] = "vulcan.ccs.neu.edu";
  118. static const base::TimeDelta kT2Ttl = base::Seconds(0x0000012c);
  119. // +1 for the CNAME record.
  120. static const unsigned kT2RecordCount = std::size(kT2IpAddresses) + 1;
  121. //-----------------------------------------------------------------------------
  122. // Query/response set for www.google.az, ID is fixed to 3.
  123. static const char kT3HostName[] = "www.google.az";
  124. static const uint16_t kT3Qtype = dns_protocol::kTypeA;
  125. static const char kT3DnsName[] = {
  126. 0x03, 'w', 'w', 'w',
  127. 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
  128. 0x02, 'a', 'z',
  129. 0x00
  130. };
  131. static const size_t kT3QuerySize = 31;
  132. static const uint8_t kT3ResponseDatagram[] = {
  133. // response contains www.google.com as CNAME for www.google.az and
  134. // www.l.google.com as CNAME for www.google.com and the following
  135. // IP addresses: 74.125.226.{178,179,180,176,177}
  136. // The TTLs on the records are: 0x00015099, 0x00025099, 0x00000415,
  137. // 0x00003015, 0x00002015, 0x00000015, 0x00001015.
  138. // The last record is an imaginary TXT record for t.google.com.
  139. 0x00, 0x03, 0x81, 0x80, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
  140. 0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x02,
  141. 0x61, 0x7a, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05, 0x00,
  142. 0x01, 0x00, 0x01, 0x50, 0x99, 0x00, 0x10, 0x03, 0x77, 0x77, 0x77, 0x06,
  143. 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0xc0,
  144. 0x2b, 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x50, 0x99, 0x00, 0x08, 0x03,
  145. 0x77, 0x77, 0x77, 0x01, 0x6c, 0xc0, 0x2f, 0xc0, 0x47, 0x00, 0x01, 0x00,
  146. 0x01, 0x00, 0x00, 0x04, 0x15, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb2, 0xc0,
  147. 0x47, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x30, 0x15, 0x00, 0x04, 0x4a,
  148. 0x7d, 0xe2, 0xb3, 0xc0, 0x47, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x20,
  149. 0x15, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb4, 0xc0, 0x47, 0x00, 0x01, 0x00,
  150. 0x01, 0x00, 0x00, 0x00, 0x15, 0x00, 0x04, 0x4a, 0x7d, 0xe2, 0xb0, 0xc0,
  151. 0x47, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x10, 0x15, 0x00, 0x04, 0x4a,
  152. 0x7d, 0xe2, 0xb1, 0x01, 0x74, 0xc0, 0x2f, 0x00, 0x10, 0x00, 0x01, 0x00,
  153. 0x00, 0x00, 0x01, 0x00, 0x04, 0xde, 0xad, 0xfe, 0xed};
  154. static const char* const kT3IpAddresses[] = {
  155. "74.125.226.178", "74.125.226.179", "74.125.226.180",
  156. "74.125.226.176", "74.125.226.177"
  157. };
  158. static const char kT3CanonName[] = "www.l.google.com";
  159. static const base::TimeDelta kT3Ttl = base::Seconds(0x00000015);
  160. // +2 for the CNAME records, +1 for TXT record.
  161. static const unsigned kT3RecordCount = std::size(kT3IpAddresses) + 3;
  162. //-----------------------------------------------------------------------------
  163. // Query/response set for www.gstatic.com, ID is fixed to 0.
  164. static const char kT4HostName[] = "www.gstatic.com";
  165. static const uint16_t kT4Qtype = dns_protocol::kTypeA;
  166. static const char kT4DnsName[] = {0x03, 'w', 'w', 'w', 0x07, 'g',
  167. 's', 't', 'a', 't', 'i', 'c',
  168. 0x03, 'c', 'o', 'm', 0x00};
  169. static const size_t kT4QuerySize = 33;
  170. static const uint8_t kT4ResponseDatagram[] = {
  171. // response contains the following IP addresses: 172.217.6.195.
  172. 0x00, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  173. 0x00, 0x00, 0x03, 0x77, 0x77, 0x77, 0x07, 0x67, 0x73, 0x74,
  174. 0x61, 0x74, 0x69, 0x63, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00,
  175. 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00,
  176. 0x00, 0x01, 0x2b, 0x00, 0x04, 0xac, 0xd9, 0x06, 0xc3};
  177. static const char* const kT4IpAddresses[] = {"172.217.6.195"};
  178. static const base::TimeDelta kT4Ttl = base::Seconds(0x0000012b);
  179. static const unsigned kT4RecordCount = std::size(kT0IpAddresses);
  180. class AddressSorter;
  181. class DnsClient;
  182. class DnsSession;
  183. class IPAddress;
  184. class ResolveContext;
  185. class URLRequestContext;
  186. DnsResourceRecord BuildTestDnsRecord(std::string name,
  187. uint16_t type,
  188. std::string rdata,
  189. base::TimeDelta ttl = base::Days(1));
  190. DnsResourceRecord BuildTestCnameRecord(std::string name,
  191. base::StringPiece canonical_name,
  192. base::TimeDelta ttl = base::Days(1));
  193. DnsResourceRecord BuildTestAddressRecord(std::string name,
  194. const IPAddress& ip,
  195. base::TimeDelta ttl = base::Days(1));
  196. DnsResourceRecord BuildTestTextRecord(std::string name,
  197. std::vector<std::string> text_strings,
  198. base::TimeDelta ttl = base::Days(1));
  199. DnsResourceRecord BuildTestHttpsAliasRecord(
  200. std::string name,
  201. base::StringPiece alias_name,
  202. base::TimeDelta ttl = base::Days(1));
  203. std::pair<uint16_t, std::string> BuildTestHttpsServiceAlpnParam(
  204. const std::vector<std::string>& alpns);
  205. std::pair<uint16_t, std::string> BuildTestHttpsServiceEchConfigParam(
  206. base::span<const uint8_t> ech_config_list);
  207. std::pair<uint16_t, std::string> BuildTestHttpsServiceMandatoryParam(
  208. std::vector<uint16_t> param_key_list);
  209. std::pair<uint16_t, std::string> BuildTestHttpsServicePortParam(uint16_t port);
  210. // `params` is a mapping from service param keys to a string containing the
  211. // encoded bytes of a service param value (without the value length prefix which
  212. // this method will automatically add).
  213. DnsResourceRecord BuildTestHttpsServiceRecord(
  214. std::string name,
  215. uint16_t priority,
  216. base::StringPiece service_name,
  217. const std::map<uint16_t, std::string>& params,
  218. base::TimeDelta ttl = base::Days(1));
  219. DnsResponse BuildTestDnsResponse(
  220. std::string name,
  221. uint16_t type,
  222. const std::vector<DnsResourceRecord>& answers,
  223. const std::vector<DnsResourceRecord>& authority = {},
  224. const std::vector<DnsResourceRecord>& additional = {},
  225. uint8_t rcode = dns_protocol::kRcodeNOERROR);
  226. DnsResponse BuildTestDnsAddressResponse(std::string name,
  227. const IPAddress& ip,
  228. std::string answer_name = "");
  229. DnsResponse BuildTestDnsAddressResponseWithCname(std::string name,
  230. const IPAddress& ip,
  231. std::string cannonname,
  232. std::string answer_name = "");
  233. // If |answer_name| is empty, |name| will be used for all answer records, as is
  234. // the normal behavior.
  235. DnsResponse BuildTestDnsTextResponse(
  236. std::string name,
  237. std::vector<std::vector<std::string>> text_records,
  238. std::string answer_name = "");
  239. DnsResponse BuildTestDnsPointerResponse(std::string name,
  240. std::vector<std::string> pointer_names,
  241. std::string answer_name = "");
  242. struct TestServiceRecord {
  243. uint16_t priority;
  244. uint16_t weight;
  245. uint16_t port;
  246. std::string target;
  247. };
  248. DnsResponse BuildTestDnsServiceResponse(
  249. std::string name,
  250. std::vector<TestServiceRecord> service_records,
  251. std::string answer_name = "");
  252. struct MockDnsClientRule {
  253. enum class ResultType {
  254. // Fail asynchronously with ERR_NAME_NOT_RESOLVED and NXDOMAIN.
  255. kNoDomain,
  256. // Fail asynchronously with `net_error` or (if nullopt)
  257. // ERR_NAME_NOT_RESOLVED and `response` if not nullopt.
  258. kFail,
  259. // Fail asynchronously with ERR_DNS_TIMED_OUT.
  260. kTimeout,
  261. // Simulates a slow transaction that will complete only with a lenient
  262. // timeout. Fails asynchronously with ERR_DNS_TIMED_OUT only if the
  263. // transaction was created with |fast_timeout|. Otherwise completes
  264. // successfully as if the ResultType were |kOk|.
  265. kSlow,
  266. // Return an empty response.
  267. kEmpty,
  268. // "Succeed" but with an unparsable response.
  269. kMalformed,
  270. // Immediately records a test failure if queried. Used to catch unexpected
  271. // queries. Alternately, if combined with `MockDnsClientRule::delay`, fails
  272. // only if the query is allowed to complete without being cancelled.
  273. kUnexpected,
  274. // Results in the response in |Result::response| or, if null, results in a
  275. // localhost IP response.
  276. kOk,
  277. };
  278. struct Result {
  279. explicit Result(ResultType type,
  280. absl::optional<DnsResponse> response = absl::nullopt,
  281. absl::optional<int> net_error = absl::nullopt);
  282. explicit Result(DnsResponse response);
  283. Result(Result&&);
  284. Result& operator=(Result&&);
  285. ~Result();
  286. ResultType type;
  287. absl::optional<DnsResponse> response;
  288. absl::optional<int> net_error;
  289. };
  290. // If |delay| is true, matching transactions will be delayed until triggered
  291. // by the consumer. If |context| is non-null, it will only match transactions
  292. // with the same context.
  293. MockDnsClientRule(const std::string& prefix,
  294. uint16_t qtype,
  295. bool secure,
  296. Result result,
  297. bool delay,
  298. URLRequestContext* context = nullptr);
  299. MockDnsClientRule(MockDnsClientRule&& rule);
  300. Result result;
  301. std::string prefix;
  302. uint16_t qtype;
  303. bool secure;
  304. bool delay;
  305. raw_ptr<URLRequestContext> context;
  306. };
  307. typedef std::vector<MockDnsClientRule> MockDnsClientRuleList;
  308. // A DnsTransactionFactory which creates MockTransaction.
  309. class MockDnsTransactionFactory : public DnsTransactionFactory {
  310. public:
  311. explicit MockDnsTransactionFactory(MockDnsClientRuleList rules);
  312. ~MockDnsTransactionFactory() override;
  313. std::unique_ptr<DnsTransaction> CreateTransaction(
  314. std::string hostname,
  315. uint16_t qtype,
  316. const NetLogWithSource&,
  317. bool secure,
  318. SecureDnsMode secure_dns_mode,
  319. ResolveContext* resolve_context,
  320. bool fast_timeout) override;
  321. std::unique_ptr<DnsProbeRunner> CreateDohProbeRunner(
  322. ResolveContext* resolve_context) override;
  323. void AddEDNSOption(std::unique_ptr<OptRecordRdata::Opt> opt) override;
  324. SecureDnsMode GetSecureDnsModeForTest() override;
  325. void CompleteDelayedTransactions();
  326. // If there are any pending transactions of the given type,
  327. // completes one and returns true. Otherwise, returns false.
  328. [[nodiscard]] bool CompleteOneDelayedTransactionOfType(DnsQueryType type);
  329. bool doh_probes_running() { return !running_doh_probe_runners_.empty(); }
  330. void CompleteDohProbeRuners() { running_doh_probe_runners_.clear(); }
  331. void set_force_doh_server_available(bool available) {
  332. force_doh_server_available_ = available;
  333. }
  334. private:
  335. class MockTransaction;
  336. class MockDohProbeRunner;
  337. using DelayedTransactionList = std::vector<base::WeakPtr<MockTransaction>>;
  338. MockDnsClientRuleList rules_;
  339. DelayedTransactionList delayed_transactions_;
  340. bool force_doh_server_available_ = true;
  341. std::set<MockDohProbeRunner*> running_doh_probe_runners_;
  342. base::WeakPtrFactory<MockDnsTransactionFactory> weak_ptr_factory_{this};
  343. };
  344. // MockDnsClient provides MockDnsTransactionFactory.
  345. class MockDnsClient : public DnsClient {
  346. public:
  347. MockDnsClient(DnsConfig config, MockDnsClientRuleList rules);
  348. ~MockDnsClient() override;
  349. // DnsClient interface:
  350. bool CanUseSecureDnsTransactions() const override;
  351. bool CanUseInsecureDnsTransactions() const override;
  352. bool CanQueryAdditionalTypesViaInsecureDns() const override;
  353. void SetInsecureEnabled(bool enabled, bool additional_types_enabled) override;
  354. bool FallbackFromSecureTransactionPreferred(
  355. ResolveContext* resolve_context) const override;
  356. bool FallbackFromInsecureTransactionPreferred() const override;
  357. bool SetSystemConfig(absl::optional<DnsConfig> system_config) override;
  358. bool SetConfigOverrides(DnsConfigOverrides config_overrides) override;
  359. void ReplaceCurrentSession() override;
  360. DnsSession* GetCurrentSession() override;
  361. const DnsConfig* GetEffectiveConfig() const override;
  362. const DnsHosts* GetHosts() const override;
  363. DnsTransactionFactory* GetTransactionFactory() override;
  364. AddressSorter* GetAddressSorter() override;
  365. void IncrementInsecureFallbackFailures() override;
  366. void ClearInsecureFallbackFailures() override;
  367. base::Value GetDnsConfigAsValueForNetLog() const override;
  368. absl::optional<DnsConfig> GetSystemConfigForTesting() const override;
  369. DnsConfigOverrides GetConfigOverridesForTesting() const override;
  370. void SetTransactionFactoryForTesting(
  371. std::unique_ptr<DnsTransactionFactory> factory) override;
  372. absl::optional<std::vector<IPEndPoint>> GetPresetAddrs(
  373. const url::SchemeHostPort& endpoint) const override;
  374. // Completes all DnsTransactions that were delayed by a rule.
  375. void CompleteDelayedTransactions();
  376. // If there are any pending transactions of the given type,
  377. // completes one and returns true. Otherwise, returns false.
  378. [[nodiscard]] bool CompleteOneDelayedTransactionOfType(DnsQueryType type);
  379. void set_max_fallback_failures(int max_fallback_failures) {
  380. max_fallback_failures_ = max_fallback_failures;
  381. }
  382. void set_ignore_system_config_changes(bool ignore_system_config_changes) {
  383. ignore_system_config_changes_ = ignore_system_config_changes;
  384. }
  385. void set_preset_endpoint(absl::optional<url::SchemeHostPort> endpoint) {
  386. preset_endpoint_ = std::move(endpoint);
  387. }
  388. void set_preset_addrs(std::vector<IPEndPoint> preset_addrs) {
  389. preset_addrs_ = std::move(preset_addrs);
  390. }
  391. void SetForceDohServerAvailable(bool available);
  392. MockDnsTransactionFactory* factory() { return factory_.get(); }
  393. private:
  394. absl::optional<DnsConfig> BuildEffectiveConfig();
  395. scoped_refptr<DnsSession> BuildSession();
  396. bool insecure_enabled_ = false;
  397. bool additional_types_enabled_ = false;
  398. int fallback_failures_ = 0;
  399. int max_fallback_failures_ = DnsClient::kMaxInsecureFallbackFailures;
  400. bool ignore_system_config_changes_ = false;
  401. // If |true|, MockDnsClient will always pretend DoH servers are available and
  402. // allow secure transactions no matter what the state is in the transaction
  403. // ResolveContext. If |false|, the ResolveContext must contain at least one
  404. // available DoH server to allow secure transactions.
  405. bool force_doh_server_available_ = true;
  406. MockClientSocketFactory socket_factory_;
  407. absl::optional<DnsConfig> config_;
  408. scoped_refptr<DnsSession> session_;
  409. DnsConfigOverrides overrides_;
  410. absl::optional<DnsConfig> effective_config_;
  411. std::unique_ptr<MockDnsTransactionFactory> factory_;
  412. std::unique_ptr<AddressSorter> address_sorter_;
  413. absl::optional<url::SchemeHostPort> preset_endpoint_;
  414. absl::optional<std::vector<IPEndPoint>> preset_addrs_;
  415. };
  416. } // namespace net
  417. #endif // NET_DNS_DNS_TEST_UTIL_H_