blob: 2728524102cfcaa581668061a4c92b0dd059d164 [file] [log] [blame]
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package report
import (
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/vulndb/internal/gitrepo"
)
var (
fname1 = "data/reports/GO-9999-0001.yaml"
r1 = Report{
ID: "GO-9999-0001",
Modules: []*Module{
{Module: "std"},
},
CVEMetadata: &CVEMeta{
ID: "CVE-9999-0001",
},
}
fname2 = "data/excluded/GO-9999-0002.yaml"
r2 = Report{
ID: "GO-9999-0002",
Modules: []*Module{
{Module: "example.com/fake/module"},
},
CVEMetadata: &CVEMeta{
ID: "CVE-9999-0002",
},
Excluded: ExcludedEffectivelyPrivate,
}
fname4 = "data/reports/GO-9999-0004.yaml"
r4 = Report{
ID: "GO-9999-0004",
Modules: []*Module{
{Module: "example.com/another/module"},
},
GHSAs: []string{
"GHSA-9999-abcd-efgh",
},
}
fname5 = "data/reports/GO-9999-0005.yaml"
r5 = Report{
ID: "GO-9999-0005",
Modules: []*Module{
{Module: "example.com/adiff/module"},
},
CVEs: []string{"CVE-9999-0005"},
}
fname6 = "data/reports/GO-9999-0006.yaml"
r6 = Report{
ID: "GO-9999-0006",
Modules: []*Module{
{Module: "example.com/another/module"},
},
GHSAs: []string{"GHSA-9999-abcd-efgh"},
}
txtarFile = filepath.Join("testdata", "repo.txtar")
)
func TestList(t *testing.T) {
repo, err := gitrepo.ReadTxtarRepo(txtarFile, time.Now())
if err != nil {
t.Fatal(err)
}
rc, err := NewClient(repo)
if err != nil {
t.Fatal(err)
}
got := rc.List()
want := []*Report{&r1, &r2, &r4, &r5, &r6}
byID := func(a, b *Report) bool { return a.ID < b.ID }
if diff := cmp.Diff(got, want, cmpopts.SortSlices(byID)); diff != "" {
t.Errorf("mismatch (-got, +want): %s", diff)
}
}
func TestXRef(t *testing.T) {
repo, err := gitrepo.ReadTxtarRepo(txtarFile, time.Now())
if err != nil {
t.Fatal(err)
}
rc, err := NewClient(repo)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
r *Report
wantXrefs *Xrefs
wantMatches map[string][]string
}{
{
name: "No matches",
r: &Report{
Modules: []*Module{
{Module: "example.com/unused/module"},
},
CVEMetadata: &CVEMeta{
ID: "CVE-9999-0003",
},
},
wantXrefs: &Xrefs{
Aliases: map[string][]*File{},
Modules: map[string][]*File{},
},
},
{
name: "Ignores std lib modules",
r: &Report{
Modules: []*Module{
{Module: "std"},
},
CVEs: []string{"CVE-9999-0003"},
},
wantXrefs: &Xrefs{
Aliases: map[string][]*File{},
Modules: map[string][]*File{},
},
},
{
name: "Match on CVE (ignores std module)",
r: &Report{
Modules: []*Module{
{Module: "std"},
},
CVEs: []string{"CVE-9999-0001"},
},
wantXrefs: &Xrefs{
Aliases: map[string][]*File{
"CVE-9999-0001": {
{Filename: fname1, IssNum: 1, Report: &r1},
},
},
Modules: map[string][]*File{},
},
},
{
name: "Match on GHSA & module",
r: &r4,
wantXrefs: &Xrefs{
Aliases: map[string][]*File{
"GHSA-9999-abcd-efgh": {
{Filename: fname6, IssNum: 6, Report: &r6},
},
},
Modules: map[string][]*File{
"example.com/another/module": {
{Filename: fname6, IssNum: 6, Report: &r6},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := rc.XRef(tt.r)
if diff := cmp.Diff(got, tt.wantXrefs); diff != "" {
t.Errorf("XRef(): mismatch (-got, +want): %s", diff)
}
})
}
}
func TestReportsByAliases(t *testing.T) {
repo, err := gitrepo.ReadTxtarRepo(txtarFile, time.Now())
if err != nil {
t.Fatal(err)
}
rc, err := NewClient(repo)
if err != nil {
t.Fatal(err)
}
got := rc.ReportsByAlias("CVE-9999-0001")
want := []*Report{&r1}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("ReportsByAliases() mismatch (-want, +got): %s", diff)
}
}
func TestAliasHasReport(t *testing.T) {
repo, err := gitrepo.ReadTxtarRepo(txtarFile, time.Now())
if err != nil {
t.Fatal(err)
}
rc, err := NewClient(repo)
if err != nil {
t.Fatal(err)
}
id := "CVE-9999-0001"
got := rc.AliasHasReport("CVE-9999-0001")
want := true
if got != want {
t.Errorf("AliasHasReport(%s) = %t, want %t", id, got, want)
}
}
func TestNewClient(t *testing.T) {
// Test that NewClient and NewTestClient have the same behavior.
repo, err := gitrepo.ReadTxtarRepo(txtarFile, time.Now())
if err != nil {
t.Fatal(err)
}
c, err := NewClient(repo)
if err != nil {
t.Fatal(err)
}
files := map[string]*Report{
fname1: &r1, fname2: &r2, fname4: &r4, fname5: &r5, fname6: &r6,
}
tc, err := NewTestClient(files)
if err != nil {
t.Fatal(err)
}
less := func(f1, f2 *File) bool {
return f1.ID < f2.ID
}
if diff := cmp.Diff(c, tc, cmp.AllowUnexported(Client{}),
cmpopts.SortSlices(less)); diff != "" {
t.Errorf("NewClient() / NewTestClient() mismatch (-New, +NewTest): %s", diff)
}
}