X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/76d0c1f61d5981603389e6267bf62636f34bef1f..f8e4cd540b87de3e308ec18a2b040b284a741522:/hotline/transaction_handlers_test.go?ds=sidebyside diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index 89ee9fc..09cb1cb 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "io" "io/fs" - "math/rand" "os" "path/filepath" "strings" @@ -108,15 +108,13 @@ func TestHandleSetChatSubject(t *testing.T) { }, } for _, tt := range tests { - rand.Seed(1) // reset seed between tests to make transaction IDs predictable - t.Run(tt.name, func(t *testing.T) { got, err := HandleSetChatSubject(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr) return } - if !assert.Equal(t, tt.want, got) { + if !tranAssertEqual(t, tt.want, got) { t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want) } }) @@ -194,14 +192,13 @@ func TestHandleLeaveChat(t *testing.T) { }, } for _, tt := range tests { - rand.Seed(1) t.Run(tt.name, func(t *testing.T) { got, err := HandleLeaveChat(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr) return } - if !assert.Equal(t, tt.want, got) { + if !tranAssertEqual(t, tt.want, got) { t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want) } }) @@ -723,8 +720,6 @@ 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 @@ -768,7 +763,6 @@ func TestHandleGetFileInfo(t *testing.T) { NewField(FieldFileName, []byte("testfile.txt")), NewField(FieldFileTypeString, []byte("Text File")), NewField(FieldFileCreatorString, []byte("ttxt")), - NewField(FieldFileComment, []byte{}), NewField(FieldFileType, []byte("TEXT")), NewField(FieldFileCreateDate, make([]byte, 8)), NewField(FieldFileModifyDate, make([]byte, 8)), @@ -781,8 +775,6 @@ func TestHandleGetFileInfo(t *testing.T) { } 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) @@ -791,9 +783,10 @@ func TestHandleGetFileInfo(t *testing.T) { // Clear the fileWrapper timestamp fields to work around problems running the tests in multiple timezones // TODO: revisit how to test this by mocking the stat calls + gotRes[0].Fields[4].Data = make([]byte, 8) gotRes[0].Fields[5].Data = make([]byte, 8) - gotRes[0].Fields[6].Data = make([]byte, 8) - if !assert.Equal(t, tt.wantRes, gotRes) { + + if !tranAssertEqual(t, tt.wantRes, gotRes) { t.Errorf("HandleGetFileInfo() gotRes = %v, want %v", gotRes, tt.wantRes) } }) @@ -1166,7 +1159,6 @@ func TestHandleUploadFile(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - rand.Seed(1) gotRes, err := HandleUploadFile(tt.args.cc, tt.args.t) if (err != nil) != tt.wantErr { t.Errorf("HandleUploadFile() error = %v, wantErr %v", err, tt.wantErr) @@ -2858,9 +2850,9 @@ func TestHandleGetFileNameList(t *testing.T) { NameScript: [2]byte{}, NameSize: [2]byte{0, 0x0b}, }, - name: []byte("testfile-1k"), + Name: []byte("testfile-1k"), } - b, _ := fnwi.MarshalBinary() + b, _ := io.ReadAll(&fnwi) return b }(), ), @@ -3191,10 +3183,8 @@ func TestHandleDelNewsItem(t *testing.T) { Server: &Server{ ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ "test": { - Type: []byte{0, 3}, - Count: nil, - NameSize: 0, - Name: "zz", + Type: [2]byte{0, 3}, + Name: "zz", }, }}, }, @@ -3237,10 +3227,8 @@ func TestHandleDelNewsItem(t *testing.T) { Server: &Server{ ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ "testcat": { - Type: []byte{0, 2}, - Count: nil, - NameSize: 0, - Name: "test", + Type: [2]byte{0, 2}, + Name: "test", }, }}, }, @@ -3293,10 +3281,8 @@ func TestHandleDelNewsItem(t *testing.T) { }(), ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ "testcat": { - Type: []byte{0, 2}, - Count: nil, - NameSize: 0, - Name: "test", + Type: [2]byte{0, 2}, + Name: "test", }, }}, }, @@ -3338,72 +3324,6 @@ func TestHandleDelNewsItem(t *testing.T) { } } -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 @@ -3664,7 +3584,6 @@ func TestHandleInviteNewChat(t *testing.T) { } 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 @@ -3771,6 +3690,78 @@ func TestHandleGetNewsArtNameList(t *testing.T) { }, wantErr: assert.NoError, }, + { + 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, + &[]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{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + 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, + }, + ), + }, + }, + }, + wantErr: assert.NoError, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -3847,11 +3838,9 @@ func TestHandleNewNewsFldr(t *testing.T) { }(), ThreadedNews: &ThreadedNews{Categories: map[string]NewsCategoryListData15{ "test": { - Type: []byte{0, 2}, - Count: nil, - NameSize: 0, - Name: "test", - SubCats: make(map[string]NewsCategoryListData15), + Type: [2]byte{0, 2}, + Name: "test", + SubCats: make(map[string]NewsCategoryListData15), }, }}, }, @@ -3883,7 +3872,7 @@ func TestHandleNewNewsFldr(t *testing.T) { wantErr: assert.NoError, }, //{ - // name: "when there is an error writing the threaded news file", + // Name: "when there is an error writing the threaded news file", // args: args{ // cc: &ClientConn{ // Account: &Account{ @@ -3951,3 +3940,27 @@ func TestHandleNewNewsFldr(t *testing.T) { }) } } + +func TestHandleDownloadBanner(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + // TODO: Add test cases. + } + 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 + } + assert.Equalf(t, tt.wantRes, gotRes, "HandleDownloadBanner(%v, %v)", tt.args.cc, tt.args.t) + }) + } +}