PyMISPWarningLists/tests/tests.py

61 lines
2.4 KiB
Python
Raw Normal View History

2017-10-29 01:40:41 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
2017-10-29 01:40:41 +02:00
import unittest
2017-10-29 01:40:41 +02:00
from glob import glob
from pymispwarninglists import WarningLists, tools
2017-10-29 01:40:41 +02:00
class TestPyMISPWarningLists(unittest.TestCase):
def setUp(self):
self.warninglists = WarningLists()
def test_dump_warninglists(self):
warninglists_from_files = {}
for warninglist_file in glob(os.path.join(self.warninglists.root_dir_warninglists, '*', 'list.json')):
2021-09-14 16:31:58 +02:00
with open(warninglist_file, mode='r', encoding="utf-8") as f:
2017-10-29 01:40:41 +02:00
warninglist = json.load(f)
warninglists_from_files[warninglist['name']] = warninglist
for name, w in self.warninglists.items():
2017-11-01 00:06:50 +01:00
out = w.to_dict()
2017-10-29 01:40:41 +02:00
self.assertDictEqual(out, warninglists_from_files[w.name])
def test_validate_schema_warninglists(self):
self.warninglists.validate_with_schema()
def test_json(self):
for w in self.warninglists.values():
2017-11-01 00:06:50 +01:00
w.to_json()
def test_search(self):
results = self.warninglists.search('8.8.8.8')
self.assertEqual(results[0].name, 'List of known IPv4 public DNS resolvers')
2017-11-01 01:06:50 +01:00
def test_slow_search(self):
self.warninglists = WarningLists(True)
results = self.warninglists.search('8.8.8.8')
self.assertEqual(results[0].name, 'List of known IPv4 public DNS resolvers')
results = self.warninglists.search('100.64.1.56')
self.assertEqual(results[0].name, 'List of RFC 6598 CIDR blocks')
results = self.warninglists.search('2001:DB8::34:1')
self.assertEqual(results[0].name, 'List of RFC 3849 CIDR blocks')
2017-11-01 01:28:54 +01:00
results = self.warninglists.search('1e100.net')
2020-04-23 15:48:35 +02:00
self.assertTrue('List of known google domains' in [r.name for r in results])
2021-01-21 15:23:15 +01:00
results = self.warninglists.search('blah.files.1drv.com')
self.assertTrue('Top 10K most-used sites from Tranco' in [r.name for r in results])
2020-04-23 15:16:54 +02:00
results = self.warninglists.search('arbitrary-domain-1e100.net')
self.assertEqual(results, [])
results = self.warninglists.search('phishing.co.uk')
self.assertEqual(results, [])
def test_fetch_xdg(self):
tools.update_warninglists()
self.assertTrue(tools.get_xdg_home_dir().exists())
warninglists = WarningLists(from_xdg_home=True)
self.assertEqual(len(warninglists), len(self.warninglists))