diff --git a/tests/discovery/bootstrap/test_bootstrap_script.py b/tests/discovery/bootstrap/test_bootstrap_script.py deleted file mode 100644 index f22fa970..00000000 --- a/tests/discovery/bootstrap/test_bootstrap_script.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -""" -Simple test script to verify bootstrap functionality -""" - -import os -import sys - -# Add the parent directory to sys.path so we can import libp2p -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../")) - -from libp2p.discovery.bootstrap.utils import ( - parse_bootstrap_peer_info, - validate_bootstrap_addresses, -) - - -def test_bootstrap_validation(): - """Test bootstrap address validation""" - print("๐Ÿงช Testing Bootstrap Address Validation") - - # Test addresses - test_addresses = [ - "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SznbYGzPwp8qDrq", - "/ip4/127.0.0.1/tcp/8000/p2p/QmTest123", # This might be invalid peer ID format - "invalid-address", - "/ip4/192.168.1.1/tcp/4001", # Missing p2p component - "/ip6/2604:a880:1:20::203:d001/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM", - ] - - print(f"๐Ÿ“‹ Testing {len(test_addresses)} addresses:") - for addr in test_addresses: - print(f" โ€ข {addr}") - - # Validate addresses - valid_addresses = validate_bootstrap_addresses(test_addresses) - - print(f"\nโœ… Valid addresses ({len(valid_addresses)}):") - for addr in valid_addresses: - print(f" โ€ข {addr}") - - # Try to parse peer info - peer_info = parse_bootstrap_peer_info(addr) - if peer_info: - print(f" โ†’ Peer ID: {peer_info.peer_id}") - print(f" โ†’ Addresses: {[str(a) for a in peer_info.addrs]}") - else: - print(" โ†’ Failed to parse peer info") - - return len(valid_addresses) > 0 - - -if __name__ == "__main__": - print("=" * 60) - print("Bootstrap Module Test") - print("=" * 60) - - try: - success = test_bootstrap_validation() - if success: - print("\n๐ŸŽ‰ Bootstrap module test completed successfully!") - else: - print("\nโŒ No valid bootstrap addresses found") - sys.exit(1) - except Exception as e: - print(f"\n๐Ÿ’ฅ Test failed with error: {e}") - import traceback - - traceback.print_exc() - sys.exit(1) diff --git a/tests/discovery/bootstrap/test_integration.py b/tests/discovery/bootstrap/test_integration.py index dc77aab2..06fba0f6 100644 --- a/tests/discovery/bootstrap/test_integration.py +++ b/tests/discovery/bootstrap/test_integration.py @@ -3,33 +3,18 @@ Test the full bootstrap discovery integration """ -import logging import secrets import pytest from libp2p import new_host -from libp2p.abc import PeerInfo from libp2p.crypto.secp256k1 import create_new_key_pair -from libp2p.discovery.events.peerDiscovery import peerDiscovery from libp2p.host.basic_host import BasicHost -# Configure logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger("bootstrap_test") - - -def on_peer_discovery(peer_info: PeerInfo) -> None: - """Handler for peer discovery events.""" - logger.info(f"๐Ÿ” Discovered peer: {peer_info.peer_id}") - logger.info(f" Addresses: {[str(addr) for addr in peer_info.addrs]}") - @pytest.mark.trio async def test_bootstrap_integration(): """Test bootstrap integration with new_host""" - print("๐Ÿงช Testing Bootstrap Integration") - # Test bootstrap addresses bootstrap_addrs = [ "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SznbYGzPwp8qDrq", @@ -40,18 +25,9 @@ async def test_bootstrap_integration(): secret = secrets.token_bytes(32) key_pair = create_new_key_pair(secret) - # Register peer discovery handler - peerDiscovery.register_peer_discovered_handler(on_peer_discovery) - - print(f"๐ŸŒ Testing with {len(bootstrap_addrs)} bootstrap peers") - # Create host with bootstrap host = new_host(key_pair=key_pair, bootstrap=bootstrap_addrs) - print("โœ… Successfully created host with bootstrap") - print(f"๐Ÿ“ Host peer ID: {host.get_id()}") - print("๐Ÿ”— Bootstrap discovery should process peers when host starts") - # 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" @@ -60,8 +36,6 @@ async def test_bootstrap_integration(): "Bootstrap addresses should match" ) - print("๐ŸŽ‰ Bootstrap integration test completed successfully!") - def test_bootstrap_no_addresses(): """Test that bootstrap is not initialized when no addresses provided""" diff --git a/tests/discovery/bootstrap/test_utils.py b/tests/discovery/bootstrap/test_utils.py new file mode 100644 index 00000000..ed5e9f43 --- /dev/null +++ b/tests/discovery/bootstrap/test_utils.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +""" +Test bootstrap address validation +""" + +import pytest +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 + "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SznbYGzPwp8qDrq", + "/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/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SznbYGzPwp8qDrq", + "/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}"