test_linux_shadow.py 8.4 KB

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