1package publicsuffix
2
3import (
4 "bytes"
5 "context"
6 "strings"
7 "testing"
8
9 "github.com/mjl-/mox/dns"
10 "github.com/mjl-/mox/mlog"
11)
12
13func TestList(t *testing.T) {
14 const data = `
15// ===BEGIN ICANN DOMAINS===
16com
17
18*.jp
19// Hosts in .hokkaido.jp can't set cookies below level 4...
20*.hokkaido.jp
21*.tokyo.jp
22// ...except hosts in pref.hokkaido.jp, which can set cookies at level 3.
23!pref.hokkaido.jp
24!metro.tokyo.jp
25
26bücher.example.com
27// ===END ICANN DOMAINS===
28
29ignored.example.com
30`
31
32 log := mlog.New("publicsuffix", nil)
33
34 l, err := ParseList(log.Logger, strings.NewReader(data))
35 if err != nil {
36 t.Fatalf("parsing list: %s", err)
37 }
38
39 test := func(domain, orgDomain string) {
40 t.Helper()
41
42 d, err := dns.ParseDomain(domain)
43 if err != nil {
44 t.Fatalf("idna to unicode %q: %s", domain, err)
45 }
46 od, err := dns.ParseDomain(orgDomain)
47 if err != nil {
48 t.Fatalf("idna to unicode org domain %q: %s", orgDomain, err)
49 }
50
51 r := l.Lookup(context.Background(), log.Logger, d)
52 if r != od {
53 t.Fatalf("got %q, expected %q, for domain %q", r, orgDomain, domain)
54 }
55 }
56
57 test("com", "com")
58 test("foo.com", "foo.com")
59 test("bar.foo.com", "foo.com")
60 test("foo.bar.jp", "foo.bar.jp")
61 test("baz.foo.bar.jp", "foo.bar.jp")
62 test("bar.jp", "bar.jp")
63 test("foo.bar.hokkaido.jp", "foo.bar.hokkaido.jp")
64 test("baz.foo.bar.hokkaido.jp", "foo.bar.hokkaido.jp")
65 test("bar.hokkaido.jp", "bar.hokkaido.jp")
66 test("pref.hokkaido.jp", "pref.hokkaido.jp")
67 test("foo.pref.hokkaido.jp", "pref.hokkaido.jp")
68 test("WwW.EXAMPLE.Com", "example.com")
69 test("bücher.example.com", "bücher.example.com")
70 test("foo.bücher.example.com", "foo.bücher.example.com")
71 test("bar.foo.bücher.example.com", "foo.bücher.example.com")
72 test("xn--bcher-kva.example.com", "bücher.example.com")
73 test("foo.xn--bcher-kva.example.com", "foo.bücher.example.com")
74 test("bar.foo.xn--bcher-kva.example.com", "foo.bücher.example.com")
75 test("x.ignored.example.com", "example.com")
76
77 l, err = ParseList(log.Logger, bytes.NewReader(publicsuffixData))
78 if err != nil {
79 t.Fatalf("parsing public suffix list: %s", err)
80 }
81
82 // todo: add testcases from https://raw.githubusercontent.com/publicsuffix/list/master/tests/test_psl.txt
83}
84