1
2
3
4
5
6
7 package idna
8
9 import "testing"
10
11
12
13
14
15
16
17
18
19
20 func TestLabelErrors(t *testing.T) {
21 encode := func(s string) string { s, _ = encode(acePrefix, s); return s }
22 type kind struct {
23 name string
24 f func(string) (string, error)
25 }
26 punyA := kind{"PunycodeA", punycode.ToASCII}
27 resolve := kind{"ResolveA", Lookup.ToASCII}
28 display := kind{"ToUnicode", Display.ToUnicode}
29 p := New(VerifyDNSLength(true), MapForLookup(), BidiRule())
30 lengthU := kind{"CheckLengthU", p.ToUnicode}
31 lengthA := kind{"CheckLengthA", p.ToASCII}
32 p = New(MapForLookup(), StrictDomainName(false))
33 std3 := kind{"STD3", p.ToASCII}
34 p = New(MapForLookup(), CheckHyphens(false))
35 hyphens := kind{"CheckHyphens", p.ToASCII}
36 p = New(MapForLookup(), Transitional(true))
37 transitional := kind{"Transitional", p.ToASCII}
38 p = New(MapForLookup(), Transitional(false))
39 nontransitional := kind{"Nontransitional", p.ToASCII}
40
41 testCases := []struct {
42 kind
43 input string
44 want string
45 wantErr string
46 }{
47 {lengthU, "", "", "A4"},
48 {lengthA, "", "", "A4"},
49
50 {lengthU, "xn--", "", "A4"},
51 {lengthU, "foo.xn--", "foo.", "A4"},
52 {lengthU, "xn--.foo", ".foo", "A4"},
53 {lengthU, "foo.xn--.bar", "foo..bar", "A4"},
54
55 {display, "xn--", "", ""},
56 {display, "foo.xn--", "foo.", ""},
57 {display, "xn--.foo", ".foo", ""},
58 {display, "foo.xn--.bar", "foo..bar", ""},
59
60 {lengthA, "a..b", "a..b", "A4"},
61 {punyA, ".b", ".b", ""},
62
63 {punyA, "\u3002b", "xn--b-83t", ""},
64 {punyA, "..b", "..b", ""},
65
66 {lengthA, ".b", ".b", "A4"},
67 {lengthA, "\u3002b", ".b", "A4"},
68 {lengthA, "..b", "..b", "A4"},
69 {lengthA, "b..", "b..", ""},
70
71
72
73 {lengthA, "\ufe05\u3002\u3002\U0002603e\u1ce0", "..xn--t6f5138v", "A4"},
74 {lengthA, "FAX\u2a77\U0001d186\u3002\U0001e942\U000e0181\u180c", "", "B6"},
75
76 {resolve, "a..b", "a..b", ""},
77
78
79 {resolve, ".b", ".b", ""},
80 {resolve, "\u3002b", ".b", ""},
81 {resolve, "..b", "..b", ""},
82 {resolve, "b..", "b..", ""},
83 {resolve, "\xed", "", "P1"},
84
85
86 {punyA, "", "", ""},
87 {punyA, "*.foo.com", "*.foo.com", ""},
88 {punyA, "Foo.com", "Foo.com", ""},
89
90
91 {display, "*.foo.com", "*.foo.com", "P1"},
92 {std3, "*.foo.com", "*.foo.com", ""},
93
94
95 {display, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "V2"},
96 {hyphens, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", ""},
97 {display, "-label-.com", "-label-.com", "V3"},
98 {hyphens, "-label-.com", "-label-.com", ""},
99
100
101
102 {resolve, "lab⒐be", "xn--labbe-zh9b", "P1"},
103 {display, "lab⒐be", "lab⒐be", "P1"},
104 {transitional, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"},
105 {display, "Plan⒐faß.de", "plan⒐faß.de", "P1"},
106
107
108 {transitional, "Plan9faß.de", "plan9fass.de", ""},
109 {nontransitional, "Plan9faß.de", "xn--plan9fa-6va.de", ""},
110
111
112
113
114
115
116
117 {transitional, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"},
118 {display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"},
119
120 {transitional, "a\u200Cb", "ab", ""},
121 {display, "a\u200Cb", "a\u200Cb", "C"},
122
123 {resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"},
124 {display, "a\u200Cb", "a\u200Cb", "C"},
125
126 {resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"},
127 {
128
129
130
131 display,
132 "gr\ufecb\ufeae\ufe91\ufef2.de",
133 "gr\u0639\u0631\u0628\u064a.de",
134 "B",
135 },
136
137 {resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"},
138 {display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"},
139
140
141 {resolve, "a\u0323\u0322", "xn--jta191l", ""},
142 {display, "a\u0323\u0322", "\u1ea1\u0322", ""},
143
144
145
146
147 {resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"},
148 {display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"},
149 }
150
151 for _, tc := range testCases {
152 doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr)
153 }
154 }
155
156 func TestTransitionalDefault(t *testing.T) {
157 want := "xn--strae-oqa.de"
158 if transitionalLookup {
159 want = "strasse.de"
160 }
161 doTest(t, Lookup.ToASCII, "Lookup", "straße.de", want, "")
162 }
163
View as plain text