new: Validate object templates

fix https://github.com/MISP/misp-objects/issues/199
pull/500/head
Raphaël Vinot 2019-11-15 13:33:06 +01:00
parent c5dfa9b509
commit 8163f8dd1d
1 changed files with 21 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import unittest
import json import json
import sys import sys
from io import BytesIO from io import BytesIO
import glob
from pymisp import MISPEvent, MISPSighting, MISPTag from pymisp import MISPEvent, MISPSighting, MISPTag
from pymisp.exceptions import InvalidMISPObject from pymisp.exceptions import InvalidMISPObject
@ -291,6 +292,26 @@ class TestMISPEvent(unittest.TestCase):
ref_json = json.load(f) ref_json = json.load(f)
self.assertEqual(self.mispevent.to_json(sort_keys=True, indent=2), json.dumps(ref_json, sort_keys=True, indent=2)) self.assertEqual(self.mispevent.to_json(sort_keys=True, indent=2), json.dumps(ref_json, sort_keys=True, indent=2))
@unittest.skipIf(sys.version_info < (3, 6), 'Not supported on python < 3.6')
def test_object_templates(self):
me = MISPEvent()
for template in glob.glob(str(me.misp_objects_path / '*' / 'definition.json')):
with open(template) as f:
t_json = json.load(f)
if 'requiredOneOf' in t_json:
obj_relations = set(t_json['attributes'].keys())
subset = set(t_json['requiredOneOf']).issubset(obj_relations)
self.assertTrue(subset, f'{t_json["name"]}')
if 'required' in t_json:
obj_relations = set(t_json['attributes'].keys())
subset = set(t_json['required']).issubset(obj_relations)
self.assertTrue(subset, f'{t_json["name"]}')
for obj_relation, entry in t_json['attributes'].items():
self.assertTrue(entry['misp-attribute'] in me.describe_types['types'])
if 'categories' in entry:
subset = set(entry['categories']).issubset(me.describe_types['categories'])
self.assertTrue(subset, f'{t_json["name"]} - {obj_relation}')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()