-
Notifications
You must be signed in to change notification settings - Fork 16
/
cve_test.go
128 lines (115 loc) · 2.46 KB
/
cve_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
package nvd
import (
"bufio"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseCVEID(t *testing.T) {
tests := []struct {
wantYear int
wantSeq int
cveID string
}{
{2020, 14882, "CVE-2020-14882"},
{2014, 4294967296, "CVE-2014-4294967296"},
{2014, 1, "CVE-2014-0001"},
{2014, 99, "CVE-2014-0099"},
{2014, 999, "CVE-2014-0999"},
}
for _, tt := range tests {
gotYear, gotSeq := ParseCVEID(tt.cveID)
assert.Equal(t, tt.wantYear, gotYear)
assert.Equal(t, tt.wantSeq, gotSeq)
}
}
func TestPadCVESequence(t *testing.T) {
tests := []struct {
want string
seq int
}{
{"0001", 1},
{"0011", 11},
{"0111", 111},
{"1111", 1111},
{"11111", 11111},
{"111111", 111111},
}
for _, tt := range tests {
got := PadCVESequence(tt.seq)
assert.Equal(t, tt.want, got)
}
}
func TestFixCVEID(t *testing.T) {
tests := []struct {
want string
cveID string
}{
{"CVE-2020-14882", "CVE-2020-0014882"},
{"CVE-2020-14882", "CVE-2020-14882"},
}
for _, tt := range tests {
got := FixCVEID(tt.cveID)
assert.Equal(t, tt.want, got)
}
}
func TestIsCVEIDStrict(t *testing.T) {
validIDs, err := loadTestData("valid-syntax.out")
if err != nil {
t.Fatal(err)
}
for _, cveID := range validIDs {
ok := IsCVEIDStrict(cveID)
if !ok {
t.Errorf("got invalid; want valid: %s", cveID)
}
}
invalidIDs, err := loadTestData("invalid-syntax.out")
if err != nil {
t.Fatal(err)
}
for _, cveID := range invalidIDs {
ok := IsCVEIDStrict(cveID)
if ok {
t.Errorf("got valid; want invalid: %s", cveID)
}
}
}
// TestIsCVEID should fail on the invalid IDs, matching some of them since based on Loose.
// Review these manually to check if acceptable range of match
func TestIsCVEID(t *testing.T) {
validIDs, err := loadTestData("valid-syntax.out")
if err != nil {
t.Fatal(err)
}
for _, cveID := range validIDs {
ok := IsCVEID(cveID)
if !ok {
t.Errorf("got invalid; want valid: %s", cveID)
}
}
invalidIDs, err := loadTestData("invalid-syntax.out")
if err != nil {
t.Fatal(err)
}
for _, cveID := range invalidIDs {
ok := IsCVEID(cveID)
if ok {
t.Errorf("got valid; want invalid: %s", cveID)
}
}
}
func loadTestData(fileName string) ([]string, error) {
f, err := os.Open(path.Join("testdata", fileName))
if err != nil {
return nil, err
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}