"""
_re = re.compile(
- r"(?=^.{,253}$)" # max 253 chars
- r"^([a-zA-Z0-9]" # do not start with hyphen
- r"([a-zA-Z0-9-]){1,61}" # max 63 chars in label
- r"[a-zA-Z0-9]\.)" # do not end with hyphen
- r"{0,126}" # max 126 levels+TLD
- r"([a-zA-Z]){2,6}($|.$)" # TLD; end with or without '.'
+ r"(?=^.{,253}\.?$)" # max 253 chars
+ r"^(?!\.)" # do not start name with dot
+ r"((?!-)" # do not start label with hyphen
+ r"\.?[a-zA-Z0-9-]{,62}" # max 63 chars in label
+ r"[a-zA-Z0-9])+" # do not end label with hyphen
+ r"\.?$" # end with or without '.'
)
def __init__(self, source_value: Any, object_path: str = "/") -> None:
return hash(self._value)
return hash(f"{self._value}.")
- def punycode(self) -> bytes:
- return self._value.encode("idna")
+ def punycode(self) -> str:
+ return self._value.encode("idna").decode("utf-8")
@classmethod
def json_schema(cls: Type["DomainName"]) -> Dict[Any, Any]:
import ipaddress
+import random
+import string
from typing import Any
import pytest
from knot_resolver_manager.utils import SchemaNode
+def _rand_domain(label_chars: int, levels: int = 1) -> str:
+ return "".join(
+ ["".join(random.choices(string.ascii_letters + string.digits, k=label_chars)) + "." for i in range(levels)]
+ )
+
+
@pytest.mark.parametrize("val", [1, 65_535, 5353, 5000])
def test_port_number_valid(val: int):
assert int(PortNumber(val)) == val
assert str(TestSchema({"p": "/tmp"}).p) == "/tmp"
-@pytest.mark.parametrize("val", ["example.com.", "test.example.com", "test-example.com", "bücher.com.", "příklad.cz"])
+@pytest.mark.parametrize(
+ "val",
+ [
+ "example.com",
+ "this.is.example.com.",
+ "test.example.com",
+ "test-example.com",
+ "bücher.com.",
+ "příklad.cz",
+ _rand_domain(63),
+ _rand_domain(1, 127),
+ ],
+)
def test_domain_name_valid(val: str):
o = DomainName(val)
assert str(o) == val
assert o == DomainName(val)
- assert o.punycode() == val.encode("idna")
+ assert o.punycode() == val.encode("idna").decode("utf-8")
-@pytest.mark.parametrize("val", ["test.example.com..", "-example.com", "test-.example.net"])
+@pytest.mark.parametrize(
+ "val",
+ [
+ "test.example..com.",
+ "-example.com",
+ "test-.example.net",
+ ".example.net",
+ _rand_domain(64),
+ _rand_domain(1, 128),
+ ],
+)
def test_domain_name_invalid(val: str):
with raises(KresManagerException):
DomainName(val)