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.

strings_test.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright (c) 2017 Euan Kemp
  2. // Copyright (c) 2017 Daniel Oaks
  3. // released under the MIT license
  4. package irc
  5. import (
  6. "fmt"
  7. "testing"
  8. )
  9. func TestCasefoldChannel(t *testing.T) {
  10. type channelTest struct {
  11. channel string
  12. folded string
  13. err bool
  14. }
  15. testCases := []channelTest{
  16. {
  17. channel: "#foo",
  18. folded: "#foo",
  19. },
  20. {
  21. channel: "#rfc1459[noncompliant]",
  22. folded: "#rfc1459[noncompliant]",
  23. },
  24. {
  25. channel: "#{[]}",
  26. folded: "#{[]}",
  27. },
  28. {
  29. channel: "#FOO",
  30. folded: "#foo",
  31. },
  32. {
  33. channel: "#bang!",
  34. folded: "#bang!",
  35. },
  36. {
  37. channel: "#",
  38. folded: "#",
  39. },
  40. {
  41. channel: "##",
  42. folded: "##",
  43. },
  44. {
  45. channel: "##Ubuntu",
  46. folded: "##ubuntu",
  47. },
  48. {
  49. channel: "#中文频道",
  50. folded: "#中文频道",
  51. },
  52. {
  53. // Hebrew; it's up to the client to display this right-to-left, including the #
  54. channel: "#שלום",
  55. folded: "#שלום",
  56. },
  57. }
  58. for _, errCase := range []string{
  59. "", "#*starpower", "# NASA", "#interro?", "OOF#", "foo",
  60. // bidi violation mixing latin and hebrew characters:
  61. "#shalomעליכם",
  62. "#tab\tcharacter", "#\t", "#carriage\rreturn",
  63. } {
  64. testCases = append(testCases, channelTest{channel: errCase, err: true})
  65. }
  66. for i, tt := range testCases {
  67. t.Run(fmt.Sprintf("case %d: %s", i, tt.channel), func(t *testing.T) {
  68. res, err := CasefoldChannel(tt.channel)
  69. if tt.err && err == nil {
  70. t.Errorf("expected error when casefolding [%s], but did not receive one", tt.channel)
  71. return
  72. }
  73. if !tt.err && err != nil {
  74. t.Errorf("unexpected error while casefolding [%s]: %s", tt.channel, err.Error())
  75. return
  76. }
  77. if tt.folded != res {
  78. t.Errorf("expected [%v] to be [%v]", res, tt.folded)
  79. }
  80. })
  81. }
  82. }
  83. func TestCasefoldName(t *testing.T) {
  84. type nameTest struct {
  85. name string
  86. folded string
  87. err bool
  88. }
  89. testCases := []nameTest{
  90. {
  91. name: "foo",
  92. folded: "foo",
  93. },
  94. {
  95. name: "FOO",
  96. folded: "foo",
  97. },
  98. }
  99. for _, errCase := range []string{
  100. "", "#", "foo,bar", "star*man*junior", "lo7t?",
  101. "f.l", "excited!nick", "foo@bar", ":trail",
  102. "~o", "&o", "@o", "%h", "+v", "-m", "\t", "a\tb",
  103. } {
  104. testCases = append(testCases, nameTest{name: errCase, err: true})
  105. }
  106. for i, tt := range testCases {
  107. t.Run(fmt.Sprintf("case %d: %s", i, tt.name), func(t *testing.T) {
  108. res, err := CasefoldName(tt.name)
  109. if tt.err && err == nil {
  110. t.Errorf("expected error when casefolding [%s], but did not receive one", tt.name)
  111. return
  112. }
  113. if !tt.err && err != nil {
  114. t.Errorf("unexpected error while casefolding [%s]: %s", tt.name, err.Error())
  115. return
  116. }
  117. if tt.folded != res {
  118. t.Errorf("expected [%v] to be [%v]", res, tt.folded)
  119. }
  120. })
  121. }
  122. }
  123. func TestIsIdent(t *testing.T) {
  124. assertIdent := func(str string, expected bool) {
  125. if isIdent(str) != expected {
  126. t.Errorf("expected [%s] to have identness [%t], but got [%t]", str, expected, !expected)
  127. }
  128. }
  129. assertIdent("warning", true)
  130. assertIdent("sid3225", true)
  131. assertIdent("dan.oak25", true)
  132. assertIdent("dan.oak[25]", true)
  133. assertIdent("phi@#$%ip", false)
  134. assertIdent("Νικηφόρος", false)
  135. assertIdent("-dan56", false)
  136. }
  137. func TestSkeleton(t *testing.T) {
  138. skeleton := func(str string) string {
  139. skel, err := Skeleton(str)
  140. if err != nil {
  141. t.Error(err)
  142. }
  143. return skel
  144. }
  145. if skeleton("warning") == skeleton("waming") {
  146. t.Errorf("Oragono shouldn't consider rn confusable with m")
  147. }
  148. if skeleton("Phi|ip") != "philip" {
  149. t.Errorf("but we still consider pipe confusable with l")
  150. }
  151. if skeleton("smt") != skeleton("smt") {
  152. t.Errorf("fullwidth characters should skeletonize to plain old ascii characters")
  153. }
  154. if skeleton("SMT") != skeleton("smt") {
  155. t.Errorf("after skeletonizing, we should casefold")
  156. }
  157. if skeleton("smt") != skeleton("smt") {
  158. t.Errorf("our friend lover successfully tricked the skeleton algorithm!")
  159. }
  160. if skeleton("еvan") != "evan" {
  161. t.Errorf("we must protect against cyrillic homoglyph attacks")
  162. }
  163. if skeleton("еmily") != skeleton("emily") {
  164. t.Errorf("we must protect against cyrillic homoglyph attacks")
  165. }
  166. if skeleton("РОТАТО") != "potato" {
  167. t.Errorf("we must protect against cyrillic homoglyph attacks")
  168. }
  169. // should not raise an error:
  170. skeleton("けらんぐ")
  171. }
  172. func TestCanonicalizeMaskWildcard(t *testing.T) {
  173. tester := func(input, expected string, expectedErr error) {
  174. out, err := CanonicalizeMaskWildcard(input)
  175. if expectedErr == nil && out != expected {
  176. t.Errorf("expected %s to canonicalize to %s, instead %s", input, expected, out)
  177. }
  178. if err != expectedErr {
  179. t.Errorf("expected %s to produce error %v, instead %v", input, expectedErr, err)
  180. }
  181. }
  182. tester("shivaram", "shivaram!*@*", nil)
  183. tester("slingamn!shivaram", "slingamn!shivaram@*", nil)
  184. tester("ברוך", "ברוך!*@*", nil)
  185. tester("hacker@monad.io", "*!hacker@monad.io", nil)
  186. tester("Evan!hacker@monad.io", "evan!hacker@monad.io", nil)
  187. tester("РОТАТО!Potato", "ротато!potato@*", nil)
  188. tester("tkadich*", "tkadich*!*@*", nil)
  189. tester("SLINGAMN!*@*", "slingamn!*@*", nil)
  190. tester("slingamn!shivaram*", "slingamn!shivaram*@*", nil)
  191. tester("slingamn!", "slingamn!*@*", nil)
  192. tester("shivaram*@good-fortune", "*!shivaram*@good-fortune", nil)
  193. tester("shivaram*", "shivaram*!*@*", nil)
  194. tester("Shivaram*", "shivaram*!*@*", nil)
  195. tester("*SHIVARAM*", "*shivaram*!*@*", nil)
  196. tester("*SHIVARAM* ", "*shivaram*!*@*", nil)
  197. tester(":shivaram", "", errInvalidCharacter)
  198. tester("shivaram!us er@host", "", errInvalidCharacter)
  199. tester("shivaram!user@ho st", "", errInvalidCharacter)
  200. }
  201. func validFoldTester(first, second string, equal bool, folder func(string) (string, error), t *testing.T) {
  202. firstFolded, err := folder(first)
  203. if err != nil {
  204. panic(err)
  205. }
  206. secondFolded, err := folder(second)
  207. if err != nil {
  208. panic(err)
  209. }
  210. foundEqual := firstFolded == secondFolded
  211. if foundEqual != equal {
  212. t.Errorf("%s and %s: expected equality %t, but got %t", first, second, equal, foundEqual)
  213. }
  214. }
  215. func TestFoldPermissive(t *testing.T) {
  216. tester := func(first, second string, equal bool) {
  217. validFoldTester(first, second, equal, foldPermissive, t)
  218. }
  219. tester("SHIVARAM", "shivaram", true)
  220. tester("shIvaram", "shivaraM", true)
  221. tester("shivaram", "DAN-", false)
  222. tester("dolph🐬n", "DOLPH🐬n", true)
  223. tester("dolph🐬n", "dolph💻n", false)
  224. tester("9FRONT", "9front", true)
  225. }
  226. func TestFoldPermissiveInvalid(t *testing.T) {
  227. _, err := foldPermissive("a\tb")
  228. if err == nil {
  229. t.Errorf("whitespace should be invalid in identifiers")
  230. }
  231. _, err = foldPermissive("a\x00b")
  232. if err == nil {
  233. t.Errorf("the null byte should be invalid in identifiers")
  234. }
  235. }
  236. func TestFoldASCII(t *testing.T) {
  237. tester := func(first, second string, equal bool) {
  238. validFoldTester(first, second, equal, foldASCII, t)
  239. }
  240. tester("shivaram", "SHIVARAM", true)
  241. tester("X|Y", "x|y", true)
  242. tester("a != b", "A != B", true)
  243. }
  244. func TestFoldASCIIInvalid(t *testing.T) {
  245. _, err := foldASCII("\x01")
  246. if err == nil {
  247. t.Errorf("control characters should be invalid in identifiers")
  248. }
  249. _, err = foldASCII("\x7F")
  250. if err == nil {
  251. t.Errorf("control characters should be invalid in identifiers")
  252. }
  253. }