from concurrent.futures import ThreadPoolExecutor import pytest import win32con import win32file def test_library_lock_excludes_other_thread_and_uploader_but_reenters(tmp_path): from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy library = tmp_path/'test.lmc' library.write_bytes(b'fixture') with LibraryLock(library) as lock: assert lock.path.name == '.test.cbb-upload.lock' with LibraryLock(library): assert lock.snapshot_bytes() with pytest.raises(Exception): win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, 0, None) def competing(): with pytest.raises(LibraryBusy): with LibraryLock(library): pass with ThreadPoolExecutor(1) as pool: pool.submit(competing).result() assert lock.path.exists() with LibraryLock(library): pass def test_complete_copy_reads_owned_lock_without_reopening(tmp_path): from cbb_refresh_verify.library_lock import LibraryLock from cbb_refresh_verify.scoped_manifest import copy_verified source = tmp_path/'library' source.mkdir() library = source/'test.lmc' library.write_bytes(b'fixture') with LibraryLock(library) as lock: expected = lock.snapshot_bytes() copy_verified(source, tmp_path/'snapshot') assert (tmp_path/'snapshot'/lock.path.name).read_bytes() == expected def test_external_lock_never_gets_snapshot_exception(tmp_path): from cbb_refresh_verify.scoped_manifest import copy_verified source = tmp_path/'library' source.mkdir() path = source/'.test.cbb-upload.lock' path.write_bytes(b'external') handle = win32file.CreateFile(str(path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None) try: with pytest.raises(OSError): copy_verified(source, tmp_path/'snapshot') finally: handle.Close() def test_verification_refuses_uploader_exclusive_lock_before_native_work(tmp_path, monkeypatch): import cbb_refresh_verify.verify_runner as runner library = tmp_path/'lib.lmc' library.touch() lock = tmp_path/'.lib.cbb-upload.lock' handle = win32file.CreateFile(str(lock), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, 0, None) monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('native work bypassed lock')) try: result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) assert result.status == 'FAIL' and result.safe_to_continue finally: handle.Close() @pytest.mark.parametrize('checkpoint_matches',[True,False]) def test_auto_verify_reuses_only_exact_active_parent_transaction(tmp_path,monkeypatch,checkpoint_matches): import json from cbb_refresh_verify import library_lock as locks,verify_runner as runner library=tmp_path/'library/lib.lmc';library.parent.mkdir();library.touch() calls=[] monkeypatch.setattr(runner,'_run_verify_serialized',lambda *a,**k: calls.append('verified') or runner.VerifyResult('PASS',library_flag='GREEN')) with locks.LibraryLock(library,block_name='A'): if not checkpoint_matches: marker=locks.blocker_path(library) value=json.loads(marker.read_text('utf-8'));value['lease_id']='b'*32 marker.write_text(json.dumps(value),'utf-8') result=runner.run_verify(runner.VerifyRequest(library,'A',tmp_path/'evidence')) assert (result.status=='PASS') is checkpoint_matches,result.stderr assert calls==(['verified'] if checkpoint_matches else []) if not checkpoint_matches: # Restore only the fixture checkpoint so the enclosing test lease can exit. owner=locks._owners[locks.LibraryLock(library).key] value['lease_id']=owner['lease_id'];marker.write_text(json.dumps(value),'utf-8') def test_unsafe_verify_records_failure_without_persistent_library_blocker(tmp_path, monkeypatch): import json import cbb_refresh_verify.verify_runner as runner import cbb_refresh_verify.library_lock as locks library = tmp_path/'lib.lmc'; library.touch() evidence = tmp_path/'out'; evidence.mkdir() (evidence/'session-release.json').write_text(json.dumps({'library': str(library), 'block': 'Block', 'worker_exited': True, 'safe_to_continue': True})) monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('FAIL', safe_to_continue=False, evidence_path=evidence/'result.json')) result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) assert not result.safe_to_continue assert not locks.blocker_path(library).exists() with locks.LibraryLock(library): pass def test_explicit_release_requires_fresh_proof_and_never_deletes_sidecar(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() with locks.LibraryLock(library, safe_to_release=lambda: False) as lease: pass try: def denied(*a): raise ValueError('native owner remains') monkeypatch.setattr(locks, '_prove_library_released', denied) with pytest.raises(ValueError, match='native owner'): locks.release_retained_library(library, tmp_path/'release') assert locks.blocker_path(library).is_file() from contextlib import contextmanager @contextmanager def proof(*a): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} monkeypatch.setattr(locks, '_prove_library_released', proof) result = locks.release_retained_library(library, tmp_path/'release') assert result['status'] == 'RELEASE_CONFIRMED' assert lease.path.is_file() and not locks.blocker_path(library).exists() with locks.LibraryLock(library): pass finally: with locks._guard: owner = locks._owners.pop(str(lease.path.resolve()).casefold(), None) if owner: owner['handle'].Close() def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe monkeypatch.setattr(probe, 'query_owners', lambda *_: []) library = tmp_path/'lib.lmc'; library.touch() address = tmp_path/'database/cdbsvr/sAddress.adr'; address.parent.mkdir(parents=True) address.touch() with pytest.raises(locks.LibraryBusy): with locks._prove_library_released(library): pass address.unlink() # Test-created empty marker, never a native address file. handle = win32file.CreateFile(str(library), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None) try: with pytest.raises(Exception): with locks._prove_library_released(library): pass finally: handle.Close() with locks._prove_library_released(library) as proof: assert proof['exclusive_files'] == [str(library)] def test_release_does_not_resolve_every_unrelated_generated_file(tmp_path, monkeypatch): from pathlib import Path import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe monkeypatch.setattr(probe, 'query_owners', lambda *_: []) library = tmp_path/'lib.lmc'; library.touch() outputs = tmp_path/'generated'; outputs.mkdir() for index in range(100): (outputs/f'{index}.log').touch() database = tmp_path/'database/icdb.dat'; database.parent.mkdir(); database.touch() resolve = Path.resolve def guarded(path, *a, **kw): assert path.parent != outputs, 'Release resolved unrelated generated content' return resolve(path, *a, **kw) monkeypatch.setattr(Path, 'resolve', guarded) with locks._prove_library_released(library) as proof: assert set(proof['exclusive_files']) == {str(library), str(database)} def test_scoped_release_ignores_another_cbb_address(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe monkeypatch.setattr(probe, 'query_owners', lambda *_: []) library = tmp_path/'lib.lmc'; library.touch() other = tmp_path/'ReusableBlocksLP/Other/Work/database/cdbsvr/sAddress.adr' other.parent.mkdir(parents=True); other.touch() with locks._prove_library_released(library, block_name='Target') as proof: assert proof['addresses'] == [] def test_release_probe_timeout_does_not_clear_retained_checkpoint(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() with locks.LibraryLock(library, safe_to_release=lambda: False) as lease: pass marker = locks.blocker_path(library) before = marker.read_bytes() def timeout(*args): raise RuntimeError('OWNER_QUERY_TIMEOUT') monkeypatch.setattr(probe, 'query_owners', timeout) try: with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'): locks.release_retained_library(library, tmp_path/'release') assert marker.read_bytes() == before finally: with locks._guard: owner = locks._owners.pop(lease.key, None) if owner: owner['handle'].Close() @pytest.mark.parametrize('name,command', [ ('cscript.exe', 'cscript.exe close_failed_block.vbs'), ('python.exe', 'python.exe -m cbb_refresh_verify --repair-worker job.json'), ]) def test_release_proof_waits_for_late_operation_helpers(tmp_path, monkeypatch, name, command): import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe library = tmp_path/'lib.lmc'; library.touch() monkeypatch.setattr(probe, 'query_owners', lambda names: [{'pid': 123456, 'name': name, 'command': command}] if name in names else []) with pytest.raises(locks.LibraryBusy, match='123456'): with locks._prove_library_released(library): pass def test_release_rechecks_owners_after_database_handles_are_acquired(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe library = tmp_path/'lib.lmc'; library.touch() calls = [] def query(names): calls.append(1) return [] if len(calls) == 1 else [{'pid': 123456, 'name': 'cscript.exe', 'command': 'cscript.exe close_failed_block.vbs'}] monkeypatch.setattr(probe, 'query_owners', query) with pytest.raises(locks.LibraryBusy, match='123456'): with locks._prove_library_released(library): pass def test_public_verify_initializes_com_in_its_operation_thread(tmp_path, monkeypatch): import ctypes import cbb_refresh_verify.verify_runner as runner library = tmp_path/'lib.lmc'; library.touch() def serialized(*a, **k): apartment, qualifier = ctypes.c_int(), ctypes.c_int() ole32 = ctypes.WinDLL('ole32') ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] initialized = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0 return runner.VerifyResult('PASS' if initialized else 'FAIL', stderr='COM apartment check') monkeypatch.setattr(runner, '_run_verify_serialized', serialized) with ThreadPoolExecutor(1) as pool: result = pool.submit(runner.run_verify, runner.VerifyRequest(library, 'Block', tmp_path/'out')).result() assert result.status == 'PASS' @pytest.mark.parametrize('mode,expected', [(0, 1), (2, 0)]) def test_public_verify_preserves_existing_apartment(tmp_path, monkeypatch, mode, expected): import ctypes, pythoncom import cbb_refresh_verify.verify_runner as runner library = tmp_path/'lib.lmc'; library.touch() monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('PASS')) def existing_mta(): pythoncom.CoInitializeEx(mode) try: apartment, qualifier = ctypes.c_int(), ctypes.c_int() ole32 = ctypes.WinDLL('ole32') ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] assert ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0 before = apartment.value # A thread can be the main STA (3) when it creates the process's # first remaining STA. Verify must preserve either STA flavor. assert before in ((1,) if mode == 0 else (0, 3)) runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) result = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) return result, apartment.value, before finally: pythoncom.CoUninitialize() with ThreadPoolExecutor(1) as pool: result, after, before = pool.submit(existing_mta).result() assert result == 0 and after == before def test_nested_snapshot_retains_runtime_checkpoint_bytes_without_semantic_conflicts(tmp_path): import cbb_refresh_verify.library_lock as locks from cbb_refresh_verify.scoped_manifest import copy_verified, build_manifest, ManifestScope library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.write_bytes(b'library') scope = ManifestScope.library('Block', library.name) with locks.LibraryLock(library) as lease: before = copy_verified(library.parent, tmp_path/'snapshot', scope=scope) marker = locks.blocker_path(library) assert (tmp_path/'snapshot'/marker.name).read_bytes() == marker.read_bytes() with locks.LibraryLock(library): owner = locks._owners[lease.key] locks._persist_blocker(library, lease_id=owner['lease_id']) assert build_manifest(library.parent, scope=scope) == before assert not marker.exists() and lease.path.exists() def test_arbitrary_guard_filename_is_preserved_and_primary(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks from cbb_refresh_verify.scoped_manifest import build_manifest, ManifestScope library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() marker = locks.blocker_path(library); marker.write_bytes(b'{not a guard record') with pytest.raises(locks.LibraryBusy): with locks.LibraryLock(library): pass manifest = build_manifest(library.parent, scope=ManifestScope.library('Block', library.name)) assert marker.name in manifest['files'] from contextlib import contextmanager @contextmanager def proof(*a, **kw): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} monkeypatch.setattr(locks, '_prove_library_released', proof) with pytest.raises(locks.LibraryBusy): locks.release_retained_library(library, tmp_path/'release') assert marker.read_bytes() == b'{not a guard record'