mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
reorg test structure to match tox and CI jobs, drop bumpversion for bump-my-version and move config to pyproject.toml, fix docs building
This commit is contained in:
32
tests/core/security/noise/test_msg_read_writer.py
Normal file
32
tests/core/security/noise/test_msg_read_writer.py
Normal file
@ -0,0 +1,32 @@
|
||||
import pytest
|
||||
|
||||
from libp2p.security.noise.io import (
|
||||
MAX_NOISE_MESSAGE_LEN,
|
||||
NoisePacketReadWriter,
|
||||
)
|
||||
from libp2p.tools.factories import (
|
||||
raw_conn_factory,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"noise_msg",
|
||||
(b"", b"data", pytest.param(b"A" * MAX_NOISE_MESSAGE_LEN, id="maximum length")),
|
||||
)
|
||||
@pytest.mark.trio
|
||||
async def test_noise_msg_read_write_round_trip(nursery, noise_msg):
|
||||
async with raw_conn_factory(nursery) as conns:
|
||||
reader, writer = (
|
||||
NoisePacketReadWriter(conns[0]),
|
||||
NoisePacketReadWriter(conns[1]),
|
||||
)
|
||||
await writer.write_msg(noise_msg)
|
||||
assert (await reader.read_msg()) == noise_msg
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_noise_msg_write_too_long(nursery):
|
||||
async with raw_conn_factory(nursery) as conns:
|
||||
writer = NoisePacketReadWriter(conns[0])
|
||||
with pytest.raises(ValueError):
|
||||
await writer.write_msg(b"1" * (MAX_NOISE_MESSAGE_LEN + 1))
|
||||
38
tests/core/security/noise/test_noise.py
Normal file
38
tests/core/security/noise/test_noise.py
Normal file
@ -0,0 +1,38 @@
|
||||
import pytest
|
||||
|
||||
from libp2p.security.noise.messages import (
|
||||
NoiseHandshakePayload,
|
||||
)
|
||||
from libp2p.tools.factories import (
|
||||
noise_conn_factory,
|
||||
noise_handshake_payload_factory,
|
||||
)
|
||||
|
||||
DATA_0 = b"data_0"
|
||||
DATA_1 = b"1" * 1000
|
||||
DATA_2 = b"data_2"
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_noise_transport(nursery):
|
||||
async with noise_conn_factory(nursery):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_noise_connection(nursery):
|
||||
async with noise_conn_factory(nursery) as conns:
|
||||
local_conn, remote_conn = conns
|
||||
await local_conn.write(DATA_0)
|
||||
await local_conn.write(DATA_1)
|
||||
assert DATA_0 == (await remote_conn.read(len(DATA_0)))
|
||||
assert DATA_1 == (await remote_conn.read(len(DATA_1)))
|
||||
await local_conn.write(DATA_2)
|
||||
assert DATA_2 == (await remote_conn.read(len(DATA_2)))
|
||||
|
||||
|
||||
def test_noise_handshake_payload():
|
||||
payload = noise_handshake_payload_factory()
|
||||
payload_serialized = payload.serialize()
|
||||
payload_deserialized = NoiseHandshakePayload.deserialize(payload_serialized)
|
||||
assert payload == payload_deserialized
|
||||
60
tests/core/security/test_secio.py
Normal file
60
tests/core/security/test_secio.py
Normal file
@ -0,0 +1,60 @@
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
from libp2p.crypto.secp256k1 import (
|
||||
create_new_key_pair,
|
||||
)
|
||||
from libp2p.peer.id import (
|
||||
ID,
|
||||
)
|
||||
from libp2p.security.secio.transport import (
|
||||
NONCE_SIZE,
|
||||
create_secure_session,
|
||||
)
|
||||
from libp2p.tools.constants import (
|
||||
MAX_READ_LEN,
|
||||
)
|
||||
from libp2p.tools.factories import (
|
||||
raw_conn_factory,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_create_secure_session(nursery):
|
||||
local_nonce = b"\x01" * NONCE_SIZE
|
||||
local_key_pair = create_new_key_pair(b"a")
|
||||
local_peer = ID.from_pubkey(local_key_pair.public_key)
|
||||
|
||||
remote_nonce = b"\x02" * NONCE_SIZE
|
||||
remote_key_pair = create_new_key_pair(b"b")
|
||||
remote_peer = ID.from_pubkey(remote_key_pair.public_key)
|
||||
|
||||
async with raw_conn_factory(nursery) as conns:
|
||||
local_conn, remote_conn = conns
|
||||
|
||||
local_secure_conn, remote_secure_conn = None, None
|
||||
|
||||
async def local_create_secure_session():
|
||||
nonlocal local_secure_conn
|
||||
local_secure_conn = await create_secure_session(
|
||||
local_nonce,
|
||||
local_peer,
|
||||
local_key_pair.private_key,
|
||||
local_conn,
|
||||
remote_peer,
|
||||
)
|
||||
|
||||
async def remote_create_secure_session():
|
||||
nonlocal remote_secure_conn
|
||||
remote_secure_conn = await create_secure_session(
|
||||
remote_nonce, remote_peer, remote_key_pair.private_key, remote_conn
|
||||
)
|
||||
|
||||
async with trio.open_nursery() as nursery_1:
|
||||
nursery_1.start_soon(local_create_secure_session)
|
||||
nursery_1.start_soon(remote_create_secure_session)
|
||||
|
||||
msg = b"abc"
|
||||
await local_secure_conn.write(msg)
|
||||
received_msg = await remote_secure_conn.read(MAX_READ_LEN)
|
||||
assert received_msg == msg
|
||||
58
tests/core/security/test_security_multistream.py
Normal file
58
tests/core/security/test_security_multistream.py
Normal file
@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
initiator_key_pair = create_new_key_pair()
|
||||
|
||||
noninitiator_key_pair = create_new_key_pair()
|
||||
|
||||
|
||||
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()]
|
||||
|
||||
# Perform assertion
|
||||
assertion_func(conn_0.muxed_conn.secured_conn)
|
||||
assertion_func(conn_1.muxed_conn.secured_conn)
|
||||
|
||||
|
||||
@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
|
||||
):
|
||||
def assertion_func(conn):
|
||||
assert isinstance(conn, transport_type)
|
||||
|
||||
await perform_simple_test(assertion_func, security_protocol)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_default_insecure_security():
|
||||
def assertion_func(conn):
|
||||
assert isinstance(conn, InsecureSession)
|
||||
|
||||
await perform_simple_test(assertion_func, None)
|
||||
Reference in New Issue
Block a user