119 lines
5.0 KiB
Python
119 lines
5.0 KiB
Python
"""Verify the Windows desktop and CLI input paths in a candidate package."""
|
|
|
|
from contextlib import contextmanager
|
|
import json
|
|
import locale
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from uuid import uuid4
|
|
|
|
import ntsecuritycon
|
|
import pywintypes
|
|
import win32api
|
|
import win32con
|
|
import win32gui
|
|
import win32process
|
|
import win32security
|
|
|
|
|
|
@contextmanager
|
|
def deny_access(path, rights):
|
|
dacl_info = win32security.DACL_SECURITY_INFORMATION
|
|
original = win32security.GetFileSecurity(str(path), dacl_info)
|
|
sid, _, _ = win32security.LookupAccountName(None, win32api.GetUserName())
|
|
acl = win32security.ACL()
|
|
acl.AddAccessDeniedAce(win32security.ACL_REVISION, rights, sid)
|
|
acl.AddAccessAllowedAce(win32security.ACL_REVISION, win32con.GENERIC_ALL, sid)
|
|
restricted = win32security.SECURITY_DESCRIPTOR()
|
|
restricted.SetSecurityDescriptorDacl(True, acl, False)
|
|
win32security.SetFileSecurity(str(path), dacl_info, restricted)
|
|
try:
|
|
yield
|
|
finally:
|
|
win32security.SetFileSecurity(str(path), dacl_info, original)
|
|
|
|
|
|
def cli(exe, library, data_root, expected):
|
|
result = subprocess.run(
|
|
[str(exe), '--batch-lmc', str(library), '--block', 'Fixture', '--data-root', str(data_root)],
|
|
capture_output=True, timeout=15, creationflags=subprocess.CREATE_NO_WINDOW,
|
|
)
|
|
try:
|
|
detail = result.stderr.decode('utf-8')
|
|
except UnicodeDecodeError:
|
|
detail = result.stderr.decode(locale.getencoding())
|
|
assert result.returncode == 1 and expected in detail, (result.returncode, detail)
|
|
assert 'Traceback' not in detail and not (data_root / 'reports').exists()
|
|
return {'exit_code': result.returncode, 'message': detail.strip()}
|
|
|
|
|
|
def startup_dialog(exe, data_root):
|
|
from cbb_refresh_verify.desktop_worker import DesktopWorker
|
|
|
|
worker = DesktopWorker([str(exe), '--data-root', str(data_root)], 'CbbInputSmoke_' + uuid4().hex[:8])
|
|
try:
|
|
deadline = time.monotonic() + 15
|
|
while worker.poll() is None and time.monotonic() < deadline:
|
|
try:
|
|
windows = worker.desktop.EnumDesktopWindows()
|
|
except pywintypes.error as error:
|
|
if error.winerror != 0:
|
|
raise
|
|
windows = ()
|
|
for hwnd in windows:
|
|
if win32process.GetWindowThreadProcessId(hwnd)[1] != worker.pid or not win32gui.IsWindowVisible(hwnd):
|
|
continue
|
|
title = win32gui.GetWindowText(hwnd)
|
|
assert title == 'CBB 刷新校验启动失败', title
|
|
if win32gui.GetClassName(hwnd) != '#32770':
|
|
continue
|
|
children = []
|
|
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child) or True, None)
|
|
button = next((child for child in children if win32gui.GetClassName(child) == 'Button'), None)
|
|
if button is None:
|
|
continue
|
|
texts = [win32gui.GetWindowText(child) for child in children]
|
|
message = next((text for text in texts if 'DATA_ROOT_UNWRITABLE' in text), '')
|
|
assert str(data_root) in message and '无法写入数据目录' in message, texts
|
|
win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0)
|
|
code = worker.wait(5)
|
|
assert code == 1
|
|
return {'exit_code': code, 'title': title, 'message': message}
|
|
time.sleep(0.1)
|
|
raise AssertionError(f'Startup dialog did not appear, exit={worker.poll()}')
|
|
finally:
|
|
if worker.poll() is None:
|
|
win32process.TerminateProcess(worker.handle, 1)
|
|
worker.wait(5)
|
|
worker.close()
|
|
|
|
|
|
def main():
|
|
workspace, executable, evidence_dir = (Path(arg).resolve() for arg in sys.argv[1:])
|
|
sys.path.insert(0, str(workspace / 'src'))
|
|
evidence_dir.mkdir(parents=True, exist_ok=False)
|
|
assert (executable.parent / '使用说明.txt').read_bytes() == (workspace / 'packaging' / '使用说明.txt').read_bytes()
|
|
empty_library = evidence_dir / 'empty.lmc'
|
|
empty_library.touch()
|
|
unreadable_library = evidence_dir / 'unreadable.lmc'
|
|
unreadable_library.write_bytes(b'fixture')
|
|
data_root = evidence_dir / 'data'
|
|
data_root.mkdir()
|
|
blocked_root = evidence_dir / 'readonly'
|
|
blocked_root.mkdir()
|
|
results = {'guide': 'matched', 'empty_lmc': cli(executable, empty_library, data_root, 'INPUT_EMPTY')}
|
|
with deny_access(unreadable_library, ntsecuritycon.FILE_READ_DATA):
|
|
results['unreadable_lmc'] = cli(executable, unreadable_library, data_root, 'INPUT_PERMISSION')
|
|
with deny_access(blocked_root, ntsecuritycon.FILE_ADD_FILE | ntsecuritycon.FILE_ADD_SUBDIRECTORY):
|
|
results['cli_data_root'] = cli(executable, empty_library, blocked_root, 'DATA_ROOT_UNWRITABLE')
|
|
results['gui_data_root'] = startup_dialog(executable, blocked_root)
|
|
summary = json.dumps(results, ensure_ascii=False, indent=2)
|
|
(evidence_dir / 'result.json').write_text(summary + '\n', encoding='utf-8')
|
|
print(summary)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|