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 t.Run(tt.name, func(t *testing.T) {
112 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
113 if (err != nil) != tt.wantErr {
114 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
117 if !tranAssertEqual(t, tt.want, got) {
118 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
124 func TestHandleLeaveChat(t *testing.T) {
136 name: "returns expected transactions",
141 PrivateChats: map[uint32]*PrivateChat{
143 ClientConn: map[uint16]*ClientConn{
146 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
152 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
159 Clients: map[uint16]*ClientConn{
162 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
168 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
175 t: NewTransaction(TranDeleteUser, nil, NewField(FieldChatID, []byte{0, 0, 0, 1})),
179 clientID: &[]byte{0, 1},
182 Type: []byte{0, 0x76},
183 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
184 ErrorCode: []byte{0, 0, 0, 0},
186 NewField(FieldChatID, []byte{0, 0, 0, 1}),
187 NewField(FieldUserID, []byte{0, 2}),
194 for _, tt := range tests {
195 t.Run(tt.name, func(t *testing.T) {
196 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
197 if (err != nil) != tt.wantErr {
198 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
201 if !tranAssertEqual(t, tt.want, got) {
202 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
208 func TestHandleGetUserNameList(t *testing.T) {
220 name: "replies with userlist transaction",
226 Clients: map[uint16]*ClientConn{
231 UserName: []byte{0, 4},
237 UserName: []byte{0, 4},
243 ID: []byte{0, 0, 0, 1},
249 clientID: &[]byte{1, 1},
253 ID: []byte{0, 0, 0, 1},
254 ErrorCode: []byte{0, 0, 0, 0},
257 FieldUsernameWithInfo,
258 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
261 FieldUsernameWithInfo,
262 []byte{00, 02, 00, 02, 00, 03, 00, 02, 00, 04},
270 for _, tt := range tests {
271 t.Run(tt.name, func(t *testing.T) {
272 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
273 if (err != nil) != tt.wantErr {
274 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
277 assert.Equal(t, tt.want, got)
282 func TestHandleChatSend(t *testing.T) {
294 name: "sends chat msg transaction to all clients",
298 Access: func() accessBitmap {
299 var bits accessBitmap
300 bits.Set(accessSendChat)
304 UserName: []byte{0x00, 0x01},
306 Clients: map[uint16]*ClientConn{
309 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
315 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
324 NewField(FieldData, []byte("hai")),
330 clientID: &[]byte{0, 1},
333 Type: []byte{0, 0x6a},
334 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
335 ErrorCode: []byte{0, 0, 0, 0},
337 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
341 clientID: &[]byte{0, 2},
344 Type: []byte{0, 0x6a},
345 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
346 ErrorCode: []byte{0, 0, 0, 0},
348 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
355 name: "treats Chat ID 00 00 00 00 as a public chat message",
359 Access: func() accessBitmap {
360 var bits accessBitmap
361 bits.Set(accessSendChat)
365 UserName: []byte{0x00, 0x01},
367 Clients: map[uint16]*ClientConn{
370 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
376 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
385 NewField(FieldData, []byte("hai")),
386 NewField(FieldChatID, []byte{0, 0, 0, 0}),
392 clientID: &[]byte{0, 1},
395 Type: []byte{0, 0x6a},
396 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
397 ErrorCode: []byte{0, 0, 0, 0},
399 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
403 clientID: &[]byte{0, 2},
406 Type: []byte{0, 0x6a},
407 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
408 ErrorCode: []byte{0, 0, 0, 0},
410 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
417 name: "when user does not have required permission",
421 Access: func() accessBitmap {
422 var bits accessBitmap
427 Accounts: map[string]*Account{},
431 TranChatSend, &[]byte{0, 1},
432 NewField(FieldData, []byte("hai")),
440 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
441 ErrorCode: []byte{0, 0, 0, 1},
443 NewField(FieldError, []byte("You are not allowed to participate in chat.")),
450 name: "sends chat msg as emote if FieldChatOptions is set to 1",
454 Access: func() accessBitmap {
455 var bits accessBitmap
456 bits.Set(accessSendChat)
460 UserName: []byte("Testy McTest"),
462 Clients: map[uint16]*ClientConn{
465 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
471 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
480 NewField(FieldData, []byte("performed action")),
481 NewField(FieldChatOptions, []byte{0x00, 0x01}),
487 clientID: &[]byte{0, 1},
490 Type: []byte{0, 0x6a},
491 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
492 ErrorCode: []byte{0, 0, 0, 0},
494 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
498 clientID: &[]byte{0, 2},
501 Type: []byte{0, 0x6a},
502 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
503 ErrorCode: []byte{0, 0, 0, 0},
505 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
512 name: "does not send chat msg as emote if FieldChatOptions is set to 0",
516 Access: func() accessBitmap {
517 var bits accessBitmap
518 bits.Set(accessSendChat)
522 UserName: []byte("Testy McTest"),
524 Clients: map[uint16]*ClientConn{
527 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
533 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
542 NewField(FieldData, []byte("hello")),
543 NewField(FieldChatOptions, []byte{0x00, 0x00}),
549 clientID: &[]byte{0, 1},
552 Type: []byte{0, 0x6a},
553 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
554 ErrorCode: []byte{0, 0, 0, 0},
556 NewField(FieldData, []byte("\r Testy McTest: hello")),
560 clientID: &[]byte{0, 2},
563 Type: []byte{0, 0x6a},
564 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
565 ErrorCode: []byte{0, 0, 0, 0},
567 NewField(FieldData, []byte("\r Testy McTest: hello")),
574 name: "only sends chat msg to clients with accessReadChat permission",
578 Access: func() accessBitmap {
579 var bits accessBitmap
580 bits.Set(accessSendChat)
584 UserName: []byte{0x00, 0x01},
586 Clients: map[uint16]*ClientConn{
589 Access: func() accessBitmap {
590 var bits accessBitmap
591 bits.Set(accessReadChat)
598 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
607 NewField(FieldData, []byte("hai")),
613 clientID: &[]byte{0, 1},
616 Type: []byte{0, 0x6a},
617 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
618 ErrorCode: []byte{0, 0, 0, 0},
620 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
627 name: "only sends private chat msg to members of private chat",
631 Access: func() accessBitmap {
632 var bits accessBitmap
633 bits.Set(accessSendChat)
637 UserName: []byte{0x00, 0x01},
639 PrivateChats: map[uint32]*PrivateChat{
641 ClientConn: map[uint16]*ClientConn{
651 Clients: map[uint16]*ClientConn{
654 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
660 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
666 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
675 NewField(FieldData, []byte("hai")),
676 NewField(FieldChatID, []byte{0, 0, 0, 1}),
682 clientID: &[]byte{0, 1},
685 Type: []byte{0, 0x6a},
686 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
687 ErrorCode: []byte{0, 0, 0, 0},
689 NewField(FieldChatID, []byte{0, 0, 0, 1}),
690 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
694 clientID: &[]byte{0, 2},
697 Type: []byte{0, 0x6a},
698 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
699 ErrorCode: []byte{0, 0, 0, 0},
701 NewField(FieldChatID, []byte{0, 0, 0, 1}),
702 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
709 for _, tt := range tests {
710 t.Run(tt.name, func(t *testing.T) {
711 got, err := HandleChatSend(tt.args.cc, tt.args.t)
713 if (err != nil) != tt.wantErr {
714 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
717 tranAssertEqual(t, tt.want, got)
722 func TestHandleGetFileInfo(t *testing.T) {
730 wantRes []Transaction
734 name: "returns expected fields when a valid file is requested",
737 ID: &[]byte{0x00, 0x01},
741 FileRoot: func() string {
742 path, _ := os.Getwd()
743 return filepath.Join(path, "/test/config/Files")
749 TranGetFileInfo, nil,
750 NewField(FieldFileName, []byte("testfile.txt")),
751 NewField(FieldFilePath, []byte{0x00, 0x00}),
754 wantRes: []Transaction{
756 clientID: &[]byte{0, 1},
760 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
761 ErrorCode: []byte{0, 0, 0, 0},
763 NewField(FieldFileName, []byte("testfile.txt")),
764 NewField(FieldFileTypeString, []byte("Text File")),
765 NewField(FieldFileCreatorString, []byte("ttxt")),
766 NewField(FieldFileType, []byte("TEXT")),
767 NewField(FieldFileCreateDate, make([]byte, 8)),
768 NewField(FieldFileModifyDate, make([]byte, 8)),
769 NewField(FieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}),
776 for _, tt := range tests {
777 t.Run(tt.name, func(t *testing.T) {
778 gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t)
779 if (err != nil) != tt.wantErr {
780 t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr)
784 // Clear the fileWrapper timestamp fields to work around problems running the tests in multiple timezones
785 // TODO: revisit how to test this by mocking the stat calls
786 gotRes[0].Fields[4].Data = make([]byte, 8)
787 gotRes[0].Fields[5].Data = make([]byte, 8)
789 if !tranAssertEqual(t, tt.wantRes, gotRes) {
790 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)
796 func TestHandleNewFolder(t *testing.T) {
804 wantRes []Transaction
808 name: "without required permission",
812 Access: func() accessBitmap {
813 var bits accessBitmap
823 wantRes: []Transaction{
828 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
829 ErrorCode: []byte{0, 0, 0, 1},
831 NewField(FieldError, []byte("You are not allowed to create folders.")),
838 name: "when path is nested",
842 Access: func() accessBitmap {
843 var bits accessBitmap
844 bits.Set(accessCreateFolder)
853 FS: func() *MockFileStore {
854 mfs := &MockFileStore{}
855 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
856 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
862 TranNewFolder, &[]byte{0, 1},
863 NewField(FieldFileName, []byte("testFolder")),
864 NewField(FieldFilePath, []byte{
872 wantRes: []Transaction{
874 clientID: &[]byte{0, 1},
878 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
879 ErrorCode: []byte{0, 0, 0, 0},
885 name: "when path is not nested",
889 Access: func() accessBitmap {
890 var bits accessBitmap
891 bits.Set(accessCreateFolder)
900 FS: func() *MockFileStore {
901 mfs := &MockFileStore{}
902 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
903 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
909 TranNewFolder, &[]byte{0, 1},
910 NewField(FieldFileName, []byte("testFolder")),
913 wantRes: []Transaction{
915 clientID: &[]byte{0, 1},
919 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
920 ErrorCode: []byte{0, 0, 0, 0},
926 name: "when Write returns an err",
930 Access: func() accessBitmap {
931 var bits accessBitmap
932 bits.Set(accessCreateFolder)
941 FS: func() *MockFileStore {
942 mfs := &MockFileStore{}
943 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
944 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
950 TranNewFolder, &[]byte{0, 1},
951 NewField(FieldFileName, []byte("testFolder")),
952 NewField(FieldFilePath, []byte{
957 wantRes: []Transaction{},
961 name: "FieldFileName does not allow directory traversal",
965 Access: func() accessBitmap {
966 var bits accessBitmap
967 bits.Set(accessCreateFolder)
976 FS: func() *MockFileStore {
977 mfs := &MockFileStore{}
978 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
979 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
985 TranNewFolder, &[]byte{0, 1},
986 NewField(FieldFileName, []byte("../../testFolder")),
989 wantRes: []Transaction{
991 clientID: &[]byte{0, 1},
995 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
996 ErrorCode: []byte{0, 0, 0, 0},
1001 name: "FieldFilePath does not allow directory traversal",
1005 Access: func() accessBitmap {
1006 var bits accessBitmap
1007 bits.Set(accessCreateFolder)
1014 FileRoot: "/Files/",
1016 FS: func() *MockFileStore {
1017 mfs := &MockFileStore{}
1018 mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
1019 mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
1025 TranNewFolder, &[]byte{0, 1},
1026 NewField(FieldFileName, []byte("testFolder")),
1027 NewField(FieldFilePath, []byte{
1038 wantRes: []Transaction{
1040 clientID: &[]byte{0, 1},
1044 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
1045 ErrorCode: []byte{0, 0, 0, 0},
1050 for _, tt := range tests {
1051 t.Run(tt.name, func(t *testing.T) {
1052 gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
1053 if (err != nil) != tt.wantErr {
1054 t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
1058 if !tranAssertEqual(t, tt.wantRes, gotRes) {
1059 t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
1065 func TestHandleUploadFile(t *testing.T) {
1073 wantRes []Transaction
1077 name: "when request is valid and user has Upload Anywhere permission",
1082 fileTransfers: map[[4]byte]*FileTransfer{},
1084 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1086 transfers: map[int]map[[4]byte]*FileTransfer{
1090 Access: func() accessBitmap {
1091 var bits accessBitmap
1092 bits.Set(accessUploadFile)
1093 bits.Set(accessUploadAnywhere)
1099 TranUploadFile, &[]byte{0, 1},
1100 NewField(FieldFileName, []byte("testFile")),
1101 NewField(FieldFilePath, []byte{
1109 wantRes: []Transaction{
1114 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1115 ErrorCode: []byte{0, 0, 0, 0},
1117 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), // rand.Seed(1)
1124 name: "when user does not have required access",
1128 Access: func() accessBitmap {
1129 var bits accessBitmap
1135 TranUploadFile, &[]byte{0, 1},
1136 NewField(FieldFileName, []byte("testFile")),
1137 NewField(FieldFilePath, []byte{
1145 wantRes: []Transaction{
1150 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1151 ErrorCode: []byte{0, 0, 0, 1},
1153 NewField(FieldError, []byte("You are not allowed to upload files.")), // rand.Seed(1)
1160 for _, tt := range tests {
1161 t.Run(tt.name, func(t *testing.T) {
1162 gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t)
1163 if (err != nil) != tt.wantErr {
1164 t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr)
1168 tranAssertEqual(t, tt.wantRes, gotRes)
1173 func TestHandleMakeAlias(t *testing.T) {
1181 wantRes []Transaction
1185 name: "with valid input and required permissions",
1188 logger: NewTestLogger(),
1190 Access: func() accessBitmap {
1191 var bits accessBitmap
1192 bits.Set(accessMakeAlias)
1198 FileRoot: func() string {
1199 path, _ := os.Getwd()
1200 return path + "/test/config/Files"
1203 Logger: NewTestLogger(),
1204 FS: func() *MockFileStore {
1205 mfs := &MockFileStore{}
1206 path, _ := os.Getwd()
1209 path+"/test/config/Files/foo/testFile",
1210 path+"/test/config/Files/bar/testFile",
1217 TranMakeFileAlias, &[]byte{0, 1},
1218 NewField(FieldFileName, []byte("testFile")),
1219 NewField(FieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1220 NewField(FieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1223 wantRes: []Transaction{
1228 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1229 ErrorCode: []byte{0, 0, 0, 0},
1230 Fields: []Field(nil),
1236 name: "when symlink returns an error",
1239 logger: NewTestLogger(),
1241 Access: func() accessBitmap {
1242 var bits accessBitmap
1243 bits.Set(accessMakeAlias)
1249 FileRoot: func() string {
1250 path, _ := os.Getwd()
1251 return path + "/test/config/Files"
1254 Logger: NewTestLogger(),
1255 FS: func() *MockFileStore {
1256 mfs := &MockFileStore{}
1257 path, _ := os.Getwd()
1260 path+"/test/config/Files/foo/testFile",
1261 path+"/test/config/Files/bar/testFile",
1262 ).Return(errors.New("ohno"))
1268 TranMakeFileAlias, &[]byte{0, 1},
1269 NewField(FieldFileName, []byte("testFile")),
1270 NewField(FieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1271 NewField(FieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1274 wantRes: []Transaction{
1279 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1280 ErrorCode: []byte{0, 0, 0, 1},
1282 NewField(FieldError, []byte("Error creating alias")),
1289 name: "when user does not have required permission",
1292 logger: NewTestLogger(),
1294 Access: func() accessBitmap {
1295 var bits accessBitmap
1301 FileRoot: func() string {
1302 path, _ := os.Getwd()
1303 return path + "/test/config/Files"
1309 TranMakeFileAlias, &[]byte{0, 1},
1310 NewField(FieldFileName, []byte("testFile")),
1311 NewField(FieldFilePath, []byte{
1317 NewField(FieldFileNewPath, []byte{
1325 wantRes: []Transaction{
1330 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1331 ErrorCode: []byte{0, 0, 0, 1},
1333 NewField(FieldError, []byte("You are not allowed to make aliases.")),
1340 for _, tt := range tests {
1341 t.Run(tt.name, func(t *testing.T) {
1342 gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
1343 if (err != nil) != tt.wantErr {
1344 t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
1348 tranAssertEqual(t, tt.wantRes, gotRes)
1353 func TestHandleGetUser(t *testing.T) {
1361 wantRes []Transaction
1362 wantErr assert.ErrorAssertionFunc
1365 name: "when account is valid",
1369 Access: func() accessBitmap {
1370 var bits accessBitmap
1371 bits.Set(accessOpenUser)
1376 Accounts: map[string]*Account{
1380 Password: "password",
1381 Access: accessBitmap{},
1387 TranGetUser, &[]byte{0, 1},
1388 NewField(FieldUserLogin, []byte("guest")),
1391 wantRes: []Transaction{
1396 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1397 ErrorCode: []byte{0, 0, 0, 0},
1399 NewField(FieldUserName, []byte("Guest")),
1400 NewField(FieldUserLogin, encodeString([]byte("guest"))),
1401 NewField(FieldUserPassword, []byte("password")),
1402 NewField(FieldUserAccess, []byte{0, 0, 0, 0, 0, 0, 0, 0}),
1406 wantErr: assert.NoError,
1409 name: "when user does not have required permission",
1413 Access: func() accessBitmap {
1414 var bits accessBitmap
1419 Accounts: map[string]*Account{},
1423 TranGetUser, &[]byte{0, 1},
1424 NewField(FieldUserLogin, []byte("nonExistentUser")),
1427 wantRes: []Transaction{
1432 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1433 ErrorCode: []byte{0, 0, 0, 1},
1435 NewField(FieldError, []byte("You are not allowed to view accounts.")),
1439 wantErr: assert.NoError,
1442 name: "when account does not exist",
1446 Access: func() accessBitmap {
1447 var bits accessBitmap
1448 bits.Set(accessOpenUser)
1453 Accounts: map[string]*Account{},
1457 TranGetUser, &[]byte{0, 1},
1458 NewField(FieldUserLogin, []byte("nonExistentUser")),
1461 wantRes: []Transaction{
1466 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1467 ErrorCode: []byte{0, 0, 0, 1},
1469 NewField(FieldError, []byte("Account does not exist.")),
1473 wantErr: assert.NoError,
1476 for _, tt := range tests {
1477 t.Run(tt.name, func(t *testing.T) {
1478 gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
1479 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
1483 tranAssertEqual(t, tt.wantRes, gotRes)
1488 func TestHandleDeleteUser(t *testing.T) {
1496 wantRes []Transaction
1497 wantErr assert.ErrorAssertionFunc
1500 name: "when user dataFile",
1504 Access: func() accessBitmap {
1505 var bits accessBitmap
1506 bits.Set(accessDeleteUser)
1511 Accounts: map[string]*Account{
1514 Name: "Testy McTest",
1515 Password: "password",
1516 Access: accessBitmap{},
1519 FS: func() *MockFileStore {
1520 mfs := &MockFileStore{}
1521 mfs.On("Remove", "Users/testuser.yaml").Return(nil)
1527 TranDeleteUser, &[]byte{0, 1},
1528 NewField(FieldUserLogin, encodeString([]byte("testuser"))),
1531 wantRes: []Transaction{
1536 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1537 ErrorCode: []byte{0, 0, 0, 0},
1538 Fields: []Field(nil),
1541 wantErr: assert.NoError,
1544 name: "when user does not have required permission",
1548 Access: func() accessBitmap {
1549 var bits accessBitmap
1554 Accounts: map[string]*Account{},
1558 TranDeleteUser, &[]byte{0, 1},
1559 NewField(FieldUserLogin, encodeString([]byte("testuser"))),
1562 wantRes: []Transaction{
1567 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1568 ErrorCode: []byte{0, 0, 0, 1},
1570 NewField(FieldError, []byte("You are not allowed to delete accounts.")),
1574 wantErr: assert.NoError,
1577 for _, tt := range tests {
1578 t.Run(tt.name, func(t *testing.T) {
1579 gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
1580 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
1584 tranAssertEqual(t, tt.wantRes, gotRes)
1589 func TestHandleGetMsgs(t *testing.T) {
1597 wantRes []Transaction
1598 wantErr assert.ErrorAssertionFunc
1601 name: "returns news data",
1605 Access: func() accessBitmap {
1606 var bits accessBitmap
1607 bits.Set(accessNewsReadArt)
1612 FlatNews: []byte("TEST"),
1616 TranGetMsgs, &[]byte{0, 1},
1619 wantRes: []Transaction{
1624 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1625 ErrorCode: []byte{0, 0, 0, 0},
1627 NewField(FieldData, []byte("TEST")),
1631 wantErr: assert.NoError,
1634 name: "when user does not have required permission",
1638 Access: func() accessBitmap {
1639 var bits accessBitmap
1644 Accounts: map[string]*Account{},
1648 TranGetMsgs, &[]byte{0, 1},
1651 wantRes: []Transaction{
1656 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1657 ErrorCode: []byte{0, 0, 0, 1},
1659 NewField(FieldError, []byte("You are not allowed to read news.")),
1663 wantErr: assert.NoError,
1666 for _, tt := range tests {
1667 t.Run(tt.name, func(t *testing.T) {
1668 gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t)
1669 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) {
1673 tranAssertEqual(t, tt.wantRes, gotRes)
1678 func TestHandleNewUser(t *testing.T) {
1686 wantRes []Transaction
1687 wantErr assert.ErrorAssertionFunc
1690 name: "when user does not have required permission",
1694 Access: func() accessBitmap {
1695 var bits accessBitmap
1700 Accounts: map[string]*Account{},
1704 TranNewUser, &[]byte{0, 1},
1707 wantRes: []Transaction{
1712 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1713 ErrorCode: []byte{0, 0, 0, 1},
1715 NewField(FieldError, []byte("You are not allowed to create new accounts.")),
1719 wantErr: assert.NoError,
1722 name: "when user attempts to create account with greater access",
1726 Access: func() accessBitmap {
1727 var bits accessBitmap
1728 bits.Set(accessCreateUser)
1733 Accounts: map[string]*Account{},
1737 TranNewUser, &[]byte{0, 1},
1738 NewField(FieldUserLogin, []byte("userB")),
1742 var bits accessBitmap
1743 bits.Set(accessDisconUser)
1749 wantRes: []Transaction{
1754 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1755 ErrorCode: []byte{0, 0, 0, 1},
1757 NewField(FieldError, []byte("Cannot create account with more access than yourself.")),
1761 wantErr: assert.NoError,
1764 for _, tt := range tests {
1765 t.Run(tt.name, func(t *testing.T) {
1766 gotRes, err := HandleNewUser(tt.args.cc, tt.args.t)
1767 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) {
1771 tranAssertEqual(t, tt.wantRes, gotRes)
1776 func TestHandleListUsers(t *testing.T) {
1784 wantRes []Transaction
1785 wantErr assert.ErrorAssertionFunc
1788 name: "when user does not have required permission",
1792 Access: func() accessBitmap {
1793 var bits accessBitmap
1798 Accounts: map[string]*Account{},
1802 TranNewUser, &[]byte{0, 1},
1805 wantRes: []Transaction{
1810 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1811 ErrorCode: []byte{0, 0, 0, 1},
1813 NewField(FieldError, []byte("You are not allowed to view accounts.")),
1817 wantErr: assert.NoError,
1820 name: "when user has required permission",
1824 Access: func() accessBitmap {
1825 var bits accessBitmap
1826 bits.Set(accessOpenUser)
1831 Accounts: map[string]*Account{
1836 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
1842 TranGetClientInfoText, &[]byte{0, 1},
1843 NewField(FieldUserID, []byte{0, 1}),
1846 wantRes: []Transaction{
1851 ID: []byte{0, 0, 0, 0},
1852 ErrorCode: []byte{0, 0, 0, 0},
1854 NewField(FieldData, []byte{
1855 0x00, 0x04, 0x00, 0x66, 0x00, 0x05, 0x67, 0x75, 0x65, 0x73, 0x74, 0x00, 0x69, 0x00, 0x05, 0x98,
1856 0x8a, 0x9a, 0x8c, 0x8b, 0x00, 0x6e, 0x00, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1857 0x00, 0x6a, 0x00, 0x01, 0x78,
1862 wantErr: assert.NoError,
1865 for _, tt := range tests {
1866 t.Run(tt.name, func(t *testing.T) {
1867 gotRes, err := HandleListUsers(tt.args.cc, tt.args.t)
1868 if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) {
1872 tranAssertEqual(t, tt.wantRes, gotRes)
1877 func TestHandleDownloadFile(t *testing.T) {
1885 wantRes []Transaction
1886 wantErr assert.ErrorAssertionFunc
1889 name: "when user does not have required permission",
1893 Access: func() accessBitmap {
1894 var bits accessBitmap
1900 t: NewTransaction(TranDownloadFile, &[]byte{0, 1}),
1902 wantRes: []Transaction{
1907 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1908 ErrorCode: []byte{0, 0, 0, 1},
1910 NewField(FieldError, []byte("You are not allowed to download files.")),
1914 wantErr: assert.NoError,
1917 name: "with a valid file",
1920 transfers: map[int]map[[4]byte]*FileTransfer{
1924 Access: func() accessBitmap {
1925 var bits accessBitmap
1926 bits.Set(accessDownloadFile)
1932 fileTransfers: map[[4]byte]*FileTransfer{},
1934 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1936 Accounts: map[string]*Account{},
1942 NewField(FieldFileName, []byte("testfile.txt")),
1943 NewField(FieldFilePath, []byte{0x0, 0x00}),
1946 wantRes: []Transaction{
1951 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1952 ErrorCode: []byte{0, 0, 0, 0},
1954 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1955 NewField(FieldWaitingCount, []byte{0x00, 0x00}),
1956 NewField(FieldTransferSize, []byte{0x00, 0x00, 0x00, 0xa5}),
1957 NewField(FieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}),
1961 wantErr: assert.NoError,
1964 name: "when client requests to resume 1k test file at offset 256",
1967 transfers: map[int]map[[4]byte]*FileTransfer{
1969 }, Account: &Account{
1970 Access: func() accessBitmap {
1971 var bits accessBitmap
1972 bits.Set(accessDownloadFile)
1979 // FS: func() *MockFileStore {
1980 // path, _ := os.Getwd()
1981 // testFile, err := os.Open(path + "/test/config/Files/testfile-1k")
1986 // mfi := &MockFileInfo{}
1987 // mfi.On("Mode").Return(fs.FileMode(0))
1988 // mfs := &MockFileStore{}
1989 // mfs.On("Stat", "/fakeRoot/Files/testfile.txt").Return(mfi, nil)
1990 // mfs.On("Open", "/fakeRoot/Files/testfile.txt").Return(testFile, nil)
1991 // mfs.On("Stat", "/fakeRoot/Files/.info_testfile.txt").Return(nil, errors.New("no"))
1992 // mfs.On("Stat", "/fakeRoot/Files/.rsrc_testfile.txt").Return(nil, errors.New("no"))
1996 fileTransfers: map[[4]byte]*FileTransfer{},
1998 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
2000 Accounts: map[string]*Account{},
2006 NewField(FieldFileName, []byte("testfile-1k")),
2007 NewField(FieldFilePath, []byte{0x00, 0x00}),
2009 FieldFileResumeData,
2011 frd := FileResumeData{
2015 ForkCount: [2]byte{0, 2},
2016 ForkInfoList: []ForkInfoList{
2018 Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
2019 DataSize: [4]byte{0, 0, 0x01, 0x00}, // request offset 256
2024 Fork: [4]byte{0x4d, 0x41, 0x43, 0x52}, // "MACR"
2025 DataSize: [4]byte{0, 0, 0, 0},
2031 b, _ := frd.BinaryMarshal()
2037 wantRes: []Transaction{
2042 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2043 ErrorCode: []byte{0, 0, 0, 0},
2045 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
2046 NewField(FieldWaitingCount, []byte{0x00, 0x00}),
2047 NewField(FieldTransferSize, []byte{0x00, 0x00, 0x03, 0x8d}),
2048 NewField(FieldFileSize, []byte{0x00, 0x00, 0x03, 0x00}),
2052 wantErr: assert.NoError,
2055 for _, tt := range tests {
2056 t.Run(tt.name, func(t *testing.T) {
2057 gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t)
2058 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) {
2062 tranAssertEqual(t, tt.wantRes, gotRes)
2067 func TestHandleUpdateUser(t *testing.T) {
2075 wantRes []Transaction
2076 wantErr assert.ErrorAssertionFunc
2079 name: "when action is create user without required permission",
2082 logger: NewTestLogger(),
2084 Logger: NewTestLogger(),
2087 Access: func() accessBitmap {
2088 var bits accessBitmap
2096 NewField(FieldData, []byte{
2097 0x00, 0x04, // field count
2099 0x00, 0x69, // FieldUserLogin = 105
2103 0x00, 0x6a, // FieldUserPassword = 106
2107 0x00, 0x66, // FieldUserName = 102
2111 0x00, 0x6e, // FieldUserAccess = 110
2113 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2117 wantRes: []Transaction{
2122 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2123 ErrorCode: []byte{0, 0, 0, 1},
2125 NewField(FieldError, []byte("You are not allowed to create new accounts.")),
2129 wantErr: assert.NoError,
2132 name: "when action is modify user without required permission",
2135 logger: NewTestLogger(),
2137 Logger: NewTestLogger(),
2138 Accounts: map[string]*Account{
2143 Access: func() accessBitmap {
2144 var bits accessBitmap
2152 NewField(FieldData, []byte{
2153 0x00, 0x04, // field count
2155 0x00, 0x69, // FieldUserLogin = 105
2159 0x00, 0x6a, // FieldUserPassword = 106
2163 0x00, 0x66, // FieldUserName = 102
2167 0x00, 0x6e, // FieldUserAccess = 110
2169 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2173 wantRes: []Transaction{
2178 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2179 ErrorCode: []byte{0, 0, 0, 1},
2181 NewField(FieldError, []byte("You are not allowed to modify accounts.")),
2185 wantErr: assert.NoError,
2188 name: "when action is delete user without required permission",
2191 logger: NewTestLogger(),
2193 Accounts: map[string]*Account{
2198 Access: func() accessBitmap {
2199 var bits accessBitmap
2207 NewField(FieldData, []byte{
2215 wantRes: []Transaction{
2220 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2221 ErrorCode: []byte{0, 0, 0, 1},
2223 NewField(FieldError, []byte("You are not allowed to delete accounts.")),
2227 wantErr: assert.NoError,
2230 for _, tt := range tests {
2231 t.Run(tt.name, func(t *testing.T) {
2232 gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t)
2233 if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) {
2237 tranAssertEqual(t, tt.wantRes, gotRes)
2242 func TestHandleDelNewsArt(t *testing.T) {
2250 wantRes []Transaction
2251 wantErr assert.ErrorAssertionFunc
2254 name: "without required permission",
2258 Access: func() accessBitmap {
2259 var bits accessBitmap
2269 wantRes: []Transaction{
2274 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2275 ErrorCode: []byte{0, 0, 0, 1},
2277 NewField(FieldError, []byte("You are not allowed to delete news articles.")),
2281 wantErr: assert.NoError,
2284 for _, tt := range tests {
2285 t.Run(tt.name, func(t *testing.T) {
2286 gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
2287 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
2290 tranAssertEqual(t, tt.wantRes, gotRes)
2295 func TestHandleDisconnectUser(t *testing.T) {
2303 wantRes []Transaction
2304 wantErr assert.ErrorAssertionFunc
2307 name: "without required permission",
2311 Access: func() accessBitmap {
2312 var bits accessBitmap
2322 wantRes: []Transaction{
2327 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2328 ErrorCode: []byte{0, 0, 0, 1},
2330 NewField(FieldError, []byte("You are not allowed to disconnect users.")),
2334 wantErr: assert.NoError,
2337 name: "when target user has 'cannot be disconnected' priv",
2341 Clients: map[uint16]*ClientConn{
2345 Access: func() accessBitmap {
2346 var bits accessBitmap
2347 bits.Set(accessCannotBeDiscon)
2355 Access: func() accessBitmap {
2356 var bits accessBitmap
2357 bits.Set(accessDisconUser)
2365 NewField(FieldUserID, []byte{0, 1}),
2368 wantRes: []Transaction{
2373 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2374 ErrorCode: []byte{0, 0, 0, 1},
2376 NewField(FieldError, []byte("unnamed is not allowed to be disconnected.")),
2380 wantErr: assert.NoError,
2383 for _, tt := range tests {
2384 t.Run(tt.name, func(t *testing.T) {
2385 gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
2386 if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
2389 tranAssertEqual(t, tt.wantRes, gotRes)
2394 func TestHandleSendInstantMsg(t *testing.T) {
2402 wantRes []Transaction
2403 wantErr assert.ErrorAssertionFunc
2406 name: "without required permission",
2410 Access: func() accessBitmap {
2411 var bits accessBitmap
2421 wantRes: []Transaction{
2426 ID: []byte{0, 0, 0, 0},
2427 ErrorCode: []byte{0, 0, 0, 1},
2429 NewField(FieldError, []byte("You are not allowed to send private messages.")),
2433 wantErr: assert.Error,
2436 name: "when client 1 sends a message to client 2",
2440 Access: func() accessBitmap {
2441 var bits accessBitmap
2442 bits.Set(accessSendPrivMsg)
2447 UserName: []byte("User1"),
2449 Clients: map[uint16]*ClientConn{
2451 AutoReply: []byte(nil),
2452 Flags: []byte{0, 0},
2460 NewField(FieldData, []byte("hai")),
2461 NewField(FieldUserID, []byte{0, 2}),
2464 wantRes: []Transaction{
2468 NewField(FieldData, []byte("hai")),
2469 NewField(FieldUserName, []byte("User1")),
2470 NewField(FieldUserID, []byte{0, 1}),
2471 NewField(FieldOptions, []byte{0, 1}),
2474 clientID: &[]byte{0, 1},
2478 ID: []byte{0, 0, 0, 0},
2479 ErrorCode: []byte{0, 0, 0, 0},
2480 Fields: []Field(nil),
2483 wantErr: assert.NoError,
2486 name: "when client 2 has autoreply enabled",
2490 Access: func() accessBitmap {
2491 var bits accessBitmap
2492 bits.Set(accessSendPrivMsg)
2497 UserName: []byte("User1"),
2499 Clients: map[uint16]*ClientConn{
2501 Flags: []byte{0, 0},
2503 UserName: []byte("User2"),
2504 AutoReply: []byte("autohai"),
2512 NewField(FieldData, []byte("hai")),
2513 NewField(FieldUserID, []byte{0, 2}),
2516 wantRes: []Transaction{
2520 NewField(FieldData, []byte("hai")),
2521 NewField(FieldUserName, []byte("User1")),
2522 NewField(FieldUserID, []byte{0, 1}),
2523 NewField(FieldOptions, []byte{0, 1}),
2528 NewField(FieldData, []byte("autohai")),
2529 NewField(FieldUserName, []byte("User2")),
2530 NewField(FieldUserID, []byte{0, 2}),
2531 NewField(FieldOptions, []byte{0, 1}),
2534 clientID: &[]byte{0, 1},
2538 ID: []byte{0, 0, 0, 0},
2539 ErrorCode: []byte{0, 0, 0, 0},
2540 Fields: []Field(nil),
2543 wantErr: assert.NoError,
2546 name: "when client 2 has refuse private messages enabled",
2550 Access: func() accessBitmap {
2551 var bits accessBitmap
2552 bits.Set(accessSendPrivMsg)
2557 UserName: []byte("User1"),
2559 Clients: map[uint16]*ClientConn{
2561 Flags: []byte{255, 255},
2563 UserName: []byte("User2"),
2571 NewField(FieldData, []byte("hai")),
2572 NewField(FieldUserID, []byte{0, 2}),
2575 wantRes: []Transaction{
2579 NewField(FieldData, []byte("User2 does not accept private messages.")),
2580 NewField(FieldUserName, []byte("User2")),
2581 NewField(FieldUserID, []byte{0, 2}),
2582 NewField(FieldOptions, []byte{0, 2}),
2585 clientID: &[]byte{0, 1},
2589 ID: []byte{0, 0, 0, 0},
2590 ErrorCode: []byte{0, 0, 0, 0},
2591 Fields: []Field(nil),
2594 wantErr: assert.NoError,
2597 for _, tt := range tests {
2598 t.Run(tt.name, func(t *testing.T) {
2599 gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
2600 if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
2604 tranAssertEqual(t, tt.wantRes, gotRes)
2609 func TestHandleDeleteFile(t *testing.T) {
2617 wantRes []Transaction
2618 wantErr assert.ErrorAssertionFunc
2621 name: "when user does not have required permission to delete a folder",
2625 Access: func() accessBitmap {
2626 var bits accessBitmap
2632 FileRoot: func() string {
2633 return "/fakeRoot/Files"
2636 FS: func() *MockFileStore {
2637 mfi := &MockFileInfo{}
2638 mfi.On("Mode").Return(fs.FileMode(0))
2639 mfi.On("Size").Return(int64(100))
2640 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2641 mfi.On("IsDir").Return(false)
2642 mfi.On("Name").Return("testfile")
2644 mfs := &MockFileStore{}
2645 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2646 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2647 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2651 Accounts: map[string]*Account{},
2655 TranDeleteFile, &[]byte{0, 1},
2656 NewField(FieldFileName, []byte("testfile")),
2657 NewField(FieldFilePath, []byte{
2665 wantRes: []Transaction{
2670 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2671 ErrorCode: []byte{0, 0, 0, 1},
2673 NewField(FieldError, []byte("You are not allowed to delete files.")),
2677 wantErr: assert.NoError,
2680 name: "deletes all associated metadata files",
2684 Access: func() accessBitmap {
2685 var bits accessBitmap
2686 bits.Set(accessDeleteFile)
2692 FileRoot: func() string {
2693 return "/fakeRoot/Files"
2696 FS: func() *MockFileStore {
2697 mfi := &MockFileInfo{}
2698 mfi.On("Mode").Return(fs.FileMode(0))
2699 mfi.On("Size").Return(int64(100))
2700 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2701 mfi.On("IsDir").Return(false)
2702 mfi.On("Name").Return("testfile")
2704 mfs := &MockFileStore{}
2705 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2706 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2707 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2709 mfs.On("RemoveAll", "/fakeRoot/Files/aaa/testfile").Return(nil)
2710 mfs.On("Remove", "/fakeRoot/Files/aaa/testfile.incomplete").Return(nil)
2711 mfs.On("Remove", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil)
2712 mfs.On("Remove", "/fakeRoot/Files/aaa/.info_testfile").Return(nil)
2716 Accounts: map[string]*Account{},
2720 TranDeleteFile, &[]byte{0, 1},
2721 NewField(FieldFileName, []byte("testfile")),
2722 NewField(FieldFilePath, []byte{
2730 wantRes: []Transaction{
2735 ID: []byte{0x0, 0x0, 0x0, 0x0},
2736 ErrorCode: []byte{0, 0, 0, 0},
2737 Fields: []Field(nil),
2740 wantErr: assert.NoError,
2743 for _, tt := range tests {
2744 t.Run(tt.name, func(t *testing.T) {
2745 gotRes, err := HandleDeleteFile(tt.args.cc, tt.args.t)
2746 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteFile(%v, %v)", tt.args.cc, tt.args.t)) {
2750 tranAssertEqual(t, tt.wantRes, gotRes)
2752 tt.args.cc.Server.FS.(*MockFileStore).AssertExpectations(t)
2757 func TestHandleGetFileNameList(t *testing.T) {
2765 wantRes []Transaction
2766 wantErr assert.ErrorAssertionFunc
2769 name: "when FieldFilePath is a drop box, but user does not have accessViewDropBoxes ",
2773 Access: func() accessBitmap {
2774 var bits accessBitmap
2781 FileRoot: func() string {
2782 path, _ := os.Getwd()
2783 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2789 TranGetFileNameList, &[]byte{0, 1},
2790 NewField(FieldFilePath, []byte{
2794 0x64, 0x72, 0x6f, 0x70, 0x20, 0x62, 0x6f, 0x78, // "drop box"
2798 wantRes: []Transaction{
2803 ID: []byte{0, 0, 0, 0},
2804 ErrorCode: []byte{0, 0, 0, 1},
2806 NewField(FieldError, []byte("You are not allowed to view drop boxes.")),
2810 wantErr: assert.NoError,
2813 name: "with file root",
2818 FileRoot: func() string {
2819 path, _ := os.Getwd()
2820 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2826 TranGetFileNameList, &[]byte{0, 1},
2827 NewField(FieldFilePath, []byte{
2833 wantRes: []Transaction{
2838 ID: []byte{0, 0, 0, 0},
2839 ErrorCode: []byte{0, 0, 0, 0},
2842 FieldFileNameWithInfo,
2844 fnwi := FileNameWithInfo{
2845 fileNameWithInfoHeader: fileNameWithInfoHeader{
2846 Type: [4]byte{0x54, 0x45, 0x58, 0x54},
2847 Creator: [4]byte{0x54, 0x54, 0x58, 0x54},
2848 FileSize: [4]byte{0, 0, 0x04, 0},
2850 NameScript: [2]byte{},
2851 NameSize: [2]byte{0, 0x0b},
2853 Name: []byte("testfile-1k"),
2855 b, _ := io.ReadAll(&fnwi)
2862 wantErr: assert.NoError,
2865 for _, tt := range tests {
2866 t.Run(tt.name, func(t *testing.T) {
2867 gotRes, err := HandleGetFileNameList(tt.args.cc, tt.args.t)
2868 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetFileNameList(%v, %v)", tt.args.cc, tt.args.t)) {
2872 tranAssertEqual(t, tt.wantRes, gotRes)
2877 func TestHandleGetClientInfoText(t *testing.T) {
2885 wantRes []Transaction
2886 wantErr assert.ErrorAssertionFunc
2889 name: "when user does not have required permission",
2893 Access: func() accessBitmap {
2894 var bits accessBitmap
2899 Accounts: map[string]*Account{},
2903 TranGetClientInfoText, &[]byte{0, 1},
2904 NewField(FieldUserID, []byte{0, 1}),
2907 wantRes: []Transaction{
2912 ID: []byte{0, 0, 0, 0},
2913 ErrorCode: []byte{0, 0, 0, 1},
2915 NewField(FieldError, []byte("You are not allowed to get client info.")),
2919 wantErr: assert.NoError,
2922 name: "with a valid user",
2925 UserName: []byte("Testy McTest"),
2926 RemoteAddr: "1.2.3.4:12345",
2928 Access: func() accessBitmap {
2929 var bits accessBitmap
2930 bits.Set(accessGetClientInfo)
2937 Accounts: map[string]*Account{},
2938 Clients: map[uint16]*ClientConn{
2940 UserName: []byte("Testy McTest"),
2941 RemoteAddr: "1.2.3.4:12345",
2943 Access: func() accessBitmap {
2944 var bits accessBitmap
2945 bits.Set(accessGetClientInfo)
2954 transfers: map[int]map[[4]byte]*FileTransfer{
2962 TranGetClientInfoText, &[]byte{0, 1},
2963 NewField(FieldUserID, []byte{0, 1}),
2966 wantRes: []Transaction{
2971 ID: []byte{0, 0, 0, 0},
2972 ErrorCode: []byte{0, 0, 0, 0},
2974 NewField(FieldData, []byte(
2975 strings.ReplaceAll(`Nickname: Testy McTest
2978 Address: 1.2.3.4:12345
2980 -------- File Downloads ---------
2984 ------- Folder Downloads --------
2988 --------- File Uploads ----------
2992 -------- Folder Uploads ---------
2996 ------- Waiting Downloads -------
3002 NewField(FieldUserName, []byte("Testy McTest")),
3006 wantErr: assert.NoError,
3009 for _, tt := range tests {
3010 t.Run(tt.name, func(t *testing.T) {
3011 gotRes, err := HandleGetClientInfoText(tt.args.cc, tt.args.t)
3012 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetClientInfoText(%v, %v)", tt.args.cc, tt.args.t)) {
3015 tranAssertEqual(t, tt.wantRes, gotRes)
3020 func TestHandleTranAgreed(t *testing.T) {
3028 wantRes []Transaction
3029 wantErr assert.ErrorAssertionFunc
3032 name: "normal request flow",
3036 Access: func() accessBitmap {
3037 var bits accessBitmap
3038 bits.Set(accessDisconUser)
3039 bits.Set(accessAnyName)
3043 Flags: []byte{0, 1},
3044 Version: []byte{0, 1},
3046 logger: NewTestLogger(),
3049 BannerFile: "banner.jpg",
3055 NewField(FieldUserName, []byte("username")),
3056 NewField(FieldUserIconID, []byte{0, 1}),
3057 NewField(FieldOptions, []byte{0, 0}),
3060 wantRes: []Transaction{
3062 clientID: &[]byte{0, 1},
3065 Type: []byte{0, 0x7a},
3066 ID: []byte{0, 0, 0, 0},
3067 ErrorCode: []byte{0, 0, 0, 0},
3069 NewField(FieldBannerType, []byte("JPEG")),
3073 clientID: &[]byte{0, 1},
3077 ID: []byte{0, 0, 0, 0},
3078 ErrorCode: []byte{0, 0, 0, 0},
3082 wantErr: assert.NoError,
3085 for _, tt := range tests {
3086 t.Run(tt.name, func(t *testing.T) {
3087 gotRes, err := HandleTranAgreed(tt.args.cc, tt.args.t)
3088 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranAgreed(%v, %v)", tt.args.cc, tt.args.t)) {
3091 tranAssertEqual(t, tt.wantRes, gotRes)
3096 func TestHandleSetClientUserInfo(t *testing.T) {
3104 wantRes []Transaction
3105 wantErr assert.ErrorAssertionFunc
3108 name: "when client does not have accessAnyName",
3112 Access: func() accessBitmap {
3113 var bits accessBitmap
3118 UserName: []byte("Guest"),
3119 Flags: []byte{0, 1},
3121 Clients: map[uint16]*ClientConn{
3129 TranSetClientUserInfo, nil,
3130 NewField(FieldUserIconID, []byte{0, 1}),
3131 NewField(FieldUserName, []byte("NOPE")),
3134 wantRes: []Transaction{
3136 clientID: &[]byte{0, 1},
3139 Type: []byte{0x01, 0x2d},
3140 ID: []byte{0, 0, 0, 0},
3141 ErrorCode: []byte{0, 0, 0, 0},
3143 NewField(FieldUserID, []byte{0, 1}),
3144 NewField(FieldUserIconID, []byte{0, 1}),
3145 NewField(FieldUserFlags, []byte{0, 1}),
3146 NewField(FieldUserName, []byte("Guest"))},
3149 wantErr: assert.NoError,
3152 for _, tt := range tests {
3153 t.Run(tt.name, func(t *testing.T) {
3154 gotRes, err := HandleSetClientUserInfo(tt.args.cc, tt.args.t)
3155 if !tt.wantErr(t, err, fmt.Sprintf("HandleSetClientUserInfo(%v, %v)", tt.args.cc, tt.args.t)) {
3159 tranAssertEqual(t, tt.wantRes, gotRes)
3164 func TestHandleDelNewsItem(t *testing.T) {
3172 wantRes []Transaction
3173 wantErr assert.ErrorAssertionFunc
3176 name: "when user does not have permission to delete a news category",
3180 Access: accessBitmap{},
3184 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3186 Type: [2]byte{0, 3},
3193 TranDelNewsItem, nil,
3194 NewField(FieldNewsPath,
3199 0x74, 0x65, 0x73, 0x74,
3204 wantRes: []Transaction{
3206 clientID: &[]byte{0, 1},
3210 ID: []byte{0, 0, 0, 0},
3211 ErrorCode: []byte{0, 0, 0, 1},
3213 NewField(FieldError, []byte("You are not allowed to delete news categories.")),
3217 wantErr: assert.NoError,
3220 name: "when user does not have permission to delete a news folder",
3224 Access: accessBitmap{},
3228 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3230 Type: [2]byte{0, 2},
3237 TranDelNewsItem, nil,
3238 NewField(FieldNewsPath,
3243 0x74, 0x65, 0x73, 0x74,
3248 wantRes: []Transaction{
3250 clientID: &[]byte{0, 1},
3254 ID: []byte{0, 0, 0, 0},
3255 ErrorCode: []byte{0, 0, 0, 1},
3257 NewField(FieldError, []byte("You are not allowed to delete news folders.")),
3261 wantErr: assert.NoError,
3264 name: "when user deletes a news folder",
3268 Access: func() accessBitmap {
3269 var bits accessBitmap
3270 bits.Set(accessNewsDeleteFldr)
3276 ConfigDir: "/fakeConfigRoot",
3277 FS: func() *MockFileStore {
3278 mfs := &MockFileStore{}
3279 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3282 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3284 Type: [2]byte{0, 2},
3291 TranDelNewsItem, nil,
3292 NewField(FieldNewsPath,
3297 0x74, 0x65, 0x73, 0x74,
3302 wantRes: []Transaction{
3304 clientID: &[]byte{0, 1},
3308 ID: []byte{0, 0, 0, 0},
3309 ErrorCode: []byte{0, 0, 0, 0},
3313 wantErr: assert.NoError,
3316 for _, tt := range tests {
3317 t.Run(tt.name, func(t *testing.T) {
3318 gotRes, err := HandleDelNewsItem(tt.args.cc, tt.args.t)
3319 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsItem(%v, %v)", tt.args.cc, tt.args.t)) {
3322 tranAssertEqual(t, tt.wantRes, gotRes)
3327 func TestHandleTranOldPostNews(t *testing.T) {
3335 wantRes []Transaction
3336 wantErr assert.ErrorAssertionFunc
3339 name: "when user does not have required permission",
3343 Access: func() accessBitmap {
3344 var bits accessBitmap
3350 TranOldPostNews, &[]byte{0, 1},
3351 NewField(FieldData, []byte("hai")),
3354 wantRes: []Transaction{
3359 ID: []byte{0, 0, 0, 0},
3360 ErrorCode: []byte{0, 0, 0, 1},
3362 NewField(FieldError, []byte("You are not allowed to post news.")),
3366 wantErr: assert.NoError,
3369 name: "when user posts news update",
3373 Access: func() accessBitmap {
3374 var bits accessBitmap
3375 bits.Set(accessNewsPostArt)
3380 FS: func() *MockFileStore {
3381 mfs := &MockFileStore{}
3382 mfs.On("WriteFile", "/fakeConfigRoot/MessageBoard.txt", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3385 ConfigDir: "/fakeConfigRoot",
3390 TranOldPostNews, &[]byte{0, 1},
3391 NewField(FieldData, []byte("hai")),
3394 wantRes: []Transaction{
3399 ID: []byte{0, 0, 0, 0},
3400 ErrorCode: []byte{0, 0, 0, 0},
3403 wantErr: assert.NoError,
3406 for _, tt := range tests {
3407 t.Run(tt.name, func(t *testing.T) {
3408 gotRes, err := HandleTranOldPostNews(tt.args.cc, tt.args.t)
3409 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranOldPostNews(%v, %v)", tt.args.cc, tt.args.t)) {
3413 tranAssertEqual(t, tt.wantRes, gotRes)
3418 func TestHandleInviteNewChat(t *testing.T) {
3426 wantRes []Transaction
3427 wantErr assert.ErrorAssertionFunc
3430 name: "when user does not have required permission",
3434 Access: func() accessBitmap {
3435 var bits accessBitmap
3440 t: NewTransaction(TranInviteNewChat, &[]byte{0, 1}),
3442 wantRes: []Transaction{
3447 ID: []byte{0, 0, 0, 0},
3448 ErrorCode: []byte{0, 0, 0, 1},
3450 NewField(FieldError, []byte("You are not allowed to request private chat.")),
3454 wantErr: assert.NoError,
3457 name: "when userA invites userB to new private chat",
3462 Access: func() accessBitmap {
3463 var bits accessBitmap
3464 bits.Set(accessOpenChat)
3468 UserName: []byte("UserA"),
3470 Flags: []byte{0, 0},
3472 Clients: map[uint16]*ClientConn{
3475 UserName: []byte("UserB"),
3476 Flags: []byte{0, 0},
3479 PrivateChats: make(map[uint32]*PrivateChat),
3483 TranInviteNewChat, &[]byte{0, 1},
3484 NewField(FieldUserID, []byte{0, 2}),
3487 wantRes: []Transaction{
3489 clientID: &[]byte{0, 2},
3492 Type: []byte{0, 0x71},
3493 ID: []byte{0, 0, 0, 0},
3494 ErrorCode: []byte{0, 0, 0, 0},
3496 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3497 NewField(FieldUserName, []byte("UserA")),
3498 NewField(FieldUserID, []byte{0, 1}),
3503 clientID: &[]byte{0, 1},
3507 ID: []byte{0, 0, 0, 0},
3508 ErrorCode: []byte{0, 0, 0, 0},
3510 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3511 NewField(FieldUserName, []byte("UserA")),
3512 NewField(FieldUserID, []byte{0, 1}),
3513 NewField(FieldUserIconID, []byte{0, 1}),
3514 NewField(FieldUserFlags, []byte{0, 0}),
3518 wantErr: assert.NoError,
3521 name: "when userA invites userB to new private chat, but UserB has refuse private chat enabled",
3526 Access: func() accessBitmap {
3527 var bits accessBitmap
3528 bits.Set(accessOpenChat)
3532 UserName: []byte("UserA"),
3534 Flags: []byte{0, 0},
3536 Clients: map[uint16]*ClientConn{
3539 UserName: []byte("UserB"),
3540 Flags: []byte{255, 255},
3543 PrivateChats: make(map[uint32]*PrivateChat),
3547 TranInviteNewChat, &[]byte{0, 1},
3548 NewField(FieldUserID, []byte{0, 2}),
3551 wantRes: []Transaction{
3553 clientID: &[]byte{0, 1},
3556 Type: []byte{0, 0x68},
3557 ID: []byte{0, 0, 0, 0},
3558 ErrorCode: []byte{0, 0, 0, 0},
3560 NewField(FieldData, []byte("UserB does not accept private chats.")),
3561 NewField(FieldUserName, []byte("UserB")),
3562 NewField(FieldUserID, []byte{0, 2}),
3563 NewField(FieldOptions, []byte{0, 2}),
3567 clientID: &[]byte{0, 1},
3571 ID: []byte{0, 0, 0, 0},
3572 ErrorCode: []byte{0, 0, 0, 0},
3574 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3575 NewField(FieldUserName, []byte("UserA")),
3576 NewField(FieldUserID, []byte{0, 1}),
3577 NewField(FieldUserIconID, []byte{0, 1}),
3578 NewField(FieldUserFlags, []byte{0, 0}),
3582 wantErr: assert.NoError,
3585 for _, tt := range tests {
3586 t.Run(tt.name, func(t *testing.T) {
3587 gotRes, err := HandleInviteNewChat(tt.args.cc, tt.args.t)
3588 if !tt.wantErr(t, err, fmt.Sprintf("HandleInviteNewChat(%v, %v)", tt.args.cc, tt.args.t)) {
3591 tranAssertEqual(t, tt.wantRes, gotRes)
3596 func TestHandleGetNewsArtData(t *testing.T) {
3604 wantRes []Transaction
3605 wantErr assert.ErrorAssertionFunc
3608 name: "when user does not have required permission",
3612 Access: func() accessBitmap {
3613 var bits accessBitmap
3618 Accounts: map[string]*Account{},
3622 TranGetNewsArtData, &[]byte{0, 1},
3625 wantRes: []Transaction{
3630 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
3631 ErrorCode: []byte{0, 0, 0, 1},
3633 NewField(FieldError, []byte("You are not allowed to read news.")),
3637 wantErr: assert.NoError,
3640 for _, tt := range tests {
3641 t.Run(tt.name, func(t *testing.T) {
3642 gotRes, err := HandleGetNewsArtData(tt.args.cc, tt.args.t)
3643 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtData(%v, %v)", tt.args.cc, tt.args.t)) {
3646 tranAssertEqual(t, tt.wantRes, gotRes)
3651 func TestHandleGetNewsArtNameList(t *testing.T) {
3659 wantRes []Transaction
3660 wantErr assert.ErrorAssertionFunc
3663 name: "when user does not have required permission",
3667 Access: func() accessBitmap {
3668 var bits accessBitmap
3673 Accounts: map[string]*Account{},
3677 TranGetNewsArtNameList, &[]byte{0, 1},
3680 wantRes: []Transaction{
3685 ErrorCode: []byte{0, 0, 0, 1},
3687 NewField(FieldError, []byte("You are not allowed to read news.")),
3691 wantErr: assert.NoError,
3694 name: "when user has required access",
3698 Access: func() accessBitmap {
3699 var bits accessBitmap
3700 bits.Set(accessNewsReadArt)
3705 ThreadedNews: &ThreadedNews{
3706 Categories: map[string]NewsCategoryListData15{
3707 "Example Category": {
3708 Type: [2]byte{0, 2},
3710 Articles: map[uint32]*NewsArtData{
3713 Poster: "testPoster",
3720 DeleteSN: [4]byte{},
3725 //Accounts: map[string]*Account{
3730 // Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
3736 TranGetNewsArtNameList,
3738 // 00000000 00 01 00 00 10 45 78 61 6d 70 6c 65 20 43 61 74 |.....Example Cat|
3739 // 00000010 65 67 6f 72 79 |egory|
3740 NewField(FieldNewsPath, []byte{
3741 0x00, 0x01, 0x00, 0x00, 0x10, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x43, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x79,
3745 wantRes: []Transaction{
3750 ErrorCode: []byte{0, 0, 0, 0},
3752 NewField(FieldNewsArtListData, []byte{
3753 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
3754 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
3755 0x09, 0x74, 0x65, 0x73, 0x74, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x50,
3756 0x6f, 0x73, 0x74, 0x65, 0x72, 0x0a, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e,
3763 wantErr: assert.NoError,
3766 for _, tt := range tests {
3767 t.Run(tt.name, func(t *testing.T) {
3768 gotRes, err := HandleGetNewsArtNameList(tt.args.cc, tt.args.t)
3769 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtNameList(%v, %v)", tt.args.cc, tt.args.t)) {
3772 tranAssertEqual(t, tt.wantRes, gotRes)
3777 func TestHandleNewNewsFldr(t *testing.T) {
3785 wantRes []Transaction
3786 wantErr assert.ErrorAssertionFunc
3789 name: "when user does not have required permission",
3793 Access: func() accessBitmap {
3794 var bits accessBitmap
3799 Accounts: map[string]*Account{},
3803 TranGetNewsArtNameList, &[]byte{0, 1},
3806 wantRes: []Transaction{
3811 ErrorCode: []byte{0, 0, 0, 1},
3813 NewField(FieldError, []byte("You are not allowed to create news folders.")),
3817 wantErr: assert.NoError,
3820 name: "with a valid request",
3824 Access: func() accessBitmap {
3825 var bits accessBitmap
3826 bits.Set(accessNewsCreateFldr)
3830 logger: NewTestLogger(),
3833 ConfigDir: "/fakeConfigRoot",
3834 FS: func() *MockFileStore {
3835 mfs := &MockFileStore{}
3836 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil)
3839 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3841 Type: [2]byte{0, 2},
3843 SubCats: make(map[string]NewsCategoryListData15),
3849 TranGetNewsArtNameList, &[]byte{0, 1},
3850 NewField(FieldFileName, []byte("testFolder")),
3851 NewField(FieldNewsPath,
3856 0x74, 0x65, 0x73, 0x74,
3861 wantRes: []Transaction{
3863 clientID: &[]byte{0, 1},
3867 ID: []byte{0, 0, 0, 0},
3868 ErrorCode: []byte{0, 0, 0, 0},
3872 wantErr: assert.NoError,
3875 // Name: "when there is an error writing the threaded news file",
3878 // Account: &Account{
3879 // Access: func() accessBitmap {
3880 // var bits accessBitmap
3881 // bits.Set(accessNewsCreateFldr)
3885 // logger: NewTestLogger(),
3886 // ID: &[]byte{0, 1},
3888 // ConfigDir: "/fakeConfigRoot",
3889 // FS: func() *MockFileStore {
3890 // mfs := &MockFileStore{}
3891 // mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(os.ErrNotExist)
3894 // ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3896 // Type: []byte{0, 2},
3900 // SubCats: make(map[string]NewsCategoryListData15),
3905 // t: NewTransaction(
3906 // TranGetNewsArtNameList, &[]byte{0, 1},
3907 // NewField(FieldFileName, []byte("testFolder")),
3908 // NewField(FieldNewsPath,
3913 // 0x74, 0x65, 0x73, 0x74,
3918 // wantRes: []Transaction{
3920 // clientID: &[]byte{0, 1},
3923 // Type: []byte{0, 0},
3924 // ErrorCode: []byte{0, 0, 0, 1},
3926 // NewField(FieldError, []byte("Error creating news folder.")),
3930 // wantErr: assert.Error,
3933 for _, tt := range tests {
3934 t.Run(tt.name, func(t *testing.T) {
3935 gotRes, err := HandleNewNewsFldr(tt.args.cc, tt.args.t)
3936 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewNewsFldr(%v, %v)", tt.args.cc, tt.args.t)) {
3939 tranAssertEqual(t, tt.wantRes, gotRes)
3944 func TestHandleDownloadBanner(t *testing.T) {
3952 wantRes []Transaction
3953 wantErr assert.ErrorAssertionFunc
3955 // TODO: Add test cases.
3957 for _, tt := range tests {
3958 t.Run(tt.name, func(t *testing.T) {
3959 gotRes, err := HandleDownloadBanner(tt.args.cc, tt.args.t)
3960 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)) {
3963 assert.Equalf(t, tt.wantRes, gotRes, "HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)