def test_binding_at_template_assignment():
    template0 = XMLTemplateParser("""
        <template name="template0" size="4">
        </template>
        """).parse()
    template1 = XMLTemplateParser("""
        <template name="template0">
            <layout name="layout0">
                <area name="area0">
                    <field name="field1_size" size="4"></field>
                    <field name="field1" size="{field1_size, byteorder=little}"></field>
                    <field name="field2" size="{field1_size, byteorder=big}"></field>
                </area>
            </layout>
        </template>""").parse()
    template_provider0 = TemplateProvider(template0)
    template_provider1 = TemplateProvider(template1)
    data_provider = DataProvider(io.BytesIO(bytes(4 * [0x0])))
    binalyzer = Binalyzer(template_provider0, data_provider)
    binalyzer.template = template_provider1.template
    binalyzer.data = io.BytesIO(bytes([0x04, 0x00, 0x00, 0x00]))
    field1_size = find_by_attr(template1, "field1_size")
    field1 = find_by_attr(template1, "field1")
    field2 = find_by_attr(template1, "field2")
    assert field1_size.size == 4
    assert field1_size.value == bytes([0x04, 0x00, 0x00, 0x00])
    assert field1.size == 0x4
    assert field2.size == 0x4000000
Ejemplo n.º 2
0
def test_binalyzer_set_data():
    data_mock1 = object()
    data_mock2 = object()
    data_provider = DataProvider(data_mock1)
    binalyzer = Binalyzer()
    binalyzer.data_provider = data_provider
    binalyzer.data = data_mock2
    assert binalyzer.data == data_mock2
    assert binalyzer.data != data_mock1
def test_sizing_stretch_root_template():
    template = XMLTemplateParser("""
        <template name="root" sizing="stretch">
        </template>
    """).parse()
    binalyzer = Binalyzer(template, bytes([0] * 512))
    binalyzer.data = io.BytesIO(bytes([0] * 512))

    assert binalyzer.template.root.size == 512
def test_stretch_with_auto_sizing_parent():
    template = XMLTemplateParser("""
        <template sizing="auto">
            <header size="4">
            </header>
            <payload name="payload" sizing="stretch">
            </payload>
        </template>
    """).parse()
    binalyzer = Binalyzer(template)
    binalyzer.data = io.BytesIO(bytes([0x01] * 8))
    binalyzer.template.payload.value = bytes([0x02] *
                                             binalyzer.template.payload.size)
    print(binalyzer.template.size)
    print(binalyzer.template.value)
def test_binding_at_stream_assignment():
    template = XMLTemplateParser("""
        <template name="template0">
            <layout name="layout0">
                <area name="area0">
                    <field name="field1_size" size="4"></field>
                    <field name="field1" size="{field1_size, byteorder=little}"></field>
                    <field name="field2" size="{field1_size, byteorder=big}"></field>
                </area>
            </layout>
        </template>""").parse()
    binalyzer = Binalyzer(template, io.BytesIO(bytes(4 * [0x0])))
    binalyzer.data = io.BytesIO(bytes([0x04, 0x00, 0x00, 0x00]))
    field1_size = find_by_attr(template, "field1_size")
    field1 = find_by_attr(template, "field1")
    field2 = find_by_attr(template, "field2")
    assert field1_size.size == 4
    assert field1_size.value == bytes([0x04, 0x00, 0x00, 0x00])
    assert field1.size == 0x4
    assert field2.size == 0x4000000