Fix deterministic ID behavior when an empty "hashes" property is
given. Now, an exception is raised. Added a unit test for this.pull/1/head
parent
c94feae623
commit
2179028c91
|
@ -410,9 +410,12 @@ class _Observable(_STIXBase):
|
||||||
obj_value = self[key]
|
obj_value = self[key]
|
||||||
|
|
||||||
if key == "hashes":
|
if key == "hashes":
|
||||||
possible_hash = _choose_one_hash(obj_value)
|
serializable_value = _choose_one_hash(obj_value)
|
||||||
if possible_hash:
|
|
||||||
serializable_value = possible_hash
|
if serializable_value is None:
|
||||||
|
raise InvalidValueError(
|
||||||
|
self, key, "No hashes given",
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
serializable_value = _make_json_serializable(obj_value)
|
serializable_value = _make_json_serializable(obj_value)
|
||||||
|
|
|
@ -2,14 +2,17 @@ from collections import OrderedDict
|
||||||
import datetime
|
import datetime
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import pytest
|
||||||
import six
|
import six
|
||||||
|
|
||||||
import stix2.base
|
import stix2.base
|
||||||
import stix2.canonicalization.Canonicalize
|
import stix2.canonicalization.Canonicalize
|
||||||
|
import stix2.exceptions
|
||||||
from stix2.properties import (
|
from stix2.properties import (
|
||||||
BooleanProperty, DictionaryProperty, EmbeddedObjectProperty,
|
BooleanProperty, DictionaryProperty, EmbeddedObjectProperty,
|
||||||
ExtensionsProperty, FloatProperty, IDProperty, IntegerProperty,
|
ExtensionsProperty, FloatProperty, HashesProperty, IDProperty,
|
||||||
ListProperty, StringProperty, TimestampProperty, TypeProperty,
|
IntegerProperty, ListProperty, StringProperty, TimestampProperty,
|
||||||
|
TypeProperty,
|
||||||
)
|
)
|
||||||
import stix2.v21.base
|
import stix2.v21.base
|
||||||
|
|
||||||
|
@ -188,3 +191,22 @@ def test_embedded_object():
|
||||||
actual_uuid5 = _uuid_from_id(sco["id"])
|
actual_uuid5 = _uuid_from_id(sco["id"])
|
||||||
|
|
||||||
assert actual_uuid5 == expected_uuid5
|
assert actual_uuid5 == expected_uuid5
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_hash():
|
||||||
|
class SomeSCO(stix2.v21.base._Observable):
|
||||||
|
_type = "some-sco"
|
||||||
|
_properties = OrderedDict((
|
||||||
|
('type', TypeProperty(_type, spec_version='2.1')),
|
||||||
|
('id', IDProperty(_type, spec_version='2.1')),
|
||||||
|
(
|
||||||
|
'extensions', ExtensionsProperty(
|
||||||
|
spec_version='2.1', enclosing_type=_type,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
('hashes', HashesProperty()),
|
||||||
|
))
|
||||||
|
_id_contributing_properties = ['hashes']
|
||||||
|
|
||||||
|
with pytest.raises(stix2.exceptions.InvalidValueError):
|
||||||
|
SomeSCO(hashes={})
|
||||||
|
|
Loading…
Reference in New Issue