util: add module to parse PE32+ files

Add an new module with utility functions to inspect PE32+ files,
mainly listing the sections and their addresses and sizes.
Include a simple test to check that we can successfully parse the
EFI stub contained in systemd (systemd-udev package).
This commit is contained in:
Christian Kellner 2022-10-12 18:55:26 +02:00
parent f34bee944b
commit ecb24a8eb7
2 changed files with 261 additions and 0 deletions

View file

@ -0,0 +1,55 @@
#
# Test for the util.lvm2 module
#
import io
import os
import pytest
from osbuild.util import pe32p
EFI_STUB = "/usr/lib/systemd/boot/efi/linuxx64.efi.stub"
def have_efi_stub() -> bool:
return os.path.exists(EFI_STUB)
@pytest.mark.skipif(not have_efi_stub(), reason="require systemd efi stub")
def test_basic():
with open(EFI_STUB, "rb") as f:
coff = pe32p.read_coff_header(f)
assert coff
opt = pe32p.read_optional_header(f, coff)
assert opt
sections = pe32p.read_sections(f, coff)
assert sections, "No sections found in stub"
@pytest.mark.skipif(not have_efi_stub(), reason="require systemd efi stub")
def test_basic_no_coff():
# check the API versions that re-reads the CoffHeader
with open(EFI_STUB, "rb") as f:
coff = pe32p.read_coff_header(f)
assert coff
f.seek(0, io.SEEK_SET)
opt = pe32p.read_optional_header(f)
f.seek(0, io.SEEK_SET)
assert opt
f.seek(0, io.SEEK_SET)
sections = pe32p.read_sections(f)
assert sections, "No sections found in stub"
with open(EFI_STUB, "rb") as f:
coff_check = pe32p.read_coff_header(f)
assert coff_check
assert coff == coff_check
opt_check = pe32p.read_optional_header(f, coff)
assert opt_check
assert opt == opt_check
sections_check = pe32p.read_sections(f, coff)
assert sections_check
assert sections == sections_check