| 1 | import bz2 |
| 2 | import gzip |
| 3 | import lzma |
| 4 | from pathlib import Path |
| 5 | import sys |
| 6 | import zipfile |
| 7 | |
| 8 | |
| 9 | root = Path(sys.argv[1]) |
| 10 | case = sys.argv[2] |
| 11 | if case == "bomb": |
| 12 | with gzip.open(root / "input", "wb", compresslevel=9) as output: |
| 13 | block = bytes(1024 * 1024) |
| 14 | for _ in range(257): |
| 15 | output.write(block) |
| 16 | elif case in {"header-budget-zip", "header-budget-tar"}: |
| 17 | names = [f"{entry_id:04}-" + "x" * 45 for entry_id in range(1000)] |
| 18 | if case.endswith("-zip"): |
| 19 | with zipfile.ZipFile(root / "input", "w") as output: |
| 20 | for name in names: |
| 21 | output.writestr(name, b"") |
| 22 | else: |
| 23 | import tarfile |
| 24 | |
| 25 | with tarfile.open(root / "input", "w", format=tarfile.USTAR_FORMAT) as output: |
| 26 | for name in names: |
| 27 | output.addfile(tarfile.TarInfo(name)) |
| 28 | elif case.startswith("header-budget-"): |
| 29 | from seven_zip_fixtures import archive |
| 30 | |
| 31 | entries = [(f"{entry_id:04}-" + "x" * 45, b"", 0x20) for entry_id in range(1000)] |
| 32 | size = int(case.rsplit("-", 1)[1]) |
| 33 | if size: |
| 34 | entries.append(("payload", b"x" * size, 0x20)) |
| 35 | (root / "input").write_bytes(archive(entries, encoded="encoded" in case)) |
| 36 | elif case.startswith("tar-stream-limit-"): |
| 37 | import io |
| 38 | import tarfile |
| 39 | |
| 40 | kind, mode = case.removeprefix("tar-stream-limit-").split("-") |
| 41 | payload = bytes(index % 251 for index in range(65_537)) |
| 42 | raw = io.BytesIO() |
| 43 | with tarfile.open(fileobj=raw, mode="w", format=tarfile.USTAR_FORMAT) as archive: |
| 44 | entry = tarfile.TarInfo("payload") |
| 45 | entry.size = len(payload) |
| 46 | archive.addfile(entry, io.BytesIO(payload)) |
| 47 | data = raw.getvalue() |
| 48 | encode = {"gz": gzip.compress, "bz2": bz2.compress, "xz": lzma.compress}[kind] |
| 49 | parts = ( |
| 50 | [data] |
| 51 | if mode == "single" |
| 52 | else [data[: len(data) // 2], data[len(data) // 2 :]] |
| 53 | ) |
| 54 | (root / "input").write_bytes(b"".join(encode(part) for part in parts)) |
| 55 | (root / "expected").write_bytes(payload) |
| 56 | (root / "stream-size").write_text(str(len(data))) |
| 57 | elif case == "wide-names": |
| 58 | with zipfile.ZipFile(root / "input", "w") as archive: |
| 59 | for entry_id in range(9000): |
| 60 | archive.writestr(f"{entry_id:04}" + "x" * 236, b"") |
| 61 | elif case == "many-entries": |
| 62 | with zipfile.ZipFile(root / "input", "w") as archive: |
| 63 | for entry_id in range(200_000): |
| 64 | archive.writestr(f"e{entry_id}", b"") |
| 65 | elif case in {"phase-directories", "phase-files"}: |
| 66 | with zipfile.ZipFile(root / "input", "w") as archive: |
| 67 | for entry_id in range(8): |
| 68 | archive.writestr( |
| 69 | f"entry-{entry_id}/" if case == "phase-directories" else f"entry-{entry_id}", |
| 70 | b"" if case == "phase-directories" else b"x", |
| 71 | ) |
| 72 | elif case == "selection-entries": |
| 73 | with zipfile.ZipFile(root / "input", "w") as archive: |
| 74 | for entry_id in range(32_770): |
| 75 | archive.writestr(f"e{entry_id}", b"x") |
| 76 | elif case == "empty-directories": |
| 77 | with zipfile.ZipFile(root / "input", "w") as archive: |
| 78 | for entry_id in range(36): |
| 79 | archive.writestr(f"entry-{entry_id}/", b"") |
| 80 | elif case == "split-zip": |
| 81 | with zipfile.ZipFile(root / "input", "w", compression=zipfile.ZIP_DEFLATED) as archive: |
| 82 | block = bytes(1024 * 1024) |
| 83 | for name in ["first.bin", "second.bin"]: |
| 84 | with archive.open(name, "w") as output: |
| 85 | for _ in range(129): |
| 86 | output.write(block) |
| 87 | elif case in {"bz2", "xz"}: |
| 88 | source = Path(__file__).with_name("fixtures").joinpath("sample.tar").read_bytes() |
| 89 | encode = bz2.compress if case == "bz2" else lzma.compress |
| 90 | (root / "input").write_bytes(encode(source)) |
| 91 | else: |
| 92 | raise ValueError(case) |