zone_checker_python_test.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (C) 2013 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 unittest
  16. import sys
  17. from pydnspp import *
  18. # A separate exception class raised from some tests to see if it's propagated.
  19. class FakeException(Exception):
  20. pass
  21. class ZoneCheckerTest(unittest.TestCase):
  22. def __callback(self, reason, reasons):
  23. # Issue callback for check_zone(). It simply records the given reason
  24. # string in the given list.
  25. reasons.append(reason)
  26. def test_check(self):
  27. errors = []
  28. warns = []
  29. # A successful case with no warning.
  30. rrsets = RRsetCollection(b'example.org. 0 SOA . . 0 0 0 0 0\n' +
  31. b'example.org. 0 NS ns.example.org.\n' +
  32. b'ns.example.org. 0 A 192.0.2.1\n',
  33. Name('example.org'), RRClass.IN)
  34. self.assertTrue(check_zone(Name('example.org'), RRClass.IN,
  35. rrsets,
  36. (lambda r: self.__callback(r, errors),
  37. lambda r: self.__callback(r, warns))))
  38. self.assertEqual([], errors)
  39. self.assertEqual([], warns)
  40. # Check fails and one additional warning.
  41. rrsets = RRsetCollection(b'example.org. 0 NS ns.example.org.',
  42. Name('example.org'), RRClass.IN)
  43. self.assertFalse(check_zone(Name('example.org'), RRClass.IN, rrsets,
  44. (lambda r: self.__callback(r, errors),
  45. lambda r: self.__callback(r, warns))))
  46. self.assertEqual(['zone example.org/IN: has 0 SOA records'], errors)
  47. self.assertEqual(['zone example.org/IN: NS has no address records ' +
  48. '(A or AAAA)'], warns)
  49. # Same RRset collection, suppressing callbacks
  50. errors = []
  51. warns = []
  52. self.assertFalse(check_zone(Name('example.org'), RRClass.IN, rrsets,
  53. (None, None)))
  54. self.assertEqual([], errors)
  55. self.assertEqual([], warns)
  56. def test_check_badarg(self):
  57. rrsets = RRsetCollection()
  58. # Bad types
  59. self.assertRaises(TypeError, check_zone, 1, RRClass.IN, rrsets,
  60. (None, None))
  61. self.assertRaises(TypeError, check_zone, Name('example'), 1, rrsets,
  62. (None, None))
  63. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  64. 1, (None, None))
  65. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  66. rrsets, 1)
  67. # Bad callbacks
  68. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  69. rrsets, (None, None, None))
  70. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  71. rrsets, (1, None))
  72. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  73. rrsets, (None, 1))
  74. # Extra/missing args
  75. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  76. rrsets, (None, None), 1)
  77. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  78. rrsets)
  79. check_zone(Name('example'), RRClass.IN, rrsets, (None, None))
  80. def test_check_callback_fail(self):
  81. # Let the call raise a Python exception. It should be propagated to
  82. # the top level.
  83. def __bad_callback(reason):
  84. raise FakeException('error in callback')
  85. # Using an empty collection, triggering an error callback.
  86. self.assertRaises(FakeException, check_zone, Name('example.org'),
  87. RRClass.IN, RRsetCollection(),
  88. (__bad_callback, None))
  89. # An unusual case: the callback is expected to return None, but if it
  90. # returns an actual object it shouldn't cause leak inside the callback.
  91. class RefChecker:
  92. pass
  93. def __callback(reason, checker):
  94. return checker
  95. ref_checker = RefChecker()
  96. orig_refcnt = sys.getrefcount(ref_checker)
  97. check_zone(Name('example.org'), RRClass.IN, RRsetCollection(),
  98. (lambda r: __callback(r, ref_checker), None))
  99. self.assertEqual(orig_refcnt, sys.getrefcount(ref_checker))
  100. def test_check_custom_collection(self):
  101. # Test if check_zone() works with pure-Python RRsetCollection.
  102. class FakeRRsetCollection(RRsetCollectionBase):
  103. # This is the Python-only collection class. Its find() makes
  104. # the check pass by default, by returning hardcoded RRsets.
  105. # If raise_on_find is set to True, find() raises an exception.
  106. # If find_result is set to something other than 'use_default'
  107. # (as a string), find() returns that specified value (note that
  108. # it can be None).
  109. def __init__(self, raise_on_find=False, find_result='use_default'):
  110. self.__raise_on_find = raise_on_find
  111. self.__find_result = find_result
  112. def find(self, name, rrclass, rrtype):
  113. if self.__raise_on_find:
  114. raise FakeException('find error')
  115. if self.__find_result is not 'use_default':
  116. return self.__find_result
  117. if rrtype == RRType.SOA:
  118. soa = RRset(Name('example'), RRClass.IN, rrtype, RRTTL(0))
  119. soa.add_rdata(Rdata(RRType.SOA, RRClass.IN,
  120. '. . 0 0 0 0 0'))
  121. return soa
  122. if rrtype == RRType.NS:
  123. ns = RRset(Name('example'), RRClass.IN, rrtype, RRTTL(0))
  124. ns.add_rdata(Rdata(RRType.NS, RRClass.IN, 'example.org.'))
  125. return ns
  126. return None
  127. # A successful case. Just checking it works in that case.
  128. rrsets = FakeRRsetCollection()
  129. self.assertTrue(check_zone(Name('example'), RRClass.IN, rrsets,
  130. (None, None)))
  131. # Likewise, normal case but zone check fails.
  132. rrsets = FakeRRsetCollection(False, None)
  133. self.assertFalse(check_zone(Name('example'), RRClass.IN, rrsets,
  134. (None, None)))
  135. # Our find() returns a bad type of result.
  136. rrsets = FakeRRsetCollection(False, 1)
  137. self.assertRaises(TypeError, check_zone, Name('example'), RRClass.IN,
  138. rrsets, (None, None))
  139. # Our find() returns an empty SOA RRset. C++ zone checker code
  140. # throws, which results in IscException.
  141. rrsets = FakeRRsetCollection(False, RRset(Name('example'),
  142. RRClass.IN,
  143. RRType.SOA, RRTTL(0)))
  144. self.assertRaises(IscException, check_zone, Name('example'),
  145. RRClass.IN, rrsets, (None, None))
  146. # Our find() raises an exception. That exception is propagated to
  147. # the top level.
  148. rrsets = FakeRRsetCollection(True)
  149. self.assertRaises(FakeException, check_zone, Name('example'),
  150. RRClass.IN, rrsets, (None, None))
  151. if __name__ == '__main__':
  152. unittest.main()