add find_parent function

This commit is contained in:
Pavel Patsey
2025-01-22 23:12:21 +03:00
parent faa2849e6b
commit 725b80544a
2 changed files with 42 additions and 60 deletions
+28 -46
View File
@@ -3,10 +3,8 @@ import pytest
from vpn_manager.cidr4_merge.cidr4_merger import (
Cidr4MergerError,
cidr4_to_node,
get_net_addr,
get_parent_ip,
find_parent,
make_cidr4,
make_parent,
sort_nodes,
)
@@ -15,23 +13,6 @@ def test_true():
assert True
bin_a = "10011000000000001000010000010000"
assert len(bin_a) == 32
ip_a = int(bin_a, 2)
bin_b = "10011100000000000000000000101011"
assert len(bin_b) == 32
ip_b = int(bin_b, 2)
bin_c = "10011000000000000000000000000000"
assert len(bin_c) == 32
ip_c = int(bin_c, 2)
bin_d = "11111100000000000000000000000000"
assert len(bin_c) == 32
ip_d = int(bin_d, 2)
def test_cidr4_to_node():
assert cidr4_to_node("4.78.139.0/24") == (72256256, 24)
assert cidr4_to_node("0.0.0.0/32") == (0, 32)
@@ -57,24 +38,6 @@ def test_make_cidr4():
assert make_cidr4(520969728, 23) == "31.13.94.0/23"
def test_get_net_addr():
assert get_net_addr(ip_a, 5) == ip_c
assert get_net_addr(ip_b, 5) == ip_c
assert get_net_addr(0, 1) == 0
assert get_net_addr(0, 0) == 0
def test_get_parent_mask():
assert get_parent_ip(ip_a, 6) == ip_c
assert get_parent_ip(ip_b, 6) == ip_c
assert get_parent_ip(0, 1) == 0
with pytest.raises(Exception) as exc_info:
get_parent_ip(0, 0)
assert str(exc_info.value) == "The top of the tree has no parent!"
assert exc_info.type is Cidr4MergerError
def test_sort_nodes():
assert sort_nodes(
[
@@ -91,14 +54,33 @@ def test_sort_nodes():
]
@pytest.mark.skip(reason="broken")
def test_make_parent():
assert make_parent((0, 2, 12, 0), (1073741824, 2, 3, 0)) == (0, 1, 15, 0)
assert make_parent(
(2147483648, 2, 1, 2147483648), (3221225472, 2, 2, 2147483648)
) == (2147483648, 1, 3, 0)
def test_find_parent():
assert find_parent((0, 2), (1073741824, 2)) == (0, 1)
assert find_parent((2147483648, 2), (3221225472, 2)) == (2147483648, 1)
assert find_parent((0, 2), (3221225472, 2)) == (0, 0)
assert find_parent((1, 32), (6, 32)) == (0, 29)
def test_find_parent__with_exception():
with pytest.raises(Exception) as exc_info:
find_parent((0, 32), (0, 29))
assert (
str(exc_info.value)
== "Error! Trying to find common parent of network and subnet! parent_node=(0, 29), a=(0, 32), b=(0, 29)."
)
assert exc_info.type is Cidr4MergerError
with pytest.raises(Exception) as exc_info:
find_parent((0, 1), (1073741824, 2))
assert (
str(exc_info.value)
== "Error! Trying to find common parent of network and subnet! parent_node=(0, 1), a=(0, 1), b=(1073741824, 2)."
)
assert exc_info.type is Cidr4MergerError
with pytest.raises(Exception) as exc_info:
make_parent((0, 2, 12, 0), (3221225472, 2, 2, 2147483648))
assert str(exc_info.value) == "Nodes must be neighbors!"
find_parent((0, 0), (3221225472, 2))
assert (
str(exc_info.value)
== "Error! Trying to find common parent of network and subnet! parent_node=(0, 0), a=(0, 0), b=(3221225472, 2)."
)
assert exc_info.type is Cidr4MergerError