X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/481631f6b541a0f00c7c3ba789c13ac934bdefbc..af0e8409bd0eb3bbd97ce8d2a249344ac4d2894d:/hotline/transaction_handlers_test.go diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index 48e0f0e..bc40254 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -4,11 +4,14 @@ import ( "errors" "fmt" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "io/fs" "math/rand" "os" + "path/filepath" "strings" "testing" + "time" ) func TestHandleSetChatSubject(t *testing.T) { @@ -34,13 +37,13 @@ func TestHandleSetChatSubject(t *testing.T) { ClientConn: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 2}, }, @@ -50,13 +53,13 @@ func TestHandleSetChatSubject(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 2}, }, @@ -142,13 +145,13 @@ func TestHandleLeaveChat(t *testing.T) { ClientConn: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 2}, }, @@ -158,13 +161,13 @@ func TestHandleLeaveChat(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 2}, }, @@ -226,22 +229,22 @@ func TestHandleGetUserNameList(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { ID: &[]byte{0, 1}, - Icon: &[]byte{0, 2}, - Flags: &[]byte{0, 3}, + Icon: []byte{0, 2}, + Flags: []byte{0, 3}, UserName: []byte{0, 4}, Agreed: true, }, uint16(2): { ID: &[]byte{0, 2}, - Icon: &[]byte{0, 2}, - Flags: &[]byte{0, 3}, + Icon: []byte{0, 2}, + Flags: []byte{0, 3}, UserName: []byte{0, 4}, Agreed: true, }, uint16(3): { ID: &[]byte{0, 3}, - Icon: &[]byte{0, 2}, - Flags: &[]byte{0, 3}, + Icon: []byte{0, 2}, + Flags: []byte{0, 3}, UserName: []byte{0, 4}, Agreed: false, }, @@ -304,11 +307,10 @@ func TestHandleChatSend(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessSendChat) - access := bits[:] - return &access + return bits }(), }, UserName: []byte{0x00, 0x01}, @@ -316,13 +318,13 @@ func TestHandleChatSend(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 2}, }, @@ -366,10 +368,9 @@ func TestHandleChatSend(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -400,11 +401,10 @@ func TestHandleChatSend(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessSendChat) - access := bits[:] - return &access + return bits }(), }, UserName: []byte("Testy McTest"), @@ -412,13 +412,13 @@ func TestHandleChatSend(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 2}, }, @@ -463,11 +463,10 @@ func TestHandleChatSend(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessSendChat) - access := bits[:] - return &access + return bits }(), }, UserName: []byte{0x00, 0x01}, @@ -475,13 +474,16 @@ func TestHandleChatSend(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, - }, + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessReadChat) + return bits + }()}, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0}, + Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0}, }, ID: &[]byte{0, 2}, }, @@ -514,11 +516,10 @@ func TestHandleChatSend(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessSendChat) - access := bits[:] - return &access + return bits }(), }, UserName: []byte{0x00, 0x01}, @@ -538,19 +539,19 @@ func TestHandleChatSend(t *testing.T) { Clients: map[uint16]*ClientConn{ uint16(1): { Account: &Account{ - Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255}, + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, }, ID: &[]byte{0, 1}, }, uint16(2): { Account: &Account{ - Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0}, + Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0}, }, ID: &[]byte{0, 2}, }, uint16(3): { Account: &Account{ - Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0}, + Access: accessBitmap{0, 0, 0, 0, 0, 0, 0, 0}, }, ID: &[]byte{0, 3}, }, @@ -625,10 +626,11 @@ func TestHandleGetFileInfo(t *testing.T) { cc: &ClientConn{ ID: &[]byte{0x00, 0x01}, Server: &Server{ + FS: &OSFileStore{}, Config: &Config{ FileRoot: func() string { path, _ := os.Getwd() - return path + "/test/config/Files" + return filepath.Join(path, "/test/config/Files") }(), }, }, @@ -649,7 +651,7 @@ func TestHandleGetFileInfo(t *testing.T) { ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ NewField(fieldFileName, []byte("testfile.txt")), - NewField(fieldFileTypeString, []byte("TEXT")), + NewField(fieldFileTypeString, []byte("Text File")), NewField(fieldFileCreatorString, []byte("ttxt")), NewField(fieldFileComment, []byte{}), NewField(fieldFileType, []byte("TEXT")), @@ -672,7 +674,7 @@ func TestHandleGetFileInfo(t *testing.T) { return } - // Clear the file timestamp fields to work around problems running the tests in multiple timezones + // Clear the fileWrapper timestamp fields to work around problems running the tests in multiple timezones // TODO: revisit how to test this by mocking the stat calls gotRes[0].Fields[5].Data = make([]byte, 8) gotRes[0].Fields[6].Data = make([]byte, 8) @@ -689,21 +691,63 @@ func TestHandleNewFolder(t *testing.T) { t *Transaction } tests := []struct { - setup func() name string args args wantRes []Transaction wantErr bool }{ + { + name: "without required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + accessCreateFolder, + &[]byte{0, 0}, + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to create folders.")), + }, + }, + }, + wantErr: false, + }, { name: "when path is nested", args: args{ cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCreateFolder) + return bits + }(), + }, ID: &[]byte{0, 1}, Server: &Server{ Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -717,12 +761,6 @@ func TestHandleNewFolder(t *testing.T) { }), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -739,11 +777,24 @@ func TestHandleNewFolder(t *testing.T) { name: "when path is not nested", args: args{ cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCreateFolder) + return bits + }(), + }, ID: &[]byte{0, 1}, Server: &Server{ Config: &Config{ FileRoot: "/Files", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -751,12 +802,6 @@ func TestHandleNewFolder(t *testing.T) { NewField(fieldFileName, []byte("testFolder")), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -770,14 +815,27 @@ func TestHandleNewFolder(t *testing.T) { wantErr: false, }, { - name: "when UnmarshalBinary returns an err", + name: "when Write returns an err", args: args{ cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCreateFolder) + return bits + }(), + }, ID: &[]byte{0, 1}, Server: &Server{ Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -788,12 +846,6 @@ func TestHandleNewFolder(t *testing.T) { }), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/aaa/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/aaa/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{}, wantErr: true, }, @@ -801,11 +853,24 @@ func TestHandleNewFolder(t *testing.T) { name: "fieldFileName does not allow directory traversal", args: args{ cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCreateFolder) + return bits + }(), + }, ID: &[]byte{0, 1}, Server: &Server{ Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -813,12 +878,6 @@ func TestHandleNewFolder(t *testing.T) { NewField(fieldFileName, []byte("../../testFolder")), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -834,11 +893,24 @@ func TestHandleNewFolder(t *testing.T) { name: "fieldFilePath does not allow directory traversal", args: args{ cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCreateFolder) + return bits + }(), + }, ID: &[]byte{0, 1}, Server: &Server{ Config: &Config{ FileRoot: "/Files/", }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil) + mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist) + return mfs + }(), }, }, t: NewTransaction( @@ -855,12 +927,6 @@ func TestHandleNewFolder(t *testing.T) { }), ), }, - setup: func() { - mfs := &MockFileStore{} - mfs.On("Mkdir", "/Files/foo/testFolder", fs.FileMode(0777)).Return(nil) - mfs.On("Stat", "/Files/foo/testFolder").Return(nil, os.ErrNotExist) - FS = mfs - }, wantRes: []Transaction{ { clientID: &[]byte{0, 1}, @@ -875,13 +941,13 @@ func TestHandleNewFolder(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.setup() gotRes, err := HandleNewFolder(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("HandleNewFolder() error = %v, wantErr %v", err, tt.wantErr) return } + if !tranAssertEqual(t, tt.wantRes, gotRes) { t.Errorf("HandleNewFolder() gotRes = %v, want %v", gotRes, tt.wantRes) } @@ -905,15 +971,20 @@ func TestHandleUploadFile(t *testing.T) { args: args{ cc: &ClientConn{ Server: &Server{ - FileTransfers: map[uint32]*FileTransfer{}, + FS: &OSFileStore{}, + fileTransfers: map[[4]byte]*FileTransfer{}, + Config: &Config{ + FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), + }}, + transfers: map[int]map[[4]byte]*FileTransfer{ + FileUpload: {}, }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessUploadFile) bits.Set(accessUploadAnywhere) - access := bits[:] - return &access + return bits }(), }, }, @@ -947,15 +1018,11 @@ func TestHandleUploadFile(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, - Server: &Server{ - FileTransfers: map[uint32]*FileTransfer{}, - }, }, t: NewTransaction( tranUploadFile, &[]byte{0, 1}, @@ -1005,31 +1072,20 @@ func TestHandleMakeAlias(t *testing.T) { } tests := []struct { name string - setup func() args args wantRes []Transaction wantErr bool }{ { name: "with valid input and required permissions", - setup: func() { - mfs := &MockFileStore{} - path, _ := os.Getwd() - mfs.On( - "Symlink", - path+"/test/config/Files/foo/testFile", - path+"/test/config/Files/bar/testFile", - ).Return(nil) - FS = mfs - }, args: args{ cc: &ClientConn{ + logger: NewTestLogger(), Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessMakeAlias) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1040,6 +1096,16 @@ func TestHandleMakeAlias(t *testing.T) { }(), }, Logger: NewTestLogger(), + FS: func() *MockFileStore { + mfs := &MockFileStore{} + path, _ := os.Getwd() + mfs.On( + "Symlink", + path+"/test/config/Files/foo/testFile", + path+"/test/config/Files/bar/testFile", + ).Return(nil) + return mfs + }(), }, }, t: NewTransaction( @@ -1063,24 +1129,14 @@ func TestHandleMakeAlias(t *testing.T) { }, { name: "when symlink returns an error", - setup: func() { - mfs := &MockFileStore{} - path, _ := os.Getwd() - mfs.On( - "Symlink", - path+"/test/config/Files/foo/testFile", - path+"/test/config/Files/bar/testFile", - ).Return(errors.New("ohno")) - FS = mfs - }, args: args{ cc: &ClientConn{ + logger: NewTestLogger(), Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessMakeAlias) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1091,6 +1147,16 @@ func TestHandleMakeAlias(t *testing.T) { }(), }, Logger: NewTestLogger(), + FS: func() *MockFileStore { + mfs := &MockFileStore{} + path, _ := os.Getwd() + mfs.On( + "Symlink", + path+"/test/config/Files/foo/testFile", + path+"/test/config/Files/bar/testFile", + ).Return(errors.New("ohno")) + return mfs + }(), }, }, t: NewTransaction( @@ -1115,15 +1181,14 @@ func TestHandleMakeAlias(t *testing.T) { wantErr: false, }, { - name: "when user does not have required permission", - setup: func() {}, + name: "when user does not have required permission", args: args{ cc: &ClientConn{ + logger: NewTestLogger(), Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1169,8 +1234,6 @@ func TestHandleMakeAlias(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.setup() - gotRes, err := HandleMakeAlias(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("HandleMakeAlias(%v, %v)", tt.args.cc, tt.args.t) @@ -1198,11 +1261,10 @@ func TestHandleGetUser(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessOpenUser) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1211,7 +1273,7 @@ func TestHandleGetUser(t *testing.T) { Login: "guest", Name: "Guest", Password: "password", - Access: &[]byte{1}, + Access: accessBitmap{}, }, }, }, @@ -1232,7 +1294,7 @@ func TestHandleGetUser(t *testing.T) { NewField(fieldUserName, []byte("Guest")), NewField(fieldUserLogin, negateString([]byte("guest"))), NewField(fieldUserPassword, []byte("password")), - NewField(fieldUserAccess, []byte{1}), + NewField(fieldUserAccess, []byte{0, 0, 0, 0, 0, 0, 0, 0}), }, }, }, @@ -1243,10 +1305,9 @@ func TestHandleGetUser(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1277,11 +1338,10 @@ func TestHandleGetUser(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessOpenUser) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1327,26 +1387,19 @@ func TestHandleDeleteUser(t *testing.T) { } tests := []struct { name string - setup func() args args wantRes []Transaction wantErr assert.ErrorAssertionFunc }{ { - name: "when user exists", - setup: func() { - mfs := &MockFileStore{} - mfs.On("Remove", "Users/testuser.yaml").Return(nil) - FS = mfs - }, + name: "when user dataFile", args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessDeleteUser) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1355,9 +1408,14 @@ func TestHandleDeleteUser(t *testing.T) { Login: "testuser", Name: "Testy McTest", Password: "password", - Access: &[]byte{1}, + Access: accessBitmap{}, }, }, + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("Remove", "Users/testuser.yaml").Return(nil) + return mfs + }(), }, }, t: NewTransaction( @@ -1378,15 +1436,13 @@ func TestHandleDeleteUser(t *testing.T) { wantErr: assert.NoError, }, { - name: "when user does not have required permission", - setup: func() {}, + name: "when user does not have required permission", args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1415,7 +1471,6 @@ func TestHandleDeleteUser(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.setup() gotRes, err := HandleDeleteUser(tt.args.cc, tt.args.t) if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteUser(%v, %v)", tt.args.cc, tt.args.t)) { return @@ -1442,11 +1497,10 @@ func TestHandleGetMsgs(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessNewsReadArt) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1476,10 +1530,9 @@ func TestHandleGetMsgs(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1533,10 +1586,9 @@ func TestHandleNewUser(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1561,6 +1613,48 @@ func TestHandleNewUser(t *testing.T) { }, wantErr: assert.NoError, }, + { + name: "when user attempts to create account with greater access", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCreateUser) + return bits + }(), + }, + Server: &Server{ + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranNewUser, &[]byte{0, 1}, + NewField(fieldUserLogin, []byte("userB")), + NewField( + fieldUserAccess, + func() []byte { + var bits accessBitmap + bits.Set(accessDisconUser) + return bits[:] + }(), + ), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("Cannot create account with more access than yourself.")), + }, + }, + }, + wantErr: assert.NoError, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1590,10 +1684,9 @@ func TestHandleListUsers(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1618,6 +1711,51 @@ func TestHandleListUsers(t *testing.T) { }, wantErr: assert.NoError, }, + { + name: "when user has required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessOpenUser) + return bits + }(), + }, + Server: &Server{ + Accounts: map[string]*Account{ + "guest": { + Name: "guest", + Login: "guest", + Password: "zz", + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, + }, + }, + }, + }, + t: NewTransaction( + tranGetClientInfoText, &[]byte{0, 1}, + NewField(fieldUserID, []byte{0, 1}), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x01, 0x2f}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte{ + 0x00, 0x04, 0x00, 0x66, 0x00, 0x05, 0x67, 0x75, 0x65, 0x73, 0x74, 0x00, 0x69, 0x00, 0x05, 0x98, + 0x8a, 0x9a, 0x8c, 0x8b, 0x00, 0x6e, 0x00, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0x00, 0x6a, 0x00, 0x01, 0x78, + }), + }, + }, + }, + wantErr: assert.NoError, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1647,10 +1785,9 @@ func TestHandleDownloadFile(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{}, @@ -1675,17 +1812,19 @@ func TestHandleDownloadFile(t *testing.T) { name: "with a valid file", args: args{ cc: &ClientConn{ - Transfers: make(map[int][]*FileTransfer), + transfers: map[int]map[[4]byte]*FileTransfer{ + FileDownload: {}, + }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessDownloadFile) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ - FileTransfers: make(map[uint32]*FileTransfer), + FS: &OSFileStore{}, + fileTransfers: map[[4]byte]*FileTransfer{}, Config: &Config{ FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), }, @@ -1716,12 +1855,100 @@ func TestHandleDownloadFile(t *testing.T) { }, wantErr: assert.NoError, }, + { + name: "when client requests to resume 1k test file at offset 256", + args: args{ + cc: &ClientConn{ + transfers: map[int]map[[4]byte]*FileTransfer{ + FileDownload: {}, + }, Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessDownloadFile) + return bits + }(), + }, + Server: &Server{ + FS: &OSFileStore{}, + + // FS: func() *MockFileStore { + // path, _ := os.Getwd() + // testFile, err := os.Open(path + "/test/config/Files/testfile-1k") + // if err != nil { + // panic(err) + // } + // + // mfi := &MockFileInfo{} + // mfi.On("Mode").Return(fs.FileMode(0)) + // mfs := &MockFileStore{} + // mfs.On("Stat", "/fakeRoot/Files/testfile.txt").Return(mfi, nil) + // mfs.On("Open", "/fakeRoot/Files/testfile.txt").Return(testFile, nil) + // mfs.On("Stat", "/fakeRoot/Files/.info_testfile.txt").Return(nil, errors.New("no")) + // mfs.On("Stat", "/fakeRoot/Files/.rsrc_testfile.txt").Return(nil, errors.New("no")) + // + // return mfs + // }(), + fileTransfers: map[[4]byte]*FileTransfer{}, + Config: &Config{ + FileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), + }, + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + accessDownloadFile, + &[]byte{0, 1}, + NewField(fieldFileName, []byte("testfile-1k")), + NewField(fieldFilePath, []byte{0x00, 0x00}), + NewField( + fieldFileResumeData, + func() []byte { + frd := FileResumeData{ + Format: [4]byte{}, + Version: [2]byte{}, + RSVD: [34]byte{}, + ForkCount: [2]byte{0, 2}, + ForkInfoList: []ForkInfoList{ + { + Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA" + DataSize: [4]byte{0, 0, 0x01, 0x00}, // request offset 256 + RSVDA: [4]byte{}, + RSVDB: [4]byte{}, + }, + { + Fork: [4]byte{0x4d, 0x41, 0x43, 0x52}, // "MACR" + DataSize: [4]byte{0, 0, 0, 0}, + RSVDA: [4]byte{}, + RSVDB: [4]byte{}, + }, + }, + } + b, _ := frd.BinaryMarshal() + return b + }(), + ), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x2}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldRefNum, []byte{0x52, 0xfd, 0xfc, 0x07}), + NewField(fieldWaitingCount, []byte{0x00, 0x00}), + NewField(fieldTransferSize, []byte{0x00, 0x00, 0x03, 0x8d}), + NewField(fieldFileSize, []byte{0x00, 0x00, 0x03, 0x00}), + }, + }, + }, + wantErr: assert.NoError, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // reset the rand seed so that the random fieldRefNum will be deterministic - rand.Seed(1) - gotRes, err := HandleDownloadFile(tt.args.cc, tt.args.t) if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadFile(%v, %v)", tt.args.cc, tt.args.t)) { return @@ -1731,3 +1958,1605 @@ func TestHandleDownloadFile(t *testing.T) { }) } } + +func TestHandleUpdateUser(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when action is create user without required permission", + args: args{ + cc: &ClientConn{ + logger: NewTestLogger(), + Server: &Server{ + Logger: NewTestLogger(), + }, + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranUpdateUser, + &[]byte{0, 0}, + NewField(fieldData, []byte{ + 0x00, 0x04, // field count + + 0x00, 0x69, // fieldUserLogin = 105 + 0x00, 0x03, + 0x9d, 0x9d, 0x9d, + + 0x00, 0x6a, // fieldUserPassword = 106 + 0x00, 0x03, + 0x9c, 0x9c, 0x9c, + + 0x00, 0x66, // fieldUserName = 102 + 0x00, 0x03, + 0x61, 0x61, 0x61, + + 0x00, 0x6e, // fieldUserAccess = 110 + 0x00, 0x08, + 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00, + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to create new accounts.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when action is modify user without required permission", + args: args{ + cc: &ClientConn{ + logger: NewTestLogger(), + Server: &Server{ + Logger: NewTestLogger(), + Accounts: map[string]*Account{ + "bbb": {}, + }, + }, + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranUpdateUser, + &[]byte{0, 0}, + NewField(fieldData, []byte{ + 0x00, 0x04, // field count + + 0x00, 0x69, // fieldUserLogin = 105 + 0x00, 0x03, + 0x9d, 0x9d, 0x9d, + + 0x00, 0x6a, // fieldUserPassword = 106 + 0x00, 0x03, + 0x9c, 0x9c, 0x9c, + + 0x00, 0x66, // fieldUserName = 102 + 0x00, 0x03, + 0x61, 0x61, 0x61, + + 0x00, 0x6e, // fieldUserAccess = 110 + 0x00, 0x08, + 0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00, + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to modify accounts.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when action is delete user without required permission", + args: args{ + cc: &ClientConn{ + logger: NewTestLogger(), + Server: &Server{ + Accounts: map[string]*Account{ + "bbb": {}, + }, + }, + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranUpdateUser, + &[]byte{0, 0}, + NewField(fieldData, []byte{ + 0x00, 0x01, + 0x00, 0x65, + 0x00, 0x03, + 0x88, 0x9e, 0x8b, + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to delete accounts.")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleUpdateUser(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleUpdateUser(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleDelNewsArt(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "without required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranDelNewsArt, + &[]byte{0, 0}, + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to delete news articles.")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleDelNewsArt(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsArt(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleDisconnectUser(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "without required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranDelNewsArt, + &[]byte{0, 0}, + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to disconnect users.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when target user has 'cannot be disconnected' priv", + args: args{ + cc: &ClientConn{ + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(1): { + Account: &Account{ + Login: "unnamed", + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessCannotBeDiscon) + return bits + }(), + }, + }, + }, + }, + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessDisconUser) + return bits + }(), + }, + }, + t: NewTransaction( + tranDelNewsArt, + &[]byte{0, 0}, + NewField(fieldUserID, []byte{0, 1}), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("unnamed is not allowed to be disconnected.")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleDisconnectUser(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleDisconnectUser(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleSendInstantMsg(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "without required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranDelNewsArt, + &[]byte{0, 0}, + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to send private messages.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when client 1 sends a message to client 2", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessSendPrivMsg) + return bits + }(), + }, + ID: &[]byte{0, 1}, + UserName: []byte("User1"), + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + AutoReply: []byte(nil), + Flags: []byte{0, 0}, + }, + }, + }, + }, + t: NewTransaction( + tranSendInstantMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + *NewTransaction( + tranServerMsg, + &[]byte{0, 2}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserName, []byte("User1")), + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldOptions, []byte{0, 1}), + ), + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x0, 0x6c}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field(nil), + }, + }, + wantErr: assert.NoError, + }, + { + name: "when client 2 has autoreply enabled", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessSendPrivMsg) + return bits + }(), + }, + ID: &[]byte{0, 1}, + UserName: []byte("User1"), + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + Flags: []byte{0, 0}, + ID: &[]byte{0, 2}, + UserName: []byte("User2"), + AutoReply: []byte("autohai"), + }, + }, + }, + }, + t: NewTransaction( + tranSendInstantMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + *NewTransaction( + tranServerMsg, + &[]byte{0, 2}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserName, []byte("User1")), + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldOptions, []byte{0, 1}), + ), + *NewTransaction( + tranServerMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("autohai")), + NewField(fieldUserName, []byte("User2")), + NewField(fieldUserID, []byte{0, 2}), + NewField(fieldOptions, []byte{0, 1}), + ), + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x0, 0x6c}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field(nil), + }, + }, + wantErr: assert.NoError, + }, + { + name: "when client 2 has refuse private messages enabled", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessSendPrivMsg) + return bits + }(), + }, + ID: &[]byte{0, 1}, + UserName: []byte("User1"), + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + Flags: []byte{255, 255}, + ID: &[]byte{0, 2}, + UserName: []byte("User2"), + }, + }, + }, + }, + t: NewTransaction( + tranSendInstantMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + *NewTransaction( + tranServerMsg, + &[]byte{0, 1}, + NewField(fieldData, []byte("User2 does not accept private messages.")), + NewField(fieldUserName, []byte("User2")), + NewField(fieldUserID, []byte{0, 2}), + NewField(fieldOptions, []byte{0, 2}), + ), + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x0, 0x6c}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field(nil), + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleSendInstantMsg(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleSendInstantMsg(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleDeleteFile(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when user does not have required permission to delete a folder", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + Server: &Server{ + Config: &Config{ + FileRoot: func() string { + return "/fakeRoot/Files" + }(), + }, + FS: func() *MockFileStore { + mfi := &MockFileInfo{} + mfi.On("Mode").Return(fs.FileMode(0)) + mfi.On("Size").Return(int64(100)) + mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout)) + mfi.On("IsDir").Return(false) + mfi.On("Name").Return("testfile") + + mfs := &MockFileStore{} + mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil) + mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err")) + mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err")) + + return mfs + }(), + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranDeleteFile, &[]byte{0, 1}, + NewField(fieldFileName, []byte("testfile")), + NewField(fieldFilePath, []byte{ + 0x00, 0x01, + 0x00, 0x00, + 0x03, + 0x61, 0x61, 0x61, + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to delete files.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "deletes all associated metadata files", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessDeleteFile) + return bits + }(), + }, + Server: &Server{ + Config: &Config{ + FileRoot: func() string { + return "/fakeRoot/Files" + }(), + }, + FS: func() *MockFileStore { + mfi := &MockFileInfo{} + mfi.On("Mode").Return(fs.FileMode(0)) + mfi.On("Size").Return(int64(100)) + mfi.On("ModTime").Return(time.Parse(time.Layout, time.Layout)) + mfi.On("IsDir").Return(false) + mfi.On("Name").Return("testfile") + + mfs := &MockFileStore{} + mfs.On("Stat", "/fakeRoot/Files/aaa/testfile").Return(mfi, nil) + mfs.On("Stat", "/fakeRoot/Files/aaa/.info_testfile").Return(nil, errors.New("err")) + mfs.On("Stat", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil, errors.New("err")) + + mfs.On("RemoveAll", "/fakeRoot/Files/aaa/testfile").Return(nil) + mfs.On("Remove", "/fakeRoot/Files/aaa/testfile.incomplete").Return(nil) + mfs.On("Remove", "/fakeRoot/Files/aaa/.rsrc_testfile").Return(nil) + mfs.On("Remove", "/fakeRoot/Files/aaa/.info_testfile").Return(nil) + + return mfs + }(), + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranDeleteFile, &[]byte{0, 1}, + NewField(fieldFileName, []byte("testfile")), + NewField(fieldFilePath, []byte{ + 0x00, 0x01, + 0x00, 0x00, + 0x03, + 0x61, 0x61, 0x61, + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x0, 0xcc}, + ID: []byte{0x0, 0x0, 0x0, 0x0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field(nil), + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleDeleteFile(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleDeleteFile(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + + tt.args.cc.Server.FS.(*MockFileStore).AssertExpectations(t) + }) + } +} + +func TestHandleGetFileNameList(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when fieldFilePath is a drop box, but user does not have accessViewDropBoxes ", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + Server: &Server{ + + Config: &Config{ + FileRoot: func() string { + path, _ := os.Getwd() + return filepath.Join(path, "/test/config/Files/getFileNameListTestDir") + }(), + }, + }, + }, + t: NewTransaction( + tranGetFileNameList, &[]byte{0, 1}, + NewField(fieldFilePath, []byte{ + 0x00, 0x01, + 0x00, 0x00, + 0x08, + 0x64, 0x72, 0x6f, 0x70, 0x20, 0x62, 0x6f, 0x78, // "drop box" + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to view drop boxes.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "with file root", + args: args{ + cc: &ClientConn{ + Server: &Server{ + Config: &Config{ + FileRoot: func() string { + path, _ := os.Getwd() + return filepath.Join(path, "/test/config/Files/getFileNameListTestDir") + }(), + }, + }, + }, + t: NewTransaction( + tranGetFileNameList, &[]byte{0, 1}, + NewField(fieldFilePath, []byte{ + 0x00, 0x00, + 0x00, 0x00, + }), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0xc8}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField( + fieldFileNameWithInfo, + func() []byte { + fnwi := FileNameWithInfo{ + fileNameWithInfoHeader: fileNameWithInfoHeader{ + Type: [4]byte{0x54, 0x45, 0x58, 0x54}, + Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, + FileSize: [4]byte{0, 0, 0x04, 0}, + RSVD: [4]byte{}, + NameScript: [2]byte{}, + NameSize: [2]byte{0, 0x0b}, + }, + name: []byte("testfile-1k"), + } + b, _ := fnwi.MarshalBinary() + return b + }(), + ), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleGetFileNameList(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleGetFileNameList(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleGetClientInfoText(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when user does not have required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + Server: &Server{ + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranGetClientInfoText, &[]byte{0, 1}, + NewField(fieldUserID, []byte{0, 1}), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to get client info.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "with a valid user", + args: args{ + cc: &ClientConn{ + UserName: []byte("Testy McTest"), + RemoteAddr: "1.2.3.4:12345", + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessGetClientInfo) + return bits + }(), + Name: "test", + Login: "test", + }, + Server: &Server{ + Accounts: map[string]*Account{}, + Clients: map[uint16]*ClientConn{ + uint16(1): { + UserName: []byte("Testy McTest"), + RemoteAddr: "1.2.3.4:12345", + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessGetClientInfo) + return bits + }(), + Name: "test", + Login: "test", + }, + }, + }, + }, + transfers: map[int]map[[4]byte]*FileTransfer{ + FileDownload: {}, + FileUpload: {}, + FolderDownload: {}, + FolderUpload: {}, + }, + }, + t: NewTransaction( + tranGetClientInfoText, &[]byte{0, 1}, + NewField(fieldUserID, []byte{0, 1}), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x1, 0x2f}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte( + strings.Replace(`Nickname: Testy McTest +Name: test +Account: test +Address: 1.2.3.4:12345 + +-------- File Downloads --------- + +None. + +------- Folder Downloads -------- + +None. + +--------- File Uploads ---------- + +None. + +-------- Folder Uploads --------- + +None. + +------- Waiting Downloads ------- + +None. + +`, "\n", "\r", -1)), + ), + NewField(fieldUserName, []byte("Testy McTest")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleGetClientInfoText(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleGetClientInfoText(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleTranAgreed(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "normal request flow", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessDisconUser) + bits.Set(accessAnyName) + return bits + }()}, + Icon: []byte{0, 1}, + Flags: []byte{0, 1}, + Version: []byte{0, 1}, + ID: &[]byte{0, 1}, + logger: NewTestLogger(), + Server: &Server{ + Config: &Config{ + BannerFile: "banner.jpg", + }, + }, + }, + t: NewTransaction( + tranAgreed, nil, + NewField(fieldUserName, []byte("username")), + NewField(fieldUserIconID, []byte{0, 1}), + NewField(fieldOptions, []byte{0, 0}), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x7a}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldBannerType, []byte("JPEG")), + }, + }, + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x79}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{}, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleTranAgreed(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleTranAgreed(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleSetClientUserInfo(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when client does not have accessAnyName", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + ID: &[]byte{0, 1}, + UserName: []byte("Guest"), + Flags: []byte{0, 1}, + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(1): { + ID: &[]byte{0, 1}, + }, + }, + }, + }, + t: NewTransaction( + tranSetClientUserInfo, nil, + NewField(fieldUserIconID, []byte{0, 1}), + NewField(fieldUserName, []byte("NOPE")), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0x01, 0x2d}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldUserIconID, []byte{0, 1}), + NewField(fieldUserFlags, []byte{0, 1}), + NewField(fieldUserName, []byte("Guest"))}, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleSetClientUserInfo(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleSetClientUserInfo(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleDelNewsItem(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when user does not have permission to delete a news category", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: accessBitmap{}, + }, + ID: &[]byte{0, 1}, + Server: &Server{ + ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ + "test": { + Type: []byte{0, 3}, + Count: nil, + NameSize: 0, + Name: "zz", + }, + }}, + }, + }, + t: NewTransaction( + tranDelNewsItem, nil, + NewField(fieldNewsPath, + []byte{ + 0, 1, + 0, 0, + 4, + 0x74, 0x65, 0x73, 0x74, + }, + ), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to delete news categories.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when user does not have permission to delete a news folder", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: accessBitmap{}, + }, + ID: &[]byte{0, 1}, + Server: &Server{ + ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ + "testcat": { + Type: []byte{0, 2}, + Count: nil, + NameSize: 0, + Name: "test", + }, + }}, + }, + }, + t: NewTransaction( + tranDelNewsItem, nil, + NewField(fieldNewsPath, + []byte{ + 0, 1, + 0, 0, + 4, + 0x74, 0x65, 0x73, 0x74, + }, + ), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to delete news folders.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when user deletes a news folder", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessNewsDeleteFldr) + return bits + }(), + }, + ID: &[]byte{0, 1}, + Server: &Server{ + ConfigDir: "/fakeConfigRoot", + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist) + return mfs + }(), + ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ + "testcat": { + Type: []byte{0, 2}, + Count: nil, + NameSize: 0, + Name: "test", + }, + }}, + }, + }, + t: NewTransaction( + tranDelNewsItem, nil, + NewField(fieldNewsPath, + []byte{ + 0, 1, + 0, 0, + 4, + 0x74, 0x65, 0x73, 0x74, + }, + ), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x01, 0x7c}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{}, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleDelNewsItem(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleDelNewsItem(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleDownloadBanner(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "returns expected response", + args: args{ + cc: &ClientConn{ + ID: &[]byte{0, 1}, + transfers: map[int]map[[4]byte]*FileTransfer{ + bannerDownload: {}, + }, + Server: &Server{ + ConfigDir: "/config", + Config: &Config{ + BannerFile: "banner.jpg", + }, + fileTransfers: map[[4]byte]*FileTransfer{}, + FS: func() *MockFileStore { + mfi := &MockFileInfo{} + mfi.On("Size").Return(int64(100)) + + mfs := &MockFileStore{} + mfs.On("Stat", "/config/banner.jpg").Return(mfi, nil) + return mfs + }(), + }, + }, + t: NewTransaction(tranDownloadBanner, nil), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0x00, 0xd4}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldRefNum, []byte{1, 2, 3, 4}), + NewField(fieldTransferSize, []byte{0, 0, 0, 0x64}), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleDownloadBanner(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleTranOldPostNews(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when user does not have required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranOldPostNews, &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to post news.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when user posts news update", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessNewsPostArt) + return bits + }(), + }, + Server: &Server{ + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("WriteFile", "/fakeConfigRoot/MessageBoard.txt", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist) + return mfs + }(), + ConfigDir: "/fakeConfigRoot", + Config: &Config{}, + }, + }, + t: NewTransaction( + tranOldPostNews, &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x67}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleTranOldPostNews(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleTranOldPostNews(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleInviteNewChat(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when user does not have required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction(tranInviteNewChat, &[]byte{0, 1}), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to request private chat.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when userA invites userB to new private chat", + args: args{ + cc: &ClientConn{ + ID: &[]byte{0, 1}, + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessOpenChat) + return bits + }(), + }, + UserName: []byte("UserA"), + Icon: []byte{0, 1}, + Flags: []byte{0, 0}, + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + ID: &[]byte{0, 2}, + UserName: []byte("UserB"), + Flags: []byte{0, 0}, + }, + }, + PrivateChats: make(map[uint32]*PrivateChat), + }, + }, + t: NewTransaction( + tranInviteNewChat, &[]byte{0, 1}, + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 2}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x71}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}), + NewField(fieldUserName, []byte("UserA")), + NewField(fieldUserID, []byte{0, 1}), + }, + }, + + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x70}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}), + NewField(fieldUserName, []byte("UserA")), + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldUserIconID, []byte{0, 1}), + NewField(fieldUserFlags, []byte{0, 0}), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when userA invites userB to new private chat, but UserB has refuse private chat enabled", + args: args{ + cc: &ClientConn{ + ID: &[]byte{0, 1}, + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessOpenChat) + return bits + }(), + }, + UserName: []byte("UserA"), + Icon: []byte{0, 1}, + Flags: []byte{0, 0}, + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(2): { + ID: &[]byte{0, 2}, + UserName: []byte("UserB"), + Flags: []byte{255, 255}, + }, + }, + PrivateChats: make(map[uint32]*PrivateChat), + }, + }, + t: NewTransaction( + tranInviteNewChat, &[]byte{0, 1}, + NewField(fieldUserID, []byte{0, 2}), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x68}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte("UserB does not accept private chats.")), + NewField(fieldUserName, []byte("UserB")), + NewField(fieldUserID, []byte{0, 2}), + NewField(fieldOptions, []byte{0, 2}), + }, + }, + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x70}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}), + NewField(fieldUserName, []byte("UserA")), + NewField(fieldUserID, []byte{0, 1}), + NewField(fieldUserIconID, []byte{0, 1}), + NewField(fieldUserFlags, []byte{0, 0}), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rand.Seed(1) + gotRes, err := HandleInviteNewChat(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleInviteNewChat(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +}