Download tests/data/test_incident.py from Kolgrim/hades: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/Kolgrim/hades/resolve/main/tests/data/test_incident.py
- Command line
-
hf download hf://Kolgrim/hades/tests/data/test_incident.py
-
curl -L -o test_incident.py https://huggingface.co/Kolgrim/hades/resolve/main/tests/data/test_incident.py
2.36 kB
| import random | |
| import pytest | |
| from hades.analyze.authlog import analyze | |
| from hades.data.synth.incident import ( | |
| OUTCOMES, | |
| STAGES, | |
| generate_incidents, | |
| score_reconstruction, | |
| ) | |
| def incidents(): | |
| return generate_incidents(60, seed=1) | |
| def test_outcomes_are_balanced_and_stages_match_outcome(incidents): | |
| assert {i.outcome for i in incidents} == set(OUTCOMES) | |
| for inc in incidents: | |
| want = {"benign": [], "contained": list(STAGES[:2]), "breach": list(STAGES)}[inc.outcome] | |
| assert inc.stage_names() == want | |
| def test_stage_lines_exist_and_contain_the_evidence(incidents): | |
| for inc in incidents: | |
| lines = inc.text.splitlines() | |
| for stage in inc.stages: | |
| assert stage.lines and all(1 <= n <= len(lines) for n in stage.lines) | |
| blob = "\n".join(lines[n - 1] for n in stage.lines) | |
| if stage.name in ("recon", "credential_access", "initial_access"): | |
| assert inc.attacker_ip in blob | |
| if stage.name == "persistence": | |
| assert "new user: name=" in blob and "crontab" in blob | |
| def test_stages_are_in_chronological_line_order(incidents): | |
| for inc in incidents: | |
| firsts = [s.lines[0] for s in inc.stages] | |
| assert firsts == sorted(firsts) | |
| def test_deterministic_and_seed_separation(): | |
| assert [i.text for i in generate_incidents(5, 3)] == [i.text for i in generate_incidents(5, 3)] | |
| with pytest.raises(ValueError): | |
| generate_incidents(1, 9000) | |
| with pytest.raises(ValueError): | |
| generate_incidents(1, 1, purpose="test") | |
| def test_analyzer_flags_breaches_and_not_benign(incidents): | |
| for inc in incidents: | |
| verdict = analyze(inc.text).verdict | |
| if inc.outcome == "breach": | |
| assert verdict == "compromise" | |
| elif inc.outcome == "benign": | |
| assert verdict in ("normal", "failed_login") | |
| def test_score_reconstruction(): | |
| inc = next(i for i in generate_incidents(9, 2) if i.outcome == "breach") | |
| assert score_reconstruction(list(STAGES), inc)["exact"] == 1.0 | |
| partial = score_reconstruction(["credential_access", "initial_access"], inc) | |
| assert partial["precision"] == 1.0 and partial["recall"] == 0.4 and partial["order_ok"] == 1.0 | |
| assert score_reconstruction(["persistence", "recon"], inc)["order_ok"] == 0.0 | |