1package dns
2
3import (
4 "errors"
5 "testing"
6)
7
8func TestParseDomain(t *testing.T) {
9 test := func(lax bool, s string, exp Domain, expErr error) {
10 t.Helper()
11 var dom Domain
12 var err error
13 if lax {
14 dom, err = ParseDomainLax(s)
15 } else {
16 dom, err = ParseDomain(s)
17 }
18 if (err == nil) != (expErr == nil) || expErr != nil && !errors.Is(err, expErr) {
19 t.Fatalf("parse domain %q: err %v, expected %v", s, err, expErr)
20 }
21 if expErr == nil && dom != exp {
22 t.Fatalf("parse domain %q: got %#v, epxected %#v", s, dom, exp)
23 }
24 }
25
26 // We rely on normalization of names throughout the code base.
27 test(false, "xmox.nl", Domain{"xmox.nl", ""}, nil)
28 test(false, "XMOX.NL", Domain{"xmox.nl", ""}, nil)
29 test(false, "TEST☺.XMOX.NL", Domain{"xn--test-3o3b.xmox.nl", "test☺.xmox.nl"}, nil)
30 test(false, "TEST☺.XMOX.NL", Domain{"xn--test-3o3b.xmox.nl", "test☺.xmox.nl"}, nil)
31 test(false, "ℂᵤⓇℒ。𝐒🄴", Domain{"curl.se", ""}, nil) // https://daniel.haxx.se/blog/2022/12/14/idn-is-crazy/
32 test(false, "xmox.nl.", Domain{}, errTrailingDot)
33
34 test(false, "_underscore.xmox.nl", Domain{}, errIDNA)
35 test(true, "_underscore.xmox.NL", Domain{ASCII: "_underscore.xmox.nl"}, nil)
36 test(true, "_underscore.☺.xmox.nl", Domain{}, errUnderscore)
37 test(true, "_underscore.xn--test-3o3b.xmox.nl", Domain{}, errUnderscore)
38}
39