tsig_keys_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (C) 2011 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. # Make sure we can load the module, put it into path
  16. import sys
  17. import os
  18. sys.path.extend(os.environ["B10_TEST_PLUGIN_DIR"].split(':'))
  19. import tsig_keys
  20. import unittest
  21. import isc.config.module_spec
  22. class TSigKeysTest(unittest.TestCase):
  23. def test_load(self):
  24. """
  25. Checks the entry point returns the correct values.
  26. """
  27. (spec, check) = tsig_keys.load()
  28. # It returns the checking function
  29. self.assertEqual(check, tsig_keys.check)
  30. # The plugin stores it's spec
  31. self.assertEqual(spec, tsig_keys.spec)
  32. def test_spec(self):
  33. """
  34. Checks the spec is looking sane (doesn't do really deep check here).
  35. """
  36. spec = tsig_keys.spec
  37. # In python, we don't generally check the type of something, because
  38. # of the duck typing.
  39. # But this is unittest, so we check it does what we intend and
  40. # supplying that's behaving the same but is different is not our
  41. # intention
  42. self.assertTrue(isinstance(spec, isc.config.module_spec.ModuleSpec))
  43. # Correct name
  44. self.assertEqual("tsig_keys", spec.get_module_name())
  45. # There are no commands, nobody would handle them anyway
  46. self.assertEqual([], spec.get_commands_spec())
  47. # There's some nonempty configuration
  48. self.assertNotEqual({}, spec.get_config_spec())
  49. def test_missing_keys(self):
  50. """
  51. Test that missing keys doesn't kill us. There are just no keys there.
  52. """
  53. self.assertEqual(None, tsig_keys.check({}))
  54. def test_data_empty(self):
  55. """Check we accept valid config with empty set of tsig keys."""
  56. self.assertEqual(None, tsig_keys.check({'keys': []}))
  57. def test_keys_valid(self):
  58. """
  59. Check we accept some valid keys (we don't check all the algorithms,
  60. that's the job of isc.dns.TSIGKey).
  61. """
  62. self.assertEqual(None, tsig_keys.check({'keys':
  63. ['testkey:QklORCAxMCBpcyBjb29sCg==',
  64. 'test.key:QklORCAxMCBpcyBjb29sCg==:hmac-sha1']}))
  65. def test_keys_same_name(self):
  66. """
  67. Test we reject when we have multiple keys with the same name.
  68. """
  69. self.assertEqual("Multiple TSIG keys with name 'test.key.'",
  70. tsig_keys.check({'keys':
  71. ['test.key:QklORCAxMCBpcyBjb29sCg==',
  72. 'test.key:b3RoZXIK']}))
  73. def test_invalid_key(self):
  74. """
  75. Test we reject invalid key.
  76. """
  77. self.assertEqual("TSIG: Invalid TSIG key string: invalid.key",
  78. tsig_keys.check({'keys': ['invalid.key']}))
  79. self.assertEqual(
  80. "TSIG: Unexpected end of input in BASE decoder",
  81. tsig_keys.check({'keys': ['invalid.key:123']}))
  82. def test_bad_format(self):
  83. """
  84. Test we fail on bad format. We don't really care much how here, though,
  85. as this should not get in trough config manager anyway.
  86. """
  87. self.assertNotEqual(None, tsig_keys.check({'bad_name': {}}))
  88. self.assertNotEqual(None, tsig_keys.check({'keys': 'not_list'}))
  89. self.assertNotEqual(None, tsig_keys.check({'keys': 42}))
  90. self.assertNotEqual(None, tsig_keys.check({'keys': {}}))
  91. if __name__ == '__main__':
  92. unittest.main()