Use StringProperty and ListProperty in Malware objects,

fix bugs in those properties
stix2.1
clenk 2017-04-07 14:53:40 -04:00
parent 474833248d
commit 93b8076ae3
3 changed files with 17 additions and 8 deletions

View File

@ -75,7 +75,7 @@ class Property(object):
class ListProperty(Property): class ListProperty(Property):
def __init__(self, contained): def __init__(self, contained, **kwargs):
""" """
contained should be a type whose constructor creates an object from the value contained should be a type whose constructor creates an object from the value
""" """
@ -85,6 +85,7 @@ class ListProperty(Property):
self.contained = bool self.contained = bool
else: else:
self.contained = contained self.contained = contained
super(ListProperty, self).__init__(**kwargs)
def validate(self, value): def validate(self, value):
try: try:
@ -108,7 +109,6 @@ class ListProperty(Property):
return list_ return list_
def clean(self, value): def clean(self, value):
return [self.contained(x) for x in value]
try: try:
return [self.contained(x) for x in value] return [self.contained(x) for x in value]
except TypeError: except TypeError:
@ -117,12 +117,12 @@ class ListProperty(Property):
class StringProperty(Property): class StringProperty(Property):
def __init__(self): def __init__(self, **kwargs):
if sys.version_info[0] == 2: if sys.version_info[0] == 2:
self.string_type = unicode self.string_type = unicode
else: else:
self.string_type = str self.string_type = str
super(StringProperty, self).__init__() super(StringProperty, self).__init__(**kwargs)
def clean(self, value): def clean(self, value):
return self.string_type(value) return self.string_type(value)

View File

@ -2,7 +2,8 @@
from .base import _STIXBase from .base import _STIXBase
from .common import COMMON_PROPERTIES from .common import COMMON_PROPERTIES
from .properties import IDProperty, TypeProperty, Property from .properties import (StringProperty, IDProperty, ListProperty,
TypeProperty, Property)
from .utils import NOW from .utils import NOW
@ -105,9 +106,9 @@ class Malware(_STIXBase):
_properties.update({ _properties.update({
'type': TypeProperty(_type), 'type': TypeProperty(_type),
'id': IDProperty(_type), 'id': IDProperty(_type),
'labels': Property(required=True), 'labels': ListProperty(StringProperty, required=True),
'name': Property(required=True), 'name': StringProperty(required=True),
'description': Property(), 'description': StringProperty(),
'kill_chain_phases': Property(), 'kill_chain_phases': Property(),
}) })

View File

@ -2,6 +2,7 @@ import datetime as dt
import pytest import pytest
import pytz import pytz
import re
import stix2 import stix2
@ -99,3 +100,10 @@ def test_parse_malware():
assert mal.modified == "2016-05-12T08:17:27Z" assert mal.modified == "2016-05-12T08:17:27Z"
assert mal.labels == ['ransomware'] assert mal.labels == ['ransomware']
assert mal.name == "Cryptolocker" assert mal.name == "Cryptolocker"
def test_parse_malware_invalid_labels():
data = re.compile('\[.+\]', re.DOTALL).sub('1', EXPECTED_MALWARE)
with pytest.raises(ValueError) as excinfo:
stix2.parse(data)
assert "Invalid value for Malware 'labels'" in str(excinfo.value)