def test_optional_attributes(self): doc = Element("root", bar=(bool_attr, "optional")) dom = doc.parse_xml_str('<root />') self.assertEquals(hasattr(dom, "bar"), False) dom = doc.parse_xml_str('<root bar="true" />') self.assertEquals(hasattr(dom, "bar"), True) self.assertEquals(dom.bar, True)
def test_bool_attributes(self): doc = Element("root", foo=(bool_attr, "required")) dom = doc.parse_xml_str('<root foo="true" />') self.assertEquals(dom.foo, True) dom = doc.parse_xml_str('<root foo="false" />') self.assertEquals(dom.foo, False) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root foo="foo" />')
def test_invalid_child(self): doc = Element("root") self.assertRaises(EzXMLError, doc.parse_xml_str, '<root><child /></root>') doc = Element("root", Element("foobar")) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root><child /></root>')
def test_inclusion(self): """Test the processing of the include element.""" child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # Test file not found. self.assertRaises(EzXMLError, doc.parse_xml_str, '<root><include file="nofile" /></root>') # Test a simple include. dom = doc.parse_xml_str( '<root><include file="data/ezxml_include.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "included") # Test including a file with multiple elements and thatalso # includes another file. dom = doc.parse_xml_str( '<root><include file="data/ezxml_include2.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 7) self.assertEqual(child_els[0].name, "included1") self.assertEqual(child_els[6].name, "included")
def test_inclusion(self): """Test the processing of the include element.""" child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # Test file not found. self.assertRaises(EzXMLError, doc.parse_xml_str, '<root><include file="nofile" /></root>') # Test a simple include. dom = doc.parse_xml_str('<root><include file="data/ezxml_include.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "included") # Test including a file with multiple elements and thatalso # includes another file. dom = doc.parse_xml_str('<root><include file="data/ezxml_include2.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 7) self.assertEqual(child_els[0].name, "included1") self.assertEqual(child_els[6].name, "included")
def test_size_attributes(self): doc = Element("root", foo=(size_attr, "required")) dom = doc.parse_xml_str('<root foo="1234" />') self.assertEquals(dom.foo, 1234) dom = doc.parse_xml_str('<root foo="1K" />') self.assertEquals(dom.foo, 1024L) dom = doc.parse_xml_str('<root foo="1M" />') self.assertEquals(dom.foo, 1024L * 1024L) dom = doc.parse_xml_str('<root foo="1g" />') self.assertEquals(dom.foo, 1024L * 1024L * 1024L) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root foo="foo" />')
def test_bogus_attribute_option(self): Element("root", bar=(bool_attr, "required")) Element("root", bar=(bool_attr, "optional")) self.assertRaises(EzXMLError, Element, "root", bar=bool_attr) self.assertRaises(EzXMLError, Element, "root", bar=(bool_attr, "asdf")) self.assertRaises(EzXMLError, Element, "root", bar=("asdf", "required", "asdf")) self.assertRaises(EzXMLError, Element, "root", bar=("asdf", "required"))
def test_dump(self): # Test the dumped output. output = """\ <root > <child name="1" > </child> <child value="0x32" name="2" > </child> </root> """ child = Element("child", name=(str_attr, "required"), value=(long_attr, "optional")) doc = Element("root", child) dom = doc.parse_xml_str('<root><child name="1" /><child name="2" value="50"/></root>') self.assertEqual(dom.dump(), output)
def test_dump(self): # Test the dumped output. output = """\ <root > <child name="1" > </child> <child value="0x32" name="2" > </child> </root> """ child = Element("child", name=(str_attr, "required"), value=(long_attr, "optional")) doc = Element("root", child) dom = doc.parse_xml_str( '<root><child name="1" /><child name="2" value="50"/></root>') self.assertEqual(dom.dump(), output)
def test_find_children(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) dom = doc.parse_xml_str('<root><child name="1" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "1") dom = doc.parse_xml_str('<root></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 0) dom = doc.parse_xml_str('<root><child name="1" /><child name="2" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 2) self.assertEqual(child_els[0].name, "1") self.assertEqual(child_els[1].name, "2")
def test_find_child(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # 1 child dom = doc.parse_xml_str('<root><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") # 0 children dom = doc.parse_xml_str('<root></root>') child_el = dom.find_child("child") self.assertEquals(child_el, None) # Multiple chidren dom = doc.parse_xml_str('<root><child name="2" /><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") self.assertEquals(child_el.name, "2")
def test_find_all_children(self): leaf = Element("leaf", name=(str_attr, "required")) child = Element("child", leaf, name=(str_attr, "required")) doc = Element("root", child, leaf) dom = doc.parse_xml_str(""" <root> <child name="1"> <leaf name="bottom_leaf" /> </child> <leaf name="top_leaf" /> </root>""") leaf_els = dom.find_all_children("leaf") self.assertEqual(len(leaf_els), 2) self.assertEqual(leaf_els[0].name, "bottom_leaf") self.assertEqual(leaf_els[1].name, "top_leaf") dom = doc.parse_xml_str('<root></root>') leaf_els = dom.find_children("leaf") self.assertEqual(len(leaf_els), 0)
def test_unknown_node_type(self): """This test is a bit hacky, because there is no real way this error could be triggered, but we want to ensure we test that exception raise somehow, this goes to lengths to do that.""" class Empty: pass empty = Empty doc = Element("root") empty.tagName = "root" empty.attributes = {} empty2 = Empty empty2.nodeType = 37 empty2.ELEMENT_NODE = 1 empty2.TEXT_NODE = 3 empty2.COMMENT_NODE = 8 empty.childNodes = [empty2] self.assertRaises(EzXMLError, doc.walkdom, empty)
class XmlCollector(object): element = NotImplemented COLLECTORS = {} image = Element("image") def register(cls): """ Register the cell implementation class with an element (and its name). """ cls.COLLECTORS[cls.element.name] = cls cls.image.append_element(cls.element) register = classmethod(register) def collect_xml(self, elem, *args, **kwargs): """ Parse XML elements and create an intermediate representation of the system. """ raise NotImplementedError
def test_find_children(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) dom = doc.parse_xml_str('<root><child name="1" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "1") dom = doc.parse_xml_str('<root></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 0) dom = doc.parse_xml_str( '<root><child name="1" /><child name="2" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 2) self.assertEqual(child_els[0].name, "1") self.assertEqual(child_els[1].name, "2")
def test_find_child(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # 1 child dom = doc.parse_xml_str('<root><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") # 0 children dom = doc.parse_xml_str('<root></root>') child_el = dom.find_child("child") self.assertEquals(child_el, None) # Multiple chidren dom = doc.parse_xml_str( '<root><child name="2" /><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") self.assertEquals(child_el.name, "2")
def test_repr(self): doc = Element("root", foo=(bool_attr, "required"), bar=(long_attr, "required")) parse = doc.parse_xml_str("<root foo='true' bar='16' />") self.assertEquals(repr(parse), '<root foo="True" bar="0x10" >')
def test_basic_usage(self): doc = Element("root") parsed = doc.parse_xml_str("<root />") self.assertEquals(parsed.tag, "root") parsed = doc.parse_xml_file("data/ezxml.1") self.assertEquals(parsed.tag, "root")
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ Processing of the memstats XML element. """ from weaver.ezxml import Element, str_attr, size_attr ############################################################################# # # Declare the XML elements # ############################################################################# RevisionStats_el = Element("revision", repository=(str_attr, "optional"), changeset=(str_attr, "optional")) # # Construction of elements leading to environment # ID_Range_el = Element("id_range", name=(str_attr, "required"), node=(str_attr, "required")) Pool_el = Element("pool", id=(str_attr, "required"), vmid=(str_attr, "required")) Thread_el = Element("thread", pool_id=(str_attr, "required"),
def test_ignoring_comments(self): doc = Element("root") parsed = doc.parse_xml_str("<root><!-- This is a comment --></root>") self.assertEquals(parsed.tag, "root") self.assertEquals(len(parsed.children), 0)
def test_str_attributes(self): doc = Element("root", foo=(str_attr, "required")) dom = doc.parse_xml_str('<root foo="1234" />') self.assertEquals(dom.foo, "1234")
def test_invalidroot(self): doc = Element("root") self.assertRaises(EzXMLError, doc.parse_xml_str, "<bar />")
def test_long_attributes(self): doc = Element("root", foo=(long_attr, "required")) dom = doc.parse_xml_str('<root foo="1234" />') self.assertEquals(dom.foo, 1234) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root foo="foo" />')
""" from elf.core import UnpreparedElfFile from elf.constants import ET_EXEC from weaver import MergeError from weaver.ezxml import ParsedElement, Element, long_attr, bool_attr, str_attr from weaver.segments_xml import collect_patches, collect_elf_segments, \ attach_to_elf_flags, Segment_el, Heap_el, Patch_el, \ start_to_value, make_pager_attr from weaver.memobjs_xml import Stack_el from weaver.cells.iguana.memobjs_xml import Memsection_el, \ collect_memsection_element, create_standard_caps, create_alias_cap from weaver.cells.iguana.bootinfo import Thread, Zone, PD, Environment, Cap import os Argv_el = Element("arg", value = (str_attr, "required")) CommandLine_el = Element("commandline", Argv_el) Thread_el = Element("thread", Stack_el, CommandLine_el, name = (str_attr, "required"), start = (str_attr, "required"), priority = (long_attr, "optional"), physpool = (str_attr, "optional"), virtpool = (str_attr, "optional")) Entry_el = Element("entry", key = (str_attr, "required"), value = (long_attr, "optional"), cap = (str_attr, "optional"), attach = (str_attr, "optional"))
def test_empty_tag(self): doc = Element("root") doc.parse_xml_str('<root> </root>')
def test_call(self): doc = Element("foo") dom = doc(ParsedElement("child"), bar=True) self.assertEquals(len(dom.children), 1) self.assertEquals(dom.bar, True)
def test_repr(self): doc = Element("root", foo=(bool_attr, "required"), bar=(long_attr, "required")) parse = doc.parse_xml_str("<root foo='true' bar='16' />") self.assertEquals(repr(parse), '<root bar="0x10" foo="True" >')
def test_simple_child(self): doc = Element("root", Element("child")) dom = doc.parse_xml_str('<root><child /></root>') self.assertEquals(len(dom.children), 1)
def test_malformed_xml(self): doc = Element("root") self.assertRaises(EzXMLError, doc.parse_xml_str, "asdf") self.assertRaises(EzXMLError, doc.parse_xml_file, "data/ezxml.2")
collect_thread, Environment_el, collect_program_pd_elements, \ Program_el, PD_el, Environment_el import weaver.cells.iguana.bootinfo from weaver.memobjs_xml import Stack_el from weaver.cells.iguana.memobjs_xml import collect_memsection_element, \ create_standard_caps from weaver.ezxml import Element, bool_attr, str_attr, long_attr, \ size_attr, ParsedElement from weaver.segments_xml import Segment_el, Patch_el, Heap_el, \ collect_elf_segments, collect_patches from weaver.cell_environment import CellEnvironment Extension_el = Element("extension", Segment_el, Patch_el, name = (str_attr, "required"), file = (str_attr, "optional"), start = (str_attr, "optional"), direct = (bool_attr, "optional"), pager = (str_attr, "optional"), physpool = (str_attr, "optional")) IRQ_el = Element("irq", value = (long_attr, "required")) Iguana_el = Element("iguana", Segment_el, Patch_el, Extension_el, Environment_el, Stack_el, Heap_el, Program_el, PD_el, IRQ_el, name = (str_attr, "required"), file = (str_attr, "required"), physpool = (str_attr, "optional"), virtpool = (str_attr, "optional"), pager = (str_attr, "optional"),
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ Processing of the memory object XML tags. """ from weaver.ezxml import Element, long_attr, bool_attr, str_attr, size_attr from weaver.memobjs_xml import collect_memobj_attrs import weaver.cells.iguana.bootinfo Right_el = Element("right", value=(str_attr, "required")) Cap_el = Element("cap", Right_el, name=(str_attr, "required")) Memsection_el = Element("memsection", Cap_el, name=(str_attr, "required"), file=(str_attr, "optional"), size=(size_attr, "optional"), virt_addr=(long_attr, "optional"), phys_addr=(long_attr, "optional"), physpool=(str_attr, "optional"), virtpool=(str_attr, "optional"), align=(size_attr, "optional"), attach=(str_attr, "optional"), direct=(bool_attr, "optional"),
def test_invalid_attributes(self): doc = Element("root") self.assertRaises(EzXMLError, doc.parse_xml_str, '<root bar="1234" />') doc = Element("root", foo=(long_attr, "required")) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root bar="1234" />')
"""Collect data from the kernel element.""" from weaver import MergeError from weaver import util from weaver.ezxml import Element, long_attr, str_attr from weaver.segments_xml import Segment_el, Patch_el, Heap_el DEFAULT_KERNEL_HEAP_SIZE = 4 * 1024 * 1024 DEFAULT_KERNEL_MAX_THREADS = 1024 UseDevice_el = Element("use_device", name=(str_attr, "required")) Option_el = Element("option", key=(str_attr, "required"), value=(long_attr, "required")) Config_el = Element("config", Option_el) # Compound Elements Kernel_el = Element("kernel", Segment_el, Patch_el, Heap_el, Config_el, UseDevice_el, file=(str_attr, "optional"), sdk=(str_attr, "optional"), configuration=(str_attr, "optional"), platform=(str_attr, "optional"), linker=(str_attr, "optional"), kernel=(str_attr, "optional"),
def test_required_attributes(self): doc = Element("root", bar=(bool_attr, "required")) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root />')
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """Process segment XML elements.""" import re from elf.constants import PF_X, PF_W, PF_R, EM_386 from weaver import MergeError from weaver.ezxml import Element, long_attr, bool_attr, str_attr, size_attr # General element Segment_el = Element("segment", name=(str_attr, "required"), phys_addr=(long_attr, "optional"), physpool=(str_attr, "optional"), align=(size_attr, "optional"), attach=(str_attr, "optional"), direct=(bool_attr, "optional"), pager=(str_attr, "optional"), protected=(bool_attr, "optional"), cache_policy=(str_attr, "optional")) Heap_el = Element("heap", size=(size_attr, "optional"), virt_addr=(long_attr, "optional"), phys_addr=(long_attr, "optional"), physpool=(str_attr, "optional"), virtpool=(str_attr, "optional"), align=(size_attr, "optional"), attach=(str_attr, "optional"), direct=(bool_attr, "optional"), zero=(bool_attr, "optional"),