From: Jeff Halter Date: Thu, 2 Jun 2022 22:22:11 +0000 (-0700) Subject: Cleanup and backfill tests X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/481631f6b541a0f00c7c3ba789c13ac934bdefbc?hp=5ae5087660f0855087a2a65181d000d4383a45f4 Cleanup and backfill tests --- diff --git a/hotline/file_store.go b/hotline/file_store.go index ed0ffa4..ba86c6a 100644 --- a/hotline/file_store.go +++ b/hotline/file_store.go @@ -2,6 +2,7 @@ package hotline import ( "github.com/stretchr/testify/mock" + "io/fs" "os" ) @@ -14,6 +15,7 @@ type FileStore interface { Symlink(oldname, newname string) error Remove(name string) error Create(name string) (*os.File, error) + WriteFile(name string, data []byte, perm fs.FileMode) error // TODO: implement these // Rename(oldpath string, newpath string) error // RemoveAll(path string) error @@ -45,6 +47,10 @@ func (fs *OSFileStore) Create(name string) (*os.File, error) { return os.Create(name) } +func (fs *OSFileStore) WriteFile(name string, data []byte, perm fs.FileMode) error { + return os.WriteFile(name, data, perm) +} + type MockFileStore struct { mock.Mock } @@ -82,3 +88,8 @@ func (mfs *MockFileStore) Create(name string) (*os.File, error) { args := mfs.Called(name) return args.Get(0).(*os.File), args.Error(1) } + +func (mfs *MockFileStore) WriteFile(name string, data []byte, perm fs.FileMode) error { + args := mfs.Called(name, data, perm) + return args.Error(0) +} diff --git a/hotline/server.go b/hotline/server.go index 067cf9f..b0ddf2d 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -355,7 +355,7 @@ func (s *Server) NewUser(login, name, password string, access []byte) error { } s.Accounts[login] = &account - return ioutil.WriteFile(s.ConfigDir+"Users/"+login+".yaml", out, 0666) + return FS.WriteFile(s.ConfigDir+"Users/"+login+".yaml", out, 0666) } // DeleteUser deletes the user account diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index ea2409a..cce303f 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -50,8 +50,7 @@ var TransactionHandlers = map[uint16]TransactionType{ Handler: HandleTranAgreed, }, tranChatSend: { - Access: accessSendChat, - DenyMsg: "You are not allowed to participate in chat.", + Access: accessAlwaysAllow, Handler: HandleChatSend, Name: "tranChatSend", RequiredFields: []requiredField{ @@ -80,8 +79,7 @@ var TransactionHandlers = map[uint16]TransactionType{ Handler: HandleDeleteFile, }, tranDeleteUser: { - Access: accessDeleteUser, - DenyMsg: "You are not allowed to delete accounts.", + Access: accessAlwaysAllow, Name: "tranDeleteUser", Handler: HandleDeleteUser, }, @@ -92,8 +90,7 @@ var TransactionHandlers = map[uint16]TransactionType{ Handler: HandleDisconnectUser, }, tranDownloadFile: { - Access: accessDownloadFile, - DenyMsg: "You are not allowed to download files.", + Access: accessAlwaysAllow, Name: "tranDownloadFile", Handler: HandleDownloadFile, }, @@ -120,8 +117,7 @@ var TransactionHandlers = map[uint16]TransactionType{ Handler: HandleGetFileNameList, }, tranGetMsgs: { - Access: accessNewsReadArt, - DenyMsg: "You are not allowed to read news.", + Access: accessAlwaysAllow, Name: "tranGetMsgs", Handler: HandleGetMsgs, }, @@ -144,8 +140,7 @@ var TransactionHandlers = map[uint16]TransactionType{ Handler: HandleGetNewsCatNameList, }, tranGetUser: { - Access: accessOpenUser, - DenyMsg: "You are not allowed to view accounts.", + Access: accessAlwaysAllow, Name: "tranGetUser", Handler: HandleGetUser, }, @@ -181,10 +176,8 @@ var TransactionHandlers = map[uint16]TransactionType{ Name: "tranJoinChat", Handler: HandleLeaveChat, }, - tranListUsers: { - Access: accessOpenUser, - DenyMsg: "You are not allowed to view accounts.", + Access: accessAlwaysAllow, Name: "tranListUsers", Handler: HandleListUsers, }, @@ -213,8 +206,7 @@ var TransactionHandlers = map[uint16]TransactionType{ Handler: HandleNewNewsFldr, }, tranNewUser: { - Access: accessCreateUser, - DenyMsg: "You are not allowed to create new accounts.", + Access: accessAlwaysAllow, Name: "tranNewUser", Handler: HandleNewUser, }, @@ -327,8 +319,10 @@ func HandleChatSend(cc *ClientConn, t *Transaction) (res []Transaction, err erro chatInt := binary.BigEndian.Uint32(chatID) privChat := cc.Server.PrivateChats[chatInt] + clients := sortedClients(privChat.ClientConn) + // send the message to all connected clients of the private chat - for _, c := range privChat.ClientConn { + for _, c := range clients { res = append(res, *NewTransaction( tranChatMsg, c.ID, @@ -669,8 +663,7 @@ func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error account := cc.Server.Accounts[string(t.GetField(fieldUserLogin).Data)] if account == nil { - errorT := cc.NewErrReply(t, "Account does not exist.") - res = append(res, errorT) + res = append(res, cc.NewErrReply(t, "Account does not exist.")) return res, err } @@ -684,6 +677,11 @@ func HandleGetUser(cc *ClientConn, t *Transaction) (res []Transaction, err error } func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err error) { + if !authorize(cc.Account.Access, accessOpenUser) { + res = append(res, cc.NewErrReply(t, "You are not allowed to view accounts.")) + return res, err + } + var userFields []Field // TODO: make order deterministic for _, acc := range cc.Server.Accounts { @@ -697,10 +695,14 @@ func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err err // HandleNewUser creates a new user account func HandleNewUser(cc *ClientConn, t *Transaction) (res []Transaction, err error) { + if !authorize(cc.Account.Access, accessCreateUser) { + res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts.")) + return res, err + } + login := DecodeUserString(t.GetField(fieldUserLogin).Data) // If the account already exists, reply with an error - // TODO: make order deterministic if _, ok := cc.Server.Accounts[login]; ok { res = append(res, cc.NewErrReply(t, "Cannot create account "+login+" because there is already an account with that login.")) return res, err @@ -1201,12 +1203,22 @@ func HandlePostNewsArt(cc *ClientConn, t *Transaction) (res []Transaction, err e // HandleGetMsgs returns the flat news data func HandleGetMsgs(cc *ClientConn, t *Transaction) (res []Transaction, err error) { + if !authorize(cc.Account.Access, accessNewsReadArt) { + res = append(res, cc.NewErrReply(t, "You are not allowed to read news.")) + return res, err + } + res = append(res, cc.NewReply(t, NewField(fieldData, cc.Server.FlatNews))) return res, err } func HandleDownloadFile(cc *ClientConn, t *Transaction) (res []Transaction, err error) { + if !authorize(cc.Account.Access, accessDownloadFile) { + res = append(res, cc.NewErrReply(t, "You are not allowed to download files.")) + return res, err + } + fileName := t.GetField(fieldFileName).Data filePath := t.GetField(fieldFilePath).Data diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index e1dc8cb..48e0f0e 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -438,7 +438,7 @@ func TestHandleChatSend(t *testing.T) { Flags: 0x00, IsReply: 0x00, Type: []byte{0, 0x6a}, - ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ NewField(fieldData, []byte("\r*** Testy McTest performed action")), @@ -449,7 +449,7 @@ func TestHandleChatSend(t *testing.T) { Flags: 0x00, IsReply: 0x00, Type: []byte{0, 0x6a}, - ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1) + ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ NewField(fieldData, []byte("\r*** Testy McTest performed action")), @@ -509,6 +509,89 @@ func TestHandleChatSend(t *testing.T) { }, wantErr: false, }, + { + name: "only sends private chat msg to members of private chat", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() *[]byte { + var bits accessBitmap + bits.Set(accessSendChat) + access := bits[:] + return &access + }(), + }, + UserName: []byte{0x00, 0x01}, + Server: &Server{ + PrivateChats: map[uint32]*PrivateChat{ + uint32(1): { + ClientConn: map[uint16]*ClientConn{ + uint16(1): { + ID: &[]byte{0, 1}, + }, + uint16(2): { + ID: &[]byte{0, 2}, + }, + }, + }, + }, + Clients: map[uint16]*ClientConn{ + uint16(1): { + Account: &Account{ + Access: &[]byte{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}, + }, + ID: &[]byte{0, 2}, + }, + uint16(3): { + Account: &Account{ + Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0}, + }, + ID: &[]byte{0, 3}, + }, + }, + }, + }, + t: &Transaction{ + Fields: []Field{ + NewField(fieldData, []byte("hai")), + NewField(fieldChatID, []byte{0, 0, 0, 1}), + }, + }, + }, + want: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x6a}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldChatID, []byte{0, 0, 0, 1}), + NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}), + }, + }, + { + clientID: &[]byte{0, 2}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x6a}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldChatID, []byte{0, 0, 0, 1}), + NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}), + }, + }, + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1342,3 +1425,309 @@ func TestHandleDeleteUser(t *testing.T) { }) } } + +func TestHandleGetMsgs(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "returns news data", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() *[]byte { + var bits accessBitmap + bits.Set(accessNewsReadArt) + access := bits[:] + return &access + }(), + }, + Server: &Server{ + FlatNews: []byte("TEST"), + }, + }, + t: NewTransaction( + tranGetMsgs, &[]byte{0, 1}, + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x65}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte("TEST")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when user does not have required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() *[]byte { + var bits accessBitmap + access := bits[:] + return &access + }(), + }, + Server: &Server{ + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranGetMsgs, &[]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("You are not allowed to read news.")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleGetMsgs(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleGetMsgs(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleNewUser(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() *[]byte { + var bits accessBitmap + access := bits[:] + return &access + }(), + }, + Server: &Server{ + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranNewUser, &[]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("You are not allowed to create new accounts.")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleNewUser(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleNewUser(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleListUsers(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() *[]byte { + var bits accessBitmap + access := bits[:] + return &access + }(), + }, + Server: &Server{ + Accounts: map[string]*Account{}, + }, + }, + t: NewTransaction( + tranNewUser, &[]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("You are not allowed to view accounts.")), + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleListUsers(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleListUsers(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} + +func TestHandleDownloadFile(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() *[]byte { + var bits accessBitmap + access := bits[:] + return &access + }(), + }, + Server: &Server{}, + }, + t: NewTransaction(tranDownloadFile, &[]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("You are not allowed to download files.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "with a valid file", + args: args{ + cc: &ClientConn{ + Transfers: make(map[int][]*FileTransfer), + Account: &Account{ + Access: func() *[]byte { + var bits accessBitmap + bits.Set(accessDownloadFile) + access := bits[:] + return &access + }(), + }, + Server: &Server{ + FileTransfers: make(map[uint32]*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.txt")), + NewField(fieldFilePath, []byte{0x0, 0x00}), + ), + }, + 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, 0x00, 0xa5}), + NewField(fieldFileSize, []byte{0x00, 0x00, 0x00, 0x17}), + }, + }, + }, + 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 + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} diff --git a/hotline/transfer_test.go b/hotline/transfer_test.go index 63e22c0..23b30ef 100644 --- a/hotline/transfer_test.go +++ b/hotline/transfer_test.go @@ -1,6 +1,12 @@ package hotline -import "testing" +import ( + "bytes" + "fmt" + "github.com/stretchr/testify/assert" + "io" + "testing" +) func TestTransfer_Read(t *testing.T) { type fields struct { @@ -96,3 +102,57 @@ func TestTransfer_Read(t *testing.T) { }) } } + +func Test_receiveFile(t *testing.T) { + type args struct { + conn io.Reader + } + tests := []struct { + name string + args args + wantTargetFile []byte + wantResForkFile []byte + wantErr assert.ErrorAssertionFunc + }{ + { + name: "transfers file", + args: args{ + conn: func() io.Reader { + testFile := flattenedFileObject{ + FlatFileHeader: NewFlatFileHeader(), + FlatFileInformationForkHeader: FlatFileInformationForkHeader{}, + FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8)), + FlatFileDataForkHeader: FlatFileDataForkHeader{ + ForkType: [4]byte{0x4d, 0x41, 0x43, 0x52}, // DATA + CompressionType: [4]byte{0, 0, 0, 0}, + RSVD: [4]byte{0, 0, 0, 0}, + DataSize: [4]byte{0x00, 0x00, 0x00, 0x03}, + }, + FileData: nil, + } + fakeFileData := []byte{1, 2, 3} + b := testFile.BinaryMarshal() + b = append(b, fakeFileData...) + return bytes.NewReader(b) + }(), + }, + wantTargetFile: []byte{1, 2, 3}, + wantResForkFile: []byte(nil), + + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + targetFile := &bytes.Buffer{} + resForkFile := &bytes.Buffer{} + err := receiveFile(tt.args.conn, targetFile, resForkFile) + if !tt.wantErr(t, err, fmt.Sprintf("receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile)) { + return + } + + assert.Equalf(t, tt.wantTargetFile, targetFile.Bytes(), "receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile) + assert.Equalf(t, tt.wantResForkFile, resForkFile.Bytes(), "receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile) + }) + } +}