tools/client/verify_linux_game.py (6874B)
1 """Run the isolated Bronto Burger input/capture check from Windows.""" 2 import hashlib 3 import json 4 import os 5 import pathlib 6 import re 7 import socket 8 import subprocess 9 import sys 10 import time 11 import urllib.request 12 13 14 root = pathlib.Path(__file__).resolve().parents[2] 15 sys.path.insert(0, str(root)) 16 import tools.client.server_fixture 17 work = root / 'local/client' 18 client = root / 'anarchy-assets-14.9.1' 19 fixture = work / 'linux-server-probe' 20 output = work / 'linux-game-smoke' 21 22 23 def run_linux(): 24 # Refuse executable content, including plug-ins with non-DLL extensions. 25 assets = [path for path in client.rglob('*') if path.is_file()] 26 for path in assets: 27 assert not path.is_symlink(), path 28 assert path.suffix.lower() not in ['.exe', '.dll', '.com', '.asi', '.flt', '.m3d'], path 29 with path.open('rb') as file: 30 magic = file.read(4) 31 assert magic[:2] != b'MZ' and magic != b'\x7fELF', path 32 33 # Refuse to share ports with a running development server. 34 for port in [7510, 7511, 8081]: 35 with socket.socket() as connection: 36 assert connection.connect_ex(('127.0.0.1', port)) != 0, 'Server port already in use: %d' % port 37 38 tools.client.server_fixture.create_player(fixture, 'Linux Smoke', [0, 0.9238795, 0, -0.3826834]) 39 (fixture / 'logs').mkdir(exist_ok=True) 40 (fixture / 'logs/server.log').write_text('') 41 with (output / 'server.log').open('w') as log: 42 server = subprocess.Popen([str(fixture / 'server'), '-trace'], cwd=fixture, 43 stdout=log, stderr=subprocess.STDOUT) 44 try: 45 deadline = time.monotonic() + 10 46 ready = False 47 while server.poll() is None and time.monotonic() < deadline: 48 with socket.socket() as connection: 49 ready = connection.connect_ex(('127.0.0.1', 8081)) == 0 50 if ready: 51 break 52 time.sleep(.1) 53 assert ready, 'Isolated server did not become ready; inspect server.log' 54 tools.client.server_fixture.admin_opener(fixture).open('http://127.0.0.1:8081/admin/time', b'hour=13&minute=30', timeout=5).read() 55 56 # GDB injects SDL events and reads completed game frames before desktop clipping. 57 environment = os.environ.copy() 58 environment.update(GALLIUM_DRIVER='d3d12', ANARCHY_CD='cd_image', ANARCHY_SETUP='Setupf', ANARCHY='.') 59 subprocess.run([ 60 'timeout', '--signal=INT', '--kill-after=5s', '180s', 'gdb', '-nx', '-batch', 61 '-x', str(root / 'tools/client/linux_game_smoke.gdb.py'), '--args', 62 str(root / 'client/build/linux/game'), 'localtest', 'localtest', 63 '73', '2130706433', '7511', '0', '0', 64 ], cwd=client, env=environment, timeout=190, check=True) 65 opened = json.loads((output / 'opened-files.json').read_text()) 66 outside_assets = [name for name in opened if pathlib.Path(name).is_relative_to(root) 67 and not pathlib.Path(name).is_relative_to(client) 68 and pathlib.Path(name) != root / 'client/build/linux/game'] 69 assert not outside_assets, outside_assets 70 print('Asset audit: %d files, no Windows binaries; no other repository files opened during traced execution.' % len(assets), flush=True) 71 finally: 72 if server.poll() is None: 73 server.terminate() 74 server.wait(timeout=5) 75 print('Isolated Linux server stopped.', flush=True) 76 77 78 def run_windows(): 79 import PIL.Image 80 81 for port in [7510, 7511, 8081]: 82 with socket.socket() as connection: 83 assert connection.connect_ex(('127.0.0.1', port)) != 0, 'Server port already in use: %d' % port 84 85 # Preserve the isolated character, preferences and client diagnostic files. 86 preferences = client / 'Prefs' 87 backups = {path: path.read_bytes() for path in preferences.rglob('*') if path.is_file()} 88 for name in ['stderr.txt', 'DXDiagOutput.txt', 'AnarchyCrashLog.txt']: 89 path = client / name 90 backups[path] = path.read_bytes() if path.exists() else None 91 database = fixture / 'data/world.sqlite' 92 backups[database] = database.read_bytes() if database.exists() else None 93 output.mkdir(exist_ok=True) 94 for path in output.glob('*.json'): 95 path.unlink() 96 try: 97 linux_root = subprocess.check_output(['wsl', '--exec', 'wslpath', str(root)], text=True).strip() 98 with (output / 'client.log').open('w') as log: 99 subprocess.run([ 100 'wsl', '--cd', linux_root, 101 '--exec', 'python3', 'tools/client/verify_linux_game.py', 102 ], stdout=log, stderr=subprocess.STDOUT, timeout=205, check=True) 103 if (client / 'stderr.txt').exists(): 104 (output / 'stderr.txt').write_bytes((client / 'stderr.txt').read_bytes()) 105 finally: 106 for path in preferences.rglob('*'): 107 if path.is_file() and path not in backups: 108 path.unlink() 109 for path, data in backups.items(): 110 if data is None: 111 path.unlink(missing_ok=True) 112 else: 113 path.parent.mkdir(parents=True, exist_ok=True) 114 path.write_bytes(data) 115 116 # Completion and movement are automatic checks; inspect the six PNGs for visual correctness. 117 log = (output / 'client.log').read_text() 118 assert 'Smoke inputs complete at frame 700.' in log, log[-4000:] 119 positions = re.findall(r'Character 73 action=([12]) position=\[([^]]+)\]', 120 (fixture / 'logs/server.log').read_text()) 121 starts = [position for action, position in positions if action == '1'] 122 stops = [position for action, position in positions if action == '2'] 123 assert starts and stops and starts[0] != stops[-1], positions 124 for label in ['baseline', 'inventory-open', 'inventory-closed', 'map-open', 'map-closed', 'moved']: 125 size = json.loads((output / (label + '.json')).read_text()) 126 pixels = (output / (label + '.rgba')).read_bytes() 127 PIL.Image.frombytes('RGBA', tuple(size), pixels).convert('RGB').save(output / (label + '.png')) 128 with (root / 'client/build/linux/game').open('rb') as executable: 129 executable_hash = hashlib.file_digest(executable, 'sha256').hexdigest() 130 report = { 131 'frames': 700, 'movement_start': starts[0], 'movement_stop': stops[-1], 132 'executable_sha256': executable_hash, 133 'visual_review_required': True, 134 'file_audit_excludes_debugger_calls': True, 135 } 136 (output / 'report.json').write_text(json.dumps(report, indent=2)) 137 print(json.dumps(report, indent=2)) 138 print('Inspect screenshots in', output) 139 140 141 if __name__ == '__main__': 142 if os.name == 'nt': 143 run_windows() 144 else: 145 run_linux()