+
+func TestHandleTranOldPostNews(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ }{
+ {
+ 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, [2]byte{0, 1},
+ NewField(FieldData, []byte("hai")),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ ErrorCode: [4]byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(FieldError, []byte("You are not allowed to post news.")),
+ },
+ },
+ },
+ },
+ {
+ 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, [2]byte{0, 1},
+ NewField(FieldData, []byte("hai")),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes := HandleTranOldPostNews(tt.args.cc, &tt.args.t)
+
+ 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
+ }{
+ {
+ 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, [2]byte{0, 1}),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ ErrorCode: [4]byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(FieldError, []byte("You are not allowed to request private chat.")),
+ },
+ },
+ },
+ },
+ {
+ name: "when userA invites userB to new private chat",
+ args: args{
+ cc: &ClientConn{
+ ID: [2]byte{0, 1},
+ Account: &Account{
+ Access: func() accessBitmap {
+ var bits accessBitmap
+ bits.Set(accessOpenChat)
+ return bits
+ }(),
+ },
+ UserName: []byte("UserA"),
+ Icon: []byte{0, 1},
+ Flags: [2]byte{0, 0},
+ Server: &Server{
+ Clients: map[[2]byte]*ClientConn{
+ [2]byte{0, 2}: {
+ ID: [2]byte{0, 2},
+ UserName: []byte("UserB"),
+ },
+ },
+ PrivateChats: make(map[[4]byte]*PrivateChat),
+ },
+ },
+ t: NewTransaction(
+ TranInviteNewChat, [2]byte{0, 1},
+ NewField(FieldUserID, []byte{0, 2}),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ clientID: [2]byte{0, 2},
+ Type: [2]byte{0, 0x71},
+ Fields: []Field{
+ NewField(FieldChatID, []byte{0x52, 0xfd, 0xfc, 0x07}),
+ NewField(FieldUserName, []byte("UserA")),
+ NewField(FieldUserID, []byte{0, 1}),
+ },
+ },
+
+ {
+ clientID: [2]byte{0, 1},
+ IsReply: 0x01,
+ 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}),
+ },
+ },
+ },
+ },
+ {
+ name: "when userA invites userB to new private chat, but UserB has refuse private chat enabled",
+ args: args{
+ cc: &ClientConn{
+ ID: [2]byte{0, 1},
+ Account: &Account{
+ Access: func() accessBitmap {
+ var bits accessBitmap
+ bits.Set(accessOpenChat)
+ return bits
+ }(),
+ },
+ UserName: []byte("UserA"),
+ Icon: []byte{0, 1},
+ Flags: [2]byte{0, 0},
+ Server: &Server{
+ Clients: map[[2]byte]*ClientConn{
+ [2]byte{0, 2}: {
+ ID: [2]byte{0, 2},
+ UserName: []byte("UserB"),
+ Flags: [2]byte{255, 255},
+ },
+ },
+ PrivateChats: make(map[[4]byte]*PrivateChat),
+ },
+ },
+ t: NewTransaction(
+ TranInviteNewChat, [2]byte{0, 1},
+ NewField(FieldUserID, []byte{0, 2}),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ clientID: [2]byte{0, 1},
+ Type: [2]byte{0, 0x68},
+ 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: [2]byte{0, 1},
+ IsReply: 0x01,
+ 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}),
+ },
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes := HandleInviteNewChat(tt.args.cc, &tt.args.t)
+
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}
+
+func TestHandleGetNewsArtData(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ }{
+ {
+ 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(
+ TranGetNewsArtData, [2]byte{0, 1},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ ErrorCode: [4]byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(FieldError, []byte("You are not allowed to read news.")),
+ },
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes := HandleGetNewsArtData(tt.args.cc, &tt.args.t)
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}
+
+func TestHandleGetNewsArtNameList(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ }{
+ {
+ 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(
+ TranGetNewsArtNameList, [2]byte{0, 1},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: [2]byte{0, 0},
+ ErrorCode: [4]byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(FieldError, []byte("You are not allowed to read news.")),
+ },
+ },
+ },
+ },
+ {
+ name: "when user has required access",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() accessBitmap {
+ var bits accessBitmap
+ bits.Set(accessNewsReadArt)
+ return bits
+ }(),
+ },
+ Server: &Server{
+ ThreadedNews: &ThreadedNews{
+ Categories: map[string]NewsCategoryListData15{
+ "Example Category": {
+ Type: [2]byte{0, 2},
+ Name: "",
+ Articles: map[uint32]*NewsArtData{
+ uint32(1): {
+ Title: "testTitle",
+ Poster: "testPoster",
+ Data: "testBody",
+ },
+ },
+ SubCats: nil,
+ GUID: [16]byte{},
+ AddSN: [4]byte{},
+ DeleteSN: [4]byte{},
+ },
+ },
+ },
+
+ //Accounts: map[string]*Account{
+ // "guest": {
+ // Name: "guest",
+ // Login: "guest",
+ // Password: "zz",
+ // Access: accessBitmap{255, 255, 255, 255, 255, 255, 255, 255},
+ // },
+ //},
+ },
+ },
+ t: NewTransaction(
+ TranGetNewsArtNameList,
+ [2]byte{0, 1},
+ // 00000000 00 01 00 00 10 45 78 61 6d 70 6c 65 20 43 61 74 |.....Example Cat|
+ // 00000010 65 67 6f 72 79 |egory|
+ NewField(FieldNewsPath, []byte{
+ 0x00, 0x01, 0x00, 0x00, 0x10, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x43, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x79,
+ }),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ Fields: []Field{
+ NewField(FieldNewsArtListData, []byte{
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+ 0x09, 0x74, 0x65, 0x73, 0x74, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x50,
+ 0x6f, 0x73, 0x74, 0x65, 0x72, 0x0a, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e,
+ 0x00, 0x08,
+ },
+ ),
+ },
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes := HandleGetNewsArtNameList(tt.args.cc, &tt.args.t)
+
+ tranAssertEqual(t, tt.wantRes, gotRes)
+ })
+ }
+}
+
+func TestHandleNewNewsFldr(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ }{
+ {
+ 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(
+ TranGetNewsArtNameList, [2]byte{0, 1},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: [2]byte{0, 0},
+ ErrorCode: [4]byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(FieldError, []byte("You are not allowed to create news folders.")),
+ },
+ },
+ },
+ },
+ {
+ name: "with a valid request",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() accessBitmap {
+ var bits accessBitmap
+ bits.Set(accessNewsCreateFldr)
+ return bits
+ }(),
+ },
+ logger: NewTestLogger(),
+ ID: [2]byte{0, 1},
+ Server: &Server{
+ ConfigDir: "/fakeConfigRoot",
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil)
+ return mfs
+ }(),
+ ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
+ "test": {
+ Type: [2]byte{0, 2},
+ Name: "test",
+ SubCats: make(map[string]NewsCategoryListData15),
+ },
+ }},
+ },
+ },
+ t: NewTransaction(
+ TranGetNewsArtNameList, [2]byte{0, 1},
+ NewField(FieldFileName, []byte("testFolder")),
+ NewField(FieldNewsPath,
+ []byte{
+ 0, 1,
+ 0, 0,
+ 4,
+ 0x74, 0x65, 0x73, 0x74,
+ },
+ ),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ clientID: [2]byte{0, 1},
+ IsReply: 0x01,
+ Fields: []Field{},
+ },
+ },
+ },
+ //{
+ // Name: "when there is an error writing the threaded news file",
+ // args: args{
+ // cc: &ClientConn{
+ // Account: &Account{
+ // Access: func() accessBitmap {
+ // var bits accessBitmap
+ // bits.Set(accessNewsCreateFldr)
+ // return bits
+ // }(),
+ // },
+ // logger: NewTestLogger(),
+ // ID: [2]byte{0, 1},
+ // Server: &Server{
+ // ConfigDir: "/fakeConfigRoot",
+ // FS: func() *MockFileStore {
+ // mfs := &MockFileStore{}
+ // mfs.On("WriteFile", "/fakeConfigRoot/ThreadedNews.yaml", mock.Anything, mock.Anything).Return(os.ErrNotExist)
+ // return mfs
+ // }(),
+ // ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{
+ // "test": {
+ // Type: []byte{0, 2},
+ // Count: nil,
+ // NameSize: 0,
+ // Name: "test",
+ // SubCats: make(map[string]NewsCategoryListData15),
+ // },
+ // }},
+ // },
+ // },
+ // t: NewTransaction(
+ // TranGetNewsArtNameList, [2]byte{0, 1},
+ // NewField(FieldFileName, []byte("testFolder")),
+ // NewField(FieldNewsPath,
+ // []byte{
+ // 0, 1,
+ // 0, 0,
+ // 4,
+ // 0x74, 0x65, 0x73, 0x74,
+ // },
+ // ),
+ // ),
+ // },
+ // wantRes: []Transaction{
+ // {
+ // clientID: [2]byte{0, 1},
+ // Flags: 0x00,
+ // IsReply: 0x01,
+ // Type: [2]byte{0, 0},
+ // ErrorCode: [4]byte{0, 0, 0, 1},
+ // Fields: []Field{
+ // NewField(FieldError, []byte("Error creating news folder.")),
+ // },
+ // },
+ // },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes := HandleNewNewsFldr(tt.args.cc, &tt.args.t)
+
+ 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
+ }{
+ // TODO: Add test cases.
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotRes := HandleDownloadBanner(tt.args.cc, &tt.args.t)
+
+ assert.Equalf(t, tt.wantRes, gotRes, "HandleDownloadBanner(%v, %v)", tt.args.cc, &tt.args.t)
+ })
+ }
+}
+
+func TestHandlePostNewsArt(t *testing.T) {
+ type args struct {
+ cc *ClientConn
+ t Transaction
+ }
+ tests := []struct {
+ name string
+ args args
+ wantRes []Transaction
+ }{
+ {
+ name: "without required permission",
+ args: args{
+ cc: &ClientConn{
+ Account: &Account{
+ Access: func() accessBitmap {
+ var bits accessBitmap
+ return bits
+ }(),
+ },
+ },
+ t: NewTransaction(
+ TranPostNewsArt,
+ [2]byte{0, 0},
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ ErrorCode: [4]byte{0, 0, 0, 1},
+ Fields: []Field{
+ NewField(FieldError, []byte("You are not allowed to post news articles.")),
+ },
+ },
+ },
+ },
+ {
+ name: "with required permission",
+ args: args{
+ cc: &ClientConn{
+ Server: &Server{
+ FS: func() *MockFileStore {
+ mfs := &MockFileStore{}
+ mfs.On("WriteFile", "ThreadedNews.yaml", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist)
+ return mfs
+ }(),
+ mux: sync.Mutex{},
+ threadedNewsMux: sync.Mutex{},
+ ThreadedNews: &ThreadedNews{
+ Categories: map[string]NewsCategoryListData15{
+ "www": {
+ Type: [2]byte{},
+ Name: "www",
+ Articles: map[uint32]*NewsArtData{},
+ SubCats: nil,
+ GUID: [16]byte{},
+ AddSN: [4]byte{},
+ DeleteSN: [4]byte{},
+ readOffset: 0,
+ },
+ },
+ },
+ },
+ Account: &Account{
+ Access: func() accessBitmap {
+ var bits accessBitmap
+ bits.Set(accessNewsPostArt)
+ return bits
+ }(),
+ },
+ },
+ t: NewTransaction(
+ TranPostNewsArt,
+ [2]byte{0, 0},
+ NewField(FieldNewsPath, []byte{0x00, 0x01, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77}),
+ NewField(FieldNewsArtID, []byte{0x00, 0x00, 0x00, 0x00}),
+ ),
+ },
+ wantRes: []Transaction{
+ {
+ IsReply: 0x01,
+ ErrorCode: [4]byte{0, 0, 0, 0},
+ Fields: []Field{},
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ tranAssertEqual(t, tt.wantRes, HandlePostNewsArt(tt.args.cc, &tt.args.t))
+ })
+ }
+}