session_tests.py 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. # Copyright (C) 2012 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import os
  16. import shutil
  17. import isc.log
  18. import unittest
  19. from isc.dns import *
  20. from isc.datasrc import DataSourceClient
  21. from isc.ddns.session import *
  22. from isc.ddns.zone_config import *
  23. # Some common test parameters
  24. TESTDATA_PATH = os.environ['TESTDATA_PATH'] + os.sep
  25. READ_ZONE_DB_FILE = TESTDATA_PATH + "rwtest.sqlite3" # original, to be copied
  26. TESTDATA_WRITE_PATH = os.environ['TESTDATA_WRITE_PATH'] + os.sep
  27. WRITE_ZONE_DB_FILE = TESTDATA_WRITE_PATH + "rwtest.sqlite3.copied"
  28. WRITE_ZONE_DB_CONFIG = "{ \"database_file\": \"" + WRITE_ZONE_DB_FILE + "\"}"
  29. TEST_ZONE_NAME = Name('example.org')
  30. UPDATE_RRTYPE = RRType.SOA()
  31. TEST_RRCLASS = RRClass.IN()
  32. TEST_ZONE_RECORD = Question(TEST_ZONE_NAME, TEST_RRCLASS, UPDATE_RRTYPE)
  33. TEST_CLIENT6 = ('2001:db8::1', 53, 0, 0)
  34. TEST_CLIENT4 = ('192.0.2.1', 53)
  35. # TSIG key for tests when needed. The key name is TEST_ZONE_NAME.
  36. TEST_TSIG_KEY = TSIGKey("example.org:SFuWd/q99SzF8Yzd1QbB9g==")
  37. def create_update_msg(zones=[TEST_ZONE_RECORD], prerequisites=[],
  38. updates=[], tsig_key=None):
  39. msg = Message(Message.RENDER)
  40. msg.set_qid(5353) # arbitrary chosen
  41. msg.set_opcode(Opcode.UPDATE())
  42. msg.set_rcode(Rcode.NOERROR())
  43. for z in zones:
  44. msg.add_question(z)
  45. for p in prerequisites:
  46. msg.add_rrset(SECTION_PREREQUISITE, p)
  47. for u in updates:
  48. msg.add_rrset(SECTION_UPDATE, u)
  49. renderer = MessageRenderer()
  50. if tsig_key is not None:
  51. msg.to_wire(renderer, TSIGContext(tsig_key))
  52. else:
  53. msg.to_wire(renderer)
  54. # re-read the created data in the parse mode
  55. msg.clear(Message.PARSE)
  56. msg.from_wire(renderer.get_data(), Message.PRESERVE_ORDER)
  57. return msg
  58. def add_rdata(rrset, rdata):
  59. '''
  60. Helper function for easily adding Rdata fields to RRsets.
  61. This function assumes the given rdata is of type string or bytes,
  62. and corresponds to the given rrset
  63. '''
  64. rrset.add_rdata(isc.dns.Rdata(rrset.get_type(),
  65. rrset.get_class(),
  66. rdata))
  67. def create_rrset(name, rrclass, rrtype, ttl, rdatas = []):
  68. '''
  69. Helper method to easily create RRsets, auto-converts
  70. name, rrclass, rrtype, and ttl (if possibly through their
  71. respective constructors)
  72. rdatas is a list of rr data strings, or bytestrings, which
  73. should match the RRType of the rrset to create
  74. '''
  75. if type(name) != Name:
  76. name = Name(name)
  77. if type(rrclass) != RRClass:
  78. rrclass = RRClass(rrclass)
  79. if type(rrtype) != RRType:
  80. rrtype = RRType(rrtype)
  81. if type(ttl) != RRTTL:
  82. ttl = RRTTL(ttl)
  83. rrset = isc.dns.RRset(name, rrclass, rrtype, ttl)
  84. for rdata in rdatas:
  85. add_rdata(rrset, rdata)
  86. return rrset
  87. class SessionModuleTests(unittest.TestCase):
  88. '''Tests for module-level functions in the session.py module'''
  89. def test_foreach_rr_in_rrset(self):
  90. rrset = create_rrset("www.example.org", TEST_RRCLASS,
  91. RRType.A(), 3600, [ "192.0.2.1" ])
  92. l = []
  93. for rr in foreach_rr(rrset):
  94. l.append(str(rr))
  95. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n"], l)
  96. add_rdata(rrset, "192.0.2.2")
  97. add_rdata(rrset, "192.0.2.3")
  98. # but through the generator, there should be several 1-line entries
  99. l = []
  100. for rr in foreach_rr(rrset):
  101. l.append(str(rr))
  102. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n",
  103. "www.example.org. 3600 IN A 192.0.2.2\n",
  104. "www.example.org. 3600 IN A 192.0.2.3\n",
  105. ], l)
  106. def test_convert_rrset_class(self):
  107. # Converting an RRSET to a different class should work
  108. # if the rdata types can be converted
  109. rrset = create_rrset("www.example.org", RRClass.NONE(), RRType.A(),
  110. 3600, [ b'\xc0\x00\x02\x01', b'\xc0\x00\x02\x02'])
  111. rrset2 = convert_rrset_class(rrset, RRClass.IN())
  112. self.assertEqual("www.example.org. 3600 IN A 192.0.2.1\n" +
  113. "www.example.org. 3600 IN A 192.0.2.2\n",
  114. str(rrset2))
  115. rrset3 = convert_rrset_class(rrset2, RRClass.NONE())
  116. self.assertEqual("www.example.org. 3600 CLASS254 A \\# 4 " +
  117. "c0000201\nwww.example.org. 3600 CLASS254 " +
  118. "A \\# 4 c0000202\n",
  119. str(rrset3))
  120. # depending on what type of bad data is given, a number
  121. # of different exceptions could be raised (TODO: i recall
  122. # there was a ticket about making a better hierarchy for
  123. # dns/parsing related exceptions)
  124. self.assertRaises(InvalidRdataLength, convert_rrset_class,
  125. rrset, RRClass.CH())
  126. add_rdata(rrset, b'\xc0\x00')
  127. self.assertRaises(DNSMessageFORMERR, convert_rrset_class,
  128. rrset, RRClass.IN())
  129. def test_collect_rrsets(self):
  130. '''
  131. Tests the 'rrset collector' method, which collects rrsets
  132. with the same name and type
  133. '''
  134. collected = []
  135. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  136. RRType.A(), 0, [ "192.0.2.1" ]))
  137. # Same name and class, different type
  138. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  139. RRType.TXT(), 0, [ "one" ]))
  140. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  141. RRType.A(), 0, [ "192.0.2.2" ]))
  142. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  143. RRType.TXT(), 0, [ "two" ]))
  144. # Same class and type as an existing one, different name
  145. collect_rrsets(collected, create_rrset("b.example.org", RRClass.IN(),
  146. RRType.A(), 0, [ "192.0.2.3" ]))
  147. # Same name and type as an existing one, different class
  148. collect_rrsets(collected, create_rrset("a.example.org", RRClass.CH(),
  149. RRType.TXT(), 0, [ "one" ]))
  150. collect_rrsets(collected, create_rrset("b.example.org", RRClass.IN(),
  151. RRType.A(), 0, [ "192.0.2.4" ]))
  152. collect_rrsets(collected, create_rrset("a.example.org", RRClass.CH(),
  153. RRType.TXT(), 0, [ "two" ]))
  154. strings = [ rrset.to_text() for rrset in collected ]
  155. # note + vs , in this list
  156. expected = ['a.example.org. 0 IN A 192.0.2.1\n' +
  157. 'a.example.org. 0 IN A 192.0.2.2\n',
  158. 'a.example.org. 0 IN TXT "one"\n' +
  159. 'a.example.org. 0 IN TXT "two"\n',
  160. 'b.example.org. 0 IN A 192.0.2.3\n' +
  161. 'b.example.org. 0 IN A 192.0.2.4\n',
  162. 'a.example.org. 0 CH TXT "one"\n' +
  163. 'a.example.org. 0 CH TXT "two"\n']
  164. self.assertEqual(expected, strings)
  165. class SessionTestBase(unittest.TestCase):
  166. '''Base class for all sesion related tests.
  167. It just initializes common test parameters in its setUp() and defines
  168. some common utility method(s).
  169. '''
  170. def setUp(self):
  171. shutil.copyfile(READ_ZONE_DB_FILE, WRITE_ZONE_DB_FILE)
  172. self._datasrc_client = DataSourceClient("sqlite3",
  173. WRITE_ZONE_DB_CONFIG)
  174. self._update_msg = create_update_msg()
  175. self._acl_map = {(TEST_ZONE_NAME, TEST_RRCLASS):
  176. REQUEST_LOADER.load([{"action": "ACCEPT"}])}
  177. self._session = UpdateSession(self._update_msg, TEST_CLIENT4,
  178. ZoneConfig([], TEST_RRCLASS,
  179. self._datasrc_client,
  180. self._acl_map))
  181. self._session._get_update_zone()
  182. self._session._create_diff()
  183. def tearDown(self):
  184. # With the Updater created in _get_update_zone, and tests
  185. # doing all kinds of crazy stuff, one might get database locked
  186. # errors if it doesn't clean up explicitely after each test
  187. self._session = None
  188. def check_response(self, msg, expected_rcode):
  189. '''Perform common checks on update resposne message.'''
  190. self.assertTrue(msg.get_header_flag(Message.HEADERFLAG_QR))
  191. # note: we convert opcode to text it'd be more helpful on failure.
  192. self.assertEqual(Opcode.UPDATE().to_text(), msg.get_opcode().to_text())
  193. self.assertEqual(expected_rcode.to_text(), msg.get_rcode().to_text())
  194. # All sections should be cleared
  195. self.assertEqual(0, msg.get_rr_count(SECTION_ZONE))
  196. self.assertEqual(0, msg.get_rr_count(SECTION_PREREQUISITE))
  197. self.assertEqual(0, msg.get_rr_count(SECTION_UPDATE))
  198. self.assertEqual(0, msg.get_rr_count(Message.SECTION_ADDITIONAL))
  199. class SessionTest(SessionTestBase):
  200. '''Basic session tests'''
  201. def test_handle(self):
  202. '''Basic update case'''
  203. result, zname, zclass = self._session.handle()
  204. self.assertEqual(UPDATE_SUCCESS, result)
  205. self.assertEqual(TEST_ZONE_NAME, zname)
  206. self.assertEqual(TEST_RRCLASS, zclass)
  207. # Just checking these are different from the success code.
  208. self.assertNotEqual(UPDATE_ERROR, result)
  209. self.assertNotEqual(UPDATE_DROP, result)
  210. def test_broken_request(self):
  211. # Zone section is empty
  212. msg = create_update_msg(zones=[])
  213. session = UpdateSession(msg, TEST_CLIENT6, None)
  214. result, zname, zclass = session.handle()
  215. self.assertEqual(UPDATE_ERROR, result)
  216. self.assertEqual(None, zname)
  217. self.assertEqual(None, zclass)
  218. self.check_response(session.get_message(), Rcode.FORMERR())
  219. # Zone section contains multiple records
  220. msg = create_update_msg(zones=[TEST_ZONE_RECORD, TEST_ZONE_RECORD])
  221. session = UpdateSession(msg, TEST_CLIENT4, None)
  222. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  223. self.check_response(session.get_message(), Rcode.FORMERR())
  224. # Zone section's type is not SOA
  225. msg = create_update_msg(zones=[Question(TEST_ZONE_NAME, TEST_RRCLASS,
  226. RRType.A())])
  227. session = UpdateSession(msg, TEST_CLIENT4, None)
  228. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  229. self.check_response(session.get_message(), Rcode.FORMERR())
  230. def test_update_secondary(self):
  231. # specified zone is configured as a secondary. Since this
  232. # implementation doesn't support update forwarding, the result
  233. # should be NOTIMP.
  234. msg = create_update_msg(zones=[Question(TEST_ZONE_NAME, TEST_RRCLASS,
  235. RRType.SOA())])
  236. session = UpdateSession(msg, TEST_CLIENT4,
  237. ZoneConfig([(TEST_ZONE_NAME, TEST_RRCLASS)],
  238. TEST_RRCLASS, self._datasrc_client))
  239. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  240. self.check_response(session.get_message(), Rcode.NOTIMP())
  241. def check_notauth(self, zname, zclass=TEST_RRCLASS):
  242. '''Common test sequence for the 'notauth' test'''
  243. msg = create_update_msg(zones=[Question(zname, zclass, RRType.SOA())])
  244. session = UpdateSession(msg, TEST_CLIENT4,
  245. ZoneConfig([(TEST_ZONE_NAME, TEST_RRCLASS)],
  246. TEST_RRCLASS, self._datasrc_client))
  247. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  248. self.check_response(session.get_message(), Rcode.NOTAUTH())
  249. def test_update_notauth(self):
  250. '''Update attempt for non authoritative zones'''
  251. # zone name doesn't match
  252. self.check_notauth(Name('example.com'))
  253. # zone name is a subdomain of the actual authoritative zone
  254. # (match must be exact)
  255. self.check_notauth(Name('sub.example.org'))
  256. # zone class doesn't match
  257. self.check_notauth(Name('example.org'), RRClass.CH())
  258. def test_update_datasrc_error(self):
  259. # if the data source client raises an exception, it should result in
  260. # a SERVFAIL.
  261. class BadDataSourceClient:
  262. def find_zone(self, name):
  263. raise isc.datasrc.Error('faked exception')
  264. msg = create_update_msg(zones=[Question(TEST_ZONE_NAME, TEST_RRCLASS,
  265. RRType.SOA())])
  266. session = UpdateSession(msg, TEST_CLIENT4,
  267. ZoneConfig([(TEST_ZONE_NAME, TEST_RRCLASS)],
  268. TEST_RRCLASS,
  269. BadDataSourceClient()))
  270. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  271. self.check_response(session.get_message(), Rcode.SERVFAIL())
  272. def test_foreach_rr_in_rrset(self):
  273. rrset = create_rrset("www.example.org", TEST_RRCLASS,
  274. RRType.A(), 3600, [ "192.0.2.1" ])
  275. l = []
  276. for rr in foreach_rr(rrset):
  277. l.append(str(rr))
  278. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n"], l)
  279. add_rdata(rrset, "192.0.2.2")
  280. add_rdata(rrset, "192.0.2.3")
  281. # but through the generator, there should be several 1-line entries
  282. l = []
  283. for rr in foreach_rr(rrset):
  284. l.append(str(rr))
  285. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n",
  286. "www.example.org. 3600 IN A 192.0.2.2\n",
  287. "www.example.org. 3600 IN A 192.0.2.3\n",
  288. ], l)
  289. def test_convert_rrset_class(self):
  290. # Converting an RRSET to a different class should work
  291. # if the rdata types can be converted
  292. rrset = create_rrset("www.example.org", RRClass.NONE(), RRType.A(),
  293. 3600, [ b'\xc0\x00\x02\x01', b'\xc0\x00\x02\x02'])
  294. rrset2 = convert_rrset_class(rrset, RRClass.IN())
  295. self.assertEqual("www.example.org. 3600 IN A 192.0.2.1\n" +
  296. "www.example.org. 3600 IN A 192.0.2.2\n",
  297. str(rrset2))
  298. rrset3 = convert_rrset_class(rrset2, RRClass.NONE())
  299. self.assertEqual("www.example.org. 3600 CLASS254 A \\# 4 " +
  300. "c0000201\nwww.example.org. 3600 CLASS254 " +
  301. "A \\# 4 c0000202\n",
  302. str(rrset3))
  303. # depending on what type of bad data is given, a number
  304. # of different exceptions could be raised (TODO: i recall
  305. # there was a ticket about making a better hierarchy for
  306. # dns/parsing related exceptions)
  307. self.assertRaises(InvalidRdataLength, convert_rrset_class,
  308. rrset, RRClass.CH())
  309. add_rdata(rrset, b'\xc0\x00')
  310. self.assertRaises(DNSMessageFORMERR, convert_rrset_class,
  311. rrset, RRClass.IN())
  312. def test_collect_rrsets(self):
  313. '''
  314. Tests the 'rrset collector' method, which collects rrsets
  315. with the same name and type
  316. '''
  317. collected = []
  318. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  319. RRType.A(), 0, [ "192.0.2.1" ]))
  320. # Same name and class, different type
  321. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  322. RRType.TXT(), 0, [ "one" ]))
  323. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  324. RRType.A(), 0, [ "192.0.2.2" ]))
  325. collect_rrsets(collected, create_rrset("a.example.org", RRClass.IN(),
  326. RRType.TXT(), 0, [ "two" ]))
  327. # Same class and type as an existing one, different name
  328. collect_rrsets(collected, create_rrset("b.example.org", RRClass.IN(),
  329. RRType.A(), 0, [ "192.0.2.3" ]))
  330. # Same name and type as an existing one, different class
  331. collect_rrsets(collected, create_rrset("a.example.org", RRClass.CH(),
  332. RRType.TXT(), 0, [ "one" ]))
  333. collect_rrsets(collected, create_rrset("b.example.org", RRClass.IN(),
  334. RRType.A(), 0, [ "192.0.2.4" ]))
  335. collect_rrsets(collected, create_rrset("a.example.org", RRClass.CH(),
  336. RRType.TXT(), 0, [ "two" ]))
  337. strings = [ rrset.to_text() for rrset in collected ]
  338. # note + vs , in this list
  339. expected = ['a.example.org. 0 IN A 192.0.2.1\n' +
  340. 'a.example.org. 0 IN A 192.0.2.2\n',
  341. 'a.example.org. 0 IN TXT "one"\n' +
  342. 'a.example.org. 0 IN TXT "two"\n',
  343. 'b.example.org. 0 IN A 192.0.2.3\n' +
  344. 'b.example.org. 0 IN A 192.0.2.4\n',
  345. 'a.example.org. 0 CH TXT "one"\n' +
  346. 'a.example.org. 0 CH TXT "two"\n']
  347. self.assertEqual(expected, strings)
  348. def __prereq_helper(self, method, expected, rrset):
  349. '''Calls the given method with self._datasrc_client
  350. and the given rrset, and compares the return value.
  351. Function does not do much but makes the code look nicer'''
  352. self.assertEqual(expected, method(rrset))
  353. def __check_prerequisite_exists_combined(self, method, rrclass, expected):
  354. '''shared code for the checks for the very similar (but reversed
  355. in behaviour) methods __prereq_rrset_exists and
  356. __prereq_rrset_does_not_exist.
  357. For rrset_exists, rrclass should be ANY, for rrset_does_not_exist,
  358. it should be NONE.
  359. '''
  360. # Basic existence checks
  361. # www.example.org should have an A, but not an MX
  362. rrset = create_rrset("www.example.org", rrclass, RRType.A(), 0)
  363. self.__prereq_helper(method, expected, rrset)
  364. rrset = create_rrset("www.example.org", rrclass, RRType.MX(), 0)
  365. self.__prereq_helper(method, not expected, rrset)
  366. # example.org should have an MX, but not an A
  367. rrset = create_rrset("example.org", rrclass, RRType.MX(), 0)
  368. self.__prereq_helper(method, expected, rrset)
  369. rrset = create_rrset("example.org", rrclass, RRType.A(), 0)
  370. self.__prereq_helper(method, not expected, rrset)
  371. # Also check the case where the name does not even exist
  372. rrset = create_rrset("doesnotexist.example.org", rrclass, RRType.A(), 0)
  373. self.__prereq_helper(method, not expected, rrset)
  374. # Wildcard expansion should not be applied, but literal matches
  375. # should work
  376. rrset = create_rrset("foo.wildcard.example.org", rrclass, RRType.A(), 0)
  377. self.__prereq_helper(method, not expected, rrset)
  378. rrset = create_rrset("*.wildcard.example.org", rrclass, RRType.A(), 0)
  379. self.__prereq_helper(method, expected, rrset)
  380. # Likewise, CNAME directly should match, but what it points to should
  381. # not
  382. rrset = create_rrset("cname.example.org", rrclass, RRType.A(), 0)
  383. self.__prereq_helper(method, not expected, rrset)
  384. rrset = create_rrset("cname.example.org", rrclass, RRType.CNAME(), 0)
  385. self.__prereq_helper(method, expected, rrset)
  386. # And also make sure a delegation (itself) is not treated as existing
  387. # data
  388. rrset = create_rrset("foo.sub.example.org", rrclass, RRType.A(), 0)
  389. self.__prereq_helper(method, not expected, rrset)
  390. # But the delegation data itself should match
  391. rrset = create_rrset("sub.example.org", rrclass, RRType.NS(), 0)
  392. self.__prereq_helper(method, expected, rrset)
  393. # As should glue
  394. rrset = create_rrset("ns.sub.example.org", rrclass, RRType.A(), 0)
  395. self.__prereq_helper(method, expected, rrset)
  396. def test_check_prerequisite_exists(self):
  397. method = self._session._UpdateSession__prereq_rrset_exists
  398. self.__check_prerequisite_exists_combined(method,
  399. RRClass.ANY(),
  400. True)
  401. def test_check_prerequisite_does_not_exist(self):
  402. method = self._session._UpdateSession__prereq_rrset_does_not_exist
  403. self.__check_prerequisite_exists_combined(method,
  404. RRClass.NONE(),
  405. False)
  406. def test_check_prerequisite_exists_value(self):
  407. method = self._session._UpdateSession__prereq_rrset_exists_value
  408. rrset = create_rrset("www.example.org", RRClass.IN(), RRType.A(), 0)
  409. # empty one should not match
  410. self.__prereq_helper(method, False, rrset)
  411. # When the rdata is added, it should match
  412. add_rdata(rrset, "192.0.2.1")
  413. self.__prereq_helper(method, True, rrset)
  414. # But adding more should not
  415. add_rdata(rrset, "192.0.2.2")
  416. self.__prereq_helper(method, False, rrset)
  417. # Also test one with more than one RR
  418. rrset = create_rrset("example.org", RRClass.IN(), RRType.NS(), 0)
  419. self.__prereq_helper(method, False, rrset)
  420. add_rdata(rrset, "ns1.example.org.")
  421. self.__prereq_helper(method, False, rrset)
  422. add_rdata(rrset, "ns2.example.org")
  423. self.__prereq_helper(method, False, rrset)
  424. add_rdata(rrset, "ns3.example.org.")
  425. self.__prereq_helper(method, True, rrset)
  426. add_rdata(rrset, "ns4.example.org.")
  427. self.__prereq_helper(method, False, rrset)
  428. # Repeat that, but try a different order of Rdata addition
  429. rrset = create_rrset("example.org", RRClass.IN(), RRType.NS(), 0)
  430. self.__prereq_helper(method, False, rrset)
  431. add_rdata(rrset, "ns3.example.org.")
  432. self.__prereq_helper(method, False, rrset)
  433. add_rdata(rrset, "ns2.example.org.")
  434. self.__prereq_helper(method, False, rrset)
  435. add_rdata(rrset, "ns1.example.org.")
  436. self.__prereq_helper(method, True, rrset)
  437. add_rdata(rrset, "ns4.example.org.")
  438. self.__prereq_helper(method, False, rrset)
  439. # and test one where the name does not even exist
  440. rrset = create_rrset("doesnotexist.example.org", RRClass.IN(),
  441. RRType.A(), 0, [ "192.0.2.1" ])
  442. self.__prereq_helper(method, False, rrset)
  443. def __check_prerequisite_name_in_use_combined(self, method, rrclass,
  444. expected):
  445. '''shared code for the checks for the very similar (but reversed
  446. in behaviour) methods __prereq_name_in_use and
  447. __prereq_name_not_in_use
  448. '''
  449. rrset = create_rrset("example.org", rrclass, RRType.ANY(), 0)
  450. self.__prereq_helper(method, expected, rrset)
  451. rrset = create_rrset("www.example.org", rrclass, RRType.ANY(), 0)
  452. self.__prereq_helper(method, expected, rrset)
  453. rrset = create_rrset("doesnotexist.example.org", rrclass,
  454. RRType.ANY(), 0)
  455. self.__prereq_helper(method, not expected, rrset)
  456. rrset = create_rrset("belowdelegation.sub.example.org", rrclass,
  457. RRType.ANY(), 0)
  458. self.__prereq_helper(method, not expected, rrset)
  459. rrset = create_rrset("foo.wildcard.example.org", rrclass,
  460. RRType.ANY(), 0)
  461. self.__prereq_helper(method, not expected, rrset)
  462. # empty nonterminal should not match
  463. rrset = create_rrset("nonterminal.example.org", rrclass,
  464. RRType.ANY(), 0)
  465. self.__prereq_helper(method, not expected, rrset)
  466. rrset = create_rrset("empty.nonterminal.example.org", rrclass,
  467. RRType.ANY(), 0)
  468. self.__prereq_helper(method, expected, rrset)
  469. def test_check_prerequisite_name_in_use(self):
  470. method = self._session._UpdateSession__prereq_name_in_use
  471. self.__check_prerequisite_name_in_use_combined(method,
  472. RRClass.ANY(),
  473. True)
  474. def test_check_prerequisite_name_not_in_use(self):
  475. method = self._session._UpdateSession__prereq_name_not_in_use
  476. self.__check_prerequisite_name_in_use_combined(method,
  477. RRClass.NONE(),
  478. False)
  479. def check_prerequisite_result(self, expected, prerequisites):
  480. '''Helper method for checking the result of a prerequisite check;
  481. creates an update session, and fills it with the list of rrsets
  482. from 'prerequisites'. Then checks if __check_prerequisites()
  483. returns the Rcode specified in 'expected'.'''
  484. msg = create_update_msg([TEST_ZONE_RECORD], prerequisites)
  485. zconfig = ZoneConfig([], TEST_RRCLASS, self._datasrc_client,
  486. self._acl_map)
  487. session = UpdateSession(msg, TEST_CLIENT4, zconfig)
  488. session._get_update_zone()
  489. session._create_diff()
  490. # compare the to_text output of the rcodes (nicer error messages)
  491. # This call itself should also be done by handle(),
  492. # but just for better failures, it is first called on its own
  493. self.assertEqual(expected.to_text(),
  494. session._UpdateSession__check_prerequisites().to_text())
  495. # Now see if handle finds the same result
  496. (result, _, _) = session.handle()
  497. self.assertEqual(expected.to_text(),
  498. session._UpdateSession__message.get_rcode().to_text())
  499. # And that the result looks right
  500. if expected == Rcode.NOERROR():
  501. self.assertEqual(UPDATE_SUCCESS, result)
  502. else:
  503. self.assertEqual(UPDATE_ERROR, result)
  504. def check_prescan_result(self, expected, updates, expected_soa = None):
  505. '''Helper method for checking the result of a prerequisite check;
  506. creates an update session, and fills it with the list of rrsets
  507. from 'updates'. Then checks if __do_prescan()
  508. returns the Rcode specified in 'expected'.'''
  509. msg = create_update_msg([TEST_ZONE_RECORD], [], updates)
  510. zconfig = ZoneConfig([], TEST_RRCLASS, self._datasrc_client,
  511. self._acl_map)
  512. session = UpdateSession(msg, TEST_CLIENT4, zconfig)
  513. session._get_update_zone()
  514. session._create_diff()
  515. # compare the to_text output of the rcodes (nicer error messages)
  516. # This call itself should also be done by handle(),
  517. # but just for better failures, it is first called on its own
  518. self.assertEqual(expected.to_text(),
  519. session._UpdateSession__do_prescan().to_text())
  520. # If there is an expected soa, check it
  521. self.assertEqual(str(expected_soa),
  522. str(session._UpdateSession__added_soa))
  523. def check_full_handle_result(self, expected, updates):
  524. '''Helper method for checking the result of a full handle;
  525. creates an update session, and fills it with the list of rrsets
  526. from 'updates'. Then checks if __handle()
  527. results in a response with rcode 'expected'.'''
  528. msg = create_update_msg([TEST_ZONE_RECORD], [], updates)
  529. zconfig = ZoneConfig([], TEST_RRCLASS, self._datasrc_client,
  530. self._acl_map)
  531. session = UpdateSession(msg, TEST_CLIENT4, zconfig)
  532. # Now see if handle finds the same result
  533. (result, _, _) = session.handle()
  534. self.assertEqual(expected.to_text(),
  535. session._UpdateSession__message.get_rcode().to_text())
  536. # And that the result looks right
  537. if expected == Rcode.NOERROR():
  538. self.assertEqual(UPDATE_SUCCESS, result)
  539. else:
  540. self.assertEqual(UPDATE_ERROR, result)
  541. def test_check_prerequisites(self):
  542. # This test checks if the actual prerequisite-type-specific
  543. # methods are called.
  544. # It does test all types of prerequisites, but it does not test
  545. # every possible result for those types (those are tested above,
  546. # in the specific prerequisite type tests)
  547. # Let's first define a number of prereq's that should succeed
  548. rrset_exists_yes = create_rrset("example.org", RRClass.ANY(),
  549. RRType.SOA(), 0)
  550. rrset_exists_value_yes = create_rrset("www.example.org", RRClass.IN(),
  551. RRType.A(), 0, [ "192.0.2.1" ])
  552. rrset_does_not_exist_yes = create_rrset("foo.example.org",
  553. RRClass.NONE(), RRType.SOA(),
  554. 0)
  555. name_in_use_yes = create_rrset("www.example.org", RRClass.ANY(),
  556. RRType.ANY(), 0)
  557. name_not_in_use_yes = create_rrset("foo.example.org", RRClass.NONE(),
  558. RRType.ANY(), 0)
  559. rrset_exists_value_1 = create_rrset("example.org", RRClass.IN(),
  560. RRType.NS(), 0,
  561. [ "ns1.example.org" ])
  562. rrset_exists_value_2 = create_rrset("example.org", RRClass.IN(),
  563. RRType.NS(), 0,
  564. [ "ns2.example.org" ])
  565. rrset_exists_value_3 = create_rrset("example.org", RRClass.IN(),
  566. RRType.NS(), 0,
  567. [ "ns3.example.org" ])
  568. # and a number that should not
  569. rrset_exists_no = create_rrset("foo.example.org", RRClass.ANY(),
  570. RRType.SOA(), 0)
  571. rrset_exists_value_no = create_rrset("www.example.org", RRClass.IN(),
  572. RRType.A(), 0, [ "192.0.2.2" ])
  573. rrset_does_not_exist_no = create_rrset("example.org", RRClass.NONE(),
  574. RRType.SOA(), 0)
  575. name_in_use_no = create_rrset("foo.example.org", RRClass.ANY(),
  576. RRType.ANY(), 0)
  577. name_not_in_use_no = create_rrset("www.example.org", RRClass.NONE(),
  578. RRType.ANY(), 0)
  579. # check 'no' result codes
  580. self.check_prerequisite_result(Rcode.NXRRSET(),
  581. [ rrset_exists_no ])
  582. self.check_prerequisite_result(Rcode.NXRRSET(),
  583. [ rrset_exists_value_no ])
  584. self.check_prerequisite_result(Rcode.YXRRSET(),
  585. [ rrset_does_not_exist_no ])
  586. self.check_prerequisite_result(Rcode.NXDOMAIN(),
  587. [ name_in_use_no ])
  588. self.check_prerequisite_result(Rcode.YXDOMAIN(),
  589. [ name_not_in_use_no ])
  590. # the 'yes' codes should result in ok
  591. # individually
  592. self.check_prerequisite_result(Rcode.NOERROR(),
  593. [ rrset_exists_yes ] )
  594. self.check_prerequisite_result(Rcode.NOERROR(),
  595. [ rrset_exists_value_yes ])
  596. self.check_prerequisite_result(Rcode.NOERROR(),
  597. [ rrset_does_not_exist_yes ])
  598. self.check_prerequisite_result(Rcode.NOERROR(),
  599. [ name_in_use_yes ])
  600. self.check_prerequisite_result(Rcode.NOERROR(),
  601. [ name_not_in_use_yes ])
  602. self.check_prerequisite_result(Rcode.NOERROR(),
  603. [ rrset_exists_value_1,
  604. rrset_exists_value_2,
  605. rrset_exists_value_3])
  606. # and together
  607. self.check_prerequisite_result(Rcode.NOERROR(),
  608. [ rrset_exists_yes,
  609. rrset_exists_value_yes,
  610. rrset_does_not_exist_yes,
  611. name_in_use_yes,
  612. name_not_in_use_yes,
  613. rrset_exists_value_1,
  614. rrset_exists_value_2,
  615. rrset_exists_value_3])
  616. # try out a permutation, note that one rrset is split up,
  617. # and the order of the RRs should not matter
  618. self.check_prerequisite_result(Rcode.NOERROR(),
  619. [ rrset_exists_value_3,
  620. rrset_exists_yes,
  621. rrset_exists_value_2,
  622. name_in_use_yes,
  623. rrset_exists_value_1])
  624. # Should fail on the first error, even if most of the
  625. # prerequisites are ok
  626. self.check_prerequisite_result(Rcode.NXDOMAIN(),
  627. [ rrset_exists_value_3,
  628. rrset_exists_yes,
  629. rrset_exists_value_2,
  630. name_in_use_yes,
  631. name_in_use_no,
  632. rrset_exists_value_1])
  633. def test_prerequisite_notzone(self):
  634. rrset = create_rrset("some.other.zone.", RRClass.ANY(), RRType.SOA(), 0)
  635. self.check_prerequisite_result(Rcode.NOTZONE(), [ rrset ])
  636. def test_prerequisites_formerr(self):
  637. # test for form errors in the prerequisite section
  638. # Class ANY, non-zero TTL
  639. rrset = create_rrset("example.org", RRClass.ANY(), RRType.SOA(), 1)
  640. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  641. # Class ANY, but with rdata
  642. rrset = create_rrset("example.org", RRClass.ANY(), RRType.A(), 0,
  643. [ b'\x00\x00\x00\x00' ])
  644. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  645. # Class NONE, non-zero TTL
  646. rrset = create_rrset("example.org", RRClass.NONE(), RRType.SOA(), 1)
  647. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  648. # Class NONE, but with rdata
  649. rrset = create_rrset("example.org", RRClass.NONE(), RRType.A(), 0,
  650. [ b'\x00\x00\x00\x00' ])
  651. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  652. # Matching class and type, but non-zero TTL
  653. rrset = create_rrset("www.example.org", RRClass.IN(), RRType.A(), 1,
  654. [ "192.0.2.1" ])
  655. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  656. # Completely different class
  657. rrset = create_rrset("example.org", RRClass.CH(), RRType.TXT(), 0,
  658. [ "foo" ])
  659. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  660. def __prereq_helper(self, method, expected, rrset):
  661. '''Calls the given method with self._datasrc_client
  662. and the given rrset, and compares the return value.
  663. Function does not do much but makes the code look nicer'''
  664. self.assertEqual(expected, method(rrset))
  665. def __initialize_update_rrsets(self):
  666. '''Prepare a number of RRsets to be used in several update tests
  667. The rrsets are stored in self'''
  668. orig_a_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  669. RRType.A(), 3600, [ "192.0.2.1" ])
  670. self.orig_a_rrset = orig_a_rrset
  671. rrset_update_a = create_rrset("www.example.org", TEST_RRCLASS,
  672. RRType.A(), 3600,
  673. [ "192.0.2.2", "192.0.2.3" ])
  674. self.rrset_update_a = rrset_update_a
  675. rrset_update_soa = create_rrset("example.org", TEST_RRCLASS,
  676. RRType.SOA(), 3600,
  677. [ "ns1.example.org. " +
  678. "admin.example.org. " +
  679. "1233 3600 1800 2419200 7200" ])
  680. self.rrset_update_soa = rrset_update_soa
  681. rrset_update_soa_del = create_rrset("example.org", RRClass.NONE(),
  682. RRType.SOA(), 0,
  683. [ "ns1.example.org. " +
  684. "admin.example.org. " +
  685. "1233 3600 1800 2419200 7200" ])
  686. self.rrset_update_soa_del = rrset_update_soa_del
  687. rrset_update_soa2 = create_rrset("example.org", TEST_RRCLASS,
  688. RRType.SOA(), 3600,
  689. [ "ns1.example.org. " +
  690. "admin.example.org. " +
  691. "4000 3600 1800 2419200 7200" ])
  692. self.rrset_update_soa2 = rrset_update_soa2
  693. rrset_update_del_name = create_rrset("www.example.org", RRClass.ANY(),
  694. RRType.ANY(), 0)
  695. self.rrset_update_del_name = rrset_update_del_name
  696. rrset_update_del_name_apex = create_rrset("example.org", RRClass.ANY(),
  697. RRType.ANY(), 0)
  698. self.rrset_update_del_name_apex = rrset_update_del_name_apex
  699. rrset_update_del_rrset = create_rrset("www.example.org", RRClass.ANY(),
  700. RRType.A(), 0)
  701. self.rrset_update_del_rrset = rrset_update_del_rrset
  702. rrset_update_del_mx_apex = create_rrset("example.org", RRClass.ANY(),
  703. RRType.MX(), 0)
  704. self.rrset_update_del_mx_apex = rrset_update_del_mx_apex
  705. rrset_update_del_soa_apex = create_rrset("example.org", RRClass.ANY(),
  706. RRType.SOA(), 0)
  707. self.rrset_update_del_soa_apex = rrset_update_del_soa_apex
  708. rrset_update_del_ns_apex = create_rrset("example.org", RRClass.ANY(),
  709. RRType.NS(), 0)
  710. self.rrset_update_del_ns_apex = rrset_update_del_ns_apex
  711. rrset_update_del_rrset_part = create_rrset("www.example.org",
  712. RRClass.NONE(), RRType.A(),
  713. 0,
  714. [ b'\xc0\x00\x02\x02',
  715. b'\xc0\x00\x02\x03' ])
  716. self.rrset_update_del_rrset_part = rrset_update_del_rrset_part
  717. rrset_update_del_rrset_ns = create_rrset("example.org", RRClass.NONE(),
  718. RRType.NS(), 0,
  719. [ b'\x03ns1\x07example\x03org\x00',
  720. b'\x03ns2\x07example\x03org\x00',
  721. b'\x03ns3\x07example\x03org\x00' ])
  722. self.rrset_update_del_rrset_ns = rrset_update_del_rrset_ns
  723. rrset_update_del_rrset_mx = create_rrset("example.org", RRClass.NONE(),
  724. RRType.MX(), 0,
  725. [ b'\x00\x0a\x04mail\x07example\x03org\x00' ])
  726. self.rrset_update_del_rrset_mx = rrset_update_del_rrset_mx
  727. def test_prescan(self):
  728. '''Test whether the prescan succeeds on data that is ok, and whether
  729. if notices the SOA if present'''
  730. # prepare a set of correct update statements
  731. self.__initialize_update_rrsets()
  732. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  733. # check if soa is noticed
  734. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_soa ],
  735. self.rrset_update_soa)
  736. # Other types of succesful prechecks
  737. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_soa2 ],
  738. self.rrset_update_soa2)
  739. self.check_prescan_result(Rcode.NOERROR(),
  740. [ self.rrset_update_del_name ])
  741. self.check_prescan_result(Rcode.NOERROR(),
  742. [ self.rrset_update_del_name_apex ])
  743. self.check_prescan_result(Rcode.NOERROR(),
  744. [ self.rrset_update_del_rrset ])
  745. self.check_prescan_result(Rcode.NOERROR(),
  746. [ self.rrset_update_del_mx_apex ])
  747. self.check_prescan_result(Rcode.NOERROR(),
  748. [ self.rrset_update_del_rrset_part ])
  749. # and check a few permutations of the above
  750. # all of them (with one of the soas)
  751. self.check_prescan_result(Rcode.NOERROR(),
  752. [
  753. self.rrset_update_a,
  754. self.rrset_update_soa,
  755. self.rrset_update_del_name,
  756. self.rrset_update_del_name_apex,
  757. self.rrset_update_del_rrset,
  758. self.rrset_update_del_mx_apex,
  759. self.rrset_update_del_rrset_part
  760. ],
  761. self.rrset_update_soa)
  762. # Two soas. Should we reject or simply use the last?
  763. # (RFC is not really explicit on this, but between the lines I read
  764. # use the last)
  765. self.check_prescan_result(Rcode.NOERROR(),
  766. [ self.rrset_update_soa,
  767. self.rrset_update_soa2 ],
  768. self.rrset_update_soa2)
  769. self.check_prescan_result(Rcode.NOERROR(),
  770. [ self.rrset_update_soa2,
  771. self.rrset_update_soa ],
  772. self.rrset_update_soa)
  773. self.check_prescan_result(Rcode.NOERROR(),
  774. [
  775. self.rrset_update_del_mx_apex,
  776. self.rrset_update_del_name,
  777. self.rrset_update_del_name_apex,
  778. self.rrset_update_del_rrset_part,
  779. self.rrset_update_a,
  780. self.rrset_update_del_rrset,
  781. self.rrset_update_soa
  782. ],
  783. self.rrset_update_soa)
  784. def test_prescan_failures(self):
  785. '''Test whether prescan fails on bad data'''
  786. # out of zone data
  787. rrset = create_rrset("different.zone", RRClass.ANY(), RRType.TXT(), 0)
  788. self.check_prescan_result(Rcode.NOTZONE(), [ rrset ])
  789. # forbidden type, zone class
  790. rrset = create_rrset(TEST_ZONE_NAME, TEST_RRCLASS, RRType.ANY(), 0,
  791. [ b'\x00' ])
  792. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  793. # non-zero TTL, class ANY
  794. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.TXT(), 1)
  795. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  796. # non-zero Rdata, class ANY
  797. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.TXT(), 0,
  798. [ "foo" ])
  799. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  800. # forbidden type, class ANY
  801. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.AXFR(), 0,
  802. [ b'\x00' ])
  803. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  804. # non-zero TTL, class NONE
  805. rrset = create_rrset(TEST_ZONE_NAME, RRClass.NONE(), RRType.TXT(), 1)
  806. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  807. # forbidden type, class NONE
  808. rrset = create_rrset(TEST_ZONE_NAME, RRClass.NONE(), RRType.AXFR(), 0,
  809. [ b'\x00' ])
  810. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  811. def __check_inzone_data(self, expected_result, name, rrtype,
  812. expected_rrset = None):
  813. '''Does a find on TEST_ZONE for the given rrset's name and type,
  814. then checks if the result matches the expected result.
  815. If so, and if expected_rrset is given, they are compared as
  816. well.'''
  817. _, finder = self._datasrc_client.find_zone(TEST_ZONE_NAME)
  818. result, found_rrset, _ = finder.find(name, rrtype,
  819. finder.NO_WILDCARD |
  820. finder.FIND_GLUE_OK)
  821. self.assertEqual(expected_result, result)
  822. # Sigh. Need rrsets.compare() again.
  823. # To be sure, compare name, class, type, and ttl
  824. if expected_rrset is not None:
  825. self.assertEqual(expected_rrset.get_name(), found_rrset.get_name())
  826. self.assertEqual(expected_rrset.get_class(), found_rrset.get_class())
  827. self.assertEqual(expected_rrset.get_type(), found_rrset.get_type())
  828. self.assertEqual(expected_rrset.get_ttl().to_text(),
  829. found_rrset.get_ttl().to_text())
  830. expected_rdata =\
  831. [ rdata.to_text() for rdata in expected_rrset.get_rdata() ]
  832. found_rdata =\
  833. [ rdata.to_text() for rdata in found_rrset.get_rdata() ]
  834. expected_rdata.sort()
  835. found_rdata.sort()
  836. self.assertEqual(expected_rdata, found_rdata)
  837. def test_update_add_delete_rrset(self):
  838. '''
  839. Tests a sequence of related add and delete updates. Some other
  840. cases are tested by later tests.
  841. '''
  842. self.__initialize_update_rrsets()
  843. # initially, the www should only contain one rr
  844. # (set to self.orig_a_rrset)
  845. # during this test, we will extend it at some point
  846. extended_a_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  847. RRType.A(), 3600,
  848. [ "192.0.2.1",
  849. "192.0.2.2",
  850. "192.0.2.3" ])
  851. # Sanity check, make sure original data is really there before updates
  852. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  853. isc.dns.Name("www.example.org"),
  854. RRType.A(),
  855. self.orig_a_rrset)
  856. # Add two rrs
  857. self.check_full_handle_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  858. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  859. isc.dns.Name("www.example.org"),
  860. RRType.A(),
  861. extended_a_rrset)
  862. # Adding the same RRsets should not make a difference.
  863. self.check_full_handle_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  864. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  865. isc.dns.Name("www.example.org"),
  866. RRType.A(),
  867. extended_a_rrset)
  868. # Now delete those two, and we should end up with the original RRset
  869. self.check_full_handle_result(Rcode.NOERROR(),
  870. [ self.rrset_update_del_rrset_part ])
  871. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  872. isc.dns.Name("www.example.org"),
  873. RRType.A(),
  874. self.orig_a_rrset)
  875. # 'Deleting' them again should make no difference
  876. self.check_full_handle_result(Rcode.NOERROR(),
  877. [ self.rrset_update_del_rrset_part ])
  878. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  879. isc.dns.Name("www.example.org"),
  880. RRType.A(),
  881. self.orig_a_rrset)
  882. # But deleting the entire rrset, independent of its contents, should
  883. # work
  884. self.check_full_handle_result(Rcode.NOERROR(),
  885. [ self.rrset_update_del_rrset ])
  886. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  887. isc.dns.Name("www.example.org"),
  888. RRType.A())
  889. # Check that if we update the SOA, it is updated to our value
  890. self.check_full_handle_result(Rcode.NOERROR(),
  891. [ self.rrset_update_soa2 ])
  892. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  893. isc.dns.Name("example.org"),
  894. RRType.SOA(),
  895. self.rrset_update_soa2)
  896. def test_glue_deletions(self):
  897. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  898. isc.dns.Name("sub.example.org."),
  899. RRType.NS())
  900. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  901. isc.dns.Name("ns.sub.example.org."),
  902. RRType.A())
  903. # See that we can delete glue
  904. rrset_delete_glue = create_rrset("ns.sub.example.org.",
  905. RRClass.ANY(),
  906. RRType.A(),
  907. 0)
  908. self.check_full_handle_result(Rcode.NOERROR(),
  909. [ rrset_delete_glue ])
  910. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  911. isc.dns.Name("sub.example.org."),
  912. RRType.NS())
  913. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  914. isc.dns.Name("ns.sub.example.org."),
  915. RRType.A())
  916. # Check that we don't accidentally delete a delegation if we
  917. # try to delete non-existent glue
  918. rrset_delete_nonexistent_glue = create_rrset("foo.sub.example.org.",
  919. RRClass.ANY(),
  920. RRType.A(),
  921. 0)
  922. self.check_full_handle_result(Rcode.NOERROR(),
  923. [ rrset_delete_nonexistent_glue ])
  924. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  925. isc.dns.Name("sub.example.org."),
  926. RRType.NS())
  927. def test_update_add_new_data(self):
  928. '''
  929. This tests adds data where none is present
  930. '''
  931. # Add data at a completely new name
  932. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  933. isc.dns.Name("new.example.org"),
  934. RRType.A())
  935. rrset = create_rrset("new.example.org", TEST_RRCLASS, RRType.A(),
  936. 3600, [ "192.0.2.1", "192.0.2.2" ])
  937. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  938. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  939. isc.dns.Name("new.example.org"),
  940. RRType.A(),
  941. rrset)
  942. # Also try a name where data is present, but none of this
  943. # specific type
  944. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  945. isc.dns.Name("new.example.org"),
  946. RRType.TXT())
  947. rrset = create_rrset("new.example.org", TEST_RRCLASS, RRType.TXT(),
  948. 3600, [ "foo" ])
  949. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  950. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  951. isc.dns.Name("new.example.org"),
  952. RRType.TXT(),
  953. rrset)
  954. def test_update_add_new_data_interspersed(self):
  955. '''
  956. This tests adds data where none is present, similar to
  957. test_update_add_new_data, but this time the second RRset
  958. is put into the record between the two RRs of the first
  959. RRset.
  960. '''
  961. # Add data at a completely new name
  962. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  963. isc.dns.Name("new_a.example.org"),
  964. RRType.A())
  965. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  966. isc.dns.Name("new_txt.example.org"),
  967. RRType.TXT())
  968. rrset1 = create_rrset("new_a.example.org", TEST_RRCLASS, RRType.A(),
  969. 3600, [ "192.0.2.1" ])
  970. rrset2 = create_rrset("new_txt.example.org", TEST_RRCLASS, RRType.TXT(),
  971. 3600, [ "foo" ])
  972. rrset3 = create_rrset("new_a.example.org", TEST_RRCLASS, RRType.A(),
  973. 3600, [ "192.0.2.2" ])
  974. self.check_full_handle_result(Rcode.NOERROR(),
  975. [ rrset1, rrset2, rrset3 ])
  976. # The update should have merged rrset1 and rrset3
  977. rrset_merged = create_rrset("new_a.example.org", TEST_RRCLASS,
  978. RRType.A(), 3600,
  979. [ "192.0.2.1", "192.0.2.2" ])
  980. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  981. isc.dns.Name("new_a.example.org"),
  982. RRType.A(),
  983. rrset_merged)
  984. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  985. isc.dns.Name("new_txt.example.org"),
  986. RRType.TXT(),
  987. rrset2)
  988. def test_update_delete_name(self):
  989. self.__initialize_update_rrsets()
  990. # First check it is there
  991. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  992. isc.dns.Name("www.example.org"),
  993. RRType.A())
  994. # Delete the entire name
  995. self.check_full_handle_result(Rcode.NOERROR(),
  996. [ self.rrset_update_del_name ])
  997. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  998. isc.dns.Name("www.example.org"),
  999. RRType.A())
  1000. # Should still be gone after pointless second delete
  1001. self.check_full_handle_result(Rcode.NOERROR(),
  1002. [ self.rrset_update_del_name ])
  1003. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  1004. isc.dns.Name("www.example.org"),
  1005. RRType.A())
  1006. def test_update_apex_special_cases(self):
  1007. '''
  1008. Tests a few special cases when deleting data from the apex
  1009. '''
  1010. self.__initialize_update_rrsets()
  1011. # the original SOA
  1012. orig_soa_rrset = create_rrset("example.org", TEST_RRCLASS,
  1013. RRType.SOA(), 3600,
  1014. [ "ns1.example.org. " +
  1015. "admin.example.org. " +
  1016. "1234 3600 1800 2419200 7200" ])
  1017. # We will delete some of the NS records
  1018. orig_ns_rrset = create_rrset("example.org", TEST_RRCLASS,
  1019. RRType.NS(), 3600,
  1020. [ "ns1.example.org.",
  1021. "ns2.example.org.",
  1022. "ns3.example.org." ])
  1023. # Sanity check, make sure original data is really there before updates
  1024. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1025. isc.dns.Name("example.org"),
  1026. RRType.NS(),
  1027. orig_ns_rrset)
  1028. # We will delete the MX record later in this test, so let's make
  1029. # sure that it exists (we do not care about its value)
  1030. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1031. isc.dns.Name("example.org"),
  1032. RRType.MX())
  1033. # Check that we cannot delete the SOA record by direction deletion
  1034. # both by name+type and by full rrset
  1035. self.check_full_handle_result(Rcode.NOERROR(),
  1036. [ self.rrset_update_del_soa_apex,
  1037. self.rrset_update_soa_del ])
  1038. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1039. isc.dns.Name("example.org"),
  1040. RRType.SOA(),
  1041. orig_soa_rrset)
  1042. # If we delete everything at the apex, the SOA and NS rrsets should be
  1043. # untouched
  1044. self.check_full_handle_result(Rcode.NOERROR(),
  1045. [ self.rrset_update_del_name_apex ])
  1046. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1047. isc.dns.Name("example.org"),
  1048. RRType.SOA(),
  1049. orig_soa_rrset)
  1050. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1051. isc.dns.Name("example.org"),
  1052. RRType.NS(),
  1053. orig_ns_rrset)
  1054. # but the MX should be gone
  1055. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  1056. isc.dns.Name("example.org"),
  1057. RRType.MX())
  1058. # Deleting the NS rrset by name and type only, it should also be left
  1059. # untouched
  1060. self.check_full_handle_result(Rcode.NOERROR(),
  1061. [ self.rrset_update_del_ns_apex ])
  1062. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1063. isc.dns.Name("example.org"),
  1064. RRType.NS(),
  1065. orig_ns_rrset)
  1066. def DISABLED_test_update_apex_special_case_ns_rrset(self):
  1067. # If we delete the NS at the apex specifically, it should still
  1068. # keep one record
  1069. self.__initialize_update_rrsets()
  1070. # When we are done, we should have a reduced NS rrset
  1071. short_ns_rrset = create_rrset("example.org", TEST_RRCLASS,
  1072. RRType.NS(), 3600,
  1073. [ "ns3.example.org." ])
  1074. self.check_full_handle_result(Rcode.NOERROR(),
  1075. [ self.rrset_update_del_rrset_ns ])
  1076. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1077. isc.dns.Name("example.org"),
  1078. RRType.NS(),
  1079. short_ns_rrset)
  1080. def test_update_delete_normal_rrset_at_apex(self):
  1081. '''
  1082. Tests a number of 'normal rrset' deletes at the apex
  1083. '''
  1084. # MX should simply be deleted
  1085. self.__initialize_update_rrsets()
  1086. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1087. isc.dns.Name("example.org"),
  1088. RRType.MX())
  1089. self.check_full_handle_result(Rcode.NOERROR(),
  1090. [ self.rrset_update_del_rrset_mx ])
  1091. self.__check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  1092. isc.dns.Name("example.org"),
  1093. RRType.MX())
  1094. def test_update_cname_special_cases(self):
  1095. self.__initialize_update_rrsets()
  1096. # Sanity check
  1097. orig_cname_rrset = create_rrset("cname.example.org", TEST_RRCLASS,
  1098. RRType.CNAME(), 3600,
  1099. [ "www.example.org." ])
  1100. self.__check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  1101. isc.dns.Name("cname.example.org"),
  1102. RRType.A(),
  1103. orig_cname_rrset)
  1104. # If we try to add data where a cname is preset
  1105. rrset = create_rrset("cname.example.org", TEST_RRCLASS, RRType.A(),
  1106. 3600, [ "192.0.2.1" ])
  1107. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  1108. self.__check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  1109. isc.dns.Name("cname.example.org"),
  1110. RRType.A(),
  1111. orig_cname_rrset)
  1112. # But updating the cname itself should work
  1113. new_cname_rrset = create_rrset("cname.example.org", TEST_RRCLASS,
  1114. RRType.CNAME(), 3600,
  1115. [ "mail.example.org." ])
  1116. self.check_full_handle_result(Rcode.NOERROR(), [ new_cname_rrset ])
  1117. self.__check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  1118. isc.dns.Name("cname.example.org"),
  1119. RRType.A(),
  1120. new_cname_rrset)
  1121. self.__initialize_update_rrsets()
  1122. # Likewise, adding a cname where other data is
  1123. # present should do nothing either
  1124. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1125. isc.dns.Name("www.example.org"),
  1126. RRType.A(),
  1127. self.orig_a_rrset)
  1128. new_cname_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  1129. RRType.CNAME(), 3600,
  1130. [ "mail.example.org." ])
  1131. self.check_full_handle_result(Rcode.NOERROR(), [ new_cname_rrset ])
  1132. self.__check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  1133. isc.dns.Name("www.example.org"),
  1134. RRType.A(),
  1135. self.orig_a_rrset)
  1136. def test_update_bad_class(self):
  1137. rrset = create_rrset("example.org.", RRClass.CH(), RRType.TXT(), 0,
  1138. [ "foo" ])
  1139. self.check_full_handle_result(Rcode.FORMERR(), [ rrset ])
  1140. def test_uncaught_exception(self):
  1141. def my_exc():
  1142. raise Exception("foo")
  1143. self._session._UpdateSession__update_soa = my_exc
  1144. self.assertEqual(Rcode.SERVFAIL().to_text(),
  1145. self._session._UpdateSession__do_update().to_text())
  1146. class SessionACLTest(SessionTestBase):
  1147. '''ACL related tests for update session.'''
  1148. def test_update_acl_check(self):
  1149. '''Test for various ACL checks.
  1150. Note that accepted cases are covered in the basic tests.
  1151. '''
  1152. # create a separate session, with default (empty) ACL map.
  1153. session = UpdateSession(self._update_msg,
  1154. TEST_CLIENT4, ZoneConfig([], TEST_RRCLASS,
  1155. self._datasrc_client))
  1156. # then the request should be rejected.
  1157. self.assertEqual((UPDATE_ERROR, None, None), session.handle())
  1158. # recreate the request message, and test with an ACL that would result
  1159. # in 'DROP'. get_message() should return None.
  1160. msg = create_update_msg()
  1161. acl_map = {(TEST_ZONE_NAME, TEST_RRCLASS):
  1162. REQUEST_LOADER.load([{"action": "DROP", "from":
  1163. TEST_CLIENT4[0]}])}
  1164. session = UpdateSession(msg, TEST_CLIENT4,
  1165. ZoneConfig([], TEST_RRCLASS,
  1166. self._datasrc_client, acl_map))
  1167. self.assertEqual((UPDATE_DROP, None, None), session.handle())
  1168. self.assertEqual(None, session.get_message())
  1169. def test_update_tsigacl_check(self):
  1170. '''Test for various ACL checks using TSIG.'''
  1171. # This ACL will accept requests from TEST_CLIENT4 (any port) *and*
  1172. # has TSIG signed by TEST_ZONE_NAME; all others will be rejected.
  1173. acl_map = {(TEST_ZONE_NAME, TEST_RRCLASS):
  1174. REQUEST_LOADER.load([{"action": "ACCEPT",
  1175. "from": TEST_CLIENT4[0],
  1176. "key": TEST_ZONE_NAME.to_text()}])}
  1177. # If the message doesn't contain TSIG, it doesn't match the ACCEPT
  1178. # ACL entry, and the request should be rejected.
  1179. session = UpdateSession(self._update_msg,
  1180. TEST_CLIENT4, ZoneConfig([], TEST_RRCLASS,
  1181. self._datasrc_client,
  1182. acl_map))
  1183. self.assertEqual((UPDATE_ERROR, None, None), session.handle())
  1184. self.check_response(session.get_message(), Rcode.REFUSED())
  1185. # If the message contains TSIG, it should match the ACCEPT
  1186. # ACL entry, and the request should be granted.
  1187. session = UpdateSession(create_update_msg(tsig_key=TEST_TSIG_KEY),
  1188. TEST_CLIENT4, ZoneConfig([], TEST_RRCLASS,
  1189. self._datasrc_client,
  1190. acl_map))
  1191. self.assertEqual((UPDATE_SUCCESS, TEST_ZONE_NAME, TEST_RRCLASS),
  1192. session.handle())
  1193. if __name__ == "__main__":
  1194. isc.log.init("bind10")
  1195. isc.log.resetUnitTestRootLogger()
  1196. unittest.main()