File size: 3,013 Bytes
f20b6e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""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())