1
0

test_reg.py 10 KB

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