"""Exercise Windows across real processes; thread-desktop mocks miss access denial.""" import json import sys import time from uuid import uuid4 import pytest @pytest.mark.parametrize('thread_access_denied', [False, True]) def test_foreign_private_desktop_window_is_readable(tmp_path, monkeypatch, thread_access_denied): import win32con import win32gui from cbb_refresh_verify.desktop_worker import DesktopWorker from cbb_refresh_verify.residual_sessions import _process_windows from cbb_refresh_verify import residual_sessions if thread_access_denied: def denied(_): raise PermissionError(5, 'GetThreadDesktop access denied') monkeypatch.setattr(residual_sessions, '_thread_desktop_name', denied) ready = tmp_path/'ready.json' child = tmp_path/'window.py' child.write_text(''' import json,sys,os from pathlib import Path import win32api,win32con,win32gui def procedure(hwnd,message,wparam,lparam): if message==win32con.WM_CLOSE: win32gui.DestroyWindow(hwnd); return 0 if message==win32con.WM_DESTROY: win32gui.PostQuitMessage(0); return 0 return win32gui.DefWindowProc(hwnd,message,wparam,lparam) window=win32gui.WNDCLASS() window.hInstance=win32api.GetModuleHandle(None) window.lpszClassName='CbbPrivateDesktopTest' window.lpfnWndProc=procedure win32gui.RegisterClass(window) hwnd=win32gui.CreateWindow(window.lpszClassName,'CBB private desktop discovery', win32con.WS_OVERLAPPEDWINDOW|win32con.WS_VISIBLE,0,0,300,150,0,0,window.hInstance,None) Path(sys.argv[1]).write_text(json.dumps({'pid':os.getpid(),'hwnd':hwnd}),'utf-8') win32gui.PumpMessages() ''', encoding='utf-8') name = 'CbbDiscoveryTest_'+uuid4().hex[:10] worker = DesktopWorker([sys.executable, str(child), str(ready)], name) row = None try: deadline = time.monotonic()+10 while not ready.exists() and worker.poll() is None and time.monotonic()