From 8163f8dd1d29c8f191ce0a36c79cd2519556ae35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Fri, 15 Nov 2019 13:33:06 +0100 Subject: [PATCH] new: Validate object templates fix https://github.com/MISP/misp-objects/issues/199 --- tests/test_mispevent.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_mispevent.py b/tests/test_mispevent.py index c0e44b1..c8ad63c 100644 --- a/tests/test_mispevent.py +++ b/tests/test_mispevent.py @@ -5,6 +5,7 @@ import unittest import json import sys from io import BytesIO +import glob from pymisp import MISPEvent, MISPSighting, MISPTag from pymisp.exceptions import InvalidMISPObject @@ -291,6 +292,26 @@ class TestMISPEvent(unittest.TestCase): 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)) + @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__': unittest.main()