team-ljyzipelo

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