Improve test coverage wrt protocol versions; other fixes/improvements

Improvements to the test suite:
* List release version names and numbers in minecraft/__init__.py.
* Make some tests, which previously ran for *all* protocol versions,
  run only for release protocol versions (to improve test performance).
* Make some tests, which previously ran only for the latest protocol
  version, run for all release protocol versions (to improve coverage).
* Print each protocol version being tested to the debug log, to help
  identify sources of errors.
* Use the `nose-timer' plugin to show the run time of each test.

Fix errors revealed by increased test coverage:
* Fix: SoundEffectPacket.Pitch is not serialised correctly for early
  protocol versions.
* Fix: handleExceptionTest finishes later than necessary because
  the test overrode an exception handler used internally by
  `_FakeServerTest', causing the server thread to time out after 4s.
* Add support for multiple exception handlers in `Connection'
  (required for the above).

Improvements to data descriptors:
* Make syntax of property declarations more consistent/Pythonic.
* Factor the definition of several aliasing properties into the
  utility methods `attribute_alias' and `multi_attribute_alias',
  which construct suitable data descriptors.
* Define and use the named tuple `Direction' for (pitch, yaw) values.
This commit is contained in:
joodicator committed 2019-05-14 18:41:58 +02:00
1 parent 6ef868bc5b
commit 7b1567c352
12 files changed
+318 -157

No files matched your search

+89 -57
View File
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import unittest
import string
import logging
import struct
from zlib import decompress
from random import choice
from minecraft import SUPPORTED_PROTOCOL_VERSIONS
from minecraft import SUPPORTED_PROTOCOL_VERSIONS, RELEASE_PROTOCOL_VERSIONS
from minecraft.networking.connection import ConnectionContext
from minecraft.networking.types import (
VarInt, Enum, Vector, PositionAndLook
@@ -14,6 +16,10 @@ from minecraft.networking.packets import (
clientbound
)
TEST_VERSIONS = list(RELEASE_PROTOCOL_VERSIONS)
if SUPPORTED_PROTOCOL_VERSIONS[-1] not in TEST_VERSIONS:
TEST_VERSIONS.append(SUPPORTED_PROTOCOL_VERSIONS[-1])
class PacketBufferTest(unittest.TestCase):
def test_basic_read_write(self):
@@ -42,7 +48,8 @@ class PacketBufferTest(unittest.TestCase):
class PacketSerializationTest(unittest.TestCase):
def test_packet(self):
for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
for protocol_version in TEST_VERSIONS:
logging.debug('protocol_version = %r' % protocol_version)
context = ConnectionContext(protocol_version=protocol_version)
packet = serverbound.play.ChatPacket(context)
@@ -63,7 +70,8 @@ class PacketSerializationTest(unittest.TestCase):
self.assertEqual(packet.message, deserialized.message)
def test_compressed_packet(self):
for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
for protocol_version in TEST_VERSIONS:
logging.debug('protocol_version = %r' % protocol_version)
context = ConnectionContext(protocol_version=protocol_version)
msg = ''.join(choice(string.ascii_lowercase) for i in range(500))
@@ -74,7 +82,8 @@ class PacketSerializationTest(unittest.TestCase):
self.write_read_packet(packet, -1)
def write_read_packet(self, packet, compression_threshold):
for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
for protocol_version in TEST_VERSIONS:
logging.debug('protocol_version = %r' % protocol_version)
context = ConnectionContext(protocol_version=protocol_version)
packet_buffer = PacketBuffer()
@@ -108,7 +117,8 @@ class PacketListenerTest(unittest.TestCase):
def test_packet(chat_packet):
self.assertEqual(chat_packet.message, message)
for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
for protocol_version in TEST_VERSIONS:
logging.debug('protocol_version = %r' % protocol_version)
context = ConnectionContext(protocol_version=protocol_version)
listener = PacketListener(test_packet, serverbound.play.ChatPacket)
@@ -145,13 +155,6 @@ class PacketEnumTest(unittest.TestCase):
class TestReadWritePackets(unittest.TestCase):
maxDiff = None
def setUp(self):
self.context = ConnectionContext()
self.context.protocol_version = SUPPORTED_PROTOCOL_VERSIONS[-1]
def tearDown(self):
del self.context
def test_explosion_packet(self):
Record = clientbound.play.ExplosionPacket.Record
packet = clientbound.play.ExplosionPacket(
@@ -159,6 +162,7 @@ class TestReadWritePackets(unittest.TestCase):
records=[Record(-14, -116, -5), Record(-77, 34, -36),
Record(-35, -127, 95), Record(11, 113, -8)],
player_motion=Vector(4, 5, 0))
self._test_read_write_packet(packet)
def test_combat_event_packet(self):
@@ -187,57 +191,85 @@ class TestReadWritePackets(unittest.TestCase):
self._test_read_write_packet(packet)
def test_spawn_object_packet(self):
EntityType = clientbound.play.SpawnObjectPacket.field_enum(
'type_id', self.context)
for protocol_version in TEST_VERSIONS:
logging.debug('protocol_version = %r' % protocol_version)
context = ConnectionContext(protocol_version=protocol_version)
object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa'
pos_look = PositionAndLook(x=68.0, y=38.0, z=76.0, yaw=16, pitch=23)
velocity = Vector(21, 55, 41)
entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG
EntityType = clientbound.play.SpawnObjectPacket.field_enum(
'type_id', context)
packet = clientbound.play.SpawnObjectPacket(
context=self.context,
x=pos_look.x, y=pos_look.y, z=pos_look.z,
yaw=pos_look.yaw, pitch=pos_look.pitch,
velocity_x=velocity.x, velocity_y=velocity.y,
velocity_z=velocity.z, object_uuid=object_uuid,
entity_id=entity_id, type_id=type_id, data=1)
self.assertEqual(packet.position_and_look, pos_look)
self.assertEqual(packet.position, pos_look.position)
self.assertEqual(packet.velocity, velocity)
self.assertEqual(packet.objectUUID, object_uuid)
self.assertEqual(packet.type, type_name)
pos_look = PositionAndLook(
position=(Vector(68.0, 38.0, 76.0) if context.protocol_version
>= 100 else Vector(68, 38, 76)),
yaw=16, pitch=23)
velocity = Vector(21, 55, 41)
entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG
packet2 = clientbound.play.SpawnObjectPacket(
context=self.context, position_and_look=pos_look,
velocity=velocity, type=type_name,
object_uuid=object_uuid, entity_id=entity_id, data=1)
self.assertEqual(packet.__dict__, packet2.__dict__)
packet = clientbound.play.SpawnObjectPacket(
context=context,
x=pos_look.x, y=pos_look.y, z=pos_look.z,
yaw=pos_look.yaw, pitch=pos_look.pitch,
velocity_x=velocity.x, velocity_y=velocity.y,
velocity_z=velocity.z,
entity_id=entity_id, type_id=type_id, data=1)
if context.protocol_version >= 49:
object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa'
packet.object_uuid = object_uuid
self.assertEqual(packet.objectUUID, object_uuid)
self.assertEqual(packet.position_and_look, pos_look)
self.assertEqual(packet.position, pos_look.position)
self.assertEqual(packet.velocity, velocity)
self.assertEqual(packet.type, type_name)
packet2.position = pos_look.position
self.assertEqual(packet.position, packet2.position)
packet2 = clientbound.play.SpawnObjectPacket(
context=context, position_and_look=pos_look,
velocity=velocity, type=type_name,
entity_id=entity_id, data=1)
if context.protocol_version >= 49:
packet2.object_uuid = object_uuid
self.assertEqual(packet.__dict__, packet2.__dict__)
packet2.data = 0
self._test_read_write_packet(packet)
self._test_read_write_packet(packet2)
packet2.position = pos_look.position
self.assertEqual(packet.position, packet2.position)
packet2.data = 0
if context.protocol_version < 49:
del packet2.velocity
self._test_read_write_packet(packet, context)
self._test_read_write_packet(packet2, context)
def test_sound_effect_packet(self):
SoundCategory = clientbound.play.SoundEffectPacket.SoundCategory
packet = clientbound.play.SoundEffectPacket(
sound_category=SoundCategory.NEUTRAL, sound_id=545,
effect_position=Vector(0.125, 300.0, 50.5), volume=0.75, pitch=1.5)
self._test_read_write_packet(packet)
for protocol_version in TEST_VERSIONS:
context = ConnectionContext(protocol_version=protocol_version)
def _test_read_write_packet(self, packet_in):
packet_in.context = self.context
packet_buffer = PacketBuffer()
packet_in.write(packet_buffer)
packet_buffer.reset_cursor()
VarInt.read(packet_buffer)
packet_id = VarInt.read(packet_buffer)
self.assertEqual(packet_id, packet_in.id)
packet = clientbound.play.SoundEffectPacket(
sound_id=545, effect_position=Vector(0.125, 300.0, 50.5),
volume=0.75)
if context.protocol_version >= 201:
packet.pitch = struct.unpack('f', struct.pack('f', 1.5))[0]
else:
packet.pitch = int(1.5 / 63.5) * 63.5
if context.protocol_version >= 95:
packet.sound_category = \
clientbound.play.SoundEffectPacket.SoundCategory.NEUTRAL
self._test_read_write_packet(packet, context)
packet_out = type(packet_in)(context=self.context)
packet_out.read(packet_buffer)
self.assertIs(type(packet_in), type(packet_out))
self.assertEqual(packet_in.__dict__, packet_out.__dict__)
def _test_read_write_packet(self, packet_in, context=None):
if context is None:
for protocol_version in TEST_VERSIONS:
logging.debug('protocol_version = %r' % protocol_version)
context = ConnectionContext(protocol_version=protocol_version)
self._test_read_write_packet(packet_in, context)
else:
packet_in.context = context
packet_buffer = PacketBuffer()
packet_in.write(packet_buffer)
packet_buffer.reset_cursor()
VarInt.read(packet_buffer)
packet_id = VarInt.read(packet_buffer)
self.assertEqual(packet_id, packet_in.id)
packet_out = type(packet_in)(context=context)
packet_out.read(packet_buffer)
self.assertIs(type(packet_in), type(packet_out))
self.assertEqual(packet_in.__dict__, packet_out.__dict__)