-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevent_test.go
407 lines (351 loc) · 11.9 KB
/
event_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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package girc
import (
"reflect"
"testing"
"unicode/utf8"
)
func mockEvent() *Event {
return &Event{
Source: &Source{Name: "nick", Ident: "user", Host: "host.com"},
Command: "PRIVMSG",
Params: []string{"#channel", "1 2 3"},
}
}
var testsParseSource = []struct {
name string
test string
wantSrc *Source
}{
{name: "full", test: "[email protected]", wantSrc: &Source{
Name: "nick", Ident: "user", Host: "hostname.com",
}},
{name: "special chars", test: "^[][email protected]", wantSrc: &Source{
Name: "^[]nick", Ident: "~user", Host: "test.host---name.com",
}},
{name: "short", test: "a!b@c", wantSrc: &Source{
Name: "a", Ident: "b", Host: "c",
}},
{name: "short", test: "a!b", wantSrc: &Source{
Name: "a", Ident: "b", Host: "",
}},
{name: "short", test: "a@b", wantSrc: &Source{
Name: "a", Ident: "", Host: "b",
}},
{name: "short", test: "test", wantSrc: &Source{
Name: "test", Ident: "", Host: "",
}},
}
func FuzzParseSource(f *testing.F) {
for _, tc := range testsParseSource {
f.Add(tc.test)
}
f.Fuzz(func(t *testing.T, orig string) {
got := ParseSource(orig)
_ = got.IsHostmask()
_ = got.IsServer()
_ = got.Len()
if utf8.ValidString(orig) {
if !utf8.ValidString(got.Host) {
t.Errorf("produced invalid UTF-8 string %q", got.Host)
}
if !utf8.ValidString(got.Ident) {
t.Errorf("produced invalid UTF-8 string %q", got.Ident)
}
if !utf8.ValidString(got.Name) {
t.Errorf("produced invalid UTF-8 string %q", got.Name)
}
if !utf8.ValidString(got.ID()) {
t.Errorf("produced invalid UTF-8 string %q", got.ID())
}
if !utf8.ValidString(got.String()) {
t.Errorf("produced invalid UTF-8 string %q", got.String())
}
if !utf8.Valid(got.Bytes()) {
t.Errorf("produced invalid UTF-8 []byte %q", got.Bytes())
}
}
})
}
func TestParseSource(t *testing.T) {
for _, tt := range testsParseSource {
gotSrc := ParseSource(tt.test)
if !reflect.DeepEqual(gotSrc, tt.wantSrc) {
t.Errorf("ParseSource() = %v, want %v", gotSrc, tt.wantSrc)
}
if gotSrc.Len() != tt.wantSrc.Len() {
t.Errorf("ParseSource().Len() = %v, want %v", gotSrc.Len(), tt.wantSrc.Len())
}
if gotSrc.String() != tt.wantSrc.String() {
t.Errorf("ParseSource().String() = %v, want %v", gotSrc.String(), tt.wantSrc.String())
}
if gotSrc.IsServer() != tt.wantSrc.IsServer() {
t.Errorf("ParseSource().IsServer() = %v, want %v", gotSrc.IsServer(), tt.wantSrc.IsServer())
}
if gotSrc.IsHostmask() != tt.wantSrc.IsHostmask() {
t.Errorf("ParseSource().IsHostmask() = %v, want %v", gotSrc.IsHostmask(), tt.wantSrc.IsHostmask())
}
if !reflect.DeepEqual(gotSrc.Bytes(), tt.wantSrc.Bytes()) {
t.Errorf("ParseSource().Bytes() = %v, want %v", gotSrc, tt.wantSrc)
}
}
}
var testsParseEvent = []struct {
in string
want string
}{
{in: "", want: ""},
{in: ":host.domain.com TEST", want: ":host.domain.com TEST"},
{in: ":host.domain.com TEST\r\n", want: ":host.domain.com TEST"},
{in: ":host.domain.com TEST arg1 arg2", want: ":host.domain.com TEST arg1 arg2"},
{in: ":host.domain.com TEST :", want: ":host.domain.com TEST :"},
{in: ":host.domain.com TEST ::", want: ":host.domain.com TEST ::"},
{in: ":host.domain.com TEST :test1", want: ":host.domain.com TEST test1"},
{in: ":host.domain.com TEST :test:test", want: ":host.domain.com TEST test:test"},
{in: ":host.domain.com TEST :test1 :test", want: ":host.domain.com TEST :test1 :test"},
{in: ":host.domain.com TEST :test1 test2", want: ":host.domain.com TEST :test1 test2"},
{in: ":host.domain.com TEST arg1 arg2 :test1", want: ":host.domain.com TEST arg1 arg2 test1"},
{in: ":host.domain.com TEST arg1 arg=:10 :test1", want: ":host.domain.com TEST arg1 arg=:10 test1"},
{in: ":nick!user@host TEST :test1", want: ":nick!user@host TEST test1"},
{in: ":nick!user@host TEST :test1 test2", want: ":nick!user@host TEST :test1 test2"},
{in: "@aaa=bbb :nick!user@host TEST :test1", want: "@aaa=bbb :nick!user@host TEST test1"},
{in: "@aaa=bbb;+ccc;example.com/ddd=eee :nick!user@host TEST :test1", want: "@aaa=bbb;+ccc;example.com/ddd=eee :nick!user@host TEST test1"},
{in: "@bbb=aaa;aaa :nick!user@host TEST :test1 test2", want: "@aaa;bbb=aaa :nick!user@host TEST :test1 test2"},
}
func FuzzParseEvent(f *testing.F) {
for _, tc := range testsParseEvent {
f.Add(tc.in)
}
for _, tc := range testsIRCDocs {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, orig string) {
got := ParseEvent(orig)
if got == nil {
return
}
_ = got.IsAction()
_ = got.IsFromChannel()
_ = got.IsFromUser()
_ = got.Len()
_, _ = got.IsCTCP()
if utf8.ValidString(orig) {
if !utf8.ValidString(got.Command) {
t.Errorf("produced invalid UTF-8 string %q", got.Command)
}
if !utf8.ValidString(got.Last()) {
t.Errorf("produced invalid UTF-8 string %q", got.Last())
}
if !utf8.ValidString(got.String()) {
t.Errorf("produced invalid UTF-8 string %q", got.String())
}
if !utf8.ValidString(got.StripAction()) {
t.Errorf("produced invalid UTF-8 string %q", got.StripAction())
}
if !utf8.Valid(got.Bytes()) {
t.Errorf("produced invalid UTF-8 []byte %q", got.Bytes())
}
}
})
}
func TestParseEvent(t *testing.T) {
for _, tt := range testsParseEvent {
got := ParseEvent(tt.in)
if got == nil && tt.want == "" {
continue
}
if got == nil {
t.Errorf("ParseEvent: got nil, want: %s", tt.want)
}
if got.String() != tt.want {
if got.Tags != nil {
if len(got.String()) != len(tt.want) {
t.Fatalf("ParseEvent: length exception in tag parse: got %q, want %q", got.String(), tt.want)
}
} else {
t.Fatalf("ParseEvent: got %q, want %q", got.String(), tt.want)
}
}
if got.Len() != len(tt.want) {
if got.Len() > 510 {
continue
}
t.Fatalf("Event.Len: got %d from %q, want %d", got.Len(), got.String(), len(tt.want))
}
}
}
func TestEventCopy(t *testing.T) {
var nilEvent *Event
if event := nilEvent.Copy(); event != nil {
t.Fatalf("Event.Copy: returned non-nil on nil event: %#v", event)
}
msg := "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST arg1 arg2 :test1"
event := ParseEvent(msg)
eventCopy := event.Copy()
if !reflect.DeepEqual(event, eventCopy) {
t.Fatalf("Event.Copy: want %#v, got %#v", event, eventCopy)
}
// Since Event.Copy() calls Source.Copy()...
if !reflect.DeepEqual(event.Source, eventCopy.Source) {
t.Fatalf("Source.Copy: want %#v, got %#v", event.Source, eventCopy.Source)
}
event.Source = nil
if src := event.Source.Copy(); src != nil {
t.Fatalf("Source.Copy: returned non-nil on nil source: %#v", src)
}
}
func TestEventIs(t *testing.T) {
event := ParseEvent(":nick!user@host PRIVMSG #test :\x01ACTION this is a test\x01")
if !event.IsAction() {
t.Fatalf("Event.IsAction: returned false on %#v", event)
}
event.Command = "TEST"
if event.IsAction() {
t.Fatalf("Event.IsAction: returned true though not privmsg; %#v", event)
}
event.Command = "PRIVMSG"
event.Params[len(event.Params)-1] = event.StripAction()
if event.IsAction() || event.Last() != "this is a test" {
t.Fatalf("Event.IsAction: returned true on %#v or trailing is not \"this is a test\": %q", event, event.Last())
}
if !event.IsFromChannel() {
t.Fatalf("Event.IsFromChannel: returned false on %#v", event)
}
event.Command = "TEST"
if event.IsFromChannel() {
t.Fatalf("Event.IsFromChannel: returned true though not privmsg; %#v", event)
}
event.Params[0] = "user1"
if event.IsFromUser() {
t.Fatalf("Event.IsFromUser: returned true when not privmsg; %#v", event)
}
event.Command = "PRIVMSG"
if !event.IsFromUser() {
t.Fatalf("Event.IsFromUser: returned false on %#v", event)
}
}
func TestEventSourceTagEquals(t *testing.T) {
// This should test events themselves, as well as tags and sources.
cases := []struct {
before, after string
equals bool
}{
{
before: ":nick!user@host PRIVMSG #test :This is a test",
after: ":nick!user@host PRIVMSG #test :This is a test",
equals: true,
},
{
before: ":nick!user@host PRIVMSG #test :This is a test",
after: ":nick!user@host1 PRIVMSG #test :This is a test",
equals: false,
},
{
before: ":nick!user@host PRIVMSG #test :This is a test",
after: ":nick!user@host PRIVMSG #tes :This is a test",
equals: false,
},
{
before: "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host PRIVMSG #test :This is a test",
after: "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host PRIVMSG #test :This is a test",
equals: true,
},
{
before: "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host PRIVMSG #test :This is a test",
after: "@aaa=bbb;ccc :nick!user@host PRIVMSG #test :This is a test",
equals: true,
},
{
before: ":nick!user@host PRIVMSG #test :This is a test",
after: "@aaa=bbb;ccc :nick!user@host PRIVMSG #test :This is a test",
equals: true,
},
{
before: "@account=bbb;ccc :nick!user@host PRIVMSG #test :This is a test",
after: "@aaa=bbb;ccc :nick!user@host PRIVMSG #test :This is a test",
equals: false,
},
}
for _, tt := range cases {
before := ParseEvent(tt.before)
after := ParseEvent(tt.after)
equals := before.Equals(after)
// It should be equal the opposite direction too.
if op := after.Equals(before); equals != op {
t.Fatalf("Event.Equals reverse order doesn't match forward order. before: %#v, after: %#v", before, after)
}
if equals != tt.equals {
t.Fatalf("Event.Equals: returned %t (wanted %t) on copied event. before: %#v, after: %#v", equals, tt.equals, before, after)
}
}
}
// // Some of these are pulled from https://github.com/ircdocs/parser-tests.
var testsIRCDocs = []string{
"foo bar baz asdf",
"foo bar baz :asdf",
":src AWAY",
":src AWAY :",
":coolguy foo bar baz asdf",
":coolguy foo bar baz :asdf",
"foo bar baz :asdf quux",
"foo bar baz :",
"foo bar baz ::asdf",
":coolguy foo bar baz :asdf quux",
":coolguy foo bar baz : asdf quux ",
":coolguy PRIVMSG bar :lol :) ",
":coolguy foo bar baz :",
":coolguy foo bar baz : ",
":coolguy foo b\tar baz",
":coolguy foo b\tar :baz",
"@asd :coolguy foo bar baz : ",
"@a=b\\\\and\\nk;d=gh\\:764 foo",
"@d=gh\\:764;a=b\\\\and\\nk foo",
"@a=b\\\\and\\nk;d=gh\\:764 foo par1 par2",
"@a=b\\\\and\\nk;d=gh\\:764 foo par1 :par2",
"@d=gh\\:764;a=b\\\\and\\nk foo par1 par2",
"@d=gh\\:764;a=b\\\\and\\nk foo par1 :par2",
"@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND",
"foo bar baz asdf",
":coolguy foo bar baz asdf",
"foo bar baz :asdf quux",
"foo bar baz :",
"foo bar baz ::asdf",
":coolguy foo bar baz :asdf quux",
":coolguy foo bar baz : asdf quux ",
":coolguy PRIVMSG bar :lol :) ",
":coolguy foo bar baz :",
":coolguy foo bar baz : ",
"@a=b;c=32;k;rt=ql7 foo",
"@a=b\\\\and\\nk;c=72\\s45;d=gh\\:764 foo",
"@c;h=;a=b :quux ab cd",
":src JOIN #chan",
":src JOIN :#chan",
":src AWAY",
":src AWAY ",
":cool\tguy foo bar baz",
":coolguy!ag@net\x035w\x03ork.admin PRIVMSG foo :bar baz",
":coolguy!~ag@n\x02et\x0305w\x0fork.admin PRIVMSG foo :bar baz",
"@tag1=value1;tag2;vendor1/tag3=value2;vendor2/tag4= :irc.example.com COMMAND param1 param2 :param3 param3",
":irc.example.com COMMAND param1 param2 :param3 param3",
"@tag1=value1;tag2;vendor1/tag3=value2;vendor2/tag4 COMMAND param1 param2 :param3 param3",
"COMMAND",
"@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND",
":gravel.mozilla.org 432 #momo :Erroneous Nickname: Illegal characters",
":gravel.mozilla.org MODE #tckk +n ",
":services.esper.net MODE #foo-bar +o foobar ",
"@tag1=value\\\\ntest COMMAND",
"@tag1=value\\1 COMMAND",
"@tag1=value1\\ COMMAND",
"@tag1=1;tag2=3;tag3=4;tag1=5 COMMAND",
"@tag1=1;tag2=3;tag3=4;tag1=5;vendor/tag2=8 COMMAND",
":SomeOp MODE #channel :+i",
":SomeOp MODE #channel +oo SomeUser :AnotherUser",
}
func TestEventIRCDocsParseTests(t *testing.T) {
for _, tt := range testsIRCDocs {
// Basic test to just verify it doesn't panic.
_ = ParseEvent(tt)
}
}