Esempio n. 1
0
class UDPSocketConnectionTest(TestCase):
    def setUp(self):
        socket = Mock()
        socket.recvfrom = Mock()
        socket.sendto = Mock()
        with patch("socket.socket") as create_socket:
            create_socket.return_value = socket
            self.connection = UDPSocketConnection(("localhost", 1234))

    def test_flush(self):
        self.assertRaises(TypeError, self.connection.flush)

    def test_receive(self):
        self.assertRaises(TypeError, self.connection.receive, "")

    def test_remaining(self):
        self.assertEqual(self.connection.remaining(), 65535)

    def test_read(self):
        self.connection.socket.recvfrom.return_value = [bytearray.fromhex("7FAA")]

        self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.sendto.assert_called_once_with(bytearray.fromhex("7FAA"), ("localhost", 1234))
Esempio n. 2
0
 def setup_method(self):
     socket = Mock()
     socket.recvfrom = Mock()
     socket.sendto = Mock()
     with patch("socket.socket") as create_socket:
         create_socket.return_value = socket
         self.connection = UDPSocketConnection(("localhost", 1234))
Esempio n. 3
0
class UDPSocketConnectionTest(TestCase):
    def setUp(self):
        socket = Mock()
        socket.recvfrom = Mock()
        socket.sendto = Mock()
        with patch("socket.socket") as create_socket:
            create_socket.return_value = socket
            self.connection = UDPSocketConnection(("localhost", 1234))

    def test_flush(self):
        self.assertRaises(TypeError, self.connection.flush)

    def test_receive(self):
        self.assertRaises(TypeError, self.connection.receive, "")

    def test_remaining(self):
        self.assertEqual(self.connection.remaining(), 65535)

    def test_read(self):
        self.connection.socket.recvfrom.return_value = [
            bytearray.fromhex("7FAA")
        ]

        self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.sendto.assert_called_once_with(
            bytearray.fromhex("7FAA"), ("localhost", 1234))
Esempio n. 4
0
class UDPSocketConnectionTest:
    def setup_method(self):
        socket = Mock()
        socket.recvfrom = Mock()
        socket.sendto = Mock()
        with patch("socket.socket") as create_socket:
            create_socket.return_value = socket
            self.connection = UDPSocketConnection(("localhost", 1234))

    def test_flush(self):
        with pytest.raises(TypeError):
            self.connection.flush()

    def test_receive(self):
        with pytest.raises(TypeError):
            self.connection.receive("")

    def test_remaining(self):
        assert self.connection.remaining() == 65535

    def test_read(self):
        self.connection.socket.recvfrom.return_value = [bytearray.fromhex("7FAA")]

        assert self.connection.read(2) == bytearray.fromhex("7FAA")

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.sendto.assert_called_once_with(bytearray.fromhex("7FAA"), ("localhost", 1234))
Esempio n. 5
0
 def setUp(self):
     socket = Mock()
     socket.recvfrom = Mock()
     socket.sendto = Mock()
     with patch("socket.socket") as create_socket:
         create_socket.return_value = socket
         self.connection = UDPSocketConnection(("localhost", 1234))
Esempio n. 6
0
    def query(self, tries: int = 3):
        """Checks the status of a Minecraft Java Edition server via the query protocol.

        :param int tries: How many times to retry if it fails.
        :return: Query status information in a `QueryResponse` instance.
        :rtype: QueryResponse
        """

        exception = None
        host = self.host
        try:
            answers = dns.resolver.query(host, "A")
            if len(answers):
                answer = answers[0]
                host = str(answer).rstrip(".")
        except Exception as e:
            pass
        for attempt in range(tries):
            try:
                connection = UDPSocketConnection((host, self.port))
                querier = ServerQuerier(connection)
                querier.handshake()
                return querier.read_query()
            except Exception as e:
                exception = e
        else:
            raise exception
Esempio n. 7
0
 def query(self, retries=3):
     exception = None
     host = self.host
     try:
         answers = dns.resolver.query(host, "A")
         if len(answers):
             answer = answers[0]
             host = str(answer).rstrip(".")
     except Exception as e:
         pass
     for attempt in range(retries):
         try:
             connection = UDPSocketConnection((host, self.port))
             querier = ServerQuerier(connection)
             querier.handshake()
             return querier.read_query()
         except Exception as e:
             exception = e
     else:
         raise exception