test_reg.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for the Reg State
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import logging
  7. import pytest
  8. import salt.utils.platform
  9. import salt.utils.win_reg as reg
  10. from tests.support.case import ModuleCase
  11. from tests.support.helpers import destructiveTest, random_string, slowTest
  12. from tests.support.mixins import SaltReturnAssertsMixin
  13. from tests.support.unit import skipIf
  14. log = logging.getLogger(__name__)
  15. UNICODE_VALUE_NAME = "Unicode Key \N{TRADE MARK SIGN}"
  16. UNICODE_VALUE = (
  17. "Unicode Value " "\N{COPYRIGHT SIGN},\N{TRADE MARK SIGN},\N{REGISTERED SIGN}"
  18. )
  19. FAKE_KEY = "SOFTWARE\\{0}".format(random_string("SaltTesting-", lowercase=False))
  20. @destructiveTest
  21. @skipIf(not salt.utils.platform.is_windows(), "Windows Specific Test")
  22. @pytest.mark.windows_whitelisted
  23. class RegTest(ModuleCase, SaltReturnAssertsMixin):
  24. """
  25. Reg state module tests
  26. These tests are destructive as the modify the registry
  27. """
  28. def tearDown(self):
  29. reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
  30. reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY, use_32bit_registry=True)
  31. @slowTest
  32. def test_present_reg_sz(self):
  33. """
  34. Testing reg.present with REG_SZ
  35. """
  36. log.debug("Testing reg.present with REG_SZ")
  37. # default type is 'REG_SZ'
  38. # Does the state return the correct data
  39. ret = self.run_state(
  40. "reg.present",
  41. name="HKLM\\{0}".format(FAKE_KEY),
  42. vname="test_reg_sz",
  43. vdata="fake string data",
  44. )
  45. expected = {
  46. "reg": {
  47. "Added": {
  48. "Entry": "test_reg_sz",
  49. "Inheritance": True,
  50. "Key": "HKLM\\{0}".format(FAKE_KEY),
  51. "Owner": None,
  52. "Perms": {"Deny": None, "Grant": None},
  53. "Value": "fake string data",
  54. }
  55. }
  56. }
  57. self.assertSaltStateChangesEqual(ret, expected)
  58. # Is the value actually set
  59. ret = reg.read_value(hive="HKLM", key=FAKE_KEY, vname="test_reg_sz")
  60. expected = {
  61. "vtype": "REG_SZ",
  62. "vname": "test_reg_sz",
  63. "success": True,
  64. "hive": "HKLM",
  65. "vdata": "fake string data",
  66. "key": FAKE_KEY,
  67. }
  68. self.assertEqual(ret, expected)
  69. @slowTest
  70. def test_present_reg_sz_unicode_value(self):
  71. """
  72. Testing reg.present with REG_SZ and a unicode value
  73. """
  74. log.debug("Testing reg.present with REG_SZ and a unicode value")
  75. # default type is 'REG_SZ'
  76. # Does the state return the correct data
  77. ret = self.run_state(
  78. "reg.present",
  79. name="HKLM\\{0}".format(FAKE_KEY),
  80. vname="test_reg_sz",
  81. vdata=UNICODE_VALUE,
  82. )
  83. expected = {
  84. "reg": {
  85. "Added": {
  86. "Entry": "test_reg_sz",
  87. "Inheritance": True,
  88. "Key": "HKLM\\{0}".format(FAKE_KEY),
  89. "Owner": None,
  90. "Perms": {"Deny": None, "Grant": None},
  91. "Value": UNICODE_VALUE,
  92. }
  93. }
  94. }
  95. self.assertSaltStateChangesEqual(ret, expected)
  96. # Is the value actually set
  97. ret = reg.read_value(hive="HKLM", key=FAKE_KEY, vname="test_reg_sz")
  98. expected = {
  99. "vtype": "REG_SZ",
  100. "vname": "test_reg_sz",
  101. "success": True,
  102. "hive": "HKLM",
  103. "vdata": UNICODE_VALUE,
  104. "key": FAKE_KEY,
  105. }
  106. self.assertEqual(ret, expected)
  107. @slowTest
  108. def test_present_reg_sz_unicode_default_value(self):
  109. """
  110. Testing reg.present with REG_SZ and a unicode default value
  111. """
  112. log.debug("Testing reg.present with REG_SZ and a unicode default value")
  113. # default type is 'REG_SZ'
  114. # Does the state return the correct data
  115. ret = self.run_state(
  116. "reg.present", name="HKLM\\{0}".format(FAKE_KEY), vdata=UNICODE_VALUE
  117. )
  118. expected = {
  119. "reg": {
  120. "Added": {
  121. "Entry": "(Default)",
  122. "Inheritance": True,
  123. "Key": "HKLM\\{0}".format(FAKE_KEY),
  124. "Owner": None,
  125. "Perms": {"Deny": None, "Grant": None},
  126. "Value": UNICODE_VALUE,
  127. }
  128. }
  129. }
  130. self.assertSaltStateChangesEqual(ret, expected)
  131. # Is the value actually set
  132. ret = reg.read_value(hive="HKLM", key=FAKE_KEY)
  133. expected = {
  134. "vtype": "REG_SZ",
  135. "vname": "(Default)",
  136. "success": True,
  137. "hive": "HKLM",
  138. "vdata": UNICODE_VALUE,
  139. "key": FAKE_KEY,
  140. }
  141. self.assertEqual(ret, expected)
  142. @slowTest
  143. def test_present_reg_sz_unicode_value_name(self):
  144. """
  145. Testing reg.present with REG_SZ and a unicode value name
  146. """
  147. log.debug("Testing reg.present with REG_SZ and a unicode value name")
  148. # default type is 'REG_SZ'
  149. # Does the state return the correct data
  150. ret = self.run_state(
  151. "reg.present",
  152. name="HKLM\\{0}".format(FAKE_KEY),
  153. vname=UNICODE_VALUE_NAME,
  154. vdata="fake string data",
  155. )
  156. expected = {
  157. "reg": {
  158. "Added": {
  159. "Entry": UNICODE_VALUE_NAME,
  160. "Inheritance": True,
  161. "Key": "HKLM\\{0}".format(FAKE_KEY),
  162. "Owner": None,
  163. "Perms": {"Deny": None, "Grant": None},
  164. "Value": "fake string data",
  165. }
  166. }
  167. }
  168. self.assertSaltStateChangesEqual(ret, expected)
  169. # Is the value actually set
  170. ret = reg.read_value(hive="HKLM", key=FAKE_KEY, vname=UNICODE_VALUE_NAME)
  171. expected = {
  172. "vtype": "REG_SZ",
  173. "vname": UNICODE_VALUE_NAME,
  174. "success": True,
  175. "hive": "HKLM",
  176. "vdata": "fake string data",
  177. "key": FAKE_KEY,
  178. }
  179. self.assertEqual(ret, expected)
  180. @slowTest
  181. def test_present_reg_binary(self):
  182. """
  183. Testing reg.present with REG_BINARY
  184. """
  185. test_data = "Salty Test"
  186. log.debug("Testing reg.present with REG_BINARY")
  187. # default type is 'REG_SZ'
  188. # Does the state return the correct data
  189. ret = self.run_state(
  190. "reg.present",
  191. name="HKLM\\{0}".format(FAKE_KEY),
  192. vname="test_reg_binary",
  193. vtype="REG_BINARY",
  194. vdata=test_data,
  195. )
  196. expected = {
  197. "reg": {
  198. "Added": {
  199. "Entry": "test_reg_binary",
  200. "Inheritance": True,
  201. "Key": "HKLM\\{0}".format(FAKE_KEY),
  202. "Owner": None,
  203. "Perms": {"Deny": None, "Grant": None},
  204. "Value": test_data,
  205. }
  206. }
  207. }
  208. self.assertSaltStateChangesEqual(ret, expected)
  209. # Is the value actually set
  210. ret = reg.read_value(hive="HKLM", key=FAKE_KEY, vname="test_reg_binary")
  211. expected = {
  212. "vtype": "REG_BINARY",
  213. "vname": "test_reg_binary",
  214. "success": True,
  215. "hive": "HKLM",
  216. "vdata": test_data.encode("utf-8"),
  217. "key": FAKE_KEY,
  218. }
  219. self.assertEqual(ret, expected)
  220. @slowTest
  221. def test_present_reg_multi_sz(self):
  222. """
  223. Testing reg.present with REG_MULTI_SZ
  224. """
  225. log.debug("Testing reg.present with REG_MULTI_SZ")
  226. # default type is 'REG_SZ'
  227. # Does the state return the correct data
  228. ret = self.run_state(
  229. "reg.present",
  230. name="HKLM\\{0}".format(FAKE_KEY),
  231. vname="test_reg_multi_sz",
  232. vtype="REG_MULTI_SZ",
  233. vdata=["item1", "item2"],
  234. )
  235. expected = {
  236. "reg": {
  237. "Added": {
  238. "Entry": "test_reg_multi_sz",
  239. "Inheritance": True,
  240. "Key": "HKLM\\{0}".format(FAKE_KEY),
  241. "Owner": None,
  242. "Perms": {"Deny": None, "Grant": None},
  243. "Value": ["item1", "item2"],
  244. }
  245. }
  246. }
  247. self.assertSaltStateChangesEqual(ret, expected)
  248. # Is the value actually set
  249. ret = reg.read_value(hive="HKLM", key=FAKE_KEY, vname="test_reg_multi_sz")
  250. expected = {
  251. "vtype": "REG_MULTI_SZ",
  252. "vname": "test_reg_multi_sz",
  253. "success": True,
  254. "hive": "HKLM",
  255. "vdata": ["item1", "item2"],
  256. "key": FAKE_KEY,
  257. }
  258. self.assertEqual(ret, expected)
  259. @slowTest
  260. def test_present_32_bit(self):
  261. """
  262. Testing reg.present with REG_SZ using 32bit registry
  263. """
  264. log.debug("Testing reg.present with REG_SZ using 32bit registry")
  265. # default type is 'REG_SZ'
  266. # Does the state return the correct data
  267. ret = self.run_state(
  268. "reg.present",
  269. name="HKLM\\{0}".format(FAKE_KEY),
  270. vname="test_reg_sz",
  271. vdata="fake string data",
  272. use_32bit_registry=True,
  273. )
  274. expected = {
  275. "reg": {
  276. "Added": {
  277. "Entry": "test_reg_sz",
  278. "Inheritance": True,
  279. "Key": "HKLM\\{0}".format(FAKE_KEY),
  280. "Owner": None,
  281. "Perms": {"Deny": None, "Grant": None},
  282. "Value": "fake string data",
  283. }
  284. }
  285. }
  286. self.assertSaltStateChangesEqual(ret, expected)
  287. # Is the value actually set
  288. ret = reg.read_value(
  289. hive="HKLM", key=FAKE_KEY, vname="test_reg_sz", use_32bit_registry=True
  290. )
  291. expected = {
  292. "vtype": "REG_SZ",
  293. "vname": "test_reg_sz",
  294. "success": True,
  295. "hive": "HKLM",
  296. "vdata": "fake string data",
  297. "key": FAKE_KEY,
  298. }
  299. self.assertEqual(ret, expected)