4 "github.com/stretchr/testify/assert"
10 func TestHandleSetChatSubject(t *testing.T) {
22 name: "sends chat subject to private chat members",
25 UserName: []byte{0x00, 0x01},
27 PrivateChats: map[uint32]*PrivateChat{
30 ClientConn: map[uint16]*ClientConn{
33 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
39 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
46 Clients: map[uint16]*ClientConn{
49 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
55 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
65 Type: []byte{0, 0x6a},
66 ID: []byte{0, 0, 0, 1},
67 ErrorCode: []byte{0, 0, 0, 0},
69 NewField(fieldChatID, []byte{0, 0, 0, 1}),
70 NewField(fieldChatSubject, []byte("Test Subject")),
76 clientID: &[]byte{0, 1},
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},
83 NewField(fieldChatID, []byte{0, 0, 0, 1}),
84 NewField(fieldChatSubject, []byte("Test Subject")),
88 clientID: &[]byte{0, 2},
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},
95 NewField(fieldChatID, []byte{0, 0, 0, 1}),
96 NewField(fieldChatSubject, []byte("Test Subject")),
103 for _, tt := range tests {
104 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
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)
112 if !assert.Equal(t, tt.want, got) {
113 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
119 func TestHandleLeaveChat(t *testing.T) {
131 name: "returns expected transactions",
136 PrivateChats: map[uint32]*PrivateChat{
138 ClientConn: map[uint16]*ClientConn{
141 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
147 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
154 Clients: map[uint16]*ClientConn{
157 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
163 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
170 t: NewTransaction(tranDeleteUser, nil, NewField(fieldChatID, []byte{0, 0, 0, 1})),
174 clientID: &[]byte{0, 1},
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},
181 NewField(fieldChatID, []byte{0, 0, 0, 1}),
182 NewField(fieldUserID, []byte{0, 2}),
189 for _, tt := range tests {
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)
197 if !assert.Equal(t, tt.want, got) {
198 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
204 func TestHandleGetUserNameList(t *testing.T) {
216 name: "replies with userlist transaction",
222 Clients: map[uint16]*ClientConn{
226 Flags: &[]byte{0, 3},
227 UserName: []byte{0, 4},
233 ID: []byte{0, 0, 0, 1},
239 clientID: &[]byte{1, 1},
243 ID: []byte{0, 0, 0, 1},
244 ErrorCode: []byte{0, 0, 0, 0},
247 fieldUsernameWithInfo,
248 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
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)
263 if !reflect.DeepEqual(got, tt.want) {
264 t.Errorf("HandleGetUserNameList() got = %v, want %v", got, tt.want)
270 func TestHandleChatSend(t *testing.T) {
282 name: "sends chat msg transaction to all clients",
285 UserName: []byte{0x00, 0x01},
287 Clients: map[uint16]*ClientConn{
290 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
296 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
305 NewField(fieldData, []byte("hai")),
311 clientID: &[]byte{0, 1},
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},
318 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
322 clientID: &[]byte{0, 2},
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},
329 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
336 name: "sends chat msg as emote if fieldChatOptions is set",
339 UserName: []byte("Testy McTest"),
341 Clients: map[uint16]*ClientConn{
344 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
350 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
359 NewField(fieldData, []byte("performed action")),
360 NewField(fieldChatOptions, []byte{0x00, 0x01}),
366 clientID: &[]byte{0, 1},
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},
373 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
377 clientID: &[]byte{0, 2},
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},
384 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
391 name: "only sends chat msg to clients with accessReadChat permission",
394 UserName: []byte{0x00, 0x01},
396 Clients: map[uint16]*ClientConn{
399 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
405 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
414 NewField(fieldData, []byte("hai")),
420 clientID: &[]byte{0, 1},
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},
427 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
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)
439 if (err != nil) != tt.wantErr {
440 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
443 if !assert.Equal(t, tt.want, got) {
444 t.Errorf("HandleChatSend() got = %v, want %v", got, tt.want)
450 func TestHandleGetFileInfo(t *testing.T) {
451 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
460 wantRes []Transaction
464 name: "returns expected fields when a valid file is requested",
467 ID: &[]byte{0x00, 0x01},
470 FileRoot: "./test/config/Files/",
475 tranGetFileInfo, nil,
476 NewField(fieldFileName, []byte("testfile.txt")),
477 NewField(fieldFilePath, []byte{0x00, 0x00}),
478 //NewField(fieldFilePath, []byte{
482 // 0x74, 0x65, 0x73, 0x74,
485 // 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67,
489 // 0x46, 0x69, 0x6c, 0x65, 73},
493 wantRes: []Transaction{
495 clientID: &[]byte{0, 1},
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},
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}),
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
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)
525 if !assert.Equal(t, tt.wantRes, gotRes) {
526 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)