+
+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)
+ })
+ }
+}