def test_delete_key_recursive_failed_to_open_key(self): ''' Test the delete_key_recursive function on failure to open the key ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='fake_name', vdata='fake_value')) expected = { 'Deleted': [], 'Failed': ['\\'.join(['HKLM', FAKE_KEY]) + ' Failed to connect to key'] } mock_true = MagicMock(return_value=True) mock_error = MagicMock(side_effect=[ 1, win32api.error(3, 'RegOpenKeyEx', 'Failed to connect to key') ]) with patch('salt.utils.win_reg.key_exists', mock_true), \ patch('salt.utils.win_reg.win32api.RegOpenKeyEx', mock_error): self.assertDictEqual( reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_read_value_multi_sz_empty_list(self): """ An empty REG_MULTI_SZ value should return an empty list, not None """ try: self.assertTrue( reg.set_value( hive="HKLM", key=FAKE_KEY, vname="empty_list", vdata=[], vtype="REG_MULTI_SZ", )) expected = { "hive": "HKLM", "key": FAKE_KEY, "success": True, "vdata": [], "vname": "empty_list", "vtype": "REG_MULTI_SZ", } self.assertEqual( reg.read_value( hive="HKLM", key=FAKE_KEY, vname="empty_list", ), expected, ) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_delete_key_recursive_unicode(self): """ Test the delete_key_recursive function on value within a unicode key """ try: self.assertTrue( reg.set_value( hive="HKLM", key="\\".join([FAKE_KEY, UNICODE_KEY]), vname="fake_name", vdata="fake_value", ) ) expected = { "Deleted": ["\\".join(["HKLM", FAKE_KEY, UNICODE_KEY])], "Failed": [], } self.assertDictEqual( reg.delete_key_recursive( hive="HKLM", key="\\".join([FAKE_KEY, UNICODE_KEY]) ), expected, ) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_read_value_multi_sz_empty_list(self): ''' An empty REG_MULTI_SZ value should return an empty list, not None ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='empty_list', vdata=[], vtype='REG_MULTI_SZ')) expected = { 'hive': 'HKLM', 'key': FAKE_KEY, 'success': True, 'vdata': [], 'vname': 'empty_list', 'vtype': 'REG_MULTI_SZ' } self.assertEqual( reg.read_value( hive='HKLM', key=FAKE_KEY, vname='empty_list', ), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_set_value_unicode_value(self): """ Test the set_value function on a unicode value """ try: self.assertTrue( 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( reg.read_value(hive="HKLM", key=FAKE_KEY, vname="fake_unicode"), expected, ) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_delete_key_recursive_failed_to_open_key(self): """ Test the delete_key_recursive function on failure to open the key """ try: self.assertTrue( reg.set_value(hive="HKLM", key=FAKE_KEY, vname="fake_name", vdata="fake_value")) expected = { "Deleted": [], "Failed": ["\\".join(["HKLM", FAKE_KEY]) + " Failed to connect to key"], } mock_true = MagicMock(return_value=True) mock_error = MagicMock(side_effect=[ 1, win32api.error(3, "RegOpenKeyEx", "Failed to connect to key"), ]) with patch("salt.utils.win_reg.key_exists", mock_true), patch( "salt.utils.win_reg.win32api.RegOpenKeyEx", mock_error): self.assertDictEqual( reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_set_value_reg_qword(self): """ Test the set_value function on a REG_QWORD value """ try: self.assertTrue( 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( reg.read_value(hive="HKLM", key=FAKE_KEY, vname="qword_value"), expected) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_delete_value_unicode_vname(self): ''' Test the delete_value function on a unicode vname ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname=UNICODE_KEY, vdata='junk data')) self.assertTrue( reg.delete_value(hive='HKLM', key=FAKE_KEY, vname=UNICODE_KEY)) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_delete_value(self): ''' Test the delete_value function ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='fake_name', vdata='fake_data')) self.assertTrue( reg.delete_value(hive='HKLM', key=FAKE_KEY, vname='fake_name')) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_delete_value_unicode_vname(self): """ Test the delete_value function on a unicode vname """ try: self.assertTrue( reg.set_value(hive="HKLM", key=FAKE_KEY, vname=UNICODE_KEY, vdata="junk data")) self.assertTrue( reg.delete_value(hive="HKLM", key=FAKE_KEY, vname=UNICODE_KEY)) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_delete_value(self): """ Test the delete_value function """ try: self.assertTrue( reg.set_value(hive="HKLM", key=FAKE_KEY, vname="fake_name", vdata="fake_data")) self.assertTrue( reg.delete_value(hive="HKLM", key=FAKE_KEY, vname="fake_name")) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_delete_value_unicode_key(self): ''' Test the delete_value function on a unicode key ''' try: self.assertTrue( reg.set_value(hive='HKLM', key='\\'.join([FAKE_KEY, UNICODE_KEY]), vname='fake_name', vdata='junk data')) self.assertTrue( reg.delete_value(hive='HKLM', key='\\'.join([FAKE_KEY, UNICODE_KEY]), vname='fake_name')) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_delete_key_recursive_key_not_found(self): """ Test the delete_key_recursive function when the passed key to delete is not found. """ self.assertFalse(reg.key_exists(hive="HKLM", key=FAKE_KEY)) self.assertFalse(reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY))
def test_delete_key_recursive(self): """ Test the delete_key_recursive function """ try: self.assertTrue( reg.set_value( hive="HKLM", key=FAKE_KEY, vname="fake_name", vdata="fake_value" ) ) expected = {"Deleted": ["\\".join(["HKLM", FAKE_KEY])], "Failed": []} self.assertDictEqual( reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY), expected ) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_del_key_recursive_user(self): ''' Test - Create directly key/value pair and Delete recusivly with salt ''' subkey = 'Software\\SaltStackTest' vname = UNICODE_TEST_KEY_DEL vdata = 'I will be deleted recursive' if PY2: handle = _winreg.CreateKeyEx( _winreg.HKEY_CURRENT_USER, subkey.encode('mbcs'), 0, _winreg.KEY_ALL_ACCESS ) _winreg.SetValueEx( handle, vname.encode('mbcs'), 0, _winreg.REG_SZ, vdata.encode('mbcs') ) else: handle = _winreg.CreateKeyEx( _winreg.HKEY_CURRENT_USER, subkey, 0, _winreg.KEY_ALL_ACCESS ) _winreg.SetValueEx(handle, vname, 0, _winreg.REG_SZ, vdata) _winreg.CloseKey(handle) # time.sleep(15) # delays for 15 seconds so you can run regedit & watch it happen test_success = win_mod_reg.delete_key_recursive('HKEY_CURRENT_USER', subkey) self.assertTrue(test_success)
def test_del_key_recursive_machine(self): ''' This is a DESTRUCTIVE TEST it creates a new registry entry. And then destroys the registry entry recusively , however it is completed in its own space within the registry. We mark this as destructiveTest as it has the potential to detroy a machine if salt reg code has a large error in it. ''' subkey = 'Software\\SaltStackTest' vname = UNICODE_TEST_KEY_DEL vdata = 'I will be deleted recursive' if PY2: handle = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE, subkey.encode('mbcs'), 0, _winreg.KEY_ALL_ACCESS) _winreg.SetValueEx(handle, vname.encode('mbcs'), 0, _winreg.REG_SZ, vdata.encode('mbcs')) else: handle = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE, subkey, 0, _winreg.KEY_ALL_ACCESS) _winreg.SetValueEx(handle, vname, 0, _winreg.REG_SZ, vdata) _winreg.CloseKey(handle) # time.sleep(15) # delays for 15 seconds so you can run regedit and watch it happen test_success = win_mod_reg.delete_key_recursive( 'HKEY_LOCAL_MACHINE', subkey) self.assertTrue(test_success)
def test_delete_key_recursive_too_close(self): """ Test the delete_key_recursive function when the passed key to delete is too close to root, such as """ mock_true = MagicMock(return_value=True) with patch("salt.utils.win_reg.key_exists", mock_true): self.assertFalse(reg.delete_key_recursive(hive="HKLM", key="FAKE_KEY"))
def test_delete_key_recursive_too_close(self): ''' Test the delete_key_recursive function when the passed key to delete is too close to root, such as ''' mock_true = MagicMock(return_value=True) with patch('salt.utils.win_reg.key_exists', mock_true): self.assertFalse( reg.delete_key_recursive(hive='HKLM', key='FAKE_KEY'))
def test_delete_key_recursive(self): ''' Test the delete_key_recursive function ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='fake_name', vdata='fake_value')) expected = { 'Deleted': ['\\'.join(['HKLM', FAKE_KEY])], 'Failed': [] } self.assertDictEqual( reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_set_value_default(self): """ Test the set_value function on the default value """ try: self.assertTrue( 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(reg.read_value(hive="HKLM", key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_delete_value_unicode_key(self): """ Test the delete_value function on a unicode key """ try: self.assertTrue( reg.set_value( hive="HKLM", key="\\".join([FAKE_KEY, UNICODE_KEY]), vname="fake_name", vdata="junk data", )) self.assertTrue( reg.delete_value( hive="HKLM", key="\\".join([FAKE_KEY, UNICODE_KEY]), vname="fake_name", )) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_set_value_default(self): ''' Test the set_value function on the default value ''' try: self.assertTrue( 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(reg.read_value(hive='HKLM', key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_delete_key_recursive_failed_to_delete(self): ''' Test the delete_key_recursive function on failure to delete a key ''' try: self.assertTrue( reg.set_value(hive='HKLM', key=FAKE_KEY, vname='fake_name', vdata='fake_value')) expected = { 'Deleted': [], 'Failed': ['\\'.join(['HKLM', FAKE_KEY]) + ' Unknown error'] } mock_error = MagicMock(side_effect=WindowsError('Unknown error')) # pylint: disable=undefined-variable with patch('salt.utils.win_reg.win32api.RegDeleteKey', mock_error): self.assertDictEqual( reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_delete_key_recursive_failed_to_delete(self): """ Test the delete_key_recursive function on failure to delete a key """ try: self.assertTrue( reg.set_value(hive="HKLM", key=FAKE_KEY, vname="fake_name", vdata="fake_value")) expected = { "Deleted": [], "Failed": ["\\".join(["HKLM", FAKE_KEY]) + " Unknown error"], } # pylint: disable=undefined-variable mock_error = MagicMock(side_effect=WindowsError("Unknown error")) # pylint: enable=undefined-variable with patch("salt.utils.win_reg.win32api.RegDeleteKey", mock_error): self.assertDictEqual( reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY), expected) finally: reg.delete_key_recursive(hive="HKLM", key=FAKE_KEY)
def test_set_value_unicode_value(self): ''' Test the set_value function on a unicode value ''' try: self.assertTrue( 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( reg.read_value(hive='HKLM', key=FAKE_KEY, vname='fake_unicode'), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_set_value_reg_qword(self): ''' Test the set_value function on a REG_QWORD value ''' try: self.assertTrue( 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( reg.read_value(hive='HKLM', key=FAKE_KEY, vname='qword_value'), expected) finally: reg.delete_key_recursive(hive='HKLM', key=FAKE_KEY)
def test_del_key_recursive_machine(self): ''' This is a DESTRUCTIVE TEST it creates a new registry entry. And then destroys the registry entry recusively , however it is completed in its own space within the registry. We mark this as destructiveTest as it has the potential to detroy a machine if salt reg code has a large error in it. ''' subkey = 'Software\\SaltStackTest' vname = UNICODE_TEST_KEY_DEL vdata = 'I will be deleted recursive' if PY2: handle = _winreg.CreateKeyEx( _winreg.HKEY_LOCAL_MACHINE, subkey.encode('mbcs'), 0, _winreg.KEY_ALL_ACCESS ) _winreg.SetValueEx( handle, vname.encode('mbcs'), 0, _winreg.REG_SZ, vdata.encode('mbcs') ) else: handle = _winreg.CreateKeyEx( _winreg.HKEY_LOCAL_MACHINE, subkey, 0, _winreg.KEY_ALL_ACCESS ) _winreg.SetValueEx(handle, vname, 0, _winreg.REG_SZ, vdata) _winreg.CloseKey(handle) # time.sleep(15) # delays for 15 seconds so you can run regedit and watch it happen test_success = win_mod_reg.delete_key_recursive('HKEY_LOCAL_MACHINE', subkey) self.assertTrue(test_success)