enforce10.0.0_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright 2015 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. //go:build go1.10
  5. // +build go1.10
  6. package precis
  7. import (
  8. "strings"
  9. "golang.org/x/text/runes"
  10. "golang.org/x/text/secure/bidirule"
  11. )
  12. var enforceTestCases = []struct {
  13. name string
  14. p *Profile
  15. cases []testCase
  16. }{
  17. {"Basic", NewFreeform(), []testCase{
  18. {"e\u0301\u031f", "\u00e9\u031f", nil}, // normalize
  19. }},
  20. {"Context Rule 1", NewFreeform(), []testCase{
  21. // Rule 1: zero-width non-joiner (U+200C)
  22. // From RFC:
  23. // False
  24. // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
  25. // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
  26. // (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
  27. //
  28. // Example runes for different joining types:
  29. // Join L: U+A872; PHAGS-PA SUPERFIXED LETTER RA
  30. // Join D: U+062C; HAH WITH DOT BELOW
  31. // Join T: U+0610; ARABIC SIGN SALLALLAHOU ALAYHE WASSALLAM
  32. // Join R: U+0627; ALEF
  33. // Virama: U+0A4D; GURMUKHI SIGN VIRAMA
  34. // Virama and Join T: U+0ACD; GUJARATI SIGN VIRAMA
  35. {"\u200c", "", errContext},
  36. {"\u200ca", "", errContext},
  37. {"a\u200c", "", errContext},
  38. {"\u200c\u0627", "", errContext}, // missing JoinStart
  39. {"\u062c\u200c", "", errContext}, // missing JoinEnd
  40. {"\u0610\u200c\u0610\u0627", "", errContext}, // missing JoinStart
  41. {"\u062c\u0610\u200c\u0610", "", errContext}, // missing JoinEnd
  42. // Variants of: D T* U+200c T* R
  43. {"\u062c\u200c\u0627", "\u062c\u200c\u0627", nil},
  44. {"\u062c\u0610\u200c\u0610\u0627", "\u062c\u0610\u200c\u0610\u0627", nil},
  45. {"\u062c\u0610\u0610\u200c\u0610\u0610\u0627", "\u062c\u0610\u0610\u200c\u0610\u0610\u0627", nil},
  46. {"\u062c\u0610\u200c\u0627", "\u062c\u0610\u200c\u0627", nil},
  47. {"\u062c\u200c\u0610\u0627", "\u062c\u200c\u0610\u0627", nil},
  48. // Variants of: L T* U+200c T* D
  49. {"\ua872\u200c\u062c", "\ua872\u200c\u062c", nil},
  50. {"\ua872\u0610\u200c\u0610\u062c", "\ua872\u0610\u200c\u0610\u062c", nil},
  51. {"\ua872\u0610\u0610\u200c\u0610\u0610\u062c", "\ua872\u0610\u0610\u200c\u0610\u0610\u062c", nil},
  52. {"\ua872\u0610\u200c\u062c", "\ua872\u0610\u200c\u062c", nil},
  53. {"\ua872\u200c\u0610\u062c", "\ua872\u200c\u0610\u062c", nil},
  54. // Virama
  55. {"\u0a4d\u200c", "\u0a4d\u200c", nil},
  56. {"\ua872\u0a4d\u200c", "\ua872\u0a4d\u200c", nil},
  57. {"\ua872\u0a4d\u0610\u200c", "", errContext},
  58. {"\ua872\u0a4d\u0610\u200c", "", errContext},
  59. {"\u0acd\u200c", "\u0acd\u200c", nil},
  60. {"\ua872\u0acd\u200c", "\ua872\u0acd\u200c", nil},
  61. {"\ua872\u0acd\u0610\u200c", "", errContext},
  62. {"\ua872\u0acd\u0610\u200c", "", errContext},
  63. // Using Virama as join T
  64. {"\ua872\u0acd\u200c\u062c", "\ua872\u0acd\u200c\u062c", nil},
  65. {"\ua872\u200c\u0acd\u062c", "\ua872\u200c\u0acd\u062c", nil},
  66. }},
  67. {"Context Rule 2", NewFreeform(), []testCase{
  68. // Rule 2: zero-width joiner (U+200D)
  69. {"\u200d", "", errContext},
  70. {"\u200da", "", errContext},
  71. {"a\u200d", "", errContext},
  72. {"\u0a4d\u200d", "\u0a4d\u200d", nil},
  73. {"\ua872\u0a4d\u200d", "\ua872\u0a4d\u200d", nil},
  74. {"\u0a4da\u200d", "", errContext},
  75. }},
  76. {"Context Rule 3", NewFreeform(), []testCase{
  77. // Rule 3: middle dot
  78. {"·", "", errContext},
  79. {"l·", "", errContext},
  80. {"·l", "", errContext},
  81. {"a·", "", errContext},
  82. {"l·a", "", errContext},
  83. {"a·a", "", errContext},
  84. {"l·l", "l·l", nil},
  85. {"al·la", "al·la", nil},
  86. }},
  87. {"Context Rule 4", NewFreeform(), []testCase{
  88. // Rule 4: Greek lower numeral U+0375
  89. {"͵", "", errContext},
  90. {"͵a", "", errContext},
  91. {"α͵", "", errContext},
  92. {"͵α", "͵α", nil},
  93. {"α͵α", "α͵α", nil},
  94. {"͵͵α", "͵͵α", nil}, // The numeric sign is itself Greek.
  95. {"α͵͵α", "α͵͵α", nil},
  96. {"α͵͵", "", errContext},
  97. {"α͵͵a", "", errContext},
  98. }},
  99. {"Context Rule 5+6", NewFreeform(), []testCase{
  100. // Rule 5+6: Hebrew preceding
  101. // U+05f3: Geresh
  102. {"׳", "", errContext},
  103. {"׳ה", "", errContext},
  104. {"a׳b", "", errContext},
  105. {"ש׳", "ש׳", nil}, // U+05e9 U+05f3
  106. {"ש׳׳׳", "ש׳׳׳", nil}, // U+05e9 U+05f3
  107. // U+05f4: Gershayim
  108. {"״", "", errContext},
  109. {"״ה", "", errContext},
  110. {"a״b", "", errContext},
  111. {"ש״", "ש״", nil}, // U+05e9 U+05f4
  112. {"ש״״״", "ש״״״", nil}, // U+05e9 U+05f4
  113. {"aש״״״", "aש״״״", nil}, // U+05e9 U+05f4
  114. }},
  115. {"Context Rule 7", NewFreeform(), []testCase{
  116. // Rule 7: Katakana middle Dot
  117. {"・", "", errContext},
  118. {"abc・", "", errContext},
  119. {"・def", "", errContext},
  120. {"abc・def", "", errContext},
  121. {"aヅc・def", "aヅc・def", nil},
  122. {"abc・dぶf", "abc・dぶf", nil},
  123. {"⺐bc・def", "⺐bc・def", nil},
  124. }},
  125. {"Context Rule 8+9", NewFreeform(), []testCase{
  126. // Rule 8+9: Arabic Indic Digit
  127. {"١٢٣٤٥۶", "", errContext},
  128. {"۱۲۳۴۵٦", "", errContext},
  129. {"١٢٣٤٥", "١٢٣٤٥", nil},
  130. {"۱۲۳۴۵", "۱۲۳۴۵", nil},
  131. }},
  132. {"Nickname", Nickname, []testCase{
  133. {" Swan of Avon ", "Swan of Avon", nil},
  134. {"", "", errEmptyString},
  135. {" ", "", errEmptyString},
  136. {" ", "", errEmptyString},
  137. {"a\u00A0a\u1680a\u2000a\u2001a\u2002a\u2003a\u2004a\u2005a\u2006a\u2007a\u2008a\u2009a\u200Aa\u202Fa\u205Fa\u3000a", "a a a a a a a a a a a a a a a a a", nil},
  138. {"Foo", "Foo", nil},
  139. {"foo", "foo", nil},
  140. {"Foo Bar", "Foo Bar", nil},
  141. {"foo bar", "foo bar", nil},
  142. {"\u03A3", "\u03A3", nil},
  143. {"\u03C3", "\u03C3", nil},
  144. // Greek final sigma is left as is (do not fold!)
  145. {"\u03C2", "\u03C2", nil},
  146. {"\u265A", "♚", nil},
  147. {"Richard \u2163", "Richard IV", nil},
  148. {"\u212B", "Å", nil},
  149. {"\uFB00", "ff", nil}, // because of NFKC
  150. {"שa", "שa", nil}, // no bidi rule
  151. {"동일조건변경허락", "동일조건변경허락", nil},
  152. }},
  153. {"OpaqueString", OpaqueString, []testCase{
  154. {" Swan of Avon ", " Swan of Avon ", nil},
  155. {"", "", errEmptyString},
  156. {" ", " ", nil},
  157. {" ", " ", nil},
  158. {"a\u00A0a\u1680a\u2000a\u2001a\u2002a\u2003a\u2004a\u2005a\u2006a\u2007a\u2008a\u2009a\u200Aa\u202Fa\u205Fa\u3000a", "a a a a a a a a a a a a a a a a a", nil},
  159. {"Foo", "Foo", nil},
  160. {"foo", "foo", nil},
  161. {"Foo Bar", "Foo Bar", nil},
  162. {"foo bar", "foo bar", nil},
  163. {"\u03C3", "\u03C3", nil},
  164. {"Richard \u2163", "Richard \u2163", nil},
  165. {"\u212B", "Å", nil},
  166. {"Jack of \u2666s", "Jack of \u2666s", nil},
  167. {"my cat is a \u0009by", "", errDisallowedRune},
  168. {"שa", "שa", nil}, // no bidi rule
  169. }},
  170. {"UsernameCaseMapped", UsernameCaseMapped, []testCase{
  171. // TODO: Should this work?
  172. // {UsernameCaseMapped, "", "", errDisallowedRune},
  173. {"juliet@example.com", "juliet@example.com", nil},
  174. {"fussball", "fussball", nil},
  175. {"fu\u00DFball", "fu\u00DFball", nil},
  176. {"\u03C0", "\u03C0", nil},
  177. {"\u03A3", "\u03C3", nil},
  178. {"\u03C3", "\u03C3", nil},
  179. // Greek final sigma is left as is (do not fold!)
  180. {"\u03C2", "\u03C2", nil},
  181. {"\u0049", "\u0069", nil},
  182. {"\u0049", "\u0069", nil},
  183. {"\u03D2", "", errDisallowedRune},
  184. {"\u03B0", "\u03B0", nil},
  185. {"foo bar", "", errDisallowedRune},
  186. {"♚", "", bidirule.ErrInvalid},
  187. {"\u007E", "~", nil},
  188. {"a", "a", nil},
  189. {"!", "!", nil},
  190. {"²", "", bidirule.ErrInvalid},
  191. {"\t", "", errDisallowedRune},
  192. {"\n", "", errDisallowedRune},
  193. {"\u26D6", "", bidirule.ErrInvalid},
  194. {"\u26FF", "", bidirule.ErrInvalid},
  195. {"\uFB00", "", errDisallowedRune},
  196. {"\u1680", "", bidirule.ErrInvalid},
  197. {" ", "", errDisallowedRune},
  198. {" ", "", errDisallowedRune},
  199. {"\u01C5", "", errDisallowedRune},
  200. {"\u16EE", "", errDisallowedRune}, // Nl RUNIC ARLAUG SYMBOL
  201. {"\u0488", "", bidirule.ErrInvalid}, // Me COMBINING CYRILLIC HUNDRED THOUSANDS SIGN
  202. {"\u212B", "\u00e5", nil}, // Angstrom sign, NFC -> U+00E5
  203. {"A\u030A", "å", nil}, // A + ring
  204. {"\u00C5", "å", nil}, // A with ring
  205. {"\u00E7", "ç", nil}, // c cedille
  206. {"\u0063\u0327", "ç", nil}, // c + cedille
  207. {"\u0158", "ř", nil},
  208. {"\u0052\u030C", "ř", nil},
  209. {"\u1E61", "\u1E61", nil}, // LATIN SMALL LETTER S WITH DOT ABOVE
  210. // Confusable characters ARE allowed and should NOT be mapped.
  211. {"\u0410", "\u0430", nil}, // CYRILLIC CAPITAL LETTER A
  212. // Full width should be mapped to the canonical decomposition.
  213. {"AB", "ab", nil},
  214. {"שc", "", bidirule.ErrInvalid}, // bidi rule
  215. }},
  216. {"UsernameCasePreserved", UsernameCasePreserved, []testCase{
  217. {"ABC", "ABC", nil},
  218. {"AB", "AB", nil},
  219. {"שc", "", bidirule.ErrInvalid}, // bidi rule
  220. {"\uFB00", "", errDisallowedRune},
  221. {"\u212B", "\u00c5", nil}, // Angstrom sign, NFC -> U+00E5
  222. {"ẛ", "", errDisallowedRune}, // LATIN SMALL LETTER LONG S WITH DOT ABOVE
  223. }},
  224. {"UsernameCaseMappedRestricted", NewRestrictedProfile(UsernameCaseMapped, runes.Predicate(func(r rune) bool {
  225. return strings.ContainsRune(`@`, r)
  226. })), []testCase{
  227. {"juliet@example.com", "", errDisallowedRune},
  228. {"\u0049", "\u0069", nil},
  229. }},
  230. }