6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/mock"
17 func TestHandleSetChatSubject(t *testing.T) {
29 name: "sends chat subject to private chat members",
32 UserName: []byte{0x00, 0x01},
34 PrivateChats: map[uint32]*PrivateChat{
37 ClientConn: map[uint16]*ClientConn{
40 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
46 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
53 Clients: map[uint16]*ClientConn{
56 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
62 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
72 Type: []byte{0, 0x6a},
73 ID: []byte{0, 0, 0, 1},
74 ErrorCode: []byte{0, 0, 0, 0},
76 NewField(FieldChatID, []byte{0, 0, 0, 1}),
77 NewField(FieldChatSubject, []byte("Test Subject")),
83 clientID: &[]byte{0, 1},
86 Type: []byte{0, 0x77},
87 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
88 ErrorCode: []byte{0, 0, 0, 0},
90 NewField(FieldChatID, []byte{0, 0, 0, 1}),
91 NewField(FieldChatSubject, []byte("Test Subject")),
95 clientID: &[]byte{0, 2},
98 Type: []byte{0, 0x77},
99 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
100 ErrorCode: []byte{0, 0, 0, 0},
102 NewField(FieldChatID, []byte{0, 0, 0, 1}),
103 NewField(FieldChatSubject, []byte("Test Subject")),
110 for _, tt := range tests {
111 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
113 t.Run(tt.name, func(t *testing.T) {
114 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
115 if (err != nil) != tt.wantErr {
116 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
119 if !assert.Equal(t, tt.want, got) {
120 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
126 func TestHandleLeaveChat(t *testing.T) {
138 name: "returns expected transactions",
143 PrivateChats: map[uint32]*PrivateChat{
145 ClientConn: map[uint16]*ClientConn{
148 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
154 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
161 Clients: map[uint16]*ClientConn{
164 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
170 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
177 t: NewTransaction(TranDeleteUser, nil, NewField(FieldChatID, []byte{0, 0, 0, 1})),
181 clientID: &[]byte{0, 1},
184 Type: []byte{0, 0x76},
185 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
186 ErrorCode: []byte{0, 0, 0, 0},
188 NewField(FieldChatID, []byte{0, 0, 0, 1}),
189 NewField(FieldUserID, []byte{0, 2}),
196 for _, tt := range tests {
198 t.Run(tt.name, func(t *testing.T) {
199 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
200 if (err != nil) != tt.wantErr {
201 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
204 if !assert.Equal(t, tt.want, got) {
205 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
211 func TestHandleGetUserNameList(t *testing.T) {
223 name: "replies with userlist transaction",
229 Clients: map[uint16]*ClientConn{
234 UserName: []byte{0, 4},
240 UserName: []byte{0, 4},
246 ID: []byte{0, 0, 0, 1},
252 clientID: &[]byte{1, 1},
256 ID: []byte{0, 0, 0, 1},
257 ErrorCode: []byte{0, 0, 0, 0},
260 FieldUsernameWithInfo,
261 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
264 FieldUsernameWithInfo,
265 []byte{00, 02, 00, 02, 00, 03, 00, 02, 00, 04},
273 for _, tt := range tests {
274 t.Run(tt.name, func(t *testing.T) {
275 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
276 if (err != nil) != tt.wantErr {
277 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
280 assert.Equal(t, tt.want, got)
285 func TestHandleChatSend(t *testing.T) {
297 name: "sends chat msg transaction to all clients",
301 Access: func() accessBitmap {
302 var bits accessBitmap
303 bits.Set(accessSendChat)
307 UserName: []byte{0x00, 0x01},
309 Clients: map[uint16]*ClientConn{
312 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
318 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
327 NewField(FieldData, []byte("hai")),
333 clientID: &[]byte{0, 1},
336 Type: []byte{0, 0x6a},
337 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
338 ErrorCode: []byte{0, 0, 0, 0},
340 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
344 clientID: &[]byte{0, 2},
347 Type: []byte{0, 0x6a},
348 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
349 ErrorCode: []byte{0, 0, 0, 0},
351 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
358 name: "treats Chat ID 00 00 00 00 as a public chat message",
362 Access: func() accessBitmap {
363 var bits accessBitmap
364 bits.Set(accessSendChat)
368 UserName: []byte{0x00, 0x01},
370 Clients: map[uint16]*ClientConn{
373 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
379 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
388 NewField(FieldData, []byte("hai")),
389 NewField(FieldChatID, []byte{0, 0, 0, 0}),
395 clientID: &[]byte{0, 1},
398 Type: []byte{0, 0x6a},
399 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
400 ErrorCode: []byte{0, 0, 0, 0},
402 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
406 clientID: &[]byte{0, 2},
409 Type: []byte{0, 0x6a},
410 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
411 ErrorCode: []byte{0, 0, 0, 0},
413 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
420 name: "when user does not have required permission",
424 Access: func() accessBitmap {
425 var bits accessBitmap
430 Accounts: map[string]*Account{},
434 TranChatSend, &[]byte{0, 1},
435 NewField(FieldData, []byte("hai")),
443 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
444 ErrorCode: []byte{0, 0, 0, 1},
446 NewField(FieldError, []byte("You are not allowed to participate in chat.")),
453 name: "sends chat msg as emote if FieldChatOptions is set to 1",
457 Access: func() accessBitmap {
458 var bits accessBitmap
459 bits.Set(accessSendChat)
463 UserName: []byte("Testy McTest"),
465 Clients: map[uint16]*ClientConn{
468 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
474 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
483 NewField(FieldData, []byte("performed action")),
484 NewField(FieldChatOptions, []byte{0x00, 0x01}),
490 clientID: &[]byte{0, 1},
493 Type: []byte{0, 0x6a},
494 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
495 ErrorCode: []byte{0, 0, 0, 0},
497 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
501 clientID: &[]byte{0, 2},
504 Type: []byte{0, 0x6a},
505 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
506 ErrorCode: []byte{0, 0, 0, 0},
508 NewField(FieldData, []byte("\r*** Testy McTest performed action")),
515 name: "does not send chat msg as emote if FieldChatOptions is set to 0",
519 Access: func() accessBitmap {
520 var bits accessBitmap
521 bits.Set(accessSendChat)
525 UserName: []byte("Testy McTest"),
527 Clients: map[uint16]*ClientConn{
530 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
536 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
545 NewField(FieldData, []byte("hello")),
546 NewField(FieldChatOptions, []byte{0x00, 0x00}),
552 clientID: &[]byte{0, 1},
555 Type: []byte{0, 0x6a},
556 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
557 ErrorCode: []byte{0, 0, 0, 0},
559 NewField(FieldData, []byte("\r Testy McTest: hello")),
563 clientID: &[]byte{0, 2},
566 Type: []byte{0, 0x6a},
567 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
568 ErrorCode: []byte{0, 0, 0, 0},
570 NewField(FieldData, []byte("\r Testy McTest: hello")),
577 name: "only sends chat msg to clients with accessReadChat permission",
581 Access: func() accessBitmap {
582 var bits accessBitmap
583 bits.Set(accessSendChat)
587 UserName: []byte{0x00, 0x01},
589 Clients: map[uint16]*ClientConn{
592 Access: func() accessBitmap {
593 var bits accessBitmap
594 bits.Set(accessReadChat)
601 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
610 NewField(FieldData, []byte("hai")),
616 clientID: &[]byte{0, 1},
619 Type: []byte{0, 0x6a},
620 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
621 ErrorCode: []byte{0, 0, 0, 0},
623 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
630 name: "only sends private chat msg to members of private chat",
634 Access: func() accessBitmap {
635 var bits accessBitmap
636 bits.Set(accessSendChat)
640 UserName: []byte{0x00, 0x01},
642 PrivateChats: map[uint32]*PrivateChat{
644 ClientConn: map[uint16]*ClientConn{
654 Clients: map[uint16]*ClientConn{
657 Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
663 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
669 Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0},
678 NewField(FieldData, []byte("hai")),
679 NewField(FieldChatID, []byte{0, 0, 0, 1}),
685 clientID: &[]byte{0, 1},
688 Type: []byte{0, 0x6a},
689 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
690 ErrorCode: []byte{0, 0, 0, 0},
692 NewField(FieldChatID, []byte{0, 0, 0, 1}),
693 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
697 clientID: &[]byte{0, 2},
700 Type: []byte{0, 0x6a},
701 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
702 ErrorCode: []byte{0, 0, 0, 0},
704 NewField(FieldChatID, []byte{0, 0, 0, 1}),
705 NewField(FieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
712 for _, tt := range tests {
713 t.Run(tt.name, func(t *testing.T) {
714 got, err := HandleChatSend(tt.args.cc, tt.args.t)
716 if (err != nil) != tt.wantErr {
717 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
720 tranAssertEqual(t, tt.want, got)
725 func TestHandleGetFileInfo(t *testing.T) {
726 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
735 wantRes []Transaction
739 name: "returns expected fields when a valid file is requested",
742 ID: &[]byte{0x00, 0x01},
746 FileRoot: func() string {
747 path, _ := os.Getwd()
748 return filepath.Join(path, "/test/config/Files")
754 TranGetFileInfo, nil,
755 NewField(FieldFileName, []byte("testfile.txt")),
756 NewField(FieldFilePath, []byte{0x00, 0x00}),
759 wantRes: []Transaction{
761 clientID: &[]byte{0, 1},
765 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
766 ErrorCode: []byte{0, 0, 0, 0},
768 NewField(FieldFileName, []byte("testfile.txt")),
769 NewField(FieldFileTypeString, []byte("Text File")),
770 NewField(FieldFileCreatorString, []byte("ttxt")),
771 NewField(FieldFileComment, []byte{}),
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[5].Data = make([]byte, 8)
795 gotRes[0].Fields[6].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, negateString([]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, negateString([]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, negateString([]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, _ := fnwi.MarshalBinary()
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{
3203 TranDelNewsItem, nil,
3204 NewField(FieldNewsPath,
3209 0x74, 0x65, 0x73, 0x74,
3214 wantRes: []Transaction{
3216 clientID: &[]byte{0, 1},
3220 ID: []byte{0, 0, 0, 0},
3221 ErrorCode: []byte{0, 0, 0, 1},
3223 NewField(FieldError, []byte("You are not allowed to delete news categories.")),
3227 wantErr: assert.NoError,
3230 name: "when user does not have permission to delete a news folder",
3234 Access: accessBitmap{},
3238 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3249 TranDelNewsItem, nil,
3250 NewField(FieldNewsPath,
3255 0x74, 0x65, 0x73, 0x74,
3260 wantRes: []Transaction{
3262 clientID: &[]byte{0, 1},
3266 ID: []byte{0, 0, 0, 0},
3267 ErrorCode: []byte{0, 0, 0, 1},
3269 NewField(FieldError, []byte("You are not allowed to delete news folders.")),
3273 wantErr: assert.NoError,
3276 name: "when user deletes a news folder",
3280 Access: func() accessBitmap {
3281 var bits accessBitmap
3282 bits.Set(accessNewsDeleteFldr)
3288 ConfigDir: "/fakeConfigRoot",
3289 FS: func() *MockFileStore {
3290 mfs := &MockFileStore{}
3291 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3294 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3305 TranDelNewsItem, nil,
3306 NewField(FieldNewsPath,
3311 0x74, 0x65, 0x73, 0x74,
3316 wantRes: []Transaction{
3318 clientID: &[]byte{0, 1},
3322 ID: []byte{0, 0, 0, 0},
3323 ErrorCode: []byte{0, 0, 0, 0},
3327 wantErr: assert.NoError,
3330 for _, tt := range tests {
3331 t.Run(tt.name, func(t *testing.T) {
3332 gotRes, err := HandleDelNewsItem(tt.args.cc, tt.args.t)
3333 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsItem(%v, %v)", tt.args.cc, tt.args.t)) {
3336 tranAssertEqual(t, tt.wantRes, gotRes)
3341 func TestHandleDownloadBanner(t *testing.T) {
3349 wantRes []Transaction
3350 wantErr assert.ErrorAssertionFunc
3353 name: "returns expected response",
3357 transfers: map[int]map[[4]byte]*FileTransfer{
3361 ConfigDir: "/config",
3363 BannerFile: "banner.jpg",
3365 fileTransfers: map[[4]byte]*FileTransfer{},
3366 FS: func() *MockFileStore {
3367 mfi := &MockFileInfo{}
3368 mfi.On("Size").Return(int64(100))
3370 mfs := &MockFileStore{}
3371 mfs.On("Stat", "/config/banner.jpg").Return(mfi, nil)
3376 t: NewTransaction(TranDownloadBanner, nil),
3378 wantRes: []Transaction{
3380 clientID: &[]byte{0, 1},
3384 ID: []byte{0, 0, 0, 0},
3385 ErrorCode: []byte{0, 0, 0, 0},
3387 NewField(FieldRefNum, []byte{1, 2, 3, 4}),
3388 NewField(FieldTransferSize, []byte{0, 0, 0, 0x64}),
3392 wantErr: assert.NoError,
3395 for _, tt := range tests {
3396 t.Run(tt.name, func(t *testing.T) {
3397 gotRes, err := HandleDownloadBanner(tt.args.cc, tt.args.t)
3398 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)) {
3402 tranAssertEqual(t, tt.wantRes, gotRes)
3407 func TestHandleTranOldPostNews(t *testing.T) {
3415 wantRes []Transaction
3416 wantErr assert.ErrorAssertionFunc
3419 name: "when user does not have required permission",
3423 Access: func() accessBitmap {
3424 var bits accessBitmap
3430 TranOldPostNews, &[]byte{0, 1},
3431 NewField(FieldData, []byte("hai")),
3434 wantRes: []Transaction{
3439 ID: []byte{0, 0, 0, 0},
3440 ErrorCode: []byte{0, 0, 0, 1},
3442 NewField(FieldError, []byte("You are not allowed to post news.")),
3446 wantErr: assert.NoError,
3449 name: "when user posts news update",
3453 Access: func() accessBitmap {
3454 var bits accessBitmap
3455 bits.Set(accessNewsPostArt)
3460 FS: func() *MockFileStore {
3461 mfs := &MockFileStore{}
3462 mfs.On("WriteFile", "/fakeConfigRoot/MessageBoard.txt", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
3465 ConfigDir: "/fakeConfigRoot",
3470 TranOldPostNews, &[]byte{0, 1},
3471 NewField(FieldData, []byte("hai")),
3474 wantRes: []Transaction{
3479 ID: []byte{0, 0, 0, 0},
3480 ErrorCode: []byte{0, 0, 0, 0},
3483 wantErr: assert.NoError,
3486 for _, tt := range tests {
3487 t.Run(tt.name, func(t *testing.T) {
3488 gotRes, err := HandleTranOldPostNews(tt.args.cc, tt.args.t)
3489 if !tt.wantErr(t, err, fmt.Sprintf("HandleTranOldPostNews(%v, %v)", tt.args.cc, tt.args.t)) {
3493 tranAssertEqual(t, tt.wantRes, gotRes)
3498 func TestHandleInviteNewChat(t *testing.T) {
3506 wantRes []Transaction
3507 wantErr assert.ErrorAssertionFunc
3510 name: "when user does not have required permission",
3514 Access: func() accessBitmap {
3515 var bits accessBitmap
3520 t: NewTransaction(TranInviteNewChat, &[]byte{0, 1}),
3522 wantRes: []Transaction{
3527 ID: []byte{0, 0, 0, 0},
3528 ErrorCode: []byte{0, 0, 0, 1},
3530 NewField(FieldError, []byte("You are not allowed to request private chat.")),
3534 wantErr: assert.NoError,
3537 name: "when userA invites userB to new private chat",
3542 Access: func() accessBitmap {
3543 var bits accessBitmap
3544 bits.Set(accessOpenChat)
3548 UserName: []byte("UserA"),
3550 Flags: []byte{0, 0},
3552 Clients: map[uint16]*ClientConn{
3555 UserName: []byte("UserB"),
3556 Flags: []byte{0, 0},
3559 PrivateChats: make(map[uint32]*PrivateChat),
3563 TranInviteNewChat, &[]byte{0, 1},
3564 NewField(FieldUserID, []byte{0, 2}),
3567 wantRes: []Transaction{
3569 clientID: &[]byte{0, 2},
3572 Type: []byte{0, 0x71},
3573 ID: []byte{0, 0, 0, 0},
3574 ErrorCode: []byte{0, 0, 0, 0},
3576 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3577 NewField(FieldUserName, []byte("UserA")),
3578 NewField(FieldUserID, []byte{0, 1}),
3583 clientID: &[]byte{0, 1},
3587 ID: []byte{0, 0, 0, 0},
3588 ErrorCode: []byte{0, 0, 0, 0},
3590 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3591 NewField(FieldUserName, []byte("UserA")),
3592 NewField(FieldUserID, []byte{0, 1}),
3593 NewField(FieldUserIconID, []byte{0, 1}),
3594 NewField(FieldUserFlags, []byte{0, 0}),
3598 wantErr: assert.NoError,
3601 name: "when userA invites userB to new private chat, but UserB has refuse private chat enabled",
3606 Access: func() accessBitmap {
3607 var bits accessBitmap
3608 bits.Set(accessOpenChat)
3612 UserName: []byte("UserA"),
3614 Flags: []byte{0, 0},
3616 Clients: map[uint16]*ClientConn{
3619 UserName: []byte("UserB"),
3620 Flags: []byte{255, 255},
3623 PrivateChats: make(map[uint32]*PrivateChat),
3627 TranInviteNewChat, &[]byte{0, 1},
3628 NewField(FieldUserID, []byte{0, 2}),
3631 wantRes: []Transaction{
3633 clientID: &[]byte{0, 1},
3636 Type: []byte{0, 0x68},
3637 ID: []byte{0, 0, 0, 0},
3638 ErrorCode: []byte{0, 0, 0, 0},
3640 NewField(FieldData, []byte("UserB does not accept private chats.")),
3641 NewField(FieldUserName, []byte("UserB")),
3642 NewField(FieldUserID, []byte{0, 2}),
3643 NewField(FieldOptions, []byte{0, 2}),
3647 clientID: &[]byte{0, 1},
3651 ID: []byte{0, 0, 0, 0},
3652 ErrorCode: []byte{0, 0, 0, 0},
3654 NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
3655 NewField(FieldUserName, []byte("UserA")),
3656 NewField(FieldUserID, []byte{0, 1}),
3657 NewField(FieldUserIconID, []byte{0, 1}),
3658 NewField(FieldUserFlags, []byte{0, 0}),
3662 wantErr: assert.NoError,
3665 for _, tt := range tests {
3666 t.Run(tt.name, func(t *testing.T) {
3668 gotRes, err := HandleInviteNewChat(tt.args.cc, tt.args.t)
3669 if !tt.wantErr(t, err, fmt.Sprintf("HandleInviteNewChat(%v, %v)", tt.args.cc, tt.args.t)) {
3672 tranAssertEqual(t, tt.wantRes, gotRes)
3677 func TestHandleGetNewsArtData(t *testing.T) {
3685 wantRes []Transaction
3686 wantErr assert.ErrorAssertionFunc
3689 name: "when user does not have required permission",
3693 Access: func() accessBitmap {
3694 var bits accessBitmap
3699 Accounts: map[string]*Account{},
3703 TranGetNewsArtData, &[]byte{0, 1},
3706 wantRes: []Transaction{
3711 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
3712 ErrorCode: []byte{0, 0, 0, 1},
3714 NewField(FieldError, []byte("You are not allowed to read news.")),
3718 wantErr: assert.NoError,
3721 for _, tt := range tests {
3722 t.Run(tt.name, func(t *testing.T) {
3723 gotRes, err := HandleGetNewsArtData(tt.args.cc, tt.args.t)
3724 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtData(%v, %v)", tt.args.cc, tt.args.t)) {
3727 tranAssertEqual(t, tt.wantRes, gotRes)
3732 func TestHandleGetNewsArtNameList(t *testing.T) {
3740 wantRes []Transaction
3741 wantErr assert.ErrorAssertionFunc
3744 name: "when user does not have required permission",
3748 Access: func() accessBitmap {
3749 var bits accessBitmap
3754 Accounts: map[string]*Account{},
3758 TranGetNewsArtNameList, &[]byte{0, 1},
3761 wantRes: []Transaction{
3766 ErrorCode: []byte{0, 0, 0, 1},
3768 NewField(FieldError, []byte("You are not allowed to read news.")),
3772 wantErr: assert.NoError,
3775 for _, tt := range tests {
3776 t.Run(tt.name, func(t *testing.T) {
3777 gotRes, err := HandleGetNewsArtNameList(tt.args.cc, tt.args.t)
3778 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetNewsArtNameList(%v, %v)", tt.args.cc, tt.args.t)) {
3781 tranAssertEqual(t, tt.wantRes, gotRes)
3786 func TestHandleNewNewsFldr(t *testing.T) {
3794 wantRes []Transaction
3795 wantErr assert.ErrorAssertionFunc
3798 name: "when user does not have required permission",
3802 Access: func() accessBitmap {
3803 var bits accessBitmap
3808 Accounts: map[string]*Account{},
3812 TranGetNewsArtNameList, &[]byte{0, 1},
3815 wantRes: []Transaction{
3820 ErrorCode: []byte{0, 0, 0, 1},
3822 NewField(FieldError, []byte("You are not allowed to create news folders.")),
3826 wantErr: assert.NoError,
3829 name: "with a valid request",
3833 Access: func() accessBitmap {
3834 var bits accessBitmap
3835 bits.Set(accessNewsCreateFldr)
3839 logger: NewTestLogger(),
3842 ConfigDir: "/fakeConfigRoot",
3843 FS: func() *MockFileStore {
3844 mfs := &MockFileStore{}
3845 mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil)
3848 ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3854 SubCats: make(map[string]NewsCategoryListData15),
3860 TranGetNewsArtNameList, &[]byte{0, 1},
3861 NewField(FieldFileName, []byte("testFolder")),
3862 NewField(FieldNewsPath,
3867 0x74, 0x65, 0x73, 0x74,
3872 wantRes: []Transaction{
3874 clientID: &[]byte{0, 1},
3878 ID: []byte{0, 0, 0, 0},
3879 ErrorCode: []byte{0, 0, 0, 0},
3883 wantErr: assert.NoError,
3886 // name: "when there is an error writing the threaded news file",
3889 // Account: &Account{
3890 // Access: func() accessBitmap {
3891 // var bits accessBitmap
3892 // bits.Set(accessNewsCreateFldr)
3896 // logger: NewTestLogger(),
3897 // ID: &[]byte{0, 1},
3899 // ConfigDir: "/fakeConfigRoot",
3900 // FS: func() *MockFileStore {
3901 // mfs := &MockFileStore{}
3902 // mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(os.ErrNotExist)
3905 // ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
3907 // Type: []byte{0, 2},
3911 // SubCats: make(map[string]NewsCategoryListData15),
3916 // t: NewTransaction(
3917 // TranGetNewsArtNameList, &[]byte{0, 1},
3918 // NewField(FieldFileName, []byte("testFolder")),
3919 // NewField(FieldNewsPath,
3924 // 0x74, 0x65, 0x73, 0x74,
3929 // wantRes: []Transaction{
3931 // clientID: &[]byte{0, 1},
3934 // Type: []byte{0, 0},
3935 // ErrorCode: []byte{0, 0, 0, 1},
3937 // NewField(FieldError, []byte("Error creating news folder.")),
3941 // wantErr: assert.Error,
3944 for _, tt := range tests {
3945 t.Run(tt.name, func(t *testing.T) {
3946 gotRes, err := HandleNewNewsFldr(tt.args.cc, tt.args.t)
3947 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewNewsFldr(%v, %v)", tt.args.cc, tt.args.t)) {
3950 tranAssertEqual(t, tt.wantRes, gotRes)