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