X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/22c599abc18895f73e96095f35b71cf3357d41b4..c5d9af5aa4d9fb20316be45ab1b775bcf61bcad5:/hotline/transaction_handlers_test.go diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index 3042da2..e62cce2 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -22,7 +22,7 @@ func TestHandleSetChatSubject(t *testing.T) { name: "sends chat subject to private chat members", args: args{ cc: &ClientConn{ - UserName: &[]byte{0x00, 0x01}, + UserName: []byte{0x00, 0x01}, Server: &Server{ PrivateChats: map[uint32]*PrivateChat{ uint32(1): { @@ -167,7 +167,7 @@ func TestHandleLeaveChat(t *testing.T) { }, }, }, - t: NewTransaction(tranDeleteUser,nil, NewField(fieldChatID, []byte{0, 0, 0, 1})), + t: NewTransaction(tranDeleteUser, nil, NewField(fieldChatID, []byte{0, 0, 0, 1})), }, want: []Transaction{ { @@ -201,7 +201,6 @@ func TestHandleLeaveChat(t *testing.T) { } } - func TestHandleGetUserNameList(t *testing.T) { type args struct { cc *ClientConn @@ -225,7 +224,7 @@ func TestHandleGetUserNameList(t *testing.T) { ID: &[]byte{0, 1}, Icon: &[]byte{0, 2}, Flags: &[]byte{0, 3}, - UserName: &[]byte{0, 4}, + UserName: []byte{0, 4}, }, }, }, @@ -283,7 +282,7 @@ func TestHandleChatSend(t *testing.T) { name: "sends chat msg transaction to all clients", args: args{ cc: &ClientConn{ - UserName: &[]byte{0x00, 0x01}, + UserName: []byte{0x00, 0x01}, Server: &Server{ Clients: map[uint16]*ClientConn{ uint16(1): { @@ -333,11 +332,66 @@ func TestHandleChatSend(t *testing.T) { }, wantErr: false, }, + { + name: "sends chat msg as emote if fieldChatOptions is set", + args: args{ + cc: &ClientConn{ + UserName: []byte("Testy McTest"), + Server: &Server{ + 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{255, 255, 255, 255, 255, 255, 255, 255}, + }, + ID: &[]byte{0, 2}, + }, + }, + }, + }, + t: &Transaction{ + Fields: []Field{ + NewField(fieldData, []byte("performed action")), + NewField(fieldChatOptions, []byte{0x00, 0x01}), + }, + }, + }, + want: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x6a}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte("\r*** Testy McTest performed action")), + }, + }, + { + clientID: &[]byte{0, 2}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x6a}, + ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1) + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte("\r*** Testy McTest performed action")), + }, + }, + }, + wantErr: false, + }, { name: "only sends chat msg to clients with accessReadChat permission", args: args{ cc: &ClientConn{ - UserName: &[]byte{0x00, 0x01}, + UserName: []byte{0x00, 0x01}, Server: &Server{ Clients: map[uint16]*ClientConn{ uint16(1): { @@ -392,3 +446,85 @@ func TestHandleChatSend(t *testing.T) { }) } } + +func TestHandleGetFileInfo(t *testing.T) { + rand.Seed(1) // reset seed between tests to make transaction IDs predictable + + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr bool + }{ + { + name: "returns expected fields when a valid file is requested", + args: args{ + cc: &ClientConn{ + ID: &[]byte{0x00, 0x01}, + Server: &Server{ + Config: &Config{ + FileRoot: "./test/config/Files/", + }, + }, + }, + t: NewTransaction( + tranGetFileInfo, nil, + NewField(fieldFileName, []byte("testfile.txt")), + NewField(fieldFilePath, []byte{0x00, 0x00}), + //NewField(fieldFilePath, []byte{ + // 0x00, 0x03, + // 0x00, 0x00, + // 0x04, + // 0x74, 0x65, 0x73, 0x74, + // 0x00, 0x00, + // 0x06, + // 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, + // + // 0x00, 0x00, + // 0x05, + // 0x46, 0x69, 0x6c, 0x65, 73}, + //), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0xce}, + ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldFileName, []byte("testfile.txt")), + NewField(fieldFileTypeString, []byte("TEXT")), + NewField(fieldFileCreatorString, []byte("TTXT")), + NewField(fieldFileComment, []byte("TODO")), + NewField(fieldFileType, []byte("TEXT")), + NewField(fieldFileCreateDate, []byte{0x07, 0x70, 0x00, 0x00, 0xba, 0x74, 0x24, 0x73}), + NewField(fieldFileModifyDate, []byte{0x07, 0x70, 0x00, 0x00, 0xba, 0x74, 0x24, 0x73}), + NewField(fieldFileSize, []byte{0x0, 0x0, 0x0, 0x17}), + }, + }, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rand.Seed(1) // reset seed between tests to make transaction IDs predictable + + gotRes, err := HandleGetFileInfo(tt.args.cc, tt.args.t) + if (err != nil) != tt.wantErr { + t.Errorf("HandleGetFileInfo() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !assert.Equal(t, tt.wantRes, gotRes) { + t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes) + } + }) + } +}