64 строк · 3 КиБ
packaging/bundle/test-webkit-relocation.py
| 1 | """Execute the installer WebKit relocation step on a synthetic library.""" |
| 2 | import os |
| 3 | import pathlib |
| 4 | import subprocess |
| 5 | import tempfile |
| 6 | import unittest |
| 7 | |
| 8 | SCRIPT = pathlib.Path(__file__).with_name("relocate.sh").read_text() |
| 9 | STEP = SCRIPT.split("# --- 2. Каталог вспомогательных процессов WebKitGTK", 1)[1] |
| 10 | STEP = STEP.split("# --- 3.", 1)[0] |
| 11 | STEP = STEP.split("\n", 1)[1] |
| 12 | ORIGINAL = b"/usr/lib/webkitgtk-6.0" |
| 13 | |
| 14 | |
| 15 | class Relocation(unittest.TestCase): |
| 16 | def relocate(self, data, runtime=None): |
| 17 | with tempfile.TemporaryDirectory() as directory: |
| 18 | prefix = pathlib.Path(directory) |
| 19 | (prefix / "lib").mkdir() |
| 20 | (prefix / "libexec/webkitgtk-6.0/injected-bundle").mkdir(parents=True) |
| 21 | library = prefix / "lib/libwebkitgtk-6.0.so.4" |
| 22 | library.write_bytes(data) |
| 23 | env = os.environ.copy() |
| 24 | env.pop("XDG_RUNTIME_DIR", None) |
| 25 | if runtime is not None: |
| 26 | env["XDG_RUNTIME_DIR"] = runtime |
| 27 | result = subprocess.run(["sh", "-eu", "-c", STEP], env=env | {"prefix": str(prefix)}, |
| 28 | capture_output=True, text=True) |
| 29 | return result, library.read_bytes() |
| 30 | |
| 31 | def test_missing_environment_uses_private_logind_directory(self): |
| 32 | data = b"start\0" + ORIGINAL + b"\0middle\0" + ORIGINAL + b"/injected-bundle/\0end" |
| 33 | runtime = pathlib.Path(f"/run/user/{os.getuid()}") |
| 34 | link = runtime / "chl-webkit"[:len(ORIGINAL) - len(str(runtime)) - 1] |
| 35 | previous = link.readlink() if link.is_symlink() else None |
| 36 | self.assertFalse(link.exists() and not link.is_symlink(), "Refuse to change an unrelated file") |
| 37 | try: |
| 38 | result, patched = self.relocate(data) |
| 39 | self.assertEqual(result.returncode, 0, result.stderr) |
| 40 | self.assertNotIn(ORIGINAL, patched) |
| 41 | self.assertEqual(len(data), len(patched)) |
| 42 | self.assertIn(f"/run/user/{os.getuid()}/chl".encode(), patched) |
| 43 | result, again = self.relocate(patched) |
| 44 | self.assertEqual(result.returncode, 0, result.stderr) |
| 45 | self.assertEqual(again, patched) |
| 46 | finally: |
| 47 | if link.is_symlink(): |
| 48 | link.unlink() |
| 49 | if previous is not None: |
| 50 | link.symlink_to(previous) |
| 51 | |
| 52 | def test_unknown_library_layout_fails_installation(self): |
| 53 | result, _ = self.relocate(b"unknown WebKit layout") |
| 54 | self.assertNotEqual(result.returncode, 0) |
| 55 | |
| 56 | def test_unusable_runtime_does_not_report_success(self): |
| 57 | result, patched = self.relocate(ORIGINAL + b"\0", "/does/not/exist") |
| 58 | self.assertNotEqual(result.returncode, 0) |
| 59 | self.assertIn(ORIGINAL, patched) |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | unittest.main() |
| 64 |