test_names.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for twisted.names.
  5. """
  6. from __future__ import absolute_import, division
  7. import copy
  8. import operator
  9. import socket
  10. from io import BytesIO
  11. from functools import partial, reduce
  12. from struct import pack
  13. from twisted.trial import unittest
  14. from twisted.internet import reactor, defer, error
  15. from twisted.internet.defer import succeed
  16. from twisted.names import client, server, common, authority, dns
  17. from twisted.names.dns import SOA, Message, RRHeader, Record_A, Record_SOA
  18. from twisted.names.error import DomainError
  19. from twisted.names.client import Resolver
  20. from twisted.names.secondary import (
  21. SecondaryAuthorityService, SecondaryAuthority)
  22. from twisted.python.compat import nativeString
  23. from twisted.python.filepath import FilePath
  24. from twisted.test.proto_helpers import (
  25. StringTransport, MemoryReactorClock, waitUntilAllDisconnected)
  26. def justPayload(results):
  27. return [r.payload for r in results[0]]
  28. class NoFileAuthority(authority.FileAuthority):
  29. def __init__(self, soa, records):
  30. # Yes, skip FileAuthority
  31. common.ResolverBase.__init__(self)
  32. self.soa, self.records = soa, records
  33. soa_record = dns.Record_SOA(
  34. mname = b'test-domain.com',
  35. rname = u'root.test-domain.com',
  36. serial = 100,
  37. refresh = 1234,
  38. minimum = 7654,
  39. expire = 19283784,
  40. retry = 15,
  41. ttl=1
  42. )
  43. reverse_soa = dns.Record_SOA(
  44. mname = b'93.84.28.in-addr.arpa',
  45. rname = b'93.84.28.in-addr.arpa',
  46. serial = 120,
  47. refresh = 54321,
  48. minimum = 382,
  49. expire = 11193983,
  50. retry = 30,
  51. ttl=3
  52. )
  53. my_soa = dns.Record_SOA(
  54. mname = u'my-domain.com',
  55. rname = b'postmaster.test-domain.com',
  56. serial = 130,
  57. refresh = 12345,
  58. minimum = 1,
  59. expire = 999999,
  60. retry = 100,
  61. )
  62. test_domain_com = NoFileAuthority(
  63. soa = (b'test-domain.com', soa_record),
  64. records = {
  65. b'test-domain.com': [
  66. soa_record,
  67. dns.Record_A(b'127.0.0.1'),
  68. dns.Record_NS(b'39.28.189.39'),
  69. dns.Record_SPF(b'v=spf1 mx/30 mx:example.org/30 -all'),
  70. dns.Record_SPF(b'v=spf1 +mx a:\0colo',
  71. b'.example.com/28 -all not valid'),
  72. dns.Record_MX(10, u'host.test-domain.com'),
  73. dns.Record_HINFO(os=b'Linux', cpu=b'A Fast One, Dontcha know'),
  74. dns.Record_CNAME(b'canonical.name.com'),
  75. dns.Record_MB(b'mailbox.test-domain.com'),
  76. dns.Record_MG(b'mail.group.someplace'),
  77. dns.Record_TXT(b'A First piece of Text', b'a SecoNd piece'),
  78. dns.Record_A6(0, b'ABCD::4321', b''),
  79. dns.Record_A6(12, b'0:0069::0', b'some.network.tld'),
  80. dns.Record_A6(8, b'0:5634:1294:AFCB:56AC:48EF:34C3:01FF',
  81. b'tra.la.la.net'),
  82. dns.Record_TXT(b'Some more text, haha! Yes. \0 Still here?'),
  83. dns.Record_MR(b'mail.redirect.or.whatever'),
  84. dns.Record_MINFO(rmailbx=b'r mail box', emailbx=b'e mail box'),
  85. dns.Record_AFSDB(subtype=1, hostname=b'afsdb.test-domain.com'),
  86. dns.Record_RP(mbox=b'whatever.i.dunno', txt=b'some.more.text'),
  87. dns.Record_WKS(b'12.54.78.12', socket.IPPROTO_TCP,
  88. b'\x12\x01\x16\xfe\xc1\x00\x01'),
  89. dns.Record_NAPTR(100, 10, b"u", b"sip+E2U",
  90. b"!^.*$!sip:information@domain.tld!"),
  91. dns.Record_AAAA(b'AF43:5634:1294:AFCB:56AC:48EF:34C3:01FF')],
  92. b'http.tcp.test-domain.com': [
  93. dns.Record_SRV(257, 16383, 43690, b'some.other.place.fool')
  94. ],
  95. b'host.test-domain.com': [
  96. dns.Record_A(b'123.242.1.5'),
  97. dns.Record_A(b'0.255.0.255'),
  98. ],
  99. b'host-two.test-domain.com': [
  100. #
  101. # Python bug
  102. # dns.Record_A('255.255.255.255'),
  103. #
  104. dns.Record_A(b'255.255.255.254'),
  105. dns.Record_A(b'0.0.0.0')
  106. ],
  107. b'cname.test-domain.com': [
  108. dns.Record_CNAME(b'test-domain.com')
  109. ],
  110. b'anothertest-domain.com': [
  111. dns.Record_A(b'1.2.3.4')],
  112. }
  113. )
  114. reverse_domain = NoFileAuthority(
  115. soa = (b'93.84.28.in-addr.arpa', reverse_soa),
  116. records = {
  117. b'123.93.84.28.in-addr.arpa': [
  118. dns.Record_PTR(b'test.host-reverse.lookup.com'),
  119. reverse_soa
  120. ]
  121. }
  122. )
  123. my_domain_com = NoFileAuthority(
  124. soa = (b'my-domain.com', my_soa),
  125. records = {
  126. b'my-domain.com': [
  127. my_soa,
  128. dns.Record_A(b'1.2.3.4', ttl='1S'),
  129. dns.Record_NS(b'ns1.domain', ttl=b'2M'),
  130. dns.Record_NS(b'ns2.domain', ttl='3H'),
  131. dns.Record_SRV(257, 16383, 43690, b'some.other.place.fool',
  132. ttl='4D')
  133. ]
  134. }
  135. )
  136. class ServerDNSTests(unittest.TestCase):
  137. """
  138. Test cases for DNS server and client.
  139. """
  140. def setUp(self):
  141. self.factory = server.DNSServerFactory([
  142. test_domain_com, reverse_domain, my_domain_com
  143. ], verbose=2)
  144. p = dns.DNSDatagramProtocol(self.factory)
  145. while 1:
  146. listenerTCP = reactor.listenTCP(0, self.factory, interface="127.0.0.1")
  147. # It's simpler to do the stop listening with addCleanup,
  148. # even though we might not end up using this TCP port in
  149. # the test (if the listenUDP below fails). Cleaning up
  150. # this TCP port sooner than "cleanup time" would mean
  151. # adding more code to keep track of the Deferred returned
  152. # by stopListening.
  153. self.addCleanup(listenerTCP.stopListening)
  154. port = listenerTCP.getHost().port
  155. try:
  156. listenerUDP = reactor.listenUDP(port, p, interface="127.0.0.1")
  157. except error.CannotListenError:
  158. pass
  159. else:
  160. self.addCleanup(listenerUDP.stopListening)
  161. break
  162. self.listenerTCP = listenerTCP
  163. self.listenerUDP = listenerUDP
  164. self.resolver = client.Resolver(servers=[('127.0.0.1', port)])
  165. def tearDown(self):
  166. """
  167. Clean up any server connections associated with the
  168. L{DNSServerFactory} created in L{setUp}
  169. """
  170. # It'd be great if DNSServerFactory had a method that
  171. # encapsulated this task. At least the necessary data is
  172. # available, though.
  173. for conn in self.factory.connections[:]:
  174. conn.transport.loseConnection()
  175. return waitUntilAllDisconnected(reactor, self.factory.connections[:])
  176. def namesTest(self, querying, expectedRecords):
  177. """
  178. Assert that the DNS response C{querying} will eventually fire with
  179. contains exactly a certain collection of records.
  180. @param querying: A L{Deferred} returned from one of the DNS client
  181. I{lookup} methods.
  182. @param expectedRecords: A L{list} of L{IRecord} providers which must be
  183. in the response or the test will be failed.
  184. @return: A L{Deferred} that fires when the assertion has been made. It
  185. fires with a success result if the assertion succeeds and with a
  186. L{Failure} if it fails.
  187. """
  188. def checkResults(response):
  189. receivedRecords = justPayload(response)
  190. self.assertEqual(set(expectedRecords), set(receivedRecords))
  191. querying.addCallback(checkResults)
  192. return querying
  193. def test_addressRecord1(self):
  194. """Test simple DNS 'A' record queries"""
  195. return self.namesTest(
  196. self.resolver.lookupAddress('test-domain.com'),
  197. [dns.Record_A('127.0.0.1', ttl=19283784)]
  198. )
  199. def test_addressRecord2(self):
  200. """Test DNS 'A' record queries with multiple answers"""
  201. return self.namesTest(
  202. self.resolver.lookupAddress('host.test-domain.com'),
  203. [dns.Record_A('123.242.1.5', ttl=19283784),
  204. dns.Record_A('0.255.0.255', ttl=19283784)]
  205. )
  206. def test_addressRecord3(self):
  207. """Test DNS 'A' record queries with edge cases"""
  208. return self.namesTest(
  209. self.resolver.lookupAddress('host-two.test-domain.com'),
  210. [dns.Record_A('255.255.255.254', ttl=19283784), dns.Record_A('0.0.0.0', ttl=19283784)]
  211. )
  212. def test_authority(self):
  213. """Test DNS 'SOA' record queries"""
  214. return self.namesTest(
  215. self.resolver.lookupAuthority('test-domain.com'),
  216. [soa_record]
  217. )
  218. def test_mailExchangeRecord(self):
  219. """
  220. The DNS client can issue an MX query and receive a response including
  221. an MX record as well as any A record hints.
  222. """
  223. return self.namesTest(
  224. self.resolver.lookupMailExchange(b"test-domain.com"),
  225. [dns.Record_MX(10, b"host.test-domain.com", ttl=19283784),
  226. dns.Record_A(b"123.242.1.5", ttl=19283784),
  227. dns.Record_A(b"0.255.0.255", ttl=19283784)])
  228. def test_nameserver(self):
  229. """Test DNS 'NS' record queries"""
  230. return self.namesTest(
  231. self.resolver.lookupNameservers('test-domain.com'),
  232. [dns.Record_NS('39.28.189.39', ttl=19283784)]
  233. )
  234. def test_HINFO(self):
  235. """Test DNS 'HINFO' record queries"""
  236. return self.namesTest(
  237. self.resolver.lookupHostInfo('test-domain.com'),
  238. [dns.Record_HINFO(os=b'Linux', cpu=b'A Fast One, Dontcha know',
  239. ttl=19283784)]
  240. )
  241. def test_PTR(self):
  242. """Test DNS 'PTR' record queries"""
  243. return self.namesTest(
  244. self.resolver.lookupPointer('123.93.84.28.in-addr.arpa'),
  245. [dns.Record_PTR('test.host-reverse.lookup.com', ttl=11193983)]
  246. )
  247. def test_CNAME(self):
  248. """Test DNS 'CNAME' record queries"""
  249. return self.namesTest(
  250. self.resolver.lookupCanonicalName('test-domain.com'),
  251. [dns.Record_CNAME('canonical.name.com', ttl=19283784)]
  252. )
  253. def test_MB(self):
  254. """Test DNS 'MB' record queries"""
  255. return self.namesTest(
  256. self.resolver.lookupMailBox('test-domain.com'),
  257. [dns.Record_MB('mailbox.test-domain.com', ttl=19283784)]
  258. )
  259. def test_MG(self):
  260. """Test DNS 'MG' record queries"""
  261. return self.namesTest(
  262. self.resolver.lookupMailGroup('test-domain.com'),
  263. [dns.Record_MG('mail.group.someplace', ttl=19283784)]
  264. )
  265. def test_MR(self):
  266. """Test DNS 'MR' record queries"""
  267. return self.namesTest(
  268. self.resolver.lookupMailRename('test-domain.com'),
  269. [dns.Record_MR('mail.redirect.or.whatever', ttl=19283784)]
  270. )
  271. def test_MINFO(self):
  272. """Test DNS 'MINFO' record queries"""
  273. return self.namesTest(
  274. self.resolver.lookupMailboxInfo('test-domain.com'),
  275. [dns.Record_MINFO(rmailbx='r mail box', emailbx='e mail box', ttl=19283784)]
  276. )
  277. def test_SRV(self):
  278. """Test DNS 'SRV' record queries"""
  279. return self.namesTest(
  280. self.resolver.lookupService('http.tcp.test-domain.com'),
  281. [dns.Record_SRV(257, 16383, 43690, 'some.other.place.fool', ttl=19283784)]
  282. )
  283. def test_AFSDB(self):
  284. """Test DNS 'AFSDB' record queries"""
  285. return self.namesTest(
  286. self.resolver.lookupAFSDatabase('test-domain.com'),
  287. [dns.Record_AFSDB(subtype=1, hostname='afsdb.test-domain.com', ttl=19283784)]
  288. )
  289. def test_RP(self):
  290. """Test DNS 'RP' record queries"""
  291. return self.namesTest(
  292. self.resolver.lookupResponsibility('test-domain.com'),
  293. [dns.Record_RP(mbox='whatever.i.dunno', txt='some.more.text', ttl=19283784)]
  294. )
  295. def test_TXT(self):
  296. """Test DNS 'TXT' record queries"""
  297. return self.namesTest(
  298. self.resolver.lookupText('test-domain.com'),
  299. [dns.Record_TXT(b'A First piece of Text', b'a SecoNd piece',
  300. ttl=19283784),
  301. dns.Record_TXT(b'Some more text, haha! Yes. \0 Still here?',
  302. ttl=19283784)]
  303. )
  304. def test_spf(self):
  305. """
  306. L{DNSServerFactory} can serve I{SPF} resource records.
  307. """
  308. return self.namesTest(
  309. self.resolver.lookupSenderPolicy('test-domain.com'),
  310. [dns.Record_SPF(b'v=spf1 mx/30 mx:example.org/30 -all',
  311. ttl=19283784),
  312. dns.Record_SPF(b'v=spf1 +mx a:\0colo',
  313. b'.example.com/28 -all not valid', ttl=19283784)]
  314. )
  315. def test_WKS(self):
  316. """Test DNS 'WKS' record queries"""
  317. return self.namesTest(
  318. self.resolver.lookupWellKnownServices('test-domain.com'),
  319. [dns.Record_WKS('12.54.78.12', socket.IPPROTO_TCP,
  320. b'\x12\x01\x16\xfe\xc1\x00\x01', ttl=19283784)]
  321. )
  322. def test_someRecordsWithTTLs(self):
  323. result_soa = copy.copy(my_soa)
  324. result_soa.ttl = my_soa.expire
  325. return self.namesTest(
  326. self.resolver.lookupAllRecords('my-domain.com'),
  327. [result_soa,
  328. dns.Record_A('1.2.3.4', ttl='1S'),
  329. dns.Record_NS('ns1.domain', ttl='2M'),
  330. dns.Record_NS('ns2.domain', ttl='3H'),
  331. dns.Record_SRV(257, 16383, 43690, 'some.other.place.fool', ttl='4D')]
  332. )
  333. def test_AAAA(self):
  334. """Test DNS 'AAAA' record queries (IPv6)"""
  335. return self.namesTest(
  336. self.resolver.lookupIPV6Address('test-domain.com'),
  337. [dns.Record_AAAA('AF43:5634:1294:AFCB:56AC:48EF:34C3:01FF', ttl=19283784)]
  338. )
  339. def test_A6(self):
  340. """Test DNS 'A6' record queries (IPv6)"""
  341. return self.namesTest(
  342. self.resolver.lookupAddress6('test-domain.com'),
  343. [dns.Record_A6(0, 'ABCD::4321', '', ttl=19283784),
  344. dns.Record_A6(12, '0:0069::0', 'some.network.tld', ttl=19283784),
  345. dns.Record_A6(8, '0:5634:1294:AFCB:56AC:48EF:34C3:01FF', 'tra.la.la.net', ttl=19283784)]
  346. )
  347. def test_zoneTransfer(self):
  348. """
  349. Test DNS 'AXFR' queries (Zone transfer)
  350. """
  351. default_ttl = soa_record.expire
  352. results = [copy.copy(r) for r in reduce(operator.add, test_domain_com.records.values())]
  353. for r in results:
  354. if r.ttl is None:
  355. r.ttl = default_ttl
  356. return self.namesTest(
  357. self.resolver.lookupZone('test-domain.com').addCallback(lambda r: (r[0][:-1],)),
  358. results
  359. )
  360. def test_similarZonesDontInterfere(self):
  361. """Tests that unrelated zones don't mess with each other."""
  362. return self.namesTest(
  363. self.resolver.lookupAddress("anothertest-domain.com"),
  364. [dns.Record_A('1.2.3.4', ttl=19283784)]
  365. )
  366. def test_NAPTR(self):
  367. """
  368. Test DNS 'NAPTR' record queries.
  369. """
  370. return self.namesTest(
  371. self.resolver.lookupNamingAuthorityPointer('test-domain.com'),
  372. [dns.Record_NAPTR(100, 10, b"u", b"sip+E2U",
  373. b"!^.*$!sip:information@domain.tld!",
  374. ttl=19283784)])
  375. class HelperTests(unittest.TestCase):
  376. def test_serialGenerator(self):
  377. f = self.mktemp()
  378. a = authority.getSerial(f)
  379. for i in range(20):
  380. b = authority.getSerial(f)
  381. self.assertTrue(a < b)
  382. a = b
  383. class AXFRTests(unittest.TestCase):
  384. def setUp(self):
  385. self.results = None
  386. self.d = defer.Deferred()
  387. self.d.addCallback(self._gotResults)
  388. self.controller = client.AXFRController('fooby.com', self.d)
  389. self.soa = dns.RRHeader(name='fooby.com', type=dns.SOA, cls=dns.IN, ttl=86400, auth=False,
  390. payload=dns.Record_SOA(mname='fooby.com',
  391. rname='hooj.fooby.com',
  392. serial=100,
  393. refresh=200,
  394. retry=300,
  395. expire=400,
  396. minimum=500,
  397. ttl=600))
  398. self.records = [
  399. self.soa,
  400. dns.RRHeader(name='fooby.com', type=dns.NS, cls=dns.IN, ttl=700, auth=False,
  401. payload=dns.Record_NS(name='ns.twistedmatrix.com', ttl=700)),
  402. dns.RRHeader(name='fooby.com', type=dns.MX, cls=dns.IN, ttl=700, auth=False,
  403. payload=dns.Record_MX(preference=10, exchange='mail.mv3d.com', ttl=700)),
  404. dns.RRHeader(name='fooby.com', type=dns.A, cls=dns.IN, ttl=700, auth=False,
  405. payload=dns.Record_A(address='64.123.27.105', ttl=700)),
  406. self.soa
  407. ]
  408. def _makeMessage(self):
  409. # hooray they all have the same message format
  410. return dns.Message(id=999, answer=1, opCode=0, recDes=0, recAv=1, auth=1, rCode=0, trunc=0, maxSize=0)
  411. def test_bindAndTNamesStyle(self):
  412. # Bind style = One big single message
  413. m = self._makeMessage()
  414. m.queries = [dns.Query('fooby.com', dns.AXFR, dns.IN)]
  415. m.answers = self.records
  416. self.controller.messageReceived(m, None)
  417. self.assertEqual(self.results, self.records)
  418. def _gotResults(self, result):
  419. self.results = result
  420. def test_DJBStyle(self):
  421. # DJB style = message per record
  422. records = self.records[:]
  423. while records:
  424. m = self._makeMessage()
  425. m.queries = [] # DJB *doesn't* specify any queries.. hmm..
  426. m.answers = [records.pop(0)]
  427. self.controller.messageReceived(m, None)
  428. self.assertEqual(self.results, self.records)
  429. class ResolvConfHandlingTests(unittest.TestCase):
  430. def test_missing(self):
  431. resolvConf = self.mktemp()
  432. r = client.Resolver(resolv=resolvConf)
  433. self.assertEqual(r.dynServers, [('127.0.0.1', 53)])
  434. r._parseCall.cancel()
  435. def test_empty(self):
  436. resolvConf = self.mktemp()
  437. open(resolvConf, 'w').close()
  438. r = client.Resolver(resolv=resolvConf)
  439. self.assertEqual(r.dynServers, [('127.0.0.1', 53)])
  440. r._parseCall.cancel()
  441. class AuthorityTests(unittest.TestCase):
  442. """
  443. Tests for the basic response record selection code in L{FileAuthority}
  444. (independent of its fileness).
  445. """
  446. def test_domainErrorForNameWithCommonSuffix(self):
  447. """
  448. L{FileAuthority} lookup methods errback with L{DomainError} if
  449. the requested C{name} shares a common suffix with its zone but
  450. is not actually a descendant of its zone, in terms of its
  451. sequence of DNS name labels. eg www.the-example.com has
  452. nothing to do with the zone example.com.
  453. """
  454. testDomain = test_domain_com
  455. testDomainName = b'nonexistent.prefix-' + testDomain.soa[0]
  456. f = self.failureResultOf(testDomain.lookupAddress(testDomainName))
  457. self.assertIsInstance(f.value, DomainError)
  458. def test_recordMissing(self):
  459. """
  460. If a L{FileAuthority} has a zone which includes an I{NS} record for a
  461. particular name and that authority is asked for another record for the
  462. same name which does not exist, the I{NS} record is not included in the
  463. authority section of the response.
  464. """
  465. authority = NoFileAuthority(
  466. soa=(str(soa_record.mname), soa_record),
  467. records={
  468. str(soa_record.mname): [
  469. soa_record,
  470. dns.Record_NS('1.2.3.4'),
  471. ]})
  472. d = authority.lookupAddress(str(soa_record.mname))
  473. result = []
  474. d.addCallback(result.append)
  475. answer, authority, additional = result[0]
  476. self.assertEqual(answer, [])
  477. self.assertEqual(
  478. authority, [
  479. dns.RRHeader(
  480. str(soa_record.mname), soa_record.TYPE,
  481. ttl=soa_record.expire, payload=soa_record,
  482. auth=True)])
  483. self.assertEqual(additional, [])
  484. def _referralTest(self, method):
  485. """
  486. Create an authority and make a request against it. Then verify that the
  487. result is a referral, including no records in the answers or additional
  488. sections, but with an I{NS} record in the authority section.
  489. """
  490. subdomain = 'example.' + str(soa_record.mname)
  491. nameserver = dns.Record_NS('1.2.3.4')
  492. authority = NoFileAuthority(
  493. soa=(str(soa_record.mname), soa_record),
  494. records={
  495. subdomain: [
  496. nameserver,
  497. ]})
  498. d = getattr(authority, method)(subdomain)
  499. answer, authority, additional = self.successResultOf(d)
  500. self.assertEqual(answer, [])
  501. self.assertEqual(
  502. authority, [dns.RRHeader(
  503. subdomain, dns.NS, ttl=soa_record.expire,
  504. payload=nameserver, auth=False)])
  505. self.assertEqual(additional, [])
  506. def test_referral(self):
  507. """
  508. When an I{NS} record is found for a child zone, it is included in the
  509. authority section of the response. It is marked as non-authoritative if
  510. the authority is not also authoritative for the child zone (RFC 2181,
  511. section 6.1).
  512. """
  513. self._referralTest('lookupAddress')
  514. def test_allRecordsReferral(self):
  515. """
  516. A referral is also generated for a request of type C{ALL_RECORDS}.
  517. """
  518. self._referralTest('lookupAllRecords')
  519. class AdditionalProcessingTests(unittest.TestCase):
  520. """
  521. Tests for L{FileAuthority}'s additional processing for those record types
  522. which require it (MX, CNAME, etc).
  523. """
  524. _A = dns.Record_A(b"10.0.0.1")
  525. _AAAA = dns.Record_AAAA(b"f080::1")
  526. def _lookupSomeRecords(self, method, soa, makeRecord, target, addresses):
  527. """
  528. Perform a DNS lookup against a L{FileAuthority} configured with records
  529. as defined by C{makeRecord} and C{addresses}.
  530. @param method: The name of the lookup method to use; for example,
  531. C{"lookupNameservers"}.
  532. @type method: L{str}
  533. @param soa: A L{Record_SOA} for the zone for which the L{FileAuthority}
  534. is authoritative.
  535. @param makeRecord: A one-argument callable which accepts a name and
  536. returns an L{IRecord} provider. L{FileAuthority} is constructed
  537. with this record. The L{FileAuthority} is queried for a record of
  538. the resulting type with the given name.
  539. @param target: The extra name which the record returned by
  540. C{makeRecord} will be pointed at; this is the name which might
  541. require extra processing by the server so that all the available,
  542. useful information is returned. For example, this is the target of
  543. a CNAME record or the mail exchange host pointed to by an MX record.
  544. @type target: L{bytes}
  545. @param addresses: A L{list} of records giving addresses of C{target}.
  546. @return: A L{Deferred} that fires with the result of the resolver
  547. method give by C{method}.
  548. """
  549. authority = NoFileAuthority(
  550. soa=(soa.mname.name, soa),
  551. records={
  552. soa.mname.name: [makeRecord(target)],
  553. target: addresses,
  554. },
  555. )
  556. return getattr(authority, method)(soa_record.mname.name)
  557. def assertRecordsMatch(self, expected, computed):
  558. """
  559. Assert that the L{RRHeader} instances given by C{expected} and
  560. C{computed} carry all the same information but without requiring the
  561. records appear in the same order.
  562. @param expected: A L{list} of L{RRHeader} instances giving the expected
  563. records.
  564. @param computed: A L{list} of L{RRHeader} instances giving the records
  565. computed by the scenario under test.
  566. @raise self.failureException: If the two collections of records
  567. disagree.
  568. """
  569. # RRHeader instances aren't inherently ordered. Impose an ordering
  570. # that's good enough for the purposes of these tests - in which we
  571. # never have more than one record of a particular type.
  572. key = lambda rr: rr.type
  573. self.assertEqual(sorted(expected, key=key), sorted(computed, key=key))
  574. def _additionalTest(self, method, makeRecord, addresses):
  575. """
  576. Verify that certain address records are included in the I{additional}
  577. section of a response generated by L{FileAuthority}.
  578. @param method: See L{_lookupSomeRecords}
  579. @param makeRecord: See L{_lookupSomeRecords}
  580. @param addresses: A L{list} of L{IRecord} providers which the
  581. I{additional} section of the response is required to match
  582. (ignoring order).
  583. @raise self.failureException: If the I{additional} section of the
  584. response consists of different records than those given by
  585. C{addresses}.
  586. """
  587. target = b"mail." + soa_record.mname.name
  588. d = self._lookupSomeRecords(
  589. method, soa_record, makeRecord, target, addresses)
  590. answer, authority, additional = self.successResultOf(d)
  591. self.assertRecordsMatch(
  592. [dns.RRHeader(
  593. target, address.TYPE, ttl=soa_record.expire, payload=address,
  594. auth=True)
  595. for address in addresses],
  596. additional)
  597. def _additionalMXTest(self, addresses):
  598. """
  599. Verify that a response to an MX query has certain records in the
  600. I{additional} section.
  601. @param addresses: See C{_additionalTest}
  602. """
  603. self._additionalTest(
  604. "lookupMailExchange", partial(dns.Record_MX, 10), addresses)
  605. def test_mailExchangeAdditionalA(self):
  606. """
  607. If the name of the MX response has A records, they are included in the
  608. additional section of the response.
  609. """
  610. self._additionalMXTest([self._A])
  611. def test_mailExchangeAdditionalAAAA(self):
  612. """
  613. If the name of the MX response has AAAA records, they are included in
  614. the additional section of the response.
  615. """
  616. self._additionalMXTest([self._AAAA])
  617. def test_mailExchangeAdditionalBoth(self):
  618. """
  619. If the name of the MX response has both A and AAAA records, they are
  620. all included in the additional section of the response.
  621. """
  622. self._additionalMXTest([self._A, self._AAAA])
  623. def _additionalNSTest(self, addresses):
  624. """
  625. Verify that a response to an NS query has certain records in the
  626. I{additional} section.
  627. @param addresses: See C{_additionalTest}
  628. """
  629. self._additionalTest(
  630. "lookupNameservers", dns.Record_NS, addresses)
  631. def test_nameserverAdditionalA(self):
  632. """
  633. If the name of the NS response has A records, they are included in the
  634. additional section of the response.
  635. """
  636. self._additionalNSTest([self._A])
  637. def test_nameserverAdditionalAAAA(self):
  638. """
  639. If the name of the NS response has AAAA records, they are included in
  640. the additional section of the response.
  641. """
  642. self._additionalNSTest([self._AAAA])
  643. def test_nameserverAdditionalBoth(self):
  644. """
  645. If the name of the NS response has both A and AAAA records, they are
  646. all included in the additional section of the response.
  647. """
  648. self._additionalNSTest([self._A, self._AAAA])
  649. def _answerCNAMETest(self, addresses):
  650. """
  651. Verify that a response to a CNAME query has certain records in the
  652. I{answer} section.
  653. @param addresses: See C{_additionalTest}
  654. """
  655. target = b"www." + soa_record.mname.name
  656. d = self._lookupSomeRecords(
  657. "lookupCanonicalName", soa_record, dns.Record_CNAME, target,
  658. addresses)
  659. answer, authority, additional = self.successResultOf(d)
  660. alias = dns.RRHeader(
  661. soa_record.mname.name, dns.CNAME, ttl=soa_record.expire,
  662. payload=dns.Record_CNAME(target), auth=True)
  663. self.assertRecordsMatch(
  664. [dns.RRHeader(
  665. target, address.TYPE, ttl=soa_record.expire, payload=address,
  666. auth=True)
  667. for address in addresses] + [alias],
  668. answer)
  669. def test_canonicalNameAnswerA(self):
  670. """
  671. If the name of the CNAME response has A records, they are included in
  672. the answer section of the response.
  673. """
  674. self._answerCNAMETest([self._A])
  675. def test_canonicalNameAnswerAAAA(self):
  676. """
  677. If the name of the CNAME response has AAAA records, they are included
  678. in the answer section of the response.
  679. """
  680. self._answerCNAMETest([self._AAAA])
  681. def test_canonicalNameAnswerBoth(self):
  682. """
  683. If the name of the CNAME response has both A and AAAA records, they are
  684. all included in the answer section of the response.
  685. """
  686. self._answerCNAMETest([self._A, self._AAAA])
  687. class NoInitialResponseTests(unittest.TestCase):
  688. def test_noAnswer(self):
  689. """
  690. If a request returns a L{dns.NS} response, but we can't connect to the
  691. given server, the request fails with the error returned at connection.
  692. """
  693. def query(self, *args):
  694. # Pop from the message list, so that it blows up if more queries
  695. # are run than expected.
  696. return succeed(messages.pop(0))
  697. def queryProtocol(self, *args, **kwargs):
  698. return defer.fail(socket.gaierror("Couldn't connect"))
  699. resolver = Resolver(servers=[('0.0.0.0', 0)])
  700. resolver._query = query
  701. messages = []
  702. # Let's patch dns.DNSDatagramProtocol.query, as there is no easy way to
  703. # customize it.
  704. self.patch(dns.DNSDatagramProtocol, "query", queryProtocol)
  705. records = [
  706. dns.RRHeader(name='fooba.com', type=dns.NS, cls=dns.IN, ttl=700,
  707. auth=False,
  708. payload=dns.Record_NS(name='ns.twistedmatrix.com',
  709. ttl=700))]
  710. m = dns.Message(id=999, answer=1, opCode=0, recDes=0, recAv=1, auth=1,
  711. rCode=0, trunc=0, maxSize=0)
  712. m.answers = records
  713. messages.append(m)
  714. return self.assertFailure(
  715. resolver.getHostByName("fooby.com"), socket.gaierror)
  716. class SecondaryAuthorityServiceTests(unittest.TestCase):
  717. """
  718. Tests for L{SecondaryAuthorityService}, a service which keeps one or more
  719. authorities up to date by doing zone transfers from a master.
  720. """
  721. def test_constructAuthorityFromHost(self):
  722. """
  723. L{SecondaryAuthorityService} can be constructed with a C{str} giving a
  724. master server address and several domains, causing the creation of a
  725. secondary authority for each domain and that master server address and
  726. the default DNS port.
  727. """
  728. primary = '192.168.1.2'
  729. service = SecondaryAuthorityService(
  730. primary, ['example.com', 'example.org'])
  731. self.assertEqual(service.primary, primary)
  732. self.assertEqual(service._port, 53)
  733. self.assertEqual(service.domains[0].primary, primary)
  734. self.assertEqual(service.domains[0]._port, 53)
  735. self.assertEqual(service.domains[0].domain, 'example.com')
  736. self.assertEqual(service.domains[1].primary, primary)
  737. self.assertEqual(service.domains[1]._port, 53)
  738. self.assertEqual(service.domains[1].domain, 'example.org')
  739. def test_constructAuthorityFromHostAndPort(self):
  740. """
  741. L{SecondaryAuthorityService.fromServerAddressAndDomains} constructs a
  742. new L{SecondaryAuthorityService} from a C{str} giving a master server
  743. address and DNS port and several domains, causing the creation of a secondary
  744. authority for each domain and that master server address and the given
  745. DNS port.
  746. """
  747. primary = '192.168.1.3'
  748. port = 5335
  749. service = SecondaryAuthorityService.fromServerAddressAndDomains(
  750. (primary, port), ['example.net', 'example.edu'])
  751. self.assertEqual(service.primary, primary)
  752. self.assertEqual(service._port, 5335)
  753. self.assertEqual(service.domains[0].primary, primary)
  754. self.assertEqual(service.domains[0]._port, port)
  755. self.assertEqual(service.domains[0].domain, 'example.net')
  756. self.assertEqual(service.domains[1].primary, primary)
  757. self.assertEqual(service.domains[1]._port, port)
  758. self.assertEqual(service.domains[1].domain, 'example.edu')
  759. class SecondaryAuthorityTests(unittest.TestCase):
  760. """
  761. L{twisted.names.secondary.SecondaryAuthority} correctly constructs objects
  762. with a specified IP address and optionally specified DNS port.
  763. """
  764. def test_defaultPort(self):
  765. """
  766. When constructed using L{SecondaryAuthority.__init__}, the default port
  767. of 53 is used.
  768. """
  769. secondary = SecondaryAuthority('192.168.1.1', 'inside.com')
  770. self.assertEqual(secondary.primary, '192.168.1.1')
  771. self.assertEqual(secondary._port, 53)
  772. self.assertEqual(secondary.domain, 'inside.com')
  773. def test_explicitPort(self):
  774. """
  775. When constructed using L{SecondaryAuthority.fromServerAddressAndDomain},
  776. the specified port is used.
  777. """
  778. secondary = SecondaryAuthority.fromServerAddressAndDomain(
  779. ('192.168.1.1', 5353), 'inside.com')
  780. self.assertEqual(secondary.primary, '192.168.1.1')
  781. self.assertEqual(secondary._port, 5353)
  782. self.assertEqual(secondary.domain, 'inside.com')
  783. def test_transfer(self):
  784. """
  785. An attempt is made to transfer the zone for the domain the
  786. L{SecondaryAuthority} was constructed with from the server address it
  787. was constructed with when L{SecondaryAuthority.transfer} is called.
  788. """
  789. secondary = SecondaryAuthority.fromServerAddressAndDomain(
  790. ('192.168.1.2', 1234), 'example.com')
  791. secondary._reactor = reactor = MemoryReactorClock()
  792. secondary.transfer()
  793. # Verify a connection attempt to the server address above
  794. host, port, factory, timeout, bindAddress = reactor.tcpClients.pop(0)
  795. self.assertEqual(host, '192.168.1.2')
  796. self.assertEqual(port, 1234)
  797. # See if a zone transfer query is issued.
  798. proto = factory.buildProtocol((host, port))
  799. transport = StringTransport()
  800. proto.makeConnection(transport)
  801. msg = Message()
  802. # DNSProtocol.writeMessage length encodes the message by prepending a
  803. # 2 byte message length to the buffered value.
  804. msg.decode(BytesIO(transport.value()[2:]))
  805. self.assertEqual(
  806. [dns.Query('example.com', dns.AXFR, dns.IN)], msg.queries)
  807. def test_lookupAddress(self):
  808. """
  809. L{SecondaryAuthority.lookupAddress} returns a L{Deferred} that fires
  810. with the I{A} records the authority has cached from the primary.
  811. """
  812. secondary = SecondaryAuthority.fromServerAddressAndDomain(
  813. ('192.168.1.2', 1234), b'example.com')
  814. secondary._reactor = reactor = MemoryReactorClock()
  815. secondary.transfer()
  816. host, port, factory, timeout, bindAddress = reactor.tcpClients.pop(0)
  817. proto = factory.buildProtocol((host, port))
  818. transport = StringTransport()
  819. proto.makeConnection(transport)
  820. query = Message(answer=1, auth=1)
  821. query.decode(BytesIO(transport.value()[2:]))
  822. # Generate a response with some data we can check.
  823. soa = Record_SOA(
  824. mname=b'ns1.example.com',
  825. rname='admin.example.com',
  826. serial=123456,
  827. refresh=3600,
  828. minimum=4800,
  829. expire=7200,
  830. retry=9600,
  831. ttl=12000,
  832. )
  833. a = Record_A(b'192.168.1.2', ttl=0)
  834. answer = Message(id=query.id, answer=1, auth=1)
  835. answer.answers.extend([
  836. RRHeader(b'example.com', type=SOA, payload=soa),
  837. RRHeader(b'example.com', payload=a),
  838. RRHeader(b'example.com', type=SOA, payload=soa),
  839. ])
  840. data = answer.toStr()
  841. proto.dataReceived(pack('!H', len(data)) + data)
  842. result = self.successResultOf(secondary.lookupAddress('example.com'))
  843. self.assertEqual((
  844. [RRHeader(b'example.com', payload=a, auth=True)], [], []), result)
  845. sampleBindZone = b"""\
  846. $ORIGIN example.com.
  847. $TTL 1w
  848. example.com. IN SOA dns.example.com (
  849. 2013120201 ; serial number of this zone file
  850. 1d ; slave refresh
  851. 2h ; slave retry time in case of a problem
  852. 4w ; slave expiration time
  853. 1h ; maximum caching time in case of failed lookups
  854. )
  855. ; A comment.
  856. @ IN AAAA 2001:db8:10::1
  857. example.com. IN A 10.0.0.1
  858. no-in.example.com. A 10.0.0.2 ; technically wrong but used to work
  859. not-fqdn IN MX 10 mx.example.com
  860. www IN CNAME example.com"""
  861. class BindAuthorityTests(unittest.TestCase):
  862. """
  863. Tests for L{twisted.names.authority.BindAuthority}.
  864. """
  865. def loadBindString(self, s):
  866. """
  867. Create a new L{twisted.names.authority.BindAuthority} from C{s}.
  868. @param s: A string with BIND zone data.
  869. @type s: bytes
  870. @return: a new bind authority
  871. @rtype: L{twisted.names.authority.BindAuthority}
  872. """
  873. fp = FilePath(self.mktemp().encode("ascii"))
  874. fp.setContent(s)
  875. return authority.BindAuthority(fp.path)
  876. def setUp(self):
  877. self.auth = self.loadBindString(sampleBindZone)
  878. def test_ttl(self):
  879. """
  880. Loads the default $TTL and applies it to all records.
  881. """
  882. for dom in self.auth.records.keys():
  883. for rec in self.auth.records[dom]:
  884. self.assertTrue(
  885. 604800 == rec.ttl
  886. )
  887. def test_originFromFile(self):
  888. """
  889. Loads the default $ORIGIN.
  890. """
  891. self.assertEqual(
  892. b"example.com.", self.auth.origin,
  893. )
  894. self.assertIn(
  895. b"not-fqdn.example.com", self.auth.records,
  896. )
  897. def test_aRecords(self):
  898. """
  899. A records are loaded.
  900. """
  901. for dom, ip in [(b"example.com", u"10.0.0.1"),
  902. (b"no-in.example.com", u"10.0.0.2")]:
  903. rr = self.successResultOf(
  904. self.auth.lookupAddress(dom)
  905. )[0][0]
  906. self.assertEqual(
  907. dns.Record_A(
  908. ip,
  909. 604800,
  910. ),
  911. rr.payload,
  912. )
  913. def test_aaaaRecords(self):
  914. """
  915. AAAA records are loaded.
  916. """
  917. rr = self.successResultOf(
  918. self.auth.lookupIPV6Address(b"example.com")
  919. )[0][0]
  920. self.assertEqual(
  921. dns.Record_AAAA(
  922. u"2001:db8:10::1",
  923. 604800,
  924. ),
  925. rr.payload,
  926. )
  927. def test_mxRecords(self):
  928. """
  929. MX records are loaded.
  930. """
  931. rr = self.successResultOf(
  932. self.auth.lookupMailExchange(b"not-fqdn.example.com")
  933. )[0][0]
  934. self.assertEqual(
  935. dns.Record_MX(
  936. preference=10, name="mx.example.com", ttl=604800,
  937. ),
  938. rr.payload,
  939. )
  940. def test_cnameRecords(self):
  941. """
  942. CNAME records are loaded.
  943. """
  944. rr = self.successResultOf(
  945. self.auth.lookupIPV6Address(b"www.example.com")
  946. )[0][0]
  947. self.assertEqual(
  948. dns.Record_CNAME(
  949. name="example.com", ttl=604800,
  950. ),
  951. rr.payload,
  952. )
  953. def test_invalidRecordClass(self):
  954. """
  955. loadBindString raises NotImplementedError on invalid records.
  956. """
  957. with self.assertRaises(NotImplementedError) as e:
  958. self.loadBindString(
  959. b"example.com. IN LOL 192.168.0.1"
  960. )
  961. self.assertEqual(
  962. "Record type 'LOL' not supported", e.exception.args[0]
  963. )
  964. def test_invalidDirectives(self):
  965. """
  966. $INCLUDE and $GENERATE raise NotImplementedError.
  967. """
  968. for directive in (b"$INCLUDE", b"$GENERATE"):
  969. with self.assertRaises(NotImplementedError) as e:
  970. self.loadBindString(directive + b" doesNotMatter")
  971. self.assertEqual(
  972. nativeString(directive + b" directive not implemented"),
  973. e.exception.args[0]
  974. )