test_reg.py 10 KB

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