Package misp-taxonomies

pull/2/head
Raphaël Vinot 2017-07-21 10:07:55 +02:00
parent e072d30d66
commit 67063b62bb
5 changed files with 28 additions and 32 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "pytaxonomies/data/misp-taxonomies"]
path = pytaxonomies/data/misp-taxonomies
url = https://github.com/MISP/misp-taxonomies.git

View File

@ -188,7 +188,7 @@ class Taxonomy(collections.Mapping):
class Taxonomies(collections.Mapping):
def __init__(self, manifest_url='https://raw.githubusercontent.com/MISP/misp-taxonomies/master/MANIFEST.json',
manifest_path=None):
manifest_path=os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'misp-taxonomies', 'MANIFEST.json')):
if manifest_path:
self.loader = self.__load_path
self.manifest = self.loader(manifest_path)

@ -0,0 +1 @@
Subproject commit 2723592e2d4d8d179b3cfea161a87412e5604068

View File

@ -26,4 +26,6 @@ setup(
tests_requires=['nose'],
test_suite='nose.collector',
install_requires=['requests'],
package_data={'pytaxonomies': ['data/misp-taxonomies/schema.json', 'data/misp-taxonomies/MANIFEST.json',
'data/misp-taxonomies/*/machinetag.json']}
)

View File

@ -11,46 +11,37 @@ import os
class TestPyTaxonomies(unittest.TestCase):
def setUp(self):
self.taxonomies = Taxonomies()
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.taxonomies_offline = Taxonomies()
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)
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)
def test_compareOnlineOffilne(self):
self.assertEqual(str(self.taxonomies), str(self.taxonomies_offline))
taxonomies_online = Taxonomies(manifest_path=None)
self.assertEqual(str(taxonomies_online), str(self.taxonomies_offline))
def test_expanded_machinetags(self):
self.taxonomies.all_machinetags(expanded=True)
self.taxonomies_offline.all_machinetags(expanded=True)
def test_machinetags(self):
self.taxonomies.all_machinetags()
self.taxonomies_offline.all_machinetags()
def test_dict(self):
len(self.taxonomies)
for n, t in self.taxonomies.items():
len(self.taxonomies_offline)
for n, t in self.taxonomies_offline.items():
len(t)
for p, value in t.items():
continue
def test_search(self):
self.taxonomies.search('phish')
self.taxonomies_offline.search('phish')
def test_search_expanded(self):
self.taxonomies.search('phish', expanded=True)
self.taxonomies_offline.search('phish', expanded=True)
def test_print_classes(self):
for taxonomy in self.taxonomies.values():
for taxonomy in self.taxonomies_offline.values():
print(taxonomy)
for predicate in taxonomy.values():
print(predicate)
@ -58,36 +49,35 @@ class TestPyTaxonomies(unittest.TestCase):
print(entry)
def test_amountEntries(self):
for tax in self.taxonomies.values():
for tax in self.taxonomies_offline.values():
tax.amount_entries()
def test_missingDependency(self):
pytaxonomies.api.HAS_REQUESTS = False
with self.assertRaises(Exception):
Taxonomies()
Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json")
Taxonomies(manifest_path=None)
Taxonomies()
pytaxonomies.api.HAS_REQUESTS = True
def test_revert_machinetags(self):
for tax in self.taxonomies.values():
for tax in self.taxonomies_offline.values():
for p in tax.values():
if tax.has_entries():
for e in p.values():
mt = tax.make_machinetag(p, e)
self.taxonomies.revert_machinetag(mt)
self.taxonomies_offline.revert_machinetag(mt)
else:
mt = tax.make_machinetag(p)
self.taxonomies.revert_machinetag(mt)
self.taxonomies_offline.revert_machinetag(mt)
def test_json(self):
for key, t in self.taxonomies.items():
for key, t in self.taxonomies_offline.items():
json.dumps(t, cls=EncodeTaxonomies)
def test_recreate_dump(self):
self.maxDiff = None
for key, t in self.taxonomies.items():
for key, t in self.taxonomies_offline.items():
out = t._json()
print(t.name)
self.assertCountEqual(out, self.loaded_tax[t.name])