test_pycrypto.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. import re
  6. # Import Salt Libs
  7. import salt.utils.pycrypto
  8. import salt.utils.platform
  9. # Import Salt Testing Libs
  10. from tests.support.unit import TestCase, skipIf
  11. log = logging.getLogger(__name__)
  12. class PycryptoTestCase(TestCase):
  13. '''
  14. TestCase for salt.utils.pycrypto module
  15. '''
  16. # The crypt module is only available on Unix systems
  17. # https://docs.python.org/dev/library/crypt.html
  18. @skipIf(not salt.utils.pycrypto.HAS_CRYPT, 'crypt module not available')
  19. def test_gen_hash(self):
  20. '''
  21. Test gen_hash
  22. '''
  23. passwd = 'test_password'
  24. id = '$'
  25. if salt.utils.platform.is_darwin():
  26. id = ''
  27. ret = salt.utils.pycrypto.gen_hash(password=passwd)
  28. self.assertTrue(ret.startswith('$6{0}'.format(id)))
  29. ret = salt.utils.pycrypto.gen_hash(password=passwd, algorithm='md5')
  30. self.assertTrue(ret.startswith('$1{0}'.format(id)))
  31. ret = salt.utils.pycrypto.gen_hash(password=passwd, algorithm='sha256')
  32. self.assertTrue(ret.startswith('$5{0}'.format(id)))
  33. def test_secure_password(self):
  34. '''
  35. test secure_password
  36. '''
  37. ret = salt.utils.pycrypto.secure_password()
  38. check = re.compile(r'[!@#$%^&*()_=+]')
  39. assert check.search(ret) is None
  40. assert ret