Exemplo n.º 1
0
def test_QDataStreamProtocol_recv_small_message(protocol,reader):
    data = QDataStreamProtocol.pack_block(b''.join([QDataStreamProtocol.pack_qstring('{"some_header": true}'),
                                                    QDataStreamProtocol.pack_qstring('Goodbye')]))
    reader.feed_data(data)

    message = yield from protocol.read_message()

    assert message == {'some_header': True, 'legacy': ['Goodbye']}
Exemplo n.º 2
0
async def test_QDataStreamProtocol_recv_small_message(protocol, reader):
    data = QDataStreamProtocol.pack_block(b''.join([QDataStreamProtocol.pack_qstring('{"some_header": true}'),
                                                    QDataStreamProtocol.pack_qstring('Goodbye')]))
    reader.feed_data(data)

    message = await protocol.read_message()

    assert message == {'some_header': True, 'legacy': ['Goodbye']}
Exemplo n.º 3
0
async def test_QDataStreamProtocol_recv_small_message(protocol, reader):
    data = QDataStreamProtocol.pack_block(b"".join([QDataStreamProtocol.pack_qstring('{"some_header": true}'),
                                                    QDataStreamProtocol.pack_qstring("Goodbye")]))
    reader.feed_data(data)

    message = await protocol.read_message()

    assert message == {"some_header": True, "legacy": ["Goodbye"]}
Exemplo n.º 4
0
def unix_protocol(unix_srv, event_loop):
    (reader, writer) = event_loop.run_until_complete(
        asyncio.open_unix_connection('/tmp/test.sock')
    )
    protocol = QDataStreamProtocol(reader, writer)
    yield protocol

    protocol.close()
Exemplo n.º 5
0
def test_serverside_abort(loop, mock_context, mock_server):
    (reader, writer) = yield from asyncio.open_connection(*mock_context.sockets[0].getsockname())
    proto = QDataStreamProtocol(reader, writer)
    proto.send_message({"some_junk": True})
    yield from writer.drain()
    yield from asyncio.sleep(0.1)

    mock_server.on_connection_lost.assert_any_call()
Exemplo n.º 6
0
def test_serverside_abort(loop, mock_context, mock_server):
    (reader, writer) = yield from asyncio.open_connection(*mock_context.sockets[0].getsockname())
    proto = QDataStreamProtocol(reader, writer)
    proto.send_message({"some_junk": True})
    yield from writer.drain()
    yield from asyncio.sleep(0.1)

    mock_server.on_connection_lost.assert_any_call()
Exemplo n.º 7
0
def test_QDataStreamProtocol_recv_large_array(protocol, reader):
    reader.feed_data(QDataStreamProtocol.pack_block(b''.join(
        [QDataStreamProtocol.pack_qstring('{"some_header": true}')] +
        [QDataStreamProtocol.pack_qstring(str(i)) for i in range(1520)])))
    reader.feed_eof()

    message = yield from protocol.read_message()

    assert message == {'some_header': True, 'legacy': [str(i) for i in range(1520)]}
Exemplo n.º 8
0
async def test_QDataStreamProtocol_recv_large_array(protocol, reader):
    reader.feed_data(QDataStreamProtocol.pack_block(b"".join(
        [QDataStreamProtocol.pack_qstring('{"some_header": true}')] +
        [QDataStreamProtocol.pack_qstring(str(i)) for i in range(1520)])))
    reader.feed_eof()

    message = await protocol.read_message()

    assert message == {"some_header": True, "legacy": [str(i) for i in range(1520)]}
Exemplo n.º 9
0
def test_QDataStreamProtocol_recv_large_array(protocol, reader):
    reader.feed_data(
        QDataStreamProtocol.pack_block(b''.join(
            [QDataStreamProtocol.pack_qstring('{"some_header": true}')] +
            [QDataStreamProtocol.pack_qstring(str(i)) for i in range(1520)])))
    reader.feed_eof()

    message = yield from protocol.read_message()

    assert message == {
        'some_header': True,
        'legacy': [str(i) for i in range(1520)]
    }
Exemplo n.º 10
0
async def perform_login(
    proto: QDataStreamProtocol, credentials: Tuple[str, str]
) -> None:
    login, pw = credentials
    pw_hash = hashlib.sha256(pw.encode('utf-8'))
    proto.send_message({
        'command': 'hello',
        'version': '1.0.0-dev',
        'user_agent': 'faf-client',
        'login': login,
        'password': pw_hash.hexdigest(),
        'unique_id': 'some_id'
    })
    await proto.drain()
