Esempio n. 1
0
class AllowedVersionsTest(fake_server._FakeServerTest):
    versions = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda p: p[1])
    versions = dict((versions[0], versions[len(versions) // 2], versions[-1]))

    client_handler_type = ConnectTest.client_handler_type

    def test_with_version_names(self):
        for version, proto in AllowedVersionsTest.versions.items():
            client_versions = {
                v
                for (v, p) in SUPPORTED_MINECRAFT_VERSIONS.items()
                if p <= proto
            }
            self._test_connect(server_version=version,
                               client_versions=client_versions)

    def test_with_protocol_numbers(self):
        for version, proto in AllowedVersionsTest.versions.items():
            client_versions = {
                p
                for (v, p) in SUPPORTED_MINECRAFT_VERSIONS.items()
                if p <= proto
            }
            self._test_connect(server_version=version,
                               client_versions=client_versions)
Esempio n. 2
0
 def test_with_protocol_numbers(self):
     for version, proto in AllowedVersionsTest.versions.items():
         client_versions = {
             p for (v, p) in SUPPORTED_MINECRAFT_VERSIONS.items()
             if p <= proto}
         self._test_connect(
             server_version=version, client_versions=client_versions)
Esempio n. 3
0
 def test_with_protocol_numbers(self):
     for version, proto in AllowedVersionsTest.versions.items():
         client_versions = {
             p for (v, p) in SUPPORTED_MINECRAFT_VERSIONS.items()
             if p <= proto}
         self._test_connect(
             server_version=version, client_versions=client_versions)
Esempio n. 4
0
    def __init__(
        self,
        minecraft_version=None,
        compression_threshold=None,
        client_handler_type=FakeClientHandler,
        private_key=None,
        public_key_bytes=None,
        test_case=None,
    ):
        if minecraft_version is None:
            minecraft_version = VERSIONS[-1][0]

        if isinstance(minecraft_version, Integral):
            proto = minecraft_version
            minecraft_version = "FakeVersion%d" % proto
            for ver, ver_proto in SUPPORTED_MINECRAFT_VERSIONS.items():
                if ver_proto == proto:
                    minecraft_version = ver
        else:
            proto = SUPPORTED_MINECRAFT_VERSIONS[minecraft_version]
        self.context = connection.ConnectionContext(protocol_version=proto)

        self.minecraft_version = minecraft_version
        self.compression_threshold = compression_threshold
        self.client_handler_type = client_handler_type
        self.private_key = private_key
        self.public_key_bytes = public_key_bytes
        self.test_case = test_case

        self.packets_handshake = {
            p.get_id(self.context): p
            for p in serverbound.handshake.get_packets(self.context)
        }

        self.packets_login = {
            p.get_id(self.context): p
            for p in serverbound.login.get_packets(self.context)
        }

        self.packets_playing = {
            p.get_id(self.context): p
            for p in serverbound.play.get_packets(self.context)
        }

        self.packets_status = {
            p.get_id(self.context): p
            for p in serverbound.status.get_packets(self.context)
        }

        self.listen_socket = socket.socket()
        self.listen_socket.settimeout(0.1)
        self.listen_socket.bind(("localhost", 0))
        self.listen_socket.listen(0)

        self.lock = threading.Lock()
        self.stopping = False

        super(FakeServer, self).__init__()
Esempio n. 5
0
    def __init__(self, minecraft_version=None, compression_threshold=None,
                 client_handler_type=FakeClientHandler, private_key=None,
                 public_key_bytes=None, test_case=None):
        if minecraft_version is None:
            minecraft_version = VERSIONS[-1][0]

        if isinstance(minecraft_version, Integral):
            proto = minecraft_version
            minecraft_version = 'FakeVersion%d' % proto
            for ver, ver_proto in SUPPORTED_MINECRAFT_VERSIONS.items():
                if ver_proto == proto:
                    minecraft_version = ver
        else:
            proto = SUPPORTED_MINECRAFT_VERSIONS[minecraft_version]
        self.context = connection.ConnectionContext(protocol_version=proto)

        self.minecraft_version = minecraft_version
        self.compression_threshold = compression_threshold
        self.client_handler_type = client_handler_type
        self.private_key = private_key
        self.public_key_bytes = public_key_bytes
        self.test_case = test_case

        self.packets_handshake = {
            p.get_id(self.context): p for p in
            serverbound.handshake.get_packets(self.context)}

        self.packets_login = {
            p.get_id(self.context): p for p in
            serverbound.login.get_packets(self.context)}

        self.packets_playing = {
            p.get_id(self.context): p for p in
            serverbound.play.get_packets(self.context)}

        self.packets_status = {
            p.get_id(self.context): p for p in
            serverbound.status.get_packets(self.context)}

        self.listen_socket = socket.socket()
        self.listen_socket.settimeout(0.1)
        self.listen_socket.bind(('localhost', 0))
        self.listen_socket.listen(0)

        self.lock = threading.Lock()
        self.stopping = False

        super(FakeServer, self).__init__()
Esempio n. 6
0
class AllowedVersionsTest(fake_server._FakeServerTest):
    versions = list(SUPPORTED_MINECRAFT_VERSIONS.items())
    test_indices = (0, len(versions) // 2, len(versions) - 1)

    client_handler_type = ConnectTest.client_handler_type

    def test_with_version_names(self):
        for index in self.test_indices:
            self._test_connect(
                server_version=self.versions[index][0],
                client_versions={v[0] for v in self.versions[:index+1]})

    def test_with_protocol_numbers(self):
        for index in self.test_indices:
            self._test_connect(
                server_version=self.versions[index][0],
                client_versions={v[1] for v in self.versions[:index+1]})
Esempio n. 7
0
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15

from numbers import Integral
import unittest
import threading
import logging
import socket
import json
import sys
import zlib
import hashlib
import uuid


VERSIONS = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda i: i[1])
VERSIONS = [v for (v, p) in VERSIONS]

THREAD_TIMEOUT_S = 2


class FakeClientDisconnect(Exception):
    """ Raised by 'FakeClientHandler.read_packet' if the client has cleanly
        disconnected prior to the call.
    """


class FakeServerDisconnect(Exception):
    """ May be raised within 'FakeClientHandler.handle_*' in order to terminate
        the client's connection. 'message' is provided as an argument to
        'handle_play_server_disconnect' or 'handle_login_server_disconnect'.
Esempio n. 8
0
from minecraft import SUPPORTED_MINECRAFT_VERSIONS
from minecraft.networking import connection
from minecraft.networking import types
from minecraft.networking import packets

from future.utils import raise_

import unittest
import threading
import logging
import socket
import json
import sys

VERSIONS = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda i: i[1])
THREAD_TIMEOUT_S = 5


class _ConnectTest(unittest.TestCase):
    def _test_connect(self, client_version=None, server_version=None):
        server = FakeServer(minecraft_version=server_version)
        addr, port = server.listen_socket.getsockname()

        cond = threading.Condition()

        def handle_client_exception(exc, exc_info):
            with cond:
                cond.exc_info = exc_info
                cond.notify_all()