from pathlib import Path from zipfile import ZipFile from l4d2web.services.global_map_cache import ( extracted_vpk_md5, global_overlay_cache_root, safe_extract_zip_vpks, source_cache_root, ) def test_global_overlay_cache_paths(tmp_path, monkeypatch): monkeypatch.setenv("LEFT4ME_ROOT", str(tmp_path)) assert global_overlay_cache_root() == tmp_path / "global_overlay_cache" assert source_cache_root("l4d2center-maps") == tmp_path / "global_overlay_cache" / "l4d2center-maps" def test_safe_extract_zip_vpks_extracts_only_vpks(tmp_path): archive = tmp_path / "maps.zip" with ZipFile(archive, "w") as zf: zf.writestr("FatalFreight.vpk", b"vpk-bytes") zf.writestr("readme.txt", b"ignore") out_dir = tmp_path / "out" files = safe_extract_zip_vpks(archive, out_dir) assert files == [out_dir / "FatalFreight.vpk"] assert (out_dir / "FatalFreight.vpk").read_bytes() == b"vpk-bytes" assert not (out_dir / "readme.txt").exists() def test_safe_extract_zip_vpks_rejects_path_traversal(tmp_path): archive = tmp_path / "bad.zip" with ZipFile(archive, "w") as zf: zf.writestr("../evil.vpk", b"bad") try: safe_extract_zip_vpks(archive, tmp_path / "out") except ValueError as exc: assert "unsafe archive member" in str(exc) else: raise AssertionError("path traversal must fail") def test_extracted_vpk_md5(tmp_path): p = tmp_path / "x.vpk" p.write_bytes(b"abc") assert extracted_vpk_md5(p) == "900150983cd24fb0d6963f7d28e17f72"