-
Notifications
You must be signed in to change notification settings - Fork 16
/
feed_test.go
227 lines (201 loc) · 4.16 KB
/
feed_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package nvd
import (
"fmt"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_FetchCVE(t *testing.T) {
tests := []struct {
cveID string
wantReserved bool
wantErr bool
wantErrNotFound bool
}{
// success
{"CVE-2019-5736", false, false, false},
// success Reserved CVE
{"CVE-2015-2231", true, false, false},
// fail invalid CVE
{"CVE-201900-5736", false, true, false},
// fail old year
{"CVE-2000-5736", false, true, false},
// fail future year
{"CVE-2025-5736", false, true, false},
// fail with ErrNotFound
{"CVE-2002-99999", false, true, true},
}
for _, tt := range tests {
t.Logf("run subtest %s", tt.cveID)
cl, err := NewClient("tmp")
if err != nil {
t.Fatal(err)
}
got, err := cl.FetchCVE(tt.cveID)
if tt.wantErr {
assert.Error(t, err)
t.Logf("want error; got error: %s", err)
if tt.wantErrNotFound {
assert.ObjectsAreEqual(ErrNotFound, err)
}
teardown()
continue
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.cveID, got.CVE.CVEDataMeta.ID)
if tt.wantReserved {
assert.True(t, got.Reserved)
} else {
assert.False(t, got.Reserved)
}
teardown()
}
}
func TestClient_FetchCVE_ReferenceData(t *testing.T) {
tests := []struct {
cveID string
}{
{"CVE-2017-7529"},
}
for _, tt := range tests {
t.Logf("run subtest %s", tt.cveID)
cl, err := NewClient("tmp")
if err != nil {
t.Fatal(err)
}
got, err := cl.FetchCVE(tt.cveID)
assert.NoError(t, err)
assert.NotNil(t, got.CVE.References.ReferenceData)
fmt.Println(got)
teardown()
}
}
// go test -run=Bench -bench=. -benchtime=20s -benchmem
func BenchmarkClient_SearchFeed(b *testing.B) {
cl, err := NewClient("tmp")
if err != nil {
b.Fatal(err)
}
err = cl.downloadFeed(
fmt.Sprintf(nvdDataFeeds, "2020"),
cl.pathToFeed("2020"),
)
if err != nil {
b.Fatal(err)
}
for n := 0; n < b.N; n++ {
_, err := cl.searchFeed("2020", "CVE-2020-14882")
if err != nil {
b.Error(err)
}
}
teardown()
}
func TestClient_SearchFeed(t *testing.T) {
cl, err := NewClient("tmp")
if err != nil {
t.Fatal(err)
}
err = cl.downloadFeed(
fmt.Sprintf(nvdDataFeeds, "2020"),
cl.pathToFeed("2020"),
)
if err != nil {
t.Fatal(err)
}
cveID := "CVE-2020-14882"
got, err := cl.searchFeed("2020", cveID)
if err != nil {
assert.NoError(t, err)
}
assert.Equal(t, cveID, got.CVE.CVEDataMeta.ID)
teardown()
}
func TestClient_NeedNVDUpdate(t *testing.T) {
tests := []struct {
want bool
year string
alreadyUpdated bool
meta string
}{
// needNVDUpdate true; stale meta sha256 hash
{
true,
"2002",
false,
`lastModifiedDate:2020-01-11T05:13:47-05:00
size:19924894
zipSize:1408391
gzSize:1408255
sha256:qwertyuiop@[
`,
},
// needNVDUpdate false; meta file already up-to-date
{
false,
"2003",
true,
"",
},
}
for i, tt := range tests {
t.Logf("run subtest %d", i+1)
var err error
cl, err := NewClient("tmp")
if err != nil {
t.Fatal(err)
}
if !tt.alreadyUpdated {
err = cl.saveNVDMeta(tt.year, []byte(tt.meta))
if err != nil {
t.Fatal(err)
}
} else {
_, err = cl.fetchRemoteMeta(tt.year)
if err != nil {
t.Fatal(err)
}
}
got, err := cl.needNVDUpdate(tt.year)
assert.NoError(t, err)
if got != tt.want {
t.Errorf("got %v != want %v; want %v", got, tt.want, tt.want)
}
teardown()
}
}
func TestFetchReserved(t *testing.T) {
tests := []struct {
wantDate string
wantError bool
cve string
}{
// Full CVE
{"2020-10-30T00:00Z", false, "CVE-2020-28002"},
// Reserved CVE with only date & generic description
{"2015-03-06T00:00Z", false, "CVE-2015-2231"},
// Invalid CVE non-existent
{"", true, "CVE-2020-28002999"},
}
for i, tt := range tests {
t.Logf("run subtest %d", i+1)
got, err := fetchReservedCVE(tt.cve)
if tt.wantError {
assert.Error(t, err)
continue
}
assert.NoError(t, err)
assert.Equal(t, tt.wantDate, got.PublishedDate)
}
}
func teardown() {
pwd := os.Getenv("PWD")
d := path.Join(pwd, "tmp", "feeds")
if _, err := os.Stat(d); !os.IsNotExist(err) {
if err := os.RemoveAll(d); err != nil {
fmt.Println(err)
}
}
}