system/tests/dkms_inputs.py

98 lines
4.9 KiB
Python

#!/usr/bin/env python3
"""Exercise the real archive gate and target-image header resolver without installing."""
import importlib.util
import io
from pathlib import Path
import subprocess
import sys
import tarfile
import tempfile
import types
import unittest
ROOT = Path(__file__).resolve().parents[1]
# Resolver helper needs package DB objects, not a live libalpm handle.
sys.modules['pyalpm'] = types.ModuleType('pyalpm')
sys.modules['pycman'] = types.ModuleType('pycman')
config = types.ModuleType('pycman.config')
config.PacmanConfig = None
sys.modules['pycman.config'] = config
spec = importlib.util.spec_from_file_location('packages', ROOT / 'adapters/package-transaction.py')
packages = importlib.util.module_from_spec(spec)
spec.loader.exec_module(packages)
class Inputs(unittest.TestCase):
def inspect(self, name, payload):
with tempfile.TemporaryDirectory() as directory:
generation = Path(directory)
(generation / 'inputs').mkdir()
(generation / 'logs').mkdir()
policy = generation / 'root/usr/lib/archipelago/hook-policy'
policy.mkdir(parents=True)
for file in ('hooks.sha256', 'scriptlets.sha256'):
(policy / file).touch()
with tarfile.open(generation / 'inputs/test.pkg.tar.gz', 'w:gz') as archive:
for path, content in {'.PKGINFO': 'pkgname = ' + name + '\n', **payload}.items():
data = content.encode()
entry = tarfile.TarInfo(path)
entry.size = len(data)
archive.addfile(entry, io.BytesIO(data))
adapter = (ROOT / 'adapters/extensions.sh').read_text()
gate = adapter.split('# Inspect exact signed/downloaded inputs', 1)[1].split('if $dkms_enabled; then', 1)[0]
gate = '# Inspect exact signed/downloaded inputs' + gate
return subprocess.run(['bash', '-euo', 'pipefail', '-c',
'generation=$1; root=$generation/root; policy=$root/usr/lib/archipelago/hook-policy\n' + gate,
'dkms-test', directory], capture_output=True, text=True)
def test_arbitrary_dkms_names_and_contents(self):
for name, version in [('broadcom-wl-dkms', '6.30.223.271'), ('custom-driver', '999')]:
result = self.inspect(name, {
f'usr/src/driver-{version}/dkms.conf': 'exit 99 # must not execute on the host\n',
f'usr/src/driver-{version}/source.c': version,
'.INSTALL': 'exit 99\n',
'usr/share/libalpm/hooks/driver.hook': 'new package hook contents\n'})
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_framework_has_no_archive_hash_gate(self):
result = self.inspect('dkms', {'.INSTALL': 'new framework revision\n',
'usr/share/libalpm/hooks/70-dkms-install.hook': 'new hook revision\n'})
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_other_payload_checks_remain(self):
for payload in ({'usr/src/random/file.c': 'x'},
{'usr/src/driver/dkms.conf': 'x', 'boot/kernel': 'x'},
{'.INSTALL': 'unknown non-DKMS action'}):
self.assertNotEqual(self.inspect('some-package', payload).returncode, 0)
def test_exact_headers_and_missing_snapshot(self):
def pkg(name, version):
return types.SimpleNamespace(name=name, version=version)
class DB:
def __init__(self, *items): self.items = {p.name: p for p in items}
def get_pkg(self, name): return self.items.get(name)
kernel = pkg('linux', '7.2.14-1')
header = pkg('linux-headers', kernel.version)
local = DB(kernel)
handle = types.SimpleNamespace(get_localdb=lambda: local)
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
modules = root / '7.2.14-arch1-1'
modules.mkdir()
(modules / 'vmlinuz').touch()
(modules / 'pkgbase').write_text('linux\n')
self.assertEqual(packages.kernel_headers(handle, [], [], root), [])
additions = [pkg('dkms', '99')]
self.assertEqual(packages.kernel_headers(handle, [DB(header)], additions, root), [header])
for database in (DB(), DB(pkg('linux-headers', '7.3-1'))):
with self.assertRaisesRegex(ValueError, 'Exact target kernel headers unavailable'):
packages.kernel_headers(handle, [database], additions, root)
local.items[header.name] = header
self.assertEqual(packages.kernel_headers(handle, [], additions, root), [])
local.items[header.name] = pkg(header.name, 'wrong')
with self.assertRaisesRegex(ValueError, 'Installed headers differ'):
packages.kernel_headers(handle, [], additions, root)
if __name__ == '__main__':
unittest.main()