Add testing for MapPacket

This commit is contained in:
Ammar Askar 2017-07-16 15:17:11 -07:00
parent 5b261b840e
commit d686b6487f
No known key found for this signature in database
GPG Key ID: 5466BE9666B54DED
2 changed files with 98 additions and 1 deletions

View File

@ -688,7 +688,30 @@ class MapPacket(Packet):
self.apply_to_map(map) self.apply_to_map(map)
def write(self, socket, compression_threshold=None): def write(self, socket, compression_threshold=None):
raise NotImplementedError packet_buffer = PacketBuffer()
VarInt.send(self.id, packet_buffer)
VarInt.send(self.map_id, packet_buffer)
Byte.send(self.scale, packet_buffer)
if self.context.protocol_version >= 107:
Boolean.send(self.is_tracking_position, packet_buffer)
VarInt.send(len(self.icons), packet_buffer)
for icon in self.icons:
type_and_direction = (icon.direction << 4) & 0xF0
type_and_direction |= (icon.type & 0xF)
UnsignedByte.send(type_and_direction, packet_buffer)
Byte.send(icon.location[0], packet_buffer)
Byte.send(icon.location[1], packet_buffer)
UnsignedByte.send(self.width, packet_buffer)
if self.width:
UnsignedByte.send(self.height, packet_buffer)
UnsignedByte.send(self.offset[0], packet_buffer) # x
UnsignedByte.send(self.offset[1], packet_buffer) # z
VarIntPrefixedByteArray.send(self.pixels, packet_buffer)
self._write_buffer(socket, packet_buffer, compression_threshold)
def __repr__(self): def __repr__(self):
return 'MapPacket(%s)' % ', '.join( return 'MapPacket(%s)' % ', '.join(

View File

@ -3,6 +3,8 @@ from minecraft.networking.types import (UUID, VarInt, String, Boolean)
from minecraft.networking.packets import PacketBuffer from minecraft.networking.packets import PacketBuffer
from minecraft.networking.packets import PlayerPositionAndLookPacket from minecraft.networking.packets import PlayerPositionAndLookPacket
from minecraft.networking.packets import PlayerListItemPacket from minecraft.networking.packets import PlayerListItemPacket
from minecraft.networking.packets import MapPacket
from minecraft.networking.connection import ConnectionContext
class PlayerPositionAndLookTest(unittest.TestCase): class PlayerPositionAndLookTest(unittest.TestCase):
@ -38,6 +40,78 @@ class PlayerPositionAndLookTest(unittest.TestCase):
self.assertEqual(current_position.pitch, 10.0) self.assertEqual(current_position.pitch, 10.0)
class MapPacketTest(unittest.TestCase):
@staticmethod
def make_map_packet(
context, width=2, height=2, offset=(2, 2), pixels=b"this"):
packet = MapPacket(context)
packet.map_id = 1
packet.scale = 42
packet.is_tracking_position = True
packet.icons = []
packet.icons.append(
MapPacket.MapIcon(type=2, direction=2, location=(1, 1))
)
packet.icons.append(
MapPacket.MapIcon(type=3, direction=3, location=(3, 3))
)
packet.width = width
packet.height = height
packet.offset = offset
packet.pixels = pixels
return packet
def packet_roundtrip(self, context):
packet = self.make_map_packet(context)
packet_buffer = PacketBuffer()
packet.write(packet_buffer)
packet_buffer.reset_cursor()
# Read the length and packet id
VarInt.read(packet_buffer)
packet_id = VarInt.read(packet_buffer)
self.assertEqual(packet_id, packet.id)
p = MapPacket(context)
p.read(packet_buffer)
self.assertEqual(p.map_id, packet.map_id)
self.assertEqual(p.scale, packet.scale)
self.assertEqual(p.is_tracking_position, packet.is_tracking_position)
self.assertEqual(p.width, packet.width)
self.assertEqual(p.height, packet.height)
self.assertEqual(p.offset, packet.offset)
self.assertEqual(p.pixels, packet.pixels)
self.assertEqual(str(p.icons[0]), str(packet.icons[0]))
self.assertEqual(str(p.icons[1]), str(packet.icons[1]))
self.assertEqual(str(p), str(packet))
def test_packet_roundtrip(self):
self.packet_roundtrip(ConnectionContext(protocol_version=106))
self.packet_roundtrip(ConnectionContext(protocol_version=107))
def test_map_set(self):
map_set = MapPacket.MapSet()
context = ConnectionContext(protocol_version=107)
packet = self.make_map_packet(context)
packet.apply_to_map_set(map_set)
self.assertEqual(len(map_set.maps_by_id), 1)
packet = self.make_map_packet(
context, width=1, height=0, offset=(2, 2), pixels=b"x"
)
packet.apply_to_map_set(map_set)
map = map_set.maps_by_id[1]
self.assertIn(b"xh", map.pixels)
self.assertIn(b"is", map.pixels)
fake_uuid = "12345678-1234-5678-1234-567812345678" fake_uuid = "12345678-1234-5678-1234-567812345678"