Skip to content

Commit 13a7773

Browse files
committed
test: cover RFC length limits for domain() and hostname()
1 parent 3aa3b67 commit 13a7773

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎tests/test_domain.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,53 @@ def test_returns_failed_validation_invalid_top_level_domain(
110110
domain(value, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782),
111111
ValidationError,
112112
)
113+
114+
115+
def _name_of_length(length: int):
116+
"""Build a dotted name of exactly `length` characters, ending with `.com`."""
117+
labels: list[str] = []
118+
remaining = length - len(".com")
119+
while remaining > 0:
120+
size = min(61, remaining)
121+
if remaining - size == 1: # avoid leaving a lone "." behind
122+
size -= 1
123+
labels.append("a" * size)
124+
remaining -= size + 1
125+
name = ".".join(labels) + ".com"
126+
assert len(name) == length
127+
return name
128+
129+
130+
@pytest.mark.parametrize(
131+
"value",
132+
[
133+
# total length: at most 253 characters (RFC 1035)
134+
_name_of_length(253),
135+
# label length: at most 63 characters (RFC 1035)
136+
"a" * 63 + ".com",
137+
# a label may start with a digit (RFC 1123)
138+
"1host.example.com",
139+
],
140+
)
141+
def test_returns_true_on_domain_within_rfc_length_limits(value: str):
142+
"""Test returns true on domain within RFC length limits."""
143+
assert domain(value)
144+
145+
146+
@pytest.mark.parametrize(
147+
"value",
148+
[
149+
_name_of_length(254),
150+
_name_of_length(255),
151+
# many short, dot-separated labels that exceed 253 characters; see #413
152+
".".join(["kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc"] * 4)
153+
+ ".kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXcab",
154+
"a" * 64 + ".com",
155+
# a label must not start or end with a hyphen
156+
"-host.example.com",
157+
"host-.example.com",
158+
],
159+
)
160+
def test_returns_failed_validation_on_domain_beyond_rfc_limits(value: str):
161+
"""Test returns failed validation on domain beyond RFC limits."""
162+
assert isinstance(domain(value), ValidationError)

‎tests/test_hostname.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,38 @@ def test_returns_true_on_valid_hostname(value: str, rfc_1034: bool, rfc_2782: bo
7070
def test_returns_failed_validation_on_invalid_hostname(value: str, rfc_1034: bool, rfc_2782: bool):
7171
"""Test returns failed validation on invalid hostname."""
7272
assert isinstance(hostname(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782), ValidationError)
73+
74+
75+
@pytest.mark.parametrize(
76+
"value",
77+
[
78+
# total length: at most 253 characters (RFC 1035)
79+
".".join(["a" * 63, "a" * 63, "a" * 63, "a" * 61]),
80+
# label length: at most 63 characters (RFC 1035)
81+
"a" * 63 + ".com",
82+
# a label may start with a digit (RFC 1123)
83+
"1host.example.com",
84+
],
85+
)
86+
def test_returns_true_on_hostname_within_rfc_length_limits(value: str):
87+
"""Test returns true on hostname within RFC length limits."""
88+
assert hostname(value)
89+
90+
91+
@pytest.mark.parametrize(
92+
"value",
93+
[
94+
# many short, dot-separated labels that exceed 253 characters; see #413
95+
".".join(["kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc"] * 4)
96+
+ ".kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXcab",
97+
".".join(["a" * 63, "a" * 63, "a" * 63, "a" * 62]),
98+
"a" * 64 + ".com",
99+
# a label must not start or end with a hyphen, nor contain an underscore
100+
"-host.example.com",
101+
"host-.example.com",
102+
"ho_st.example.com",
103+
],
104+
)
105+
def test_returns_failed_validation_on_hostname_beyond_rfc_limits(value: str):
106+
"""Test returns failed validation on hostname beyond RFC limits."""
107+
assert isinstance(hostname(value), ValidationError)

0 commit comments

Comments
 (0)