Skip to content

Commit

Permalink
Add tests for ip functions
Browse files Browse the repository at this point in the history
  • Loading branch information
itdependsnetworks committed Jul 31, 2023
1 parent f8e6e4e commit dfc9d6a
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 12 deletions.
2 changes: 1 addition & 1 deletion netutils/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ACLRule:
filter_same_ip: bool = True

def __init__(self, data: t.Any, *args: t.Any, **kwargs: t.Any): # pylint: disable=unused-argument
"""Initialze and load data.
"""Initialize and load data.
Args:
data: A dictionary with string keys and either string or list of string values
Expand Down
25 changes: 14 additions & 11 deletions netutils/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def is_ip_range(ip_range: str) -> bool:
"""Verifies whether or not a string is a valid IP address range.
Args:
ip: An IP address range in string format that is properly formatter.
ip_range: An IP address range in string format that is properly formatter.
Returns:
The result as to whether or not the string is a valid IP address.
Expand All @@ -251,7 +251,7 @@ def is_ip_range(ip_range: str) -> bool:
>>> from netutils.ip import is_ip_range
>>> is_ip_range("10.100.100.255")
False
>>> is_ip_range("10.100.100.255-10.100.100.1")
>>> is_ip_range("10.100.100.255-10.100.101.1")
True
>>>
"""
Expand All @@ -264,28 +264,31 @@ def is_ip_range(ip_range: str) -> bool:
end_ip_obj = ipaddress.ip_address(end_ip)
if not type(start_ip_obj) == type(end_ip_obj): # pylint: disable=unidiomatic-typecheck
return False
# IP version being the same is enforced above
if not start_ip_obj < end_ip_obj: # type: ignore
return False
return True


def get_range_ips(ip_range: str) -> t.Tuple[IPAddress, IPAddress]:
"""Verifies whether or not a string is a valid IP address range.
"""Get's the two IPs as a tuple of IPAddress objects.
Args:
ip: An IP address range in string format that is properly formatter.
ip_range: An IP address range in string format that is properly formatter.
Returns:
The result as to whether or not the string is a valid IP address.
The start and end IP address of the range provided.
Examples:
>>> from netutils.ip import is_ip_range
>>> is_ip_range("10.100.100.255")
False
>>> is_ip_range("10.100.100.255-10.100.100.1")
True
>>> from netutils.ip import get_range_ips
>>> get_range_ips("10.100.100.255-10.100.101.1")
(IPv4Address('10.100.100.255'), IPv4Address('10.100.101.1'))
>>> get_range_ips("2001::1-2001::10")
(IPv6Address('2001::1'), IPv6Address('2001::10'))
>>>
"""
if not is_ip_range(ip_range):
raise ValueError("Not a valid IP range format of $start_ip-$endip")
raise ValueError("Not a valid IP range format of $start_ip-$end_ip")
start_ip, end_ip = ip_range.split("-")
start_ip_obj = ipaddress.ip_address(start_ip)
end_ip_obj = ipaddress.ip_address(end_ip)
Expand Down
167 changes: 167 additions & 0 deletions tests/unit/test_ip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test for the IP functions."""
import ipaddress
import pytest

from netutils import ip
Expand Down Expand Up @@ -160,6 +161,145 @@
},
]

IS_IP_RANGE = [
{
"sent": {
"ip_range": "10.1.1.1",
},
"received": False,
},
{
"sent": {
"ip_range": "10.1.100.10-10.1.100.1",
},
"received": False,
},
{
"sent": {
"ip_range": "2001::10-2001::1",
},
"received": False,
},
{
"sent": {
"ip_range": "10.500.100.10-10.1.100.1",
},
"received": False,
},
{
"sent": {
"ip_range": "NOT AN IP",
},
"received": False,
},
{
"sent": {
"ip_range": "255.255.255.256",
},
"received": False,
},
{
"sent": {
"ip_range": "10.1.100.10-10.1.100.100",
},
"received": True,
},
{
"sent": {
"ip_range": "2001::10-2001::100",
},
"received": True,
},
]

GET_RANGE_IPS = [
{
"sent": {
"ip_range": "2001::10-2001::100",
},
"received": (ipaddress.IPv6Address("2001::10"), ipaddress.IPv6Address("2001::100")),
},
{
"sent": {
"ip_range": "10.1.100.10-10.1.100.100",
},
"received": (ipaddress.IPv4Address("10.1.100.10"), ipaddress.IPv4Address("10.1.100.100")),
},
]

IS_IP_WITHIN = [
{
"sent": {
"ip": "192.168.1.10",
"ip_compare": "192.168.1.10",
},
"received": True,
},
{
"sent": {
"ip": "192.168.1.10",
"ip_compare": "192.168.1.0-192.168.1.20",
},
"received": True,
},
{
"sent": {
"ip": "192.168.1.0/24",
"ip_compare": ["192.168.1.0-192.168.1.20", "192.168.2.0-192.168.2.20"],
},
"received": False,
},
{
"sent": {
"ip": "192.168.1.10-192.168.1.15",
"ip_compare": ["192.168.1.0-192.168.1.20", "192.168.2.0-192.168.2.20"],
},
"received": True,
},
{
"sent": {
"ip": "10.0.0.0/8",
"ip_compare": ["192.168.1.0/24", "172.16.0.0/12"],
},
"received": False,
},
{
"sent": {
"ip": "192.168.1.10",
"ip_compare": "192.168.1.20",
},
"received": False,
},
{
"sent": {
"ip": "192.168.1.10",
"ip_compare": "192.168.2.0-192.168.2.20",
},
"received": False,
},
{
"sent": {
"ip": "192.168.1.0/24",
"ip_compare": ["192.168.2.0-192.168.2.20", "192.168.3.0-192.168.3.20"],
},
"received": False,
},
{
"sent": {
"ip": "192.168.1.50-192.168.1.60",
"ip_compare": ["192.168.2.0-192.168.2.20", "192.168.3.0-192.168.3.20"],
},
"received": False,
},
{
"sent": {
"ip": "192.168.1.10",
"ip_compare": "192.168.2.0/24",
},
"received": False,
},
]

GET_BROADCAST_ADDRESS = [
{
"sent": {"ip_network": "10.1.1.0/24"},
Expand Down Expand Up @@ -357,6 +497,33 @@ def test_is_ip(data):
assert ip.is_ip(**data["sent"]) == data["received"]


@pytest.mark.parametrize("data", IS_IP_RANGE)
def test_is_ip_range(data):
assert ip.is_ip_range(**data["sent"]) == data["received"]


@pytest.mark.parametrize("data", GET_RANGE_IPS)
def test_get_range_ips(data):
assert ip.get_range_ips(**data["sent"]) == data["received"]


def test_get_range_ips_fail():
with pytest.raises(ValueError, match=r"Not a valid IP range format of .*"):
data = {"ip_range": "10.1.100.10-10.1.100.1"}
ip.get_range_ips(**data)


@pytest.mark.parametrize("data", IS_IP_WITHIN)
def test_is_ip_within(data):
assert ip.is_ip_within(**data["sent"]) == data["received"]


def test_is_ip_within_fail():
with pytest.raises(AssertionError):
data = {"ip": "10.1.100.100", "ip_compare": "10.1.100.10-2001::1"}
ip.is_ip_within(**data)


@pytest.mark.parametrize("data", GET_ALL_HOST)
def test_get_all_host(data):
assert list(ip.get_all_host(**data["sent"])) == data["received"]
Expand Down

0 comments on commit dfc9d6a

Please sign in to comment.