Files
WoW-Hybrid-gateway/tests/hotfix_baked_audit_test.py

65 lines
3.9 KiB
Python

#!/usr/bin/env python3
import copy
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import struct
import subprocess
import sys
import tempfile
from types import SimpleNamespace
ROOT=Path(__file__).resolve().parents[1]
if len(sys.argv)>1 and sys.argv[1]=='--build':
out=Path(sys.argv[2]).resolve();out.mkdir(parents=True,exist_ok=True)
subprocess.run(['dotnet','build',str(ROOT/'ops/hotfix_baked_audit/Db2Read.csproj'),'-o',str(out),
'-p:BaseIntermediateOutputPath='+str(out/'obj')+'/',
'-p:RestoreConfigFile='+str(ROOT/'ops/hotfix_baked_audit/NuGet.Config'),'-p:NuGetAudit=false',
'--verbosity','quiet','--nologo'],check=True)
raise SystemExit(0)
spec=importlib.util.spec_from_file_location('audit',ROOT/'ops/hotfix_baked_audit.py');audit=importlib.util.module_from_spec(spec);spec.loader.exec_module(audit)
with tempfile.TemporaryDirectory() as directory:
definition=Path(directory)/'Fixture.dbd'
definition.write_text('COLUMNS\nint ID\nfloat Values\nstring Name\nint Flags\nint Parent\n\nLAYOUT 12345678\nBUILD 1.60.1.70334\n$noninline,id$ID<32>\nValues[2]\nName\nFlags<8>\n$noninline,relation$Parent<u32>\n\nBUILD 1.12.1.5875\nObsoleteField<8>\n')
unsafe=SimpleNamespace(output=Path(directory)/'baked/output',baked_dir=Path(directory)/'baked',defs=Path(directory)/'defs',closure=Path(directory)/'closure/manifest.json',package=Path(directory)/'package.json',registry=Path(directory)/'registry.json',reader=Path(directory)/'reader.dll')
try:audit.audit(unsafe)
except ValueError as error:assert 'separate' in str(error)
else:raise AssertionError('audit could write into a source tree')
fields=audit.schema(definition,0x12345678)
# Independently specified wire bytes include negative zero and a noncanonical NaN payload.
body=bytes.fromhex('000000800100c07f')+'Écho'.encode()+b'\0\xff'+struct.pack('<I',17)
row={'ID':4,'Values':[{'f32_bits':0x80000000},{'f32_bits':0x7fc00001}],'Name':'Écho','Flags':-1,'Parent':17}
assert audit.encode(row,fields)==body
payload={'table_hash':'0x123','id':4,'record_status':'valid','wire_hex':body.hex(),'sha256':hashlib.sha256(body).hexdigest()}
assert audit.classify(payload,row,fields)[0]=='identical'
changed=copy.deepcopy(row);changed['Values'][0]['f32_bits']=0
assert audit.classify(payload,changed,fields)[0]=='different','negative zero must not normalize to equal'
assert audit.classify(payload,None,fields)[0]=='missing'
removed={**payload,'record_status':'removed','wire_hex':'','sha256':hashlib.sha256(b'').hexdigest()}
assert audit.classify(removed,None,fields)[0]=='removed_absent'
assert audit.classify(removed,row,fields)[0]=='removed_present'
changed=copy.deepcopy(row);changed['Values']=[0,0]
assert audit.classify(payload,changed,fields)[0]=='uncomparable'
package={'build':'1.60.1.70334','rows':[payload],'pushes':[{'first':0,'count':1,'push_id':7}]}
audit.validate_package(package)
for mutate in (
lambda p:p['rows'][0].update(sha256='0'*64),
lambda p:p['pushes'][0].update(first=1),
lambda p:p['rows'].append(dict(p['rows'][0])),
lambda p:p.update(build='other')):
bad=copy.deepcopy(package);mutate(bad)
try:audit.validate_package(bad)
except ValueError:pass
else:raise AssertionError('invalid package accepted')
try:audit.schema(definition,0x87654321)
except ValueError:pass
else:raise AssertionError('layout drift accepted')
# Exercise the real gate's audit-only entry, which must not call full ninja/CTest.
config=os.environ.get('GW_HOTFIX_AUDIT_CONFIG')
if config:
options=json.loads(Path(config).read_text())
args=[part for key,value in options.items() for part in ('--'+key,value)]
subprocess.run(['bash',str(ROOT/'ops/regression_gate.sh'),'--hotfix-audit-only',*args],check=True)
print('PASS exact floats/strings/arrays/relation fields, removal semantics, package pins and layout drift')