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

167 lines
7.9 KiB
Python

#!/usr/bin/env python3
"""Independent original70334 loot-writer parity; no live loot/session claim.
--loot-event is neutral context plus genuine source payload, not a public wire
format. Context cannot establish server authorization or native client support.
Method/threshold and money modifier/sole-looter require independent evidence.
"""
import struct
import os
import subprocess
import sys
from pathlib import Path
from native_codec_driver import codec_library_path, require_optional_file, load, serialize, MODES
ROOT = Path(__file__).resolve().parents[1]
library = None
if len(sys.argv) > 1 and Path(sys.argv[1]).resolve() == ROOT / 'tests/native_codec_driver.py':
require_optional_file(codec_library_path())
library = load()
P = struct.pack
SOURCE_OWNER = 0xF13000000600119D
def guid(kind, counter, realm=70, map_id=0, entry=0):
return P('<QQ', counter, (kind << 58) | (realm << 42) |
(map_id << 29) | (entry << 6))
def packed(raw):
return P('<H', sum((b != 0) << i for i, b in enumerate(raw))) + bytes(b for b in raw if b)
def run(data):
if library is not None:
# The registered fixture exercises the C ABI. Reuse its loaded library
# for the full malformed-prefix matrix, rather than launch Python and
# dlopen the same library once per packet under parallel CTest load.
try:
output = serialize(library, MODES.index('--loot-event'), bytes(data),
1 if 'GATEWAY_TALENT_EXPERIMENT' in os.environ else 0)
return subprocess.CompletedProcess(sys.argv[1:], 0, output, b'')
except ValueError:
return subprocess.CompletedProcess(sys.argv[1:], 1, b'', b'Bounded serializer rejected packet')
return subprocess.run([sys.argv[1], '--loot-event'], input=data,
capture_output=True, timeout=15)
def rejects(data):
assert run(data).returncode != 0, 'Malformed or unsupported loot context accepted'
def boundaries(data):
for length in range(len(data)):
rejects(data[:length])
rejects(data + b'\0')
owner = guid(8, 4509, entry=6)
obj = guid(15, 9, map_id=5) # Explicit adapter presentation identity, not source wire GUID.
self_guid = guid(2, 4509)
empty = bytes(16)
rows = [(0, 25, 2, 100, 0), (2, 0xffffffff, 0xffffffff, 0xffffffff, 1),
(5, 26, 1, 0, 2), (9, 27, 3, 200, 3), (15, 28, 1, 300, 4)]
def source_response(items=rows, typ=1, money=0xffffffff):
return P('<QBIB', SOURCE_OWNER, typ, money, len(items)) + b''.join(
P('<BIIIIiB', slot, entry, count, display, 0, 0, ui)
for slot, entry, count, display, ui in items)
def context(event, method=0, threshold=2, loot_obj=obj, world_owner=owner):
return bytes([0]) + P('<Q', SOURCE_OWNER) + world_owner + loot_obj + bytes([method, threshold]) + event
def expected_response(items=rows, typ=1, money=0xffffffff, method=0, threshold=2):
# Original LootItemData writer: Type Bits2=Item0, UI Bits3,
# CanTrade Bits1=false; flush; quantity32, ItemType8=0, slot8,
# ItemInstance entry32 + zero seven-bit modifier list + absent bonus bit.
native_ui = {0: 0, 1: 1, 2: 3, 3: 2, 4: 4}
body = packed(owner) + packed(obj) + bytes([17, typ, method, threshold])
body += P('<III', money, len(items), 0)
for slot, entry, count, display, ui in items:
body += bytes([native_ui[ui] << 3]) + P('<IBBI', count, 0, slot, entry) + bytes(2)
return body + bytes([0x80]) # Acquired=true; AE/SuppressError=false.
normal = context(source_response())
for method in range(5):
for threshold in range(7):
result = run(context(source_response(), method, threshold))
assert result.returncode == 0, result.stderr.decode(errors='replace')
assert result.stdout == expected_response(method=method, threshold=threshold), \
'Original loot response field order, native UI enum, ItemInstance or required metadata differ'
for typ in range(1, 5):
result = run(context(source_response([], typ, 0)))
assert result.returncode == 0 and result.stdout == expected_response([], typ, 0)
boundaries(normal)
# Real source conditional repetitions normalize before the original native
# writer: the count and item payload occur once, with the final source UI.
for prior, final in ((0,0),(0,1),(1,0),(2,0),(2,2)):
repeated = source_response([(2,25,3,100,prior),(2,25,3,100,final)])
result = run(context(repeated))
assert result.returncode == 0 and result.stdout == expected_response([(2,25,3,100,final)])
# Match all named source/native error meanings, explicitly including failure0.
for error in (0, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16):
source = P('<QBB', SOURCE_OWNER, 0, error)
result = run(context(source, 4, 6, empty))
# Original Player::SendLootError keeps default method0/threshold2 even when
# normal-response context differs. Failure is Acquired=false, not code0.
expected = packed(owner) + packed(empty) + bytes([error, 0, 0, 2]) + bytes(12) + bytes([0])
assert result.returncode == 0 and result.stdout == expected, \
'Source error meaning or original native failure defaults changed'
boundaries(context(P('<QBB', SOURCE_OWNER, 0, 0), loot_obj=empty))
removed = bytes([1]) + owner + obj + bytes([15])
coin = bytes([2]) + obj
release = bytes([3]) + P('<Q', SOURCE_OWNER) + owner + self_guid + P('<QB', SOURCE_OWNER, 1)
notify = bytes([4]) + P('<QB', 0xffffffffffffffff, 1) + P('<I', 0xffffffff)
for data, expected in ((removed, packed(owner) + packed(obj) + bytes([15])),
(coin, packed(obj)),
(release, packed(owner) + packed(self_guid)),
(notify, P('<QQB', 0xffffffff, 0xffffffffffffffff, 0x80))):
result = run(data)
assert result.returncode == 0 and result.stdout == expected, \
'Original removed/coin/release/money writer context or order differs'
boundaries(data)
assert run(bytes([4]) + P('<QBI', 0, 0, 0)).stdout == bytes(17)
# Source/context identity and semantic restrictions are checked before output.
rejects(context(source_response(), loot_obj=empty))
rejects(context(source_response(), method=5))
rejects(context(source_response(), threshold=7))
wrong_owner = bytearray(normal);wrong_owner[1] ^= 1;rejects(wrong_owner)
wrong_release = bytearray(release);wrong_release[1] ^= 1;rejects(wrong_release)
for bad in (empty, guid(15, 9), guid(47, 1)):
rejects(context(source_response(), world_owner=bad))
rejects(bytes([1]) + bad + obj + bytes([0]))
for bad in (empty, owner, guid(15, 0)):
rejects(context(source_response(), loot_obj=bad))
rejects(bytes([1]) + owner + bad + bytes([0]))
rejects(bytes([2]) + bad)
rejects(bytes([3]) + P('<Q', SOURCE_OWNER) + owner + owner + P('<QB', SOURCE_OWNER, 1))
rejects(bytes([1]) + owner + obj + bytes([16]))
rejects(bytes([2]) + obj + bytes([0]))
rejects(bytes([4]) + P('<QBI', 0, 2, 0))
for bad_kind in (5, 255):
rejects(bytes([bad_kind]))
duplicate = source_response([(2, 25, 1, 100, 0), (2, 26, 1, 100, 0)])
rejects(context(duplicate))
for prop in (-1, -0x80000000, 1, 0x7fffffff):
random_item = P('<QBIBBIIIIiB', SOURCE_OWNER, 1, 0, 1, 0, 25, 1, 100, 0, prop, 0)
if prop < 0:
rejects(context(random_item)) # Random suffix factors remain held.
continue
# Unmapped positive RandomProperties: base item, never an unrelated bonus.
result = run(context(random_item))
assert result.returncode == 0 and result.stdout == expected_response([(0, 25, 1, 100, 0)], 1, 0), \
'Unmapped random property must present the base item without a bonus list'
bad_placeholder = bytearray(source_response([(0, 25, 1, 100, 0)]))
bad_placeholder[27] = 1;rejects(context(bad_placeholder))
print('Original loot writer parity:35 explicit method/threshold contexts, all UI types, '
'13 genuine failures, empty contents, separate world/LootObj identities, '
'release semantics, coin/money and strict malformed/random-property isolation; '
'no real-client loot acceptance claim.')