test_win_reg.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.helpers import destructiveTest, generate_random_name
  6. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch
  7. from tests.support.unit import TestCase, skipIf
  8. # Import Salt Libs
  9. import salt.utils.platform
  10. import salt.utils.win_reg as win_reg
  11. UNICODE_KEY = 'Unicode Key \N{TRADE MARK SIGN}'
  12. UNICODE_VALUE = 'Unicode Value ' \
  13. '\N{COPYRIGHT SIGN},\N{TRADE MARK SIGN},\N{REGISTERED SIGN}'
  14. FAKE_KEY = 'SOFTWARE\\{0}'.format(generate_random_name('SaltTesting-'))
  15. @skipIf(NO_MOCK, NO_MOCK_REASON)
  16. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  17. class WinFunctionsTestCase(TestCase):
  18. '''
  19. Test cases for salt.utils.win_reg
  20. '''
  21. def test_broadcast_change_success(self):
  22. '''
  23. Tests the broadcast_change function
  24. '''
  25. with patch('win32gui.SendMessageTimeout', return_value=('', 0)):
  26. self.assertEqual(win_reg.broadcast_change(), True)
  27. def test_broadcast_change_fail(self):
  28. '''
  29. Tests the broadcast_change function failure
  30. '''
  31. with patch('win32gui.SendMessageTimeout', return_value=('', 1)):
  32. self.assertEqual(win_reg.broadcast_change(), False)
  33. def test_key_exists_existing(self):
  34. '''
  35. Tests the key exists function using a well known registry key
  36. '''
  37. self.assertEqual(
  38. win_reg.key_exists(
  39. hive='HKLM',
  40. key='SOFTWARE\\Microsoft'
  41. ),
  42. True
  43. )
  44. def test_key_exists_non_existing(self):
  45. '''
  46. Tests the key exists function using a non existing registry key
  47. '''
  48. self.assertEqual(
  49. win_reg.key_exists(
  50. hive='HKLM',
  51. key=FAKE_KEY
  52. ),
  53. False
  54. )
  55. def test_list_keys_existing(self):
  56. '''
  57. Test the list_keys function using a well known registry key
  58. '''
  59. self.assertIn(
  60. 'Microsoft',
  61. win_reg.list_keys(
  62. hive='HKLM',
  63. key='SOFTWARE'
  64. )
  65. )
  66. def test_list_keys_non_existing(self):
  67. '''
  68. Test the list_keys function using a non existing registry key
  69. '''
  70. expected = (False, 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY))
  71. self.assertEqual(
  72. win_reg.list_keys(
  73. hive='HKLM',
  74. key=FAKE_KEY
  75. ),
  76. expected
  77. )
  78. def test_list_values_existing(self):
  79. '''
  80. Test the list_values function using a well known registry key
  81. '''
  82. values = win_reg.list_values(
  83. hive='HKLM',
  84. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
  85. )
  86. keys = []
  87. for value in values:
  88. keys.append(value['vname'])
  89. self.assertIn('ProgramFilesDir', keys)
  90. def test_list_values_non_existing(self):
  91. '''
  92. Test the list_values function using a non existing registry key
  93. '''
  94. expected = (False, 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY))
  95. self.assertEqual(
  96. win_reg.list_values(
  97. hive='HKLM',
  98. key=FAKE_KEY
  99. ),
  100. expected
  101. )
  102. def test_read_value_existing(self):
  103. '''
  104. Test the read_value function using a well known registry value
  105. '''
  106. ret = win_reg.read_value(
  107. hive='HKLM',
  108. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  109. vname='ProgramFilesPath'
  110. )
  111. self.assertEqual(ret['vdata'], '%ProgramFiles%')
  112. def test_read_value_default(self):
  113. '''
  114. Test the read_value function reading the default value using a well
  115. known registry key
  116. '''
  117. ret = win_reg.read_value(
  118. hive='HKLM',
  119. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
  120. )
  121. self.assertEqual(ret['vdata'], '(value not set)')
  122. def test_read_value_non_existing(self):
  123. '''
  124. Test the read_value function using a non existing value pair
  125. '''
  126. expected = {
  127. 'comment': 'Cannot find fake_name in HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  128. 'vdata': None,
  129. 'vname': 'fake_name',
  130. 'success': False,
  131. 'hive': 'HKLM',
  132. 'key': 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
  133. }
  134. self.assertEqual(
  135. win_reg.read_value(
  136. hive='HKLM',
  137. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  138. vname='fake_name'
  139. ),
  140. expected
  141. )
  142. def test_read_value_non_existing_key(self):
  143. '''
  144. Test the read_value function using a non existing registry key
  145. '''
  146. expected = {
  147. 'comment': 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY),
  148. 'vdata': None,
  149. 'vname': 'fake_name',
  150. 'success': False,
  151. 'hive': 'HKLM',
  152. 'key': FAKE_KEY
  153. }
  154. self.assertEqual(
  155. win_reg.read_value(
  156. hive='HKLM',
  157. key=FAKE_KEY,
  158. vname='fake_name'
  159. ),
  160. expected
  161. )
  162. @destructiveTest
  163. def test_set_value(self):
  164. '''
  165. Test the set_value function
  166. '''
  167. try:
  168. self.assertTrue(
  169. win_reg.set_value(
  170. hive='HKLM',
  171. key=FAKE_KEY,
  172. vname='fake_name',
  173. vdata='fake_data'
  174. )
  175. )
  176. expected = {
  177. 'hive': 'HKLM',
  178. 'key': FAKE_KEY,
  179. 'success': True,
  180. 'vdata': 'fake_data',
  181. 'vname': 'fake_name',
  182. 'vtype': 'REG_SZ'
  183. }
  184. self.assertEqual(
  185. win_reg.read_value(
  186. hive='HKLM',
  187. key=FAKE_KEY,
  188. vname='fake_name'
  189. ),
  190. expected
  191. )
  192. finally:
  193. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  194. @destructiveTest
  195. def test_set_value_default(self):
  196. '''
  197. Test the set_value function on the default value
  198. '''
  199. try:
  200. self.assertTrue(
  201. win_reg.set_value(
  202. hive='HKLM',
  203. key=FAKE_KEY,
  204. vdata='fake_default_data'
  205. )
  206. )
  207. expected = {
  208. 'hive': 'HKLM',
  209. 'key': FAKE_KEY,
  210. 'success': True,
  211. 'vdata': 'fake_default_data',
  212. 'vname': '(Default)',
  213. 'vtype': 'REG_SZ'
  214. }
  215. self.assertEqual(
  216. win_reg.read_value(
  217. hive='HKLM',
  218. key=FAKE_KEY,
  219. ),
  220. expected
  221. )
  222. finally:
  223. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  224. @destructiveTest
  225. def test_set_value_unicode_key(self):
  226. '''
  227. Test the set_value function on a unicode key
  228. '''
  229. try:
  230. self.assertTrue(
  231. win_reg.set_value(
  232. hive='HKLM',
  233. key='{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
  234. vname='fake_name',
  235. vdata='fake_value'
  236. )
  237. )
  238. expected = {
  239. 'hive': 'HKLM',
  240. 'key': '{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
  241. 'success': True,
  242. 'vdata': 'fake_value',
  243. 'vname': 'fake_name',
  244. 'vtype': 'REG_SZ'
  245. }
  246. self.assertEqual(
  247. win_reg.read_value(
  248. hive='HKLM',
  249. key='{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
  250. vname='fake_name'
  251. ),
  252. expected
  253. )
  254. finally:
  255. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  256. @destructiveTest
  257. def test_set_value_unicode_value(self):
  258. '''
  259. Test the set_value function on a unicode value
  260. '''
  261. try:
  262. self.assertTrue(
  263. win_reg.set_value(
  264. hive='HKLM',
  265. key=FAKE_KEY,
  266. vname='fake_unicode',
  267. vdata=UNICODE_VALUE
  268. )
  269. )
  270. expected = {
  271. 'hive': 'HKLM',
  272. 'key': FAKE_KEY,
  273. 'success': True,
  274. 'vdata': UNICODE_VALUE,
  275. 'vname': 'fake_unicode',
  276. 'vtype': 'REG_SZ'
  277. }
  278. self.assertEqual(
  279. win_reg.read_value(
  280. hive='HKLM',
  281. key=FAKE_KEY,
  282. vname='fake_unicode'
  283. ),
  284. expected
  285. )
  286. finally:
  287. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  288. @destructiveTest
  289. def test_set_value_reg_dword(self):
  290. '''
  291. Test the set_value function on a unicode value
  292. '''
  293. try:
  294. self.assertTrue(
  295. win_reg.set_value(
  296. hive='HKLM',
  297. key=FAKE_KEY,
  298. vname='dword_value',
  299. vdata=123,
  300. vtype='REG_DWORD'
  301. )
  302. )
  303. expected = {
  304. 'hive': 'HKLM',
  305. 'key': FAKE_KEY,
  306. 'success': True,
  307. 'vdata': 123,
  308. 'vname': 'dword_value',
  309. 'vtype': 'REG_DWORD'
  310. }
  311. self.assertEqual(
  312. win_reg.read_value(
  313. hive='HKLM',
  314. key=FAKE_KEY,
  315. vname='dword_value'
  316. ),
  317. expected
  318. )
  319. finally:
  320. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  321. @destructiveTest
  322. def test_set_value_reg_qword(self):
  323. '''
  324. Test the set_value function on a unicode value
  325. '''
  326. try:
  327. self.assertTrue(
  328. win_reg.set_value(
  329. hive='HKLM',
  330. key=FAKE_KEY,
  331. vname='qword_value',
  332. vdata=123,
  333. vtype='REG_QWORD'
  334. )
  335. )
  336. expected = {
  337. 'hive': 'HKLM',
  338. 'key': FAKE_KEY,
  339. 'success': True,
  340. 'vdata': 123,
  341. 'vname': 'qword_value',
  342. 'vtype': 'REG_QWORD'
  343. }
  344. self.assertEqual(
  345. win_reg.read_value(
  346. hive='HKLM',
  347. key=FAKE_KEY,
  348. vname='qword_value'
  349. ),
  350. expected
  351. )
  352. finally:
  353. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  354. @destructiveTest
  355. def test_delete_value(self):
  356. '''
  357. Test the delete_value function
  358. '''
  359. try:
  360. self.assertTrue(
  361. win_reg.set_value(
  362. hive='HKLM',
  363. key=FAKE_KEY,
  364. vname='fake_name',
  365. vdata='fake_data'
  366. )
  367. )
  368. self.assertTrue(
  369. win_reg.delete_value(
  370. hive='HKLM',
  371. key=FAKE_KEY,
  372. vname='fake_name'
  373. )
  374. )
  375. finally:
  376. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  377. def test_delete_value_non_existing(self):
  378. '''
  379. Test the delete_value function on non existing value
  380. '''
  381. self.assertEqual(
  382. win_reg.delete_value(
  383. hive='HKLM',
  384. key=FAKE_KEY,
  385. vname='fake_name'
  386. ),
  387. None
  388. )
  389. @destructiveTest
  390. def test_delete_value_unicode(self):
  391. '''
  392. Test the delete_value function on a unicode value
  393. '''
  394. try:
  395. self.assertTrue(
  396. win_reg.set_value(
  397. hive='HKLM',
  398. key=FAKE_KEY,
  399. vname='fake_unicode',
  400. vdata=UNICODE_VALUE
  401. )
  402. )
  403. self.assertTrue(
  404. win_reg.delete_value(
  405. hive='HKLM',
  406. key=FAKE_KEY,
  407. vname='fake_unicode'
  408. )
  409. )
  410. finally:
  411. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  412. @destructiveTest
  413. def test_delete_key_unicode(self):
  414. '''
  415. Test the delete_value function on value within a unicode key
  416. '''
  417. try:
  418. self.assertTrue(
  419. win_reg.set_value(
  420. hive='HKLM',
  421. key='{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
  422. vname='fake_name',
  423. vdata='fake_value'
  424. )
  425. )
  426. expected = {
  427. 'Deleted': ['HKLM\\{0}\\{1}\\'.format(FAKE_KEY, UNICODE_KEY)],
  428. 'Failed': []
  429. }
  430. self.assertEqual(
  431. win_reg.delete_key_recursive(
  432. hive='HKLM',
  433. key='{0}\\{1}\\'.format(FAKE_KEY, UNICODE_KEY),
  434. ),
  435. expected
  436. )
  437. finally:
  438. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)