import sys
import ipaddress
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from cloudcheck.providers.amazon import Amazon
from cloudcheck.helpers import defrag_cidrs, cidrs_to_strings, strings_to_cidrs
def test_v2fly_domains():
amazon = Amazon()
domains, errors = amazon.fetch_v2fly_domains()
assert domains, "No domains fetched from v2fly"
assert not errors, "Failed to fetch domains from v2fly"
print(f"Successfully fetched {len(domains)} domains")
assert "kindle" in domains, "Kindle domain not found in fetched domains"
def test_cidr_defragmentation():
print("Testing CIDR defragmentation...")
cidr_strings = [
"192.168.1.0/30", "192.168.1.4/30", "192.168.1.8/30", "192.168.1.12/30", "192.168.1.16/30", "192.168.1.20/30", "192.168.1.24/30", "192.168.1.28/30", "192.168.2.0/30", "192.168.2.4/30", "192.168.2.8/30", "192.168.2.12/30", "192.168.2.16/30", "192.168.2.20/30", "192.168.2.24/30", "192.168.2.28/30", "192.168.3.0/28", "192.168.3.16/28", "192.168.3.32/28", "192.168.3.48/28", "192.168.3.64/28", "192.168.3.80/28", "192.168.3.96/28", "192.168.3.112/28", "192.168.3.128/28", "192.168.3.144/28", "192.168.3.160/28", "192.168.3.176/28", "192.168.3.192/28", "192.168.3.208/28", "192.168.3.224/28", "192.168.3.240/28", "10.0.0.0/24",
"172.16.0.0/16",
]
print(f"Starting with {len(cidr_strings)} CIDR blocks")
networks = strings_to_cidrs(cidr_strings)
print(f"Converted to {len(networks)} network objects")
defragmented = defrag_cidrs(networks)
defragmented_strings = cidrs_to_strings(defragmented)
print(f"After defragmentation: {len(defragmented)} CIDR blocks")
print("Defragmented CIDRs:")
for cidr in sorted(defragmented_strings):
print(f" {cidr}")
assert len(defragmented) < len(networks), (
"Defragmentation should reduce the number of networks"
)
expected_networks = {
"192.168.1.0/27", "192.168.2.0/27", "192.168.3.0/24", "10.0.0.0/24", "172.16.0.0/16", }
actual_networks = set(defragmented_strings)
assert actual_networks == expected_networks, (
f"Expected {expected_networks}, got {actual_networks}"
)
print("CIDR defragmentation test passed!")
def test_cidr_defragmentation_ipv6():
print("Testing IPv6 CIDR defragmentation...")
cidr_strings = [
"2001:db8::/126", "2001:db8::4/126", "2001:db8::8/126", "2001:db8::c/126", "2001:db8::10/126", "2001:db8::14/126", "2001:db8::18/126", "2001:db8::1c/126", "2001:db8:1::/126", "2001:db8:1::4/126", "2001:db8:1::8/126", "2001:db8:1::c/126", "2001:db8:1::10/126", "2001:db8:1::14/126", "2001:db8:1::18/126", "2001:db8:1::1c/126", "2001:db8:2::/124", "2001:db8:2::10/124", "2001:db8:2::20/124", "2001:db8:2::30/124", "2001:db8:2::40/124", "2001:db8:2::50/124", "2001:db8:2::60/124", "2001:db8:2::70/124", "2001:db8:2::80/124", "2001:db8:2::90/124", "2001:db8:2::a0/124", "2001:db8:2::b0/124", "2001:db8:2::c0/124", "2001:db8:2::d0/124", "2001:db8:2::e0/124", "2001:db8:2::f0/124", "2001:db8:3::/120",
"2001:db8:4::/112",
]
print(f"Starting with {len(cidr_strings)} IPv6 CIDR blocks")
networks = strings_to_cidrs(cidr_strings)
print(f"Converted to {len(networks)} network objects")
defragmented = defrag_cidrs(networks)
defragmented_strings = cidrs_to_strings(defragmented)
print(f"After defragmentation: {len(defragmented)} IPv6 CIDR blocks")
print("Defragmented IPv6 CIDRs:")
for cidr in sorted(defragmented_strings):
print(f" {cidr}")
assert len(defragmented) < len(networks), (
"Defragmentation should reduce the number of networks"
)
expected_networks = {
"2001:db8::/123", "2001:db8:1::/123", "2001:db8:2::/120", "2001:db8:3::/120", "2001:db8:4::/112", }
actual_networks = set(defragmented_strings)
assert actual_networks == expected_networks, (
f"Expected {expected_networks}, got {actual_networks}"
)
print("IPv6 CIDR defragmentation test passed!")
def test_cidr_defragmentation_mixed_ipv4_ipv6():
print("Testing mixed IPv4/IPv6 CIDR defragmentation...")
cidr_strings = [
"192.168.1.0/30",
"192.168.1.4/30",
"192.168.1.8/30",
"192.168.1.12/30",
"2001:db8::/126",
"2001:db8::4/126",
"2001:db8::8/126",
"2001:db8::c/126",
"172.16.0.0/30",
"172.16.0.4/30",
"2001:db8::/30",
"2001:dbc::/30",
"10.0.0.0/24",
"2001:db8:4::/120",
]
print(f"Starting with {len(cidr_strings)} mixed CIDR blocks")
networks = strings_to_cidrs(cidr_strings)
print(f"Converted to {len(networks)} network objects")
ipv4_count = sum(1 for n in networks if isinstance(n, ipaddress.IPv4Network))
ipv6_count = sum(1 for n in networks if isinstance(n, ipaddress.IPv6Network))
assert ipv4_count > 0, "Should have IPv4 networks"
assert ipv6_count > 0, "Should have IPv6 networks"
print(f"Found {ipv4_count} IPv4 and {ipv6_count} IPv6 networks")
defragmented = defrag_cidrs(networks)
defragmented_strings = cidrs_to_strings(defragmented)
print(f"After defragmentation: {len(defragmented)} CIDR blocks")
print("Defragmented CIDRs:")
for cidr in sorted(defragmented_strings):
print(f" {cidr}")
assert len(defragmented) < len(networks), (
"Defragmentation should reduce the number of networks"
)
defrag_ipv4_count = sum(
1 for n in defragmented if isinstance(n, ipaddress.IPv4Network)
)
defrag_ipv6_count = sum(
1 for n in defragmented if isinstance(n, ipaddress.IPv6Network)
)
assert defrag_ipv4_count > 0, (
"Should still have IPv4 networks after defragmentation"
)
assert defrag_ipv6_count > 0, (
"Should still have IPv6 networks after defragmentation"
)
expected_networks = {
"192.168.1.0/28", "172.16.0.0/29", "10.0.0.0/24", "2001:db8::/124", "2001:db8::/29", "2001:db8:4::/120", }
actual_networks = set(defragmented_strings)
assert actual_networks == expected_networks, (
f"Expected {expected_networks}, got {actual_networks}"
)
print("Mixed IPv4/IPv6 CIDR defragmentation test passed!")