aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/feed/feed.go28
-rw-r--r--internal/feed/v3userinteraction.go40
-rw-r--r--internal/feed/v3userinteraction_string.go24
-rw-r--r--internal/feed/v3userinteraction_test.go77
4 files changed, 142 insertions, 27 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index 4b18b9d..98e62ef 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -29,32 +29,6 @@ const (
// TODO: parse cpe
-// CVSS user interaction
-type UserInteraction int
-
-// Unmarshal CVSS user interaction from JSON.
-func (me *UserInteraction) UnmarshalJSON(b []byte) error {
- // decode string, check for error
- var s string
- if err := json.Unmarshal(b, &s); err != nil {
- return err
- }
-
- // check value
- switch s {
- case "NONE":
- *me = None
- case "REQUIRED":
- *me = Required
- default:
- // return error
- return fmt.Errorf("unknown user interaction: %s", s)
- }
-
- // return success
- return nil
-}
-
// CVSS scope
type Scope int
@@ -432,7 +406,7 @@ type CvssV3 struct {
PrivilegesRequired V3PrivilegesRequired `json:"privilegesRequired"`
// user interaction
- UserInteraction UserInteraction `json:"userInteraction"`
+ UserInteraction V3UserInteraction `json:"userInteraction"`
// scope
Scope Scope `json:"scope"`
diff --git a/internal/feed/v3userinteraction.go b/internal/feed/v3userinteraction.go
new file mode 100644
index 0000000..a6a53ca
--- /dev/null
+++ b/internal/feed/v3userinteraction.go
@@ -0,0 +1,40 @@
+package feed
+
+//go:generate stringer -linecomment -type=V3UserInteraction
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// CVSS v3 user interaction
+type V3UserInteraction byte
+
+const (
+ V3UINone V3UserInteraction = iota // NONE
+ V3UIRequired // REQUIRED
+)
+
+// Unmarshal CVSS user interaction from JSON.
+func (me *V3UserInteraction) UnmarshalJSON(b []byte) error {
+ // decode string, check for error
+ var s string
+ if err := json.Unmarshal(b, &s); err != nil {
+ return err
+ }
+
+ // check value
+ switch s {
+ case "NONE":
+ *me = V3UINone
+ case "REQUIRED":
+ *me = V3UIRequired
+ default:
+ // return error
+ return fmt.Errorf("unknown CVSS v3 user interaction: %s", s)
+ }
+
+ // return success
+ return nil
+}
+
diff --git a/internal/feed/v3userinteraction_string.go b/internal/feed/v3userinteraction_string.go
new file mode 100644
index 0000000..be78920
--- /dev/null
+++ b/internal/feed/v3userinteraction_string.go
@@ -0,0 +1,24 @@
+// Code generated by "stringer -linecomment -type=V3UserInteraction"; DO NOT EDIT.
+
+package feed
+
+import "strconv"
+
+func _() {
+ // An "invalid array index" compiler error signifies that the constant values have changed.
+ // Re-run the stringer command to generate them again.
+ var x [1]struct{}
+ _ = x[V3UINone-0]
+ _ = x[V3UIRequired-1]
+}
+
+const _V3UserInteraction_name = "NONEREQUIRED"
+
+var _V3UserInteraction_index = [...]uint8{0, 4, 12}
+
+func (i V3UserInteraction) String() string {
+ if i >= V3UserInteraction(len(_V3UserInteraction_index)-1) {
+ return "V3UserInteraction(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _V3UserInteraction_name[_V3UserInteraction_index[i]:_V3UserInteraction_index[i+1]]
+}
diff --git a/internal/feed/v3userinteraction_test.go b/internal/feed/v3userinteraction_test.go
new file mode 100644
index 0000000..c5949c2
--- /dev/null
+++ b/internal/feed/v3userinteraction_test.go
@@ -0,0 +1,77 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestV3UserInteractionUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val V3UserInteraction
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestV3UserInteractionUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown CVSS v3 user interaction: foo"
+ var val V3UserInteraction
+
+ err := json.Unmarshal(test, &val)
+ if err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ return
+ }
+
+ if err.Error() != exp {
+ t.Errorf("got \"%s\", exp \"%s\"", err.Error(), exp)
+ }
+}
+
+func TestV3UserInteractionUnmarshalValid(t *testing.T) {
+ tests := []struct {
+ val string
+ exp V3UserInteraction
+ } {
+ { "\"NONE\"", V3UINone },
+ { "\"REQUIRED\"", V3UIRequired },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got V3UserInteraction
+ if err := json.Unmarshal([]byte(test.val), &got); err != nil {
+ t.Error(err)
+ return
+ }
+
+ if got != test.exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
+ }
+ })
+ }
+}
+
+func TestV3UserInteractionString(t *testing.T) {
+ tests := []struct {
+ val V3UserInteraction
+ exp string
+ } {
+ { V3UINone, "NONE" },
+ { V3UIRequired, "REQUIRED" },
+
+ { V3UserInteraction(255), "V3UserInteraction(255)" },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.exp, func(t *testing.T) {
+ got := test.val.String()
+
+ if got != test.exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
+ }
+ })
+ }
+}