-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcritical_processes.py
66 lines (56 loc) · 2.68 KB
/
critical_processes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# List of critical Windows processes that should not be suspended or killed
CRITICAL_PROCESSES = [
"System",
"smss.exe",
"csrss.exe",
"wininit.exe",
"services.exe",
"lsass.exe",
"winlogon.exe",
"explorer.exe",
"svchost.exe",
"taskmgr.exe",
"dwm.exe",
"conhost.exe",
"rundll32.exe",
]
"""import unittest
from unittest.mock import patch, MagicMock
from window_handler import HotkeyHandler
class TestCriticalProcesses(unittest.TestCase):
def setUp(self):
self.hotkey_handler = HotkeyHandler()
@patch('window_handler.get_focused_window_pid')
@patch('process_killer.get_process_name')
def test_kill_critical_process(self, mock_get_process_name, mock_get_focused_window_pid):
mock_get_focused_window_pid.return_value = 123
mock_get_process_name.return_value = 'System'
with self.assertLogs('window_handler', level='WARNING') as cm:
self.hotkey_handler.on_kill_hotkey()
self.assertIn('Attempted to kill critical process System (PID 123). Operation aborted.', cm.output[0])
@patch('window_handler.get_focused_window_pid')
@patch('process_killer.get_process_name')
def test_suspend_critical_process(self, mock_get_process_name, mock_get_focused_window_pid):
mock_get_focused_window_pid.return_value = 456
mock_get_process_name.return_value = 'explorer.exe'
with self.assertLogs('window_handler', level='WARNING') as cm:
self.hotkey_handler.on_suspend_hotkey()
self.assertIn('Attempted to suspend critical process explorer.exe (PID 456). Operation aborted.', cm.output[0])
@patch('window_handler.get_focused_window_pid')
@patch('process_killer.get_process_name')
@patch('process_killer.force_kill_process')
def test_kill_non_critical_process(self, mock_force_kill_process, mock_get_process_name, mock_get_focused_window_pid):
mock_get_focused_window_pid.return_value = 789
mock_get_process_name.return_value = 'non_critical_process.exe'
self.hotkey_handler.on_kill_hotkey()
mock_force_kill_process.assert_called_once_with(789)
@patch('window_handler.get_focused_window_pid')
@patch('process_killer.get_process_name')
@patch('process_killer.suspend_process')
def test_suspend_non_critical_process(self, mock_suspend_process, mock_get_process_name, mock_get_focused_window_pid):
mock_get_focused_window_pid.return_value = 101
mock_get_process_name.return_value = 'non_critical_process.exe'
self.hotkey_handler.on_suspend_hotkey()
mock_suspend_process.assert_called_once_with(101)
if __name__ == '__main__':
unittest.main()"""