tsigctx_mock.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. from pydnspp import *
  16. class MockTSIGContext(TSIGContext):
  17. """Tthis is a mock of TSIGContext class for testing.
  18. Via its "error" attribute, you can fake the result of verify(), thereby
  19. you can test many of TSIG related tests without requiring actual crypto
  20. setups. "error" should be either a TSIGError type value or a callable
  21. object (typically a function). In the latter case, the callable object
  22. will take the context as a parameter, and is expected to return a
  23. TSIGError object.
  24. """
  25. def __init__(self, tsig_key):
  26. super().__init__(tsig_key)
  27. self.error = None
  28. self.verify_called = 0 # number of verify() called
  29. def sign(self, qid, data):
  30. """Transparently delegate the processing to the super class.
  31. It doesn't matter much anyway because normal applications that would
  32. be implemented in Python normally won't call TSIGContext.sign()
  33. directly.
  34. """
  35. return super().sign(qid, data)
  36. def verify(self, tsig_record, data):
  37. self.verify_called += 1
  38. # call real "verify" so that we can notice any misue (which would
  39. # result in exception.
  40. super().verify(tsig_record, data)
  41. return self.get_error()
  42. def get_error(self):
  43. if self.error is None:
  44. return super().get_error()
  45. if hasattr(self.error, '__call__'):
  46. return self.error(self)
  47. return self.error
  48. def last_had_signature(self):
  49. return True