Bladeren bron

strings: disallow ':' in nicks

This matches the behavior of inspircd at the very least.

Previously, the comment above that section claimed ':' should be
disallowed, but the code didn't do so.

I also simplified the code a little bit and added tests.
tags/v0.9.0
Euan Kemp 7 jaren geleden
bovenliggende
commit
449ef4cea1
2 gewijzigde bestanden met toevoegingen van 107 en 7 verwijderingen
  1. 2
    7
      irc/strings.go
  2. 105
    0
      irc/strings_test.go

+ 2
- 7
irc/strings.go Bestand weergeven

@@ -44,8 +44,7 @@ func CasefoldChannel(name string) (string, error) {
44 44
 	// , is used as a separator
45 45
 	// * is used in mask matching
46 46
 	// ? is used in mask matching
47
-	if strings.Contains(lowered, " ") || strings.Contains(lowered, ",") ||
48
-		strings.Contains(lowered, "*") || strings.Contains(lowered, "?") {
47
+	if strings.ContainsAny(lowered, " ,*?") {
49 48
 		return "", errInvalidCharacter
50 49
 	}
51 50
 
@@ -73,11 +72,7 @@ func CasefoldName(name string) (string, error) {
73 72
 	// # is a channel prefix
74 73
 	// ~&@%+ are channel membership prefixes
75 74
 	// - I feel like disallowing
76
-	if strings.Contains(lowered, " ") || strings.Contains(lowered, ",") ||
77
-		strings.Contains(lowered, "*") || strings.Contains(lowered, "?") ||
78
-		strings.Contains(lowered, ".") || strings.Contains(lowered, "!") ||
79
-		strings.Contains(lowered, "@") ||
80
-		strings.Contains("#~&@%+-", string(lowered[0])) {
75
+	if strings.ContainsAny(lowered, " ,*?.!@:") || strings.ContainsAny(string(lowered[0]), "#~&@%+-") {
81 76
 		return "", errInvalidCharacter
82 77
 	}
83 78
 

+ 105
- 0
irc/strings_test.go Bestand weergeven

@@ -0,0 +1,105 @@
1
+// Copyright (c) 2017 Euan Kemp
2
+// released under the MIT license
3
+
4
+package irc
5
+
6
+import (
7
+	"fmt"
8
+	"testing"
9
+)
10
+
11
+func TestCasefoldChannel(t *testing.T) {
12
+	type channelTest struct {
13
+		channel string
14
+		folded  string
15
+		err     bool
16
+	}
17
+	testCases := []channelTest{
18
+		{
19
+			channel: "#foo",
20
+			folded:  "#foo",
21
+		},
22
+		{
23
+			channel: "#rfc1459[noncompliant]",
24
+			folded:  "#rfc1459[noncompliant]",
25
+		},
26
+		{
27
+			channel: "#{[]}",
28
+			folded:  "#{[]}",
29
+		},
30
+		{
31
+			channel: "#FOO",
32
+			folded:  "#foo",
33
+		},
34
+		{
35
+			channel: "#bang!",
36
+			folded:  "#bang!",
37
+		},
38
+		{
39
+			channel: "#",
40
+			folded:  "#",
41
+		},
42
+	}
43
+
44
+	for _, errCase := range []string{
45
+		"", "#*starpower", "# NASA", "#interro?", "OOF#", "foo",
46
+	} {
47
+		testCases = append(testCases, channelTest{channel: errCase, err: true})
48
+	}
49
+
50
+	for i, tt := range testCases {
51
+		t.Run(fmt.Sprintf("case %d: %s", i, tt.channel), func(t *testing.T) {
52
+			res, err := CasefoldChannel(tt.channel)
53
+			if tt.err {
54
+				if err == nil {
55
+					t.Errorf("expected error")
56
+				}
57
+				return
58
+			}
59
+			if tt.folded != res {
60
+				t.Errorf("expected %v to be %v", tt.folded, res)
61
+			}
62
+		})
63
+	}
64
+}
65
+
66
+func TestCasefoldName(t *testing.T) {
67
+	type nameTest struct {
68
+		name   string
69
+		folded string
70
+		err    bool
71
+	}
72
+	testCases := []nameTest{
73
+		{
74
+			name:   "foo",
75
+			folded: "foo",
76
+		},
77
+		{
78
+			name:   "FOO",
79
+			folded: "foo",
80
+		},
81
+	}
82
+
83
+	for _, errCase := range []string{
84
+		"", "#", "foo,bar", "star*man*junior", "lo7t?",
85
+		"f.l", "excited!nick", "foo@bar", ":trail",
86
+		"~o", "&o", "@o", "%h", "+v", "-m",
87
+	} {
88
+		testCases = append(testCases, nameTest{name: errCase, err: true})
89
+	}
90
+
91
+	for i, tt := range testCases {
92
+		t.Run(fmt.Sprintf("case %d: %s", i, tt.name), func(t *testing.T) {
93
+			res, err := CasefoldName(tt.name)
94
+			if tt.err {
95
+				if err == nil {
96
+					t.Errorf("expected error")
97
+				}
98
+				return
99
+			}
100
+			if tt.folded != res {
101
+				t.Errorf("expected %v to be %v", tt.folded, res)
102
+			}
103
+		})
104
+	}
105
+}

Laden…
Annuleren
Opslaan