Normalize IDs in tests.
parent
da75833400
commit
e683acbf48
|
@ -21,6 +21,10 @@ def test_timestamp_formatting(dt, timestamp):
|
|||
assert stix2.format_datetime(dt) == timestamp
|
||||
|
||||
|
||||
INDICATOR_ID = "indicator--01234567-89ab-cdef-0123-456789abcdef"
|
||||
MALWARE_ID = "malware--fedcba98-7654-3210-fedc-ba9876543210"
|
||||
RELATIONSHIP_ID = "relationship--00000000-1111-2222-3333-444444444444"
|
||||
|
||||
# Minimum required args for an Indicator instance
|
||||
INDICATOR_KWARGS = dict(
|
||||
labels=['malicious-activity'],
|
||||
|
@ -37,25 +41,19 @@ MALWARE_KWARGS = dict(
|
|||
# Minimum required args for a Relationship instance
|
||||
RELATIONSHIP_KWARGS = dict(
|
||||
relationship_type="indicates",
|
||||
source_ref="indicator--01234567-89ab-cdef-0123-456789abcdef",
|
||||
target_ref="malware--fedcba98-7654-3210-fedc-ba9876543210",
|
||||
source_ref=INDICATOR_ID,
|
||||
target_ref=MALWARE_ID,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def indicator():
|
||||
return stix2.Indicator(
|
||||
id="indicator--01234567-89ab-cdef-0123-456789abcdef",
|
||||
**INDICATOR_KWARGS
|
||||
)
|
||||
return stix2.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def malware():
|
||||
return stix2.Malware(
|
||||
id="malware--fedcba98-7654-3210-fedc-ba9876543210",
|
||||
**MALWARE_KWARGS
|
||||
)
|
||||
return stix2.Malware(id=MALWARE_ID, **MALWARE_KWARGS)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -82,7 +80,7 @@ def test_indicator_with_all_required_fields():
|
|||
|
||||
indicator = stix2.Indicator(
|
||||
type="indicator",
|
||||
id="indicator--01234567-89ab-cdef-0123-456789abcdef",
|
||||
id=INDICATOR_ID,
|
||||
labels=['malicious-activity'],
|
||||
pattern="[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']",
|
||||
created=now,
|
||||
|
@ -153,7 +151,7 @@ def test_invalid_kwarg_to_indicator():
|
|||
|
||||
EXPECTED_MALWARE = """{
|
||||
"created": "2016-05-12T08:17:27Z",
|
||||
"id": "malware--0c7b5b88-8ff7-4a4d-aa9d-feb398cd0061",
|
||||
"id": "malware--fedcba98-7654-3210-fedc-ba9876543210",
|
||||
"labels": [
|
||||
"ransomware"
|
||||
],
|
||||
|
@ -168,7 +166,7 @@ def test_malware_with_all_required_fields():
|
|||
|
||||
malware = stix2.Malware(
|
||||
type="malware",
|
||||
id="malware--0c7b5b88-8ff7-4a4d-aa9d-feb398cd0061",
|
||||
id=MALWARE_ID,
|
||||
created=now,
|
||||
modified=now,
|
||||
labels=["ransomware"],
|
||||
|
@ -236,7 +234,7 @@ def test_invalid_kwarg_to_malware():
|
|||
|
||||
EXPECTED_RELATIONSHIP = """{
|
||||
"created": "2016-04-06T20:06:37Z",
|
||||
"id": "relationship--44298a74-ba52-4f0c-87a3-1824e67d7fad",
|
||||
"id": "relationship--00000000-1111-2222-3333-444444444444",
|
||||
"modified": "2016-04-06T20:06:37Z",
|
||||
"relationship_type": "indicates",
|
||||
"source_ref": "indicator--01234567-89ab-cdef-0123-456789abcdef",
|
||||
|
@ -245,17 +243,17 @@ EXPECTED_RELATIONSHIP = """{
|
|||
}"""
|
||||
|
||||
|
||||
def test_relationship_all_required_fields(indicator, malware):
|
||||
def test_relationship_all_required_fields():
|
||||
now = datetime.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)
|
||||
|
||||
relationship = stix2.Relationship(
|
||||
type='relationship',
|
||||
id='relationship--44298a74-ba52-4f0c-87a3-1824e67d7fad',
|
||||
id=RELATIONSHIP_ID,
|
||||
created=now,
|
||||
modified=now,
|
||||
relationship_type='indicates',
|
||||
source_ref=indicator.id,
|
||||
target_ref=malware.id,
|
||||
source_ref=INDICATOR_ID,
|
||||
target_ref=MALWARE_ID,
|
||||
)
|
||||
assert str(relationship) == EXPECTED_RELATIONSHIP
|
||||
|
||||
|
@ -266,16 +264,16 @@ def test_relationship_autogenerated_fields(relationship):
|
|||
assert relationship.created is not None
|
||||
assert relationship.modified is not None
|
||||
assert relationship.relationship_type == 'indicates'
|
||||
assert relationship.source_ref == "indicator--01234567-89ab-cdef-0123-456789abcdef"
|
||||
assert relationship.target_ref == "malware--fedcba98-7654-3210-fedc-ba9876543210"
|
||||
assert relationship.source_ref == INDICATOR_ID
|
||||
assert relationship.target_ref == MALWARE_ID
|
||||
|
||||
assert relationship['type'] == 'relationship'
|
||||
assert relationship['id'].startswith('relationship--')
|
||||
assert relationship['created'] is not None
|
||||
assert relationship['modified'] is not None
|
||||
assert relationship['relationship_type'] == 'indicates'
|
||||
assert relationship['source_ref'] == "indicator--01234567-89ab-cdef-0123-456789abcdef"
|
||||
assert relationship['target_ref'] == "malware--fedcba98-7654-3210-fedc-ba9876543210"
|
||||
assert relationship['source_ref'] == INDICATOR_ID
|
||||
assert relationship['target_ref'] == MALWARE_ID
|
||||
|
||||
|
||||
def test_relationship_type_must_be_relationship():
|
||||
|
@ -310,7 +308,7 @@ def test_relationship_required_field_target_ref():
|
|||
# relationship_type and source_ref are checked first, so make sure those are provided
|
||||
relationship = stix2.Relationship(
|
||||
relationship_type='indicates',
|
||||
source_ref='indicator--01234567-89ab-cdef-0123-456789abcdef'
|
||||
source_ref=INDICATOR_ID
|
||||
)
|
||||
assert "Missing required field for Relationship: 'target_ref'." in str(excinfo)
|
||||
|
||||
|
|
Loading…
Reference in New Issue