idna9.0.0_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Only strip leading empty labels for certain profiles. Stripping
  52. // leading empty labels here but not for "empty" punycode above seems
  53. // inconsistent, but seems to be applied by both the conformance test
  54. // and Chrome. So we turn it off by default, support it as an option,
  55. // and enable it in profiles where it seems commonplace.
  56. {lengthA, ".b", "b", ""},
  57. {lengthA, "\u3002b", "b", ""},
  58. {lengthA, "..b", "b", ""},
  59. {lengthA, "b..", "b..", ""},
  60. {resolve, "a..b", "a..b", ""},
  61. {resolve, ".b", "b", ""},
  62. {resolve, "\u3002b", "b", ""},
  63. {resolve, "..b", "b", ""},
  64. {resolve, "b..", "b..", ""},
  65. // Raw punycode
  66. {punyA, "", "", ""},
  67. {punyA, "*.foo.com", "*.foo.com", ""},
  68. {punyA, "Foo.com", "Foo.com", ""},
  69. // STD3 rules
  70. {display, "*.foo.com", "*.foo.com", "P1"},
  71. {std3, "*.foo.com", "*.foo.com", ""},
  72. // Don't map U+2490 (DIGIT NINE FULL STOP). This is the behavior of
  73. // Chrome, Safari, and IE. Firefox will first map ⒐ to 9. and return
  74. // lab9.be.
  75. {resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be")
  76. {display, "lab⒐be", "lab⒐be", "P1"},
  77. {resolve, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de"
  78. {display, "Plan⒐faß.de", "plan⒐faß.de", "P1"},
  79. // Chrome 54.0 recognizes the error and treats this input verbatim as a
  80. // search string.
  81. // Safari 10.0 (non-conform spec) decomposes "⒈" and computes the
  82. // punycode on the result using transitional mapping.
  83. // Firefox 49.0.1 goes haywire on this string and prints a bunch of what
  84. // seems to be nested punycode encodings.
  85. {resolve, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"},
  86. {display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"},
  87. {resolve, "a\u200Cb", "ab", ""},
  88. {display, "a\u200Cb", "a\u200Cb", "C"},
  89. {resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"},
  90. {display, "a\u200Cb", "a\u200Cb", "C"},
  91. {resolve, "grﻋﺮﺑﻲ.de", "xn--gr-gtd9a1b0g.de", "B"},
  92. {
  93. // Notice how the string gets transformed, even with an error.
  94. // Chrome will use the original string if it finds an error, so not
  95. // the transformed one.
  96. display,
  97. "gr\ufecb\ufeae\ufe91\ufef2.de",
  98. "gr\u0639\u0631\u0628\u064a.de",
  99. "B",
  100. },
  101. {resolve, "\u0671.\u03c3\u07dc", "xn--qib.xn--4xa21s", "B"}, // ٱ.σߜ
  102. {display, "\u0671.\u03c3\u07dc", "\u0671.\u03c3\u07dc", "B"},
  103. // normalize input
  104. {resolve, "a\u0323\u0322", "xn--jta191l", ""}, // ạ̢
  105. {display, "a\u0323\u0322", "\u1ea1\u0322", ""},
  106. // Non-normalized strings are not normalized when they originate from
  107. // punycode. Despite the error, Chrome, Safari and Firefox will attempt
  108. // to look up the input punycode.
  109. {resolve, encode("a\u0323\u0322") + ".com", "xn--a-tdbc.com", "V1"},
  110. {display, encode("a\u0323\u0322") + ".com", "a\u0323\u0322.com", "V1"},
  111. }
  112. for _, tc := range testCases {
  113. doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr)
  114. }
  115. }