2019-08-16 07:26:59 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-08-16 09:36:50 +08:00
|
|
|
from libp2p.crypto.rsa import create_new_key_pair
|
2020-02-19 23:15:03 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-02-19 23:15:03 +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
|
2020-02-19 23:15:03 +08:00
|
|
|
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
|
2020-02-19 23:15:03 +08:00
|
|
|
@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):
|
2020-02-19 23:15:03 +08:00
|
|
|
assert isinstance(conn, transport_type)
|
2019-04-30 06:05:49 +08:00
|
|
|
|
2020-02-19 23:15:03 +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):
|
2020-02-19 23:15:03 +08:00
|
|
|
assert isinstance(conn, InsecureSession)
|
2019-05-02 05:13:01 +08:00
|
|
|
|
2020-02-19 23:15:03 +08:00
|
|
|
await perform_simple_test(assertion_func, None)
|