forked from quay/claircore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher_test.go
165 lines (152 loc) · 3.7 KB
/
matcher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package rhel
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/datastore/postgres"
"github.com/quay/claircore/internal/matcher"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/libvuln/updates"
"github.com/quay/claircore/pkg/ctxlock"
"github.com/quay/claircore/test/integration"
pgtest "github.com/quay/claircore/test/postgres"
)
func TestMain(m *testing.M) {
var c int
defer func() { os.Exit(c) }()
defer integration.DBSetup()()
c = m.Run()
}
func serveOVAL(t *testing.T) (string, *http.Client) {
srv := httptest.NewServer(http.FileServer(http.Dir("testdata")))
t.Cleanup(srv.Close)
return srv.URL, srv.Client()
}
func TestMatcherIntegration(t *testing.T) {
t.Parallel()
integration.NeedDB(t)
ctx := zlog.Test(context.Background(), t)
pool := pgtest.TestMatcherDB(ctx, t)
store := postgres.NewMatcherStore(pool)
m := &Matcher{}
fs, err := filepath.Glob("testdata/*.xml")
if err != nil {
t.Error(err)
}
root, c := serveOVAL(t)
locks, err := ctxlock.New(ctx, pool)
if err != nil {
t.Error(err)
return
}
defer locks.Close(ctx)
facs := make(map[string]driver.UpdaterSetFactory, len(fs))
for _, fn := range fs {
u, err := NewUpdater(
fmt.Sprintf("test-updater-%s", filepath.Base(fn)),
1,
root+"/"+filepath.Base(fn),
false,
)
if err != nil {
t.Error(err)
continue
}
u.Configure(ctx, func(v interface{}) error { return nil }, c)
if err != nil {
t.Error(err)
continue
}
s := driver.NewUpdaterSet()
if err := s.Add(u); err != nil {
t.Error(err)
continue
}
facs[u.Name()] = driver.StaticSet(s)
}
mgr, err := updates.NewManager(ctx, store, locks, c, updates.WithFactories(facs))
if err != nil {
t.Error(err)
}
// force update
if err := mgr.Run(ctx); err != nil {
t.Error(err)
}
f, err := os.Open(filepath.Join("testdata", "rhel-report.json"))
if err != nil {
t.Fatalf("%v", err)
}
defer f.Close()
var ir claircore.IndexReport
if err := json.NewDecoder(f).Decode(&ir); err != nil {
t.Fatalf("failed to decode IndexReport: %v", err)
}
vr, err := matcher.Match(ctx, &ir, []driver.Matcher{m}, store)
if err != nil {
t.Fatal(err)
}
if err := json.NewEncoder(io.Discard).Encode(&vr); err != nil {
t.Fatalf("failed to marshal VR: %v", err)
}
}
type vulnerableTestCase struct {
ir *claircore.IndexRecord
v *claircore.Vulnerability
name string
want bool
}
func TestVulnerable(t *testing.T) {
record := &claircore.IndexRecord{
Package: &claircore.Package{
Version: "0.33.0-6.el8",
},
}
fixedVulnPast := &claircore.Vulnerability{
Package: &claircore.Package{
Version: "",
},
FixedInVersion: "0.33.0-5.el8",
}
fixedVulnCurrent := &claircore.Vulnerability{
Package: &claircore.Package{
Version: "",
},
FixedInVersion: "0.33.0-6.el8",
}
fixedVulnFuture := &claircore.Vulnerability{
Package: &claircore.Package{
Version: "",
},
FixedInVersion: "0.33.0-7.el8",
}
unfixedVuln := &claircore.Vulnerability{
Package: &claircore.Package{
Version: "",
},
FixedInVersion: "",
}
testCases := []vulnerableTestCase{
{ir: record, v: fixedVulnPast, want: false, name: "vuln fixed in past version"},
{ir: record, v: fixedVulnCurrent, want: false, name: "vuln fixed in current version"},
{ir: record, v: fixedVulnFuture, want: true, name: "outdated package"},
{ir: record, v: unfixedVuln, want: true, name: "unfixed vuln"},
}
m := &Matcher{}
for _, tc := range testCases {
got, err := m.Vulnerable(nil, tc.ir, tc.v)
if err != nil {
t.Error(err)
}
if tc.want != got {
t.Errorf("%q failed: want %t, got %t", tc.name, tc.want, got)
}
}
}