| 1 | """Independent reader checks against repositories produced by the real Oak CLI."""
|
| 2 | from contextlib import contextmanager
|
| 3 | from unittest import mock
|
| 4 | import importlib.util
|
| 5 | import json
|
| 6 | import os
|
| 7 | from pathlib import Path
|
| 8 | import shutil
|
| 9 | import sqlite3
|
| 10 | import subprocess
|
| 11 | import sys
|
| 12 | import tempfile
|
| 13 | import unittest
|
| 14 |
|
| 15 | ROOT = Path(__file__).resolve().parents[1]
|
| 16 | READER = ROOT / 'scripts/oakbench/oak_sqlite_reader.py'
|
| 17 |
|
| 18 | @unittest.skipUnless(importlib.util.find_spec('blake3') and importlib.util.find_spec('zstandard') and shutil.which('oak'), 'optional reader dependencies and Oak required')
|
| 19 | class OakSQLiteReaderTest(unittest.TestCase):
|
| 20 | def setUp(self):
|
| 21 | self.tmp = tempfile.TemporaryDirectory()
|
| 22 | self.addCleanup(self.tmp.cleanup)
|
| 23 | self.root = Path(self.tmp.name)
|
| 24 | self.repo = self.root / 'repo'
|
| 25 | self.repo.mkdir()
|
| 26 | env = {**os.environ, 'OAK_NO_UPDATE_CHECK': '1', 'HOME': str(self.root)}
|
| 27 | for command in ([shutil.which('oak'), 'init'],):
|
| 28 | subprocess.run(command, cwd=self.repo, env=env, check=True, capture_output=True)
|
| 29 | (self.repo / 'payload.txt').write_text('committed payload\n')
|
| 30 | subprocess.run([shutil.which('oak'), 'commit', '--no-verify'], cwd=self.repo, env=env, check=True, capture_output=True)
|
| 31 |
|
| 32 | def read(self):
|
| 33 | return subprocess.run([sys.executable, '-I', str(READER), str(self.repo), str(self.root / 'out')], capture_output=True, text=True)
|
| 34 |
|
| 35 | def test_reads_committed_content_without_subject_or_worktree(self):
|
| 36 | (self.repo / 'payload.txt').write_text('uncommitted replacement')
|
| 37 | result = self.read()
|
| 38 | self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
|
| 39 | self.assertEqual((self.root / 'out/payload.txt').read_text(), 'committed payload\n')
|
| 40 | self.assertEqual(json.loads(result.stdout)['reader_version'], 'oak-sqlite-v1-2026-09-18.1')
|
| 41 |
|
| 42 | def test_corrupt_blob_is_not_verified(self):
|
| 43 | with sqlite3.connect(str(self.repo / '.oak/oak.db')) as db:
|
| 44 | db.execute('PRAGMA journal_mode=DELETE')
|
| 45 | db.execute('UPDATE blobs SET content=?, size=?, codec=0', (b'corrupt', 7))
|
| 46 | db.close()
|
| 47 | result = self.read()
|
| 48 | self.assertEqual(result.returncode, 1, result.stderr + result.stdout)
|
| 49 | self.assertIn('hash', result.stdout)
|
| 50 |
|
| 51 | def test_unsupported_format_is_unmeasured(self):
|
| 52 | with sqlite3.connect(str(self.repo / '.oak/oak.db')) as db:
|
| 53 | db.execute('PRAGMA journal_mode=DELETE')
|
| 54 | db.execute("INSERT OR REPLACE INTO metadata(key,value) VALUES ('hash_format','future')")
|
| 55 | db.close()
|
| 56 | result = self.read()
|
| 57 | self.assertEqual(result.returncode, 77, result.stderr + result.stdout)
|
| 58 | self.assertIn('unsupported', result.stdout)
|
| 59 |
|
| 60 | @contextmanager
|
| 61 | def database(self):
|
| 62 | db = sqlite3.connect(str(self.repo / '.oak/oak.db'))
|
| 63 | try:
|
| 64 | db.execute('PRAGMA journal_mode=DELETE')
|
| 65 | yield db
|
| 66 | db.commit()
|
| 67 | finally:
|
| 68 | db.close()
|
| 69 |
|
| 70 | def install_tree(self, entries, *, normalized=False):
|
| 71 | """Author a hash-valid adversarial tree without Oak serializing it."""
|
| 72 | import blake3
|
| 73 | import zstandard
|
| 74 | raw = '\n'.join('\t'.join(entry) for entry in entries).encode()
|
| 75 | tree_hash = blake3.blake3(raw).hexdigest()
|
| 76 | with self.database() as db:
|
| 77 | db.execute('INSERT OR REPLACE INTO trees(hash,content) VALUES (?,?)',
|
| 78 | (tree_hash, None if normalized else zstandard.ZstdCompressor().compress(raw)))
|
| 79 | if normalized:
|
| 80 | for kind, name, object_hash, mode in entries:
|
| 81 | db.execute('INSERT INTO tree_entries(tree_hash,name,kind,hash,mode) VALUES (?,?,?,?,?)',
|
| 82 | (tree_hash, name, kind, object_hash, mode))
|
| 83 | old_head = db.execute("SELECT value FROM metadata WHERE key='head'").fetchone()[0]
|
| 84 | commit = list(db.execute('SELECT branch_name,parent_hash,merge_parent_hash,manifest_hash,author,message,timestamp FROM commits WHERE hash=?', (old_head,)).fetchone())
|
| 85 | commit[3] = tree_hash
|
| 86 | head = blake3.blake3('\n'.join(value or '' for value in commit).encode()).hexdigest()
|
| 87 | db.execute('UPDATE commits SET hash=?,manifest_hash=? WHERE hash=?', (head, tree_hash, old_head))
|
| 88 | db.execute("UPDATE metadata SET value=? WHERE key='head'", (head,))
|
| 89 | return tree_hash
|
| 90 |
|
| 91 | def blob_hash(self):
|
| 92 | with self.database() as db:
|
| 93 | return db.execute('SELECT hash FROM blobs WHERE size=?', (len(b'committed payload\n'),)).fetchone()[0]
|
| 94 |
|
| 95 | def assert_corrupt(self, result, reason):
|
| 96 | self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
|
| 97 | payload = json.loads(result.stdout)
|
| 98 | self.assertIs(payload['corrupt'], True)
|
| 99 | self.assertIn(reason, payload['reason'])
|
| 100 |
|
| 101 | def test_corrupt_commit_hash_is_not_verified(self):
|
| 102 | with self.database() as db:
|
| 103 | db.execute("UPDATE commits SET message='tampered'")
|
| 104 | self.assert_corrupt(self.read(), 'hash')
|
| 105 |
|
| 106 | def test_corrupt_tree_hash_is_not_verified(self):
|
| 107 | import zstandard
|
| 108 | with self.database() as db:
|
| 109 | db.execute('UPDATE trees SET content=?', (zstandard.ZstdCompressor().compress(b'not original tree'),))
|
| 110 | self.assert_corrupt(self.read(), 'hash')
|
| 111 |
|
| 112 | def test_hash_valid_traversal_is_rejected_without_external_write(self):
|
| 113 | self.install_tree([('blob', '../escaped', self.blob_hash(), 'regular')])
|
| 114 | self.assert_corrupt(self.read(), 'unsafe')
|
| 115 | self.assertFalse((self.root / 'escaped').exists())
|
| 116 |
|
| 117 | def test_case_colliding_paths_cannot_overwrite(self):
|
| 118 | probe = self.root / 'casecheck'
|
| 119 | probe.write_text('case')
|
| 120 | if not (self.root / 'CASECHECK').exists():
|
| 121 | self.skipTest('filesystem distinguishes case; no alias to test')
|
| 122 | h = self.blob_hash()
|
| 123 | self.install_tree([('blob', 'A', h, 'regular'), ('blob', 'a', h, 'regular')])
|
| 124 | result = self.read()
|
| 125 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 126 | self.assertIn('collision', result.stdout)
|
| 127 |
|
| 128 | def test_symlink_mode_is_explicitly_unavailable(self):
|
| 129 | self.install_tree([('blob', 'link', self.blob_hash(), 'symlink')])
|
| 130 | result = self.read()
|
| 131 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 132 | self.assertIn('unsupported tree entry', result.stdout)
|
| 133 | self.assertFalse((self.root / 'out/link').exists())
|
| 134 |
|
| 135 | def test_chunk_only_or_missing_complete_blob_is_unavailable(self):
|
| 136 | with self.database() as db:
|
| 137 | db.execute('DELETE FROM blobs')
|
| 138 | result = self.read()
|
| 139 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 140 | self.assertIn('chunk-only', result.stdout)
|
| 141 |
|
| 142 | def test_active_wal_is_not_silently_ignored(self):
|
| 143 | db = sqlite3.connect(str(self.repo / '.oak/oak.db'))
|
| 144 | try:
|
| 145 | db.execute('PRAGMA journal_mode=WAL')
|
| 146 | db.execute("INSERT OR REPLACE INTO metadata(key,value) VALUES ('pending-reader-test','1')")
|
| 147 | db.commit()
|
| 148 | self.assertTrue((self.repo / '.oak/oak.db-wal').exists())
|
| 149 | result = self.read()
|
| 150 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 151 | self.assertIn('active SQLite journal', result.stdout)
|
| 152 | finally:
|
| 153 | db.close()
|
| 154 |
|
| 155 | def test_declared_blob_size_over_bound_is_rejected(self):
|
| 156 | with self.database() as db:
|
| 157 | db.execute('UPDATE blobs SET size=?', (32 * 1024 * 1024 + 1,))
|
| 158 | self.assert_corrupt(self.read(), 'size exceeds')
|
| 159 |
|
| 160 | def test_declared_zstd_window_over_bound_is_rejected_without_allocation(self):
|
| 161 | # A tiny frame header declaring a >32MiB single-segment window.
|
| 162 | frame = b'\x28\xb5\x2f\xfd\xa0' + (32 * 1024 * 1024 + 1).to_bytes(4, 'little') + b'\x01\x00\x00'
|
| 163 | with self.database() as db:
|
| 164 | db.execute('UPDATE blobs SET content=?,codec=1', (frame,))
|
| 165 | self.assert_corrupt(self.read(), 'window exceeds')
|
| 166 |
|
| 167 | def test_normalized_legacy_tree_roundtrip(self):
|
| 168 | self.install_tree([('blob', 'legacy', self.blob_hash(), 'regular')], normalized=True)
|
| 169 | result = self.read()
|
| 170 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 171 | self.assertEqual((self.root / 'out/legacy').read_bytes(), b'committed payload\n')
|
| 172 |
|
| 173 | def test_legacy_entry_field_bound_precedes_materialization(self):
|
| 174 | self.install_tree([('blob', 'x' * 4097, self.blob_hash(), 'regular')], normalized=True)
|
| 175 | self.assert_corrupt(self.read(), 'entry exceeds')
|
| 176 |
|
| 177 | def test_executable_mode_is_preserved_for_downstream_validation(self):
|
| 178 | self.install_tree([('blob', 'executable', self.blob_hash(), 'executable')])
|
| 179 | result = self.read()
|
| 180 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 181 | self.assertTrue((self.root / 'out/executable').stat().st_mode & 0o111)
|
| 182 |
|
| 183 | def test_source_database_change_after_snapshot_is_unavailable(self):
|
| 184 | spec = importlib.util.spec_from_file_location('independent_reader_control', READER)
|
| 185 | reader = importlib.util.module_from_spec(spec)
|
| 186 | spec.loader.exec_module(reader)
|
| 187 | original_write = Path.write_bytes
|
| 188 | def write_then_change_database(path, content):
|
| 189 | result = original_write(path, content)
|
| 190 | if path.name == 'snapshot.db':
|
| 191 | with self.database() as db:
|
| 192 | db.execute("INSERT OR REPLACE INTO metadata(key,value) VALUES ('changed-after-snapshot','1')")
|
| 193 | return result
|
| 194 | with mock.patch.object(Path, 'write_bytes', write_then_change_database):
|
| 195 | with self.assertRaisesRegex(reader.Unavailable, 'database changed'):
|
| 196 | reader.read_committed_tree(self.repo, self.root / 'out')
|
| 197 |
|
| 198 | def test_unknown_blob_codec_is_unavailable(self):
|
| 199 | with self.database() as db:
|
| 200 | db.execute('UPDATE blobs SET codec=99')
|
| 201 | result = self.read()
|
| 202 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 203 | self.assertIn('unsupported object codec', result.stdout)
|
| 204 |
|
| 205 | def test_corrupt_reader_envelope_reaches_workflow_failure(self):
|
| 206 | sys.path.insert(0, str(ROOT / 'scripts'))
|
| 207 | from oakbench.workflow_oracles import independent_oak_evidence, VERSION
|
| 208 | with self.database() as db:
|
| 209 | db.execute('UPDATE blobs SET content=?,size=?,codec=0', (b'corrupt', 7))
|
| 210 | result = independent_oak_evidence('bugfix_test_loop', self.repo, sys.executable,
|
| 211 | {'oracle_version': VERSION, 'workflow_integrity_source': 'trusted_task_worktree',
|
| 212 | 'workflow_integrity_passed': None, 'workflow_integrity_unmeasured_reason': None})
|
| 213 | self.assertIs(result['workflow_integrity_passed'], False, result)
|
| 214 | self.assertIn('oak_stored_content_invalid', result['workflow_integrity_failure_reason'])
|
| 215 | self.assertIsNone(result['workflow_integrity_unmeasured_reason'])
|