| 1 | import gzip |
| 2 | import hashlib |
| 3 | import io |
| 4 | import json |
| 5 | import tarfile |
| 6 | import zlib |
| 7 | |
| 8 | |
| 9 | def build(): |
| 10 | payload = b"123456789" |
| 11 | archive = io.BytesIO() |
| 12 | with tarfile.open(fileobj=archive, mode="w", format=tarfile.USTAR_FORMAT) as writer: |
| 13 | info = tarfile.TarInfo("digits.txt") |
| 14 | info.size = len(payload) |
| 15 | info.mode = 0o644 |
| 16 | info.mtime = 0 |
| 17 | writer.addfile(info, io.BytesIO(payload)) |
| 18 | tar_bytes = archive.getvalue()[:2048] |
| 19 | values = { |
| 20 | "digits": payload, |
| 21 | "gzip_digits": gzip.compress(payload, compresslevel=6, mtime=0), |
| 22 | "gzip_empty": gzip.compress(b"", compresslevel=6, mtime=0), |
| 23 | "ustar_one": tar_bytes, |
| 24 | "ustar_sha256": hashlib.sha256(tar_bytes).digest(), |
| 25 | } |
| 26 | if gzip.decompress(values["gzip_digits"]) != payload: |
| 27 | raise RuntimeError("GZip fixture 검증 실패") |
| 28 | with tarfile.open(fileobj=io.BytesIO(tar_bytes), mode="r:") as reader: |
| 29 | if reader.getnames() != ["digits.txt"] or reader.extractfile("digits.txt").read() != payload: |
| 30 | raise RuntimeError("USTAR fixture 검증 실패") |
| 31 | metadata = { |
| 32 | "provenance": "Python 표준 gzip/zlib와 tarfile USTAR_FORMAT으로 만든 합성 bytes임", |
| 33 | "gzip": {"mtime": 0, "level": 6, "crc32": zlib.crc32(payload)}, |
| 34 | "ustar": {"trimmed_length": 2048, "terminator_blocks": 2, "mtime": 0, "mode": "0644"}, |
| 35 | "files": {name: {"bytes": len(data), "sha256": hashlib.sha256(data).hexdigest()} |
| 36 | for name, data in sorted(values.items())}, |
| 37 | "limitations": "ZIP 결과는 native 호출과 비교함. 독립 ZIP parser나 codec 검증이 아님", |
| 38 | } |
| 39 | return ( |
| 40 | "".join(name + " " + data.hex() + "\n" for name, data in sorted(values.items())), |
| 41 | json.dumps(metadata, ensure_ascii=False, indent=4) + "\n", |
| 42 | ) |