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

152 lines
6.5 KiB
Python

"""Independent expected payloads for mode 77, AUCTION_PERSONAL.
Inputs cover the original ordinary-item DTO (v1) and the commodity extension (v2). Expected bytes follow Trinity's
AuctionHousePackets.cpp, ItemPacketsCommon.cpp and ObjectGuid.cpp. The original
listing has no item GUID, owner account or absolute end time, so both list types
are expected to use the gateway's documented server-info censorship.
"""
from struct import pack
from typing import NamedTuple, Optional
class Row(NamedTuple):
item: int
level: int
auction_id: int
charges: int
remaining_ms: int
owner: bytes
minimum: Optional[int] = None
increment: Optional[int] = None
buyout: Optional[int] = None
bidder: Optional[bytes] = None
bid: Optional[int] = None
enchant: int = 0
def _bits(*fields):
"""Pack (value, width) pairs MSB-first and pad the last byte on the right."""
encoded = 0
size = 0
for value, width in fields:
if not 0 <= value < (1 << width):
raise ValueError("value does not fit wire field")
encoded = (encoded << width) | value
size += width
return (encoded << (-size % 8)).to_bytes((size + 7) // 8, "big")
def _packed_guid(raw):
assert len(raw) == 16
mask = sum(1 << i for i, octet in enumerate(raw) if octet)
return pack("<H", mask) + bytes(octet for octet in raw if octet)
def _input(owned, rows):
data = bytearray(pack("<BBI", 1, 1 if owned else 2, len(rows)))
for row in rows:
data += pack("<IH", row.item, row.level)
data += pack("<6I", row.item, 1, row.charges & 0xFFFFFFFF,
row.auction_id, row.remaining_ms, row.enchant)
data += row.owner
prices = (row.minimum, row.increment, row.buyout)
marked = sum(1 << i for i, price in enumerate(prices) if price is not None)
data += bytes([marked | (8 if row.bidder is not None else 0)])
for price in prices:
if price is not None:
data += pack("<Q", price)
if row.bidder is not None:
data += row.bidder + pack("<Q", row.bid)
return bytes(data)
def _wire_row(row):
bidder_present = row.bidder is not None
assert bidder_present == (row.bid is not None)
# Item / enchant count / gem count; four prices; server and bid censors;
# bucket and creator presence; bidder and amount presence (uncensored).
data = bytearray(_bits(
(1, 1), (int(bool(row.enchant)), 4), (0, 2),
(row.minimum is not None, 1), (row.increment is not None, 1),
(row.buyout is not None, 1), (0, 1),
(1, 1), (0, 1), (1, 1), (0, 1),
(bidder_present, 1), (row.bid is not None, 1),
))
if bidder_present:
data += _packed_guid(row.bidder)
data += pack("<Q", row.bid)
data += pack("<4i", 1, row.charges, 0, row.auction_id)
data += _packed_guid(row.owner)
data += pack("<iBI", row.remaining_ms, 0, 0)
# Native ItemInstance: entry, zero modifications, absent bonuses.
data += pack("<iBB", row.item, 0, 0)
if row.enchant:
data += pack("<iIiB", row.enchant, 0, 0, 0)
for price in (row.minimum, row.increment, row.buyout):
if price is not None:
data += pack("<Q", price)
# Bucket: 20-bit item, absent species, 11-bit level, absent suffix.
data += _bits((row.item, 20), (0, 1), (row.level, 11), (0, 1))
return bytes(data)
def cases():
"""Yield (gateway input, expected native packet body) for both variants."""
owner = bytes.fromhex("05040000000000000000000000140008")
buyer = bytes.fromhex("00300001000000000000000000140008")
waiting = Row(0x1234, 55, 319, -1, 60000, owner, minimum=90)
active = Row(0x54321, 1053, 16004, 4, 725000, owner, minimum=145,
increment=5, buyout=900, bidder=buyer, bid=211, enchant=313)
# Deliberately exercise the DTO writer above uint32, independent of the
# original auction producer's money cap; this is a serialization case.
wide = Row(0xFFFFF, 0x7FF, 105, 0, 0, buyer, buyout=(1 << 33) + 0x102)
for owned, rows in (
(True, ()), (False, ()),
(True, (waiting,)), (False, (active,)),
(True, (active, waiting, wide)), (False, (waiting, active)),
):
# SoldItems (owned only), DesiredDelay and HasMoreResults are zero.
header = pack("<iiI", len(rows), 0, 0) if owned else pack("<II", len(rows), 0)
native = header + b"".join(_wire_row(row) for row in rows) + bytes([0])
yield _input(owned, rows), native
def commodity_cases():
"""Full original 70334 bidded reply and original writer with the owned censor set.
The synthetic Source posting is five units at 1,200,000 each. Both lists
retain its item instance and bucket key. Only noncommodity postings expose
spell charges and inspected enchantments in Trinity's BuildAuctionItem.
"""
# Pinned original writers at 2372335865a54d7deba6b911e5fca36afcc83b7d.
# The two response headers differ: owned also includes the SoldItems count.
expected_by_kind = {
1: bytes.fromhex("01000000000000000000000080340005000000000000000000000004030201"
"0180420800fa00000000000000393000000000804f120000000000030390110000"),
2: bytes.fromhex("010000000000000080340005000000000000000000000004030201"
"0180420800fa00000000000000393000000000804f120000000000030390110000"),
}
item, level, auction_id = 12345, 17, 0x01020304
for kind, expected in expected_by_kind.items():
header = pack("<BBIIH", 2, kind, 1, item, level)
for charges, enchant in ((0, 0), (7, 313)):
row = pack("<6I", item, 5, charges, auction_id, 64000, enchant)
owner = pack("<QQ", 0x42, 2 << 58)
prices = pack("<BBQ", 0, 1, 1_200_000) # No ordinary price/bidder; unit price supplied.
yield header + row + owner + prices, expected
if __name__ == "__main__":
from native_codec_driver import codec_library_path, require_optional_file, MODES, load, serialize
mode = MODES.index("--auction-personal")
assert mode == 77 and MODES[mode - 1] == "--pet-stable"
require_optional_file(codec_library_path())
codec = load()
for dto, expected in cases():
assert serialize(codec, mode, dto) == expected
for dto, expected in commodity_cases():
actual = serialize(codec, mode, dto)
if actual != expected:
raise AssertionError(f"mode 77 v2 mismatch for {dto.hex()}: {actual.hex()} != {expected.hex()}")
print("PASS personal auction writers, ordinary and commodity")