A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test_blowfish.py 3.0 KiB

13年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. import random
  4. import string
  5. import support
  6. import blowfish
  7. class TestBlowfish(unittest.TestCase):
  8. def test_key_sizes(self):
  9. b = blowfish.Blowfish
  10. e = blowfish.KeyLengthError
  11. self.assertRaisesRegexp(e, "no key given", b, None)
  12. self.assertRaisesRegexp(e, "no key given", b, "")
  13. self.assertRaisesRegexp(e, "at least", b, " " * 3)
  14. self.assertRaisesRegexp(e, "at least", b, "1234567")
  15. self.assertRaisesRegexp(e, "less than", b, " " * 57)
  16. self.assertRaisesRegexp(e, "less than", b, "x" * 60)
  17. self.assertRaisesRegexp(e, "less than", b, "1" * 128)
  18. b("These keys should be valid!")
  19. b("'!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'")
  20. b(" " * 8)
  21. b(" " * 56)
  22. def test_symmetry(self):
  23. def _test_symmetry():
  24. key_length = random.randint(8, 56)
  25. msg_length = random.randint(0, 4096)
  26. key = "".join([random.choice(chars) for i in xrange(key_length)])
  27. msg = "".join([random.choice(chars) for i in xrange(msg_length)])
  28. enc = blowfish.encrypt(key, msg)
  29. dec = blowfish.decrypt(key, enc)
  30. self.assertEqual(dec, msg)
  31. chars = string.letters + string.digits + string.punctuation
  32. for i in xrange(8):
  33. _test_symmetry()
  34. def test_encryption(self):
  35. tests = [
  36. ("example_key", "Hello, I'm a message!", "8411a21574431176cdff9a549d27962c616014a9fe2a1fe3b0c7a823e8a1e635"),
  37. ("another random key", "Another random message! :(", "2cdcdf4e53145897ed9d4cc2433aa4bf59b087b14d0ac76a13eff12dec00e60c40857109da3c7bc4"),
  38. ("HEY LET'S TRY |°|_J|\|C7|_J/-\\710|\|", "Yes, that was my fail attempt at 1337SP33K >_>", "d4901c7c0956da3b9507cd81cd3c880d7cda25ec6c5336deb9280ce67c099eeddf7c7e052f3a946afbd92c32ae0ab8dbdd875bc5a3f0d686")
  39. ]
  40. for test in tests:
  41. self.assertEquals(blowfish.encrypt(test[0], test[1]), test[2])
  42. def test_decryption(self):
  43. tests = [
  44. ("blah blah blah", "ab35274c66bb8b3b03c9bd26ab477f3de06857e1d369ad35", "Blah, blah, blah!"),
  45. ("random key", "eb2fe950c5c12bca9534ffdd27631f33d3e4bcae53a634b4aaa09f9fe14c4386", "Random message as well!"),
  46. ("Okay, now I'm just desperate", "0da74e1cec41e8323da93d0c05bcf3919084130cef93021991da174fd97f8e1c9b125ed5263b41a8", "Unit testing is SO FUN ISN'T IT.")
  47. ]
  48. for test in tests:
  49. self.assertEquals(blowfish.decrypt(test[0], test[1]), test[2])
  50. def test_decryption_exceptions(self):
  51. d = blowfish.decrypt
  52. e = blowfish.BlowfishError
  53. e1 = "could not be decoded"
  54. e2 = "cannot be broken into 8-byte blocks"
  55. e3 = "key is incorrect"
  56. self.assertRaisesRegexp(e, e1, d, "some_key", "arr!")
  57. self.assertRaisesRegexp(e, e2, d, "some_key", "abcd")
  58. self.assertRaisesRegexp(e, e3, d, "some_key", "abcdabcdabcdabcd")
  59. if __name__ == "__main__":
  60. unittest.main(verbosity=2)