Download tests/data/test_authlog.py from Kolgrim/hades: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/Kolgrim/hades/resolve/main/tests/data/test_authlog.py
- Command line
-
hf download hf://Kolgrim/hades/tests/data/test_authlog.py
-
curl -L -o test_authlog.py https://huggingface.co/Kolgrim/hades/resolve/main/tests/data/test_authlog.py
5.25 kB
| import random | |
| import re | |
| from collections import Counter | |
| import pytest | |
| from hades.data.synth.authlog import ( | |
| LABELS, | |
| SYSTEM_USERS, | |
| generate_dataset, | |
| generate_unlabelled, | |
| generate_window, | |
| make_vocab, | |
| ) | |
| FAILED = re.compile(r"Failed password for (invalid user )?(\S+) from (\S+) port") | |
| ACCEPTED = re.compile(r"Accepted \S+ for (\S+) from (\S+) port") | |
| INVALID = re.compile(r"Invalid user (\S+) from (\S+)") | |
| def facts(lines): | |
| fails = [(m.group(2), m.group(3), bool(m.group(1))) for l in lines if (m := FAILED.search(l))] | |
| accepted = [(m.group(1), m.group(2)) for l in lines if (m := ACCEPTED.search(l))] | |
| invalid = [m.group(1) for l in lines if (m := INVALID.search(l))] | |
| sudo_denied = [l for l in lines if "NOT in sudoers" in l or "incorrect password attempts" in l] | |
| return fails, accepted, invalid, sudo_denied | |
| def windows(label, n=60, split="train"): | |
| rng = random.Random(label) | |
| vocab = make_vocab(split) | |
| return [generate_window(rng, label, vocab) for _ in range(n)] | |
| def test_labels(): | |
| assert LABELS == ("normal", "failed_login", "brute_force", "password_spray", "invalid_user_scan", "sudo_denied", "compromise") | |
| def test_window_length(label): | |
| for w in windows(label): | |
| assert 5 <= len(w) <= 20 | |
| def test_normal(): | |
| for w in windows("normal"): | |
| fails, _, invalid, denied = facts(w) | |
| assert not fails and not invalid and not denied | |
| def test_failed_login(): | |
| for w in windows("failed_login"): | |
| fails, accepted, invalid, denied = facts(w) | |
| assert 1 <= len(fails) <= 2 and len({ip for _, ip, _ in fails}) == 1 | |
| assert not invalid and not denied | |
| def test_brute_force(): | |
| for w in windows("brute_force"): | |
| fails, accepted, _, _ = facts(w) | |
| ip = Counter(ip for _, ip, _ in fails).most_common(1)[0][0] | |
| from_ip = [u for u, i, _ in fails if i == ip] | |
| assert len(from_ip) >= 6 and len(set(from_ip)) <= 3 | |
| assert ip not in {i for _, i in accepted} | |
| def test_password_spray(): | |
| for w in windows("password_spray"): | |
| fails, _, _, _ = facts(w) | |
| by_ip = {} | |
| for u, ip, inv in fails: | |
| if not inv: | |
| by_ip.setdefault(ip, set()).add(u) | |
| assert max(len(s) for s in by_ip.values()) >= 5 | |
| def test_invalid_user_scan(): | |
| for w in windows("invalid_user_scan"): | |
| fails, _, invalid, _ = facts(w) | |
| assert len(invalid) >= 4 | |
| assert all(inv for _, _, inv in fails) # only invalid-user failures | |
| def test_sudo_denied(): | |
| for w in windows("sudo_denied"): | |
| fails, _, invalid, denied = facts(w) | |
| assert denied and not fails and not invalid | |
| def test_compromise(): | |
| for w in windows("compromise"): | |
| fails, accepted, _, _ = facts(w) | |
| ip = Counter(ip for _, ip, _ in fails).most_common(1)[0][0] | |
| assert sum(1 for _, i, _ in fails if i == ip) >= 6 | |
| acc_idx = [k for k, l in enumerate(w) if (m := ACCEPTED.search(l)) and m.group(2) == ip] | |
| last_fail = max(k for k, l in enumerate(w) if (m := FAILED.search(l)) and m.group(3) == ip) | |
| assert acc_idx and acc_idx[0] > last_fail | |
| def test_deterministic(): | |
| assert generate_dataset(14, seed=5) == generate_dataset(14, seed=5) | |
| assert generate_dataset(14, seed=5) != generate_dataset(14, seed=6) | |
| def test_balanced(): | |
| counts = Counter(w.label for w in generate_dataset(70, seed=1)) | |
| assert set(counts.values()) == {10} | |
| def test_heldout_vocab_disjoint(): | |
| train, held = make_vocab("train"), make_vocab("heldout") | |
| assert not set(train.usernames) & set(held.usernames) | |
| assert not set(train.invalid_usernames) & set(held.invalid_usernames) | |
| assert not set(train.hostnames) & set(held.hostnames) | |
| assert not set(SYSTEM_USERS) & (set(train.usernames) | set(held.usernames)) | |
| train_names = set(train.usernames) | set(train.invalid_usernames) | set(train.hostnames) | |
| for w in generate_dataset(70, seed=2, split="heldout"): | |
| words = set(re.findall(r"[A-Za-z0-9_.-]+", w.text)) | |
| assert not words & train_names | |
| def test_iso_space_only_heldout(): | |
| iso_space = re.compile(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d ", re.M) | |
| assert not any(iso_space.search(w.text) for w in generate_dataset(140, seed=3)) | |
| assert any(iso_space.search(w.text) for w in generate_dataset(140, seed=3, split="heldout")) | |
| def test_token_cap(): | |
| for w in generate_dataset(70, seed=4, count_tokens=len, max_tokens=900): | |
| assert len(w.text) <= 900 | |
| def test_test_seeds_reserved(): | |
| with pytest.raises(ValueError): | |
| generate_dataset(7, seed=9001) | |
| with pytest.raises(ValueError): | |
| generate_dataset(7, seed=5, purpose="test") | |
| assert len(generate_dataset(7, seed=9001, purpose="test")) == 7 | |
| def test_unlabelled(): | |
| docs = list(generate_unlabelled(100_000, 0)) | |
| text = "\n".join(docs) | |
| assert len(text.encode()) >= 100_000 | |
| assert all(50 <= len(d.splitlines()) <= 500 for d in docs) | |
| for label in LABELS: | |
| assert label not in text | |
| def test_impossible_token_cap_raises_instead_of_looping(): | |
| with pytest.raises(ValueError, match="max_tokens"): | |
| generate_dataset(7, seed=1, count_tokens=len, max_tokens=50) | |