1
0

test_linux_shadow.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration tests for shadow linux
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import salt.modules.linux_shadow
  8. import salt.utils.files
  9. import salt.utils.platform
  10. from tests.support.case import ModuleCase
  11. from tests.support.helpers import (
  12. destructiveTest,
  13. flaky,
  14. random_string,
  15. skip_if_not_root,
  16. slowTest,
  17. )
  18. from tests.support.unit import skipIf
  19. @skip_if_not_root
  20. @skipIf(not salt.utils.platform.is_linux(), "These tests can only be run on linux")
  21. class ShadowModuleTest(ModuleCase):
  22. """
  23. Validate the linux shadow system module
  24. """
  25. def setUp(self):
  26. """
  27. Get current settings
  28. """
  29. self._password = self.run_function("shadow.gen_password", ["Password1234"])
  30. if "ERROR" in self._password:
  31. self.fail("Failed to generate password: {0}".format(self._password))
  32. super(ShadowModuleTest, self).setUp()
  33. self._no_user = random_string("tu-", uppercase=False)
  34. self._test_user = random_string("tu-", uppercase=False)
  35. self._password = salt.modules.linux_shadow.gen_password("Password1234")
  36. @destructiveTest
  37. @slowTest
  38. def test_info(self):
  39. """
  40. Test shadow.info
  41. """
  42. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  43. self.run_function("user.add", [self._test_user])
  44. # Correct Functionality
  45. ret = self.run_function("shadow.info", [self._test_user])
  46. self.assertEqual(ret["name"], self._test_user)
  47. # User does not exist
  48. ret = self.run_function("shadow.info", [self._no_user])
  49. self.assertEqual(ret["name"], "")
  50. @destructiveTest
  51. @slowTest
  52. def test_del_password(self):
  53. """
  54. Test shadow.del_password
  55. """
  56. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  57. self.run_function("user.add", [self._test_user])
  58. # Correct Functionality
  59. self.assertTrue(self.run_function("shadow.del_password", [self._test_user]))
  60. self.assertEqual(
  61. self.run_function("shadow.info", [self._test_user])["passwd"], ""
  62. )
  63. # User does not exist
  64. self.assertFalse(self.run_function("shadow.del_password", [self._no_user]))
  65. @destructiveTest
  66. @slowTest
  67. def test_set_password(self):
  68. """
  69. Test shadow.set_password
  70. """
  71. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  72. self.run_function("user.add", [self._test_user])
  73. # Correct Functionality
  74. self.assertTrue(
  75. self.run_function("shadow.set_password", [self._test_user, self._password])
  76. )
  77. # User does not exist
  78. self.assertFalse(
  79. self.run_function("shadow.set_password", [self._no_user, self._password])
  80. )
  81. @destructiveTest
  82. @slowTest
  83. def test_set_inactdays(self):
  84. """
  85. Test shadow.set_inactdays
  86. """
  87. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  88. self.run_function("user.add", [self._test_user])
  89. # Correct Functionality
  90. self.assertTrue(
  91. self.run_function("shadow.set_inactdays", [self._test_user, 12])
  92. )
  93. # User does not exist (set_inactdays return None is user does not exist)
  94. self.assertFalse(self.run_function("shadow.set_inactdays", [self._no_user, 12]))
  95. @destructiveTest
  96. @slowTest
  97. def test_set_maxdays(self):
  98. """
  99. Test shadow.set_maxdays
  100. """
  101. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  102. self.run_function("user.add", [self._test_user])
  103. # Correct Functionality
  104. self.assertTrue(self.run_function("shadow.set_maxdays", [self._test_user, 12]))
  105. # User does not exist (set_inactdays return None is user does not exist)
  106. self.assertFalse(self.run_function("shadow.set_maxdays", [self._no_user, 12]))
  107. @destructiveTest
  108. @slowTest
  109. def test_set_mindays(self):
  110. """
  111. Test shadow.set_mindays
  112. """
  113. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  114. self.run_function("user.add", [self._test_user])
  115. # Correct Functionality
  116. self.assertTrue(self.run_function("shadow.set_mindays", [self._test_user, 12]))
  117. # User does not exist (set_inactdays return None is user does not exist)
  118. self.assertFalse(self.run_function("shadow.set_mindays", [self._no_user, 12]))
  119. @flaky
  120. @destructiveTest
  121. @slowTest
  122. def test_lock_password(self):
  123. """
  124. Test shadow.lock_password
  125. """
  126. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  127. self.run_function("user.add", [self._test_user])
  128. self.run_function("shadow.set_password", [self._test_user, self._password])
  129. # Correct Functionality
  130. self.assertTrue(self.run_function("shadow.lock_password", [self._test_user]))
  131. # User does not exist (set_inactdays return None is user does not exist)
  132. self.assertFalse(self.run_function("shadow.lock_password", [self._no_user]))
  133. @destructiveTest
  134. @slowTest
  135. def test_unlock_password(self):
  136. """
  137. Test shadow.lock_password
  138. """
  139. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  140. self.run_function("user.add", [self._test_user])
  141. self.run_function("shadow.set_password", [self._test_user, self._password])
  142. # Correct Functionality
  143. self.assertTrue(self.run_function("shadow.unlock_password", [self._test_user]))
  144. # User does not exist (set_inactdays return None is user does not exist)
  145. self.assertFalse(self.run_function("shadow.unlock_password", [self._no_user]))
  146. @destructiveTest
  147. @slowTest
  148. def test_set_warndays(self):
  149. """
  150. Test shadow.set_warndays
  151. """
  152. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  153. self.run_function("user.add", [self._test_user])
  154. # Correct Functionality
  155. self.assertTrue(self.run_function("shadow.set_warndays", [self._test_user, 12]))
  156. # User does not exist (set_inactdays return None is user does not exist)
  157. self.assertFalse(self.run_function("shadow.set_warndays", [self._no_user, 12]))
  158. @destructiveTest
  159. @slowTest
  160. def test_set_date(self):
  161. """
  162. Test shadow.set_date
  163. """
  164. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  165. self.run_function("user.add", [self._test_user])
  166. # Correct Functionality
  167. self.assertTrue(
  168. self.run_function("shadow.set_date", [self._test_user, "2016-08-19"])
  169. )
  170. # User does not exist (set_inactdays return None is user does not exist)
  171. self.assertFalse(
  172. self.run_function("shadow.set_date", [self._no_user, "2016-08-19"])
  173. )
  174. @destructiveTest
  175. @slowTest
  176. def test_set_expire(self):
  177. """
  178. Test shadow.set_exipre
  179. """
  180. self.addCleanup(self.run_function, "user.delete", [self._test_user])
  181. self.run_function("user.add", [self._test_user])
  182. # Correct Functionality
  183. self.assertTrue(
  184. self.run_function("shadow.set_expire", [self._test_user, "2016-08-25"])
  185. )
  186. # User does not exist (set_inactdays return None is user does not exist)
  187. self.assertFalse(
  188. self.run_function("shadow.set_expire", [self._no_user, "2016-08-25"])
  189. )
  190. @destructiveTest
  191. @slowTest
  192. def test_set_del_root_password(self):
  193. """
  194. Test set/del password for root
  195. """
  196. # saving shadow file
  197. if not os.access("/etc/shadow", os.R_OK | os.W_OK):
  198. self.skipTest("Could not save initial state of /etc/shadow")
  199. def restore_shadow_file(contents):
  200. # restore shadow file
  201. with salt.utils.files.fopen("/etc/shadow", "w") as wfh:
  202. wfh.write(contents)
  203. with salt.utils.files.fopen("/etc/shadow", "r") as rfh:
  204. contents = rfh.read()
  205. self.addCleanup(restore_shadow_file, contents)
  206. # set root password
  207. self.assertTrue(
  208. self.run_function("shadow.set_password", ["root", self._password])
  209. )
  210. self.assertEqual(
  211. self.run_function("shadow.info", ["root"])["passwd"], self._password
  212. )
  213. # delete root password
  214. self.assertTrue(self.run_function("shadow.del_password", ["root"]))
  215. self.assertEqual(self.run_function("shadow.info", ["root"])["passwd"], "")