2017-07-25 18:04:15 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import unittest
|
2017-07-26 17:11:17 +02:00
|
|
|
from pymispgalaxies import Galaxies, Clusters, UnableToRevertMachinetag, EncodeGalaxies
|
2017-07-25 18:04:15 +02:00
|
|
|
from glob import glob
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
class TestPyMISPGalaxies(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.galaxies = Galaxies()
|
|
|
|
self.clusters = Clusters()
|
|
|
|
self.maxDiff = None
|
|
|
|
|
|
|
|
def test_dump_galaxies(self):
|
|
|
|
galaxies_from_files = {}
|
|
|
|
for galaxy_file in glob(os.path.join(self.galaxies.root_dir_galaxies, '*.json')):
|
|
|
|
with open(galaxy_file, 'r') as f:
|
|
|
|
galaxy = json.load(f)
|
|
|
|
galaxies_from_files[galaxy['name']] = galaxy
|
|
|
|
for name, g in self.galaxies.items():
|
|
|
|
out = g._json()
|
|
|
|
self.assertDictEqual(out, galaxies_from_files[g.name])
|
|
|
|
|
|
|
|
def test_dump_clusters(self):
|
|
|
|
clusters_from_files = {}
|
|
|
|
for cluster_file in glob(os.path.join(self.clusters.root_dir_clusters, '*.json')):
|
|
|
|
with open(cluster_file, 'r') as f:
|
|
|
|
cluster = json.load(f)
|
|
|
|
clusters_from_files[cluster['name']] = cluster
|
|
|
|
for name, c in self.clusters.items():
|
|
|
|
out = c._json()
|
2017-07-26 15:12:35 +02:00
|
|
|
self.assertCountEqual(out, clusters_from_files[c.name])
|
2017-07-25 18:04:15 +02:00
|
|
|
|
|
|
|
def test_validate_schema_clusters(self):
|
|
|
|
self.clusters.validate_with_schema()
|
|
|
|
|
|
|
|
def test_validate_schema_galaxies(self):
|
|
|
|
self.galaxies.validate_with_schema()
|
|
|
|
|
|
|
|
def test_meta_additional_properties(self):
|
|
|
|
# All the properties in the meta key of the bundled-in clusters should be known
|
|
|
|
for c in self.clusters.values():
|
2017-07-26 16:37:13 +02:00
|
|
|
for cv in c.values():
|
2017-07-25 18:04:15 +02:00
|
|
|
if cv.meta:
|
|
|
|
self.assertIsNot(cv.meta.additional_properties, {})
|
2017-07-25 18:43:49 +02:00
|
|
|
|
|
|
|
def test_machinetags(self):
|
|
|
|
self.clusters.all_machinetags()
|
|
|
|
|
|
|
|
def test_print(self):
|
|
|
|
print(self.clusters)
|
2017-07-26 15:29:57 +02:00
|
|
|
|
|
|
|
def test_search(self):
|
|
|
|
self.assertIsNot(len(self.clusters.search('apt')), 0)
|
2017-07-26 15:52:39 +02:00
|
|
|
|
|
|
|
def test_revert_machinetag(self):
|
|
|
|
self.assertEqual(len(self.clusters.revert_machinetag('misp-galaxy:tool="Babar"')), 2)
|
2017-07-26 17:06:44 +02:00
|
|
|
with self.assertRaises(UnableToRevertMachinetag):
|
|
|
|
self.clusters.revert_machinetag('blah')
|
2017-07-26 17:01:26 +02:00
|
|
|
|
|
|
|
def test_len(self):
|
|
|
|
self.assertIsNot(len(self.clusters), 0)
|
|
|
|
self.assertIsNot(len(self.galaxies), 0)
|
|
|
|
for c in self.clusters.values():
|
|
|
|
self.assertIsNot(len(c), 0)
|
2017-07-26 17:11:17 +02:00
|
|
|
|
|
|
|
def test_json(self):
|
|
|
|
for c in self.clusters.values():
|
|
|
|
json.dumps(c, cls=EncodeGalaxies)
|