Files
dxdesigner-part-replacement/tests/v1_1/test_desktop_diagnostics.py
T

75 lines
3.8 KiB
Python

"""Desktop diagnosis must find threads without windows and leave their owner alone."""
import json
import os
from pathlib import Path
import subprocess
import sys
from uuid import uuid4
import pytest
from dx_productivity.trial_task_center.isolated_desktop import PrivateDesktop, _Win32Api, build_startup_info
@pytest.mark.skipif(os.name != 'nt', reason='Windows desktop diagnosis')
def test_diagnostic_finds_windowless_desktop_thread_without_changing_owner(tmp_path):
from dx_productivity.trial_task_center.cad_identity import current_desktop_name
before = current_desktop_name()
desktop = PrivateDesktop('dx-diagnostic-' + uuid4().hex, enabled=True)
desktop.__enter__()
# Explicitly connect a thread without creating any windows.
child = _Win32Api().create_process((sys.executable, '-u', '-c',
'import win32service,time,sys; '
'd=win32service.OpenDesktop(sys.argv[1],0,False,0x10000000);d.SetThreadDesktop(); '
'print("ready",flush=True);time.sleep(30)', desktop.name), build_startup_info(desktop.name).lpDesktop,
None, None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
assert child.stdout.readline() == b'ready\r\n'
record = tmp_path / 'private-desktop.json'
record.write_text(json.dumps({'desktop': desktop.name}))
original = record.read_bytes()
output = tmp_path / 'diagnostic.json'
script = Path(__file__).resolve().parents[2] / 'tools/diagnose_private_desktop.py'
result = subprocess.run((sys.executable, str(script), '--record', str(record), '--output', str(output)),
capture_output=True, timeout=20)
assert result.returncode == 0, result.stderr.decode(errors='replace')
report = json.loads(output.read_text(encoding='utf-8'))
assert any(row['pid'] == child.pid for row in report['thread_scan']['threads'])
assert report['windows'] == [] and report['desktop_still_listed']
assert child.poll() is None and current_desktop_name() == before
assert record.read_bytes() == original
finally:
child.close()
desktop.close()
@pytest.mark.skipif(os.name != 'nt', reason='Windows desktop diagnosis')
def test_no_thread_owner_is_not_mistaken_for_released_desktop(tmp_path):
with PrivateDesktop('dx-diagnostic-' + uuid4().hex, enabled=True) as desktop:
output = tmp_path / 'diagnostic.json'
script = Path(__file__).resolve().parents[2] / 'tools/diagnose_private_desktop.py'
result = subprocess.run((sys.executable, str(script), '--desktop', desktop.name, '--output', str(output)),
capture_output=True, timeout=20)
assert result.returncode == 0, result.stderr.decode(errors='replace')
report = json.loads(output.read_text(encoding='utf-8'))
assert report['thread_scan']['threads'] == []
assert report['desktop_still_listed'] and report['windows'] == []
# A leaked query handle would make this context's strict close fail.
@pytest.mark.skipif(os.name != 'nt', reason='Windows desktop diagnosis')
@pytest.mark.parametrize('hardlink', [False, True])
def test_output_cannot_overwrite_input_record_even_through_hardlink(tmp_path, hardlink):
record = tmp_path / 'private-desktop.json'
original = json.dumps({'desktop': 'dx-no-such-' + uuid4().hex}).encode()
record.write_bytes(original)
output = record
if hardlink:
output = tmp_path / 'diagnostic.json'
os.link(record, output)
script = Path(__file__).resolve().parents[2] / 'tools/diagnose_private_desktop.py'
result = subprocess.run((sys.executable, str(script), '--record', str(record), '--output', str(output)),
capture_output=True, timeout=20)
assert result.returncode != 0
assert record.read_bytes() == original