Update tests for new logic

This commit is contained in:
Alex Stokes 2019-08-02 18:36:58 -07:00
parent 9e18d7561d
commit cda74dd382
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
4 changed files with 122 additions and 49 deletions

View File

@ -19,6 +19,7 @@ from tests.utils import (
cleanup,
echo_stream_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():
events_b = []
node_a_key = generate_new_private_key()
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]))
# 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_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)
async def my_stream_handler(stream):

View File

@ -1,17 +1,21 @@
import factory
from libp2p import initialize_default_swarm
from libp2p.host.basic_host import BasicHost
from libp2p.pubsub.floodsub import FloodSub
from libp2p.pubsub.gossipsub import GossipSub
from libp2p.pubsub.pubsub import Pubsub
from tests.configs import LISTEN_MADDR
from .configs import FLOODSUB_PROTOCOL_ID, GOSSIPSUB_PARAMS, GOSSIPSUB_PROTOCOL_ID
from tests.configs import (
FLOODSUB_PROTOCOL_ID,
GOSSIPSUB_PARAMS,
GOSSIPSUB_PROTOCOL_ID,
LISTEN_MADDR,
)
from tests.utils import generate_new_private_key
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):

View File

@ -3,11 +3,11 @@ import asyncio
from libp2p import new_node
from libp2p.peer.peerinfo import info_from_p2p_addr
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
import multiaddr
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
# protocols through the same connection
@ -19,6 +19,15 @@ def peer_id_for_node(node):
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(
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)]
# Perform assertion
assertion_func(node1_conn.secured_conn.get_security_details())
assertion_func(node2_conn.secured_conn.get_security_details())
assertion_func(node1_conn.conn)
assertion_func(node2_conn.conn)
# Success, terminate pending tasks.
await cleanup()
@ -58,11 +67,19 @@ async def perform_simple_test(
@pytest.mark.asyncio
async def test_single_insecure_security_transport_succeeds():
transports_for_initiator = {"foo": InsecureTransport("foo")}
transports_for_noninitiator = {"foo": InsecureTransport("foo")}
transports_for_initiator = {
"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):
assert details["id"] == "foo"
def assertion_func(conn):
assert isinstance(conn, InsecureSession)
await perform_simple_test(
assertion_func, transports_for_initiator, transports_for_noninitiator
@ -71,11 +88,19 @@ async def test_single_insecure_security_transport_succeeds():
@pytest.mark.asyncio
async def test_single_simple_test_security_transport_succeeds():
transports_for_initiator = {"tacos": SimpleSecurityTransport("tacos")}
transports_for_noninitiator = {"tacos": SimpleSecurityTransport("tacos")}
transports_for_initiator = {
"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):
assert details["key_phrase"] == "tacos"
def assertion_func(conn):
assert conn.key_phrase == "tacos"
await perform_simple_test(
assertion_func, transports_for_initiator, transports_for_noninitiator
@ -85,13 +110,21 @@ async def test_single_simple_test_security_transport_succeeds():
@pytest.mark.asyncio
async def test_two_simple_test_security_transport_for_initiator_succeeds():
transports_for_initiator = {
"tacos": SimpleSecurityTransport("tacos"),
"shleep": SimpleSecurityTransport("shleep"),
"tacos": SimpleSecurityTransport(
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):
assert details["key_phrase"] == "shleep"
def assertion_func(conn):
assert conn.key_phrase == "shleep"
await perform_simple_test(
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
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 = {
"shleep": SimpleSecurityTransport("shleep"),
"tacos": SimpleSecurityTransport("tacos"),
"shleep": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "shleep"
),
"tacos": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "tacos"
),
}
def assertion_func(details):
assert details["key_phrase"] == "tacos"
def assertion_func(conn):
assert conn.key_phrase == "tacos"
await perform_simple_test(
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
async def test_two_simple_test_security_transport_for_both_succeeds():
transports_for_initiator = {
"a": SimpleSecurityTransport("a"),
"b": SimpleSecurityTransport("b"),
"a": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "a"
),
"b": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "b"
),
}
transports_for_noninitiator = {
"c": SimpleSecurityTransport("c"),
"b": SimpleSecurityTransport("b"),
"b": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "b"
),
"c": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "c"
),
}
def assertion_func(details):
assert details["key_phrase"] == "b"
def assertion_func(conn):
assert conn.key_phrase == "b"
await perform_simple_test(
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
async def test_multiple_security_none_the_same_fails():
transports_for_initiator = {
"a": SimpleSecurityTransport("a"),
"b": SimpleSecurityTransport("b"),
"a": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "a"
),
"b": SimpleSecurityTransport(
initiator_private_key_bytes, initiator_public_key_bytes, "b"
),
}
transports_for_noninitiator = {
"c": SimpleSecurityTransport("c"),
"d": SimpleSecurityTransport("d"),
"d": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "d"
),
"c": SimpleSecurityTransport(
noninitiator_private_key_bytes, noninitiator_public_key_bytes, "c"
),
}
def assertion_func(_):
@ -160,18 +217,18 @@ async def test_default_insecure_security():
transports_for_initiator = None
transports_for_noninitiator = None
details1 = None
details2 = None
conn1 = None
conn2 = None
def assertion_func(details):
nonlocal details1
nonlocal details2
if not details1:
details1 = details
elif not details2:
details2 = details
def assertion_func(conn):
nonlocal conn1
nonlocal conn2
if not conn1:
conn1 = conn
elif not conn2:
conn2 = conn
else:
assert details1 == details2
assert conn1 == conn2
await perform_simple_test(
assertion_func, transports_for_initiator, transports_for_noninitiator

View File

@ -3,6 +3,8 @@ from contextlib import suppress
import multiaddr
from Crypto.PublicKey import RSA
from libp2p import new_node
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())
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
return node_a, node_b
def generate_new_private_key():
new_key = RSA.generate(2048, e=65537)
return new_key