team-ljyzipelo

공개
124줄6.2 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 completed = subprocess.run(["makensis", "-WX", "installer.nsi"], cwd=self.root,
64 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
65 self.assertEqual(completed.returncode, 0, completed.stdout)
66 installer = self.root / "Zipelo-0.1.0-dev-windows-x64-setup.exe"
67 self.assertIn(MODULE.pe_machine(installer), (0x14C, 0x8664))
68 self.assertGreater(installer.stat().st_size, 100000)
69
70 def test_command_line_directory_is_reset_before_install_side_effects(self):
71 source = (ROOT / "packaging/windows/rust/installer.nsi").read_text()
72 section = source.split('Section "Zipelo"', 1)[1].split("SectionEnd", 1)[0]
73 commands = [line.strip() for line in section.splitlines()
74 if line.strip() and not line.strip().startswith(";")]
75 self.assertEqual(commands[0], 'StrCpy $INSTDIR "${INSTALLROOT}"')
76 root = re.search(r'^!define INSTALLROOT "([^"\n]+)"$', source, re.MULTILINE)
77 self.assertIsNotNone(root)
78 self.assertEqual(root.group(1), "$LOCALAPPDATA\\Programs\\Zipelo-Dev")
79 self.assertIn('InstallDir "${INSTALLROOT}"', source)
80 uninstaller = source.split("Function un.onInit", 1)[1].split("FunctionEnd", 1)[0]
81 self.assertIn('StrCmp $INSTDIR "${INSTALLROOT}" 0 invalid', uninstaller)
82
83 def test_source_commit_change_cannot_reuse_an_old_binary(self):
84 repo = self.root / "source"
85 repo.mkdir()
86 manifest = repo / "Cargo.toml"
87 manifest.write_text("fixture")
88 real_run = subprocess.run
89 environment = dict(MODULE.os.environ, GIT_AUTHOR_NAME="Zipelo test",
90 GIT_AUTHOR_EMAIL="[email protected]", GIT_COMMITTER_NAME="Zipelo test",
91 GIT_COMMITTER_EMAIL="[email protected]")
92
93 def git(*args):
94 real_run(["git", *args], cwd=repo, env=environment, check=True,
95 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
96
97 git("init", "-b", "features/installer-fixture")
98 git("add", ".")
99 git("commit", "-m", "첫 시험 소스를 기록함")
100
101 def cached_compiler(command, **options):
102 if command[0] != "cargo":
103 return real_run(command, **options)
104 target = Path(command[command.index("--target-dir") + 1])
105 output = target / MODULE.TARGET / "debug/fixture.exe"
106 output.parent.mkdir(parents=True, exist_ok=True)
107 if not output.exists():
108 header = b"MZ" + bytes(58) + struct.pack("<I", 128) + bytes(64)
109 output.write_bytes(header + b"PE\0\0" + struct.pack("<H", 0x8664)
110 + manifest.read_bytes())
111 return subprocess.CompletedProcess(command, 0)
112
113 with patch.dict(MODULE.os.environ, {"CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER": sys.executable}):
114 with patch.object(MODULE.subprocess, "run", side_effect=cached_compiler):
115 first, first_record = MODULE.build_binary(manifest, "fixture", self.root / "target", "dev")
116 manifest.write_text("changed fixture")
117 git("add", ".")
118 git("commit", "-m", "변경된 시험 소스를 기록함")
119 with patch.object(MODULE.subprocess, "run", side_effect=cached_compiler):
120 second, second_record = MODULE.build_binary(manifest, "fixture", self.root / "target", "dev")
121 self.assertNotEqual(first_record["commit"], second_record["commit"])
122 self.assertNotEqual(first_record["sha256"], second_record["sha256"])
123 self.assertTrue(second.read_bytes().endswith(b"changed fixture"))
124 self.assertTrue(first.read_bytes().endswith(b"fixture"))