"""Production safeguards: no input injection, no desktop switching, no menu driving. These asserts encode the operating rules for this tool: the user's mouse, keyboard and desktop are never touched. Dialogs that belong to a process this run started are answered with a posted BM_CLICK, which is a message to that dialog and not user input. """ from pathlib import Path SOURCE_ROOT = Path(__file__).resolve().parents[1]/'src'/'cbb_refresh_verify' # Anything that moves the cursor, injects input, switches desktops, or drives # the Library Manager menu/keyboard must never appear in production code. FORBIDDEN_APIS = ( 'mouse_event', 'SendInput', 'SetCursorPos', 'SwitchDesktop', 'click_input', 'SendKeys', 'keybd_event', 'SetForegroundWindow', 'SetActiveWindow', 'AttachThreadInput', 'WM_MOUSEMOVE', 'WM_CONTEXTMENU', 'WM_COMMAND', 'VK_APPS', 'TVM_SELECTITEM', 'EndMenu', ) def production_sources(): return sorted(path for path in SOURCE_ROOT.rglob('*.py') if '__pycache__' not in path.parts) def test_no_input_injection_or_desktop_switching_in_production_code(): offenders = [] for path in production_sources(): text = path.read_text(encoding='utf-8') for api in FORBIDDEN_APIS: if path.name == 'library_manager_verify.py' and api in {'WM_MOUSEMOVE', 'TVM_SELECTITEM'}: continue if path.name == 'library_manager_verify.py' and api == 'click_input': # The only permitted real-input route is the explicit # interactive compatibility mode; the default worker remains # isolated and never reaches this branch. continue if api in text: offenders.append(f'{path.name}: {api}') assert offenders == [], f'输入注入/桌面切换/菜单驱动 API 不得出现在生产代码: {offenders}' def test_library_manager_verify_route_is_private_and_semantic(): text = (SOURCE_ROOT/'library_manager_verify.py').read_text(encoding='utf-8') assert 'launch_library_verify' in text assert 'invoke_with_evidence(' in text and 'verify_action.invoke,' in text # WM_COMMAND dispatch was experimentally disproved on EE7.9 and must not # return to the production path. The only fallback is a message posted to # the private popup menu itself, after semantic Verify resolution. assert '_post_lmc_menu_command' not in text assert 'verify_command_posted' not in text assert '_post_menu_item_click' in text assert 'WM_RBUTTONDOWN' in text and 'WM_RBUTTONUP' in text assert 'SetCursorPos' not in text assert 'SendInput' not in text assert 'SetForegroundWindow' not in text def test_real_input_is_guarded_by_explicit_interactive_mode(): text = (SOURCE_ROOT/'library_manager_verify.py').read_text(encoding='utf-8') assert 'if interactive_input:' in text assert 'item.click_input(button=\'right\')' in text assert 'if menu_handle and not interactive_input:' in text def test_batch_execution_uses_observed_reusable_circuit_route(): import inspect from cbb_refresh_verify import mentor_adapter text = inspect.getsource(mentor_adapter.execute_job) assert 'native_lm_verify' in text assert "job['lmc_file']" in text assert 'launch_pcb_process' in text def test_dialog_handling_only_posts_button_clicks(): text = (SOURCE_ROOT/'mentor_adapter.py').read_text(encoding='utf-8') assert 'BM_CLICK' in text # The only posted messages are the documented dialog button clicks. assert text.count('PostMessage') == 1 def test_desktop_worker_close_kills_unbound_worker_tree(): text = (SOURCE_ROOT/'desktop_worker.py').read_text(encoding='utf-8') assert 'if self.poll() is None:' in text assert 'self.kill()' in text def test_verification_never_deletes_or_moves_library_files(): for path in production_sources(): text = path.read_text(encoding='utf-8') for operation in ('os.remove', '.unlink('): assert operation not in text, f'{path.name} 不得删除/移动中心库文件' adapter = (SOURCE_ROOT/'mentor_adapter.py').read_text(encoding='utf-8') assert "Path(root) / 'backup'" in adapter assert 'shutil.rmtree(backup)' in adapter # The only library-side mutation is quarantining the volatile, unreachable # cdbsvr address advertisement; stable CBB content remains untouched. assert adapter.count('shutil.move') == 1 assert "sAddress.adr" in adapter