github.com/liquid-dev/text@v0.3.3-liquid/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  // +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  
    35  	testCases := []struct {
    36  		kind
    37  		input   string
    38  		want    string
    39  		wantErr string
    40  	}{
    41  		{lengthU, "", "", "A4"}, // From UTS 46 conformance test.
    42  		{lengthA, "", "", "A4"},
    43  
    44  		{lengthU, "xn--", "", "A4"},
    45  		{lengthU, "foo.xn--", "foo.", "A4"}, // TODO: is dropping xn-- correct?
    46  		{lengthU, "xn--.foo", ".foo", "A4"},
    47  		{lengthU, "foo.xn--.bar", "foo..bar", "A4"},
    48  
    49  		{display, "xn--", "", ""},
    50  		{display, "foo.xn--", "foo.", ""}, // TODO: is dropping xn-- correct?
    51  		{display, "xn--.foo", ".foo", ""},
    52  		{display, "foo.xn--.bar", "foo..bar", ""},
    53  
    54  		{lengthA, "a..b", "a..b", "A4"},
    55  		{punyA, ".b", ".b", ""},
    56  		// For backwards compatibility, the Punycode profile does not map runes.
    57  		{punyA, "\u3002b", "xn--b-83t", ""},
    58  		{punyA, "..b", "..b", ""},
    59  
    60  		{lengthA, ".b", ".b", "A4"},
    61  		{lengthA, "\u3002b", ".b", "A4"},
    62  		{lengthA, "..b", "..b", "A4"},
    63  		{lengthA, "b..", "b..", ""},
    64  
    65  		// Sharpened Bidi rules for Unicode 10.0.0. Apply for ALL labels in ANY
    66  		// of the labels is RTL.
    67  		{lengthA, "\ufe05\u3002\u3002\U0002603e\u1ce0", "..xn--t6f5138v", "A4"},
    68  		{lengthA, "FAX\u2a77\U0001d186\u3002\U0001e942\U000e0181\u180c", "", "B6"},
    69  
    70  		{resolve, "a..b", "a..b", ""},
    71  		// Note that leading dots are not stripped. This is to be consistent
    72  		// with the Punycode profile as well as the conformance test.
    73  		{resolve, ".b", ".b", ""},
    74  		{resolve, "\u3002b", ".b", ""},
    75  		{resolve, "..b", "..b", ""},
    76  		{resolve, "b..", "b..", ""},
    77  		{resolve, "\xed", "", "P1"},
    78  
    79  		// Raw punycode
    80  		{punyA, "", "", ""},
    81  		{punyA, "*.foo.com", "*.foo.com", ""},
    82  		{punyA, "Foo.com", "Foo.com", ""},
    83  
    84  		// STD3 rules
    85  		{display, "*.foo.com", "*.foo.com", "P1"},
    86  		{std3, "*.foo.com", "*.foo.com", ""},
    87  
    88  		// Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of
    89  		// Chrome, Safari, and IE. Firefox will first map ⒐ to 9. and return
    90  		// lab9.be.
    91  		{resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be")
    92  		{display, "lab⒐be", "lab⒐be", "P1"},
    93  
    94  		{resolve, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de"
    95  		{display, "Plan⒐faß.de", "plan⒐faß.de", "P1"},
    96  
    97  		// Chrome 54.0 recognizes the error and treats this input verbatim as a
    98  		// search string.
    99  		// Safari 10.0 (non-conform spec) decomposes "⒈" and computes the
   100  		// punycode on the result using transitional mapping.
   101  		// Firefox 49.0.1 goes haywire on this string and prints a bunch of what
   102  		// seems to be nested punycode encodings.
   103  		{resolve, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"},
   104  		{display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"},
   105  
   106  		{resolve, "a\u200Cb", "ab", ""},
   107  		{display, "a\u200Cb", "a\u200Cb", "C"},
   108  
   109  		{resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"},
   110  		{display, "a\u200Cb", "a\u200Cb", "C"},
   111  
   112  		{resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"},
   113  		{
   114  			// Notice how the string gets transformed, even with an error.
   115  			// Chrome will use the original string if it finds an error, so not
   116  			// the transformed one.
   117  			display,
   118  			"gr\ufecb\ufeae\ufe91\ufef2.de",
   119  			"gr\u0639\u0631\u0628\u064a.de",
   120  			"B",
   121  		},
   122  
   123  		{resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"}, // ٱ.σߜ
   124  		{display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"},
   125  
   126  		// normalize input
   127  		{resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢
   128  		{display, "a\u0323\u0322", "\u1ea1\u0322", ""},
   129  
   130  		// Non-normalized strings are not normalized when they originate from
   131  		// punycode. Despite the error, Chrome, Safari and Firefox will attempt
   132  		// to look up the input punycode.
   133  		{resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"},
   134  		{display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"},
   135  	}
   136  
   137  	for _, tc := range testCases {
   138  		doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr)
   139  	}
   140  }