Beispiel #1
0
async def test_handshake_with_single_protocol():
    protocol_class = ProtocolFactory()
    alice_handshakers = bob_handshakers = (NoopHandshaker(protocol_class), )
    alice_version = bob_version = 5
    async with _do_handshake(alice_handshakers, alice_version, bob_handshakers,
                             bob_version) as (alice_multiplexer,
                                              alice_p2p_receipt,
                                              alice_receipts, bob_multiplexer,
                                              bob_p2p_receipt, bob_receipts):
        assert len(alice_receipts) == 1
        assert len(bob_receipts) == 1

        alice_receipt = alice_receipts[0]
        bob_receipt = bob_receipts[0]

        assert alice_p2p_receipt.client_version_string == 'bob-client'
        assert bob_p2p_receipt.client_version_string == 'alice-client'

        assert isinstance(alice_receipt.protocol, protocol_class)
        assert isinstance(bob_receipt.protocol, protocol_class)

        assert isinstance(alice_multiplexer.get_base_protocol(), P2PProtocolV5)
        assert isinstance(alice_multiplexer.get_protocols()[0], P2PProtocolV5)
        assert isinstance(alice_multiplexer.get_protocols()[1], protocol_class)

        assert isinstance(bob_multiplexer.get_base_protocol(), P2PProtocolV5)
        assert isinstance(bob_multiplexer.get_protocols()[0], P2PProtocolV5)
        assert isinstance(bob_multiplexer.get_protocols()[1], protocol_class)

        alice_p2p_protocol = alice_p2p_receipt.protocol
        assert alice_p2p_protocol.snappy_support is True

        bob_p2p_protocol = bob_p2p_receipt.protocol
        assert bob_p2p_protocol.snappy_support is True
Beispiel #2
0
def test_has_protocol_qualifier(alice, my_logic):
    MyProtocol = ProtocolFactory()

    has_my_protocol = HasProtocol(MyProtocol)
    has_paragon = HasProtocol(ParagonProtocol)

    assert has_paragon(alice, my_logic) is True
    assert has_my_protocol(alice, my_logic) is False
Beispiel #3
0
async def test_handshake_with_single_protocol():
    token = CancelTokenFactory()
    alice_transport, bob_transport = MemoryTransportPairFactory()

    alice_p2p_params = DevP2PHandshakeParamsFactory(
        client_version_string='alice-client',
        listen_port=alice_transport.remote.address.tcp_port,
    )
    bob_p2p_params = DevP2PHandshakeParamsFactory(
        client_version_string='bob-client',
        listen_port=bob_transport.remote.address.tcp_port,
    )

    protocol_class = ProtocolFactory()

    alice_coro = negotiate_protocol_handshakes(
        alice_transport,
        alice_p2p_params,
        (NoopHandshaker(protocol_class), ),
        token=token,
    )
    bob_coro = negotiate_protocol_handshakes(
        bob_transport,
        bob_p2p_params,
        (NoopHandshaker(protocol_class), ),
        token=token,
    )

    alice_result, bob_result = await asyncio.gather(alice_coro, bob_coro)

    alice_multiplexer, alice_p2p_receipt, alice_receipts = alice_result
    bob_multiplexer, bob_p2p_receipt, bob_receipts = bob_result

    assert len(alice_receipts) == 1
    assert len(bob_receipts) == 1

    alice_receipt = alice_receipts[0]
    bob_receipt = bob_receipts[0]

    assert alice_p2p_receipt.client_version_string == 'bob-client'
    assert bob_p2p_receipt.client_version_string == 'alice-client'

    assert isinstance(alice_receipt.protocol, protocol_class)
    assert isinstance(bob_receipt.protocol, protocol_class)

    assert isinstance(alice_multiplexer.get_base_protocol(), P2PProtocolV5)
    assert isinstance(alice_multiplexer.get_protocols()[0], P2PProtocolV5)
    assert isinstance(alice_multiplexer.get_protocols()[1], protocol_class)

    assert isinstance(bob_multiplexer.get_base_protocol(), P2PProtocolV5)
    assert isinstance(bob_multiplexer.get_protocols()[0], P2PProtocolV5)
    assert isinstance(bob_multiplexer.get_protocols()[1], protocol_class)

    alice_p2p_protocol = alice_p2p_receipt.protocol
    assert alice_p2p_protocol.snappy_support is True

    bob_p2p_protocol = bob_p2p_receipt.protocol
    assert bob_p2p_protocol.snappy_support is True
Beispiel #4
0
async def test_connection_pair_factory_with_single_protocol():
    protocol_class = ProtocolFactory()

    alice_handshaker = NoopHandshaker(protocol_class)
    bob_handshaker = NoopHandshaker(protocol_class)

    pair_factory = ConnectionPairFactory(
        alice_handshakers=(alice_handshaker,),
        bob_handshakers=(bob_handshaker,),
    )

    async with pair_factory as (alice_connection, bob_connection):
        expected_caps = (protocol_class.as_capability(),)

        assert alice_connection.remote_capabilities == expected_caps
        assert bob_connection.remote_capabilities == expected_caps

        await do_ping_pong_test(alice_connection, bob_connection)
