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

38 lines
2.5 KiB
Python

#!/usr/bin/env python3
import hashlib,json,struct,subprocess,sys,tempfile
from pathlib import Path
root=Path(__file__).resolve().parents[1];sys.path.insert(0,str(root/'ops'))
from live_hotfix_rows import decode,decode_db_reply,inventory
subprocess.run([sys.argv[1]],check=True)
def packet(status=1,payload=b'abc'):
return struct.pack('<I',1)+struct.pack('<iIIiIB',-7,19,0x12345678,99,len(payload),status<<5)+struct.pack('<I',len(payload))+payload
row=decode(packet())[0];assert row['push_id']==-7 and row['unique_id']==19 and row['payload_sha256']==hashlib.sha256(b'abc').hexdigest()
assert decode(packet(2,b''))[0]['status']=='removed'
for bad in [packet()[:-1],packet()+b'x',packet(2),bytes(7),struct.pack('<I',999)+bytes(4),packet()[:24]+b'!'+packet()[25:]]:
try:decode(bad)
except ValueError:pass
else:raise AssertionError('Malformed hotfix accepted')
def dbreply(status=1,payload=b'xyz'):
return struct.pack('<IIIBI',0x12345678,99,123,status<<5,len(payload))+payload
assert decode_db_reply(dbreply())[0]['payload_sha256']==hashlib.sha256(b'xyz').hexdigest()
assert decode_db_reply(dbreply(3,b''))[0]['status']=='invalid'
for bad in [dbreply()[:-1],dbreply()+b'x',dbreply(2),bytes(16),dbreply()[:12]+b'!'+dbreply()[13:]]:
try:decode_db_reply(bad)
except ValueError:pass
else:raise AssertionError('Malformed DBReply accepted')
with tempfile.TemporaryDirectory() as directory:
d=Path(directory);capture=d/'capture.jsonl';out=d/'result'
event={'sequence':1,'opcode':0x4b0003,'direction':'native_in','capture_stage':'pe-after-engine-submit','scope':'authenticated-world-transport','bytes':len(packet()),'body_hex':packet().hex()}
capture.write_text(json.dumps(event)+'\n');inventory(capture,out)
assert json.loads((out/'sent-hotfix-summary.json').read_text())['sent_row_occurrences']==1
reply=dict(event,sequence=2,opcode=0x4b0000,bytes=len(dbreply()),body_hex=dbreply().hex())
capture.write_text(json.dumps(event)+'\n'+json.dumps(reply)+'\n');inventory(capture,out)
rows=list(map(json.loads,(out/'sent-hotfix-rows.jsonl').read_text().splitlines()))
assert [r['opcode'] for r in rows]==[0x4b0003,0x4b0000] and rows[1]['timestamp']==123
for change in [{'body_hex':None},{'body_omitted':True},{'capture_stage':'serialized-only'},{'scope':'unauthenticated'}]:
capture.write_text(json.dumps(event|change)+'\n')
try:inventory(capture,out)
except ValueError:pass
else:raise AssertionError('Unproved transmission accepted')
print('PASS submitted hotfix capture framing, status, bounds and exact payload hashes')