6 "github.com/stretchr/testify/assert"
14 func TestHandleSetChatSubject(t *testing.T) {
26 name: "sends chat subject to private chat members",
29 UserName: []byte{0x00, 0x01},
31 PrivateChats: map[uint32]*PrivateChat{
34 ClientConn: map[uint16]*ClientConn{
37 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
43 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
50 Clients: map[uint16]*ClientConn{
53 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
59 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
69 Type: []byte{0, 0x6a},
70 ID: []byte{0, 0, 0, 1},
71 ErrorCode: []byte{0, 0, 0, 0},
73 NewField(fieldChatID, []byte{0, 0, 0, 1}),
74 NewField(fieldChatSubject, []byte("Test Subject")),
80 clientID: &[]byte{0, 1},
83 Type: []byte{0, 0x77},
84 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
85 ErrorCode: []byte{0, 0, 0, 0},
87 NewField(fieldChatID, []byte{0, 0, 0, 1}),
88 NewField(fieldChatSubject, []byte("Test Subject")),
92 clientID: &[]byte{0, 2},
95 Type: []byte{0, 0x77},
96 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
97 ErrorCode: []byte{0, 0, 0, 0},
99 NewField(fieldChatID, []byte{0, 0, 0, 1}),
100 NewField(fieldChatSubject, []byte("Test Subject")),
107 for _, tt := range tests {
108 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
110 t.Run(tt.name, func(t *testing.T) {
111 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
112 if (err != nil) != tt.wantErr {
113 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
116 if !assert.Equal(t, tt.want, got) {
117 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
123 func TestHandleLeaveChat(t *testing.T) {
135 name: "returns expected transactions",
140 PrivateChats: map[uint32]*PrivateChat{
142 ClientConn: map[uint16]*ClientConn{
145 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
151 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
158 Clients: map[uint16]*ClientConn{
161 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
167 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
174 t: NewTransaction(tranDeleteUser, nil, NewField(fieldChatID, []byte{0, 0, 0, 1})),
178 clientID: &[]byte{0, 1},
181 Type: []byte{0, 0x76},
182 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
183 ErrorCode: []byte{0, 0, 0, 0},
185 NewField(fieldChatID, []byte{0, 0, 0, 1}),
186 NewField(fieldUserID, []byte{0, 2}),
193 for _, tt := range tests {
195 t.Run(tt.name, func(t *testing.T) {
196 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
197 if (err != nil) != tt.wantErr {
198 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
201 if !assert.Equal(t, tt.want, got) {
202 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
208 func TestHandleGetUserNameList(t *testing.T) {
220 name: "replies with userlist transaction",
226 Clients: map[uint16]*ClientConn{
230 Flags: &[]byte{0, 3},
231 UserName: []byte{0, 4},
237 Flags: &[]byte{0, 3},
238 UserName: []byte{0, 4},
244 Flags: &[]byte{0, 3},
245 UserName: []byte{0, 4},
252 ID: []byte{0, 0, 0, 1},
258 clientID: &[]byte{1, 1},
262 ID: []byte{0, 0, 0, 1},
263 ErrorCode: []byte{0, 0, 0, 0},
266 fieldUsernameWithInfo,
267 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
270 fieldUsernameWithInfo,
271 []byte{00, 02, 00, 02, 00, 03, 00, 02, 00, 04},
279 for _, tt := range tests {
280 t.Run(tt.name, func(t *testing.T) {
281 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
282 if (err != nil) != tt.wantErr {
283 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
286 assert.Equal(t, tt.want, got)
291 func TestHandleChatSend(t *testing.T) {
303 name: "sends chat msg transaction to all clients",
307 Access: func() *[]byte {
308 var bits accessBitmap
309 bits.Set(accessSendChat)
314 UserName: []byte{0x00, 0x01},
316 Clients: map[uint16]*ClientConn{
319 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
325 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
334 NewField(fieldData, []byte("hai")),
340 clientID: &[]byte{0, 1},
343 Type: []byte{0, 0x6a},
344 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
345 ErrorCode: []byte{0, 0, 0, 0},
347 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
351 clientID: &[]byte{0, 2},
354 Type: []byte{0, 0x6a},
355 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
356 ErrorCode: []byte{0, 0, 0, 0},
358 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
365 name: "when user does not have required permission",
369 Access: func() *[]byte {
370 var bits accessBitmap
376 Accounts: map[string]*Account{},
380 tranChatSend, &[]byte{0, 1},
381 NewField(fieldData, []byte("hai")),
388 Type: []byte{0, 0x00},
389 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
390 ErrorCode: []byte{0, 0, 0, 1},
392 NewField(fieldError, []byte("You are not allowed to participate in chat.")),
399 name: "sends chat msg as emote if fieldChatOptions is set",
403 Access: func() *[]byte {
404 var bits accessBitmap
405 bits.Set(accessSendChat)
410 UserName: []byte("Testy McTest"),
412 Clients: map[uint16]*ClientConn{
415 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
421 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
430 NewField(fieldData, []byte("performed action")),
431 NewField(fieldChatOptions, []byte{0x00, 0x01}),
437 clientID: &[]byte{0, 1},
440 Type: []byte{0, 0x6a},
441 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
442 ErrorCode: []byte{0, 0, 0, 0},
444 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
448 clientID: &[]byte{0, 2},
451 Type: []byte{0, 0x6a},
452 ID: []byte{0xf0, 0xc5, 0x34, 0x1e},
453 ErrorCode: []byte{0, 0, 0, 0},
455 NewField(fieldData, []byte("\r*** Testy McTest performed action")),
462 name: "only sends chat msg to clients with accessReadChat permission",
466 Access: func() *[]byte {
467 var bits accessBitmap
468 bits.Set(accessSendChat)
473 UserName: []byte{0x00, 0x01},
475 Clients: map[uint16]*ClientConn{
478 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
484 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
493 NewField(fieldData, []byte("hai")),
499 clientID: &[]byte{0, 1},
502 Type: []byte{0, 0x6a},
503 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
504 ErrorCode: []byte{0, 0, 0, 0},
506 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
513 name: "only sends private chat msg to members of private chat",
517 Access: func() *[]byte {
518 var bits accessBitmap
519 bits.Set(accessSendChat)
524 UserName: []byte{0x00, 0x01},
526 PrivateChats: map[uint32]*PrivateChat{
528 ClientConn: map[uint16]*ClientConn{
538 Clients: map[uint16]*ClientConn{
541 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
547 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
553 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
562 NewField(fieldData, []byte("hai")),
563 NewField(fieldChatID, []byte{0, 0, 0, 1}),
569 clientID: &[]byte{0, 1},
572 Type: []byte{0, 0x6a},
573 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
574 ErrorCode: []byte{0, 0, 0, 0},
576 NewField(fieldChatID, []byte{0, 0, 0, 1}),
577 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
581 clientID: &[]byte{0, 2},
584 Type: []byte{0, 0x6a},
585 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
586 ErrorCode: []byte{0, 0, 0, 0},
588 NewField(fieldChatID, []byte{0, 0, 0, 1}),
589 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
596 for _, tt := range tests {
597 t.Run(tt.name, func(t *testing.T) {
598 got, err := HandleChatSend(tt.args.cc, tt.args.t)
600 if (err != nil) != tt.wantErr {
601 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
604 tranAssertEqual(t, tt.want, got)
609 func TestHandleGetFileInfo(t *testing.T) {
610 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
619 wantRes []Transaction
623 name: "returns expected fields when a valid file is requested",
626 ID: &[]byte{0x00, 0x01},
629 FileRoot: func() string {
630 path, _ := os.Getwd()
631 return path + "/test/config/Files"
637 tranGetFileInfo, nil,
638 NewField(fieldFileName, []byte("testfile.txt")),
639 NewField(fieldFilePath, []byte{0x00, 0x00}),
642 wantRes: []Transaction{
644 clientID: &[]byte{0, 1},
647 Type: []byte{0, 0xce},
648 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
649 ErrorCode: []byte{0, 0, 0, 0},
651 NewField(fieldFileName, []byte("testfile.txt")),
652 NewField(fieldFileTypeString, []byte("Text File")),
653 NewField(fieldFileCreatorString, []byte("ttxt")),
654 NewField(fieldFileComment, []byte{}),
655 NewField(fieldFileType, []byte("TEXT")),
656 NewField(fieldFileCreateDate, make([]byte, 8)),
657 NewField(fieldFileModifyDate, make([]byte, 8)),
658 NewField(fieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}),
665 for _, tt := range tests {
666 t.Run(tt.name, func(t *testing.T) {
667 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
669 gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t)
670 if (err != nil) != tt.wantErr {
671 t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr)
675 // Clear the file timestamp fields to work around problems running the tests in multiple timezones
676 // TODO: revisit how to test this by mocking the stat calls
677 gotRes[0].Fields[5].Data = make([]byte, 8)
678 gotRes[0].Fields[6].Data = make([]byte, 8)
679 if !assert.Equal(t, tt.wantRes, gotRes) {
680 t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes)
686 func TestHandleNewFolder(t *testing.T) {
695 wantRes []Transaction
699 name: "without required permission",
704 Access: func() *[]byte {
705 var bits accessBitmap
716 wantRes: []Transaction{
720 Type: []byte{0, 0x00},
721 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
722 ErrorCode: []byte{0, 0, 0, 1},
724 NewField(fieldError, []byte("You are not allowed to create folders.")),
731 name: "when path is nested",
735 Access: func() *[]byte {
736 var bits accessBitmap
737 bits.Set(accessCreateFolder)
750 tranNewFolder, &[]byte{0, 1},
751 NewField(fieldFileName, []byte("testFolder")),
752 NewField(fieldFilePath, []byte{
761 mfs := &MockFileStore{}
762 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
763 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
766 wantRes: []Transaction{
768 clientID: &[]byte{0, 1},
771 Type: []byte{0, 0xcd},
772 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
773 ErrorCode: []byte{0, 0, 0, 0},
779 name: "when path is not nested",
783 Access: func() *[]byte {
784 var bits accessBitmap
785 bits.Set(accessCreateFolder)
798 tranNewFolder, &[]byte{0, 1},
799 NewField(fieldFileName, []byte("testFolder")),
803 mfs := &MockFileStore{}
804 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
805 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
808 wantRes: []Transaction{
810 clientID: &[]byte{0, 1},
813 Type: []byte{0, 0xcd},
814 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
815 ErrorCode: []byte{0, 0, 0, 0},
821 name: "when UnmarshalBinary returns an err",
825 Access: func() *[]byte {
826 var bits accessBitmap
827 bits.Set(accessCreateFolder)
840 tranNewFolder, &[]byte{0, 1},
841 NewField(fieldFileName, []byte("testFolder")),
842 NewField(fieldFilePath, []byte{
848 mfs := &MockFileStore{}
849 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
850 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
853 wantRes: []Transaction{},
857 name: "fieldFileName does not allow directory traversal",
861 Access: func() *[]byte {
862 var bits accessBitmap
863 bits.Set(accessCreateFolder)
876 tranNewFolder, &[]byte{0, 1},
877 NewField(fieldFileName, []byte("../../testFolder")),
881 mfs := &MockFileStore{}
882 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
883 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
886 wantRes: []Transaction{
888 clientID: &[]byte{0, 1},
891 Type: []byte{0, 0xcd},
892 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
893 ErrorCode: []byte{0, 0, 0, 0},
898 name: "fieldFilePath does not allow directory traversal",
902 Access: func() *[]byte {
903 var bits accessBitmap
904 bits.Set(accessCreateFolder)
917 tranNewFolder, &[]byte{0, 1},
918 NewField(fieldFileName, []byte("testFolder")),
919 NewField(fieldFilePath, []byte{
931 mfs := &MockFileStore{}
932 mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
933 mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
936 wantRes: []Transaction{
938 clientID: &[]byte{0, 1},
941 Type: []byte{0, 0xcd},
942 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
943 ErrorCode: []byte{0, 0, 0, 0},
948 for _, tt := range tests {
949 t.Run(tt.name, func(t *testing.T) {
952 gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
953 if (err != nil) != tt.wantErr {
954 t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
958 if !tranAssertEqual(t, tt.wantRes, gotRes) {
959 t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
965 func TestHandleUploadFile(t *testing.T) {
973 wantRes []Transaction
977 name: "when request is valid and user has Upload Anywhere permission",
981 FileTransfers: map[uint32]*FileTransfer{},
984 Access: func() *[]byte {
985 var bits accessBitmap
986 bits.Set(accessUploadFile)
987 bits.Set(accessUploadAnywhere)
994 tranUploadFile, &[]byte{0, 1},
995 NewField(fieldFileName, []byte("testFile")),
996 NewField(fieldFilePath, []byte{
1004 wantRes: []Transaction{
1008 Type: []byte{0, 0xcb},
1009 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1010 ErrorCode: []byte{0, 0, 0, 0},
1012 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), // rand.Seed(1)
1019 name: "when user does not have required access",
1023 Access: func() *[]byte {
1024 var bits accessBitmap
1030 FileTransfers: map[uint32]*FileTransfer{},
1034 tranUploadFile, &[]byte{0, 1},
1035 NewField(fieldFileName, []byte("testFile")),
1036 NewField(fieldFilePath, []byte{
1044 wantRes: []Transaction{
1048 Type: []byte{0, 0x00},
1049 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1050 ErrorCode: []byte{0, 0, 0, 1},
1052 NewField(fieldError, []byte("You are not allowed to upload files.")), // rand.Seed(1)
1059 for _, tt := range tests {
1060 t.Run(tt.name, func(t *testing.T) {
1062 gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t)
1063 if (err != nil) != tt.wantErr {
1064 t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr)
1068 tranAssertEqual(t, tt.wantRes, gotRes)
1074 func TestHandleMakeAlias(t *testing.T) {
1083 wantRes []Transaction
1087 name: "with valid input and required permissions",
1089 mfs := &MockFileStore{}
1090 path, _ := os.Getwd()
1093 path+"/test/config/Files/foo/testFile",
1094 path+"/test/config/Files/bar/testFile",
1101 Access: func() *[]byte {
1102 var bits accessBitmap
1103 bits.Set(accessMakeAlias)
1110 FileRoot: func() string {
1111 path, _ := os.Getwd()
1112 return path + "/test/config/Files"
1115 Logger: NewTestLogger(),
1119 tranMakeFileAlias, &[]byte{0, 1},
1120 NewField(fieldFileName, []byte("testFile")),
1121 NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1122 NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1125 wantRes: []Transaction{
1129 Type: []byte{0, 0xd1},
1130 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1131 ErrorCode: []byte{0, 0, 0, 0},
1132 Fields: []Field(nil),
1138 name: "when symlink returns an error",
1140 mfs := &MockFileStore{}
1141 path, _ := os.Getwd()
1144 path+"/test/config/Files/foo/testFile",
1145 path+"/test/config/Files/bar/testFile",
1146 ).Return(errors.New("ohno"))
1152 Access: func() *[]byte {
1153 var bits accessBitmap
1154 bits.Set(accessMakeAlias)
1161 FileRoot: func() string {
1162 path, _ := os.Getwd()
1163 return path + "/test/config/Files"
1166 Logger: NewTestLogger(),
1170 tranMakeFileAlias, &[]byte{0, 1},
1171 NewField(fieldFileName, []byte("testFile")),
1172 NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1173 NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1176 wantRes: []Transaction{
1180 Type: []byte{0, 0x00},
1181 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1182 ErrorCode: []byte{0, 0, 0, 1},
1184 NewField(fieldError, []byte("Error creating alias")),
1191 name: "when user does not have required permission",
1196 Access: func() *[]byte {
1197 var bits accessBitmap
1204 FileRoot: func() string {
1205 path, _ := os.Getwd()
1206 return path + "/test/config/Files"
1212 tranMakeFileAlias, &[]byte{0, 1},
1213 NewField(fieldFileName, []byte("testFile")),
1214 NewField(fieldFilePath, []byte{
1220 NewField(fieldFileNewPath, []byte{
1228 wantRes: []Transaction{
1232 Type: []byte{0, 0x00},
1233 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1234 ErrorCode: []byte{0, 0, 0, 1},
1236 NewField(fieldError, []byte("You are not allowed to make aliases.")),
1243 for _, tt := range tests {
1244 t.Run(tt.name, func(t *testing.T) {
1247 gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
1248 if (err != nil) != tt.wantErr {
1249 t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
1253 tranAssertEqual(t, tt.wantRes, gotRes)
1258 func TestHandleGetUser(t *testing.T) {
1266 wantRes []Transaction
1267 wantErr assert.ErrorAssertionFunc
1270 name: "when account is valid",
1274 Access: func() *[]byte {
1275 var bits accessBitmap
1276 bits.Set(accessOpenUser)
1282 Accounts: map[string]*Account{
1286 Password: "password",
1293 tranGetUser, &[]byte{0, 1},
1294 NewField(fieldUserLogin, []byte("guest")),
1297 wantRes: []Transaction{
1301 Type: []byte{0x01, 0x60},
1302 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1303 ErrorCode: []byte{0, 0, 0, 0},
1305 NewField(fieldUserName, []byte("Guest")),
1306 NewField(fieldUserLogin, negateString([]byte("guest"))),
1307 NewField(fieldUserPassword, []byte("password")),
1308 NewField(fieldUserAccess, []byte{1}),
1312 wantErr: assert.NoError,
1315 name: "when user does not have required permission",
1319 Access: func() *[]byte {
1320 var bits accessBitmap
1326 Accounts: map[string]*Account{},
1330 tranGetUser, &[]byte{0, 1},
1331 NewField(fieldUserLogin, []byte("nonExistentUser")),
1334 wantRes: []Transaction{
1338 Type: []byte{0, 0x00},
1339 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1340 ErrorCode: []byte{0, 0, 0, 1},
1342 NewField(fieldError, []byte("You are not allowed to view accounts.")),
1346 wantErr: assert.NoError,
1349 name: "when account does not exist",
1353 Access: func() *[]byte {
1354 var bits accessBitmap
1355 bits.Set(accessOpenUser)
1361 Accounts: map[string]*Account{},
1365 tranGetUser, &[]byte{0, 1},
1366 NewField(fieldUserLogin, []byte("nonExistentUser")),
1369 wantRes: []Transaction{
1373 Type: []byte{0, 0x00},
1374 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1375 ErrorCode: []byte{0, 0, 0, 1},
1377 NewField(fieldError, []byte("Account does not exist.")),
1381 wantErr: assert.NoError,
1384 for _, tt := range tests {
1385 t.Run(tt.name, func(t *testing.T) {
1386 gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
1387 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
1391 tranAssertEqual(t, tt.wantRes, gotRes)
1396 func TestHandleDeleteUser(t *testing.T) {
1405 wantRes []Transaction
1406 wantErr assert.ErrorAssertionFunc
1409 name: "when user exists",
1411 mfs := &MockFileStore{}
1412 mfs.On("Remove", "Users/testuser.yaml").Return(nil)
1418 Access: func() *[]byte {
1419 var bits accessBitmap
1420 bits.Set(accessDeleteUser)
1426 Accounts: map[string]*Account{
1429 Name: "Testy McTest",
1430 Password: "password",
1437 tranDeleteUser, &[]byte{0, 1},
1438 NewField(fieldUserLogin, negateString([]byte("testuser"))),
1441 wantRes: []Transaction{
1445 Type: []byte{0x1, 0x5f},
1446 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1447 ErrorCode: []byte{0, 0, 0, 0},
1448 Fields: []Field(nil),
1451 wantErr: assert.NoError,
1454 name: "when user does not have required permission",
1459 Access: func() *[]byte {
1460 var bits accessBitmap
1466 Accounts: map[string]*Account{},
1470 tranDeleteUser, &[]byte{0, 1},
1471 NewField(fieldUserLogin, negateString([]byte("testuser"))),
1474 wantRes: []Transaction{
1478 Type: []byte{0, 0x00},
1479 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1480 ErrorCode: []byte{0, 0, 0, 1},
1482 NewField(fieldError, []byte("You are not allowed to delete accounts.")),
1486 wantErr: assert.NoError,
1489 for _, tt := range tests {
1490 t.Run(tt.name, func(t *testing.T) {
1492 gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
1493 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
1497 tranAssertEqual(t, tt.wantRes, gotRes)
1502 func TestHandleGetMsgs(t *testing.T) {
1510 wantRes []Transaction
1511 wantErr assert.ErrorAssertionFunc
1514 name: "returns news data",
1518 Access: func() *[]byte {
1519 var bits accessBitmap
1520 bits.Set(accessNewsReadArt)
1526 FlatNews: []byte("TEST"),
1530 tranGetMsgs, &[]byte{0, 1},
1533 wantRes: []Transaction{
1537 Type: []byte{0, 0x65},
1538 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1539 ErrorCode: []byte{0, 0, 0, 0},
1541 NewField(fieldData, []byte("TEST")),
1545 wantErr: assert.NoError,
1548 name: "when user does not have required permission",
1552 Access: func() *[]byte {
1553 var bits accessBitmap
1559 Accounts: map[string]*Account{},
1563 tranGetMsgs, &[]byte{0, 1},
1566 wantRes: []Transaction{
1570 Type: []byte{0, 0x00},
1571 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1572 ErrorCode: []byte{0, 0, 0, 1},
1574 NewField(fieldError, []byte("You are not allowed to read news.")),
1578 wantErr: assert.NoError,
1581 for _, tt := range tests {
1582 t.Run(tt.name, func(t *testing.T) {
1583 gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t)
1584 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) {
1588 tranAssertEqual(t, tt.wantRes, gotRes)
1593 func TestHandleNewUser(t *testing.T) {
1601 wantRes []Transaction
1602 wantErr assert.ErrorAssertionFunc
1605 name: "when user does not have required permission",
1609 Access: func() *[]byte {
1610 var bits accessBitmap
1616 Accounts: map[string]*Account{},
1620 tranNewUser, &[]byte{0, 1},
1623 wantRes: []Transaction{
1627 Type: []byte{0, 0x00},
1628 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1629 ErrorCode: []byte{0, 0, 0, 1},
1631 NewField(fieldError, []byte("You are not allowed to create new accounts.")),
1635 wantErr: assert.NoError,
1638 for _, tt := range tests {
1639 t.Run(tt.name, func(t *testing.T) {
1640 gotRes, err := HandleNewUser(tt.args.cc, tt.args.t)
1641 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) {
1645 tranAssertEqual(t, tt.wantRes, gotRes)
1650 func TestHandleListUsers(t *testing.T) {
1658 wantRes []Transaction
1659 wantErr assert.ErrorAssertionFunc
1662 name: "when user does not have required permission",
1666 Access: func() *[]byte {
1667 var bits accessBitmap
1673 Accounts: map[string]*Account{},
1677 tranNewUser, &[]byte{0, 1},
1680 wantRes: []Transaction{
1684 Type: []byte{0, 0x00},
1685 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1686 ErrorCode: []byte{0, 0, 0, 1},
1688 NewField(fieldError, []byte("You are not allowed to view accounts.")),
1692 wantErr: assert.NoError,
1695 for _, tt := range tests {
1696 t.Run(tt.name, func(t *testing.T) {
1697 gotRes, err := HandleListUsers(tt.args.cc, tt.args.t)
1698 if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) {
1702 tranAssertEqual(t, tt.wantRes, gotRes)
1707 func TestHandleDownloadFile(t *testing.T) {
1715 wantRes []Transaction
1716 wantErr assert.ErrorAssertionFunc
1719 name: "when user does not have required permission",
1723 Access: func() *[]byte {
1724 var bits accessBitmap
1731 t: NewTransaction(tranDownloadFile, &[]byte{0, 1}),
1733 wantRes: []Transaction{
1737 Type: []byte{0, 0x00},
1738 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1739 ErrorCode: []byte{0, 0, 0, 1},
1741 NewField(fieldError, []byte("You are not allowed to download files.")),
1745 wantErr: assert.NoError,
1748 name: "with a valid file",
1751 Transfers: make(map[int][]*FileTransfer),
1753 Access: func() *[]byte {
1754 var bits accessBitmap
1755 bits.Set(accessDownloadFile)
1761 FileTransfers: make(map[uint32]*FileTransfer),
1763 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1765 Accounts: map[string]*Account{},
1771 NewField(fieldFileName, []byte("testfile.txt")),
1772 NewField(fieldFilePath, []byte{0x0, 0x00}),
1775 wantRes: []Transaction{
1779 Type: []byte{0, 0x2},
1780 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1781 ErrorCode: []byte{0, 0, 0, 0},
1783 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1784 NewField(fieldWaitingCount, []byte{0x00, 0x00}),
1785 NewField(fieldTransferSize, []byte{0x00, 0x00, 0x00, 0xa5}),
1786 NewField(fieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}),
1790 wantErr: assert.NoError,
1793 for _, tt := range tests {
1794 t.Run(tt.name, func(t *testing.T) {
1795 // reset the rand seed so that the random fieldRefNum will be deterministic
1798 gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t)
1799 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) {
1803 tranAssertEqual(t, tt.wantRes, gotRes)
1808 func TestHandleUpdateUser(t *testing.T) {
1816 wantRes []Transaction
1817 wantErr assert.ErrorAssertionFunc
1820 name: "when action is create user without required permission",
1824 Logger: NewTestLogger(),
1827 Access: func() *[]byte {
1828 var bits accessBitmap
1837 NewField(fieldData, []byte{
1838 0x00, 0x04, // field count
1840 0x00, 0x69, // fieldUserLogin = 105
1844 0x00, 0x6a, // fieldUserPassword = 106
1848 0x00, 0x66, // fieldUserName = 102
1852 0x00, 0x6e, // fieldUserAccess = 110
1854 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
1858 wantRes: []Transaction{
1862 Type: []byte{0, 0x00},
1863 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1864 ErrorCode: []byte{0, 0, 0, 1},
1866 NewField(fieldError, []byte("You are not allowed to create new accounts.")),
1870 wantErr: assert.NoError,
1873 name: "when action is modify user without required permission",
1877 Logger: NewTestLogger(),
1878 Accounts: map[string]*Account{
1883 Access: func() *[]byte {
1884 var bits accessBitmap
1893 NewField(fieldData, []byte{
1894 0x00, 0x04, // field count
1896 0x00, 0x69, // fieldUserLogin = 105
1900 0x00, 0x6a, // fieldUserPassword = 106
1904 0x00, 0x66, // fieldUserName = 102
1908 0x00, 0x6e, // fieldUserAccess = 110
1910 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
1914 wantRes: []Transaction{
1918 Type: []byte{0, 0x00},
1919 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1920 ErrorCode: []byte{0, 0, 0, 1},
1922 NewField(fieldError, []byte("You are not allowed to modify accounts.")),
1926 wantErr: assert.NoError,
1929 name: "when action is delete user without required permission",
1933 Logger: NewTestLogger(),
1934 Accounts: map[string]*Account{
1939 Access: func() *[]byte {
1940 var bits accessBitmap
1949 NewField(fieldData, []byte{
1957 wantRes: []Transaction{
1961 Type: []byte{0, 0x00},
1962 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1963 ErrorCode: []byte{0, 0, 0, 1},
1965 NewField(fieldError, []byte("You are not allowed to delete accounts.")),
1969 wantErr: assert.NoError,
1972 for _, tt := range tests {
1973 t.Run(tt.name, func(t *testing.T) {
1974 gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t)
1975 if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) {
1979 tranAssertEqual(t, tt.wantRes, gotRes)
1984 func TestHandleDelNewsArt(t *testing.T) {
1992 wantRes []Transaction
1993 wantErr assert.ErrorAssertionFunc
1996 name: "without required permission",
2000 Access: func() *[]byte {
2001 var bits accessBitmap
2012 wantRes: []Transaction{
2016 Type: []byte{0, 0x00},
2017 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2018 ErrorCode: []byte{0, 0, 0, 1},
2020 NewField(fieldError, []byte("You are not allowed to delete news articles.")),
2024 wantErr: assert.NoError,
2027 for _, tt := range tests {
2028 t.Run(tt.name, func(t *testing.T) {
2029 gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
2030 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
2033 tranAssertEqual(t, tt.wantRes, gotRes)
2038 func TestHandleDisconnectUser(t *testing.T) {
2046 wantRes []Transaction
2047 wantErr assert.ErrorAssertionFunc
2050 name: "without required permission",
2054 Access: func() *[]byte {
2055 var bits accessBitmap
2066 wantRes: []Transaction{
2070 Type: []byte{0, 0x00},
2071 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2072 ErrorCode: []byte{0, 0, 0, 1},
2074 NewField(fieldError, []byte("You are not allowed to disconnect users.")),
2078 wantErr: assert.NoError,
2081 name: "when target user has 'cannot be disconnected' priv",
2085 Clients: map[uint16]*ClientConn{
2089 Access: func() *[]byte {
2090 var bits accessBitmap
2091 bits.Set(accessCannotBeDiscon)
2100 Access: func() *[]byte {
2101 var bits accessBitmap
2102 bits.Set(accessDisconUser)
2111 NewField(fieldUserID, []byte{0, 1}),
2114 wantRes: []Transaction{
2118 Type: []byte{0, 0x00},
2119 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2120 ErrorCode: []byte{0, 0, 0, 1},
2122 NewField(fieldError, []byte("unnamed is not allowed to be disconnected.")),
2126 wantErr: assert.NoError,
2129 for _, tt := range tests {
2130 t.Run(tt.name, func(t *testing.T) {
2131 gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
2132 if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
2135 tranAssertEqual(t, tt.wantRes, gotRes)