test_rootresolve.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for Twisted.names' root resolver.
  5. """
  6. from zope.interface import implementer
  7. from zope.interface.verify import verifyClass
  8. from twisted.python.log import msg
  9. from twisted.trial import util
  10. from twisted.trial.unittest import SynchronousTestCase, TestCase
  11. from twisted.internet.defer import Deferred, succeed, gatherResults, TimeoutError
  12. from twisted.internet.interfaces import IResolverSimple
  13. from twisted.names import client, root
  14. from twisted.names.root import Resolver
  15. from twisted.names.dns import (
  16. IN, HS, A, NS, CNAME, OK, ENAME, Record_CNAME,
  17. Name, Query, Message, RRHeader, Record_A, Record_NS)
  18. from twisted.names.error import DNSNameError, ResolverError
  19. from twisted.names.test.test_util import MemoryReactor
  20. def getOnePayload(results):
  21. """
  22. From the result of a L{Deferred} returned by L{IResolver.lookupAddress},
  23. return the payload of the first record in the answer section.
  24. """
  25. ans, auth, add = results
  26. return ans[0].payload
  27. def getOneAddress(results):
  28. """
  29. From the result of a L{Deferred} returned by L{IResolver.lookupAddress},
  30. return the first IPv4 address from the answer section.
  31. """
  32. return getOnePayload(results).dottedQuad()
  33. class RootResolverTests(TestCase):
  34. """
  35. Tests for L{twisted.names.root.Resolver}.
  36. """
  37. def _queryTest(self, filter):
  38. """
  39. Invoke L{Resolver._query} and verify that it sends the correct DNS
  40. query. Deliver a canned response to the query and return whatever the
  41. L{Deferred} returned by L{Resolver._query} fires with.
  42. @param filter: The value to pass for the C{filter} parameter to
  43. L{Resolver._query}.
  44. """
  45. reactor = MemoryReactor()
  46. resolver = Resolver([], reactor=reactor)
  47. d = resolver._query(
  48. Query(b'foo.example.com', A, IN), [('1.1.2.3', 1053)], (30,),
  49. filter)
  50. # A UDP port should have been started.
  51. portNumber, transport = reactor.udpPorts.popitem()
  52. # And a DNS packet sent.
  53. [(packet, address)] = transport._sentPackets
  54. message = Message()
  55. message.fromStr(packet)
  56. # It should be a query with the parameters used above.
  57. self.assertEqual(message.queries, [Query(b'foo.example.com', A, IN)])
  58. self.assertEqual(message.answers, [])
  59. self.assertEqual(message.authority, [])
  60. self.assertEqual(message.additional, [])
  61. response = []
  62. d.addCallback(response.append)
  63. self.assertEqual(response, [])
  64. # Once a reply is received, the Deferred should fire.
  65. del message.queries[:]
  66. message.answer = 1
  67. message.answers.append(RRHeader(
  68. b'foo.example.com', payload=Record_A('5.8.13.21')))
  69. transport._protocol.datagramReceived(
  70. message.toStr(), ('1.1.2.3', 1053))
  71. return response[0]
  72. def test_filteredQuery(self):
  73. """
  74. L{Resolver._query} accepts a L{Query} instance and an address, issues
  75. the query, and returns a L{Deferred} which fires with the response to
  76. the query. If a true value is passed for the C{filter} parameter, the
  77. result is a three-tuple of lists of records.
  78. """
  79. answer, authority, additional = self._queryTest(True)
  80. self.assertEqual(
  81. answer,
  82. [RRHeader(b'foo.example.com', payload=Record_A('5.8.13.21', ttl=0))])
  83. self.assertEqual(authority, [])
  84. self.assertEqual(additional, [])
  85. def test_unfilteredQuery(self):
  86. """
  87. Similar to L{test_filteredQuery}, but for the case where a false value
  88. is passed for the C{filter} parameter. In this case, the result is a
  89. L{Message} instance.
  90. """
  91. message = self._queryTest(False)
  92. self.assertIsInstance(message, Message)
  93. self.assertEqual(message.queries, [])
  94. self.assertEqual(
  95. message.answers,
  96. [RRHeader(b'foo.example.com', payload=Record_A('5.8.13.21', ttl=0))])
  97. self.assertEqual(message.authority, [])
  98. self.assertEqual(message.additional, [])
  99. def _respond(self, answers=[], authority=[], additional=[], rCode=OK):
  100. """
  101. Create a L{Message} suitable for use as a response to a query.
  102. @param answers: A C{list} of two-tuples giving data for the answers
  103. section of the message. The first element of each tuple is a name
  104. for the L{RRHeader}. The second element is the payload.
  105. @param authority: A C{list} like C{answers}, but for the authority
  106. section of the response.
  107. @param additional: A C{list} like C{answers}, but for the
  108. additional section of the response.
  109. @param rCode: The response code the message will be created with.
  110. @return: A new L{Message} initialized with the given values.
  111. """
  112. response = Message(rCode=rCode)
  113. for (section, data) in [(response.answers, answers),
  114. (response.authority, authority),
  115. (response.additional, additional)]:
  116. section.extend([
  117. RRHeader(name, record.TYPE, getattr(record, 'CLASS', IN),
  118. payload=record)
  119. for (name, record) in data])
  120. return response
  121. def _getResolver(self, serverResponses, maximumQueries=10):
  122. """
  123. Create and return a new L{root.Resolver} modified to resolve queries
  124. against the record data represented by C{servers}.
  125. @param serverResponses: A mapping from dns server addresses to
  126. mappings. The inner mappings are from query two-tuples (name,
  127. type) to dictionaries suitable for use as **arguments to
  128. L{_respond}. See that method for details.
  129. """
  130. roots = ['1.1.2.3']
  131. resolver = Resolver(roots, maximumQueries)
  132. def query(query, serverAddresses, timeout, filter):
  133. msg("Query for QNAME %s at %r" % (query.name, serverAddresses))
  134. for addr in serverAddresses:
  135. try:
  136. server = serverResponses[addr]
  137. except KeyError:
  138. continue
  139. records = server[query.name, query.type]
  140. return succeed(self._respond(**records))
  141. resolver._query = query
  142. return resolver
  143. def test_lookupAddress(self):
  144. """
  145. L{root.Resolver.lookupAddress} looks up the I{A} records for the
  146. specified hostname by first querying one of the root servers the
  147. resolver was created with and then following the authority delegations
  148. until a result is received.
  149. """
  150. servers = {
  151. ('1.1.2.3', 53): {
  152. (b'foo.example.com', A): {
  153. 'authority': [(b'foo.example.com', Record_NS(b'ns1.example.com'))],
  154. 'additional': [(b'ns1.example.com', Record_A('34.55.89.144'))],
  155. },
  156. },
  157. ('34.55.89.144', 53): {
  158. (b'foo.example.com', A): {
  159. 'answers': [(b'foo.example.com', Record_A('10.0.0.1'))],
  160. }
  161. },
  162. }
  163. resolver = self._getResolver(servers)
  164. d = resolver.lookupAddress(b'foo.example.com')
  165. d.addCallback(getOneAddress)
  166. d.addCallback(self.assertEqual, '10.0.0.1')
  167. return d
  168. def test_lookupChecksClass(self):
  169. """
  170. If a response includes a record with a class different from the one
  171. in the query, it is ignored and lookup continues until a record with
  172. the right class is found.
  173. """
  174. badClass = Record_A('10.0.0.1')
  175. badClass.CLASS = HS
  176. servers = {
  177. ('1.1.2.3', 53): {
  178. (b'foo.example.com', A): {
  179. 'answers': [(b'foo.example.com', badClass)],
  180. 'authority': [(b'foo.example.com', Record_NS(b'ns1.example.com'))],
  181. 'additional': [(b'ns1.example.com', Record_A('10.0.0.2'))],
  182. },
  183. },
  184. ('10.0.0.2', 53): {
  185. (b'foo.example.com', A): {
  186. 'answers': [(b'foo.example.com', Record_A('10.0.0.3'))],
  187. },
  188. },
  189. }
  190. resolver = self._getResolver(servers)
  191. d = resolver.lookupAddress(b'foo.example.com')
  192. d.addCallback(getOnePayload)
  193. d.addCallback(self.assertEqual, Record_A('10.0.0.3'))
  194. return d
  195. def test_missingGlue(self):
  196. """
  197. If an intermediate response includes no glue records for the
  198. authorities, separate queries are made to find those addresses.
  199. """
  200. servers = {
  201. ('1.1.2.3', 53): {
  202. (b'foo.example.com', A): {
  203. 'authority': [(b'foo.example.com', Record_NS(b'ns1.example.org'))],
  204. # Conspicuous lack of an additional section naming ns1.example.com
  205. },
  206. (b'ns1.example.org', A): {
  207. 'answers': [(b'ns1.example.org', Record_A('10.0.0.1'))],
  208. },
  209. },
  210. ('10.0.0.1', 53): {
  211. (b'foo.example.com', A): {
  212. 'answers': [(b'foo.example.com', Record_A('10.0.0.2'))],
  213. },
  214. },
  215. }
  216. resolver = self._getResolver(servers)
  217. d = resolver.lookupAddress(b'foo.example.com')
  218. d.addCallback(getOneAddress)
  219. d.addCallback(self.assertEqual, '10.0.0.2')
  220. return d
  221. def test_missingName(self):
  222. """
  223. If a name is missing, L{Resolver.lookupAddress} returns a L{Deferred}
  224. which fails with L{DNSNameError}.
  225. """
  226. servers = {
  227. ('1.1.2.3', 53): {
  228. (b'foo.example.com', A): {
  229. 'rCode': ENAME,
  230. },
  231. },
  232. }
  233. resolver = self._getResolver(servers)
  234. d = resolver.lookupAddress(b'foo.example.com')
  235. return self.assertFailure(d, DNSNameError)
  236. def test_answerless(self):
  237. """
  238. If a query is responded to with no answers or nameserver records, the
  239. L{Deferred} returned by L{Resolver.lookupAddress} fires with
  240. L{ResolverError}.
  241. """
  242. servers = {
  243. ('1.1.2.3', 53): {
  244. (b'example.com', A): {
  245. },
  246. },
  247. }
  248. resolver = self._getResolver(servers)
  249. d = resolver.lookupAddress(b'example.com')
  250. return self.assertFailure(d, ResolverError)
  251. def test_delegationLookupError(self):
  252. """
  253. If there is an error resolving the nameserver in a delegation response,
  254. the L{Deferred} returned by L{Resolver.lookupAddress} fires with that
  255. error.
  256. """
  257. servers = {
  258. ('1.1.2.3', 53): {
  259. (b'example.com', A): {
  260. 'authority': [(b'example.com', Record_NS(b'ns1.example.com'))],
  261. },
  262. (b'ns1.example.com', A): {
  263. 'rCode': ENAME,
  264. },
  265. },
  266. }
  267. resolver = self._getResolver(servers)
  268. d = resolver.lookupAddress(b'example.com')
  269. return self.assertFailure(d, DNSNameError)
  270. def test_delegationLookupEmpty(self):
  271. """
  272. If there are no records in the response to a lookup of a delegation
  273. nameserver, the L{Deferred} returned by L{Resolver.lookupAddress} fires
  274. with L{ResolverError}.
  275. """
  276. servers = {
  277. ('1.1.2.3', 53): {
  278. (b'example.com', A): {
  279. 'authority': [(b'example.com', Record_NS(b'ns1.example.com'))],
  280. },
  281. (b'ns1.example.com', A): {
  282. },
  283. },
  284. }
  285. resolver = self._getResolver(servers)
  286. d = resolver.lookupAddress(b'example.com')
  287. return self.assertFailure(d, ResolverError)
  288. def test_lookupNameservers(self):
  289. """
  290. L{Resolver.lookupNameservers} is like L{Resolver.lookupAddress}, except
  291. it queries for I{NS} records instead of I{A} records.
  292. """
  293. servers = {
  294. ('1.1.2.3', 53): {
  295. (b'example.com', A): {
  296. 'rCode': ENAME,
  297. },
  298. (b'example.com', NS): {
  299. 'answers': [(b'example.com', Record_NS(b'ns1.example.com'))],
  300. },
  301. },
  302. }
  303. resolver = self._getResolver(servers)
  304. d = resolver.lookupNameservers(b'example.com')
  305. def getOneName(results):
  306. ans, auth, add = results
  307. return ans[0].payload.name
  308. d.addCallback(getOneName)
  309. d.addCallback(self.assertEqual, Name(b'ns1.example.com'))
  310. return d
  311. def test_returnCanonicalName(self):
  312. """
  313. If a I{CNAME} record is encountered as the answer to a query for
  314. another record type, that record is returned as the answer.
  315. """
  316. servers = {
  317. ('1.1.2.3', 53): {
  318. (b'example.com', A): {
  319. 'answers': [(b'example.com', Record_CNAME(b'example.net')),
  320. (b'example.net', Record_A('10.0.0.7'))],
  321. },
  322. },
  323. }
  324. resolver = self._getResolver(servers)
  325. d = resolver.lookupAddress(b'example.com')
  326. d.addCallback(lambda results: results[0]) # Get the answer section
  327. d.addCallback(
  328. self.assertEqual,
  329. [RRHeader(b'example.com', CNAME, payload=Record_CNAME(b'example.net')),
  330. RRHeader(b'example.net', A, payload=Record_A('10.0.0.7'))])
  331. return d
  332. def test_followCanonicalName(self):
  333. """
  334. If no record of the requested type is included in a response, but a
  335. I{CNAME} record for the query name is included, queries are made to
  336. resolve the value of the I{CNAME}.
  337. """
  338. servers = {
  339. ('1.1.2.3', 53): {
  340. (b'example.com', A): {
  341. 'answers': [(b'example.com', Record_CNAME(b'example.net'))],
  342. },
  343. (b'example.net', A): {
  344. 'answers': [(b'example.net', Record_A('10.0.0.5'))],
  345. },
  346. },
  347. }
  348. resolver = self._getResolver(servers)
  349. d = resolver.lookupAddress(b'example.com')
  350. d.addCallback(lambda results: results[0]) # Get the answer section
  351. d.addCallback(
  352. self.assertEqual,
  353. [RRHeader(b'example.com', CNAME, payload=Record_CNAME(b'example.net')),
  354. RRHeader(b'example.net', A, payload=Record_A('10.0.0.5'))])
  355. return d
  356. def test_detectCanonicalNameLoop(self):
  357. """
  358. If there is a cycle between I{CNAME} records in a response, this is
  359. detected and the L{Deferred} returned by the lookup method fails
  360. with L{ResolverError}.
  361. """
  362. servers = {
  363. ('1.1.2.3', 53): {
  364. (b'example.com', A): {
  365. 'answers': [(b'example.com', Record_CNAME(b'example.net')),
  366. (b'example.net', Record_CNAME(b'example.com'))],
  367. },
  368. },
  369. }
  370. resolver = self._getResolver(servers)
  371. d = resolver.lookupAddress(b'example.com')
  372. return self.assertFailure(d, ResolverError)
  373. def test_boundedQueries(self):
  374. """
  375. L{Resolver.lookupAddress} won't issue more queries following
  376. delegations than the limit passed to its initializer.
  377. """
  378. servers = {
  379. ('1.1.2.3', 53): {
  380. # First query - force it to start over with a name lookup of
  381. # ns1.example.com
  382. (b'example.com', A): {
  383. 'authority': [(b'example.com', Record_NS(b'ns1.example.com'))],
  384. },
  385. # Second query - let it resume the original lookup with the
  386. # address of the nameserver handling the delegation.
  387. (b'ns1.example.com', A): {
  388. 'answers': [(b'ns1.example.com', Record_A('10.0.0.2'))],
  389. },
  390. },
  391. ('10.0.0.2', 53): {
  392. # Third query - let it jump straight to asking the
  393. # delegation server by including its address here (different
  394. # case from the first query).
  395. (b'example.com', A): {
  396. 'authority': [(b'example.com', Record_NS(b'ns2.example.com'))],
  397. 'additional': [(b'ns2.example.com', Record_A('10.0.0.3'))],
  398. },
  399. },
  400. ('10.0.0.3', 53): {
  401. # Fourth query - give it the answer, we're done.
  402. (b'example.com', A): {
  403. 'answers': [(b'example.com', Record_A('10.0.0.4'))],
  404. },
  405. },
  406. }
  407. # Make two resolvers. One which is allowed to make 3 queries
  408. # maximum, and so will fail, and on which may make 4, and so should
  409. # succeed.
  410. failer = self._getResolver(servers, 3)
  411. failD = self.assertFailure(
  412. failer.lookupAddress(b'example.com'), ResolverError)
  413. succeeder = self._getResolver(servers, 4)
  414. succeedD = succeeder.lookupAddress(b'example.com')
  415. succeedD.addCallback(getOnePayload)
  416. succeedD.addCallback(self.assertEqual, Record_A('10.0.0.4'))
  417. return gatherResults([failD, succeedD])
  418. class ResolverFactoryArguments(Exception):
  419. """
  420. Raised by L{raisingResolverFactory} with the *args and **kwargs passed to
  421. that function.
  422. """
  423. def __init__(self, args, kwargs):
  424. """
  425. Store the supplied args and kwargs as attributes.
  426. @param args: Positional arguments.
  427. @param kwargs: Keyword arguments.
  428. """
  429. self.args = args
  430. self.kwargs = kwargs
  431. def raisingResolverFactory(*args, **kwargs):
  432. """
  433. Raise a L{ResolverFactoryArguments} exception containing the
  434. positional and keyword arguments passed to resolverFactory.
  435. @param args: A L{list} of all the positional arguments supplied by
  436. the caller.
  437. @param kwargs: A L{list} of all the keyword arguments supplied by
  438. the caller.
  439. """
  440. raise ResolverFactoryArguments(args, kwargs)
  441. class RootResolverResolverFactoryTests(TestCase):
  442. """
  443. Tests for L{root.Resolver._resolverFactory}.
  444. """
  445. def test_resolverFactoryArgumentPresent(self):
  446. """
  447. L{root.Resolver.__init__} accepts a C{resolverFactory}
  448. argument and assigns it to C{self._resolverFactory}.
  449. """
  450. r = Resolver(hints=[None], resolverFactory=raisingResolverFactory)
  451. self.assertIs(r._resolverFactory, raisingResolverFactory)
  452. def test_resolverFactoryArgumentAbsent(self):
  453. """
  454. L{root.Resolver.__init__} sets L{client.Resolver} as the
  455. C{_resolverFactory} if a C{resolverFactory} argument is not
  456. supplied.
  457. """
  458. r = Resolver(hints=[None])
  459. self.assertIs(r._resolverFactory, client.Resolver)
  460. def test_resolverFactoryOnlyExpectedArguments(self):
  461. """
  462. L{root.Resolver._resolverFactory} is supplied with C{reactor} and
  463. C{servers} keyword arguments.
  464. """
  465. dummyReactor = object()
  466. r = Resolver(hints=['192.0.2.101'],
  467. resolverFactory=raisingResolverFactory,
  468. reactor=dummyReactor)
  469. e = self.assertRaises(ResolverFactoryArguments,
  470. r.lookupAddress, 'example.com')
  471. self.assertEqual(
  472. ((), {'reactor': dummyReactor, 'servers': [('192.0.2.101', 53)]}),
  473. (e.args, e.kwargs)
  474. )
  475. ROOT_SERVERS = [
  476. 'a.root-servers.net',
  477. 'b.root-servers.net',
  478. 'c.root-servers.net',
  479. 'd.root-servers.net',
  480. 'e.root-servers.net',
  481. 'f.root-servers.net',
  482. 'g.root-servers.net',
  483. 'h.root-servers.net',
  484. 'i.root-servers.net',
  485. 'j.root-servers.net',
  486. 'k.root-servers.net',
  487. 'l.root-servers.net',
  488. 'm.root-servers.net']
  489. @implementer(IResolverSimple)
  490. class StubResolver(object):
  491. """
  492. An L{IResolverSimple} implementer which traces all getHostByName
  493. calls and their deferred results. The deferred results can be
  494. accessed and fired synchronously.
  495. """
  496. def __init__(self):
  497. """
  498. @type calls: L{list} of L{tuple} containing C{args} and
  499. C{kwargs} supplied to C{getHostByName} calls.
  500. @type pendingResults: L{list} of L{Deferred} returned by
  501. C{getHostByName}.
  502. """
  503. self.calls = []
  504. self.pendingResults = []
  505. def getHostByName(self, *args, **kwargs):
  506. """
  507. A fake implementation of L{IResolverSimple.getHostByName}
  508. @param args: A L{list} of all the positional arguments supplied by
  509. the caller.
  510. @param kwargs: A L{list} of all the keyword arguments supplied by
  511. the caller.
  512. @return: A L{Deferred} which may be fired later from the test
  513. fixture.
  514. """
  515. self.calls.append((args, kwargs))
  516. d = Deferred()
  517. self.pendingResults.append(d)
  518. return d
  519. verifyClass(IResolverSimple, StubResolver)
  520. class BootstrapTests(SynchronousTestCase):
  521. """
  522. Tests for L{root.bootstrap}
  523. """
  524. def test_returnsDeferredResolver(self):
  525. """
  526. L{root.bootstrap} returns an object which is initially a
  527. L{root.DeferredResolver}.
  528. """
  529. deferredResolver = root.bootstrap(StubResolver())
  530. self.assertIsInstance(deferredResolver, root.DeferredResolver)
  531. def test_resolves13RootServers(self):
  532. """
  533. The L{IResolverSimple} supplied to L{root.bootstrap} is used to lookup
  534. the IP addresses of the 13 root name servers.
  535. """
  536. stubResolver = StubResolver()
  537. root.bootstrap(stubResolver)
  538. self.assertEqual(
  539. stubResolver.calls,
  540. [((s,), {}) for s in ROOT_SERVERS])
  541. def test_becomesResolver(self):
  542. """
  543. The L{root.DeferredResolver} initially returned by L{root.bootstrap}
  544. becomes a L{root.Resolver} when the supplied resolver has successfully
  545. looked up all root hints.
  546. """
  547. stubResolver = StubResolver()
  548. deferredResolver = root.bootstrap(stubResolver)
  549. for d in stubResolver.pendingResults:
  550. d.callback('192.0.2.101')
  551. self.assertIsInstance(deferredResolver, Resolver)
  552. def test_resolverReceivesRootHints(self):
  553. """
  554. The L{root.Resolver} which eventually replaces L{root.DeferredResolver}
  555. is supplied with the IP addresses of the 13 root servers.
  556. """
  557. stubResolver = StubResolver()
  558. deferredResolver = root.bootstrap(stubResolver)
  559. for d in stubResolver.pendingResults:
  560. d.callback('192.0.2.101')
  561. self.assertEqual(deferredResolver.hints, ['192.0.2.101'] * 13)
  562. def test_continuesWhenSomeRootHintsFail(self):
  563. """
  564. The L{root.Resolver} is eventually created, even if some of the root
  565. hint lookups fail. Only the working root hint IP addresses are supplied
  566. to the L{root.Resolver}.
  567. """
  568. stubResolver = StubResolver()
  569. deferredResolver = root.bootstrap(stubResolver)
  570. results = iter(stubResolver.pendingResults)
  571. d1 = next(results)
  572. for d in results:
  573. d.callback('192.0.2.101')
  574. d1.errback(TimeoutError())
  575. def checkHints(res):
  576. self.assertEqual(deferredResolver.hints, ['192.0.2.101'] * 12)
  577. d1.addBoth(checkHints)
  578. def test_continuesWhenAllRootHintsFail(self):
  579. """
  580. The L{root.Resolver} is eventually created, even if all of the root hint
  581. lookups fail. Pending and new lookups will then fail with
  582. AttributeError.
  583. """
  584. stubResolver = StubResolver()
  585. deferredResolver = root.bootstrap(stubResolver)
  586. results = iter(stubResolver.pendingResults)
  587. d1 = next(results)
  588. for d in results:
  589. d.errback(TimeoutError())
  590. d1.errback(TimeoutError())
  591. def checkHints(res):
  592. self.assertEqual(deferredResolver.hints, [])
  593. d1.addBoth(checkHints)
  594. self.addCleanup(self.flushLoggedErrors, TimeoutError)
  595. def test_passesResolverFactory(self):
  596. """
  597. L{root.bootstrap} accepts a C{resolverFactory} argument which is passed
  598. as an argument to L{root.Resolver} when it has successfully looked up
  599. root hints.
  600. """
  601. stubResolver = StubResolver()
  602. deferredResolver = root.bootstrap(
  603. stubResolver, resolverFactory=raisingResolverFactory)
  604. for d in stubResolver.pendingResults:
  605. d.callback('192.0.2.101')
  606. self.assertIs(
  607. deferredResolver._resolverFactory, raisingResolverFactory)
  608. class StubDNSDatagramProtocol:
  609. """
  610. A do-nothing stand-in for L{DNSDatagramProtocol} which can be used to avoid
  611. network traffic in tests where that kind of thing doesn't matter.
  612. """
  613. def query(self, *a, **kw):
  614. return Deferred()
  615. _retrySuppression = util.suppress(
  616. category=DeprecationWarning,
  617. message=(
  618. 'twisted.names.root.retry is deprecated since Twisted 10.0. Use a '
  619. 'Resolver object for retry logic.'))