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

View File

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

View File

@ -2,6 +2,7 @@ import datetime as dt
import pytest
import pytz
import re
import stix2
@ -99,3 +100,10 @@ def test_parse_malware():
assert mal.modified == "2016-05-12T08:17:27Z"
assert mal.labels == ['ransomware']
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)