Handler: HandleTranAgreed,
},
tranChatSend: {
- Access: accessSendChat,
- DenyMsg: "You are not allowed to participate in chat.",
+ Access: accessAlwaysAllow,
Handler: HandleChatSend,
Name: "tranChatSend",
RequiredFields: []requiredField{
Handler: HandleDeleteFile,
},
tranDeleteUser: {
- Access: accessDeleteUser,
- DenyMsg: "You are not allowed to delete accounts.",
+ Access: accessAlwaysAllow,
Name: "tranDeleteUser",
Handler: HandleDeleteUser,
},
Handler: HandleDisconnectUser,
},
tranDownloadFile: {
- Access: accessDownloadFile,
- DenyMsg: "You are not allowed to download files.",
+ Access: accessAlwaysAllow,
Name: "tranDownloadFile",
Handler: HandleDownloadFile,
},
Handler: HandleGetFileNameList,
},
tranGetMsgs: {
- Access: accessNewsReadArt,
- DenyMsg: "You are not allowed to read news.",
+ Access: accessAlwaysAllow,
Name: "tranGetMsgs",
Handler: HandleGetMsgs,
},
Handler: HandleGetNewsCatNameList,
},
tranGetUser: {
- Access: accessOpenUser,
- DenyMsg: "You are not allowed to view accounts.",
+ Access: accessAlwaysAllow,
Name: "tranGetUser",
Handler: HandleGetUser,
},
Name: "tranJoinChat",
Handler: HandleLeaveChat,
},
-
tranListUsers: {
- Access: accessOpenUser,
- DenyMsg: "You are not allowed to view accounts.",
+ Access: accessAlwaysAllow,
Name: "tranListUsers",
Handler: HandleListUsers,
},
Handler: HandleNewNewsFldr,
},
tranNewUser: {
- Access: accessCreateUser,
- DenyMsg: "You are not allowed to create new accounts.",
+ Access: accessAlwaysAllow,
Name: "tranNewUser",
Handler: HandleNewUser,
},
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,
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
}
}
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 {
// 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
// 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
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")),
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")),
},
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) {
})
}
}
+
+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)
+ })
+ }
+}
package hotline
-import "testing"
+import (
+ "bytes"
+ "fmt"
+ "github.com/stretchr/testify/assert"
+ "io"
+ "testing"
+)
func TestTransfer_Read(t *testing.T) {
type fields struct {
})
}
}
+
+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)
+ })
+ }
+}