test_Counter.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Util/test_Counter: Self-test for the Crypto.Util.Counter module
  4. #
  5. # Written in 2009 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self-tests for Crypto.Util.Counter"""
  25. from tls.Crypto.Util.py3compat import *
  26. import unittest
  27. class CounterTests(unittest.TestCase):
  28. def setUp(self):
  29. global Counter
  30. from tls.Crypto.Util import Counter
  31. def test_BE(self):
  32. """Big endian"""
  33. c = Counter.new(128)
  34. c = Counter.new(128, little_endian=False)
  35. def test_LE(self):
  36. """Little endian"""
  37. c = Counter.new(128, little_endian=True)
  38. def test_nbits(self):
  39. c = Counter.new(nbits=128)
  40. self.assertRaises(ValueError, Counter.new, 129)
  41. def test_prefix(self):
  42. c = Counter.new(128, prefix=b("xx"))
  43. def test_suffix(self):
  44. c = Counter.new(128, suffix=b("xx"))
  45. def test_iv(self):
  46. c = Counter.new(128, initial_value=2)
  47. def get_tests(config={}):
  48. from tls.Crypto.SelfTest.st_common import list_test_cases
  49. return list_test_cases(CounterTests)
  50. if __name__ == '__main__':
  51. suite = lambda: unittest.TestSuite(get_tests())
  52. unittest.main(defaultTest='suite')
  53. # vim:set ts=4 sw=4 sts=4 expandtab: