123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- # -*- coding: utf-8 -*-
- # Import Python Libs
- from __future__ import absolute_import, unicode_literals, print_function
- import pytest
- # Import Salt Testing Libs
- from tests.support.helpers import generate_random_name
- from tests.support.mock import patch
- from tests.support.unit import TestCase, skipIf
- # Import Salt Libs
- import salt.utils.platform
- import salt.utils.win_reg as win_reg
- UNICODE_KEY = 'Unicode Key \N{TRADE MARK SIGN}'
- UNICODE_VALUE = 'Unicode Value ' \
- '\N{COPYRIGHT SIGN},\N{TRADE MARK SIGN},\N{REGISTERED SIGN}'
- FAKE_KEY = 'SOFTWARE\\{0}'.format(generate_random_name('SaltTesting-'))
- @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
- class WinFunctionsTestCase(TestCase):
- '''
- Test cases for salt.utils.win_reg
- '''
- def test_broadcast_change_success(self):
- '''
- Tests the broadcast_change function
- '''
- with patch('win32gui.SendMessageTimeout', return_value=('', 0)):
- self.assertEqual(win_reg.broadcast_change(), True)
- def test_broadcast_change_fail(self):
- '''
- Tests the broadcast_change function failure
- '''
- with patch('win32gui.SendMessageTimeout', return_value=('', 1)):
- self.assertEqual(win_reg.broadcast_change(), False)
- def test_key_exists_existing(self):
- '''
- Tests the key exists function using a well known registry key
- '''
- self.assertEqual(
- win_reg.key_exists(
- hive='HKLM',
- key='SOFTWARE\\Microsoft'
- ),
- True
- )
- def test_key_exists_non_existing(self):
- '''
- Tests the key exists function using a non existing registry key
- '''
- self.assertEqual(
- win_reg.key_exists(
- hive='HKLM',
- key=FAKE_KEY
- ),
- False
- )
- def test_list_keys_existing(self):
- '''
- Test the list_keys function using a well known registry key
- '''
- self.assertIn(
- 'Microsoft',
- win_reg.list_keys(
- hive='HKLM',
- key='SOFTWARE'
- )
- )
- def test_list_keys_non_existing(self):
- '''
- Test the list_keys function using a non existing registry key
- '''
- expected = (False, 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY))
- self.assertEqual(
- win_reg.list_keys(
- hive='HKLM',
- key=FAKE_KEY
- ),
- expected
- )
- def test_list_values_existing(self):
- '''
- Test the list_values function using a well known registry key
- '''
- values = win_reg.list_values(
- hive='HKLM',
- key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
- )
- keys = []
- for value in values:
- keys.append(value['vname'])
- self.assertIn('ProgramFilesDir', keys)
- def test_list_values_non_existing(self):
- '''
- Test the list_values function using a non existing registry key
- '''
- expected = (False, 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY))
- self.assertEqual(
- win_reg.list_values(
- hive='HKLM',
- key=FAKE_KEY
- ),
- expected
- )
- def test_read_value_existing(self):
- '''
- Test the read_value function using a well known registry value
- '''
- ret = win_reg.read_value(
- hive='HKLM',
- key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
- vname='ProgramFilesPath'
- )
- self.assertEqual(ret['vdata'], '%ProgramFiles%')
- def test_read_value_default(self):
- '''
- Test the read_value function reading the default value using a well
- known registry key
- '''
- ret = win_reg.read_value(
- hive='HKLM',
- key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
- )
- self.assertEqual(ret['vdata'], '(value not set)')
- def test_read_value_non_existing(self):
- '''
- Test the read_value function using a non existing value pair
- '''
- expected = {
- 'comment': 'Cannot find fake_name in HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
- 'vdata': None,
- 'vname': 'fake_name',
- 'success': False,
- 'hive': 'HKLM',
- 'key': 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key='SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
- vname='fake_name'
- ),
- expected
- )
- def test_read_value_non_existing_key(self):
- '''
- Test the read_value function using a non existing registry key
- '''
- expected = {
- 'comment': 'Cannot find key: HKLM\\{0}'.format(FAKE_KEY),
- 'vdata': None,
- 'vname': 'fake_name',
- 'success': False,
- 'hive': 'HKLM',
- 'key': FAKE_KEY
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_name'
- ),
- expected
- )
- @pytest.mark.destructive_test
- def test_set_value(self):
- '''
- Test the set_value function
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_name',
- vdata='fake_data'
- )
- )
- expected = {
- 'hive': 'HKLM',
- 'key': FAKE_KEY,
- 'success': True,
- 'vdata': 'fake_data',
- 'vname': 'fake_name',
- 'vtype': 'REG_SZ'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_name'
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_set_value_default(self):
- '''
- Test the set_value function on the default value
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vdata='fake_default_data'
- )
- )
- expected = {
- 'hive': 'HKLM',
- 'key': FAKE_KEY,
- 'success': True,
- 'vdata': 'fake_default_data',
- 'vname': '(Default)',
- 'vtype': 'REG_SZ'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key=FAKE_KEY,
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_set_value_unicode_key(self):
- '''
- Test the set_value function on a unicode key
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key='{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
- vname='fake_name',
- vdata='fake_value'
- )
- )
- expected = {
- 'hive': 'HKLM',
- 'key': '{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
- 'success': True,
- 'vdata': 'fake_value',
- 'vname': 'fake_name',
- 'vtype': 'REG_SZ'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key='{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
- vname='fake_name'
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_set_value_unicode_value(self):
- '''
- Test the set_value function on a unicode value
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_unicode',
- vdata=UNICODE_VALUE
- )
- )
- expected = {
- 'hive': 'HKLM',
- 'key': FAKE_KEY,
- 'success': True,
- 'vdata': UNICODE_VALUE,
- 'vname': 'fake_unicode',
- 'vtype': 'REG_SZ'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_unicode'
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_set_value_reg_dword(self):
- '''
- Test the set_value function on a unicode value
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='dword_value',
- vdata=123,
- vtype='REG_DWORD'
- )
- )
- expected = {
- 'hive': 'HKLM',
- 'key': FAKE_KEY,
- 'success': True,
- 'vdata': 123,
- 'vname': 'dword_value',
- 'vtype': 'REG_DWORD'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='dword_value'
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_set_value_reg_qword(self):
- '''
- Test the set_value function on a unicode value
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='qword_value',
- vdata=123,
- vtype='REG_QWORD'
- )
- )
- expected = {
- 'hive': 'HKLM',
- 'key': FAKE_KEY,
- 'success': True,
- 'vdata': 123,
- 'vname': 'qword_value',
- 'vtype': 'REG_QWORD'
- }
- self.assertEqual(
- win_reg.read_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='qword_value'
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_delete_value(self):
- '''
- Test the delete_value function
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_name',
- vdata='fake_data'
- )
- )
- self.assertTrue(
- win_reg.delete_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_name'
- )
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- def test_delete_value_non_existing(self):
- '''
- Test the delete_value function on non existing value
- '''
- self.assertEqual(
- win_reg.delete_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_name'
- ),
- None
- )
- @pytest.mark.destructive_test
- def test_delete_value_unicode(self):
- '''
- Test the delete_value function on a unicode value
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_unicode',
- vdata=UNICODE_VALUE
- )
- )
- self.assertTrue(
- win_reg.delete_value(
- hive='HKLM',
- key=FAKE_KEY,
- vname='fake_unicode'
- )
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
- @pytest.mark.destructive_test
- def test_delete_key_unicode(self):
- '''
- Test the delete_value function on value within a unicode key
- '''
- try:
- self.assertTrue(
- win_reg.set_value(
- hive='HKLM',
- key='{0}\\{1}'.format(FAKE_KEY, UNICODE_KEY),
- vname='fake_name',
- vdata='fake_value'
- )
- )
- expected = {
- 'Deleted': ['HKLM\\{0}\\{1}\\'.format(FAKE_KEY, UNICODE_KEY)],
- 'Failed': []
- }
- self.assertEqual(
- win_reg.delete_key_recursive(
- hive='HKLM',
- key='{0}\\{1}\\'.format(FAKE_KEY, UNICODE_KEY),
- ),
- expected
- )
- finally:
- win_reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
|