#!/usr/bin/env python3 """At UEFI, verify idempotency, stale inputs, real directory I/O and missing EFI.""" import argparse import asyncio import datetime import json from pathlib import Path import uuid import aiohttp class Client: def __init__(self, ws): self.ws, self.state = ws, None async def call(self, method, params): rid = uuid.uuid4().hex await self.ws.send_json({'v': 1, 'id': rid, 'method': method, 'params': params}) async with asyncio.timeout(30): while True: frame = await self.ws.receive_json() if frame.get('event') == 'state': self.state = frame['result'] if frame.get('id') == rid: if not frame.get('ok'): raise RuntimeError(str(frame.get('error'))) return frame['result'] async def command(self, payload): result = await self.call('StageAction', payload) async with asyncio.timeout(60): while result['state'] not in ('succeeded', 'failed', 'interrupted'): await asyncio.sleep(.2) result = await self.call('GetCommand', {'command_id': payload['command_id']}) return result async def station(self): value = await self.call('GetState', {}) return next(x for x in value['stations'] if x['station_id'] == 'S1') async def main(): parser = argparse.ArgumentParser() parser.add_argument('--host', default='192.168.2.79:8080') parser.add_argument('--output', required=True) args = parser.parse_args() report = {'observed_utc': datetime.datetime.now(datetime.timezone.utc).isoformat(), 'checks': [], 'passed': False, 'memory_verdict': 'NOT_TESTED'} def check(name, value, detail): report['checks'].append({'name': name, 'passed': bool(value), 'detail': detail}) print(json.dumps({'name': name, 'passed': bool(value)}, ensure_ascii=False), flush=True) if not value: raise AssertionError(name) try: async with aiohttp.ClientSession() as session: async with session.ws_connect('http://' + args.host + '/ws') as full_ws, \ session.ws_connect('http://' + args.host + '/ws?view=qt-clean') as clean_ws: full, clean = Client(full_ws), Client(clean_ws) info = await full.call('GetSystemInfo', {}) report['system'] = info state = await clean.station() check('actual_uefi_ready', state['input']['ready'] and state['input']['stage'] == 'uefi', {'phase': state['phase'], 'boot_id': state['boot_id']}) queue = await full.call('GetQueue', {'station_id': 'S1', 'limit': 100}) started = next(x for x in queue['items'] if x['method'] == 'QtStartTest' and x['state'] == 'succeeded') boot = state['boot_id'] duplicate = await clean.call('StartTest', {'station_id': 'S1', 'command_id': started['command_id']}) check('duplicate_real_start_does_not_reboot', duplicate['state'] == 'succeeded' and duplicate['result']['boot_id'] == boot and (await clean.station())['boot_id'] == boot, duplicate) old = next(x for x in queue['items'] if x['method'] == 'QtStageAction' and x['arguments']['action'] == 'continue_uip') stale = await clean.command({'station_id': 'S1', 'command_id': 'stale-' + uuid.uuid4().hex, 'input_id': old['arguments']['input_id'], 'action': 'continue_uip'}) check('stale_real_prompt_rejected', stale['state'] == 'failed' and stale['error']['code'] == 'FAILED_PRECONDITION', stale) state = await clean.station() listing = await clean.command({'station_id': 'S1', 'command_id': 'listing-' + uuid.uuid4().hex, 'input_id': state['input']['id'], 'action': 'list_uefi_files', 'path': 'FS0:\\EFI'}) check('real_uefi_directory_read', listing['state'] == 'succeeded' and isinstance(listing['result'].get('files'), list), listing) state = await clean.station() missing = 'FS0:\\EFI\\__qt_missing_' + uuid.uuid4().hex + '.efi' mt = await clean.command({'station_id': 'S1', 'command_id': 'missing-' + uuid.uuid4().hex, 'input_id': state['input']['id'], 'action': 'start_mt', 'mt': {'efi_path': missing}}) check('missing_efi_is_explicit_failure', mt['state'] == 'failed' and mt['error']['code'] == 'FAILED_PRECONDITION', mt) state = await clean.station() check('no_memory_pass_after_missing_efi', state['verdict'] != 'pass', {'verdict': state['verdict'], 'run_state': state['run_state']}) # A harmless subsequent directory query proves that errors do not # corrupt the connection, while leaving the host at its Shell. listing2 = await clean.command({'station_id': 'S1', 'command_id': 'listing-' + uuid.uuid4().hex, 'input_id': state['input']['id'], 'action': 'list_uefi_files', 'path': 'FS0:\\EFI'}) check('link_usable_after_rejected_efi', listing2['state'] == 'succeeded', listing2) report['passed'] = True except Exception as exc: report['error'] = type(exc).__name__ + ': ' + str(exc) print(report['error'], flush=True) finally: target = Path(args.output) target.parent.mkdir(parents=True, exist_ok=True) target.write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n') return 0 if report['passed'] else 1 if __name__ == '__main__': raise SystemExit(asyncio.run(main()))