golang.org/x/text@v0.14.0/internal/export/idna/idna10.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 7 package idna 8 9 import "testing" 10 11 // TestLabelErrors tests strings returned in case of error. All results should 12 // be identical to the reference implementation and can be verified at 13 // https://unicode.org/cldr/utility/idna.jsp. The reference implementation, 14 // however, seems to not display Bidi and ContextJ errors. 15 // 16 // In some cases the behavior of browsers is added as a comment. In all cases, 17 // whenever a resolve search returns an error here, Chrome will treat the input 18 // string as a search string (including those for Bidi and Context J errors), 19 // unless noted otherwise. 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"}, // From UTS 46 conformance test. 48 {lengthA, "", "", "A4"}, 49 50 {lengthU, "xn--", "", "A4"}, 51 {lengthU, "foo.xn--", "foo.", "A4"}, // TODO: is dropping xn-- correct? 52 {lengthU, "xn--.foo", ".foo", "A4"}, 53 {lengthU, "foo.xn--.bar", "foo..bar", "A4"}, 54 55 {display, "xn--", "", ""}, 56 {display, "foo.xn--", "foo.", ""}, // TODO: is dropping xn-- correct? 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 // For backwards compatibility, the Punycode profile does not map runes. 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 // Sharpened Bidi rules for Unicode 10.0.0. Apply for ALL labels in ANY 72 // of the labels is RTL. 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 // Note that leading dots are not stripped. This is to be consistent 78 // with the Punycode profile as well as the conformance test. 79 {resolve, ".b", ".b", ""}, 80 {resolve, "\u3002b", ".b", ""}, 81 {resolve, "..b", "..b", ""}, 82 {resolve, "b..", "b..", ""}, 83 {resolve, "\xed", "", "P1"}, 84 85 // Raw punycode 86 {punyA, "", "", ""}, 87 {punyA, "*.foo.com", "*.foo.com", ""}, 88 {punyA, "Foo.com", "Foo.com", ""}, 89 90 // STD3 rules 91 {display, "*.foo.com", "*.foo.com", "P1"}, 92 {std3, "*.foo.com", "*.foo.com", ""}, 93 94 // Hyphens 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 // Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of 101 // Chrome, modern Firefox, Safari, and IE. 102 {resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be") 103 {display, "lab⒐be", "lab⒐be", "P1"}, 104 {transitional, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de" 105 {display, "Plan⒐faß.de", "plan⒐faß.de", "P1"}, 106 107 // Transitional vs Nontransitional processing 108 {transitional, "Plan9faß.de", "plan9fass.de", ""}, 109 {nontransitional, "Plan9faß.de", "xn--plan9fa-6va.de", ""}, 110 111 // Chrome 54.0 recognizes the error and treats this input verbatim as a 112 // search string. 113 // Safari 10.0 (non-conform spec) decomposes "⒈" and computes the 114 // punycode on the result using transitional mapping. 115 // Firefox 49.0.1 goes haywire on this string and prints a bunch of what 116 // seems to be nested punycode encodings. 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 // Notice how the string gets transformed, even with an error. 129 // Chrome will use the original string if it finds an error, so not 130 // the transformed one. 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 // normalize input 141 {resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢ 142 {display, "a\u0323\u0322", "\u1ea1\u0322", ""}, 143 144 // Non-normalized strings are not normalized when they originate from 145 // punycode. Despite the error, Chrome, Safari and Firefox will attempt 146 // to look up the input punycode. 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 }