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