1
0

test_win_reg.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 patch, MagicMock
  7. from tests.support.unit import TestCase, skipIf
  8. # Import Salt Libs
  9. import salt.utils.stringutils
  10. import salt.utils.win_reg as win_reg
  11. from salt.exceptions import CommandExecutionError
  12. from salt.ext import six
  13. try:
  14. import win32api
  15. HAS_WIN32 = True
  16. except ImportError:
  17. HAS_WIN32 = False
  18. UNICODE_KEY = '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 = '\\'.join(['SOFTWARE', generate_random_name('SaltTesting-')])
  22. @skipIf(not HAS_WIN32, 'Tests require win32 libraries')
  23. class WinFunctionsTestCase(TestCase):
  24. '''
  25. Test cases for salt.utils.win_reg
  26. '''
  27. def test_broadcast_change_success(self):
  28. '''
  29. Tests the broadcast_change function
  30. '''
  31. with patch('win32gui.SendMessageTimeout', return_value=('', 0)):
  32. self.assertTrue(win_reg.broadcast_change())
  33. def test_broadcast_change_fail(self):
  34. '''
  35. Tests the broadcast_change function failure
  36. '''
  37. with patch('win32gui.SendMessageTimeout', return_value=('', 1)):
  38. self.assertFalse(win_reg.broadcast_change())
  39. def test_key_exists_existing(self):
  40. '''
  41. Tests the key_exists function using a well known registry key
  42. '''
  43. self.assertTrue(
  44. win_reg.key_exists(hive='HKLM', key='SOFTWARE\\Microsoft'))
  45. def test_key_exists_non_existing(self):
  46. '''
  47. Tests the key_exists function using a non existing registry key
  48. '''
  49. self.assertFalse(win_reg.key_exists(hive='HKLM', key=FAKE_KEY))
  50. def test_key_exists_invalid_hive(self):
  51. '''
  52. Tests the key_exists function using an invalid hive
  53. '''
  54. self.assertRaises(CommandExecutionError,
  55. win_reg.key_exists,
  56. hive='BADHIVE',
  57. key='SOFTWARE\\Microsoft')
  58. def test_key_exists_unknown_key_error(self):
  59. '''
  60. Tests the key_exists function with an unknown key error
  61. '''
  62. mock_error = MagicMock(
  63. side_effect=win32api.error(123, 'RegOpenKeyEx', 'Unknown error'))
  64. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  65. self.assertRaises(win32api.error,
  66. win_reg.key_exists,
  67. hive='HKLM',
  68. key='SOFTWARE\\Microsoft')
  69. def test_value_exists_existing(self):
  70. '''
  71. Tests the value_exists function using a well known registry key
  72. '''
  73. self.assertTrue(
  74. win_reg.value_exists(
  75. hive='HKLM',
  76. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  77. vname='CommonFilesDir'))
  78. def test_value_exists_non_existing(self):
  79. '''
  80. Tests the value_exists function using a non existing registry key
  81. '''
  82. self.assertFalse(
  83. win_reg.value_exists(
  84. hive='HKLM',
  85. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  86. vname='NonExistingValueName'))
  87. def test_value_exists_invalid_hive(self):
  88. '''
  89. Tests the value_exists function using an invalid hive
  90. '''
  91. self.assertRaises(CommandExecutionError,
  92. win_reg.value_exists,
  93. hive='BADHIVE',
  94. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  95. vname='CommonFilesDir')
  96. def test_value_exists_key_not_exist(self):
  97. '''
  98. Tests the value_exists function when the key does not exist
  99. '''
  100. mock_error = MagicMock(
  101. side_effect=win32api.error(2, 'RegOpenKeyEx', 'Unknown error'))
  102. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  103. self.assertFalse(
  104. win_reg.value_exists(
  105. hive='HKLM',
  106. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  107. vname='CommonFilesDir'))
  108. def test_value_exists_unknown_key_error(self):
  109. '''
  110. Tests the value_exists function with an unknown error when opening the
  111. key
  112. '''
  113. mock_error = MagicMock(
  114. side_effect=win32api.error(123, 'RegOpenKeyEx', 'Unknown error'))
  115. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  116. self.assertRaises(
  117. win32api.error,
  118. win_reg.value_exists,
  119. hive='HKLM',
  120. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  121. vname='CommonFilesDir')
  122. def test_value_exists_empty_default_value(self):
  123. '''
  124. Tests the value_exists function when querying the default value
  125. '''
  126. mock_error = MagicMock(
  127. side_effect=win32api.error(2, 'RegQueryValueEx', 'Empty Value'))
  128. with patch('salt.utils.win_reg.win32api.RegQueryValueEx', mock_error):
  129. self.assertTrue(
  130. win_reg.value_exists(
  131. hive='HKLM',
  132. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  133. vname=None))
  134. def test_value_exists_no_vname(self):
  135. '''
  136. Tests the value_exists function when the vname does not exist
  137. '''
  138. mock_error = MagicMock(
  139. side_effect=win32api.error(123, 'RegQueryValueEx', 'Empty Value'))
  140. with patch('salt.utils.win_reg.win32api.RegQueryValueEx', mock_error):
  141. self.assertFalse(
  142. win_reg.value_exists(
  143. hive='HKLM',
  144. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  145. vname='NonExistingValuePair'))
  146. def test_list_keys_existing(self):
  147. '''
  148. Test the list_keys function using a well known registry key
  149. '''
  150. self.assertIn(
  151. 'Microsoft',
  152. win_reg.list_keys(hive='HKLM', key='SOFTWARE'))
  153. def test_list_keys_non_existing(self):
  154. '''
  155. Test the list_keys function using a non existing registry key
  156. '''
  157. expected = (False, 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY))
  158. self.assertEqual(win_reg.list_keys(hive='HKLM', key=FAKE_KEY), expected)
  159. def test_list_keys_invalid_hive(self):
  160. '''
  161. Test the list_keys function when passing an invalid hive
  162. '''
  163. self.assertRaises(CommandExecutionError,
  164. win_reg.list_keys,
  165. hive='BADHIVE',
  166. key='SOFTWARE\\Microsoft')
  167. def test_list_keys_unknown_key_error(self):
  168. '''
  169. Tests the list_keys function with an unknown key error
  170. '''
  171. mock_error = MagicMock(
  172. side_effect=win32api.error(123, 'RegOpenKeyEx', 'Unknown error'))
  173. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  174. self.assertRaises(win32api.error,
  175. win_reg.list_keys,
  176. hive='HKLM',
  177. key='SOFTWARE\\Microsoft')
  178. def test_list_values_existing(self):
  179. '''
  180. Test the list_values function using a well known registry key
  181. '''
  182. values = win_reg.list_values(
  183. hive='HKLM',
  184. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion')
  185. keys = []
  186. for value in values:
  187. keys.append(value['vname'])
  188. self.assertIn('ProgramFilesDir', keys)
  189. def test_list_values_non_existing(self):
  190. '''
  191. Test the list_values function using a non existing registry key
  192. '''
  193. expected = (False, 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY))
  194. self.assertEqual(win_reg.list_values(hive='HKLM', key=FAKE_KEY),
  195. expected)
  196. def test_list_values_invalid_hive(self):
  197. '''
  198. Test the list_values function when passing an invalid hive
  199. '''
  200. self.assertRaises(CommandExecutionError,
  201. win_reg.list_values,
  202. hive='BADHIVE',
  203. key='SOFTWARE\\Microsoft')
  204. def test_list_values_unknown_key_error(self):
  205. '''
  206. Tests the list_values function with an unknown key error
  207. '''
  208. mock_error = MagicMock(
  209. side_effect=win32api.error(123, 'RegOpenKeyEx', 'Unknown error'))
  210. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  211. self.assertRaises(win32api.error,
  212. win_reg.list_values,
  213. hive='HKLM',
  214. key='SOFTWARE\\Microsoft')
  215. def test_read_value_existing(self):
  216. '''
  217. Test the read_value function using a well known registry value
  218. '''
  219. ret = win_reg.read_value(
  220. hive='HKLM',
  221. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  222. vname='ProgramFilesPath')
  223. self.assertEqual(ret['vdata'], '%ProgramFiles%')
  224. def test_read_value_default(self):
  225. '''
  226. Test the read_value function reading the default value using a well
  227. known registry key
  228. '''
  229. ret = win_reg.read_value(
  230. hive='HKLM',
  231. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion')
  232. self.assertEqual(ret['vdata'], '(value not set)')
  233. def test_read_value_non_existing(self):
  234. '''
  235. Test the read_value function using a non existing value pair
  236. '''
  237. expected = {
  238. 'comment': 'Cannot find fake_name in HKLM\\SOFTWARE\\Microsoft\\'
  239. 'Windows\\CurrentVersion',
  240. 'vdata': None,
  241. 'vname': 'fake_name',
  242. 'success': False,
  243. 'hive': 'HKLM',
  244. 'key': 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'}
  245. self.assertDictEqual(
  246. win_reg.read_value(
  247. hive='HKLM',
  248. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  249. vname='fake_name'),
  250. expected)
  251. def test_read_value_non_existing_key(self):
  252. '''
  253. Test the read_value function using a non existing registry key
  254. '''
  255. expected = {
  256. 'comment': 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY),
  257. 'vdata': None,
  258. 'vname': 'fake_name',
  259. 'success': False,
  260. 'hive': 'HKLM',
  261. 'key': FAKE_KEY}
  262. self.assertDictEqual(
  263. win_reg.read_value(
  264. hive='HKLM',
  265. key=FAKE_KEY,
  266. vname='fake_name'),
  267. expected)
  268. def test_read_value_invalid_hive(self):
  269. '''
  270. Test the read_value function when passing an invalid hive
  271. '''
  272. self.assertRaises(CommandExecutionError,
  273. win_reg.read_value,
  274. hive='BADHIVE',
  275. key='SOFTWARE\\Microsoft',
  276. vname='ProgramFilesPath')
  277. def test_read_value_unknown_key_error(self):
  278. '''
  279. Tests the read_value function with an unknown key error
  280. '''
  281. mock_error = MagicMock(
  282. side_effect=win32api.error(123, 'RegOpenKeyEx', 'Unknown error'))
  283. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  284. self.assertRaises(
  285. win32api.error,
  286. win_reg.read_value,
  287. hive='HKLM',
  288. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  289. vname='ProgramFilesPath')
  290. def test_read_value_unknown_value_error(self):
  291. '''
  292. Tests the read_value function with an unknown value error
  293. '''
  294. mock_error = MagicMock(
  295. side_effect=win32api.error(123, 'RegQueryValueEx', 'Unknown error'))
  296. with patch('salt.utils.win_reg.win32api.RegQueryValueEx', mock_error):
  297. self.assertRaises(
  298. win32api.error,
  299. win_reg.read_value,
  300. hive='HKLM',
  301. key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
  302. vname='ProgramFilesPath')
  303. @destructiveTest
  304. def test_read_value_multi_sz_empty_list(self):
  305. '''
  306. An empty REG_MULTI_SZ value should return an empty list, not None
  307. '''
  308. try:
  309. self.assertTrue(
  310. win_reg.set_value(
  311. hive='HKLM',
  312. key=FAKE_KEY,
  313. vname='empty_list',
  314. vdata=[],
  315. vtype='REG_MULTI_SZ'
  316. )
  317. )
  318. expected = {
  319. 'hive': 'HKLM',
  320. 'key': FAKE_KEY,
  321. 'success': True,
  322. 'vdata': [],
  323. 'vname': 'empty_list',
  324. 'vtype': 'REG_MULTI_SZ'
  325. }
  326. self.assertEqual(
  327. win_reg.read_value(
  328. hive='HKLM',
  329. key=FAKE_KEY,
  330. vname='empty_list',
  331. ),
  332. expected
  333. )
  334. finally:
  335. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  336. @destructiveTest
  337. def test_set_value(self):
  338. '''
  339. Test the set_value function
  340. '''
  341. try:
  342. self.assertTrue(
  343. win_reg.set_value(
  344. hive='HKLM',
  345. key=FAKE_KEY,
  346. vname='fake_name',
  347. vdata='fake_data'))
  348. expected = {
  349. 'hive': 'HKLM',
  350. 'key': FAKE_KEY,
  351. 'success': True,
  352. 'vdata': 'fake_data',
  353. 'vname': 'fake_name',
  354. 'vtype': 'REG_SZ'}
  355. self.assertEqual(
  356. win_reg.read_value(
  357. hive='HKLM',
  358. key=FAKE_KEY,
  359. vname='fake_name'),
  360. expected)
  361. finally:
  362. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  363. @destructiveTest
  364. def test_set_value_default(self):
  365. '''
  366. Test the set_value function on the default value
  367. '''
  368. try:
  369. self.assertTrue(
  370. win_reg.set_value(
  371. hive='HKLM',
  372. key=FAKE_KEY,
  373. vdata='fake_default_data'))
  374. expected = {
  375. 'hive': 'HKLM',
  376. 'key': FAKE_KEY,
  377. 'success': True,
  378. 'vdata': 'fake_default_data',
  379. 'vname': '(Default)',
  380. 'vtype': 'REG_SZ'}
  381. self.assertEqual(
  382. win_reg.read_value(
  383. hive='HKLM',
  384. key=FAKE_KEY),
  385. expected)
  386. finally:
  387. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  388. @destructiveTest
  389. def test_set_value_unicode_key(self):
  390. '''
  391. Test the set_value function on a unicode key
  392. '''
  393. try:
  394. self.assertTrue(
  395. win_reg.set_value(
  396. hive='HKLM',
  397. key='\\'.join([FAKE_KEY, UNICODE_KEY]),
  398. vname='fake_name',
  399. vdata='fake_value'))
  400. expected = {
  401. 'hive': 'HKLM',
  402. 'key': '\\'.join([FAKE_KEY, UNICODE_KEY]),
  403. 'success': True,
  404. 'vdata': 'fake_value',
  405. 'vname': 'fake_name',
  406. 'vtype': 'REG_SZ'}
  407. self.assertEqual(
  408. win_reg.read_value(
  409. hive='HKLM',
  410. key='\\'.join([FAKE_KEY, UNICODE_KEY]),
  411. vname='fake_name'),
  412. expected)
  413. finally:
  414. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  415. @destructiveTest
  416. def test_set_value_unicode_value(self):
  417. '''
  418. Test the set_value function on a unicode value
  419. '''
  420. try:
  421. self.assertTrue(
  422. win_reg.set_value(
  423. hive='HKLM',
  424. key=FAKE_KEY,
  425. vname='fake_unicode',
  426. vdata=UNICODE_VALUE))
  427. expected = {
  428. 'hive': 'HKLM',
  429. 'key': FAKE_KEY,
  430. 'success': True,
  431. 'vdata': UNICODE_VALUE,
  432. 'vname': 'fake_unicode',
  433. 'vtype': 'REG_SZ'}
  434. self.assertEqual(
  435. win_reg.read_value(
  436. hive='HKLM',
  437. key=FAKE_KEY,
  438. vname='fake_unicode'),
  439. expected)
  440. finally:
  441. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  442. @destructiveTest
  443. def test_set_value_reg_dword(self):
  444. '''
  445. Test the set_value function on a REG_DWORD value
  446. '''
  447. try:
  448. self.assertTrue(
  449. win_reg.set_value(
  450. hive='HKLM',
  451. key=FAKE_KEY,
  452. vname='dword_value',
  453. vdata=123,
  454. vtype='REG_DWORD'))
  455. expected = {
  456. 'hive': 'HKLM',
  457. 'key': FAKE_KEY,
  458. 'success': True,
  459. 'vdata': 123,
  460. 'vname': 'dword_value',
  461. 'vtype': 'REG_DWORD'}
  462. self.assertEqual(
  463. win_reg.read_value(
  464. hive='HKLM',
  465. key=FAKE_KEY,
  466. vname='dword_value'),
  467. expected)
  468. finally:
  469. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  470. @destructiveTest
  471. def test_set_value_reg_qword(self):
  472. '''
  473. Test the set_value function on a REG_QWORD value
  474. '''
  475. try:
  476. self.assertTrue(
  477. win_reg.set_value(
  478. hive='HKLM',
  479. key=FAKE_KEY,
  480. vname='qword_value',
  481. vdata=123,
  482. vtype='REG_QWORD'))
  483. expected = {
  484. 'hive': 'HKLM',
  485. 'key': FAKE_KEY,
  486. 'success': True,
  487. 'vdata': 123,
  488. 'vname': 'qword_value',
  489. 'vtype': 'REG_QWORD'}
  490. self.assertEqual(
  491. win_reg.read_value(
  492. hive='HKLM',
  493. key=FAKE_KEY,
  494. vname='qword_value'),
  495. expected)
  496. finally:
  497. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  498. def test_set_value_invalid_hive(self):
  499. '''
  500. Test the set_value function when passing an invalid hive
  501. '''
  502. self.assertRaises(CommandExecutionError,
  503. win_reg.set_value,
  504. hive='BADHIVE',
  505. key=FAKE_KEY,
  506. vname='fake_name',
  507. vdata='fake_data')
  508. def test_set_value_open_create_failure(self):
  509. '''
  510. Test the set_value function when there is a problem opening/creating
  511. the key
  512. '''
  513. mock_error = MagicMock(
  514. side_effect=win32api.error(123, 'RegCreateKeyEx', 'Unknown error'))
  515. with patch('salt.utils.win_reg.win32api.RegCreateKeyEx', mock_error):
  516. self.assertFalse(
  517. win_reg.set_value(
  518. hive='HKLM',
  519. key=FAKE_KEY,
  520. vname='fake_name',
  521. vdata='fake_data'))
  522. def test_set_value_type_error(self):
  523. '''
  524. Test the set_value function when the wrong type of data is passed
  525. '''
  526. mock_error = MagicMock(
  527. side_effect=TypeError('Mocked TypeError'))
  528. with patch('salt.utils.win_reg.win32api.RegSetValueEx', mock_error):
  529. self.assertFalse(
  530. win_reg.set_value(
  531. hive='HKLM',
  532. key=FAKE_KEY,
  533. vname='fake_name',
  534. vdata='fake_data'))
  535. def test_set_value_system_error(self):
  536. '''
  537. Test the set_value function when a SystemError occurs while setting the
  538. value
  539. '''
  540. mock_error = MagicMock(
  541. side_effect=SystemError('Mocked SystemError'))
  542. with patch('salt.utils.win_reg.win32api.RegSetValueEx', mock_error):
  543. self.assertFalse(
  544. win_reg.set_value(
  545. hive='HKLM',
  546. key=FAKE_KEY,
  547. vname='fake_name',
  548. vdata='fake_data'))
  549. def test_set_value_value_error(self):
  550. '''
  551. Test the set_value function when a ValueError occurs while setting the
  552. value
  553. '''
  554. mock_error = MagicMock(
  555. side_effect=ValueError('Mocked ValueError'))
  556. with patch('salt.utils.win_reg.win32api.RegSetValueEx', mock_error):
  557. self.assertFalse(
  558. win_reg.set_value(
  559. hive='HKLM',
  560. key=FAKE_KEY,
  561. vname='fake_name',
  562. vdata='fake_data'))
  563. def test_cast_vdata_reg_binary(self):
  564. '''
  565. Test the cast_vdata function with REG_BINARY
  566. Should always return binary data
  567. '''
  568. vdata = salt.utils.stringutils.to_bytes('test data')
  569. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_BINARY')
  570. self.assertTrue(isinstance(result, six.binary_type))
  571. vdata = salt.utils.stringutils.to_str('test data')
  572. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_BINARY')
  573. self.assertTrue(isinstance(result, six.binary_type))
  574. vdata = salt.utils.stringutils.to_unicode('test data')
  575. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_BINARY')
  576. self.assertTrue(isinstance(result, six.binary_type))
  577. def test_cast_vdata_reg_dword(self):
  578. '''
  579. Test the cast_vdata function with REG_DWORD
  580. Should always return integer
  581. '''
  582. vdata = 1
  583. expected = 1
  584. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_DWORD')
  585. self.assertEqual(result, expected)
  586. vdata = '1'
  587. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_DWORD')
  588. self.assertEqual(result, expected)
  589. vdata = '0000001'
  590. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_DWORD')
  591. self.assertEqual(result, expected)
  592. def test_cast_vdata_reg_expand_sz(self):
  593. '''
  594. Test the cast_vdata function with REG_EXPAND_SZ
  595. Should always return unicode
  596. '''
  597. vdata = salt.utils.stringutils.to_str('test data')
  598. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_EXPAND_SZ')
  599. self.assertTrue(isinstance(result, six.text_type))
  600. vdata = salt.utils.stringutils.to_bytes('test data')
  601. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_EXPAND_SZ')
  602. self.assertTrue(isinstance(result, six.text_type))
  603. def test_cast_vdata_reg_multi_sz(self):
  604. '''
  605. Test the cast_vdata function with REG_MULTI_SZ
  606. Should always return a list of unicode strings
  607. '''
  608. vdata = [salt.utils.stringutils.to_str('test string'),
  609. salt.utils.stringutils.to_bytes('test bytes')]
  610. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_MULTI_SZ')
  611. self.assertTrue(isinstance(result, list))
  612. for item in result:
  613. self.assertTrue(isinstance(item, six.text_type))
  614. def test_cast_vdata_reg_qword(self):
  615. '''
  616. Test the cast_vdata function with REG_QWORD
  617. Should always return a long integer
  618. `int` is `long` is default on Py3
  619. '''
  620. vdata = 1
  621. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_QWORD')
  622. if six.PY2:
  623. self.assertTrue(isinstance(result, long)) # pylint: disable=incompatible-py3-code,undefined-variable
  624. else:
  625. self.assertTrue(isinstance(result, int))
  626. vdata = '1'
  627. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_QWORD')
  628. if six.PY2:
  629. self.assertTrue(isinstance(result, long)) # pylint: disable=incompatible-py3-code,undefined-variable
  630. else:
  631. self.assertTrue(isinstance(result, int))
  632. def test_cast_vdata_reg_sz(self):
  633. '''
  634. Test the cast_vdata function with REG_SZ
  635. Should always return unicode
  636. '''
  637. vdata = salt.utils.stringutils.to_str('test data')
  638. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_SZ')
  639. self.assertTrue(isinstance(result, six.text_type))
  640. vdata = salt.utils.stringutils.to_bytes('test data')
  641. result = win_reg.cast_vdata(vdata=vdata, vtype='REG_SZ')
  642. self.assertTrue(isinstance(result, six.text_type))
  643. @destructiveTest
  644. def test_delete_value(self):
  645. '''
  646. Test the delete_value function
  647. '''
  648. try:
  649. self.assertTrue(
  650. win_reg.set_value(
  651. hive='HKLM',
  652. key=FAKE_KEY,
  653. vname='fake_name',
  654. vdata='fake_data'))
  655. self.assertTrue(
  656. win_reg.delete_value(
  657. hive='HKLM',
  658. key=FAKE_KEY,
  659. vname='fake_name'))
  660. finally:
  661. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  662. def test_delete_value_non_existing(self):
  663. '''
  664. Test the delete_value function on non existing value
  665. '''
  666. mock_error = MagicMock(
  667. side_effect=win32api.error(2, 'RegOpenKeyEx', 'Unknown error'))
  668. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  669. self.assertIsNone(
  670. win_reg.delete_value(
  671. hive='HKLM',
  672. key=FAKE_KEY,
  673. vname='fake_name'))
  674. def test_delete_value_invalid_hive(self):
  675. '''
  676. Test the delete_value function when passing an invalid hive
  677. '''
  678. self.assertRaises(CommandExecutionError,
  679. win_reg.delete_value,
  680. hive='BADHIVE',
  681. key=FAKE_KEY,
  682. vname='fake_name')
  683. def test_delete_value_unknown_error(self):
  684. '''
  685. Test the delete_value function when there is a problem opening the key
  686. '''
  687. mock_error = MagicMock(
  688. side_effect=win32api.error(123, 'RegOpenKeyEx', 'Unknown error'))
  689. with patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  690. self.assertRaises(win32api.error,
  691. win_reg.delete_value,
  692. hive='HKLM',
  693. key=FAKE_KEY,
  694. vname='fake_name')
  695. @destructiveTest
  696. def test_delete_value_unicode(self):
  697. '''
  698. Test the delete_value function on a unicode value
  699. '''
  700. try:
  701. self.assertTrue(
  702. win_reg.set_value(
  703. hive='HKLM',
  704. key=FAKE_KEY,
  705. vname='fake_unicode',
  706. vdata=UNICODE_VALUE))
  707. self.assertTrue(
  708. win_reg.delete_value(
  709. hive='HKLM',
  710. key=FAKE_KEY,
  711. vname='fake_unicode'))
  712. finally:
  713. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  714. @destructiveTest
  715. def test_delete_value_unicode_vname(self):
  716. '''
  717. Test the delete_value function on a unicode vname
  718. '''
  719. try:
  720. self.assertTrue(
  721. win_reg.set_value(
  722. hive='HKLM',
  723. key=FAKE_KEY,
  724. vname=UNICODE_KEY,
  725. vdata='junk data'))
  726. self.assertTrue(
  727. win_reg.delete_value(
  728. hive='HKLM',
  729. key=FAKE_KEY,
  730. vname=UNICODE_KEY))
  731. finally:
  732. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  733. @destructiveTest
  734. def test_delete_value_unicode_key(self):
  735. '''
  736. Test the delete_value function on a unicode key
  737. '''
  738. try:
  739. self.assertTrue(
  740. win_reg.set_value(
  741. hive='HKLM',
  742. key='\\'.join([FAKE_KEY, UNICODE_KEY]),
  743. vname='fake_name',
  744. vdata='junk data'))
  745. self.assertTrue(
  746. win_reg.delete_value(
  747. hive='HKLM',
  748. key='\\'.join([FAKE_KEY, UNICODE_KEY]),
  749. vname='fake_name'))
  750. finally:
  751. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  752. def test_delete_key_recursive_invalid_hive(self):
  753. '''
  754. Test the delete_key_recursive function when passing an invalid hive
  755. '''
  756. self.assertRaises(CommandExecutionError,
  757. win_reg.delete_key_recursive,
  758. hive='BADHIVE',
  759. key=FAKE_KEY)
  760. def test_delete_key_recursive_key_not_found(self):
  761. '''
  762. Test the delete_key_recursive function when the passed key to delete is
  763. not found.
  764. '''
  765. self.assertFalse(
  766. win_reg.key_exists(hive='HKLM', key=FAKE_KEY))
  767. self.assertFalse(
  768. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY))
  769. def test_delete_key_recursive_too_close(self):
  770. '''
  771. Test the delete_key_recursive function when the passed key to delete is
  772. too close to root, such as
  773. '''
  774. mock_true = MagicMock(return_value=True)
  775. with patch('salt.utils.win_reg.key_exists', mock_true):
  776. self.assertFalse(
  777. win_reg.delete_key_recursive(hive='HKLM', key='FAKE_KEY'))
  778. @destructiveTest
  779. def test_delete_key_recursive(self):
  780. '''
  781. Test the delete_key_recursive function
  782. '''
  783. try:
  784. self.assertTrue(
  785. win_reg.set_value(
  786. hive='HKLM',
  787. key=FAKE_KEY,
  788. vname='fake_name',
  789. vdata='fake_value'))
  790. expected = {
  791. 'Deleted': ['\\'.join(['HKLM', FAKE_KEY])],
  792. 'Failed': []}
  793. self.assertDictEqual(
  794. win_reg.delete_key_recursive(
  795. hive='HKLM',
  796. key=FAKE_KEY),
  797. expected)
  798. finally:
  799. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  800. @destructiveTest
  801. def test_delete_key_recursive_failed_to_open_key(self):
  802. '''
  803. Test the delete_key_recursive function on failure to open the key
  804. '''
  805. try:
  806. self.assertTrue(
  807. win_reg.set_value(
  808. hive='HKLM',
  809. key=FAKE_KEY,
  810. vname='fake_name',
  811. vdata='fake_value'))
  812. expected = {
  813. 'Deleted': [],
  814. 'Failed': ['\\'.join(['HKLM', FAKE_KEY]) +
  815. ' Failed to connect to key']}
  816. mock_true = MagicMock(return_value=True)
  817. mock_error = MagicMock(
  818. side_effect=[
  819. 1,
  820. win32api.error(3, 'RegOpenKeyEx',
  821. 'Failed to connect to key')
  822. ])
  823. with patch('salt.utils.win_reg.key_exists', mock_true), \
  824. patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error):
  825. self.assertDictEqual(
  826. win_reg.delete_key_recursive(
  827. hive='HKLM',
  828. key=FAKE_KEY),
  829. expected)
  830. finally:
  831. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  832. @destructiveTest
  833. def test_delete_key_recursive_failed_to_delete(self):
  834. '''
  835. Test the delete_key_recursive function on failure to delete a key
  836. '''
  837. try:
  838. self.assertTrue(
  839. win_reg.set_value(
  840. hive='HKLM',
  841. key=FAKE_KEY,
  842. vname='fake_name',
  843. vdata='fake_value'))
  844. expected = {
  845. 'Deleted': [],
  846. 'Failed': ['\\'.join(['HKLM', FAKE_KEY]) + ' Unknown error']}
  847. mock_error = MagicMock(side_effect=WindowsError('Unknown error')) # pylint: disable=undefined-variable
  848. with patch('salt.utils.win_reg.win32api.RegDeleteKey', mock_error):
  849. self.assertDictEqual(
  850. win_reg.delete_key_recursive(
  851. hive='HKLM',
  852. key=FAKE_KEY),
  853. expected)
  854. finally:
  855. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  856. @destructiveTest
  857. def test_delete_key_recursive_unicode(self):
  858. '''
  859. Test the delete_key_recursive function on value within a unicode key
  860. '''
  861. try:
  862. self.assertTrue(
  863. win_reg.set_value(
  864. hive='HKLM',
  865. key='\\'.join([FAKE_KEY, UNICODE_KEY]),
  866. vname='fake_name',
  867. vdata='fake_value'))
  868. expected = {
  869. 'Deleted': ['\\'.join(['HKLM', FAKE_KEY, UNICODE_KEY])],
  870. 'Failed': []}
  871. self.assertDictEqual(
  872. win_reg.delete_key_recursive(
  873. hive='HKLM',
  874. key='\\'.join([FAKE_KEY, UNICODE_KEY])),
  875. expected)
  876. finally:
  877. win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
  878. def test__to_unicode_int(self):
  879. '''
  880. Test the ``_to_unicode`` function when it receives an integer value.
  881. Should return a unicode value, which is unicode in PY2 and str in PY3.
  882. '''
  883. if six.PY3:
  884. self.assertTrue(isinstance(win_reg._to_unicode(1), str))
  885. else:
  886. self.assertTrue(isinstance(win_reg._to_unicode(1), unicode)) # pylint: disable=incompatible-py3-code,undefined-variable