From a5a76a8e1c37e5401043a2088029bfaf85508388 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Sun, 12 Apr 2015 07:26:12 +0500 Subject: [PATCH 1/3] Added more tests, fixed compat.py to run on py3 as well --- minecraft/compat.py | 2 +- minecraft/networking/packets.py | 7 ++- minecraft/networking/types.py | 6 +-- tests/test_packets.py | 78 +++++++++++++++++++++++++++++++++ tests/test_serialization.py | 67 ++++++---------------------- 5 files changed, 101 insertions(+), 59 deletions(-) create mode 100644 tests/test_packets.py diff --git a/minecraft/compat.py b/minecraft/compat.py index 865d71c..9a06b6d 100644 --- a/minecraft/compat.py +++ b/minecraft/compat.py @@ -15,5 +15,5 @@ both Python2 and Python3 while using the same codebase. try: input = raw_input except NameError: - pass + input = input # pylint: enable=undefined-variable,redefined-builtin,invalid-name diff --git a/minecraft/networking/packets.py b/minecraft/networking/packets.py index 464b0c3..cf8a9bf 100644 --- a/minecraft/networking/packets.py +++ b/minecraft/networking/packets.py @@ -54,9 +54,14 @@ class Packet(object): id = -0x01 definition = [] - def __init__(self): + def __init__(self, **kwargs): pass + def set_values(self, **kwargs): + for key, value in kwargs.items(): + setattr(self, key, value) + return self + def read(self, file_object): for field in self.definition: for var_name, data_type in field.items(): diff --git a/minecraft/networking/types.py b/minecraft/networking/types.py index 3995d35..8ce00db 100644 --- a/minecraft/networking/types.py +++ b/minecraft/networking/types.py @@ -8,11 +8,11 @@ import struct class Type(object): @staticmethod def read(file_object): - pass + raise NotImplementedError("Base data type not serializable") @staticmethod def send(value, socket): - pass + raise NotImplementedError("Base data type not serializable") # ========================================================= @@ -84,7 +84,7 @@ class VarInt(Type): number = 0 for i in range(5): byte = socket.recv(1) - if byte == "": + if byte == "" or len(byte) == 0: raise RuntimeError("Socket disconnected") byte = ord(byte) number |= (byte & 0x7F) << 7 * i diff --git a/tests/test_packets.py b/tests/test_packets.py new file mode 100644 index 0000000..2943bfe --- /dev/null +++ b/tests/test_packets.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +import unittest +import string +from zlib import decompress +from random import choice +from minecraft.networking.types import VarInt +from minecraft.networking.packets import ( + PacketBuffer, ChatPacket, KeepAlivePacket, PacketListener) + + +class PacketSerializatonTest(unittest.TestCase): + + def test_packet(self): + packet = ChatPacket() + packet.message = u"κόσμε" + + 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) + + deserialized = ChatPacket() + deserialized.read(packet_buffer) + + self.assertEqual(packet.message, deserialized.message) + + def test_compressed_packet(self): + msg = ''.join(choice(string.ascii_lowercase) for i in range(500)) + packet = ChatPacket() + packet.message = msg + + self.write_read_packet(packet, 20) + self.write_read_packet(packet, -1) + + def write_read_packet(self, packet, compression_threshold): + + packet_buffer = PacketBuffer() + packet.write(packet_buffer, compression_threshold) + + packet_buffer.reset_cursor() + + VarInt.read(packet_buffer) + compressed_size = VarInt.read(packet_buffer) + + if compressed_size > 0: + decompressed = decompress(packet_buffer.read(compressed_size)) + packet_buffer.reset() + packet_buffer.send(decompressed) + packet_buffer.reset_cursor() + + packet_id = VarInt.read(packet_buffer) + self.assertEqual(packet_id, packet.id) + + deserialized = ChatPacket() + deserialized.read(packet_buffer) + + self.assertEqual(packet.message, deserialized.message) + + +class PacketListenerTest(unittest.TestCase): + + def test_listener(self): + message = "hello world" + + def test_packet(chat_packet): + self.assertEqual(chat_packet.message, message) + + listener = PacketListener(test_packet, ChatPacket) + + packet = ChatPacket().set_values(message=message) + uncalled_packet = KeepAlivePacket().set_values(keep_alive_id=0) + + listener.call_packet(packet) + listener.call_packet(uncalled_packet) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index c45e543..6a26426 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -1,15 +1,12 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import unittest -import string -from random import choice -from zlib import decompress from minecraft.networking.types import ( Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray, VarIntPrefixedByteArray, String as StringType ) -from minecraft.networking.packets import PacketBuffer, ChatPacket +from minecraft.networking.packets import PacketBuffer TEST_DATA = { @@ -47,6 +44,18 @@ class SerializationTest(unittest.TestCase): else: self.assertEqual(test_data, deserialized) + def test_exceptions(self): + base_type = Type() + with self.assertRaises(NotImplementedError): + base_type.read(None) + + with self.assertRaises(NotImplementedError): + base_type.send(None, None) + + empty_socket = PacketBuffer() + with self.assertRaises(RuntimeError): + VarInt.read_socket(empty_socket) + def test_varint(self): self.assertEqual(VarInt.size(2), 1) self.assertEqual(VarInt.size(1250), 2) @@ -56,53 +65,3 @@ class SerializationTest(unittest.TestCase): packet_buffer.reset_cursor() self.assertEqual(VarInt.read_socket(packet_buffer), 50000) - - def test_packet(self): - packet = ChatPacket() - packet.message = u"κόσμε" - - 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) - - deserialized = ChatPacket() - deserialized.read(packet_buffer) - - self.assertEqual(packet.message, deserialized.message) - - def test_compressed_packet(self): - msg = ''.join(choice(string.ascii_lowercase) for i in range(500)) - packet = ChatPacket() - packet.message = msg - - self.write_read_packet(packet, 20) - self.write_read_packet(packet, -1) - - def write_read_packet(self, packet, compression_threshold): - - packet_buffer = PacketBuffer() - packet.write(packet_buffer, compression_threshold) - - packet_buffer.reset_cursor() - - VarInt.read(packet_buffer) - compressed_size = VarInt.read(packet_buffer) - - if compressed_size > 0: - decompressed = decompress(packet_buffer.read(compressed_size)) - packet_buffer.reset() - packet_buffer.send(decompressed) - packet_buffer.reset_cursor() - - packet_id = VarInt.read(packet_buffer) - self.assertEqual(packet_id, packet.id) - - deserialized = ChatPacket() - deserialized.read(packet_buffer) - - self.assertEqual(packet.message, deserialized.message) From 9202c4399b75bb232dda66d9eae6440ade344213 Mon Sep 17 00:00:00 2001 From: Jeppe Klitgaard Date: Wed, 15 Apr 2015 22:09:42 +0200 Subject: [PATCH 2/3] Update README.rst Travis-CI status image is now targeted at the master branch in order to prevent an ugly "build failing" image, when the build fails on development branches. --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 31c92bc..01daa89 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,6 @@ pyCraft ======= -.. image:: https://travis-ci.org/ammaraskar/pyCraft.svg +.. image:: https://travis-ci.org/ammaraskar/pyCraft.svg?branch=master :target: https://travis-ci.org/ammaraskar/pyCraft .. image:: https://readthedocs.org/projects/pycraft/badge/?version=latest :target: https://pycraft.readthedocs.org/en/latest From 89a4368c1e7a5040bc7c691c55532578c2a31ae2 Mon Sep 17 00:00:00 2001 From: Jeppe Klitgaard Date: Thu, 16 Apr 2015 09:12:26 +0200 Subject: [PATCH 3/3] Added tests for compat.py --- tests/test_compat.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/test_compat.py diff --git a/tests/test_compat.py b/tests/test_compat.py new file mode 100644 index 0000000..d114ee9 --- /dev/null +++ b/tests/test_compat.py @@ -0,0 +1,8 @@ +from minecraft import compat # noqa unused-import + +import unittest + + +class TestCompatInput(unittest.TestCase): + def test_import_input(self): + from minecraft.compat import input # noqa unused-import