mirror of
https://github.com/C24Be/AS_Network_List.git
synced 2026-03-30 22:28:50 +03:00
big update
This commit is contained in:
26
tests/test_check_nft_blacklist.py
Normal file
26
tests/test_check_nft_blacklist.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from check_nft_blacklist import check_ip_in_blacklist, parse_nft_config
|
||||
from generate_nft_blacklist import make_nft_config
|
||||
|
||||
|
||||
class CheckNftBlacklistTests(unittest.TestCase):
|
||||
def test_vk_sets_are_parsed(self):
|
||||
config = make_nft_config(["87.240.128.0/18"], [], usage_profile="vk_forward")
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
config_path = Path(tmpdir) / "blacklist-vk-v4.nft"
|
||||
config_path.write_text(config, encoding="utf-8")
|
||||
|
||||
v4_prefixes, v6_prefixes = parse_nft_config(config_path)
|
||||
blocked, prefix = check_ip_in_blacklist("87.240.128.1", v4_prefixes, v6_prefixes)
|
||||
|
||||
self.assertEqual(len(v4_prefixes), 1)
|
||||
self.assertTrue(blocked)
|
||||
self.assertEqual(str(prefix), "87.240.128.0/18")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
25
tests/test_generate_nft_blacklist.py
Normal file
25
tests/test_generate_nft_blacklist.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
|
||||
from generate_nft_blacklist import make_nft_config
|
||||
|
||||
|
||||
class GenerateNftBlacklistTests(unittest.TestCase):
|
||||
def test_general_profile_generates_plain_sets_only(self):
|
||||
config = make_nft_config(["10.0.0.0/24"], [], usage_profile="vm_input")
|
||||
|
||||
self.assertIn("set blacklist_v4", config)
|
||||
self.assertNotIn("chain input", config)
|
||||
self.assertIn("ip saddr @blacklist_v4", config)
|
||||
|
||||
def test_vk_profile_uses_vk_set_names_and_forward_example(self):
|
||||
config = make_nft_config(["10.0.0.0/24"], ["2001:db8::/32"], usage_profile="vk_forward")
|
||||
|
||||
self.assertIn("set blacklist_vk_v4", config)
|
||||
self.assertIn("set blacklist_vk_v6", config)
|
||||
self.assertNotIn("chain forward", config)
|
||||
self.assertIn("ip daddr @blacklist_vk_v4", config)
|
||||
self.assertIn("ip6 daddr @blacklist_vk_v6", config)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
41
tests/test_parse_ripe_db.py
Normal file
41
tests/test_parse_ripe_db.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from parse_ripe_db import parse
|
||||
|
||||
|
||||
class ParseRipeDbTests(unittest.TestCase):
|
||||
def test_skips_non_ru_last_record_and_normalizes_last_ru_record(self):
|
||||
sample = """\
|
||||
inetnum: 10.0.0.0 - 10.0.0.255
|
||||
netname: TEST1
|
||||
country: RU
|
||||
org: ORG-1
|
||||
descr: desc1
|
||||
inetnum: 20.0.0.0 - 20.0.0.255
|
||||
netname: TEST2
|
||||
country: US
|
||||
org: ORG-2
|
||||
"""
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
source = Path(tmpdir) / "ripe.db.inetnum"
|
||||
output_text = Path(tmpdir) / "out.txt"
|
||||
output_json = Path(tmpdir) / "out.json"
|
||||
source.write_text(sample, encoding="latin-1")
|
||||
|
||||
parse(str(source), str(output_text), str(output_json))
|
||||
|
||||
payload = json.loads(output_json.read_text(encoding="utf-8"))
|
||||
self.assertEqual(len(payload), 1)
|
||||
self.assertEqual(payload[0]["inetnum"], ["10.0.0.0/24"])
|
||||
self.assertEqual(payload[0]["country"], "RU")
|
||||
|
||||
text_lines = output_text.read_text(encoding="utf-8").splitlines()
|
||||
self.assertEqual(text_lines, ["10.0.0.0/24 TEST1 (ORG-1) [desc1]"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user