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

165 lines
9.2 KiB
Python

"""Full 70334 commodity replies from Trinity commit 2372335865a54d7deba6b911e5fca36afcc83b7d.
vectors() and invalid_vectors() also supply the standalone Win64 ABI runner.
The original seven vector lines have SHA256 dc21c3e170def93fe792896e7a2cc83488f8495d06c1f118d38e4bbf733dcec1.
The second invocation of the same writer used delay zero and an uncollected
bucket. Its seven lines have SHA256 e39f04bb88f536e4a19e8bdbd7eb0c72a6820a48af0aa44a8a088fae81d6b9f8.
"""
from struct import pack
import unittest
from auction_browse_serializer_test import cases as browse_cases, TimedReply, packed_guid, serialize_checked
from native_codec_driver import MODES, codec_library_path, optional_file_available, load, serialize
ITEM, LEVEL, DELAY = 12345, 17, 321
PRICE, QUANTITY, AUCTION = 1_200_000_000, 5, 0x01020304
OWNER = pack('<QQ', 0x42, 0x0800000000000000)
BUCKETS, ITEMS, QUOTE = 74, 75, 78
# Literal results of the two original C++ fixture executions.
ORIGINAL = {
'quote-available': 'e0393000004101000000bca06501000000050000003075000000000000',
'quote-unavailable': '003930000041010000',
'one-bucket': '010000004101000000000000010000000303901100050000000c000000008c864700000000000000000400',
'items-by-bucket': '0100000001000000410100000038050000000000000000000000040302010180420800fa00000000000000008c86470000000040030390110005000000',
'items-by-id': '010000000100000041010000003c050000000000000000000000040302010180420800fa00000000000000008c864700000000030390110040030390000005000000',
'confirm-ok': '0000000002000000000000000000000000000000000000000000000000000000000041010000',
'confirm-failed': '0000000002000000150000000000000000000000000000000000000000000000000041010000',
}
ALIGNED = {
'one-bucket': '010000000000000000000000010000000303901100050000000c000000008c864700000000000000000000',
'items-by-bucket': '0100000001000000000000000038050000000000000000000000040302010180420800fa00000000000000008c86470000000040030390110005000000',
'items-by-id': '010000000100000000000000003c050000000000000000000000040302010180420800fa00000000000000008c864700000000030390110040030390000005000000',
'confirm-ok': '0000000002000000000000000000000000000000000000000000000000000000000000000000',
'confirm-failed': '0000000002000000150000000000000000000000000000000000000000000000000000000000',
}
def item_dto(*, by_id=False, item=ITEM, outer_level=None, row_key=None, unit_price=PRICE,
n=1, ext_flags=0, list_type=1, price_mask=0, price_fields=b''):
if outer_level is None:
outer_level = 0 if by_id else LEVEL
header = pack('<BIIBIHBBI', 2, n, QUANTITY * n, 0, item, outer_level, list_type, by_id, n)
flags = ext_flags | int(row_key is not None) | (2 if unit_price is not None else 0)
optional = pack('<IH', *row_key) if row_key is not None else b''
if unit_price is not None:
optional += pack('<Q', unit_price)
rows = []
for index in range(n):
fields = pack('<6I', item, QUANTITY, 0, AUCTION + index, 64000, 0)
rows.append(fields + OWNER + bytes([price_mask]) + price_fields + bytes([flags]) + optional)
return header + b''.join(rows)
def quote_dto(present, *, item=ITEM, total=PRICE * QUANTITY, quantity=QUANTITY, duration=30000):
header = pack('<BiIB', 1, item, DELAY, present)
return header + (pack('<QIq', total, quantity, duration) if present else b'')
def seller_search_reply(old, *, rows=1, row_key=False, max_key=False):
# Keep the pinned original commodity row layout. The reviewed seller
# projection changes only censor bit11 and its declared server-info tail.
row=bytearray(old[12:-10]);row[1]&=~0x10
insertion=len(row)-(5 if row_key else 0)
account=packed_guid(pack('<QQ',0x42,29<<58))
prefix=bytes(row[:insertion])+bytes(2)+account
suffix=bytes(row[insertion:])
result=pack('<III',rows,rows,0);timestamps=[]
for index in range(rows):
owned=bytearray(prefix);owned[14:18]=pack('<I',AUCTION+index)
result+=owned;timestamps.append((len(result),64));result+=bytes(4)+suffix
tail=old[-10:]
if max_key:tail=tail[:1]+bytes.fromhex('fffff7ff00')+tail[-4:]
result+=tail[:-4]+pack('<I',rows*QUANTITY)
return TimedReply(result,timestamps)
def vectors():
for present, label in ((True, 'quote-available'), (False, 'quote-unavailable')):
yield label, QUOTE, quote_dto(present), bytes.fromhex(ORIGINAL[label])
bucket = pack('<BIIBI', 1, 0, 1, 0, 1) + pack('<IHIIQB', ITEM, LEVEL, QUANTITY, 12, PRICE, False)
yield 'one-bucket', BUCKETS, bucket, bytes.fromhex(ALIGNED['one-bucket'])
by_bucket = bytes.fromhex(ALIGNED['items-by-bucket'])
yield 'items-by-bucket', ITEMS, item_dto(), seller_search_reply(by_bucket)
yield 'items-by-id', ITEMS, item_dto(by_id=True, row_key=(ITEM, LEVEL)), seller_search_reply(bytes.fromhex(ALIGNED['items-by-id']),row_key=True)
# Keep the authentic row and change only the count, auction IDs and aggregate tail for a full window.
yield '50-detail-rows', ITEMS, item_dto(n=50), seller_search_reply(by_bucket,rows=50)
# Maximum 20-bit item and 11-bit level, zero species/suffix presence bits.
yield 'max-outer-key', ITEMS, item_dto(item=0xfffff, outer_level=0x7ff), seller_search_reply(by_bucket,max_key=True)
# Ordinary v1 controls built by the independently maintained original browse fixtures.
for index, (mode, dto, expected) in enumerate(browse_cases()):
yield f'unchanged-v1-{index}', MODES.index(mode), dto, expected
def invalid_vectors():
for label, change in [
('version', lambda dto: bytes([3]) + dto[1:]),
('truncated-unit-price', lambda dto: dto[:-1]),
('trailing-byte', lambda dto: dto + bytes(1)),
]:
yield label, ITEMS, change(item_dto())
for label, overrides in [
('out-of-range-outer-id', dict(item=1 << 20)), ('out-of-range-outer-level', dict(outer_level=1 << 11)),
('row-limit', dict(n=51)), ('unknown-list-type', dict(list_type=2)), ('unknown-query-kind', dict(by_id=2)),
('unknown-row-flags', dict(ext_flags=4)), ('missing-key-by-id', dict(by_id=True)),
('unexpected-key-by-bucket', dict(row_key=(ITEM, LEVEL))), ('missing-unit-price', dict(unit_price=None)),
('unit-price-on-regular-list', dict(list_type=0)), ('zero-unit-price', dict(unit_price=0)),
('different-row-item-id', dict(by_id=True, row_key=(ITEM + 1, LEVEL))),
('nonzero-by-id-outer-level', dict(by_id=True, outer_level=LEVEL, row_key=(ITEM, LEVEL))),
('invalid-row-level', dict(by_id=True, row_key=(ITEM, 1 << 11))),
('ordinary-buyout-plus-unit-price', dict(price_mask=4, price_fields=pack('<Q', 1))),
('ordinary-bidder-plus-unit-price', dict(price_mask=8, price_fields=OWNER + pack('<Q', 1))),
]:
yield label, ITEMS, item_dto(**overrides)
for label, dto in [
('quote-version', bytes([2]) + quote_dto(False)[1:]), ('quote-invalid-presence', quote_dto(2)),
('quote-zero-item', quote_dto(False, item=0)), ('quote-negative-item', quote_dto(False, item=-1)),
('quote-zero-total', quote_dto(True, total=0)), ('quote-zero-quantity', quote_dto(True, quantity=0)),
('quote-negative-duration', quote_dto(True, duration=-1)), ('quote-missing-amount', quote_dto(True)[:10]),
('quote-trailing-available-fields', quote_dto(False) + quote_dto(True)[10:]),
]:
yield label, QUOTE, dto
class CommoditySerializer(unittest.TestCase):
def test_original_input_differences(self):
# Command results are generated by the translator separately from the codec ABI.
for label, delay_at in [('items-by-bucket', 8), ('items-by-id', 8), ('one-bucket', 4),
('confirm-ok', 34), ('confirm-failed', 34)]:
with self.subTest(case=label):
source = bytearray.fromhex(ORIGINAL[label])
self.assertEqual(source[delay_at:delay_at + 4], pack('<I', DELAY))
source[delay_at:delay_at + 4] = bytes(4)
if label == 'one-bucket':
self.assertEqual(source[-2], 4)
source[-2] = 0
self.assertEqual(source, bytes.fromhex(ALIGNED[label]))
def test_complete_bodies(self):
self.assertEqual([MODES[i] for i in (BUCKETS, ITEMS, QUOTE)],
['--auction-buckets', '--auction-items', '--commodity-quote'])
if not optional_file_available(codec_library_path()):
self.skipTest('Isolated codec unavailable')
library = load()
for name, mode, dto, expected in vectors():
with self.subTest(case=name, mode=mode):
serialize_checked(library, mode, dto, expected)
def test_invalid_dtos(self):
if not optional_file_available(codec_library_path()):
self.skipTest('Isolated codec unavailable')
library = load()
for name, mode, dto in invalid_vectors():
with self.subTest(case=name, mode=mode):
with self.assertRaises(ValueError):
serialize(library, mode, dto)
if __name__ == '__main__':
result = unittest.main(exit=False).result
if not result.wasSuccessful():
raise SystemExit(1)
if result.skipped and not optional_file_available(codec_library_path()):
raise SystemExit(77)