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
|
2016-07-28 16:01:27 +02:00
|
|
|
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
|
|
|
|
2016-07-26 10:56:40 +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-26 10:56:40 +02:00
|
|
|
|
2016-07-28 12:58:20 +02:00
|
|
|
def test_amountEntries(self):
|
|
|
|
list(self.taxonomies.values())[0].amount_entries()
|
|
|
|
|
2016-07-28 16:01:27 +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-06 13:25:46 +02:00
|
|
|
tax = list(self.taxonomies.values())[0]
|
|
|
|
for p in tax.values():
|
|
|
|
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()
|