本文整理汇总了Python中scap.Model.Model类的典型用法代码示例。如果您正苦于以下问题:Python Model类的具体用法?Python Model怎么用?Python Model使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Model类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_load_attribute_required
def test_load_attribute_required():
attr = Model.load(None, ET.fromstring('<test:RequiredAttributeFixture xmlns:test="http://jaymes.biz/test" required_attribute="test" />'))
assert isinstance(attr, RequiredAttributeFixture)
assert hasattr(attr, 'required_attribute')
assert attr.required_attribute == 'test'
with pytest.raises(RequiredAttributeException):
attr = Model.load(None, ET.fromstring('<test:RequiredAttributeFixture xmlns:test="http://jaymes.biz/test" />'))
示例2: test_load_root_model
def test_load_root_model():
root = Model.load(None, ET.fromstring('<test:RootFixture xmlns:test="http://jaymes.biz/test" />'))
assert isinstance(root, RootFixture)
with pytest.raises(UnregisteredNamespaceException):
Model.load(None, ET.fromstring('<test:RootFixture xmlns:test="http://jaymes.biz/derp" />'))
with pytest.raises(TagMappingException):
Model.load(None, ET.fromstring('<test:Derp xmlns:test="http://jaymes.biz/test" />'))
示例3: test_namespace_registration
def test_namespace_registration():
Model.register_namespace('scap.model.derp', 'http://jaymes.biz/derp')
Model.xmlns_to_package('http://jaymes.biz/derp') == 'scap.model.derp'
Model.unregister_namespace('scap.model.derp')
with pytest.raises(UnregisteredNamespaceException):
Model.xmlns_to_package('http://jaymes.biz/derp')
示例4: test_load_element_min
def test_load_element_min():
el = Model.load(None, ET.fromstring('''
<test:MinMaxElementFixture xmlns:test="http://jaymes.biz/test">
<test:min>test1</test:min>
<test:min>test2</test:min>
<test:min>test3</test:min>
<test:max>test4</test:max>
<test:max>test5</test:max>
</test:MinMaxElementFixture>
'''))
assert isinstance(el, MinMaxElementFixture)
assert hasattr(el, 'min')
assert isinstance(el.min, ModelList)
assert len(el.min) == 3
assert el.min[0].text == 'test1'
assert el.min[1].text == 'test2'
assert el.min[2].text == 'test3'
assert hasattr(el, 'max')
assert isinstance(el.max, ModelList)
assert len(el.max) == 2
assert el.max[0].text == 'test4'
assert el.max[1].text == 'test5'
with pytest.raises(MinimumElementException):
el = Model.load(None, ET.fromstring('''
<test:MinMaxElementFixture xmlns:test="http://jaymes.biz/test">
<test:min>test1</test:min>
<test:max>test4</test:max>
<test:max>test5</test:max>
</test:MinMaxElementFixture>
'''))
with pytest.raises(MaximumElementException):
el = Model.load(None, ET.fromstring('''
<test:MinMaxElementFixture xmlns:test="http://jaymes.biz/test">
<test:min>test1</test:min>
<test:min>test2</test:min>
<test:min>test3</test:min>
<test:max>test4</test:max>
<test:max>test5</test:max>
<test:max>test6</test:max>
</test:MinMaxElementFixture>
'''))
示例5: test_simple_remote
def test_simple_remote():
model = Model.load(None, ET.fromstring('<test:XLinkFixture ' +
'xmlns:test="http://jaymes.biz/test" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" ' +
'xlink:href="http://jaymes.biz/test.xml" />'))
assert isinstance(model._elements, ModelList)
assert len(model._elements) > 0
assert isinstance(model._elements[0], RootFixture)
assert isinstance(model._elements[0].EnclosedFixture, EnclosedFixture)
示例6: resolve
def resolve(self, benchmark):
### Loading.Resolve.Items
# For each Item in the Benchmark that has an extends property, resolve
# it by using the following steps:
# (1) if the Item is Group, resolve all the enclosed Items,
for item_id in self.items:
logger.debug('Resolving item: ' + item_id)
self.items[item_id].resolve(benchmark)
# (2) resolve the extended Item,
if self.extends is None:
return
extended = self.get_extended(benchmark)
extended.resolve(benchmark)
# (3) prepend the property sequence from the extended Item to the
# extending Item,
# (5) remove duplicate properties and apply property overrides, and
for name in self._model_map['attributes']:
attr_map = self._model_map['attributes'][name]
if 'in' in attr_map:
attr_name = attr_map['in']
else:
xmlns, attr_name = Model.parse_tag(name)
attr_name = attr_name.replace('-', '_')
self.resolve_property(extended, attr_name)
for element_def in self._model_map['elements']:
if element_def['tag_name'].endswith('*'):
continue
if 'list' in element_def:
self.resolve_property(extended, element_def['list'])
elif 'dict' in element_def:
self.resolve_property(extended, element_def['dict'])
else:
if 'in' in element_def:
name = element_def['in']
else:
name = element_def['tag_name'].replace('-', '_')
self.resolve_property(extended, name)
# (4) if the Item is a Group, assign values for the id properties of
# Items copied from the extended Group,
if hasattr(extended, 'items') and len(extended.items) > 0:
for ext_item in extended.items:
# make a copy of the item and append to our items
self.items.append(ext_item.copy())
# (6) remove the extends property.
self.extends = None
示例7: check
def check(self, benchmark, host, exports, import_names):
content = Model.find_content(self.href)
if content is None:
raise ReferenceException(self.href + ' was not loaded')
# find the named content
if self.name is not None:
content = content.find_reference(self.name)
# apply content
return content.check(host, exports, import_names)
示例8: test_load_enclosed_model
def test_load_enclosed_model():
root = RootFixture()
el = Model.load(root, ET.fromstring('<test:EnclosedFixture xmlns:test="http://jaymes.biz/test" />'))
assert isinstance(el, EnclosedFixture)
el = Model.load(root, ET.fromstring('<EnclosedFixture />'))
assert isinstance(el, EnclosedFixture)
with pytest.raises(UnregisteredNamespaceException):
Model.load(root, ET.fromstring('<test:EnclosedFixture xmlns:test="http://jaymes.biz/derp" />'))
with pytest.raises(UnregisteredNamespaceException):
Model.load(None, ET.fromstring('<EnclosedFixture />'))
with pytest.raises(TagMappingException):
Model.load(root, ET.fromstring('<Derp />'))
示例9: test_oval_5_3_detected
def test_oval_5_3_detected():
test_xml = '<oval_definitions ' + \
'xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" ' + \
'xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" ' + \
'xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5">' + \
'<generator>' + \
'<oval:product_name>The OVAL Repository</oval:product_name>' + \
'<oval:schema_version>5.3</oval:schema_version>' + \
'<oval:timestamp>2008-04-10T09:00:10.653-04:00</oval:timestamp>' + \
'</generator>' + \
'</oval_definitions>'
model = Model.load(None, ET.fromstring(test_xml))
from scap.model.oval_5.defs.OvalDefinitionsElement import OvalDefinitionsElement
assert isinstance(model, OvalDefinitionsElement)
示例10: test_simple_local
def test_simple_local():
path = (
pathlib.Path(str(pytest.config.rootdir)) / 'tests' / 'model' / 'test_xlink.xml'
).as_posix()
if not path.startswith('/'):
path = '/' + path
model = Model.load(None, ET.fromstring('<test:XLinkFixture ' +
'xmlns:test="http://jaymes.biz/test" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" ' +
'xlink:href="file://' + path + '" />'))
assert isinstance(model._elements, ModelList)
assert len(model._elements) > 0
assert isinstance(model._elements[0], RootFixture)
assert isinstance(model._elements[0].EnclosedFixture, EnclosedFixture)
示例11: test_load_element_wildcard_not_in
def test_load_element_wildcard_not_in():
el = Model.load(None, ET.fromstring('''
<test:WildcardElementNotInFixture xmlns:test2="http://jaymes.biz/test2" xmlns:test="http://jaymes.biz/test">
<test:wildcard_element>test1</test:wildcard_element>
<test2:wildcard_element>test2</test2:wildcard_element>
</test:WildcardElementNotInFixture>
'''))
assert isinstance(el, WildcardElementNotInFixture)
assert hasattr(el, '_elements')
assert isinstance(el._elements, ModelList)
assert len(el._elements) == 2
assert isinstance(el._elements[0], EnclosedFixture)
assert isinstance(el._elements[1], EnclosedFixture2)
assert el._elements[0].text == 'test1'
assert el._elements[1].text == 'test2'
示例12: test_load_element_map_value_type
def test_load_element_map_value_type():
el = Model.load(None, ET.fromstring('''
<test:MapElementFixture xmlns:test="http://jaymes.biz/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<test:map_value_type id="test1">test1</test:map_value_type>
<test:map_value_type id="test2">test2</test:map_value_type>
</test:MapElementFixture>
'''))
assert isinstance(el, MapElementFixture)
assert hasattr(el, 'map_value_type')
assert len(el.map_value_type) == 2
assert 'test1' in el.map_value_type
assert el.map_value_type['test1'] == 'test1'
assert 'test2' in el.map_value_type
assert el.map_value_type['test2'] == 'test2'
示例13: test_load_element_append_nil
def test_load_element_append_nil():
el = Model.load(None, ET.fromstring('''
<test:AppendElementFixture xmlns:test="http://jaymes.biz/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<test:append_nil xsi:nil="true" />
<test:append_nil>test2</test:append_nil>
</test:AppendElementFixture>
'''))
assert isinstance(el, AppendElementFixture)
assert hasattr(el, 'append_nil')
assert isinstance(el.append_nil, ModelList)
assert len(el.append_nil) == 2
assert el.append_nil[0] is None
assert isinstance(el.append_nil[1], EnclosedFixture)
assert el.append_nil[1].text == 'test2'
示例14: resolve
def resolve(self, benchmark):
### Loading.Resolve.Items
# For each Item in the Benchmark that has an extends property, resolve
# it by using the following steps:
if self.extends is None:
logger.debug('Extendable not extending: ' + self.id)
return
# (2) resolve the extended Item,
extended = self.get_extended(benchmark)
logger.debug('Found extended Extendable: ' + extended.id)
extended.resolve(benchmark)
# (3) prepend the property sequence from the extended Item to the
# extending Item,
# (5) remove duplicate properties and apply property overrides, and
for name in self._model_map['attributes']:
attr_map = self._model_map['attributes'][name]
if 'in' in attr_map:
attr_name = attr_map['in']
else:
xmlns, attr_name = Model.parse_tag(name)
attr_name = attr_name.replace('-', '_')
self.resolve_property(extended, attr_name)
for element_def in self._model_map['elements']:
if element_def['tag_name'].endswith('*'):
continue
if 'list' in element_def:
self.resolve_property(extended, element_def['list'])
elif 'dict' in tag_map:
self.resolve_property(extended, element_def['dict'])
else:
if 'in' in element_def:
name = element_def['in']
else:
name = element_def['tag_name'].replace('-', '_')
self.resolve_property(extended, name)
# (6) remove the extends property.
self.extends = None
示例15: test_def
def test_def():
test_xml = '''<?xml version="1.0" encoding="UTF-8"?>
<oval_definitions
xmlns:unix-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix"
xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent"
xmlns:lin-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#linux"
xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5"
xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<generator>
<oval:product_name>Text Editors</oval:product_name>
<oval:schema_version>5.8</oval:schema_version>
<oval:timestamp>2013-12-17T12:00:00-04:00</oval:timestamp>
</generator>
<definitions>
<definition class="compliance" id="oval:gov.nist.usgcb.rhel:def:20000" version="2">
<metadata>
<title>Ensure that /tmp has its own partition or logical volume</title>
<description>The /tmp directory is a world-writable directory used for temporary file storage. Verify that it has its own partition or logical volume.</description>
</metadata>
<criteria>
<!-- <criterion test_ref="oval:gov.nist.usgcb.rhel:tst:20000" comment="Check in /etc/fstab for a /tmp mount point"/> -->
<criterion test_ref="oval:gov.nist.usgcb.rhel:tst:2000000" comment="Check if /tmp partition exists"/>
</criteria>
</definition>
</definitions>
<tests>
<lin-def:partition_test id="oval:gov.nist.usgcb.rhel:tst:2000000" comment="Check if /tmp partition exists" check_existence="at_least_one_exists" check="at least one" version="1">
<lin-def:object object_ref="oval:gov.nist.usgcb.rhel:obj:144120"/>
</lin-def:partition_test>
</tests>
<objects>
<lin-def:partition_object version="1" id="oval:gov.nist.usgcb.rhel:obj:144120">
<lin-def:mount_point>/tmp</lin-def:mount_point>
</lin-def:partition_object>
</objects>
<states>
</states>
<variables>
</variables>
</oval_definitions>'''
model = Model.load(None, ET.fromstring(test_xml))
from scap.model.oval_5.defs.OvalDefinitionsElement import OvalDefinitionsElement
assert isinstance(model, OvalDefinitionsElement)