]> git.r.bdr.sh - rbdr/mobius/blame - hotline/transaction_handlers_test.go
More cleanup
[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{
72dd37f1 25 UserName: []byte{0x00, 0x01},
6988a057
JH
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},
72dd37f1 227 UserName: []byte{0, 4},
6988a057
JH
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{
72dd37f1 285 UserName: []byte{0x00, 0x01},
6988a057
JH
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 },
72dd37f1
JH
335 {
336 name: "sends chat msg as emote if fieldChatOptions is set",
337 args: args{
338 cc: &ClientConn{
339 UserName: []byte("Testy McTest"),
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{255, 255, 255, 255, 255, 255, 255, 255},
351 },
352 ID: &[]byte{0, 2},
353 },
354 },
355 },
356 },
357 t: &Transaction{
358 Fields: []Field{
359 NewField(fieldData, []byte("performed action")),
360 NewField(fieldChatOptions, []byte{0x00, 0x01}),
361 },
362 },
363 },
364 want: []Transaction{
365 {
366 clientID: &[]byte{0, 1},
367 Flags: 0x00,
368 IsReply: 0x00,
369 Type: []byte{0, 0x6a},
370 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
371 ErrorCode: []byte{0, 0, 0, 0},
372 Fields: []Field{
373 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
374 },
375 },
376 {
377 clientID: &[]byte{0, 2},
378 Flags: 0x00,
379 IsReply: 0x00,
380 Type: []byte{0, 0x6a},
381 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
382 ErrorCode: []byte{0, 0, 0, 0},
383 Fields: []Field{
384 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
385 },
386 },
387 },
388 wantErr: false,
389 },
6988a057
JH
390 {
391 name: "only sends chat msg to clients with accessReadChat permission",
392 args: args{
393 cc: &ClientConn{
72dd37f1 394 UserName: []byte{0x00, 0x01},
6988a057
JH
395 Server: &Server{
396 Clients: map[uint16]*ClientConn{
397 uint16(1): {
398 Account: &Account{
399 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
400 },
401 ID: &[]byte{0, 1},
402 },
403 uint16(2): {
404 Account: &Account{
405 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
406 },
407 ID: &[]byte{0, 2},
408 },
409 },
410 },
411 },
412 t: &Transaction{
413 Fields: []Field{
414 NewField(fieldData, []byte("hai")),
415 },
416 },
417 },
418 want: []Transaction{
419 {
420 clientID: &[]byte{0, 1},
421 Flags: 0x00,
422 IsReply: 0x00,
423 Type: []byte{0, 0x6a},
424 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
425 ErrorCode: []byte{0, 0, 0, 0},
426 Fields: []Field{
427 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
428 },
429 },
430 },
431 wantErr: false,
432 },
433 }
434 for _, tt := range tests {
435 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
436 t.Run(tt.name, func(t *testing.T) {
437 got, err := HandleChatSend(tt.args.cc, tt.args.t)
438
439 if (err != nil) != tt.wantErr {
440 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
441 return
442 }
443 if !assert.Equal(t, tt.want, got) {
444 t.Errorf("HandleChatSend() got = %v, want %v", got, tt.want)
445 }
446 })
447 }
448}
72dd37f1
JH
449
450func TestHandleGetFileInfo(t *testing.T) {
451 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
452
453 type args struct {
454 cc *ClientConn
455 t *Transaction
456 }
457 tests := []struct {
458 name string
459 args args
460 wantRes []Transaction
461 wantErr bool
462 }{
463 {
464 name: "returns expected fields when a valid file is requested",
465 args: args{
466 cc: &ClientConn{
467 ID: &[]byte{0x00, 0x01},
468 Server: &Server{
469 Config: &Config{
470 FileRoot: "./test/config/Files/",
471 },
472 },
473 },
474 t: NewTransaction(
475 tranGetFileInfo, nil,
476 NewField(fieldFileName, []byte("testfile.txt")),
477 NewField(fieldFilePath, []byte{0x00, 0x00}),
478 //NewField(fieldFilePath, []byte{
479 // 0x00, 0x03,
480 // 0x00, 0x00,
481 // 0x04,
482 // 0x74, 0x65, 0x73, 0x74,
483 // 0x00, 0x00,
484 // 0x06,
485 // 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67,
486 //
487 // 0x00, 0x00,
488 // 0x05,
489 // 0x46, 0x69, 0x6c, 0x65, 73},
490 //),
491 ),
492 },
493 wantRes: []Transaction{
494 {
495 clientID: &[]byte{0, 1},
496 Flags: 0x00,
497 IsReply: 0x01,
498 Type: []byte{0, 0xce},
499 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
500 ErrorCode: []byte{0, 0, 0, 0},
501 Fields: []Field{
502 NewField(fieldFileName, []byte("testfile.txt")),
503 NewField(fieldFileTypeString, []byte("TEXT")),
504 NewField(fieldFileCreatorString, []byte("TTXT")),
505 NewField(fieldFileComment, []byte("TODO")),
506 NewField(fieldFileType, []byte("TEXT")),
507 NewField(fieldFileCreateDate, []byte{0x07, 0x70, 0x00, 0x00, 0xba, 0x74, 0x24, 0x73}),
508 NewField(fieldFileModifyDate, []byte{0x07, 0x70, 0x00, 0x00, 0xba, 0x74, 0x24, 0x73}),
509 NewField(fieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}),
510 },
511 },
512 },
513 wantErr: false,
514 },
515 }
516 for _, tt := range tests {
517 t.Run(tt.name, func(t *testing.T) {
518 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
519
520 gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t)
521 if (err != nil) != tt.wantErr {
522 t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr)
523 return
524 }
525 if !assert.Equal(t, tt.wantRes, gotRes) {
526 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)
527 }
528 })
529 }
530}