2015-04-03 05:51:53 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2015-04-03 05:04:59 +02:00
|
|
|
import unittest
|
|
|
|
from minecraft.networking.types import (
|
|
|
|
Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort,
|
2019-05-15 14:52:13 +02:00
|
|
|
Integer, FixedPointInteger, Angle, VarInt, Long, Float, Double,
|
2018-06-20 06:32:35 +02:00
|
|
|
ShortPrefixedByteArray, VarIntPrefixedByteArray, UUID,
|
|
|
|
String as StringType, Position, TrailingByteArray, UnsignedLong,
|
2015-04-03 05:04:59 +02:00
|
|
|
)
|
2015-04-12 04:26:12 +02:00
|
|
|
from minecraft.networking.packets import PacketBuffer
|
2019-05-11 08:43:08 +02:00
|
|
|
from minecraft.networking.connection import ConnectionContext
|
2019-05-14 18:41:58 +02:00
|
|
|
from minecraft import SUPPORTED_PROTOCOL_VERSIONS, RELEASE_PROTOCOL_VERSIONS
|
2015-04-03 05:51:53 +02:00
|
|
|
|
2015-04-03 05:04:59 +02:00
|
|
|
|
2019-05-14 18:41:58 +02:00
|
|
|
TEST_VERSIONS = list(RELEASE_PROTOCOL_VERSIONS)
|
|
|
|
if SUPPORTED_PROTOCOL_VERSIONS[-1] not in TEST_VERSIONS:
|
|
|
|
TEST_VERSIONS.append(SUPPORTED_PROTOCOL_VERSIONS[-1])
|
|
|
|
|
2015-04-03 05:04:59 +02:00
|
|
|
TEST_DATA = {
|
|
|
|
Boolean: [True, False],
|
|
|
|
UnsignedByte: [0, 125],
|
|
|
|
Byte: [-22, 22],
|
|
|
|
Short: [-340, 22, 350],
|
|
|
|
UnsignedShort: [0, 400],
|
2018-05-27 18:12:50 +02:00
|
|
|
UnsignedLong: [0, 400],
|
2015-04-03 05:04:59 +02:00
|
|
|
Integer: [-1000, 1000],
|
2018-06-20 06:32:35 +02:00
|
|
|
FixedPointInteger: [float(-13098.3435), float(-0.83), float(1000)],
|
2019-05-17 22:31:33 +02:00
|
|
|
Angle: [0, 360.0, 720, 47.12947238973, -108.7],
|
2015-04-03 05:04:59 +02:00
|
|
|
VarInt: [1, 250, 50000, 10000000],
|
|
|
|
Long: [50000000],
|
|
|
|
Float: [21.000301],
|
|
|
|
Double: [36.004002],
|
|
|
|
ShortPrefixedByteArray: [bytes(245)],
|
|
|
|
VarIntPrefixedByteArray: [bytes(1234)],
|
2018-05-27 18:12:50 +02:00
|
|
|
TrailingByteArray: [b'Q^jO<5*|+o LGc('],
|
2017-07-16 10:00:14 +02:00
|
|
|
UUID: ["12345678-1234-5678-1234-567812345678"],
|
2018-05-27 18:12:50 +02:00
|
|
|
StringType: ["hello world"],
|
2018-06-21 08:06:45 +02:00
|
|
|
Position: [(758, 0, 691), (-500, -12, -684)],
|
2015-04-03 05:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class SerializationTest(unittest.TestCase):
|
|
|
|
def test_serialization(self):
|
2019-05-14 18:41:58 +02:00
|
|
|
for protocol_version in TEST_VERSIONS:
|
2019-05-11 08:43:08 +02:00
|
|
|
context = ConnectionContext(protocol_version=protocol_version)
|
|
|
|
|
|
|
|
for data_type in Type.__subclasses__():
|
|
|
|
if data_type in TEST_DATA:
|
|
|
|
test_cases = TEST_DATA[data_type]
|
|
|
|
|
|
|
|
for test_data in test_cases:
|
|
|
|
packet_buffer = PacketBuffer()
|
|
|
|
data_type.send_with_context(
|
|
|
|
test_data, packet_buffer, context)
|
|
|
|
packet_buffer.reset_cursor()
|
|
|
|
|
|
|
|
deserialized = data_type.read_with_context(
|
|
|
|
packet_buffer, context)
|
|
|
|
if data_type is FixedPointInteger:
|
|
|
|
self.assertAlmostEqual(
|
|
|
|
test_data, deserialized, delta=1.0/32.0)
|
2019-05-15 14:52:13 +02:00
|
|
|
elif data_type is Angle:
|
2019-05-17 22:31:33 +02:00
|
|
|
self.assertAlmostEqual(test_data % 360, deserialized,
|
|
|
|
delta=360/256)
|
2019-05-11 08:43:08 +02:00
|
|
|
elif data_type is Float or data_type is Double:
|
2019-05-17 22:31:33 +02:00
|
|
|
self.assertAlmostEqual(test_data, deserialized, 3)
|
2019-05-11 08:43:08 +02:00
|
|
|
else:
|
|
|
|
self.assertEqual(test_data, deserialized)
|
2015-04-03 05:51:53 +02:00
|
|
|
|
2015-04-12 04:26:12 +02:00
|
|
|
def test_exceptions(self):
|
|
|
|
base_type = Type()
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
base_type.read(None)
|
|
|
|
|
2019-05-11 08:43:08 +02:00
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
base_type.read_with_context(None, None)
|
|
|
|
|
2015-04-12 04:26:12 +02:00
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
base_type.send(None, None)
|
|
|
|
|
2019-05-11 08:43:08 +02:00
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
base_type.send_with_context(None, None, None)
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Position.read(None)
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Position.send(None, None)
|
|
|
|
|
2015-04-12 04:26:12 +02:00
|
|
|
empty_socket = PacketBuffer()
|
2016-06-17 23:54:21 +02:00
|
|
|
with self.assertRaises(Exception):
|
|
|
|
VarInt.read(empty_socket)
|
2015-04-12 04:26:12 +02:00
|
|
|
|
2015-04-03 05:51:53 +02:00
|
|
|
def test_varint(self):
|
|
|
|
self.assertEqual(VarInt.size(2), 1)
|
|
|
|
self.assertEqual(VarInt.size(1250), 2)
|
|
|
|
|
2017-07-16 10:00:14 +02:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
VarInt.size(2 ** 90)
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
packet_buffer = PacketBuffer()
|
|
|
|
VarInt.send(2 ** 49, packet_buffer)
|
|
|
|
packet_buffer.reset_cursor()
|
|
|
|
VarInt.read(packet_buffer)
|
|
|
|
|
2015-04-03 05:51:53 +02:00
|
|
|
packet_buffer = PacketBuffer()
|
|
|
|
VarInt.send(50000, packet_buffer)
|
|
|
|
packet_buffer.reset_cursor()
|
|
|
|
|
2016-06-17 23:54:21 +02:00
|
|
|
self.assertEqual(VarInt.read(packet_buffer), 50000)
|