Skip to content

Commit d71721f

Browse files
author
Bryan Worrell
committed
Updated GenericTestMechanism to use TypedFields and updated TestMechanismFactory.
1 parent 07cc98a commit d71721f

3 files changed

Lines changed: 11 additions & 80 deletions

File tree

stix/extensions/test_mechanism/generic_test_mechanism.py

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,28 @@
55

66
import stix
77
import stix.indicator.test_mechanism
8+
from stix.common.vocabs import VocabField
89
from stix.common import EncodedCDATA, StructuredTextList, VocabString
910
from stix.indicator.test_mechanism import _BaseTestMechanism
1011
import stix.bindings.extensions.test_mechanism.generic as generic_tm_binding
1112

13+
1214
@stix.register_extension
1315
class GenericTestMechanism(_BaseTestMechanism):
1416
_namespace = "http://stix.mitre.org/extensions/TestMechanism#Generic-1"
1517
_binding = generic_tm_binding
1618
_binding_class = _binding.GenericTestMechanismType
1719
_XSI_TYPE = "genericTM:GenericTestMechanismType"
1820

19-
reference_location = fields.TypedField("Reference_Location")
21+
reference_location = fields.TypedField("reference_location")
2022
descriptions = fields.TypedField("Description", StructuredTextList)
2123
specification = fields.TypedField("Specification", EncodedCDATA)
22-
type_ = fields.TypedField("type", VocabString, key_name="type")
24+
type_ = VocabField("Type")
2325

2426
def __init__(self, id_=None, idref=None):
2527
super(GenericTestMechanism, self).__init__(id_=id_, idref=idref)
26-
self.reference_location = None
27-
self.description = None
28-
self.type_ = None
29-
self.specification = None
30-
31-
"""
32-
@property
33-
def specification(self):
34-
return self._specification
35-
36-
@specification.setter
37-
def specification(self, value):
38-
if not value:
39-
self._specification = None
40-
if isinstance(value, EncodedCDATA):
41-
self._specification = value
42-
else:
43-
self._specification = EncodedCDATA(value=value)
44-
"""
45-
28+
self.descriptions = StructuredTextList()
29+
4630
@property
4731
def description(self):
4832
"""A single description about the contents or purpose of this object.
@@ -71,56 +55,3 @@ def add_description(self, description):
7155
7256
"""
7357
self.descriptions.add(description)
74-
75-
"""
76-
@property
77-
def type_(self):
78-
return self._type
79-
80-
@type_.setter
81-
def type_(self, value):
82-
if not value:
83-
self._type = None
84-
elif isinstance(value, VocabString):
85-
self._type = value
86-
else:
87-
self._type = VocabString(value)
88-
"""
89-
90-
@classmethod
91-
def from_obj(cls, obj):
92-
if not obj:
93-
return None
94-
if not return_obj:
95-
return_obj = cls()
96-
97-
super(GenericTestMechanism, cls).from_obj(obj, return_obj)
98-
return_obj.reference_location = obj.reference_location
99-
return_obj.descriptions = StructuredTextList.from_obj(obj.Description)
100-
return_obj.type_ = VocabString.from_obj(obj.Type)
101-
return_obj.specification = EncodedCDATA.from_obj(obj.Specification)
102-
103-
return return_obj
104-
105-
def to_obj(self, ns_info=None):
106-
obj = super(GenericTestMechanism, self).to_obj(ns_info=ns_info)
107-
108-
return obj
109-
110-
@classmethod
111-
def from_dict(cls, d):
112-
if not d:
113-
return None
114-
if not return_obj:
115-
return_obj = cls()
116-
117-
super(GenericTestMechanism, cls).from_dict(d, return_obj)
118-
return_obj.reference_location = d.get('reference_location')
119-
return_obj.descriptions = StructuredTextList.from_dict(d.get('description'))
120-
return_obj.type_ = VocabString.from_dict(d.get('type'))
121-
return_obj.specification = EncodedCDATA.from_dict(d.get('specification'))
122-
123-
return return_obj
124-
125-
def to_dict(self):
126-
return super(GenericTestMechanism, self).to_dict()

stix/indicator/indicator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class Indicator(stix.BaseCoreComponent):
182182
indicator_types = fields.TypedField("Type", IndicatorType, multiple=True, key_name="indicator_types")
183183
confidence = fields.TypedField("Confidence", Confidence)
184184
indicated_ttps = fields.TypedField("Indicated_TTP", RelatedTTP, multiple=True, key_name="indicated_ttps")
185-
test_mechanisms = fields.TypedField("Test_Mechanisms", TestMechanisms, factory=TestMechanismFactory)
185+
test_mechanisms = fields.TypedField("Test_Mechanisms", TestMechanisms)
186186
alternative_id = fields.TypedField("Alternative_ID", multiple=True)
187187
suggested_coas = fields.TypedField("Suggested_COAs", SuggestedCOAs)
188188
sightings = fields.TypedField("Sightings", Sightings)

stix/indicator/test_mechanism.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def entity_class(self, key):
100100
import stix.extensions.test_mechanism.open_ioc_2010_test_mechanism # noqa
101101
import stix.extensions.test_mechanism.yara_test_mechanism # noqa
102102
import stix.extensions.test_mechanism.generic_test_mechanism # noqa
103-
stix.lookup_extension(key)
103+
return stix.lookup_extension(key)
104104

105105

106106
class TestMechanisms(stix.EntityList):
@@ -109,9 +109,9 @@ class TestMechanisms(stix.EntityList):
109109
_binding_class = _binding.TestMechanismsType
110110
_contained_type = _BaseTestMechanism
111111
_binding_var = "Test_Mechanism"
112-
_inner_name = "test_mechanisms"
113-
_dict_as_list = True
114-
_entity_factory=TestMechanismFactory
112+
_entity_factory = TestMechanismFactory
113+
114+
115115

116116
# Backwards compatibility
117117
add_extension = stix.add_extension

0 commit comments

Comments
 (0)