Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

strings_test.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. } {
  63. testCases = append(testCases, channelTest{channel: errCase, err: true})
  64. }
  65. for i, tt := range testCases {
  66. t.Run(fmt.Sprintf("case %d: %s", i, tt.channel), func(t *testing.T) {
  67. res, err := CasefoldChannel(tt.channel)
  68. if tt.err && err == nil {
  69. t.Errorf("expected error when casefolding [%s], but did not receive one", tt.channel)
  70. return
  71. }
  72. if !tt.err && err != nil {
  73. t.Errorf("unexpected error while casefolding [%s]: %s", tt.channel, err.Error())
  74. return
  75. }
  76. if tt.folded != res {
  77. t.Errorf("expected [%v] to be [%v]", res, tt.folded)
  78. }
  79. })
  80. }
  81. }
  82. func TestCasefoldName(t *testing.T) {
  83. type nameTest struct {
  84. name string
  85. folded string
  86. err bool
  87. }
  88. testCases := []nameTest{
  89. {
  90. name: "foo",
  91. folded: "foo",
  92. },
  93. {
  94. name: "FOO",
  95. folded: "foo",
  96. },
  97. }
  98. for _, errCase := range []string{
  99. "", "#", "foo,bar", "star*man*junior", "lo7t?",
  100. "f.l", "excited!nick", "foo@bar", ":trail",
  101. "~o", "&o", "@o", "%h", "+v", "-m",
  102. } {
  103. testCases = append(testCases, nameTest{name: errCase, err: true})
  104. }
  105. for i, tt := range testCases {
  106. t.Run(fmt.Sprintf("case %d: %s", i, tt.name), func(t *testing.T) {
  107. res, err := CasefoldName(tt.name)
  108. if tt.err && err == nil {
  109. t.Errorf("expected error when casefolding [%s], but did not receive one", tt.name)
  110. return
  111. }
  112. if !tt.err && err != nil {
  113. t.Errorf("unexpected error while casefolding [%s]: %s", tt.name, err.Error())
  114. return
  115. }
  116. if tt.folded != res {
  117. t.Errorf("expected [%v] to be [%v]", res, tt.folded)
  118. }
  119. })
  120. }
  121. }
  122. func TestIsIdent(t *testing.T) {
  123. assertIdent := func(str string, expected bool) {
  124. if isIdent(str) != expected {
  125. t.Errorf("expected [%s] to have identness [%t], but got [%t]", str, expected, !expected)
  126. }
  127. }
  128. assertIdent("warning", true)
  129. assertIdent("sid3225", true)
  130. assertIdent("dan.oak25", true)
  131. assertIdent("dan.oak[25]", true)
  132. assertIdent("phi@#$%ip", false)
  133. assertIdent("Νικηφόρος", false)
  134. assertIdent("-dan56", false)
  135. }
  136. func TestSkeleton(t *testing.T) {
  137. skeleton := func(str string) string {
  138. skel, err := Skeleton(str)
  139. if err != nil {
  140. t.Error(err)
  141. }
  142. return skel
  143. }
  144. if skeleton("warning") == skeleton("waming") {
  145. t.Errorf("Oragono shouldn't consider rn confusable with m")
  146. }
  147. if skeleton("Phi|ip") != "philip" {
  148. t.Errorf("but we still consider pipe confusable with l")
  149. }
  150. if skeleton("smt") != skeleton("smt") {
  151. t.Errorf("fullwidth characters should skeletonize to plain old ascii characters")
  152. }
  153. if skeleton("SMT") != skeleton("smt") {
  154. t.Errorf("after skeletonizing, we should casefold")
  155. }
  156. if skeleton("smt") != skeleton("smt") {
  157. t.Errorf("our friend lover successfully tricked the skeleton algorithm!")
  158. }
  159. if skeleton("еvan") != "evan" {
  160. t.Errorf("we must protect against cyrillic homoglyph attacks")
  161. }
  162. if skeleton("еmily") != skeleton("emily") {
  163. t.Errorf("we must protect against cyrillic homoglyph attacks")
  164. }
  165. if skeleton("РОТАТО") != "potato" {
  166. t.Errorf("we must protect against cyrillic homoglyph attacks")
  167. }
  168. // should not raise an error:
  169. skeleton("けらんぐ")
  170. }
  171. func TestCanonicalizeMaskWildcard(t *testing.T) {
  172. tester := func(input, expected string, expectedErr error) {
  173. out, err := CanonicalizeMaskWildcard(input)
  174. if out != expected {
  175. t.Errorf("expected %s to canonicalize to %s, instead %s", input, expected, out)
  176. }
  177. if err != expectedErr {
  178. t.Errorf("expected %s to produce error %v, instead %v", input, expectedErr, err)
  179. }
  180. }
  181. tester("shivaram", "shivaram!*@*", nil)
  182. tester("slingamn!shivaram", "slingamn!shivaram@*", nil)
  183. tester("ברוך", "ברוך!*@*", nil)
  184. tester("hacker@monad.io", "*!hacker@monad.io", nil)
  185. tester("Evan!hacker@monad.io", "evan!hacker@monad.io", nil)
  186. tester("РОТАТО!Potato", "ротато!potato@*", nil)
  187. tester("tkadich*", "tkadich*!*@*", nil)
  188. tester("SLINGAMN!*@*", "slingamn!*@*", nil)
  189. tester("slingamn!shivaram*", "slingamn!shivaram*@*", nil)
  190. tester("slingamn!", "slingamn!*@*", nil)
  191. tester("shivaram*@good-fortune", "*!shivaram*@good-fortune", nil)
  192. tester("shivaram*", "shivaram*!*@*", nil)
  193. tester("Shivaram*", "shivaram*!*@*", nil)
  194. tester("*SHIVARAM*", "*shivaram*!*@*", nil)
  195. }