Exemplo n.º 1
0
def test_successful_handshake_with_new_enr():
    initiator_private_key = PrivateKeyFactory().to_bytes()
    recipient_private_key = PrivateKeyFactory().to_bytes()
    initiator_enr = ENRFactory(private_key=initiator_private_key)
    recipient_enr = ENRFactory(private_key=recipient_private_key)

    initiator = HandshakeInitiatorFactory(
        local_private_key=initiator_private_key,
        local_enr=initiator_enr,
        remote_enr=recipient_enr,
    )
    recipient = HandshakeRecipientFactory(
        local_private_key=recipient_private_key,
        local_enr=recipient_enr,
        remote_enr=None,
        remote_node_id=initiator_enr.node_id,
        initiating_packet_auth_tag=initiator.first_packet_to_send.auth_tag)

    initiator_result = initiator.complete_handshake(
        recipient.first_packet_to_send)
    recipient_result = recipient.complete_handshake(
        initiator_result.auth_header_packet)

    assert initiator_result.enr is None
    assert recipient_result.enr == initiator_enr
Exemplo n.º 2
0
def test_successful_handshake():
    initiator_private_key = PrivateKeyFactory().to_bytes()
    recipient_private_key = PrivateKeyFactory().to_bytes()
    initiator_enr = ENRFactory(private_key=initiator_private_key)
    recipient_enr = ENRFactory(private_key=recipient_private_key)
    initial_message = PingMessageFactory()

    initiator = HandshakeInitiatorFactory(
        local_private_key=initiator_private_key,
        local_enr=initiator_enr,
        remote_enr=recipient_enr,
        initial_message=initial_message,
    )
    recipient = HandshakeRecipientFactory(
        local_private_key=recipient_private_key,
        local_enr=recipient_enr,
        remote_enr=initiator_enr,
        initiating_packet_auth_tag=initiator.first_packet_to_send.auth_tag)

    initiator_result = initiator.complete_handshake(
        recipient.first_packet_to_send)
    recipient_result = recipient.complete_handshake(
        initiator_result.auth_header_packet)

    assert_session_keys_equal(initiator_result.session_keys,
                              recipient_result.session_keys)

    assert initiator_result.message is None
    assert initiator_result.enr is None
    assert initiator_result.auth_header_packet is not None

    assert recipient_result.message == initial_message
    assert recipient_result.enr is None
    assert recipient_result.auth_header_packet is None
Exemplo n.º 3
0
def test_successful_handshake_with_enr_update():
    initiator_private_key = PrivateKeyFactory().to_bytes()
    recipient_private_key = PrivateKeyFactory().to_bytes()
    old_initiator_enr = ENRFactory(private_key=initiator_private_key)
    new_initiator_enr = ENRFactory(
        private_key=initiator_private_key,
        sequence_number=old_initiator_enr.sequence_number + 1,
    )

    initiator = HandshakeInitiatorFactory(
        local_private_key=initiator_private_key,
        local_enr=new_initiator_enr,
        remote_private_key=recipient_private_key,
    )
    recipient = HandshakeRecipientFactory(
        local_private_key=recipient_private_key,
        remote_enr=old_initiator_enr,
        initiating_packet_auth_tag=initiator.first_packet_to_send.auth_tag)

    initiator_result = initiator.complete_handshake(
        recipient.first_packet_to_send)
    recipient_result = recipient.complete_handshake(
        initiator_result.auth_header_packet)

    assert initiator_result.enr is None
    assert recipient_result.enr == new_initiator_enr
Exemplo n.º 4
0
async def test_insert_or_update(db):
    private_key = PrivateKeyFactory().to_bytes()
    enr = ENRFactory(private_key=private_key)

    await db.insert_or_update(enr)
    assert await db.get(enr.node_id) == enr

    await db.insert_or_update(enr)
    assert await db.get(enr.node_id) == enr

    updated_enr = ENRFactory(private_key=private_key,
                             sequence_number=enr.sequence_number + 1)
    await db.insert_or_update(updated_enr)
    assert await db.get(enr.node_id) == updated_enr
Exemplo n.º 5
0
async def test_insert(db):
    private_key = PrivateKeyFactory().to_bytes()
    enr = ENRFactory(private_key=private_key)

    await db.insert(enr)
    assert await db.contains(enr.node_id)
    assert await db.get(enr.node_id) == enr

    with pytest.raises(ValueError):
        await db.insert(enr)

    updated_enr = ENRFactory(private_key=private_key,
                             sequence_number=enr.sequence_number + 1)
    with pytest.raises(ValueError):
        await db.insert(updated_enr)
Exemplo n.º 6
0
async def test_update(db):
    private_key = PrivateKeyFactory().to_bytes()
    enr = ENRFactory(private_key=private_key)

    with pytest.raises(KeyError):
        await db.update(enr)

    await db.insert(enr)

    await db.update(enr)
    assert await db.get(enr.node_id) == enr

    updated_enr = ENRFactory(private_key=private_key,
                             sequence_number=enr.sequence_number + 1)
    await db.update(updated_enr)
    assert await db.get(enr.node_id) == updated_enr
Exemplo n.º 7
0
def test_get_and_set_enr(node_db):
    private_key = PrivateKeyFactory().to_bytes()
    db = node_db
    enr = ENRFactory(private_key=private_key)

    with pytest.raises(KeyError):
        db.get_enr(enr.node_id)

    db.set_enr(enr)
    assert db.get_enr(enr.node_id) == enr

    updated_enr = ENRFactory(private_key=private_key, sequence_number=enr.sequence_number + 1)
    db.set_enr(updated_enr)
    assert db.get_enr(enr.node_id) == updated_enr

    with pytest.raises(ValueError):
        db.set_enr(enr)
Exemplo n.º 8
0
def remote_private_key():
    return PrivateKeyFactory().to_bytes()