2017-02-02 17:58:46 +01:00
|
|
|
"""Tests for stix.ExternalReference"""
|
|
|
|
|
2017-04-17 20:15:01 +02:00
|
|
|
import re
|
2017-02-02 17:58:46 +01:00
|
|
|
|
2017-04-25 00:29:56 +02:00
|
|
|
import pytest
|
2017-05-09 21:10:53 +02:00
|
|
|
|
2017-02-02 17:58:46 +01:00
|
|
|
import stix2
|
|
|
|
|
|
|
|
VERIS = """{
|
|
|
|
"source_name": "veris",
|
2017-08-15 19:41:51 +02:00
|
|
|
"url": "https://github.com/vz-risk/VCDB/blob/master/data/json/0001AA7F-C601-424A-B2B8-BE6C9F5164E7.json",
|
|
|
|
"hashes": {
|
|
|
|
"SHA-256": "6db12788c37247f2316052e142f42f4b259d6561751e5f401a1ae2a6df9c674b"
|
|
|
|
},
|
|
|
|
"external_id": "0001AA7F-C601-424A-B2B8-BE6C9F5164E7"
|
2017-02-02 17:58:46 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_veris():
|
2018-07-05 21:23:25 +02:00
|
|
|
ref = stix2.v20.ExternalReference(
|
2017-02-02 17:58:46 +01:00
|
|
|
source_name="veris",
|
|
|
|
external_id="0001AA7F-C601-424A-B2B8-BE6C9F5164E7",
|
2017-08-15 19:41:51 +02:00
|
|
|
hashes={
|
|
|
|
"SHA-256": "6db12788c37247f2316052e142f42f4b259d6561751e5f401a1ae2a6df9c674b"
|
|
|
|
},
|
2017-02-02 17:58:46 +01:00
|
|
|
url="https://github.com/vz-risk/VCDB/blob/master/data/json/0001AA7F-C601-424A-B2B8-BE6C9F5164E7.json",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ref) == VERIS
|
|
|
|
|
|
|
|
|
|
|
|
CAPEC = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"source_name": "capec",
|
|
|
|
"external_id": "CAPEC-550"
|
2017-02-02 17:58:46 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_capec():
|
2018-07-05 21:23:25 +02:00
|
|
|
ref = stix2.v20.ExternalReference(
|
2017-02-02 17:58:46 +01:00
|
|
|
source_name="capec",
|
|
|
|
external_id="CAPEC-550",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ref) == CAPEC
|
2018-04-11 21:46:17 +02:00
|
|
|
assert re.match("ExternalReference\\(source_name=u?'capec', external_id=u?'CAPEC-550'\\)", repr(ref))
|
2017-02-02 17:58:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
CAPEC_URL = """{
|
|
|
|
"source_name": "capec",
|
2017-08-15 19:41:51 +02:00
|
|
|
"url": "http://capec.mitre.org/data/definitions/550.html",
|
|
|
|
"external_id": "CAPEC-550"
|
2017-02-02 17:58:46 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_capec_url():
|
2018-07-05 21:23:25 +02:00
|
|
|
ref = stix2.v20.ExternalReference(
|
2017-02-02 17:58:46 +01:00
|
|
|
source_name="capec",
|
|
|
|
external_id="CAPEC-550",
|
|
|
|
url="http://capec.mitre.org/data/definitions/550.html",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ref) == CAPEC_URL
|
|
|
|
|
|
|
|
|
|
|
|
THREAT_REPORT = """{
|
|
|
|
"source_name": "ACME Threat Intel",
|
2017-08-15 19:41:51 +02:00
|
|
|
"description": "Threat report",
|
2017-02-02 17:58:46 +01:00
|
|
|
"url": "http://www.example.com/threat-report.pdf"
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_threat_report():
|
2018-07-05 21:23:25 +02:00
|
|
|
ref = stix2.v20.ExternalReference(
|
2017-02-02 17:58:46 +01:00
|
|
|
source_name="ACME Threat Intel",
|
|
|
|
description="Threat report",
|
|
|
|
url="http://www.example.com/threat-report.pdf",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ref) == THREAT_REPORT
|
|
|
|
|
|
|
|
|
|
|
|
BUGZILLA = """{
|
|
|
|
"source_name": "ACME Bugzilla",
|
2017-08-15 19:41:51 +02:00
|
|
|
"url": "https://www.example.com/bugs/1370",
|
|
|
|
"external_id": "1370"
|
2017-02-02 17:58:46 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_bugzilla():
|
2018-07-05 21:23:25 +02:00
|
|
|
ref = stix2.v20.ExternalReference(
|
2017-02-02 17:58:46 +01:00
|
|
|
source_name="ACME Bugzilla",
|
|
|
|
external_id="1370",
|
|
|
|
url="https://www.example.com/bugs/1370",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ref) == BUGZILLA
|
|
|
|
|
|
|
|
|
|
|
|
OFFLINE = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"source_name": "ACME Threat Intel",
|
|
|
|
"description": "Threat report"
|
2017-02-02 17:58:46 +01:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_offline():
|
2018-07-05 21:23:25 +02:00
|
|
|
ref = stix2.v20.ExternalReference(
|
2017-02-02 17:58:46 +01:00
|
|
|
source_name="ACME Threat Intel",
|
|
|
|
description="Threat report",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ref) == OFFLINE
|
2018-04-11 21:46:17 +02:00
|
|
|
assert re.match("ExternalReference\\(source_name=u?'ACME Threat Intel', description=u?'Threat report'\\)", repr(ref))
|
2017-02-10 23:09:37 +01:00
|
|
|
# Yikes! This works
|
|
|
|
assert eval("stix2." + repr(ref)) == ref
|
2017-02-02 17:58:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_external_reference_source_required():
|
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.ExternalReference()
|
2017-04-18 21:41:18 +02:00
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.ExternalReference
|
2017-05-16 18:27:30 +02:00
|
|
|
assert excinfo.value.properties == ["source_name"]
|