test_XOR.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Cipher/XOR.py: Self-test for the XOR "cipher"
  4. #
  5. # Written in 2008 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-test suite for Crypto.Cipher.XOR"""
  25. import unittest
  26. __revision__ = "$Id$"
  27. from Crypto.Util.py3compat import *
  28. # This is a list of (plaintext, ciphertext, key) tuples.
  29. test_data = [
  30. # Test vectors written from scratch. (Nobody posts XOR test vectors on the web? How disappointing.)
  31. ('01', '01',
  32. '00',
  33. 'zero key'),
  34. ('0102040810204080', '0003050911214181',
  35. '01',
  36. '1-byte key'),
  37. ('0102040810204080', 'cda8c8a2dc8a8c2a',
  38. 'ccaa',
  39. '2-byte key'),
  40. ('ff'*64, 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0'*2,
  41. '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
  42. '32-byte key'),
  43. ]
  44. class TruncationSelfTest(unittest.TestCase):
  45. def runTest(self):
  46. """33-byte key (should raise ValueError under current implementation)"""
  47. # Crypto.Cipher.XOR previously truncated its inputs at 32 bytes. Now
  48. # it should raise a ValueError if the length is too long.
  49. self.assertRaises(ValueError, XOR.new, "x"*33)
  50. def get_tests(config={}):
  51. global XOR
  52. from Crypto.Cipher import XOR
  53. from common import make_stream_tests
  54. return make_stream_tests(XOR, "XOR", test_data) + [TruncationSelfTest()]
  55. if __name__ == '__main__':
  56. import unittest
  57. suite = lambda: unittest.TestSuite(get_tests())
  58. unittest.main(defaultTest='suite')
  59. # vim:set ts=4 sw=4 sts=4 expandtab: