fix: require owned fixture authority for native Add research

This commit is contained in:
阿白叔 committed 2026-11-03 11:22:27 +08:00
1 parent 07a9c1eaaf
commit b97556dcc1
4 files changed
+208 -18

No files matched your search

+86 -7
View File
@@ -13,6 +13,82 @@ def test_installed_binary_mismatch_disables_removal(tmp_path):
assert not caps['add'].supported and not caps['update'].supported
def test_public_add_cannot_bypass_disabled_capability(tmp_path, monkeypatch):
import cbb_refresh_verify.native_management as native
from cbb_refresh_verify.management import ManagementRequest
adapter = native.NativeManagementAdapter(mentor_home=tmp_path)
monkeypatch.setattr(adapter, '_worker', lambda *a: pytest.fail('public disabled Add launched worker'))
monkeypatch.setattr(native, 'engineering_identity', lambda *a: pytest.fail('public disabled Add accessed source'))
result = adapter.add(ManagementRequest('add', tmp_path/'arbitrary.lmc', 'Block',
source=tmp_path/'source/original.prj'), tmp_path/'add')
assert not result.success and result.safe_to_continue
def test_unprepared_add_rejected_before_native_activation(tmp_path, monkeypatch):
import cbb_refresh_verify.native_management as native
import win32com.client
monkeypatch.setattr(win32com.client, 'DispatchEx', lambda *a: pytest.fail('unprepared native activation'))
with pytest.raises(ValueError):
native._catalog_com({'operation': 'add', 'library': str(tmp_path/'arbitrary.lmc')}, tmp_path/'add')
def test_removed_marker_cannot_cross_carriage_return_record():
from cbb_refresh_verify.native_management import removal_output_ok
assert not removal_output_ok(0, "# Note: Removed unrelated\rrecord 'Block' from partition 'Logical-Physical'.",
'Block', 'Logical-Physical')
@pytest.mark.parametrize('invalid', ['', 'nonce', 'lease', 'source', 'backup', 'expired', 'token'])
def test_fixture_add_authority_requires_live_lease_and_fresh_staging(tmp_path, monkeypatch, invalid):
import cbb_refresh_verify.native_management as native
from cbb_refresh_verify.management import ManagementRequest
from cbb_refresh_verify.library_lock import LibraryLock
library = tmp_path/'native-add/fixture/mentor_lib.lmc'
library.parent.mkdir(parents=True); library.write_bytes(b'owned fixture')
source = tmp_path/'original/Source.prj'
source.parent.mkdir(); source.write_bytes(b'original source')
(tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8')
adapter = native.NativeManagementAdapter(mentor_home=tmp_path)
monkeypatch.setattr(adapter, 'route_capabilities', lambda: {'add': native.Capability(True, 'ready')})
monkeypatch.setattr(native, 'engineering_identity', lambda _: {'source': 'bytes'})
def launch(job, root):
staged = Path(job['source'])
assert staged == root/'source/Source.prj' and staged != source
prepared_path = root/'add-prepared.json'
prepared = json.loads(prepared_path.read_text(encoding='utf-8'))
if invalid == 'nonce': job['transaction_nonce'] = 'foreign'
if invalid == 'token': job['probe_token'] = 'foreign'
if invalid == 'lease': prepared['lease_id'] = 'foreign'
if invalid == 'source': job['source'] = str(source)
if invalid == 'backup': (root/'library-before/mentor_lib.lmc').write_bytes(b'changed')
if invalid == 'expired':
import cbb_refresh_verify.library_lock as locks
monkeypatch.setattr(locks, '_supervisor_alive', lambda _: False)
prepared_path.write_text(json.dumps(prepared), encoding='utf-8')
if invalid:
with pytest.raises(ValueError): native.validate_owned_fixture_add(job, root)
else:
assert native.validate_owned_fixture_add(job, root) == staged
assert source.read_bytes() == b'original source'
return {'success': not invalid, 'safe_to_continue': True, 'identity_matches': not invalid}
monkeypatch.setattr(adapter, '_launch_worker', launch)
with LibraryLock(library):
result = adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source),
tmp_path/'shared-add', probe_token='owned')
assert result.success is (not invalid)
def test_fixture_metadata_without_current_thread_lease_cannot_stage_source(tmp_path):
import cbb_refresh_verify.native_management as native
library = tmp_path/'native-add/fixture/mentor_lib.lmc'
library.parent.mkdir(parents=True); library.touch()
(tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8')
root = tmp_path/'add'; root.mkdir()
with pytest.raises(ValueError, match='current thread'):
native.prepare_owned_fixture_add({'library': str(library), 'probe_token': 'owned'}, root)
assert not (root/'source').exists()
def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_path, monkeypatch):
import hashlib
import cbb_refresh_verify.native_management as native
@@ -25,7 +101,7 @@ def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_p
assert '身份' in adapter.capabilities()['add'].reason
def test_native_add_passes_existing_library_and_staged_source_to_worker(tmp_path, monkeypatch):
def test_owned_fixture_add_passes_existing_library_and_source_to_worker(tmp_path, monkeypatch):
from cbb_refresh_verify.management import ManagementRequest
import cbb_refresh_verify.native_management as native
source = tmp_path/'source.prj'; source.write_bytes(b'source')
@@ -42,13 +118,13 @@ def test_native_add_passes_existing_library_and_staged_source_to_worker(tmp_path
monkeypatch.setattr(adapter, '_worker', worker)
request = ManagementRequest('add', library, 'Block', source=source,
mapped_drive='S:', mapped_remote_root=r'\\server\share', timeout_seconds=23)
result = adapter.add(request, tmp_path/'evidence')
result = adapter.add_owned_fixture(request, tmp_path/'evidence', probe_token='owned')
assert result.success and result.safe_to_continue and result.identity_matches is True
assert seen['job'] == {
'operation': 'add', 'library': str(library), 'source': str(source),
'name': 'Block', 'partition': 'Logical-Physical', 'top_level_block': '',
'mapped_drive': 'S:', 'mapped_remote_root': r'\\server\share',
'timeout_seconds': 23, 'source_identity': {'source': str(source)},
'timeout_seconds': 23, 'source_identity': {'source': str(source)}, 'probe_token': 'owned',
}
@@ -66,7 +142,8 @@ def test_native_add_identity_mismatch_is_native_success_with_evidence(tmp_path,
'detail': 'engineering identity readback mismatch',
})
original = source.read_bytes()
result = adapter.add(ManagementRequest('add', library, 'Block', source=source), tmp_path/'evidence')
result = adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source),
tmp_path/'evidence', probe_token='owned')
assert result.success and result.identity_matches is False
assert 'identity' in result.detail
assert result.evidence_path == tmp_path/'evidence'
@@ -160,7 +237,8 @@ def test_probe_requires_owned_new_native_library_path(tmp_path):
@pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]])
@pytest.mark.parametrize('mapped', [False, True])
def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped):
@pytest.mark.parametrize('flow', [1, 2])
def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped, flow):
import cbb_refresh_verify.native_management as native
import cbb_refresh_verify.mentor_adapter as mentor
import win32com.client
@@ -177,7 +255,7 @@ def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path,
items = Collection([SimpleNamespace(Name=name, PartitionName=partition) for name, partition in records])
class Library:
FullName = str(library_path)
FlowType = 1
FlowType = flow
def Partitions(self, kind):
assert kind == 7
return Collection([items])
@@ -197,7 +275,7 @@ def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path,
'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path)}, tmp_path)
assert result['success'] and result['safe_to_continue']
assert [(entry['name'], entry['partition']) for entry in result['entries']] == records
assert result['flow'] == 1
assert result['flow'] == flow
def test_production_caps_require_all_validated_binary_hashes(tmp_path, monkeypatch):
@@ -268,6 +346,7 @@ def test_shared_add_reopens_exact_catalog_and_reports_identity_mismatch(tmp_path
monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test'))
monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else [])
monkeypatch.setattr(native, '_probe_add_dialog', lambda *a, **k: events.append('add'))
monkeypatch.setattr(native, 'validate_owned_fixture_add', lambda *a: None)
result = native._catalog_com({'operation': 'add', 'library': str(library_path),
'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path),
'name': 'Block', 'partition': 'Logical-Physical', 'source_identity': {'expected': 'bytes'}}, tmp_path)