nsec3hash_python_test.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 unittest
  16. from pydnspp import *
  17. class NSEC3HashTest(unittest.TestCase):
  18. '''These tests are mostly straightforward conversion of C++ tests
  19. except for python specific type checks.
  20. '''
  21. def setUp(self):
  22. self.nsec3_common = "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG"
  23. self.test_hash = NSEC3Hash(Rdata(RRType.NSEC3PARAM(), RRClass.IN(),
  24. "1 0 12 aabbccdd"))
  25. self.test_hash_nsec3 = NSEC3Hash(Rdata(RRType.NSEC3(), RRClass.IN(),
  26. "1 0 12 aabbccdd " +
  27. self.nsec3_common))
  28. def test_bad_construct(self):
  29. # missing parameter
  30. self.assertRaises(TypeError, NSEC3Hash)
  31. # invalid type of argument
  32. self.assertRaises(TypeError, NSEC3Hash, "1 0 12 aabbccdd")
  33. # additional parameter
  34. self.assertRaises(TypeError, NSEC3Hash, Rdata(RRType.NSEC3PARAM(),
  35. RRClass.IN(),
  36. "1 0 12 aabbccdd"), 1)
  37. # Invaid type of RDATA
  38. self.assertRaises(TypeError, NSEC3Hash, Rdata(RRType.A(), RRClass.IN(),
  39. "192.0.2.1"))
  40. def test_unknown_algorithm(self):
  41. self.assertRaises(UnknownNSEC3HashAlgorithm, NSEC3Hash,
  42. Rdata(RRType.NSEC3PARAM(), RRClass.IN(),
  43. "2 0 12 aabbccdd"))
  44. self.assertRaises(UnknownNSEC3HashAlgorithm, NSEC3Hash,
  45. Rdata(RRType.NSEC3(), RRClass.IN(),
  46. "2 0 12 aabbccdd " + self.nsec3_common))
  47. def calculate_check(self, hash):
  48. # A couple of normal cases from the RFC5155 example.
  49. self.assertEqual("0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  50. hash.calculate(Name("example")))
  51. self.assertEqual("35MTHGPGCU1QG68FAB165KLNSNK3DPVL",
  52. hash.calculate(Name("a.example")))
  53. # Check case-insensitiveness
  54. self.assertEqual("0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  55. hash.calculate(Name("EXAMPLE")))
  56. def test_calculate(self):
  57. self.calculate_check(self.test_hash)
  58. self.calculate_check(self.test_hash_nsec3)
  59. # Using unusually large iterations, something larger than the 8-bit
  60. #range. (expected hash value generated by BIND 9's dnssec-signzone)
  61. self.test_hash = NSEC3Hash(Rdata(RRType.NSEC3PARAM(),
  62. RRClass.IN(), "1 0 256 AABBCCDD"))
  63. self.assertEqual("COG6A52MJ96MNMV3QUCAGGCO0RHCC2Q3",
  64. self.test_hash.calculate(Name("example.org")))
  65. # Some boundary cases: 0-iteration and empty salt. Borrowed from the
  66. # .com zone data.
  67. self.test_hash = NSEC3Hash(Rdata(RRType.NSEC3PARAM(),
  68. RRClass.IN(),"1 0 0 -"))
  69. self.assertEqual("CK0POJMG874LJREF7EFN8430QVIT8BSM",
  70. self.test_hash.calculate(Name("com")))
  71. def test_calculate_badparam(self):
  72. self.assertRaises(TypeError, self.test_hash.calculate, "example")
  73. self.assertRaises(TypeError, self.test_hash.calculate)
  74. self.assertRaises(TypeError, self.test_hash.calculate, Name("."), 1)
  75. def check_match(self, hash, rrtype, postfix):
  76. # If all parameters match, it's considered to be matched.
  77. self.assertTrue(hash.match(Rdata(rrtype, RRClass.IN(),
  78. "1 0 12 aabbccdd" + postfix)))
  79. # Algorithm doesn't match
  80. self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(),
  81. "2 0 12 aabbccdd" + postfix)))
  82. # Iterations doesn't match
  83. self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(),
  84. "1 0 1 aabbccdd" + postfix)))
  85. # Salt doesn't match
  86. self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(),
  87. "1 0 12 aabbccde" + postfix)))
  88. # Salt doesn't match: the other has an empty salt
  89. self.assertFalse(hash.match(Rdata(rrtype, RRClass.IN(),
  90. "1 0 12 -" + postfix)))
  91. # Flag doesn't matter
  92. self.assertTrue(hash.match(Rdata(rrtype, RRClass.IN(),
  93. "1 1 12 aabbccdd" + postfix)))
  94. def test_match(self):
  95. self.check_match(self.test_hash, RRType.NSEC3(),
  96. " " + self.nsec3_common)
  97. self.check_match(self.test_hash_nsec3, RRType.NSEC3(),
  98. " " + self.nsec3_common)
  99. self.check_match(self.test_hash, RRType.NSEC3PARAM(), "")
  100. self.check_match(self.test_hash_nsec3, RRType.NSEC3PARAM(), "")
  101. # bad parameter checks
  102. self.assertRaises(TypeError, self.test_hash.match, 1)
  103. self.assertRaises(TypeError, self.test_hash.match,
  104. Rdata(RRType.NSEC3(), RRClass.IN(),
  105. "1 0 12 aabbccdd " + self.nsec3_common), 1)
  106. self.assertRaises(TypeError, self.test_hash.match,
  107. Rdata(RRType.A(), RRClass.IN(), "192.0.2.1"))
  108. if __name__ == '__main__':
  109. unittest.main()