session_tests.py 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. def create_update_msg(zones=[TEST_ZONE_RECORD], prerequisites=[],
  36. updates=[]):
  37. msg = Message(Message.RENDER)
  38. msg.set_qid(5353) # arbitrary chosen
  39. msg.set_opcode(Opcode.UPDATE())
  40. msg.set_rcode(Rcode.NOERROR())
  41. for z in zones:
  42. msg.add_question(z)
  43. for p in prerequisites:
  44. msg.add_rrset(SECTION_PREREQUISITE, p)
  45. for u in updates:
  46. msg.add_rrset(SECTION_UPDATE, u)
  47. renderer = MessageRenderer()
  48. msg.to_wire(renderer)
  49. # re-read the created data in the parse mode
  50. msg.clear(Message.PARSE)
  51. msg.from_wire(renderer.get_data())
  52. return renderer.get_data(), msg
  53. def create_rrset(name, rrclass, rrtype, ttl, rdatas = []):
  54. '''
  55. Helper method to easily create RRsets, auto-converts
  56. name, rrclass, rrtype, and ttl (if possibly through their
  57. respective constructors)
  58. rdatas is a list of rr data strings, or bytestrings, which
  59. should match the RRType of the rrset to create
  60. '''
  61. if type(name) != Name:
  62. name = Name(name)
  63. if type(rrclass) != RRClass:
  64. rrclass = RRClass(rrclass)
  65. if type(rrtype) != RRType:
  66. rrtype = RRType(rrtype)
  67. if type(ttl) != RRTTL:
  68. ttl = RRTTL(ttl)
  69. rrset = isc.dns.RRset(name, rrclass, rrtype, ttl)
  70. for rdata in rdatas:
  71. add_rdata(rrset, rdata)
  72. return rrset
  73. def add_rdata(rrset, rdata):
  74. '''
  75. Helper function for easily adding Rdata fields to RRsets.
  76. This function assumes the given rdata is of type string or bytes,
  77. and corresponds to the given rrset
  78. '''
  79. rrset.add_rdata(isc.dns.Rdata(rrset.get_type(),
  80. rrset.get_class(),
  81. rdata))
  82. class SessionTest(unittest.TestCase):
  83. '''Session tests'''
  84. def setUp(self):
  85. shutil.copyfile(READ_ZONE_DB_FILE, WRITE_ZONE_DB_FILE)
  86. self.__datasrc_client = DataSourceClient("sqlite3",
  87. WRITE_ZONE_DB_CONFIG)
  88. self.__update_msgdata, self.__update_msg = create_update_msg()
  89. self.__session = UpdateSession(self.__update_msg,
  90. self.__update_msgdata, TEST_CLIENT4,
  91. ZoneConfig([], TEST_RRCLASS,
  92. self.__datasrc_client))
  93. self.__session._UpdateSession__get_update_zone()
  94. def check_response(self, msg, expected_rcode):
  95. '''Perform common checks on update resposne message.'''
  96. self.assertTrue(msg.get_header_flag(Message.HEADERFLAG_QR))
  97. # note: we convert opcode to text it'd be more helpful on failure.
  98. self.assertEqual(Opcode.UPDATE().to_text(), msg.get_opcode().to_text())
  99. self.assertEqual(expected_rcode.to_text(), msg.get_rcode().to_text())
  100. # All sections should be cleared
  101. self.assertEqual(0, msg.get_rr_count(SECTION_ZONE))
  102. self.assertEqual(0, msg.get_rr_count(SECTION_PREREQUISITE))
  103. self.assertEqual(0, msg.get_rr_count(SECTION_UPDATE))
  104. self.assertEqual(0, msg.get_rr_count(Message.SECTION_ADDITIONAL))
  105. def test_handle(self):
  106. '''Basic update case'''
  107. result, zname, zclass = self.__session.handle()
  108. self.assertEqual(UPDATE_SUCCESS, result)
  109. self.assertEqual(TEST_ZONE_NAME, zname)
  110. self.assertEqual(TEST_RRCLASS, zclass)
  111. # Just checking these are different from the success code.
  112. self.assertNotEqual(UPDATE_ERROR, result)
  113. self.assertNotEqual(UPDATE_DROP, result)
  114. def test_broken_request(self):
  115. # Zone section is empty
  116. msg_data, msg = create_update_msg(zones=[])
  117. session = UpdateSession(msg, msg_data, TEST_CLIENT6, None)
  118. result, zname, zclass = session.handle()
  119. self.assertEqual(UPDATE_ERROR, result)
  120. self.assertEqual(None, zname)
  121. self.assertEqual(None, zclass)
  122. self.check_response(session.get_message(), Rcode.FORMERR())
  123. # Zone section contains multiple records
  124. msg_data, msg = create_update_msg(zones=[TEST_ZONE_RECORD,
  125. TEST_ZONE_RECORD])
  126. session = UpdateSession(msg, msg_data, TEST_CLIENT4, None)
  127. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  128. self.check_response(session.get_message(), Rcode.FORMERR())
  129. # Zone section's type is not SOA
  130. msg_data, msg = create_update_msg(zones=[Question(TEST_ZONE_NAME,
  131. TEST_RRCLASS,
  132. RRType.A())])
  133. session = UpdateSession(msg, msg_data, TEST_CLIENT4, None)
  134. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  135. self.check_response(session.get_message(), Rcode.FORMERR())
  136. def test_update_secondary(self):
  137. # specified zone is configured as a secondary. Since this
  138. # implementation doesn't support update forwarding, the result
  139. # should be NOTIMP.
  140. msg_data, msg = create_update_msg(zones=[Question(TEST_ZONE_NAME,
  141. TEST_RRCLASS,
  142. RRType.SOA())])
  143. session = UpdateSession(msg, msg_data, TEST_CLIENT4,
  144. ZoneConfig([(TEST_ZONE_NAME, TEST_RRCLASS)],
  145. TEST_RRCLASS,
  146. self.__datasrc_client))
  147. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  148. self.check_response(session.get_message(), Rcode.NOTIMP())
  149. def check_notauth(self, zname, zclass=TEST_RRCLASS):
  150. '''Common test sequence for the 'notauth' test'''
  151. msg_data, msg = create_update_msg(zones=[Question(zname, zclass,
  152. RRType.SOA())])
  153. session = UpdateSession(msg, msg_data, TEST_CLIENT4,
  154. ZoneConfig([(TEST_ZONE_NAME, TEST_RRCLASS)],
  155. TEST_RRCLASS,
  156. self.__datasrc_client))
  157. self.assertEqual(UPDATE_ERROR, session.handle()[0])
  158. self.check_response(session.get_message(), Rcode.NOTAUTH())
  159. def test_update_notauth(self):
  160. '''Update attempt for non authoritative zones'''
  161. # zone name doesn't match
  162. self.check_notauth(Name('example.com'))
  163. # zone name is a subdomain of the actual authoritative zone
  164. # (match must be exact)
  165. self.check_notauth(Name('sub.example.org'))
  166. # zone class doesn't match
  167. self.check_notauth(Name('example.org'), RRClass.CH())
  168. def foreach_rr_in_rrset_helper(self, rr, l):
  169. l.append(rr.to_text())
  170. def test_foreach_rr_in_rrset(self):
  171. rrset = create_rrset("www.example.org", TEST_RRCLASS,
  172. RRType.A(), 3600, [ "192.0.2.1" ])
  173. l = []
  174. foreach_rr_in_rrset(rrset, self.foreach_rr_in_rrset_helper, rrset, l)
  175. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n"], l)
  176. add_rdata(rrset, "192.0.2.2")
  177. add_rdata(rrset, "192.0.2.3")
  178. # if the helper is called directly, the list should have
  179. # one entry, with a multiline string
  180. # but through the helper, there should be several 1-line entries
  181. l = []
  182. self.foreach_rr_in_rrset_helper(rrset, l)
  183. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n" +
  184. "www.example.org. 3600 IN A 192.0.2.2\n" +
  185. "www.example.org. 3600 IN A 192.0.2.3\n"
  186. ], l)
  187. # but through the helper, there should be several 1-line entries
  188. l = []
  189. foreach_rr_in_rrset(rrset, self.foreach_rr_in_rrset_helper, rrset, l)
  190. self.assertEqual(["www.example.org. 3600 IN A 192.0.2.1\n",
  191. "www.example.org. 3600 IN A 192.0.2.2\n",
  192. "www.example.org. 3600 IN A 192.0.2.3\n",
  193. ], l)
  194. def test_convert_rrset_class(self):
  195. # Converting an RRSET to a different class should work
  196. # if the rdata types can be converted
  197. rrset = create_rrset("www.example.org", RRClass.NONE(), RRType.A(),
  198. 3600, [ b'\xc0\x00\x02\x01', b'\xc0\x00\x02\x02'])
  199. rrset2 = convert_rrset_class(rrset, RRClass.IN())
  200. self.assertEqual("www.example.org. 3600 IN A 192.0.2.1\n" +
  201. "www.example.org. 3600 IN A 192.0.2.2\n",
  202. str(rrset2))
  203. rrset3 = convert_rrset_class(rrset2, RRClass.NONE())
  204. self.assertEqual("www.example.org. 3600 CLASS254 A \\# 4 " +
  205. "c0000201\nwww.example.org. 3600 CLASS254 " +
  206. "A \\# 4 c0000202\n",
  207. str(rrset3))
  208. # depending on what type of bad data is given, a number
  209. # of different exceptions could be raised (TODO: i recall
  210. # there was a ticket about making a better hierarchy for
  211. # dns/parsing related exceptions)
  212. self.assertRaises(InvalidRdataLength, convert_rrset_class,
  213. rrset, RRClass.CH())
  214. add_rdata(rrset, b'\xc0\x00')
  215. self.assertRaises(DNSMessageFORMERR, convert_rrset_class,
  216. rrset, RRClass.IN())
  217. def __prereq_helper(self, method, expected, rrset):
  218. '''Calls the given method with self.__datasrc_client
  219. and the given rrset, and compares the return value.
  220. Function does not do much but makes the code look nicer'''
  221. self.assertEqual(expected, method(rrset))
  222. def __check_prerequisite_exists_combined(self, method, rrclass, expected):
  223. '''shared code for the checks for the very similar (but reversed
  224. in behaviour) methods __prereq_rrset_exists and
  225. __prereq_rrset_does_not_exist.
  226. For rrset_exists, rrclass should be ANY, for rrset_does_not_exist,
  227. it should be NONE.
  228. '''
  229. # Basic existence checks
  230. # www.example.org should have an A, but not an MX
  231. rrset = create_rrset("www.example.org", rrclass, RRType.A(), 0)
  232. self.__prereq_helper(method, expected, rrset)
  233. rrset = create_rrset("www.example.org", rrclass, RRType.MX(), 0)
  234. self.__prereq_helper(method, not expected, rrset)
  235. # example.org should have an MX, but not an A
  236. rrset = create_rrset("example.org", rrclass, RRType.MX(), 0)
  237. self.__prereq_helper(method, expected, rrset)
  238. rrset = create_rrset("example.org", rrclass, RRType.A(), 0)
  239. self.__prereq_helper(method, not expected, rrset)
  240. # Also check the case where the name does not even exist
  241. rrset = create_rrset("doesnotexist.example.org", rrclass, RRType.A(), 0)
  242. self.__prereq_helper(method, not expected, rrset)
  243. # Wildcard expansion should not be applied, but literal matches
  244. # should work
  245. rrset = create_rrset("foo.wildcard.example.org", rrclass, RRType.A(), 0)
  246. self.__prereq_helper(method, not expected, rrset)
  247. rrset = create_rrset("*.wildcard.example.org", rrclass, RRType.A(), 0)
  248. self.__prereq_helper(method, expected, rrset)
  249. # Likewise, CNAME directly should match, but what it points to should
  250. # not
  251. rrset = create_rrset("cname.example.org", rrclass, RRType.A(), 0)
  252. self.__prereq_helper(method, not expected, rrset)
  253. rrset = create_rrset("cname.example.org", rrclass, RRType.CNAME(), 0)
  254. self.__prereq_helper(method, expected, rrset)
  255. # And also make sure a delegation (itself) is not treated as existing
  256. # data
  257. rrset = create_rrset("foo.sub.example.org", rrclass, RRType.A(), 0)
  258. self.__prereq_helper(method, not expected, rrset)
  259. # But the delegation data itself should match
  260. rrset = create_rrset("sub.example.org", rrclass, RRType.NS(), 0)
  261. self.__prereq_helper(method, expected, rrset)
  262. # As should glue
  263. rrset = create_rrset("ns.sub.example.org", rrclass, RRType.A(), 0)
  264. self.__prereq_helper(method, expected, rrset)
  265. def test_check_prerequisite_exists(self):
  266. method = self.__session._UpdateSession__prereq_rrset_exists
  267. self.__check_prerequisite_exists_combined(method,
  268. RRClass.ANY(),
  269. True)
  270. def test_check_prerequisite_does_not_exist(self):
  271. method = self.__session._UpdateSession__prereq_rrset_does_not_exist
  272. self.__check_prerequisite_exists_combined(method,
  273. RRClass.NONE(),
  274. False)
  275. def test_check_prerequisite_exists_value(self):
  276. method = self.__session._UpdateSession__prereq_rrset_exists_value
  277. rrset = create_rrset("www.example.org", RRClass.IN(), RRType.A(), 0)
  278. # empty one should not match
  279. self.__prereq_helper(method, False, rrset)
  280. # When the rdata is added, it should match
  281. add_rdata(rrset, "192.0.2.1")
  282. self.__prereq_helper(method, True, rrset)
  283. # But adding more should not
  284. add_rdata(rrset, "192.0.2.2")
  285. self.__prereq_helper(method, False, rrset)
  286. # Also test one with more than one RR
  287. rrset = create_rrset("example.org", RRClass.IN(), RRType.NS(), 0)
  288. self.__prereq_helper(method, False, rrset)
  289. add_rdata(rrset, "ns1.example.org.")
  290. self.__prereq_helper(method, False, rrset)
  291. add_rdata(rrset, "ns2.example.org")
  292. self.__prereq_helper(method, False, rrset)
  293. add_rdata(rrset, "ns3.example.org.")
  294. self.__prereq_helper(method, True, rrset)
  295. add_rdata(rrset, "ns4.example.org.")
  296. self.__prereq_helper(method, False, rrset)
  297. # Repeat that, but try a different order of Rdata addition
  298. rrset = create_rrset("example.org", RRClass.IN(), RRType.NS(), 0)
  299. self.__prereq_helper(method, False, rrset)
  300. add_rdata(rrset, "ns3.example.org.")
  301. self.__prereq_helper(method, False, rrset)
  302. add_rdata(rrset, "ns2.example.org.")
  303. self.__prereq_helper(method, False, rrset)
  304. add_rdata(rrset, "ns1.example.org.")
  305. self.__prereq_helper(method, True, rrset)
  306. add_rdata(rrset, "ns4.example.org.")
  307. self.__prereq_helper(method, False, rrset)
  308. # and test one where the name does not even exist
  309. rrset = create_rrset("doesnotexist.example.org", RRClass.IN(),
  310. RRType.A(), 0, [ "192.0.2.1" ])
  311. self.__prereq_helper(method, False, rrset)
  312. def __check_prerequisite_name_in_use_combined(self, method, rrclass,
  313. expected):
  314. '''shared code for the checks for the very similar (but reversed
  315. in behaviour) methods __prereq_name_in_use and
  316. __prereq_name_not_in_use
  317. '''
  318. rrset = create_rrset("example.org", rrclass, RRType.ANY(), 0)
  319. self.__prereq_helper(method, expected, rrset)
  320. rrset = create_rrset("www.example.org", rrclass, RRType.ANY(), 0)
  321. self.__prereq_helper(method, expected, rrset)
  322. rrset = create_rrset("doesnotexist.example.org", rrclass,
  323. RRType.ANY(), 0)
  324. self.__prereq_helper(method, not expected, rrset)
  325. rrset = create_rrset("belowdelegation.sub.example.org", rrclass,
  326. RRType.ANY(), 0)
  327. self.__prereq_helper(method, not expected, rrset)
  328. rrset = create_rrset("foo.wildcard.example.org", rrclass,
  329. RRType.ANY(), 0)
  330. self.__prereq_helper(method, not expected, rrset)
  331. # empty nonterminal should not match
  332. rrset = create_rrset("nonterminal.example.org", rrclass,
  333. RRType.ANY(), 0)
  334. self.__prereq_helper(method, not expected, rrset)
  335. rrset = create_rrset("empty.nonterminal.example.org", rrclass,
  336. RRType.ANY(), 0)
  337. self.__prereq_helper(method, expected, rrset)
  338. def test_check_prerequisite_name_in_use(self):
  339. method = self.__session._UpdateSession__prereq_name_in_use
  340. self.__check_prerequisite_name_in_use_combined(method,
  341. RRClass.ANY(),
  342. True)
  343. def test_check_prerequisite_name_not_in_use(self):
  344. method = self.__session._UpdateSession__prereq_name_not_in_use
  345. self.__check_prerequisite_name_in_use_combined(method,
  346. RRClass.NONE(),
  347. False)
  348. def check_prerequisite_result(self, expected, prerequisites):
  349. '''Helper method for checking the result of a prerequisite check;
  350. creates an update session, and fills it with the list of rrsets
  351. from 'prerequisites'. Then checks if __check_prerequisites()
  352. returns the Rcode specified in 'expected'.'''
  353. msg_data, msg = create_update_msg([TEST_ZONE_RECORD],
  354. prerequisites)
  355. zconfig = ZoneConfig([], TEST_RRCLASS, self.__datasrc_client)
  356. session = UpdateSession(msg, msg_data, TEST_CLIENT4, zconfig)
  357. session._UpdateSession__get_update_zone()
  358. # compare the to_text output of the rcodes (nicer error messages)
  359. # This call itself should also be done by handle(),
  360. # but just for better failures, it is first called on its own
  361. self.assertEqual(expected.to_text(),
  362. session._UpdateSession__check_prerequisites().to_text())
  363. # Now see if handle finds the same result
  364. (result, _, _) = session.handle()
  365. self.assertEqual(expected,
  366. session._UpdateSession__message.get_rcode())
  367. # And that the result looks right
  368. if expected == Rcode.NOERROR():
  369. self.assertEqual(UPDATE_SUCCESS, result)
  370. else:
  371. self.assertEqual(UPDATE_ERROR, result)
  372. # TODO: remove dupe with above one
  373. def check_prescan_result(self, expected, updates, expected_soa = None):
  374. '''Helper method for checking the result of a prerequisite check;
  375. creates an update session, and fills it with the list of rrsets
  376. from 'updates'. Then checks if __do_prescan()
  377. returns the Rcode specified in 'expected'.'''
  378. msg_data, msg = create_update_msg([TEST_ZONE_RECORD],
  379. [], updates)
  380. zconfig = ZoneConfig([], TEST_RRCLASS, self.__datasrc_client)
  381. session = UpdateSession(msg, msg_data, TEST_CLIENT4, zconfig)
  382. session._UpdateSession__get_update_zone()
  383. # compare the to_text output of the rcodes (nicer error messages)
  384. # This call itself should also be done by handle(),
  385. # but just for better failures, it is first called on its own
  386. self.assertEqual(expected.to_text(),
  387. session._UpdateSession__do_prescan().to_text())
  388. # If there is an expected soa, check it
  389. self.assertEqual(str(expected_soa),
  390. str(session._UpdateSession__added_soa))
  391. # REMOVED, don't mess with actual data during prescan tests
  392. # Now see if handle finds the same result
  393. #(result, _, _) = session.handle()
  394. #self.assertEqual(expected,
  395. # session._UpdateSession__message.get_rcode())
  396. ## And that the result looks right
  397. #if expected == Rcode.NOERROR():
  398. # self.assertEqual(UPDATE_SUCCESS, result)
  399. #else:
  400. # self.assertEqual(UPDATE_ERROR, result)
  401. # TODO XXX: remove dupe with above
  402. def check_full_handle_result(self, expected, updates):
  403. '''Helper method for checking the result of a full handle;
  404. creates an update session, and fills it with the list of rrsets
  405. from 'updates'. Then checks if __handle()
  406. results in a response with rcode 'expected'.'''
  407. msg_data, msg = create_update_msg([TEST_ZONE_RECORD],
  408. [], updates)
  409. zconfig = ZoneConfig([], TEST_RRCLASS, self.__datasrc_client)
  410. session = UpdateSession(msg, msg_data, TEST_CLIENT4, zconfig)
  411. # Now see if handle finds the same result
  412. (result, _, _) = session.handle()
  413. self.assertEqual(expected.to_text(),
  414. session._UpdateSession__message.get_rcode().to_text())
  415. # And that the result looks right
  416. if expected == Rcode.NOERROR():
  417. self.assertEqual(UPDATE_SUCCESS, result)
  418. else:
  419. self.assertEqual(UPDATE_ERROR, result)
  420. def test_check_prerequisites(self):
  421. # This test checks if the actual prerequisite-type-specific
  422. # methods are called.
  423. # It does test all types of prerequisites, but it does not test
  424. # every possible result for those types (those are tested above,
  425. # in the specific prerequisite type tests)
  426. # Let's first define a number of prereq's that should succeed
  427. rrset_exists_yes = create_rrset("example.org", RRClass.ANY(),
  428. RRType.SOA(), 0)
  429. rrset_exists_value_yes = create_rrset("www.example.org", RRClass.IN(),
  430. RRType.A(), 0, [ "192.0.2.1" ])
  431. rrset_does_not_exist_yes = create_rrset("foo.example.org",
  432. RRClass.NONE(), RRType.SOA(),
  433. 0)
  434. name_in_use_yes = create_rrset("www.example.org", RRClass.ANY(),
  435. RRType.ANY(), 0)
  436. name_not_in_use_yes = create_rrset("foo.example.org", RRClass.NONE(),
  437. RRType.ANY(), 0)
  438. rrset_exists_value_1 = create_rrset("example.org", RRClass.IN(),
  439. RRType.NS(), 0,
  440. [ "ns1.example.org" ])
  441. rrset_exists_value_2 = create_rrset("example.org", RRClass.IN(),
  442. RRType.NS(), 0,
  443. [ "ns2.example.org" ])
  444. rrset_exists_value_3 = create_rrset("example.org", RRClass.IN(),
  445. RRType.NS(), 0,
  446. [ "ns3.example.org" ])
  447. # and a number that should not
  448. rrset_exists_no = create_rrset("foo.example.org", RRClass.ANY(),
  449. RRType.SOA(), 0)
  450. rrset_exists_value_no = create_rrset("www.example.org", RRClass.IN(),
  451. RRType.A(), 0, [ "192.0.2.2" ])
  452. rrset_does_not_exist_no = create_rrset("example.org", RRClass.NONE(),
  453. RRType.SOA(), 0)
  454. name_in_use_no = create_rrset("foo.example.org", RRClass.ANY(),
  455. RRType.ANY(), 0)
  456. name_not_in_use_no = create_rrset("www.example.org", RRClass.NONE(),
  457. RRType.ANY(), 0)
  458. # Create an UPDATE with all 5 'yes' prereqs
  459. data, update = create_update_msg([TEST_ZONE_RECORD],
  460. [
  461. rrset_exists_yes,
  462. rrset_does_not_exist_yes,
  463. name_in_use_yes,
  464. name_not_in_use_yes,
  465. rrset_exists_value_yes,
  466. ])
  467. # check 'no' result codes
  468. self.check_prerequisite_result(Rcode.NXRRSET(),
  469. [ rrset_exists_no ])
  470. self.check_prerequisite_result(Rcode.NXRRSET(),
  471. [ rrset_exists_value_no ])
  472. self.check_prerequisite_result(Rcode.YXRRSET(),
  473. [ rrset_does_not_exist_no ])
  474. self.check_prerequisite_result(Rcode.NXDOMAIN(),
  475. [ name_in_use_no ])
  476. self.check_prerequisite_result(Rcode.YXDOMAIN(),
  477. [ name_not_in_use_no ])
  478. # the 'yes' codes should result in ok
  479. self.check_prerequisite_result(Rcode.NOERROR(),
  480. [ rrset_exists_yes,
  481. rrset_exists_value_yes,
  482. rrset_does_not_exist_yes,
  483. name_in_use_yes,
  484. name_not_in_use_yes,
  485. rrset_exists_value_1,
  486. rrset_exists_value_2,
  487. rrset_exists_value_3])
  488. # try out a permutation, note that one rrset is split up,
  489. # and the order of the RRs should not matter
  490. self.check_prerequisite_result(Rcode.NOERROR(),
  491. [ rrset_exists_value_3,
  492. rrset_exists_yes,
  493. rrset_exists_value_2,
  494. name_in_use_yes,
  495. rrset_exists_value_1])
  496. # Should fail on the first error, even if most of the
  497. # prerequisites are ok
  498. self.check_prerequisite_result(Rcode.NXDOMAIN(),
  499. [ rrset_exists_value_3,
  500. rrset_exists_yes,
  501. rrset_exists_value_2,
  502. name_in_use_yes,
  503. name_in_use_no,
  504. rrset_exists_value_1])
  505. def test_prerequisite_notzone(self):
  506. rrset = create_rrset("some.other.zone.", RRClass.ANY(), RRType.SOA(), 0)
  507. self.check_prerequisite_result(Rcode.NOTZONE(), [ rrset ])
  508. def test_prerequisites_formerr(self):
  509. # test for form errors in the prerequisite section
  510. # Class ANY, non-zero TTL
  511. rrset = create_rrset("example.org", RRClass.ANY(), RRType.SOA(), 1)
  512. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  513. # Class ANY, but with rdata
  514. rrset = create_rrset("example.org", RRClass.ANY(), RRType.A(), 0,
  515. [ b'\x00\x00\x00\x00' ])
  516. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  517. # Class NONE, non-zero TTL
  518. rrset = create_rrset("example.org", RRClass.NONE(), RRType.SOA(), 1)
  519. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  520. # Class NONE, but with rdata
  521. rrset = create_rrset("example.org", RRClass.NONE(), RRType.A(), 0,
  522. [ b'\x00\x00\x00\x00' ])
  523. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  524. # Matching class and type, but non-zero TTL
  525. rrset = create_rrset("www.example.org", RRClass.IN(), RRType.A(), 1,
  526. [ "192.0.2.1" ])
  527. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  528. # Completely different class
  529. rrset = create_rrset("example.org", RRClass.CH(), RRType.TXT(), 0,
  530. [ "foo" ])
  531. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  532. def __prereq_helper(self, method, expected, rrset):
  533. '''Calls the given method with self.__datasrc_client
  534. and the given rrset, and compares the return value.
  535. Function does not do much but makes the code look nicer'''
  536. self.assertEqual(expected, method(rrset))
  537. def initialize_update_rrsets(self):
  538. '''Prepare a number of RRsets to be used in several update tests
  539. The rrsets are stored in self'''
  540. orig_a_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  541. RRType.A(), 3600, [ "192.0.2.1" ])
  542. self.orig_a_rrset = orig_a_rrset
  543. rrset_update_a = create_rrset("www.example.org", TEST_RRCLASS,
  544. RRType.A(), 3600,
  545. [ "192.0.2.2", "192.0.2.3" ])
  546. self.rrset_update_a = rrset_update_a
  547. rrset_update_soa = create_rrset("example.org", TEST_RRCLASS,
  548. RRType.SOA(), 3600,
  549. [ "ns1.example.org. " +
  550. "admin.example.org. " +
  551. "1233 3600 1800 2419200 7200" ])
  552. self.rrset_update_soa = rrset_update_soa
  553. rrset_update_soa_del = create_rrset("example.org", RRClass.NONE(),
  554. RRType.SOA(), 0,
  555. [ "ns1.example.org. " +
  556. "admin.example.org. " +
  557. "1233 3600 1800 2419200 7200" ])
  558. self.rrset_update_soa_del = rrset_update_soa_del
  559. rrset_update_soa2 = create_rrset("example.org", TEST_RRCLASS,
  560. RRType.SOA(), 3600,
  561. [ "ns1.example.org. " +
  562. "admin.example.org. " +
  563. "4000 3600 1800 2419200 7200" ])
  564. self.rrset_update_soa2 = rrset_update_soa2
  565. rrset_update_del_name = create_rrset("www.example.org", RRClass.ANY(),
  566. RRType.ANY(), 0)
  567. self.rrset_update_del_name = rrset_update_del_name
  568. rrset_update_del_name_apex = create_rrset("example.org", RRClass.ANY(),
  569. RRType.ANY(), 0)
  570. self.rrset_update_del_name_apex = rrset_update_del_name_apex
  571. rrset_update_del_rrset = create_rrset("www.example.org", RRClass.ANY(),
  572. RRType.A(), 0)
  573. self.rrset_update_del_rrset = rrset_update_del_rrset
  574. rrset_update_del_mx_apex = create_rrset("example.org", RRClass.ANY(),
  575. RRType.MX(), 0)
  576. self.rrset_update_del_mx_apex = rrset_update_del_mx_apex
  577. rrset_update_del_soa_apex = create_rrset("example.org", RRClass.ANY(),
  578. RRType.SOA(), 0)
  579. self.rrset_update_del_soa_apex = rrset_update_del_soa_apex
  580. rrset_update_del_ns_apex = create_rrset("example.org", RRClass.ANY(),
  581. RRType.NS(), 0)
  582. self.rrset_update_del_ns_apex = rrset_update_del_ns_apex
  583. rrset_update_del_rrset_part = create_rrset("www.example.org",
  584. RRClass.NONE(), RRType.A(),
  585. 0,
  586. [ b'\xc0\x00\x02\x02',
  587. b'\xc0\x00\x02\x03' ])
  588. self.rrset_update_del_rrset_part = rrset_update_del_rrset_part
  589. rrset_update_del_rrset_ns = create_rrset("example.org", RRClass.NONE(),
  590. RRType.NS(), 0,
  591. [ b'\x03ns1\x07example\x03org\x00',
  592. b'\x03ns2\x07example\x03org\x00',
  593. b'\x03ns3\x07example\x03org\x00' ])
  594. self.rrset_update_del_rrset_ns = rrset_update_del_rrset_ns
  595. rrset_update_del_rrset_mx = create_rrset("example.org", RRClass.NONE(),
  596. RRType.MX(), 0,
  597. [ b'\x00\x0a\x04mail\x07example\x03org\x00' ])
  598. self.rrset_update_del_rrset_mx = rrset_update_del_rrset_mx
  599. def test_prescan(self):
  600. '''Test whether the prescan succeeds on data that is ok, and whether
  601. if notices the SOA if present'''
  602. # prepare a set of correct update statements
  603. self.initialize_update_rrsets()
  604. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  605. # check if soa is noticed
  606. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_soa ],
  607. self.rrset_update_soa)
  608. # Other types of succesful prechecks
  609. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_soa2 ],
  610. self.rrset_update_soa2)
  611. self.check_prescan_result(Rcode.NOERROR(),
  612. [ self.rrset_update_del_name ])
  613. self.check_prescan_result(Rcode.NOERROR(),
  614. [ self.rrset_update_del_name_apex ])
  615. self.check_prescan_result(Rcode.NOERROR(),
  616. [ self.rrset_update_del_rrset ])
  617. self.check_prescan_result(Rcode.NOERROR(),
  618. [ self.rrset_update_del_mx_apex ])
  619. self.check_prescan_result(Rcode.NOERROR(),
  620. [ self.rrset_update_del_rrset_part ])
  621. # and check a few permutations of the above
  622. # all of them (with one of the soas)
  623. self.check_prescan_result(Rcode.NOERROR(),
  624. [
  625. self.rrset_update_a,
  626. self.rrset_update_soa,
  627. self.rrset_update_del_name,
  628. self.rrset_update_del_name_apex,
  629. self.rrset_update_del_rrset,
  630. self.rrset_update_del_mx_apex,
  631. self.rrset_update_del_rrset_part
  632. ],
  633. self.rrset_update_soa)
  634. # Two soas. Should we reject or simply use the last?
  635. # (RFC is not really explicit on this, but between the lines I read
  636. # use the last)
  637. # TODO this fails ;)
  638. self.check_prescan_result(Rcode.NOERROR(),
  639. [ self.rrset_update_soa,
  640. self.rrset_update_soa2 ],
  641. self.rrset_update_soa2)
  642. self.check_prescan_result(Rcode.NOERROR(),
  643. [ self.rrset_update_soa2,
  644. self.rrset_update_soa ],
  645. self.rrset_update_soa)
  646. self.check_prescan_result(Rcode.NOERROR(),
  647. [
  648. self.rrset_update_del_mx_apex,
  649. self.rrset_update_del_name,
  650. self.rrset_update_del_name_apex,
  651. self.rrset_update_del_rrset_part,
  652. self.rrset_update_a,
  653. self.rrset_update_del_rrset,
  654. self.rrset_update_soa
  655. ],
  656. self.rrset_update_soa)
  657. def test_prescan_failures(self):
  658. '''Test whether prescan fails on bad data'''
  659. # out of zone data
  660. rrset = create_rrset("different.zone", RRClass.ANY(), RRType.TXT(), 0)
  661. self.check_prescan_result(Rcode.NOTZONE(), [ rrset ])
  662. # forbidden type, zone class
  663. rrset = create_rrset(TEST_ZONE_NAME, TEST_RRCLASS, RRType.ANY(), 0,
  664. [ b'\x00' ])
  665. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  666. # non-zero TTL, class ANY
  667. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.TXT(), 1)
  668. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  669. # non-zero Rdata, class ANY
  670. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.TXT(), 0,
  671. [ "foo" ])
  672. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  673. # forbidden type, class ANY
  674. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.AXFR(), 0,
  675. [ b'\x00' ])
  676. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  677. # non-zero TTL, class NONE
  678. rrset = create_rrset(TEST_ZONE_NAME, RRClass.NONE(), RRType.TXT(), 1)
  679. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  680. # forbidden type, class NONE
  681. rrset = create_rrset(TEST_ZONE_NAME, RRClass.NONE(), RRType.AXFR(), 0,
  682. [ b'\x00' ])
  683. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  684. def check_inzone_data(self, expected_result, name, rrtype,
  685. expected_rrset = None):
  686. '''Does a find on TEST_ZONE for the given rrset's name and type,
  687. then checks if the result matches the expected result.
  688. If so, and if expected_rrset is given, they are compared as
  689. well.'''
  690. _, finder = self.__datasrc_client.find_zone(TEST_ZONE_NAME)
  691. result, found_rrset, _ = finder.find(name, rrtype,
  692. finder.NO_WILDCARD |
  693. finder.FIND_GLUE_OK)
  694. self.assertEqual(expected_result, result)
  695. # Sigh. Need rrsets.compare() again.
  696. # To be sure, compare name, class, type, and ttl
  697. if expected_rrset is not None:
  698. self.assertEqual(expected_rrset.get_name(), found_rrset.get_name())
  699. self.assertEqual(expected_rrset.get_class(), found_rrset.get_class())
  700. self.assertEqual(expected_rrset.get_type(), found_rrset.get_type())
  701. self.assertEqual(expected_rrset.get_ttl().to_text(),
  702. found_rrset.get_ttl().to_text())
  703. expected_rdata =\
  704. [ rdata.to_text() for rdata in expected_rrset.get_rdata() ]
  705. found_rdata =\
  706. [ rdata.to_text() for rdata in found_rrset.get_rdata() ]
  707. expected_rdata.sort()
  708. found_rdata.sort()
  709. self.assertEqual(expected_rdata, found_rdata)
  710. def test_update_add_delete_rrset(self):
  711. '''
  712. Tests a sequence of related add and delete updates. Some other
  713. cases are tested by later tests.
  714. '''
  715. self.initialize_update_rrsets()
  716. # initially, the www should only contain one rr
  717. # (set to self.orig_a_rrset)
  718. # during this test, we will extend it at some point
  719. extended_a_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  720. RRType.A(), 3600,
  721. [ "192.0.2.1",
  722. "192.0.2.2",
  723. "192.0.2.3" ])
  724. # Sanity check, make sure original data is really there before updates
  725. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  726. isc.dns.Name("www.example.org"),
  727. RRType.A(),
  728. self.orig_a_rrset)
  729. # Add two rrs
  730. self.check_full_handle_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  731. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  732. isc.dns.Name("www.example.org"),
  733. RRType.A(),
  734. extended_a_rrset)
  735. # Adding the same RRsets should not make a difference.
  736. self.check_full_handle_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  737. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  738. isc.dns.Name("www.example.org"),
  739. RRType.A(),
  740. extended_a_rrset)
  741. # Now delete those two, and we should end up with the original RRset
  742. self.check_full_handle_result(Rcode.NOERROR(),
  743. [ self.rrset_update_del_rrset_part ])
  744. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  745. isc.dns.Name("www.example.org"),
  746. RRType.A(),
  747. self.orig_a_rrset)
  748. # 'Deleting' them again should make no difference
  749. self.check_full_handle_result(Rcode.NOERROR(),
  750. [ self.rrset_update_del_rrset_part ])
  751. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  752. isc.dns.Name("www.example.org"),
  753. RRType.A(),
  754. self.orig_a_rrset)
  755. # But deleting the entire rrset, independent of its contents, should
  756. # work
  757. self.check_full_handle_result(Rcode.NOERROR(),
  758. [ self.rrset_update_del_rrset ])
  759. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  760. isc.dns.Name("www.example.org"),
  761. RRType.A())
  762. # Check that if we update the SOA, it is updated to our value
  763. self.check_full_handle_result(Rcode.NOERROR(),
  764. [ self.rrset_update_soa2 ])
  765. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  766. isc.dns.Name("example.org"),
  767. RRType.SOA(),
  768. self.rrset_update_soa2)
  769. def test_update_add_new_data(self):
  770. '''
  771. This tests adds data where none is present
  772. '''
  773. # Add data at a completely new name
  774. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  775. isc.dns.Name("new.example.org"),
  776. RRType.A())
  777. rrset = create_rrset("new.example.org", TEST_RRCLASS, RRType.A(),
  778. 3600, [ "192.0.2.1", "192.0.2.2" ])
  779. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  780. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  781. isc.dns.Name("new.example.org"),
  782. RRType.A(),
  783. rrset)
  784. # Also try a name where data is present, but none of this
  785. # specific type
  786. self.check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  787. isc.dns.Name("new.example.org"),
  788. RRType.TXT())
  789. rrset = create_rrset("new.example.org", TEST_RRCLASS, RRType.TXT(),
  790. 3600, [ "foo" ])
  791. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  792. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  793. isc.dns.Name("new.example.org"),
  794. RRType.TXT(),
  795. rrset)
  796. def test_update_delete_name(self):
  797. self.initialize_update_rrsets()
  798. # First check it is there
  799. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  800. isc.dns.Name("www.example.org"),
  801. RRType.A())
  802. # Delete the entire name
  803. self.check_full_handle_result(Rcode.NOERROR(),
  804. [ self.rrset_update_del_name ])
  805. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  806. isc.dns.Name("www.example.org"),
  807. RRType.A())
  808. # Should still be gone after pointless second delete
  809. self.check_full_handle_result(Rcode.NOERROR(),
  810. [ self.rrset_update_del_name ])
  811. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  812. isc.dns.Name("www.example.org"),
  813. RRType.A())
  814. def test_update_apex_special_cases(self):
  815. '''
  816. Tests a few special cases when deleting data from the apex
  817. '''
  818. self.initialize_update_rrsets()
  819. # the original SOA
  820. orig_soa_rrset = create_rrset("example.org", TEST_RRCLASS,
  821. RRType.SOA(), 3600,
  822. [ "ns1.example.org. " +
  823. "admin.example.org. " +
  824. "1234 3600 1800 2419200 7200" ])
  825. # We will delete some of the NS records
  826. orig_ns_rrset = create_rrset("example.org", TEST_RRCLASS,
  827. RRType.NS(), 3600,
  828. [ "ns1.example.org.",
  829. "ns2.example.org.",
  830. "ns3.example.org." ])
  831. # When we are done, we should have a reduced NS rrset
  832. short_ns_rrset = create_rrset("example.org", TEST_RRCLASS,
  833. RRType.NS(), 3600,
  834. [ "ns3.example.org." ])
  835. # Sanity check, make sure original data is really there before updates
  836. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  837. isc.dns.Name("example.org"),
  838. RRType.NS(),
  839. orig_ns_rrset)
  840. # We will delete the MX record later in this test, so let's make
  841. # sure that it exists (we do not care about its value)
  842. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  843. isc.dns.Name("example.org"),
  844. RRType.MX())
  845. # Check that we cannot delete the SOA record by direction deletion
  846. # both by name+type and by full rrset
  847. self.check_full_handle_result(Rcode.NOERROR(),
  848. [ self.rrset_update_del_soa_apex,
  849. self.rrset_update_soa_del ])
  850. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  851. isc.dns.Name("example.org"),
  852. RRType.SOA(),
  853. orig_soa_rrset)
  854. # If we delete everything at the apex, the SOA and NS rrsets should be
  855. # untouched
  856. self.check_full_handle_result(Rcode.NOERROR(),
  857. [ self.rrset_update_del_name_apex ])
  858. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  859. isc.dns.Name("example.org"),
  860. RRType.SOA(),
  861. orig_soa_rrset)
  862. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  863. isc.dns.Name("example.org"),
  864. RRType.NS(),
  865. orig_ns_rrset)
  866. # but the MX should be gone
  867. self.check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  868. isc.dns.Name("example.org"),
  869. RRType.MX())
  870. # Deleting the NS rrset by name and type only, it should also be left
  871. # untouched
  872. self.check_full_handle_result(Rcode.NOERROR(),
  873. [ self.rrset_update_del_ns_apex ])
  874. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  875. isc.dns.Name("example.org"),
  876. RRType.NS(),
  877. orig_ns_rrset)
  878. # If we delete the NS at the apex specifically, it should still
  879. # keep one record
  880. self.check_full_handle_result(Rcode.NOERROR(),
  881. [ self.rrset_update_del_rrset_ns ])
  882. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  883. isc.dns.Name("example.org"),
  884. RRType.NS(),
  885. short_ns_rrset)
  886. def test_update_delete_normal_rrset_at_apex(self):
  887. '''
  888. Tests a number of 'normal rrset' deletes at the apex
  889. '''
  890. # MX should simply be deleted
  891. self.initialize_update_rrsets()
  892. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  893. isc.dns.Name("example.org"),
  894. RRType.MX())
  895. self.check_full_handle_result(Rcode.NOERROR(),
  896. [ self.rrset_update_del_rrset_mx ])
  897. self.check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  898. isc.dns.Name("example.org"),
  899. RRType.MX())
  900. def test_update_cname_special_cases(self):
  901. self.initialize_update_rrsets()
  902. # Sanity check
  903. orig_cname_rrset = create_rrset("cname.example.org", TEST_RRCLASS,
  904. RRType.CNAME(), 3600,
  905. [ "www.example.org." ])
  906. self.check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  907. isc.dns.Name("cname.example.org"),
  908. RRType.A(),
  909. orig_cname_rrset)
  910. # If we try to add data where a cname is preset
  911. rrset = create_rrset("cname.example.org", TEST_RRCLASS, RRType.A(),
  912. 3600, [ "192.0.2.1" ])
  913. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  914. self.check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  915. isc.dns.Name("cname.example.org"),
  916. RRType.A(),
  917. orig_cname_rrset)
  918. # But updating the cname itself should work
  919. new_cname_rrset = create_rrset("cname.example.org", TEST_RRCLASS,
  920. RRType.CNAME(), 3600,
  921. [ "mail.example.org." ])
  922. self.check_full_handle_result(Rcode.NOERROR(), [ new_cname_rrset ])
  923. self.check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  924. isc.dns.Name("cname.example.org"),
  925. RRType.A(),
  926. new_cname_rrset)
  927. self.initialize_update_rrsets()
  928. # Likewise, adding a cname where other data is
  929. # present should do nothing either
  930. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  931. isc.dns.Name("www.example.org"),
  932. RRType.A(),
  933. self.orig_a_rrset)
  934. new_cname_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  935. RRType.CNAME(), 3600,
  936. [ "mail.example.org." ])
  937. self.check_full_handle_result(Rcode.NOERROR(), [ new_cname_rrset ])
  938. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  939. isc.dns.Name("www.example.org"),
  940. RRType.A(),
  941. self.orig_a_rrset)
  942. def test_update_bad_class(self):
  943. rrset = create_rrset("example.org.", RRClass.CH(), RRType.TXT(), 0,
  944. [ "foo" ])
  945. self.check_full_handle_result(Rcode.FORMERR(), [ rrset ])
  946. if __name__ == "__main__":
  947. isc.log.init("bind10")
  948. isc.log.resetUnitTestRootLogger()
  949. unittest.main()