async def test_connection_properties():
    Protocol_A = ProtocolFactory()
    Protocol_B = ProtocolFactory()

    alice_handshakers = (NoopHandshaker(Protocol_A), )
    bob_handshakers = (NoopHandshaker(Protocol_A), NoopHandshaker(Protocol_B))
    bob_capabilities = (Protocol_A.as_capability(), Protocol_B.as_capability())

    bob_remote = NodeFactory()
    bob_private_key = PrivateKeyFactory()

    pair_factory = ConnectionPairFactory(
        alice_handshakers=alice_handshakers,
        bob_handshakers=bob_handshakers,
        alice_p2p_version=DEVP2P_V4,
        bob_client_version='bob-client',
        bob_p2p_version=DEVP2P_V5,
        bob_private_key=bob_private_key,
        bob_remote=bob_remote,
    )
    async with pair_factory as (connection, _):
        assert type(connection.get_base_protocol()) is P2PProtocolV4
        assert connection.remote_capabilities == bob_capabilities
        assert connection.remote_p2p_version == DEVP2P_V5
        assert connection.negotiated_p2p_version == DEVP2P_V4
        assert connection.remote_public_key == bob_private_key.public_key
        assert connection.client_version_string == 'bob-client'
        assert connection.safe_client_version_string == 'bob-client'
Beispiel #6
0
async def test_handshake_with_v4_and_v5_disables_snappy():
    protocol_class = ProtocolFactory()
    alice_handshakers = bob_handshakers = (NoopHandshaker(protocol_class), )
    alice_version = 5
    bob_version = 4
    async with _do_handshake(alice_handshakers, alice_version, bob_handshakers,
                             bob_version) as (_, alice_p2p_receipt, _, _,
                                              bob_p2p_receipt, _):
        alice_p2p_protocol = alice_p2p_receipt.protocol
        bob_p2p_protocol = bob_p2p_receipt.protocol

        assert isinstance(alice_p2p_protocol, P2PProtocolV5)
        assert alice_p2p_protocol.snappy_support is False

        assert isinstance(bob_p2p_protocol, P2PProtocolV4)
        assert bob_p2p_protocol.snappy_support is False
Beispiel #7
0
async def test_handshake_with_multiple_protocols():
    A1 = ProtocolFactory(name='a', version=1)
    A2 = ProtocolFactory(name='a', version=2)
    A3 = ProtocolFactory(name='a', version=3)
    B5 = ProtocolFactory(name='b', version=5)
    B6 = ProtocolFactory(name='b', version=6)
    B7 = ProtocolFactory(name='b', version=7)
    C2 = ProtocolFactory(name='c', version=2)
    C3 = ProtocolFactory(name='c', version=3)

    alice_protos = (A1, A2, A3, B5, B7, C3)
    bob_protos = (A2, B6, C3, C2)
    # expected overlap A2, C3

    alice_handshakers = tuple(map(NoopHandshaker, alice_protos))
    bob_handshakers = tuple(map(NoopHandshaker, bob_protos))
    alice_version = bob_version = 5

    async with _do_handshake(alice_handshakers, alice_version, bob_handshakers,
                             bob_version) as (alice_multiplexer,
                                              alice_p2p_receipt,
                                              alice_receipts, bob_multiplexer,
                                              bob_p2p_receipt, bob_receipts):
        assert len(alice_receipts) == 2
        assert len(bob_receipts) == 2

        assert isinstance(alice_p2p_receipt.protocol, P2PProtocolV5)
        assert isinstance(alice_receipts[0].protocol, A2)
        assert isinstance(alice_receipts[1].protocol, C3)

        assert isinstance(alice_p2p_receipt.protocol, P2PProtocolV5)
        assert isinstance(bob_receipts[0].protocol, A2)
        assert isinstance(bob_receipts[1].protocol, C3)

        assert isinstance(alice_multiplexer.get_base_protocol(), P2PProtocolV5)
        assert isinstance(alice_multiplexer.get_protocols()[0], P2PProtocolV5)
        assert isinstance(alice_multiplexer.get_protocols()[1], A2)
        assert isinstance(alice_multiplexer.get_protocols()[2], C3)

        assert isinstance(bob_multiplexer.get_base_protocol(), P2PProtocolV5)
        assert isinstance(bob_multiplexer.get_protocols()[0], P2PProtocolV5)
        assert isinstance(bob_multiplexer.get_protocols()[1], A2)
        assert isinstance(bob_multiplexer.get_protocols()[2], C3)
