feat!: set flag token by default in 0.6.5

Set the token flag automatically if a token is provided. So lib users
have less boilerplate code to send a packet. But it can still be
set explicitly to false if desired.

Also add a test for cl_start_info
This commit is contained in:
ChillerDragon 2023-06-01 09:22:06 +02:00
parent 7e11d6d129
commit 9f06c95e8d
4 changed files with 134 additions and 1 deletions

View file

@ -0,0 +1,124 @@
from twnet_parser.packet import parse6, TwPacket, PacketHeader6
from twnet_parser.messages6.game.cl_start_info import MsgClStartInfo
def test_unpack_cl_start_info() -> None:
"""
Sent by a vanilla 0.6.5 client
copied from a real traffic capture
https://chillerdragon.github.io/teeworlds-protocol/06/game_messages.html#NETMSGTYPE_CL_STARTINFO
"""
data = \
b'\x08\x03\x01' \
b'\x9c\x6b\xcb\xda' \
b'\x41' \
b'\x0f\x03\x28\x6e\x61\x6d\x65\x6c\x65\x73' \
b'\x73\x20\x74\x65\x65\x00\x00\x40\x64\x65\x66\x61\x75\x6c\x74\x00' \
b'\x00\x80\xfe\x07\x80\xfe\x07'
packet = parse6(data)
assert packet.version == '0.6'
assert packet.header.ack == 3
assert packet.header.num_chunks == 1
assert packet.header.flags.token is True
assert packet.header.flags.control is False
assert packet.header.flags.connless is False
assert packet.header.flags.resend is False
assert packet.header.flags.compression is False
assert len(packet.messages) == 1
msg: MsgClStartInfo = packet.messages[0]
assert msg.system_message is False
assert msg.message_name == 'cl_start_info'
assert msg.name == "nameless tee"
assert msg.clan == ""
assert msg.country == -1
assert msg.skin == "default"
assert msg.use_custom_color is False
assert msg.color_body == 65408
assert msg.color_feet == 65408
repack = packet.pack()
assert repack == data
def test_pack_start_info():
packet: TwPacket = TwPacket()
packet.header = PacketHeader6()
packet.header.ack = 3
packet.header.token = b'\x9c\x6b\xcb\xda'
msg: MsgClStartInfo = MsgClStartInfo()
msg.name = "nameless tee"
msg.clan = ""
msg.country = -1
msg.skin = "default"
msg.use_custom_color = False
msg.color_body = 65408
msg.color_feet = 65408
packet.messages.append(msg)
data: bytes = packet.pack()
packet2: TwPacket = parse6(data)
assert packet2.version == '0.6'
assert packet2.header.ack == 3
assert packet2.header.num_chunks == 1
assert packet2.header.flags.token is True
assert packet2.header.flags.control is False
assert packet2.header.flags.connless is False
assert packet2.header.flags.resend is False
assert packet2.header.flags.compression is False
assert len(packet2.messages) == 1
msg2: MsgClStartInfo = packet2.messages[0]
assert msg2.system_message is False
assert msg2.message_name == 'cl_start_info'
assert msg2.name == "nameless tee"
assert msg2.clan == ""
assert msg2.country == -1
assert msg2.skin == "default"
assert msg2.use_custom_color is False
assert msg2.color_body == 65408
assert msg2.color_feet == 65408
assert packet2.pack() == data
def test_pack_start_info_no_token():
packet: TwPacket = TwPacket()
packet.header = PacketHeader6()
packet.header.ack = 3
packet.header.flags.token = False
packet.header.token = b''
msg: MsgClStartInfo = MsgClStartInfo()
msg.name = "nameless tee"
msg.clan = ""
msg.country = -1
msg.skin = "default"
msg.use_custom_color = False
msg.color_body = 65408
msg.color_feet = 65408
packet.messages.append(msg)
data: bytes = packet.pack()
packet2: TwPacket = parse6(data)
assert packet2.version == '0.6'
assert packet2.header.ack == 3
assert packet2.header.num_chunks == 1
assert packet2.header.flags.token is False
assert packet2.header.flags.control is False
assert packet2.header.flags.connless is False
assert packet2.header.flags.resend is False
assert packet2.header.flags.compression is False
assert len(packet2.messages) == 1
msg2: MsgClStartInfo = packet2.messages[0]
assert msg2.system_message is False
assert msg2.message_name == 'cl_start_info'
assert msg2.name == "nameless tee"
assert msg2.clan == ""
assert msg2.country == -1
assert msg2.skin == "default"
assert msg2.use_custom_color is False
assert msg2.color_body == 65408
assert msg2.color_feet == 65408
assert packet2.pack() == data

View file

@ -71,6 +71,7 @@ def test_ctrl_connect():
def test_pack_ctrl_connect():
packet: TwPacket = TwPacket()
packet.header = PacketHeader6()
packet.header.flags.token = False
msg: CtrlConnect = CtrlConnect()
msg.response_token = b'\x37\xb5\xbb\x06'
packet.messages.append(msg)

View file

@ -21,6 +21,7 @@ def test_unpack_info():
assert packet.version == '0.6'
assert packet.header.ack == 0
assert packet.header.num_chunks == 1
assert packet.header.token == b'\x9c\x6b\xcb\xda'
assert packet.header.flags.token is True
assert packet.header.flags.control is False
assert packet.header.flags.connless is False
@ -42,6 +43,7 @@ def test_unpack_info():
def test_pack_info():
packet: TwPacket = TwPacket()
packet.header = PacketHeader6()
packet.header.token = b'\xfa\xfa\xfa\xfa'
msg: MsgInfo = MsgInfo()
msg.version = '0.6 626fce9a778df4d4'
msg.password = ''
@ -52,7 +54,8 @@ def test_pack_info():
assert packet2.version == '0.6'
assert packet2.header.ack == 0
assert packet2.header.num_chunks == 1
assert packet2.header.flags.token is False
assert packet2.header.token == b'\xfa\xfa\xfa\xfa'
assert packet2.header.flags.token is True
assert packet2.header.flags.control is False
assert packet2.header.flags.connless is False
assert packet2.header.flags.resend is False

View file

@ -97,6 +97,11 @@ class PacketHeader6(PrettyPrint):
TTTTTTTT
"""
flags = 0
if self.flags.token is None:
# do not automatically set the token flag
# if the token field has the empty token value
if self.token != b'\xff\xff\xff\xff':
self.flags.token = True
if self.flags.token:
flags |= PACKETFLAG6_TOKEN
if self.flags.control: