-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
203 lines (174 loc) · 7.33 KB
/
server_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
package prekeyserver
import (
"errors"
"testing"
"time"
"github.com/otrv4/gotrx"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type GenericServerSuite struct{}
var _ = Suite(&GenericServerSuite{})
func (s *GenericServerSuite) Test_Handle_ReturnsErrorIfGivenEmptyMessage(c *C) {
gs := &GenericServer{}
msgs, e := gs.Handle("myname", "")
c.Assert(msgs, IsNil)
c.Assert(e, DeepEquals, errors.New("empty message"))
}
type mockMessageHandler struct {
receivedFrom string
receivedMessage []byte
toReturnMessage []byte
toReturnError error
}
func (m *mockMessageHandler) handleMessage(from string, message []byte) ([]byte, error) {
m.receivedFrom = from
m.receivedMessage = message
return m.toReturnMessage, m.toReturnError
}
func (m *mockMessageHandler) handleInnerMessage(from string, message []byte) (serializable, error) {
return nil, nil
}
func (s *GenericServerSuite) Test_Handle_WillPassOnTheIdentityToTheMessageHandler(c *C) {
gs := &GenericServer{
fragmentations: gotrx.NewFragmentor(fragmentationPrefix),
storageImpl: createInMemoryStorage(),
sessions: newSessionManager(),
}
m := &mockMessageHandler{}
gs.messageHandler = m
gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=.")
c.Assert(m.receivedFrom, Equals, "myname")
}
func (s *GenericServerSuite) Test_Handle_WillDecodeBase64EncodedMessage(c *C) {
gs := &GenericServer{
fragmentations: gotrx.NewFragmentor(fragmentationPrefix),
storageImpl: createInMemoryStorage(),
sessions: newSessionManager(),
}
m := &mockMessageHandler{}
gs.messageHandler = m
gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=.")
c.Assert(m.receivedMessage, DeepEquals, []byte("hi, this is not a valid otr4 message, but still..."))
}
func (s *GenericServerSuite) Test_Handle_AMessageWithoutProperFormatSHhouldGenerateAnError(c *C) {
gs := &GenericServer{fragmentations: gotrx.NewFragmentor(fragmentationPrefix)}
m := &mockMessageHandler{}
gs.messageHandler = m
_, e := gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=")
c.Assert(e, DeepEquals, errors.New("invalid message format - missing ending punctuation"))
}
func (s *GenericServerSuite) Test_Handle_ACorruptedBase64MessageGeneratesAnError(c *C) {
gs := &GenericServer{fragmentations: gotrx.NewFragmentor(fragmentationPrefix)}
m := &mockMessageHandler{}
gs.messageHandler = m
_, e := gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4.")
c.Assert(e, DeepEquals, errors.New("invalid message format - corrupted base64 encoding"))
}
func (s *GenericServerSuite) Test_Handle_WillBase64EncodeAndFormatReturnValues(c *C) {
gs := &GenericServer{
fragmentations: gotrx.NewFragmentor(fragmentationPrefix),
storageImpl: createInMemoryStorage(),
sessions: newSessionManager(),
}
m := &mockMessageHandler{
toReturnMessage: []byte("this is our fancy return"),
}
gs.messageHandler = m
msgs, _ := gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=.")
c.Assert(len(msgs), Equals, 1)
c.Assert(msgs[0], Equals, "dGhpcyBpcyBvdXIgZmFuY3kgcmV0dXJu.")
}
func (s *GenericServerSuite) Test_Handle_ReturnsAnErrorFromMessageHandler(c *C) {
gs := &GenericServer{fragmentations: gotrx.NewFragmentor(fragmentationPrefix)}
m := &mockMessageHandler{
toReturnError: errors.New("yipii"),
}
gs.messageHandler = m
msgs, e := gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=.")
c.Assert(msgs, IsNil)
c.Assert(e, DeepEquals, errors.New("yipii"))
}
func (s *GenericServerSuite) Test_Handle_HandlesAFragmentedMessage(c *C) {
gs := &GenericServer{fragmentations: gotrx.NewFragmentor(fragmentationPrefix), storageImpl: createInMemoryStorage(), sessions: newSessionManager()}
m := &mockMessageHandler{
toReturnMessage: []byte("this is our fancy return"),
}
gs.messageHandler = m
msgs, e := gs.Handle("myname", "?OTRP|1234|BEEF|CADE,2,2,dmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=.,")
c.Assert(e, IsNil)
c.Assert(len(msgs), Equals, 0)
msgs, e = gs.Handle("myname", "?OTRP|1234|BEEF|CADE,1,2,aGksIHRoaXMgaXMgbm90IGEg,")
c.Assert(e, IsNil)
c.Assert(len(msgs), Equals, 1)
c.Assert(msgs[0], Equals, "dGhpcyBpcyBvdXIgZmFuY3kgcmV0dXJu.")
}
func (s *GenericServerSuite) Test_Handle_PassesOnAFragmentationError(c *C) {
gs := &GenericServer{fragmentations: gotrx.NewFragmentor(fragmentationPrefix)}
m := &mockMessageHandler{
toReturnMessage: []byte("this is our fancy return"),
}
gs.messageHandler = m
_, e := gs.Handle("myname", "?OTRP|1234|BEEF|CADE,3,2,aGksIHRoaXMgaXMgbm90IGEg,")
c.Assert(e, Not(IsNil))
c.Assert(e, DeepEquals, errors.New("invalid fragmentation parse"))
}
func (s *GenericServerSuite) Test_Handle_WillPotentiallyFragmentReturnValues(c *C) {
gs := &GenericServer{
fragLen: 54,
rand: gotrx.FixtureRand(),
fragmentations: gotrx.NewFragmentor(fragmentationPrefix),
storageImpl: createInMemoryStorage(),
sessions: newSessionManager(),
}
m := &mockMessageHandler{
toReturnMessage: []byte("this is our fancy return"),
}
gs.messageHandler = m
msgs, _ := gs.Handle("myname", "aGksIHRoaXMgaXMgbm90IGEgdmFsaWQgb3RyNCBtZXNzYWdlLCBidXQgc3RpbGwuLi4=.")
c.Assert(msgs, HasLen, 6)
c.Assert(msgs[0], Equals, "?OTRP|2882382797|0000DEAD|0000BEEF,1,6,dGhpcy,")
c.Assert(msgs[1], Equals, "?OTRP|2882382797|0000DEAD|0000BEEF,2,6,BpcyBv,")
c.Assert(msgs[2], Equals, "?OTRP|2882382797|0000DEAD|0000BEEF,3,6,dXIgZm,")
c.Assert(msgs[3], Equals, "?OTRP|2882382797|0000DEAD|0000BEEF,4,6,FuY3kg,")
c.Assert(msgs[4], Equals, "?OTRP|2882382797|0000DEAD|0000BEEF,5,6,cmV0dX,")
c.Assert(msgs[5], Equals, "?OTRP|2882382797|0000DEAD|0000BEEF,6,6,Ju.,")
}
func (s *GenericServerSuite) Test_handleMessage_panicsWhenNoMessageHandlerIsConfigured(c *C) {
gs := &GenericServer{fragLen: 7, rand: gotrx.FixtureRand()}
c.Assert(func() { gs.handleMessage("[email protected]", nil) }, PanicMatches, "programmer error, missing message handler")
}
func (s *GenericServerSuite) Test_sessionComplete_returnsWhenNoSession(c *C) {
gs := &GenericServer{
sessions: newSessionManager(),
}
gs.sessionComplete("[email protected]")
}
func (s *GenericServerSuite) Test_hasSession_returnsFalseWhenNoSessionsExist(c *C) {
gs := &GenericServer{
sessions: newSessionManager(),
}
c.Assert(gs.hasSession("[email protected]"), Equals, false)
}
func (s *GenericServerSuite) Test_cleanupAfter_removesOldSessions(c *C) {
gs := &GenericServer{
sessionTimeout: time.Duration(30) * time.Minute,
fragmentations: gotrx.NewFragmentor(fragmentationPrefix),
storageImpl: createInMemoryStorage(),
sessions: newSessionManager(),
}
gs.session("[email protected]").(*realSession).lastTouched = time.Now().Add(time.Duration(-56) * time.Minute)
gs.session("[email protected]").(*realSession).lastTouched = time.Now().Add(time.Duration(-26) * time.Minute)
gs.cleanupAfter()
c.Assert(gs.hasSession("[email protected]"), Equals, false)
c.Assert(gs.hasSession("[email protected]"), Equals, true)
}
func (s *GenericServerSuite) Test_cleanupAfter_doesntDoAnythingWithEmptySessions(c *C) {
gs := &GenericServer{
sessionTimeout: time.Duration(30) * time.Minute,
fragmentations: gotrx.NewFragmentor(fragmentationPrefix),
storageImpl: createInMemoryStorage(),
sessions: newSessionManager(),
}
gs.cleanupAfter()
}