2017-07-26 08:02:35 +02:00
|
|
|
|
// Copyright (c) 2017 Euan Kemp
|
2017-08-17 10:23:24 +02:00
|
|
|
|
// Copyright (c) 2017 Daniel Oaks
|
2017-07-26 08:02:35 +02:00
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCasefoldChannel(t *testing.T) {
|
|
|
|
|
type channelTest struct {
|
|
|
|
|
channel string
|
|
|
|
|
folded string
|
|
|
|
|
err bool
|
|
|
|
|
}
|
|
|
|
|
testCases := []channelTest{
|
|
|
|
|
{
|
|
|
|
|
channel: "#foo",
|
|
|
|
|
folded: "#foo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#rfc1459[noncompliant]",
|
|
|
|
|
folded: "#rfc1459[noncompliant]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#{[]}",
|
|
|
|
|
folded: "#{[]}",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#FOO",
|
|
|
|
|
folded: "#foo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#bang!",
|
|
|
|
|
folded: "#bang!",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#",
|
|
|
|
|
folded: "#",
|
|
|
|
|
},
|
2018-12-06 12:46:11 +01:00
|
|
|
|
{
|
|
|
|
|
channel: "##",
|
|
|
|
|
folded: "##",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "##Ubuntu",
|
|
|
|
|
folded: "##ubuntu",
|
|
|
|
|
},
|
2018-12-06 04:35:36 +01:00
|
|
|
|
{
|
|
|
|
|
channel: "#中文频道",
|
|
|
|
|
folded: "#中文频道",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// Hebrew; it's up to the client to display this right-to-left, including the #
|
|
|
|
|
channel: "#שלום",
|
|
|
|
|
folded: "#שלום",
|
|
|
|
|
},
|
2017-07-26 08:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, errCase := range []string{
|
|
|
|
|
"", "#*starpower", "# NASA", "#interro?", "OOF#", "foo",
|
2018-12-06 04:35:36 +01:00
|
|
|
|
// bidi violation mixing latin and hebrew characters:
|
|
|
|
|
"#shalomעליכם",
|
2019-12-22 15:19:28 +01:00
|
|
|
|
"#tab\tcharacter", "#\t", "#carriage\rreturn",
|
2017-07-26 08:02:35 +02:00
|
|
|
|
} {
|
|
|
|
|
testCases = append(testCases, channelTest{channel: errCase, err: true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, tt := range testCases {
|
|
|
|
|
t.Run(fmt.Sprintf("case %d: %s", i, tt.channel), func(t *testing.T) {
|
|
|
|
|
res, err := CasefoldChannel(tt.channel)
|
2017-08-17 10:23:24 +02:00
|
|
|
|
if tt.err && err == nil {
|
|
|
|
|
t.Errorf("expected error when casefolding [%s], but did not receive one", tt.channel)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !tt.err && err != nil {
|
|
|
|
|
t.Errorf("unexpected error while casefolding [%s]: %s", tt.channel, err.Error())
|
2017-07-26 08:02:35 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if tt.folded != res {
|
2017-08-17 10:23:24 +02:00
|
|
|
|
t.Errorf("expected [%v] to be [%v]", res, tt.folded)
|
2017-07-26 08:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCasefoldName(t *testing.T) {
|
|
|
|
|
type nameTest struct {
|
|
|
|
|
name string
|
|
|
|
|
folded string
|
|
|
|
|
err bool
|
|
|
|
|
}
|
|
|
|
|
testCases := []nameTest{
|
|
|
|
|
{
|
|
|
|
|
name: "foo",
|
|
|
|
|
folded: "foo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "FOO",
|
|
|
|
|
folded: "foo",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, errCase := range []string{
|
|
|
|
|
"", "#", "foo,bar", "star*man*junior", "lo7t?",
|
|
|
|
|
"f.l", "excited!nick", "foo@bar", ":trail",
|
2020-06-05 00:40:14 +02:00
|
|
|
|
"~o", "&o", "@o", "%h", "+v", "-m", "\t", "a\tb",
|
2017-07-26 08:02:35 +02:00
|
|
|
|
} {
|
|
|
|
|
testCases = append(testCases, nameTest{name: errCase, err: true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, tt := range testCases {
|
|
|
|
|
t.Run(fmt.Sprintf("case %d: %s", i, tt.name), func(t *testing.T) {
|
|
|
|
|
res, err := CasefoldName(tt.name)
|
2017-08-17 10:23:24 +02:00
|
|
|
|
if tt.err && err == nil {
|
|
|
|
|
t.Errorf("expected error when casefolding [%s], but did not receive one", tt.name)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !tt.err && err != nil {
|
|
|
|
|
t.Errorf("unexpected error while casefolding [%s]: %s", tt.name, err.Error())
|
2017-07-26 08:02:35 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if tt.folded != res {
|
2017-08-17 10:23:24 +02:00
|
|
|
|
t.Errorf("expected [%v] to be [%v]", res, tt.folded)
|
2017-07-26 08:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-31 00:59:49 +01:00
|
|
|
|
|
2019-02-03 09:49:42 +01:00
|
|
|
|
func TestIsIdent(t *testing.T) {
|
|
|
|
|
assertIdent := func(str string, expected bool) {
|
|
|
|
|
if isIdent(str) != expected {
|
|
|
|
|
t.Errorf("expected [%s] to have identness [%t], but got [%t]", str, expected, !expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assertIdent("warning", true)
|
|
|
|
|
assertIdent("sid3225", true)
|
|
|
|
|
assertIdent("dan.oak25", true)
|
|
|
|
|
assertIdent("dan.oak[25]", true)
|
|
|
|
|
assertIdent("phi@#$%ip", false)
|
|
|
|
|
assertIdent("Νικηφόρος", false)
|
|
|
|
|
assertIdent("-dan56", false)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 00:59:49 +01:00
|
|
|
|
func TestSkeleton(t *testing.T) {
|
|
|
|
|
skeleton := func(str string) string {
|
|
|
|
|
skel, err := Skeleton(str)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
return skel
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 12:18:18 +02:00
|
|
|
|
if skeleton("warning") == skeleton("waming") {
|
|
|
|
|
t.Errorf("Oragono shouldn't consider rn confusable with m")
|
2019-01-31 00:59:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if skeleton("Phi|ip") != "philip" {
|
|
|
|
|
t.Errorf("but we still consider pipe confusable with l")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 06:14:39 +02:00
|
|
|
|
if skeleton("smt") != skeleton("smt") {
|
2019-01-31 00:59:49 +01:00
|
|
|
|
t.Errorf("fullwidth characters should skeletonize to plain old ascii characters")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 12:18:18 +02:00
|
|
|
|
if skeleton("SMT") != skeleton("smt") {
|
2019-01-31 00:59:49 +01:00
|
|
|
|
t.Errorf("after skeletonizing, we should casefold")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 06:14:39 +02:00
|
|
|
|
if skeleton("smt") != skeleton("smt") {
|
2019-06-18 08:34:16 +02:00
|
|
|
|
t.Errorf("our friend lover successfully tricked the skeleton algorithm!")
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 00:59:49 +01:00
|
|
|
|
if skeleton("еvan") != "evan" {
|
|
|
|
|
t.Errorf("we must protect against cyrillic homoglyph attacks")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 10:53:15 +02:00
|
|
|
|
if skeleton("еmily") != skeleton("emily") {
|
|
|
|
|
t.Errorf("we must protect against cyrillic homoglyph attacks")
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 00:59:49 +01:00
|
|
|
|
if skeleton("РОТАТО") != "potato" {
|
|
|
|
|
t.Errorf("we must protect against cyrillic homoglyph attacks")
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 08:45:02 +01:00
|
|
|
|
// should not raise an error:
|
|
|
|
|
skeleton("けらんぐ")
|
2019-01-31 00:59:49 +01:00
|
|
|
|
}
|
2019-07-14 22:17:37 +02:00
|
|
|
|
|
|
|
|
|
func TestCanonicalizeMaskWildcard(t *testing.T) {
|
|
|
|
|
tester := func(input, expected string, expectedErr error) {
|
|
|
|
|
out, err := CanonicalizeMaskWildcard(input)
|
2020-10-26 04:31:45 +01:00
|
|
|
|
if expectedErr == nil && out != expected {
|
2019-07-14 22:17:37 +02:00
|
|
|
|
t.Errorf("expected %s to canonicalize to %s, instead %s", input, expected, out)
|
|
|
|
|
}
|
|
|
|
|
if err != expectedErr {
|
|
|
|
|
t.Errorf("expected %s to produce error %v, instead %v", input, expectedErr, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tester("shivaram", "shivaram!*@*", nil)
|
|
|
|
|
tester("slingamn!shivaram", "slingamn!shivaram@*", nil)
|
|
|
|
|
tester("ברוך", "ברוך!*@*", nil)
|
|
|
|
|
tester("hacker@monad.io", "*!hacker@monad.io", nil)
|
|
|
|
|
tester("Evan!hacker@monad.io", "evan!hacker@monad.io", nil)
|
|
|
|
|
tester("РОТАТО!Potato", "ротато!potato@*", nil)
|
|
|
|
|
tester("tkadich*", "tkadich*!*@*", nil)
|
|
|
|
|
tester("SLINGAMN!*@*", "slingamn!*@*", nil)
|
|
|
|
|
tester("slingamn!shivaram*", "slingamn!shivaram*@*", nil)
|
|
|
|
|
tester("slingamn!", "slingamn!*@*", nil)
|
|
|
|
|
tester("shivaram*@good-fortune", "*!shivaram*@good-fortune", nil)
|
2019-10-10 10:17:44 +02:00
|
|
|
|
tester("shivaram*", "shivaram*!*@*", nil)
|
2019-10-23 08:18:45 +02:00
|
|
|
|
tester("Shivaram*", "shivaram*!*@*", nil)
|
|
|
|
|
tester("*SHIVARAM*", "*shivaram*!*@*", nil)
|
2020-10-16 06:58:12 +02:00
|
|
|
|
tester("*SHIVARAM* ", "*shivaram*!*@*", nil)
|
2020-10-26 04:31:45 +01:00
|
|
|
|
|
|
|
|
|
tester(":shivaram", "", errInvalidCharacter)
|
|
|
|
|
tester("shivaram!us er@host", "", errInvalidCharacter)
|
|
|
|
|
tester("shivaram!user@ho st", "", errInvalidCharacter)
|
2019-07-14 22:17:37 +02:00
|
|
|
|
}
|
2019-12-18 13:06:04 +01:00
|
|
|
|
|
2019-12-22 15:57:49 +01:00
|
|
|
|
func validFoldTester(first, second string, equal bool, folder func(string) (string, error), t *testing.T) {
|
|
|
|
|
firstFolded, err := folder(first)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
secondFolded, err := folder(second)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
foundEqual := firstFolded == secondFolded
|
|
|
|
|
if foundEqual != equal {
|
|
|
|
|
t.Errorf("%s and %s: expected equality %t, but got %t", first, second, equal, foundEqual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 13:06:04 +01:00
|
|
|
|
func TestFoldPermissive(t *testing.T) {
|
|
|
|
|
tester := func(first, second string, equal bool) {
|
2019-12-22 15:57:49 +01:00
|
|
|
|
validFoldTester(first, second, equal, foldPermissive, t)
|
2019-12-18 13:06:04 +01:00
|
|
|
|
}
|
|
|
|
|
tester("SHIVARAM", "shivaram", true)
|
|
|
|
|
tester("shIvaram", "shivaraM", true)
|
|
|
|
|
tester("shivaram", "DAN-", false)
|
|
|
|
|
tester("dolph🐬n", "DOLPH🐬n", true)
|
|
|
|
|
tester("dolph🐬n", "dolph💻n", false)
|
2019-12-22 15:31:51 +01:00
|
|
|
|
tester("9FRONT", "9front", true)
|
2019-12-18 13:06:04 +01:00
|
|
|
|
}
|
2019-12-22 15:19:28 +01:00
|
|
|
|
|
|
|
|
|
func TestFoldPermissiveInvalid(t *testing.T) {
|
|
|
|
|
_, err := foldPermissive("a\tb")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("whitespace should be invalid in identifiers")
|
|
|
|
|
}
|
|
|
|
|
_, err = foldPermissive("a\x00b")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("the null byte should be invalid in identifiers")
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-22 15:57:49 +01:00
|
|
|
|
|
|
|
|
|
func TestFoldASCII(t *testing.T) {
|
|
|
|
|
tester := func(first, second string, equal bool) {
|
|
|
|
|
validFoldTester(first, second, equal, foldASCII, t)
|
|
|
|
|
}
|
|
|
|
|
tester("shivaram", "SHIVARAM", true)
|
|
|
|
|
tester("X|Y", "x|y", true)
|
|
|
|
|
tester("a != b", "A != B", true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFoldASCIIInvalid(t *testing.T) {
|
|
|
|
|
_, err := foldASCII("\x01")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("control characters should be invalid in identifiers")
|
|
|
|
|
}
|
|
|
|
|
_, err = foldASCII("\x7F")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("control characters should be invalid in identifiers")
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-28 04:10:55 +02:00
|
|
|
|
|
|
|
|
|
func TestFoldRFC1459(t *testing.T) {
|
|
|
|
|
folder := func(str string) (string, error) {
|
|
|
|
|
return foldRFC1459(str, false)
|
|
|
|
|
}
|
|
|
|
|
tester := func(first, second string, equal bool) {
|
|
|
|
|
validFoldTester(first, second, equal, folder, t)
|
|
|
|
|
}
|
|
|
|
|
tester("shivaram", "SHIVARAM", true)
|
|
|
|
|
tester("shivaram[a]", "shivaram{a}", true)
|
|
|
|
|
tester("shivaram\\a]", "shivaram{a}", false)
|
|
|
|
|
tester("shivaram\\a]", "shivaram|a}", true)
|
|
|
|
|
tester("shivaram~a]", "shivaram^a}", true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFoldRFC1459Strict(t *testing.T) {
|
|
|
|
|
folder := func(str string) (string, error) {
|
|
|
|
|
return foldRFC1459(str, true)
|
|
|
|
|
}
|
|
|
|
|
tester := func(first, second string, equal bool) {
|
|
|
|
|
validFoldTester(first, second, equal, folder, t)
|
|
|
|
|
}
|
|
|
|
|
tester("shivaram", "SHIVARAM", true)
|
|
|
|
|
tester("shivaram[a]", "shivaram{a}", true)
|
|
|
|
|
tester("shivaram\\a]", "shivaram{a}", false)
|
|
|
|
|
tester("shivaram\\a]", "shivaram|a}", true)
|
|
|
|
|
tester("shivaram~a]", "shivaram^a}", false)
|
|
|
|
|
}
|