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
예제 #2
0
def test_has_extension():
    binalyzer = Binalyzer()
    mock = MockExtension(binalyzer)
    assert binalyzer.has_extension("mock")
    assert len(binalyzer.extensions), 1
    assert isinstance(binalyzer.extensions["mock"], MockExtension)
    assert id(mock) == id(binalyzer.extension("mock"))
예제 #3
0
def test_add_extension_at_object_creation():
    binalyzer = Binalyzer()
    mock = MockExtension(binalyzer)
    assert binalyzer.has_extension("mock")
    assert len(binalyzer.extensions), 1
    assert isinstance(binalyzer.extensions["mock"], MockExtension)
    assert id(mock) == id(binalyzer.extension("mock"))
    assert id(mock) == id(binalyzer.mock)
예제 #4
0
def test_binalyzer_set_template():
    template_mock1 = Template(name="a")
    template_mock2 = Template(name="b")
    template_provider = TemplateProvider(template_mock1)
    binalyzer = Binalyzer()
    binalyzer.template_provider = template_provider
    binalyzer.template = template_mock2
    assert binalyzer.template.name == template_mock2.name
    assert binalyzer.template.name != template_mock1.name
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
예제 #6
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
예제 #7
0
def test_count_array_access():
    a = Template(name="a")
    b = Template(name="b", parent=a)
    b.count = 3

    binalyzer = Binalyzer()
    binalyzer.template = a

    assert len(binalyzer.template.b) == 3
    assert binalyzer.template.b[0].name == "b-0"
    assert binalyzer.template.b[1].name == "b-1"
    assert binalyzer.template.b[2].name == "b-2"
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_sizing_fix_override_with_sizing_set_to_stretch():
    template = XMLTemplateParser("""
        <template name="root" sizing="stretch" size="256">
        </template>
    """).parse()
    binalyzer = Binalyzer(template)
    assert binalyzer.template.root.size == 256
예제 #10
0
def test_binalyzer_value_read_resets_data_stream_position():
    binalyzer = Binalyzer()
    binalyzer.template.size = 64
    binalyzer.template.value = bytes([0x01] * 64)
    value = binalyzer.template.value
    assert binalyzer.data.tell() == 0
    assert value == bytes([0x01] * 64)
def test_sizing_implicit_auto_root_no_children():
    template = XMLTemplateParser("""
        <template name="root">
        </template>
    """).parse()
    binalyzer = Binalyzer(template)

    assert binalyzer.template.root.size == 0
def test_sizing_explicit_fix():
    template = XMLTemplateParser("""
        <template name="root" sizing="fix" size="256">
        </template>
    """).parse()
    binalyzer = Binalyzer(template)

    assert binalyzer.template.root.size == 256
예제 #13
0
def test_binalyzer_get_data_provider():
    template = PlainTemplateProvider().template
    data_provider = ZeroedDataProvider()
    data = data_provider.data
    binalyzer = Binalyzer(template, data)
    assert binalyzer.data_provider != data_provider
    assert binalyzer.data_provider.data == data
    assert binalyzer.data == data
def test_sizing_explicit_auto_root_children():
    template = XMLTemplateParser("""
        <template name="root" sizing="auto">
            <area name="child" size="128"></area>
        </template>
    """).parse()
    binalyzer = Binalyzer(template)

    assert binalyzer.template.root.size == 128
예제 #15
0
def test_binalyzer_get_template_provider():
    template_provider = PlainTemplateProvider()
    template = template_provider.template
    data_provider = ZeroedDataProvider()
    data = data_provider.data
    binalyzer = Binalyzer(template, data)
    assert binalyzer.template_provider != template_provider
    assert binalyzer.template_provider.template.name == template.name
    assert binalyzer.template.name == template.name
예제 #16
0
def test_dispose_extension():
    binalyzer = Binalyzer()
    binalyzer.extensions = {}
    mock = MockExtension(binalyzer)
    assert binalyzer.has_extension("mock")
    assert len(binalyzer.extensions) == 1
    assert isinstance(binalyzer.extensions["mock"], MockExtension)
    assert id(mock) == id(binalyzer.extension("mock"))
    binalyzer.del_extension("mock")
    assert not binalyzer.has_extension("mock")
    assert len(binalyzer.extensions) == 0
    assert mock.disposed
def test_sizing_stretch_to_parent():
    template = XMLTemplateParser("""
        <template name="root" size="96">
            <area size="32"></area>
            <area name="area1" sizing="stretch"></area>
        </template>
    """).parse()
    binalyzer = Binalyzer(template)

    assert binalyzer.template.root.area1.size == 64
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
def test_sizing_stretch_to_sibling():
    template = XMLTemplateParser("""
        <template name="root" size="96">
            <area name="area0" size="32"></area>
            <area name="area1" sizing="stretch"></area>
            <area name="area2" origin="end" size="4"></area>
        </template>
    """).parse()
    binalyzer = Binalyzer(template)

    assert binalyzer.template.root.area1.size == 60