Exemplo n.º 11
0
 async def client_connected(self, stream_reader, stream_writer):
     self._logger.debug("%s: Client connected", self)
     protocol = QDataStreamProtocol(stream_reader, stream_writer)
     connection = self._connection_factory()
     try:
         await connection.on_connection_made(
             protocol, Address(*stream_writer.get_extra_info('peername')))
         self.connections[connection] = protocol
         server.stats.gauge('user.agents.None', 1, delta=True)
         while True:
             message = await protocol.read_message()
             with server.stats.timer('connection.on_message_received'):
                 await connection.on_message_received(message)
             with server.stats.timer('servercontext.drain'):
                 await asyncio.sleep(0)
                 await connection.drain()
     except ConnectionResetError:
         pass
     except ConnectionAbortedError:
         pass
     except TimeoutError:
         pass
     except asyncio.IncompleteReadError as ex:
         if not stream_reader.at_eof():
             self._logger.exception(ex)
     except Exception as ex:
         self._logger.exception(ex)
     finally:
         del self.connections[connection]
         server.stats.gauge('user.agents.{}'.format(connection.user_agent),
                            -1,
                            delta=True)
         protocol.writer.close()
         await connection.on_connection_lost()
Exemplo n.º 12
0
async def test_pack_unpack(protocol, reader):
    message = {
        "Some": "crazy",
        "Message": ["message", 10],
        "with": 1000
    }
    reader.feed_data(QDataStreamProtocol.pack_message(json.dumps(message)))

    assert message == await protocol.read_message()
Exemplo n.º 13
0
async def test_serverside_abort(event_loop, mock_context, mock_server):
    srv, ctx = mock_context
    (reader,
     writer) = await asyncio.open_connection(*srv.sockets[0].getsockname())
    proto = QDataStreamProtocol(reader, writer)
    await proto.send_message({"some_junk": True})
    await exhaust_callbacks(event_loop)

    mock_server.on_connection_lost.assert_any_call()
Exemplo n.º 14
0
async def test_QDataStreamProtocol_pack_unpack(protocol, reader, message):
    reader.feed_data(QDataStreamProtocol.pack_message(json.dumps(message)))

    assert message == await protocol.read_message()
Exemplo n.º 15
0
def protocol(reader, writer):
    return QDataStreamProtocol(reader, writer)
Exemplo n.º 16
0
def mock_protocol():
    return mock.create_autospec(QDataStreamProtocol(mock.Mock(), mock.Mock()))
Exemplo n.º 17
0
 async def connect(self, host, port):
     self.proto = QDataStreamProtocol(
         *(await asyncio.open_connection(host, port)))
Exemplo n.º 18
0
async def test_types():
    with pytest.raises(NotImplementedError):
        QDataStreamProtocol.pack_message({"Not": ["a", "string"]})
Exemplo n.º 19
0
async def unix_protocol(unix_srv):
    (reader, writer) = await asyncio.open_unix_connection("/tmp/test.sock")
    protocol = QDataStreamProtocol(reader, writer)
    yield protocol

    await protocol.close()
Exemplo n.º 20
0
async def connect_client(server) -> QDataStreamProtocol:
    return QDataStreamProtocol(
        *(await asyncio.open_connection(*server.sockets[0].getsockname()))
    )
Exemplo n.º 21
0
async def test_QDataStreamProtocol_deterministic(message):
    assert (
        QDataStreamProtocol.encode_message(message) ==
        QDataStreamProtocol.encode_message(message) ==
        QDataStreamProtocol.encode_message(message)
    )
Exemplo n.º 22
0
def connect_client(server):
    return QDataStreamProtocol(*(yield from asyncio.open_connection(
        *server.sockets[0].getsockname())))
Exemplo n.º 23
0
async def test_QDataStreamProtocol_encode_ping_pong():
    assert QDataStreamProtocol.encode_message({"command": "ping"}) == \
        b"\x00\x00\x00\x0c\x00\x00\x00\x08\x00P\x00I\x00N\x00G"
    assert QDataStreamProtocol.encode_message({"command": "pong"}) == \
        b"\x00\x00\x00\x0c\x00\x00\x00\x08\x00P\x00O\x00N\x00G"