Files
cbb-refresh-verify-795/tests/test_scoped_manifest.py
T

362 lines
17 KiB
Python

"""File-level regressions: complete snapshots, scoped semantic conflicts."""
import hashlib
import os
from pathlib import Path
import shutil
import pytest
from cbb_refresh_verify import backup_recovery as recovery
def put(root, relative, data=b'engineering'):
path = root / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
return path
@pytest.mark.parametrize('relative', [
'A.prj', 'PCB/A.pcb', 'PCB/A.dxp', 'database/icdb.dat',
'PCB/Layout/LayoutDB.lyt', 'schematic/sheet.1', 'logic/block.1',
'symbols/device.sym', 'CES/constraints.ces', 'config/native.cfg',
'unknown/new-engineering-format.bin', 'notes.txt',
])
def test_primary_edits_additions_and_deletions_change_semantics(tmp_path, relative):
path = put(tmp_path, relative)
before = recovery._manifest(tmp_path)
assert before['version'] == 1
entry = before['files'][relative]
assert entry == {'size': len(b'engineering'),
'sha256': hashlib.sha256(b'engineering').hexdigest(), 'scope': 'project'}
path.write_bytes(b'changed')
assert recovery._manifest(tmp_path) != before
path.unlink()
assert recovery._manifest(tmp_path) != before
put(tmp_path, relative, b'engineering')
put(tmp_path, 'additional.dat')
assert recovery._manifest(tmp_path) != before
@pytest.mark.parametrize('relative', [
'logs/session.txt', 'log/native.log', 'session.log', 'temp/native.bin',
'tmp/native.bin', 'session.tmp', 'database/icdb.lck',
'database/cdbsvr/sAddress.adr', 'database/cdbsvr/sAddress_.adr',
'PCB/database/cdbsvr/SADDRESS_.ADR', '.target.cbb-upload.lock',
'backups/old/A.prj', 'backup/old/icdb.dat', 'A.prj.bak',
'drawing.pdf', 'output/netlist.txt', 'reports/check.txt',
])
def test_runtime_history_and_generated_content_is_copied_but_not_hashed(tmp_path, relative):
source = tmp_path/'source'
put(source, 'A.prj')
runtime = put(source, relative, b'old')
destination = tmp_path/'copy'
progress = []
manifest = recovery._copy_verified(source, destination, progress=progress.append)
assert (destination/relative).read_bytes() == b'old'
assert manifest['version'] == 1
assert set(manifest['files']) == {'A.prj'}
assert progress[-1]['hashed_files'] == 1
assert progress[-1]['hashed_bytes'] == len(b'engineering')
assert progress[-1]['stage'] == 'copy'
runtime.write_bytes(b'new runtime content')
put(source, 'logs/new-session.log')
assert recovery._manifest(source) == manifest
runtime.unlink()
assert recovery._manifest(source) == manifest
def test_central_scope_hashes_target_work_cert_root_config_and_shared_dependencies(tmp_path):
from cbb_refresh_verify.scoped_manifest import ManifestScope
scope = ManifestScope.library('A', 'target.lmc')
expected = {
'target.lmc': 'library_config',
'library.xml': 'library_config',
'ReusableBlocksLP/A/Work/A.prj': 'target_work',
'ReusableBlocksLP/A/Cert/A.prj': 'target_cert',
'SymbolLibs/__ReusableBlocksSymb/icdb.dat': 'shared_dependency',
'CellDB/custom.cell': 'shared_dependency',
'PartDB/parts.pdb': 'shared_dependency',
'NewRequiredDependency/native.bin': 'shared_dependency',
}
for relative in expected:
put(tmp_path, relative)
unrelated = put(tmp_path, 'ReusableBlocksLP/B/Work/B.prj')
put(tmp_path, 'ReusableBlocksLP/A/Backups/old.prj')
put(tmp_path, 'other.lmc')
before = recovery._manifest(tmp_path, scope=scope)
assert before['scope'] == {'kind': 'library', 'block_name': 'A', 'library_name': 'target.lmc'}
assert {p: e['scope'] for p, e in before['files'].items()} == expected
unrelated.write_bytes(b'other project changed')
put(tmp_path, 'ReusableBlocksLP/C/Work/C.prj')
assert recovery._manifest(tmp_path, scope=scope) == before
for relative in expected:
path = tmp_path/relative
path.write_bytes(b'changed')
assert recovery._manifest(tmp_path, scope=scope) != before
path.write_bytes(b'engineering')
destination = tmp_path.parent/(tmp_path.name + '-copy')
assert recovery._copy_verified(tmp_path, destination, scope=scope) == before
assert (destination/'ReusableBlocksLP/B/Work/B.prj').read_bytes() == b'other project changed'
@pytest.mark.parametrize('relative', ['A.prj', 'database/icdb.dat', 'target.lmc'])
def test_corrupted_destination_bytes_are_rejected_and_evidence_is_preserved(tmp_path, monkeypatch, relative):
source = tmp_path/'source'
put(source, relative)
destination = tmp_path/'destination'
real_hash = recovery._hash
def corrupt_then_hash(path):
if path == destination/relative:
metadata = path.stat()
path.write_bytes(b'X' * metadata.st_size)
os.utime(path, ns=(metadata.st_atime_ns, metadata.st_mtime_ns))
return real_hash(path)
monkeypatch.setattr(recovery, '_hash', corrupt_then_hash)
with pytest.raises(OSError):
recovery._copy_verified(source, destination)
assert (source/relative).read_bytes() == b'engineering'
assert (destination/relative).exists()
def test_unlocked_primary_copy_reads_source_twice_and_target_once(tmp_path, monkeypatch):
from collections import Counter
source, destination = tmp_path/'source', tmp_path/'destination'
primary = put(source, 'A.prj', b'engineering' * 100000)
reads = Counter()
original_open = Path.open
def opened(path, mode='r', *args, **kwargs):
if mode == 'rb':
reads[path] += 1
return original_open(path, mode, *args, **kwargs)
monkeypatch.setattr(Path, 'open', opened)
hashes = Counter()
original_hash = recovery._hash
def hashed(path):
hashes[path] += 1
return original_hash(path)
monkeypatch.setattr(recovery, '_hash', hashed)
manifest = recovery._copy_verified(source, destination)
assert hashes[primary] == 1 # Only the final source rehash is a separate pass.
assert hashes[destination/'A.prj'] == 1
assert reads[primary] == 2 # Copy/hash stream, then final source content proof.
assert reads[destination/'A.prj'] == 1
assert manifest['files']['A.prj']['sha256'] == hashlib.sha256(primary.read_bytes()).hexdigest()
def test_source_byte_changes_with_preserved_metadata_are_rejected(tmp_path, monkeypatch):
source, destination = tmp_path/'source', tmp_path/'destination'
primary = put(source, 'A.prj')
real_copy = shutil.copystat
def copy_then_change_source(src, dst, *args, **kwargs):
result = real_copy(src, dst, *args, **kwargs)
metadata = primary.stat()
primary.write_bytes(b'X' * metadata.st_size)
os.utime(primary, ns=(metadata.st_atime_ns, metadata.st_mtime_ns))
return result
monkeypatch.setattr(shutil, 'copystat', copy_then_change_source)
with pytest.raises(OSError):
recovery._copy_verified(source, destination)
assert (destination/'A.prj').read_bytes() == b'engineering'
@pytest.mark.parametrize('change', ['changed', 'deleted', 'added'])
def test_complete_tree_metadata_must_stay_stable_during_copy(tmp_path, monkeypatch, change):
source = tmp_path/'source'
put(source, 'A.prj')
runtime = put(source, 'logs/session.log')
destination = tmp_path/'destination'
real_copy = shutil.copystat
def copy_then_mutate(src, dst, *args, **kwargs):
result = real_copy(src, dst, *args, **kwargs)
if Path(src).name == 'A.prj':
if change == 'changed':
runtime.write_bytes(b'changed while copying')
elif change == 'deleted':
runtime.unlink()
else:
put(source, 'logs/added.log')
return result
monkeypatch.setattr(shutil, 'copystat', copy_then_mutate)
with pytest.raises(OSError):
recovery._copy_verified(source, destination)
assert destination.exists()
def test_destination_nonprimary_membership_and_size_are_validated(tmp_path, monkeypatch):
source = tmp_path/'source'
put(source, 'logs/session.log')
destination = tmp_path/'destination'
real_copy = shutil.copy2
def truncated(src, dst, *args, **kwargs):
result = real_copy(src, dst, *args, **kwargs)
Path(dst).write_bytes(b'x')
return result
monkeypatch.setattr(shutil, 'copy2', truncated)
with pytest.raises(OSError):
recovery._copy_verified(source, destination)
def test_semantic_scan_does_not_visit_runtime_or_unrelated_cbb_trees(tmp_path, monkeypatch):
from cbb_refresh_verify.scoped_manifest import ManifestScope
scope = ManifestScope.library('A', 'target.lmc')
put(tmp_path, 'target.lmc')
put(tmp_path, 'ReusableBlocksLP/A/Work/A.prj')
put(tmp_path, 'ReusableBlocksLP/B/Work/B.prj')
put(tmp_path, 'logs/transient.log')
before = recovery._manifest(tmp_path, scope=scope)
real_scan = os.scandir
def runtime_unavailable(path):
# These trees may be actively changing after the successful snapshot.
if Path(path) in (tmp_path/'logs', tmp_path/'ReusableBlocksLP/B'):
raise OSError('transient runtime/unrelated tree is unavailable')
return real_scan(path)
monkeypatch.setattr(os, 'scandir', runtime_unavailable)
assert recovery._manifest(tmp_path, scope=scope) == before
def test_only_primary_paths_are_read_for_sha256_and_manifest_is_relocatable(tmp_path, monkeypatch):
from cbb_refresh_verify.scoped_manifest import ManifestScope
scope = ManifestScope.library('A', 'target.lmc')
source, destination = tmp_path/'source', tmp_path/'destination'
primary = {'target.lmc', 'ReusableBlocksLP/A/Work/A.prj',
'SymbolLibs/shared/icdb.dat', 'config.xml'}
excluded = {'ReusableBlocksLP/B/Work/B.prj', 'logs/native.log', 'export.pdf',
'.target.cbb-upload.lock', 'backup/old/icdb.dat'}
for relative in primary | excluded:
put(source, relative)
real_hash = recovery._hash
hashed = []
def record_hash(path):
root = source if path.is_relative_to(source) else destination
relative = path.relative_to(root).as_posix()
assert relative in primary, f'Nonprimary content hashed: {relative}'
hashed.append(relative)
return real_hash(path)
monkeypatch.setattr(recovery, '_hash', record_hash)
manifest = recovery._copy_verified(source, destination, scope=scope)
assert set(hashed) == primary
assert recovery._manifest(source, scope=scope) == manifest
assert recovery._manifest(destination, scope=scope) == manifest
assert {p.relative_to(destination).as_posix() for p in destination.rglob('*') if p.is_file()} == primary | excluded
@pytest.mark.parametrize('block', ['Logs', 'lOgS', 'Temp', 'tEmP', 'Backup', 'bAcKuP', 'Output', 'rEpOrT',
'Log Files', 'ProjectBackup', 'Work.before-restore-engineering'])
@pytest.mark.parametrize('role', ['Work', 'Cert'])
def test_structural_cbb_identity_is_primary_in_semantic_and_copy_manifests(tmp_path, block, role):
from cbb_refresh_verify.scoped_manifest import ManifestScope
scope = ManifestScope.library(block, 'target.lmc')
source, destination = tmp_path/'source', tmp_path/'destination'
relative = f'ReusableBlocksLP/{block}/{role}/database/icdb.dat'
primary = put(source, relative)
runtime = f'ReusableBlocksLP/{block}/{role}/logs/session.txt'
put(source, runtime)
before = recovery._copy_verified(source, destination, scope=scope)
assert set(before['files']) == {relative}
assert (destination/runtime).read_bytes() == b'engineering'
assert recovery._manifest(source, scope=scope) == before
primary.write_bytes(b'concurrent primary edit')
assert recovery._manifest(source, scope=scope) != before
@pytest.mark.parametrize('relative', [
'SymbolLibs/temp/icdb.dat', 'SymbolLibs/Temp_a/device.sym',
'SymbolLibs/Logs/device.sym', 'SymbolLibs/BACKUP/device.sym',
'SymbolLibs/temp/cdbsvr/persistent.bin',
'ReusableBlocksLP/A/Work/hdl/Logs/design.v',
'ReusableBlocksLP/A/Cert/hdl/bAcKuP/design.v',
'SymbolLibs/temp/hdl/Backup/design.v', 'SharedDependency/hdl/Logs/design.v',
])
def test_library_engineering_partition_names_are_not_runtime_categories(tmp_path, relative):
from cbb_refresh_verify.scoped_manifest import ManifestScope
scope = ManifestScope.library('A', 'target.lmc')
source, destination = tmp_path/'source', tmp_path/'destination'
primary = put(source, relative)
before = recovery._copy_verified(source, destination, scope=scope)
assert relative in before['files']
assert recovery._manifest(source, scope=scope) == before
primary.write_bytes(b'concurrent shared/design edit')
assert recovery._manifest(source, scope=scope) != before
@pytest.mark.parametrize('relative', ['hdl/Logs/design.v', 'hdl/Backup/design.v', 'symbols/Temp/device.sym',
'hdl/Log Files/design.v', 'hdl/ProjectBackup/backups/design.v',
'hdl/Work.before-restore-engineering/design.v', 'database/cdbsvr/persistent.bin'])
def test_nested_project_engineering_names_remain_primary(tmp_path, relative):
primary = put(tmp_path, relative)
before = recovery._manifest(tmp_path)
assert relative in before['files']
primary.unlink()
assert recovery._manifest(tmp_path) != before
@pytest.mark.parametrize('relative', [
'ReusableBlocksLP/Logs/Work/Logs.prj',
'ReusableBlocksLP/Backup/Cert/Backup.prj',
'SymbolLibs/temp/device.sym', 'SharedDependency/hdl/Logs/design.v',
])
@pytest.mark.parametrize('side', ['source', 'destination'])
def test_colliding_engineering_names_cannot_bypass_primary_copy_byte_checks(tmp_path, monkeypatch, relative, side):
from cbb_refresh_verify.scoped_manifest import ManifestScope
block = relative.split('/')[1] if relative.startswith('ReusableBlocksLP/') else 'A'
scope = ManifestScope.library(block, 'target.lmc')
source, destination = tmp_path/'source', tmp_path/'destination'
put(source, relative)
real_copy = shutil.copystat
def copy_then_corrupt(src, dst, *args, **kwargs):
result = real_copy(src, dst, *args, **kwargs)
corrupt = Path(src) if side == 'source' else Path(dst)
metadata = corrupt.stat()
corrupt.write_bytes(b'X' * metadata.st_size)
os.utime(corrupt, ns=(metadata.st_atime_ns, metadata.st_mtime_ns))
return result
monkeypatch.setattr(shutil, 'copystat', copy_then_corrupt)
with pytest.raises(OSError):
recovery._copy_verified(source, destination, scope=scope)
assert (destination/relative).exists()
@pytest.mark.parametrize('prefix', ['', 'ReusableBlocksLP/Logs/Work/', 'ReusableBlocksLP/Temp/Cert/'])
@pytest.mark.parametrize('relative', [
'Log Files/ProjectBackupLog.txt', 'ProjectBackup/backups/old.zip',
'database/cdbsvr/log/session/text.txt', 'database/cdbsvr/dbcheck/session/text.txt',
'PCB/database/cdbsvr/dbrestore/session/text.txt',
])
def test_known_native_runtime_and_history_locations_are_ignored_contextually(tmp_path, prefix, relative):
from cbb_refresh_verify.scoped_manifest import ManifestScope
scope = ManifestScope.library(prefix.split('/')[1], 'target.lmc') if prefix else None
runtime = put(tmp_path, prefix + relative)
before = recovery._manifest(tmp_path, scope=scope)
assert not before['files']
runtime.write_bytes(b'updated runtime')
assert recovery._manifest(tmp_path, scope=scope) == before
@pytest.mark.parametrize('relative', ['SymbolLibs/temp/icdb.dat',
'ReusableBlocksLP/Logs/Work/database/icdb.dat'])
def test_locked_critical_bytes_in_colliding_names_are_checked_independently(tmp_path, relative):
from cbb_refresh_verify.scoped_manifest import ManifestScope
source, destination = tmp_path/'source', tmp_path/'destination'
put(source, relative)
def corrupt_after_copy(event):
if event['copied_files'] == event['total_files']:
copied = destination/relative
metadata = copied.stat()
copied.write_bytes(b'X' * metadata.st_size)
os.utime(copied, ns=(metadata.st_atime_ns, metadata.st_mtime_ns))
with pytest.raises(OSError):
recovery._copy_verified(source, destination,
scope=ManifestScope.library('Logs', 'target.lmc'), progress=corrupt_after_copy)
assert (source/relative).read_bytes() == b'engineering'
assert (destination/relative).read_bytes() == b'XXXXXXXXXXX'
@pytest.mark.parametrize('relative', ['SymbolLibs/temp/cdbsvr/log/session/text.txt',
'SymbolLibs/Temp_a/cdbsvr/dbcheck/session/text.txt',
'CellDB/cdbsvr/dbrestore/session/text.txt'])
def test_shared_native_runtime_has_explicit_database_context(tmp_path, relative):
from cbb_refresh_verify.scoped_manifest import ManifestScope
put(tmp_path, relative)
assert not recovery._manifest(tmp_path, scope=ManifestScope.library('A', 'target.lmc'))['files']