test_win_functions.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.utils.platform
  6. import salt.utils.win_functions as win_functions
  7. from tests.support.mock import MagicMock, patch
  8. # Import Salt Testing Libs
  9. from tests.support.unit import TestCase, skipIf
  10. # Import 3rd Party Libs
  11. try:
  12. import win32net
  13. HAS_WIN32 = True
  14. class WinError(win32net.error):
  15. winerror = 0
  16. except ImportError:
  17. HAS_WIN32 = False
  18. class WinFunctionsTestCase(TestCase):
  19. """
  20. Test cases for salt.utils.win_functions
  21. """
  22. def test_escape_argument_simple(self):
  23. """
  24. Test to make sure we encode simple arguments correctly
  25. """
  26. encoded = win_functions.escape_argument("simple")
  27. self.assertEqual(encoded, "simple")
  28. def test_escape_argument_with_space(self):
  29. """
  30. Test to make sure we encode arguments containing spaces correctly
  31. """
  32. encoded = win_functions.escape_argument("with space")
  33. self.assertEqual(encoded, '^"with space^"')
  34. def test_escape_argument_simple_path(self):
  35. """
  36. Test to make sure we encode simple path arguments correctly
  37. """
  38. encoded = win_functions.escape_argument("C:\\some\\path")
  39. self.assertEqual(encoded, "C:\\some\\path")
  40. def test_escape_argument_path_with_space(self):
  41. """
  42. Test to make sure we encode path arguments containing spaces correctly
  43. """
  44. encoded = win_functions.escape_argument("C:\\Some Path\\With Spaces")
  45. self.assertEqual(encoded, '^"C:\\Some Path\\With Spaces^"')
  46. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  47. def test_broadcast_setting_change(self):
  48. """
  49. Test to rehash the Environment variables
  50. """
  51. self.assertTrue(win_functions.broadcast_setting_change())
  52. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  53. def test_get_user_groups(self):
  54. groups = ["Administrators", "Users"]
  55. with patch("win32net.NetUserGetLocalGroups", return_value=groups):
  56. ret = win_functions.get_user_groups("Administrator")
  57. self.assertListEqual(groups, ret)
  58. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  59. def test_get_user_groups_sid(self):
  60. groups = ["Administrators", "Users"]
  61. expected = ["S-1-5-32-544", "S-1-5-32-545"]
  62. with patch("win32net.NetUserGetLocalGroups", return_value=groups):
  63. ret = win_functions.get_user_groups("Administrator", sid=True)
  64. self.assertListEqual(expected, ret)
  65. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  66. def test_get_user_groups_system(self):
  67. groups = ["SYSTEM"]
  68. with patch("win32net.NetUserGetLocalGroups", return_value=groups):
  69. ret = win_functions.get_user_groups("SYSTEM")
  70. self.assertListEqual(groups, ret)
  71. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  72. @skipIf(not HAS_WIN32, "Requires pywin32 libraries")
  73. def test_get_user_groups_unavailable_dc(self):
  74. groups = ["Administrators", "Users"]
  75. win_error = WinError()
  76. win_error.winerror = 1722
  77. effect = [win_error, groups]
  78. with patch("win32net.NetUserGetLocalGroups", side_effect=effect):
  79. ret = win_functions.get_user_groups("Administrator")
  80. self.assertListEqual(groups, ret)
  81. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  82. @skipIf(not HAS_WIN32, "Requires pywin32 libraries")
  83. def test_get_user_groups_unknown_dc(self):
  84. groups = ["Administrators", "Users"]
  85. win_error = WinError()
  86. win_error.winerror = 2453
  87. effect = [win_error, groups]
  88. with patch("win32net.NetUserGetLocalGroups", side_effect=effect):
  89. ret = win_functions.get_user_groups("Administrator")
  90. self.assertListEqual(groups, ret)
  91. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  92. @skipIf(not HAS_WIN32, "Requires pywin32 libraries")
  93. def test_get_user_groups_missing_permission(self):
  94. groups = ["Administrators", "Users"]
  95. win_error = WinError()
  96. win_error.winerror = 5
  97. effect = [win_error, groups]
  98. with patch("win32net.NetUserGetLocalGroups", side_effect=effect):
  99. ret = win_functions.get_user_groups("Administrator")
  100. self.assertListEqual(groups, ret)
  101. @skipIf(not salt.utils.platform.is_windows(), "WinDLL only available on Windows")
  102. @skipIf(not HAS_WIN32, "Requires pywin32 libraries")
  103. def test_get_user_groups_error(self):
  104. win_error = WinError()
  105. win_error.winerror = 1927
  106. mock_error = MagicMock(side_effect=win_error)
  107. with patch("win32net.NetUserGetLocalGroups", side_effect=mock_error):
  108. with self.assertRaises(WinError):
  109. win_functions.get_user_groups("Administrator")