diff --git a/tests/test_limit_window.py b/tests/test_limit_window.py new file mode 100644 index 000000000..3b6944a35 --- /dev/null +++ b/tests/test_limit_window.py @@ -0,0 +1,76 @@ +"""--limit ending inside a stream: the index tools leave that stream out instead of counting its whole stored size. + +story_index (#712) and navmesh_index (#766) already inflate only inside the scan window; anim_index, rbf0_index, +restofworld_index and bvh_index inflated up to the end of the archive, so a stream that started in the window but +ended after it was counted as readable and readable + zero + unread exceeded the span. rpf9_carve.py has its own +test in test_rpf9_carve.py. +""" +import hashlib +import os +import struct +import sys +import tempfile +import unittest +import zlib + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(ROOT, "tools")) + +import anim_index # noqa: E402 +import bvh_index # noqa: E402 +import rbf0_index # noqa: E402 +import restofworld_index # noqa: E402 + +GAP = 0x100 + + +def noisy(n, seed): + """Bytes zlib can't shrink, so a stream's stored size is about n.""" + out = bytearray() + while len(out) < n: + out += hashlib.sha256(bytes([seed]) + len(out).to_bytes(4, "little")).digest() + return bytes(out[:n]) + + +def summary_of(result): + """The tools return (rows, summary), (rows, records, summary) or (rows, [summary]).""" + last = result[-1] + return last[0] if isinstance(last, list) else last + + +def first_of(summary, *keys): + for key in keys: + if key in summary: + return summary[key] + raise KeyError(keys) + + +class LimitWindowTest(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.first = zlib.compress(noisy(4000, 1)) + data = bytearray(0x40 + GAP) + self.first + zlib.compress(noisy(4000, 2)) + data[0:4] = b"9FPR" + struct.pack_into(" int: break s = mt.start() if m[s:s + 3] == b"KB2": - if s + 0x1C > size: # header cut off by the end of the file (a truncated copy): not a video + if s + 0x1C > end: # header cut off by the end of the file or of --limit's window: not a video pos = s + 1 continue blen = struct.unpack_from(" int: d = zlib.decompressobj() try: chunks, p = [], s - while not d.eof and p < size: - chunk = m[p:p + (1 << 20)] + # Inflate only inside the scan window (all of the file without --limit): a stream that runs past + # its end is left out, so readable + zero + unread add up to the span, as in story_index (#712). + while not d.eof and p < end: + chunk = m[p:min(p + (1 << 20), end)] if not chunk: break chunks.append(d.decompress(chunk))