Beispiel #8
0
async def test_handshake_with_v4_and_v5_disables_snappy():
    token = CancelTokenFactory()
    alice_transport, bob_transport = MemoryTransportPairFactory()

    alice_p2p_params = DevP2PHandshakeParamsFactory(
        client_version_string='alice-client',
        listen_port=alice_transport.remote.address.tcp_port,
    )
    bob_p2p_params = DevP2PHandshakeParams(
        client_version_string='bob-client',
        listen_port=bob_transport.remote.address.tcp_port,
        version=4,
    )

    protocol_class = ProtocolFactory()

    alice_coro = negotiate_protocol_handshakes(
        alice_transport,
        alice_p2p_params,
        (NoopHandshaker(protocol_class), ),
        token=token,
    )
    bob_coro = negotiate_protocol_handshakes(
        bob_transport,
        bob_p2p_params,
        (NoopHandshaker(protocol_class), ),
        token=token,
    )

    alice_result, bob_result = await asyncio.gather(alice_coro, bob_coro)

    alice_multiplexer, alice_p2p_receipt, alice_receipts = alice_result
    bob_multiplexer, bob_p2p_receipt, bob_receipts = bob_result

    alice_p2p_protocol = alice_p2p_receipt.protocol
    bob_p2p_protocol = bob_p2p_receipt.protocol

    assert isinstance(alice_p2p_protocol, P2PProtocolV5)
    assert alice_p2p_protocol.snappy_support is False

    assert isinstance(bob_p2p_protocol, P2PProtocolV4)
    assert bob_p2p_protocol.snappy_support is False
Beispiel #9
0
async def test_connection_pair_factory_with_multiple_protocols():
    protocol_class_a = ProtocolFactory()
    protocol_class_b = ProtocolFactory()

    alice_handshaker_a = NoopHandshaker(protocol_class_a)
    alice_handshaker_b = NoopHandshaker(protocol_class_b)
    bob_handshaker_a = NoopHandshaker(protocol_class_a)
    bob_handshaker_b = NoopHandshaker(protocol_class_b)

    pair_factory = ConnectionPairFactory(
        alice_handshakers=(alice_handshaker_a, alice_handshaker_b),
        bob_handshakers=(bob_handshaker_a, bob_handshaker_b),
    )

    async with pair_factory as (alice_connection, bob_connection):
        expected_caps = (
            protocol_class_a.as_capability(),
            protocol_class_b.as_capability(),
        )
        assert alice_connection.remote_capabilities == expected_caps
        assert bob_connection.remote_capabilities == expected_caps

        await do_ping_pong_test(alice_connection, bob_connection)
Beispiel #10
0
async def test_handshake_with_multiple_protocols():
    A1 = ProtocolFactory(name='a', version=1)
    A2 = ProtocolFactory(name='a', version=2)
    A3 = ProtocolFactory(name='a', version=3)
    B5 = ProtocolFactory(name='b', version=5)
    B6 = ProtocolFactory(name='b', version=6)
    B7 = ProtocolFactory(name='b', version=7)
    C2 = ProtocolFactory(name='c', version=2)
    C3 = ProtocolFactory(name='c', version=3)

    alice_protos = (A1, A2, A3, B5, B7, C3)
    bob_protos = (A2, B6, C3, C2)
    # expected overlap A2, C3

    alice_handshakers = tuple(map(NoopHandshaker, alice_protos))
    bob_handshakers = tuple(map(NoopHandshaker, bob_protos))

    token = CancelTokenFactory()
    alice_transport, bob_transport = MemoryTransportPairFactory()

    alice_p2p_params = DevP2PHandshakeParamsFactory(
        client_version_string='alice-client',
        listen_port=alice_transport.remote.address.tcp_port,
    )
    bob_p2p_params = DevP2PHandshakeParamsFactory(
        client_version_string='bob-client',
        listen_port=bob_transport.remote.address.tcp_port,
    )

    alice_coro = negotiate_protocol_handshakes(
        alice_transport,
        alice_p2p_params,
        alice_handshakers,
        token=token,
    )
    bob_coro = negotiate_protocol_handshakes(
        bob_transport,
        bob_p2p_params,
        bob_handshakers,
        token=token,
    )

    alice_result, bob_result = await asyncio.gather(alice_coro, bob_coro)

    alice_multiplexer, alice_p2p_receipt, alice_receipts = alice_result
    bob_multiplexer, bob_p2p_receipt, bob_receipts = bob_result

    assert len(alice_receipts) == 2
    assert len(bob_receipts) == 2

    assert isinstance(alice_p2p_receipt.protocol, P2PProtocolV5)
    assert isinstance(alice_receipts[0].protocol, A2)
    assert isinstance(alice_receipts[1].protocol, C3)

    assert isinstance(alice_p2p_receipt.protocol, P2PProtocolV5)
    assert isinstance(bob_receipts[0].protocol, A2)
    assert isinstance(bob_receipts[1].protocol, C3)

    assert isinstance(alice_multiplexer.get_base_protocol(), P2PProtocolV5)
    assert isinstance(alice_multiplexer.get_protocols()[0], P2PProtocolV5)
    assert isinstance(alice_multiplexer.get_protocols()[1], A2)
    assert isinstance(alice_multiplexer.get_protocols()[2], C3)

    assert isinstance(bob_multiplexer.get_base_protocol(), P2PProtocolV5)
    assert isinstance(bob_multiplexer.get_protocols()[0], P2PProtocolV5)
    assert isinstance(bob_multiplexer.get_protocols()[1], A2)
    assert isinstance(bob_multiplexer.get_protocols()[2], C3)