Esempio n. 1
0
def test_incorrect_ConnectionStart_handling(arguments):
    conn = Connection(locale=b'ru_RU')
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(**arguments)
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    with pytest.raises(amqproto.exceptions.UnrecoverableError):
        conn.handle_frame(frame)
Esempio n. 2
0
def test_ConnectionOpenOK_handling():
    conn = Connection()
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    channel_max = 10
    frame_max = 131072
    heartbeat = 100
    payload = amqpframe.methods.ConnectionTune(
        channel_max=channel_max,
        frame_max=frame_max,
        heartbeat=heartbeat,
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    payload = amqpframe.methods.ConnectionOpenOK()
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    assert fut.done() and not fut.cancelled()
    assert conn.alive
Esempio n. 3
0
def test_ConnectionSecure_handling():
    conn = Connection(auth=CustomAuth(b'foo', b'bar'))
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN CUSTOMAUTH',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    payload = amqpframe.methods.ConnectionSecure(challenge=b'123')
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    method_bytes = io.BytesIO()
    method = amqpframe.methods.ConnectionSecureOK(response=b'246')
    method.to_bytestream(method_bytes)

    assert method_bytes.getvalue() in conn.data_to_send()
    assert conn._handshake_properties['secure']['challenge'] == b'123'
    assert conn._handshake_properties['secure']['response'] == b'246'
Esempio n. 4
0
def test_incorrect_connection_initiation():
    conn = Connection()
    fut = conn.initiate_connection()

    payload = amqpframe.ProtocolHeaderPayload(1, 0, 0)
    frame = amqpframe.ProtocolHeaderFrame(conn._channel_id, payload)

    with pytest.raises(amqproto.exceptions.UnsupportedProtocol) as excinfo:
        conn.handle_frame(frame)

    exc = excinfo.value
    assert exc.protocol_major == 1
    assert exc.protocol_minor == 0
    assert exc.protocol_revision == 0
Esempio n. 5
0
def test_correct_connection_initiation():
    conn = Connection(
        protocol_major=12,
        protocol_minor=34,
        protocol_revision=56,
    )
    assert conn._channel_id == 0
    fut = conn.initiate_connection()

    frame_bytes = io.BytesIO()
    payload = amqpframe.ProtocolHeaderPayload(12, 34, 56)
    frame = amqpframe.ProtocolHeaderFrame(conn._channel_id, payload)
    frame.to_bytestream(frame_bytes)

    assert frame_bytes.getvalue() in conn.data_to_send()
Esempio n. 6
0
def test_ConnectionTune_handling(properties):
    conn = Connection(**properties['client'])
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    payload = amqpframe.methods.ConnectionTune(**properties['server'])
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    negotiated = properties['negotiated']
    for prop in ('channel_max', 'frame_max', 'heartbeat'):
        assert conn.properties[prop] == negotiated[prop]

    data_to_send = conn.data_to_send()

    # Make sure ConnectionTuneOK is sent
    stream = io.BytesIO()
    method = amqpframe.methods.ConnectionTuneOK(**conn.properties)
    method.to_bytestream(stream)
    method_bytes = stream.getvalue()

    assert method_bytes in data_to_send

    # Make sure ConnectionOpen is also sent
    stream = io.BytesIO()
    method = amqpframe.methods.ConnectionOpen(virtual_host='/')
    method.to_bytestream(stream)
    method_bytes = stream.getvalue()

    assert method_bytes in data_to_send
Esempio n. 7
0
def test_correct_ConnectionStart_handling():
    conn = Connection()
    fut = conn.initiate_connection()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(conn._channel_id, payload)

    conn.handle_frame(frame)

    # Make sure all properties are handled
    server_properties = conn._handshake_properties['server']
    assert server_properties['properties']
    assert server_properties['locales'] == [b'en_US', b'ru_RU']
    assert server_properties['mechanisms'] == [b'PLAIN', b'AMQPLAIN']

    chosen_properties = conn._handshake_properties['chosen']
    assert chosen_properties['locale'] == b'en_US'
    assert chosen_properties['mechanism'] == b'PLAIN'

    # Make sure ConnectionStartOK is sent
    stream = io.BytesIO()
    PLAIN(b'guest', b'guest').to_bytestream(stream)
    response = stream.getvalue()

    stream = io.BytesIO()
    client_properties = conn._handshake_properties['client']['properties']
    mechanism = conn._handshake_properties['chosen']['mechanism']
    locale = conn._handshake_properties['chosen']['locale']

    method = amqpframe.methods.ConnectionStartOK(
        client_properties=client_properties,
        mechanism=mechanism,
        response=response,
        locale=locale)
    method.to_bytestream(stream)
    method_bytes = stream.getvalue()

    assert method_bytes in conn.data_to_send()
Esempio n. 8
0
def ready_connection():
    conn = Connection(heartbeat=1)
    conn.initiate_connection()

    conn._heartbeater.update_received_time()

    payload = amqpframe.methods.ConnectionStart(
        version_major=0,
        version_minor=9,
        server_properties={'foo': 'bar'},
        mechanisms=b'PLAIN AMQPLAIN',
        locales=b'en_US ru_RU',
    )
    frame = amqpframe.MethodFrame(0, payload)
    conn.handle_frame(frame)

    conn._heartbeater.update_received_time()

    channel_max = 2
    frame_max = 1000
    heartbeat = 1
    payload = amqpframe.methods.ConnectionTune(
        channel_max=channel_max,
        frame_max=frame_max,
        heartbeat=heartbeat,
    )
    frame = amqpframe.MethodFrame(0, payload)
    conn.handle_frame(frame)

    conn._heartbeater.update_received_time()

    payload = amqpframe.methods.ConnectionOpenOK()
    frame = amqpframe.MethodFrame(0, payload)
    conn.handle_frame(frame)

    conn.data_to_send()
    return conn