2 # -*- coding: utf-8 -*-
4 from minecraft
.networking
.types
import (
5 Type
, Boolean
, UnsignedByte
, Byte
, Short
, UnsignedShort
,
6 Integer
, FixedPointInteger
, Angle
, VarInt
, Long
, Float
, Double
,
7 ShortPrefixedByteArray
, VarIntPrefixedByteArray
, UUID
,
8 String
as StringType
, Position
, TrailingByteArray
, UnsignedLong
,
10 from minecraft
.networking
.packets
.clientbound
.play
import (
11 MultiBlockChangePacket
13 from minecraft
.networking
.packets
import PacketBuffer
14 from minecraft
.networking
.connection
import ConnectionContext
15 from minecraft
import SUPPORTED_PROTOCOL_VERSIONS
, RELEASE_PROTOCOL_VERSIONS
18 TEST_VERSIONS
= list(RELEASE_PROTOCOL_VERSIONS
)
19 if SUPPORTED_PROTOCOL_VERSIONS
[-1] not in TEST_VERSIONS
:
20 TEST_VERSIONS
.append(SUPPORTED_PROTOCOL_VERSIONS
[-1])
23 Boolean
: [True, False],
24 UnsignedByte
: [0, 125],
26 Short
: [-340, 22, 350],
27 UnsignedShort
: [0, 400],
28 UnsignedLong
: [0, 400],
29 Integer
: [-1000, 1000],
30 FixedPointInteger
: [float(-13098.3435), float(-0.83), float(1000)],
31 Angle
: [0, 360.0, 720, 47.12947238973, -108.7],
32 VarInt
: [1, 250, 50000, 10000000],
36 ShortPrefixedByteArray
: [bytes(245)],
37 VarIntPrefixedByteArray
: [bytes(1234)],
38 TrailingByteArray
: [b
'Q^jO<5*|+o LGc('],
39 UUID
: ["12345678-1234-5678-1234-567812345678"],
40 StringType
: ["hello world"],
41 Position
: [(758, 0, 691), (-500, -12, -684)],
42 MultiBlockChangePacket
.ChunkSectionPos
: [
44 for x
in [-0x200000, -123, -1, 0, 123, 0x1FFFFF]
45 for z
in [-0x200000, -456, -1, 0, 456, 0x1FFFFF]
46 for y
in [-0x80000, -789, -1, 0, 789, 0x7FFFF]
51 class SerializationTest(unittest
.TestCase
):
52 def test_serialization(self
):
53 for protocol_version
in TEST_VERSIONS
:
54 context
= ConnectionContext(protocol_version
=protocol_version
)
56 for data_type
in Type
.__subclasses
__():
57 if data_type
in TEST_DATA
:
58 test_cases
= TEST_DATA
[data_type
]
60 for test_data
in test_cases
:
61 packet_buffer
= PacketBuffer()
62 data_type
.send_with_context(
63 test_data
, packet_buffer
, context
)
64 packet_buffer
.reset_cursor()
66 deserialized
= data_type
.read_with_context(
67 packet_buffer
, context
)
68 if data_type
is FixedPointInteger
:
69 self
.assertAlmostEqual(
70 test_data
, deserialized
, delta
=1.0/32.0)
71 elif data_type
is Angle
:
72 self
.assertAlmostEqual(test_data
% 360,
75 elif data_type
is Float
or data_type
is Double
:
76 self
.assertAlmostEqual(test_data
, deserialized
, 3)
78 self
.assertEqual(test_data
, deserialized
)
80 def test_exceptions(self
):
82 with self
.assertRaises(NotImplementedError):
85 with self
.assertRaises(NotImplementedError):
86 base_type
.read_with_context(None, None)
88 with self
.assertRaises(NotImplementedError):
89 base_type
.send(None, None)
91 with self
.assertRaises(NotImplementedError):
92 base_type
.send_with_context(None, None, None)
94 with self
.assertRaises(TypeError):
97 with self
.assertRaises(TypeError):
98 Position
.send(None, None)
100 empty_socket
= PacketBuffer()
101 with self
.assertRaises(Exception):
102 VarInt
.read(empty_socket
)
104 def test_varint(self
):
105 self
.assertEqual(VarInt
.size(2), 1)
106 self
.assertEqual(VarInt
.size(1250), 2)
108 with self
.assertRaises(ValueError):
111 with self
.assertRaises(ValueError):
112 packet_buffer
= PacketBuffer()
113 VarInt
.send(2 ** 49, packet_buffer
)
114 packet_buffer
.reset_cursor()
115 VarInt
.read(packet_buffer
)
117 packet_buffer
= PacketBuffer()
118 VarInt
.send(50000, packet_buffer
)
119 packet_buffer
.reset_cursor()
121 self
.assertEqual(VarInt
.read(packet_buffer
), 50000)