116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
|
import stix2
|
||
|
from stix2.markings import TLP_WHITE
|
||
|
import pytest
|
||
|
|
||
|
EXPECTED_TLP_MARKING_DEFINITION = """{
|
||
|
"created": "2017-01-20T00:00:00.000Z",
|
||
|
"definition": {
|
||
|
"tlp": "white"
|
||
|
},
|
||
|
"definition_type": "tlp",
|
||
|
"id": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
"type": "marking-definition"
|
||
|
}"""
|
||
|
|
||
|
EXPECTED_STATEMENT_MARKING_DEFINITION = """{
|
||
|
"created": "2017-01-20T00:00:00.000Z",
|
||
|
"definition": {
|
||
|
"statement": "Copyright 2016, Example Corp"
|
||
|
},
|
||
|
"definition_type": "statement",
|
||
|
"id": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
"type": "marking-definition"
|
||
|
}"""
|
||
|
|
||
|
EXPECTED_GRANULAR_MARKING = """{
|
||
|
"marking_ref": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
"selectors": [
|
||
|
"abc",
|
||
|
"abc.[23]",
|
||
|
"abc.def",
|
||
|
"abc.[2].efg"
|
||
|
]
|
||
|
}"""
|
||
|
|
||
|
EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS = """{
|
||
|
"created": "2016-04-06T20:03:00.000Z",
|
||
|
"created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
|
||
|
"description": "Campaign by Green Group against a series of targets in the financial services sector.",
|
||
|
"granular_markings": [
|
||
|
{
|
||
|
"marking_ref": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
"selectors": [
|
||
|
"description"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
|
||
|
"modified": "2016-04-06T20:03:00.000Z",
|
||
|
"name": "Green Group Attacks Against Finance",
|
||
|
"type": "campaign"
|
||
|
}"""
|
||
|
|
||
|
|
||
|
def test_marking_def_example_with_tlp():
|
||
|
assert str(TLP_WHITE) == EXPECTED_TLP_MARKING_DEFINITION
|
||
|
|
||
|
|
||
|
def test_marking_def_example_with_statement():
|
||
|
marking_definition = stix2.MarkingDefinition(
|
||
|
id="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
created="2017-01-20T00:00:00.000Z",
|
||
|
definition_type="statement",
|
||
|
definition=stix2.StatementMarking(statement="Copyright 2016, Example Corp")
|
||
|
)
|
||
|
|
||
|
assert str(marking_definition) == EXPECTED_STATEMENT_MARKING_DEFINITION
|
||
|
|
||
|
|
||
|
def test_marking_def_example_with_positional_statement():
|
||
|
marking_definition = stix2.MarkingDefinition(
|
||
|
id="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
created="2017-01-20T00:00:00.000Z",
|
||
|
definition_type="statement",
|
||
|
definition=stix2.StatementMarking("Copyright 2016, Example Corp")
|
||
|
)
|
||
|
|
||
|
assert str(marking_definition) == EXPECTED_STATEMENT_MARKING_DEFINITION
|
||
|
|
||
|
|
||
|
def test_granular_example():
|
||
|
granular_marking = stix2.GranularMarking(
|
||
|
marking_ref="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
selectors=["abc", "abc.[23]", "abc.def", "abc.[2].efg"]
|
||
|
)
|
||
|
|
||
|
assert str(granular_marking) == EXPECTED_GRANULAR_MARKING
|
||
|
|
||
|
|
||
|
def test_granular_example_with_bad_selector():
|
||
|
with pytest.raises(ValueError) as excinfo:
|
||
|
stix2.GranularMarking(
|
||
|
marking_ref="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
selectors=["abc[0]"] # missing "."
|
||
|
)
|
||
|
|
||
|
assert str(excinfo.value) == "Invalid value for GranularMarking 'selectors': values must adhere to selector syntax"
|
||
|
|
||
|
|
||
|
def test_campaign_with_granular_markings_example():
|
||
|
campaign = stix2.Campaign(
|
||
|
id="campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
|
||
|
created_by_ref="identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
|
||
|
created="2016-04-06T20:03:00.000Z",
|
||
|
modified="2016-04-06T20:03:00.000Z",
|
||
|
name="Green Group Attacks Against Finance",
|
||
|
description="Campaign by Green Group against a series of targets in the financial services sector.",
|
||
|
granular_markings=[
|
||
|
stix2.GranularMarking(
|
||
|
marking_ref="marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
||
|
selectors=["description"])
|
||
|
])
|
||
|
print(str(campaign))
|
||
|
assert str(campaign) == EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS
|
||
|
|
||
|
# TODO: Add other examples
|