|
| 1 | +import subprocess |
| 2 | +import unittest |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import anti_cheat |
| 6 | + |
| 7 | + |
| 8 | +class AntiCheatTests(unittest.TestCase): |
| 9 | + def test_list_process_names_parses_tasklist_csv(self) -> None: |
| 10 | + sample = '"explorer.exe","1234","Console","1","11,000 K"\n"x64dbg.exe","999","Console","1","4,096 K"\n' |
| 11 | + with mock.patch.object(anti_cheat, "is_windows", return_value=True): |
| 12 | + self.assertEqual( |
| 13 | + anti_cheat.list_process_names(sample), |
| 14 | + ["explorer.exe", "x64dbg.exe"], |
| 15 | + ) |
| 16 | + |
| 17 | + def test_list_process_names_returns_empty_off_windows(self) -> None: |
| 18 | + with mock.patch.object(anti_cheat, "is_windows", return_value=False): |
| 19 | + self.assertEqual(anti_cheat.list_process_names(""), []) |
| 20 | + |
| 21 | + def test_debugger_attached_uses_kernel32_on_windows(self) -> None: |
| 22 | + kernel32 = mock.Mock() |
| 23 | + kernel32.IsDebuggerPresent.return_value = 1 |
| 24 | + with mock.patch.object(anti_cheat, "is_windows", return_value=True): |
| 25 | + self.assertTrue(anti_cheat.debugger_attached(kernel32=kernel32)) |
| 26 | + |
| 27 | + def test_debugger_attached_is_false_off_windows(self) -> None: |
| 28 | + kernel32 = mock.Mock() |
| 29 | + kernel32.IsDebuggerPresent.return_value = 1 |
| 30 | + with mock.patch.object(anti_cheat, "is_windows", return_value=False): |
| 31 | + self.assertFalse(anti_cheat.debugger_attached(kernel32=kernel32)) |
| 32 | + |
| 33 | + def test_suspicious_process_found_detects_known_tool(self) -> None: |
| 34 | + found = anti_cheat.suspicious_process_found(["explorer.exe", "CheatEngine.exe"]) |
| 35 | + self.assertEqual(found, "cheatengine.exe") |
| 36 | + |
| 37 | + def test_suspicious_process_found_returns_none_when_clean(self) -> None: |
| 38 | + self.assertIsNone(anti_cheat.suspicious_process_found(["explorer.exe", "python.exe"])) |
| 39 | + |
| 40 | + def test_run_checks_returns_debugger_exit_code(self) -> None: |
| 41 | + with mock.patch.object(anti_cheat, "debugger_attached", return_value=True): |
| 42 | + self.assertEqual(anti_cheat.run_checks(), anti_cheat.DEBUGGER_EXIT_CODE) |
| 43 | + |
| 44 | + def test_run_checks_returns_suspicious_process_exit_code(self) -> None: |
| 45 | + with mock.patch.object(anti_cheat, "debugger_attached", return_value=False): |
| 46 | + with mock.patch.object( |
| 47 | + anti_cheat, |
| 48 | + "suspicious_process_found", |
| 49 | + return_value="x64dbg.exe", |
| 50 | + ): |
| 51 | + self.assertEqual( |
| 52 | + anti_cheat.run_checks(), |
| 53 | + anti_cheat.SUSPICIOUS_PROCESS_EXIT_CODE, |
| 54 | + ) |
| 55 | + |
| 56 | + def test_run_checks_returns_success_when_clean(self) -> None: |
| 57 | + with mock.patch.object(anti_cheat, "debugger_attached", return_value=False): |
| 58 | + with mock.patch.object(anti_cheat, "suspicious_process_found", return_value=None): |
| 59 | + self.assertEqual(anti_cheat.run_checks(), 0) |
| 60 | + |
| 61 | + def test_tasklist_command_failure_propagates(self) -> None: |
| 62 | + with mock.patch.object(anti_cheat, "is_windows", return_value=True): |
| 63 | + with mock.patch("anti_cheat.subprocess.run") as run: |
| 64 | + run.side_effect = subprocess.CalledProcessError(1, "tasklist") |
| 65 | + with self.assertRaises(subprocess.CalledProcessError): |
| 66 | + anti_cheat.list_process_names() |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + unittest.main() |
0 commit comments