"""PPTX: many highly-compressible slides must not be decompressed past the budgets.""" import io from pathlib import Path import sys import unittest from unittest import mock import zipfile sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "bot")) import file_intake # noqa: E402 def _pptx(slides: int, text: str) -> bytes: buf = io.BytesIO() with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf: for i in range(1, slides + 1): zf.writestr(f"ppt/slides/slide{i}.xml", f"{text}") return buf.getvalue() class PptxBudgetTest(unittest.TestCase): def test_stops_reading_after_text_budget(self): data = _pptx(200, "x" * 5000) with mock.patch.object(file_intake, "_read_entry", wraps=file_intake._read_entry) as rd: res = file_intake.extract("deck.pptx", data) self.assertTrue(res.ok) self.assertTrue(res.truncated) self.assertLess(rd.call_count, 10) def test_total_unpacked_budget_rejects(self): data = _pptx(20, "" * 262144) # 20 MiB of markup, no text with mock.patch.object(file_intake, "_MAX_UNPACKED_BYTES", 8 * 1024 * 1024): res = file_intake.extract("deck.pptx", data) self.assertFalse(res.ok) if __name__ == "__main__": unittest.main()