more on issue 365

master
Rich Piazza 2020-03-19 14:16:48 -04:00
parent 9699c78ad8
commit 844ec2c3bf
2 changed files with 37 additions and 13 deletions

View File

@ -17,7 +17,7 @@ def _custom_object_builder(cls, type, properties, version):
if version == "2.0": if version == "2.0":
if not re.match(TYPE_REGEX, type): if not re.match(TYPE_REGEX, type):
raise ValueError( raise ValueError(
"Invalid type type name '%s': must only contain the " "Invalid type name '%s': must only contain the "
"characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % "characters a-z (lowercase ASCII), 0-9, and hyphen (-)." %
type, type,
) )
@ -39,7 +39,7 @@ def _custom_object_builder(cls, type, properties, version):
if version == "2.1": if version == "2.1":
for prop_name, prop in properties: for prop_name, prop in properties:
if not re.match(r'^[a-z]', prop_name): if not re.match(PREFIX_21_REGEX, prop_name):
raise ValueError("Property name %s must begin with an alpha character" % prop_name) raise ValueError("Property name %s must begin with an alpha character" % prop_name)
_type = type _type = type
@ -54,33 +54,46 @@ def _custom_object_builder(cls, type, properties, version):
def _custom_marking_builder(cls, type, properties, version): def _custom_marking_builder(cls, type, properties, version):
try:
prop_dict = OrderedDict(properties)
except TypeError as e:
six.raise_from(
ValueError(
"Marking properties must be dict-like, e.g. a list "
"containing tuples. For example, "
"[('property1', IntegerProperty())]",
),
e,
)
class _CustomMarking(cls, _STIXBase): class _CustomMarking(cls, _STIXBase):
if version == "2.0": if version == "2.0":
if not re.match(TYPE_REGEX, type): if not re.match(TYPE_REGEX, type):
raise ValueError( raise ValueError(
"Invalid type type name '%s': must only contain the " "Invalid marking type name '%s': must only contain the "
"characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % "characters a-z (lowercase ASCII), 0-9, and hyphen (-)." %
type, type,
) )
else: # 2.1+ else: # 2.1+
if not re.match(SCO21_TYPE_REGEX, type): if not re.match(SCO21_TYPE_REGEX, type):
raise ValueError( raise ValueError(
"Invalid type name '%s': must only contain the " "Invalid marking type name '%s': must only contain the "
"characters a-z (lowercase ASCII), 0-9, and hyphen (-) " "characters a-z (lowercase ASCII), 0-9, and hyphen (-) "
"and must begin with an a-z character" % type, "and must begin with an a-z character" % type,
) )
if len(type) < 3 or len(type) > 250: if len(type) < 3 or len(type) > 250:
raise ValueError( raise ValueError(
"Invalid type name '%s': must be between 3 and 250 characters." % type, "Invalid marking type name '%s': must be between 3 and 250 characters." % type,
) )
if not properties or not isinstance(properties, list): if not properties or not isinstance(properties, list):
raise ValueError("Must supply a list, containing tuples. For example, [('property1', IntegerProperty())]") raise ValueError("Must supply a list, containing tuples. For example, [('property1', IntegerProperty())]")
if version == "2.1": if version == "2.1 ":
for prop_name, prop in properties: for prop_name, prop_value in prop_dict.items():
if not re.match(PREFIX_21_REGEX, prop_name): if not re.match(PREFIX_21_REGEX, prop_name):
raise ValueError("Property name %s must begin with an alpha character." % prop_name) raise ValueError("Property name %s must begin with an alpha character." % prop_name)
@ -104,7 +117,7 @@ def _custom_observable_builder(cls, type, properties, version, id_contrib_props=
if version == "2.0": if version == "2.0":
if not re.match(TYPE_REGEX, type): if not re.match(TYPE_REGEX, type):
raise ValueError( raise ValueError(
"Invalid type type name '%s': must only contain the " "Invalid observable type name '%s': must only contain the "
"characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % "characters a-z (lowercase ASCII), 0-9, and hyphen (-)." %
type, type,
) )

View File

@ -292,6 +292,19 @@ def test_identity_custom_property_edit_markings():
identity2.clear_markings('x_foo') identity2.clear_markings('x_foo')
def test_invalid_custom_property_in_marking():
with pytest.raises(ValueError) as excinfo:
@stix2.v21.CustomMarking(
'x-new-obj', [
('9property1', stix2.properties.StringProperty(required=True)),
],
)
class NewObj():
pass
assert "Property names must begin with an alpha character." in str(excinfo.value)
def test_custom_marking_no_init_1(): def test_custom_marking_no_init_1():
@stix2.v21.CustomMarking( @stix2.v21.CustomMarking(
'x-new-obj', [ 'x-new-obj', [
@ -327,7 +340,7 @@ def test_custom_marking_invalid_type_name():
) )
class NewObj(object): class NewObj(object):
pass # pragma: no cover pass # pragma: no cover
assert "Invalid type name 'x': " in str(excinfo.value) assert "Invalid marking type name 'x': " in str(excinfo.value)
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
@stix2.v21.CustomMarking( @stix2.v21.CustomMarking(
@ -337,7 +350,7 @@ def test_custom_marking_invalid_type_name():
) )
class NewObj2(object): class NewObj2(object):
pass # pragma: no cover pass # pragma: no cover
assert "Invalid type name 'x_new_marking':" in str(excinfo.value) assert "Invalid marking type name 'x_new_marking':" in str(excinfo.value)
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
@stix2.v21.CustomMarking( @stix2.v21.CustomMarking(
@ -347,7 +360,7 @@ def test_custom_marking_invalid_type_name():
) )
class NewObj3(object): class NewObj3(object):
pass # pragma: no cover pass # pragma: no cover
assert "Invalid type name '7x-new-marking':" in str(excinfo.value) assert "Invalid marking type name '7x-new-marking':" in str(excinfo.value)
@stix2.v21.CustomObject( @stix2.v21.CustomObject(
@ -442,7 +455,6 @@ def test_custom_object_invalid_type_name():
assert "Invalid type name '7x-new-object':" in str(excinfo.value) assert "Invalid type name '7x-new-object':" in str(excinfo.value)
def test_parse_custom_object_type(): def test_parse_custom_object_type():
nt_string = """{ nt_string = """{
"type": "x-new-type", "type": "x-new-type",
@ -579,7 +591,6 @@ def test_custom_observable_object_invalid_type_name():
assert "Invalid observable type name '7x-new-obs':" in str(excinfo.value) assert "Invalid observable type name '7x-new-obs':" in str(excinfo.value)
def test_custom_observable_object_invalid_ref_property(): def test_custom_observable_object_invalid_ref_property():
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
@stix2.v21.CustomObservable( @stix2.v21.CustomObservable(