]> git.r.bdr.sh - rbdr/mobius/blame - hotline/transaction_handlers_test.go
Add keepalive to Client
[rbdr/mobius] / hotline / transaction_handlers_test.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "github.com/stretchr/testify/assert"
5 "math/rand"
6 "reflect"
7 "testing"
8)
9
10func TestHandleSetChatSubject(t *testing.T) {
11 type args struct {
12 cc *ClientConn
13 t *Transaction
14 }
15 tests := []struct {
16 name string
17 args args
18 want []Transaction
19 wantErr bool
20 }{
21 {
22 name: "sends chat subject to private chat members",
23 args: args{
24 cc: &ClientConn{
25 UserName: &[]byte{0x00, 0x01},
26 Server: &Server{
27 PrivateChats: map[uint32]*PrivateChat{
28 uint32(1): {
29 Subject: "unset",
30 ClientConn: map[uint16]*ClientConn{
31 uint16(1): {
32 Account: &Account{
33 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
34 },
35 ID: &[]byte{0, 1},
36 },
37 uint16(2): {
38 Account: &Account{
39 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
40 },
41 ID: &[]byte{0, 2},
42 },
43 },
44 },
45 },
46 Clients: map[uint16]*ClientConn{
47 uint16(1): {
48 Account: &Account{
49 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
50 },
51 ID: &[]byte{0, 1},
52 },
53 uint16(2): {
54 Account: &Account{
55 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
56 },
57 ID: &[]byte{0, 2},
58 },
59 },
60 },
61 },
62 t: &Transaction{
63 Flags: 0x00,
64 IsReply: 0x00,
65 Type: []byte{0, 0x6a},
66 ID: []byte{0, 0, 0, 1},
67 ErrorCode: []byte{0, 0, 0, 0},
68 Fields: []Field{
69 NewField(fieldChatID, []byte{0, 0, 0, 1}),
70 NewField(fieldChatSubject, []byte("Test Subject")),
71 },
72 },
73 },
74 want: []Transaction{
75 {
76 clientID: &[]byte{0, 1},
77 Flags: 0x00,
78 IsReply: 0x00,
79 Type: []byte{0, 0x77},
80 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
81 ErrorCode: []byte{0, 0, 0, 0},
82 Fields: []Field{
83 NewField(fieldChatID, []byte{0, 0, 0, 1}),
84 NewField(fieldChatSubject, []byte("Test Subject")),
85 },
86 },
87 {
88 clientID: &[]byte{0, 2},
89 Flags: 0x00,
90 IsReply: 0x00,
91 Type: []byte{0, 0x77},
92 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
93 ErrorCode: []byte{0, 0, 0, 0},
94 Fields: []Field{
95 NewField(fieldChatID, []byte{0, 0, 0, 1}),
96 NewField(fieldChatSubject, []byte("Test Subject")),
97 },
98 },
99 },
100 wantErr: false,
101 },
102 }
103 for _, tt := range tests {
104 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
105
106 t.Run(tt.name, func(t *testing.T) {
107 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
108 if (err != nil) != tt.wantErr {
109 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
110 return
111 }
112 if !assert.Equal(t, tt.want, got) {
113 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
114 }
115 })
116 }
117}
118
119func TestHandleLeaveChat(t *testing.T) {
120 type args struct {
121 cc *ClientConn
122 t *Transaction
123 }
124 tests := []struct {
125 name string
126 args args
127 want []Transaction
128 wantErr bool
129 }{
130 {
131 name: "returns expected transactions",
132 args: args{
133 cc: &ClientConn{
134 ID: &[]byte{0, 2},
135 Server: &Server{
136 PrivateChats: map[uint32]*PrivateChat{
137 uint32(1): {
138 ClientConn: map[uint16]*ClientConn{
139 uint16(1): {
140 Account: &Account{
141 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
142 },
143 ID: &[]byte{0, 1},
144 },
145 uint16(2): {
146 Account: &Account{
147 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
148 },
149 ID: &[]byte{0, 2},
150 },
151 },
152 },
153 },
154 Clients: map[uint16]*ClientConn{
155 uint16(1): {
156 Account: &Account{
157 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
158 },
159 ID: &[]byte{0, 1},
160 },
161 uint16(2): {
162 Account: &Account{
163 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
164 },
165 ID: &[]byte{0, 2},
166 },
167 },
168 },
169 },
5c34f875 170 t: NewTransaction(tranDeleteUser, nil, NewField(fieldChatID, []byte{0, 0, 0, 1})),
6988a057
JH
171 },
172 want: []Transaction{
173 {
174 clientID: &[]byte{0, 1},
175 Flags: 0x00,
176 IsReply: 0x00,
177 Type: []byte{0, 0x76},
178 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
179 ErrorCode: []byte{0, 0, 0, 0},
180 Fields: []Field{
181 NewField(fieldChatID, []byte{0, 0, 0, 1}),
182 NewField(fieldUserID, []byte{0, 2}),
183 },
184 },
185 },
186 wantErr: false,
187 },
188 }
189 for _, tt := range tests {
190 rand.Seed(1)
191 t.Run(tt.name, func(t *testing.T) {
192 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
193 if (err != nil) != tt.wantErr {
194 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
195 return
196 }
197 if !assert.Equal(t, tt.want, got) {
198 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
199 }
200 })
201 }
202}
203
6988a057
JH
204func TestHandleGetUserNameList(t *testing.T) {
205 type args struct {
206 cc *ClientConn
207 t *Transaction
208 }
209 tests := []struct {
210 name string
211 args args
212 want []Transaction
213 wantErr bool
214 }{
215 {
216 name: "replies with userlist transaction",
217 args: args{
218 cc: &ClientConn{
219
220 ID: &[]byte{1, 1},
221 Server: &Server{
222 Clients: map[uint16]*ClientConn{
223 uint16(1): {
224 ID: &[]byte{0, 1},
225 Icon: &[]byte{0, 2},
226 Flags: &[]byte{0, 3},
227 UserName: &[]byte{0, 4},
228 },
229 },
230 },
231 },
232 t: &Transaction{
233 ID: []byte{0, 0, 0, 1},
234 Type: []byte{0, 1},
235 },
236 },
237 want: []Transaction{
238 {
239 clientID: &[]byte{1, 1},
240 Flags: 0x00,
241 IsReply: 0x01,
242 Type: []byte{0, 1},
243 ID: []byte{0, 0, 0, 1},
244 ErrorCode: []byte{0, 0, 0, 0},
245 Fields: []Field{
246 NewField(
247 fieldUsernameWithInfo,
248 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
249 ),
250 },
251 },
252 },
253 wantErr: false,
254 },
255 }
256 for _, tt := range tests {
257 t.Run(tt.name, func(t *testing.T) {
258 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
259 if (err != nil) != tt.wantErr {
260 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
261 return
262 }
263 if !reflect.DeepEqual(got, tt.want) {
264 t.Errorf("HandleGetUserNameList() got = %v, want %v", got, tt.want)
265 }
266 })
267 }
268}
269
270func TestHandleChatSend(t *testing.T) {
271 type args struct {
272 cc *ClientConn
273 t *Transaction
274 }
275 tests := []struct {
276 name string
277 args args
278 want []Transaction
279 wantErr bool
280 }{
281 {
282 name: "sends chat msg transaction to all clients",
283 args: args{
284 cc: &ClientConn{
285 UserName: &[]byte{0x00, 0x01},
286 Server: &Server{
287 Clients: map[uint16]*ClientConn{
288 uint16(1): {
289 Account: &Account{
290 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
291 },
292 ID: &[]byte{0, 1},
293 },
294 uint16(2): {
295 Account: &Account{
296 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
297 },
298 ID: &[]byte{0, 2},
299 },
300 },
301 },
302 },
303 t: &Transaction{
304 Fields: []Field{
305 NewField(fieldData, []byte("hai")),
306 },
307 },
308 },
309 want: []Transaction{
310 {
311 clientID: &[]byte{0, 1},
312 Flags: 0x00,
313 IsReply: 0x00,
314 Type: []byte{0, 0x6a},
315 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
316 ErrorCode: []byte{0, 0, 0, 0},
317 Fields: []Field{
318 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
319 },
320 },
321 {
322 clientID: &[]byte{0, 2},
323 Flags: 0x00,
324 IsReply: 0x00,
325 Type: []byte{0, 0x6a},
326 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
327 ErrorCode: []byte{0, 0, 0, 0},
328 Fields: []Field{
329 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
330 },
331 },
332 },
333 wantErr: false,
334 },
335 {
336 name: "only sends chat msg to clients with accessReadChat permission",
337 args: args{
338 cc: &ClientConn{
339 UserName: &[]byte{0x00, 0x01},
340 Server: &Server{
341 Clients: map[uint16]*ClientConn{
342 uint16(1): {
343 Account: &Account{
344 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
345 },
346 ID: &[]byte{0, 1},
347 },
348 uint16(2): {
349 Account: &Account{
350 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
351 },
352 ID: &[]byte{0, 2},
353 },
354 },
355 },
356 },
357 t: &Transaction{
358 Fields: []Field{
359 NewField(fieldData, []byte("hai")),
360 },
361 },
362 },
363 want: []Transaction{
364 {
365 clientID: &[]byte{0, 1},
366 Flags: 0x00,
367 IsReply: 0x00,
368 Type: []byte{0, 0x6a},
369 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
370 ErrorCode: []byte{0, 0, 0, 0},
371 Fields: []Field{
372 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
373 },
374 },
375 },
376 wantErr: false,
377 },
378 }
379 for _, tt := range tests {
380 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
381 t.Run(tt.name, func(t *testing.T) {
382 got, err := HandleChatSend(tt.args.cc, tt.args.t)
383
384 if (err != nil) != tt.wantErr {
385 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
386 return
387 }
388 if !assert.Equal(t, tt.want, got) {
389 t.Errorf("HandleChatSend() got = %v, want %v", got, tt.want)
390 }
391 })
392 }
393}