| 1 |
# Copyright (c) 2021, Felix Fontein <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this file contains some public domain test data from |
| 6 |
# https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt |
| 7 |
# The data is marked and documented as public domain appropriately. |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import typing as t |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from ansible_collections.community.dns.plugins.plugin_utils._public_suffix import ( |
| 16 |
PUBLIC_SUFFIX_LIST, |
| 17 |
PublicSuffixList, |
| 18 |
) |
| 19 |
|
| 20 |
TEST_GET_SUFFIX = [ |
| 21 |
("", {}, {}, "", ""), |
| 22 |
(".", {}, {}, "", ""), |
| 23 |
("foo.com", {}, {}, "com", "foo.com"), |
| 24 |
("bar.foo.com.", {}, {}, "com.", "foo.com."), |
| 25 |
("BaR.fOo.CoM.", {"normalize_result": True}, {}, "com.", "foo.com."), |
| 26 |
("BaR.fOo.CoM.", {}, {}, "CoM.", "fOo.CoM."), |
| 27 |
("com", {}, {}, "com", ""), |
| 28 |
("com", {}, {"only_if_registerable": False}, "com", "com"), |
| 29 |
("com", {"keep_unknown_suffix": False}, {}, "com", ""), |
| 30 |
("foo.com", {}, {}, "com", "foo.com"), |
| 31 |
("foo.com", {"keep_unknown_suffix": False}, {}, "com", "foo.com"), |
| 32 |
("foobarbaz", {}, {}, "foobarbaz", ""), |
| 33 |
("foobarbaz", {}, {"only_if_registerable": False}, "foobarbaz", "foobarbaz"), |
| 34 |
("foobarbaz", {"keep_unknown_suffix": False}, {}, "", ""), |
| 35 |
("foo.foobarbaz", {}, {}, "foobarbaz", "foo.foobarbaz"), |
| 36 |
("foo.foobarbaz", {"keep_unknown_suffix": False}, {}, "", ""), |
| 37 |
("-a.com", {}, {}, "", ""), # invalid domain name (leading dash in label) |
| 38 |
("a-.com", {}, {}, "", ""), # invalid domain name (trailing dash in label) |
| 39 |
( |
| 40 |
"-.com", |
| 41 |
{}, |
| 42 |
{}, |
| 43 |
"", |
| 44 |
"", |
| 45 |
), # invalid domain name (leading and trailing dash in label) |
| 46 |
(".com", {}, {}, "", ""), # invalid domain name (empty label) |
| 47 |
( |
| 48 |
"test.cloudfront.net", |
| 49 |
{}, |
| 50 |
{}, |
| 51 |
"cloudfront.net", |
| 52 |
"test.cloudfront.net", |
| 53 |
), # private rule |
| 54 |
("test.cloudfront.net", {"icann_only": True}, {}, "net", "cloudfront.net"), |
| 55 |
] |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.parametrize( |
| 59 |
"domain, kwargs, reg_extra_kwargs, suffix, reg_domain", TEST_GET_SUFFIX |
| 60 |
) |
| 61 |
def test_get_suffix( |
| 62 |
domain: str, |
| 63 |
kwargs: dict[str, t.Any], |
| 64 |
reg_extra_kwargs: dict[str, t.Any], |
| 65 |
suffix: str, |
| 66 |
reg_domain: str, |
| 67 |
) -> None: |
| 68 |
assert PUBLIC_SUFFIX_LIST.get_suffix(domain, **kwargs) == suffix |
| 69 |
kwargs.update(reg_extra_kwargs) |
| 70 |
assert PUBLIC_SUFFIX_LIST.get_registrable_domain(domain, **kwargs) == reg_domain |
| 71 |
|
| 72 |
|
| 73 |
# ------------------------------------------------------------------------------------------------- |
| 74 |
# The following list is taken from https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt |
| 75 |
# Any copyright for this list is dedicated to the Public Domain. (https://creativecommons.org/publicdomain/zero/1.0/) |
| 76 |
# This list has been provided by Rob Stradling of Comodo (see last section on https://publicsuffix.org/list/). |
| 77 |
TEST_SUFFIX_OFFICIAL_TESTS = [ |
| 78 |
# '' input. |
| 79 |
("", "", {}), |
| 80 |
# Mixed case. |
| 81 |
("COM", "", {}), |
| 82 |
("example.COM", "example.com", {"normalize_result": True}), |
| 83 |
("WwW.example.COM", "example.com", {"normalize_result": True}), |
| 84 |
("example.COM", "example.COM", {}), |
| 85 |
("WwW.example.COM", "example.COM", {}), |
| 86 |
# Leading dot. |
| 87 |
(".com", "", {}), |
| 88 |
(".example", "", {}), |
| 89 |
(".example.com", "", {}), |
| 90 |
(".example.example", "", {}), |
| 91 |
# Unlisted TLD. |
| 92 |
("example", "", {}), |
| 93 |
("example.example", "example.example", {}), |
| 94 |
("b.example.example", "example.example", {}), |
| 95 |
("a.b.example.example", "example.example", {}), |
| 96 |
# Listed, but non-Internet, TLD. |
| 97 |
# ('local', '', {}), |
| 98 |
# ('example.local', '', {}), |
| 99 |
# ('b.example.local', '', {}), |
| 100 |
# ('a.b.example.local', '', {}), |
| 101 |
# TLD with only 1 rule. |
| 102 |
("biz", "", {}), |
| 103 |
("domain.biz", "domain.biz", {}), |
| 104 |
("b.domain.biz", "domain.biz", {}), |
| 105 |
("a.b.domain.biz", "domain.biz", {}), |
| 106 |
# TLD with some 2-level rules. |
| 107 |
("com", "", {}), |
| 108 |
("example.com", "example.com", {}), |
| 109 |
("b.example.com", "example.com", {}), |
| 110 |
("a.b.example.com", "example.com", {}), |
| 111 |
("uk.com", "", {}), |
| 112 |
("example.uk.com", "example.uk.com", {}), |
| 113 |
("b.example.uk.com", "example.uk.com", {}), |
| 114 |
("a.b.example.uk.com", "example.uk.com", {}), |
| 115 |
("test.ac", "test.ac", {}), |
| 116 |
# TLD with only 1 (wildcard) rule. |
| 117 |
("mm", "", {}), |
| 118 |
("c.mm", "", {}), |
| 119 |
("b.c.mm", "b.c.mm", {}), |
| 120 |
("a.b.c.mm", "b.c.mm", {}), |
| 121 |
# More complex TLD. |
| 122 |
("jp", "", {}), |
| 123 |
("test.jp", "test.jp", {}), |
| 124 |
("www.test.jp", "test.jp", {}), |
| 125 |
("ac.jp", "", {}), |
| 126 |
("test.ac.jp", "test.ac.jp", {}), |
| 127 |
("www.test.ac.jp", "test.ac.jp", {}), |
| 128 |
("kyoto.jp", "", {}), |
| 129 |
("test.kyoto.jp", "test.kyoto.jp", {}), |
| 130 |
("ide.kyoto.jp", "", {}), |
| 131 |
("b.ide.kyoto.jp", "b.ide.kyoto.jp", {}), |
| 132 |
("a.b.ide.kyoto.jp", "b.ide.kyoto.jp", {}), |
| 133 |
("c.kobe.jp", "", {}), |
| 134 |
("b.c.kobe.jp", "b.c.kobe.jp", {}), |
| 135 |
("a.b.c.kobe.jp", "b.c.kobe.jp", {}), |
| 136 |
("city.kobe.jp", "city.kobe.jp", {}), |
| 137 |
("www.city.kobe.jp", "city.kobe.jp", {}), |
| 138 |
# TLD with a wildcard rule and exceptions. |
| 139 |
("ck", "", {}), |
| 140 |
("test.ck", "", {}), |
| 141 |
("b.test.ck", "b.test.ck", {}), |
| 142 |
("a.b.test.ck", "b.test.ck", {}), |
| 143 |
("www.ck", "www.ck", {}), |
| 144 |
("www.www.ck", "www.ck", {}), |
| 145 |
# US K12. |
| 146 |
("us", "", {}), |
| 147 |
("test.us", "test.us", {}), |
| 148 |
("www.test.us", "test.us", {}), |
| 149 |
("ak.us", "", {}), |
| 150 |
("test.ak.us", "test.ak.us", {}), |
| 151 |
("www.test.ak.us", "test.ak.us", {}), |
| 152 |
("k12.ak.us", "", {}), |
| 153 |
("test.k12.ak.us", "test.k12.ak.us", {}), |
| 154 |
("www.test.k12.ak.us", "test.k12.ak.us", {}), |
| 155 |
# IDN labels. |
| 156 |
("食狮.com.cn", "食狮.com.cn", {}), |
| 157 |
("食狮.公司.cn", "食狮.公司.cn", {}), |
| 158 |
("www.食狮.公司.cn", "食狮.公司.cn", {}), |
| 159 |
("shishi.公司.cn", "shishi.公司.cn", {}), |
| 160 |
("公司.cn", "", {}), |
| 161 |
("食狮.中国", "食狮.中国", {}), |
| 162 |
("www.食狮.中国", "食狮.中国", {}), |
| 163 |
("shishi.中国", "shishi.中国", {}), |
| 164 |
("中国", "", {}), |
| 165 |
# Same as above, but punycoded. (TODO: punycode not supported yet!) |
| 166 |
("xn--85x722f.com.cn", "xn--85x722f.com.cn", {}), |
| 167 |
("xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn", {}), |
| 168 |
("www.xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn", {}), |
| 169 |
("shishi.xn--55qx5d.cn", "shishi.xn--55qx5d.cn", {}), |
| 170 |
("xn--55qx5d.cn", "", {}), |
| 171 |
("xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s", {}), |
| 172 |
("www.xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s", {}), |
| 173 |
("shishi.xn--fiqs8s", "shishi.xn--fiqs8s", {}), |
| 174 |
("xn--fiqs8s", "", {}), |
| 175 |
] |
| 176 |
# End of public domain test data |
| 177 |
# ------------------------------------------------------------------------------------------------- |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize( |
| 181 |
"domain, registrable_domain, kwargs", TEST_SUFFIX_OFFICIAL_TESTS |
| 182 |
) |
| 183 |
def test_get_suffix_official( |
| 184 |
domain: str, registrable_domain: str, kwargs: dict[str, t.Any] |
| 185 |
) -> None: |
| 186 |
reg_domain = PUBLIC_SUFFIX_LIST.get_registrable_domain(domain, **kwargs) |
| 187 |
assert reg_domain == registrable_domain |
| 188 |
|
| 189 |
|
| 190 |
def test_load_psl_dot(tmpdir) -> None: |
| 191 |
fn = tmpdir / "psl.dat" |
| 192 |
fn.write("""// ===BEGIN BLA BLA DOMAINS=== |
| 193 |
.com. |
| 194 |
// ===END BLA BLA DOMAINS===""".encode("utf-8")) |
| 195 |
psl = PublicSuffixList.load(str(fn)) |
| 196 |
assert len(psl._rules) == 1 |
| 197 |
rule = psl._rules[0] |
| 198 |
assert rule.labels == ("com",) |
| 199 |
assert rule.exception_rule is False |
| 200 |
assert rule.part == "bla bla" |
| 201 |
|
| 202 |
|
| 203 |
def test_load_psl_no_part(tmpdir) -> None: |
| 204 |
fn = tmpdir / "psl.dat" |
| 205 |
fn.write("""// ===BEGIN BLA BLA DOMAINS=== |
| 206 |
com |
| 207 |
// ===END BLA BLA DOMAINS=== |
| 208 |
net""".encode("utf-8")) |
| 209 |
with pytest.raises(Exception) as excinfo: |
| 210 |
PublicSuffixList.load(str(fn)) |
| 211 |
assert str(excinfo.value) == "Internal error: found PSL entry with no part!" |