216 lines
10 KiB
Python
216 lines
10 KiB
Python
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 == 'BLOCKED' and not result.safe_to_continue
|
|
finally:
|
|
handle.Close()
|
|
|
|
|
|
def test_unsafe_verify_retains_lease_and_persistent_blocker(tmp_path, monkeypatch):
|
|
import cbb_refresh_verify.verify_runner as runner
|
|
import cbb_refresh_verify.library_lock as locks
|
|
library = tmp_path/'lib.lmc'; library.touch()
|
|
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k:
|
|
runner.VerifyResult('FAIL', safe_to_continue=False))
|
|
result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
|
assert not result.safe_to_continue
|
|
assert locks.blocker_path(library).is_file()
|
|
with pytest.raises(locks.LibraryBusy):
|
|
with locks.LibraryLock(library): pass
|
|
# Simulate OS handle loss on process exit; persistent marker still blocks.
|
|
with locks._guard:
|
|
owner = locks._owners.pop(str((tmp_path/'.lib.cbb-upload.lock').resolve()).casefold(), None)
|
|
if owner: owner['handle'].Close()
|
|
with pytest.raises(locks.LibraryBusy):
|
|
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 win32com.client
|
|
from types import SimpleNamespace
|
|
monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=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_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): 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'
|