diff options
| author | Bobby <[email protected]> | 2022-09-10 20:36:32 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-10 20:36:32 -0400 |
| commit | 55a13a4ab4004ca38177e1c8735df2341ad935b7 (patch) | |
| tree | 31312271b27e4aee9fb1a1636ca12cccac2c8b15 | |
| parent | 5085e5d09086766e2b3a55f6d9e2f797015f25d0 (diff) | |
| download | edify-55a13a4ab4004ca38177e1c8735df2341ad935b7.tar.xz edify-55a13a4ab4004ca38177e1c8735df2341ad935b7.zip | |
Added ipv4 and ipv6 validations
| -rw-r--r-- | src/edify/library/__init__.py | 2 | ||||
| -rw-r--r-- | src/edify/library/ip.py | 34 | ||||
| -rw-r--r-- | tests/test_ip.py | 35 |
3 files changed, 71 insertions, 0 deletions
diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index e77c7f6..6bf12a5 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -1,6 +1,8 @@ # flake8: noqa # Import everything from the library. +from .ip import ipv4 +from .ip import ipv6 from .mail import email from .mail import email_rfc_5322 from .phone import phone_number diff --git a/src/edify/library/ip.py b/src/edify/library/ip.py new file mode 100644 index 0000000..d04b69b --- /dev/null +++ b/src/edify/library/ip.py @@ -0,0 +1,34 @@ +import re + +ipv4_pattern = "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" # noqa +ipv6_pattern = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$" # noqa + + +def ipv4(ip: str) -> bool: + """Checks if a string is a valid IPv4 address. + + Args: + ip (str): The string to check. + Returns: + bool: True if the string is a valid IPv4 address, False otherwise. + """ + + if re.match(ipv4_pattern, ip): + return True + else: + return False + + +def ipv6(ip: str) -> bool: + """Checks if a string is a valid IPv6 address. + + Args: + ip (str): The string to check. + Returns: + bool: True if the string is a valid IPv6 address, False otherwise. + """ + + if re.match(ipv6_pattern, ip): + return True + else: + return False diff --git a/tests/test_ip.py b/tests/test_ip.py new file mode 100644 index 0000000..dc081a5 --- /dev/null +++ b/tests/test_ip.py @@ -0,0 +1,35 @@ +from edify.library import ipv4 +from edify.library import ipv6 + +# Generate ipv4 dictionary +ipv4_dict = { + "192.168.0.1": True, + "244.232.123.233": True, + "363.232.123.233": False, + "234.234234.234.234": False, + "12.12.12.12.12": False, + "0.0.0.0": True, + "987.987.987.987": False, +} + +# Generate ipv6 dictionary +ipv6_dict = { + "2001:0db8:85a3:0000:0000:8a2e:0370:7334": True, + "2001:db8:85a3:0:0:8a2e:370:7334": True, + "2001:db8:85a3::8a2e:370:7334": True, + "2001:db8:85a3:0:0:8A2E:370:7334": True, + "2001:db8:85a3:0:0:8a2e:370:7334:": False, + "2001:db8:85a3:0:0:8a2e:370:7334:7334": False, + "2001:db8:85a3:0:0:8a2e:370:7334:7334:7334": False, + "2001:db8:85a3:0:0:8a2e:370:7334:7334:7334:7334": False, +} + + +def test_ipv4(): + for ip, expectation in ipv4_dict.items(): + assert ipv4(ip) == expectation + + +def test_ipv6(): + for ip, expectation in ipv6_dict.items(): + assert ipv6(ip) == expectation |
