Update tests to match new API.

This commit is contained in:
joo committed 2016-06-17 22:54:21 +01:00
1 parent c8b3e7a809
commit 3ff7662684
7 files changed
+85 -62

No files matched your search

-1
View File
@@ -1,7 +1,6 @@
from __future__ import print_function from __future__ import print_function
from collections import deque from collections import deque
from collections import namedtuple
from threading import Lock from threading import Lock
from zlib import decompress from zlib import decompress
from itertools import * from itertools import *
+4
View File
@@ -401,6 +401,10 @@ class PlayerListItemPacket(Packet):
self.uuid = UUID.read(file_object) self.uuid = UUID.read(file_object)
self._read(file_object) self._read(file_object)
def _read(self, file_object):
raise NotImplementedError(
'This abstract method must be overridden in a subclass.')
@classmethod @classmethod
def type_from_id(cls, action_id): def type_from_id(cls, action_id):
subcls = { subcls = {
+4 -1
View File
@@ -84,7 +84,10 @@ class VarInt(Type):
def read(file_object): def read(file_object):
number = 0 number = 0
for i in range(5): for i in range(5):
byte = ord(file_object.read(1)) byte = file_object.read(1)
if len(byte) < 1:
raise RuntimeError("Unexpected end of message.")
byte = ord(byte)
number |= (byte & 0x7F) << 7 * i number |= (byte & 0x7F) << 7 * i
if not byte & 0x80: if not byte & 0x80:
break break
+61 -47
View File
@@ -3,6 +3,9 @@ import unittest
import string import string
from zlib import decompress from zlib import decompress
from random import choice from random import choice
from minecraft import SUPPORTED_PROTOCOL_VERSIONS
from minecraft.networking.connection import ConnectionContext
from minecraft.networking.types import VarInt from minecraft.networking.types import VarInt
from minecraft.networking.packets import ( from minecraft.networking.packets import (
PacketBuffer, ChatPacket, KeepAlivePacket, PacketListener) PacketBuffer, ChatPacket, KeepAlivePacket, PacketListener)
@@ -11,68 +14,79 @@ from minecraft.networking.packets import (
class PacketSerializatonTest(unittest.TestCase): class PacketSerializatonTest(unittest.TestCase):
def test_packet(self): def test_packet(self):
packet = ChatPacket() for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
packet.message = u"κόσμε" context = ConnectionContext(protocol_version=protocol_version)
packet_buffer = PacketBuffer() packet = ChatPacket(context)
packet.write(packet_buffer) packet.message = u"κόσμε"
packet_buffer.reset_cursor() packet_buffer = PacketBuffer()
# Read the length and packet id packet.write(packet_buffer)
VarInt.read(packet_buffer)
packet_id = VarInt.read(packet_buffer) packet_buffer.reset_cursor()
self.assertEqual(packet_id, packet.id) # Read the length and packet id
VarInt.read(packet_buffer)
deserialized = ChatPacket() packet_id = VarInt.read(packet_buffer)
deserialized.read(packet_buffer) self.assertEqual(packet_id, packet.id)
self.assertEqual(packet.message, deserialized.message) deserialized = ChatPacket(context)
deserialized.read(packet_buffer)
self.assertEqual(packet.message, deserialized.message)
def test_compressed_packet(self): def test_compressed_packet(self):
msg = ''.join(choice(string.ascii_lowercase) for i in range(500)) for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
packet = ChatPacket() context = ConnectionContext(protocol_version=protocol_version)
packet.message = msg
self.write_read_packet(packet, 20) msg = ''.join(choice(string.ascii_lowercase) for i in range(500))
self.write_read_packet(packet, -1) packet = ChatPacket(context)
packet.message = msg
self.write_read_packet(packet, 20)
self.write_read_packet(packet, -1)
def write_read_packet(self, packet, compression_threshold): def write_read_packet(self, packet, compression_threshold):
for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
context = ConnectionContext(protocol_version=protocol_version)
packet_buffer = PacketBuffer() packet_buffer = PacketBuffer()
packet.write(packet_buffer, compression_threshold) 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_buffer.reset_cursor()
packet_id = VarInt.read(packet_buffer) VarInt.read(packet_buffer)
self.assertEqual(packet_id, packet.id) compressed_size = VarInt.read(packet_buffer)
deserialized = ChatPacket() if compressed_size > 0:
deserialized.read(packet_buffer) decompressed = decompress(packet_buffer.read(compressed_size))
packet_buffer.reset()
self.assertEqual(packet.message, deserialized.message) packet_buffer.send(decompressed)
packet_buffer.reset_cursor()
packet_id = VarInt.read(packet_buffer)
self.assertEqual(packet_id, packet.id)
deserialized = ChatPacket(context)
deserialized.read(packet_buffer)
self.assertEqual(packet.message, deserialized.message)
class PacketListenerTest(unittest.TestCase): class PacketListenerTest(unittest.TestCase):
def test_listener(self): def test_listener(self):
message = "hello world" message = "hello world"
def test_packet(chat_packet): def test_packet(chat_packet):
self.assertEqual(chat_packet.message, message) self.assertEqual(chat_packet.message, message)
listener = PacketListener(test_packet, ChatPacket) for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
context = ConnectionContext(protocol_version=protocol_version)
packet = ChatPacket().set_values(message=message) listener = PacketListener(test_packet, ChatPacket)
uncalled_packet = KeepAlivePacket().set_values(keep_alive_id=0)
packet = ChatPacket().set_values(message=message)
listener.call_packet(packet) uncalled_packet = KeepAlivePacket().set_values(keep_alive_id=0)
listener.call_packet(uncalled_packet)
listener.call_packet(packet)
listener.call_packet(uncalled_packet)
+3 -3
View File
@@ -53,8 +53,8 @@ class SerializationTest(unittest.TestCase):
base_type.send(None, None) base_type.send(None, None)
empty_socket = PacketBuffer() empty_socket = PacketBuffer()
with self.assertRaises(RuntimeError): with self.assertRaises(Exception):
VarInt.read_socket(empty_socket) VarInt.read(empty_socket)
def test_varint(self): def test_varint(self):
self.assertEqual(VarInt.size(2), 1) self.assertEqual(VarInt.size(2), 1)
@@ -64,4 +64,4 @@ class SerializationTest(unittest.TestCase):
VarInt.send(50000, packet_buffer) VarInt.send(50000, packet_buffer)
packet_buffer.reset_cursor() packet_buffer.reset_cursor()
self.assertEqual(VarInt.read_socket(packet_buffer), 50000) self.assertEqual(VarInt.read(packet_buffer), 50000)
+4 -2
View File
@@ -9,7 +9,9 @@ class VersionTest(unittest.TestCase):
SV(minecraft.__version__) SV(minecraft.__version__)
def test_minecraft_version_is_a_valid_pep_386_strict_version(self): def test_minecraft_version_is_a_valid_pep_386_strict_version(self):
SV(minecraft.MINECRAFT_VERSION) for version in minecraft.SUPPORTED_MINECRAFT_VERSIONS.keys():
SV(version)
def test_protocol_version_is_an_int(self): def test_protocol_version_is_an_int(self):
self.assertTrue(type(minecraft.PROTOCOL_VERSION) is int) for version in minecraft.SUPPORTED_PROTOCOL_VERSIONS:
self.assertTrue(type(version) is int)
+9 -8
View File
@@ -4,7 +4,7 @@
# and then run "tox" from this directory. # and then run "tox" from this directory.
[tox] [tox]
envlist = py27, py33, py34, pypy, cover, flake8, pylint-errors, pylint-full, verify-manifest envlist = py27, py33, py34, py35, pypy, cover, flake8, pylint-errors, pylint-full, verify-manifest
[testenv] [testenv]
commands = nosetests commands = nosetests
@@ -13,6 +13,7 @@ deps =
nose nose
requests requests
cryptography cryptography
future
[testenv:py27] [testenv:py27]
deps = deps =
@@ -25,7 +26,7 @@ deps =
mock mock
[testenv:cover] [testenv:cover]
basepython = python3.4 basepython = python3.5
commands = commands =
nosetests --with-xunit --with-xcoverage --cover-package=minecraft --nocapture --cover-erase --cover-inclusive --cover-tests --cover-branches --cover-min-percentage=60 nosetests --with-xunit --with-xcoverage --cover-package=minecraft --nocapture --cover-erase --cover-inclusive --cover-tests --cover-branches --cover-min-percentage=60
deps = deps =
@@ -43,7 +44,7 @@ deps =
coveralls coveralls
[testenv:flake8] [testenv:flake8]
basepython = python3.4 basepython = python3.5
commands = commands =
flake8 minecraft tests setup.py start.py bin/generate_travis_yml.py flake8 minecraft tests setup.py start.py bin/generate_travis_yml.py
deps = deps =
@@ -51,14 +52,14 @@ deps =
flake8 flake8
[testenv:pylint-errors] [testenv:pylint-errors]
basepython = python3.4 basepython = python3.5
deps = deps =
{[testenv]deps} {[testenv]deps}
pylint pylint
commands = pylint minecraft -E commands = pylint minecraft -E
[testenv:pylint-full] [testenv:pylint-full]
basepython = python3.4 basepython = python3.5
deps = deps =
{[testenv]deps} {[testenv]deps}
pylint pylint
@@ -66,7 +67,7 @@ commands =
- pylint minecraft --disable=E - pylint minecraft --disable=E
[testenv:docs] [testenv:docs]
basepython = python3.4 basepython = python3.5
deps = deps =
{[testenv:cover]deps} {[testenv:cover]deps}
sphinx sphinx
@@ -75,9 +76,9 @@ commands =
{toxinidir}/bin/build_docs {toxinidir}/bin/build_docs
[testenv:verify-manifest] [testenv:verify-manifest]
basepython = python3.4 basepython = python3.5
deps = deps =
check-manifest check-manifest
commands = commands =
check-manifest check-manifest