You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 lines
5.0 KiB

  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. // +build go1.10
  5. package idna
  6. import "testing"
  7. // TestLabelErrors tests strings returned in case of error. All results should
  8. // be identical to the reference implementation and can be verified at
  9. // https://unicode.org/cldr/utility/idna.jsp. The reference implementation,
  10. // however, seems to not display Bidi and ContextJ errors.
  11. //
  12. // In some cases the behavior of browsers is added as a comment. In all cases,
  13. // whenever a resolve search returns an error here, Chrome will treat the input
  14. // string as a search string (including those for Bidi and Context J errors),
  15. // unless noted otherwise.
  16. func TestLabelErrors(t *testing.T) {
  17. encode := func(s string) string { s, _ = encode(acePrefix, s); return s }
  18. type kind struct {
  19. name string
  20. f func(string) (string, error)
  21. }
  22. punyA := kind{"PunycodeA", punycode.ToASCII}
  23. resolve := kind{"ResolveA", Lookup.ToASCII}
  24. display := kind{"ToUnicode", Display.ToUnicode}
  25. p := New(VerifyDNSLength(true), MapForLookup(), BidiRule())
  26. lengthU := kind{"CheckLengthU", p.ToUnicode}
  27. lengthA := kind{"CheckLengthA", p.ToASCII}
  28. p = New(MapForLookup(), StrictDomainName(false))
  29. std3 := kind{"STD3", p.ToASCII}
  30. testCases := []struct {
  31. kind
  32. input string
  33. want string
  34. wantErr string
  35. }{
  36. {lengthU, "", "", "A4"}, // From UTS 46 conformance test.
  37. {lengthA, "", "", "A4"},
  38. {lengthU, "xn--", "", "A4"},
  39. {lengthU, "foo.xn--", "foo.", "A4"}, // TODO: is dropping xn-- correct?
  40. {lengthU, "xn--.foo", ".foo", "A4"},
  41. {lengthU, "foo.xn--.bar", "foo..bar", "A4"},
  42. {display, "xn--", "", ""},
  43. {display, "foo.xn--", "foo.", ""}, // TODO: is dropping xn-- correct?
  44. {display, "xn--.foo", ".foo", ""},
  45. {display, "foo.xn--.bar", "foo..bar", ""},
  46. {lengthA, "a..b", "a..b", "A4"},
  47. {punyA, ".b", ".b", ""},
  48. // For backwards compatibility, the Punycode profile does not map runes.
  49. {punyA, "\u3002b", "xn--b-83t", ""},
  50. {punyA, "..b", "..b", ""},
  51. {lengthA, ".b", ".b", "A4"},
  52. {lengthA, "\u3002b", ".b", "A4"},
  53. {lengthA, "..b", "..b", "A4"},
  54. {lengthA, "b..", "b..", ""},
  55. // Sharpened Bidi rules for Unicode 10.0.0. Apply for ALL labels in ANY
  56. // of the labels is RTL.
  57. {lengthA, "\ufe05\u3002\u3002\U0002603e\u1ce0", "..xn--t6f5138v", "A4"},
  58. {lengthA, "FAX\u2a77\U0001d186\u3002\U0001e942\U000e0181\u180c", "", "B6"},
  59. {resolve, "a..b", "a..b", ""},
  60. // Note that leading dots are not stripped. This is to be consistent
  61. // with the Punycode profile as well as the conformance test.
  62. {resolve, ".b", ".b", ""},
  63. {resolve, "\u3002b", ".b", ""},
  64. {resolve, "..b", "..b", ""},
  65. {resolve, "b..", "b..", ""},
  66. {resolve, "\xed", "", "P1"},
  67. // Raw punycode
  68. {punyA, "", "", ""},
  69. {punyA, "*.foo.com", "*.foo.com", ""},
  70. {punyA, "Foo.com", "Foo.com", ""},
  71. // STD3 rules
  72. {display, "*.foo.com", "*.foo.com", "P1"},
  73. {std3, "*.foo.com", "*.foo.com", ""},
  74. // Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of
  75. // Chrome, Safari, and IE. Firefox will first map ⒐ to 9. and return
  76. // lab9.be.
  77. {resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be")
  78. {display, "lab⒐be", "lab⒐be", "P1"},
  79. {resolve, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de"
  80. {display, "Plan⒐faß.de", "plan⒐faß.de", "P1"},
  81. // Chrome 54.0 recognizes the error and treats this input verbatim as a
  82. // search string.
  83. // Safari 10.0 (non-conform spec) decomposes "⒈" and computes the
  84. // punycode on the result using transitional mapping.
  85. // Firefox 49.0.1 goes haywire on this string and prints a bunch of what
  86. // seems to be nested punycode encodings.
  87. {resolve, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"},
  88. {display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"},
  89. {resolve, "a\u200Cb", "ab", ""},
  90. {display, "a\u200Cb", "a\u200Cb", "C"},
  91. {resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"},
  92. {display, "a\u200Cb", "a\u200Cb", "C"},
  93. {resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"},
  94. {
  95. // Notice how the string gets transformed, even with an error.
  96. // Chrome will use the original string if it finds an error, so not
  97. // the transformed one.
  98. display,
  99. "gr\ufecb\ufeae\ufe91\ufef2.de",
  100. "gr\u0639\u0631\u0628\u064a.de",
  101. "B",
  102. },
  103. {resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"}, // ٱ.σߜ
  104. {display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"},
  105. // normalize input
  106. {resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢
  107. {display, "a\u0323\u0322", "\u1ea1\u0322", ""},
  108. // Non-normalized strings are not normalized when they originate from
  109. // punycode. Despite the error, Chrome, Safari and Firefox will attempt
  110. // to look up the input punycode.
  111. {resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"},
  112. {display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"},
  113. }
  114. for _, tc := range testCases {
  115. doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr)
  116. }
  117. }