| 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, *scope):
|
| 33 | extra = ['--scope', *scope] if scope else []
|
| 34 | return subprocess.run([sys.executable, '-I', str(READER), str(self.repo), str(self.root / 'out'), *extra], capture_output=True, text=True)
|
| 35 |
|
| 36 | def assert_unavailable(self, result, reason):
|
| 37 | self.assertEqual(result.returncode, 77, result.stdout + result.stderr)
|
| 38 | self.assertIn(reason, json.loads(result.stdout)['unavailable'])
|
| 39 |
|
| 40 | def test_reads_committed_content_without_subject_or_worktree(self):
|
| 41 | (self.repo / 'payload.txt').write_text('uncommitted replacement')
|
| 42 | result = self.read()
|
| 43 | self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
|
| 44 | self.assertEqual((self.root / 'out/payload.txt').read_text(), 'committed payload\n')
|
| 45 | self.assertEqual(json.loads(result.stdout)['reader_version'], 'oak-sqlite-v1-2026-09-27.1')
|
| 46 |
|
| 47 | def test_corrupt_blob_is_not_verified(self):
|
| 48 | with sqlite3.connect(str(self.repo / '.oak/oak.db')) as db:
|
| 49 | db.execute('PRAGMA journal_mode=DELETE')
|
| 50 | db.execute('UPDATE blobs SET content=?, size=?, codec=0', (b'corrupt', 7))
|
| 51 | db.close()
|
| 52 | result = self.read()
|
| 53 | self.assertEqual(result.returncode, 1, result.stderr + result.stdout)
|
| 54 | self.assertIn('hash', result.stdout)
|
| 55 |
|
| 56 | def test_unsupported_format_is_unmeasured(self):
|
| 57 | with sqlite3.connect(str(self.repo / '.oak/oak.db')) as db:
|
| 58 | db.execute('PRAGMA journal_mode=DELETE')
|
| 59 | db.execute("INSERT OR REPLACE INTO metadata(key,value) VALUES ('hash_format','future')")
|
| 60 | db.close()
|
| 61 | result = self.read()
|
| 62 | self.assertEqual(result.returncode, 77, result.stderr + result.stdout)
|
| 63 | self.assertIn('unsupported', result.stdout)
|
| 64 |
|
| 65 | @contextmanager
|
| 66 | def database(self):
|
| 67 | db = sqlite3.connect(str(self.repo / '.oak/oak.db'))
|
| 68 | try:
|
| 69 | db.execute('PRAGMA journal_mode=DELETE')
|
| 70 | yield db
|
| 71 | db.commit()
|
| 72 | finally:
|
| 73 | db.close()
|
| 74 |
|
| 75 | def install_tree(self, entries, *, normalized=False):
|
| 76 | """Author a hash-valid adversarial tree without Oak serializing it."""
|
| 77 | import blake3
|
| 78 | import zstandard
|
| 79 | raw = '\n'.join('\t'.join(entry) for entry in entries).encode()
|
| 80 | tree_hash = blake3.blake3(raw).hexdigest()
|
| 81 | with self.database() as db:
|
| 82 | db.execute('INSERT OR REPLACE INTO trees(hash,content) VALUES (?,?)',
|
| 83 | (tree_hash, None if normalized else zstandard.ZstdCompressor().compress(raw)))
|
| 84 | if normalized:
|
| 85 | for kind, name, object_hash, mode in entries:
|
| 86 | db.execute('INSERT INTO tree_entries(tree_hash,name,kind,hash,mode) VALUES (?,?,?,?,?)',
|
| 87 | (tree_hash, name, kind, object_hash, mode))
|
| 88 | old_head = db.execute("SELECT value FROM metadata WHERE key='head'").fetchone()[0]
|
| 89 | commit = list(db.execute('SELECT branch_name,parent_hash,merge_parent_hash,manifest_hash,author,message,timestamp FROM commits WHERE hash=?', (old_head,)).fetchone())
|
| 90 | commit[3] = tree_hash
|
| 91 | head = blake3.blake3('\n'.join(value or '' for value in commit).encode()).hexdigest()
|
| 92 | db.execute('UPDATE commits SET hash=?,manifest_hash=? WHERE hash=?', (head, tree_hash, old_head))
|
| 93 | db.execute("UPDATE metadata SET value=? WHERE key='head'", (head,))
|
| 94 | return tree_hash
|
| 95 |
|
| 96 | def blob_hash(self):
|
| 97 | with self.database() as db:
|
| 98 | return db.execute('SELECT hash FROM blobs WHERE size=?', (len(b'committed payload\n'),)).fetchone()[0]
|
| 99 |
|
| 100 | def assert_corrupt(self, result, reason):
|
| 101 | self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
|
| 102 | payload = json.loads(result.stdout)
|
| 103 | self.assertIs(payload['corrupt'], True)
|
| 104 | self.assertIn(reason, payload['reason'])
|
| 105 |
|
| 106 | def test_commit_preimage_mismatch_fails_closed_as_unrecognized_format(self):
|
| 107 | # The HEAD preimage is the format pin: a mismatch may be a future Oak
|
| 108 | # serialization, so it is never published as an Oak correctness failure.
|
| 109 | with self.database() as db:
|
| 110 | db.execute("UPDATE commits SET message='tampered'")
|
| 111 | self.assert_unavailable(self.read(), 'commit preimage format unrecognized')
|
| 112 |
|
| 113 | def test_root_tree_mismatch_fails_closed_as_unrecognized_format(self):
|
| 114 | import zstandard
|
| 115 | with self.database() as db:
|
| 116 | db.execute('UPDATE trees SET content=?', (zstandard.ZstdCompressor().compress(b'not original tree'),))
|
| 117 | self.assert_unavailable(self.read(), 'root tree serialization unrecognized')
|
| 118 |
|
| 119 | def test_corrupt_nested_tree_after_verified_root_is_invalid(self):
|
| 120 | import blake3
|
| 121 | import zstandard
|
| 122 | bogus = blake3.blake3(b'expected subtree').hexdigest()
|
| 123 | with self.database() as db:
|
| 124 | db.execute('INSERT INTO trees(hash,content) VALUES (?,?)',
|
| 125 | (bogus, zstandard.ZstdCompressor().compress(b'different subtree bytes')))
|
| 126 | self.install_tree([('tree', 'dir', bogus, 'tree')])
|
| 127 | self.assert_corrupt(self.read(), 'hash')
|
| 128 |
|
| 129 | def test_unrecognized_schema_is_unavailable(self):
|
| 130 | with self.database() as db:
|
| 131 | db.execute('ALTER TABLE blobs RENAME COLUMN codec TO codec_v2')
|
| 132 | self.assert_unavailable(self.read(), 'unrecognized Oak storage schema')
|
| 133 |
|
| 134 | def test_hash_valid_traversal_is_rejected_without_external_write(self):
|
| 135 | self.install_tree([('blob', '../escaped', self.blob_hash(), 'regular')])
|
| 136 | self.assert_corrupt(self.read(), 'unsafe')
|
| 137 | self.assertFalse((self.root / 'escaped').exists())
|
| 138 |
|
| 139 | def test_case_colliding_paths_cannot_overwrite(self):
|
| 140 | probe = self.root / 'casecheck'
|
| 141 | probe.write_text('case')
|
| 142 | if not (self.root / 'CASECHECK').exists():
|
| 143 | self.skipTest('filesystem distinguishes case; no alias to test')
|
| 144 | h = self.blob_hash()
|
| 145 | self.install_tree([('blob', 'A', h, 'regular'), ('blob', 'a', h, 'regular')])
|
| 146 | result = self.read()
|
| 147 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 148 | self.assertIn('collision', result.stdout)
|
| 149 |
|
| 150 | def test_symlink_mode_is_explicitly_unavailable(self):
|
| 151 | self.install_tree([('blob', 'link', self.blob_hash(), 'symlink')])
|
| 152 | result = self.read()
|
| 153 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 154 | self.assertIn('unsupported tree entry', result.stdout)
|
| 155 | self.assertFalse((self.root / 'out/link').exists())
|
| 156 |
|
| 157 | def test_chunk_only_or_missing_complete_blob_is_unavailable(self):
|
| 158 | with self.database() as db:
|
| 159 | db.execute('DELETE FROM blobs')
|
| 160 | result = self.read()
|
| 161 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 162 | self.assertIn('chunk-only', result.stdout)
|
| 163 |
|
| 164 | def test_active_wal_is_not_silently_ignored(self):
|
| 165 | db = sqlite3.connect(str(self.repo / '.oak/oak.db'))
|
| 166 | try:
|
| 167 | db.execute('PRAGMA journal_mode=WAL')
|
| 168 | db.execute("INSERT OR REPLACE INTO metadata(key,value) VALUES ('pending-reader-test','1')")
|
| 169 | db.commit()
|
| 170 | self.assertTrue((self.repo / '.oak/oak.db-wal').exists())
|
| 171 | result = self.read()
|
| 172 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 173 | self.assertIn('active SQLite journal', result.stdout)
|
| 174 | finally:
|
| 175 | db.close()
|
| 176 |
|
| 177 | # Reader budgets are harness limits, not facts about Oak's content:
|
| 178 | # exceeding one is unmeasured (77), never a corrupt (1) Oak failure.
|
| 179 | def test_declared_blob_size_over_bound_is_unmeasured(self):
|
| 180 | with self.database() as db:
|
| 181 | db.execute('UPDATE blobs SET size=?', (32 * 1024 * 1024 + 1,))
|
| 182 | self.assert_unavailable(self.read(), 'size exceeds')
|
| 183 |
|
| 184 | def test_declared_zstd_window_over_bound_is_unmeasured_without_allocation(self):
|
| 185 | # A tiny frame header declaring a >32MiB single-segment window.
|
| 186 | frame = b'\x28\xb5\x2f\xfd\xa0' + (32 * 1024 * 1024 + 1).to_bytes(4, 'little') + b'\x01\x00\x00'
|
| 187 | with self.database() as db:
|
| 188 | db.execute('UPDATE blobs SET content=?,codec=1', (frame,))
|
| 189 | self.assert_unavailable(self.read(), 'window exceeds')
|
| 190 |
|
| 191 | def test_out_of_scope_entries_are_never_decoded(self):
|
| 192 | h = self.blob_hash()
|
| 193 | self.install_tree([('blob', 'huge.bin', 'f' * 64, 'regular'), ('blob', 'link', h, 'symlink'),
|
| 194 | ('blob', 'payload.txt', h, 'regular')])
|
| 195 | result = self.read('payload.txt')
|
| 196 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 197 | payload = json.loads(result.stdout)
|
| 198 | self.assertEqual(payload['scope'], ['payload.txt'])
|
| 199 | self.assertEqual(payload['nonregular_paths'], [])
|
| 200 | self.assertEqual(sorted(p.name for p in (self.root / 'out').iterdir()), ['payload.txt'])
|
| 201 |
|
| 202 | def test_in_scope_symlink_is_reported_for_oracle_failure(self):
|
| 203 | h = self.blob_hash()
|
| 204 | self.install_tree([('blob', 'link', h, 'symlink'), ('blob', 'payload.txt', h, 'regular')])
|
| 205 | result = self.read('link', 'payload.txt')
|
| 206 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 207 | self.assertEqual(json.loads(result.stdout)['nonregular_paths'], ['link'])
|
| 208 | self.assertFalse((self.root / 'out/link').exists())
|
| 209 |
|
| 210 | def test_scoped_directory_reads_only_its_subtree(self):
|
| 211 | import blake3
|
| 212 | import zstandard
|
| 213 | h = self.blob_hash()
|
| 214 | inner = '\t'.join(('blob', 'config.toml', h, 'regular')).encode()
|
| 215 | inner_hash = blake3.blake3(inner).hexdigest()
|
| 216 | with self.database() as db:
|
| 217 | db.execute('INSERT INTO trees(hash,content) VALUES (?,?)', (inner_hash, zstandard.ZstdCompressor().compress(inner)))
|
| 218 | self.install_tree([('blob', 'other', 'e' * 64, 'regular'), ('tree', 'services', inner_hash, 'tree')])
|
| 219 | result = self.read('services')
|
| 220 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 221 | self.assertEqual((self.root / 'out/services/config.toml').read_bytes(), b'committed payload\n')
|
| 222 | self.assertFalse((self.root / 'out/other').exists())
|
| 223 |
|
| 224 | def test_normalized_legacy_tree_roundtrip(self):
|
| 225 | self.install_tree([('blob', 'legacy', self.blob_hash(), 'regular')], normalized=True)
|
| 226 | result = self.read()
|
| 227 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 228 | self.assertEqual((self.root / 'out/legacy').read_bytes(), b'committed payload\n')
|
| 229 |
|
| 230 | def test_legacy_entry_field_bound_precedes_materialization(self):
|
| 231 | self.install_tree([('blob', 'x' * 4097, self.blob_hash(), 'regular')], normalized=True)
|
| 232 | self.assert_unavailable(self.read(), 'entry exceeds')
|
| 233 |
|
| 234 | def test_executable_mode_is_preserved_for_downstream_validation(self):
|
| 235 | self.install_tree([('blob', 'executable', self.blob_hash(), 'executable')])
|
| 236 | result = self.read()
|
| 237 | self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
| 238 | self.assertTrue((self.root / 'out/executable').stat().st_mode & 0o111)
|
| 239 |
|
| 240 | def test_source_database_change_after_snapshot_is_unavailable(self):
|
| 241 | spec = importlib.util.spec_from_file_location('independent_reader_control', READER)
|
| 242 | reader = importlib.util.module_from_spec(spec)
|
| 243 | spec.loader.exec_module(reader)
|
| 244 | original_write = Path.write_bytes
|
| 245 | def write_then_change_database(path, content):
|
| 246 | result = original_write(path, content)
|
| 247 | if path.name == 'snapshot.db':
|
| 248 | with self.database() as db:
|
| 249 | db.execute("INSERT OR REPLACE INTO metadata(key,value) VALUES ('changed-after-snapshot','1')")
|
| 250 | return result
|
| 251 | with mock.patch.object(Path, 'write_bytes', write_then_change_database):
|
| 252 | with self.assertRaisesRegex(reader.Unavailable, 'database changed'):
|
| 253 | reader.read_committed_tree(self.repo, self.root / 'out')
|
| 254 |
|
| 255 | def test_unknown_blob_codec_is_unavailable(self):
|
| 256 | with self.database() as db:
|
| 257 | db.execute('UPDATE blobs SET codec=99')
|
| 258 | result = self.read()
|
| 259 | self.assertEqual(result.returncode, 77, result.stdout)
|
| 260 | self.assertIn('unsupported object codec', result.stdout)
|
| 261 |
|
| 262 | def commit_bugfix_payload(self):
|
| 263 | env = {**os.environ, 'OAK_NO_UPDATE_CHECK': '1', 'HOME': str(self.root)}
|
| 264 | (self.repo / 'app').mkdir()
|
| 265 | (self.repo / 'app/pricing.py').write_text('def f(cents, percent):\n return cents - ((cents * percent) // 100)\n')
|
| 266 | subprocess.run([shutil.which('oak'), 'commit', '--no-verify'], cwd=self.repo, env=env, check=True, capture_output=True)
|
| 267 |
|
| 268 | def evidence(self):
|
| 269 | sys.path.insert(0, str(ROOT / 'scripts'))
|
| 270 | from oakbench.workflow_oracles import independent_oak_evidence, VERSION
|
| 271 | return independent_oak_evidence('bugfix_test_loop', self.repo, sys.executable,
|
| 272 | {'oracle_version': VERSION, 'workflow_integrity_source': 'trusted_task_worktree',
|
| 273 | 'workflow_integrity_passed': None, 'workflow_integrity_unmeasured_reason': None})
|
| 274 |
|
| 275 | def test_reader_bound_on_in_scope_blob_is_unmeasured_not_oak_failure(self):
|
| 276 | self.commit_bugfix_payload()
|
| 277 | with self.database() as db:
|
| 278 | db.execute('UPDATE blobs SET size=? WHERE size>?', (32 * 1024 * 1024 + 1, len(b'committed payload\n')))
|
| 279 | result = self.evidence()
|
| 280 | self.assertIsNone(result['workflow_integrity_passed'], result)
|
| 281 | self.assertIn('oak_reader_unavailable', result['workflow_integrity_unmeasured_reason'])
|
| 282 |
|
| 283 | def test_out_of_scope_oversized_blob_does_not_fail_oak(self):
|
| 284 | self.commit_bugfix_payload()
|
| 285 | with self.database() as db:
|
| 286 | db.execute('UPDATE blobs SET size=? WHERE size=?', (32 * 1024 * 1024 + 1, len(b'committed payload\n')))
|
| 287 | result = self.evidence()
|
| 288 | self.assertIs(result['workflow_integrity_passed'], True, result)
|
| 289 | self.assertEqual(result['workflow_integrity_committed_scope'], ['app/pricing.py'])
|
| 290 |
|
| 291 | def test_corrupt_reader_envelope_reaches_workflow_failure(self):
|
| 292 | sys.path.insert(0, str(ROOT / 'scripts'))
|
| 293 | from oakbench.workflow_oracles import independent_oak_evidence, VERSION
|
| 294 | self.commit_bugfix_payload()
|
| 295 | with self.database() as db:
|
| 296 | db.execute('UPDATE blobs SET content=?,size=?,codec=0', (b'corrupt', 7))
|
| 297 | result = independent_oak_evidence('bugfix_test_loop', self.repo, sys.executable,
|
| 298 | {'oracle_version': VERSION, 'workflow_integrity_source': 'trusted_task_worktree',
|
| 299 | 'workflow_integrity_passed': None, 'workflow_integrity_unmeasured_reason': None})
|
| 300 | self.assertIs(result['workflow_integrity_passed'], False, result)
|
| 301 | self.assertIn('oak_stored_content_invalid', result['workflow_integrity_failure_reason'])
|
| 302 | self.assertIsNone(result['workflow_integrity_unmeasured_reason'])
|