aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/feed/feed.go30
-rw-r--r--internal/feed/v3attackcomplexity.go42
-rw-r--r--internal/feed/v3attackcomplexity_string.go25
-rw-r--r--internal/feed/v3attackcomplexity_test.go79
4 files changed, 147 insertions, 29 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index a04c3ce..38a7767 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -29,34 +29,6 @@ const (
// TODO: parse cpe
-// CVSS attack complexity
-type AttackComplexity int
-
-// Unmarshal CVSS attack complexity from JSON.
-func (me *AttackComplexity) 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 "LOW":
- *me = Low
- case "MEDIUM":
- *me = Medium
- case "HIGH":
- *me = High
- default:
- // return error
- return fmt.Errorf("unknown attack complexity: %s", s)
- }
-
- // return success
- return nil
-}
-
// CVSS privileges required
type PrivilegesRequired int
@@ -484,7 +456,7 @@ type CvssV3 struct {
AttackVector V3AttackVector `json:"attackVector"`
// attack complexity
- AttackComplexity AttackComplexity `json:"attackComplexity"`
+ AttackComplexity V3AttackComplexity `json:"attackComplexity"`
// privileges required
PrivilegesRequired PrivilegesRequired `json:"privilegesRequired"`
diff --git a/internal/feed/v3attackcomplexity.go b/internal/feed/v3attackcomplexity.go
new file mode 100644
index 0000000..6e7481c
--- /dev/null
+++ b/internal/feed/v3attackcomplexity.go
@@ -0,0 +1,42 @@
+package feed
+
+//go:generate stringer -linecomment -type=V3AttackComplexity
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// CVSS v3 attack complexity
+type V3AttackComplexity byte
+
+const (
+ V3ACLow V3AttackComplexity = iota // LOW
+ V3ACMedium // MEDIUM
+ V3ACHigh // HIGH
+)
+
+// Unmarshal CVSS v3 attack complexity from JSON.
+func (me *V3AttackComplexity) 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 "LOW":
+ *me = V3ACLow
+ case "MEDIUM":
+ *me = V3ACMedium
+ case "HIGH":
+ *me = V3ACHigh
+ default:
+ // return error
+ return fmt.Errorf("unknown CVSS v3 attack complexity: %s", s)
+ }
+
+ // return success
+ return nil
+}
diff --git a/internal/feed/v3attackcomplexity_string.go b/internal/feed/v3attackcomplexity_string.go
new file mode 100644
index 0000000..12110c8
--- /dev/null
+++ b/internal/feed/v3attackcomplexity_string.go
@@ -0,0 +1,25 @@
+// Code generated by "stringer -linecomment -type=V3AttackComplexity"; 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[V3ACLow-0]
+ _ = x[V3ACMedium-1]
+ _ = x[V3ACHigh-2]
+}
+
+const _V3AttackComplexity_name = "LOWMEDIUMHIGH"
+
+var _V3AttackComplexity_index = [...]uint8{0, 3, 9, 13}
+
+func (i V3AttackComplexity) String() string {
+ if i >= V3AttackComplexity(len(_V3AttackComplexity_index)-1) {
+ return "V3AttackComplexity(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _V3AttackComplexity_name[_V3AttackComplexity_index[i]:_V3AttackComplexity_index[i+1]]
+}
diff --git a/internal/feed/v3attackcomplexity_test.go b/internal/feed/v3attackcomplexity_test.go
new file mode 100644
index 0000000..a76efe3
--- /dev/null
+++ b/internal/feed/v3attackcomplexity_test.go
@@ -0,0 +1,79 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestV3AttackComplexityUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val V3AttackComplexity
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestV3AttackComplexityUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown CVSS v3 attack complexity: foo"
+ var val V3AttackComplexity
+
+ 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 TestV3AttackComplexityUnmarshalValid(t *testing.T) {
+ tests := []struct {
+ val string
+ exp V3AttackComplexity
+ } {
+ { "\"LOW\"", V3ACLow },
+ { "\"MEDIUM\"", V3ACMedium },
+ { "\"HIGH\"", V3ACHigh },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got V3AttackComplexity
+ 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 TestV3AttackComplexityString(t *testing.T) {
+ tests := []struct {
+ val V3AttackComplexity
+ exp string
+ } {
+ { V3ACLow, "LOW" },
+ { V3ACMedium, "MEDIUM" },
+ { V3ACHigh, "HIGH" },
+
+ { V3AttackComplexity(255), "V3AttackComplexity(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)
+ }
+ })
+ }
+}