aboutsummaryrefslogtreecommitdiff
path: root/internal/feed
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-01 14:42:44 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-01 14:42:44 -0500
commit3044aef20f4aecb7963e826b976c6d72a970cda1 (patch)
tree114295370dc27f17d5115f9cf4634e5e01fad20b /internal/feed
parent68714974f6d2887f66101aa020e25faea1631467 (diff)
downloadcvez-3044aef20f4aecb7963e826b976c6d72a970cda1.tar.bz2
cvez-3044aef20f4aecb7963e826b976c6d72a970cda1.zip
internal/feed: add v2version, v3version, tests
Diffstat (limited to 'internal/feed')
-rw-r--r--internal/feed/feed.go62
-rw-r--r--internal/feed/v2version.go36
-rw-r--r--internal/feed/v2version_string.go23
-rw-r--r--internal/feed/v2version_test.go75
-rw-r--r--internal/feed/v3version.go36
-rw-r--r--internal/feed/v3version_string.go23
-rw-r--r--internal/feed/v3version_test.go75
7 files changed, 270 insertions, 60 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index affa5d2..3a6e695 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -1,15 +1,5 @@
package feed
-import (
- "encoding/json"
- "fmt"
-)
-
-const (
- CvssVersion31 = iota // CVSS version 3.1
- CvssVersion20 // CVSS version 2.0
-)
-
// TODO: parse cpe, cve id
// CVE metadata
@@ -126,58 +116,10 @@ type ItemConfigurations struct {
Nodes []ConfigurationNode `json:"nodes"`
}
-// CVSS version.
-type CvssV3Version int
-
-// Unmarshal CVSS version from JSON.
-func (me *CvssV3Version) 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 "3.1":
- *me = CvssVersion31
- default:
- // return error
- return fmt.Errorf("unknown CVSS V3 version: %s", s)
- }
-
- // return success
- return nil
-}
-
-// CVSS V2 version.
-type CvssV2Version int
-
-// Unmarshal CVSS version from JSON.
-func (me *CvssV2Version) 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 "2.0":
- *me = CvssVersion20
- default:
- // return error
- return fmt.Errorf("unknown CVSS V2 version: %s", s)
- }
-
- // return success
- return nil
-}
-
// CVSS V3
type CvssV3 struct {
// CVSS V3 version
- Version CvssV3Version `json:"version"`
+ Version V3Version `json:"version"`
// CVSS V3 vector string (FIXME: add custom type)
VectorString string `json:"vectorString"`
@@ -220,7 +162,7 @@ type BaseMetricV3 struct {
// CVSS V2
type CvssV2 struct {
// CVSS V2 version
- Version CvssV2Version `json:"version"`
+ Version V2Version `json:"version"`
// CVSS V3 vector string (FIXME: add custom type)
VectorString string `json:"vectorString"`
diff --git a/internal/feed/v2version.go b/internal/feed/v2version.go
new file mode 100644
index 0000000..76e6134
--- /dev/null
+++ b/internal/feed/v2version.go
@@ -0,0 +1,36 @@
+package feed
+
+//go:generate stringer -linecomment -type=V2Version
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// CVSS v2 version
+type V2Version byte
+
+const (
+ V20 V2Version = iota // 2.0
+)
+
+// Unmarshal CVSS V2 version from JSON.
+func (me *V2Version) 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 "2.0":
+ *me = V20
+ default:
+ // return error
+ return fmt.Errorf("unknown CVSS version: %s", s)
+ }
+
+ // return success
+ return nil
+}
diff --git a/internal/feed/v2version_string.go b/internal/feed/v2version_string.go
new file mode 100644
index 0000000..6b13870
--- /dev/null
+++ b/internal/feed/v2version_string.go
@@ -0,0 +1,23 @@
+// Code generated by "stringer -linecomment -type=V2Version"; 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[V20-0]
+}
+
+const _V2Version_name = "2.0"
+
+var _V2Version_index = [...]uint8{0, 3}
+
+func (i V2Version) String() string {
+ if i >= V2Version(len(_V2Version_index)-1) {
+ return "V2Version(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _V2Version_name[_V2Version_index[i]:_V2Version_index[i+1]]
+}
diff --git a/internal/feed/v2version_test.go b/internal/feed/v2version_test.go
new file mode 100644
index 0000000..3b9b029
--- /dev/null
+++ b/internal/feed/v2version_test.go
@@ -0,0 +1,75 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestV2VersionUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val V2Version
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestV2VersionUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown CVSS version: foo"
+ var val V2Version
+
+ 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 TestV2VersionUnmarshalValid(t *testing.T) {
+ tests := []struct {
+ val string
+ exp V2Version
+ } {
+ { "\"2.0\"", V20 },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got V2Version
+ 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 TestV2VersionString(t *testing.T) {
+ tests := []struct {
+ val V2Version
+ exp string
+ } {
+ { V20, "2.0" },
+
+ { V2Version(255), "V2Version(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)
+ }
+ })
+ }
+}
diff --git a/internal/feed/v3version.go b/internal/feed/v3version.go
new file mode 100644
index 0000000..537fecc
--- /dev/null
+++ b/internal/feed/v3version.go
@@ -0,0 +1,36 @@
+package feed
+
+//go:generate stringer -linecomment -type=V3Version
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// CVSS v3 version
+type V3Version byte
+
+const (
+ V31 V3Version = iota // 3.1
+)
+
+// Unmarshal CVSS V3 version from JSON.
+func (me *V3Version) 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 "3.1":
+ *me = V31
+ default:
+ // return error
+ return fmt.Errorf("unknown CVSS version: %s", s)
+ }
+
+ // return success
+ return nil
+}
diff --git a/internal/feed/v3version_string.go b/internal/feed/v3version_string.go
new file mode 100644
index 0000000..9de58a7
--- /dev/null
+++ b/internal/feed/v3version_string.go
@@ -0,0 +1,23 @@
+// Code generated by "stringer -linecomment -type=V3Version"; 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[V31-0]
+}
+
+const _V3Version_name = "3.1"
+
+var _V3Version_index = [...]uint8{0, 3}
+
+func (i V3Version) String() string {
+ if i >= V3Version(len(_V3Version_index)-1) {
+ return "V3Version(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _V3Version_name[_V3Version_index[i]:_V3Version_index[i+1]]
+}
diff --git a/internal/feed/v3version_test.go b/internal/feed/v3version_test.go
new file mode 100644
index 0000000..89cc6ed
--- /dev/null
+++ b/internal/feed/v3version_test.go
@@ -0,0 +1,75 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestV3VersionUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val V3Version
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestV3VersionUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown CVSS version: foo"
+ var val V3Version
+
+ 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 TestV3VersionUnmarshalValid(t *testing.T) {
+ tests := []struct {
+ val string
+ exp V3Version
+ } {
+ { "\"3.1\"", V31 },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val, func(t *testing.T) {
+ var got V3Version
+ 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 TestV3VersionString(t *testing.T) {
+ tests := []struct {
+ val V3Version
+ exp string
+ } {
+ { V31, "3.1" },
+
+ { V3Version(255), "V3Version(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)
+ }
+ })
+ }
+}