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// +build !go1.10 6 7package idna 8 9import "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. 20func 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 37 testCases := []struct { 38 kind 39 input string 40 want string 41 wantErr string 42 }{ 43 {lengthU, "", "", "A4"}, // From UTS 46 conformance test. 44 {lengthA, "", "", "A4"}, 45 46 {lengthU, "xn--", "", "A4"}, 47 {lengthU, "foo.xn--", "foo.", "A4"}, // TODO: is dropping xn-- correct? 48 {lengthU, "xn--.foo", ".foo", "A4"}, 49 {lengthU, "foo.xn--.bar", "foo..bar", "A4"}, 50 51 {display, "xn--", "", ""}, 52 {display, "foo.xn--", "foo.", ""}, // TODO: is dropping xn-- correct? 53 {display, "xn--.foo", ".foo", ""}, 54 {display, "foo.xn--.bar", "foo..bar", ""}, 55 56 {lengthA, "a..b", "a..b", "A4"}, 57 {punyA, ".b", ".b", ""}, 58 // For backwards compatibility, the Punycode profile does not map runes. 59 {punyA, "\u3002b", "xn--b-83t", ""}, 60 {punyA, "..b", "..b", ""}, 61 // Only strip leading empty labels for certain profiles. Stripping 62 // leading empty labels here but not for "empty" punycode above seems 63 // inconsistent, but seems to be applied by both the conformance test 64 // and Chrome. So we turn it off by default, support it as an option, 65 // and enable it in profiles where it seems commonplace. 66 {lengthA, ".b", "b", ""}, 67 {lengthA, "\u3002b", "b", ""}, 68 {lengthA, "..b", "b", ""}, 69 {lengthA, "b..", "b..", ""}, 70 71 {resolve, "a..b", "a..b", ""}, 72 {resolve, ".b", "b", ""}, 73 {resolve, "\u3002b", "b", ""}, 74 {resolve, "..b", "b", ""}, 75 {resolve, "b..", "b..", ""}, 76 77 // Raw punycode 78 {punyA, "", "", ""}, 79 {punyA, "*.foo.com", "*.foo.com", ""}, 80 {punyA, "Foo.com", "Foo.com", ""}, 81 82 // STD3 rules 83 {display, "*.foo.com", "*.foo.com", "P1"}, 84 {std3, "*.foo.com", "*.foo.com", ""}, 85 86 // Hyphens 87 {display, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "V2"}, 88 {hyphens, "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", "r3---sn-apo3qvuoxuxbt-j5pe.googlevideo.com", ""}, 89 {display, "-label-.com", "-label-.com", "V3"}, 90 {hyphens, "-label-.com", "-label-.com", ""}, 91 92 // Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of 93 // Chrome, Safari, and IE. Firefox will first map ⒐ to 9. and return 94 // lab9.be. 95 {resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be") 96 {display, "lab⒐be", "lab⒐be", "P1"}, 97 98 {resolve, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de" 99 {display, "Plan⒐faß.de", "plan⒐faß.de", "P1"}, 100 101 // Chrome 54.0 recognizes the error and treats this input verbatim as a 102 // search string. 103 // Safari 10.0 (non-conform spec) decomposes "⒈" and computes the 104 // punycode on the result using transitional mapping. 105 // Firefox 49.0.1 goes haywire on this string and prints a bunch of what 106 // seems to be nested punycode encodings. 107 {resolve, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"}, 108 {display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"}, 109 110 {resolve, "a\u200Cb", "ab", ""}, 111 {display, "a\u200Cb", "a\u200Cb", "C"}, 112 113 {resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"}, 114 {display, "a\u200Cb", "a\u200Cb", "C"}, 115 116 {resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"}, 117 { 118 // Notice how the string gets transformed, even with an error. 119 // Chrome will use the original string if it finds an error, so not 120 // the transformed one. 121 display, 122 "gr\ufecb\ufeae\ufe91\ufef2.de", 123 "gr\u0639\u0631\u0628\u064a.de", 124 "B", 125 }, 126 127 {resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"}, // ٱ.σߜ 128 {display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"}, 129 130 // normalize input 131 {resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢ 132 {display, "a\u0323\u0322", "\u1ea1\u0322", ""}, 133 134 // Non-normalized strings are not normalized when they originate from 135 // punycode. Despite the error, Chrome, Safari and Firefox will attempt 136 // to look up the input punycode. 137 {resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"}, 138 {display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"}, 139 } 140 141 for _, tc := range testCases { 142 doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr) 143 } 144} 145