-
Notifications
You must be signed in to change notification settings - Fork 174
/
test_blacklists.py
executable file
·87 lines (75 loc) · 2.75 KB
/
test_blacklists.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# coding=utf-8
import json
from glob import glob
from os import unlink
import pytest
from blacklists import Blacklist, JSONParserCIDR, JSONParserASN
from helpers import files_changed, blacklist_integrity_check
def test_blacklist_integrity():
errors = blacklist_integrity_check()
if len(errors) == 1:
pytest.fail(errors[0])
elif len(errors) > 1:
pytest.fail("\n\t".join(["{} errors has occurred:".format(len(errors))] + errors))
def test_remote_diff():
file_set = set("abcdefg")
true_diff = "a c k p"
false_diff = "h j q t"
assert files_changed(true_diff, file_set)
assert not files_changed(false_diff, file_set)
def test_json_blacklist():
with open('test_ip.json', 'w') as j:
json.dump({
'schema': 'json_cidr',
'schema_version': '2019120601',
'items': [
{'ip': '1.2.3.4'},
{'ip': '2.3.4.5', 'disable': True},
{'ip': '3.4.5.6', 'comment': 'comment'},
]}, j)
blacklist = Blacklist(('test_ip.json', JSONParserCIDR))
with pytest.raises(ValueError) as e:
blacklist.add('1.3.34')
with pytest.raises(ValueError) as e:
blacklist.add({'ip': '1.3.4'})
with pytest.raises(ValueError) as e:
blacklist.add({'ip': '1.2.3.4'})
with pytest.raises(ValueError) as e:
blacklist.add({'ip': '2.3.4.5'})
with pytest.raises(ValueError) as e:
blacklist.remove({'ip': '34.45.56.67'})
blacklist.add({'ip': '1.3.4.5'})
assert '1.2.3.4' in blacklist.parse()
assert '2.3.4.5' not in blacklist.parse()
assert '3.4.5.6' in blacklist.parse()
blacklist.remove({'ip': '3.4.5.6'})
assert '3.4.5.6' not in blacklist.parse()
unlink('test_ip.json')
def test_json_asn():
with open('test_asn.json', 'w') as j:
json.dump({
'schema': 'json_asn',
'schema_version': '2019120601',
'items': [
{'asn': '123'},
{'asn': '234', 'disable': True},
{'asn': '345', 'comment': 'comment'},
]}, j)
blacklist = Blacklist(('test_asn.json', JSONParserASN))
with pytest.raises(ValueError) as e:
blacklist.add('123')
with pytest.raises(ValueError) as e:
blacklist.add({'asn': 'invalid'})
with pytest.raises(ValueError) as e:
blacklist.add({'asn': '123'})
with pytest.raises(ValueError) as e:
blacklist.add({'asn': '234'})
with pytest.raises(ValueError) as e:
blacklist.remove({'asn': '9897'})
assert '123' in blacklist.parse()
assert '234' not in blacklist.parse()
assert '345' in blacklist.parse()
blacklist.remove({'asn': '345'})
assert '345' not in blacklist.parse()
unlink('test_asn.json')