fix: reject native source renames before management writes

This commit is contained in:
阿白叔 committed 2026-11-17 01:22:57 +08:00
1 parent 36ff964230
commit d40fa3a750
7 files changed
+102 -20

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
"""EE7.9 imports by source PRJ name; reject rename before any native mutation."""
from dataclasses import replace
import pytest
from cbb_refresh_verify.management import ManagementRequest, run_management
from cbb_refresh_verify.native_management import NativeManagementAdapter
from test_management import project
@pytest.mark.parametrize('operation', ['add', 'update'])
def test_real_adapter_rejects_mismatched_source_before_native_catalog_or_remove(tmp_path, monkeypatch, operation):
library=tmp_path/'library/mentor.lmc';library.parent.mkdir();library.write_bytes(b'unchanged library')
target=project(library.parent/'ReusableBlocksLP/Block/Work','Block',b'unchanged target')
source=project(tmp_path/'source','Other')
adapter=NativeManagementAdapter()
monkeypatch.setattr(adapter,'_binary_capability',lambda:'')
calls=[]
monkeypatch.setattr(adapter,'_worker',lambda *args:calls.append(args) or pytest.fail('native boundary reached'))
request=ManagementRequest(operation,library,'Block',source=source,evidence_dir=tmp_path/'evidence')
result=run_management(request,adapter=adapter,idle_check=lambda *_:None)
assert result.safe_to_continue and 'SOURCE_NAME_MISMATCH' in result.detail
assert calls == []
assert library.read_bytes()==b'unchanged library'
assert (target.parent/'database/icdb.dat').read_bytes()==b'unchanged target'
def test_source_name_check_uses_windows_case_insensitive_identity(tmp_path):
source=project(tmp_path/'source','BLOCK')
request=ManagementRequest('update',tmp_path/'library.lmc','Block',source=source)
NativeManagementAdapter().validate_source(request)
def test_management_preview_reports_mismatch_and_allows_rechoose(qtbot,tmp_path,monkeypatch):
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
from cbb_refresh_verify.ui import timed_prompt
wrong=project(tmp_path/'wrong','Other');right=project(tmp_path/'right','Block')
request=ManagementRequest('update',tmp_path/'library.lmc','Block',source=wrong)
dialog=ManagementDialog([request],adapter=NativeManagementAdapter())
qtbot.addWidget(dialog);dialog.show()
questions=[]
monkeypatch.setattr(timed_prompt,'question',lambda *a,**k:questions.append(a) or QMessageBox.No)
dialog.execute_button.click()
assert 'SOURCE_NAME_MISMATCH' in dialog.status_label.text()
assert not questions and dialog.execute_button.isEnabled()
dialog.table.item(0,2).setText(str(right))
dialog.execute_button.click()
assert len(questions)==1 and not dialog.busy
+2 -2
View File
@@ -18,7 +18,7 @@ def transaction(tmp_path, monkeypatch):
library = root/'native-add/fixture/mentor_lib.lmc'
library.parent.mkdir(parents=True); library.write_bytes(b'fixture library')
(root/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8')
source = project(tmp_path/'source')
source = project(tmp_path/'source', 'Block')
for path in (source, source.parent/'PCB/pcb.prj'):
with path.open('a') as stream: stream.write(f'KEY CentralLibrary "{library}"\n')
state = {'entries': [], 'calls': [], 'corrupt': False, 'fail_add': False}
@@ -80,7 +80,7 @@ def test_owned_fixture_engine_add_conflict_update_remove_and_identity(transactio
root, request, state = transaction
original = request.source.read_bytes()
assert execute(root, request).status == 'UPLOADED'
registered = request.library.parent/'ReusableBlocksLP/Block/Work/Source.prj'
registered = request.library.parent/'ReusableBlocksLP/Block/Work/Block.prj'
assert project_library(registered) == request.library.resolve()
calls = len(state['calls'])
assert execute(root, request).status == 'UPLOAD_FAILED'
+10 -5
View File
@@ -68,7 +68,8 @@ def test_add_timeout_publishes_cancellation_without_killing_native_owner(tmp_pat
@pytest.mark.parametrize('cancel', [True, False])
@pytest.mark.parametrize('journal_error', [False, True])
def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, cancel, journal_error, owned_add_process):
@pytest.mark.parametrize('changed_name', [False, True])
def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, cancel, journal_error, changed_name, owned_add_process):
import os,sys
from types import SimpleNamespace
import win32gui,win32process
@@ -90,7 +91,10 @@ def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, canc
monkeypatch.setattr(win32gui,'GetWindowText',lambda h:{100:'Reusable Blocks',200:'New Reusable Block'}[int(h)])
monkeypatch.setattr(win32gui,'GetDlgItem',lambda h,c:110 if h==100 else 210 if c==1083 else c+1000)
monkeypatch.setattr(win32gui,'GetClassName',lambda h:'Edit')
monkeypatch.setattr(native,'_set_probe_field',lambda *a:None)
def field(*args, read_only=False):
if read_only and args[1] == 1215 and changed_name:
raise RuntimeError('NATIVE_ADD_NAME_CHANGED')
monkeypatch.setattr(native,'_set_probe_field',field)
def post(h,*args):
state['posted'].append(h)
if h==110:state['new']=True
@@ -113,11 +117,12 @@ def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, canc
if journal_error:
def fail(*a, **kw): raise OSError('phase journal unavailable')
monkeypatch.setattr('cbb_refresh_verify.session_record.write_session_record', fail)
expected = OSError if journal_error and not cancel else RuntimeError
message = 'NATIVE_ADD_CANCELLED' if cancel else 'phase journal unavailable' if journal_error else 'unsafe OK posted'
expected = OSError if journal_error and not cancel and not changed_name else RuntimeError
message = ('NATIVE_ADD_NAME_CHANGED' if changed_name else 'NATIVE_ADD_CANCELLED' if cancel
else 'phase journal unavailable' if journal_error else 'unsafe OK posted')
with pytest.raises(expected, match=message):
native._probe_add_dialog_impl(SimpleNamespace(OnReusableBlocksEditor=lambda:None),job,desktop,{owned_add_process.pid},root)
assert state['posted']==([110] if cancel or journal_error else [110,210])
assert state['posted']==([110] if cancel or journal_error or changed_name else [110,210])
def test_unknown_pending_add_does_not_send_cleanup_dialog_actions(tmp_path, monkeypatch):
+6 -6
View File
@@ -63,7 +63,7 @@ def test_fixture_add_authority_requires_live_lease_and_fresh_staging(tmp_path, m
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 = tmp_path/'original/Block.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)
@@ -71,7 +71,7 @@ def test_fixture_add_authority_requires_live_lease_and_fresh_staging(tmp_path, m
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
assert staged == root/'source/Block.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'
@@ -114,7 +114,7 @@ def test_owned_fixture_retains_default_caller_lease_until_native_release(tmp_pat
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy, blocker_path, _owners, _guard
library = tmp_path/'native-add/fixture/mentor_lib.lmc'
library.parent.mkdir(parents=True); library.write_bytes(b'fixture')
source = tmp_path/'original/Source.prj'; source.parent.mkdir(); source.write_bytes(b'source')
source = tmp_path/'original/Block.prj'; source.parent.mkdir(); source.write_bytes(b'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')})
@@ -156,7 +156,7 @@ def test_fixture_nested_inside_source_tree_rejected_before_staging(tmp_path):
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'fixture')
source = tmp_path/'native-add/Source.prj'; source.write_bytes(b'original source')
source = tmp_path/'native-add/Block.prj'; source.write_bytes(b'original source')
(tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8')
root = tmp_path/'add'; root.mkdir()
with LibraryLock(library):
@@ -217,7 +217,7 @@ def test_public_crud_scope_refuses_unproven_flow_or_partition(tmp_path, operatio
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')
source = tmp_path/'Block.prj'; source.write_bytes(b'source')
library = tmp_path/'library.lmc'; library.write_bytes(b'library')
seen = {}
adapter = native.NativeManagementAdapter(mentor_home=tmp_path)
@@ -244,7 +244,7 @@ def test_owned_fixture_add_passes_existing_library_and_source_to_worker(tmp_path
def test_native_add_identity_mismatch_is_native_success_with_evidence(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')
source = tmp_path/'Block.prj'; source.write_bytes(b'source')
library = tmp_path/'library.lmc'; library.write_bytes(b'library')
adapter = native.NativeManagementAdapter(mentor_home=tmp_path)
monkeypatch.setattr(adapter, 'route_capabilities', lambda: {