File size: 7,193 Bytes
901a5f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from __future__ import annotations

import csv
import hashlib
import json
from pathlib import Path

import pytest
from verify_archive import verify_archive


def _write_archive(root: Path) -> None:
    for relative, content in {
        "images/example.png": b"image",
        "results/metrics.csv": b"condition,success\npng,1\n",
        "samples/example.png": b"sample",
        "weights/model.png": b"weights",
    }.items():
        path = root / relative
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_bytes(content)
    rows = []
    for relative in ("weights/model.png",):
        path = root / relative
        rows.append(
            {
                "path": relative,
                "bytes": str(path.stat().st_size),
                "sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
            }
        )
    with (root / "weights/manifest.csv").open("w", newline="", encoding="utf-8") as handle:
        writer = csv.DictWriter(handle, fieldnames=["path", "bytes", "sha256"])
        writer.writeheader()
        writer.writerows(rows)
    manifest = {
        "files": [
            {
                "path": relative,
                "bytes": (root / relative).stat().st_size,
                "sha256": hashlib.sha256((root / relative).read_bytes()).hexdigest(),
            }
            for relative in (
                "images/example.png",
                "results/metrics.csv",
                "samples/example.png",
                "weights/manifest.csv",
            )
        ]
    }
    (root / "results/archive_manifest.json").write_text(json.dumps(manifest), encoding="utf-8")


def _refresh_manifest(root: Path) -> None:
    manifest_path = root / "results/archive_manifest.json"
    manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    manifest.pop("files", None)
    manifest["artifacts"] = []
    for path in sorted(p for directory in ("images", "results", "samples", "weights")
                       for p in (root / directory).rglob("*") if p.is_file() and p.name != "archive_manifest.json"):
        relative = path.relative_to(root).as_posix()
        manifest["artifacts"].append({"path": relative, "bytes": path.stat().st_size,
                                      "sha256": hashlib.sha256(path.read_bytes()).hexdigest()})
    manifest.update({"profile": "test", "prompt_count": 1, "conditions": ["png_baseline", "webp_lossless"]})
    manifest_path.write_text(json.dumps(manifest), encoding="utf-8")


def _add_generation_rows(root: Path) -> None:
    sample = root / "samples/example.png"
    rows = [
        {"row_type": "generation", "condition": "png_baseline", "prompt_id": "p1", "success": True,
         "sample_path": "samples/example.png", "sample_bytes": sample.stat().st_size,
         "sample_hash": hashlib.sha256(sample.read_bytes()).hexdigest(), "sample_sha256": None},
        {"row_type": "generation", "condition": "webp_lossless", "prompt_id": "p1", "success": False,
         "sample_path": None, "sample_bytes": None, "sample_hash": None, "sample_sha256": None},
    ]
    (root / "results/raw_rows.jsonl").write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")
    _refresh_manifest(root)


def test_verify_archive_accepts_hash_and_containment_valid_archive(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)

    report = verify_archive(tmp_path)

    assert report["checked_files"] == 7
    assert report["checked_weight_entries"] == 1
    assert report["generation_rows"] == 2
    assert report["successful_samples"] == 1


def test_verify_archive_rejects_path_escape(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    manifest_path = tmp_path / "results/archive_manifest.json"
    manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    manifest["artifacts"].append({"path": "../outside", "bytes": 0, "sha256": ""})
    manifest_path.write_text(json.dumps(manifest), encoding="utf-8")

    with pytest.raises(ValueError, match="containment"):
        verify_archive(tmp_path)


@pytest.mark.parametrize("mutation, message", [
    ("missing", "sample"),
    ("hash", "SHA-256"),
    ("bytes", "byte count"),
])
def test_verify_archive_rejects_invalid_success_provenance(tmp_path: Path, mutation: str, message: str) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    raw = tmp_path / "results/raw_rows.jsonl"
    row = json.loads(raw.read_text(encoding="utf-8").splitlines()[0])
    if mutation == "missing":
        row["sample_path"] = None
    elif mutation == "hash":
        row["sample_hash"] = "0" * 64
    else:
        row["sample_bytes"] += 1
    raw.write_text(json.dumps(row) + "\n" + raw.read_text(encoding="utf-8").splitlines()[1] + "\n", encoding="utf-8")
    _refresh_manifest(tmp_path)

    with pytest.raises(ValueError, match=message):
        verify_archive(tmp_path)


def test_verify_archive_rejects_failed_row_with_sample(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    raw = tmp_path / "results/raw_rows.jsonl"
    rows = [json.loads(line) for line in raw.read_text(encoding="utf-8").splitlines()]
    rows[1]["sample_path"] = "samples/example.png"
    raw.write_text("\n".join(json.dumps(row) for row in rows) + "\n", encoding="utf-8")
    _refresh_manifest(tmp_path)

    with pytest.raises(ValueError, match="failed row"):
        verify_archive(tmp_path)


def test_verify_archive_rejects_raw_rows_symlink(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    raw = tmp_path / "results/raw_rows.jsonl"
    raw.unlink()
    raw.symlink_to(tmp_path / "outside.jsonl")
    (tmp_path / "outside.jsonl").write_text("", encoding="utf-8")

    with pytest.raises(ValueError, match="symlink"):
        verify_archive(tmp_path)


def test_verify_archive_rejects_duplicate_manifest_path(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    manifest_path = tmp_path / "results/archive_manifest.json"
    manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    manifest["artifacts"].append(manifest["artifacts"][0])
    manifest_path.write_text(json.dumps(manifest), encoding="utf-8")

    with pytest.raises(ValueError, match="duplicate archive path"):
        verify_archive(tmp_path)


def test_verify_archive_rejects_listed_hash_tamper(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    path = tmp_path / "samples/example.png"
    path.write_bytes(b"tampeR")

    with pytest.raises(ValueError, match="SHA-256"):
        verify_archive(tmp_path)


def test_verify_archive_rejects_archived_tree_symlink_and_unlisted_file(tmp_path: Path) -> None:
    _write_archive(tmp_path)
    _add_generation_rows(tmp_path)
    extra = tmp_path / "samples/unlisted.bin"
    extra.write_bytes(b"extra")
    with pytest.raises(ValueError, match="unlisted"):
        verify_archive(tmp_path)
    extra.unlink()
    extra.symlink_to(tmp_path / "weights/model.png")

    with pytest.raises(ValueError, match="symlink"):
        verify_archive(tmp_path)