aboutsummaryrefslogtreecommitdiff
path: root/cvss/scores_test.go
blob: f35904c3c52ab3a32035f49e9ade3b36bf5017d6 (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package cvss

import (
  "reflect"
  "testing"
)

func TestNewScores(t *testing.T) {
  // test out of bound errors
  failTests := []struct {
    name    string // test name
    vals    []float64 // invalid base, temporal, and env scores
  } {{
    name: "invalid base",
    vals: []float64 { 11.0, 0.0, 0.0 },
  }, {
    name: "invalid temporal",
    vals: []float64 { 0.0, 11.0, 0.0 },
  }, {
    name: "invalid env",
    vals: []float64 { 0.0, 0.0, 11.0 },
  }}

  for _, test := range(failTests) {
    t.Run(test.name, func(t *testing.T) {
      got, err := NewScores(test.vals[0], test.vals[1], test.vals[2])
      if err == nil {
        t.Errorf("got %v, exp error", got)
      }
    })
  }
}

func TestNewScoresFromV2Vector(t *testing.T) {
  // test vectors from section 3.3
  passTests := []struct {
    name    string // test name
    val     string // test cvss v2 vector
    exps    []float64 // expected base, temporal, and env scores
  } {{
    name: "CVE-2002-0392/base", // 3.3.1
    val:  "AV:N/AC:L/Au:N/C:N/I:N/A:C",
    exps: []float64 { 7.8, 0.0, 0.0 },
  }, {
    name: "CVE-2002-0392/temporal", // 3.3.1
    val:  "AV:N/AC:L/Au:N/C:N/I:N/A:C/E:F/RL:OF/RC:C",
    exps: []float64 { 7.8, 6.4, 0.0 },
  }, {
    name: "CVE-2002-0392/all", // 3.3.1
    val:  "AV:N/AC:L/Au:N/C:N/I:N/A:C/E:F/RL:OF/RC:C/CDP:H/TD:H/CR:M/IR:M/AR:H",
    exps: []float64 { 7.8, 6.4, 9.2 },
  }, {
    name: "CVE-2003-0818/base", // 3.3.2
    val:  "AV:N/AC:L/Au:N/C:C/I:C/A:C",
    exps: []float64 { 10.0, 0.0, 0.0 },
  }, {
    name: "CVE-2003-0818/temporal", // 3.3.2
    val:  "AV:N/AC:L/Au:N/C:C/I:C/A:C/E:F/RL:OF/RC:C",
    exps: []float64 { 10.0, 8.3, 0.0 },
  }, {
    name: "CVE-2003-0818/all", // 3.3.2
    val:  "AV:N/AC:L/Au:N/C:C/I:C/A:C/E:F/RL:OF/RC:C/CDP:H/TD:H/CR:M/IR:M/AR:L",
    exps: []float64 { 10.0, 8.3, 9.0 },
  }, {
    name: "CVE-2003-0062/base", // 3.3.3
    val:  "AV:L/AC:H/Au:N/C:C/I:C/A:C",
    exps: []float64 { 6.2, 0.0, 0.0 },
  }, {
    name: "CVE-2003-0062/temporal", // 3.3.3
    val:  "AV:L/AC:H/Au:N/C:C/I:C/A:C/E:POC/RL:OF/RC:C",
    exps: []float64 { 6.2, 4.9, 0.0 },
  }, {
    name: "CVE-2003-0062/all", // 3.3.3
    val: "AV:L/AC:H/Au:N/C:C/I:C/A:C/E:POC/RL:OF/RC:C/CDP:H/TD:H/CR:M/IR:M/AR:M",
    exps: []float64 { 6.2, 4.9, 7.5 },
  }, {
    name: "A:N", // from nvd v2 calc
    val: "AV:A/AC:M/Au:M/C:P/I:P/A:N",
    exps: []float64 { 3.4, 0.0, 0.0 },
  }, {
    name: "Au:S", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P",
    exps: []float64 { 4.9, 0.0, 0.0 },
  }, {
    name: "E:ND", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:ND",
    exps: []float64 { 4.9, 0.0, 0.0 },
  }, {
    name: "E:U", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:U/RL:ND/RC:ND",
    exps: []float64 { 4.9, 4.2, 0.0 },
  }, {
    name: "E:H", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:ND/RC:ND",
    exps: []float64 { 4.9, 4.9, 0.0 },
  }, {
    name: "RL:TF", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:TF/RC:ND",
    exps: []float64 { 4.9, 4.4, 0.0 },
  }, {
    name: "RL:W", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND",
    exps: []float64 { 4.9, 4.7, 0.0 },
  }, {
    name: "RL:U", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:U/RC:ND",
    exps: []float64 { 4.9, 4.9, 0.0 },
  }, {
    name: "RC:UC", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:U/RC:UC",
    exps: []float64 { 4.9, 4.4, 0.0 },
  }, {
    name: "RC:UR", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:U/RC:UR",
    exps: []float64 { 4.9, 4.7, 0.0 },
  }, {
    name: "CDP:ND", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:ND/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 2.8 },
  }, {
    name: "CDP:N", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:N/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 2.8 },
  }, {
    name: "CDP:L", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:L/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 3.5 },
  }, {
    name: "CDP:LM", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:LM/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 5.0 },
  }, {
    name: "CDP:MH", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:MH/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 5.7 },
  }, {
    name: "CDP:H", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.4 },
  }, {
    name: "TD:ND", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:ND/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.4 },
  }, {
    name: "TD:N", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:N/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 0.0 },
  }, {
    name: "TD:L", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:L/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 1.6 },
  }, {
    name: "TD:M", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:M/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 4.8 },
  }, {
    name: "TD:H", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.4 },
  }, {
    name: "CR:ND", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:ND/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.8 },
  }, {
    name: "CR:L", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.4 },
  }, {
    name: "CR:M", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:M/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.8 },
  }, {
    name: "CR:H", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:H/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 7.1 },
  }, {
    name: "IR:ND", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:ND/AR:L",
    exps: []float64 { 4.9, 4.7, 6.8 },
  }, {
    name: "IR:L", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.4 },
  }, {
    name: "IR:M", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:M/AR:L",
    exps: []float64 { 4.9, 4.7, 6.8 },
  }, {
    name: "IR:H", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:H/AR:L",
    exps: []float64 { 4.9, 4.7, 7.1 },
  }, {
    name: "AR:ND", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:ND",
    exps: []float64 { 4.9, 4.7, 6.8 },
  }, {
    name: "AR:L", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:L",
    exps: []float64 { 4.9, 4.7, 6.4 },
  }, {
    name: "AR:M", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:M",
    exps: []float64 { 4.9, 4.7, 6.8 },
  }, {
    name: "AR:H", // from nvd v2 calc
    val: "AV:A/AC:M/Au:S/C:P/I:P/A:P/E:H/RL:W/RC:ND/CDP:H/TD:H/CR:L/IR:L/AR:H",
    exps: []float64 { 4.9, 4.7, 7.1 },
  }}
  // TODO: add additional test vectors using v2 calc

  for _, test := range(passTests) {
    t.Run(test.name, func(t *testing.T) {
      // build expected result
      exp, err := NewScores(test.exps[0], test.exps[1], test.exps[2])
      if err != nil {
        t.Error(err)
        return
      }

      // create vector, check for error
      vec, err := newV2Vector(test.val)
      if err != nil {
        t.Error(err)
        return
      }

      // get scores
      got, err := vec.Scores()
      if err != nil {
        t.Error(err)
        return
      }

      // compare to expected scores
      if !reflect.DeepEqual(got, exp) {
        t.Errorf("got %v, exp %v", got, exp)
        return
      }
    })
  }
}