Use package ipaddress for IP address validation in API tests

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-11-15 11:49:03 +01:00
parent a356cbd13b
commit a69130585e
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
1 changed files with 12 additions and 15 deletions

View File

@ -223,24 +223,21 @@ class ResponseVerifyer():
# Check if a string is a valid IPv4 address
def valid_ipv4(self, addr: str) -> bool:
octets = addr.split(".") # type: list[str]
if len(octets) != 4:
return False
for octet in octets:
if not octet.isdigit():
return False
if int(octet) < 0 or int(octet) > 255:
return False
return True
try:
if type(ipaddress.ip_address(addr)) is ipaddress.IPv4Address:
return True
except ValueError:
pass
return False
# Check if a string is a valid IPv6 address
def valid_ipv6(self, addr: str) -> bool:
# Split the address into parts
parts = addr.split(":") # type: list[str]
# Check if the address is a valid IPv6 address
if len(parts) != 8:
return False
try:
if type(ipaddress.ip_address(addr)) is ipaddress.IPv6Address:
return True
except ValueError:
pass
return False
# Verify a single property's type