Merge branch 'master' of github.com:oasis-open/cti-python-stix2

master
chrisr3d 2019-01-10 09:51:41 +01:00
commit 83709358c3
7 changed files with 33 additions and 21 deletions

View File

@ -11,7 +11,7 @@ import uuid
from six import string_types, text_type from six import string_types, text_type
from stix2patterns.validator import run_validator from stix2patterns.validator import run_validator
from .base import _STIXBase from .base import _Observable, _STIXBase
from .core import STIX2_OBJ_MAPS, parse, parse_observable from .core import STIX2_OBJ_MAPS, parse, parse_observable
from .exceptions import CustomContentError, DictionaryKeyError from .exceptions import CustomContentError, DictionaryKeyError
from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime
@ -174,6 +174,19 @@ class ListProperty(Property):
return result return result
class CallableValues(list):
"""Wrapper to allow `values()` method on WindowsRegistryKey objects.
Needed because `values` is also a property.
"""
def __init__(self, parent_instance, *args, **kwargs):
self.parent_instance = parent_instance
super(CallableValues, self).__init__(*args, **kwargs)
def __call__(self):
return _Observable.values(self.parent_instance)
class StringProperty(Property): class StringProperty(Property):
def __init__(self, **kwargs): def __init__(self, **kwargs):
@ -302,8 +315,6 @@ class DictionaryProperty(Property):
dictified = _get_dict(value) dictified = _get_dict(value)
except ValueError: except ValueError:
raise ValueError("The dictionary property must contain a dictionary") raise ValueError("The dictionary property must contain a dictionary")
if dictified == {}:
raise ValueError("The dictionary property must contain a non-empty dictionary")
for k in dictified.keys(): for k in dictified.keys():
if self.spec_version == '2.0': if self.spec_version == '2.0':
if len(k) < 3: if len(k) < 3:
@ -513,8 +524,6 @@ class ExtensionsProperty(DictionaryProperty):
dictified = copy.deepcopy(dictified) dictified = copy.deepcopy(dictified)
except ValueError: except ValueError:
raise ValueError("The extensions property must contain a dictionary") raise ValueError("The extensions property must contain a dictionary")
if dictified == {}:
raise ValueError("The extensions property must contain a non-empty dictionary")
v = 'v' + self.spec_version.replace('.', '') v = 'v' + self.spec_version.replace('.', '')

View File

@ -1141,12 +1141,13 @@ def test_process_example_windows_process_ext_empty():
def test_process_example_extensions_empty(): def test_process_example_extensions_empty():
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo: proc = stix2.v20.Process(
stix2.v20.Process(extensions={}) pid=314,
name="foobar.exe",
extensions={},
)
assert excinfo.value.cls == stix2.v20.Process assert '{}' in str(proc)
assert excinfo.value.prop_name == 'extensions'
assert 'non-empty dictionary' in excinfo.value.reason
def test_process_example_with_WindowsProcessExt_Object(): def test_process_example_with_WindowsProcessExt_Object():
@ -1290,6 +1291,8 @@ def test_windows_registry_key_example():
assert w.values[0].name == "Foo" assert w.values[0].name == "Foo"
assert w.values[0].data == "qwerty" assert w.values[0].data == "qwerty"
assert w.values[0].data_type == "REG_SZ" assert w.values[0].data_type == "REG_SZ"
# ensure no errors in serialization because of 'values'
assert "Foo" in str(w)
def test_x509_certificate_example(): def test_x509_certificate_example():

View File

