A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_blowfish.py 4.1 KiB

13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import unittest
  23. import random
  24. import string
  25. from earwigbot import blowfish
  26. class TestBlowfish(unittest.TestCase):
  27. def test_key_sizes(self):
  28. b = blowfish.Blowfish
  29. e = blowfish.KeyLengthError
  30. self.assertRaisesRegexp(e, "no key given", b, None)
  31. self.assertRaisesRegexp(e, "no key given", b, "")
  32. self.assertRaisesRegexp(e, "at least", b, " " * 3)
  33. self.assertRaisesRegexp(e, "at least", b, "1234567")
  34. self.assertRaisesRegexp(e, "less than", b, " " * 57)
  35. self.assertRaisesRegexp(e, "less than", b, "x" * 60)
  36. self.assertRaisesRegexp(e, "less than", b, "1" * 128)
  37. b("These keys should be valid!")
  38. b("'!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'")
  39. b(" " * 8)
  40. b(" " * 56)
  41. def test_symmetry(self):
  42. def _test_symmetry():
  43. key_length = random.randint(8, 56)
  44. msg_length = random.randint(0, 4096)
  45. key = "".join([random.choice(chars) for i in xrange(key_length)])
  46. msg = "".join([random.choice(chars) for i in xrange(msg_length)])
  47. enc = blowfish.encrypt(key, msg)
  48. dec = blowfish.decrypt(key, enc)
  49. self.assertEqual(dec, msg)
  50. chars = string.letters + string.digits + string.punctuation
  51. for i in xrange(8):
  52. _test_symmetry()
  53. def test_encryption(self):
  54. tests = [
  55. ("example_key", "Hello, I'm a message!", "8411a21574431176cdff9a549d27962c616014a9fe2a1fe3b0c7a823e8a1e635"),
  56. ("another random key", "Another random message! :(", "2cdcdf4e53145897ed9d4cc2433aa4bf59b087b14d0ac76a13eff12dec00e60c40857109da3c7bc4"),
  57. ("HEY LET'S TRY |°|_J|\|C7|_J/-\\710|\|", "Yes, that was my fail attempt at 1337SP33K >_>", "d4901c7c0956da3b9507cd81cd3c880d7cda25ec6c5336deb9280ce67c099eeddf7c7e052f3a946afbd92c32ae0ab8dbdd875bc5a3f0d686")
  58. ]
  59. for test in tests:
  60. self.assertEquals(blowfish.encrypt(test[0], test[1]), test[2])
  61. def test_decryption(self):
  62. tests = [
  63. ("blah blah blah", "ab35274c66bb8b3b03c9bd26ab477f3de06857e1d369ad35", "Blah, blah, blah!"),
  64. ("random key", "eb2fe950c5c12bca9534ffdd27631f33d3e4bcae53a634b4aaa09f9fe14c4386", "Random message as well!"),
  65. ("Okay, now I'm just desperate", "0da74e1cec41e8323da93d0c05bcf3919084130cef93021991da174fd97f8e1c9b125ed5263b41a8", "Unit testing is SO FUN ISN'T IT.")
  66. ]
  67. for test in tests:
  68. self.assertEquals(blowfish.decrypt(test[0], test[1]), test[2])
  69. def test_decryption_exceptions(self):
  70. d = blowfish.decrypt
  71. e = blowfish.BlowfishError
  72. e1 = "could not be decoded"
  73. e2 = "cannot be broken into 8-byte blocks"
  74. e3 = "key is incorrect"
  75. self.assertRaisesRegexp(e, e1, d, "some_key", "arr!")
  76. self.assertRaisesRegexp(e, e2, d, "some_key", "abcd")
  77. self.assertRaisesRegexp(e, e3, d, "some_key", "abcdabcdabcdabcd")
  78. if __name__ == "__main__":
  79. unittest.main(verbosity=2)