forked from go-irc/irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
445 lines (363 loc) · 9.21 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package irc
import (
"testing"
"github.com/stretchr/testify/assert"
)
var messageTests = []struct {
// Message parsing
Prefix, Cmd string
Params []string
// Tag parsing
Tags Tags
// Prefix parsing
Name, User, Host string
// Total output
Expect string
ExpectIn []string
Err error
// FromChannel
FromChan bool
}{
{ // Empty message should error
Err: ErrZeroLengthMessage,
},
{ // Make sure we've got a command
Expect: ":asd :",
Err: ErrMissingCommand,
},
{ // Need data after tags
Expect: "@A",
Err: ErrMissingDataAfterTags,
},
{ // Need data after prefix
Expect: ":A",
Err: ErrMissingDataAfterPrefix,
},
{ // Basic prefix test
Prefix: "server.kevlar.net",
Cmd: "PING",
Params: []string{},
Name: "server.kevlar.net",
Expect: ":server.kevlar.net PING\n",
},
{ // Trailing argument test
Prefix: "server.kevlar.net",
Cmd: "NOTICE",
Params: []string{"user", "*** This is a test"},
Name: "server.kevlar.net",
Expect: ":server.kevlar.net NOTICE user :*** This is a test\n",
},
{ // Full prefix test
Prefix: "[email protected]",
Cmd: "PRIVMSG",
Params: []string{"#somewhere", "*** This is a test"},
Name: "belakA",
User: "belakB",
Host: "a.host.com",
Expect: ":[email protected] PRIVMSG #somewhere :*** This is a test\n",
FromChan: true,
},
{ // Test : in the middle of a param
Prefix: "freenode",
Cmd: "005",
Params: []string{"starkbot", "CHANLIMIT=#:120", "MORE", "are supported by this server"},
Name: "freenode",
Expect: ":freenode 005 starkbot CHANLIMIT=#:120 MORE :are supported by this server\n",
},
{ // Test FromChannel on a different channel prefix
Prefix: "[email protected]",
Cmd: "PRIVMSG",
Params: []string{"&somewhere", "*** This is a test"},
Name: "belakA",
User: "belakB",
Host: "a.host.com",
Expect: ":[email protected] PRIVMSG &somewhere :*** This is a test\n",
FromChan: true,
},
{ // Test FromChannel on a single user
Prefix: "[email protected]",
Cmd: "PRIVMSG",
Params: []string{"belak", "*** This is a test"},
Name: "belakA",
User: "belakB",
Host: "a.host.com",
Expect: ":[email protected] PRIVMSG belak :*** This is a test\n",
},
{ // Simple message
Cmd: "B",
Params: []string{"C"},
Expect: "B C\n",
},
{ // Simple message with tags
Prefix: "A@B",
Cmd: "C",
Params: []string{"D"},
Name: "A",
Host: "B",
Expect: ":A@B C D\n",
},
{ // Simple message with prefix
Prefix: "A",
Cmd: "B",
Params: []string{"C"},
Name: "A",
Expect: ":A B C\n",
},
{ // Message with prefix and multiple params
Prefix: "A",
Cmd: "B",
Params: []string{"C", "D"},
Name: "A",
Expect: ":A B C D\n",
},
{ // Message with empty trailing
Cmd: "A",
Params: []string{""},
Expect: "A :\n",
},
{ // Test basic tag parsing
Tags: Tags{
"tag": "value",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=value A\n",
},
{ // Escaped \n in tag
Tags: Tags{
"tag": "\n",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=\\n A\n",
},
{ // Escaped \ in tag
Tags: Tags{
"tag": "\\",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=\\ A\n",
ExpectIn: []string{"@tag=\\\\ A\n"},
},
{ // Escaped ; in tag
Tags: Tags{
"tag": ";",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=\\: A\n",
},
{ // Empty tag
Tags: Tags{
"tag": "",
},
Params: []string{},
Cmd: "A",
Expect: "@tag A\n",
},
{ // Escaped & in tag
Tags: Tags{
"tag": "\\&",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=\\& A\n",
ExpectIn: []string{"@tag=\\\\& A\n"},
},
{ // Multiple simple tags
Tags: Tags{
"tag": "x",
"tag2": "asd",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=x;tag2=asd A\n",
ExpectIn: []string{"@tag=x;tag2=asd A\n", "@tag2=asd;tag=x A\n"},
},
{ // Complicated escaped tag
Tags: Tags{
"tag": "; \\\r\n",
},
Params: []string{},
Cmd: "A",
Expect: "@tag=\\:\\s\\\\\\r\\n A\n",
},
{ // Tags example from the spec
Tags: Tags{
"aaa": "bbb",
"ccc": "",
"example.com/ddd": "eee",
},
Name: "nick",
User: "ident",
Host: "host.com",
Prefix: "[email protected]",
Cmd: "PRIVMSG",
Params: []string{"me", "Hello"},
Expect: "@aaa=bbb;ccc;example.com/ddd=eee :[email protected] PRIVMSG me :Hello\n",
ExpectIn: []string{
"@aaa=bbb;ccc;example.com/ddd=eee :[email protected] PRIVMSG me Hello\n",
"@aaa=bbb;example.com/ddd=eee;ccc :[email protected] PRIVMSG me Hello\n",
"@ccc;aaa=bbb;example.com/ddd=eee :[email protected] PRIVMSG me Hello\n",
"@ccc;example.com/ddd=eee;aaa=bbb :[email protected] PRIVMSG me Hello\n",
"@example.com/ddd=eee;aaa=bbb;ccc :[email protected] PRIVMSG me Hello\n",
"@example.com/ddd=eee;ccc;aaa=bbb :[email protected] PRIVMSG me Hello\n",
},
},
{ // = in tag
Tags: Tags{
"a": "a=a",
},
Name: "nick",
User: "ident",
Host: "host.com",
Prefix: "[email protected]",
Cmd: "PRIVMSG",
Params: []string{"me", "Hello"},
Expect: "@a=a=a :[email protected] PRIVMSG me :Hello\n",
ExpectIn: []string{"@a=a=a :[email protected] PRIVMSG me Hello\n"},
},
}
func TestMustParseMessage(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
if test.Err != nil {
assert.Panics(t, func() {
MustParseMessage(test.Expect)
}, "%d. Didn't get expected panic", i)
} else {
assert.NotPanics(t, func() {
MustParseMessage(test.Expect)
}, "%d. Got unexpected panic", i)
}
}
}
func TestParseMessage(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
m, err := ParseMessage(test.Expect)
if test.Err != nil {
assert.Equal(t, test.Err, err, "%d. Didn't get correct error for invalid message.", i)
} else {
assert.NotNil(t, m, "%d. Got nil for valid message.", i)
}
if m == nil {
continue
}
assert.Equal(t, test.Cmd, m.Command, "%d. Command doesn't match.", i)
assert.EqualValues(t, test.Params, m.Params, "%d. Params don't match.", i)
}
}
func BenchmarkParseMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseMessage(messageTests[i%len(messageTests)].Prefix)
}
}
func TestParsePrefix(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
pi := ParsePrefix(test.Prefix)
if pi == nil {
t.Errorf("%d. Got nil for valid identity", i)
continue
}
assert.EqualValues(t, &Prefix{
Name: test.Name,
User: test.User,
Host: test.Host,
}, pi, "%d. Identity did not match", i)
}
}
func BenchmarkParsePrefix(b *testing.B) {
for i := 0; i < b.N; i++ {
ParsePrefix(messageTests[i%len(messageTests)].Expect)
}
}
func TestMessageTrailing(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
if test.Err != nil {
continue
}
m, _ := ParseMessage(test.Expect)
tr := m.Trailing()
if len(test.Params) < 1 {
assert.Equal(t, "", tr, "%d. Expected empty trailing", i)
} else {
assert.Equal(t, test.Params[len(test.Params)-1], tr, "%d. Expected matching traling", i)
}
}
}
func TestMessageFromChan(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
if test.Err != nil {
continue
}
m, _ := ParseMessage(test.Expect)
assert.Equal(t, test.FromChan, m.FromChannel(), "%d. Wrong FromChannel value", i)
}
}
func TestMessageCopy(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
if test.Err != nil {
continue
}
m, _ := ParseMessage(test.Expect)
c := m.Copy()
assert.EqualValues(t, m, c, "%d. Copied values are not equal", i)
if len(m.Tags) > 0 {
c = m.Copy()
for k := range c.Tags {
c.Tags[k] += "junk"
}
assert.False(t, assert.ObjectsAreEqualValues(m, c), "%d. Copied with modified tags should not match", i)
}
c = m.Copy()
c.Prefix.Name += "junk"
assert.False(t, assert.ObjectsAreEqualValues(m, c), "%d. Copied with modified identity should not match", i)
c = m.Copy()
c.Params = append(c.Params, "junk")
assert.False(t, assert.ObjectsAreEqualValues(m, c), "%d. Copied with additional params should not match", i)
}
// The message itself doesn't matter, we just need to make sure we
// don't error if the user does something crazy and makes Params
// nil.
m, _ := ParseMessage("PING :hello world")
m.Prefix = nil
c := m.Copy()
assert.EqualValues(t, m, c, "nil prefix copy failed")
}
func TestMessageString(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
if test.Err != nil {
continue
}
m, _ := ParseMessage(test.Expect)
if test.ExpectIn != nil {
assert.Contains(t, test.ExpectIn, m.String()+"\n", "%d. Message Stringification failed", i)
} else {
assert.Equal(t, test.Expect, m.String()+"\n", "%d. Message Stringification failed", i)
}
}
}
func TestMessageTags(t *testing.T) {
t.Parallel()
for i, test := range messageTests {
if test.Err != nil || test.Tags == nil {
continue
}
m, _ := ParseMessage(test.Expect)
assert.EqualValues(t, test.Tags, m.Tags, "%d. Tag parsing failed", i)
// Ensure we have all the tags we expected.
for k, v := range test.Tags {
tag, ok := m.GetTag(k)
assert.True(t, ok, "%d. Missing tag %q", i, k)
assert.EqualValues(t, v, tag, "%d. Wrong tag value", i)
}
assert.EqualValues(t, test.Tags, m.Tags, "%d. Tags don't match", i)
}
}