add tests to cover the language aspect of the markings

master
Emmanuelle Vargas-Gonzalez 2019-04-22 15:26:21 -04:00
parent 4bbabaecb2
commit 0c78acafd0
3 changed files with 272 additions and 3 deletions

View File

@ -31,6 +31,12 @@ MARKING_IDS = [
"marking-definition--68520ae2-fefe-43a9-84ee-2c2a934d2c7d", "marking-definition--68520ae2-fefe-43a9-84ee-2c2a934d2c7d",
"marking-definition--2802dfb1-1019-40a8-8848-68d0ec0e417f", "marking-definition--2802dfb1-1019-40a8-8848-68d0ec0e417f",
] ]
MARKING_LANGS = [
"en",
"es",
"de",
"ja",
]
RELATIONSHIP_IDS = [ RELATIONSHIP_IDS = [
'relationship--06520621-5352-4e6a-b976-e8fa3d437ffd', 'relationship--06520621-5352-4e6a-b976-e8fa3d437ffd',
'relationship--181c9c09-43e6-45dd-9374-3bec192f05ef', 'relationship--181c9c09-43e6-45dd-9374-3bec192f05ef',

View File

@ -5,7 +5,7 @@ from stix2.exceptions import MarkingNotFoundError
from stix2.v21 import TLP_RED, Malware from stix2.v21 import TLP_RED, Malware
from .constants import MALWARE_MORE_KWARGS as MALWARE_KWARGS_CONST from .constants import MALWARE_MORE_KWARGS as MALWARE_KWARGS_CONST
from .constants import MARKING_IDS from .constants import MARKING_IDS, MARKING_LANGS
"""Tests for the Data Markings API.""" """Tests for the Data Markings API."""
@ -111,6 +111,37 @@ def test_add_marking_mark_multiple_selector_multiple_refs():
assert m in after["granular_markings"] assert m in after["granular_markings"]
def test_add_marking_mark_multiple_selector_multiple_refs_mixed():
before = Malware(
**MALWARE_KWARGS
)
after = Malware(
granular_markings=[
{
"selectors": ["description", "name"],
"marking_ref": MARKING_IDS[0],
},
{
"selectors": ["description", "name"],
"marking_ref": MARKING_IDS[1],
},
{
"selectors": ["description", "name"],
"lang": MARKING_LANGS[0],
},
{
"selectors": ["description", "name"],
"lang": MARKING_LANGS[1],
},
],
**MALWARE_KWARGS
)
before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1], MARKING_LANGS[0], MARKING_LANGS[1]], ["description", "name"])
for m in before["granular_markings"]:
assert m in after["granular_markings"]
def test_add_marking_mark_another_property_same_marking(): def test_add_marking_mark_another_property_same_marking():
before = Malware( before = Malware(
granular_markings=[ granular_markings=[
@ -376,6 +407,98 @@ def test_get_markings_positional_arguments_combinations(data):
assert set(markings.get_markings(data, "x.z.foo2", False, True)) == set(["10"]) assert set(markings.get_markings(data, "x.z.foo2", False, True)) == set(["10"])
GET_MARKINGS_TEST_DATA_LANGS = {
"a": 333,
"b": "value",
"c": [
17,
"list value",
{
"g": "nested",
"h": 45,
},
],
"x": {
"y": [
"hello",
88,
],
"z": {
"foo1": "bar",
"foo2": 65,
},
},
"granular_markings": [
{
"marking_ref": "m1",
"selectors": ["a"],
},
{
"marking_ref": "m2",
"selectors": ["c"],
},
{
"marking_ref": "m3",
"selectors": ["c.[1]"],
},
{
"marking_ref": "m4",
"selectors": ["c.[2]"],
},
{
"marking_ref": "m5",
"selectors": ["c.[2].g"],
},
{
"marking_ref": "m6",
"selectors": ["x"],
},
{
"lang": "l7",
"selectors": ["x.y"],
},
{
"marking_ref": "m8",
"selectors": ["x.y.[1]"],
},
{
"lang": "l9",
"selectors": ["x.z"],
},
{
"marking_ref": "m9",
"selectors": ["x.z"],
},
{
"marking_ref": "m10",
"selectors": ["x.z.foo2"],
},
],
}
@pytest.mark.parametrize("data", [GET_MARKINGS_TEST_DATA_LANGS])
def test_get_markings_multiple_selectors(data):
"""Test multiple selectors return combination of markings."""
total = markings.get_markings(data, ["x.y", "x.z"])
xy_markings = markings.get_markings(data, ["x.y"])
xz_markings = markings.get_markings(data, ["x.z"])
assert set(xy_markings).issubset(total)
assert set(xz_markings).issubset(total)
assert set(xy_markings).union(xz_markings).issuperset(total)
@pytest.mark.parametrize("data", [GET_MARKINGS_TEST_DATA_LANGS])
def test_get_markings_multiple_selectors_with_options(data):
"""Test multiple selectors return combination of markings."""
total = markings.get_markings(data, ["x.y", "x.z"], lang=False)
xz_markings = markings.get_markings(data, ["x.z"], marking_ref=False)
assert len(total) == 1
assert len(xz_markings) == 1
@pytest.mark.parametrize( @pytest.mark.parametrize(
"data", [ "data", [
( (
@ -455,6 +578,38 @@ def test_remove_marking_mark_one_selector_from_multiple_ones():
assert m in after["granular_markings"] assert m in after["granular_markings"]
def test_remove_marking_mark_one_selector_from_multiple_ones_mixed():
after = Malware(
granular_markings=[
{
"selectors": ["description"],
"marking_ref": MARKING_IDS[0],
},
{
"selectors": ["description"],
"lang": MARKING_LANGS[0],
},
],
**MALWARE_KWARGS
)
before = Malware(
granular_markings=[
{
"selectors": ["description", "modified"],
"marking_ref": MARKING_IDS[0],
},
{
"selectors": ["description", "modified"],
"lang": MARKING_LANGS[0],
},
],
**MALWARE_KWARGS
)
before = markings.remove_markings(before, [MARKING_IDS[0], MARKING_LANGS[0]], ["modified"])
for m in before["granular_markings"]:
assert m in after["granular_markings"]
def test_remove_marking_mark_one_selector_markings_from_multiple_ones(): def test_remove_marking_mark_one_selector_markings_from_multiple_ones():
after = Malware( after = Malware(
granular_markings=[ granular_markings=[
@ -592,6 +747,10 @@ IS_MARKED_TEST_DATA = [
"selectors": ["malware_types", "description"], "selectors": ["malware_types", "description"],
"marking_ref": MARKING_IDS[3], "marking_ref": MARKING_IDS[3],
}, },
{
"selectors": ["name"],
"lang": MARKING_LANGS[1],
},
], ],
**MALWARE_KWARGS **MALWARE_KWARGS
), ),
@ -609,6 +768,10 @@ IS_MARKED_TEST_DATA = [
"selectors": ["malware_types", "description"], "selectors": ["malware_types", "description"],
"marking_ref": MARKING_IDS[3], "marking_ref": MARKING_IDS[3],
}, },
{
"selectors": ["name"],
"lang": MARKING_LANGS[1],
},
], ],
**MALWARE_KWARGS **MALWARE_KWARGS
), ),
@ -620,6 +783,7 @@ def test_is_marked_smoke(data):
"""Smoke test is_marked call does not fail.""" """Smoke test is_marked call does not fail."""
assert markings.is_marked(data, selectors=["description"]) assert markings.is_marked(data, selectors=["description"])
assert markings.is_marked(data, selectors=["modified"]) is False assert markings.is_marked(data, selectors=["modified"]) is False
assert markings.is_marked(data, selectors=["name"])
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -666,6 +830,7 @@ def test_is_marked_valid_selector_and_refs(data):
"""Test that a valid selector returns True when marking_refs match.""" """Test that a valid selector returns True when marking_refs match."""
assert markings.is_marked(data, [MARKING_IDS[1]], ["description"]) assert markings.is_marked(data, [MARKING_IDS[1]], ["description"])
assert markings.is_marked(data, [MARKING_IDS[1]], ["modified"]) is False assert markings.is_marked(data, [MARKING_IDS[1]], ["modified"]) is False
assert markings.is_marked(data, [MARKING_LANGS[1]], ["name"])
@pytest.mark.parametrize("data", IS_MARKED_TEST_DATA) @pytest.mark.parametrize("data", IS_MARKED_TEST_DATA)
@ -870,6 +1035,28 @@ def test_set_marking_mark_one_selector_multiple_refs():
assert m in after["granular_markings"] assert m in after["granular_markings"]
def test_set_marking_mark_one_selector_multiple_lang_refs():
before = Malware(
**MALWARE_KWARGS
)
after = Malware(
granular_markings=[
{
"selectors": ["description"],
"lang": MARKING_LANGS[0],
},
{
"selectors": ["description"],
"lang": MARKING_LANGS[1],
},
],
**MALWARE_KWARGS
)
before = markings.set_markings(before, [MARKING_LANGS[0], MARKING_LANGS[1]], ["description"])
for m in before["granular_markings"]:
assert m in after["granular_markings"]
def test_set_marking_mark_multiple_selector_one_refs(): def test_set_marking_mark_multiple_selector_one_refs():
before = Malware( before = Malware(
granular_markings=[ granular_markings=[
@ -894,6 +1081,38 @@ def test_set_marking_mark_multiple_selector_one_refs():
assert m in after["granular_markings"] assert m in after["granular_markings"]
def test_set_marking_mark_multiple_mixed_markings():
before = Malware(
granular_markings=[
{
"selectors": ["description", "modified"],
"marking_ref": MARKING_IDS[1],
},
{
"selectors": ["description", "modified"],
"lang": MARKING_LANGS[2],
},
],
**MALWARE_KWARGS
)
after = Malware(
granular_markings=[
{
"selectors": ["description", "modified"],
"marking_ref": MARKING_IDS[2],
},
{
"selectors": ["description", "modified"],
"lang": MARKING_LANGS[3],
},
],
**MALWARE_KWARGS
)
before = markings.set_markings(before, [MARKING_IDS[2], MARKING_LANGS[3]], ["description", "modified"])
for m in before["granular_markings"]:
assert m in after["granular_markings"]
def test_set_marking_mark_multiple_selector_multiple_refs_from_none(): def test_set_marking_mark_multiple_selector_multiple_refs_from_none():
before = Malware( before = Malware(
**MALWARE_KWARGS **MALWARE_KWARGS

View File

@ -54,7 +54,7 @@ EXPECTED_GRANULAR_MARKING = """{
] ]
}""" }"""
EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS = """{ EXPECTED_CAMPAIGN_WITH_GRANULAR_REF_MARKINGS = """{
"type": "campaign", "type": "campaign",
"spec_version": "2.1", "spec_version": "2.1",
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f", "id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
@ -74,6 +74,27 @@ EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS = """{
}""" }"""
EXPECTED_CAMPAIGN_WITH_GRANULAR_LANG_MARKINGS = """{
"type": "campaign",
"spec_version": "2.1",
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
"created_by_ref": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
"created": "2016-04-06T20:03:00.000Z",
"modified": "2016-04-06T20:03:00.000Z",
"name": "Bank Attack",
"description": "Weitere Informationen über Banküberfall",
"lang": "en",
"granular_markings": [
{
"lang": "de",
"selectors": [
"description"
]
}
]
}"""
def test_marking_def_example_with_tlp(): def test_marking_def_example_with_tlp():
assert str(TLP_WHITE) == EXPECTED_TLP_MARKING_DEFINITION assert str(TLP_WHITE) == EXPECTED_TLP_MARKING_DEFINITION
@ -161,7 +182,7 @@ def test_campaign_with_granular_markings_example():
), ),
], ],
) )
assert str(campaign) == EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS assert str(campaign) == EXPECTED_CAMPAIGN_WITH_GRANULAR_REF_MARKINGS
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -273,3 +294,26 @@ def test_campaign_add_markings():
) )
campaign = campaign.add_markings(TLP_WHITE) campaign = campaign.add_markings(TLP_WHITE)
assert campaign.object_marking_refs[0] == TLP_WHITE.id assert campaign.object_marking_refs[0] == TLP_WHITE.id
def test_campaign_with_granular_lang_markings_example():
campaign = stix2.v21.Campaign(
id="campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
created_by_ref=IDENTITY_ID,
created="2016-04-06T20:03:00Z",
modified="2016-04-06T20:03:00Z",
name="Bank Attack",
lang="en",
description="Weitere Informationen über Banküberfall",
granular_markings=[
stix2.v21.GranularMarking(
lang="de",
selectors=["description"],
),
],
)
# In order to provide the same representation, we need to disable escaping
# in json.dumps(). https://docs.python.org/3/library/json.html#json.dumps
# or https://docs.python.org/2/library/json.html#json.dumps
assert campaign.serialize(pretty=True, ensure_ascii=False) == EXPECTED_CAMPAIGN_WITH_GRANULAR_LANG_MARKINGS