File size: 2,069 Bytes
49b92fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json

from laya_onnx.verify import load_manifest, sha256_file, verify_bundle


def _manifest(tmp_path, payload: bytes):
    file_path = tmp_path / "laya_config.json"
    file_path.write_bytes(payload)
    manifest = {
        "algorithm": "sha256",
        "files": {"laya_config.json": {"size": len(payload), "sha256": sha256_file(file_path)}},
    }
    manifest_path = tmp_path / "manifest.json"
    manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
    return file_path, manifest_path


def test_verify_bundle_ok(tmp_path):
    _, manifest_path = _manifest(tmp_path, b'{"max_len": 64}\n')
    ok, rows = verify_bundle(tmp_path, manifest_path)
    assert ok is True
    assert rows == [{"file": "laya_config.json", "status": "ok", "sha256": rows[0]["sha256"]}]


def test_verify_bundle_detects_tampering(tmp_path):
    file_path, manifest_path = _manifest(tmp_path, b'{"max_len": 64}\n')
    file_path.write_bytes(b'{"max_len": 128}\n')  # same directory, changed bytes
    ok, rows = verify_bundle(tmp_path, manifest_path)
    assert ok is False
    assert rows[0]["status"] == "size-mismatch"


def test_verify_bundle_hash_mismatch(tmp_path):
    file_path, manifest_path = _manifest(tmp_path, b"abc")
    file_path.write_bytes(b"abd")  # same size, different bytes
    ok, rows = verify_bundle(tmp_path, manifest_path)
    assert ok is False
    assert rows[0]["status"] == "sha256-mismatch"


def test_verify_bundle_missing_file(tmp_path):
    _, manifest_path = _manifest(tmp_path, b"x")
    (tmp_path / "laya_config.json").unlink()
    ok, rows = verify_bundle(tmp_path, manifest_path)
    assert ok is False
    assert rows[0]["status"] == "missing"


def test_bundled_manifest_is_wellformed():
    manifest = load_manifest()
    assert manifest["repo"] == "receptron/laya-onnx"
    assert manifest["algorithm"] == "sha256"
    assert "laya.onnx" in manifest["files"]
    for name, entry in manifest["files"].items():
        assert len(entry["sha256"]) == 64 and entry["size"] > 0