"""Yükleme deposu testleri: python3 tests/test_upload_cache.py""" import hashlib import io import tempfile import zipfile from pathlib import Path from _t import run import upload_cache import zipcheck def _cache(root: Path, max_bytes=None): cache = upload_cache.UploadCache(root, max_bytes=max_bytes) cache.free_bytes = lambda: 10 ** 12 # disk doluluğu testleri etkilemesin return cache def _put(cache, data: bytes, name="oyun.zip"): entry_id, part = cache.new_staging_path() part.write_bytes(data) return cache.commit(entry_id, part, name, hashlib.sha256(data).hexdigest()) def test_commit_and_list(): with tempfile.TemporaryDirectory() as tmp: cache = _cache(Path(tmp)) entry = _put(cache, b"zipdata", "Benim Oyunum!.zip") assert [e.id for e in cache.list()] == [entry.id] assert entry.public()["name"] == "Benim_Oyunum_.zip", entry.public() assert cache.path_of(entry.id).read_bytes() == b"zipdata" def test_same_hash_deduplicated(): with tempfile.TemporaryDirectory() as tmp: cache = _cache(Path(tmp)) a = _put(cache, b"same") b = _put(cache, b"same", "kopya.zip") assert a.id == b.id assert len([p for p in Path(tmp).iterdir() if p.is_dir()]) == 1 def test_reload_from_disk(): with tempfile.TemporaryDirectory() as tmp: entry = _put(_cache(Path(tmp)), b"kalici") again = _cache(Path(tmp)) assert again.get(entry.id) is not None assert again.get(entry.id).sha256 == hashlib.sha256(b"kalici").hexdigest() def test_lru_evicts_oldest_unused(): with tempfile.TemporaryDirectory() as tmp: cache = _cache(Path(tmp), max_bytes=25) first = _put(cache, b"a" * 10) cache.get(first.id).last_used = 1 second = _put(cache, b"b" * 10) cache.get(second.id).last_used = 2 cache.acquire(first.id) # kullanımda: silinmemeli cache.get(first.id).last_used = 1 third = _put(cache, b"c" * 10) ids = {e.id for e in cache.list()} assert first.id in ids and third.id in ids and second.id not in ids, ids def test_remove_refuses_active(): with tempfile.TemporaryDirectory() as tmp: cache = _cache(Path(tmp)) entry = _put(cache, b"x") cache.acquire(entry.id) assert cache.remove(entry.id) is False cache.release(entry.id) assert cache.remove(entry.id) is True assert cache.list() == [] def test_derin_denetle_rejects_truncated_zip(): with tempfile.TemporaryDirectory() as tmp: buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: zf.writestr("www/index.html", "x" * 5000) good = Path(tmp) / "good.zip" good.write_bytes(buf.getvalue()) assert zipcheck.derin_denetle(good).ok bad = Path(tmp) / "bad.zip" bad.write_bytes(buf.getvalue()[:-100]) assert zipcheck.derin_denetle(bad).ok is False if __name__ == "__main__": run(globals())