1
0

test_shadow.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Erik Johnson <erik@saltstack.com>
  4. '''
  5. # Import Pytohn libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing libs
  8. import salt.utils.platform
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase, skipIf
  11. # Import salt libs
  12. try:
  13. import salt.modules.shadow as shadow
  14. HAS_SHADOW = True
  15. except ImportError:
  16. HAS_SHADOW = False
  17. # Import 3rd-party libs
  18. import pytest
  19. from salt.ext import six
  20. _PASSWORD = 'lamepassword'
  21. # Not testing blowfish as it is not available on most Linux distros
  22. _HASHES = dict(
  23. md5=dict(
  24. pw_salt='TgIp9OTu',
  25. pw_hash='$1$TgIp9OTu$.d0FFP6jVi5ANoQmk6GpM1'
  26. ),
  27. sha256=dict(
  28. pw_salt='3vINbSrC',
  29. pw_hash='$5$3vINbSrC$hH8A04jAY3bG123yU4FQ0wvP678QDTvWBhHHFbz6j0D'
  30. ),
  31. sha512=dict(
  32. pw_salt='PiGA3V2o',
  33. pw_hash='$6$PiGA3V2o$/PrntRYufz49bRV/V5Eb1V6DdHaS65LB0fu73Tp/xxmDFr6HWJKptY2TvHRDViXZugWpnAcOnrbORpOgZUGTn.'
  34. ),
  35. )
  36. @skipIf(not salt.utils.platform.is_linux(), 'minion is not Linux')
  37. @skipIf(not HAS_SHADOW, 'shadow module is not available')
  38. class LinuxShadowTest(TestCase, LoaderModuleMockMixin):
  39. def setup_loader_modules(self):
  40. return {shadow: {}}
  41. def test_gen_password(self):
  42. '''
  43. Test shadow.gen_password
  44. '''
  45. self.assertTrue(HAS_SHADOW)
  46. for algorithm, hash_info in six.iteritems(_HASHES):
  47. self.assertEqual(
  48. shadow.gen_password(
  49. _PASSWORD,
  50. crypt_salt=hash_info['pw_salt'],
  51. algorithm=algorithm
  52. ),
  53. hash_info['pw_hash']
  54. )
  55. @pytest.mark.skip_if_not_root
  56. def test_list_users(self):
  57. '''
  58. Test if it returns a list of all users
  59. '''
  60. self.assertTrue(shadow.list_users())