test_AllOrNothing.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # Test script for Crypto.Protocol.AllOrNothing
  3. #
  4. # Part of the Python Cryptography Toolkit
  5. #
  6. # Written by Andrew Kuchling and others
  7. #
  8. # ===================================================================
  9. # The contents of this file are dedicated to the public domain. To
  10. # the extent that dedication to the public domain is not available,
  11. # everyone is granted a worldwide, perpetual, royalty-free,
  12. # non-exclusive license to exercise all rights associated with the
  13. # contents of this file for any purpose whatsoever.
  14. # No rights are reserved.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # ===================================================================
  25. __revision__ = "$Id$"
  26. import unittest
  27. from Crypto.Protocol import AllOrNothing
  28. from Crypto.Util.py3compat import *
  29. text = b("""\
  30. When in the Course of human events, it becomes necessary for one people to
  31. dissolve the political bands which have connected them with another, and to
  32. assume among the powers of the earth, the separate and equal station to which
  33. the Laws of Nature and of Nature's God entitle them, a decent respect to the
  34. opinions of mankind requires that they should declare the causes which impel
  35. them to the separation.
  36. We hold these truths to be self-evident, that all men are created equal, that
  37. they are endowed by their Creator with certain unalienable Rights, that among
  38. these are Life, Liberty, and the pursuit of Happiness. That to secure these
  39. rights, Governments are instituted among Men, deriving their just powers from
  40. the consent of the governed. That whenever any Form of Government becomes
  41. destructive of these ends, it is the Right of the People to alter or to
  42. abolish it, and to institute new Government, laying its foundation on such
  43. principles and organizing its powers in such form, as to them shall seem most
  44. likely to effect their Safety and Happiness.
  45. """)
  46. class AllOrNothingTest (unittest.TestCase):
  47. def runTest(self):
  48. "Simple test of AllOrNothing"
  49. from Crypto.Cipher import AES
  50. import base64
  51. # The current AllOrNothing will fail
  52. # every so often. Repeat the test
  53. # several times to force this.
  54. for i in range(50):
  55. x = AllOrNothing.AllOrNothing(AES)
  56. msgblocks = x.digest(text)
  57. # get a new undigest-only object so there's no leakage
  58. y = AllOrNothing.AllOrNothing(AES)
  59. text2 = y.undigest(msgblocks)
  60. self.assertEqual(text, text2)
  61. def get_tests(config={}):
  62. return [AllOrNothingTest()]
  63. if __name__ == "__main__":
  64. unittest.main()