| 1 | """Test the release overlay layout and rejection of a changed base.""" |
| 2 | |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | from importlib import import_module |
| 6 | import os |
| 7 | from pathlib import Path |
| 8 | import sys |
| 9 | import tarfile |
| 10 | import tempfile |
| 11 | import unittest |
| 12 | |
| 13 | |
| 14 | sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "tools")) |
| 15 | sidecar = import_module("bundle-x11") |
| 16 | |
| 17 | |
| 18 | class BundleTest(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | work = tempfile.TemporaryDirectory() |
| 21 | self.addCleanup(work.cleanup) |
| 22 | self.base, self.full, self.output = (Path(work.name, name) for name in ("base", "full", "output")) |
| 23 | for client in (self.base, self.full): |
| 24 | self.install_record(client, "vinix-release-desktop", "1-1") |
| 25 | (client / "root/usr/bin").mkdir(parents=True) |
| 26 | (client / "root/usr/bin/pkg").write_bytes(b"desktop package manager") |
| 27 | self.install_record(self.full, "vinix-release-x11", "2-1") |
| 28 | for path in sidecar.RELEASE_COMMANDS: |
| 29 | command = self.full / "root" / path |
| 30 | command.write_bytes(path.encode()) |
| 31 | command.chmod(0o755) |
| 32 | |
| 33 | def install_record(self, client, name, version): |
| 34 | directory = client / "db/local" / f"{name}-{version}" |
| 35 | directory.mkdir(parents=True) |
| 36 | (directory / "desc").write_text(f"%NAME%\n{name}\n\n%VERSION%\n{version}\n\n") |
| 37 | |
| 38 | def write_bundle(self, arch="x86_64", *, output=None, prepared=None): |
| 39 | # Signature and ELF checks run in the signed-root release fixture. |
| 40 | sidecar.bundle(argparse.Namespace( |
| 41 | desktop=self.base, x11=self.full, output=output or self.output, |
| 42 | auditor="/bin/true", arch=arch, prepared=prepared)) |
| 43 | |
| 44 | def test_arm64_overlay_preserves_links_and_omits_desktop_paths(self): |
| 45 | drivers = self.full / "root/usr/lib/dri" |
| 46 | drivers.mkdir(parents=True) |
| 47 | driver = drivers / "swrast_dri.so" |
| 48 | driver.write_bytes(b"software driver") |
| 49 | os.link(driver, drivers / "kms_swrast_dri.so") |
| 50 | (drivers / "swrast.so").symlink_to(driver.name) |
| 51 | |
| 52 | self.write_bundle("aarch64") |
| 53 | |
| 54 | archive = self.output / "vinix-x11-arm64.tar.gz" |
| 55 | digest = hashlib.sha256(archive.read_bytes()).hexdigest() |
| 56 | self.assertEqual(archive.with_suffix(".gz.sha256").read_text(), f"{digest} {archive.name}\n") |
| 57 | with tarfile.open(archive) as tar: |
| 58 | self.assertNotIn("./usr/bin/pkg", tar.getnames()) |
| 59 | self.assertNotIn("./usr/bin", tar.getnames()) |
| 60 | self.assertNotIn("./db", tar.getnames()) |
| 61 | binary = tar.getmember("./usr/bin/Xvfb") |
| 62 | self.assertEqual((binary.uid, binary.gid, binary.mode), (0, 0, 0o755)) |
| 63 | self.assertEqual(tar.getmember("./usr/lib/dri/swrast.so").linkname, driver.name) |
| 64 | self.assertTrue(tar.getmember("./usr/lib/dri/swrast_dri.so").islnk()) |
| 65 | reused = self.output.with_name("reused") |
| 66 | self.write_bundle("aarch64", output=reused, prepared=self.output) |
| 67 | self.assertEqual((reused / archive.name).read_bytes(), archive.read_bytes()) |
| 68 | |
| 69 | def test_changed_desktop_bytes_prevent_publication(self): |
| 70 | (self.full / "root/usr/bin/pkg").write_bytes(b"replaced package manager") |
| 71 | with self.assertRaisesRegex(ValueError, "changed /usr/bin/pkg"): |
| 72 | self.write_bundle() |
| 73 | self.assertFalse(self.output.exists()) |
| 74 | |
| 75 | def test_missing_glx_server_prevents_publication(self): |
| 76 | (self.full / "root/usr/bin/Xvfb-glx").unlink() |
| 77 | with self.assertRaisesRegex(ValueError, "missing executable /usr/bin/Xvfb-glx"): |
| 78 | self.write_bundle() |
| 79 | self.assertFalse(self.output.exists()) |
| 80 | |
| 81 | def test_prepared_archive_must_match_current_overlay_and_checksum(self): |
| 82 | self.write_bundle() |
| 83 | reused = self.output.with_name("reused") |
| 84 | checksum = self.output / "vinix-x11-amd64.tar.gz.sha256" |
| 85 | original = checksum.read_text() |
| 86 | checksum.write_text(original.replace(original[:64], "0" * 64)) |
| 87 | with self.assertRaisesRegex(ValueError, "archive checksum differs"): |
| 88 | self.write_bundle(output=reused, prepared=self.output) |
| 89 | self.assertFalse(reused.exists()) |
| 90 | checksum.write_text(original) |
| 91 | binary = self.full / "root/usr/bin/Xvfb-glx" |
| 92 | metadata = binary.stat() |
| 93 | binary.write_bytes(b"x" * metadata.st_size) |
| 94 | os.utime(binary, ns=(metadata.st_atime_ns, metadata.st_mtime_ns)) |
| 95 | with self.assertRaisesRegex(ValueError, "archive differs from the audited overlay"): |
| 96 | self.write_bundle(output=reused, prepared=self.output) |
| 97 | self.assertFalse(reused.exists()) |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
| 101 | unittest.main() |
| 102 | |