| 1 | """실제 Ubuntu 패키지와 격리된 사용자 설정으로 제거 후 ZIP 열기를 검증함.""" |
| 2 | |
| 3 | import argparse |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import subprocess |
| 7 | import tempfile |
| 8 | import zipfile |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[2] |
| 11 | |
| 12 | |
| 13 | def run(*args, **kwargs): |
| 14 | return subprocess.run(args, check=True, **kwargs) |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | parser = argparse.ArgumentParser(description=__doc__) |
| 19 | parser.add_argument("package", type=Path) |
| 20 | args = parser.parse_args() |
| 21 | package = args.package.resolve(strict=True) |
| 22 | status = subprocess.run(["dpkg-query", "-W", "-f=${db:Status-Status}", "zipelo"], |
| 23 | capture_output=True, text=True) |
| 24 | if status.returncode == 0 and status.stdout not in ("not-installed", "config-files"): |
| 25 | raise RuntimeError("기존 Zipelo 패키지가 있어 검증을 중단함") |
| 26 | for name in ("/usr/lib/zipelo", "/usr/bin/zipelo", "/usr/share/applications/zipelo.desktop"): |
| 27 | if Path(name).exists() or Path(name).is_symlink(): |
| 28 | raise RuntimeError("기존 설치 파일이 있어 검증을 중단함") |
| 29 | work = ROOT / "tests/work" |
| 30 | work.mkdir(exist_ok=True) |
| 31 | with tempfile.TemporaryDirectory(dir=work) as temporary: |
| 32 | root = Path(temporary) |
| 33 | config = root / "config" |
| 34 | data = root / "data" |
| 35 | config.mkdir() |
| 36 | apps = data / "applications" |
| 37 | apps.mkdir(parents=True) |
| 38 | output = root / "opened.txt" |
| 39 | opener = root / "open-zip" |
| 40 | opener.write_text("#!/bin/sh\n/usr/bin/unzip -p \"$1\" proof.txt > '" + str(output) + "'\n") |
| 41 | opener.chmod(0o755) |
| 42 | (apps / "previous.desktop").write_text( |
| 43 | "[Desktop Entry]\nType=Application\nName=Previous archive app\n" |
| 44 | f"Exec={opener} %f\nMimeType=application/zip;\nTerminal=false\n") |
| 45 | archive = root / "proof.zip" |
| 46 | with zipfile.ZipFile(archive, "w") as zipped: |
| 47 | zipped.writestr("proof.txt", "opened by another app") |
| 48 | env = dict(os.environ, HOME=str(root), XDG_CONFIG_HOME=str(config), |
| 49 | XDG_DATA_HOME=str(data), XDG_STATE_HOME=str(root / "state"), |
| 50 | XDG_DATA_DIRS="/usr/local/share:/usr/share", |
| 51 | XDG_CONFIG_DIRS=str(root / "system-config"), XDG_CURRENT_DESKTOP="X-Generic", |
| 52 | DE="generic", DISPLAY=":987", BROWSER="") |
| 53 | env.pop("WAYLAND_DISPLAY", None) |
| 54 | env.pop("DBUS_SESSION_BUS_ADDRESS", None) |
| 55 | helper = "/usr/lib/zipelo/default-apps" |
| 56 | attempted = False |
| 57 | try: |
| 58 | # 호스트 기본 앱은 건드리지 않고 등록 후보만 실제 설치함. |
| 59 | attempted = True |
| 60 | run("sudo", "-n", "env", "-u", "SUDO_USER", "dpkg", "-i", str(package)) |
| 61 | run("xdg-mime", "default", "previous.desktop", "application/zip", env=env) |
| 62 | run("sh", helper, "install", env=env) |
| 63 | selected = subprocess.check_output( |
| 64 | ["xdg-mime", "query", "default", "application/zip"], env=env, text=True).strip() |
| 65 | assert selected == "zipelo.desktop", selected |
| 66 | run("sh", helper, "remove", env=env) |
| 67 | selected = subprocess.check_output( |
| 68 | ["xdg-mime", "query", "default", "application/zip"], env=env, text=True).strip() |
| 69 | assert selected == "previous.desktop", selected |
| 70 | finally: |
| 71 | if attempted: |
| 72 | run("sudo", "-n", "env", "-u", "SUDO_USER", "dpkg", "-P", "zipelo") |
| 73 | assert not Path(helper).exists() |
| 74 | assert not Path("/usr/share/applications/zipelo.desktop").exists() |
| 75 | run("xdg-open", str(archive), env=env) |
| 76 | assert output.read_text() == "opened by another app" |
| 77 | print("PASS actual package install/remove, isolated default restore and ZIP open via unzip") |
| 78 | print("LIMIT no native desktop UI; SUDO_USER dispatch is covered separately") |
| 79 | |
| 80 | |
| 81 | if __name__ == "__main__": |
| 82 | main() |