68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Actual70334 chosen/known title field goldens against original UF writers.
|
|
|
|
The masks, widths and field order come from the pinned native readers, not
|
|
from importing the codec implementation. No title is granted by this fixture.
|
|
"""
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
|
|
codec = sys.argv[1]
|
|
pack = struct.pack
|
|
|
|
|
|
def bits(value):
|
|
value += "0" * (-len(value) % 8)
|
|
return int(value, 2).to_bytes(len(value) // 8, "big")
|
|
|
|
|
|
def words_for(masks):
|
|
words = [0] * 5
|
|
for mask in masks:
|
|
words[mask // 64] |= 1 << (mask % 64)
|
|
return words
|
|
|
|
|
|
def dto(chosen, words, realm=70, map_id=0, owner=4509):
|
|
return pack("<HIQi5Q", realm, map_id, owner, chosen, *words)
|
|
|
|
|
|
def golden(chosen, words):
|
|
# Player: 11 block-presence bits, group0 + native field27, then the
|
|
# original noQuestLogChangesMask=false bit. Chosen title is signed32.
|
|
player = bits(f"{1:011b}{(1 << 27) | 1:032b}" + "0") + pack("<i", chosen)
|
|
# ActivePlayer: 14 block-presence bits, group0 + field7; dynamic count32
|
|
# followed by five changed bits. Every actual word is uint64, including0.
|
|
active = bits(f"{1:014b}{0x81:032b}{5:032b}" + "11111") + pack("<5Q", *words)
|
|
values = b"\x01\x00\x03" + pack("<I", 0xC0) + player + active
|
|
guid = pack("<QQ", 4509, (2 << 58) | (70 << 42))
|
|
packed_guid = pack("<H", sum(1 << i for i, byte in enumerate(guid) if byte))
|
|
packed_guid += bytes(byte for byte in guid if byte)
|
|
block = b"\x00" + packed_guid + pack("<I", len(values)) + values
|
|
return pack("<HIBI", 0, 1, 0x80, len(block)) + block
|
|
|
|
|
|
def run(data):
|
|
return subprocess.run([codec, "--title-values"], input=data, capture_output=True, timeout=15)
|
|
|
|
|
|
cases = [(0, words_for([]))]
|
|
cases += [(mask, words_for([mask])) for mask in (201, 255, 256, 294)]
|
|
cases += [(248, words_for(range(201, 295))), (0, words_for([248]))]
|
|
for chosen, words in cases:
|
|
result = run(dto(chosen, words))
|
|
assert result.returncode == 0, result.stderr.decode(errors="replace")
|
|
assert result.stdout == golden(chosen, words), (chosen, result.stdout.hex())
|
|
|
|
valid = dto(248, words_for([248]))
|
|
invalid = [valid[:size] for size in (0, 13, len(valid) - 1)] + [valid + b"\x00"]
|
|
invalid += [dto(value, words_for([248])) for value in (-1, 111, 20048, 201)]
|
|
invalid += [dto(0, words_for([mask])) for mask in (0, 200, 295, 319)]
|
|
invalid += [dto(0, [0] * 5, realm=0), dto(0, [0] * 5, map_id=65536)]
|
|
invalid += [dto(0, [0] * 5, owner=value) for value in (0, 1 << 32)]
|
|
for data in invalid:
|
|
assert run(data).returncode != 0
|
|
|
|
print("PASS original70334 chosen27/known7 fields, exact masks/32-bit count/64-bit words, clear/revocation and identity/earned bounds")
|