217 lines
11 KiB
Python
217 lines
11 KiB
Python
"""Exercise the seven additive 70334 SpellAuraRestrictions rows through the host codec."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
from native_codec_driver import MODES, codec_library_path, load, require_optional_file, serialize
|
|
|
|
|
|
TABLE = 0xBA978F4E
|
|
REALM = 0x01000031
|
|
STAMP = 12345
|
|
PUSH = 1294782901
|
|
UNKNOWN_PUSH = 0x7FFFFFFE
|
|
ADVERT = 0x4B0001
|
|
VALID = 0x20
|
|
INVALID = 0x60
|
|
# Reviewed tuples are (new record ID, SpellID, native caster state, native target state).
|
|
REVIEWED = (
|
|
(2600000, 76, 1, 0),
|
|
(2600001, 6174, 0, 2),
|
|
(2600002, 6175, 0, 2),
|
|
(2600003, 6186, 1, 0),
|
|
(2600004, 6568, 1, 0),
|
|
(2600005, 10374, 1, 0),
|
|
(2600006, 45800, 13, 0),
|
|
)
|
|
# CKey ab04f1f0bf3126fb525e39c0bca0a836. The 317 public base IDs and 17
|
|
# native section-index IDs are sufficient to check ownership without row bodies.
|
|
NATIVE_IDS = (
|
|
14246, 14247, 14252, 14253, 14254, 14255, 14257, 14258, 14259, 14260, 14261, 14262,
|
|
14263, 14264, 14265, 14266, 14267, 14268, 14269, 14270, 14271, 14272, 14273, 14274,
|
|
14275, 14276, 14277, 14278, 14279, 14281, 14282, 14284, 17054, 17900, 17902, 17904,
|
|
17906, 17908, 17910, 17912, 17914, 17916, 17920, 17921, 17932, 17933, 17935, 17936,
|
|
17953, 17954, 17955, 17956, 17961, 17964, 17976, 17991, 18033, 18078, 18079, 18516,
|
|
19129, 19180, 19827, 19828, 19829, 19892, 19893, 20035, 20038, 20046, 20051, 20086,
|
|
20087, 20099, 20173, 20196, 20265, 20352, 20387, 20388, 20394, 20398, 20399, 20407,
|
|
20409, 20425, 20445, 20450, 20455, 20458, 20552, 20555, 20556, 20579, 20613, 20645,
|
|
20647, 20648, 20726, 20773, 20788, 20802, 20844, 20883, 20929, 20966, 21025, 21030,
|
|
21038, 21110, 21148, 21169, 21175, 21192, 21202, 21203, 21224, 21229, 21233, 21260,
|
|
21261, 21265, 21269, 21271, 21272, 21273, 21280, 21285, 21289, 21290, 21296, 21298,
|
|
21318, 21319, 21323, 21338, 21339, 21340, 21353, 21360, 21361, 21362, 21372, 21396,
|
|
21397, 21401, 21451, 21532, 21539, 21540, 21573, 21581, 21584, 21587, 21590, 21591,
|
|
21592, 21598, 21599, 21600, 21601, 21610, 21667, 21668, 21670, 21672, 21674, 21676,
|
|
21798, 21914, 21921, 21925, 21950, 21970, 21971, 21982, 21983, 21984, 21985, 21986,
|
|
21987, 21988, 22012, 22013, 22029, 22038, 22145, 22190, 22198, 22223, 22224, 22233,
|
|
22244, 22261, 22293, 22377, 22519, 22558, 22570, 22633, 22634, 22665, 22666, 22671,
|
|
22780, 22856, 22857, 22889, 22894, 22966, 22975, 22976, 22993, 22994, 22995, 22996,
|
|
22997, 22999, 23000, 23004, 23006, 23009, 23010, 23015, 23016, 23017, 23020, 23021,
|
|
23023, 23024, 23026, 23027, 23028, 23029, 23030, 23032, 23035, 23036, 23044, 23104,
|
|
23128, 23130, 23132, 23144, 23205, 23227, 23270, 23271, 23299, 23322, 23341, 23448,
|
|
23505, 23555, 23577, 23580, 23627, 23707, 23793, 23836, 23892, 23955, 23956, 23957,
|
|
23958, 23959, 23960, 23961, 23962, 24040, 24063, 24071, 24072, 24073, 24074, 24173,
|
|
24185, 24186, 24187, 24266, 24272, 24284, 24314, 24318, 24328, 24335, 24387, 24388,
|
|
24454, 24455, 24456, 24457, 24458, 24459, 24608, 24609, 24677, 24722, 24754, 24757,
|
|
24758, 24761, 24787, 24846, 24847, 24866, 24913, 24918, 24920, 24921, 24922, 24923,
|
|
24924, 24925, 24926, 24927, 24928, 24940, 24941, 24942, 24943, 24944, 24945, 24968,
|
|
24969, 24989, 24990, 24991, 25008, 25062, 25102, 25197, 25211, 25260,
|
|
)
|
|
|
|
|
|
def codec_at(path):
|
|
os.environ['GW_TEST_CODEC_PATH'] = str(path.resolve())
|
|
return load()
|
|
|
|
|
|
def init_packet(codec, phase):
|
|
return serialize(codec, MODES.index('--native-client-initialization'),
|
|
struct.pack('<IIQ', REALM, phase, 0))
|
|
|
|
|
|
def initialization(codec):
|
|
bundle = init_packet(codec, 0)
|
|
count, = struct.unpack_from('<I', bundle)
|
|
offset = 4
|
|
frames = []
|
|
listings = []
|
|
for _ in range(count):
|
|
opcode, size = struct.unpack_from('<II', bundle, offset)
|
|
body = bundle[offset + 8:offset + 8 + size]
|
|
assert len(body) == size
|
|
offset += 8 + size
|
|
if opcode == ADVERT:
|
|
realm, total = struct.unpack_from('<II', body)
|
|
assert realm == REALM and size == 8 + 8*total
|
|
listings.append(list(struct.iter_unpack('<II', body[8:])))
|
|
frames.append((opcode, None))
|
|
else:
|
|
frames.append((opcode, body))
|
|
assert offset == len(bundle) and len(listings) == 1
|
|
entries = listings[0]
|
|
assert len({key for key, _ in entries}) == len(entries)
|
|
return frames, entries
|
|
|
|
|
|
def request(codec, pushes):
|
|
data = struct.pack('<III', 70334, 0, len(pushes))
|
|
data += b''.join(struct.pack('<I', p) for p in pushes)
|
|
return serialize(codec, MODES.index('--native-hotfix-reply'), data)
|
|
|
|
|
|
def db_query(codec, table, key):
|
|
return serialize(codec, MODES.index('--native-db-reply-missing'),
|
|
struct.pack('<III', table, key, STAMP))
|
|
|
|
|
|
def expected_query(table, key, status, data=b''):
|
|
return struct.pack('<IIIBI', table, key, STAMP, status, len(data)) + data
|
|
|
|
|
|
def reviewed_payloads():
|
|
# DB2StorageBase::WriteRecord serializes all 14 DB2Meta fields, omitting the
|
|
# noninline ID and including the final noninline parent relation (SpellID).
|
|
layout = struct.Struct('<h8i4hi')
|
|
assert layout.size == 46
|
|
return [(key, layout.pack(0, caster, target, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, spell))
|
|
for key, spell, caster, target in REVIEWED]
|
|
|
|
|
|
def check_source_rows(rows):
|
|
root = Path(__file__).resolve().parents[1]
|
|
path = root / 'docs/evidence/data/spell-aura-restrictions-20261127/reviewed-rows.json'
|
|
fields = ('DifficultyID', 'CasterAuraState', 'TargetAuraState', 'ExcludeCasterAuraState',
|
|
'ExcludeTargetAuraState', 'CasterAuraSpell', 'TargetAuraSpell', 'ExcludeCasterAuraSpell',
|
|
'ExcludeTargetAuraSpell', 'CasterAuraType', 'TargetAuraType', 'ExcludeCasterAuraType',
|
|
'ExcludeTargetAuraType', 'SpellID')
|
|
from_source = [(r['ID'], struct.pack('<h8i4hi', *(r[k] for k in fields)))
|
|
for r in json.loads(path.read_bytes())]
|
|
assert from_source == rows
|
|
|
|
|
|
def check_cache_bake(rows):
|
|
# A LOCAL cache can contain this push before its rows have been baked.
|
|
# A second, baked control push must still be accepted by the generator.
|
|
script = Path(__file__).resolve().parents[1] / 'ops/db2-bake-20261123/baked_hotfix_pushes.py'
|
|
control = 371195158
|
|
def entry(push, unique, ident, payload, table):
|
|
return b'XFTH' + struct.pack('<IiIIiII', 0, push, unique, table, ident, len(payload), 1) + payload
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
directory = Path(tmp)
|
|
digests, cache, header = (directory / name for name in ('digests.txt', 'cache.bin', 'baked.hpp'))
|
|
digests.write_text(f'{control} 1111111111111111\n{PUSH} e6c85265dd6952a5\n')
|
|
for count in (1, len(rows)):
|
|
body = b'XFTH' + struct.pack('<II', 9, 70334) + bytes(32)
|
|
body += entry(control, 1, 1, b'control', 0x46c66698)
|
|
for unique, (key, raw) in enumerate(rows[:count], 1):
|
|
body += entry(PUSH, unique, key, raw, TABLE)
|
|
cache.write_bytes(body)
|
|
subprocess.run([sys.executable, script, digests, cache, header], check=True, capture_output=True)
|
|
result = header.read_text()
|
|
assert f'{{{control}, 0x1111111111111111ull}}' in result
|
|
assert f'{{{PUSH},' not in result
|
|
|
|
|
|
def check(candidate, baseline):
|
|
rows = reviewed_payloads()
|
|
check_source_rows(rows)
|
|
check_cache_bake(rows)
|
|
digest = hashlib.sha256(b''.join(struct.pack('<II', TABLE, key) + raw for key, raw in rows)).digest()
|
|
assert int.from_bytes(digest[:4], 'little') & 0x7fffffff == PUSH
|
|
assert len(set(NATIVE_IDS)) == len(NATIVE_IDS) == 334
|
|
assert hashlib.sha256(struct.pack('<334I', *NATIVE_IDS)).hexdigest() == (
|
|
'0b7dc4b8f584baea441c0eeb2deaa034fa1301731a3a392c1dda7a190744e677')
|
|
assert set(NATIVE_IDS).isdisjoint(key for key, _ in rows)
|
|
|
|
frames, advertised = initialization(candidate)
|
|
assert (PUSH, 1) in advertised, 'required SpellAuraRestrictions push missing from initialization'
|
|
assert all(key != UNKNOWN_PUSH for key, _ in advertised)
|
|
expected_data = b''.join(raw for _, raw in rows)
|
|
meta = b''.join(struct.pack('<IIIIIB', PUSH, unique, TABLE, key, len(raw), VALID)
|
|
for unique, (key, raw) in enumerate(rows, 1))
|
|
expected_push = struct.pack('<I', len(rows)) + meta + struct.pack('<I', len(expected_data)) + expected_data
|
|
assert request(candidate, [PUSH]) == expected_push
|
|
assert request(candidate, [PUSH, UNKNOWN_PUSH, PUSH]) == expected_push
|
|
assert request(candidate, [UNKNOWN_PUSH]) == request(candidate, []) == bytes(8)
|
|
|
|
for key, payload in rows:
|
|
assert db_query(candidate, TABLE, key) == expected_query(TABLE, key, VALID, payload)
|
|
assert db_query(candidate, 0xDEAD0042, key) == expected_query(0xDEAD0042, key, INVALID)
|
|
for key in (0, REVIEWED[0][0]-1, REVIEWED[-1][0]+1, 0x7fffffff):
|
|
assert db_query(candidate, TABLE, key) == expected_query(TABLE, key, INVALID)
|
|
|
|
for key in NATIVE_IDS:
|
|
response = db_query(candidate, TABLE, key)
|
|
assert response == expected_query(TABLE, key, INVALID), f'native-owned record {key} changed'
|
|
if baseline is not None:
|
|
assert response == db_query(baseline, TABLE, key), f'baseline differs for native {key}'
|
|
if baseline is not None:
|
|
old_frames, old_advertised = initialization(baseline)
|
|
assert all(key != PUSH for key, _ in old_advertised)
|
|
assert frames == old_frames
|
|
assert [(key, value) for key, value in advertised if key != PUSH] == old_advertised
|
|
assert init_packet(candidate, 1) == init_packet(baseline, 1)
|
|
assert request(baseline, [PUSH]) == bytes(8)
|
|
for key, _ in rows:
|
|
assert db_query(baseline, TABLE, key) == expected_query(TABLE, key, INVALID)
|
|
for old_push, _ in old_advertised:
|
|
assert request(candidate, [old_push]) == request(baseline, [old_push])
|
|
print('PASS: seven 46-byte rows in initialization, bulk and direct DBReply; '
|
|
'334 native identifiers preserved; duplicate and unknown requests covered.')
|
|
if baseline is not None:
|
|
print('PASS: prior initialization frames, adverts, advertised pushes and all 334 native replies match baseline.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('codec', type=Path, nargs='?', default=codec_library_path())
|
|
parser.add_argument('--baseline', type=Path)
|
|
args = parser.parse_args()
|
|
require_optional_file(args.codec)
|
|
check(codec_at(args.codec), codec_at(args.baseline) if args.baseline else None)
|