PyTaxonomies/tests/tests.py

91 lines
3.0 KiB
Python
Raw Normal View History

2016-07-25 18:48:08 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2016-10-13 17:23:10 +02:00
import json
2016-07-25 18:48:08 +02:00
import unittest
2020-11-05 19:20:53 +01:00
from pytaxonomies import Taxonomies
import pytaxonomies.api
2016-07-25 18:48:08 +02:00
class TestPyTaxonomies(unittest.TestCase):
def setUp(self):
2020-02-17 19:15:39 +01:00
self.maxDiff = None
2017-07-21 10:07:55 +02:00
self.taxonomies_offline = Taxonomies()
2016-10-14 12:03:39 +02:00
self.loaded_tax = {}
2017-07-21 10:07:55 +02:00
for t in self.taxonomies_offline.manifest['taxonomies']:
with open('{}/{}/{}'.format(self.taxonomies_offline.url, t['name'], 'machinetag.json'), 'r') as f:
self.loaded_tax[t['name']] = json.load(f)
2016-07-25 18:48:08 +02:00
2016-07-28 11:56:02 +02:00
def test_compareOnlineOffilne(self):
2022-05-13 16:35:20 +02:00
taxonomies_online = Taxonomies(manifest_url='https://raw.githubusercontent.com/MISP/misp-taxonomies/main/MANIFEST.json')
2018-10-30 18:58:57 +01:00
for t_online, t_offline in zip(taxonomies_online.values(), self.taxonomies_offline.values()):
self.assertEqual(str(t_online), str(t_offline))
2017-07-21 10:07:55 +02:00
self.assertEqual(str(taxonomies_online), str(self.taxonomies_offline))
2016-07-25 18:48:08 +02:00
2016-07-28 11:56:02 +02:00
def test_expanded_machinetags(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.all_machinetags(expanded=True)
2016-07-25 19:38:17 +02:00
2016-07-28 11:56:02 +02:00
def test_machinetags(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.all_machinetags()
2016-07-25 19:38:17 +02:00
2016-07-28 11:56:02 +02:00
def test_dict(self):
2017-07-21 10:07:55 +02:00
len(self.taxonomies_offline)
for n, t in self.taxonomies_offline.items():
2016-07-25 19:38:17 +02:00
len(t)
for p, value in t.items():
continue
2016-07-28 11:56:02 +02:00
def test_search(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.search('phish')
2016-07-28 11:56:02 +02:00
def test_search_expanded(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.search('phish', expanded=True)
2016-07-25 19:38:17 +02:00
def test_print_classes(self):
2017-07-21 10:07:55 +02:00
for taxonomy in self.taxonomies_offline.values():
2016-10-14 18:24:21 +02:00
print(taxonomy)
for predicate in taxonomy.values():
print(predicate)
for entry in predicate.values():
print(entry)
2016-07-28 12:58:20 +02:00
def test_amountEntries(self):
2017-07-21 10:07:55 +02:00
for tax in self.taxonomies_offline.values():
tax.amount_entries()
2016-07-28 12:58:20 +02:00
def test_missingDependency(self):
pytaxonomies.api.HAS_REQUESTS = False
with self.assertRaises(Exception):
2022-05-13 16:35:20 +02:00
Taxonomies(manifest_url='foo')
2017-07-21 10:07:55 +02:00
Taxonomies()
pytaxonomies.api.HAS_REQUESTS = True
2016-10-14 12:03:39 +02:00
def test_revert_machinetags(self):
2017-07-21 10:07:55 +02:00
for tax in self.taxonomies_offline.values():
2016-10-14 18:29:10 +02:00
for p in tax.values():
2016-10-14 18:36:30 +02:00
if tax.has_entries():
for e in p.values():
mt = tax.make_machinetag(p, e)
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.revert_machinetag(mt)
2016-10-14 18:36:30 +02:00
else:
mt = tax.make_machinetag(p)
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.revert_machinetag(mt)
2016-07-25 19:38:17 +02:00
2016-10-13 17:23:10 +02:00
def test_json(self):
2017-07-21 10:07:55 +02:00
for key, t in self.taxonomies_offline.items():
2020-11-05 19:20:53 +01:00
t.to_json()
2016-10-13 17:23:10 +02:00
2016-10-14 12:03:39 +02:00
def test_recreate_dump(self):
self.maxDiff = None
2017-07-21 10:07:55 +02:00
for key, t in self.taxonomies_offline.items():
2017-11-01 22:13:24 +01:00
out = t.to_dict()
2017-07-25 15:06:37 +02:00
self.assertDictEqual(out, self.loaded_tax[t.name])
2016-10-14 12:03:39 +02:00
2017-07-25 16:19:34 +02:00
def test_validate_schema(self):
self.taxonomies_offline.validate_with_schema()
2016-10-13 17:23:10 +02:00
2018-10-30 18:58:57 +01:00
2016-07-25 18:48:08 +02:00
if __name__ == "__main__":
unittest.main()