Skip to content

Commit

Permalink
Add test for ukify-UKIs
Browse files Browse the repository at this point in the history
  • Loading branch information
SoapGentoo committed Dec 4, 2023
1 parent 71aa42c commit 27bbddf
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def write_compress(path: Path,

def write_efistub(path: Path,
version_line: bytes,
uname: bool = False,
) -> None:
"""Write an EFIstub kernel image at `path`, with `version_line`"""

Expand All @@ -68,23 +69,35 @@ def write_efistub(path: Path,
f.write(b"PE\0\0\0\0\4\0" + 12 * b"\0" + b"\x08\0\0\0")
# opt header (padding)
f.write(8 * b"\0")
# 4 sections
# 5 sections
if uname:
switch_section = b".uname\0\0" + bytes([len(version_line)])
val = b"\x68\1\0\0"
else:
switch_section = b".cmdline"
val = b"\0\0\0\0"

sections = {
b".code": b"\0\0\0\0",
b".data": b"\0\0\0\0",
b".linux": b"\x80\1\0\0",
switch_section: val,
b".linux": b"\xA8\1\0\0",
b".initrd": b"\0\0\0\0",
}
for name, offset in sections.items():
f.write(name + (20 - len(name)) * b"\0" + offset + 16 * b"\0")
# padding
f.write(64 * b"\0")
# .uname/padding
if uname:
f.write(version_line + (64 - len(version_line)) * b'\0')
else:
f.write(64 * b'\0')
# bzImage
f.write(0x202 * b'\0')
f.write(b'HdrS')
f.write(8 * b'\0')
f.write(b'\x10\x00')
f.write(version_line)
if not uname:
f.write(version_line)


class KernelImageTests(unittest.TestCase):
Expand Down Expand Up @@ -122,6 +135,20 @@ def test_read_internal_version_efistub(self) -> None:
KernelImage(path).read_internal_version(),
"1.2.3")

def test_read_internal_version_efistub_uname(self) -> None:
path = Path(self.td.name) / "vmlinuz"
write_efistub(path, b"1.2.3 built on test", True)
self.assertEqual(
KernelImage(path).read_internal_version(),
"1.2.3")

def test_read_internal_version_efistub_uname_nowhitespace(self) -> None:
path = Path(self.td.name) / "vmlinuz"
write_efistub(path, b"1.2.3", True)
self.assertEqual(
KernelImage(path).read_internal_version(),
"1.2.3")

def test_very_short(self) -> None:
path = Path(self.td.name) / 'vmlinuz'
with open(path, 'wb') as f:
Expand Down

0 comments on commit 27bbddf

Please sign in to comment.