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