-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc_test.go
358 lines (299 loc) · 7.89 KB
/
irc_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
package irc
import (
"crypto/tls"
"math/rand"
"sort"
"testing"
"time"
)
const server = "irc.freenode.net:6667"
const serverssl = "irc.freenode.net:7000"
const channel = "#go-eventirc-test"
const dict = "abcdefghijklmnopqrstuvwxyz"
// Spammy
const verbose_tests = false
const debug_tests = true
func TestConnectionEmtpyServer(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("")
if err == nil {
t.Fatal("emtpy server string not detected")
}
}
func TestConnectionDoubleColon(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("::")
if err == nil {
t.Fatal("wrong number of ':' not detected")
}
}
func TestConnectionMissingHost(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect(":6667")
if err == nil {
t.Fatal("missing host not detected")
}
}
func TestConnectionMissingPort(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("chat.freenode.net:")
if err == nil {
t.Fatal("missing port not detected")
}
}
func TestConnectionNegativePort(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("chat.freenode.net:-1")
if err == nil {
t.Fatal("negative port number not detected")
}
}
func TestConnectionTooLargePort(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("chat.freenode.net:65536")
if err == nil {
t.Fatal("too large port number not detected")
}
}
func TestConnectionMissingLog(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
irccon.Log = nil
err := irccon.Connect("chat.freenode.net:6667")
if err == nil {
t.Fatal("missing 'Log' not detected")
}
}
func TestConnectionEmptyUser(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
// user may be changed after creation
irccon.user = ""
err := irccon.Connect("chat.freenode.net:6667")
if err == nil {
t.Fatal("empty 'user' not detected")
}
}
func TestConnectionEmptyNick(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
// nick may be changed after creation
irccon.nick = ""
err := irccon.Connect("chat.freenode.net:6667")
if err == nil {
t.Fatal("empty 'nick' not detected")
}
}
func TestRemoveCallback(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
debugTest(irccon)
done := make(chan int, 10)
irccon.AddCallback("TEST", func(e *Event) { done <- 1 })
id := irccon.AddCallback("TEST", func(e *Event) { done <- 2 })
irccon.AddCallback("TEST", func(e *Event) { done <- 3 })
// Should remove callback at index 1
irccon.RemoveCallback("TEST", id)
irccon.RunCallbacks(&Event{
Code: "TEST",
})
var results []int
results = append(results, <-done)
results = append(results, <-done)
if !compareResults(results, 1, 3) {
t.Error("Callback 2 not removed")
}
}
func TestWildcardCallback(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
debugTest(irccon)
done := make(chan int, 10)
irccon.AddCallback("TEST", func(e *Event) { done <- 1 })
irccon.AddCallback("*", func(e *Event) { done <- 2 })
irccon.RunCallbacks(&Event{
Code: "TEST",
})
var results []int
results = append(results, <-done)
results = append(results, <-done)
if !compareResults(results, 1, 2) {
t.Error("Wildcard callback not called")
}
}
func TestClearCallback(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
debugTest(irccon)
done := make(chan int, 10)
irccon.AddCallback("TEST", func(e *Event) { done <- 0 })
irccon.AddCallback("TEST", func(e *Event) { done <- 1 })
irccon.ClearCallback("TEST")
irccon.AddCallback("TEST", func(e *Event) { done <- 2 })
irccon.AddCallback("TEST", func(e *Event) { done <- 3 })
irccon.RunCallbacks(&Event{
Code: "TEST",
})
var results []int
results = append(results, <-done)
results = append(results, <-done)
if !compareResults(results, 2, 3) {
t.Error("Callbacks not cleared")
}
}
func TestIRCemptyNick(t *testing.T) {
irccon := IRC("", "go-eventirc")
if irccon != nil {
t.Error("empty nick didn't result in error")
}
}
func TestIRCemptyUser(t *testing.T) {
irccon := IRC("go-eventirc", "")
if irccon != nil {
t.Error("empty user didn't result in error")
}
}
func TestConnection(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
rand.Seed(time.Now().UnixNano())
ircnick1 := randStr(8)
ircnick2 := randStr(8)
irccon1 := IRC(ircnick1, "IRCTest1")
irccon1.PingFreq = time.Second * 3
debugTest(irccon1)
irccon2 := IRC(ircnick2, "IRCTest2")
debugTest(irccon2)
teststr := randStr(20)
testmsgok := make(chan bool, 1)
irccon1.AddCallback("001", func(e *Event) { irccon1.Join(channel) })
irccon2.AddCallback("001", func(e *Event) { irccon2.Join(channel) })
irccon1.AddCallback("366", func(e *Event) {
go func(e *Event) {
tick := time.NewTicker(1 * time.Second)
i := 10
for {
select {
case <-tick.C:
irccon1.Privmsgf(channel, "%s\n", teststr)
if i == 0 {
t.Errorf("Timeout while wating for test message from the other thread.")
return
}
case <-testmsgok:
tick.Stop()
irccon1.Quit()
return
}
i -= 1
}
}(e)
})
irccon2.AddCallback("366", func(e *Event) {
ircnick2 = randStr(8)
irccon2.Nick(ircnick2)
})
irccon2.AddCallback("PRIVMSG", func(e *Event) {
if e.Message() == teststr {
if e.Nick == ircnick1 {
testmsgok <- true
irccon2.Quit()
} else {
t.Errorf("Test message came from an unexpected nickname")
}
} else {
//this may fail if there are other incoming messages, unlikely.
t.Errorf("Test message mismatch")
}
})
irccon2.AddCallback("NICK", func(e *Event) {
if irccon2.nickcurrent == ircnick2 {
t.Errorf("Nick change did not work!")
}
})
err := irccon1.Connect(server)
if err != nil {
t.Log(err.Error())
t.Errorf("Can't connect to freenode.")
}
err = irccon2.Connect(server)
if err != nil {
t.Log(err.Error())
t.Errorf("Can't connect to freenode.")
}
go irccon2.Loop()
irccon1.Loop()
}
func TestReconnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ircnick1 := randStr(8)
irccon := IRC(ircnick1, "IRCTestRe")
irccon.PingFreq = time.Second * 3
debugTest(irccon)
connects := 0
irccon.AddCallback("001", func(e *Event) { irccon.Join(channel) })
irccon.AddCallback("366", func(e *Event) {
connects += 1
if connects > 2 {
irccon.Privmsgf(channel, "Connection nr %d (test done)\n", connects)
go irccon.Quit()
} else {
irccon.Privmsgf(channel, "Connection nr %d\n", connects)
time.Sleep(100) //Need to let the thraed actually send before closing socket
go irccon.Disconnect()
}
})
err := irccon.Connect(server)
if err != nil {
t.Log(err.Error())
t.Errorf("Can't connect to freenode.")
}
irccon.Loop()
if connects != 3 {
t.Errorf("Reconnect test failed. Connects = %d", connects)
}
}
func TestConnectionSSL(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ircnick1 := randStr(8)
irccon := IRC(ircnick1, "IRCTestSSL")
debugTest(irccon)
irccon.UseTLS = true
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
irccon.AddCallback("001", func(e *Event) { irccon.Join(channel) })
irccon.AddCallback("366", func(e *Event) {
irccon.Privmsg(channel, "Test Message from SSL\n")
irccon.Quit()
})
err := irccon.Connect(serverssl)
if err != nil {
t.Log(err.Error())
t.Errorf("Can't connect to freenode.")
}
irccon.Loop()
}
// Helper Functions
func randStr(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = dict[rand.Intn(len(dict))]
}
return string(b)
}
func debugTest(irccon *Connection) *Connection {
irccon.VerboseCallbackHandler = verbose_tests
irccon.Debug = debug_tests
return irccon
}
func compareResults(received []int, desired ...int) bool {
if len(desired) != len(received) {
return false
}
sort.IntSlice(desired).Sort()
sort.IntSlice(received).Sort()
for i := 0; i < len(desired); i++ {
if desired[i] != received[i] {
return false
}
}
return true
}