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
|
package cpe
import (
"encoding/json"
"testing"
)
func TestNewV23Binding(t *testing.T) {
passTests := []string {
"cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
}
for _, val := range(passTests) {
t.Run(val, func(t *testing.T) {
if _, err := NewV23Binding(val); err != nil {
t.Error(err)
}
})
}
failTests := []struct {
val string
exp string
} {{
val: "o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
exp: "missing CPE 2.3 prefix",
}, {
val: "cpe:2.3:\n",
exp: "invalid byte: 0x0a",
}, {
val: "cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*",
exp: "invalid attribute count: 10 != 11",
}, {
val: "cpe:2.3:foo:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
exp: "unknown part: \"foo\"",
}}
for _, test := range(failTests) {
t.Run(test.val, func(t *testing.T) {
got, err := NewV23Binding(test.val)
if 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)
}
})
}
}
func TestV23BindingString(t *testing.T) {
tests := []string {
"cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
}
for _, val := range(tests) {
t.Run(val, func(t *testing.T) {
if got, err := NewV23Binding(val); err != nil {
t.Error(err)
} else if got.String() != val {
t.Errorf("got \"%s\", exp \"%s\"", got.String(), val)
}
})
}
}
func TestV23BindingUnmarshalJSON(t *testing.T) {
passTests := []string {
`"cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*"`,
}
for _, val := range(passTests) {
t.Run(val, func(t *testing.T) {
var b V23Binding
if err := json.Unmarshal([]byte(val), &b); err != nil {
t.Error(err)
}
})
}
failTests := []struct {
val string
exp string
} {{
val: `"o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*`,
exp: "unexpected end of JSON input",
}, {
val: `"o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*"`,
exp: "missing CPE 2.3 prefix",
}, {
val: `x`,
exp: "invalid character 'x' looking for beginning of value",
}}
for _, test := range(failTests) {
t.Run(test.val, func(t *testing.T) {
var got V23Binding
// NOTE: we use got.UnmarshalJSON() instead of json.Unmarshal(),
// because if we use the latter with invalid json it fails before
// testing the underlying method.
if err := got.UnmarshalJSON([]byte(test.val)); 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)
}
})
}
}
func TestV23BindingMarshalJSON(t *testing.T) {
tests := []string {
"cpe:2.3:o:intel:ethernet_controller_e810_firmware:*:*:*:*:*:*:*:*",
}
for _, val := range(tests) {
t.Run(val, func(t *testing.T) {
// create binding, check for error
b, err := NewV23Binding(val)
if err != nil {
t.Error(err)
return
}
// marshal json, check for error
got, err := json.Marshal(b)
if err != nil {
t.Error(err)
return
}
// build/check expected value
exp := "\"" + val + "\""
if string(got) != exp {
t.Errorf("got \"%s\", exp \"%s\"", string(got), exp)
return
}
})
}
}
|