-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_conduit_proof.py
More file actions
408 lines (315 loc) · 15.1 KB
/
test_conduit_proof.py
File metadata and controls
408 lines (315 loc) · 15.1 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""
tests/test_conduit_proof.py — Tests for ConduitProof (session proof bundle export).
Verifies:
- Bundle is created as a valid .tar.gz archive
- verify.py is embedded inside the bundle
- audit_log.jsonl is inside the bundle
- manifest.json contains correct metadata
- chain_hash matches the computed hash over row_hashes
- Empty session returns failure dict
"""
import hashlib
import json
import sys
import tarfile
import tempfile
import time
import types
import unittest
from pathlib import Path
# ---------------------------------------------------------------------------
# Import conduit_proof standalone
# ---------------------------------------------------------------------------
_PROOF_PATH = Path(__file__).parent.parent / "tools" / "conduit_proof.py"
_proof_src = _PROOF_PATH.read_text(encoding="utf-8")
_proof_mod = types.ModuleType("conduit_proof_standalone")
_proof_mod.__file__ = str(_PROOF_PATH)
exec(compile(_proof_src, str(_PROOF_PATH), "exec"), _proof_mod.__dict__)
ConduitProof = _proof_mod.ConduitProof
VERIFY_PY = _proof_mod.VERIFY_PY
# ---------------------------------------------------------------------------
# Import audit.py standalone for integration tests
# ---------------------------------------------------------------------------
_AUDIT_PATH = Path(__file__).parent.parent / "audit.py"
_audit_src = _AUDIT_PATH.read_text(encoding="utf-8")
# Patch the relative import
_audit_src_patched = _audit_src.replace(
"from .platform import get_data_dir",
"def get_data_dir(): return Path.home() / '.cato_test'"
)
_audit_mod = types.ModuleType("audit_standalone")
_audit_mod.__file__ = str(_AUDIT_PATH)
exec(compile(_audit_src_patched, str(_AUDIT_PATH), "exec"), _audit_mod.__dict__)
AuditLog = _audit_mod.AuditLog
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
class MockAuditLog:
"""In-memory mock that supports get_session_rows()."""
def __init__(self, rows=None):
self._rows = rows or []
def get_session_rows(self, session_id):
return [r for r in self._rows if r.get("session_id") == session_id]
def make_fake_rows(session_id="sess-001", count=3):
"""Create fake audit rows with valid row_hash structure."""
rows = []
prev_hash = ""
for i in range(1, count + 1):
ts = time.time() + i
rh = hashlib.sha256(
f"{i}:{session_id}:tool_call:browser.navigate:0:{ts}:{prev_hash}".encode()
).hexdigest()
row = {
"id": i,
"session_id": session_id,
"action_type": "tool_call",
"tool_name": "browser.navigate",
"inputs_json": '{"url": "https://example.com"}',
"outputs_json": '{"title": "Example"}',
"cost_cents": 0,
"error": "",
"timestamp": ts,
"prev_hash": prev_hash,
"row_hash": rh,
}
rows.append(row)
prev_hash = rh
return rows
# ---------------------------------------------------------------------------
# Tests: ConduitProof.export()
# ---------------------------------------------------------------------------
class TestConduitProofExport(unittest.TestCase):
def test_export_returns_success_dict(self):
rows = make_fake_rows("sess-001", count=2)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-001")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
self.assertTrue(result["success"])
self.assertIn("path", result)
self.assertIn("action_count", result)
self.assertIn("chain_hash", result)
self.assertIn("bundle_name", result)
def test_export_creates_tar_gz_file(self):
rows = make_fake_rows("sess-002", count=3)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-002")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
bundle_path = Path(result["path"])
# Check existence INSIDE the context manager while the temp dir still exists
self.assertTrue(bundle_path.exists())
self.assertTrue(bundle_path.name.endswith(".tar.gz"))
def test_export_bundle_is_valid_tarball(self):
rows = make_fake_rows("sess-003", count=2)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-003")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
bundle_path = result["path"]
# Must be openable as a gzip tarball
with tarfile.open(bundle_path, "r:gz") as tar:
members = tar.getnames()
self.assertGreater(len(members), 0)
def test_export_bundle_contains_verify_py(self):
rows = make_fake_rows("sess-004", count=1)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-004")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
member_names = tar.getnames()
self.assertTrue(any("verify.py" in n for n in member_names))
def test_export_bundle_contains_audit_log_jsonl(self):
rows = make_fake_rows("sess-005", count=2)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-005")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
member_names = tar.getnames()
self.assertTrue(any("audit_log.jsonl" in n for n in member_names))
def test_export_bundle_contains_manifest_json(self):
rows = make_fake_rows("sess-006", count=1)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-006")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
member_names = tar.getnames()
self.assertTrue(any("manifest.json" in n for n in member_names))
def test_export_action_count_matches_rows(self):
rows = make_fake_rows("sess-007", count=5)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-007")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
self.assertEqual(result["action_count"], 5)
def test_export_chain_hash_computed_from_row_hashes(self):
rows = make_fake_rows("sess-008", count=3)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-008")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
# Manually compute expected chain hash
combined = "".join(r["row_hash"] for r in rows)
expected_hash = hashlib.sha256(combined.encode()).hexdigest()
self.assertEqual(result["chain_hash"], expected_hash)
def test_export_manifest_contains_session_id(self):
rows = make_fake_rows("sess-009", count=2)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-009")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
manifest_member = next(m for m in tar.getmembers() if "manifest.json" in m.name)
manifest_data = json.loads(tar.extractfile(manifest_member).read().decode())
self.assertEqual(manifest_data["session_id"], "sess-009")
self.assertIn("exported_at", manifest_data)
self.assertIn("action_count", manifest_data)
self.assertIn("chain_hash", manifest_data)
def test_export_verify_py_content_is_correct(self):
rows = make_fake_rows("sess-010", count=1)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "sess-010")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
verify_member = next(m for m in tar.getmembers() if "verify.py" in m.name)
verify_content = tar.extractfile(verify_member).read().decode()
self.assertIn("def verify()", verify_content)
self.assertIn("hashlib", verify_content)
self.assertIn("VERIFIED", verify_content)
self.assertIn("audit_log.jsonl", verify_content)
def test_export_returns_failure_for_empty_session(self):
audit = MockAuditLog(rows=[])
proof = ConduitProof(audit, "empty-session")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
self.assertFalse(result["success"])
self.assertIn("error", result)
def test_export_with_public_key_pem(self):
rows = make_fake_rows("sess-011", count=1)
audit = MockAuditLog(rows=rows)
pem = "# Ed25519 public key: abcdef1234567890\n"
proof = ConduitProof(audit, "sess-011", public_key_pem=pem)
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
pk_member = next(m for m in tar.getmembers() if "public_key.pem" in m.name)
pk_content = tar.extractfile(pk_member).read().decode()
self.assertIn("abcdef1234567890", pk_content)
def test_compute_chain_hash_for_empty_rows_returns_hash_of_empty(self):
audit = MockAuditLog(rows=[])
proof = ConduitProof(audit, "sess-x")
result = proof._compute_chain_hash([])
expected = hashlib.sha256(b"empty").hexdigest()
self.assertEqual(result, expected)
def test_bundle_filename_contains_session_prefix_and_timestamp(self):
rows = make_fake_rows("abcdefgh-xyz", count=1)
audit = MockAuditLog(rows=rows)
proof = ConduitProof(audit, "abcdefgh-xyz")
with tempfile.TemporaryDirectory() as tmpdir:
result = proof.export(output_dir=tmpdir)
self.assertIn("abcdefg", result["bundle_name"]) # first 8 chars of session_id
self.assertTrue(result["bundle_name"].endswith(".tar.gz"))
# ---------------------------------------------------------------------------
# Tests: Integration with real AuditLog
# ---------------------------------------------------------------------------
class TestConduitProofWithRealAuditLog(unittest.TestCase):
"""Integration tests using the actual AuditLog (in-memory SQLite)."""
def _make_audit_log(self, tmpdir):
db_path = Path(tmpdir) / "test_audit.db"
log = AuditLog(db_path=db_path)
log.connect()
return log
def test_export_proof_with_real_audit_log(self):
with tempfile.TemporaryDirectory() as tmpdir:
audit = self._make_audit_log(tmpdir)
session_id = "integration-sess-001"
# Write some rows
audit.log(
session_id=session_id,
action_type="tool_call",
tool_name="browser.navigate",
inputs={"url": "https://example.com"},
outputs={"title": "Example"},
cost_cents=0,
)
audit.log(
session_id=session_id,
action_type="tool_call",
tool_name="browser.eval",
inputs={"js_code": "document.title", "code_hash": "abc123"},
outputs={"result": "Example", "success": True},
cost_cents=0,
)
proof = ConduitProof(audit, session_id)
result = proof.export(output_dir=tmpdir)
self.assertTrue(result["success"])
self.assertEqual(result["action_count"], 2)
# Verify bundle structure
with tarfile.open(result["path"], "r:gz") as tar:
names = tar.getnames()
self.assertTrue(any("verify.py" in n for n in names))
self.assertTrue(any("audit_log.jsonl" in n for n in names))
self.assertTrue(any("manifest.json" in n for n in names))
audit.close()
def test_audit_jsonl_in_bundle_has_valid_rows(self):
with tempfile.TemporaryDirectory() as tmpdir:
audit = self._make_audit_log(tmpdir)
session_id = "integration-sess-002"
audit.log(
session_id=session_id,
action_type="tool_call",
tool_name="browser.navigate",
inputs={"url": "https://test.com"},
outputs={"title": "Test"},
cost_cents=0,
)
proof = ConduitProof(audit, session_id)
result = proof.export(output_dir=tmpdir)
with tarfile.open(result["path"], "r:gz") as tar:
jsonl_member = next(m for m in tar.getmembers() if "audit_log.jsonl" in m.name)
jsonl_content = tar.extractfile(jsonl_member).read().decode()
rows = [json.loads(line) for line in jsonl_content.splitlines() if line.strip()]
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["session_id"], session_id)
self.assertIn("row_hash", rows[0])
audit.close()
# ---------------------------------------------------------------------------
# Tests: VERIFY_PY constant
# ---------------------------------------------------------------------------
class TestVerifyPyContent(unittest.TestCase):
def test_verify_py_is_valid_python(self):
"""The embedded verify.py must compile without syntax errors."""
try:
compile(VERIFY_PY, "<verify.py>", "exec")
except SyntaxError as e:
self.fail(f"VERIFY_PY has syntax error: {e}")
def test_verify_py_contains_hash_chain_verification(self):
self.assertIn("hashlib.sha256", VERIFY_PY)
self.assertIn("row_hash", VERIFY_PY)
self.assertIn("prev_hash", VERIFY_PY)
def test_verify_py_has_main_guard(self):
self.assertIn('if __name__ == "__main__"', VERIFY_PY)
self.assertIn("verify()", VERIFY_PY)
def test_verify_py_uses_only_stdlib(self):
"""No third-party imports — just stdlib."""
import ast
tree = ast.parse(VERIFY_PY)
imports = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
imports.add(alias.name.split(".")[0])
elif isinstance(node, ast.ImportFrom):
if node.module:
imports.add(node.module.split(".")[0])
# cryptography is optionally imported inside try/except for Ed25519
# signature verification — verify.py still works without it (stdlib only)
stdlib_and_optional = {"json", "hashlib", "base64", "sys", "pathlib", "cryptography"}
non_stdlib = imports - stdlib_and_optional
self.assertEqual(non_stdlib, set(), f"Non-stdlib imports in verify.py: {non_stdlib}")
if __name__ == "__main__":
unittest.main()