github.com/go-enjin/golang-org-x-text@v0.12.1-enjin.2/internal/export/idna/idna9.0.0_test.go (about) 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 //go:build !go1.10 6 // +build !go1.10 7 8 package idna 9 10 import "testing" 11 12 // TestLabelErrors tests strings returned in case of error. All results should 13 // be identical to the reference implementation and can be verified at 14 // https://unicode.org/cldr/utility/idna.jsp. The reference implementation, 15 // however, seems to not display Bidi and ContextJ errors. 16 // 17 // In some cases the behavior of browsers is added as a comment. In all cases, 18 // whenever a resolve search returns an error here, Chrome will treat the input 19 // string as a search string (including those for Bidi and Context J errors), 20 // unless noted otherwise. 21 func TestLabelErrors(t *testing.T) { 22 encode := func(s string) string { s, _ = encode(acePrefix, s); return s } 23 type kind struct { 24 name string 25 f func(string) (string, error) 26 } 27 punyA := kind{"PunycodeA", punycode.ToASCII} 28 resolve := kind{"ResolveA", Lookup.ToASCII} 29 display := kind{"ToUnicode", Display.ToUnicode} 30 p := New(VerifyDNSLength(true), MapForLookup(), BidiRule()) 31 lengthU := kind{"CheckLengthU", p.ToUnicode} 32 lengthA := kind{"CheckLengthA", p.ToASCII} 33 p = New(MapForLookup(), StrictDomainName(false)) 34 std3 := kind{"STD3", p.ToASCII} 35 p = New(MapForLookup(), CheckHyphens(false)) 36 hyphens := kind{"CheckHyphens", p.ToASCII} 37 p = New(MapForLookup(), Transitional(true)) 38 transitional := kind{"Transitional", p.ToASCII} 39 p = New(MapForLookup(), Transitional(false)) 40 nontransitional := kind{"Nontransitional", p.ToASCII} 41 42 testCases := []struct { 43 kind 44 input string 45 want string 46 wantErr string 47 }{ 48 {lengthU, "", "", "A4"}, // From UTS 46 conformance test. 49 {lengthA, "", "", "A4"}, 50 51 {lengthU, "xn--", "", "A4"}, 52 {lengthU, "foo.xn--", "foo.", "A4"}, // TODO: is dropping xn-- correct? 53 {lengthU, "xn--.foo", ".foo", "A4"}, 54 {lengthU, "foo.xn--.bar", "foo..bar", "A4"}, 55 56 {display, "xn--", "", ""}, 57 {display, "foo.xn--", "foo.", ""}, // TODO: is dropping xn-- correct? 58 {display, "xn--.foo", ".foo", ""}, 59 {display, "foo.xn--.bar", "foo..bar", ""}, 60 61 {lengthA, "a..b", "a..b", "A4"}, 62 {punyA, ".b", ".b", ""}, 63 // For backwards compatibility, the Punycode profile does not map runes. 64 {punyA, "\u3002b", "xn--b-83t", ""}, 65 {punyA, "..b", "..b", ""}, 66 // Only strip leading empty labels for certain profiles. Stripping 67 // leading empty labels here but not for "empty" punycode above seems 68 // inconsistent, but seems to be applied by both the conformance test 69 // and Chrome. So we turn it off by default, support it as an option, 70 // and enable it in profiles where it seems commonplace. 71 {lengthA, ".b", "b", ""}, 72 {lengthA, "\u3002b", "b", ""}, 73 {lengthA, "..b", "b", ""}, 74 {lengthA, "b..", "b..", ""}, 75 76 {resolve, "a..b", "a..b", ""}, 77 {resolve, ".b", "b", ""}, 78 {resolve, "\u3002b", "b", ""}, 79 {resolve, "..b", "b", ""}, 80 {resolve, "b..", "b..", ""}, 81 {resolve, "\xed", "", "P1"}, 82 83 // Raw punycode 84 {punyA, "", "", ""}, 85 {punyA, "*.foo.com", "*.foo.com", ""}, 86 {punyA, "Foo.com", "Foo.com", ""}, 87 88 // STD3 rules 89 {display, "*.foo.com", "*.foo.com", "P1"}, 90 {std3, "*.foo.com", "*.foo.com", ""}, 91 92 // Hyphens 93 {display, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "V2"}, 94 {hyphens, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", ""}, 95 {display, "-label-.com", "-label-.com", "V3"}, 96 {hyphens, "-label-.com", "-label-.com", ""}, 97 98 // Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of 99 // Chrome, modern Firefox, Safari, and IE. 100 {resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be") 101 {display, "lab⒐be", "lab⒐be", "P1"}, 102 {resolve, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de" 103 {display, "Plan⒐faß.de", "plan⒐faß.de", "P1"}, 104 105 // Transitional vs Nontransitional processing 106 {transitional, "Plan9faß.de", "plan9fass.de", ""}, 107 {nontransitional, "Plan9faß.de", "xn--plan9fa-6va.de", ""}, 108 109 // Chrome 54.0 recognizes the error and treats this input verbatim as a 110 // search string. 111 // Safari 10.0 (non-conform spec) decomposes "⒈" and computes the 112 // punycode on the result using transitional mapping. 113 // Firefox 49.0.1 goes haywire on this string and prints a bunch of what 114 // seems to be nested punycode encodings. 115 {resolve, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"}, 116 {display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"}, 117 118 {resolve, "a\u200Cb", "ab", ""}, 119 {display, "a\u200Cb", "a\u200Cb", "C"}, 120 121 {resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"}, 122 {display, "a\u200Cb", "a\u200Cb", "C"}, 123 124 {resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"}, 125 { 126 // Notice how the string gets transformed, even with an error. 127 // Chrome will use the original string if it finds an error, so not 128 // the transformed one. 129 display, 130 "gr\ufecb\ufeae\ufe91\ufef2.de", 131 "gr\u0639\u0631\u0628\u064a.de", 132 "B", 133 }, 134 135 {resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"}, // ٱ.σߜ 136 {display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"}, 137 138 // normalize input 139 {resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢ 140 {display, "a\u0323\u0322", "\u1ea1\u0322", ""}, 141 142 // Non-normalized strings are not normalized when they originate from 143 // punycode. Despite the error, Chrome, Safari and Firefox will attempt 144 // to look up the input punycode. 145 {resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"}, 146 {display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"}, 147 } 148 149 for _, tc := range testCases { 150 doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr) 151 } 152 }