mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-03-22 13:11:27 +00:00
refactor: performed pre-commit checks
This commit is contained in:
@ -8,6 +8,7 @@ from libp2p.utils.address_validation import (
|
||||
get_optimal_binding_address,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
print("=== Address Validation Utilities Demo ===\n")
|
||||
|
||||
@ -60,5 +61,6 @@ def main():
|
||||
if "0.0.0.0" in str(addr):
|
||||
print(f" ⚠️ Warning: {protocol} returned wildcard address")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@ -2,8 +2,6 @@
|
||||
Tests to verify that all examples use 127.0.0.1 instead of 0.0.0.0
|
||||
"""
|
||||
|
||||
import ast
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@ -17,19 +15,19 @@ class TestExamplesBindAddress:
|
||||
|
||||
def check_file_for_wildcard_binding(self, filepath):
|
||||
"""Check if a file contains 0.0.0.0 binding"""
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
with open(filepath, encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
# Check for various forms of wildcard binding
|
||||
wildcard_patterns = [
|
||||
'0.0.0.0',
|
||||
'/ip4/0.0.0.0/',
|
||||
"0.0.0.0",
|
||||
"/ip4/0.0.0.0/",
|
||||
]
|
||||
|
||||
found_wildcards = []
|
||||
for line_num, line in enumerate(content.splitlines(), 1):
|
||||
for pattern in wildcard_patterns:
|
||||
if pattern in line and not line.strip().startswith('#'):
|
||||
if pattern in line and not line.strip().startswith("#"):
|
||||
found_wildcards.append((line_num, line.strip()))
|
||||
|
||||
return found_wildcards
|
||||
@ -40,7 +38,7 @@ class TestExamplesBindAddress:
|
||||
|
||||
# Skip certain files that might legitimately discuss wildcards
|
||||
skip_files = [
|
||||
'network_discover.py', # This demonstrates wildcard expansion
|
||||
"network_discover.py", # This demonstrates wildcard expansion
|
||||
]
|
||||
|
||||
files_with_wildcards = {}
|
||||
@ -69,11 +67,11 @@ class TestExamplesBindAddress:
|
||||
|
||||
# Files that should contain listen addresses
|
||||
files_with_networking = [
|
||||
'ping/ping.py',
|
||||
'chat/chat.py',
|
||||
'bootstrap/bootstrap.py',
|
||||
'pubsub/pubsub.py',
|
||||
'identify/identify.py',
|
||||
"ping/ping.py",
|
||||
"chat/chat.py",
|
||||
"bootstrap/bootstrap.py",
|
||||
"pubsub/pubsub.py",
|
||||
"identify/identify.py",
|
||||
]
|
||||
|
||||
for filename in files_with_networking:
|
||||
@ -86,15 +84,16 @@ class TestExamplesBindAddress:
|
||||
if filepath is None:
|
||||
continue
|
||||
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
with open(filepath, encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
# Check for proper loopback usage
|
||||
has_loopback = '127.0.0.1' in content or 'localhost' in content
|
||||
has_multiaddr_loopback = '/ip4/127.0.0.1/' in content
|
||||
has_loopback = "127.0.0.1" in content or "localhost" in content
|
||||
has_multiaddr_loopback = "/ip4/127.0.0.1/" in content
|
||||
|
||||
assert has_loopback or has_multiaddr_loopback, \
|
||||
assert has_loopback or has_multiaddr_loopback, (
|
||||
f"{filepath} should use loopback address (127.0.0.1)"
|
||||
)
|
||||
|
||||
def test_doc_examples_use_loopback(self):
|
||||
"""Test that documentation examples use secure addresses"""
|
||||
@ -106,5 +105,6 @@ class TestExamplesBindAddress:
|
||||
|
||||
for filepath in doc_example_files:
|
||||
wildcards = self.check_file_for_wildcard_binding(filepath)
|
||||
assert not wildcards, \
|
||||
assert not wildcards, (
|
||||
f"Documentation example {filepath} contains wildcard binding"
|
||||
)
|
||||
|
||||
@ -13,7 +13,10 @@ from libp2p.utils.address_validation import (
|
||||
|
||||
|
||||
class TestDefaultBindAddress:
|
||||
"""Test suite for verifying default bind addresses use secure addresses (not 0.0.0.0)"""
|
||||
"""
|
||||
Test suite for verifying default bind addresses use
|
||||
secure addresses (not 0.0.0.0)
|
||||
"""
|
||||
|
||||
def test_default_bind_address_is_not_wildcard(self):
|
||||
"""Test that default bind address is NOT 0.0.0.0 (wildcard)"""
|
||||
@ -93,7 +96,9 @@ class TestDefaultBindAddress:
|
||||
|
||||
# Check that we don't expose on all interfaces by default
|
||||
wildcard_addrs = [addr for addr in interfaces if "0.0.0.0" in str(addr)]
|
||||
assert len(wildcard_addrs) == 0, "Found wildcard addresses in default interfaces"
|
||||
assert len(wildcard_addrs) == 0, (
|
||||
"Found wildcard addresses in default interfaces"
|
||||
)
|
||||
|
||||
# Verify optimal address selection doesn't choose wildcard
|
||||
optimal = get_optimal_binding_address(port)
|
||||
@ -114,8 +119,12 @@ class TestDefaultBindAddress:
|
||||
assert len(loopback_addrs) > 0, "Loopback address should always be available"
|
||||
|
||||
# At least one loopback address should have the correct port
|
||||
loopback_with_port = [addr for addr in loopback_addrs if f"/tcp/{port}" in str(addr)]
|
||||
assert len(loopback_with_port) > 0, f"Loopback address with port {port} should be available"
|
||||
loopback_with_port = [
|
||||
addr for addr in loopback_addrs if f"/tcp/{port}" in str(addr)
|
||||
]
|
||||
assert len(loopback_with_port) > 0, (
|
||||
f"Loopback address with port {port} should be available"
|
||||
)
|
||||
|
||||
def test_optimal_address_selection_behavior(self):
|
||||
"""Test that optimal address selection works correctly"""
|
||||
@ -129,16 +138,24 @@ class TestDefaultBindAddress:
|
||||
# Should return one of the available interfaces
|
||||
optimal_str = str(optimal)
|
||||
interface_strs = [str(addr) for addr in interfaces]
|
||||
assert optimal_str in interface_strs, f"Optimal address {optimal_str} should be in available interfaces"
|
||||
assert optimal_str in interface_strs, (
|
||||
f"Optimal address {optimal_str} should be in available interfaces"
|
||||
)
|
||||
|
||||
# If non-loopback interfaces are available, should prefer them
|
||||
non_loopback_interfaces = [addr for addr in interfaces if "127.0.0.1" not in str(addr)]
|
||||
non_loopback_interfaces = [
|
||||
addr for addr in interfaces if "127.0.0.1" not in str(addr)
|
||||
]
|
||||
if non_loopback_interfaces:
|
||||
# Should prefer non-loopback when available
|
||||
assert "127.0.0.1" not in str(optimal), "Should prefer non-loopback when available"
|
||||
assert "127.0.0.1" not in str(optimal), (
|
||||
"Should prefer non-loopback when available"
|
||||
)
|
||||
else:
|
||||
# Should use loopback when no other interfaces available
|
||||
assert "127.0.0.1" in str(optimal), "Should use loopback when no other interfaces available"
|
||||
assert "127.0.0.1" in str(optimal), (
|
||||
"Should use loopback when no other interfaces available"
|
||||
)
|
||||
|
||||
def test_address_validation_utilities_behavior(self):
|
||||
"""Test that address validation utilities behave as expected"""
|
||||
@ -146,7 +163,9 @@ class TestDefaultBindAddress:
|
||||
|
||||
# Test that we get multiple interface options
|
||||
interfaces = get_available_interfaces(port)
|
||||
assert len(interfaces) >= 2, "Should have at least loopback + one network interface"
|
||||
assert len(interfaces) >= 2, (
|
||||
"Should have at least loopback + one network interface"
|
||||
)
|
||||
|
||||
# Test that loopback is always included
|
||||
has_loopback = any("127.0.0.1" in str(addr) for addr in interfaces)
|
||||
@ -158,4 +177,6 @@ class TestDefaultBindAddress:
|
||||
|
||||
# Test optimal selection
|
||||
optimal = get_optimal_binding_address(port)
|
||||
assert optimal in interfaces, "Optimal address should be from available interfaces"
|
||||
assert optimal in interfaces, (
|
||||
"Optimal address should be from available interfaces"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user