PyTaxonomies/tests/tests.py

96 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
2016-10-13 17:23:10 +02:00
from pytaxonomies import Taxonomies, EncodeTaxonomies
import pytaxonomies.api
2016-10-14 12:03:39 +02:00
import os
2016-07-25 18:48:08 +02:00
class TestPyTaxonomies(unittest.TestCase):
def setUp(self):
self.taxonomies = Taxonomies()
2016-10-14 12:03:39 +02:00
self.manifest_path = "./misp-taxonomies/MANIFEST.json"
self.taxonomies_offline = Taxonomies(manifest_path=self.manifest_path)
self.json_load_taxonomies()
def __load_path(self, path):
with open(path, 'r') as f:
return json.load(f)
def json_load_taxonomies(self):
self.manifest = self.__load_path(self.manifest_path)
self.loaded_tax = {}
for t in self.manifest['taxonomies']:
path = '{}/{}/{}'.format(os.path.dirname(os.path.realpath(self.manifest_path)), t['name'], self.manifest['path'])
self.loaded_tax[t['name']] = self.__load_path(path)
2016-07-25 18:48:08 +02:00
2016-07-28 11:56:02 +02:00
def test_compareOnlineOffilne(self):
self.assertEqual(str(self.taxonomies), 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):
self.taxonomies.all_machinetags(expanded=True)
2016-07-25 19:38:17 +02:00
2016-07-28 11:56:02 +02:00
def test_machinetags(self):
self.taxonomies.all_machinetags()
2016-07-25 19:38:17 +02:00
2016-07-28 11:56:02 +02:00
def test_dict(self):
len(self.taxonomies)
2016-07-25 19:38:17 +02:00
for n, t in self.taxonomies.items():
len(t)
for p, value in t.items():
continue
2016-07-28 11:56:02 +02:00
def test_search(self):
self.taxonomies.search('phish')
def test_search_expanded(self):
self.taxonomies.search('phish', expanded=True)
2016-07-25 19:38:17 +02:00
def test_print_classes(self):
2016-10-14 18:24:21 +02:00
for taxonomy in self.taxonomies.values():
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):
for tax in self.taxonomies.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):
Taxonomies()
Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json")
pytaxonomies.api.HAS_REQUESTS = True
2016-10-14 12:03:39 +02:00
def test_revert_machinetags(self):
2016-10-14 18:29:10 +02:00
for tax in self.taxonomies.values():
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)
self.taxonomies.revert_machinetag(mt)
else:
mt = tax.make_machinetag(p)
self.taxonomies.revert_machinetag(mt)
2016-07-25 19:38:17 +02:00
2016-10-13 17:23:10 +02:00
def test_json(self):
2016-10-14 12:03:39 +02:00
for key, t in self.taxonomies.items():
2016-10-13 17:23:10 +02:00
json.dumps(t, cls=EncodeTaxonomies)
2016-10-14 12:03:39 +02:00
def test_recreate_dump(self):
self.maxDiff = None
for key, t in self.taxonomies.items():
out = t._json()
print(t.name)
self.assertCountEqual(out, self.loaded_tax[t.name])
2016-10-13 17:23:10 +02:00
2016-07-25 18:48:08 +02:00
if __name__ == "__main__":
unittest.main()