File size: 1,849 Bytes
5f02c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from hades.data.clean import dedupe, keep, normalize_text
from hades.data.sources.base import Record

ENGLISH = ("Secure Shell is a cryptographic network protocol for operating network services securely over an "
           "unsecured network. Typical applications include remote command-line login and remote command execution. "
           "Administrators should disable password authentication where keys are available.")


def rec(text, source="s", id="1", group="security"):
    return Record(source=source, id=id, license="L", group=group, text=text)


def test_lone_surrogate_replaced():
    assert normalize_text("a\ud800b") == "a�b"


def test_control_chars_replaced_but_whitespace_kept():
    assert normalize_text("a\x00b\x1bc\td\re\nf") == "a�b�c\td\re\nf"


def test_nfc():
    assert normalize_text("é") == "é"


def test_inline_whitespace_preserved():
    assert normalize_text("x   y") == "x   y"


def test_html_stripped():
    assert normalize_text("<p>hi</p>", html=True) == "hi"
    assert normalize_text("<p>a &amp; b</p><script>x()</script>", html=True) == "a & b"


def test_blank_lines_collapsed():
    assert normalize_text("a\n\n\n\n\n\nb") == "a\n\n\nb"


def test_keep_rules():
    assert keep(rec(ENGLISH))
    assert not keep(rec("too short"))
    assert not keep(rec("Привет мир, это тест на русском языке. " * 10))
    assert not keep(rec("\n".join(["same line here"] * 50)))
    assert keep(rec("\n".join(["Oct 28 sshd[1]: Failed password for root"] * 50), group="logs"))


def test_dedupe_across_sources():
    out = list(dedupe([rec(ENGLISH, "a", "1"), rec(ENGLISH, "b", "2"), rec(ENGLISH + "!", "b", "3")]))
    assert [r.id for r in out] == ["1", "3"]


def test_record_json_roundtrip():
    r = rec("x\ny é", "cve", "CVE-1")
    assert Record.from_json(r.to_json()) == r