예제 #20
0
def test_add_extension():
    mock = MockExtension(None)
    binalyzer = Binalyzer()
    binalyzer.extensions = {}
    binalyzer.add_extension("mock", mock)
    assert binalyzer.has_extension("mock")
    assert len(binalyzer.extensions) == 1
    assert isinstance(binalyzer.extensions["mock"], MockExtension)
    assert id(mock) == id(binalyzer.extension("mock"))
    assert id(mock) == id(binalyzer.mock)
def test_from_url():
    url = "https://raw.githubusercontent.com/denisvasilik/binalyzer-template-provider/"
    binalyzer = Binalyzer()
    XMLTemplateProviderExtension(binalyzer)
    WebAssemblyExtension(binalyzer)
    binalyzer.xml.from_url(
        url + "master/tests/resources/wasm_module_format.xml",
        url + "master/tests/resources/wasm_module.wasm",
    )

    instructions = binalyzer.template.code_section.code.function.func_body.instructions
    instructions.value = bytes([0x0B, 0x00])

    assert binalyzer.template.magic.value == bytes([0x00, 0x61, 0x73, 0x6D])
    assert binalyzer.template.version.value == bytes([0x01, 0x00, 0x00, 0x00])
    assert instructions.value == bytes([0x0B, 0x00])
예제 #22
0
def test_field_relative_offset():
    expected_data_stream = bytes([0] * 32 + [1] * 32 + [2] * 32 + [3] * 32)
    test_data_stream = io.BytesIO(bytes([0] * 128))
    template = XMLTemplateParser("""<template name="template0">
            <field name="field0" size="32"></field>
            <field name="field1" size="32"></field>
            <field name="field2" size="32"></field>
            <field name="field3" size="32"></field>
        </template>""").parse()
    binalyzer = Binalyzer(template, test_data_stream)

    binalyzer.template.field0.value = bytes([0] * 32)
    binalyzer.template.field1.value = bytes([1] * 32)
    binalyzer.template.field2.value = bytes([2] * 32)
    binalyzer.template.field3.value = bytes([3] * 32)
    assert binalyzer.template.value == expected_data_stream
def test_field_cross_reference():
    data = io.BytesIO(bytes([0x04, 0x00, 0x00, 0x00]))
    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, data)
    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
def test_template_reference_with_provider(binalyzer):
    data = io.BytesIO(bytes([0xE5, 0x8E, 0x26]))
    template = XMLTemplateParser(
        """
        <template name="template0">
            <layout name="layout0">
                <area name="area0">
                    <field name="field1_size" size="3"></field>
                    <field name="field1" size="{field1_size, provider=mock.ref_custom}"></field>
                </area>
            </layout>
        </template>""",
        binalyzer=binalyzer,
    ).parse()
    binalyzer = Binalyzer(template, data)
    field1_size = find_by_attr(template, "field1_size")
    field1 = find_by_attr(template, "field1")
    assert field1_size.size == 3
    assert field1_size.value == bytes([0xE5, 0x8E, 0x26])
    assert field1.size == 2526949
예제 #25
0
def test_add_extension_twice():
    binalyzer = Binalyzer()
    mock = MockExtension(None)
    binalyzer.add_extension("mock", mock)
    with pytest.raises(RuntimeError):
        binalyzer.add_extension("mock", mock)
예제 #26
0
def test_binalyzer_set_template_provider():
    template_provider = PlainTemplateProvider()
    binalyzer = Binalyzer()
    binalyzer.template_provider = template_provider
    assert binalyzer.template_provider == template_provider
예제 #27
0
def test_binalyzer_get_template():
    binalyzer = Binalyzer()
    binalyzer.template_provider = PlainTemplateProvider()
    binalyzer.data_provider = ZeroedDataProvider()
    assert binalyzer.template
예제 #28
0
def test_del_non_existent_extension():
    binalyzer = Binalyzer()
    with pytest.raises(RuntimeError):
        binalyzer.del_extension("mock")
예제 #29
0
def test_binalyzer_instantiation_with_default_parameters():
    binalyzer = Binalyzer()
    assert isinstance(binalyzer.template_provider, TemplateProvider)
    assert isinstance(binalyzer.data_provider, DataProvider)
예제 #30
0
def test_binalyzer_instantiation():
    template_provider = PlainTemplateProvider()
    data_provider = ZeroedDataProvider()
    binalyzer = Binalyzer(template_provider.template, data_provider.data)
    assert isinstance(binalyzer, Binalyzer)