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) {
694 wantRes []Transaction
698 name: "without required permission",
702 Access: func() *[]byte {
703 var bits accessBitmap
714 wantRes: []Transaction{
718 Type: []byte{0, 0x00},
719 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
720 ErrorCode: []byte{0, 0, 0, 1},
722 NewField(fieldError, []byte("You are not allowed to create folders.")),
729 name: "when path is nested",
733 Access: func() *[]byte {
734 var bits accessBitmap
735 bits.Set(accessCreateFolder)
745 FS: func() *MockFileStore {
746 mfs := &MockFileStore{}
747 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
748 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
754 tranNewFolder, &[]byte{0, 1},
755 NewField(fieldFileName, []byte("testFolder")),
756 NewField(fieldFilePath, []byte{
764 wantRes: []Transaction{
766 clientID: &[]byte{0, 1},
769 Type: []byte{0, 0xcd},
770 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
771 ErrorCode: []byte{0, 0, 0, 0},
777 name: "when path is not nested",
781 Access: func() *[]byte {
782 var bits accessBitmap
783 bits.Set(accessCreateFolder)
793 FS: func() *MockFileStore {
794 mfs := &MockFileStore{}
795 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
796 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
802 tranNewFolder, &[]byte{0, 1},
803 NewField(fieldFileName, []byte("testFolder")),
806 wantRes: []Transaction{
808 clientID: &[]byte{0, 1},
811 Type: []byte{0, 0xcd},
812 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
813 ErrorCode: []byte{0, 0, 0, 0},
819 name: "when UnmarshalBinary returns an err",
823 Access: func() *[]byte {
824 var bits accessBitmap
825 bits.Set(accessCreateFolder)
835 FS: func() *MockFileStore {
836 mfs := &MockFileStore{}
837 mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil)
838 mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist)
844 tranNewFolder, &[]byte{0, 1},
845 NewField(fieldFileName, []byte("testFolder")),
846 NewField(fieldFilePath, []byte{
851 wantRes: []Transaction{},
855 name: "fieldFileName does not allow directory traversal",
859 Access: func() *[]byte {
860 var bits accessBitmap
861 bits.Set(accessCreateFolder)
871 FS: func() *MockFileStore {
872 mfs := &MockFileStore{}
873 mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil)
874 mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist)
880 tranNewFolder, &[]byte{0, 1},
881 NewField(fieldFileName, []byte("../../testFolder")),
884 wantRes: []Transaction{
886 clientID: &[]byte{0, 1},
889 Type: []byte{0, 0xcd},
890 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
891 ErrorCode: []byte{0, 0, 0, 0},
896 name: "fieldFilePath does not allow directory traversal",
900 Access: func() *[]byte {
901 var bits accessBitmap
902 bits.Set(accessCreateFolder)
912 FS: func() *MockFileStore {
913 mfs := &MockFileStore{}
914 mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil)
915 mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist)
921 tranNewFolder, &[]byte{0, 1},
922 NewField(fieldFileName, []byte("testFolder")),
923 NewField(fieldFilePath, []byte{
934 wantRes: []Transaction{
936 clientID: &[]byte{0, 1},
939 Type: []byte{0, 0xcd},
940 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
941 ErrorCode: []byte{0, 0, 0, 0},
946 for _, tt := range tests {
947 t.Run(tt.name, func(t *testing.T) {
949 gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t)
950 if (err != nil) != tt.wantErr {
951 t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr)
955 if !tranAssertEqual(t, tt.wantRes, gotRes) {
956 t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes)
962 func TestHandleUploadFile(t *testing.T) {
970 wantRes []Transaction
974 name: "when request is valid and user has Upload Anywhere permission",
978 FileTransfers: map[uint32]*FileTransfer{},
981 Access: func() *[]byte {
982 var bits accessBitmap
983 bits.Set(accessUploadFile)
984 bits.Set(accessUploadAnywhere)
991 tranUploadFile, &[]byte{0, 1},
992 NewField(fieldFileName, []byte("testFile")),
993 NewField(fieldFilePath, []byte{
1001 wantRes: []Transaction{
1005 Type: []byte{0, 0xcb},
1006 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1007 ErrorCode: []byte{0, 0, 0, 0},
1009 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), // rand.Seed(1)
1016 name: "when user does not have required access",
1020 Access: func() *[]byte {
1021 var bits accessBitmap
1027 FileTransfers: map[uint32]*FileTransfer{},
1031 tranUploadFile, &[]byte{0, 1},
1032 NewField(fieldFileName, []byte("testFile")),
1033 NewField(fieldFilePath, []byte{
1041 wantRes: []Transaction{
1045 Type: []byte{0, 0x00},
1046 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1047 ErrorCode: []byte{0, 0, 0, 1},
1049 NewField(fieldError, []byte("You are not allowed to upload files.")), // rand.Seed(1)
1056 for _, tt := range tests {
1057 t.Run(tt.name, func(t *testing.T) {
1059 gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t)
1060 if (err != nil) != tt.wantErr {
1061 t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr)
1065 tranAssertEqual(t, tt.wantRes, gotRes)
1071 func TestHandleMakeAlias(t *testing.T) {
1079 wantRes []Transaction
1083 name: "with valid input and required permissions",
1087 Access: func() *[]byte {
1088 var bits accessBitmap
1089 bits.Set(accessMakeAlias)
1096 FileRoot: func() string {
1097 path, _ := os.Getwd()
1098 return path + "/test/config/Files"
1101 Logger: NewTestLogger(),
1102 FS: func() *MockFileStore {
1103 mfs := &MockFileStore{}
1104 path, _ := os.Getwd()
1107 path+"/test/config/Files/foo/testFile",
1108 path+"/test/config/Files/bar/testFile",
1115 tranMakeFileAlias, &[]byte{0, 1},
1116 NewField(fieldFileName, []byte("testFile")),
1117 NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1118 NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1121 wantRes: []Transaction{
1125 Type: []byte{0, 0xd1},
1126 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1127 ErrorCode: []byte{0, 0, 0, 0},
1128 Fields: []Field(nil),
1134 name: "when symlink returns an error",
1138 Access: func() *[]byte {
1139 var bits accessBitmap
1140 bits.Set(accessMakeAlias)
1147 FileRoot: func() string {
1148 path, _ := os.Getwd()
1149 return path + "/test/config/Files"
1152 Logger: NewTestLogger(),
1153 FS: func() *MockFileStore {
1154 mfs := &MockFileStore{}
1155 path, _ := os.Getwd()
1158 path+"/test/config/Files/foo/testFile",
1159 path+"/test/config/Files/bar/testFile",
1160 ).Return(errors.New("ohno"))
1166 tranMakeFileAlias, &[]byte{0, 1},
1167 NewField(fieldFileName, []byte("testFile")),
1168 NewField(fieldFilePath, EncodeFilePath(strings.Join([]string{"foo"}, "/"))),
1169 NewField(fieldFileNewPath, EncodeFilePath(strings.Join([]string{"bar"}, "/"))),
1172 wantRes: []Transaction{
1176 Type: []byte{0, 0x00},
1177 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1178 ErrorCode: []byte{0, 0, 0, 1},
1180 NewField(fieldError, []byte("Error creating alias")),
1187 name: "when user does not have required permission",
1191 Access: func() *[]byte {
1192 var bits accessBitmap
1199 FileRoot: func() string {
1200 path, _ := os.Getwd()
1201 return path + "/test/config/Files"
1207 tranMakeFileAlias, &[]byte{0, 1},
1208 NewField(fieldFileName, []byte("testFile")),
1209 NewField(fieldFilePath, []byte{
1215 NewField(fieldFileNewPath, []byte{
1223 wantRes: []Transaction{
1227 Type: []byte{0, 0x00},
1228 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1229 ErrorCode: []byte{0, 0, 0, 1},
1231 NewField(fieldError, []byte("You are not allowed to make aliases.")),
1238 for _, tt := range tests {
1239 t.Run(tt.name, func(t *testing.T) {
1240 gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t)
1241 if (err != nil) != tt.wantErr {
1242 t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t)
1246 tranAssertEqual(t, tt.wantRes, gotRes)
1251 func TestHandleGetUser(t *testing.T) {
1259 wantRes []Transaction
1260 wantErr assert.ErrorAssertionFunc
1263 name: "when account is valid",
1267 Access: func() *[]byte {
1268 var bits accessBitmap
1269 bits.Set(accessOpenUser)
1275 Accounts: map[string]*Account{
1279 Password: "password",
1286 tranGetUser, &[]byte{0, 1},
1287 NewField(fieldUserLogin, []byte("guest")),
1290 wantRes: []Transaction{
1294 Type: []byte{0x01, 0x60},
1295 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1296 ErrorCode: []byte{0, 0, 0, 0},
1298 NewField(fieldUserName, []byte("Guest")),
1299 NewField(fieldUserLogin, negateString([]byte("guest"))),
1300 NewField(fieldUserPassword, []byte("password")),
1301 NewField(fieldUserAccess, []byte{1}),
1305 wantErr: assert.NoError,
1308 name: "when user does not have required permission",
1312 Access: func() *[]byte {
1313 var bits accessBitmap
1319 Accounts: map[string]*Account{},
1323 tranGetUser, &[]byte{0, 1},
1324 NewField(fieldUserLogin, []byte("nonExistentUser")),
1327 wantRes: []Transaction{
1331 Type: []byte{0, 0x00},
1332 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1333 ErrorCode: []byte{0, 0, 0, 1},
1335 NewField(fieldError, []byte("You are not allowed to view accounts.")),
1339 wantErr: assert.NoError,
1342 name: "when account does not exist",
1346 Access: func() *[]byte {
1347 var bits accessBitmap
1348 bits.Set(accessOpenUser)
1354 Accounts: map[string]*Account{},
1358 tranGetUser, &[]byte{0, 1},
1359 NewField(fieldUserLogin, []byte("nonExistentUser")),
1362 wantRes: []Transaction{
1366 Type: []byte{0, 0x00},
1367 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1368 ErrorCode: []byte{0, 0, 0, 1},
1370 NewField(fieldError, []byte("Account does not exist.")),
1374 wantErr: assert.NoError,
1377 for _, tt := range tests {
1378 t.Run(tt.name, func(t *testing.T) {
1379 gotRes, err := HandleGetUser(tt.args.cc, tt.args.t)
1380 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetUser(%v, %v)", tt.args.cc, tt.args.t)) {
1384 tranAssertEqual(t, tt.wantRes, gotRes)
1389 func TestHandleDeleteUser(t *testing.T) {
1397 wantRes []Transaction
1398 wantErr assert.ErrorAssertionFunc
1401 name: "when user exists",
1405 Access: func() *[]byte {
1406 var bits accessBitmap
1407 bits.Set(accessDeleteUser)
1413 Accounts: map[string]*Account{
1416 Name: "Testy McTest",
1417 Password: "password",
1421 FS: func() *MockFileStore {
1422 mfs := &MockFileStore{}
1423 mfs.On("Remove", "Users/testuser.yaml").Return(nil)
1429 tranDeleteUser, &[]byte{0, 1},
1430 NewField(fieldUserLogin, negateString([]byte("testuser"))),
1433 wantRes: []Transaction{
1437 Type: []byte{0x1, 0x5f},
1438 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1439 ErrorCode: []byte{0, 0, 0, 0},
1440 Fields: []Field(nil),
1443 wantErr: assert.NoError,
1446 name: "when user does not have required permission",
1450 Access: func() *[]byte {
1451 var bits accessBitmap
1457 Accounts: map[string]*Account{},
1461 tranDeleteUser, &[]byte{0, 1},
1462 NewField(fieldUserLogin, negateString([]byte("testuser"))),
1465 wantRes: []Transaction{
1469 Type: []byte{0, 0x00},
1470 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1471 ErrorCode: []byte{0, 0, 0, 1},
1473 NewField(fieldError, []byte("You are not allowed to delete accounts.")),
1477 wantErr: assert.NoError,
1480 for _, tt := range tests {
1481 t.Run(tt.name, func(t *testing.T) {
1482 gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t)
1483 if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) {
1487 tranAssertEqual(t, tt.wantRes, gotRes)
1492 func TestHandleGetMsgs(t *testing.T) {
1500 wantRes []Transaction
1501 wantErr assert.ErrorAssertionFunc
1504 name: "returns news data",
1508 Access: func() *[]byte {
1509 var bits accessBitmap
1510 bits.Set(accessNewsReadArt)
1516 FlatNews: []byte("TEST"),
1520 tranGetMsgs, &[]byte{0, 1},
1523 wantRes: []Transaction{
1527 Type: []byte{0, 0x65},
1528 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1529 ErrorCode: []byte{0, 0, 0, 0},
1531 NewField(fieldData, []byte("TEST")),
1535 wantErr: assert.NoError,
1538 name: "when user does not have required permission",
1542 Access: func() *[]byte {
1543 var bits accessBitmap
1549 Accounts: map[string]*Account{},
1553 tranGetMsgs, &[]byte{0, 1},
1556 wantRes: []Transaction{
1560 Type: []byte{0, 0x00},
1561 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1562 ErrorCode: []byte{0, 0, 0, 1},
1564 NewField(fieldError, []byte("You are not allowed to read news.")),
1568 wantErr: assert.NoError,
1571 for _, tt := range tests {
1572 t.Run(tt.name, func(t *testing.T) {
1573 gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t)
1574 if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) {
1578 tranAssertEqual(t, tt.wantRes, gotRes)
1583 func TestHandleNewUser(t *testing.T) {
1591 wantRes []Transaction
1592 wantErr assert.ErrorAssertionFunc
1595 name: "when user does not have required permission",
1599 Access: func() *[]byte {
1600 var bits accessBitmap
1606 Accounts: map[string]*Account{},
1610 tranNewUser, &[]byte{0, 1},
1613 wantRes: []Transaction{
1617 Type: []byte{0, 0x00},
1618 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1619 ErrorCode: []byte{0, 0, 0, 1},
1621 NewField(fieldError, []byte("You are not allowed to create new accounts.")),
1625 wantErr: assert.NoError,
1628 for _, tt := range tests {
1629 t.Run(tt.name, func(t *testing.T) {
1630 gotRes, err := HandleNewUser(tt.args.cc, tt.args.t)
1631 if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) {
1635 tranAssertEqual(t, tt.wantRes, gotRes)
1640 func TestHandleListUsers(t *testing.T) {
1648 wantRes []Transaction
1649 wantErr assert.ErrorAssertionFunc
1652 name: "when user does not have required permission",
1656 Access: func() *[]byte {
1657 var bits accessBitmap
1663 Accounts: map[string]*Account{},
1667 tranNewUser, &[]byte{0, 1},
1670 wantRes: []Transaction{
1674 Type: []byte{0, 0x00},
1675 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1676 ErrorCode: []byte{0, 0, 0, 1},
1678 NewField(fieldError, []byte("You are not allowed to view accounts.")),
1682 wantErr: assert.NoError,
1685 for _, tt := range tests {
1686 t.Run(tt.name, func(t *testing.T) {
1687 gotRes, err := HandleListUsers(tt.args.cc, tt.args.t)
1688 if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) {
1692 tranAssertEqual(t, tt.wantRes, gotRes)
1697 func TestHandleDownloadFile(t *testing.T) {
1705 wantRes []Transaction
1706 wantErr assert.ErrorAssertionFunc
1709 name: "when user does not have required permission",
1713 Access: func() *[]byte {
1714 var bits accessBitmap
1721 t: NewTransaction(tranDownloadFile, &[]byte{0, 1}),
1723 wantRes: []Transaction{
1727 Type: []byte{0, 0x00},
1728 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1729 ErrorCode: []byte{0, 0, 0, 1},
1731 NewField(fieldError, []byte("You are not allowed to download files.")),
1735 wantErr: assert.NoError,
1738 name: "with a valid file",
1741 Transfers: make(map[int][]*FileTransfer),
1743 Access: func() *[]byte {
1744 var bits accessBitmap
1745 bits.Set(accessDownloadFile)
1751 FileTransfers: make(map[uint32]*FileTransfer),
1753 FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
1755 Accounts: map[string]*Account{},
1761 NewField(fieldFileName, []byte("testfile.txt")),
1762 NewField(fieldFilePath, []byte{0x0, 0x00}),
1765 wantRes: []Transaction{
1769 Type: []byte{0, 0x2},
1770 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1771 ErrorCode: []byte{0, 0, 0, 0},
1773 NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}),
1774 NewField(fieldWaitingCount, []byte{0x00, 0x00}),
1775 NewField(fieldTransferSize, []byte{0x00, 0x00, 0x00, 0xa5}),
1776 NewField(fieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}),
1780 wantErr: assert.NoError,
1783 for _, tt := range tests {
1784 t.Run(tt.name, func(t *testing.T) {
1785 // reset the rand seed so that the random fieldRefNum will be deterministic
1788 gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t)
1789 if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) {
1793 tranAssertEqual(t, tt.wantRes, gotRes)
1798 func TestHandleUpdateUser(t *testing.T) {
1806 wantRes []Transaction
1807 wantErr assert.ErrorAssertionFunc
1810 name: "when action is create user without required permission",
1814 Logger: NewTestLogger(),
1817 Access: func() *[]byte {
1818 var bits accessBitmap
1827 NewField(fieldData, []byte{
1828 0x00, 0x04, // field count
1830 0x00, 0x69, // fieldUserLogin = 105
1834 0x00, 0x6a, // fieldUserPassword = 106
1838 0x00, 0x66, // fieldUserName = 102
1842 0x00, 0x6e, // fieldUserAccess = 110
1844 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
1848 wantRes: []Transaction{
1852 Type: []byte{0, 0x00},
1853 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1854 ErrorCode: []byte{0, 0, 0, 1},
1856 NewField(fieldError, []byte("You are not allowed to create new accounts.")),
1860 wantErr: assert.NoError,
1863 name: "when action is modify user without required permission",
1867 Logger: NewTestLogger(),
1868 Accounts: map[string]*Account{
1873 Access: func() *[]byte {
1874 var bits accessBitmap
1883 NewField(fieldData, []byte{
1884 0x00, 0x04, // field count
1886 0x00, 0x69, // fieldUserLogin = 105
1890 0x00, 0x6a, // fieldUserPassword = 106
1894 0x00, 0x66, // fieldUserName = 102
1898 0x00, 0x6e, // fieldUserAccess = 110
1900 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00,
1904 wantRes: []Transaction{
1908 Type: []byte{0, 0x00},
1909 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1910 ErrorCode: []byte{0, 0, 0, 1},
1912 NewField(fieldError, []byte("You are not allowed to modify accounts.")),
1916 wantErr: assert.NoError,
1919 name: "when action is delete user without required permission",
1923 Logger: NewTestLogger(),
1924 Accounts: map[string]*Account{
1929 Access: func() *[]byte {
1930 var bits accessBitmap
1939 NewField(fieldData, []byte{
1947 wantRes: []Transaction{
1951 Type: []byte{0, 0x00},
1952 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
1953 ErrorCode: []byte{0, 0, 0, 1},
1955 NewField(fieldError, []byte("You are not allowed to delete accounts.")),
1959 wantErr: assert.NoError,
1962 for _, tt := range tests {
1963 t.Run(tt.name, func(t *testing.T) {
1964 gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t)
1965 if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) {
1969 tranAssertEqual(t, tt.wantRes, gotRes)
1974 func TestHandleDelNewsArt(t *testing.T) {
1982 wantRes []Transaction
1983 wantErr assert.ErrorAssertionFunc
1986 name: "without required permission",
1990 Access: func() *[]byte {
1991 var bits accessBitmap
2002 wantRes: []Transaction{
2006 Type: []byte{0, 0x00},
2007 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2008 ErrorCode: []byte{0, 0, 0, 1},
2010 NewField(fieldError, []byte("You are not allowed to delete news articles.")),
2014 wantErr: assert.NoError,
2017 for _, tt := range tests {
2018 t.Run(tt.name, func(t *testing.T) {
2019 gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t)
2020 if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) {
2023 tranAssertEqual(t, tt.wantRes, gotRes)
2028 func TestHandleDisconnectUser(t *testing.T) {
2036 wantRes []Transaction
2037 wantErr assert.ErrorAssertionFunc
2040 name: "without required permission",
2044 Access: func() *[]byte {
2045 var bits accessBitmap
2056 wantRes: []Transaction{
2060 Type: []byte{0, 0x00},
2061 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2062 ErrorCode: []byte{0, 0, 0, 1},
2064 NewField(fieldError, []byte("You are not allowed to disconnect users.")),
2068 wantErr: assert.NoError,
2071 name: "when target user has 'cannot be disconnected' priv",
2075 Clients: map[uint16]*ClientConn{
2079 Access: func() *[]byte {
2080 var bits accessBitmap
2081 bits.Set(accessCannotBeDiscon)
2090 Access: func() *[]byte {
2091 var bits accessBitmap
2092 bits.Set(accessDisconUser)
2101 NewField(fieldUserID, []byte{0, 1}),
2104 wantRes: []Transaction{
2108 Type: []byte{0, 0x00},
2109 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
2110 ErrorCode: []byte{0, 0, 0, 1},
2112 NewField(fieldError, []byte("unnamed is not allowed to be disconnected.")),
2116 wantErr: assert.NoError,
2119 for _, tt := range tests {
2120 t.Run(tt.name, func(t *testing.T) {
2121 gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t)
2122 if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) {
2125 tranAssertEqual(t, tt.wantRes, gotRes)
2130 func TestHandleSendInstantMsg(t *testing.T) {
2138 wantRes []Transaction
2139 wantErr assert.ErrorAssertionFunc
2142 name: "when client 1 sends a message to client 2",
2146 UserName: []byte("User1"),
2148 Clients: map[uint16]*ClientConn{
2150 AutoReply: []byte(nil),
2158 NewField(fieldData, []byte("hai")),
2159 NewField(fieldUserID, []byte{0, 2}),
2162 wantRes: []Transaction{
2166 NewField(fieldData, []byte("hai")),
2167 NewField(fieldUserName, []byte("User1")),
2168 NewField(fieldUserID, []byte{0, 1}),
2169 NewField(fieldOptions, []byte{0, 1}),
2172 clientID: &[]byte{0, 1},
2175 Type: []byte{0x0, 0x6c},
2176 ID: []byte{0, 0, 0, 0},
2177 ErrorCode: []byte{0, 0, 0, 0},
2178 Fields: []Field(nil),
2181 wantErr: assert.NoError,
2184 name: "when client 2 has autoreply enabled",
2188 UserName: []byte("User1"),
2190 Clients: map[uint16]*ClientConn{
2193 UserName: []byte("User2"),
2194 AutoReply: []byte("autohai"),
2202 NewField(fieldData, []byte("hai")),
2203 NewField(fieldUserID, []byte{0, 2}),
2206 wantRes: []Transaction{
2210 NewField(fieldData, []byte("hai")),
2211 NewField(fieldUserName, []byte("User1")),
2212 NewField(fieldUserID, []byte{0, 1}),
2213 NewField(fieldOptions, []byte{0, 1}),
2218 NewField(fieldData, []byte("autohai")),
2219 NewField(fieldUserName, []byte("User2")),
2220 NewField(fieldUserID, []byte{0, 2}),
2221 NewField(fieldOptions, []byte{0, 1}),
2224 clientID: &[]byte{0, 1},
2227 Type: []byte{0x0, 0x6c},
2228 ID: []byte{0, 0, 0, 0},
2229 ErrorCode: []byte{0, 0, 0, 0},
2230 Fields: []Field(nil),
2233 wantErr: assert.NoError,
2236 for _, tt := range tests {
2237 t.Run(tt.name, func(t *testing.T) {
2238 gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t)
2239 if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) {
2243 tranAssertEqual(t, tt.wantRes, gotRes)