6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/mock"
18 func TestHandleSetChatSubject(t *testing.T) {
30 name: "sends chat subject to private chat members",
33 UserName: []byte{0x00, 0x01},
35 PrivateChats: map[uint32]*PrivateChat{
38 ClientConn: map[uint16]*ClientConn{
41 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
47 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
54 Clients: map[uint16]*ClientConn{
57 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
63 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
73 Type: []byte{0, 0x6a},
74 ID: []byte{0, 0, 0, 1},
75 ErrorCode: []byte{0, 0, 0, 0},
77 NewField(FieldChatID, []byte{0, 0, 0, 1}),
78 NewField(FieldChatSubject, []byte("Test Subject")),
84 clientID: &[]byte{0, 1},
87 Type: []byte{0, 0x77},
88 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
89 ErrorCode: []byte{0, 0, 0, 0},
91 NewField(FieldChatID, []byte{0, 0, 0, 1}),
92 NewField(FieldChatSubject, []byte("Test Subject")),
96 clientID: &[]byte{0, 2},
99 Type: []byte{0, 0x77},
100 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
101 ErrorCode: []byte{0, 0, 0, 0},
103 NewField(FieldChatID, []byte{0, 0, 0, 1}),
104 NewField(FieldChatSubject, []byte("Test Subject")),
111 for _, tt := range tests {
112 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
114 t.Run(tt.name, func(t *testing.T) {
115 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
116 if (err != nil) != tt.wantErr {
117 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
120 if !assert.Equal(t, tt.want, got) {
121 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
127 func TestHandleLeaveChat(t *testing.T) {
139 name: "returns expected transactions",
144 PrivateChats: map[uint32]*PrivateChat{
146 ClientConn: map[uint16]*ClientConn{
149 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
155 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
162 Clients: map[uint16]*ClientConn{
165 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
171 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
178 t: NewTransaction(TranDeleteUser, nil, NewField(FieldChatID, []byte{0, 0, 0, 1})),
182 clientID: &[]byte{0, 1},
185 Type: []byte{0, 0x76},
186 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
187 ErrorCode: []byte{0, 0, 0, 0},
189 NewField(FieldChatID, []byte{0, 0, 0, 1}),
190 NewField(FieldUserID, []byte{0, 2}),
197 for _, tt := range tests {
199 t.Run(tt.name, func(t *testing.T) {
200 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
201 if (err != nil) != tt.wantErr {
202 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
205 if !assert.Equal(t, tt.want, got) {
206 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
212 func TestHandleGetUserNameList(t *testing.T) {
224 name: "replies with userlist transaction",
230 Clients: map[uint16]*ClientConn{
235 UserName: []byte{0, 4},
241 UserName: []byte{0, 4},
247 ID: []byte{0, 0, 0, 1},
253 clientID: &[]byte{1, 1},
257 ID: []byte{0, 0, 0, 1},
258 ErrorCode: []byte{0, 0, 0, 0},
261 FieldUsernameWithInfo,
262 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
265 FieldUsernameWithInfo,
266 []byte{00, 02, 00, 02, 00, 03, 00, 02, 00, 04},
274 for _, tt := range tests {
275 t.Run(tt.name, func(t *testing.T) {
276 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
277 if (err != nil) != tt.wantErr {
278 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
281 assert.Equal(t, tt.want, got)
286 func TestHandleChatSend(t *testing.T) {
298 name: "sends chat msg transaction to all clients",
302 Access: func() accessBitmap {
303 var bits accessBitmap
304 bits.Set(accessSendChat)
308 UserName: []byte{0x00, 0x01},
310 Clients: map[uint16]*ClientConn{
313 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
319 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
328 NewField(FieldData, []byte("hai")),
334 clientID: &[]byte{0, 1},
337 Type: []byte{0, 0x6a},
338 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
339 ErrorCode: []byte{0, 0, 0, 0},
341 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
345 clientID: &[]byte{0, 2},
348 Type: []byte{0, 0x6a},
349 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
350 ErrorCode: []byte{0, 0, 0, 0},
352 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
359 name: "treats Chat ID 00 00 00 00 as a public chat message",
363 Access: func() accessBitmap {
364 var bits accessBitmap
365 bits.Set(accessSendChat)
369 UserName: []byte{0x00, 0x01},
371 Clients: map[uint16]*ClientConn{
374 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
380 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
389 NewField(FieldData, []byte("hai")),
390 NewField(FieldChatID, []byte{0, 0, 0, 0}),
396 clientID: &[]byte{0, 1},
399 Type: []byte{0, 0x6a},
400 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
401 ErrorCode: []byte{0, 0, 0, 0},
403 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
407 clientID: &[]byte{0, 2},
410 Type: []byte{0, 0x6a},
411 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
412 ErrorCode: []byte{0, 0, 0, 0},
414 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
421 name: "when user does not have required permission",
425 Access: func() accessBitmap {
426 var bits accessBitmap
431 Accounts: map[string]*Account{},
435 TranChatSend, &[]byte{0, 1},
436 NewField(FieldData, []byte("hai")),
444 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
445 ErrorCode: []byte{0, 0, 0, 1},
447 NewField(FieldError, []byte("You are not allowed to participate in chat.")),
454 name: "sends chat msg as emote if FieldChatOptions is set to 1",
458 Access: func() accessBitmap {
459 var bits accessBitmap
460 bits.Set(accessSendChat)
464 UserName: []byte("Testy McTest"),
466 Clients: map[uint16]*ClientConn{
469 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
475 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
484 NewField(FieldData, []byte("performed action")),
485 NewField(FieldChatOptions, []byte{0x00, 0x01}),
491 clientID: &[]byte{0, 1},
494 Type: []byte{0, 0x6a},
495 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
496 ErrorCode: []byte{0, 0, 0, 0},
498 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
502 clientID: &[]byte{0, 2},
505 Type: []byte{0, 0x6a},
506 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
507 ErrorCode: []byte{0, 0, 0, 0},
509 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
516 name: "does not send chat msg as emote if FieldChatOptions is set to 0",
520 Access: func() accessBitmap {
521 var bits accessBitmap
522 bits.Set(accessSendChat)
526 UserName: []byte("Testy McTest"),
528 Clients: map[uint16]*ClientConn{
531 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
537 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
546 NewField(FieldData, []byte("hello")),
547 NewField(FieldChatOptions, []byte{0x00, 0x00}),
553 clientID: &[]byte{0, 1},
556 Type: []byte{0, 0x6a},
557 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
558 ErrorCode: []byte{0, 0, 0, 0},
560 NewField(FieldData, []byte("\r Testy McTest: hello")),
564 clientID: &[]byte{0, 2},
567 Type: []byte{0, 0x6a},
568 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
569 ErrorCode: []byte{0, 0, 0, 0},
571 NewField(FieldData, []byte("\r Testy McTest: hello")),
578 name: "only sends chat msg to clients with accessReadChat permission",
582 Access: func() accessBitmap {
583 var bits accessBitmap
584 bits.Set(accessSendChat)
588 UserName: []byte{0x00, 0x01},
590 Clients: map[uint16]*ClientConn{
593 Access: func() accessBitmap {
594 var bits accessBitmap
595 bits.Set(accessReadChat)
602 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
611 NewField(FieldData, []byte("hai")),
617 clientID: &[]byte{0, 1},
620 Type: []byte{0, 0x6a},
621 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
622 ErrorCode: []byte{0, 0, 0, 0},
624 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
631 name: "only sends private chat msg to members of private chat",
635 Access: func() accessBitmap {
636 var bits accessBitmap
637 bits.Set(accessSendChat)
641 UserName: []byte{0x00, 0x01},
643 PrivateChats: map[uint32]*PrivateChat{
645 ClientConn: map[uint16]*ClientConn{
655 Clients: map[uint16]*ClientConn{
658 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
664 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
670 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
679 NewField(FieldData, []byte("hai")),
680 NewField(FieldChatID, []byte{0, 0, 0, 1}),
686 clientID: &[]byte{0, 1},
689 Type: []byte{0, 0x6a},
690 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
691 ErrorCode: []byte{0, 0, 0, 0},
693 NewField(FieldChatID, []byte{0, 0, 0, 1}),
694 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
698 clientID: &[]byte{0, 2},
701 Type: []byte{0, 0x6a},
702 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
703 ErrorCode: []byte{0, 0, 0, 0},
705 NewField(FieldChatID, []byte{0, 0, 0, 1}),
706 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
713 for _, tt := range tests {
714 t.Run(tt.name, func(t *testing.T) {
715 got, err := HandleChatSend(tt.args.cc, tt.args.t)
717 if (err != nil) != tt.wantErr {
718 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
721 tranAssertEqual(t, tt.want, got)
726 func TestHandleGetFileInfo(t *testing.T) {
727 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
736 wantRes []Transaction
740 name: "returns expected fields when a valid file is requested",
743 ID: &[]byte{0x00, 0x01},
747 FileRoot: func() string {
748 path, _ := os.Getwd()
749 return filepath.Join(path, "/test/config/Files")
755 TranGetFileInfo, nil,
756 NewField(FieldFileName, []byte("testfile.txt")),
757 NewField(FieldFilePath, []byte{0x00, 0x00}),
760 wantRes: []Transaction{
762 clientID: &[]byte{0, 1},
766 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
767 ErrorCode: []byte{0, 0, 0, 0},
769 NewField(FieldFileName, []byte("testfile.txt")),
770 NewField(FieldFileTypeString, []byte("Text File")),
771 NewField(FieldFileCreatorString, []byte("ttxt")),
772 NewField(FieldFileType, []byte("TEXT")),
773 NewField(FieldFileCreateDate, make([]byte, 8)),
774 NewField(FieldFileModifyDate, make([]byte, 8)),
775 NewField(FieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}),
782 for _, tt := range tests {
783 t.Run(tt.name, func(t *testing.T) {
784 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
786 gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t)
787 if (err != nil) != tt.wantErr {
788 t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr)
792 // Clear the fileWrapper timestamp fields to work around problems running the tests in multiple timezones
793 // TODO: revisit how to test this by mocking the stat calls
794 gotRes[0].Fields[4].Data = make([]byte, 8)
795 gotRes[0].Fields[5].Data = make([]byte, 8)
796 if !assert.Equal(t, tt.wantRes, gotRes) {
797 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)
803 func TestHandleNewFolder(t *testing.T) {
811 wantRes []Transaction
815 name: "without required permission",
819 Access: func() accessBitmap {
820 var bits accessBitmap
830 wantRes: []Transaction{
835 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
836 ErrorCode: []byte{0, 0, 0, 1},
838 NewField(FieldError, []byte("You are not allowed to create folders.")),
845 name: "when path is nested",
849 Access: func() accessBitmap {
850 var bits accessBitmap
851 bits.Set(accessCreateFolder)
860 FS: func() *MockFileStore {
861 mfs := &MockFileStore{}
862 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
863 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
869 TranNewFolder, &[]byte{0, 1},
870 NewField(FieldFileName, []byte("testFolder")),
871 NewField(FieldFilePath, []byte{
879 wantRes: []Transaction{
881 clientID: &[]byte{0, 1},
885 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
886 ErrorCode: []byte{0, 0, 0, 0},
892 name: "when path is not nested",
896 Access: func() accessBitmap {
897 var bits accessBitmap
898 bits.Set(accessCreateFolder)
907 FS: func() *MockFileStore {
908 mfs := &MockFileStore{}
909 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
910 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
916 TranNewFolder, &[]byte{0, 1},
917 NewField(FieldFileName, []byte("testFolder")),
920 wantRes: []Transaction{
922 clientID: &[]byte{0, 1},
926 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
927 ErrorCode: []byte{0, 0, 0, 0},
933 name: "when Write returns an err",
937 Access: func() accessBitmap {
938 var bits accessBitmap
939 bits.Set(accessCreateFolder)
948 FS: func() *MockFileStore {
949 mfs := &MockFileStore{}
950 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
951 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
957 TranNewFolder, &[]byte{0, 1},
958 NewField(FieldFileName, []byte("testFolder")),
959 NewField(FieldFilePath, []byte{
964 wantRes: []Transaction{},
968 name: "FieldFileName does not allow directory traversal",
972 Access: func() accessBitmap {
973 var bits accessBitmap
974 bits.Set(accessCreateFolder)
983 FS: func() *MockFileStore {
984 mfs := &MockFileStore{}
985 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
986 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
992 TranNewFolder, &[]byte{0, 1},
993 NewField(FieldFileName, []byte("../../testFolder")),
996 wantRes: []Transaction{
998 clientID: &[]byte{0, 1},
1002 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
1003 ErrorCode: []byte{0, 0, 0, 0},
1008 name: "FieldFilePath does not allow directory traversal",
1012 Access: func() accessBitmap {
1013 var bits accessBitmap
1014 bits.Set(accessCreateFolder)
1021 FileRoot: "/Files/",
1023 FS: func() *MockFileStore {
1024 mfs := &MockFileStore{}
1025 mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
1026 mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
1032 TranNewFolder, &[]byte{0, 1},
1033 NewField(FieldFileName, []byte("testFolder")),
1034 NewField(FieldFilePath, []byte{
1045 wantRes: []Transaction{
1047 clientID: &[]byte{0, 1},
1051 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
1052 ErrorCode: []byte{0, 0, 0, 0},
1057 for _, tt := range tests {
1058 t.Run(tt.name, func(t *testing.T) {
1059 gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
1060 if (err != nil) != tt.wantErr {
1061 t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
1065 if !tranAssertEqual(t, tt.wantRes, gotRes) {
1066 t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
1072 func TestHandleUploadFile(t *testing.T) {
1080 wantRes []Transaction
1084 name: "when request is valid and user has Upload Anywhere permission",
1089 fileTransfers: map[[4]byte]*FileTransfer{},
1091 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1093 transfers: map[int]map[[4]byte]*FileTransfer{
1097 Access: func() accessBitmap {
1098 var bits accessBitmap
1099 bits.Set(accessUploadFile)
1100 bits.Set(accessUploadAnywhere)
1106 TranUploadFile, &[]byte{0, 1},
1107 NewField(FieldFileName, []byte("testFile")),
1108 NewField(FieldFilePath, []byte{
1116 wantRes: []Transaction{
1121 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1122 ErrorCode: []byte{0, 0, 0, 0},
1124 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), // rand.Seed(1)
1131 name: "when user does not have required access",
1135 Access: func() accessBitmap {
1136 var bits accessBitmap
1142 TranUploadFile, &[]byte{0, 1},
1143 NewField(FieldFileName, []byte("testFile")),
1144 NewField(FieldFilePath, []byte{
1152 wantRes: []Transaction{
1157 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1158 ErrorCode: []byte{0, 0, 0, 1},
1160 NewField(FieldError, []byte("You are not allowed to upload files.")), // rand.Seed(1)
1167 for _, tt := range tests {
1168 t.Run(tt.name, func(t *testing.T) {
1170 gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t)
1171 if (err != nil) != tt.wantErr {
1172 t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr)
1176 tranAssertEqual(t, tt.wantRes, gotRes)
1181 func TestHandleMakeAlias(t *testing.T) {
1189 wantRes []Transaction
1193 name: "with valid input and required permissions",
1196 logger: NewTestLogger(),
1198 Access: func() accessBitmap {
1199 var bits accessBitmap
1200 bits.Set(accessMakeAlias)
1206 FileRoot: func() string {
1207 path, _ := os.Getwd()
1208 return path + "/test/config/Files"
1211 Logger: NewTestLogger(),
1212 FS: func() *MockFileStore {
1213 mfs := &MockFileStore{}
1214 path, _ := os.Getwd()
1217 path+"/test/config/Files/foo/testFile",
1218 path+"/test/config/Files/bar/testFile",
1225 TranMakeFileAlias, &[]byte{0, 1},
1226 NewField(FieldFileName, []byte("testFile")),
1227 NewField(FieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1228 NewField(FieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1231 wantRes: []Transaction{
1236 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1237 ErrorCode: []byte{0, 0, 0, 0},
1238 Fields: []Field(nil),
1244 name: "when symlink returns an error",
1247 logger: NewTestLogger(),
1249 Access: func() accessBitmap {
1250 var bits accessBitmap
1251 bits.Set(accessMakeAlias)
1257 FileRoot: func() string {
1258 path, _ := os.Getwd()
1259 return path + "/test/config/Files"
1262 Logger: NewTestLogger(),
1263 FS: func() *MockFileStore {
1264 mfs := &MockFileStore{}
1265 path, _ := os.Getwd()
1268 path+"/test/config/Files/foo/testFile",
1269 path+"/test/config/Files/bar/testFile",
1270 ).Return(errors.New("ohno"))
1276 TranMakeFileAlias, &[]byte{0, 1},
1277 NewField(FieldFileName, []byte("testFile")),
1278 NewField(FieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1279 NewField(FieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1282 wantRes: []Transaction{
1287 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1288 ErrorCode: []byte{0, 0, 0, 1},
1290 NewField(FieldError, []byte("Error creating alias")),
1297 name: "when user does not have required permission",
1300 logger: NewTestLogger(),
1302 Access: func() accessBitmap {
1303 var bits accessBitmap
1309 FileRoot: func() string {
1310 path, _ := os.Getwd()
1311 return path + "/test/config/Files"
1317 TranMakeFileAlias, &[]byte{0, 1},
1318 NewField(FieldFileName, []byte("testFile")),
1319 NewField(FieldFilePath, []byte{
1325 NewField(FieldFileNewPath, []byte{
1333 wantRes: []Transaction{
1338 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1339 ErrorCode: []byte{0, 0, 0, 1},
1341 NewField(FieldError, []byte("You are not allowed to make aliases.")),
1348 for _, tt := range tests {
1349 t.Run(tt.name, func(t *testing.T) {
1350 gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
1351 if (err != nil) != tt.wantErr {
1352 t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
1356 tranAssertEqual(t, tt.wantRes, gotRes)
1361 func TestHandleGetUser(t *testing.T) {
1369 wantRes []Transaction
1370 wantErr assert.ErrorAssertionFunc
1373 name: "when account is valid",
1377 Access: func() accessBitmap {
1378 var bits accessBitmap
1379 bits.Set(accessOpenUser)
1384 Accounts: map[string]*Account{
1388 Password: "password",
1389 Access: accessBitmap{},
1395 TranGetUser, &[]byte{0, 1},
1396 NewField(FieldUserLogin, []byte("guest")),
1399 wantRes: []Transaction{
1404 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1405 ErrorCode: []byte{0, 0, 0, 0},
1407 NewField(FieldUserName, []byte("Guest")),
1408 NewField(FieldUserLogin, encodeString([]byte("guest"))),
1409 NewField(FieldUserPassword, []byte("password")),
1410 NewField(FieldUserAccess, []byte{0, 0, 0, 0, 0, 0, 0, 0}),
1414 wantErr: assert.NoError,
1417 name: "when user does not have required permission",
1421 Access: func() accessBitmap {
1422 var bits accessBitmap
1427 Accounts: map[string]*Account{},
1431 TranGetUser, &[]byte{0, 1},
1432 NewField(FieldUserLogin, []byte("nonExistentUser")),
1435 wantRes: []Transaction{
1440 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1441 ErrorCode: []byte{0, 0, 0, 1},
1443 NewField(FieldError, []byte("You are not allowed to view accounts.")),
1447 wantErr: assert.NoError,
1450 name: "when account does not exist",
1454 Access: func() accessBitmap {
1455 var bits accessBitmap
1456 bits.Set(accessOpenUser)
1461 Accounts: map[string]*Account{},
1465 TranGetUser, &[]byte{0, 1},
1466 NewField(FieldUserLogin, []byte("nonExistentUser")),
1469 wantRes: []Transaction{
1474 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1475 ErrorCode: []byte{0, 0, 0, 1},
1477 NewField(FieldError, []byte("Account does not exist.")),
1481 wantErr: assert.NoError,
1484 for _, tt := range tests {
1485 t.Run(tt.name, func(t *testing.T) {
1486 gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
1487 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
1491 tranAssertEqual(t, tt.wantRes, gotRes)
1496 func TestHandleDeleteUser(t *testing.T) {
1504 wantRes []Transaction
1505 wantErr assert.ErrorAssertionFunc
1508 name: "when user dataFile",
1512 Access: func() accessBitmap {
1513 var bits accessBitmap
1514 bits.Set(accessDeleteUser)
1519 Accounts: map[string]*Account{
1522 Name: "Testy McTest",
1523 Password: "password",
1524 Access: accessBitmap{},
1527 FS: func() *MockFileStore {
1528 mfs := &MockFileStore{}
1529 mfs.On("Remove", "Users/testuser.yaml").Return(nil)
1535 TranDeleteUser, &[]byte{0, 1},
1536 NewField(FieldUserLogin, encodeString([]byte("testuser"))),
1539 wantRes: []Transaction{
1544 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1545 ErrorCode: []byte{0, 0, 0, 0},
1546 Fields: []Field(nil),
1549 wantErr: assert.NoError,
1552 name: "when user does not have required permission",
1556 Access: func() accessBitmap {
1557 var bits accessBitmap
1562 Accounts: map[string]*Account{},
1566 TranDeleteUser, &[]byte{0, 1},
1567 NewField(FieldUserLogin, encodeString([]byte("testuser"))),
1570 wantRes: []Transaction{
1575 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1576 ErrorCode: []byte{0, 0, 0, 1},
1578 NewField(FieldError, []byte("You are not allowed to delete accounts.")),
1582 wantErr: assert.NoError,
1585 for _, tt := range tests {
1586 t.Run(tt.name, func(t *testing.T) {
1587 gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
1588 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
1592 tranAssertEqual(t, tt.wantRes, gotRes)
1597 func TestHandleGetMsgs(t *testing.T) {
1605 wantRes []Transaction
1606 wantErr assert.ErrorAssertionFunc
1609 name: "returns news data",
1613 Access: func() accessBitmap {
1614 var bits accessBitmap
1615 bits.Set(accessNewsReadArt)
1620 FlatNews: []byte("TEST"),
1624 TranGetMsgs, &[]byte{0, 1},
1627 wantRes: []Transaction{
1632 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1633 ErrorCode: []byte{0, 0, 0, 0},
1635 NewField(FieldData, []byte("TEST")),
1639 wantErr: assert.NoError,
1642 name: "when user does not have required permission",
1646 Access: func() accessBitmap {
1647 var bits accessBitmap
1652 Accounts: map[string]*Account{},
1656 TranGetMsgs, &[]byte{0, 1},
1659 wantRes: []Transaction{
1664 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1665 ErrorCode: []byte{0, 0, 0, 1},
1667 NewField(FieldError, []byte("You are not allowed to read news.")),
1671 wantErr: assert.NoError,
1674 for _, tt := range tests {
1675 t.Run(tt.name, func(t *testing.T) {
1676 gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t)
1677 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) {
1681 tranAssertEqual(t, tt.wantRes, gotRes)
1686 func TestHandleNewUser(t *testing.T) {
1694 wantRes []Transaction
1695 wantErr assert.ErrorAssertionFunc
1698 name: "when user does not have required permission",
1702 Access: func() accessBitmap {
1703 var bits accessBitmap
1708 Accounts: map[string]*Account{},
1712 TranNewUser, &[]byte{0, 1},
1715 wantRes: []Transaction{
1720 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1721 ErrorCode: []byte{0, 0, 0, 1},
1723 NewField(FieldError, []byte("You are not allowed to create new accounts.")),
1727 wantErr: assert.NoError,
1730 name: "when user attempts to create account with greater access",
1734 Access: func() accessBitmap {
1735 var bits accessBitmap
1736 bits.Set(accessCreateUser)
1741 Accounts: map[string]*Account{},
1745 TranNewUser, &[]byte{0, 1},
1746 NewField(FieldUserLogin, []byte("userB")),
1750 var bits accessBitmap
1751 bits.Set(accessDisconUser)
1757 wantRes: []Transaction{
1762 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1763 ErrorCode: []byte{0, 0, 0, 1},
1765 NewField(FieldError, []byte("Cannot create account with more access than yourself.")),
1769 wantErr: assert.NoError,
1772 for _, tt := range tests {
1773 t.Run(tt.name, func(t *testing.T) {
1774 gotRes, err := HandleNewUser(tt.args.cc, tt.args.t)
1775 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) {
1779 tranAssertEqual(t, tt.wantRes, gotRes)
1784 func TestHandleListUsers(t *testing.T) {
1792 wantRes []Transaction
1793 wantErr assert.ErrorAssertionFunc
1796 name: "when user does not have required permission",
1800 Access: func() accessBitmap {
1801 var bits accessBitmap
1806 Accounts: map[string]*Account{},
1810 TranNewUser, &[]byte{0, 1},
1813 wantRes: []Transaction{
1818 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1819 ErrorCode: []byte{0, 0, 0, 1},
1821 NewField(FieldError, []byte("You are not allowed to view accounts.")),
1825 wantErr: assert.NoError,
1828 name: "when user has required permission",
1832 Access: func() accessBitmap {
1833 var bits accessBitmap
1834 bits.Set(accessOpenUser)
1839 Accounts: map[string]*Account{
1844 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
1850 TranGetClientInfoText, &[]byte{0, 1},
1851 NewField(FieldUserID, []byte{0, 1}),
1854 wantRes: []Transaction{
1859 ID: []byte{0, 0, 0, 0},
1860 ErrorCode: []byte{0, 0, 0, 0},
1862 NewField(FieldData, []byte{
1863 0x00, 0x04, 0x00, 0x66, 0x00, 0x05, 0x67, 0x75, 0x65, 0x73, 0x74, 0x00, 0x69, 0x00, 0x05, 0x98,
1864 0x8a, 0x9a, 0x8c, 0x8b, 0x00, 0x6e, 0x00, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1865 0x00, 0x6a, 0x00, 0x01, 0x78,
1870 wantErr: assert.NoError,
1873 for _, tt := range tests {
1874 t.Run(tt.name, func(t *testing.T) {
1875 gotRes, err := HandleListUsers(tt.args.cc, tt.args.t)
1876 if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) {
1880 tranAssertEqual(t, tt.wantRes, gotRes)
1885 func TestHandleDownloadFile(t *testing.T) {
1893 wantRes []Transaction
1894 wantErr assert.ErrorAssertionFunc
1897 name: "when user does not have required permission",
1901 Access: func() accessBitmap {
1902 var bits accessBitmap
1908 t: NewTransaction(TranDownloadFile, &[]byte{0, 1}),
1910 wantRes: []Transaction{
1915 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1916 ErrorCode: []byte{0, 0, 0, 1},
1918 NewField(FieldError, []byte("You are not allowed to download files.")),
1922 wantErr: assert.NoError,
1925 name: "with a valid file",
1928 transfers: map[int]map[[4]byte]*FileTransfer{
1932 Access: func() accessBitmap {
1933 var bits accessBitmap
1934 bits.Set(accessDownloadFile)
1940 fileTransfers: map[[4]byte]*FileTransfer{},
1942 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1944 Accounts: map[string]*Account{},
1950 NewField(FieldFileName, []byte("testfile.txt")),
1951 NewField(FieldFilePath, []byte{0x0, 0x00}),
1954 wantRes: []Transaction{
1959 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1960 ErrorCode: []byte{0, 0, 0, 0},
1962 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1963 NewField(FieldWaitingCount, []byte{0x00, 0x00}),
1964 NewField(FieldTransferSize, []byte{0x00, 0x00, 0x00, 0xa5}),
1965 NewField(FieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}),
1969 wantErr: assert.NoError,
1972 name: "when client requests to resume 1k test file at offset 256",
1975 transfers: map[int]map[[4]byte]*FileTransfer{
1977 }, Account: &Account{
1978 Access: func() accessBitmap {
1979 var bits accessBitmap
1980 bits.Set(accessDownloadFile)
1987 // FS: func() *MockFileStore {
1988 // path, _ := os.Getwd()
1989 // testFile, err := os.Open(path + "/test/config/Files/testfile-1k")
1994 // mfi := &MockFileInfo{}
1995 // mfi.On("Mode").Return(fs.FileMode(0))
1996 // mfs := &MockFileStore{}
1997 // mfs.On("Stat", "/fakeRoot/Files/testfile.txt").Return(mfi, nil)
1998 // mfs.On("Open", "/fakeRoot/Files/testfile.txt").Return(testFile, nil)
1999 // mfs.On("Stat", "/fakeRoot/Files/.info_testfile.txt").Return(nil, errors.New("no"))
2000 // mfs.On("Stat", "/fakeRoot/Files/.rsrc_testfile.txt").Return(nil, errors.New("no"))
2004 fileTransfers: map[[4]byte]*FileTransfer{},
2006 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
2008 Accounts: map[string]*Account{},
2014 NewField(FieldFileName, []byte("testfile-1k")),
2015 NewField(FieldFilePath, []byte{0x00, 0x00}),
2017 FieldFileResumeData,
2019 frd := FileResumeData{
2023 ForkCount: [2]byte{0, 2},
2024 ForkInfoList: []ForkInfoList{
2026 Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
2027 DataSize: [4]byte{0, 0, 0x01, 0x00}, // request offset 256
2032 Fork: [4]byte{0x4d, 0x41, 0x43, 0x52}, // "MACR"
2033 DataSize: [4]byte{0, 0, 0, 0},
2039 b, _ := frd.BinaryMarshal()
2045 wantRes: []Transaction{
2050 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2051 ErrorCode: []byte{0, 0, 0, 0},
2053 NewField(FieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
2054 NewField(FieldWaitingCount, []byte{0x00, 0x00}),
2055 NewField(FieldTransferSize, []byte{0x00, 0x00, 0x03, 0x8d}),
2056 NewField(FieldFileSize, []byte{0x00, 0x00, 0x03, 0x00}),
2060 wantErr: assert.NoError,
2063 for _, tt := range tests {
2064 t.Run(tt.name, func(t *testing.T) {
2065 gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t)
2066 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) {
2070 tranAssertEqual(t, tt.wantRes, gotRes)
2075 func TestHandleUpdateUser(t *testing.T) {
2083 wantRes []Transaction
2084 wantErr assert.ErrorAssertionFunc
2087 name: "when action is create user without required permission",
2090 logger: NewTestLogger(),
2092 Logger: NewTestLogger(),
2095 Access: func() accessBitmap {
2096 var bits accessBitmap
2104 NewField(FieldData, []byte{
2105 0x00, 0x04, // field count
2107 0x00, 0x69, // FieldUserLogin = 105
2111 0x00, 0x6a, // FieldUserPassword = 106
2115 0x00, 0x66, // FieldUserName = 102
2119 0x00, 0x6e, // FieldUserAccess = 110
2121 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2125 wantRes: []Transaction{
2130 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2131 ErrorCode: []byte{0, 0, 0, 1},
2133 NewField(FieldError, []byte("You are not allowed to create new accounts.")),
2137 wantErr: assert.NoError,
2140 name: "when action is modify user without required permission",
2143 logger: NewTestLogger(),
2145 Logger: NewTestLogger(),
2146 Accounts: map[string]*Account{
2151 Access: func() accessBitmap {
2152 var bits accessBitmap
2160 NewField(FieldData, []byte{
2161 0x00, 0x04, // field count
2163 0x00, 0x69, // FieldUserLogin = 105
2167 0x00, 0x6a, // FieldUserPassword = 106
2171 0x00, 0x66, // FieldUserName = 102
2175 0x00, 0x6e, // FieldUserAccess = 110
2177 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
2181 wantRes: []Transaction{
2186 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2187 ErrorCode: []byte{0, 0, 0, 1},
2189 NewField(FieldError, []byte("You are not allowed to modify accounts.")),
2193 wantErr: assert.NoError,
2196 name: "when action is delete user without required permission",
2199 logger: NewTestLogger(),
2201 Accounts: map[string]*Account{
2206 Access: func() accessBitmap {
2207 var bits accessBitmap
2215 NewField(FieldData, []byte{
2223 wantRes: []Transaction{
2228 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2229 ErrorCode: []byte{0, 0, 0, 1},
2231 NewField(FieldError, []byte("You are not allowed to delete accounts.")),
2235 wantErr: assert.NoError,
2238 for _, tt := range tests {
2239 t.Run(tt.name, func(t *testing.T) {
2240 gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t)
2241 if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) {
2245 tranAssertEqual(t, tt.wantRes, gotRes)
2250 func TestHandleDelNewsArt(t *testing.T) {
2258 wantRes []Transaction
2259 wantErr assert.ErrorAssertionFunc
2262 name: "without required permission",
2266 Access: func() accessBitmap {
2267 var bits accessBitmap
2277 wantRes: []Transaction{
2282 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2283 ErrorCode: []byte{0, 0, 0, 1},
2285 NewField(FieldError, []byte("You are not allowed to delete news articles.")),
2289 wantErr: assert.NoError,
2292 for _, tt := range tests {
2293 t.Run(tt.name, func(t *testing.T) {
2294 gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
2295 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
2298 tranAssertEqual(t, tt.wantRes, gotRes)
2303 func TestHandleDisconnectUser(t *testing.T) {
2311 wantRes []Transaction
2312 wantErr assert.ErrorAssertionFunc
2315 name: "without required permission",
2319 Access: func() accessBitmap {
2320 var bits accessBitmap
2330 wantRes: []Transaction{
2335 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2336 ErrorCode: []byte{0, 0, 0, 1},
2338 NewField(FieldError, []byte("You are not allowed to disconnect users.")),
2342 wantErr: assert.NoError,
2345 name: "when target user has 'cannot be disconnected' priv",
2349 Clients: map[uint16]*ClientConn{
2353 Access: func() accessBitmap {
2354 var bits accessBitmap
2355 bits.Set(accessCannotBeDiscon)
2363 Access: func() accessBitmap {
2364 var bits accessBitmap
2365 bits.Set(accessDisconUser)
2373 NewField(FieldUserID, []byte{0, 1}),
2376 wantRes: []Transaction{
2381 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2382 ErrorCode: []byte{0, 0, 0, 1},
2384 NewField(FieldError, []byte("unnamed is not allowed to be disconnected.")),
2388 wantErr: assert.NoError,
2391 for _, tt := range tests {
2392 t.Run(tt.name, func(t *testing.T) {
2393 gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
2394 if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
2397 tranAssertEqual(t, tt.wantRes, gotRes)
2402 func TestHandleSendInstantMsg(t *testing.T) {
2410 wantRes []Transaction
2411 wantErr assert.ErrorAssertionFunc
2414 name: "without required permission",
2418 Access: func() accessBitmap {
2419 var bits accessBitmap
2429 wantRes: []Transaction{
2434 ID: []byte{0, 0, 0, 0},
2435 ErrorCode: []byte{0, 0, 0, 1},
2437 NewField(FieldError, []byte("You are not allowed to send private messages.")),
2441 wantErr: assert.Error,
2444 name: "when client 1 sends a message to client 2",
2448 Access: func() accessBitmap {
2449 var bits accessBitmap
2450 bits.Set(accessSendPrivMsg)
2455 UserName: []byte("User1"),
2457 Clients: map[uint16]*ClientConn{
2459 AutoReply: []byte(nil),
2460 Flags: []byte{0, 0},
2468 NewField(FieldData, []byte("hai")),
2469 NewField(FieldUserID, []byte{0, 2}),
2472 wantRes: []Transaction{
2476 NewField(FieldData, []byte("hai")),
2477 NewField(FieldUserName, []byte("User1")),
2478 NewField(FieldUserID, []byte{0, 1}),
2479 NewField(FieldOptions, []byte{0, 1}),
2482 clientID: &[]byte{0, 1},
2486 ID: []byte{0, 0, 0, 0},
2487 ErrorCode: []byte{0, 0, 0, 0},
2488 Fields: []Field(nil),
2491 wantErr: assert.NoError,
2494 name: "when client 2 has autoreply enabled",
2498 Access: func() accessBitmap {
2499 var bits accessBitmap
2500 bits.Set(accessSendPrivMsg)
2505 UserName: []byte("User1"),
2507 Clients: map[uint16]*ClientConn{
2509 Flags: []byte{0, 0},
2511 UserName: []byte("User2"),
2512 AutoReply: []byte("autohai"),
2520 NewField(FieldData, []byte("hai")),
2521 NewField(FieldUserID, []byte{0, 2}),
2524 wantRes: []Transaction{
2528 NewField(FieldData, []byte("hai")),
2529 NewField(FieldUserName, []byte("User1")),
2530 NewField(FieldUserID, []byte{0, 1}),
2531 NewField(FieldOptions, []byte{0, 1}),
2536 NewField(FieldData, []byte("autohai")),
2537 NewField(FieldUserName, []byte("User2")),
2538 NewField(FieldUserID, []byte{0, 2}),
2539 NewField(FieldOptions, []byte{0, 1}),
2542 clientID: &[]byte{0, 1},
2546 ID: []byte{0, 0, 0, 0},
2547 ErrorCode: []byte{0, 0, 0, 0},
2548 Fields: []Field(nil),
2551 wantErr: assert.NoError,
2554 name: "when client 2 has refuse private messages enabled",
2558 Access: func() accessBitmap {
2559 var bits accessBitmap
2560 bits.Set(accessSendPrivMsg)
2565 UserName: []byte("User1"),
2567 Clients: map[uint16]*ClientConn{
2569 Flags: []byte{255, 255},
2571 UserName: []byte("User2"),
2579 NewField(FieldData, []byte("hai")),
2580 NewField(FieldUserID, []byte{0, 2}),
2583 wantRes: []Transaction{
2587 NewField(FieldData, []byte("User2 does not accept private messages.")),
2588 NewField(FieldUserName, []byte("User2")),
2589 NewField(FieldUserID, []byte{0, 2}),
2590 NewField(FieldOptions, []byte{0, 2}),
2593 clientID: &[]byte{0, 1},
2597 ID: []byte{0, 0, 0, 0},
2598 ErrorCode: []byte{0, 0, 0, 0},
2599 Fields: []Field(nil),
2602 wantErr: assert.NoError,
2605 for _, tt := range tests {
2606 t.Run(tt.name, func(t *testing.T) {
2607 gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
2608 if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
2612 tranAssertEqual(t, tt.wantRes, gotRes)
2617 func TestHandleDeleteFile(t *testing.T) {
2625 wantRes []Transaction
2626 wantErr assert.ErrorAssertionFunc
2629 name: "when user does not have required permission to delete a folder",
2633 Access: func() accessBitmap {
2634 var bits accessBitmap
2640 FileRoot: func() string {
2641 return "/fakeRoot/Files"
2644 FS: func() *MockFileStore {
2645 mfi := &MockFileInfo{}
2646 mfi.On("Mode").Return(fs.FileMode(0))
2647 mfi.On("Size").Return(int64(100))
2648 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2649 mfi.On("IsDir").Return(false)
2650 mfi.On("Name").Return("testfile")
2652 mfs := &MockFileStore{}
2653 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2654 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2655 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2659 Accounts: map[string]*Account{},
2663 TranDeleteFile, &[]byte{0, 1},
2664 NewField(FieldFileName, []byte("testfile")),
2665 NewField(FieldFilePath, []byte{
2673 wantRes: []Transaction{
2678 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2679 ErrorCode: []byte{0, 0, 0, 1},
2681 NewField(FieldError, []byte("You are not allowed to delete files.")),
2685 wantErr: assert.NoError,
2688 name: "deletes all associated metadata files",
2692 Access: func() accessBitmap {
2693 var bits accessBitmap
2694 bits.Set(accessDeleteFile)
2700 FileRoot: func() string {
2701 return "/fakeRoot/Files"
2704 FS: func() *MockFileStore {
2705 mfi := &MockFileInfo{}
2706 mfi.On("Mode").Return(fs.FileMode(0))
2707 mfi.On("Size").Return(int64(100))
2708 mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout))
2709 mfi.On("IsDir").Return(false)
2710 mfi.On("Name").Return("testfile")
2712 mfs := &MockFileStore{}
2713 mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil)
2714 mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err"))
2715 mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err"))
2717 mfs.On("RemoveAll", "/fakeRoot/Files/aaa/testfile").Return(nil)
2718 mfs.On("Remove", "/fakeRoot/Files/aaa/testfile.incomplete").Return(nil)
2719 mfs.On("Remove", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil)
2720 mfs.On("Remove", "/fakeRoot/Files/aaa/.info_testfile").Return(nil)
2724 Accounts: map[string]*Account{},
2728 TranDeleteFile, &[]byte{0, 1},
2729 NewField(FieldFileName, []byte("testfile")),
2730 NewField(FieldFilePath, []byte{
2738 wantRes: []Transaction{
2743 ID: []byte{0x0, 0x0, 0x0, 0x0},
2744 ErrorCode: []byte{0, 0, 0, 0},
2745 Fields: []Field(nil),
2748 wantErr: assert.NoError,
2751 for _, tt := range tests {
2752 t.Run(tt.name, func(t *testing.T) {
2753 gotRes, err := HandleDeleteFile(tt.args.cc, tt.args.t)
2754 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteFile(%v, %v)", tt.args.cc, tt.args.t)) {
2758 tranAssertEqual(t, tt.wantRes, gotRes)
2760 tt.args.cc.Server.FS.(*MockFileStore).AssertExpectations(t)
2765 func TestHandleGetFileNameList(t *testing.T) {
2773 wantRes []Transaction
2774 wantErr assert.ErrorAssertionFunc
2777 name: "when FieldFilePath is a drop box, but user does not have accessViewDropBoxes ",
2781 Access: func() accessBitmap {
2782 var bits accessBitmap
2789 FileRoot: func() string {
2790 path, _ := os.Getwd()
2791 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2797 TranGetFileNameList, &[]byte{0, 1},
2798 NewField(FieldFilePath, []byte{
2802 0x64, 0x72, 0x6f, 0x70, 0x20, 0x62, 0x6f, 0x78, // "drop box"
2806 wantRes: []Transaction{
2811 ID: []byte{0, 0, 0, 0},
2812 ErrorCode: []byte{0, 0, 0, 1},
2814 NewField(FieldError, []byte("You are not allowed to view drop boxes.")),
2818 wantErr: assert.NoError,
2821 name: "with file root",
2826 FileRoot: func() string {
2827 path, _ := os.Getwd()
2828 return filepath.Join(path, "/test/config/Files/getFileNameListTestDir")
2834 TranGetFileNameList, &[]byte{0, 1},
2835 NewField(FieldFilePath, []byte{
2841 wantRes: []Transaction{
2846 ID: []byte{0, 0, 0, 0},
2847 ErrorCode: []byte{0, 0, 0, 0},
2850 FieldFileNameWithInfo,
2852 fnwi := FileNameWithInfo{
2853 fileNameWithInfoHeader: fileNameWithInfoHeader{
2854 Type: [4]byte{0x54, 0x45, 0x58, 0x54},
2855 Creator: [4]byte{0x54, 0x54, 0x58, 0x54},
2856 FileSize: [4]byte{0, 0, 0x04, 0},
2858 NameScript: [2]byte{},
2859 NameSize: [2]byte{0, 0x0b},
2861 Name: []byte("testfile-1k"),
2863 b, _ := io.ReadAll(&fnwi)
2870 wantErr: assert.NoError,
2873 for _, tt := range tests {
2874 t.Run(tt.name, func(t *testing.T) {
2875 gotRes, err := HandleGetFileNameList(tt.args.cc, tt.args.t)
2876 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetFileNameList(%v, %v)", tt.args.cc, tt.args.t)) {
2880 tranAssertEqual(t, tt.wantRes, gotRes)
2885 func TestHandleGetClientInfoText(t *testing.T) {
2893 wantRes []Transaction
2894 wantErr assert.ErrorAssertionFunc
2897 name: "when user does not have required permission",
2901 Access: func() accessBitmap {
2902 var bits accessBitmap
2907 Accounts: map[string]*Account{},
2911 TranGetClientInfoText, &[]byte{0, 1},
2912 NewField(FieldUserID, []byte{0, 1}),
2915 wantRes: []Transaction{
2920 ID: []byte{0, 0, 0, 0},
2921 ErrorCode: []byte{0, 0, 0, 1},
2923 NewField(FieldError, []byte("You are not allowed to get client info.")),
2927 wantErr: assert.NoError,
2930 name: "with a valid user",
2933 UserName: []byte("Testy McTest"),
2934 RemoteAddr: "1.2.3.4:12345",
2936 Access: func() accessBitmap {
2937 var bits accessBitmap
2938 bits.Set(accessGetClientInfo)
2945 Accounts: map[string]*Account{},
2946 Clients: map[uint16]*ClientConn{
2948 UserName: []byte("Testy McTest"),
2949 RemoteAddr: "1.2.3.4:12345",
2951 Access: func() accessBitmap {
2952 var bits accessBitmap
2953 bits.Set(accessGetClientInfo)
2962 transfers: map[int]map[[4]byte]*FileTransfer{
2970 TranGetClientInfoText, &[]byte{0, 1},
2971 NewField(FieldUserID, []byte{0, 1}),
2974 wantRes: []Transaction{
2979 ID: []byte{0, 0, 0, 0},
2980 ErrorCode: []byte{0, 0, 0, 0},
2982 NewField(FieldData, []byte(
2983 strings.ReplaceAll(`Nickname: Testy McTest
2986 Address: 1.2.3.4:12345
2988 -------- File Downloads ---------
2992 ------- Folder Downloads --------
2996 --------- File Uploads ----------
3000 -------- Folder Uploads ---------
3004 ------- Waiting Downloads -------
3010 NewField(FieldUserName, []byte("Testy McTest")),
3014 wantErr: assert.NoError,
3017 for _, tt := range tests {
3018 t.Run(tt.name, func(t *testing.T) {
3019 gotRes, err := HandleGetClientInfoText(tt.args.cc, tt.args.t)
3020 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetClientInfoText(%v, %v)", tt.args.cc, tt.args.t)) {
3023 tranAssertEqual(t, tt.wantRes, gotRes)
3028 func TestHandleTranAgreed(t *testing.T) {
3036 wantRes []Transaction
3037 wantErr assert.ErrorAssertionFunc
3040 name: "normal request flow",
3044 Access: func() accessBitmap {
3045 var bits accessBitmap
3046 bits.Set(accessDisconUser)
3047 bits.Set(accessAnyName)
3051 Flags: []byte{0, 1},
3052 Version: []byte{0, 1},
3054 logger: NewTestLogger(),
3057 BannerFile: "banner.jpg",
3063 NewField(FieldUserName, []byte("username")),
3064 NewField(FieldUserIconID, []byte{0, 1}),
3065 NewField(FieldOptions, []byte{0, 0}),
3068 wantRes: []Transaction{
3070 clientID: &[]byte{0, 1},
3073 Type: []byte{0, 0x7a},
3074 ID: []byte{0, 0, 0, 0},
3075 ErrorCode: []byte{0, 0, 0, 0},
3077 NewField(FieldBannerType, []byte("JPEG")),
3081 clientID: &[]byte{0, 1},
3085 ID: []byte{0, 0, 0, 0},
3086 ErrorCode: []byte{0, 0, 0, 0},
3090 wantErr: assert.NoError,
3093 for _, tt := range tests {
3094 t.Run(tt.name, func(t *testing.T) {
3095 gotRes, err := HandleTranAgreed(tt.args.cc, tt.args.t)
3096 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranAgreed(%v, %v)", tt.args.cc, tt.args.t)) {
3099 tranAssertEqual(t, tt.wantRes, gotRes)
3104 func TestHandleSetClientUserInfo(t *testing.T) {
3112 wantRes []Transaction
3113 wantErr assert.ErrorAssertionFunc
3116 name: "when client does not have accessAnyName",
3120 Access: func() accessBitmap {
3121 var bits accessBitmap
3126 UserName: []byte("Guest"),
3127 Flags: []byte{0, 1},
3129 Clients: map[uint16]*ClientConn{
3137 TranSetClientUserInfo, nil,
3138 NewField(FieldUserIconID, []byte{0, 1}),
3139 NewField(FieldUserName, []byte("NOPE")),
3142 wantRes: []Transaction{
3144 clientID: &[]byte{0, 1},
3147 Type: []byte{0x01, 0x2d},
3148 ID: []byte{0, 0, 0, 0},
3149 ErrorCode: []byte{0, 0, 0, 0},
3151 NewField(FieldUserID, []byte{0, 1}),
3152 NewField(FieldUserIconID, []byte{0, 1}),
3153 NewField(FieldUserFlags, []byte{0, 1}),
3154 NewField(FieldUserName, []byte("Guest"))},
3157 wantErr: assert.NoError,
3160 for _, tt := range tests {
3161 t.Run(tt.name, func(t *testing.T) {
3162 gotRes, err := HandleSetClientUserInfo(tt.args.cc, tt.args.t)
3163 if !tt.wantErr(t, err, fmt.Sprintf("HandleSetClientUserInfo(%v, %v)", tt.args.cc, tt.args.t)) {
3167 tranAssertEqual(t, tt.wantRes, gotRes)
3172 func TestHandleDelNewsItem(t *testing.T) {
3180 wantRes []Transaction
3181 wantErr assert.ErrorAssertionFunc
3184 name: "when user does not have permission to delete a news category",
3188 Access: accessBitmap{},
3192 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3194 Type: [2]byte{0, 3},
3201 TranDelNewsItem, nil,
3202 NewField(FieldNewsPath,
3207 0x74, 0x65, 0x73, 0x74,
3212 wantRes: []Transaction{
3214 clientID: &[]byte{0, 1},
3218 ID: []byte{0, 0, 0, 0},
3219 ErrorCode: []byte{0, 0, 0, 1},
3221 NewField(FieldError, []byte("You are not allowed to delete news categories.")),
3225 wantErr: assert.NoError,
3228 name: "when user does not have permission to delete a news folder",
3232 Access: accessBitmap{},
3236 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3238 Type: [2]byte{0, 2},
3245 TranDelNewsItem, nil,
3246 NewField(FieldNewsPath,
3251 0x74, 0x65, 0x73, 0x74,
3256 wantRes: []Transaction{
3258 clientID: &[]byte{0, 1},
3262 ID: []byte{0, 0, 0, 0},
3263 ErrorCode: []byte{0, 0, 0, 1},
3265 NewField(FieldError, []byte("You are not allowed to delete news folders.")),
3269 wantErr: assert.NoError,
3272 name: "when user deletes a news folder",
3276 Access: func() accessBitmap {
3277 var bits accessBitmap
3278 bits.Set(accessNewsDeleteFldr)
3284 ConfigDir: "/fakeConfigRoot",
3285 FS: func() *MockFileStore {
3286 mfs := &MockFileStore{}
3287 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3290 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3292 Type: [2]byte{0, 2},
3299 TranDelNewsItem, nil,
3300 NewField(FieldNewsPath,
3305 0x74, 0x65, 0x73, 0x74,
3310 wantRes: []Transaction{
3312 clientID: &[]byte{0, 1},
3316 ID: []byte{0, 0, 0, 0},
3317 ErrorCode: []byte{0, 0, 0, 0},
3321 wantErr: assert.NoError,
3324 for _, tt := range tests {
3325 t.Run(tt.name, func(t *testing.T) {
3326 gotRes, err := HandleDelNewsItem(tt.args.cc, tt.args.t)
3327 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsItem(%v, %v)", tt.args.cc, tt.args.t)) {
3330 tranAssertEqual(t, tt.wantRes, gotRes)
3335 func TestHandleTranOldPostNews(t *testing.T) {
3343 wantRes []Transaction
3344 wantErr assert.ErrorAssertionFunc
3347 name: "when user does not have required permission",
3351 Access: func() accessBitmap {
3352 var bits accessBitmap
3358 TranOldPostNews, &[]byte{0, 1},
3359 NewField(FieldData, []byte("hai")),
3362 wantRes: []Transaction{
3367 ID: []byte{0, 0, 0, 0},
3368 ErrorCode: []byte{0, 0, 0, 1},
3370 NewField(FieldError, []byte("You are not allowed to post news.")),
3374 wantErr: assert.NoError,
3377 name: "when user posts news update",
3381 Access: func() accessBitmap {
3382 var bits accessBitmap
3383 bits.Set(accessNewsPostArt)
3388 FS: func() *MockFileStore {
3389 mfs := &MockFileStore{}
3390 mfs.On("WriteFile", "/fakeConfigRoot/MessageBoard.txt", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3393 ConfigDir: "/fakeConfigRoot",
3398 TranOldPostNews, &[]byte{0, 1},
3399 NewField(FieldData, []byte("hai")),
3402 wantRes: []Transaction{
3407 ID: []byte{0, 0, 0, 0},
3408 ErrorCode: []byte{0, 0, 0, 0},
3411 wantErr: assert.NoError,
3414 for _, tt := range tests {
3415 t.Run(tt.name, func(t *testing.T) {
3416 gotRes, err := HandleTranOldPostNews(tt.args.cc, tt.args.t)
3417 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranOldPostNews(%v, %v)", tt.args.cc, tt.args.t)) {
3421 tranAssertEqual(t, tt.wantRes, gotRes)
3426 func TestHandleInviteNewChat(t *testing.T) {
3434 wantRes []Transaction
3435 wantErr assert.ErrorAssertionFunc
3438 name: "when user does not have required permission",
3442 Access: func() accessBitmap {
3443 var bits accessBitmap
3448 t: NewTransaction(TranInviteNewChat, &[]byte{0, 1}),
3450 wantRes: []Transaction{
3455 ID: []byte{0, 0, 0, 0},
3456 ErrorCode: []byte{0, 0, 0, 1},
3458 NewField(FieldError, []byte("You are not allowed to request private chat.")),
3462 wantErr: assert.NoError,
3465 name: "when userA invites userB to new private chat",
3470 Access: func() accessBitmap {
3471 var bits accessBitmap
3472 bits.Set(accessOpenChat)
3476 UserName: []byte("UserA"),
3478 Flags: []byte{0, 0},
3480 Clients: map[uint16]*ClientConn{
3483 UserName: []byte("UserB"),
3484 Flags: []byte{0, 0},
3487 PrivateChats: make(map[uint32]*PrivateChat),
3491 TranInviteNewChat, &[]byte{0, 1},
3492 NewField(FieldUserID, []byte{0, 2}),
3495 wantRes: []Transaction{
3497 clientID: &[]byte{0, 2},
3500 Type: []byte{0, 0x71},
3501 ID: []byte{0, 0, 0, 0},
3502 ErrorCode: []byte{0, 0, 0, 0},
3504 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3505 NewField(FieldUserName, []byte("UserA")),
3506 NewField(FieldUserID, []byte{0, 1}),
3511 clientID: &[]byte{0, 1},
3515 ID: []byte{0, 0, 0, 0},
3516 ErrorCode: []byte{0, 0, 0, 0},
3518 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3519 NewField(FieldUserName, []byte("UserA")),
3520 NewField(FieldUserID, []byte{0, 1}),
3521 NewField(FieldUserIconID, []byte{0, 1}),
3522 NewField(FieldUserFlags, []byte{0, 0}),
3526 wantErr: assert.NoError,
3529 name: "when userA invites userB to new private chat, but UserB has refuse private chat enabled",
3534 Access: func() accessBitmap {
3535 var bits accessBitmap
3536 bits.Set(accessOpenChat)
3540 UserName: []byte("UserA"),
3542 Flags: []byte{0, 0},
3544 Clients: map[uint16]*ClientConn{
3547 UserName: []byte("UserB"),
3548 Flags: []byte{255, 255},
3551 PrivateChats: make(map[uint32]*PrivateChat),
3555 TranInviteNewChat, &[]byte{0, 1},
3556 NewField(FieldUserID, []byte{0, 2}),
3559 wantRes: []Transaction{
3561 clientID: &[]byte{0, 1},
3564 Type: []byte{0, 0x68},
3565 ID: []byte{0, 0, 0, 0},
3566 ErrorCode: []byte{0, 0, 0, 0},
3568 NewField(FieldData, []byte("UserB does not accept private chats.")),
3569 NewField(FieldUserName, []byte("UserB")),
3570 NewField(FieldUserID, []byte{0, 2}),
3571 NewField(FieldOptions, []byte{0, 2}),
3575 clientID: &[]byte{0, 1},
3579 ID: []byte{0, 0, 0, 0},
3580 ErrorCode: []byte{0, 0, 0, 0},
3582 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3583 NewField(FieldUserName, []byte("UserA")),
3584 NewField(FieldUserID, []byte{0, 1}),
3585 NewField(FieldUserIconID, []byte{0, 1}),
3586 NewField(FieldUserFlags, []byte{0, 0}),
3590 wantErr: assert.NoError,
3593 for _, tt := range tests {
3594 t.Run(tt.name, func(t *testing.T) {
3596 gotRes, err := HandleInviteNewChat(tt.args.cc, tt.args.t)
3597 if !tt.wantErr(t, err, fmt.Sprintf("HandleInviteNewChat(%v, %v)", tt.args.cc, tt.args.t)) {
3600 tranAssertEqual(t, tt.wantRes, gotRes)
3605 func TestHandleGetNewsArtData(t *testing.T) {
3613 wantRes []Transaction
3614 wantErr assert.ErrorAssertionFunc
3617 name: "when user does not have required permission",
3621 Access: func() accessBitmap {
3622 var bits accessBitmap
3627 Accounts: map[string]*Account{},
3631 TranGetNewsArtData, &[]byte{0, 1},
3634 wantRes: []Transaction{
3639 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
3640 ErrorCode: []byte{0, 0, 0, 1},
3642 NewField(FieldError, []byte("You are not allowed to read news.")),
3646 wantErr: assert.NoError,
3649 for _, tt := range tests {
3650 t.Run(tt.name, func(t *testing.T) {
3651 gotRes, err := HandleGetNewsArtData(tt.args.cc, tt.args.t)
3652 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtData(%v, %v)", tt.args.cc, tt.args.t)) {
3655 tranAssertEqual(t, tt.wantRes, gotRes)
3660 func TestHandleGetNewsArtNameList(t *testing.T) {
3668 wantRes []Transaction
3669 wantErr assert.ErrorAssertionFunc
3672 name: "when user does not have required permission",
3676 Access: func() accessBitmap {
3677 var bits accessBitmap
3682 Accounts: map[string]*Account{},
3686 TranGetNewsArtNameList, &[]byte{0, 1},
3689 wantRes: []Transaction{
3694 ErrorCode: []byte{0, 0, 0, 1},
3696 NewField(FieldError, []byte("You are not allowed to read news.")),
3700 wantErr: assert.NoError,
3703 for _, tt := range tests {
3704 t.Run(tt.name, func(t *testing.T) {
3705 gotRes, err := HandleGetNewsArtNameList(tt.args.cc, tt.args.t)
3706 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtNameList(%v, %v)", tt.args.cc, tt.args.t)) {
3709 tranAssertEqual(t, tt.wantRes, gotRes)
3714 func TestHandleNewNewsFldr(t *testing.T) {
3722 wantRes []Transaction
3723 wantErr assert.ErrorAssertionFunc
3726 name: "when user does not have required permission",
3730 Access: func() accessBitmap {
3731 var bits accessBitmap
3736 Accounts: map[string]*Account{},
3740 TranGetNewsArtNameList, &[]byte{0, 1},
3743 wantRes: []Transaction{
3748 ErrorCode: []byte{0, 0, 0, 1},
3750 NewField(FieldError, []byte("You are not allowed to create news folders.")),
3754 wantErr: assert.NoError,
3757 name: "with a valid request",
3761 Access: func() accessBitmap {
3762 var bits accessBitmap
3763 bits.Set(accessNewsCreateFldr)
3767 logger: NewTestLogger(),
3770 ConfigDir: "/fakeConfigRoot",
3771 FS: func() *MockFileStore {
3772 mfs := &MockFileStore{}
3773 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil)
3776 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3778 Type: [2]byte{0, 2},
3780 SubCats: make(map[string]NewsCategoryListData15),
3786 TranGetNewsArtNameList, &[]byte{0, 1},
3787 NewField(FieldFileName, []byte("testFolder")),
3788 NewField(FieldNewsPath,
3793 0x74, 0x65, 0x73, 0x74,
3798 wantRes: []Transaction{
3800 clientID: &[]byte{0, 1},
3804 ID: []byte{0, 0, 0, 0},
3805 ErrorCode: []byte{0, 0, 0, 0},
3809 wantErr: assert.NoError,
3812 // Name: "when there is an error writing the threaded news file",
3815 // Account: &Account{
3816 // Access: func() accessBitmap {
3817 // var bits accessBitmap
3818 // bits.Set(accessNewsCreateFldr)
3822 // logger: NewTestLogger(),
3823 // ID: &[]byte{0, 1},
3825 // ConfigDir: "/fakeConfigRoot",
3826 // FS: func() *MockFileStore {
3827 // mfs := &MockFileStore{}
3828 // mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(os.ErrNotExist)
3831 // ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3833 // Type: []byte{0, 2},
3837 // SubCats: make(map[string]NewsCategoryListData15),
3842 // t: NewTransaction(
3843 // TranGetNewsArtNameList, &[]byte{0, 1},
3844 // NewField(FieldFileName, []byte("testFolder")),
3845 // NewField(FieldNewsPath,
3850 // 0x74, 0x65, 0x73, 0x74,
3855 // wantRes: []Transaction{
3857 // clientID: &[]byte{0, 1},
3860 // Type: []byte{0, 0},
3861 // ErrorCode: []byte{0, 0, 0, 1},
3863 // NewField(FieldError, []byte("Error creating news folder.")),
3867 // wantErr: assert.Error,
3870 for _, tt := range tests {
3871 t.Run(tt.name, func(t *testing.T) {
3872 gotRes, err := HandleNewNewsFldr(tt.args.cc, tt.args.t)
3873 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewNewsFldr(%v, %v)", tt.args.cc, tt.args.t)) {
3876 tranAssertEqual(t, tt.wantRes, gotRes)
3881 func TestHandleDownloadBanner(t *testing.T) {
3889 wantRes []Transaction
3890 wantErr assert.ErrorAssertionFunc
3892 // TODO: Add test cases.
3894 for _, tt := range tests {
3895 t.Run(tt.name, func(t *testing.T) {
3896 gotRes, err := HandleDownloadBanner(tt.args.cc, tt.args.t)
3897 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)) {
3900 assert.Equalf(t, tt.wantRes, gotRes, "HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)