2017-03-22 13:46:39 +01:00
|
|
|
import datetime as dt
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
import stix2
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
from .constants import (
|
|
|
|
FAKE_TIME, INDICATOR_ID, MALWARE_ID, RELATIONSHIP_ID, RELATIONSHIP_KWARGS,
|
|
|
|
)
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
EXPECTED_RELATIONSHIP = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"type": "relationship",
|
2018-06-27 18:34:49 +02:00
|
|
|
"id": "relationship--df7c87eb-75d2-4948-af81-9d49d246f301",
|
2017-08-15 19:41:51 +02:00
|
|
|
"created": "2016-04-06T20:06:37.000Z",
|
2017-06-23 00:47:35 +02:00
|
|
|
"modified": "2016-04-06T20:06:37.000Z",
|
2017-03-22 13:46:39 +01:00
|
|
|
"relationship_type": "indicates",
|
2018-06-27 18:34:49 +02:00
|
|
|
"source_ref": "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7",
|
|
|
|
"target_ref": "malware--9c4638ec-f1de-4ddb-abf4-1b760417654e"
|
2017-03-22 13:46:39 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
2017-05-16 18:27:30 +02:00
|
|
|
def test_relationship_all_required_properties():
|
2017-03-22 13:46:39 +01:00
|
|
|
now = dt.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)
|
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
rel = stix2.v20.Relationship(
|
2017-03-22 13:46:39 +01:00
|
|
|
type='relationship',
|
|
|
|
id=RELATIONSHIP_ID,
|
|
|
|
created=now,
|
|
|
|
modified=now,
|
|
|
|
relationship_type='indicates',
|
|
|
|
source_ref=INDICATOR_ID,
|
|
|
|
target_ref=MALWARE_ID,
|
|
|
|
)
|
2017-03-22 14:05:59 +01:00
|
|
|
assert str(rel) == EXPECTED_RELATIONSHIP
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
2017-05-16 18:27:30 +02:00
|
|
|
def test_relationship_autogenerated_properties(relationship):
|
2017-03-22 13:46:39 +01:00
|
|
|
assert relationship.type == 'relationship'
|
2018-06-27 18:34:49 +02:00
|
|
|
assert relationship.id == 'relationship--00000000-0000-4000-8000-000000000001'
|
2017-03-22 13:46:39 +01:00
|
|
|
assert relationship.created == FAKE_TIME
|
|
|
|
assert relationship.modified == FAKE_TIME
|
|
|
|
assert relationship.relationship_type == 'indicates'
|
|
|
|
assert relationship.source_ref == INDICATOR_ID
|
|
|
|
assert relationship.target_ref == MALWARE_ID
|
|
|
|
|
|
|
|
assert relationship['type'] == 'relationship'
|
2018-06-27 18:34:49 +02:00
|
|
|
assert relationship['id'] == 'relationship--00000000-0000-4000-8000-000000000001'
|
2017-03-22 13:46:39 +01:00
|
|
|
assert relationship['created'] == FAKE_TIME
|
|
|
|
assert relationship['modified'] == FAKE_TIME
|
|
|
|
assert relationship['relationship_type'] == 'indicates'
|
|
|
|
assert relationship['source_ref'] == INDICATOR_ID
|
|
|
|
assert relationship['target_ref'] == MALWARE_ID
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationship_type_must_be_relationship():
|
2017-04-18 21:42:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.Relationship(type='xxx', **RELATIONSHIP_KWARGS)
|
2017-03-22 13:46:39 +01:00
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.Relationship
|
2017-04-18 21:19:16 +02:00
|
|
|
assert excinfo.value.prop_name == "type"
|
|
|
|
assert excinfo.value.reason == "must equal 'relationship'."
|
2017-03-22 13:46:39 +01:00
|
|
|
assert str(excinfo.value) == "Invalid value for Relationship 'type': must equal 'relationship'."
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationship_id_must_start_with_relationship():
|
2017-04-18 21:42:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.Relationship(id='my-prefix--', **RELATIONSHIP_KWARGS)
|
2017-03-22 13:46:39 +01:00
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.Relationship
|
2017-04-18 21:19:16 +02:00
|
|
|
assert excinfo.value.prop_name == "id"
|
|
|
|
assert excinfo.value.reason == "must start with 'relationship--'."
|
2017-03-22 13:46:39 +01:00
|
|
|
assert str(excinfo.value) == "Invalid value for Relationship 'id': must start with 'relationship--'."
|
|
|
|
|
|
|
|
|
2017-05-16 18:27:30 +02:00
|
|
|
def test_relationship_required_property_relationship_type():
|
2017-05-19 19:51:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.Relationship()
|
|
|
|
assert excinfo.value.cls == stix2.v20.Relationship
|
2017-05-16 18:27:30 +02:00
|
|
|
assert excinfo.value.properties == ["relationship_type", "source_ref", "target_ref"]
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
2017-05-16 18:27:30 +02:00
|
|
|
def test_relationship_missing_some_required_properties():
|
2017-05-19 19:51:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.Relationship(relationship_type='indicates')
|
2017-05-04 22:34:08 +02:00
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.Relationship
|
2017-05-16 18:27:30 +02:00
|
|
|
assert excinfo.value.properties == ["source_ref", "target_ref"]
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
2017-05-16 18:27:30 +02:00
|
|
|
def test_relationship_required_properties_target_ref():
|
2017-05-19 19:51:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.Relationship(
|
2017-03-22 13:46:39 +01:00
|
|
|
relationship_type='indicates',
|
2018-07-13 17:10:05 +02:00
|
|
|
source_ref=INDICATOR_ID,
|
2017-03-22 13:46:39 +01:00
|
|
|
)
|
2017-04-18 21:41:18 +02:00
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.Relationship
|
2017-05-16 18:27:30 +02:00
|
|
|
assert excinfo.value.properties == ["target_ref"]
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
2017-04-07 22:36:42 +02:00
|
|
|
def test_cannot_assign_to_relationship_attributes(relationship):
|
2017-04-18 22:05:20 +02:00
|
|
|
with pytest.raises(stix2.exceptions.ImmutableError) as excinfo:
|
2017-03-22 13:46:39 +01:00
|
|
|
relationship.relationship_type = "derived-from"
|
|
|
|
|
2017-06-02 13:34:37 +02:00
|
|
|
assert str(excinfo.value) == "Cannot modify 'relationship_type' property in 'Relationship' after creation."
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_kwarg_to_relationship():
|
2017-05-19 19:51:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.ExtraPropertiesError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.Relationship(my_custom_property="foo", **RELATIONSHIP_KWARGS)
|
2017-04-18 21:56:16 +02:00
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.Relationship
|
2017-05-16 18:27:30 +02:00
|
|
|
assert excinfo.value.properties == ['my_custom_property']
|
|
|
|
assert str(excinfo.value) == "Unexpected properties for Relationship: (my_custom_property)."
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
2017-04-07 22:36:42 +02:00
|
|
|
def test_create_relationship_from_objects_rather_than_ids(indicator, malware):
|
2018-07-05 21:23:25 +02:00
|
|
|
rel = stix2.v20.Relationship(
|
2017-03-22 13:46:39 +01:00
|
|
|
relationship_type="indicates",
|
|
|
|
source_ref=indicator,
|
|
|
|
target_ref=malware,
|
|
|
|
)
|
|
|
|
|
2017-03-22 14:05:59 +01:00
|
|
|
assert rel.relationship_type == 'indicates'
|
2018-06-27 18:34:49 +02:00
|
|
|
assert rel.source_ref == 'indicator--00000000-0000-4000-8000-000000000001'
|
|
|
|
assert rel.target_ref == 'malware--00000000-0000-4000-8000-000000000003'
|
|
|
|
assert rel.id == 'relationship--00000000-0000-4000-8000-000000000005'
|
2017-03-22 13:46:39 +01:00
|
|
|
|
|
|
|
|
2017-04-07 22:36:42 +02:00
|
|
|
def test_create_relationship_with_positional_args(indicator, malware):
|
2018-07-05 21:23:25 +02:00
|
|
|
rel = stix2.v20.Relationship(indicator, 'indicates', malware)
|
2017-03-22 13:46:39 +01:00
|
|
|
|
2017-03-22 14:05:59 +01:00
|
|
|
assert rel.relationship_type == 'indicates'
|
2018-06-27 18:34:49 +02:00
|
|
|
assert rel.source_ref == 'indicator--00000000-0000-4000-8000-000000000001'
|
|
|
|
assert rel.target_ref == 'malware--00000000-0000-4000-8000-000000000003'
|
|
|
|
assert rel.id == 'relationship--00000000-0000-4000-8000-000000000005'
|
2017-04-19 15:22:08 +02:00
|
|
|
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED_RELATIONSHIP,
|
|
|
|
{
|
|
|
|
"created": "2016-04-06T20:06:37Z",
|
|
|
|
"id": "relationship--df7c87eb-75d2-4948-af81-9d49d246f301",
|
|
|
|
"modified": "2016-04-06T20:06:37Z",
|
|
|
|
"relationship_type": "indicates",
|
|
|
|
"source_ref": "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7",
|
|
|
|
"target_ref": "malware--9c4638ec-f1de-4ddb-abf4-1b760417654e",
|
|
|
|
"type": "relationship",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2017-04-19 15:22:08 +02:00
|
|
|
def test_parse_relationship(data):
|
2018-07-05 21:23:25 +02:00
|
|
|
rel = stix2.parse(data, version="2.0")
|
2017-04-19 15:22:08 +02:00
|
|
|
|
|
|
|
assert rel.type == 'relationship'
|
|
|
|
assert rel.id == RELATIONSHIP_ID
|
|
|
|
assert rel.created == dt.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)
|
|
|
|
assert rel.modified == dt.datetime(2016, 4, 6, 20, 6, 37, tzinfo=pytz.utc)
|
|
|
|
assert rel.relationship_type == "indicates"
|
2018-06-27 18:34:49 +02:00
|
|
|
assert rel.source_ref == "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7"
|
|
|
|
assert rel.target_ref == "malware--9c4638ec-f1de-4ddb-abf4-1b760417654e"
|