1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
import pytest
from briefcase.exceptions import CorruptToolError
from briefcase.integrations.linuxdeploy import (
ELF_PATCH_OFFSET,
ELF_PATCH_ORIGINAL_BYTES,
ELF_PATCH_PATCHED_BYTES,
)
from tests.integrations.linuxdeploy.utils import create_mock_appimage
def test_patch_linuxdeploy_elf_header_unpatched(linuxdeploy, tmp_path):
"""If the linuxdeploy tool/plugin is not patched, patch it."""
appimage_path = tmp_path / "tools/linuxdeploy-i386.AppImage"
# Mock an unpatched linuxdeploy AppImage
pre_patch_header = create_mock_appimage(
appimage_path=appimage_path, mock_appimage_kind="original"
)
# Create a linuxdeploy wrapper, then patch the elf header
linuxdeploy.patch_elf_header()
# Ensure the patch was applied.
with open(appimage_path, "rb") as mock_appimage:
mock_appimage.seek(ELF_PATCH_OFFSET)
patched_header = mock_appimage.read(len(ELF_PATCH_PATCHED_BYTES))
assert pre_patch_header == ELF_PATCH_ORIGINAL_BYTES
assert patched_header == ELF_PATCH_PATCHED_BYTES
def test_patch_linuxdeploy_elf_header_already_patched(linuxdeploy, tmp_path):
"""If linuxdeploy is already patched, don't patch it."""
appimage_path = tmp_path / "tools/linuxdeploy-i386.AppImage"
# Mock a patched linuxdeploy AppImage
pre_patch_header = create_mock_appimage(
appimage_path=appimage_path, mock_appimage_kind="patched"
)
# Create a linuxdeploy wrapper, then patch the elf header
linuxdeploy.patch_elf_header()
# Ensure the patch was applied.
with open(appimage_path, "rb") as mock_appimage:
mock_appimage.seek(ELF_PATCH_OFFSET)
patched_header = mock_appimage.read(len(ELF_PATCH_PATCHED_BYTES))
assert pre_patch_header == ELF_PATCH_PATCHED_BYTES
assert patched_header == ELF_PATCH_PATCHED_BYTES
def test_patch_linuxdeploy_elf_header_bad_appimage(linuxdeploy, tmp_path):
"""If linuxdeploy does not have a valid header, raise an error."""
appimage_path = tmp_path / "tools/linuxdeploy-i386.AppImage"
# Mock a bad linuxdeploy AppImage
create_mock_appimage(appimage_path=appimage_path, mock_appimage_kind="corrupt")
# Create a linuxdeploy wrapper, then patch the elf header
with pytest.raises(CorruptToolError):
linuxdeploy = linuxdeploy.patch_elf_header()
def test_patch_linuxdeploy_elf_header_empty_appimage(linuxdeploy, tmp_path):
"""If file is empty, raise an error."""
appimage_path = tmp_path / "tools/linuxdeploy-i386.AppImage"
# Mock a bad linuxdeploy AppImage
create_mock_appimage(appimage_path=appimage_path, mock_appimage_kind="empty")
# Create a linuxdeploy wrapper, then patch the elf header
with pytest.raises(CorruptToolError):
linuxdeploy.patch_elf_header()
|