package exploit import ( "testing" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "google.golang.org/protobuf/testing/protocmp" epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits" ) func TestCache_Diff(t *testing.T) { for _, tc := range []struct { have []*State new []*epb.ExploitState diff []*epb.ExploitState }{ { have: nil, new: nil, diff: nil, }, { have: []*State{ {ID: "1", Version: 1}, }, new: []*epb.ExploitState{{ ExploitId: "1", Version: 1, File: nil, }}, diff: nil, }, { have: []*State{ {ID: "1", Version: 1}, }, new: []*epb.ExploitState{{ ExploitId: "2", Version: 1, File: nil, }}, diff: []*epb.ExploitState{{ ExploitId: "2", Version: 1, File: nil, }}, }, { have: []*State{ {ID: "1", Version: 1}, }, new: []*epb.ExploitState{{ ExploitId: "1", Version: 2, File: nil, }}, diff: []*epb.ExploitState{{ ExploitId: "1", Version: 2, File: nil, }}, }, { have: []*State{ {ID: "1", Version: 1}, {ID: "2", Version: 1}, }, new: []*epb.ExploitState{ { ExploitId: "1", Version: 2, File: nil, }, { ExploitId: "2", Version: 1, File: nil, }, { ExploitId: "3", Version: 1, File: nil, }, }, diff: []*epb.ExploitState{ { ExploitId: "1", Version: 2, File: nil, }, { ExploitId: "3", Version: 1, File: nil, }, }, }, { have: []*State{ {ID: "1", Version: 1}, {ID: "2", Version: 1}, }, new: nil, diff: nil, }, { have: []*State{ {ID: "1", Version: 1, Endless: false}, }, new: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: true}, }, }, diff: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: true}, }, }, }, { have: []*State{ {ID: "1", Version: 1, Endless: true}, }, new: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: false}, }, }, diff: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: false}, }, }, }, { have: nil, new: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: true}, }, }, diff: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: true}, }, }, }, { have: nil, new: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: false}, }, }, diff: []*epb.ExploitState{ { ExploitId: "1", Version: 2, Config: &epb.ExploitConfiguration{Endless: false}, }, }, }, } { c := NewCache() c.Update(tc.have) got := c.Diff(tc.new) if diff := cmp.Diff(tc.diff, got, protocmp.Transform(), cmpopts.EquateEmpty()); diff != "" { t.Errorf("Diff() mismatch (-want +got):\n%s", diff) } } } func TestCache_Exploits(t *testing.T) { c := NewCache() data := []*State{{ ID: "1", Version: 1, Dir: "/test", Path: "kek", }} c.Update(data) if diff := cmp.Diff(data, c.Exploits(), cmpopts.EquateEmpty()); diff != "" { t.Errorf("Diff() mismatch (-want +got):\n%s", diff) } }