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
|
package feed
import (
"encoding/json"
"github.com/pablotron/cvez/cvss"
"testing"
)
func TestVectorUnmarshalInvalidData(t *testing.T) {
test := []byte(`{}`)
var val Vector
if err := json.Unmarshal(test, &val); err == nil {
t.Errorf("got \"%s\", exp error", val)
}
}
func TestVectorUnmarshalJSON(t *testing.T) {
failTests := []struct {
val string
exp string
} {
{
val: "\"AV:N/junk/Au:S/C:P/I:P/A:P\"",
exp: "invalid CVSS vector: AV:N/junk/Au:S/C:P/I:P/A:P",
}, {
val: "\"CVSS:3.0/junk/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H\"",
exp: "invalid CVSS vector: CVSS:3.0/junk/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H",
}, {
val: "\"CVSS:3.1/AV:A/junk/PR:N/UI:N/S:U/C:H/I:H/A:H\"",
exp: "invalid CVSS vector: CVSS:3.1/AV:A/junk/PR:N/UI:N/S:U/C:H/I:H/A:H",
},
}
for _, test := range(failTests) {
t.Run(test.val, func(t *testing.T) {
var got Vector
if err := json.Unmarshal([]byte(test.val), &got); err == nil {
t.Errorf("got \"%v\", exp error", got)
} else if err.Error() != test.exp {
t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp)
}
})
}
passTests := []string {
"AV:N/AC:M/Au:S/C:P/I:P/A:P",
"CVSS:3.0/AV:A/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H",
"CVSS:3.1/AV:A/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H",
}
for _, val := range(passTests) {
t.Run(val, func(t *testing.T) {
var got Vector
if err := json.Unmarshal([]byte("\"" + val + "\""), &got); err != nil {
t.Error(err)
} else if got.Vector.String() != val {
t.Errorf("got \"%s\", exp \"%s\"", got.Vector.String(), val)
}
})
}
}
func TestVectorMarshalJSON(t *testing.T) {
tests := []string {
"AV:N/AC:M/Au:S/C:P/I:P/A:P",
"CVSS:3.0/AV:A/AC:H/PR:H/UI:R/S:U/C:H/I:H/A:H",
"CVSS:3.1/AV:A/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H",
}
for _, val := range(tests) {
t.Run(val, func(t *testing.T) {
// get expected string
exp := "\"" + val + "\""
// create inner vector
vec, err := cvss.NewVector(val)
if err != nil {
t.Error(err)
return
}
// serialize as json
buf, err := json.Marshal(Vector { vec })
if err != nil {
t.Error(err)
return
}
// check result
got := string(buf)
if got != exp {
t.Errorf("got \"%s\", exp \"%s\"", got, exp)
}
})
}
}
|