From 76303abaecf1fdb59b7b47bf4bea9f51bfe9d0f5 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Sat, 8 Aug 2026 16:50:31 -0500 Subject: [PATCH] test(hashing): cover tar read failures Truncated tars surface while listing, since `getmembers()` walks the whole archive before any member is streamed, so the streaming guard is covered directly for the archive types that share it. Co-Authored-By: Claude Opus 5 --- backend/tests/utils/test_archives.py | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/backend/tests/utils/test_archives.py b/backend/tests/utils/test_archives.py index ecd9a598c..2e4c706a5 100644 --- a/backend/tests/utils/test_archives.py +++ b/backend/tests/utils/test_archives.py @@ -1,3 +1,5 @@ +import io +import tarfile import time import zipfile from pathlib import Path @@ -532,3 +534,34 @@ class TestZipAndTarReadFailures: with pytest.raises(archives.ArchiveReadError): list(archives.read_tar_archive_files(path, [], [])) + + def test_truncated_tar_raises(self, tmp_path): + path = tmp_path / "game.tar" + with tarfile.open(path, "w") as tf: + for name, data in (("a.bin", b"A" * 4096), ("b.bin", b"B" * 8192)): + info = tarfile.TarInfo(name) + info.size = len(data) + tf.addfile(info, io.BytesIO(data)) + + # Cut into the second member's data, not just the trailing padding. + raw = path.read_bytes() + path.write_bytes(raw[: len(raw) // 2]) + + with pytest.raises(archives.ArchiveReadError): + for _name, _size, chunks in archives.read_tar_archive_files(path, [], []): + list(chunks) + + def test_member_read_failure_is_wrapped(self): + """A member that fails mid-stream is reported as an archive error. + + Tar failures surface while listing rather than while streaming, since + `getmembers()` walks the whole archive first, so the streaming guard is + covered directly here for every archive type that uses it. + """ + + class _FailingReader(io.RawIOBase): + def read(self, size=-1): + raise EOFError("Compressed file ended before the end-of-stream marker") + + with pytest.raises(archives.ArchiveReadError): + list(archives._iter_chunks(_FailingReader(), Path("/fake.tar.gz"), "a.bin"))