session_tests.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. def check_prescan_result(self, expected, updates, expected_soa = None):
  373. '''Helper method for checking the result of a prerequisite check;
  374. creates an update session, and fills it with the list of rrsets
  375. from 'updates'. Then checks if __do_prescan()
  376. returns the Rcode specified in 'expected'.'''
  377. msg_data, msg = create_update_msg([TEST_ZONE_RECORD],
  378. [], updates)
  379. zconfig = ZoneConfig([], TEST_RRCLASS, self.__datasrc_client)
  380. session = UpdateSession(msg, msg_data, TEST_CLIENT4, zconfig)
  381. session._UpdateSession__get_update_zone()
  382. # compare the to_text output of the rcodes (nicer error messages)
  383. # This call itself should also be done by handle(),
  384. # but just for better failures, it is first called on its own
  385. self.assertEqual(expected.to_text(),
  386. session._UpdateSession__do_prescan().to_text())
  387. # If there is an expected soa, check it
  388. self.assertEqual(str(expected_soa),
  389. str(session._UpdateSession__added_soa))
  390. def check_full_handle_result(self, expected, updates):
  391. '''Helper method for checking the result of a full handle;
  392. creates an update session, and fills it with the list of rrsets
  393. from 'updates'. Then checks if __handle()
  394. results in a response with rcode 'expected'.'''
  395. msg_data, msg = create_update_msg([TEST_ZONE_RECORD],
  396. [], updates)
  397. zconfig = ZoneConfig([], TEST_RRCLASS, self.__datasrc_client)
  398. session = UpdateSession(msg, msg_data, TEST_CLIENT4, zconfig)
  399. # Now see if handle finds the same result
  400. (result, _, _) = session.handle()
  401. self.assertEqual(expected.to_text(),
  402. session._UpdateSession__message.get_rcode().to_text())
  403. # And that the result looks right
  404. if expected == Rcode.NOERROR():
  405. self.assertEqual(UPDATE_SUCCESS, result)
  406. else:
  407. self.assertEqual(UPDATE_ERROR, result)
  408. def test_check_prerequisites(self):
  409. # This test checks if the actual prerequisite-type-specific
  410. # methods are called.
  411. # It does test all types of prerequisites, but it does not test
  412. # every possible result for those types (those are tested above,
  413. # in the specific prerequisite type tests)
  414. # Let's first define a number of prereq's that should succeed
  415. rrset_exists_yes = create_rrset("example.org", RRClass.ANY(),
  416. RRType.SOA(), 0)
  417. rrset_exists_value_yes = create_rrset("www.example.org", RRClass.IN(),
  418. RRType.A(), 0, [ "192.0.2.1" ])
  419. rrset_does_not_exist_yes = create_rrset("foo.example.org",
  420. RRClass.NONE(), RRType.SOA(),
  421. 0)
  422. name_in_use_yes = create_rrset("www.example.org", RRClass.ANY(),
  423. RRType.ANY(), 0)
  424. name_not_in_use_yes = create_rrset("foo.example.org", RRClass.NONE(),
  425. RRType.ANY(), 0)
  426. rrset_exists_value_1 = create_rrset("example.org", RRClass.IN(),
  427. RRType.NS(), 0,
  428. [ "ns1.example.org" ])
  429. rrset_exists_value_2 = create_rrset("example.org", RRClass.IN(),
  430. RRType.NS(), 0,
  431. [ "ns2.example.org" ])
  432. rrset_exists_value_3 = create_rrset("example.org", RRClass.IN(),
  433. RRType.NS(), 0,
  434. [ "ns3.example.org" ])
  435. # and a number that should not
  436. rrset_exists_no = create_rrset("foo.example.org", RRClass.ANY(),
  437. RRType.SOA(), 0)
  438. rrset_exists_value_no = create_rrset("www.example.org", RRClass.IN(),
  439. RRType.A(), 0, [ "192.0.2.2" ])
  440. rrset_does_not_exist_no = create_rrset("example.org", RRClass.NONE(),
  441. RRType.SOA(), 0)
  442. name_in_use_no = create_rrset("foo.example.org", RRClass.ANY(),
  443. RRType.ANY(), 0)
  444. name_not_in_use_no = create_rrset("www.example.org", RRClass.NONE(),
  445. RRType.ANY(), 0)
  446. # Create an UPDATE with all 5 'yes' prereqs
  447. data, update = create_update_msg([TEST_ZONE_RECORD],
  448. [
  449. rrset_exists_yes,
  450. rrset_does_not_exist_yes,
  451. name_in_use_yes,
  452. name_not_in_use_yes,
  453. rrset_exists_value_yes,
  454. ])
  455. # check 'no' result codes
  456. self.check_prerequisite_result(Rcode.NXRRSET(),
  457. [ rrset_exists_no ])
  458. self.check_prerequisite_result(Rcode.NXRRSET(),
  459. [ rrset_exists_value_no ])
  460. self.check_prerequisite_result(Rcode.YXRRSET(),
  461. [ rrset_does_not_exist_no ])
  462. self.check_prerequisite_result(Rcode.NXDOMAIN(),
  463. [ name_in_use_no ])
  464. self.check_prerequisite_result(Rcode.YXDOMAIN(),
  465. [ name_not_in_use_no ])
  466. # the 'yes' codes should result in ok
  467. self.check_prerequisite_result(Rcode.NOERROR(),
  468. [ rrset_exists_yes,
  469. rrset_exists_value_yes,
  470. rrset_does_not_exist_yes,
  471. name_in_use_yes,
  472. name_not_in_use_yes,
  473. rrset_exists_value_1,
  474. rrset_exists_value_2,
  475. rrset_exists_value_3])
  476. # try out a permutation, note that one rrset is split up,
  477. # and the order of the RRs should not matter
  478. self.check_prerequisite_result(Rcode.NOERROR(),
  479. [ rrset_exists_value_3,
  480. rrset_exists_yes,
  481. rrset_exists_value_2,
  482. name_in_use_yes,
  483. rrset_exists_value_1])
  484. # Should fail on the first error, even if most of the
  485. # prerequisites are ok
  486. self.check_prerequisite_result(Rcode.NXDOMAIN(),
  487. [ rrset_exists_value_3,
  488. rrset_exists_yes,
  489. rrset_exists_value_2,
  490. name_in_use_yes,
  491. name_in_use_no,
  492. rrset_exists_value_1])
  493. def test_prerequisite_notzone(self):
  494. rrset = create_rrset("some.other.zone.", RRClass.ANY(), RRType.SOA(), 0)
  495. self.check_prerequisite_result(Rcode.NOTZONE(), [ rrset ])
  496. def test_prerequisites_formerr(self):
  497. # test for form errors in the prerequisite section
  498. # Class ANY, non-zero TTL
  499. rrset = create_rrset("example.org", RRClass.ANY(), RRType.SOA(), 1)
  500. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  501. # Class ANY, but with rdata
  502. rrset = create_rrset("example.org", RRClass.ANY(), RRType.A(), 0,
  503. [ b'\x00\x00\x00\x00' ])
  504. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  505. # Class NONE, non-zero TTL
  506. rrset = create_rrset("example.org", RRClass.NONE(), RRType.SOA(), 1)
  507. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  508. # Class NONE, but with rdata
  509. rrset = create_rrset("example.org", RRClass.NONE(), RRType.A(), 0,
  510. [ b'\x00\x00\x00\x00' ])
  511. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  512. # Matching class and type, but non-zero TTL
  513. rrset = create_rrset("www.example.org", RRClass.IN(), RRType.A(), 1,
  514. [ "192.0.2.1" ])
  515. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  516. # Completely different class
  517. rrset = create_rrset("example.org", RRClass.CH(), RRType.TXT(), 0,
  518. [ "foo" ])
  519. self.check_prerequisite_result(Rcode.FORMERR(), [ rrset ])
  520. def __prereq_helper(self, method, expected, rrset):
  521. '''Calls the given method with self.__datasrc_client
  522. and the given rrset, and compares the return value.
  523. Function does not do much but makes the code look nicer'''
  524. self.assertEqual(expected, method(rrset))
  525. def initialize_update_rrsets(self):
  526. '''Prepare a number of RRsets to be used in several update tests
  527. The rrsets are stored in self'''
  528. orig_a_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  529. RRType.A(), 3600, [ "192.0.2.1" ])
  530. self.orig_a_rrset = orig_a_rrset
  531. rrset_update_a = create_rrset("www.example.org", TEST_RRCLASS,
  532. RRType.A(), 3600,
  533. [ "192.0.2.2", "192.0.2.3" ])
  534. self.rrset_update_a = rrset_update_a
  535. rrset_update_soa = create_rrset("example.org", TEST_RRCLASS,
  536. RRType.SOA(), 3600,
  537. [ "ns1.example.org. " +
  538. "admin.example.org. " +
  539. "1233 3600 1800 2419200 7200" ])
  540. self.rrset_update_soa = rrset_update_soa
  541. rrset_update_soa_del = create_rrset("example.org", RRClass.NONE(),
  542. RRType.SOA(), 0,
  543. [ "ns1.example.org. " +
  544. "admin.example.org. " +
  545. "1233 3600 1800 2419200 7200" ])
  546. self.rrset_update_soa_del = rrset_update_soa_del
  547. rrset_update_soa2 = create_rrset("example.org", TEST_RRCLASS,
  548. RRType.SOA(), 3600,
  549. [ "ns1.example.org. " +
  550. "admin.example.org. " +
  551. "4000 3600 1800 2419200 7200" ])
  552. self.rrset_update_soa2 = rrset_update_soa2
  553. rrset_update_del_name = create_rrset("www.example.org", RRClass.ANY(),
  554. RRType.ANY(), 0)
  555. self.rrset_update_del_name = rrset_update_del_name
  556. rrset_update_del_name_apex = create_rrset("example.org", RRClass.ANY(),
  557. RRType.ANY(), 0)
  558. self.rrset_update_del_name_apex = rrset_update_del_name_apex
  559. rrset_update_del_rrset = create_rrset("www.example.org", RRClass.ANY(),
  560. RRType.A(), 0)
  561. self.rrset_update_del_rrset = rrset_update_del_rrset
  562. rrset_update_del_mx_apex = create_rrset("example.org", RRClass.ANY(),
  563. RRType.MX(), 0)
  564. self.rrset_update_del_mx_apex = rrset_update_del_mx_apex
  565. rrset_update_del_soa_apex = create_rrset("example.org", RRClass.ANY(),
  566. RRType.SOA(), 0)
  567. self.rrset_update_del_soa_apex = rrset_update_del_soa_apex
  568. rrset_update_del_ns_apex = create_rrset("example.org", RRClass.ANY(),
  569. RRType.NS(), 0)
  570. self.rrset_update_del_ns_apex = rrset_update_del_ns_apex
  571. rrset_update_del_rrset_part = create_rrset("www.example.org",
  572. RRClass.NONE(), RRType.A(),
  573. 0,
  574. [ b'\xc0\x00\x02\x02',
  575. b'\xc0\x00\x02\x03' ])
  576. self.rrset_update_del_rrset_part = rrset_update_del_rrset_part
  577. rrset_update_del_rrset_ns = create_rrset("example.org", RRClass.NONE(),
  578. RRType.NS(), 0,
  579. [ b'\x03ns1\x07example\x03org\x00',
  580. b'\x03ns2\x07example\x03org\x00',
  581. b'\x03ns3\x07example\x03org\x00' ])
  582. self.rrset_update_del_rrset_ns = rrset_update_del_rrset_ns
  583. rrset_update_del_rrset_mx = create_rrset("example.org", RRClass.NONE(),
  584. RRType.MX(), 0,
  585. [ b'\x00\x0a\x04mail\x07example\x03org\x00' ])
  586. self.rrset_update_del_rrset_mx = rrset_update_del_rrset_mx
  587. def test_prescan(self):
  588. '''Test whether the prescan succeeds on data that is ok, and whether
  589. if notices the SOA if present'''
  590. # prepare a set of correct update statements
  591. self.initialize_update_rrsets()
  592. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  593. # check if soa is noticed
  594. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_soa ],
  595. self.rrset_update_soa)
  596. # Other types of succesful prechecks
  597. self.check_prescan_result(Rcode.NOERROR(), [ self.rrset_update_soa2 ],
  598. self.rrset_update_soa2)
  599. self.check_prescan_result(Rcode.NOERROR(),
  600. [ self.rrset_update_del_name ])
  601. self.check_prescan_result(Rcode.NOERROR(),
  602. [ self.rrset_update_del_name_apex ])
  603. self.check_prescan_result(Rcode.NOERROR(),
  604. [ self.rrset_update_del_rrset ])
  605. self.check_prescan_result(Rcode.NOERROR(),
  606. [ self.rrset_update_del_mx_apex ])
  607. self.check_prescan_result(Rcode.NOERROR(),
  608. [ self.rrset_update_del_rrset_part ])
  609. # and check a few permutations of the above
  610. # all of them (with one of the soas)
  611. self.check_prescan_result(Rcode.NOERROR(),
  612. [
  613. self.rrset_update_a,
  614. self.rrset_update_soa,
  615. self.rrset_update_del_name,
  616. self.rrset_update_del_name_apex,
  617. self.rrset_update_del_rrset,
  618. self.rrset_update_del_mx_apex,
  619. self.rrset_update_del_rrset_part
  620. ],
  621. self.rrset_update_soa)
  622. # Two soas. Should we reject or simply use the last?
  623. # (RFC is not really explicit on this, but between the lines I read
  624. # use the last)
  625. self.check_prescan_result(Rcode.NOERROR(),
  626. [ self.rrset_update_soa,
  627. self.rrset_update_soa2 ],
  628. self.rrset_update_soa2)
  629. self.check_prescan_result(Rcode.NOERROR(),
  630. [ self.rrset_update_soa2,
  631. self.rrset_update_soa ],
  632. self.rrset_update_soa)
  633. self.check_prescan_result(Rcode.NOERROR(),
  634. [
  635. self.rrset_update_del_mx_apex,
  636. self.rrset_update_del_name,
  637. self.rrset_update_del_name_apex,
  638. self.rrset_update_del_rrset_part,
  639. self.rrset_update_a,
  640. self.rrset_update_del_rrset,
  641. self.rrset_update_soa
  642. ],
  643. self.rrset_update_soa)
  644. def test_prescan_failures(self):
  645. '''Test whether prescan fails on bad data'''
  646. # out of zone data
  647. rrset = create_rrset("different.zone", RRClass.ANY(), RRType.TXT(), 0)
  648. self.check_prescan_result(Rcode.NOTZONE(), [ rrset ])
  649. # forbidden type, zone class
  650. rrset = create_rrset(TEST_ZONE_NAME, TEST_RRCLASS, RRType.ANY(), 0,
  651. [ b'\x00' ])
  652. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  653. # non-zero TTL, class ANY
  654. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.TXT(), 1)
  655. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  656. # non-zero Rdata, class ANY
  657. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.TXT(), 0,
  658. [ "foo" ])
  659. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  660. # forbidden type, class ANY
  661. rrset = create_rrset(TEST_ZONE_NAME, RRClass.ANY(), RRType.AXFR(), 0,
  662. [ b'\x00' ])
  663. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  664. # non-zero TTL, class NONE
  665. rrset = create_rrset(TEST_ZONE_NAME, RRClass.NONE(), RRType.TXT(), 1)
  666. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  667. # forbidden type, class NONE
  668. rrset = create_rrset(TEST_ZONE_NAME, RRClass.NONE(), RRType.AXFR(), 0,
  669. [ b'\x00' ])
  670. self.check_prescan_result(Rcode.FORMERR(), [ rrset ])
  671. def check_inzone_data(self, expected_result, name, rrtype,
  672. expected_rrset = None):
  673. '''Does a find on TEST_ZONE for the given rrset's name and type,
  674. then checks if the result matches the expected result.
  675. If so, and if expected_rrset is given, they are compared as
  676. well.'''
  677. _, finder = self.__datasrc_client.find_zone(TEST_ZONE_NAME)
  678. result, found_rrset, _ = finder.find(name, rrtype,
  679. finder.NO_WILDCARD |
  680. finder.FIND_GLUE_OK)
  681. self.assertEqual(expected_result, result)
  682. # Sigh. Need rrsets.compare() again.
  683. # To be sure, compare name, class, type, and ttl
  684. if expected_rrset is not None:
  685. self.assertEqual(expected_rrset.get_name(), found_rrset.get_name())
  686. self.assertEqual(expected_rrset.get_class(), found_rrset.get_class())
  687. self.assertEqual(expected_rrset.get_type(), found_rrset.get_type())
  688. self.assertEqual(expected_rrset.get_ttl().to_text(),
  689. found_rrset.get_ttl().to_text())
  690. expected_rdata =\
  691. [ rdata.to_text() for rdata in expected_rrset.get_rdata() ]
  692. found_rdata =\
  693. [ rdata.to_text() for rdata in found_rrset.get_rdata() ]
  694. expected_rdata.sort()
  695. found_rdata.sort()
  696. self.assertEqual(expected_rdata, found_rdata)
  697. def test_update_add_delete_rrset(self):
  698. '''
  699. Tests a sequence of related add and delete updates. Some other
  700. cases are tested by later tests.
  701. '''
  702. self.initialize_update_rrsets()
  703. # initially, the www should only contain one rr
  704. # (set to self.orig_a_rrset)
  705. # during this test, we will extend it at some point
  706. extended_a_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  707. RRType.A(), 3600,
  708. [ "192.0.2.1",
  709. "192.0.2.2",
  710. "192.0.2.3" ])
  711. # Sanity check, make sure original data is really there before updates
  712. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  713. isc.dns.Name("www.example.org"),
  714. RRType.A(),
  715. self.orig_a_rrset)
  716. # Add two rrs
  717. self.check_full_handle_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  718. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  719. isc.dns.Name("www.example.org"),
  720. RRType.A(),
  721. extended_a_rrset)
  722. # Adding the same RRsets should not make a difference.
  723. self.check_full_handle_result(Rcode.NOERROR(), [ self.rrset_update_a ])
  724. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  725. isc.dns.Name("www.example.org"),
  726. RRType.A(),
  727. extended_a_rrset)
  728. # Now delete those two, and we should end up with the original RRset
  729. self.check_full_handle_result(Rcode.NOERROR(),
  730. [ self.rrset_update_del_rrset_part ])
  731. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  732. isc.dns.Name("www.example.org"),
  733. RRType.A(),
  734. self.orig_a_rrset)
  735. # 'Deleting' them again should make no difference
  736. self.check_full_handle_result(Rcode.NOERROR(),
  737. [ self.rrset_update_del_rrset_part ])
  738. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  739. isc.dns.Name("www.example.org"),
  740. RRType.A(),
  741. self.orig_a_rrset)
  742. # But deleting the entire rrset, independent of its contents, should
  743. # work
  744. self.check_full_handle_result(Rcode.NOERROR(),
  745. [ self.rrset_update_del_rrset ])
  746. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  747. isc.dns.Name("www.example.org"),
  748. RRType.A())
  749. # Check that if we update the SOA, it is updated to our value
  750. self.check_full_handle_result(Rcode.NOERROR(),
  751. [ self.rrset_update_soa2 ])
  752. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  753. isc.dns.Name("example.org"),
  754. RRType.SOA(),
  755. self.rrset_update_soa2)
  756. def test_update_add_new_data(self):
  757. '''
  758. This tests adds data where none is present
  759. '''
  760. # Add data at a completely new name
  761. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  762. isc.dns.Name("new.example.org"),
  763. RRType.A())
  764. rrset = create_rrset("new.example.org", TEST_RRCLASS, RRType.A(),
  765. 3600, [ "192.0.2.1", "192.0.2.2" ])
  766. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  767. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  768. isc.dns.Name("new.example.org"),
  769. RRType.A(),
  770. rrset)
  771. # Also try a name where data is present, but none of this
  772. # specific type
  773. self.check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  774. isc.dns.Name("new.example.org"),
  775. RRType.TXT())
  776. rrset = create_rrset("new.example.org", TEST_RRCLASS, RRType.TXT(),
  777. 3600, [ "foo" ])
  778. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  779. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  780. isc.dns.Name("new.example.org"),
  781. RRType.TXT(),
  782. rrset)
  783. def test_update_delete_name(self):
  784. self.initialize_update_rrsets()
  785. # First check it is there
  786. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  787. isc.dns.Name("www.example.org"),
  788. RRType.A())
  789. # Delete the entire name
  790. self.check_full_handle_result(Rcode.NOERROR(),
  791. [ self.rrset_update_del_name ])
  792. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  793. isc.dns.Name("www.example.org"),
  794. RRType.A())
  795. # Should still be gone after pointless second delete
  796. self.check_full_handle_result(Rcode.NOERROR(),
  797. [ self.rrset_update_del_name ])
  798. self.check_inzone_data(isc.datasrc.ZoneFinder.NXDOMAIN,
  799. isc.dns.Name("www.example.org"),
  800. RRType.A())
  801. def test_update_apex_special_cases(self):
  802. '''
  803. Tests a few special cases when deleting data from the apex
  804. '''
  805. self.initialize_update_rrsets()
  806. # the original SOA
  807. orig_soa_rrset = create_rrset("example.org", TEST_RRCLASS,
  808. RRType.SOA(), 3600,
  809. [ "ns1.example.org. " +
  810. "admin.example.org. " +
  811. "1234 3600 1800 2419200 7200" ])
  812. # We will delete some of the NS records
  813. orig_ns_rrset = create_rrset("example.org", TEST_RRCLASS,
  814. RRType.NS(), 3600,
  815. [ "ns1.example.org.",
  816. "ns2.example.org.",
  817. "ns3.example.org." ])
  818. # When we are done, we should have a reduced NS rrset
  819. short_ns_rrset = create_rrset("example.org", TEST_RRCLASS,
  820. RRType.NS(), 3600,
  821. [ "ns3.example.org." ])
  822. # Sanity check, make sure original data is really there before updates
  823. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  824. isc.dns.Name("example.org"),
  825. RRType.NS(),
  826. orig_ns_rrset)
  827. # We will delete the MX record later in this test, so let's make
  828. # sure that it exists (we do not care about its value)
  829. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  830. isc.dns.Name("example.org"),
  831. RRType.MX())
  832. # Check that we cannot delete the SOA record by direction deletion
  833. # both by name+type and by full rrset
  834. self.check_full_handle_result(Rcode.NOERROR(),
  835. [ self.rrset_update_del_soa_apex,
  836. self.rrset_update_soa_del ])
  837. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  838. isc.dns.Name("example.org"),
  839. RRType.SOA(),
  840. orig_soa_rrset)
  841. # If we delete everything at the apex, the SOA and NS rrsets should be
  842. # untouched
  843. self.check_full_handle_result(Rcode.NOERROR(),
  844. [ self.rrset_update_del_name_apex ])
  845. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  846. isc.dns.Name("example.org"),
  847. RRType.SOA(),
  848. orig_soa_rrset)
  849. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  850. isc.dns.Name("example.org"),
  851. RRType.NS(),
  852. orig_ns_rrset)
  853. # but the MX should be gone
  854. self.check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  855. isc.dns.Name("example.org"),
  856. RRType.MX())
  857. # Deleting the NS rrset by name and type only, it should also be left
  858. # untouched
  859. self.check_full_handle_result(Rcode.NOERROR(),
  860. [ self.rrset_update_del_ns_apex ])
  861. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  862. isc.dns.Name("example.org"),
  863. RRType.NS(),
  864. orig_ns_rrset)
  865. # If we delete the NS at the apex specifically, it should still
  866. # keep one record
  867. self.check_full_handle_result(Rcode.NOERROR(),
  868. [ self.rrset_update_del_rrset_ns ])
  869. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  870. isc.dns.Name("example.org"),
  871. RRType.NS(),
  872. short_ns_rrset)
  873. def test_update_delete_normal_rrset_at_apex(self):
  874. '''
  875. Tests a number of 'normal rrset' deletes at the apex
  876. '''
  877. # MX should simply be deleted
  878. self.initialize_update_rrsets()
  879. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  880. isc.dns.Name("example.org"),
  881. RRType.MX())
  882. self.check_full_handle_result(Rcode.NOERROR(),
  883. [ self.rrset_update_del_rrset_mx ])
  884. self.check_inzone_data(isc.datasrc.ZoneFinder.NXRRSET,
  885. isc.dns.Name("example.org"),
  886. RRType.MX())
  887. def test_update_cname_special_cases(self):
  888. self.initialize_update_rrsets()
  889. # Sanity check
  890. orig_cname_rrset = create_rrset("cname.example.org", TEST_RRCLASS,
  891. RRType.CNAME(), 3600,
  892. [ "www.example.org." ])
  893. self.check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  894. isc.dns.Name("cname.example.org"),
  895. RRType.A(),
  896. orig_cname_rrset)
  897. # If we try to add data where a cname is preset
  898. rrset = create_rrset("cname.example.org", TEST_RRCLASS, RRType.A(),
  899. 3600, [ "192.0.2.1" ])
  900. self.check_full_handle_result(Rcode.NOERROR(), [ rrset ])
  901. self.check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  902. isc.dns.Name("cname.example.org"),
  903. RRType.A(),
  904. orig_cname_rrset)
  905. # But updating the cname itself should work
  906. new_cname_rrset = create_rrset("cname.example.org", TEST_RRCLASS,
  907. RRType.CNAME(), 3600,
  908. [ "mail.example.org." ])
  909. self.check_full_handle_result(Rcode.NOERROR(), [ new_cname_rrset ])
  910. self.check_inzone_data(isc.datasrc.ZoneFinder.CNAME,
  911. isc.dns.Name("cname.example.org"),
  912. RRType.A(),
  913. new_cname_rrset)
  914. self.initialize_update_rrsets()
  915. # Likewise, adding a cname where other data is
  916. # present should do nothing either
  917. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  918. isc.dns.Name("www.example.org"),
  919. RRType.A(),
  920. self.orig_a_rrset)
  921. new_cname_rrset = create_rrset("www.example.org", TEST_RRCLASS,
  922. RRType.CNAME(), 3600,
  923. [ "mail.example.org." ])
  924. self.check_full_handle_result(Rcode.NOERROR(), [ new_cname_rrset ])
  925. self.check_inzone_data(isc.datasrc.ZoneFinder.SUCCESS,
  926. isc.dns.Name("www.example.org"),
  927. RRType.A(),
  928. self.orig_a_rrset)
  929. def test_update_bad_class(self):
  930. rrset = create_rrset("example.org.", RRClass.CH(), RRType.TXT(), 0,
  931. [ "foo" ])
  932. self.check_full_handle_result(Rcode.FORMERR(), [ rrset ])
  933. def test_uncaught_exception(self):
  934. def my_exc():
  935. raise Exception("foo")
  936. self.__session._UpdateSession__update_soa = my_exc
  937. self.assertEqual(Rcode.SERVFAIL().to_text(),
  938. self.__session._UpdateSession__do_update().to_text())
  939. if __name__ == "__main__":
  940. isc.log.init("bind10")
  941. isc.log.resetUnitTestRootLogger()
  942. unittest.main()