X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c6a3fa2cdbf7eb78964a5df7622482cdd8782d74..fa0552fe1f5dc7862e24073423aeed15f9fe4ffe:/hotline/transaction_handlers_test.go?ds=sidebyside diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index e379f09..4dee0b5 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "io/fs" "math/rand" "os" @@ -36,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}, }, @@ -52,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}, }, @@ -144,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}, }, @@ -160,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}, }, @@ -228,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, }, @@ -260,7 +261,7 @@ func TestHandleGetUserNameList(t *testing.T) { clientID: &[]byte{1, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 1}, + Type: []byte{0, 0}, ID: []byte{0, 0, 0, 1}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -306,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}, @@ -318,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}, }, @@ -363,15 +363,76 @@ func TestHandleChatSend(t *testing.T) { }, wantErr: false, }, + { + name: "treats Chat ID 00 00 00 00 as a public chat message", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessSendChat) + return bits + }(), + }, + UserName: []byte{0x00, 0x01}, + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(1): { + Account: &Account{ + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, + }, + ID: &[]byte{0, 1}, + }, + uint16(2): { + Account: &Account{ + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, + }, + ID: &[]byte{0, 2}, + }, + }, + }, + }, + t: &Transaction{ + Fields: []Field{ + NewField(fieldData, []byte("hai")), + NewField(fieldChatID, []byte{0, 0, 0, 0}), + }, + }, + }, + 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{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{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1) + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + 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, + }, { 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{ @@ -398,15 +459,14 @@ func TestHandleChatSend(t *testing.T) { wantErr: false, }, { - name: "sends chat msg as emote if fieldChatOptions is set", + name: "sends chat msg as emote if fieldChatOptions is set to 1", 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"), @@ -414,13 +474,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}, }, @@ -460,16 +520,77 @@ func TestHandleChatSend(t *testing.T) { }, wantErr: false, }, + { + name: "does not send chat msg as emote if fieldChatOptions is set to 0", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessSendChat) + return bits + }(), + }, + UserName: []byte("Testy McTest"), + Server: &Server{ + Clients: map[uint16]*ClientConn{ + uint16(1): { + Account: &Account{ + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, + }, + ID: &[]byte{0, 1}, + }, + uint16(2): { + Account: &Account{ + Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255}, + }, + ID: &[]byte{0, 2}, + }, + }, + }, + }, + t: &Transaction{ + Fields: []Field{ + NewField(fieldData, []byte("hello")), + NewField(fieldChatOptions, []byte{0x00, 0x00}), + }, + }, + }, + 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(fieldData, []byte("\r Testy McTest: hello")), + }, + }, + { + clientID: &[]byte{0, 2}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x6a}, + ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldData, []byte("\r Testy McTest: hello")), + }, + }, + }, + wantErr: false, + }, { name: "only sends chat msg to clients with accessReadChat permission", 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}, @@ -477,13 +598,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}, }, @@ -516,11 +640,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}, @@ -540,19 +663,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}, }, @@ -647,7 +770,7 @@ func TestHandleGetFileInfo(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xce}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -702,10 +825,9 @@ func TestHandleNewFolder(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, }, @@ -733,11 +855,10 @@ func TestHandleNewFolder(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessCreateFolder) - access := bits[:] - return &access + return bits }(), }, ID: &[]byte{0, 1}, @@ -769,7 +890,7 @@ func TestHandleNewFolder(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xcd}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) ErrorCode: []byte{0, 0, 0, 0}, }, @@ -781,11 +902,10 @@ func TestHandleNewFolder(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessCreateFolder) - access := bits[:] - return &access + return bits }(), }, ID: &[]byte{0, 1}, @@ -811,7 +931,7 @@ func TestHandleNewFolder(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xcd}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) ErrorCode: []byte{0, 0, 0, 0}, }, @@ -819,15 +939,14 @@ 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() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessCreateFolder) - access := bits[:] - return &access + return bits }(), }, ID: &[]byte{0, 1}, @@ -859,11 +978,10 @@ func TestHandleNewFolder(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessCreateFolder) - access := bits[:] - return &access + return bits }(), }, ID: &[]byte{0, 1}, @@ -889,7 +1007,7 @@ func TestHandleNewFolder(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xcd}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) ErrorCode: []byte{0, 0, 0, 0}, }, @@ -900,11 +1018,10 @@ func TestHandleNewFolder(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessCreateFolder) - access := bits[:] - return &access + return bits }(), }, ID: &[]byte{0, 1}, @@ -939,7 +1056,7 @@ func TestHandleNewFolder(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xcd}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1) ErrorCode: []byte{0, 0, 0, 0}, }, @@ -987,12 +1104,11 @@ func TestHandleUploadFile(t *testing.T) { FileUpload: {}, }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessUploadFile) bits.Set(accessUploadAnywhere) - access := bits[:] - return &access + return bits }(), }, }, @@ -1011,7 +1127,7 @@ func TestHandleUploadFile(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xcb}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -1026,10 +1142,9 @@ 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 }(), }, }, @@ -1091,11 +1206,10 @@ func TestHandleMakeAlias(t *testing.T) { 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{ @@ -1129,7 +1243,7 @@ func TestHandleMakeAlias(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xd1}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field(nil), @@ -1143,11 +1257,10 @@ func TestHandleMakeAlias(t *testing.T) { 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{ @@ -1197,10 +1310,9 @@ func TestHandleMakeAlias(t *testing.T) { cc: &ClientConn{ logger: NewTestLogger(), Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1273,11 +1385,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{ @@ -1286,7 +1397,7 @@ func TestHandleGetUser(t *testing.T) { Login: "guest", Name: "Guest", Password: "password", - Access: &[]byte{1}, + Access: accessBitmap{}, }, }, }, @@ -1300,14 +1411,14 @@ func TestHandleGetUser(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0x01, 0x60}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ 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}), }, }, }, @@ -1318,10 +1429,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{ @@ -1352,11 +1462,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{ @@ -1411,11 +1520,10 @@ func TestHandleDeleteUser(t *testing.T) { 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{ @@ -1424,7 +1532,7 @@ func TestHandleDeleteUser(t *testing.T) { Login: "testuser", Name: "Testy McTest", Password: "password", - Access: &[]byte{1}, + Access: accessBitmap{}, }, }, FS: func() *MockFileStore { @@ -1443,7 +1551,7 @@ func TestHandleDeleteUser(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0x1, 0x5f}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field(nil), @@ -1456,10 +1564,9 @@ func TestHandleDeleteUser(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{ @@ -1514,11 +1621,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{ @@ -1533,7 +1639,7 @@ func TestHandleGetMsgs(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0x65}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -1548,10 +1654,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{ @@ -1605,10 +1710,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{ @@ -1633,6 +1737,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) { @@ -1662,10 +1808,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{ @@ -1690,6 +1835,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{0, 0}, + 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) { @@ -1719,10 +1909,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{}, @@ -1751,11 +1940,10 @@ func TestHandleDownloadFile(t *testing.T) { FileDownload: {}, }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessDownloadFile) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -1778,7 +1966,7 @@ func TestHandleDownloadFile(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0x2}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -1798,11 +1986,10 @@ func TestHandleDownloadFile(t *testing.T) { 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{ @@ -1870,7 +2057,7 @@ func TestHandleDownloadFile(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0x2}, + Type: []byte{0, 0}, ID: []byte{0x9a, 0xcb, 0x04, 0x42}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -1916,10 +2103,9 @@ func TestHandleUpdateUser(t *testing.T) { Logger: NewTestLogger(), }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, }, @@ -1973,10 +2159,9 @@ func TestHandleUpdateUser(t *testing.T) { }, }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, }, @@ -2029,10 +2214,9 @@ func TestHandleUpdateUser(t *testing.T) { }, }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, }, @@ -2090,10 +2274,9 @@ func TestHandleDelNewsArt(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, }, @@ -2144,10 +2327,9 @@ func TestHandleDisconnectUser(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap - access := bits[:] - return &access + return bits }(), }, }, @@ -2179,22 +2361,20 @@ func TestHandleDisconnectUser(t *testing.T) { uint16(1): { Account: &Account{ Login: "unnamed", - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessCannotBeDiscon) - access := bits[:] - return &access + return bits }(), }, }, }, }, Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessDisconUser) - access := bits[:] - return &access + return bits }(), }, }, @@ -2241,16 +2421,54 @@ func TestHandleSendInstantMsg(t *testing.T) { 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}, }, }, }, @@ -2275,7 +2493,7 @@ func TestHandleSendInstantMsg(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0x0, 0x6c}, + Type: []byte{0, 0}, ID: []byte{0, 0, 0, 0}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field(nil), @@ -2287,11 +2505,19 @@ func TestHandleSendInstantMsg(t *testing.T) { 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"), @@ -2327,7 +2553,58 @@ func TestHandleSendInstantMsg(t *testing.T) { clientID: &[]byte{0, 1}, Flags: 0x00, IsReply: 0x01, - Type: []byte{0x0, 0x6c}, + Type: []byte{0, 0}, + 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{0, 0}, ID: []byte{0, 0, 0, 0}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field(nil), @@ -2364,10 +2641,9 @@ func TestHandleDeleteFile(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{ @@ -2424,11 +2700,10 @@ func TestHandleDeleteFile(t *testing.T) { args: args{ cc: &ClientConn{ Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessDeleteFile) - access := bits[:] - return &access + return bits }(), }, Server: &Server{ @@ -2475,7 +2750,7 @@ func TestHandleDeleteFile(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0x0, 0xcc}, + Type: []byte{0, 0}, ID: []byte{0x0, 0x0, 0x0, 0x0}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field(nil), @@ -2514,10 +2789,9 @@ func TestHandleGetFileNameList(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{ @@ -2579,7 +2853,7 @@ func TestHandleGetFileNameList(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0xc8}, + Type: []byte{0, 0}, ID: []byte{0, 0, 0, 0}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -2635,10 +2909,9 @@ func TestHandleGetClientInfoText(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{ @@ -2671,11 +2944,10 @@ func TestHandleGetClientInfoText(t *testing.T) { UserName: []byte("Testy McTest"), RemoteAddr: "1.2.3.4:12345", Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessGetClientInfo) - access := bits[:] - return &access + return bits }(), Name: "test", Login: "test", @@ -2687,11 +2959,10 @@ func TestHandleGetClientInfoText(t *testing.T) { UserName: []byte("Testy McTest"), RemoteAddr: "1.2.3.4:12345", Account: &Account{ - Access: func() *[]byte { + Access: func() accessBitmap { var bits accessBitmap bits.Set(accessGetClientInfo) - access := bits[:] - return &access + return bits }(), Name: "test", Login: "test", @@ -2715,7 +2986,7 @@ func TestHandleGetClientInfoText(t *testing.T) { { Flags: 0x00, IsReply: 0x01, - Type: []byte{0x1, 0x2f}, + Type: []byte{0, 0}, ID: []byte{0, 0, 0, 0}, ErrorCode: []byte{0, 0, 0, 0}, Fields: []Field{ @@ -2764,3 +3035,652 @@ None. }) } } + +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, 0}, + 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{0, 0}, + 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{0, 0}, + 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, 0}, + 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, 0}, + 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, 0}, + 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) + }) + } +}