session_tests.py 58 KB

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