81 lines
3.7 KiB
Python
81 lines
3.7 KiB
Python
"""Source pet stable payloads through the compiled native writer, with synthetic pets."""
|
|
from struct import pack
|
|
import unittest
|
|
|
|
from native_codec_driver import codec_library_path, require_optional_file, load, MODES, serialize
|
|
from self_party_type_serializer_test import self_create, packed_guid, SELF
|
|
|
|
# Golden ActivePlayerData fragments from the independently linked original
|
|
# Trinity libgame.a, commit 23723358. No gateway code produced these bytes.
|
|
EMPTY = bytes.fromhex('0043000001000240e00000000001a311c0f90420')
|
|
CLOSE = bytes.fromhex('00410000010040a00000')
|
|
MASTER = 0xf1300003e7000011
|
|
CONTEXT = pack('<HIQ', 1, 0, SELF)
|
|
MODE = MODES.index('--pet-stable')
|
|
|
|
|
|
def row(kind, number=7001, entry=2001, display=3001, level=12, loyalty=4,
|
|
metrics=(0, 0, 0, 0), name=b'Ash'):
|
|
return pack('<BIIIIBIIII', kind, number, entry, display, level, loyalty, *metrics) + name + b'\0'
|
|
|
|
|
|
def snapshot(rows, slots=2, master=MASTER):
|
|
return pack('<QBB', master, slots, len(rows)) + b''.join(rows)
|
|
|
|
|
|
def active_values(fragment):
|
|
values = pack('<BBBI', 1, 0, 3, 1 << 7) + fragment
|
|
block = b'\x00' + packed_guid(SELF, (2 << 58) | (1 << 42)) + pack('<I', len(values)) + values
|
|
return pack('<HIBI', 0, 1, 0x80, len(block)) + block
|
|
|
|
|
|
class StableCodec(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.lib = load()
|
|
|
|
def encode(self, dto):
|
|
return serialize(self.lib, MODE, dto)
|
|
|
|
def test_open_and_close(self):
|
|
self.assertEqual(self.encode(b'\x01'+CONTEXT+snapshot([])), active_values(EMPTY))
|
|
self.assertEqual(self.encode(b'\x02'+CONTEXT), active_values(CLOSE))
|
|
|
|
def test_synthetic_current_and_stabled(self):
|
|
metrics = (1493, 223000, 270, 800)
|
|
current = row(1, metrics=metrics)
|
|
other = row(2, 7002, 2002, 3002, 13, 5, name=b'Moss')
|
|
data = self.encode(b'\x01'+CONTEXT+snapshot([current,other]))
|
|
self.assertIn(pack('<BB4IB', 1,4,*metrics,3)+b'Ash', data)
|
|
self.assertIn(pack('<BB4IB', 3,5,0,0,0,0,4)+b'Moss', data)
|
|
cold = self_create()
|
|
# Use the same realm/map as CONTEXT and retain the existing source self GUID.
|
|
cold = cold[:8]+pack('<HII',1,0x01000001,0)+cold[18:]
|
|
create = self.encode(b'\0\0'+snapshot([current,other])+cold)
|
|
native_master = packed_guid(17,(8 << 58)|(1 << 42)|(999 << 6))
|
|
stable = pack('<I',2)+native_master
|
|
for slot,n,e,d,l,flags,loyalty,me,name in ((0,7001,2001,3001,12,1,4,metrics,b'Ash'),(1,7002,2002,3002,13,3,5,(0,0,0,0),b'Moss')):
|
|
stable+=pack('<5IBB4IB',slot,n,e,d,l,flags,loyalty,*me,len(name))+name
|
|
self.assertEqual(create.count(stable),1)
|
|
|
|
def test_acquired_source_slots_and_invalid_frames(self):
|
|
full = [row(2,i,display=3000+i) for i in range(1,5)]
|
|
self.encode(b'\x01'+CONTEXT+snapshot(full,slots=4))
|
|
for wire in (b'',b'\x04',b'\x03\x08',b'\x03\x08\0',b'\x02'+CONTEXT[:-1],
|
|
b'\x02'+CONTEXT+b'\0',b'\x01'+CONTEXT+snapshot([],master=0),
|
|
b'\x01'+CONTEXT+snapshot([],master=0x12345),
|
|
b'\x01'+CONTEXT+snapshot([],slots=5),
|
|
b'\x01'+CONTEXT+snapshot(full,slots=3),
|
|
b'\x01'+CONTEXT+snapshot([row(2,metrics=(0,1,0,0))]),
|
|
b'\x01'+CONTEXT+snapshot([row(1),row(1,7002)]),
|
|
b'\x01'+CONTEXT+snapshot([row(2),row(1,7002)]),
|
|
b'\x01'+CONTEXT+snapshot([row(1),row(2)]),
|
|
b'\x01'+CONTEXT+snapshot([row(2,display=0)])):
|
|
with self.subTest(wire=wire[:10]), self.assertRaises(ValueError):
|
|
self.encode(wire)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
require_optional_file(codec_library_path())
|
|
unittest.main()
|