6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/mock"
17 func TestHandleSetChatSubject(t *testing.T) {
29 name: "sends chat subject to private chat members",
32 UserName: []byte{0x00, 0x01},
34 PrivateChats: map[uint32]*PrivateChat{
37 ClientConn: map[uint16]*ClientConn{
40 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
46 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
53 Clients: map[uint16]*ClientConn{
56 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
62 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
72 Type: []byte{0, 0x6a},
73 ID: []byte{0, 0, 0, 1},
74 ErrorCode: []byte{0, 0, 0, 0},
76 NewField(FieldChatID, []byte{0, 0, 0, 1}),
77 NewField(FieldChatSubject, []byte("Test Subject")),
83 clientID: &[]byte{0, 1},
86 Type: []byte{0, 0x77},
87 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
88 ErrorCode: []byte{0, 0, 0, 0},
90 NewField(FieldChatID, []byte{0, 0, 0, 1}),
91 NewField(FieldChatSubject, []byte("Test Subject")),
95 clientID: &[]byte{0, 2},
98 Type: []byte{0, 0x77},
99 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
100 ErrorCode: []byte{0, 0, 0, 0},
102 NewField(FieldChatID, []byte{0, 0, 0, 1}),
103 NewField(FieldChatSubject, []byte("Test Subject")),
110 for _, tt := range tests {
111 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
113 t.Run(tt.name, func(t *testing.T) {
114 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
115 if (err != nil) != tt.wantErr {
116 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
119 if !assert.Equal(t, tt.want, got) {
120 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
126 func TestHandleLeaveChat(t *testing.T) {
138 name: "returns expected transactions",
143 PrivateChats: map[uint32]*PrivateChat{
145 ClientConn: map[uint16]*ClientConn{
148 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
154 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
161 Clients: map[uint16]*ClientConn{
164 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
170 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
177 t: NewTransaction(TranDeleteUser, nil, NewField(FieldChatID, []byte{0, 0, 0, 1})),
181 clientID: &[]byte{0, 1},
184 Type: []byte{0, 0x76},
185 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
186 ErrorCode: []byte{0, 0, 0, 0},
188 NewField(FieldChatID, []byte{0, 0, 0, 1}),
189 NewField(FieldUserID, []byte{0, 2}),
196 for _, tt := range tests {
198 t.Run(tt.name, func(t *testing.T) {
199 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
200 if (err != nil) != tt.wantErr {
201 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
204 if !assert.Equal(t, tt.want, got) {
205 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
211 func TestHandleGetUserNameList(t *testing.T) {
223 name: "replies with userlist transaction",
229 Clients: map[uint16]*ClientConn{
234 UserName: []byte{0, 4},
240 UserName: []byte{0, 4},
246 ID: []byte{0, 0, 0, 1},
252 clientID: &[]byte{1, 1},
256 ID: []byte{0, 0, 0, 1},
257 ErrorCode: []byte{0, 0, 0, 0},
260 FieldUsernameWithInfo,
261 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
264 FieldUsernameWithInfo,
265 []byte{00, 02, 00, 02, 00, 03, 00, 02, 00, 04},
273 for _, tt := range tests {
274 t.Run(tt.name, func(t *testing.T) {
275 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
276 if (err != nil) != tt.wantErr {
277 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
280 assert.Equal(t, tt.want, got)
285 func TestHandleChatSend(t *testing.T) {
297 name: "sends chat msg transaction to all clients",
301 Access: func() accessBitmap {
302 var bits accessBitmap
303 bits.Set(accessSendChat)
307 UserName: []byte{0x00, 0x01},
309 Clients: map[uint16]*ClientConn{
312 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
318 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
327 NewField(FieldData, []byte("hai")),
333 clientID: &[]byte{0, 1},
336 Type: []byte{0, 0x6a},
337 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
338 ErrorCode: []byte{0, 0, 0, 0},
340 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
344 clientID: &[]byte{0, 2},
347 Type: []byte{0, 0x6a},
348 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
349 ErrorCode: []byte{0, 0, 0, 0},
351 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
358 name: "treats Chat ID 00 00 00 00 as a public chat message",
362 Access: func() accessBitmap {
363 var bits accessBitmap
364 bits.Set(accessSendChat)
368 UserName: []byte{0x00, 0x01},
370 Clients: map[uint16]*ClientConn{
373 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
379 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
388 NewField(FieldData, []byte("hai")),
389 NewField(FieldChatID, []byte{0, 0, 0, 0}),
395 clientID: &[]byte{0, 1},
398 Type: []byte{0, 0x6a},
399 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
400 ErrorCode: []byte{0, 0, 0, 0},
402 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
406 clientID: &[]byte{0, 2},
409 Type: []byte{0, 0x6a},
410 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
411 ErrorCode: []byte{0, 0, 0, 0},
413 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
420 name: "when user does not have required permission",
424 Access: func() accessBitmap {
425 var bits accessBitmap
430 Accounts: map[string]*Account{},
434 TranChatSend, &[]byte{0, 1},
435 NewField(FieldData, []byte("hai")),
443 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
444 ErrorCode: []byte{0, 0, 0, 1},
446 NewField(FieldError, []byte("You are not allowed to participate in chat.")),
453 name: "sends chat msg as emote if FieldChatOptions is set to 1",
457 Access: func() accessBitmap {
458 var bits accessBitmap
459 bits.Set(accessSendChat)
463 UserName: []byte("Testy McTest"),
465 Clients: map[uint16]*ClientConn{
468 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
474 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
483 NewField(FieldData, []byte("performed action")),
484 NewField(FieldChatOptions, []byte{0x00, 0x01}),
490 clientID: &[]byte{0, 1},
493 Type: []byte{0, 0x6a},
494 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
495 ErrorCode: []byte{0, 0, 0, 0},
497 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
501 clientID: &[]byte{0, 2},
504 Type: []byte{0, 0x6a},
505 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
506 ErrorCode: []byte{0, 0, 0, 0},
508 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
515 name: "does not send chat msg as emote if FieldChatOptions is set to 0",
519 Access: func() accessBitmap {
520 var bits accessBitmap
521 bits.Set(accessSendChat)
525 UserName: []byte("Testy McTest"),
527 Clients: map[uint16]*ClientConn{
530 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
536 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
545 NewField(FieldData, []byte("hello")),
546 NewField(FieldChatOptions, []byte{0x00, 0x00}),
552 clientID: &[]byte{0, 1},
555 Type: []byte{0, 0x6a},
556 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
557 ErrorCode: []byte{0, 0, 0, 0},
559 NewField(FieldData, []byte("\r Testy McTest: hello")),
563 clientID: &[]byte{0, 2},
566 Type: []byte{0, 0x6a},
567 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
568 ErrorCode: []byte{0, 0, 0, 0},
570 NewField(FieldData, []byte("\r Testy McTest: hello")),
577 name: "only sends chat msg to clients with accessReadChat permission",
581 Access: func() accessBitmap {
582 var bits accessBitmap
583 bits.Set(accessSendChat)
587 UserName: []byte{0x00, 0x01},
589 Clients: map[uint16]*ClientConn{
592 Access: func() accessBitmap {
593 var bits accessBitmap
594 bits.Set(accessReadChat)
601 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
610 NewField(FieldData, []byte("hai")),
616 clientID: &[]byte{0, 1},
619 Type: []byte{0, 0x6a},
620 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
621 ErrorCode: []byte{0, 0, 0, 0},
623 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
630 name: "only sends private chat msg to members of private chat",
634 Access: func() accessBitmap {
635 var bits accessBitmap
636 bits.Set(accessSendChat)
640 UserName: []byte{0x00, 0x01},
642 PrivateChats: map[uint32]*PrivateChat{
644 ClientConn: map[uint16]*ClientConn{
654 Clients: map[uint16]*ClientConn{
657 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
663 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
669 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
678 NewField(FieldData, []byte("hai")),
679 NewField(FieldChatID, []byte{0, 0, 0, 1}),
685 clientID: &[]byte{0, 1},
688 Type: []byte{0, 0x6a},
689 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
690 ErrorCode: []byte{0, 0, 0, 0},
692 NewField(FieldChatID, []byte{0, 0, 0, 1}),
693 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
697 clientID: &[]byte{0, 2},
700 Type: []byte{0, 0x6a},
701 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
702 ErrorCode: []byte{0, 0, 0, 0},
704 NewField(FieldChatID, []byte{0, 0, 0, 1}),
705 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
712 for _, tt := range tests {
713 t.Run(tt.name, func(t *testing.T) {
714 got, err := HandleChatSend(tt.args.cc, tt.args.t)
716 if (err != nil) != tt.wantErr {
717 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
720 tranAssertEqual(t, tt.want, got)
725 func TestHandleGetFileInfo(t *testing.T) {
726 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
735 wantRes []Transaction
739 name: "returns expected fields when a valid file is requested",
742 ID: &[]byte{0x00, 0x01},
746 FileRoot: func() string {
747 path, _ := os.Getwd()
748 return filepath.Join(path, "/test/config/Files")
754 TranGetFileInfo, nil,
755 NewField(FieldFileName, []byte("testfile.txt")),
756 NewField(FieldFilePath, []byte{0x00, 0x00}),
759 wantRes: []Transaction{
761 clientID: &[]byte{0, 1},
765 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
766 ErrorCode: []byte{0, 0, 0, 0},
768 NewField(FieldFileName, []byte("testfile.txt")),
769 NewField(FieldFileTypeString, []byte("Text File")),
770 NewField(FieldFileCreatorString, []byte("ttxt")),
771 NewField(FieldFileType, []byte("TEXT")),
772 NewField(FieldFileCreateDate, make([]byte, 8)),
773 NewField(FieldFileModifyDate, make([]byte, 8)),
774 NewField(FieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}),
781 for _, tt := range tests {
782 t.Run(tt.name, func(t *testing.T) {
783 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
785 gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t)
786 if (err != nil) != tt.wantErr {
787 t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr)
791 // Clear the fileWrapper timestamp fields to work around problems running the tests in multiple timezones
792 // TODO: revisit how to test this by mocking the stat calls
793 gotRes[0].Fields[4].Data = make([]byte, 8)
794 gotRes[0].Fields[5].Data = make([]byte, 8)
795 if !assert.Equal(t, tt.wantRes, gotRes) {
796 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)
802 func TestHandleNewFolder(t *testing.T) {
810 wantRes []Transaction
814 name: "without required permission",
818 Access: func() accessBitmap {
819 var bits accessBitmap
829 wantRes: []Transaction{
834 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
835 ErrorCode: []byte{0, 0, 0, 1},
837 NewField(FieldError, []byte("You are not allowed to create folders.")),
844 name: "when path is nested",
848 Access: func() accessBitmap {
849 var bits accessBitmap
850 bits.Set(accessCreateFolder)
859 FS: func() *MockFileStore {
860 mfs := &MockFileStore{}
861 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
862 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
868 TranNewFolder, &[]byte{0, 1},
869 NewField(FieldFileName, []byte("testFolder")),
870 NewField(FieldFilePath, []byte{
878 wantRes: []Transaction{
880 clientID: &[]byte{0, 1},
884 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
885 ErrorCode: []byte{0, 0, 0, 0},
891 name: "when path is not nested",
895 Access: func() accessBitmap {
896 var bits accessBitmap
897 bits.Set(accessCreateFolder)
906 FS: func() *MockFileStore {
907 mfs := &MockFileStore{}
908 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
909 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
915 TranNewFolder, &[]byte{0, 1},
916 NewField(FieldFileName, []byte("testFolder")),
919 wantRes: []Transaction{
921 clientID: &[]byte{0, 1},
925 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
926 ErrorCode: []byte{0, 0, 0, 0},
932 name: "when Write returns an err",
936 Access: func() accessBitmap {
937 var bits accessBitmap
938 bits.Set(accessCreateFolder)
947 FS: func() *MockFileStore {
948 mfs := &MockFileStore{}
949 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
950 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
956 TranNewFolder, &[]byte{0, 1},
957 NewField(FieldFileName, []byte("testFolder")),
958 NewField(FieldFilePath, []byte{
963 wantRes: []Transaction{},
967 name: "FieldFileName does not allow directory traversal",
971 Access: func() accessBitmap {
972 var bits accessBitmap
973 bits.Set(accessCreateFolder)
982 FS: func() *MockFileStore {
983 mfs := &MockFileStore{}
984 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
985 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
991 TranNewFolder, &[]byte{0, 1},
992 NewField(FieldFileName, []byte("../../testFolder")),
995 wantRes: []Transaction{
997 clientID: &[]byte{0, 1},
1001 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
1002 ErrorCode: []byte{0, 0, 0, 0},
1007 name: "FieldFilePath does not allow directory traversal",
1011 Access: func() accessBitmap {
1012 var bits accessBitmap
1013 bits.Set(accessCreateFolder)
1020 FileRoot: "/Files/",
1022 FS: func() *MockFileStore {
1023 mfs := &MockFileStore{}
1024 mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
1025 mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
1031 TranNewFolder, &[]byte{0, 1},
1032 NewField(FieldFileName, []byte("testFolder")),
1033 NewField(FieldFilePath, []byte{
1044 wantRes: []Transaction{
1046 clientID: &[]byte{0, 1},
1050 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
1051 ErrorCode: []byte{0, 0, 0, 0},
1056 for _, tt := range tests {
1057 t.Run(tt.name, func(t *testing.T) {
1058 gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
1059 if (err != nil) != tt.wantErr {
1060 t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
1064 if !tranAssertEqual(t, tt.wantRes, gotRes) {
1065 t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
1071 func TestHandleUploadFile(t *testing.T) {
1079 wantRes []Transaction
1083 name: "when request is valid and user has Upload Anywhere permission",
1088 fileTransfers: map[[4]byte]*FileTransfer{},
1090 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1092 transfers: map[int]map[[4]byte]*FileTransfer{
1096 Access: func() accessBitmap {
1097 var bits accessBitmap
1098 bits.Set(accessUploadFile)
1099 bits.Set(accessUploadAnywhere)
1105 TranUploadFile, &[]byte{0, 1},
1106 NewField(FieldFileName, []byte("testFile")),
1107 NewField(FieldFilePath, []byte{
1115 wantRes: []Transaction{
1120 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1121 ErrorCode: []byte{0, 0, 0, 0},
1123 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), // rand.Seed(1)
1130 name: "when user does not have required access",
1134 Access: func() accessBitmap {
1135 var bits accessBitmap
1141 TranUploadFile, &[]byte{0, 1},
1142 NewField(FieldFileName, []byte("testFile")),
1143 NewField(FieldFilePath, []byte{
1151 wantRes: []Transaction{
1156 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1157 ErrorCode: []byte{0, 0, 0, 1},
1159 NewField(FieldError, []byte("You are not allowed to upload files.")), // rand.Seed(1)
1166 for _, tt := range tests {
1167 t.Run(tt.name, func(t *testing.T) {
1169 gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t)
1170 if (err != nil) != tt.wantErr {
1171 t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr)
1175 tranAssertEqual(t, tt.wantRes, gotRes)
1180 func TestHandleMakeAlias(t *testing.T) {
1188 wantRes []Transaction
1192 name: "with valid input and required permissions",
1195 logger: NewTestLogger(),
1197 Access: func() accessBitmap {
1198 var bits accessBitmap
1199 bits.Set(accessMakeAlias)
1205 FileRoot: func() string {
1206 path, _ := os.Getwd()
1207 return path + "/test/config/Files"
1210 Logger: NewTestLogger(),
1211 FS: func() *MockFileStore {
1212 mfs := &MockFileStore{}
1213 path, _ := os.Getwd()
1216 path+"/test/config/Files/foo/testFile",
1217 path+"/test/config/Files/bar/testFile",
1224 TranMakeFileAlias, &[]byte{0, 1},
1225 NewField(FieldFileName, []byte("testFile")),
1226 NewField(FieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1227 NewField(FieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1230 wantRes: []Transaction{
1235 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1236 ErrorCode: []byte{0, 0, 0, 0},
1237 Fields: []Field(nil),
1243 name: "when symlink returns an error",
1246 logger: NewTestLogger(),
1248 Access: func() accessBitmap {
1249 var bits accessBitmap
1250 bits.Set(accessMakeAlias)
1256 FileRoot: func() string {
1257 path, _ := os.Getwd()
1258 return path + "/test/config/Files"
1261 Logger: NewTestLogger(),
1262 FS: func() *MockFileStore {
1263 mfs := &MockFileStore{}
1264 path, _ := os.Getwd()
1267 path+"/test/config/Files/foo/testFile",
1268 path+"/test/config/Files/bar/testFile",
1269 ).Return(errors.New("ohno"))
1275 TranMakeFileAlias, &[]byte{0, 1},
1276 NewField(FieldFileName, []byte("testFile")),
1277 NewField(FieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1278 NewField(FieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1281 wantRes: []Transaction{
1286 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1287 ErrorCode: []byte{0, 0, 0, 1},
1289 NewField(FieldError, []byte("Error creating alias")),
1296 name: "when user does not have required permission",
1299 logger: NewTestLogger(),
1301 Access: func() accessBitmap {
1302 var bits accessBitmap
1308 FileRoot: func() string {
1309 path, _ := os.Getwd()
1310 return path + "/test/config/Files"
1316 TranMakeFileAlias, &[]byte{0, 1},
1317 NewField(FieldFileName, []byte("testFile")),
1318 NewField(FieldFilePath, []byte{
1324 NewField(FieldFileNewPath, []byte{
1332 wantRes: []Transaction{
1337 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1338 ErrorCode: []byte{0, 0, 0, 1},
1340 NewField(FieldError, []byte("You are not allowed to make aliases.")),
1347 for _, tt := range tests {
1348 t.Run(tt.name, func(t *testing.T) {
1349 gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
1350 if (err != nil) != tt.wantErr {
1351 t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
1355 tranAssertEqual(t, tt.wantRes, gotRes)
1360 func TestHandleGetUser(t *testing.T) {
1368 wantRes []Transaction
1369 wantErr assert.ErrorAssertionFunc
1372 name: "when account is valid",
1376 Access: func() accessBitmap {
1377 var bits accessBitmap
1378 bits.Set(accessOpenUser)
1383 Accounts: map[string]*Account{
1387 Password: "password",
1388 Access: accessBitmap{},
1394 TranGetUser, &[]byte{0, 1},
1395 NewField(FieldUserLogin, []byte("guest")),
1398 wantRes: []Transaction{
1403 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1404 ErrorCode: []byte{0, 0, 0, 0},
1406 NewField(FieldUserName, []byte("Guest")),
1407 NewField(FieldUserLogin, encodeString([]byte("guest"))),
1408 NewField(FieldUserPassword, []byte("password")),
1409 NewField(FieldUserAccess, []byte{0, 0, 0, 0, 0, 0, 0, 0}),
1413 wantErr: assert.NoError,
1416 name: "when user does not have required permission",
1420 Access: func() accessBitmap {
1421 var bits accessBitmap
1426 Accounts: map[string]*Account{},
1430 TranGetUser, &[]byte{0, 1},
1431 NewField(FieldUserLogin, []byte("nonExistentUser")),
1434 wantRes: []Transaction{
1439 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1440 ErrorCode: []byte{0, 0, 0, 1},
1442 NewField(FieldError, []byte("You are not allowed to view accounts.")),
1446 wantErr: assert.NoError,
1449 name: "when account does not exist",
1453 Access: func() accessBitmap {
1454 var bits accessBitmap
1455 bits.Set(accessOpenUser)
1460 Accounts: map[string]*Account{},
1464 TranGetUser, &[]byte{0, 1},
1465 NewField(FieldUserLogin, []byte("nonExistentUser")),
1468 wantRes: []Transaction{
1473 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1474 ErrorCode: []byte{0, 0, 0, 1},
1476 NewField(FieldError, []byte("Account does not exist.")),
1480 wantErr: assert.NoError,
1483 for _, tt := range tests {
1484 t.Run(tt.name, func(t *testing.T) {
1485 gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
1486 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
1490 tranAssertEqual(t, tt.wantRes, gotRes)
1495 func TestHandleDeleteUser(t *testing.T) {
1503 wantRes []Transaction
1504 wantErr assert.ErrorAssertionFunc
1507 name: "when user dataFile",
1511 Access: func() accessBitmap {
1512 var bits accessBitmap
1513 bits.Set(accessDeleteUser)
1518 Accounts: map[string]*Account{
1521 Name: "Testy McTest",
1522 Password: "password",
1523 Access: accessBitmap{},
1526 FS: func() *MockFileStore {
1527 mfs := &MockFileStore{}
1528 mfs.On("Remove", "Users/testuser.yaml").Return(nil)
1534 TranDeleteUser, &[]byte{0, 1},
1535 NewField(FieldUserLogin, encodeString([]byte("testuser"))),
1538 wantRes: []Transaction{
1543 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1544 ErrorCode: []byte{0, 0, 0, 0},
1545 Fields: []Field(nil),
1548 wantErr: assert.NoError,
1551 name: "when user does not have required permission",
1555 Access: func() accessBitmap {
1556 var bits accessBitmap
1561 Accounts: map[string]*Account{},
1565 TranDeleteUser, &[]byte{0, 1},
1566 NewField(FieldUserLogin, encodeString([]byte("testuser"))),
1569 wantRes: []Transaction{
1574 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1575 ErrorCode: []byte{0, 0, 0, 1},
1577 NewField(FieldError, []byte("You are not allowed to delete accounts.")),
1581 wantErr: assert.NoError,
1584 for _, tt := range tests {
1585 t.Run(tt.name, func(t *testing.T) {
1586 gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
1587 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
1591 tranAssertEqual(t, tt.wantRes, gotRes)
1596 func TestHandleGetMsgs(t *testing.T) {
1604 wantRes []Transaction
1605 wantErr assert.ErrorAssertionFunc
1608 name: "returns news data",
1612 Access: func() accessBitmap {
1613 var bits accessBitmap
1614 bits.Set(accessNewsReadArt)
1619 FlatNews: []byte("TEST"),
1623 TranGetMsgs, &[]byte{0, 1},
1626 wantRes: []Transaction{
1631 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1632 ErrorCode: []byte{0, 0, 0, 0},
1634 NewField(FieldData, []byte("TEST")),
1638 wantErr: assert.NoError,
1641 name: "when user does not have required permission",
1645 Access: func() accessBitmap {
1646 var bits accessBitmap
1651 Accounts: map[string]*Account{},
1655 TranGetMsgs, &[]byte{0, 1},
1658 wantRes: []Transaction{
1663 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1664 ErrorCode: []byte{0, 0, 0, 1},
1666 NewField(FieldError, []byte("You are not allowed to read news.")),
1670 wantErr: assert.NoError,
1673 for _, tt := range tests {
1674 t.Run(tt.name, func(t *testing.T) {
1675 gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t)
1676 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) {
1680 tranAssertEqual(t, tt.wantRes, gotRes)
1685 func TestHandleNewUser(t *testing.T) {
1693 wantRes []Transaction
1694 wantErr assert.ErrorAssertionFunc
1697 name: "when user does not have required permission",
1701 Access: func() accessBitmap {
1702 var bits accessBitmap
1707 Accounts: map[string]*Account{},
1711 TranNewUser, &[]byte{0, 1},
1714 wantRes: []Transaction{
1719 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1720 ErrorCode: []byte{0, 0, 0, 1},
1722 NewField(FieldError, []byte("You are not allowed to create new accounts.")),
1726 wantErr: assert.NoError,
1729 name: "when user attempts to create account with greater access",
1733 Access: func() accessBitmap {
1734 var bits accessBitmap
1735 bits.Set(accessCreateUser)
1740 Accounts: map[string]*Account{},
1744 TranNewUser, &[]byte{0, 1},
1745 NewField(FieldUserLogin, []byte("userB")),
1749 var bits accessBitmap
1750 bits.Set(accessDisconUser)
1756 wantRes: []Transaction{
1761 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1762 ErrorCode: []byte{0, 0, 0, 1},
1764 NewField(FieldError, []byte("Cannot create account with more access than yourself.")),
1768 wantErr: assert.NoError,
1771 for _, tt := range tests {
1772 t.Run(tt.name, func(t *testing.T) {
1773 gotRes, err := HandleNewUser(tt.args.cc, tt.args.t)
1774 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) {
1778 tranAssertEqual(t, tt.wantRes, gotRes)
1783 func TestHandleListUsers(t *testing.T) {
1791 wantRes []Transaction
1792 wantErr assert.ErrorAssertionFunc
1795 name: "when user does not have required permission",
1799 Access: func() accessBitmap {
1800 var bits accessBitmap
1805 Accounts: map[string]*Account{},
1809 TranNewUser, &[]byte{0, 1},
1812 wantRes: []Transaction{
1817 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1818 ErrorCode: []byte{0, 0, 0, 1},
1820 NewField(FieldError, []byte("You are not allowed to view accounts.")),
1824 wantErr: assert.NoError,
1827 name: "when user has required permission",
1831 Access: func() accessBitmap {
1832 var bits accessBitmap
1833 bits.Set(accessOpenUser)
1838 Accounts: map[string]*Account{
1843 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
1849 TranGetClientInfoText, &[]byte{0, 1},
1850 NewField(FieldUserID, []byte{0, 1}),
1853 wantRes: []Transaction{
1858 ID: []byte{0, 0, 0, 0},
1859 ErrorCode: []byte{0, 0, 0, 0},
1861 NewField(FieldData, []byte{
1862 0x00, 0x04, 0x00, 0x66, 0x00, 0x05, 0x67, 0x75, 0x65, 0x73, 0x74, 0x00, 0x69, 0x00, 0x05, 0x98,
1863 0x8a, 0x9a, 0x8c, 0x8b, 0x00, 0x6e, 0x00, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1864 0x00, 0x6a, 0x00, 0x01, 0x78,
1869 wantErr: assert.NoError,
1872 for _, tt := range tests {
1873 t.Run(tt.name, func(t *testing.T) {
1874 gotRes, err := HandleListUsers(tt.args.cc, tt.args.t)
1875 if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) {
1879 tranAssertEqual(t, tt.wantRes, gotRes)
1884 func TestHandleDownloadFile(t *testing.T) {
1892 wantRes []Transaction
1893 wantErr assert.ErrorAssertionFunc
1896 name: "when user does not have required permission",
1900 Access: func() accessBitmap {
1901 var bits accessBitmap
1907 t: NewTransaction(TranDownloadFile, &[]byte{0, 1}),
1909 wantRes: []Transaction{
1914 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1915 ErrorCode: []byte{0, 0, 0, 1},
1917 NewField(FieldError, []byte("You are not allowed to download files.")),
1921 wantErr: assert.NoError,
1924 name: "with a valid file",
1927 transfers: map[int]map[[4]byte]*FileTransfer{
1931 Access: func() accessBitmap {
1932 var bits accessBitmap
1933 bits.Set(accessDownloadFile)
1939 fileTransfers: map[[4]byte]*FileTransfer{},
1941 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1943 Accounts: map[string]*Account{},
1949 NewField(FieldFileName, []byte("testfile.txt")),
1950 NewField(FieldFilePath, []byte{0x0, 0x00}),
1953 wantRes: []Transaction{
1958 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1959 ErrorCode: []byte{0, 0, 0, 0},
1961 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1962 NewField(FieldWaitingCount, []byte{0x00, 0x00}),
1963 NewField(FieldTransferSize, []byte{0x00, 0x00, 0x00, 0xa5}),
1964 NewField(FieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}),
1968 wantErr: assert.NoError,
1971 name: "when client requests to resume 1k test file at offset 256",
1974 transfers: map[int]map[[4]byte]*FileTransfer{
1976 }, Account: &Account{
1977 Access: func() accessBitmap {
1978 var bits accessBitmap
1979 bits.Set(accessDownloadFile)
1986 // FS: func() *MockFileStore {
1987 // path, _ := os.Getwd()
1988 // testFile, err := os.Open(path + "/test/config/Files/testfile-1k")
1993 // mfi := &MockFileInfo{}
1994 // mfi.On("Mode").Return(fs.FileMode(0))
1995 // mfs := &MockFileStore{}
1996 // mfs.On("Stat", "/fakeRoot/Files/testfile.txt").Return(mfi, nil)
1997 // mfs.On("Open", "/fakeRoot/Files/testfile.txt").Return(testFile, nil)
1998 // mfs.On("Stat", "/fakeRoot/Files/.info_testfile.txt").Return(nil, errors.New("no"))
1999 // mfs.On("Stat", "/fakeRoot/Files/.rsrc_testfile.txt").Return(nil, errors.New("no"))
2003 fileTransfers: map[[4]byte]*FileTransfer{},
2005 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
2007 Accounts: map[string]*Account{},
2013 NewField(FieldFileName, []byte("testfile-1k")),
2014 NewField(FieldFilePath, []byte{0x00, 0x00}),
2016 FieldFileResumeData,
2018 frd := FileResumeData{
2022 ForkCount: [2]byte{0, 2},
2023 ForkInfoList: []ForkInfoList{
2025 Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
2026 DataSize: [4]byte{0, 0, 0x01, 0x00}, // request offset 256
2031 Fork: [4]byte{0x4d, 0x41, 0x43, 0x52}, // "MACR"
2032 DataSize: [4]byte{0, 0, 0, 0},
2038 b, _ := frd.BinaryMarshal()
2044 wantRes: []Transaction{
2049 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2050 ErrorCode: []byte{0, 0, 0, 0},
2052 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
2053 NewField(FieldWaitingCount, []byte{0x00, 0x00}),
2054 NewField(FieldTransferSize, []byte{0x00, 0x00, 0x03, 0x8d}),
2055 NewField(FieldFileSize, []byte{0x00, 0x00, 0x03, 0x00}),
2059 wantErr: assert.NoError,
2062 for _, tt := range tests {
2063 t.Run(tt.name, func(t *testing.T) {
2064 gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t)
2065 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) {
2069 tranAssertEqual(t, tt.wantRes, gotRes)
2074 func TestHandleUpdateUser(t *testing.T) {
2082 wantRes []Transaction
2083 wantErr assert.ErrorAssertionFunc
2086 name: "when action is create user without required permission",
2089 logger: NewTestLogger(),
2091 Logger: NewTestLogger(),
2094 Access: func() accessBitmap {
2095 var bits accessBitmap
2103 NewField(FieldData, []byte{
2104 0x00, 0x04, // field count
2106 0x00, 0x69, // FieldUserLogin = 105
2110 0x00, 0x6a, // FieldUserPassword = 106
2114 0x00, 0x66, // FieldUserName = 102
2118 0x00, 0x6e, // FieldUserAccess = 110
2120 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2124 wantRes: []Transaction{
2129 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2130 ErrorCode: []byte{0, 0, 0, 1},
2132 NewField(FieldError, []byte("You are not allowed to create new accounts.")),
2136 wantErr: assert.NoError,
2139 name: "when action is modify user without required permission",
2142 logger: NewTestLogger(),
2144 Logger: NewTestLogger(),
2145 Accounts: map[string]*Account{
2150 Access: func() accessBitmap {
2151 var bits accessBitmap
2159 NewField(FieldData, []byte{
2160 0x00, 0x04, // field count
2162 0x00, 0x69, // FieldUserLogin = 105
2166 0x00, 0x6a, // FieldUserPassword = 106
2170 0x00, 0x66, // FieldUserName = 102
2174 0x00, 0x6e, // FieldUserAccess = 110
2176 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2180 wantRes: []Transaction{
2185 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2186 ErrorCode: []byte{0, 0, 0, 1},
2188 NewField(FieldError, []byte("You are not allowed to modify accounts.")),
2192 wantErr: assert.NoError,
2195 name: "when action is delete user without required permission",
2198 logger: NewTestLogger(),
2200 Accounts: map[string]*Account{
2205 Access: func() accessBitmap {
2206 var bits accessBitmap
2214 NewField(FieldData, []byte{
2222 wantRes: []Transaction{
2227 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2228 ErrorCode: []byte{0, 0, 0, 1},
2230 NewField(FieldError, []byte("You are not allowed to delete accounts.")),
2234 wantErr: assert.NoError,
2237 for _, tt := range tests {
2238 t.Run(tt.name, func(t *testing.T) {
2239 gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t)
2240 if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) {
2244 tranAssertEqual(t, tt.wantRes, gotRes)
2249 func TestHandleDelNewsArt(t *testing.T) {
2257 wantRes []Transaction
2258 wantErr assert.ErrorAssertionFunc
2261 name: "without required permission",
2265 Access: func() accessBitmap {
2266 var bits accessBitmap
2276 wantRes: []Transaction{
2281 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2282 ErrorCode: []byte{0, 0, 0, 1},
2284 NewField(FieldError, []byte("You are not allowed to delete news articles.")),
2288 wantErr: assert.NoError,
2291 for _, tt := range tests {
2292 t.Run(tt.name, func(t *testing.T) {
2293 gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
2294 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
2297 tranAssertEqual(t, tt.wantRes, gotRes)
2302 func TestHandleDisconnectUser(t *testing.T) {
2310 wantRes []Transaction
2311 wantErr assert.ErrorAssertionFunc
2314 name: "without required permission",
2318 Access: func() accessBitmap {
2319 var bits accessBitmap
2329 wantRes: []Transaction{
2334 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2335 ErrorCode: []byte{0, 0, 0, 1},
2337 NewField(FieldError, []byte("You are not allowed to disconnect users.")),
2341 wantErr: assert.NoError,
2344 name: "when target user has 'cannot be disconnected' priv",
2348 Clients: map[uint16]*ClientConn{
2352 Access: func() accessBitmap {
2353 var bits accessBitmap
2354 bits.Set(accessCannotBeDiscon)
2362 Access: func() accessBitmap {
2363 var bits accessBitmap
2364 bits.Set(accessDisconUser)
2372 NewField(FieldUserID, []byte{0, 1}),
2375 wantRes: []Transaction{
2380 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2381 ErrorCode: []byte{0, 0, 0, 1},
2383 NewField(FieldError, []byte("unnamed is not allowed to be disconnected.")),
2387 wantErr: assert.NoError,
2390 for _, tt := range tests {
2391 t.Run(tt.name, func(t *testing.T) {
2392 gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
2393 if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
2396 tranAssertEqual(t, tt.wantRes, gotRes)
2401 func TestHandleSendInstantMsg(t *testing.T) {
2409 wantRes []Transaction
2410 wantErr assert.ErrorAssertionFunc
2413 name: "without required permission",
2417 Access: func() accessBitmap {
2418 var bits accessBitmap
2428 wantRes: []Transaction{
2433 ID: []byte{0, 0, 0, 0},
2434 ErrorCode: []byte{0, 0, 0, 1},
2436 NewField(FieldError, []byte("You are not allowed to send private messages.")),
2440 wantErr: assert.Error,
2443 name: "when client 1 sends a message to client 2",
2447 Access: func() accessBitmap {
2448 var bits accessBitmap
2449 bits.Set(accessSendPrivMsg)
2454 UserName: []byte("User1"),
2456 Clients: map[uint16]*ClientConn{
2458 AutoReply: []byte(nil),
2459 Flags: []byte{0, 0},
2467 NewField(FieldData, []byte("hai")),
2468 NewField(FieldUserID, []byte{0, 2}),
2471 wantRes: []Transaction{
2475 NewField(FieldData, []byte("hai")),
2476 NewField(FieldUserName, []byte("User1")),
2477 NewField(FieldUserID, []byte{0, 1}),
2478 NewField(FieldOptions, []byte{0, 1}),
2481 clientID: &[]byte{0, 1},
2485 ID: []byte{0, 0, 0, 0},
2486 ErrorCode: []byte{0, 0, 0, 0},
2487 Fields: []Field(nil),
2490 wantErr: assert.NoError,
2493 name: "when client 2 has autoreply enabled",
2497 Access: func() accessBitmap {
2498 var bits accessBitmap
2499 bits.Set(accessSendPrivMsg)
2504 UserName: []byte("User1"),
2506 Clients: map[uint16]*ClientConn{
2508 Flags: []byte{0, 0},
2510 UserName: []byte("User2"),
2511 AutoReply: []byte("autohai"),
2519 NewField(FieldData, []byte("hai")),
2520 NewField(FieldUserID, []byte{0, 2}),
2523 wantRes: []Transaction{
2527 NewField(FieldData, []byte("hai")),
2528 NewField(FieldUserName, []byte("User1")),
2529 NewField(FieldUserID, []byte{0, 1}),
2530 NewField(FieldOptions, []byte{0, 1}),
2535 NewField(FieldData, []byte("autohai")),
2536 NewField(FieldUserName, []byte("User2")),
2537 NewField(FieldUserID, []byte{0, 2}),
2538 NewField(FieldOptions, []byte{0, 1}),
2541 clientID: &[]byte{0, 1},
2545 ID: []byte{0, 0, 0, 0},
2546 ErrorCode: []byte{0, 0, 0, 0},
2547 Fields: []Field(nil),
2550 wantErr: assert.NoError,
2553 name: "when client 2 has refuse private messages enabled",
2557 Access: func() accessBitmap {
2558 var bits accessBitmap
2559 bits.Set(accessSendPrivMsg)
2564 UserName: []byte("User1"),
2566 Clients: map[uint16]*ClientConn{
2568 Flags: []byte{255, 255},
2570 UserName: []byte("User2"),
2578 NewField(FieldData, []byte("hai")),
2579 NewField(FieldUserID, []byte{0, 2}),
2582 wantRes: []Transaction{
2586 NewField(FieldData, []byte("User2 does not accept private messages.")),
2587 NewField(FieldUserName, []byte("User2")),
2588 NewField(FieldUserID, []byte{0, 2}),
2589 NewField(FieldOptions, []byte{0, 2}),
2592 clientID: &[]byte{0, 1},
2596 ID: []byte{0, 0, 0, 0},
2597 ErrorCode: []byte{0, 0, 0, 0},
2598 Fields: []Field(nil),
2601 wantErr: assert.NoError,
2604 for _, tt := range tests {
2605 t.Run(tt.name, func(t *testing.T) {
2606 gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
2607 if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
2611 tranAssertEqual(t, tt.wantRes, gotRes)
2616 func TestHandleDeleteFile(t *testing.T) {
2624 wantRes []Transaction
2625 wantErr assert.ErrorAssertionFunc
2628 name: "when user does not have required permission to delete a folder",
2632 Access: func() accessBitmap {
2633 var bits accessBitmap
2639 FileRoot: func() string {
2640 return "/fakeRoot/Files"
2643 FS: func() *MockFileStore {
2644 mfi := &MockFileInfo{}
2645 mfi.On("Mode").Return(fs.FileMode(0))
2646 mfi.On("Size").Return(int64(100))
2647 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2648 mfi.On("IsDir").Return(false)
2649 mfi.On("Name").Return("testfile")
2651 mfs := &MockFileStore{}
2652 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2653 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2654 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2658 Accounts: map[string]*Account{},
2662 TranDeleteFile, &[]byte{0, 1},
2663 NewField(FieldFileName, []byte("testfile")),
2664 NewField(FieldFilePath, []byte{
2672 wantRes: []Transaction{
2677 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2678 ErrorCode: []byte{0, 0, 0, 1},
2680 NewField(FieldError, []byte("You are not allowed to delete files.")),
2684 wantErr: assert.NoError,
2687 name: "deletes all associated metadata files",
2691 Access: func() accessBitmap {
2692 var bits accessBitmap
2693 bits.Set(accessDeleteFile)
2699 FileRoot: func() string {
2700 return "/fakeRoot/Files"
2703 FS: func() *MockFileStore {
2704 mfi := &MockFileInfo{}
2705 mfi.On("Mode").Return(fs.FileMode(0))
2706 mfi.On("Size").Return(int64(100))
2707 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2708 mfi.On("IsDir").Return(false)
2709 mfi.On("Name").Return("testfile")
2711 mfs := &MockFileStore{}
2712 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2713 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2714 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2716 mfs.On("RemoveAll", "/fakeRoot/Files/aaa/testfile").Return(nil)
2717 mfs.On("Remove", "/fakeRoot/Files/aaa/testfile.incomplete").Return(nil)
2718 mfs.On("Remove", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil)
2719 mfs.On("Remove", "/fakeRoot/Files/aaa/.info_testfile").Return(nil)
2723 Accounts: map[string]*Account{},
2727 TranDeleteFile, &[]byte{0, 1},
2728 NewField(FieldFileName, []byte("testfile")),
2729 NewField(FieldFilePath, []byte{
2737 wantRes: []Transaction{
2742 ID: []byte{0x0, 0x0, 0x0, 0x0},
2743 ErrorCode: []byte{0, 0, 0, 0},
2744 Fields: []Field(nil),
2747 wantErr: assert.NoError,
2750 for _, tt := range tests {
2751 t.Run(tt.name, func(t *testing.T) {
2752 gotRes, err := HandleDeleteFile(tt.args.cc, tt.args.t)
2753 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteFile(%v, %v)", tt.args.cc, tt.args.t)) {
2757 tranAssertEqual(t, tt.wantRes, gotRes)
2759 tt.args.cc.Server.FS.(*MockFileStore).AssertExpectations(t)
2764 func TestHandleGetFileNameList(t *testing.T) {
2772 wantRes []Transaction
2773 wantErr assert.ErrorAssertionFunc
2776 name: "when FieldFilePath is a drop box, but user does not have accessViewDropBoxes ",
2780 Access: func() accessBitmap {
2781 var bits accessBitmap
2788 FileRoot: func() string {
2789 path, _ := os.Getwd()
2790 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2796 TranGetFileNameList, &[]byte{0, 1},
2797 NewField(FieldFilePath, []byte{
2801 0x64, 0x72, 0x6f, 0x70, 0x20, 0x62, 0x6f, 0x78, // "drop box"
2805 wantRes: []Transaction{
2810 ID: []byte{0, 0, 0, 0},
2811 ErrorCode: []byte{0, 0, 0, 1},
2813 NewField(FieldError, []byte("You are not allowed to view drop boxes.")),
2817 wantErr: assert.NoError,
2820 name: "with file root",
2825 FileRoot: func() string {
2826 path, _ := os.Getwd()
2827 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2833 TranGetFileNameList, &[]byte{0, 1},
2834 NewField(FieldFilePath, []byte{
2840 wantRes: []Transaction{
2845 ID: []byte{0, 0, 0, 0},
2846 ErrorCode: []byte{0, 0, 0, 0},
2849 FieldFileNameWithInfo,
2851 fnwi := FileNameWithInfo{
2852 fileNameWithInfoHeader: fileNameWithInfoHeader{
2853 Type: [4]byte{0x54, 0x45, 0x58, 0x54},
2854 Creator: [4]byte{0x54, 0x54, 0x58, 0x54},
2855 FileSize: [4]byte{0, 0, 0x04, 0},
2857 NameScript: [2]byte{},
2858 NameSize: [2]byte{0, 0x0b},
2860 name: []byte("testfile-1k"),
2862 b, _ := fnwi.MarshalBinary()
2869 wantErr: assert.NoError,
2872 for _, tt := range tests {
2873 t.Run(tt.name, func(t *testing.T) {
2874 gotRes, err := HandleGetFileNameList(tt.args.cc, tt.args.t)
2875 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetFileNameList(%v, %v)", tt.args.cc, tt.args.t)) {
2879 tranAssertEqual(t, tt.wantRes, gotRes)
2884 func TestHandleGetClientInfoText(t *testing.T) {
2892 wantRes []Transaction
2893 wantErr assert.ErrorAssertionFunc
2896 name: "when user does not have required permission",
2900 Access: func() accessBitmap {
2901 var bits accessBitmap
2906 Accounts: map[string]*Account{},
2910 TranGetClientInfoText, &[]byte{0, 1},
2911 NewField(FieldUserID, []byte{0, 1}),
2914 wantRes: []Transaction{
2919 ID: []byte{0, 0, 0, 0},
2920 ErrorCode: []byte{0, 0, 0, 1},
2922 NewField(FieldError, []byte("You are not allowed to get client info.")),
2926 wantErr: assert.NoError,
2929 name: "with a valid user",
2932 UserName: []byte("Testy McTest"),
2933 RemoteAddr: "1.2.3.4:12345",
2935 Access: func() accessBitmap {
2936 var bits accessBitmap
2937 bits.Set(accessGetClientInfo)
2944 Accounts: map[string]*Account{},
2945 Clients: map[uint16]*ClientConn{
2947 UserName: []byte("Testy McTest"),
2948 RemoteAddr: "1.2.3.4:12345",
2950 Access: func() accessBitmap {
2951 var bits accessBitmap
2952 bits.Set(accessGetClientInfo)
2961 transfers: map[int]map[[4]byte]*FileTransfer{
2969 TranGetClientInfoText, &[]byte{0, 1},
2970 NewField(FieldUserID, []byte{0, 1}),
2973 wantRes: []Transaction{
2978 ID: []byte{0, 0, 0, 0},
2979 ErrorCode: []byte{0, 0, 0, 0},
2981 NewField(FieldData, []byte(
2982 strings.ReplaceAll(`Nickname: Testy McTest
2985 Address: 1.2.3.4:12345
2987 -------- File Downloads ---------
2991 ------- Folder Downloads --------
2995 --------- File Uploads ----------
2999 -------- Folder Uploads ---------
3003 ------- Waiting Downloads -------
3009 NewField(FieldUserName, []byte("Testy McTest")),
3013 wantErr: assert.NoError,
3016 for _, tt := range tests {
3017 t.Run(tt.name, func(t *testing.T) {
3018 gotRes, err := HandleGetClientInfoText(tt.args.cc, tt.args.t)
3019 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetClientInfoText(%v, %v)", tt.args.cc, tt.args.t)) {
3022 tranAssertEqual(t, tt.wantRes, gotRes)
3027 func TestHandleTranAgreed(t *testing.T) {
3035 wantRes []Transaction
3036 wantErr assert.ErrorAssertionFunc
3039 name: "normal request flow",
3043 Access: func() accessBitmap {
3044 var bits accessBitmap
3045 bits.Set(accessDisconUser)
3046 bits.Set(accessAnyName)
3050 Flags: []byte{0, 1},
3051 Version: []byte{0, 1},
3053 logger: NewTestLogger(),
3056 BannerFile: "banner.jpg",
3062 NewField(FieldUserName, []byte("username")),
3063 NewField(FieldUserIconID, []byte{0, 1}),
3064 NewField(FieldOptions, []byte{0, 0}),
3067 wantRes: []Transaction{
3069 clientID: &[]byte{0, 1},
3072 Type: []byte{0, 0x7a},
3073 ID: []byte{0, 0, 0, 0},
3074 ErrorCode: []byte{0, 0, 0, 0},
3076 NewField(FieldBannerType, []byte("JPEG")),
3080 clientID: &[]byte{0, 1},
3084 ID: []byte{0, 0, 0, 0},
3085 ErrorCode: []byte{0, 0, 0, 0},
3089 wantErr: assert.NoError,
3092 for _, tt := range tests {
3093 t.Run(tt.name, func(t *testing.T) {
3094 gotRes, err := HandleTranAgreed(tt.args.cc, tt.args.t)
3095 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranAgreed(%v, %v)", tt.args.cc, tt.args.t)) {
3098 tranAssertEqual(t, tt.wantRes, gotRes)
3103 func TestHandleSetClientUserInfo(t *testing.T) {
3111 wantRes []Transaction
3112 wantErr assert.ErrorAssertionFunc
3115 name: "when client does not have accessAnyName",
3119 Access: func() accessBitmap {
3120 var bits accessBitmap
3125 UserName: []byte("Guest"),
3126 Flags: []byte{0, 1},
3128 Clients: map[uint16]*ClientConn{
3136 TranSetClientUserInfo, nil,
3137 NewField(FieldUserIconID, []byte{0, 1}),
3138 NewField(FieldUserName, []byte("NOPE")),
3141 wantRes: []Transaction{
3143 clientID: &[]byte{0, 1},
3146 Type: []byte{0x01, 0x2d},
3147 ID: []byte{0, 0, 0, 0},
3148 ErrorCode: []byte{0, 0, 0, 0},
3150 NewField(FieldUserID, []byte{0, 1}),
3151 NewField(FieldUserIconID, []byte{0, 1}),
3152 NewField(FieldUserFlags, []byte{0, 1}),
3153 NewField(FieldUserName, []byte("Guest"))},
3156 wantErr: assert.NoError,
3159 for _, tt := range tests {
3160 t.Run(tt.name, func(t *testing.T) {
3161 gotRes, err := HandleSetClientUserInfo(tt.args.cc, tt.args.t)
3162 if !tt.wantErr(t, err, fmt.Sprintf("HandleSetClientUserInfo(%v, %v)", tt.args.cc, tt.args.t)) {
3166 tranAssertEqual(t, tt.wantRes, gotRes)
3171 func TestHandleDelNewsItem(t *testing.T) {
3179 wantRes []Transaction
3180 wantErr assert.ErrorAssertionFunc
3183 name: "when user does not have permission to delete a news category",
3187 Access: accessBitmap{},
3191 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3202 TranDelNewsItem, nil,
3203 NewField(FieldNewsPath,
3208 0x74, 0x65, 0x73, 0x74,
3213 wantRes: []Transaction{
3215 clientID: &[]byte{0, 1},
3219 ID: []byte{0, 0, 0, 0},
3220 ErrorCode: []byte{0, 0, 0, 1},
3222 NewField(FieldError, []byte("You are not allowed to delete news categories.")),
3226 wantErr: assert.NoError,
3229 name: "when user does not have permission to delete a news folder",
3233 Access: accessBitmap{},
3237 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3248 TranDelNewsItem, nil,
3249 NewField(FieldNewsPath,
3254 0x74, 0x65, 0x73, 0x74,
3259 wantRes: []Transaction{
3261 clientID: &[]byte{0, 1},
3265 ID: []byte{0, 0, 0, 0},
3266 ErrorCode: []byte{0, 0, 0, 1},
3268 NewField(FieldError, []byte("You are not allowed to delete news folders.")),
3272 wantErr: assert.NoError,
3275 name: "when user deletes a news folder",
3279 Access: func() accessBitmap {
3280 var bits accessBitmap
3281 bits.Set(accessNewsDeleteFldr)
3287 ConfigDir: "/fakeConfigRoot",
3288 FS: func() *MockFileStore {
3289 mfs := &MockFileStore{}
3290 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3293 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3304 TranDelNewsItem, nil,
3305 NewField(FieldNewsPath,
3310 0x74, 0x65, 0x73, 0x74,
3315 wantRes: []Transaction{
3317 clientID: &[]byte{0, 1},
3321 ID: []byte{0, 0, 0, 0},
3322 ErrorCode: []byte{0, 0, 0, 0},
3326 wantErr: assert.NoError,
3329 for _, tt := range tests {
3330 t.Run(tt.name, func(t *testing.T) {
3331 gotRes, err := HandleDelNewsItem(tt.args.cc, tt.args.t)
3332 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsItem(%v, %v)", tt.args.cc, tt.args.t)) {
3335 tranAssertEqual(t, tt.wantRes, gotRes)
3340 func TestHandleDownloadBanner(t *testing.T) {
3348 wantRes []Transaction
3349 wantErr assert.ErrorAssertionFunc
3352 name: "returns expected response",
3356 transfers: map[int]map[[4]byte]*FileTransfer{
3360 ConfigDir: "/config",
3362 BannerFile: "banner.jpg",
3364 fileTransfers: map[[4]byte]*FileTransfer{},
3365 FS: func() *MockFileStore {
3366 mfi := &MockFileInfo{}
3367 mfi.On("Size").Return(int64(100))
3369 mfs := &MockFileStore{}
3370 mfs.On("Stat", "/config/banner.jpg").Return(mfi, nil)
3375 t: NewTransaction(TranDownloadBanner, nil),
3377 wantRes: []Transaction{
3379 clientID: &[]byte{0, 1},
3383 ID: []byte{0, 0, 0, 0},
3384 ErrorCode: []byte{0, 0, 0, 0},
3386 NewField(FieldRefNum, []byte{1, 2, 3, 4}),
3387 NewField(FieldTransferSize, []byte{0, 0, 0, 0x64}),
3391 wantErr: assert.NoError,
3394 for _, tt := range tests {
3395 t.Run(tt.name, func(t *testing.T) {
3396 gotRes, err := HandleDownloadBanner(tt.args.cc, tt.args.t)
3397 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)) {
3401 tranAssertEqual(t, tt.wantRes, gotRes)
3406 func TestHandleTranOldPostNews(t *testing.T) {
3414 wantRes []Transaction
3415 wantErr assert.ErrorAssertionFunc
3418 name: "when user does not have required permission",
3422 Access: func() accessBitmap {
3423 var bits accessBitmap
3429 TranOldPostNews, &[]byte{0, 1},
3430 NewField(FieldData, []byte("hai")),
3433 wantRes: []Transaction{
3438 ID: []byte{0, 0, 0, 0},
3439 ErrorCode: []byte{0, 0, 0, 1},
3441 NewField(FieldError, []byte("You are not allowed to post news.")),
3445 wantErr: assert.NoError,
3448 name: "when user posts news update",
3452 Access: func() accessBitmap {
3453 var bits accessBitmap
3454 bits.Set(accessNewsPostArt)
3459 FS: func() *MockFileStore {
3460 mfs := &MockFileStore{}
3461 mfs.On("WriteFile", "/fakeConfigRoot/MessageBoard.txt", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3464 ConfigDir: "/fakeConfigRoot",
3469 TranOldPostNews, &[]byte{0, 1},
3470 NewField(FieldData, []byte("hai")),
3473 wantRes: []Transaction{
3478 ID: []byte{0, 0, 0, 0},
3479 ErrorCode: []byte{0, 0, 0, 0},
3482 wantErr: assert.NoError,
3485 for _, tt := range tests {
3486 t.Run(tt.name, func(t *testing.T) {
3487 gotRes, err := HandleTranOldPostNews(tt.args.cc, tt.args.t)
3488 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranOldPostNews(%v, %v)", tt.args.cc, tt.args.t)) {
3492 tranAssertEqual(t, tt.wantRes, gotRes)
3497 func TestHandleInviteNewChat(t *testing.T) {
3505 wantRes []Transaction
3506 wantErr assert.ErrorAssertionFunc
3509 name: "when user does not have required permission",
3513 Access: func() accessBitmap {
3514 var bits accessBitmap
3519 t: NewTransaction(TranInviteNewChat, &[]byte{0, 1}),
3521 wantRes: []Transaction{
3526 ID: []byte{0, 0, 0, 0},
3527 ErrorCode: []byte{0, 0, 0, 1},
3529 NewField(FieldError, []byte("You are not allowed to request private chat.")),
3533 wantErr: assert.NoError,
3536 name: "when userA invites userB to new private chat",
3541 Access: func() accessBitmap {
3542 var bits accessBitmap
3543 bits.Set(accessOpenChat)
3547 UserName: []byte("UserA"),
3549 Flags: []byte{0, 0},
3551 Clients: map[uint16]*ClientConn{
3554 UserName: []byte("UserB"),
3555 Flags: []byte{0, 0},
3558 PrivateChats: make(map[uint32]*PrivateChat),
3562 TranInviteNewChat, &[]byte{0, 1},
3563 NewField(FieldUserID, []byte{0, 2}),
3566 wantRes: []Transaction{
3568 clientID: &[]byte{0, 2},
3571 Type: []byte{0, 0x71},
3572 ID: []byte{0, 0, 0, 0},
3573 ErrorCode: []byte{0, 0, 0, 0},
3575 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3576 NewField(FieldUserName, []byte("UserA")),
3577 NewField(FieldUserID, []byte{0, 1}),
3582 clientID: &[]byte{0, 1},
3586 ID: []byte{0, 0, 0, 0},
3587 ErrorCode: []byte{0, 0, 0, 0},
3589 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3590 NewField(FieldUserName, []byte("UserA")),
3591 NewField(FieldUserID, []byte{0, 1}),
3592 NewField(FieldUserIconID, []byte{0, 1}),
3593 NewField(FieldUserFlags, []byte{0, 0}),
3597 wantErr: assert.NoError,
3600 name: "when userA invites userB to new private chat, but UserB has refuse private chat enabled",
3605 Access: func() accessBitmap {
3606 var bits accessBitmap
3607 bits.Set(accessOpenChat)
3611 UserName: []byte("UserA"),
3613 Flags: []byte{0, 0},
3615 Clients: map[uint16]*ClientConn{
3618 UserName: []byte("UserB"),
3619 Flags: []byte{255, 255},
3622 PrivateChats: make(map[uint32]*PrivateChat),
3626 TranInviteNewChat, &[]byte{0, 1},
3627 NewField(FieldUserID, []byte{0, 2}),
3630 wantRes: []Transaction{
3632 clientID: &[]byte{0, 1},
3635 Type: []byte{0, 0x68},
3636 ID: []byte{0, 0, 0, 0},
3637 ErrorCode: []byte{0, 0, 0, 0},
3639 NewField(FieldData, []byte("UserB does not accept private chats.")),
3640 NewField(FieldUserName, []byte("UserB")),
3641 NewField(FieldUserID, []byte{0, 2}),
3642 NewField(FieldOptions, []byte{0, 2}),
3646 clientID: &[]byte{0, 1},
3650 ID: []byte{0, 0, 0, 0},
3651 ErrorCode: []byte{0, 0, 0, 0},
3653 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3654 NewField(FieldUserName, []byte("UserA")),
3655 NewField(FieldUserID, []byte{0, 1}),
3656 NewField(FieldUserIconID, []byte{0, 1}),
3657 NewField(FieldUserFlags, []byte{0, 0}),
3661 wantErr: assert.NoError,
3664 for _, tt := range tests {
3665 t.Run(tt.name, func(t *testing.T) {
3667 gotRes, err := HandleInviteNewChat(tt.args.cc, tt.args.t)
3668 if !tt.wantErr(t, err, fmt.Sprintf("HandleInviteNewChat(%v, %v)", tt.args.cc, tt.args.t)) {
3671 tranAssertEqual(t, tt.wantRes, gotRes)
3676 func TestHandleGetNewsArtData(t *testing.T) {
3684 wantRes []Transaction
3685 wantErr assert.ErrorAssertionFunc
3688 name: "when user does not have required permission",
3692 Access: func() accessBitmap {
3693 var bits accessBitmap
3698 Accounts: map[string]*Account{},
3702 TranGetNewsArtData, &[]byte{0, 1},
3705 wantRes: []Transaction{
3710 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
3711 ErrorCode: []byte{0, 0, 0, 1},
3713 NewField(FieldError, []byte("You are not allowed to read news.")),
3717 wantErr: assert.NoError,
3720 for _, tt := range tests {
3721 t.Run(tt.name, func(t *testing.T) {
3722 gotRes, err := HandleGetNewsArtData(tt.args.cc, tt.args.t)
3723 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtData(%v, %v)", tt.args.cc, tt.args.t)) {
3726 tranAssertEqual(t, tt.wantRes, gotRes)
3731 func TestHandleGetNewsArtNameList(t *testing.T) {
3739 wantRes []Transaction
3740 wantErr assert.ErrorAssertionFunc
3743 name: "when user does not have required permission",
3747 Access: func() accessBitmap {
3748 var bits accessBitmap
3753 Accounts: map[string]*Account{},
3757 TranGetNewsArtNameList, &[]byte{0, 1},
3760 wantRes: []Transaction{
3765 ErrorCode: []byte{0, 0, 0, 1},
3767 NewField(FieldError, []byte("You are not allowed to read news.")),
3771 wantErr: assert.NoError,
3774 for _, tt := range tests {
3775 t.Run(tt.name, func(t *testing.T) {
3776 gotRes, err := HandleGetNewsArtNameList(tt.args.cc, tt.args.t)
3777 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtNameList(%v, %v)", tt.args.cc, tt.args.t)) {
3780 tranAssertEqual(t, tt.wantRes, gotRes)
3785 func TestHandleNewNewsFldr(t *testing.T) {
3793 wantRes []Transaction
3794 wantErr assert.ErrorAssertionFunc
3797 name: "when user does not have required permission",
3801 Access: func() accessBitmap {
3802 var bits accessBitmap
3807 Accounts: map[string]*Account{},
3811 TranGetNewsArtNameList, &[]byte{0, 1},
3814 wantRes: []Transaction{
3819 ErrorCode: []byte{0, 0, 0, 1},
3821 NewField(FieldError, []byte("You are not allowed to create news folders.")),
3825 wantErr: assert.NoError,
3828 name: "with a valid request",
3832 Access: func() accessBitmap {
3833 var bits accessBitmap
3834 bits.Set(accessNewsCreateFldr)
3838 logger: NewTestLogger(),
3841 ConfigDir: "/fakeConfigRoot",
3842 FS: func() *MockFileStore {
3843 mfs := &MockFileStore{}
3844 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil)
3847 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3853 SubCats: make(map[string]NewsCategoryListData15),
3859 TranGetNewsArtNameList, &[]byte{0, 1},
3860 NewField(FieldFileName, []byte("testFolder")),
3861 NewField(FieldNewsPath,
3866 0x74, 0x65, 0x73, 0x74,
3871 wantRes: []Transaction{
3873 clientID: &[]byte{0, 1},
3877 ID: []byte{0, 0, 0, 0},
3878 ErrorCode: []byte{0, 0, 0, 0},
3882 wantErr: assert.NoError,
3885 // name: "when there is an error writing the threaded news file",
3888 // Account: &Account{
3889 // Access: func() accessBitmap {
3890 // var bits accessBitmap
3891 // bits.Set(accessNewsCreateFldr)
3895 // logger: NewTestLogger(),
3896 // ID: &[]byte{0, 1},
3898 // ConfigDir: "/fakeConfigRoot",
3899 // FS: func() *MockFileStore {
3900 // mfs := &MockFileStore{}
3901 // mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(os.ErrNotExist)
3904 // ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3906 // Type: []byte{0, 2},
3910 // SubCats: make(map[string]NewsCategoryListData15),
3915 // t: NewTransaction(
3916 // TranGetNewsArtNameList, &[]byte{0, 1},
3917 // NewField(FieldFileName, []byte("testFolder")),
3918 // NewField(FieldNewsPath,
3923 // 0x74, 0x65, 0x73, 0x74,
3928 // wantRes: []Transaction{
3930 // clientID: &[]byte{0, 1},
3933 // Type: []byte{0, 0},
3934 // ErrorCode: []byte{0, 0, 0, 1},
3936 // NewField(FieldError, []byte("Error creating news folder.")),
3940 // wantErr: assert.Error,
3943 for _, tt := range tests {
3944 t.Run(tt.name, func(t *testing.T) {
3945 gotRes, err := HandleNewNewsFldr(tt.args.cc, tt.args.t)
3946 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewNewsFldr(%v, %v)", tt.args.cc, tt.args.t)) {
3949 tranAssertEqual(t, tt.wantRes, gotRes)