Debian Codegraph @

internal/export/idna/idna_test.go

raw
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package idna
6
7 import (
8 "encoding/hex"
9 "fmt"
10 "regexp"
11 "strconv"
12 "strings"
13 "testing"
14
15 "golang.org/x/text/internal/testtext"
16 )
17
18 func TestAllocToUnicode(t *testing.T) {
19 avg := testtext.AllocsPerRun(1000, func() {
20 ToUnicode("www.golang.org")
21 })
22 if avg > 0 {
23 t.Errorf("got %f; want 0", avg)
24 }
25 }
26
27 func TestAllocToASCII(t *testing.T) {
28 avg := testtext.AllocsPerRun(1000, func() {
29 ToASCII("www.golang.org")
30 })
31 if avg > 0 {
32 t.Errorf("got %f; want 0", avg)
33 }
34 }
35
36 func TestProfiles(t *testing.T) {
37 testCases := []struct {
38 name string
39 want, got *Profile
40 }{
41 {"Punycode", punycode, New()},
42 {"Registration", registration, New(ValidateForRegistration())},
43 {"Registration", registration, New(
44 ValidateForRegistration(),
45 VerifyDNSLength(true),
46 BidiRule(),
47 )},
48 {"Lookup", lookup, New(MapForLookup(), BidiRule(), Transitional(transitionalLookup))},
49 {"Display", display, New(MapForLookup(), BidiRule())},
50 }
51 for _, tc := range testCases {
52 // Functions are not comparable, but the printed version will include
53 // their pointers.
54 got := fmt.Sprintf("%#v", tc.got)
55 want := fmt.Sprintf("%#v", tc.want)
56 if got != want {
57 t.Errorf("%s: \ngot %#v,\nwant %#v", tc.name, got, want)
58 }
59 }
60 }
61
62 // doTest performs a single test f(input) and verifies that the output matches
63 // out and that the returned error is expected. The errors string contains
64 // all allowed error codes as categorized in
65 // https://www.unicode.org/Public/idna/9.0.0/IdnaTest.txt:
66 // P: Processing
67 // V: Validity
68 // A: to ASCII
69 // B: Bidi
70 // C: Context J
71 func doTest(t *testing.T, f func(string) (string, error), name, input, want, errors string) {
72 errors = strings.Trim(errors, "[]")
73 test := "ok"
74 if errors != "" {
75 test = "err:" + errors
76 }
77 // Replace some of the escape sequences to make it easier to single out
78 // tests on the command name.
79 in := strings.Trim(strconv.QuoteToASCII(input), `"`)
80 in = strings.Replace(in, `\u`, "#", -1)
81 in = strings.Replace(in, `\U`, "#", -1)
82 name = fmt.Sprintf("%s/%s/%s", name, in, test)
83
84 t.Run(name, func(t *testing.T) {
85 got, err := f(input)
86
87 if err != nil {
88 code := err.(interface {
89 code() string
90 }).code()
91 if strings.Index(errors, code) == -1 {
92 t.Errorf("error %q not in set of expected errors {%v}", code, errors)
93 }
94 } else if errors != "" {
95 t.Errorf("got %+q, no errors; want error in {%v}", got, errors)
96 }
97
98 if want != "" && got != want {
99 t.Errorf(`input=%+q string: got %+q; want %+q`, input, got, want)
100 }
101 })
102 }
103
104 var unescapeRE = regexp.MustCompile(`\\u([0-9a-zA-Z]{4})`)
105
106 func unescape(s string) string {
107 return unescapeRE.ReplaceAllStringFunc(s, func(v string) string {
108 var d [2]byte
109 hex.Decode(d[:], []byte(v[2:]))
110 return string(rune(d[0])<<8 | rune(d[1]))
111 })
112 }
113
114 func BenchmarkProfile(b *testing.B) {
115 for i := 0; i < b.N; i++ {
116 Lookup.ToASCII("www.yahoogle.com")
117 }
118 }
119
120 // TestLabelErrors tests strings returned in case of error. All results should
121 // be identical to the reference implementation and can be verified at
122 // https://unicode.org/cldr/utility/idna.jsp. The reference implementation,
123 // however, seems to not display Bidi and ContextJ errors.
124 //
125 // In some cases the behavior of browsers is added as a comment. In all cases,
126 // whenever a resolve search returns an error here, Chrome will treat the input
127 // string as a search string (including those for Bidi and Context J errors),
128 // unless noted otherwise.
129 func TestLabelErrors(t *testing.T) {
130 encode := func(s string) string { s, _ = encode(acePrefix, s); return s }
131 type kind struct {
132 name string
133 f func(string) (string, error)
134 }
135 punyA := kind{"PunycodeA", punycode.ToASCII}
136 resolve := kind{"ResolveA", Lookup.ToASCII}
137 display := kind{"ToUnicode", Display.ToUnicode}
138 p := New(VerifyDNSLength(true), MapForLookup(), BidiRule())
139 lengthU := kind{"CheckLengthU", p.ToUnicode}
140 lengthA := kind{"CheckLengthA", p.ToASCII}
141 p = New(MapForLookup(), StrictDomainName(false))
142 std3 := kind{"STD3", p.ToASCII}
143 p = New(MapForLookup(), CheckHyphens(false))
144 hyphens := kind{"CheckHyphens", p.ToASCII}
145 p = New(MapForLookup(), Transitional(true))
146 transitional := kind{"Transitional", p.ToASCII}
147 p = New(MapForLookup(), Transitional(false))
148 nontransitional := kind{"Nontransitional", p.ToASCII}
149
150 testCases := []struct {
151 kind
152 input string
153 want string
154 wantErr string
155 }{
156 {lengthU, "", "", code16("A4", "X4_2")}, // From UTS 46 conformance test.
157 {lengthA, "", "", "A4"},
158
159 {lengthU, "xn--", "", code16("A4", "X4_2")},
160 {lengthU, "foo.xn--", "foo.", code16("A4", "X4_2")}, // TODO: is dropping xn-- correct?
161 {lengthU, "xn--.foo", ".foo", code16("A4", "X4_2")},
162 {lengthU, "foo.xn--.bar", "foo..bar", code16("A4", "X4_2")},
163
164 {display, "xn--", "", ""},
165 {display, "foo.xn--", "foo.", ""}, // TODO: is dropping xn-- correct?
166 {display, "xn--.foo", ".foo", ""},
167 {display, "foo.xn--.bar", "foo..bar", ""},
168
169 {lengthA, "a..b", "a..b", "A4"},
170 {punyA, ".b", ".b", ""},
171 // For backwards compatibility, the Punycode profile does not map runes.
172 {punyA, "\u3002b", "xn--b-83t", ""},
173 {punyA, "..b", "..b", ""},
174
175 {lengthA, ".b", ".b", "A4"},
176 {lengthA, "\u3002b", ".b", "A4"},
177 {lengthA, "..b", "..b", "A4"},
178 {lengthA, "b..", "b..", code16("", "A4")},
179
180 // Sharpened Bidi rules for Unicode 10.0.0. Apply for ALL labels in ANY
181 // of the labels is RTL.
182 {lengthA, "\ufe05\u3002\u3002\U0002603e\u1ce0", "..xn--t6f5138v", "A4"},
183 {lengthA, "FAX\u2a77\U0001d186\u3002\U0001e942\U000e0181\u180c", "", "B6"},
184
185 {resolve, "a..b", "a..b", ""},
186 // Note that leading dots are not stripped. This is to be consistent
187 // with the Punycode profile as well as the conformance test.
188 {resolve, ".b", ".b", ""},
189 {resolve, "\u3002b", ".b", ""},
190 {resolve, "..b", "..b", ""},
191 {resolve, "b..", "b..", ""},
192 {resolve, "\xed", "", "P1"},
193
194 // Raw punycode
195 {punyA, "", "", ""},
196 {punyA, "*.foo.com", "*.foo.com", ""},
197 {punyA, "Foo.com", "Foo.com", ""},
198
199 // STD3 rules
200 {display, "*.foo.com", "*.foo.com", code16("P1", "U1")},
201 {std3, "*.foo.com", "*.foo.com", ""},
202
203 // Hyphens
204 {display, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "V2"},
205 {hyphens, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", ""},
206 {display, "-label-.com", "-label-.com", "V3"},
207 {hyphens, "-label-.com", "-label-.com", ""},
208
209 // Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of
210 // Chrome, modern Firefox, Safari, and IE.
211 {resolve, "lab⒐be", "xn--labbe-zh9b", code16("P1", "V7")}, // encode("lab⒐be")
212 {display, "lab⒐be", "lab⒐be", code16("P1", "V7")},
213 {transitional, "plan⒐faß.de", "xn--planfass-c31e.de", code16("P1", "V7")}, // encode("plan⒐fass") + ".de"
214 {display, "Plan⒐faß.de", "plan⒐faß.de", code16("P1", "V7")},
215
216 // Transitional vs Nontransitional processing
217 {transitional, "Plan9faß.de", "plan9fass.de", ""},
218 {nontransitional, "Plan9faß.de", "xn--plan9fa-6va.de", ""},
219
220 // Chrome 54.0 recognizes the error and treats this input verbatim as a
221 // search string.
222 // Safari 10.0 (non-conform spec) decomposes "⒈" and computes the
223 // punycode on the result using transitional mapping.
224 // Firefox 49.0.1 goes haywire on this string and prints a bunch of what
225 // seems to be nested punycode encodings.
226 {transitional, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", code16("P1", "V7")},
227 {display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", code16("P1", "V7")},
228
229 {transitional, "a\u200Cb", "ab", ""},
230 {display, "a\u200Cb", "a\u200Cb", "C"},
231
232 {resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"},
233 {display, "a\u200Cb", "a\u200Cb", "C"},
234
235 {resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"},
236 {
237 // Notice how the string gets transformed, even with an error.
238 // Chrome will use the original string if it finds an error, so not
239 // the transformed one.
240 display,
241 "gr\ufecb\ufeae\ufe91\ufef2.de",
242 "gr\u0639\u0631\u0628\u064a.de",
243 "B",
244 },
245
246 {resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"}, // ٱ.σߜ
247 {display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"},
248
249 // normalize input
250 {resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢
251 {display, "a\u0323\u0322", "\u1ea1\u0322", ""},
252
253 // Non-normalized strings are not normalized when they originate from
254 // punycode. Despite the error, Chrome, Safari and Firefox will attempt
255 // to look up the input punycode.
256 {resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"},
257 {display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"},
258 }
259
260 for _, tc := range testCases {
261 doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr)
262 }
263 }
264
265 func TestTransitionalDefault(t *testing.T) {
266 want := "xn--strae-oqa.de"
267 if transitionalLookup {
268 want = "strasse.de"
269 }
270 doTest(t, Lookup.ToASCII, "Lookup", "straße.de", want, "")
271 }