Improve tests of types.py
This commit is contained in:
3 files changed
+52
-3
No files matched your search
@@ -83,14 +83,23 @@ class VarInt(Type):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
number = 0
|
number = 0
|
||||||
for i in range(5):
|
# Limit of 5 bytes, otherwise its possible to cause
|
||||||
|
# a DOS attack by sending VarInts that just keep
|
||||||
|
# going
|
||||||
|
bytes_encountered = 0
|
||||||
|
while True:
|
||||||
byte = file_object.read(1)
|
byte = file_object.read(1)
|
||||||
if len(byte) < 1:
|
if len(byte) < 1:
|
||||||
raise EOFError("Unexpected end of message.")
|
raise EOFError("Unexpected end of message.")
|
||||||
|
|
||||||
byte = ord(byte)
|
byte = ord(byte)
|
||||||
number |= (byte & 0x7F) << 7 * i
|
number |= (byte & 0x7F) << 7 * bytes_encountered
|
||||||
if not byte & 0x80:
|
if not byte & 0x80:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
bytes_encountered += 1
|
||||||
|
if bytes_encountered > 5:
|
||||||
|
raise ValueError("Tried to read too long of a VarInt")
|
||||||
return number
|
return number
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -109,6 +118,7 @@ class VarInt(Type):
|
|||||||
for max_value, size in VARINT_SIZE_TABLE.items():
|
for max_value, size in VARINT_SIZE_TABLE.items():
|
||||||
if value < max_value:
|
if value < max_value:
|
||||||
return size
|
return size
|
||||||
|
raise ValueError("Integer too large")
|
||||||
|
|
||||||
|
|
||||||
# Maps (maximum integer value -> size of VarInt in bytes)
|
# Maps (maximum integer value -> size of VarInt in bytes)
|
||||||
@@ -199,3 +209,7 @@ class UUID(Type):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return str(uuid.UUID(bytes=file_object.read(16)))
|
return str(uuid.UUID(bytes=file_object.read(16)))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def send(value, socket):
|
||||||
|
socket.send(uuid.UUID(value).bytes)
|
||||||
@@ -11,6 +11,30 @@ from minecraft.networking.packets import (
|
|||||||
PacketBuffer, ChatPacket, KeepAlivePacket, PacketListener)
|
PacketBuffer, ChatPacket, KeepAlivePacket, PacketListener)
|
||||||
|
|
||||||
|
|
||||||
|
class PacketBufferTest(unittest.TestCase):
|
||||||
|
def test_basic_read_write(self):
|
||||||
|
message = b"hello"
|
||||||
|
|
||||||
|
packet_buffer = PacketBuffer()
|
||||||
|
packet_buffer.send(message)
|
||||||
|
|
||||||
|
packet_buffer.reset_cursor()
|
||||||
|
self.assertEqual(packet_buffer.read(), message)
|
||||||
|
packet_buffer.reset_cursor()
|
||||||
|
self.assertEqual(packet_buffer.recv(), message)
|
||||||
|
|
||||||
|
packet_buffer.reset()
|
||||||
|
self.assertNotEqual(packet_buffer.read(), message)
|
||||||
|
|
||||||
|
def test_get_writable(self):
|
||||||
|
message = b"hello"
|
||||||
|
|
||||||
|
packet_buffer = PacketBuffer()
|
||||||
|
packet_buffer.send(message)
|
||||||
|
|
||||||
|
self.assertEqual(packet_buffer.get_writable(), message)
|
||||||
|
|
||||||
|
|
||||||
class PacketSerializatonTest(unittest.TestCase):
|
class PacketSerializatonTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_packet(self):
|
def test_packet(self):
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import unittest
|
import unittest
|
||||||
|
import uuid
|
||||||
from minecraft.networking.types import (
|
from minecraft.networking.types import (
|
||||||
Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort,
|
Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort,
|
||||||
Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray,
|
Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray,
|
||||||
VarIntPrefixedByteArray, String as StringType
|
VarIntPrefixedByteArray, UUID, String as StringType
|
||||||
)
|
)
|
||||||
from minecraft.networking.packets import PacketBuffer
|
from minecraft.networking.packets import PacketBuffer
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ TEST_DATA = {
|
|||||||
Double: [36.004002],
|
Double: [36.004002],
|
||||||
ShortPrefixedByteArray: [bytes(245)],
|
ShortPrefixedByteArray: [bytes(245)],
|
||||||
VarIntPrefixedByteArray: [bytes(1234)],
|
VarIntPrefixedByteArray: [bytes(1234)],
|
||||||
|
UUID: ["12345678-1234-5678-1234-567812345678"],
|
||||||
StringType: ["hello world"]
|
StringType: ["hello world"]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +62,15 @@ class SerializationTest(unittest.TestCase):
|
|||||||
self.assertEqual(VarInt.size(2), 1)
|
self.assertEqual(VarInt.size(2), 1)
|
||||||
self.assertEqual(VarInt.size(1250), 2)
|
self.assertEqual(VarInt.size(1250), 2)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
packet_buffer = PacketBuffer()
|
packet_buffer = PacketBuffer()
|
||||||
VarInt.send(50000, packet_buffer)
|
VarInt.send(50000, packet_buffer)
|
||||||
packet_buffer.reset_cursor()
|
packet_buffer.reset_cursor()
|
||||||
|
|||||||
Reference in new issue
Block a user