Beispiel #1
0
async def test_packer_sends_packets(
    nursery,
    packer,
    remote_enr,
    remote_endpoint,
    outbound_message_channels,
    outbound_packet_channels,
):
    assert not packer.is_peer_packer_registered(remote_enr.node_id)

    # send message
    outbound_message = OutboundMessage(
        message=PingMessageFactory(),
        receiver_endpoint=remote_endpoint,
        receiver_node_id=remote_enr.node_id,
    )
    outbound_message_channels[0].send_nowait(outbound_message)

    with trio.fail_after(0.5):
        outbound_packet = await outbound_packet_channels[1].receive()

    assert packer.is_peer_packer_registered(remote_enr.node_id)

    assert isinstance(outbound_packet.packet, AuthTagPacket)
    assert outbound_packet.receiver_endpoint == remote_endpoint
Beispiel #2
0
async def test_dispatcher_subscribe_request_response(tester, alice, bob):
    carol = tester.node()
    driver_a = tester.session_pair(alice, bob)
    driver_b = tester.session_pair(carol, alice)

    await driver_a.handshake()
    await driver_b.handshake()

    async with tester.dispatcher_pair(alice, bob) as (alice_dispatcher, _):
        async with tester.dispatcher_pair(alice, carol):
            request = OutboundMessage(
                PingMessage(b"\x12", alice.enr.sequence_number),
                bob.endpoint,
                bob.node_id,
            )
            async with alice_dispatcher.subscribe_request(
                    request, PongMessage) as subscription:
                await driver_a.initiator.send_ping(b"\x12")
                await driver_b.initiator.send_ping(b"\x34")
                await driver_b.recipient.send_pong(b"\x34")
                await driver_a.recipient.send_pong(b"\x12")

                with trio.fail_after(1):
                    response = await subscription.receive()

    assert response.sender_node_id == bob.node_id
    assert response.message.request_id == b"\x12"
Beispiel #3
0
async def test_full_peer_packer_handshake(
    peer_packer,
    remote_peer_packer,
    endpoint,
    remote_endpoint,
    enr,
    remote_enr,
    outbound_message_channels,
    remote_outbound_message_channels,
    inbound_message_channels,
    remote_inbound_message_channels,
    bridged_channels,
    nursery,
):
    # to remote
    outbound_message = OutboundMessage(
        message=PingMessageFactory(),
        receiver_endpoint=remote_endpoint,
        receiver_node_id=remote_enr.node_id,
    )
    outbound_message_channels[0].send_nowait(outbound_message)

    with trio.fail_after(0.5):
        inbound_message = await remote_inbound_message_channels[1].receive()

    assert inbound_message.message == outbound_message.message
    assert inbound_message.sender_endpoint == endpoint
    assert inbound_message.sender_node_id == enr.node_id

    # from remote
    outbound_message = OutboundMessage(
        message=PingMessageFactory(),
        receiver_endpoint=endpoint,
        receiver_node_id=enr.node_id,
    )
    remote_outbound_message_channels[0].send_nowait(outbound_message)

    with trio.fail_after(0.5):
        inbound_message = await inbound_message_channels[1].receive()

    assert inbound_message.message == outbound_message.message
    assert inbound_message.sender_endpoint == remote_endpoint
    assert inbound_message.sender_node_id == remote_enr.node_id
Beispiel #4
0
async def test_dispatcher_send_message_with_existing_session(
        driver, alice, bob, paired_dispatchers):
    alice_dispatcher, _ = paired_dispatchers

    async with bob.events.ping_received.subscribe_and_wait():
        await alice_dispatcher.send_message(
            OutboundMessage(
                PingMessage(b"\x12", alice.enr.sequence_number),
                bob.endpoint,
                bob.node_id,
            ))

    async with bob.events.ping_received.subscribe_and_wait():
        await alice_dispatcher.send_message(
            OutboundMessage(
                PingMessage(b"\x34", alice.enr.sequence_number),
                bob.endpoint,
                bob.node_id,
            ))
Beispiel #5
0
async def test_dispatcher_send_message_creates_session(tester, driver, alice,
                                                       paired_dispatchers):
    carol = tester.node()
    alice_dispatcher, _ = paired_dispatchers

    async with alice.events.session_created.subscribe_and_wait():
        await alice_dispatcher.send_message(
            OutboundMessage(
                PingMessage(b"\x12", alice.enr.sequence_number),
                carol.endpoint,
                carol.node_id,
            ))
Beispiel #6
0
async def test_dispatcher_bidirectional_communication(driver, alice, bob,
                                                      paired_dispatchers):
    alice_dispatcher, bob_dispatcher = paired_dispatchers
    async with alice.events.session_handshake_complete.subscribe_and_wait():
        async with bob.events.session_handshake_complete.subscribe_and_wait():
            async with alice.events.ping_sent.subscribe_and_wait():
                async with bob.events.ping_received.subscribe_and_wait():
                    await alice_dispatcher.send_message(
                        OutboundMessage(
                            PingMessage(b"\x12", alice.enr.sequence_number),
                            bob.endpoint,
                            bob.node_id,
                        ))
Beispiel #7
0
async def test_peer_packer_sends_auth_header(
    peer_packer,
    enr,
    remote_private_key,
    remote_enr,
    remote_endpoint,
    inbound_packet_channels,
    outbound_packet_channels,
    outbound_message_channels,
    nursery,
):
    outbound_message = OutboundMessage(
        PingMessageFactory(),
        remote_endpoint,
        peer_packer.remote_node_id,
    )
    outbound_message_channels[0].send_nowait(outbound_message)
    with trio.fail_after(0.5):
        outbound_auth_tag_packet = await outbound_packet_channels[1].receive()

    handshake_recipient = HandshakeRecipientFactory(
        local_private_key=remote_private_key,
        local_enr=remote_enr,
        remote_private_key=peer_packer.local_private_key,
        remote_enr=enr,
        remote_node_id=peer_packer.local_node_id,
        initiating_packet_auth_tag=outbound_auth_tag_packet.packet.auth_tag,
    )
    inbound_packet = InboundPacket(
        handshake_recipient.first_packet_to_send,
        sender_endpoint=remote_endpoint,
    )
    inbound_packet_channels[0].send_nowait(inbound_packet)
    with trio.fail_after(0.5):
        outbound_auth_header_packet = await outbound_packet_channels[
            1].receive()

    assert peer_packer.is_post_handshake
    assert isinstance(outbound_auth_header_packet.packet, AuthHeaderPacket)
    assert outbound_auth_header_packet.receiver_endpoint == remote_endpoint

    handshake_result = handshake_recipient.complete_handshake(
        outbound_auth_header_packet.packet, )
    initiator_keys = peer_packer.session_keys
    recipient_keys = handshake_result.session_keys
    assert initiator_keys.auth_response_key == recipient_keys.auth_response_key
    assert initiator_keys.encryption_key == recipient_keys.decryption_key
    assert initiator_keys.decryption_key == recipient_keys.encryption_key
Beispiel #8
0
async def test_peer_packer_initiates_handshake(peer_packer,
                                               outbound_message_channels,
                                               outbound_packet_channels,
                                               nursery):
    outbound_message = OutboundMessage(
        PingMessageFactory(),
        EndpointFactory(),
        peer_packer.remote_node_id,
    )

    outbound_message_channels[0].send_nowait(outbound_message)
    with trio.fail_after(0.5):
        outbound_packet = await outbound_packet_channels[1].receive()

    assert peer_packer.is_during_handshake
    assert outbound_packet.receiver_endpoint == outbound_message.receiver_endpoint
    assert isinstance(outbound_packet.packet, AuthTagPacket)