Update Pubsub fixture and test

pull/362/head
NIC619 2019-11-26 16:12:50 +08:00
parent 0fd400fdf8
commit d5d6962dce
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
4 changed files with 39 additions and 7 deletions

View File

@ -532,7 +532,7 @@ class Pubsub:
# Check if signing is required and if so signature should be attached.
if self.strict_signing:
if msg.signature == b'':
if msg.signature == b"":
logger.debug("Reject because no signature attached for msg: %s", msg)
return
# Validate the signature of the message

View File

@ -153,6 +153,7 @@ class PubsubFactory(factory.Factory):
router = None
my_id = factory.LazyAttribute(lambda obj: obj.host.get_id())
cache_size = None
strict_signing = False
async def swarm_pair_factory(

View File

@ -4,14 +4,24 @@ from libp2p.tools.constants import GOSSIPSUB_PARAMS
from libp2p.tools.factories import FloodsubFactory, GossipsubFactory, PubsubFactory
def _make_pubsubs(hosts, pubsub_routers, cache_size):
@pytest.fixture
def is_strict_signing():
return False
def _make_pubsubs(hosts, pubsub_routers, cache_size, is_strict_signing):
if len(pubsub_routers) != len(hosts):
raise ValueError(
f"lenght of pubsub_routers={pubsub_routers} should be equaled to the "
f"length of hosts={len(hosts)}"
)
return tuple(
PubsubFactory(host=host, router=router, cache_size=cache_size)
PubsubFactory(
host=host,
router=router,
cache_size=cache_size,
strict_signing=is_strict_signing,
)
for host, router in zip(hosts, pubsub_routers)
)
@ -27,16 +37,22 @@ def gossipsub_params():
@pytest.fixture
def pubsubs_fsub(num_hosts, hosts, pubsub_cache_size):
def pubsubs_fsub(num_hosts, hosts, pubsub_cache_size, is_strict_signing):
floodsubs = FloodsubFactory.create_batch(num_hosts)
_pubsubs_fsub = _make_pubsubs(hosts, floodsubs, pubsub_cache_size)
_pubsubs_fsub = _make_pubsubs(
hosts, floodsubs, pubsub_cache_size, is_strict_signing
)
yield _pubsubs_fsub
# TODO: Clean up
@pytest.fixture
def pubsubs_gsub(num_hosts, hosts, pubsub_cache_size, gossipsub_params):
def pubsubs_gsub(
num_hosts, hosts, pubsub_cache_size, gossipsub_params, is_strict_signing
):
gossipsubs = GossipsubFactory.create_batch(num_hosts, **gossipsub_params._asdict())
_pubsubs_gsub = _make_pubsubs(hosts, gossipsubs, pubsub_cache_size)
_pubsubs_gsub = _make_pubsubs(
hosts, gossipsubs, pubsub_cache_size, is_strict_signing
)
yield _pubsubs_gsub
# TODO: Clean up

View File

@ -510,3 +510,18 @@ async def test_push_msg(pubsubs_fsub, monkeypatch):
await pubsubs_fsub[0].push_msg(pubsubs_fsub[0].my_id, msg_2)
await asyncio.sleep(0.01)
assert not event.is_set()
@pytest.mark.parametrize("num_hosts, is_strict_signing", ((2, True),))
@pytest.mark.asyncio
async def test_strict_signing(pubsubs_fsub, hosts, monkeypatch):
await connect(hosts[0], hosts[1])
await pubsubs_fsub[0].subscribe(TESTING_TOPIC)
await pubsubs_fsub[1].subscribe(TESTING_TOPIC)
await asyncio.sleep(1)
await pubsubs_fsub[0].publish(TESTING_TOPIC, TESTING_DATA)
await asyncio.sleep(1)
assert len(pubsubs_fsub[0].seen_messages) == 1
assert len(pubsubs_fsub[1].seen_messages) == 1