| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- """Tests for user-mode OSS CAD Suite bootstrap safety guards."""
- from __future__ import annotations
- import sys
- import tempfile
- import unittest
- import zipfile
- from pathlib import Path
- from unittest import mock
- sys.dont_write_bytecode = True
- ROOT = Path(__file__).resolve().parents[2]
- sys.path.insert(0, str(ROOT / "tools"))
- import bootstrap_oss_cad_suite as bootstrap # noqa: E402
- class BootstrapOssCadSuiteTests(unittest.TestCase):
- def test_default_paths_use_path_home_not_administration_literal(self) -> None:
- for path in [bootstrap.DEFAULT_ROOT, bootstrap.DEFAULT_DOWNLOAD_DIR, bootstrap.DEFAULT_COCOTB_VENV]:
- self.assertIn(str(Path.home()), str(path))
- self.assertNotIn(r"C:\Users\Administration", str(path))
- def test_safe_toolchain_root_rejects_workspace_root(self) -> None:
- with self.assertRaises(ValueError):
- bootstrap._assert_safe_toolchain_root(ROOT)
- def test_zip_extract_rejects_parent_traversal_member(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- base = Path(tmp) / "CodexToolchains" / "CPLD"
- archive = Path(tmp) / "bad.zip"
- with zipfile.ZipFile(archive, "w") as zf:
- zf.writestr("../escape.txt", "bad")
- with mock.patch.object(bootstrap, "DEFAULT_TOOLCHAIN_BASE", base):
- with self.assertRaises(ValueError):
- bootstrap.extract_archive(archive, base / "oss-cad-suite" / "latest")
- def test_extract_archive_refuses_to_replace_unowned_install_root(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- base = Path(tmp) / "CodexToolchains" / "CPLD"
- install_root = base / "oss-cad-suite" / "latest"
- install_root.mkdir(parents=True)
- archive = Path(tmp) / "tool.zip"
- with zipfile.ZipFile(archive, "w") as zf:
- zf.writestr("bin/iverilog.exe", "fake")
- with mock.patch.object(bootstrap, "DEFAULT_TOOLCHAIN_BASE", base):
- with self.assertRaises(RuntimeError):
- bootstrap.extract_archive(archive, install_root)
- def test_install_latest_writes_owner_marker_and_manifest(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- base = Path(tmp) / "CodexToolchains" / "CPLD"
- download_dir = base / "downloads"
- archive = download_dir / "oss-cad-suite-windows-x64-test.exe"
- release = {
- "tag_name": "test",
- "assets": [
- {
- "name": archive.name,
- "browser_download_url": "https://example.invalid/tool.exe",
- "size": 1,
- }
- ],
- }
- def fake_download(_url: str, output: Path) -> None:
- output.parent.mkdir(parents=True, exist_ok=True)
- output.write_text("fake sfx", encoding="utf-8")
- def fake_extract(_archive: Path, install_root: Path) -> None:
- (install_root / "bin").mkdir(parents=True, exist_ok=True)
- for tool in bootstrap.REQUIRED_TOOLS:
- (install_root / "bin" / f"{tool}.exe").write_text("fake", encoding="utf-8")
- with mock.patch.object(bootstrap, "DEFAULT_TOOLCHAIN_BASE", base):
- payload = bootstrap.install_latest_windows_x64(
- install_root=base / "oss-cad-suite" / "latest",
- download_dir=download_dir,
- release_payload=release,
- download_func=fake_download,
- extract_func=fake_extract,
- )
- install_root = Path(payload["install_root"])
- self.assertEqual(payload["status"], "present")
- self.assertTrue((install_root / bootstrap.OWNER_MARKER_NAME).is_file())
- self.assertTrue((install_root / bootstrap.INSTALL_MANIFEST_NAME).is_file())
- def test_install_cocotb_venv_installs_extended_user_mode_packages(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- base = Path(tmp) / "CodexToolchains" / "CPLD"
- source_python = Path(tmp) / "python.exe"
- source_python.write_text("python", encoding="utf-8")
- venv_dir = base / "venvs" / "pid-study-cocotb-venv"
- venv_python = venv_dir / "Scripts" / "python.exe"
- commands: list[list[str]] = []
- def fake_run_checked(cmd: list[str], **_kwargs):
- commands.append(cmd)
- if cmd[:3] == [str(source_python), "-m", "venv"]:
- venv_python.parent.mkdir(parents=True, exist_ok=True)
- venv_python.write_text("python", encoding="utf-8")
- if cmd[-1].startswith("import cocotb"):
- return mock.Mock(returncode=0, stdout=bootstrap.DEFAULT_COCOTB_VERSION + "\n", stderr="")
- return mock.Mock(returncode=0, stdout="", stderr="")
- with mock.patch.object(bootstrap, "DEFAULT_TOOLCHAIN_BASE", base), \
- mock.patch.object(bootstrap, "run_checked", side_effect=fake_run_checked):
- payload = bootstrap.install_cocotb_venv(venv_dir=venv_dir, python_exe=source_python)
- install_commands = [" ".join(cmd) for cmd in commands if "install" in cmd]
- joined = "\n".join(install_commands)
- self.assertEqual(payload["status"], "present")
- self.assertIn("pytest", joined)
- self.assertIn("hypothesis", joined)
- self.assertIn("pyserial", joined)
- self.assertTrue((venv_dir / bootstrap.OWNER_MARKER_NAME).is_file())
- self.assertTrue((venv_dir / bootstrap.INSTALL_MANIFEST_NAME).is_file())
- def test_uninstall_owned_toolchain_dry_run_does_not_delete_marker_owned_root(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- base = Path(tmp) / "CodexToolchains" / "CPLD"
- install_root = base / "oss-cad-suite" / "latest"
- install_root.mkdir(parents=True)
- bootstrap._write_owner_marker(install_root, "oss_cad_suite", {"test": True})
- payload = bootstrap.uninstall_owned_toolchain(install_root=install_root, cocotb_venv=base / "venv", dry_run=True)
- self.assertEqual(payload["schema"], "offline-toolchain-uninstall-v1")
- self.assertEqual(payload["mode"], "dry_run")
- self.assertEqual(payload["status"], "pass")
- self.assertTrue(install_root.exists())
- self.assertEqual(payload["targets"][0]["action"], "would_delete")
- def test_uninstall_owned_toolchain_refuses_unowned_path(self) -> None:
- with tempfile.TemporaryDirectory() as tmp:
- base = Path(tmp) / "CodexToolchains" / "CPLD"
- install_root = base / "oss-cad-suite" / "latest"
- install_root.mkdir(parents=True)
- payload = bootstrap.uninstall_owned_toolchain(install_root=install_root, cocotb_venv=base / "venv", dry_run=False)
- self.assertEqual(payload["status"], "blocked_not_owned")
- self.assertTrue(install_root.exists())
- if __name__ == "__main__":
- unittest.main()
|