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

126 lines
5.7 KiB
Python

"""Native lookup compatibility, pinned 70334 reader RVA 0x1df4d40.
The independent reader below follows the actual zero-table linear search and
cumulative-square probe at 0x1df4f7e..0x1df5040. It does not call the builder's
insertion helper. Crash caller 0x1da0b64 asks for animation 0 and requires an
in-range index; a missing result is 0xffff.
"""
import struct
import json
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
from m2_sequence_hash import native_sequence_hash
def model(ids):
body = bytearray(304)
body[:4] = b'MD20'
struct.pack_into('<I', body, 4, 272)
struct.pack_into('<II', body, 0x1c, len(ids), len(body))
for ident in ids:
row = bytearray(64)
struct.pack_into('<H', row, 0, ident)
body.extend(row)
return bytes(body)
def native_lookup(body, animation):
count, sequences = struct.unpack_from('<II', body, 0x1c)
size, table = struct.unpack_from('<II', body, 0x24)
def ident(index):
if index >= count:
raise AssertionError('native sequence-index assertion')
return struct.unpack_from('<H', body, sequences + index * 64)[0]
if not size:
return next((i for i in range(count) if ident(i) == animation), 0xffff)
slot, step = animation % size, 1
# Finite host guard detects a bad table instead of hanging like the client.
for _ in range(size * 3 + 1):
index = struct.unpack_from('<H', body, table + slot * 2)[0]
if index == 0xffff or ident(index) == animation:
return index
slot = ((slot + step * step) & 0xffffffff) % size
step += 1
raise AssertionError('native sequence lookup does not terminate')
class NativeSequenceHash(unittest.TestCase):
def test_stand_after_collisions_is_reachable(self):
# Three IDs hash to slot 0 in size 7. Native visits 0, 1, then 5.
# Linear insertion puts Stand at 2, so the real caller receives FFFF.
out, _ = native_sequence_hash(model([7, 14, 0]))
self.assertEqual(native_lookup(out, 0), 2)
def test_all_authored_ids_and_first_variation(self):
for ids in ([0], [0, 0], [0, 7, 14], [7, 14, 0],
[11, 22, 33, 0, 1], [148, 146, 147, 149, 0]):
with self.subTest(ids=ids):
original = model(ids)
out, _ = native_sequence_hash(original)
for ident in set(ids):
self.assertEqual(native_lookup(out, ident), ids.index(ident))
self.assertEqual(native_lookup(out, 65534), 0xffff)
self.assertEqual(out[304:304 + 64 * len(ids)], original[304:])
self.assertEqual(native_sequence_hash(out), (out, False))
def test_signed_creature_stand_tables(self):
facts = json.loads((Path(__file__).parent / 'data' / 'm2_sequence_hash_signed_stand76.json').read_text())
for case in facts['cases']:
with self.subTest(fdid=case['fdid'], ckey=case['ckey']):
body = bytearray(model(case['sequence_ids']))
offset = len(body)
body.extend(struct.pack('<%dH' % len(case['table']), *[v & 0xffff for v in case['table']]))
struct.pack_into('<II', body, 0x24, len(case['table']), offset)
# Exact signed table is the failing exhibit, not a generated guess.
self.assertEqual(native_lookup(body, 0), 0xffff)
repaired, _ = native_sequence_hash(body)
for animation in set(case['sequence_ids']):
self.assertEqual(native_lookup(repaired, animation), case['sequence_ids'].index(animation))
self.assertEqual(repaired[304:offset], bytes(body[304:offset]))
def test_insert_stand_rebuilds_a_full_single_animation_hash(self):
from m2_sequence_contract import insert_stand
body = bytearray(model([147]))
struct.pack_into('<I', body, 304 + 4, 1000)
struct.pack_into('<I', body, 304 + 12, 0x20)
struct.pack_into('<hH', body, 304 + 60, -1, 0)
original, _ = native_sequence_hash(body)
# A valid native two-slot hash [-1, 0]. Filling slot zero directly
# leaves no FFFF terminator and hangs absent-animation lookup.
self.assertEqual(native_lookup(original, 65534), 0xffff)
out, audit = insert_stand(original)
self.assertTrue(audit['changed'])
self.assertEqual(native_lookup(out, 0), 1)
self.assertEqual(native_lookup(out, 147), 0)
self.assertEqual(native_lookup(out, 65534), 0xffff)
self.assertEqual(insert_stand(out), (out, {'changed': False}))
def test_insert_stand_accepts_native_hash_collision_at_slot_zero(self):
from m2_sequence_contract import insert_stand
body = bytearray(model([5, 10]))
for index in range(2):
at = 304 + index * 64
struct.pack_into('<I', body, at + 4, 1000)
struct.pack_into('<I', body, at + 12, 0x20)
struct.pack_into('<hH', body, at + 60, -1, index)
original, _ = native_sequence_hash(body)
# Slot zero belongs to animation 5: it is not evidence of Stand.
out, _ = insert_stand(original)
for animation, index in ((5, 0), (10, 1), (0, 2), (65534, 0xffff)):
self.assertEqual(native_lookup(out, animation), index)
def test_missing_stand_is_not_invented(self):
out, _ = native_sequence_hash(model([148, 146, 147, 149]))
self.assertEqual(native_lookup(out, 0), 0xffff)
def test_empty_sequence_table_preserved(self):
out, _ = native_sequence_hash(model([]))
self.assertEqual(native_lookup(out, 0), 0xffff)
self.assertEqual(struct.unpack_from('<II', out, 0x24), (0, 0))
if __name__ == '__main__':
unittest.main()