@ -360,7 +360,6 @@ def test_dictionary_property_invalid_key(d):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"d", [ "d", [
({}, "The dictionary property must contain a non-empty dictionary"),
# TODO: This error message could be made more helpful. The error is caused # TODO: This error message could be made more helpful. The error is caused
# because `json.loads()` doesn't like the *single* quotes around the key # because `json.loads()` doesn't like the *single* quotes around the key
# name, even though they are valid in a Python dictionary. While technically # name, even though they are valid in a Python dictionary. While technically

View File

@ -1115,12 +1115,12 @@ def test_process_example_windows_process_ext_empty():
def test_process_example_extensions_empty(): def test_process_example_extensions_empty():
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo: proc = stix2.v21.Process(
stix2.v21.Process(extensions={}) pid=314,
extensions={},
)
assert excinfo.value.cls == stix2.v21.Process assert '{}' in str(proc)
assert excinfo.value.prop_name == 'extensions'
assert 'non-empty dictionary' in excinfo.value.reason
def test_process_example_with_WindowsProcessExt_Object(): def test_process_example_with_WindowsProcessExt_Object():
@ -1264,6 +1264,8 @@ def test_windows_registry_key_example():
assert w.values[0].name == "Foo" assert w.values[0].name == "Foo"
assert w.values[0].data == "qwerty" assert w.values[0].data == "qwerty"
assert w.values[0].data_type == "REG_SZ" assert w.values[0].data_type == "REG_SZ"
# ensure no errors in serialization because of 'values'
assert "Foo" in str(w)
def test_x509_certificate_example(): def test_x509_certificate_example():

View File

@ -369,7 +369,6 @@ def test_dictionary_property_invalid_key(d):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"d", [ "d", [
({}, "The dictionary property must contain a non-empty dictionary"),
# TODO: This error message could be made more helpful. The error is caused # TODO: This error message could be made more helpful. The error is caused
# because `json.loads()` doesn't like the *single* quotes around the key # because `json.loads()` doesn't like the *single* quotes around the key
# name, even though they are valid in a Python dictionary. While technically # name, even though they are valid in a Python dictionary. While technically

View File

@ -12,7 +12,7 @@ from ..base import _Extension, _Observable, _STIXBase
from ..custom import _custom_extension_builder, _custom_observable_builder from ..custom import _custom_extension_builder, _custom_observable_builder
from ..exceptions import AtLeastOnePropertyError, DependentPropertiesError from ..exceptions import AtLeastOnePropertyError, DependentPropertiesError
from ..properties import ( from ..properties import (
BinaryProperty, BooleanProperty, DictionaryProperty, BinaryProperty, BooleanProperty, CallableValues, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty, EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty,
HashesProperty, HexProperty, IntegerProperty, ListProperty, HashesProperty, HexProperty, IntegerProperty, ListProperty,
ObjectReferenceProperty, StringProperty, TimestampProperty, TypeProperty, ObjectReferenceProperty, StringProperty, TimestampProperty, TypeProperty,
@ -726,7 +726,7 @@ class WindowsRegistryKey(_Observable):
@property @property
def values(self): def values(self):
# Needed because 'values' is a property on collections.Mapping objects # Needed because 'values' is a property on collections.Mapping objects
return self._inner['values'] return CallableValues(self, self._inner['values'])
class X509V3ExtenstionsType(_STIXBase): class X509V3ExtenstionsType(_STIXBase):

View File

@ -12,7 +12,7 @@ from ..base import _Extension, _Observable, _STIXBase
from ..custom import _custom_extension_builder, _custom_observable_builder from ..custom import _custom_extension_builder, _custom_observable_builder
from ..exceptions import AtLeastOnePropertyError, DependentPropertiesError from ..exceptions import AtLeastOnePropertyError, DependentPropertiesError
from ..properties import ( from ..properties import (
BinaryProperty, BooleanProperty, DictionaryProperty, BinaryProperty, BooleanProperty, CallableValues, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty, EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty,
HashesProperty, HexProperty, IntegerProperty, ListProperty, HashesProperty, HexProperty, IntegerProperty, ListProperty,
ObjectReferenceProperty, StringProperty, TimestampProperty, TypeProperty, ObjectReferenceProperty, StringProperty, TimestampProperty, TypeProperty,
@ -779,7 +779,7 @@ class WindowsRegistryKey(_Observable):
@property @property
def values(self): def values(self):
# Needed because 'values' is a property on collections.Mapping objects # Needed because 'values' is a property on collections.Mapping objects
return self._inner['values'] return CallableValues(self, self._inner['values'])
class X509V3ExtenstionsType(_STIXBase): class X509V3ExtenstionsType(_STIXBase):