| 1 | import argparse |
| 2 | import importlib.util |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import struct |
| 6 | import re |
| 7 | import shutil |
| 8 | import subprocess |
| 9 | import tempfile |
| 10 | import sys |
| 11 | from unittest.mock import patch |
| 12 | import unittest |
| 13 | |
| 14 | import run_rust |
| 15 | |
| 16 | ROOT = Path(__file__).resolve().parents[2] |
| 17 | SPEC = importlib.util.spec_from_file_location("windows_installer", ROOT / "packaging/windows/rust/build.py") |
| 18 | MODULE = importlib.util.module_from_spec(SPEC) |
| 19 | SPEC.loader.exec_module(MODULE) |
| 20 | |
| 21 | |
| 22 | class InstallerInputTests(unittest.TestCase): |
| 23 | def setUp(self): |
| 24 | runs = run_rust.test_work_directory(ROOT) / "fixtures/windows-installer" |
| 25 | runs.mkdir(parents=True, exist_ok=True) |
| 26 | self.directory = tempfile.TemporaryDirectory(dir=runs) |
| 27 | self.addCleanup(self.directory.cleanup) |
| 28 | self.root = Path(self.directory.name) |
| 29 | |
| 30 | def test_truncated_and_non_pe_payloads_are_rejected(self): |
| 31 | for content in (b"", b"MZ", b"not an executable" * 8, |
| 32 | b"MZ" + bytes(58) + struct.pack("<I", 65535)): |
| 33 | payload = self.root / "invalid.exe" |
| 34 | payload.write_bytes(content) |
| 35 | with self.assertRaises(ValueError): |
| 36 | MODULE.pe_machine(payload) |
| 37 | |
| 38 | def test_x64_coff_machine_is_read_from_pe_offset(self): |
| 39 | payload = self.root / "header.exe" |
| 40 | payload.write_bytes(b"MZ" + bytes(58) + struct.pack("<I", 128) |
| 41 | + bytes(64) + b"PE\0\0" + struct.pack("<H", 0x8664)) |
| 42 | self.assertEqual(MODULE.pe_machine(payload), 0x8664) |
| 43 | payload.write_bytes(payload.read_bytes()[:128] + b"badPE!") |
| 44 | with self.assertRaises(ValueError): |
| 45 | MODULE.pe_machine(payload) |
| 46 | |
| 47 | def test_existing_output_is_preserved_before_any_build(self): |
| 48 | output = self.root / "output" |
| 49 | output.mkdir() |
| 50 | sentinel = output / "user-data" |
| 51 | sentinel.write_bytes(b"preserve") |
| 52 | args = argparse.Namespace(output=output, target_dir=self.root / "target") |
| 53 | with self.assertRaises(FileExistsError): |
| 54 | MODULE.build(args) |
| 55 | self.assertEqual(sentinel.read_bytes(), b"preserve") |
| 56 | self.assertEqual(list(output.iterdir()), [sentinel]) |
| 57 | self.assertFalse(args.target_dir.exists()) |
| 58 | |
| 59 | @unittest.skipUnless(shutil.which("makensis"), "NSIS가 설치되지 않음") |
| 60 | def test_nsis_compiles_payloads_and_finish_callbacks(self): |
| 61 | payload = self.root / "payload" |
| 62 | payload.mkdir() |
| 63 | for name in ("Zipelo.exe", "zipelo-archive-worker.exe", "LICENSE", "BUILD-INFO.json", "THIRD-PARTY-LICENSES.txt"): |
| 64 | (payload / name).write_bytes(b"compile-only test fixture") |
| 65 | shutil.copyfile(ROOT / "packaging/windows/rust/installer.nsi", self.root / "installer.nsi") |
| 66 | shutil.copyfile(ROOT / "experiments/rust-ui-memory-app/src/assets/icons/zipelo.ico", self.root / "zipelo.ico") |
| 67 | completed = subprocess.run(["makensis", "-WX", "installer.nsi"], cwd=self.root, |
| 68 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 69 | self.assertEqual(completed.returncode, 0, completed.stdout) |
| 70 | installer = self.root / "Zipelo-0.2.2-windows-x64-setup.exe" |
| 71 | self.assertIn(MODULE.pe_machine(installer), (0x14C, 0x8664)) |
| 72 | self.assertGreater(installer.stat().st_size, 100000) |
| 73 | |
| 74 | def test_command_line_directory_is_reset_before_install_side_effects(self): |
| 75 | source = (ROOT / "packaging/windows/rust/installer.nsi").read_text() |
| 76 | section = source.split('Section "Zipelo"', 1)[1].split("SectionEnd", 1)[0] |
| 77 | commands = [line.strip() for line in section.splitlines() |
| 78 | if line.strip() and not line.strip().startswith(";")] |
| 79 | self.assertEqual(commands[0], 'StrCpy $INSTDIR "${INSTALLROOT}"') |
| 80 | root = re.search(r'^!define INSTALLROOT "([^"\n]+)"$', source, re.MULTILINE) |
| 81 | self.assertIsNotNone(root) |
| 82 | self.assertEqual(root.group(1), "$LOCALAPPDATA\\Programs\\Zipelo") |
| 83 | self.assertIn('InstallDir "${INSTALLROOT}"', source) |
| 84 | uninstaller = source.split("Function un.onInit", 1)[1].split("FunctionEnd", 1)[0] |
| 85 | self.assertIn('StrCmp $INSTDIR "${INSTALLROOT}" 0 invalid', uninstaller) |
| 86 | |
| 87 | def test_source_commit_change_cannot_reuse_an_old_binary(self): |
| 88 | repo = self.root / "source" |
| 89 | repo.mkdir() |
| 90 | manifest = repo / "Cargo.toml" |
| 91 | manifest.write_text("fixture") |
| 92 | real_run = subprocess.run |
| 93 | environment = dict(MODULE.os.environ, GIT_AUTHOR_NAME="Zipelo test", |
| 94 | GIT_AUTHOR_EMAIL="[email protected]", GIT_COMMITTER_NAME="Zipelo test", |
| 95 | GIT_COMMITTER_EMAIL="[email protected]") |
| 96 | |
| 97 | def git(*args): |
| 98 | real_run(["git", *args], cwd=repo, env=environment, check=True, |
| 99 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 100 | |
| 101 | git("init", "-b", "features/installer-fixture") |
| 102 | git("add", ".") |
| 103 | git("commit", "-m", "첫 시험 소스를 기록함") |
| 104 | |
| 105 | def cached_compiler(command, **options): |
| 106 | if command[0] != "cargo": |
| 107 | return real_run(command, **options) |
| 108 | target = Path(command[command.index("--target-dir") + 1]) |
| 109 | output = target / MODULE.TARGET / "debug/fixture.exe" |
| 110 | output.parent.mkdir(parents=True, exist_ok=True) |
| 111 | if not output.exists(): |
| 112 | header = b"MZ" + bytes(58) + struct.pack("<I", 128) + bytes(64) |
| 113 | output.write_bytes(header + b"PE\0\0" + struct.pack("<H", 0x8664) |
| 114 | + manifest.read_bytes()) |
| 115 | return subprocess.CompletedProcess(command, 0) |
| 116 | |
| 117 | with patch.dict(MODULE.os.environ, {"CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER": sys.executable}): |
| 118 | with patch.object(MODULE.subprocess, "run", side_effect=cached_compiler): |
| 119 | first, first_record = MODULE.build_binary(manifest, "fixture", self.root / "target", "dev") |
| 120 | manifest.write_text("changed fixture") |
| 121 | git("add", ".") |
| 122 | git("commit", "-m", "변경된 시험 소스를 기록함") |
| 123 | with patch.object(MODULE.subprocess, "run", side_effect=cached_compiler): |
| 124 | second, second_record = MODULE.build_binary(manifest, "fixture", self.root / "target", "dev") |
| 125 | self.assertNotEqual(first_record["commit"], second_record["commit"]) |
| 126 | self.assertNotEqual(first_record["sha256"], second_record["sha256"]) |
| 127 | self.assertTrue(second.read_bytes().endswith(b"changed fixture")) |
| 128 | self.assertTrue(first.read_bytes().endswith(b"fixture")) |