forked from googlearchive/go-gcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpp_test.go
517 lines (457 loc) · 13.3 KB
/
xmpp_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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package gcm
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/mattn/go-xmpp"
"github.com/kolesa-team/go-gcm/mocks"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func getMsgStr(msg *XMPPMessage) string {
msgData, _ := json.Marshal(msg)
return fmt.Sprintf(stanzaFmtStr, msg.MessageID, string(msgData))
}
var _ = Describe("GCM XMPP Client", func() {
Describe("initializing", func() {
It("should fail to initialize due to connect error", func() {
c, err := newXMPPClient(false, "sender id", "api key", false)
Expect(err).To(HaveOccurred())
Expect(c).To(BeNil())
Expect(err.Error()).To(HavePrefix("error connecting gcm xmpp client: auth failure"))
})
})
Context("initialized", func() {
var (
xm *mocks.XMPPClient
c *gcmXMPP
)
BeforeEach(func() {
xm = new(mocks.XMPPClient)
c = &gcmXMPP{xmppClient: xm, pongs: make(chan struct{}, 100)}
})
AfterEach(func() {
xm.AssertExpectations(GinkgoT())
})
Describe("pinging", func() {
BeforeEach(func() {
//xm.On("ID").Return("id")
})
It("should fail on ping error", func() {
xm.On("PingC2S", "", "").Return(errors.New("Ping"))
err := c.Ping(time.Second)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("Ping"))
})
It("should fail with ping timeout", func() {
xm.On("PingC2S", "", "").Return(nil)
err := c.Ping(100 * time.Millisecond)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("gcm xmpp pong timed out after 100ms"))
})
It("should succeed with pong", func() {
c.pongs <- struct{}{}
xm.On("PingC2S", "", "").Return(nil)
err := c.Ping(100 * time.Millisecond)
Expect(err).To(Succeed())
})
})
Describe("sending", func() {
var (
ack, msg XMPPMessage
ackPayload, msgPayload string
)
BeforeEach(func() {
c.messages = struct {
sync.RWMutex
m map[string]*messageLogEntry
}{
m: make(map[string]*messageLogEntry),
}
ack = XMPPMessage{MessageType: CCSAck, MessageID: "id1"}
ackPayload = getMsgStr(&ack)
var data Data = map[string]interface{}{"key": "value"}
msg = XMPPMessage{MessageID: "id2", Data: data}
msgPayload = getMsgStr(&msg)
})
It("should send ack successfully", func() {
xm.On("SendOrg", ackPayload).Return(100, nil)
id, bytes, err := c.Send(ack)
Expect(err).To(Succeed())
Expect(id).To(Equal("id1"))
Expect(bytes).To(Equal(100))
})
It("should fail sending ack", func() {
xm.On("SendOrg", ackPayload).Return(0, errors.New("SendOrg"))
id, bytes, err := c.Send(ack)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("SendOrg"))
Expect(id).To(Equal("id1"))
Expect(bytes).To(Equal(0))
})
It("should send successfully", func() {
xm.On("SendOrg", msgPayload).Return(100, nil)
id, bytes, err := c.Send(msg)
Expect(err).To(Succeed())
Expect(id).To(Equal("id2"))
Expect(bytes).To(Equal(100))
Expect(len(c.messages.m)).To(Equal(1))
})
It("should fail to send", func() {
xm.On("SendOrg", msgPayload).Return(0, errors.New("Send"))
id, bytes, err := c.Send(msg)
Expect(err).To(HaveOccurred())
Expect(id).To(Equal("id2"))
Expect(err).To(MatchError("Send"))
Expect(bytes).To(Equal(0))
Expect(len(c.messages.m)).To(Equal(0))
})
})
Describe("listening", func() {
Context("recv error", func() {
BeforeEach(func() {
xm.On("Recv").Return("", errors.New("Recv"))
})
It("should return nil when the client is closed", func() {
c.closed = true
err := c.Listen(nil)
Expect(err).To(Succeed())
})
It("should return error when the client is not closed", func() {
err := c.Listen(nil)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("error on Recv: Recv"))
})
})
Context("recv successful", func() {
var (
c1, c2 int
ret interface{}
errCm = CCSMessage{
From: "from",
MessageID: "id1",
MessageType: "error",
Error: "ccs error",
ErrorDescription: "description",
}
errCmStr, ackPayload string
)
f1 := func() interface{} {
if c1 == 0 {
c1++
return ret
}
return nil
}
f2 := func() error {
if c2 == 0 {
c2++
return nil
}
return errors.New("Recv")
}
BeforeEach(func() {
// Recv will be called twice, first time with our payload
// and the second time with error to exit listen routine.
c1 = 0
c2 = 0
xm.On("Recv").Return(f1, f2).Twice()
c.closed = true
data, _ := json.Marshal(&errCm)
errCmStr = string(data)
ack := XMPPMessage{MessageType: CCSAck, MessageID: "id1"}
ackPayload = getMsgStr(&ack)
})
Context("iq stanza", func() {
It("should handle xmpp iq stanza when ping", func() {
ret = xmpp.IQ{Type: "result", ID: "c2s1"}
err := c.Listen(nil)
Expect(err).To(Succeed())
Expect(c.pongs).To(HaveLen(1))
})
It("should skip xmpp iq stanza when not ping", func() {
ret = xmpp.IQ{}
err := c.Listen(nil)
Expect(err).To(Succeed())
})
})
Context("presence stanza", func() {
It("should skip presence stanza", func() {
ret = xmpp.Presence{}
err := c.Listen(nil)
Expect(err).To(Succeed())
})
})
Context("chat stanza", func() {
It("should skip chat stanza with unknown type", func() {
ret = xmpp.Chat{Type: "bogus"}
err := c.Listen(nil)
Expect(err).To(Succeed())
})
Context("empty message type", func() {
It("should skip stanza if ccs message not decoded", func() {
ret = xmpp.Chat{Type: "", Other: []string{"bogus"}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Fail("should not be called")
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
Context("ccs ack", func() {
var (
um = CCSMessage{
MessageID: "id1",
MessageType: CCSAck,
}
umStr string
)
BeforeEach(func() {
d, _ := json.Marshal(&um)
umStr = string(d)
ret = xmpp.Chat{Type: "", Other: []string{string(umStr)}}
})
It("should ignore ack if no such message in the log", func() {
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Fail("should not be called")
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
It("should handle ack if message is found in the log", func() {
xxm := XMPPMessage{MessageID: "id1"}
c.messages.m = make(map[string]*messageLogEntry)
c.messages.m[um.MessageID] = &messageLogEntry{
body: &xxm,
backoff: newExponentialBackoff(),
}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(um))
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
Expect(c.messages.m).To(BeEmpty())
})
})
Context("ccs nack", func() {
var (
um CCSMessage
umStr string
)
BeforeEach(func() {
um = CCSMessage{
MessageID: "id1",
MessageType: CCSNack,
}
umData, _ := json.Marshal(&um)
umStr = string(umData)
ret = xmpp.Chat{Type: "", Other: []string{string(umStr)}}
})
It("should ignore nack if no such message in the log", func() {
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Fail("should not be called")
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
It("should handle nack if message is found in the log", func() {
xxm := XMPPMessage{MessageID: "id1"}
c.messages.m = make(map[string]*messageLogEntry)
c.messages.m[um.MessageID] = &messageLogEntry{
body: &xxm,
backoff: newExponentialBackoff(),
}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(um))
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
Expect(c.messages.m).To(BeEmpty())
})
})
It("should process control message type", func() {
um := CCSMessage{
MessageID: "id1",
MessageType: CCSControl,
}
umStr, _ := json.Marshal(&um)
ret = xmpp.Chat{Type: "", Other: []string{string(umStr)}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(um))
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
It("should process unknown message type", func() {
um := CCSMessage{
MessageID: "id1",
MessageType: "unknown",
}
umStr, _ := json.Marshal(&um)
ret = xmpp.Chat{Type: "", Other: []string{string(umStr)}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(um))
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
})
Context("normal message type", func() {
It("should skip stanza if ccs message not decoded", func() {
ret = xmpp.Chat{Type: "normal", Other: []string{"bogus"}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Fail("should not be called")
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
It("should process receipt", func() {
um := CCSMessage{
MessageID: "id1",
MessageType: CCSReceipt,
}
umStr, _ := json.Marshal(&um)
ret = xmpp.Chat{Type: "normal", Other: []string{string(umStr)}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(um))
return nil
}
ack := XMPPMessage{MessageType: CCSAck, MessageID: "id1"}
ackPayload = getMsgStr(&ack)
xm.On("SendOrg", ackPayload).Return(0, nil)
err := c.Listen(h)
Expect(err).To(Succeed())
})
It("should process unknown message type", func() {
um := CCSMessage{
MessageID: "id1",
MessageType: "unknown",
}
umStr, _ := json.Marshal(&um)
ret = xmpp.Chat{Type: "normal", Other: []string{string(umStr)}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(um))
return nil
}
ack := XMPPMessage{MessageType: CCSAck, MessageID: "id1"}
ackPayload = getMsgStr(&ack)
xm.On("SendOrg", ackPayload).Return(0, nil)
err := c.Listen(h)
Expect(err).To(Succeed())
})
})
Context("error message type", func() {
It("should skip stanza errors if ccs message not decoded", func() {
ret = xmpp.Chat{Type: "error"}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Fail("should not be called")
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
It("should handle stanza errors if ccs message is decoded", func() {
ret = xmpp.Chat{Type: "error", Other: []string{errCmStr}}
h := func(cm CCSMessage) error {
defer GinkgoRecover()
Expect(cm).To(Equal(errCm))
return nil
}
err := c.Listen(h)
Expect(err).To(Succeed())
})
})
})
})
})
Describe("closing", func() {
It("should show if closed", func() {
cc := &gcmXMPP{closed: true}
Expect(cc.IsClosed()).To(BeTrue())
})
Context("not graceful close", func() {
BeforeEach(func() {
//xm.On("ID").Return("id")
})
It("should succeed when internal is successful", func() {
Expect(c.IsClosed()).To(BeFalse())
xm.On("Close").Return(nil)
err := c.Close(false)
Expect(err).To(Succeed())
Expect(c.closed).To(BeTrue())
})
It("should fail when internal fails", func() {
Expect(c.IsClosed()).To(BeFalse())
xm.On("Close").Return(errors.New("Close"))
err := c.Close(false)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("Close"))
})
})
Context("graceful close", func() {
BeforeEach(func() {
c.messages = struct {
sync.RWMutex
m map[string]*messageLogEntry
}{
m: make(map[string]*messageLogEntry),
}
//xm.On("ID").Return("id")
})
It("should succeed when already closed", func() {
xm.On("Close").Return(nil)
err := c.Close(true)
Expect(err).To(Succeed())
// Close again.
err = c.Close(true)
Expect(err).To(Succeed())
})
It("should succeed when all done", func() {
xm.On("Close").Return(nil)
err := c.Close(true)
Expect(err).To(Succeed())
})
It("should succeed with timeout", func() {
xm.On("Close").Return(nil)
c.messages.m["id"] = &messageLogEntry{}
err := c.Close(true)
Expect(err).To(Succeed())
})
})
})
Describe("misc", func() {
It("should return client id", func() {
c := &gcmXMPP{xmppClient: xm}
Expect(c.ID()).To(Equal(fmt.Sprintf("%p", c)))
})
It("should return client jid", func() {
c := &gcmXMPP{xmppClient: xm}
xm.On("JID").Return("jid")
Expect(c.JID()).To(Equal("jid"))
})
It("should return valid xmpp user", func() {
Expect(xmppUser("host", "sender")).To(Equal("sender@host"))
})
})
})
})