-
Notifications
You must be signed in to change notification settings - Fork 62
/
all_test.go
184 lines (172 loc) · 4.89 KB
/
all_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2021 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.
//go:build go1.17 && !windows
// +build go1.17,!windows
package main
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/vulndb/internal/cve5"
"golang.org/x/vulndb/internal/osvutils"
"golang.org/x/vulndb/internal/proxy"
"golang.org/x/vulndb/internal/report"
"golang.org/x/vulndb/internal/triage/priority"
)
func TestChecksBash(t *testing.T) {
bash, err := exec.LookPath("bash")
if err != nil {
t.Skipf("skipping: %v", err)
}
// In short mode (used by presubmit checks), only do offline checks.
var cmd *exec.Cmd
if testing.Short() {
cmd = exec.Command(bash, "./checks.bash", "offline")
} else {
cmd = exec.Command(bash, "./checks.bash")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}
func TestLintReports(t *testing.T) {
if runtime.GOOS == "android" {
t.Skipf("android builder does not have access to reports/")
}
allFiles := make(map[string]string)
var reports []string
for _, dir := range []string{report.YAMLDir, report.ExcludedDir} {
files, err := os.ReadDir(dir)
if err != nil && !errors.Is(err, os.ErrNotExist) {
t.Fatalf("unable to read %v/: %s", dir, err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if filepath.Ext(file.Name()) != ".yaml" {
continue
}
filename := filepath.Join(dir, file.Name())
if allFiles[file.Name()] != "" {
t.Errorf("report appears in multiple locations: %v, %v", allFiles[file.Name()], filename)
}
allFiles[file.Name()] = filename
reports = append(reports, filename)
}
}
// Skip network calls in short mode.
var lint func(r *report.Report) []string
if testing.Short() {
lint = func(r *report.Report) []string {
return r.LintOffline()
}
} else {
pc := proxy.NewDefaultClient()
lint = func(r *report.Report) []string {
return r.Lint(pc)
}
}
ctx := context.Background()
rc, err := report.NewLocalClient(ctx, ".")
if err != nil {
t.Fatal(err)
}
modulesToImports, err := priority.LoadModuleMap()
if err != nil {
t.Fatal(err)
}
// Map from summaries to report paths, used to check for duplicate summaries.
summaries := make(map[string]string)
sort.Strings(reports)
for _, filename := range reports {
t.Run(filename, func(t *testing.T) {
r, err := report.Read(filename)
if err != nil {
t.Fatal(err)
}
if err := r.CheckFilename(filename); err != nil {
t.Error(err)
}
lints := lint(r)
if len(lints) > 0 {
t.Error(strings.Join(lints, "\n"))
}
duplicates := make(map[string][]string)
for _, alias := range r.Aliases() {
for _, r2 := range rc.ReportsByAlias(alias) {
if r2.ID != r.ID {
duplicates[r2.ID] = append(duplicates[r2.ID], alias)
}
}
}
for r2, aliases := range duplicates {
t.Errorf("report %s shares duplicate alias(es) %s with report %s", filename, aliases, r2)
}
// Ensure that each reviewed report has a unique summary.
if r.IsReviewed() {
if summary := r.Summary.String(); summary != "" {
if report, ok := summaries[summary]; ok {
t.Errorf("report %s shares duplicate summary %q with report %s", filename, summary, report)
} else {
summaries[summary] = filename
}
}
}
// Ensure that no unreviewed reports are high priority.
// This can happen because the initial quick triage algorithm
// doesn't know about all affected modules - just the one
// listed in the Github issue.
if r.IsUnreviewed() && !r.IsExcluded() && !r.UnreviewedOK {
pr, _ := priority.AnalyzeReport(r, rc, modulesToImports)
if pr.Priority == priority.High {
t.Errorf("UNREVIEWED report %s is high priority (should be NEEDS_REVIEW or REVIEWED) - reason: %s", filename, pr.Reason)
}
}
// Check that a correct OSV file was generated for each YAML report.
if r.Excluded == "" {
generated, err := r.ToOSV(time.Time{})
if err != nil {
t.Fatal(err)
}
osvFilename := r.OSVFilename()
current, err := report.ReadOSV(osvFilename)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(generated, current, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("%s does not match report:\n%v", osvFilename, diff)
}
if err := osvutils.ValidateExceptTimestamps(¤t); err != nil {
t.Error(err)
}
}
if r.CVEMetadata != nil {
generated, err := cve5.FromReport(r)
if err != nil {
t.Fatal(err)
}
cvePath := r.CVEFilename()
current, err := cve5.Read(cvePath)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(generated, current, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("%s does not match report:\n%v", cvePath, diff)
}
}
})
}
}