py-libp2p/tests/security/test_security_multistream.py

50 lines
1.6 KiB
Python
Raw Normal View History

import pytest
2019-08-16 09:36:50 +08:00
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureSession
from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
from libp2p.security.secio.transport import ID as SECIO_PROTOCOL_ID
from libp2p.security.secure_session import SecureSession
from libp2p.tools.factories import host_pair_factory
2019-08-01 06:00:12 +08:00
2019-08-16 09:36:50 +08:00
initiator_key_pair = create_new_key_pair()
2019-08-03 09:36:58 +08:00
2019-08-16 09:36:50 +08:00
noninitiator_key_pair = create_new_key_pair()
2019-08-03 09:36:58 +08:00
async def perform_simple_test(assertion_func, security_protocol):
async with host_pair_factory(security_protocol=security_protocol) as hosts:
conn_0 = hosts[0].get_network().connections[hosts[1].get_id()]
conn_1 = hosts[1].get_network().connections[hosts[0].get_id()]
2019-04-30 06:05:49 +08:00
2019-12-07 00:14:01 +08:00
# Perform assertion
assertion_func(conn_0.muxed_conn.secured_conn)
assertion_func(conn_1.muxed_conn.secured_conn)
2019-04-30 06:05:49 +08:00
2019-12-07 00:14:01 +08:00
@pytest.mark.trio
@pytest.mark.parametrize(
"security_protocol, transport_type",
(
(PLAINTEXT_PROTOCOL_ID, InsecureSession),
(SECIO_PROTOCOL_ID, SecureSession),
(NOISE_PROTOCOL_ID, SecureSession),
),
)
@pytest.mark.trio
async def test_single_insecure_security_transport_succeeds(
security_protocol, transport_type
):
2019-08-03 09:36:58 +08:00
def assertion_func(conn):
assert isinstance(conn, transport_type)
2019-04-30 06:05:49 +08:00
await perform_simple_test(assertion_func, security_protocol)
2019-08-01 06:00:12 +08:00
2019-04-30 06:05:49 +08:00
2019-12-07 00:14:01 +08:00
@pytest.mark.trio
2019-05-02 05:13:01 +08:00
async def test_default_insecure_security():
2019-08-03 09:36:58 +08:00
def assertion_func(conn):
assert isinstance(conn, InsecureSession)
2019-05-02 05:13:01 +08:00
await perform_simple_test(assertion_func, None)