Fix and finish map change msg

This commit is contained in:
ChillerDragon 2023-03-26 12:18:42 +02:00
parent a55817e8ab
commit 992a855a57
3 changed files with 101 additions and 16 deletions

View file

@ -2,7 +2,7 @@ from twnet_parser.packet import *
from twnet_parser.messages7.system.map_change import MsgMapChange
def test_parse_7_real_map_change():
def test_repack_map_change7_chunk() -> None:
msg = MsgMapChange()
msg.unpack(
b'BlmapChill\x00' \
@ -10,7 +10,8 @@ def test_parse_7_real_map_change():
b'\x8b\xbe\x8a\x01' \
b'\x08' \
b'\xa8\x15' \
b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28')
b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04' \
b'\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28')
assert msg.message_name == 'map_change'
@ -19,5 +20,63 @@ def test_parse_7_real_map_change():
assert msg.size == 1134475
assert msg.chunks_per_request == 8
assert msg.chunk_size == 1384
# TODO: fix sha unpack
# assert msg.sha256 == b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28'
assert msg.sha256 == b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4' \
b'\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1' \
b'\x69\x76\x32\xf0\x44\x58\x74\x54\x55' \
b'\x89\x84\x00\xfb\x28'
# change msg and repack to bytes
msg.name = 'dm1'
data: bytes = msg.pack()
# unpack bytes to new message and
# check if the field updated
msg_dm1 = MsgMapChange()
msg_dm1.unpack(data)
assert msg_dm1.name == 'dm1'
def test_build_map_change7_chunk() -> None:
msg = MsgMapChange(
name='BlmapChill',
crc=-1592087519,
size=1134475,
chunks_per_request=8,
chunk_size=1384,
sha256=b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4' \
b'\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1' \
b'\x69\x76\x32\xf0\x44\x58\x74\x54\x55' \
b'\x89\x84\x00\xfb\x28'
)
map_msg_raw = b'BlmapChill\x00' \
b'\xde\xcf\xaa\xee\x0b' \
b'\x8b\xbe\x8a\x01' \
b'\x08' \
b'\xa8\x15' \
b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04' \
b'\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28'
assert msg.pack() == map_msg_raw
def test_build_ctf5_map_change7_chunk() -> None:
msg = MsgMapChange(
name='ctf5',
crc=-1592087519,
size=1134475,
chunks_per_request=6,
chunk_size=1384,
sha256=b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4' \
b'\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1' \
b'\x69\x76\x32\xf0\x44\x58\x74\x54\x55' \
b'\x89\x84\x00\xfb\x28'
)
map_msg_raw = b'ctf5\x00' \
b'\xde\xcf\xaa\xee\x0b' \
b'\x8b\xbe\x8a\x01' \
b'\x06' \
b'\xa8\x15' \
b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04' \
b'\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28'
assert msg.pack() == map_msg_raw

View file

@ -25,6 +25,40 @@ def test_non_ascii_repack() -> None:
u = Unpacker(data)
assert u.get_str() == '💩'
def test_raw_repack_at_end() -> None:
data: bytes = b''
data += pack_int(1)
data += pack_str('a')
data += b'rawr'
u = Unpacker(data)
assert u.get_int() == 1
assert u.get_str() == 'a'
assert u.get_raw() == b'rawr'
def test_raw_sized_repack() -> None:
data: bytes = b''
data += pack_int(1)
data += pack_str('a')
data += b'rawr'
data += b'abc'
data += b'\x00\x00'
data += b'\x01\x02'
data += pack_int(1)
data += pack_int(2)
data += b'\x00\x00'
u = Unpacker(data)
assert u.get_int() == 1
assert u.get_str() == 'a'
assert u.get_raw(4) == b'rawr'
assert u.get_raw(3) == b'abc'
assert u.get_raw(2) == b'\x00\x00'
assert u.get_raw(2) == b'\x01\x02'
assert u.get_int() == 1
assert u.get_int() == 2
assert u.get_raw(2) == b'\x00\x00'
def test_multi_repack() -> None:
strs: list[str] = [
'foo',

View file

@ -14,6 +14,8 @@ class MsgMapChange(PrettyPrint):
sha256: bytes = bytes(32)
) -> None:
self.message_name = 'map_change'
self.system_message = True
self.name = name
self.crc = crc
self.size = size
@ -30,6 +32,7 @@ class MsgMapChange(PrettyPrint):
self.crc = unpacker.get_int()
self.size = unpacker.get_int()
self.chunks_per_request = unpacker.get_int()
self.chunk_size = unpacker.get_int()
self.sha256 = unpacker.get_raw()
return True
@ -38,16 +41,5 @@ class MsgMapChange(PrettyPrint):
pack_int(self.crc) + \
pack_int(self.size) + \
pack_int(self.chunks_per_request) + \
pack_int(self.chunk_size) + \
self.sha256
# msg = MsgMapChange()
# msg.unpack(
# b'BlmapChill\x00' \
# b'\xde\xcf\xaa\xee\x0b' \
# b'\x8b\xbe\x8a\x01' \
# b'\x08' \
# b'\xa8\x15' \
# b'\x81\x7d\xbf\x48\xc5\xf1\x94\x37\xc4\x58\x2c\x6f\x98\xc9\xc2\x04\xc1\xf1\x69\x76\x32\xf0\x44\x58\x74\x54\x55\x89\x84\x00\xfb\x28')
#
# print(msg.pack())
# print(msg)