mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Merge branch 'main' into todo/handletimeout
This commit is contained in:
@ -0,0 +1 @@
|
||||
"""Discovery tests for py-libp2p."""
|
||||
|
||||
1
tests/discovery/bootstrap/__init__.py
Normal file
1
tests/discovery/bootstrap/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""Bootstrap discovery tests for py-libp2p."""
|
||||
52
tests/discovery/bootstrap/test_integration.py
Normal file
52
tests/discovery/bootstrap/test_integration.py
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test the full bootstrap discovery integration
|
||||
"""
|
||||
|
||||
import secrets
|
||||
|
||||
import pytest
|
||||
|
||||
from libp2p import new_host
|
||||
from libp2p.crypto.secp256k1 import create_new_key_pair
|
||||
from libp2p.host.basic_host import BasicHost
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_bootstrap_integration():
|
||||
"""Test bootstrap integration with new_host"""
|
||||
# Test bootstrap addresses
|
||||
bootstrap_addrs = [
|
||||
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SznbYGzPwp8qDrq",
|
||||
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
|
||||
]
|
||||
|
||||
# Generate key pair
|
||||
secret = secrets.token_bytes(32)
|
||||
key_pair = create_new_key_pair(secret)
|
||||
|
||||
# Create host with bootstrap
|
||||
host = new_host(key_pair=key_pair, bootstrap=bootstrap_addrs)
|
||||
|
||||
# Verify bootstrap discovery is set up (cast to BasicHost for type checking)
|
||||
assert isinstance(host, BasicHost), "Host should be a BasicHost instance"
|
||||
assert hasattr(host, "bootstrap"), "Host should have bootstrap attribute"
|
||||
assert host.bootstrap is not None, "Bootstrap discovery should be initialized"
|
||||
assert len(host.bootstrap.bootstrap_addrs) == len(bootstrap_addrs), (
|
||||
"Bootstrap addresses should match"
|
||||
)
|
||||
|
||||
|
||||
def test_bootstrap_no_addresses():
|
||||
"""Test that bootstrap is not initialized when no addresses provided"""
|
||||
secret = secrets.token_bytes(32)
|
||||
key_pair = create_new_key_pair(secret)
|
||||
|
||||
# Create host without bootstrap
|
||||
host = new_host(key_pair=key_pair)
|
||||
|
||||
# Verify bootstrap is not initialized
|
||||
assert isinstance(host, BasicHost)
|
||||
assert not hasattr(host, "bootstrap") or host.bootstrap is None, (
|
||||
"Bootstrap should not be initialized when no addresses provided"
|
||||
)
|
||||
39
tests/discovery/bootstrap/test_utils.py
Normal file
39
tests/discovery/bootstrap/test_utils.py
Normal file
@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test bootstrap address validation
|
||||
"""
|
||||
|
||||
from libp2p.discovery.bootstrap.utils import (
|
||||
parse_bootstrap_peer_info,
|
||||
validate_bootstrap_addresses,
|
||||
)
|
||||
|
||||
|
||||
def test_validate_addresses():
|
||||
"""Test validation with a mix of valid and invalid addresses in one list."""
|
||||
addresses = [
|
||||
# Valid - using proper peer IDs
|
||||
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
|
||||
# Invalid
|
||||
"invalid-address",
|
||||
"/ip4/192.168.1.1/tcp/4001", # Missing p2p part
|
||||
"", # Empty
|
||||
"/ip4/127.0.0.1/tcp/4001/p2p/InvalidPeerID", # Bad peer ID
|
||||
]
|
||||
valid_expected = [
|
||||
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
|
||||
]
|
||||
validated = validate_bootstrap_addresses(addresses)
|
||||
assert validated == valid_expected, (
|
||||
f"Expected only valid addresses, got: {validated}"
|
||||
)
|
||||
for addr in addresses:
|
||||
peer_info = parse_bootstrap_peer_info(addr)
|
||||
if addr in valid_expected:
|
||||
assert peer_info is not None and peer_info.peer_id is not None, (
|
||||
f"Should parse valid address: {addr}"
|
||||
)
|
||||
else:
|
||||
assert peer_info is None, f"Should not parse invalid address: {addr}"
|
||||
Reference in New Issue
Block a user