This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from ddns.update import get_ipv4, get_ipv6
|
||||
|
||||
regex_match_ipv4 = (
|
||||
r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
||||
)
|
||||
|
||||
regex_match_ipv6 = (
|
||||
r"(([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}:)"
|
||||
r"{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]"
|
||||
r"{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]"
|
||||
r"{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})|:((:"
|
||||
r"[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})"
|
||||
r"{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])"
|
||||
r"{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}"
|
||||
r"(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
|
||||
)
|
||||
|
||||
|
||||
class TestGetIpv4:
|
||||
def test_get_ipv4_returns_valid_address(self) -> None:
|
||||
mock_result = MagicMock()
|
||||
mock_result.stdout = b"203.0.113.1\n"
|
||||
|
||||
with patch("ddns.update.subprocess.run", return_value=mock_result):
|
||||
ip = get_ipv4()
|
||||
|
||||
assert re.match(regex_match_ipv4, ip.strip())
|
||||
|
||||
def test_get_ipv4_with_trailing_whitespace(self) -> None:
|
||||
mock_result = MagicMock()
|
||||
mock_result.stdout = b" 198.51.100.42 \n"
|
||||
|
||||
with patch("ddns.update.subprocess.run", return_value=mock_result):
|
||||
ip = get_ipv4()
|
||||
|
||||
assert re.match(regex_match_ipv4, ip.strip())
|
||||
|
||||
|
||||
class TestGetIpv6:
|
||||
def test_get_ipv6_returns_valid_address(self) -> None:
|
||||
mock_result = MagicMock()
|
||||
mock_result.stdout = b"2001:db8::1\n"
|
||||
|
||||
with patch("ddns.update.subprocess.run", return_value=mock_result):
|
||||
ip = get_ipv6()
|
||||
|
||||
assert re.match(regex_match_ipv6, ip.strip())
|
||||
|
||||
def test_get_ipv6_with_trailing_whitespace(self) -> None:
|
||||
mock_result = MagicMock()
|
||||
mock_result.stdout = b" 2001:db8::dead:beef \n"
|
||||
|
||||
with patch("ddns.update.subprocess.run", return_value=mock_result):
|
||||
ip = get_ipv6()
|
||||
|
||||
assert re.match(regex_match_ipv6, ip.strip())
|
||||
Reference in New Issue
Block a user