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