aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/feed/feed.go32
-rw-r--r--internal/feed/v3privilegesrequired.go45
-rw-r--r--internal/feed/v3privilegesrequired_string.go26
-rw-r--r--internal/feed/v3privilegesrequired_test.go81
4 files changed, 153 insertions, 31 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index 38a7767..4b18b9d 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -29,36 +29,6 @@ const (
// TODO: parse cpe
-// CVSS privileges required
-type PrivilegesRequired int
-
-// Unmarshal CVSS privileges required from JSON.
-func (me *PrivilegesRequired) 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 "LOW":
- *me = Low
- case "MEDIUM":
- *me = Medium
- case "HIGH":
- *me = High
- default:
- // return error
- return fmt.Errorf("unknown privileges required: %s", s)
- }
-
- // return success
- return nil
-}
-
// CVSS user interaction
type UserInteraction int
@@ -459,7 +429,7 @@ type CvssV3 struct {
AttackComplexity V3AttackComplexity `json:"attackComplexity"`
// privileges required
- PrivilegesRequired PrivilegesRequired `json:"privilegesRequired"`
+ PrivilegesRequired V3PrivilegesRequired `json:"privilegesRequired"`
// user interaction
UserInteraction UserInteraction `json:"userInteraction"`
diff --git a/internal/feed/v3privilegesrequired.go b/internal/feed/v3privilegesrequired.go
new file mode 100644
index 0000000..3e69334
--- /dev/null
+++ b/internal/feed/v3privilegesrequired.go
@@ -0,0 +1,45 @@
+package feed
+
+//go:generate stringer -linecomment -type=V3PrivilegesRequired
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// CVSS v3 privileges required.
+type V3PrivilegesRequired byte
+
+const (
+ V3PRNone V3PrivilegesRequired = iota // NONE
+ V3PRLow // LOW
+ V3PRMedium // MEDIUM
+ V3PRHigh // HIGH
+)
+
+// Unmarshal CVSS privileges required from JSON.
+func (me *V3PrivilegesRequired) 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 = V3PRNone
+ case "LOW":
+ *me = V3PRLow
+ case "MEDIUM":
+ *me = V3PRMedium
+ case "HIGH":
+ *me = V3PRHigh
+ default:
+ // return error
+ return fmt.Errorf("unknown CVSS v3 privileges required: %s", s)
+ }
+
+ // return success
+ return nil
+}
diff --git a/internal/feed/v3privilegesrequired_string.go b/internal/feed/v3privilegesrequired_string.go
new file mode 100644
index 0000000..2951a64
--- /dev/null
+++ b/internal/feed/v3privilegesrequired_string.go
@@ -0,0 +1,26 @@
+// Code generated by "stringer -linecomment -type=V3PrivilegesRequired"; 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[V3PRNone-0]
+ _ = x[V3PRLow-1]
+ _ = x[V3PRMedium-2]
+ _ = x[V3PRHigh-3]
+}
+
+const _V3PrivilegesRequired_name = "NONELOWMEDIUMHIGH"
+
+var _V3PrivilegesRequired_index = [...]uint8{0, 4, 7, 13, 17}
+
+func (i V3PrivilegesRequired) String() string {
+ if i >= V3PrivilegesRequired(len(_V3PrivilegesRequired_index)-1) {
+ return "V3PrivilegesRequired(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _V3PrivilegesRequired_name[_V3PrivilegesRequired_index[i]:_V3PrivilegesRequired_index[i+1]]
+}
diff --git a/internal/feed/v3privilegesrequired_test.go b/internal/feed/v3privilegesrequired_test.go
new file mode 100644
index 0000000..f200ed1
--- /dev/null
+++ b/internal/feed/v3privilegesrequired_test.go
@@ -0,0 +1,81 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestV3PrivilegesRequiredUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val V3PrivilegesRequired
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestV3PrivilegesRequiredUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown CVSS v3 privileges required: foo"
+ var val V3PrivilegesRequired
+
+ 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 TestV3PrivilegesRequiredUnmarshalValid(t *testing.T) {
+ tests := []struct {
+ val string
+ exp V3PrivilegesRequired
+ } {
+ { "\"NONE\"", V3PRNone },
+ { "\"LOW\"", V3PRLow },
+ { "\"MEDIUM\"", V3PRMedium },
+ { "\"HIGH\"", V3PRHigh },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got V3PrivilegesRequired
+ 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 TestV3PrivilegesRequiredString(t *testing.T) {
+ tests := []struct {
+ val V3PrivilegesRequired
+ exp string
+ } {
+ { V3PRNone, "NONE" },
+ { V3PRLow, "LOW" },
+ { V3PRMedium, "MEDIUM" },
+ { V3PRHigh, "HIGH" },
+
+ { V3PrivilegesRequired(255), "V3PrivilegesRequired(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)
+ }
+ })
+ }
+}