6 "github.com/stretchr/testify/assert"
16 func TestHandleSetChatSubject(t *testing.T) {
28 name: "sends chat subject to private chat members",
31 UserName: []byte{0x00, 0x01},
33 PrivateChats: map[uint32]*PrivateChat{
36 ClientConn: map[uint16]*ClientConn{
39 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
45 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
52 Clients: map[uint16]*ClientConn{
55 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
61 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
71 Type: []byte{0, 0x6a},
72 ID: []byte{0, 0, 0, 1},
73 ErrorCode: []byte{0, 0, 0, 0},
75 NewField(fieldChatID, []byte{0, 0, 0, 1}),
76 NewField(fieldChatSubject, []byte("Test Subject")),
82 clientID: &[]byte{0, 1},
85 Type: []byte{0, 0x77},
86 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
87 ErrorCode: []byte{0, 0, 0, 0},
89 NewField(fieldChatID, []byte{0, 0, 0, 1}),
90 NewField(fieldChatSubject, []byte("Test Subject")),
94 clientID: &[]byte{0, 2},
97 Type: []byte{0, 0x77},
98 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
99 ErrorCode: []byte{0, 0, 0, 0},
101 NewField(fieldChatID, []byte{0, 0, 0, 1}),
102 NewField(fieldChatSubject, []byte("Test Subject")),
109 for _, tt := range tests {
110 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
112 t.Run(tt.name, func(t *testing.T) {
113 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
114 if (err != nil) != tt.wantErr {
115 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
118 if !assert.Equal(t, tt.want, got) {
119 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
125 func TestHandleLeaveChat(t *testing.T) {
137 name: "returns expected transactions",
142 PrivateChats: map[uint32]*PrivateChat{
144 ClientConn: map[uint16]*ClientConn{
147 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
153 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
160 Clients: map[uint16]*ClientConn{
163 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
169 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
176 t: NewTransaction(tranDeleteUser, nil, NewField(fieldChatID, []byte{0, 0, 0, 1})),
180 clientID: &[]byte{0, 1},
183 Type: []byte{0, 0x76},
184 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
185 ErrorCode: []byte{0, 0, 0, 0},
187 NewField(fieldChatID, []byte{0, 0, 0, 1}),
188 NewField(fieldUserID, []byte{0, 2}),
195 for _, tt := range tests {
197 t.Run(tt.name, func(t *testing.T) {
198 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
199 if (err != nil) != tt.wantErr {
200 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
203 if !assert.Equal(t, tt.want, got) {
204 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
210 func TestHandleGetUserNameList(t *testing.T) {
222 name: "replies with userlist transaction",
228 Clients: map[uint16]*ClientConn{
232 Flags: &[]byte{0, 3},
233 UserName: []byte{0, 4},
239 Flags: &[]byte{0, 3},
240 UserName: []byte{0, 4},
246 Flags: &[]byte{0, 3},
247 UserName: []byte{0, 4},
254 ID: []byte{0, 0, 0, 1},
260 clientID: &[]byte{1, 1},
264 ID: []byte{0, 0, 0, 1},
265 ErrorCode: []byte{0, 0, 0, 0},
268 fieldUsernameWithInfo,
269 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
272 fieldUsernameWithInfo,
273 []byte{00, 02, 00, 02, 00, 03, 00, 02, 00, 04},
281 for _, tt := range tests {
282 t.Run(tt.name, func(t *testing.T) {
283 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
284 if (err != nil) != tt.wantErr {
285 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
288 assert.Equal(t, tt.want, got)
293 func TestHandleChatSend(t *testing.T) {
305 name: "sends chat msg transaction to all clients",
309 Access: func() *[]byte {
310 var bits accessBitmap
311 bits.Set(accessSendChat)
316 UserName: []byte{0x00, 0x01},
318 Clients: map[uint16]*ClientConn{
321 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
327 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
336 NewField(fieldData, []byte("hai")),
342 clientID: &[]byte{0, 1},
345 Type: []byte{0, 0x6a},
346 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
347 ErrorCode: []byte{0, 0, 0, 0},
349 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
353 clientID: &[]byte{0, 2},
356 Type: []byte{0, 0x6a},
357 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
358 ErrorCode: []byte{0, 0, 0, 0},
360 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
367 name: "when user does not have required permission",
371 Access: func() *[]byte {
372 var bits accessBitmap
378 Accounts: map[string]*Account{},
382 tranChatSend, &[]byte{0, 1},
383 NewField(fieldData, []byte("hai")),
390 Type: []byte{0, 0x00},
391 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
392 ErrorCode: []byte{0, 0, 0, 1},
394 NewField(fieldError, []byte("You are not allowed to participate in chat.")),
401 name: "sends chat msg as emote if fieldChatOptions is set",
405 Access: func() *[]byte {
406 var bits accessBitmap
407 bits.Set(accessSendChat)
412 UserName: []byte("Testy McTest"),
414 Clients: map[uint16]*ClientConn{
417 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
423 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
432 NewField(fieldData, []byte("performed action")),
433 NewField(fieldChatOptions, []byte{0x00, 0x01}),
439 clientID: &[]byte{0, 1},
442 Type: []byte{0, 0x6a},
443 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
444 ErrorCode: []byte{0, 0, 0, 0},
446 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
450 clientID: &[]byte{0, 2},
453 Type: []byte{0, 0x6a},
454 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
455 ErrorCode: []byte{0, 0, 0, 0},
457 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
464 name: "only sends chat msg to clients with accessReadChat permission",
468 Access: func() *[]byte {
469 var bits accessBitmap
470 bits.Set(accessSendChat)
475 UserName: []byte{0x00, 0x01},
477 Clients: map[uint16]*ClientConn{
480 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
486 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
495 NewField(fieldData, []byte("hai")),
501 clientID: &[]byte{0, 1},
504 Type: []byte{0, 0x6a},
505 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
506 ErrorCode: []byte{0, 0, 0, 0},
508 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
515 name: "only sends private chat msg to members of private chat",
519 Access: func() *[]byte {
520 var bits accessBitmap
521 bits.Set(accessSendChat)
526 UserName: []byte{0x00, 0x01},
528 PrivateChats: map[uint32]*PrivateChat{
530 ClientConn: map[uint16]*ClientConn{
540 Clients: map[uint16]*ClientConn{
543 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
549 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
555 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
564 NewField(fieldData, []byte("hai")),
565 NewField(fieldChatID, []byte{0, 0, 0, 1}),
571 clientID: &[]byte{0, 1},
574 Type: []byte{0, 0x6a},
575 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
576 ErrorCode: []byte{0, 0, 0, 0},
578 NewField(fieldChatID, []byte{0, 0, 0, 1}),
579 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
583 clientID: &[]byte{0, 2},
586 Type: []byte{0, 0x6a},
587 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
588 ErrorCode: []byte{0, 0, 0, 0},
590 NewField(fieldChatID, []byte{0, 0, 0, 1}),
591 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
598 for _, tt := range tests {
599 t.Run(tt.name, func(t *testing.T) {
600 got, err := HandleChatSend(tt.args.cc, tt.args.t)
602 if (err != nil) != tt.wantErr {
603 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
606 tranAssertEqual(t, tt.want, got)
611 func TestHandleGetFileInfo(t *testing.T) {
612 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
621 wantRes []Transaction
625 name: "returns expected fields when a valid file is requested",
628 ID: &[]byte{0x00, 0x01},
632 FileRoot: func() string {
633 path, _ := os.Getwd()
634 return filepath.Join(path, "/test/config/Files")
640 tranGetFileInfo, nil,
641 NewField(fieldFileName, []byte("testfile.txt")),
642 NewField(fieldFilePath, []byte{0x00, 0x00}),
645 wantRes: []Transaction{
647 clientID: &[]byte{0, 1},
650 Type: []byte{0, 0xce},
651 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
652 ErrorCode: []byte{0, 0, 0, 0},
654 NewField(fieldFileName, []byte("testfile.txt")),
655 NewField(fieldFileTypeString, []byte("Text File")),
656 NewField(fieldFileCreatorString, []byte("ttxt")),
657 NewField(fieldFileComment, []byte{}),
658 NewField(fieldFileType, []byte("TEXT")),
659 NewField(fieldFileCreateDate, make([]byte, 8)),
660 NewField(fieldFileModifyDate, make([]byte, 8)),
661 NewField(fieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}),
668 for _, tt := range tests {
669 t.Run(tt.name, func(t *testing.T) {
670 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
672 gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t)
673 if (err != nil) != tt.wantErr {
674 t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr)
678 // Clear the fileWrapper timestamp fields to work around problems running the tests in multiple timezones
679 // TODO: revisit how to test this by mocking the stat calls
680 gotRes[0].Fields[5].Data = make([]byte, 8)
681 gotRes[0].Fields[6].Data = make([]byte, 8)
682 if !assert.Equal(t, tt.wantRes, gotRes) {
683 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)
689 func TestHandleNewFolder(t *testing.T) {
697 wantRes []Transaction
701 name: "without required permission",
705 Access: func() *[]byte {
706 var bits accessBitmap
717 wantRes: []Transaction{
721 Type: []byte{0, 0x00},
722 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
723 ErrorCode: []byte{0, 0, 0, 1},
725 NewField(fieldError, []byte("You are not allowed to create folders.")),
732 name: "when path is nested",
736 Access: func() *[]byte {
737 var bits accessBitmap
738 bits.Set(accessCreateFolder)
748 FS: func() *MockFileStore {
749 mfs := &MockFileStore{}
750 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
751 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
757 tranNewFolder, &[]byte{0, 1},
758 NewField(fieldFileName, []byte("testFolder")),
759 NewField(fieldFilePath, []byte{
767 wantRes: []Transaction{
769 clientID: &[]byte{0, 1},
772 Type: []byte{0, 0xcd},
773 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
774 ErrorCode: []byte{0, 0, 0, 0},
780 name: "when path is not nested",
784 Access: func() *[]byte {
785 var bits accessBitmap
786 bits.Set(accessCreateFolder)
796 FS: func() *MockFileStore {
797 mfs := &MockFileStore{}
798 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
799 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
805 tranNewFolder, &[]byte{0, 1},
806 NewField(fieldFileName, []byte("testFolder")),
809 wantRes: []Transaction{
811 clientID: &[]byte{0, 1},
814 Type: []byte{0, 0xcd},
815 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
816 ErrorCode: []byte{0, 0, 0, 0},
822 name: "when UnmarshalBinary returns an err",
826 Access: func() *[]byte {
827 var bits accessBitmap
828 bits.Set(accessCreateFolder)
838 FS: func() *MockFileStore {
839 mfs := &MockFileStore{}
840 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
841 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
847 tranNewFolder, &[]byte{0, 1},
848 NewField(fieldFileName, []byte("testFolder")),
849 NewField(fieldFilePath, []byte{
854 wantRes: []Transaction{},
858 name: "fieldFileName does not allow directory traversal",
862 Access: func() *[]byte {
863 var bits accessBitmap
864 bits.Set(accessCreateFolder)
874 FS: func() *MockFileStore {
875 mfs := &MockFileStore{}
876 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
877 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
883 tranNewFolder, &[]byte{0, 1},
884 NewField(fieldFileName, []byte("../../testFolder")),
887 wantRes: []Transaction{
889 clientID: &[]byte{0, 1},
892 Type: []byte{0, 0xcd},
893 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
894 ErrorCode: []byte{0, 0, 0, 0},
899 name: "fieldFilePath does not allow directory traversal",
903 Access: func() *[]byte {
904 var bits accessBitmap
905 bits.Set(accessCreateFolder)
915 FS: func() *MockFileStore {
916 mfs := &MockFileStore{}
917 mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
918 mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
924 tranNewFolder, &[]byte{0, 1},
925 NewField(fieldFileName, []byte("testFolder")),
926 NewField(fieldFilePath, []byte{
937 wantRes: []Transaction{
939 clientID: &[]byte{0, 1},
942 Type: []byte{0, 0xcd},
943 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
944 ErrorCode: []byte{0, 0, 0, 0},
949 for _, tt := range tests {
950 t.Run(tt.name, func(t *testing.T) {
952 gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
953 if (err != nil) != tt.wantErr {
954 t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
958 if !tranAssertEqual(t, tt.wantRes, gotRes) {
959 t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
965 func TestHandleUploadFile(t *testing.T) {
973 wantRes []Transaction
977 name: "when request is valid and user has Upload Anywhere permission",
981 FileTransfers: map[uint32]*FileTransfer{},
984 Access: func() *[]byte {
985 var bits accessBitmap
986 bits.Set(accessUploadFile)
987 bits.Set(accessUploadAnywhere)
994 tranUploadFile, &[]byte{0, 1},
995 NewField(fieldFileName, []byte("testFile")),
996 NewField(fieldFilePath, []byte{
1004 wantRes: []Transaction{
1008 Type: []byte{0, 0xcb},
1009 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1010 ErrorCode: []byte{0, 0, 0, 0},
1012 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), // rand.Seed(1)
1019 name: "when user does not have required access",
1023 Access: func() *[]byte {
1024 var bits accessBitmap
1030 FileTransfers: map[uint32]*FileTransfer{},
1034 tranUploadFile, &[]byte{0, 1},
1035 NewField(fieldFileName, []byte("testFile")),
1036 NewField(fieldFilePath, []byte{
1044 wantRes: []Transaction{
1048 Type: []byte{0, 0x00},
1049 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1050 ErrorCode: []byte{0, 0, 0, 1},
1052 NewField(fieldError, []byte("You are not allowed to upload files.")), // rand.Seed(1)
1059 for _, tt := range tests {
1060 t.Run(tt.name, func(t *testing.T) {
1062 gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t)
1063 if (err != nil) != tt.wantErr {
1064 t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr)
1068 tranAssertEqual(t, tt.wantRes, gotRes)
1074 func TestHandleMakeAlias(t *testing.T) {
1082 wantRes []Transaction
1086 name: "with valid input and required permissions",
1089 logger: NewTestLogger(),
1091 Access: func() *[]byte {
1092 var bits accessBitmap
1093 bits.Set(accessMakeAlias)
1100 FileRoot: func() string {
1101 path, _ := os.Getwd()
1102 return path + "/test/config/Files"
1105 Logger: NewTestLogger(),
1106 FS: func() *MockFileStore {
1107 mfs := &MockFileStore{}
1108 path, _ := os.Getwd()
1111 path+"/test/config/Files/foo/testFile",
1112 path+"/test/config/Files/bar/testFile",
1119 tranMakeFileAlias, &[]byte{0, 1},
1120 NewField(fieldFileName, []byte("testFile")),
1121 NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1122 NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1125 wantRes: []Transaction{
1129 Type: []byte{0, 0xd1},
1130 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1131 ErrorCode: []byte{0, 0, 0, 0},
1132 Fields: []Field(nil),
1138 name: "when symlink returns an error",
1141 logger: NewTestLogger(),
1143 Access: func() *[]byte {
1144 var bits accessBitmap
1145 bits.Set(accessMakeAlias)
1152 FileRoot: func() string {
1153 path, _ := os.Getwd()
1154 return path + "/test/config/Files"
1157 Logger: NewTestLogger(),
1158 FS: func() *MockFileStore {
1159 mfs := &MockFileStore{}
1160 path, _ := os.Getwd()
1163 path+"/test/config/Files/foo/testFile",
1164 path+"/test/config/Files/bar/testFile",
1165 ).Return(errors.New("ohno"))
1171 tranMakeFileAlias, &[]byte{0, 1},
1172 NewField(fieldFileName, []byte("testFile")),
1173 NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1174 NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1177 wantRes: []Transaction{
1181 Type: []byte{0, 0x00},
1182 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1183 ErrorCode: []byte{0, 0, 0, 1},
1185 NewField(fieldError, []byte("Error creating alias")),
1192 name: "when user does not have required permission",
1195 logger: NewTestLogger(),
1197 Access: func() *[]byte {
1198 var bits accessBitmap
1205 FileRoot: func() string {
1206 path, _ := os.Getwd()
1207 return path + "/test/config/Files"
1213 tranMakeFileAlias, &[]byte{0, 1},
1214 NewField(fieldFileName, []byte("testFile")),
1215 NewField(fieldFilePath, []byte{
1221 NewField(fieldFileNewPath, []byte{
1229 wantRes: []Transaction{
1233 Type: []byte{0, 0x00},
1234 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1235 ErrorCode: []byte{0, 0, 0, 1},
1237 NewField(fieldError, []byte("You are not allowed to make aliases.")),
1244 for _, tt := range tests {
1245 t.Run(tt.name, func(t *testing.T) {
1246 gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
1247 if (err != nil) != tt.wantErr {
1248 t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
1252 tranAssertEqual(t, tt.wantRes, gotRes)
1257 func TestHandleGetUser(t *testing.T) {
1265 wantRes []Transaction
1266 wantErr assert.ErrorAssertionFunc
1269 name: "when account is valid",
1273 Access: func() *[]byte {
1274 var bits accessBitmap
1275 bits.Set(accessOpenUser)
1281 Accounts: map[string]*Account{
1285 Password: "password",
1292 tranGetUser, &[]byte{0, 1},
1293 NewField(fieldUserLogin, []byte("guest")),
1296 wantRes: []Transaction{
1300 Type: []byte{0x01, 0x60},
1301 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1302 ErrorCode: []byte{0, 0, 0, 0},
1304 NewField(fieldUserName, []byte("Guest")),
1305 NewField(fieldUserLogin, negateString([]byte("guest"))),
1306 NewField(fieldUserPassword, []byte("password")),
1307 NewField(fieldUserAccess, []byte{1}),
1311 wantErr: assert.NoError,
1314 name: "when user does not have required permission",
1318 Access: func() *[]byte {
1319 var bits accessBitmap
1325 Accounts: map[string]*Account{},
1329 tranGetUser, &[]byte{0, 1},
1330 NewField(fieldUserLogin, []byte("nonExistentUser")),
1333 wantRes: []Transaction{
1337 Type: []byte{0, 0x00},
1338 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1339 ErrorCode: []byte{0, 0, 0, 1},
1341 NewField(fieldError, []byte("You are not allowed to view accounts.")),
1345 wantErr: assert.NoError,
1348 name: "when account does not exist",
1352 Access: func() *[]byte {
1353 var bits accessBitmap
1354 bits.Set(accessOpenUser)
1360 Accounts: map[string]*Account{},
1364 tranGetUser, &[]byte{0, 1},
1365 NewField(fieldUserLogin, []byte("nonExistentUser")),
1368 wantRes: []Transaction{
1372 Type: []byte{0, 0x00},
1373 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1374 ErrorCode: []byte{0, 0, 0, 1},
1376 NewField(fieldError, []byte("Account does not exist.")),
1380 wantErr: assert.NoError,
1383 for _, tt := range tests {
1384 t.Run(tt.name, func(t *testing.T) {
1385 gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
1386 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
1390 tranAssertEqual(t, tt.wantRes, gotRes)
1395 func TestHandleDeleteUser(t *testing.T) {
1403 wantRes []Transaction
1404 wantErr assert.ErrorAssertionFunc
1407 name: "when user dataFile",
1411 Access: func() *[]byte {
1412 var bits accessBitmap
1413 bits.Set(accessDeleteUser)
1419 Accounts: map[string]*Account{
1422 Name: "Testy McTest",
1423 Password: "password",
1427 FS: func() *MockFileStore {
1428 mfs := &MockFileStore{}
1429 mfs.On("Remove", "Users/testuser.yaml").Return(nil)
1435 tranDeleteUser, &[]byte{0, 1},
1436 NewField(fieldUserLogin, negateString([]byte("testuser"))),
1439 wantRes: []Transaction{
1443 Type: []byte{0x1, 0x5f},
1444 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1445 ErrorCode: []byte{0, 0, 0, 0},
1446 Fields: []Field(nil),
1449 wantErr: assert.NoError,
1452 name: "when user does not have required permission",
1456 Access: func() *[]byte {
1457 var bits accessBitmap
1463 Accounts: map[string]*Account{},
1467 tranDeleteUser, &[]byte{0, 1},
1468 NewField(fieldUserLogin, negateString([]byte("testuser"))),
1471 wantRes: []Transaction{
1475 Type: []byte{0, 0x00},
1476 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1477 ErrorCode: []byte{0, 0, 0, 1},
1479 NewField(fieldError, []byte("You are not allowed to delete accounts.")),
1483 wantErr: assert.NoError,
1486 for _, tt := range tests {
1487 t.Run(tt.name, func(t *testing.T) {
1488 gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
1489 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
1493 tranAssertEqual(t, tt.wantRes, gotRes)
1498 func TestHandleGetMsgs(t *testing.T) {
1506 wantRes []Transaction
1507 wantErr assert.ErrorAssertionFunc
1510 name: "returns news data",
1514 Access: func() *[]byte {
1515 var bits accessBitmap
1516 bits.Set(accessNewsReadArt)
1522 FlatNews: []byte("TEST"),
1526 tranGetMsgs, &[]byte{0, 1},
1529 wantRes: []Transaction{
1533 Type: []byte{0, 0x65},
1534 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1535 ErrorCode: []byte{0, 0, 0, 0},
1537 NewField(fieldData, []byte("TEST")),
1541 wantErr: assert.NoError,
1544 name: "when user does not have required permission",
1548 Access: func() *[]byte {
1549 var bits accessBitmap
1555 Accounts: map[string]*Account{},
1559 tranGetMsgs, &[]byte{0, 1},
1562 wantRes: []Transaction{
1566 Type: []byte{0, 0x00},
1567 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1568 ErrorCode: []byte{0, 0, 0, 1},
1570 NewField(fieldError, []byte("You are not allowed to read news.")),
1574 wantErr: assert.NoError,
1577 for _, tt := range tests {
1578 t.Run(tt.name, func(t *testing.T) {
1579 gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t)
1580 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) {
1584 tranAssertEqual(t, tt.wantRes, gotRes)
1589 func TestHandleNewUser(t *testing.T) {
1597 wantRes []Transaction
1598 wantErr assert.ErrorAssertionFunc
1601 name: "when user does not have required permission",
1605 Access: func() *[]byte {
1606 var bits accessBitmap
1612 Accounts: map[string]*Account{},
1616 tranNewUser, &[]byte{0, 1},
1619 wantRes: []Transaction{
1623 Type: []byte{0, 0x00},
1624 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1625 ErrorCode: []byte{0, 0, 0, 1},
1627 NewField(fieldError, []byte("You are not allowed to create new accounts.")),
1631 wantErr: assert.NoError,
1634 for _, tt := range tests {
1635 t.Run(tt.name, func(t *testing.T) {
1636 gotRes, err := HandleNewUser(tt.args.cc, tt.args.t)
1637 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) {
1641 tranAssertEqual(t, tt.wantRes, gotRes)
1646 func TestHandleListUsers(t *testing.T) {
1654 wantRes []Transaction
1655 wantErr assert.ErrorAssertionFunc
1658 name: "when user does not have required permission",
1662 Access: func() *[]byte {
1663 var bits accessBitmap
1669 Accounts: map[string]*Account{},
1673 tranNewUser, &[]byte{0, 1},
1676 wantRes: []Transaction{
1680 Type: []byte{0, 0x00},
1681 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1682 ErrorCode: []byte{0, 0, 0, 1},
1684 NewField(fieldError, []byte("You are not allowed to view accounts.")),
1688 wantErr: assert.NoError,
1691 for _, tt := range tests {
1692 t.Run(tt.name, func(t *testing.T) {
1693 gotRes, err := HandleListUsers(tt.args.cc, tt.args.t)
1694 if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) {
1698 tranAssertEqual(t, tt.wantRes, gotRes)
1703 func TestHandleDownloadFile(t *testing.T) {
1711 wantRes []Transaction
1712 wantErr assert.ErrorAssertionFunc
1715 name: "when user does not have required permission",
1719 Access: func() *[]byte {
1720 var bits accessBitmap
1727 t: NewTransaction(tranDownloadFile, &[]byte{0, 1}),
1729 wantRes: []Transaction{
1733 Type: []byte{0, 0x00},
1734 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1735 ErrorCode: []byte{0, 0, 0, 1},
1737 NewField(fieldError, []byte("You are not allowed to download files.")),
1741 wantErr: assert.NoError,
1744 name: "with a valid file",
1747 Transfers: make(map[int][]*FileTransfer),
1749 Access: func() *[]byte {
1750 var bits accessBitmap
1751 bits.Set(accessDownloadFile)
1758 FileTransfers: make(map[uint32]*FileTransfer),
1760 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1762 Accounts: map[string]*Account{},
1768 NewField(fieldFileName, []byte("testfile.txt")),
1769 NewField(fieldFilePath, []byte{0x0, 0x00}),
1772 wantRes: []Transaction{
1776 Type: []byte{0, 0x2},
1777 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1778 ErrorCode: []byte{0, 0, 0, 0},
1780 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1781 NewField(fieldWaitingCount, []byte{0x00, 0x00}),
1782 NewField(fieldTransferSize, []byte{0x00, 0x00, 0x00, 0xa5}),
1783 NewField(fieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}),
1787 wantErr: assert.NoError,
1790 name: "when client requests to resume 1k test file at offset 256",
1793 Transfers: make(map[int][]*FileTransfer),
1795 Access: func() *[]byte {
1796 var bits accessBitmap
1797 bits.Set(accessDownloadFile)
1804 // FS: func() *MockFileStore {
1805 // path, _ := os.Getwd()
1806 // testFile, err := os.Open(path + "/test/config/Files/testfile-1k")
1811 // mfi := &MockFileInfo{}
1812 // mfi.On("Mode").Return(fs.FileMode(0))
1813 // mfs := &MockFileStore{}
1814 // mfs.On("Stat", "/fakeRoot/Files/testfile.txt").Return(mfi, nil)
1815 // mfs.On("Open", "/fakeRoot/Files/testfile.txt").Return(testFile, nil)
1816 // mfs.On("Stat", "/fakeRoot/Files/.info_testfile.txt").Return(nil, errors.New("no"))
1817 // mfs.On("Stat", "/fakeRoot/Files/.rsrc_testfile.txt").Return(nil, errors.New("no"))
1821 FileTransfers: make(map[uint32]*FileTransfer),
1823 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1825 Accounts: map[string]*Account{},
1831 NewField(fieldFileName, []byte("testfile-1k")),
1832 NewField(fieldFilePath, []byte{0x00, 0x00}),
1834 fieldFileResumeData,
1836 frd := FileResumeData{
1840 ForkCount: [2]byte{0, 2},
1841 ForkInfoList: []ForkInfoList{
1843 Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
1844 DataSize: [4]byte{0, 0, 0x01, 0x00}, // request offset 256
1849 Fork: [4]byte{0x4d, 0x41, 0x43, 0x52}, // "MACR"
1850 DataSize: [4]byte{0, 0, 0, 0},
1856 b, _ := frd.BinaryMarshal()
1862 wantRes: []Transaction{
1866 Type: []byte{0, 0x2},
1867 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1868 ErrorCode: []byte{0, 0, 0, 0},
1870 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1871 NewField(fieldWaitingCount, []byte{0x00, 0x00}),
1872 NewField(fieldTransferSize, []byte{0x00, 0x00, 0x03, 0x8d}),
1873 NewField(fieldFileSize, []byte{0x00, 0x00, 0x03, 0x00}),
1877 wantErr: assert.NoError,
1880 for _, tt := range tests {
1881 t.Run(tt.name, func(t *testing.T) {
1882 gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t)
1883 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) {
1887 tranAssertEqual(t, tt.wantRes, gotRes)
1892 func TestHandleUpdateUser(t *testing.T) {
1900 wantRes []Transaction
1901 wantErr assert.ErrorAssertionFunc
1904 name: "when action is create user without required permission",
1907 logger: NewTestLogger(),
1909 Logger: NewTestLogger(),
1912 Access: func() *[]byte {
1913 var bits accessBitmap
1922 NewField(fieldData, []byte{
1923 0x00, 0x04, // field count
1925 0x00, 0x69, // fieldUserLogin = 105
1929 0x00, 0x6a, // fieldUserPassword = 106
1933 0x00, 0x66, // fieldUserName = 102
1937 0x00, 0x6e, // fieldUserAccess = 110
1939 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
1943 wantRes: []Transaction{
1947 Type: []byte{0, 0x00},
1948 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1949 ErrorCode: []byte{0, 0, 0, 1},
1951 NewField(fieldError, []byte("You are not allowed to create new accounts.")),
1955 wantErr: assert.NoError,
1958 name: "when action is modify user without required permission",
1961 logger: NewTestLogger(),
1963 Logger: NewTestLogger(),
1964 Accounts: map[string]*Account{
1969 Access: func() *[]byte {
1970 var bits accessBitmap
1979 NewField(fieldData, []byte{
1980 0x00, 0x04, // field count
1982 0x00, 0x69, // fieldUserLogin = 105
1986 0x00, 0x6a, // fieldUserPassword = 106
1990 0x00, 0x66, // fieldUserName = 102
1994 0x00, 0x6e, // fieldUserAccess = 110
1996 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2000 wantRes: []Transaction{
2004 Type: []byte{0, 0x00},
2005 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2006 ErrorCode: []byte{0, 0, 0, 1},
2008 NewField(fieldError, []byte("You are not allowed to modify accounts.")),
2012 wantErr: assert.NoError,
2015 name: "when action is delete user without required permission",
2018 logger: NewTestLogger(),
2020 Accounts: map[string]*Account{
2025 Access: func() *[]byte {
2026 var bits accessBitmap
2035 NewField(fieldData, []byte{
2043 wantRes: []Transaction{
2047 Type: []byte{0, 0x00},
2048 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2049 ErrorCode: []byte{0, 0, 0, 1},
2051 NewField(fieldError, []byte("You are not allowed to delete accounts.")),
2055 wantErr: assert.NoError,
2058 for _, tt := range tests {
2059 t.Run(tt.name, func(t *testing.T) {
2060 gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t)
2061 if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) {
2065 tranAssertEqual(t, tt.wantRes, gotRes)
2070 func TestHandleDelNewsArt(t *testing.T) {
2078 wantRes []Transaction
2079 wantErr assert.ErrorAssertionFunc
2082 name: "without required permission",
2086 Access: func() *[]byte {
2087 var bits accessBitmap
2098 wantRes: []Transaction{
2102 Type: []byte{0, 0x00},
2103 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2104 ErrorCode: []byte{0, 0, 0, 1},
2106 NewField(fieldError, []byte("You are not allowed to delete news articles.")),
2110 wantErr: assert.NoError,
2113 for _, tt := range tests {
2114 t.Run(tt.name, func(t *testing.T) {
2115 gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
2116 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
2119 tranAssertEqual(t, tt.wantRes, gotRes)
2124 func TestHandleDisconnectUser(t *testing.T) {
2132 wantRes []Transaction
2133 wantErr assert.ErrorAssertionFunc
2136 name: "without required permission",
2140 Access: func() *[]byte {
2141 var bits accessBitmap
2152 wantRes: []Transaction{
2156 Type: []byte{0, 0x00},
2157 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2158 ErrorCode: []byte{0, 0, 0, 1},
2160 NewField(fieldError, []byte("You are not allowed to disconnect users.")),
2164 wantErr: assert.NoError,
2167 name: "when target user has 'cannot be disconnected' priv",
2171 Clients: map[uint16]*ClientConn{
2175 Access: func() *[]byte {
2176 var bits accessBitmap
2177 bits.Set(accessCannotBeDiscon)
2186 Access: func() *[]byte {
2187 var bits accessBitmap
2188 bits.Set(accessDisconUser)
2197 NewField(fieldUserID, []byte{0, 1}),
2200 wantRes: []Transaction{
2204 Type: []byte{0, 0x00},
2205 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2206 ErrorCode: []byte{0, 0, 0, 1},
2208 NewField(fieldError, []byte("unnamed is not allowed to be disconnected.")),
2212 wantErr: assert.NoError,
2215 for _, tt := range tests {
2216 t.Run(tt.name, func(t *testing.T) {
2217 gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
2218 if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
2221 tranAssertEqual(t, tt.wantRes, gotRes)
2226 func TestHandleSendInstantMsg(t *testing.T) {
2234 wantRes []Transaction
2235 wantErr assert.ErrorAssertionFunc
2238 name: "when client 1 sends a message to client 2",
2242 UserName: []byte("User1"),
2244 Clients: map[uint16]*ClientConn{
2246 AutoReply: []byte(nil),
2254 NewField(fieldData, []byte("hai")),
2255 NewField(fieldUserID, []byte{0, 2}),
2258 wantRes: []Transaction{
2262 NewField(fieldData, []byte("hai")),
2263 NewField(fieldUserName, []byte("User1")),
2264 NewField(fieldUserID, []byte{0, 1}),
2265 NewField(fieldOptions, []byte{0, 1}),
2268 clientID: &[]byte{0, 1},
2271 Type: []byte{0x0, 0x6c},
2272 ID: []byte{0, 0, 0, 0},
2273 ErrorCode: []byte{0, 0, 0, 0},
2274 Fields: []Field(nil),
2277 wantErr: assert.NoError,
2280 name: "when client 2 has autoreply enabled",
2284 UserName: []byte("User1"),
2286 Clients: map[uint16]*ClientConn{
2289 UserName: []byte("User2"),
2290 AutoReply: []byte("autohai"),
2298 NewField(fieldData, []byte("hai")),
2299 NewField(fieldUserID, []byte{0, 2}),
2302 wantRes: []Transaction{
2306 NewField(fieldData, []byte("hai")),
2307 NewField(fieldUserName, []byte("User1")),
2308 NewField(fieldUserID, []byte{0, 1}),
2309 NewField(fieldOptions, []byte{0, 1}),
2314 NewField(fieldData, []byte("autohai")),
2315 NewField(fieldUserName, []byte("User2")),
2316 NewField(fieldUserID, []byte{0, 2}),
2317 NewField(fieldOptions, []byte{0, 1}),
2320 clientID: &[]byte{0, 1},
2323 Type: []byte{0x0, 0x6c},
2324 ID: []byte{0, 0, 0, 0},
2325 ErrorCode: []byte{0, 0, 0, 0},
2326 Fields: []Field(nil),
2329 wantErr: assert.NoError,
2332 for _, tt := range tests {
2333 t.Run(tt.name, func(t *testing.T) {
2334 gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
2335 if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
2339 tranAssertEqual(t, tt.wantRes, gotRes)
2344 func TestHandleDeleteFile(t *testing.T) {
2352 wantRes []Transaction
2353 wantErr assert.ErrorAssertionFunc
2356 name: "when user does not have required permission to delete a folder",
2360 Access: func() *[]byte {
2361 var bits accessBitmap
2368 FileRoot: func() string {
2369 return "/fakeRoot/Files"
2372 FS: func() *MockFileStore {
2373 mfi := &MockFileInfo{}
2374 mfi.On("Mode").Return(fs.FileMode(0))
2375 mfi.On("Size").Return(int64(100))
2376 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2377 mfi.On("IsDir").Return(false)
2378 mfi.On("Name").Return("testfile")
2380 mfs := &MockFileStore{}
2381 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2382 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2383 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2387 Accounts: map[string]*Account{},
2391 tranDeleteFile, &[]byte{0, 1},
2392 NewField(fieldFileName, []byte("testfile")),
2393 NewField(fieldFilePath, []byte{
2401 wantRes: []Transaction{
2405 Type: []byte{0, 0x00},
2406 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2407 ErrorCode: []byte{0, 0, 0, 1},
2409 NewField(fieldError, []byte("You are not allowed to delete files.")),
2413 wantErr: assert.NoError,
2416 name: "deletes all associated metadata files",
2420 Access: func() *[]byte {
2421 var bits accessBitmap
2422 bits.Set(accessDeleteFile)
2429 FileRoot: func() string {
2430 return "/fakeRoot/Files"
2433 FS: func() *MockFileStore {
2434 mfi := &MockFileInfo{}
2435 mfi.On("Mode").Return(fs.FileMode(0))
2436 mfi.On("Size").Return(int64(100))
2437 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2438 mfi.On("IsDir").Return(false)
2439 mfi.On("Name").Return("testfile")
2441 mfs := &MockFileStore{}
2442 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2443 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2444 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2446 mfs.On("RemoveAll", "/fakeRoot/Files/aaa/testfile").Return(nil)
2447 mfs.On("Remove", "/fakeRoot/Files/aaa/testfile.incomplete").Return(nil)
2448 mfs.On("Remove", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil)
2449 mfs.On("Remove", "/fakeRoot/Files/aaa/.info_testfile").Return(nil)
2453 Accounts: map[string]*Account{},
2457 tranDeleteFile, &[]byte{0, 1},
2458 NewField(fieldFileName, []byte("testfile")),
2459 NewField(fieldFilePath, []byte{
2467 wantRes: []Transaction{
2471 Type: []byte{0x0, 0xcc},
2472 ID: []byte{0x0, 0x0, 0x0, 0x0},
2473 ErrorCode: []byte{0, 0, 0, 0},
2474 Fields: []Field(nil),
2477 wantErr: assert.NoError,
2480 for _, tt := range tests {
2481 t.Run(tt.name, func(t *testing.T) {
2482 gotRes, err := HandleDeleteFile(tt.args.cc, tt.args.t)
2483 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteFile(%v, %v)", tt.args.cc, tt.args.t)) {
2487 tranAssertEqual(t, tt.wantRes, gotRes)
2489 tt.args.cc.Server.FS.(*MockFileStore).AssertExpectations(t)
2494 func TestHandleGetFileNameList(t *testing.T) {
2502 wantRes []Transaction
2503 wantErr assert.ErrorAssertionFunc
2506 name: "when fieldFilePath is a drop box, but user does not have accessViewDropBoxes ",
2510 Access: func() *[]byte {
2511 var bits accessBitmap
2519 FileRoot: func() string {
2520 path, _ := os.Getwd()
2521 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2527 tranGetFileNameList, &[]byte{0, 1},
2528 NewField(fieldFilePath, []byte{
2532 0x64, 0x72, 0x6f, 0x70, 0x20, 0x62, 0x6f, 0x78, // "drop box"
2536 wantRes: []Transaction{
2540 Type: []byte{0, 0x00},
2541 ID: []byte{0, 0, 0, 0},
2542 ErrorCode: []byte{0, 0, 0, 1},
2544 NewField(fieldError, []byte("You are not allowed to view drop boxes.")),
2548 wantErr: assert.NoError,
2551 name: "with file root",
2556 FileRoot: func() string {
2557 path, _ := os.Getwd()
2558 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2564 tranGetFileNameList, &[]byte{0, 1},
2565 NewField(fieldFilePath, []byte{
2571 wantRes: []Transaction{
2575 Type: []byte{0, 0xc8},
2576 ID: []byte{0, 0, 0, 0},
2577 ErrorCode: []byte{0, 0, 0, 0},
2580 fieldFileNameWithInfo,
2582 fnwi := FileNameWithInfo{
2583 fileNameWithInfoHeader: fileNameWithInfoHeader{
2584 Type: [4]byte{0x54, 0x45, 0x58, 0x54},
2585 Creator: [4]byte{0x54, 0x54, 0x58, 0x54},
2586 FileSize: [4]byte{0, 0, 0x04, 0},
2588 NameScript: [2]byte{},
2589 NameSize: [2]byte{0, 0x0b},
2591 name: []byte("testfile-1k"),
2593 b, _ := fnwi.MarshalBinary()
2600 wantErr: assert.NoError,
2603 for _, tt := range tests {
2604 t.Run(tt.name, func(t *testing.T) {
2605 gotRes, err := HandleGetFileNameList(tt.args.cc, tt.args.t)
2606 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetFileNameList(%v, %v)", tt.args.cc, tt.args.t)) {
2610 tranAssertEqual(t, tt.wantRes, gotRes)