From 168105603bfedb944f1e2860d2e735b3ff9fdc17 Mon Sep 17 00:00:00 2001 From: clenk Date: Mon, 10 Apr 2017 10:42:07 -0400 Subject: [PATCH] Parse dictionaries as well as strings and file-like objects --- stix2/__init__.py | 11 +++++++---- stix2/test/test_malware.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/stix2/__init__.py b/stix2/__init__.py index 0e6dd94..149829e 100644 --- a/stix2/__init__.py +++ b/stix2/__init__.py @@ -15,10 +15,13 @@ from .sro import Relationship def parse(data): """Deserialize a string or file-like object into a STIX object""" - try: - obj = json.loads(data) - except TypeError: - obj = json.load(data) + if type(data) is dict: + obj = data + else: + try: + obj = json.loads(data) + except TypeError: + obj = json.load(data) if 'type' not in obj: # TODO parse external references, kill chain phases, and granular markings diff --git a/stix2/test/test_malware.py b/stix2/test/test_malware.py index 5206e60..c80c28e 100644 --- a/stix2/test/test_malware.py +++ b/stix2/test/test_malware.py @@ -91,8 +91,19 @@ def test_invalid_kwarg_to_malware(): assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']" -def test_parse_malware(): - mal = stix2.parse(EXPECTED_MALWARE) +@pytest.mark.parametrize("data", [ + EXPECTED_MALWARE, + { + "type": "malware", + "id": "malware--fedcba98-7654-3210-fedc-ba9876543210", + "created": "2016-05-12T08:17:27Z", + "modified": "2016-05-12T08:17:27Z", + "labels": ["ransomware"], + "name": "Cryptolocker", + }, +]) +def test_parse_malware(data): + mal = stix2.parse(data) assert mal.type == 'malware' assert mal.id == MALWARE_ID