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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 TestIsBoring(t *testing.T) {
  123. assertBoring := func(str string, expected bool) {
  124. if isBoring(str) != expected {
  125. t.Errorf("expected [%s] to have boringness [%t], but got [%t]", str, expected, !expected)
  126. }
  127. }
  128. assertBoring("warning", true)
  129. assertBoring("phi|ip", false)
  130. assertBoring("Νικηφόρος", false)
  131. }
  132. func TestIsIdent(t *testing.T) {
  133. assertIdent := func(str string, expected bool) {
  134. if isIdent(str) != expected {
  135. t.Errorf("expected [%s] to have identness [%t], but got [%t]", str, expected, !expected)
  136. }
  137. }
  138. assertIdent("warning", true)
  139. assertIdent("sid3225", true)
  140. assertIdent("dan.oak25", true)
  141. assertIdent("dan.oak[25]", true)
  142. assertIdent("phi@#$%ip", false)
  143. assertIdent("Νικηφόρος", false)
  144. assertIdent("-dan56", false)
  145. }
  146. func TestSkeleton(t *testing.T) {
  147. skeleton := func(str string) string {
  148. skel, err := Skeleton(str)
  149. if err != nil {
  150. t.Error(err)
  151. }
  152. return skel
  153. }
  154. if skeleton("warning") == skeleton("waming") {
  155. t.Errorf("Oragono shouldn't consider rn confusable with m")
  156. }
  157. if skeleton("Phi|ip") != "philip" {
  158. t.Errorf("but we still consider pipe confusable with l")
  159. }
  160. if skeleton("smt") != "smt" {
  161. t.Errorf("fullwidth characters should skeletonize to plain old ascii characters")
  162. }
  163. if skeleton("SMT") != "smt" {
  164. t.Errorf("after skeletonizing, we should casefold")
  165. }
  166. if skeleton("еvan") != "evan" {
  167. t.Errorf("we must protect against cyrillic homoglyph attacks")
  168. }
  169. if skeleton("РОТАТО") != "potato" {
  170. t.Errorf("we must protect against cyrillic homoglyph attacks")
  171. }
  172. // should not raise an error:
  173. skeleton("けらんぐ")
  174. }