Cotton code
89 lines · 4.0 KB · RustRaw
use super::*;#[test]fn a_host_is_read_from_a_site_or_an_address() {    assert_eq!(host_of("https://Login.Example.co.uk:8443/x?y#z").as_deref(), Some("login.example.co.uk"));    assert_eq!(host_of("example.com").as_deref(), Some("example.com"));    assert_eq!(host_of("example.com.").as_deref(), Some("example.com"));    assert_eq!(host_of("http://192.168.1.10:8080").as_deref(), Some("192.168.1.10"));    assert_eq!(host_of("https://bücher.example/").as_deref(), Some("xn--bcher-kva.example"));    assert_eq!(host_of(""), None);    assert_eq!(host_of("   "), None);}/// The public suffix list's own test vectors (publicsuffix.org/list/tests.txt), a selection.#[test]fn registrable_domains_by_the_list() {    for (host, want) in [        ("com", None),        ("example.com", Some("example.com")),        ("b.example.com", Some("example.com")),        ("a.b.example.com", Some("example.com")),        ("uk.com", None),        ("example.uk.com", Some("example.uk.com")),        ("b.example.uk.com", Some("example.uk.com")),        ("ac.jp", None),        ("test.ac.jp", Some("test.ac.jp")),        ("www.test.ac.jp", Some("test.ac.jp")),        ("kyoto.jp", None),        ("test.kyoto.jp", Some("test.kyoto.jp")),        ("ide.kyoto.jp", None),        ("b.ide.kyoto.jp", Some("b.ide.kyoto.jp")),        ("c.kobe.jp", None),        ("b.c.kobe.jp", Some("b.c.kobe.jp")),        ("city.kobe.jp", Some("city.kobe.jp")),        ("www.city.kobe.jp", Some("city.kobe.jp")),        ("ck", None),        ("test.ck", None),        ("b.test.ck", Some("b.test.ck")),        ("www.ck", Some("www.ck")),        ("www.www.ck", Some("www.ck")),        ("us", None),        ("test.us", Some("test.us")),        ("ak.us", None),        ("test.ak.us", Some("test.ak.us")),        ("k12.ak.us", None),        ("test.k12.ak.us", Some("test.k12.ak.us")),        ("xn--85x722f.com.cn", Some("xn--85x722f.com.cn")),        ("xn--55qx5d.cn", None),        ("xn--85x722f.xn--55qx5d.cn", Some("xn--85x722f.xn--55qx5d.cn")),        ("www.xn--85x722f.xn--55qx5d.cn", Some("xn--85x722f.xn--55qx5d.cn")),        ("example.co.uk", Some("example.co.uk")),        ("login.example.co.uk", Some("example.co.uk")),        ("alice.github.io", Some("alice.github.io")),        ("github.io", None),        ("unknowntld", None),        ("example.unknowntld", Some("example.unknowntld")),        ("192.168.1.10", None),    ] {        assert_eq!(registrable(host).as_deref(), want, "{host}");    }}#[test]fn a_login_fills_on_its_host_and_its_registrable_domain_never_by_substring() {    assert_eq!(site_match("example.com", "https://example.com/login"), Some(Match::Exact));    assert_eq!(site_match("example.com", "https://accounts.example.com/"), Some(Match::SameSite));    assert_eq!(site_match("https://accounts.example.com", "example.com"), Some(Match::SameSite));    assert_eq!(site_match("example.co.uk", "login.example.co.uk"), Some(Match::SameSite));    for asking in ["notexample.com", "example.com.evil.test", "examp1e.com", "example.co", "example.net", "evil-example.com"] {        assert_eq!(site_match("example.com", asking), None, "{asking}");    }    // two people's pages on one host's suffix are two sites    assert_eq!(site_match("alice.github.io", "bob.github.io"), None);    assert_eq!(site_match("co.uk", "example.co.uk"), None);    // an address or a single label matches only itself    assert_eq!(site_match("192.168.1.10", "http://192.168.1.10:8080"), Some(Match::Exact));    assert_eq!(site_match("192.168.1.10", "192.168.1.11"), None);    assert_eq!(site_match("localhost", "localhost"), Some(Match::Exact));    assert_eq!(site_match("router", "other"), None);}#[test]fn the_bundled_list_reads_whole() {    let rules = rules();    assert!(rules.normal.len() > 9_000, "{}", rules.normal.len());    assert!(rules.wildcard.contains("ck") && rules.exception.contains("www.ck"));    assert!(rules.normal.contains("github.io"), "the private section is kept");    assert!(rules.normal.iter().all(|rule| rule.is_ascii()), "every rule is read as its ASCII spelling");}