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

161 lines
10 KiB
Python

#!/usr/bin/env python3
"""Source-auth writer bounds and decoded fields; no authentication/game claim."""
import struct,sys,json,os,re
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parent))
from native_codec_driver import ROOT, MODES, codec_library_path, require_optional_file, load, serialize
require_optional_file(codec_library_path())
library=load();mode=MODES.index('--native-auth-success')
sys.path.insert(0,str(ROOT/'ops'))
from auth_metadata_fixture import Metadata
metadata=Metadata(ROOT/'include/gateway')
# Reviewed TurtleNativeCreationRaceClasses59 contract (turtle_login_profile.hpp).
# Pin every class, ordering and zero expansion requirement; a race-count-only
# check would miss silently dropped or spuriously granted capabilities.
race_classes = (
(1, (1,2,3,4,5,8,9)), (2, (1,3,4,7,8,9)),
(3, (1,2,3,4,5,8,9)), (4, (1,3,4,5,11)),
(5, (1,3,4,5,8,9)), (6, (1,3,5,7,11)),
(7, (1,3,4,8,9)), (8, (1,3,4,5,7,8,9)),
(9, (1,3,4,8,9)), (10, (1,2,3,4,5,8)),
)
assert sum(len(classes) for _, classes in race_classes) == 59
def available_pushes():
# AvailableHotfixes is the unbaked set, not every page in the historical
# package.json. Compare exact served reply digests with the reviewed baked
# header; do not infer 'baked' from an empty bootstrap reply.
root = ROOT / 'include/gateway'
ids = metadata.pages
baked = {int(ident): int(digest, 16) for ident, digest in re.findall(
r'\{(\d+),\s*0x([0-9a-f]+)ull\}',
(root / 'baked_hotfix_pushes.hpp').read_text())}
assert baked, 'Missing baked hotfix digest contract'
pending = []
for ident in sorted(ids):
reply = serialize(library, MODES.index('--native-hotfix-reply'),
struct.pack('<4I', 70334, 70334, 1, ident))
assert reply == metadata.pages[ident], ('Exact HotfixConnect mismatch', ident)
digest = 1469598103934665603
for byte in reply:
digest = ((digest ^ byte) * 1099511628211) & 0xffffffffffffffff
if baked.get(ident) != digest:
pending.append(ident)
return pending
def input_for(name=b'Fixture Realm',address=0x46010046,clock=1792050000):return struct.pack('<IQH',address,clock,len(name))+name
wire=serialize(library,mode,input_for())
assert wire[:5]==bytes(4)+b'\x80'
assert struct.unpack_from('<III',wire,5)==(0x46010046,1,0)
assert wire[17:19]==bytes(2) # Classic expansion profile.
assert wire[19:23]==bytes(4) and struct.unpack_from('<I',wire,23)==(len(race_classes),)
assert wire[27:48]==bytes(21) # No account entitlements/templates/billing flags.
assert struct.unpack_from('<Q',wire,48)==(1792050000,)
assert struct.unpack_from('<I',wire,56)==(0x46010046,)
bits=int.from_bytes(wire[60:63],'big')>>6
assert bits>>17==1 and (bits>>16)&1==0
actual=(bits>>8)&255;normalized=bits&255
assert wire[63:63+actual]==b'Fixture Realm'
assert wire[63+actual:63+actual+normalized]==b'FixtureRealm'
# AuthSuccessInfo writes each RaceID8/count32/ClassAvailability4 contiguously.
# Its six trial/template/optional flags are ONE final flushed byte after all
# race rows, not a padding byte at the end of each RaceClassAvailability.
assert wire[63+actual+normalized:]==b''.join(bytes([race])+struct.pack('<I',len(classes))+b''.join(bytes([klass,0,0,0]) for klass in classes) for race,classes in race_classes)+b'\0'
for name in (b'A',b'X'*127,b'Multiple Words Here'):
assert serialize(library,mode,input_for(name)).startswith(bytes(4)+b'\x80')
for data in (b'',input_for()[:-1],input_for()+b'x',input_for(b''),input_for(b'X'*128),input_for(b' '),input_for(b'bad\0name'),input_for(address=0),input_for(clock=0xffffffffffffffff)):
try:serialize(library,mode,data)
except ValueError:pass
else:raise AssertionError('Malformed source auth input accepted')
print('Verbatim70334 auth writer produces bounded realm metadata and all 59 reviewed capabilities across ten races from matching DBC/public server definitions, without account entitlements. Actual gameplay remains separately unverified.')
bootstrap=MODES.index('--native-client-initialization')
wire=serialize(library,bootstrap,struct.pack('<IIQ',0x46010046,0,12345))
pending_pushes=available_pushes()
available_bytes=8+8*len(pending_pushes)
assert struct.unpack_from('<5I',wire)==(2,0x4b0001,available_bytes,0x46010046,len(pending_pushes))
assert [struct.unpack_from('<II',wire,20+8*i)for i in range(len(pending_pushes))]==[(ident,1)for ident in pending_pushes]
at=12+available_bytes
assert struct.unpack_from('<II',wire,at)==(0x4701b6,170)
assert wire[at+8:at+10]==bytes(2) and struct.unpack_from('<Q',wire,at+10)==(12345,)
assert wire[at+18:]==bytes(160) and len(wire)==at+178
stored=serialize(library,bootstrap,struct.pack('<IIQQ',0x46010046,0,12345,9876))
assert stored[:at+18]==wire[:at+18]
assert struct.unpack_from('<20Q',stored,at+18)==(0,)*13+(9876,)+(0,)*6
# Current Wire supplies all twenty account-data clocks; retain the legacy
# Edit Mode-only tail above and verify the complete form independently.
account_times=tuple(range(100,120))
complete=serialize(library,bootstrap,struct.pack('<IIQ20Q',0x46010046,0,12345,*account_times))
assert complete[:at+18]==wire[:at+18]
assert len(complete)==at+178 and struct.unpack_from('<20Q',complete,at+18)==account_times
for index in range(20):
invalid=list(account_times);invalid[index]=2**63
try:serialize(library,bootstrap,struct.pack('<IIQ20Q',0x46010046,0,12345,*invalid))
except ValueError:pass
else:raise AssertionError(f'Invalid account timestamp accepted at index {index}')
wire=serialize(library,bootstrap,struct.pack('<IIQ',0x46010046,1,12345))
assert struct.unpack_from('<3I',wire)==(1,0x470363,10)
assert wire[12:]==bytes(4)+b'\x07'+bytes(5)
for data in (b'',bytes(16),struct.pack('<IIQ',1,2,12345),struct.pack('<IIQ',1,0,12345)+b'x',struct.pack('<IIQ',1,0,0xffffffffffffffff),struct.pack('<IIQQ',1,1,12345,9876),struct.pack('<IIQQ',1,0,12345,2**63),struct.pack('<IIQ19Q',1,0,12345,*range(19)),struct.pack('<IIQ21Q',1,0,12345,*range(21)),struct.pack('<IIQ20Q',1,1,12345,*range(20))):
try:serialize(library,bootstrap,data)
except ValueError:pass
else:raise AssertionError('Invalid bootstrap namespace accepted')
print(f'AvailableHotfixes: {len(pending_pushes)} unbaked pushes; exact source reply digests compared with baked header.')
print('Source AvailableHotfixes writer and Classic-specific empty collection framing verified; actual-client readiness remains separately tested.')
missing=MODES.index('--native-db-reply-missing')
wire=serialize(library,missing,struct.pack('<III',0xdf2f53cf,7421,12345))
assert len(wire)==17 and struct.unpack_from('<III',wire)==(0xdf2f53cf,7421,12345)
assert wire[12:]==b'\x60'+bytes(4) # Explicit Invalid3, no content or keys.
for data in (b'',bytes(12),bytes(13)):
try:serialize(library,missing,data)
except ValueError:pass
else:raise AssertionError('Malformed negative DB2 response input accepted')
print('Source DBReply writer encodes explicit missing records without valid/deleted rows or key material.')
# All expected metadata comes from the tracked current compact source registry,
# plus the source-owned override/removal components; never from codec replies.
# An explicitly supplied package remains an additional strict source check.
if 'GW_TEST_ITEM_PACKAGE' in os.environ:
profile=json.loads(Path(os.environ['GW_TEST_ITEM_PACKAGE']).read_text())
for record in profile['rows']:
key=(int(record['table_hash'],16),record['id'])
expected=metadata.db[key]
assert expected.body==bytes.fromhex(record['wire_hex']) and expected.status==(2 if record.get('record_status','valid')=='removed' else 1), ('Explicit package differs from effective source metadata',key)
# Full pages above prove catalogue-wide bytes. DBReply uses linear lookups,
# so exercise all overrides/removal policies and table/page boundaries rather
# than making the fixture quadratic in the full registry size. Reuse buffers.
import ctypes as C
source=C.create_string_buffer(12)
output=C.create_string_buffer(65535+17)
written=C.c_uint32()
for key in sorted(metadata.db_probes):
record=metadata.db[key]
request=struct.pack('<III',*key,12345)
C.memmove(source,request,12)
result=library.gw_codec_serialize(missing,source,12,0,output,len(output),C.byref(written))
assert result==0, ('DBReply rejected source record',key)
expected=request+struct.pack('<BI',record.status<<5,len(record.body))+record.body
assert output.raw[:written.value]==expected, ('Exact DBReply mismatch',key)
assert metadata.db[(0x919be54e,23043)].status==1
wire=serialize(library,missing,struct.pack('<III',0x919be54e,0xffffffff,12345))
assert wire==struct.pack('<III',0x919be54e,0xffffffff,12345)+b'\x60'+bytes(4)
print(f'Exact DBReply PASS: {len(metadata.db_probes)} policy/boundary probes; full pages cover {len(metadata.db)} effective table/ID records; source counts {metadata.source_counts}.',flush=True)
hotfix=MODES.index('--native-hotfix-reply')
# available_pushes already checked every single-page body byte-for-byte.
for ident,expected in metadata.pages.items():
request=struct.pack('<4I',70334,70334,1,ident)
assert serialize(library,hotfix,struct.pack('<5I',70334,70334,2,ident,ident))==expected, ('Duplicate push mismatch',ident)
assert serialize(library,hotfix,struct.pack('<3I',70334,70334,0))==bytes(8)
assert serialize(library,hotfix,struct.pack('<4I',70334,70334,1,0))==bytes(8)
assert sum(len(wire)-8 for wire in metadata.pages.values())+8>1048576
try:serialize(library,hotfix,struct.pack('<3I',70334,70334,len(metadata.pages))+b''.join(struct.pack('<I',ident)for ident in metadata.pages))
except ValueError:pass
else:raise AssertionError('Oversized aggregate bypassed page boundary')
for bad in (b'',request[:-1],request+b'x',struct.pack('<3I',70334,70334,4097),struct.pack('<4I',0,70334,1,next(iter(metadata.pages)))):
try:serialize(library,hotfix,bad)
except ValueError:pass
else:raise AssertionError('Malformed hotfix request accepted')
print(f'Exact HotfixConnect PASS: {len(metadata.pages)} pages, {metadata.superseded} superseded rows retain sibling unique IDs; unknown/duplicate pushes and bounds verified. No actual-client consumption claim.')