Update tests for new logic
This commit is contained in:
parent
9e18d7561d
commit
cda74dd382
|
@ -19,6 +19,7 @@ from tests.utils import (
|
||||||
cleanup,
|
cleanup,
|
||||||
echo_stream_handler,
|
echo_stream_handler,
|
||||||
perform_two_host_set_up_custom_handler,
|
perform_two_host_set_up_custom_handler,
|
||||||
|
generate_new_private_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -172,14 +173,18 @@ async def test_one_notifier_on_two_nodes():
|
||||||
async def test_one_notifier_on_two_nodes_with_listen():
|
async def test_one_notifier_on_two_nodes_with_listen():
|
||||||
events_b = []
|
events_b = []
|
||||||
|
|
||||||
|
node_a_key = generate_new_private_key()
|
||||||
node_a_transport_opt = ["/ip4/127.0.0.1/tcp/0"]
|
node_a_transport_opt = ["/ip4/127.0.0.1/tcp/0"]
|
||||||
node_a = await new_node(transport_opt=node_a_transport_opt)
|
node_a = await new_node(node_a_key, transport_opt=node_a_transport_opt)
|
||||||
await node_a.get_network().listen(multiaddr.Multiaddr(node_a_transport_opt[0]))
|
await node_a.get_network().listen(multiaddr.Multiaddr(node_a_transport_opt[0]))
|
||||||
|
|
||||||
# Set up node_b swarm to pass into host
|
# Set up node_b swarm to pass into host
|
||||||
|
node_b_key = generate_new_private_key()
|
||||||
node_b_transport_opt = ["/ip4/127.0.0.1/tcp/0"]
|
node_b_transport_opt = ["/ip4/127.0.0.1/tcp/0"]
|
||||||
node_b_multiaddr = multiaddr.Multiaddr(node_b_transport_opt[0])
|
node_b_multiaddr = multiaddr.Multiaddr(node_b_transport_opt[0])
|
||||||
node_b_swarm = initialize_default_swarm(transport_opt=node_b_transport_opt)
|
node_b_swarm = initialize_default_swarm(
|
||||||
|
node_b_key, transport_opt=node_b_transport_opt
|
||||||
|
)
|
||||||
node_b = BasicHost(node_b_swarm)
|
node_b = BasicHost(node_b_swarm)
|
||||||
|
|
||||||
async def my_stream_handler(stream):
|
async def my_stream_handler(stream):
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
import factory
|
import factory
|
||||||
|
|
||||||
from libp2p import initialize_default_swarm
|
from libp2p import initialize_default_swarm
|
||||||
from libp2p.host.basic_host import BasicHost
|
from libp2p.host.basic_host import BasicHost
|
||||||
from libp2p.pubsub.floodsub import FloodSub
|
from libp2p.pubsub.floodsub import FloodSub
|
||||||
from libp2p.pubsub.gossipsub import GossipSub
|
from libp2p.pubsub.gossipsub import GossipSub
|
||||||
from libp2p.pubsub.pubsub import Pubsub
|
from libp2p.pubsub.pubsub import Pubsub
|
||||||
from tests.configs import LISTEN_MADDR
|
from tests.configs import (
|
||||||
|
FLOODSUB_PROTOCOL_ID,
|
||||||
from .configs import FLOODSUB_PROTOCOL_ID, GOSSIPSUB_PARAMS, GOSSIPSUB_PROTOCOL_ID
|
GOSSIPSUB_PARAMS,
|
||||||
|
GOSSIPSUB_PROTOCOL_ID,
|
||||||
|
LISTEN_MADDR,
|
||||||
|
)
|
||||||
|
from tests.utils import generate_new_private_key
|
||||||
|
|
||||||
|
|
||||||
def swarm_factory():
|
def swarm_factory():
|
||||||
return initialize_default_swarm(transport_opt=[str(LISTEN_MADDR)])
|
private_key = generate_new_private_key()
|
||||||
|
return initialize_default_swarm(private_key, transport_opt=[str(LISTEN_MADDR)])
|
||||||
|
|
||||||
|
|
||||||
class HostFactory(factory.Factory):
|
class HostFactory(factory.Factory):
|
||||||
|
|
|
@ -3,11 +3,11 @@ import asyncio
|
||||||
from libp2p import new_node
|
from libp2p import new_node
|
||||||
from libp2p.peer.peerinfo import info_from_p2p_addr
|
from libp2p.peer.peerinfo import info_from_p2p_addr
|
||||||
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
|
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
|
||||||
from libp2p.security.insecure.transport import InsecureTransport
|
from libp2p.security.insecure.transport import InsecureSession, InsecureTransport
|
||||||
from libp2p.security.simple.transport import SimpleSecurityTransport
|
from libp2p.security.simple.transport import SimpleSecurityTransport
|
||||||
import multiaddr
|
import multiaddr
|
||||||
import pytest
|
import pytest
|
||||||
from tests.utils import cleanup, connect
|
from tests.utils import cleanup, connect, generate_new_private_key
|
||||||
|
|
||||||
# TODO: Add tests for multiple streams being opened on different
|
# TODO: Add tests for multiple streams being opened on different
|
||||||
# protocols through the same connection
|
# protocols through the same connection
|
||||||
|
@ -19,6 +19,15 @@ def peer_id_for_node(node):
|
||||||
return info.peer_id
|
return info.peer_id
|
||||||
|
|
||||||
|
|
||||||
|
initiator_private_key = generate_new_private_key()
|
||||||
|
initiator_private_key_bytes = initiator_private_key.export_key("DER")
|
||||||
|
initiator_public_key_bytes = initiator_private_key.publickey().export_key("DER")
|
||||||
|
|
||||||
|
noninitiator_private_key = generate_new_private_key()
|
||||||
|
noninitiator_private_key_bytes = noninitiator_private_key.export_key("DER")
|
||||||
|
noninitiator_public_key_bytes = noninitiator_private_key.publickey().export_key("DER")
|
||||||
|
|
||||||
|
|
||||||
async def perform_simple_test(
|
async def perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
):
|
):
|
||||||
|
@ -49,8 +58,8 @@ async def perform_simple_test(
|
||||||
node2_conn = node2.get_network().connections[peer_id_for_node(node1)]
|
node2_conn = node2.get_network().connections[peer_id_for_node(node1)]
|
||||||
|
|
||||||
# Perform assertion
|
# Perform assertion
|
||||||
assertion_func(node1_conn.secured_conn.get_security_details())
|
assertion_func(node1_conn.conn)
|
||||||
assertion_func(node2_conn.secured_conn.get_security_details())
|
assertion_func(node2_conn.conn)
|
||||||
|
|
||||||
# Success, terminate pending tasks.
|
# Success, terminate pending tasks.
|
||||||
await cleanup()
|
await cleanup()
|
||||||
|
@ -58,11 +67,19 @@ async def perform_simple_test(
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_single_insecure_security_transport_succeeds():
|
async def test_single_insecure_security_transport_succeeds():
|
||||||
transports_for_initiator = {"foo": InsecureTransport("foo")}
|
transports_for_initiator = {
|
||||||
transports_for_noninitiator = {"foo": InsecureTransport("foo")}
|
"foo": InsecureTransport(
|
||||||
|
initiator_private_key_bytes, initiator_public_key_bytes
|
||||||
|
)
|
||||||
|
}
|
||||||
|
transports_for_noninitiator = {
|
||||||
|
"foo": InsecureTransport(
|
||||||
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
def assertion_func(details):
|
def assertion_func(conn):
|
||||||
assert details["id"] == "foo"
|
assert isinstance(conn, InsecureSession)
|
||||||
|
|
||||||
await perform_simple_test(
|
await perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
|
@ -71,11 +88,19 @@ async def test_single_insecure_security_transport_succeeds():
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_single_simple_test_security_transport_succeeds():
|
async def test_single_simple_test_security_transport_succeeds():
|
||||||
transports_for_initiator = {"tacos": SimpleSecurityTransport("tacos")}
|
transports_for_initiator = {
|
||||||
transports_for_noninitiator = {"tacos": SimpleSecurityTransport("tacos")}
|
"tacos": SimpleSecurityTransport(
|
||||||
|
initiator_private_key_bytes, initiator_public_key_bytes, "tacos"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
transports_for_noninitiator = {
|
||||||
|
"tacos": SimpleSecurityTransport(
|
||||||
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "tacos"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
def assertion_func(details):
|
def assertion_func(conn):
|
||||||
assert details["key_phrase"] == "tacos"
|
assert conn.key_phrase == "tacos"
|
||||||
|
|
||||||
await perform_simple_test(
|
await perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
|
@ -85,13 +110,21 @@ async def test_single_simple_test_security_transport_succeeds():
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_two_simple_test_security_transport_for_initiator_succeeds():
|
async def test_two_simple_test_security_transport_for_initiator_succeeds():
|
||||||
transports_for_initiator = {
|
transports_for_initiator = {
|
||||||
"tacos": SimpleSecurityTransport("tacos"),
|
"tacos": SimpleSecurityTransport(
|
||||||
"shleep": SimpleSecurityTransport("shleep"),
|
initiator_private_key_bytes, initiator_public_key_bytes, "tacos"
|
||||||
|
),
|
||||||
|
"shleep": SimpleSecurityTransport(
|
||||||
|
initiator_private_key_bytes, initiator_public_key_bytes, "shleep"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
transports_for_noninitiator = {
|
||||||
|
"shleep": SimpleSecurityTransport(
|
||||||
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "shleep"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
transports_for_noninitiator = {"shleep": SimpleSecurityTransport("shleep")}
|
|
||||||
|
|
||||||
def assertion_func(details):
|
def assertion_func(conn):
|
||||||
assert details["key_phrase"] == "shleep"
|
assert conn.key_phrase == "shleep"
|
||||||
|
|
||||||
await perform_simple_test(
|
await perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
|
@ -100,14 +133,22 @@ async def test_two_simple_test_security_transport_for_initiator_succeeds():
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_two_simple_test_security_transport_for_noninitiator_succeeds():
|
async def test_two_simple_test_security_transport_for_noninitiator_succeeds():
|
||||||
transports_for_initiator = {"tacos": SimpleSecurityTransport("tacos")}
|
transports_for_initiator = {
|
||||||
|
"tacos": SimpleSecurityTransport(
|
||||||
|
initiator_private_key_bytes, initiator_public_key_bytes, "tacos"
|
||||||
|
)
|
||||||
|
}
|
||||||
transports_for_noninitiator = {
|
transports_for_noninitiator = {
|
||||||
"shleep": SimpleSecurityTransport("shleep"),
|
"shleep": SimpleSecurityTransport(
|
||||||
"tacos": SimpleSecurityTransport("tacos"),
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "shleep"
|
||||||
|
),
|
||||||
|
"tacos": SimpleSecurityTransport(
|
||||||
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "tacos"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
def assertion_func(details):
|
def assertion_func(conn):
|
||||||
assert details["key_phrase"] == "tacos"
|
assert conn.key_phrase == "tacos"
|
||||||
|
|
||||||
await perform_simple_test(
|
await perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
|
@ -117,16 +158,24 @@ async def test_two_simple_test_security_transport_for_noninitiator_succeeds():
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_two_simple_test_security_transport_for_both_succeeds():
|
async def test_two_simple_test_security_transport_for_both_succeeds():
|
||||||
transports_for_initiator = {
|
transports_for_initiator = {
|
||||||
"a": SimpleSecurityTransport("a"),
|
"a": SimpleSecurityTransport(
|
||||||
"b": SimpleSecurityTransport("b"),
|
initiator_private_key_bytes, initiator_public_key_bytes, "a"
|
||||||
|
),
|
||||||
|
"b": SimpleSecurityTransport(
|
||||||
|
initiator_private_key_bytes, initiator_public_key_bytes, "b"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
transports_for_noninitiator = {
|
transports_for_noninitiator = {
|
||||||
"c": SimpleSecurityTransport("c"),
|
"b": SimpleSecurityTransport(
|
||||||
"b": SimpleSecurityTransport("b"),
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "b"
|
||||||
|
),
|
||||||
|
"c": SimpleSecurityTransport(
|
||||||
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "c"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
def assertion_func(details):
|
def assertion_func(conn):
|
||||||
assert details["key_phrase"] == "b"
|
assert conn.key_phrase == "b"
|
||||||
|
|
||||||
await perform_simple_test(
|
await perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
|
@ -136,12 +185,20 @@ async def test_two_simple_test_security_transport_for_both_succeeds():
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_multiple_security_none_the_same_fails():
|
async def test_multiple_security_none_the_same_fails():
|
||||||
transports_for_initiator = {
|
transports_for_initiator = {
|
||||||
"a": SimpleSecurityTransport("a"),
|
"a": SimpleSecurityTransport(
|
||||||
"b": SimpleSecurityTransport("b"),
|
initiator_private_key_bytes, initiator_public_key_bytes, "a"
|
||||||
|
),
|
||||||
|
"b": SimpleSecurityTransport(
|
||||||
|
initiator_private_key_bytes, initiator_public_key_bytes, "b"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
transports_for_noninitiator = {
|
transports_for_noninitiator = {
|
||||||
"c": SimpleSecurityTransport("c"),
|
"d": SimpleSecurityTransport(
|
||||||
"d": SimpleSecurityTransport("d"),
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "d"
|
||||||
|
),
|
||||||
|
"c": SimpleSecurityTransport(
|
||||||
|
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "c"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
def assertion_func(_):
|
def assertion_func(_):
|
||||||
|
@ -160,18 +217,18 @@ async def test_default_insecure_security():
|
||||||
transports_for_initiator = None
|
transports_for_initiator = None
|
||||||
transports_for_noninitiator = None
|
transports_for_noninitiator = None
|
||||||
|
|
||||||
details1 = None
|
conn1 = None
|
||||||
details2 = None
|
conn2 = None
|
||||||
|
|
||||||
def assertion_func(details):
|
def assertion_func(conn):
|
||||||
nonlocal details1
|
nonlocal conn1
|
||||||
nonlocal details2
|
nonlocal conn2
|
||||||
if not details1:
|
if not conn1:
|
||||||
details1 = details
|
conn1 = conn
|
||||||
elif not details2:
|
elif not conn2:
|
||||||
details2 = details
|
conn2 = conn
|
||||||
else:
|
else:
|
||||||
assert details1 == details2
|
assert conn1 == conn2
|
||||||
|
|
||||||
await perform_simple_test(
|
await perform_simple_test(
|
||||||
assertion_func, transports_for_initiator, transports_for_noninitiator
|
assertion_func, transports_for_initiator, transports_for_noninitiator
|
||||||
|
|
|
@ -3,6 +3,8 @@ from contextlib import suppress
|
||||||
|
|
||||||
import multiaddr
|
import multiaddr
|
||||||
|
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
|
||||||
from libp2p import new_node
|
from libp2p import new_node
|
||||||
from libp2p.peer.peerinfo import info_from_p2p_addr
|
from libp2p.peer.peerinfo import info_from_p2p_addr
|
||||||
|
|
||||||
|
@ -53,3 +55,8 @@ async def perform_two_host_set_up_custom_handler(handler):
|
||||||
# Associate the peer with local ip address (see default parameters of Libp2p())
|
# Associate the peer with local ip address (see default parameters of Libp2p())
|
||||||
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
|
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
|
||||||
return node_a, node_b
|
return node_a, node_b
|
||||||
|
|
||||||
|
|
||||||
|
def generate_new_private_key():
|
||||||
|
new_key = RSA.generate(2048, e=65537)
|
||||||
|
return new_key
|
||||||
|
|
Loading…
Reference in New Issue
Block a user